Selective Mamba #
The Mamba-1 recurrence, expressed through the same differentiable operations used by other TorchLean layers. A token determines its convolution feature, time step, and input/output state vectors. The previous hidden state enters only the affine state update.
Parameter matrices use the orientation of Models.SelectiveMambaBlockSpec: a row vector multiplies
a matrix on the right. The convolution kernel is newest-first. We store logA and compute
A = exp(logA) before the recurrence; over the reals this makes every continuous-time rate -A
negative throughout training. Floating-point overflow and underflow still follow the chosen
backend's arithmetic.
step and runArray carry both the hidden state and projected-token history. Keeping references
in that cache lets a continued computation retain gradients through earlier chunks. Callers that
want truncated backpropagation can detach those references explicitly.
This implementation follows the Spec's dense time-step projection. It does not use a fused variable-coefficient scan or the low-rank time-step parameterization of the authors' default block.
Dimensions inside a selective Mamba layer, independently of its input and output widths.
- expansion : ℕ
Expanded channels per output feature:
innerWidth = expansion * outputWidth. - stateWidth : ℕ
Number of diagonal recurrent states carried by each expanded channel.
- kernelWidth : ℕ
Number of newest-first taps in the causal depthwise convolution.
Instances For
Instances For
The eleven trainable tensors, in Spec matrix orientation.
The content, gate, B, C, and output projections have no bias. The convolution and time-step
projection each have a bias. logA parameterizes positive rate magnitudes, and dSkip multiplies
the activated convolution feature directly.
Project raw tokens into the content path carried by the convolution cache.
Project raw tokens into the SiLU gate, independently of the convolution path.
Depthwise taps: row zero multiplies the current projected token.
Additive channel bias before the content SiLU.
Dense map from activated content to per-channel time steps before softplus.
Time-step bias before softplus, stored separately from the dense projection.
Logarithms of rate magnitudes; transitions use
exp(-delta * exp(logA)).Produce a token's B vector, shared by the expanded channels.
Produce a token's C vector for reading out the updated state.
Per-channel coefficient of the direct
D * ureadout path.Project gated expanded channels back to the requested output width.
Instances For
State at a chunk boundary.
history[0] is the most recent projected content token, before convolution and SiLU. A step
prepends the new projection and retains at most kernelWidth entries. Missing entries mean zero
padding; the hidden state has one stateWidth vector for each expanded channel.
Projected content tokens in newest-first order.
Instances For
Right-multiply a vector by a Spec-oriented projection without introducing a bias parameter.
Instances For
One step with rate magnitudes already computed.
Sharing exp(logA) across a sequence avoids recording the same exponential for every token. Its
reference still participates in each transition, so reverse accumulation sums all rate gradients.
Instances For
Advance one token using the selective Mamba-1 equations.
With u = SiLU(causalConv(x @ xProj)), the update is
h'[d,n] = exp(-softplus(u @ dtProj + dtBias)[d] * exp(logA[d,n])) * h[d,n] + (delta[d] * (u @ bProj)[n]) * u[d].
The readout contracts h' with u @ cProj, adds dSkip * u, gates by SiLU(x @ zProj),
and applies outProj.
Instances For
Run a chunk and return the complete cache needed by the next chunk, together with its outputs.
An empty chunk preserves both pieces of state. Chunk boundaries introduce no detach operation: using the returned references in a later chunk keeps gradients through the earlier computation.