rohitg00/ai-engineering-from-scratch · error · ValueError
token_ids too short for the requested context_length
Error message
token_ids too short for the requested context_length
What it means
Error "token_ids too short for the requested context_length" thrown in rohitg00/ai-engineering-from-scratch.
Source
Thrown at phases/19-capstone-projects/36-training-loop-eval/code/main.py:195
x = block(x)
return self.lm_head(self.final_ln(x))
def make_batches(
token_ids: torch.Tensor,
batch_size: int,
context_length: int,
seed: int = 0,
) -> Iterator[tuple[torch.Tensor, torch.Tensor]]:
"""Yield (input, target) batches where target is input shifted by one position.
Sampling is uniform random over valid start positions. With a fixed seed the
sequence of batches is reproducible across runs.
"""
if token_ids.dim() != 1:
raise ValueError("token_ids must be a 1D tensor")
if token_ids.numel() < context_length + 1:
raise ValueError("token_ids too short for the requested context_length")
generator = torch.Generator().manual_seed(seed)
max_start = token_ids.numel() - context_length - 1
while True:
starts = torch.randint(0, max_start + 1, (batch_size,), generator=generator)
inputs = torch.stack([token_ids[s : s + context_length] for s in starts.tolist()])
targets = torch.stack(
[token_ids[s + 1 : s + 1 + context_length] for s in starts.tolist()]
)
yield inputs, targets
def calc_loss_batch(
model: GPTModel,
inputs: torch.Tensor,
targets: torch.Tensor,
) -> torch.Tensor:View on GitHub (pinned to 39ea8a1c6d)
When it happens
Trigger: Thrown at phases/19-capstone-projects/36-training-loop-eval/code/main.py:195 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26).
Data as JSON: /api/errors/d49fc7f68d27da99.
Report an issue: GitHub.