TensorTonic
Study Plans
Study Plan
Parallel and Recurrent Linear Attention
Language ModelsLinear AlgebraNeural Networks
Medium

Implement causal softmax-free linear attention in parallel and recurrent forms. At position ttt, qtq_tqt​, ktk_tkt​, and vtv_tvt​ are the query, key, and value vectors; StS_tSt​ is the state and yty_tyt​ is the output. Superscript T denotes transpose.

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

The state starts at zero, and each output includes the current token. Compute the sequence outputs both in parallel and by a recurrent scan. Return a dictionary containing parallel_output and recurrent_output, each with shape (B, S, Dv), and final_state with shape (B, Dk, Dv). All three values are PyTorch tensors with the same dtype and device as q.

Loading visualization...

Examples

Input: q = [[[1],[4]]], k = [[[2],[5]]], v = [[[3],[6]]]

Output: {"parallel_output":[[[6],[144]]],"recurrent_output":[[[6],[144]]],"final_state":[[[36]]]}

Explanation: The state is 6 after the first write and 36 after the second. Reading those states with queries 1 and 4 produces 6 and 144 in both formulations.

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

Output: {"parallel_output":[[[2],[3]]],"recurrent_output":[[[2],[3]]],"final_state":[[[2],[3]]]}

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

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

Hint 1

torch.cumsum computes prefix sums along a chosen dimension.

Hint 2

torch.bmm multiplies a batch of matrices without mixing batch entries.

Requirements

  • Use PyTorch, with float32 intermediate calculations and results cast to the input dtype.
  • The parallel and recurrent outputs must represent the same inclusive causal attention.
  • Do not modify any input tensor.

Constraints

  • q and k have shape (B, S, Dk); v has shape (B, S, Dv).
  • 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 sharing a device and dtype, either float32 or bfloat16.
  • Inputs contain finite values and may be noncontiguous.
Try Similar Problems
Mamba 2 Gated State ScanMediumGated 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

You must run your code first.

Parallel and Recurrent Linear Attention

Language ModelsLinear AlgebraNeural Networks
Medium

Implement causal softmax-free linear attention in parallel and recurrent forms. At position ttt, qtq_tqt​, ktk_tkt​, and vtv_tvt​ are the query, key, and value vectors; StS_tSt​ is the state and yty_tyt​ is the output. Superscript T denotes transpose.

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

The state starts at zero, and each output includes the current token. Compute the sequence outputs both in parallel and by a recurrent scan. Return a dictionary containing parallel_output and recurrent_output, each with shape (B, S, Dv), and final_state with shape (B, Dk, Dv). All three values are PyTorch tensors with the same dtype and device as q.

Loading visualization...

Examples

Input: q = [[[1],[4]]], k = [[[2],[5]]], v = [[[3],[6]]]

Output: {"parallel_output":[[[6],[144]]],"recurrent_output":[[[6],[144]]],"final_state":[[[36]]]}

Explanation: The state is 6 after the first write and 36 after the second. Reading those states with queries 1 and 4 produces 6 and 144 in both formulations.

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

Output: {"parallel_output":[[[2],[3]]],"recurrent_output":[[[2],[3]]],"final_state":[[[2],[3]]]}

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

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

Hint 1

torch.cumsum computes prefix sums along a chosen dimension.

Hint 2

torch.bmm multiplies a batch of matrices without mixing batch entries.

Requirements

  • Use PyTorch, with float32 intermediate calculations and results cast to the input dtype.
  • The parallel and recurrent outputs must represent the same inclusive causal attention.
  • Do not modify any input tensor.

Constraints

  • q and k have shape (B, S, Dk); v has shape (B, S, Dv).
  • 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 sharing a device and dtype, either float32 or bfloat16.
  • Inputs contain finite values and may be noncontiguous.
Try Similar Problems
Mamba 2 Gated State ScanMediumGated 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

You must run your code first.