The stdlib ai module provides 50+ SIMD-accelerated functions for
neural network development, tensor operations, and machine learning workflows.
import stdlib ai as ai
Tensor Creation
Function
Description
Example
Zeros(n)
Create array of zeros
ai.Zeros(100)
Ones(n)
Create array of ones
ai.Ones(100)
RandN(n)
Normal distribution (μ=0, σ=1)
ai.RandN(100)
RandU(n, min, max)
Uniform distribution
ai.RandU(100, -1, 1)
Example
import stdlib ai as ai
// Create tensors
zeros = ai.Zeros(10)
ones = ai.Ones(10)
normal = ai.RandN(10)
uniform = ai.RandU(10, -1, 1)
print("Zeros:", zeros)
print("Normal sample:", normal)
Element-wise Operations
Function
Description
Add(a, b)
Element-wise addition (SIMD)
Sub(a, b)
Element-wise subtraction
Mul(a, b)
Element-wise multiplication (SIMD)
Div(a, b)
Element-wise division
Scale(a, scalar)
Multiply by scalar
a = [1.0, 2.0, 3.0, 4.0]
b = [0.5, 0.5, 0.5, 0.5]
sum = ai.Add(a, b) // [1.5, 2.5, 3.5, 4.5]
prod = ai.Mul(a, b) // [0.5, 1.0, 1.5, 2.0]
scaled = ai.Scale(a, 2) // [2.0, 4.0, 6.0, 8.0]
Reduction Operations
Function
Description
Sum(a)
Sum of elements (SIMD)
Mean(a)
Average value
Max(a)
Maximum value
Min(a)
Minimum value
Argmax(a)
Index of maximum
Argmin(a)
Index of minimum
Matrix Operations
Function
Description
Dot(a, b)
Dot product (SIMD, 4B+ ops/sec)
MatMul(A, B, m, k, n)
Matrix multiply (cache-blocked SIMD)
Transpose(M, rows, cols)
Matrix transpose
Matrix Multiplication Example
import stdlib ai as ai
// 2x3 matrix A
A = [1, 2, 3,
4, 5, 6]
// 3x2 matrix B
B = [7, 8,
9, 10,
11, 12]
// C = A * B (2x2 result)
C = ai.MatMul(A, B, 2, 3, 2)
print("Result:", C) // [58, 64, 139, 154]
weight = [1.0, 2.0, 3.0]
grad = [0.1, 0.2, 0.3]
lr = 0.01
newWeight = ai.SGDUpdate(weight, grad, lr)
print("Updated weight:", newWeight)
Adam Update
weight = [1.0, 2.0, 3.0]
grad = [0.1, 0.2, 0.3]
m = ai.Zeros(3) // First moment
v = ai.Zeros(3) // Second moment
result = ai.AdamUpdate(weight, grad, m, v, 0.001, 0.9, 0.999, 1)
newWeight = result[0]
newM = result[1]
newV = result[2]
import stdlib ai as ai
// Create a neural network: 4 inputs -> 8 hidden -> 3 outputs
model = ai.Sequential([
ai.Layer("linear", 4, 8),
ai.Layer("relu"),
ai.Layer("linear", 8, 3),
ai.Layer("softmax")
])
print("Model created with ID:", model)
Model Training
Function
Description
train(model, xTrain, yTrain, config)
Train model and return final loss
predict(model, input)
Run inference on input
Training Config
The config array: [epochs, learningRate, optimizer, loss]
optimizer: "sgd" or "adam"
loss: "mse" or "crossentropy"
// Training data
xTrain = [
[0.0, 0.0, 0.0, 0.0],
[1.0, 1.0, 1.0, 1.0]
]
yTrain = [0, 2] // Class labels
// Train for 50 epochs with Adam, lr=0.01
config = [50, 0.01, "adam", "crossentropy"]
loss = ai.train(model, xTrain, yTrain, config)
print("Final loss:", loss)
// Run prediction
output = ai.predict(model, [0.5, 0.5, 0.5, 0.5])
predicted = ai.Argmax(output)
print("Predicted class:", predicted)
Model Persistence
Save and load trained models to .nmod files:
Function
Description
saveModel(model, path)
Save model to file, returns true on success
loadModel(path)
Load model from file, returns model ID or -1
Path Resolution
Paths are resolved relative to the source file, not the working directory.
This matches the behavior of import statements.
// Save trained model (saved next to this script)
saved = ai.saveModel(model, "mymodel.nmod")
if (saved) {
print("Model saved successfully!")
}
// Load model later
loaded = ai.loadModel("mymodel.nmod")
if (loaded >= 0) {
output = ai.predict(loaded, [1.0, 0.0, 1.0, 0.0])
print("Prediction:", ai.Argmax(output))
}
Complete Training Workflow
import stdlib ai as ai
// 1. Define model architecture
model = ai.Sequential([
ai.Layer("linear", 4, 8),
ai.Layer("relu"),
ai.Layer("linear", 8, 3),
ai.Layer("softmax")
])
// 2. Prepare training data
xTrain = [
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 1.0],
[1.0, 1.0, 0.0, 0.0],
[1.0, 1.0, 1.0, 1.0]
]
yTrain = [0, 1, 1, 2]
// 3. Train the model
config = [50, 0.01, "adam", "crossentropy"]
loss = ai.train(model, xTrain, yTrain, config)
// 4. Test prediction
output = ai.predict(model, [1.0, 1.0, 1.0, 1.0])
print("Predicted:", ai.Argmax(output))
// 5. Save for deployment
ai.saveModel(model, "classifier.nmod")
Model CLI Commands
Nevaarize provides CLI commands for model training and inference:
Train a Model
nevaarize model train script.nva to model.nmod
This runs your training script and saves the model. The output path
is resolved relative to the script location.
Run Inference
# View model info
nevaarize model run model.nmod
# Run inference with input
nevaarize model run model.nmod --input "[1.0, 0.0, 1.0, 0.0]"
Example output:
Loading model: model.nmod
Model info:
Input size: 4
Output size: 3
Epochs trained: 50
Final loss: 0.219
Running inference with input size: 4
Output: [0.05, 0.65, 0.30]
Prediction: 1
Performance
All SIMD-accelerated functions use AVX2 instructions: