Skip to content

Tensors and Math

Tensor is Jacobi's core array type. Every operation works the same way eagerly and inside jit, grad, jvp, vmap, and compiled loops.

Creating Tensors

let a = Tensor::from_f32(&[1.0, 2.0, 3.0, 4.0], &[2, 2]);
let b = Tensor::zeros(&[2, 2]);
let c = Tensor::ones(&[2, 2]);
let eye = Tensor::eye(4);
let d = Tensor::arange(0.0, 10.0, 1.0);
let e = Tensor::linspace(-1.0, 1.0, 16);
let s = Tensor::scalar(3.5);
let q = Tensor::from_i8(&[-4, 0, 7, 12], &[2, 2]);
let on_dev1 = Tensor::from_f32_on(&[1.0, 2.0, 3.0, 4.0], &[2, 2], 1)?;
let half = Tensor::from_bf16(&[0x3f80, 0x4000], &[2])?;

Randomness and Sampling

Call manual_seed(42) once and sample anywhere without manual key splitting. Jacobi stores a Threefry PRNG state on device and threads it automatically through jit, for_loop, while_loop, and vmap.

manual_seed(42);

let u = Tensor::rand(&[32, 64]);
let n = Tensor::randn(&[32, 64]);
let mask = bernoulli(0.5, &[32, 64]);
let trunc = truncated_normal(&[32, 64], -2.0, 2.0);
let w = kaiming_normal(&[64, 128]);
let idx = Tensor::randint(0, 100, &[8]);
let perm = Tensor::randperm(32);

let dropped = nn::dropout(&n, 0.1);
let token = logits.sample_top_k(40, 0.8);
let nucleus = logits.sample_top_p(0.9, 0.8);

Linear Algebra and FFT

Jacobi includes differentiable matrix decompositions, linear solvers, and 1D Fourier transforms backed by StableHLO.

let chol = spd_matrix.cholesky();
let x_spd = chol.cholesky_solve(&rhs);
let x_gen = square_a.solve(&rhs);

let (re, im) = signal.rfft();
let reconstructed = Tensor::irfft(&re, &im, signal_len);
let (fre, fim) = Tensor::fft(&re, &im);
let (ire, iim) = Tensor::ifft(&fre, &fim);

Operations Reference

Standard Rust operators (+, -, *, /, -x) and NumPy/PyTorch broadcasting work out of the box.

Group Methods
Elementwise pow, powf, atan2, abs, exp, expm1, log, log1p, erf, sqrt, rsqrt, sin, cos, tanh, sigmoid, log_sigmoid, relu, leaky_relu, elu, softplus, mish, gelu, gelu_exact, silu, floor, ceil, round, sign, maximum, minimum, clamp, modulo
Comparisons & Predicates eq_tensor, ne_tensor, lt, le, gt, ge, *_scalar, Tensor::where_select, where_scalars, masked_fill, logical_and, logical_or, logical_xor, logical_not, is_nan, is_inf, is_finite
Reductions sum, mean, max, min, prod, all, any, var, std, norm, logsumexp, softmax, log_softmax (plus *_axis and *_axes)
Linear Algebra & FFT matmul, matmul_in, einsum, einsum_in, cholesky, triangular_solve, cholesky_solve, solve, rfft, irfft, fft, ifft
Shapes reshape, flatten, unsqueeze, squeeze, broadcast_to, transpose, permute, concat, stack, slice_axis, dynamic_slice, pad, reverse, triu, tril, diag, chunk, split, repeat, repeat_interleave, roll
Indexing & Sort take, take_axis, gather, scatter_add, scatter_set, segment_sum, index (supports negative indices like -1), sort, sort_axis, argsort, argsort_axis, argsort_stable, argsort_stable_axis, top_k, top_k_axis, argmax, argmax_axis, argmin, argmin_axis, searchsorted, unique, one_hot, bincount, cumsum, cumsum_axis, cumprod, cumprod_axis, cumlogsumexp, cumlogsumexp_axis
Vision & Sequences conv1d, conv1d_grouped, conv_transpose1d, conv2d, conv2d_grouped, conv_transpose2d, conv3d, conv3d_grouped, max_pool1d, avg_pool1d, max_pool2d, avg_pool2d, adaptive_avg_pool2d, upsample_nearest, resize_bilinear, crop
Reading & Casting item, item_f64, to_vec_f32, to_vec_f64, to_host, to_raw_bytes, to_dtype, to_f32, to_f64, to_bf16, to_f16, to_fp8_e4m3, to_fp8_e5m2, to_fp4, to_i8, to_i16, to_u8, to_i32, to_i64, to_u32, detach, shape, dtype, numel

Error Handling

Errors never panic inside math expressions. They propagate lazily on Tensor and return Err(jacobi::Error) when you read a result or call a compiled function.