NURLNURL registrynurl-lang.org →

← tensor

tensor 0.1.0 API

tensor.nu

tensor/tensor.nu — an n-dimensional array over gpukit.

The reusable ndarray the ML packages want: onnx keeps its tensors welded to the graph runtime, and gpukit is a marshalling floor nobody wants to program against. Tensor is the shared layer — shape, broadcasting, matmul and reductions — that onnx, anomaly and a future training package can all sit on.

Storage is row-major contiguous f64 (NURL's native float). dtype records the logical type: an F64 tensor is exact f64; an F32 tensor keeps its values on the f32 grid (results are rounded to f32 after each op), so it behaves like float32 for interop while computing in f64. Bytes in/out (from_f32 / to_f32) bridge to onnx weights and files.

API

: i TE_F64 0

: i TE_F32 1

: Tensor

: Tensor {
    i dtype
    ( Vec i ) shape
    ( Vec f ) data
}

@ tensor_of i dtype ( Vec i ) shape ( Vec f ) data → Tensor

Adopt a data vector (moved in) under a shape. nelem must equal prod(shape).

@ tensor_free Tensor t → v

@ tensor_full i dtype ( Vec i ) shape f v → Tensor

@ tensor_zeros i dtype ( Vec i ) shape → Tensor

@ tensor_ones i dtype ( Vec i ) shape → Tensor

@ tensor_from_data i dtype ( Vec i ) shape ( Vec f ) data → Tensor

From an explicit row-major f64 buffer (data is copied and rounded to the dtype grid; shape is adopted — the tensor takes ownership of it).

@ tensor_arange i dtype i n → Tensor

1-D [0, 1, …, n-1].

@ tensor_ndim Tensor t → i

@ tensor_size Tensor t → i

@ tensor_dtype Tensor t → i

@ tensor_dim Tensor t i ax → i

@ tensor_shape Tensor t → ( Vec i )

@ tensor_data Tensor t → ( Vec f )

@ tensor_flat Tensor t i k → f

Flat element read/write.

@ tensor_set_flat Tensor t i k f v → v

@ tensor_reshape Tensor t ( Vec i ) shape → ?Tensor

The same data under a new shape (must have equal nelem). Data is copied so the result is independent; shape is adopted (or freed on a size mismatch).

@ tensor_clone Tensor t → Tensor

Clone a tensor (independent copy).

@ tensor_astype Tensor t i dtype → Tensor


ops.nu

tensor/ops.nu — broadcasting elementwise, unary maps, matmul and reductions.

Elementwise binary ops broadcast with numpy rules (align trailing dims, size-1 stretches). matmul runs on the GPU via gpukit for large f64 problems and on a plain triple loop otherwise — identical results either way. F32 tensors keep their outputs on the f32 grid.

API

: ~ i g_t_gpu 0 // 0 unprobed, 1 ready, -1 unavailable

── GPU singleton (probed lazily; matmul only) ────────────────────────

: ~ i g_t_kit 0 // *GpuKit as an int

@ tensor_gpu_close → v

Release the tensor GPU singleton (tests call this so leak checkers are happy).

@ tensor_add Tensor a Tensor b → ?Tensor

@ tensor_sub Tensor a Tensor b → ?Tensor

@ tensor_mul Tensor a Tensor b → ?Tensor

@ tensor_div Tensor a Tensor b → ?Tensor

@ tensor_maximum Tensor a Tensor b → ?Tensor

@ tensor_minimum Tensor a Tensor b → ?Tensor

@ tensor_pow Tensor a Tensor b → ?Tensor

@ tensor_adds Tensor t f s → Tensor

@ tensor_muls Tensor t f s → Tensor

@ tensor_subs Tensor t f s → Tensor

@ tensor_divs Tensor t f s → Tensor

@ tensor_neg Tensor t → Tensor

@ tensor_abs Tensor t → Tensor

@ tensor_exp Tensor t → Tensor

@ tensor_log Tensor t → Tensor

@ tensor_sqrt Tensor t → Tensor

@ tensor_relu Tensor t → Tensor

@ tensor_sigmoid Tensor t → Tensor

@ tensor_tanh Tensor t → Tensor

@ tensor_matmul Tensor a Tensor b → ?Tensor

@ tensor_transpose Tensor t → ?Tensor

@ tensor_permute Tensor t ( Vec i ) perm → ?Tensor

General axis permutation. perm is a length-ndim ordering of the axes.

@ tensor_sum Tensor t i axis b keepdim → Tensor

axis < 0 → reduce over everything (scalar result).

@ tensor_max Tensor t i axis b keepdim → Tensor

@ tensor_min Tensor t i axis b keepdim → Tensor

@ tensor_prod Tensor t i axis b keepdim → Tensor

@ tensor_mean Tensor t i axis b keepdim → Tensor