TensorTonic
Study Plans
Study Plan
Gated DeltaNet State Update
Language ModelsLinear AlgebraNeural Networks
Hard

Implement the Gated DeltaNet recurrent update. At position ttt, qtq_tqt​, ktk_tkt​, and vtv_tvt​ are query, normalized key, and value vectors; γt\gamma_tγt​ and βt\beta_tβt​ are scalar decay and update gates. StS_tSt​ is the state, yty_tyt​ is the output, and III is the Dk-by-Dk identity matrix. Superscript T denotes transpose.

St=γt(I−βtktktT)St−1+βtktvtT.S_t=\gamma_t\left(I-\beta_tk_tk_t^{\mathsf T}\right)S_{t-1}+\beta_tk_tv_t^{\mathsf T}.St​=γt​(I−βt​kt​ktT​)St−1​+βt​kt​vtT​. yt=qtTSt.y_t=q_t^{\mathsf T}S_t.yt​=qtT​St​.

Each batch state starts at zero. 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],[1,0]]], k = [[[1,0],[1,0]]], v = [[[2,0],[5,0]]], gamma = [[1,1]], beta = [[1,1]]

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

Explanation: The repeated unit key erases its previous state direction before the second value is written, so 5 replaces 2.

Input: q = [[[1,0],[1,0]]], k = [[[1,0],[1,0]]], v = [[[4,0],[99,0]]], gamma = [[1,0.5]], beta = [[1,0]]

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

Input: q = [[[1,0]],[[0,1]]], k = [[[1,0]],[[0,1]]], v = [[[7]],[[9]]], gamma = [[1],[1]], beta = [[1],[1]]

Output: {"outputs":[[[7]],[[9]]],"final_state":[[[7],[0]],[[0],[9]]]}

Hint 1

torch.eye creates the identity matrix on a chosen device.

Hint 2

torch.bmm applies a different matrix to each batch item.

Hint 3

torch.unsqueeze supports batched outer products through broadcasting.

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 and beta have 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 and beta are float32.
  • Keys are unit-normalized before conversion to the input dtype; bfloat16 rounding may change their norms slightly. Use the supplied keys without renormalizing them.
  • All gate values lie in [0, 1].
  • Inputs contain finite values and may be noncontiguous.
Try Similar Problems
Parallel and Recurrent Linear AttentionMediumMamba 2 Gated State ScanMediumTop-k MoE Router with Load StatisticsHardOrthogonalize a Muon Momentum UpdateEasyRMSNorm Forward PassEasyDecode Attention with a KV CacheHardRecover an IsoFLOP Compute-Optimal FrontierHardFit a Data Power Law with a Noise FloorHardJacobian of a Neural Network LayerHardJVP and VJP for a 2-Layer NetworkHardScaled Dot-Product Attention in PythonMediumRBF Kernel MatrixHard

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: array

You must run your code first.

Gated DeltaNet State Update

Language ModelsLinear AlgebraNeural Networks
Hard

Implement the Gated DeltaNet recurrent update. At position ttt, qtq_tqt​, ktk_tkt​, and vtv_tvt​ are query, normalized key, and value vectors; γt\gamma_tγt​ and βt\beta_tβt​ are scalar decay and update gates. StS_tSt​ is the state, yty_tyt​ is the output, and III is the Dk-by-Dk identity matrix. Superscript T denotes transpose.

St=γt(I−βtktktT)St−1+βtktvtT.S_t=\gamma_t\left(I-\beta_tk_tk_t^{\mathsf T}\right)S_{t-1}+\beta_tk_tv_t^{\mathsf T}.St​=γt​(I−βt​kt​ktT​)St−1​+βt​kt​vtT​. yt=qtTSt.y_t=q_t^{\mathsf T}S_t.yt​=qtT​St​.

Each batch state starts at zero. 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],[1,0]]], k = [[[1,0],[1,0]]], v = [[[2,0],[5,0]]], gamma = [[1,1]], beta = [[1,1]]

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

Explanation: The repeated unit key erases its previous state direction before the second value is written, so 5 replaces 2.

Input: q = [[[1,0],[1,0]]], k = [[[1,0],[1,0]]], v = [[[4,0],[99,0]]], gamma = [[1,0.5]], beta = [[1,0]]

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

Input: q = [[[1,0]],[[0,1]]], k = [[[1,0]],[[0,1]]], v = [[[7]],[[9]]], gamma = [[1],[1]], beta = [[1],[1]]

Output: {"outputs":[[[7]],[[9]]],"final_state":[[[7],[0]],[[0],[9]]]}

Hint 1

torch.eye creates the identity matrix on a chosen device.

Hint 2

torch.bmm applies a different matrix to each batch item.

Hint 3

torch.unsqueeze supports batched outer products through broadcasting.

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 and beta have 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 and beta are float32.
  • Keys are unit-normalized before conversion to the input dtype; bfloat16 rounding may change their norms slightly. Use the supplied keys without renormalizing them.
  • All gate values lie in [0, 1].
  • Inputs contain finite values and may be noncontiguous.
Try Similar Problems
Parallel and Recurrent Linear AttentionMediumMamba 2 Gated State ScanMediumTop-k MoE Router with Load StatisticsHardOrthogonalize a Muon Momentum UpdateEasyRMSNorm Forward PassEasyDecode Attention with a KV CacheHardRecover an IsoFLOP Compute-Optimal FrontierHardFit a Data Power Law with a Noise FloorHardJacobian of a Neural Network LayerHardJVP and VJP for a 2-Layer NetworkHardScaled Dot-Product Attention in PythonMediumRBF Kernel MatrixHard

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: array

You must run your code first.