Tensors

Tensors are the fundamental data structure in AodhML. They are first-class types with compile-time shape tracking and runtime optimization.

Prototype Status: The Tensor type system is implemented. Basic CPU operations are supported. GPU backends are planned for v0.5.0.

Creating Tensors

import "std:tensor"

fn main() {
    // From literal
    let a = Tensor::from([[1.0, 2.0], [3.0, 4.0]])

    // Zeros
    let zeros = Tensor::zeros([3, 3])           // 3x3 of f32 zeros
    let zeros_f64 = Tensor::zeros<f64>([3, 3])   // Explicit dtype

    // Ones
    let ones = Tensor::ones([2, 4])

    // Random (normal distribution)
    let rand = Tensor::randn([64, 128])

    // From array
    let data: [f32] = [1.0, 2.0, 3.0, 4.0]
    let t = Tensor::from(data).reshape([2, 2])
}

Tensor Types

SyntaxMeaning
Tensor<f32>32-bit float tensor
Tensor<f64>64-bit float tensor
Tensor<i32>32-bit integer tensor
Tensor<bool>Boolean tensor

Basic Operations

let a = Tensor::from([[1.0, 2.0], [3.0, 4.0]])
let b = Tensor::from([[5.0, 6.0], [7.0, 8.0]])

// Element-wise
let c = a + b      // [[6, 8], [10, 12]]
let d = a * b      // [[5, 12], [21, 32]]
let e = a - 1.0    // Broadcasting: [[0, 1], [2, 3]]

// Matrix multiply
let f = a @ b      // [[19, 22], [43, 50]]

// Transpose
let g = a.T        // [[1, 3], [2, 4]]

// Reshape
let h = a.reshape([4])  // [1, 2, 3, 4]

Shape Inspection

let t = Tensor::randn([2, 3, 4])

println(t.shape())   // [2, 3, 4]
println(t.ndim())    // 3
println(t.numel())   // 24
println(t.dtype())   // f32