LeetKernels
ProblemsStart solving
Problems

76. Adder Transformer Inference medium

Run batched autoregressive inference for a 10-parameter transformer that adds two 10-digit numbers. Given prompts of shape [batch_size, 31] (int32) and a 10-float weight buffer, write output logits of shape [batch_size, 11, 10] — one logit row per decode step over the 10-digit vocabulary (0–9). All tensors are float32 except the int32 prompts.

The model comes from the AdderBoard competition for the smallest autoregressive transformer that adds 10-digit numbers at ≥99% accuracy. It encodes carry propagation in 10 learned parameters via RoPE geometry, tied embeddings, and SwiGLU gating.

Token Prompt [B,31] Embed: [w0-w1*d², -d] Unit RMSNorm Self-Attention (1 head, dim=2) Q Proj [2p] K Proj [0p] V Proj [1p] QK Norm + RoPE(ω=2π/19) + Causal Attn + residual + Unit RMSNorm MLP: Gate + SwiGLU + Carry [3p] RMSNorm [2p] + Logits Total: 10 parameters (2+2+1+2+1+2)

Model Architecture

Single-layer pre-norm transformer. Hidden dim 2, 1 head, head dim 2, vocab 10 (digits 0–9), tied input/output embeddings.

Each step runs the full sequence [batch_size, seq_len, 2] through:

1. Token Embedding (2 parameters: w0, w1)

e(d)=[w0​−w1​⋅d2−d​]

2. Unit RMSNorm (no parameters)

UnitRMSNorm(x)=mean(x2)+ϵ​x​,ϵ=10−6

3. Self-Attention (3 parameters: q0, q1, v0)

Projections applied to the normed hidden state h with shape [*, 2]:

Q=[h0​⋅q0​h0​⋅q1​​],K=[h0​0​],V=[h1​⋅v0​0​]

After projection, Q and K are each normalized with Unit RMSNorm, then RoPE is applied with angular frequency ω = 2π/19:

RoPE(x,p)=[x0​cos(pω)−x1​sin(pω)x0​sin(pω)+x1​cos(pω)​]

Scaled dot-product attention with causal mask uses scale factor:

scale=dh​​1​⋅S2

where dh​=2 is the head dimension and S2 is the QK-norm scale constant (see weight table below for exact value).

The output projection maps [attn_0, attn_1] → [0, attn_0] (no parameters), followed by a residual connection.

4. MLP (3 parameters: a, c, carry)

Applied to the unit-RMSNorm of the post-attention hidden state:

g0​=h0​⋅a+h1​⋅c,g1​=h0​⋅(a−c/1000)+h1​⋅c

base=h0​,up=[base,base]

mix=SiLU([g0​,g1​])⊙up

MLP(h)=[0carry⋅(mix1​−mix0​)​]

followed by a residual connection.

5. Final RMSNorm (2 parameters: n0, n1)

Standard RMSNorm with learned weight:

out=mean(h2)+ϵ​h​⊙[n0​,n1​]

6. Output Logits (tied with embedding)

logits=out⋅ETwhere Ed​=e(d)

Autoregressive Decoding

Starting from the 31-token prompt, repeat 11 times:

  1. Run the full forward pass on the current sequence
  2. Extract logits at the last position → store in output
  3. Append argmax(logits) as the next token

The sequence grows from length 31 to 42 over the 11 decode steps.

Weight Layout

OffsetSizeNameDescription
02embedEmbedding: e(d) = [w0 - w1*d², -d]
22q_projQ projection weights [q0, q1]
41v_projV projection weight v0
52gateMLP gate weights [a, c]
71carryMLP carry weight
82normFinal RMSNorm weight [n0, n1]

Token Encoding

Each input pair (a, b) of 10-digit numbers is encoded as a 31-token sequence:

[0, a_rev_0, ..., a_rev_9, 0, 0, 0, 0, 0, 0, 0, 0, 0, b_rev_0, ..., b_rev_9, 0]

where a_rev and b_rev are the digits in least-significant-first order, zero-padded to 10 digits. The model then generates 11 output tokens (digits of the sum, also least-significant-first).

Implementation Requirements

  • Implement solve(prompts, output, weights, batch_size) with the exact signature shown (JAX exception: solve(prompts, weights, batch_size) returns the output tensor directly)
  • Do not use any external libraries beyond what the framework provides
  • The function must write logits into the output buffer (except JAX, which returns it)
  • Architecture constants are fixed: vocab_size = 10, hidden_dim = 2, head_dim = 2, num_heads = 1, prompt_len = 31, decode_steps = 11
  • RMSNorm epsilon = 10−6
  • RoPE angular frequency ω = 2π/19
  • Attention scale = (1/√2) · S² where S² = ln(10) / (√2 · (cos(0.3ω) − cos(0.7ω)))
  • SiLU activation: silu(x) = x · sigmoid(x)

Example

With batch_size = 2 and pairs (3, 5), (99, 1):

Input prompts (shape [2, 31]):
  [0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  [0, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Output logits shape: [2, 11, 10]
  (logits at each of 11 decode steps over 10 digit classes)

Expected decoded tokens (via argmax):
  Pair (3, 5):   sum = 8       → [8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  Pair (99, 1):  sum = 100     → [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]

Constraints

  • batch_size: 1 ≤ batch_size ≤ 100,000
  • prompts: 32-bit integer tensor, values in [0, 9]
  • weights: 32-bit float tensor with exactly 10 elements
  • output: 32-bit float tensor of shape [batch_size, 11, 10]
  • Input numbers are in range [0, 9,999,999,999] (10-digit unsigned integers)
  • Performance is measured with batch_size = 100,000

Starter code (CUDA)

#include <cuda_runtime.h>

// prompts, output, weights are device pointers
extern "C" void solve(const int* prompts, float* output, const float* weights, int batch_size) {}

Solve it on your GPU

Previous: Sparse Matrix-Dense Matrix MultiplicationNext: 2D FFT

From LeetGPU, CC BY-NC-ND 4.0.