TensorTonic
Study Plans
Study Plan
Mamba 2 Gated State Scan
Language ModelsLinear AlgebraNeural Networks
Medium

Implement a gated recurrent state scan. At position ttt, γt\gamma_tγt​ is a scalar decay gate, qtq_tqt​, ktk_tkt​, and vtv_tvt​ are query, key, and value vectors, StS_tSt​ is the state, and yty_tyt​ is the output. Superscript T denotes transpose.

St=γtSt−1+ktvtT.S_t=\gamma_tS_{t-1}+k_tv_t^{\mathsf T}.St​=γt​St−1​+kt​vtT​. yt=qtTSt.y_t=q_t^{\mathsf T}S_t.yt​=qtT​St​.

Each batch state starts at zero, and the current output reads the updated state. Return a dictionary containing outputs with shape (B, S, Dv) and final_state with shape (B, Dk, Dv), both PyTorch tensors with the same dtype and device as q.

Loading visualization...

Examples

Input: q = [[[1,0],[0,1]]], k = [[[1,0],[0,1]]], v = [[[2,3],[4,5]]], gamma = [[0.5,1]]

Output: {"outputs":[[[2,3],[4,5]]],"final_state":[[[2,3],[4,5]]]}

Explanation: The first key writes the first state row. The second gate preserves that row while the orthogonal key writes the second row.

Input: q = [[[1,1],[2,0]]], k = [[[1,0],[0,1]]], v = [[[3],[5]]], gamma = [[0,0]]

Output: {"outputs":[[[3],[0]]],"final_state":[[[0],[5]]]}

Input: q = [[[1]]], k = [[[2]]], v = [[[3]]], gamma = [[0.25]]

Output: {"outputs":[[[6]]],"final_state":[[[6]]]}

Hint 1

torch.unsqueeze adds singleton dimensions for broadcasting a scalar gate.

Hint 2

torch.bmm handles the per-batch matrix products.

Requirements

  • Use PyTorch, with float32 intermediate calculations and results cast to the query dtype.
  • Each batch item has its own independent recurrent state.
  • Do not modify any input tensor.

Constraints

  • q and k have shape (B, S, Dk), v has shape (B, S, Dv), and gamma has shape (B, S).
  • B is batch size, S is sequence length, and Dk and Dv are key and value widths; all are positive.
  • All inputs are PyTorch tensors on the same device; q, k, and v share dtype float32 or bfloat16, while gamma is float32.
  • Every gamma value lies in [0, 1].
  • Inputs contain finite values and may be noncontiguous.
Try Similar Problems
Parallel and Recurrent Linear AttentionMediumGated DeltaNet State UpdateHardTop-k MoE Router with Load StatisticsHardOrthogonalize a Muon Momentum UpdateEasyRMSNorm Forward PassEasyTrain a BPE Tokenizer From Scratch in PythonMediumCanonicalize Pairwise Preference DataMediumCompute Temperature-Based Data MixturesMediumScaled Dot-Product Attention in PythonMediumJacobian of a Neural Network LayerHardJVP and VJP for a 2-Layer NetworkHardHessian Tensor of a Vector-Valued FunctionMedium

Accepts: array

Accepts: array

Accepts: array

Accepts: array

You must run your code first.

Mamba 2 Gated State Scan

Language ModelsLinear AlgebraNeural Networks
Medium

Implement a gated recurrent state scan. At position ttt, γt\gamma_tγt​ is a scalar decay gate, qtq_tqt​, ktk_tkt​, and vtv_tvt​ are query, key, and value vectors, StS_tSt​ is the state, and yty_tyt​ is the output. Superscript T denotes transpose.

St=γtSt−1+ktvtT.S_t=\gamma_tS_{t-1}+k_tv_t^{\mathsf T}.St​=γt​St−1​+kt​vtT​. yt=qtTSt.y_t=q_t^{\mathsf T}S_t.yt​=qtT​St​.

Each batch state starts at zero, and the current output reads the updated state. Return a dictionary containing outputs with shape (B, S, Dv) and final_state with shape (B, Dk, Dv), both PyTorch tensors with the same dtype and device as q.

Loading visualization...

Examples

Input: q = [[[1,0],[0,1]]], k = [[[1,0],[0,1]]], v = [[[2,3],[4,5]]], gamma = [[0.5,1]]

Output: {"outputs":[[[2,3],[4,5]]],"final_state":[[[2,3],[4,5]]]}

Explanation: The first key writes the first state row. The second gate preserves that row while the orthogonal key writes the second row.

Input: q = [[[1,1],[2,0]]], k = [[[1,0],[0,1]]], v = [[[3],[5]]], gamma = [[0,0]]

Output: {"outputs":[[[3],[0]]],"final_state":[[[0],[5]]]}

Input: q = [[[1]]], k = [[[2]]], v = [[[3]]], gamma = [[0.25]]

Output: {"outputs":[[[6]]],"final_state":[[[6]]]}

Hint 1

torch.unsqueeze adds singleton dimensions for broadcasting a scalar gate.

Hint 2

torch.bmm handles the per-batch matrix products.

Requirements

  • Use PyTorch, with float32 intermediate calculations and results cast to the query dtype.
  • Each batch item has its own independent recurrent state.
  • Do not modify any input tensor.

Constraints

  • q and k have shape (B, S, Dk), v has shape (B, S, Dv), and gamma has shape (B, S).
  • B is batch size, S is sequence length, and Dk and Dv are key and value widths; all are positive.
  • All inputs are PyTorch tensors on the same device; q, k, and v share dtype float32 or bfloat16, while gamma is float32.
  • Every gamma value lies in [0, 1].
  • Inputs contain finite values and may be noncontiguous.
Try Similar Problems
Parallel and Recurrent Linear AttentionMediumGated DeltaNet State UpdateHardTop-k MoE Router with Load StatisticsHardOrthogonalize a Muon Momentum UpdateEasyRMSNorm Forward PassEasyTrain a BPE Tokenizer From Scratch in PythonMediumCanonicalize Pairwise Preference DataMediumCompute Temperature-Based Data MixturesMediumScaled Dot-Product Attention in PythonMediumJacobian of a Neural Network LayerHardJVP and VJP for a 2-Layer NetworkHardHessian Tensor of a Vector-Valued FunctionMedium

Accepts: array

Accepts: array

Accepts: array

Accepts: array

You must run your code first.