rohitg00/ai-engineering-from-scratch · error · ValueError
sequence length {seq} exceeds context_length={self.cfg.conte
Error message
sequence length {seq} exceeds context_length={self.cfg.context_length} What it means
Error "sequence length {seq} exceeds context_length={self.cfg.context_length}" thrown in rohitg00/ai-engineering-from-scratch.
Source
Thrown at phases/19-capstone-projects/37-loading-pretrained-weights/code/main.py:145
self.cfg = cfg
self.tok_embed = nn.Embedding(cfg.vocab_size, cfg.d_model)
self.pos_embed = nn.Embedding(cfg.context_length, cfg.d_model)
self.embed_dropout = nn.Dropout(cfg.dropout)
self.blocks = nn.ModuleList([TransformerBlock(cfg) for _ in range(cfg.num_layers)])
self.final_ln = LayerNorm(cfg.d_model)
self.lm_head = nn.Linear(cfg.d_model, cfg.vocab_size, bias=False)
if cfg.weight_tying:
self.lm_head.weight = self.tok_embed.weight
self.register_buffer(
"position_ids",
torch.arange(cfg.context_length, dtype=torch.long),
persistent=False,
)
def forward(self, tokens: torch.Tensor) -> torch.Tensor:
batch, seq = tokens.shape
if seq > self.cfg.context_length:
raise ValueError(
f"sequence length {seq} exceeds context_length={self.cfg.context_length}"
)
tok = self.tok_embed(tokens)
pos = self.pos_embed(self.position_ids[:seq])
x = self.embed_dropout(tok + pos)
for block in self.blocks:
x = block(x)
return self.lm_head(self.final_ln(x))
@dataclass
class LoadReport:
"""Outcome of a load. Print this; it tells you whether the load succeeded."""
loaded: list[tuple[str, str, tuple[int, ...]]] = field(default_factory=list)
missing: list[str] = field(default_factory=list)
unexpected: list[str] = field(default_factory=list)
shape_mismatch: list[tuple[str, tuple[int, ...], tuple[int, ...]]] = field(default_factory=list)View on GitHub (pinned to 39ea8a1c6d)
When it happens
Trigger: Thrown at phases/19-capstone-projects/37-loading-pretrained-weights/code/main.py:145 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/9fb4567bc488e872.
Report an issue: GitHub.