82. Linear Recurrence medium
Given two matrices a and x, each of shape [B, L] (batch size × sequence length),
compute the linear recurrence h of shape [B, L] defined by:
h[b, 0] = x[b, 0] and h[b, t] = a[b, t] × h[b, t−1] + x[b, t] for t ≥ 1.
All values are float32. This operation is the core computational primitive of
State Space Models (SSMs) such as Mamba, S4, and H3.
Implementation Requirements
- Use only native features (external libraries are not permitted)
- The
solvefunction signature must remain unchanged - The result must be stored in the output tensor
h
Examples
Example 1 — exponential decay (a = 0.5, single impulse):
Example 2 — prefix sum (a = 1, unit inputs):
Full example with B = 2, L = 4:
Constraints
- 1 ≤
B≤ 256 (batch size) - 1 ≤
L≤ 65,536 (sequence length) - All values in
aandxarefloat32 - Performance is measured with
B= 64,L= 16,384
Starter code (CUDA)
#include <cuda_runtime.h>
// a, x, h are device pointers
extern "C" void solve(const float* a, const float* x, float* h, int B, int L) {}
From LeetGPU, CC BY-NC-ND 4.0.