A teammate wrote a one-block GPT-style language model and the loop that trains it. The code runs without errors and the loss goes down, but the model is wrong in ways the loss curve does not show. This implementation has 5 bugs. Find and fix them, keeping the class names, function names and signatures.
What each piece must do
CausalSelfAttention(d_model, n_heads, max_len): multi-head self-attention on x of shape (B, T, d_model) with max_len. Each token's d_model features are split into n_heads consecutive chunks of size , one chunk per head. Each head computes
where the causal mask lets position attend to positions (itself included) and to nothing later. The heads are concatenated back per token and passed through proj. Output shape (B, T, d_model).TinyLM(vocab_size, d_model, n_heads, max_len, dropout): token plus position embeddings, one pre-LayerNorm block (attention sublayer, then MLP sublayer, each added to the residual stream after dropout), a final LayerNorm and a linear head. forward(idx) maps integer tokens (B, T) to logits (B, T, vocab_size).lm_loss(model, seq): seq has shape (B, T + 1). The model reads seq[:, :T] and at every position is scored on predicting the token that comes next, seq[:, t + 1]. Returns the mean cross-entropy over all predictions as a scalar tensor.train(model, data, steps, batch_size, lr=1e-2, seed=0): data is a (N, T + 1) tensor of token rows. Puts the model in training mode, creates one AdamW(lr=lr) optimizer and one torch.Generator seeded with seed, and for each step draws batch_size row indices with torch.randint(0, N, (batch_size,), generator=gen) and takes one optimizer step on lm_loss of those rows. Each update must use only the gradient of that step's batch. Returns the per-step losses as a list of Python floats.evaluate(model, seq): the lm_loss of seq as a Python float, computed with dropout off and without tracking gradients. Afterwards the model is back in whichever mode (train or eval) it was in before the call.