LeetKernels
ProblemsStart solving
Problems

93. Llama Transformer Block hard

Implement a single Llama-style transformer decoder block. Given an input tensor x of shape (seq_len, 512), a packed weight buffer, and precomputed RoPE tables, compute the output using pre-norm architecture with Grouped Query Attention (GQA), Rotary Position Embeddings (RoPE), and a SwiGLU feed-forward network.

x (seq_len, 512) residual RMSNorm 1 QKV Projection (GQA) RoPE (Q and K) Causal Attention Output Projection + residual RMSNorm 2 Gate & Up Proj (512→1408) SiLU(gate) ⊙ up Down Proj (1408→512) + output (seq_len, 512)

The block follows Llama's pre-norm architecture. Unlike GPT-2, it uses RMSNorm (no mean subtraction, no additive bias), Grouped Query Attention with 8 query heads and 2 key/value heads, Rotary Position Embeddings applied to Q and K, and a SwiGLU feed-forward network. None of the linear projections have bias terms.

x′output​=x+Attn(RMSNorm1​(x),cos,sin)=x′+FFN(RMSNorm2​(x′))​

The sub-operations in detail:

RMSNorm(z,w)QKVRoPE(q,cos,sin)GQAheadi​Attn(x)FFN(z)​=d1​∑i​zi2​+ε​z​⊙w,ε=10−5=RMSNorm1​(x)WQ⊤​∈RT×512,reshape to (T,8,64)=RMSNorm1​(x)WK⊤​∈RT×128,reshape to (T,2,64)=RMSNorm1​(x)WV⊤​∈RT×128,reshape to (T,2,64):[q1​∣q2​]↦[q1​⊙cos−q2​⊙sin∣q1​⊙sin+q2​⊙cos]q1​=q[…,:32],q2​=q[…,32:]:repeat K,V along head dim 4× to match 8 Q heads=softmax(64​Qi​Ki⊤​​+Mcausal​)Vi​=Concat(head1​,…,head8​)WO⊤​=(SiLU(zWgate⊤​)⊙zWup⊤​)Wdown⊤​​

where Mcausal​ is the upper-triangular causal mask (−∞ above the diagonal) and SiLU(x)=x⋅σ(x).

Implementation Requirements

  • Use only native features (external libraries are not permitted)
  • The solve function signature must remain unchanged
  • The final result must be stored in the output tensor
  • RMSNorm uses ε=10−5, no additive bias
  • Apply causal masking: position i attends only to positions ≤i
  • Repeat K and V heads 4× (GQA groups) before computing attention
  • cos and sin have shape (seq_len, 32) — apply them to both Q and K heads independently

Weight Layout

All parameters are packed into a single contiguous weights buffer (2,819,072 floats) in the order below. All 2-D matrices are stored row-major with shape (out_dim, in_dim). There are no bias terms.

Parameter Shape Size Offset
w1​ (RMSNorm 1 scale) (512,) 512 0
WQ​ (512, 512) 262,144 512
WK​ (128, 512) 65,536 262,656
WV​ (128, 512) 65,536 328,192
WO​ (512, 512) 262,144 393,728
w2​ (RMSNorm 2 scale) (512,) 512 655,872
Wgate​ (1408, 512) 720,896 656,384
Wup​ (1408, 512) 720,896 1,377,280
Wdown​ (512, 1408) 720,896 2,098,176

Example

With seq_len = 4, x drawn uniformly from [−1, 1], and randomly initialized weights:

Input:  x.shape       = (4, 512)       # 4 token hidden states
        weights.shape = (2,819,072,)   # packed weight buffer
        cos.shape     = (4, 32)        # precomputed RoPE cosines
        sin.shape     = (4, 32)        # precomputed RoPE sines
        seq_len       = 4
Output: output.shape  = (4, 512)       # transformed token hidden states

Constraints

  • d_model = 512, n_q_heads = 8, n_kv_heads = 2, head_dim = 64, ffn_hidden = 1,408
  • 1 ≤ seq_len ≤ 4,096
  • All tensors use 32-bit floating point
  • Performance is measured with seq_len = 2,048

Starter code (CUDA)

#include <cuda_runtime.h>

// x, output, weights, cos, sin are device pointers
extern "C" void solve(const float* x, float* output, const float* weights, const float* cos,
                      const float* sin, int seq_len) {}

Solve it on your GPU

Previous: Decaying Causal AttentionNext: SSM Selective Scan

From LeetGPU, CC BY-NC-ND 4.0.