{"record":{"id":"11b2be8fffb9dd8a","repo":"rohitg00/ai-engineering-from-scratch","slug":"sequence-length-seq-exceeds-context-length-self-11b2be","errorCode":null,"errorMessage":"sequence length {seq} exceeds context length {self.cfg.context_length}","messagePattern":"sequence length (.+?) exceeds context length (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/35-gpt-model-assembly/code/main.py","lineNumber":162,"sourceCode":"\n    def _init_weights(self, module: nn.Module) -> None:\n        if isinstance(module, nn.Linear):\n            nn.init.normal_(module.weight, mean=0.0, std=0.02)\n            if module.bias is not None:\n                nn.init.zeros_(module.bias)\n        elif isinstance(module, nn.Embedding):\n            nn.init.normal_(module.weight, mean=0.0, std=0.02)\n\n    def _scale_residual_projections(self) -> None:\n        scale = 1.0 / math.sqrt(2 * self.cfg.num_layers)\n        for block in self.blocks:\n            block.attn.out_proj.weight.data.mul_(scale)\n            block.mlp.fc2.weight.data.mul_(scale)\n\n    def forward(self, tokens: torch.Tensor) -> torch.Tensor:\n        batch, seq = tokens.shape\n        if seq > self.cfg.context_length:\n            raise ValueError(\n                f\"sequence length {seq} exceeds context length {self.cfg.context_length}\"\n            )\n        tok = self.tok_embed(tokens)\n        pos = self.pos_embed(self.position_ids[:seq])\n        x = self.embed_dropout(tok + pos)\n        for block in self.blocks:\n            x = block(x)\n        x = self.final_ln(x)\n        logits = self.lm_head(x)\n        return logits\n\n\ndef count_parameters(model: nn.Module) -> int:\n    \"\"\"Count unique parameters. Weight tied tensors are counted once.\"\"\"\n    seen: dict[int, int] = {}\n    for param in model.parameters():\n        seen[id(param)] = param.numel()\n    return sum(seen.values())","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/35-gpt-model-assembly/code/main.py#L144-L180","documentation":"Error \"sequence length {seq} exceeds context length {self.cfg.context_length}\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/19-capstone-projects/35-gpt-model-assembly/code/main.py:162 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","schemaVersion":2},"datasetVersion":"2026-08-26T07:17:17.940Z"}