axonml-tensor
Overview
axonml-tensor provides the core Tensor type for the AxonML framework. Tensors are N-dimensional arrays with support for automatic broadcasting, efficient memory sharing through views, and device-agnostic operations for machine learning computations.
Features
-
N-Dimensional Arrays - Create tensors of arbitrary shape with generic element types (f32, f64, i32, etc.).
-
Automatic Broadcasting - NumPy-style broadcasting for element-wise operations between tensors of different shapes.
-
Efficient Views - Zero-copy slicing, transposing, and reshaping through stride manipulation without data copying.
-
Device Agnostic - Transparent tensor operations across CPU, CUDA, Vulkan, Metal, and WebGPU backends.
-
Rich Operations - Comprehensive arithmetic, reduction, activation, and matrix operations including matmul with batching support.
-
Factory Functions - Convenient tensor creation with
zeros,ones,rand,randn,arange,linspace, and more.
Modules
| Module | Description |
|---|---|
tensor |
Core Tensor struct with arithmetic, reduction, activation, and shape operations |
shape |
Shape and stride utilities including broadcasting, reshape, and index computation |
creation |
Factory functions for tensor initialization (zeros, ones, rand, randn, arange, linspace, eye) |
view |
Slicing, indexing, and view operations (select, narrow, chunk, split) |
ops |
Additional operations including softmax, GELU, comparisons, and clipping |
Usage
Add this to your Cargo.toml:
[]
= "0.1.0"
Basic Example
use ;
// Create tensors
let a = ;
let b = ;
let c = ;
// Arithmetic operations
let sum = a.add.unwrap;
let product = b.mul.unwrap;
let scaled = c.mul_scalar;
// Reductions
let total = scaled.sum;
let average = scaled.mean.unwrap;
let maximum = scaled.max.unwrap;
Shape Operations
use Tensor;
let t = from_vec.unwrap;
// Reshape
let flat = t.reshape.unwrap; // [6]
let reshaped = t.reshape.unwrap;
// Transpose
let transposed = t.t.unwrap; // [3, 2]
// Squeeze and unsqueeze
let unsqueezed = t.unsqueeze.unwrap; // [1, 2, 3]
let squeezed = unsqueezed.squeeze.unwrap; // [2, 3]
Matrix Operations
use Tensor;
// Matrix multiplication
let a = from_vec.unwrap;
let b = from_vec.unwrap;
let c = a.matmul.unwrap; // [2, 2]
// Batched matmul
let batch_a = ;
let batch_b = ;
let batch_c = batch_a.matmul.unwrap; // [4, 2, 5]
// Dot product
let v1 = from_vec.unwrap;
let v2 = from_vec.unwrap;
let dot = v1.dot.unwrap; // Scalar tensor
Activation Functions
use Tensor;
let x = from_vec.unwrap;
let relu_out = x.relu; // [0.0, 0.0, 1.0, 2.0]
let sigmoid_out = x.sigmoid;
let tanh_out = x.tanh;
let gelu_out = x.gelu;
let softmax_out = x.softmax;
Broadcasting
use Tensor;
// Automatic broadcasting
let a = from_vec.unwrap;
let b = from_vec.unwrap;
let c = a.add.unwrap; // [11.0, 12.0, 13.0]
// 2D broadcasting
let matrix = from_vec.unwrap;
let row = from_vec.unwrap;
let result = matrix.add.unwrap; // [2, 3]
Tests
Run the test suite:
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.