Differentiable sequence primitives
Compose native embeddings, causal attention and custom objectives with bounded tensor shapes, tested gradients and explicit CPU or Metal/MPS execution.
Released in Aner 0.1.0Aner 0.1.0 or newerAner provides differentiable tensor composition, embeddings, normalization, token loss and attention in the existing aner.tensor and aner.nn imports. The implementations use native C++ and the existing reverse mode graph. They let a researcher build and modify a decoder from ordinary functions and classes.
These primitives underpin the native generative workflow, including byte/BPE and supported GPT 2 tokenization, training, fine tuning, LoRA, checkpoint/resume, Metal Float32 and CUDA Float64/Float32 execution, cached generation and memory estimates. Current bounds still target small experiments; larger model budgets, arbitrary external architectures and distributed sequence training remain future work. The service interface in aner.llm remains independent.
Parameter reference and model ownership
Neural networks links the supported model families. The exact API inventory complements the shape specific primitives below, while generative model parameters describes the built in decoder trainer and optimizer parameters describes custom keyed loops. An arbitrary function/class composition does not inherit the built in decoder's optimizer registration, checkpoint schema or data role auditing automatically.
Run a composed decoder
The complete sequence example implements a custom decoder block with two heads, learned token and position embeddings, causal and padding masks, LayerNorm, GELU, residuals, a feed forward expansion, tied input/output embeddings and named Adam slots.
aner check examples/neural_sequence.aner
aner examples/neural_sequence.aner --prod
aner examples/neural_sequence.aner --dev reports/sequence-firstUse a new report directory. The example trains on a fixed synthetic sequence fixture to check composition and updates. Its loss reduction demonstrates that the native computation and gradients connect; it is not a language quality benchmark. Replace its data and architecture deliberately for research applications, keeping independent training and evaluation partitions.
Shapes and ownership
A differentiable Tensor has positive row major dimensions with rank 1 through 8. Existing tensor literals and two argument constructors still create matrices. Use tensor.reshape to declare batch, time and head axes. Shapes are runtime checked; the language type remains Tensor.
import aner.tensor
import aner.nn
let values = [[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]]
let batch = tensor.reshape(values, shape: List(2, 2, 2))
print(tensor.rank(batch)) // 3
print(tensor.dim(batch, 0)) // 2
let heads = tensor.permute(batch, axes: List(1, 0, 2))tensor.rank(value) returns rank and tensor.dim(value, axis) returns an extent. Axes are zero based and nonnegative. nn.rows/tensor.rows flatten all leading dimensions, and nn.cols/tensor.cols report the last extent. The legacy two index value access uses that flattened matrix view.
Composition operations preserve immutable contiguous logical values. CPU operations materialize copies; Metal and CUDA reshape alias immutable resident storage while permutation/gather operations materialize device results. They do not introduce writable or strided aliases. Copying a Tensor handle shares its immutable node; reshape and permutation create new nodes linked to their source. nn.parameter creates an independent trainable leaf, while nn.detach creates detached data. Gradients, precision/device changes and optimizer updates preserve logical shapes. Equal flattened sizes do not make different logical shapes compatible.
Tensor composition API
| Call | Contract |
|---|---|
tensor.reshape(value, shape) | Positive List of Int64 extents, same element count. No inferred -1 extent. |
tensor.permute(value, axes) | List every axis exactly once; forward permutes and backward reverses the mapping. |
tensor.slice(value, axis, start, length) | Nonempty in bounds interval; backward accumulates into the selected source region. |
tensor.concatenate(a, b, axis) | Equal rank and matching other axes; gradients reach both inputs. |
tensor.broadcast_to(value, shape) | Right align dimensions; each source extent must equal its target or be one. Backward sums repeated uses. |
tensor.sum(value, axis) | Sum one axis and retain it with extent one. |
tensor.batched_matmul(a, b, accumulation: "default") | Multiply the final two axes and broadcast leading batch axes from the right. Both operands have rank at least two. The optional accumulation mode controls forward precision while preserving the operand dtype. |
For example, [batch,time,width] multiplied by [width,output] yields [batch,time,output]; the projection receives gradients summed across batches and time. [batch,heads,time,width] multiplied by [batch,heads,width,keys] yields [batch,heads,time,keys].
The accumulation option and native Float32 CPU default are part of the Aner 0.1.1 release candidate. For CPU Float32 inputs, the default uses native Float32 accumulation when a conservative range check permits it. It uses Float64 accumulation when Float32 intermediates might overflow. Pass accumulation: "Float64" to use Float64 accumulation for the forward product and round the result to Float32. Use accumulation: "Float32" to require native Float32 accumulation on Float32 operands. For GPU operands, an explicit mode must match the tensor dtype. To use Float64 accumulation with Float32 GPU tensors, transfer the operands to CPU first.
Elementwise arithmetic requires identical logical shapes. Make broadcasts explicit before nn.add or nn.multiply. Pointwise activations, scaling, bias addition and softmax preserve logical shape. Softmax acts on the last axis. nn.argmax replaces the final extent with one. nn.matmul and nn.transpose remain matrix APIs and reject other ranks; use batched products and permutations instead.
For source document IDs, exact padded token batches and an inspectable classification workflow, use aner.nlp and the NLP case studies. Numerical primitives retain the aner.nn owner.
Neural primitives
The nn.feed_forward and nn.masked_mean NLP bridge operations require Aner 0.0.5 or newer. Other sequence primitives retain their existing availability.
| Call | Contract |
|---|---|
nn.embedding(table, ids, shape) | Table is [vocabulary,width]. ids is an exact Int64 list whose count matches shape. Output is shape + [width]; IDs must be in range. Repeated IDs scatter add their gradients. |
nn.feed_forward(input, w_in, b_in, w_out, b_out) | Rank 2 through 8 GELU FFN with explicit rank two projections and biases; preserves leading axes, device, dtype and autograd. No implicit normalization, dropout or residual. |
nn.masked_mean(input, mask) | Pool [batch,time,width] using detached exact binary [batch,time] mask to [batch,width]; reject all padding rows and give masked tokens zero gradient. |
nn.layer_norm(value, gain, bias, epsilon) | Normalize the final axis using population variance. Gain and bias are [width] or [1,width] and may be parameters. Epsilon is finite and positive. |
nn.gelu(value) | Exact error function GELU with its analytic derivative. |
nn.dropout(value, probability, seed, training) | Inverted dropout with probability in [0,1). A call owns a deterministic SplitMix64 stream; the same seed reproduces the mask. Evaluation and zero probability return the input. |
nn.cross_entropy_ids(logits, targets, ignore_index) | Stable log sum exp categorical loss. Integer targets flatten the leading logits axes. Ignored rows have zero gradient; average by nonignored target count. A fully ignored batch is an error. |
nn.masked_softmax(scores, mask, causal, offset) | Last axes are query and key positions; normalize permitted keys only. |
nn.attention(query, key, value, mask, causal, offset) | Scaled dot product attention composed from batched products, permutation, scaling and masked softmax, with gradients for Q, K and V. |
Shape/ID/target/axis lists reject non Int64 elements; token IDs never round through a floating point tensor. Existing nn.cross_entropy remains available for dense one hot targets.
Dropout's training argument is model behavior. --dev and --prod do not change it. Use an explicit seed schedule across training calls rather than unintentionally repeating one mask. No operation here consumes a shared global random stream.
Primitive argument meanings
These registered primitives use explicit arguments; the table below does not add implicit defaults. Make device/dtype equal before composition. Constructor sizes and axis/ID lists use exact Int64 values rather than rounded floating point indices.
| Variables | Type / meaning / constraints |
|---|---|
embedding.table | Trainable or detached [vocabulary,width] Tensor. Vocabulary rows are the exact lookup identities; repeated IDs accumulate gradients into the same row. |
embedding.ids, shape | Exact Int64 List IDs in [0,vocabulary), and positive logical output ID extents whose product equals ID count. Result appends the table width. |
layer_norm.value, gain, bias | Normalize the final axis. Gain/bias have [width] or [1,width] shape and may be parameters. They are learned scale/offset, not preprocessing statistics fitted across a Dataset. |
layer_norm.epsilon | Required finite positive Float64 stability term. There is no omitted source epsilon overload for this sequence primitive. |
dropout.probability | Required finite Float64 in [0,1). An element's drop probability, with inverted scaling; not a count or a guaranteed exact fraction per call. |
dropout.seed | Required signed Int64 call local seed. Repeating it with the same shape/probability repeats the mask; a custom trainer must own an explicit step schedule. |
dropout.training | Required Bool. True applies the mask; false returns identity. Dev/Prod recording does not select this flag. |
cross_entropy_ids.logits, targets | Tensor scores with classes on the final axis and one exact Int64 target per flattened leading row. Targets are a List, not a rounded Tensor. |
cross_entropy_ids.ignore_index | Required Int64 target value to exclude. Other targets must be valid class IDs. Ignored rows have zero gradient; an entirely ignored batch fails. |
masked_softmax.scores | Tensor with keys on the final axis and query/key positions on the final two axes. Scaling scores is the caller's responsibility for this primitive. |
attention.query, key, value | Tensor head vectors with matching compatible final widths and declared leading batch/head axes. The operation applies its own inverse square root head width scaling. |
mask in masked softmax/attention | Detached same-device/dtype finite binary Tensor. One permits a key, zero excludes it; it must broadcast to complete scores. A row cannot exclude every key. |
causal | Required Bool; restrict a key to positions at most query_index + offset when true. A padding mask and causal restriction can apply together. |
offset | Required nonnegative Int64 positional offset. Use zero for ordinary self attention. It does not create position embeddings or an inference KV cache. |
For a managed decoder whose trainer owns random progression, named parameters, corpus lineage and checkpoint continuation, use generative model parameters. The explicit primitives remain useful when a custom architecture requires different composition; that custom model owns its own state and persistence design.
Attention masks and positions
Use Q and K shaped [...,queries,head_width] and [...,keys,head_width], with V shaped [...,keys,value_width]. A typical self attention shape is [batch,heads,time,head_width]. The scores are Q × transpose(K) / sqrt(head_width). V may broadcast into the Q/K batch shape but cannot introduce additional heads or batches.
Masks are detached finite 0/1 tensors, in the same dtype and device as scores. A one permits a key; a zero excludes it. They broadcast from the right to the complete score shape. [batch,1,1,keys] represents key padding across all heads and query positions. [[1.0]] permits every key before applying causality. Masked probabilities and gradients are exactly zero.
With causal: true, key index must be at most query_index + offset. Offset is nonnegative and key positions start at zero. For self attention use offset zero. For queries representing a tail starting at position s of a longer key sequence, pass s. Offset controls masking only; it does not apply positional embeddings or maintain a KV cache. A row with no permitted keys fails explicitly. Pad queries can attend valid keys and then be excluded from the loss using ignore_index.
Masking occurs inside stable softmax without creating infinite tensor entries. To inspect selected probabilities, compose the scores yourself and call nn.masked_softmax; nn.attention does not hide projection weights or learnable parameters.
Custom model and parameter contract
Use ordinary functions or a class with Tensor fields and a forward method. Its output must be a connected Tensor built from differentiable operations. Initialize trainable leaves with nn.parameter; plain tensors remain constants. The decoder example provides the complete class pattern.
Assign a stable name to every distinct trainable parameter when calling nn.update(optimizer, parameter, key). The existing optimizer registers a slot on its first update and checks the complete logical shape, dtype and device on later updates. Explicit class update methods enumerate parameters; automatic parameter discovery is not implemented. An update returns a new leaf and must be assigned back to the field.
For tied weights, use the same parameter handle in both forward paths and update it once. Backward accumulates both uses into that parameter. Replacing each tied field with separate nn.parameter calls breaks that identity. A custom forward can freeze a parameter by using detached data and excluding it from updates. The reference aner.gen decoder already supports full fine tuning, exact name freezing and LoRA; custom classes retain responsibility for their own training and state lifecycle. See the from scratch guide.
nn.backward(loss) replaces reachable gradients. It does not accumulate across calls. Build a fresh forward graph each step, update leaves, and release previous graph handles. Do not retain every historical loss tensor in a list. The tests verify stable tracked buffer use over repeated updates.
Devices precision and resource limits
Composition and sequence kernels support CPU Float64/Float32, Metal Float32 and CUDA Float64/Float32. Unsupported GPU calls fail explicitly without a hidden CPU fallback. CPU reference reductions accumulate in Float64 and round Float32 outputs at tensor boundaries. CPU batched matrix products use the accumulation mode described above. Metal uses native Float32 arithmetic and reductions, and CUDA uses native arithmetic in the selected Float32 or Float64 dtype with strict cuBLAS products. CUDA sequence reductions and CSR scatter own their output locations, and broadcast product gradients accumulate in batch order without floating point atomic sums. CPU/device comparisons use declared numerical tolerances, while strict future token causality and same backend resume are checked separately.
Bounds remain 1,000,000 elements per tensor, rank at most 8, graph depth 256, at most 4,096 graph nodes and 4,000,000 graph elements. Batched matrix products allow at most 50,000,000 multiply adds per call. The attention implementation materializes scores and probabilities; its workspace grows quadratically with sequence length. These are checked resource limits, not estimates of total process memory. Raising them is not a substitute for implementing scalable memory management and accelerator kernels.
Inspection and validation
Development reports display logical shapes. Their JSON records shape in addition to flattened rows and cols, both for ordinary tensor captures and differentiation nodes. Raw values retain the existing opt in limits. Production uses identical mathematical operations, masks and seeds, without recording inspection. Expensive explicit diagnostic expressions still evaluate unless guarded as described in development mode.
aner.sequence checks Float64 finite differences for composition, broadcasts, batched products, embeddings, LayerNorm, GELU, dropout, token loss and causal Q/K/V attention. It checks repeated IDs, padding, score scaling, position offsets, future token isolation in forward and backward, shape/type errors, Float32 propagation, optimizer shape validation and graph lifetime. aner.sequence_language checks named arguments, custom decoder training, persistent sessions, source diagnostics, logical inspection shapes and dev/prod equality.
The CUDA workflow exercises an explicit device/dtype choice, native BPE, training, checkpoint/resume and cached/uncached generation. The native correctness suites exercise both CUDA precisions alongside generative CUDA tests. These accelerator comparisons supplement the independent CPU Float64 gradients; a faster timing is not a correctness criterion. See the execution and verification workflow.