Shift language-model targets instead of predicting the present
A next-token objective pairs the logits at position t with the token at position t+1. Scoring a position against its own input can produce a deceptively easy task and a misleading loss. The final input position has no next token in the provided sequence and must be excluded.
This exercise computes actual cross-entropy on shifted tensors. It leaves attention construction outside the contract: a correct target shift is still invalid if the model's attention can see future tokens. Sequence packing also needs boundary masks so an unrelated document is not silently treated as the continuation of another.
import torch
ids=torch.tensor([[2,4,1]])
print(ids[:,:-1])
print(ids[:,1:])Review the design
Target alignment, causal attention and document-boundary masking are separate correctness conditions. Passing this loss check establishes alignment, not that a complete language-model training pipeline is leak-free.
Your task
Implement next_token_loss(logits,tokens), logits [B,T,C], tokens [B,T], T>=2. Score logits[:,:-1] against tokens[:,1:] with mean cross entropy. Reject T<2.
The explanation opens here once every check passes.