{"record":{"id":"2198a674469ededc","repo":"rohitg00/ai-engineering-from-scratch","slug":"seq-len-t-exceeds-max-context-length-self-max-c","errorCode":null,"errorMessage":"seq_len {t} exceeds max_context_length {self.max_context_length}","messagePattern":"seq_len (.+?) exceeds max_context_length (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/33-multihead-self-attention/code/main.py","lineNumber":74,"sourceCode":"        b, t, _ = x.shape\n        return x.view(b, t, self.n_heads, self.d_head).transpose(1, 2)\n\n    def _merge_heads(self, x: torch.Tensor) -> torch.Tensor:\n        b, h, t, dh = x.shape\n        return x.transpose(1, 2).contiguous().view(b, t, h * dh)\n\n    def forward(\n        self,\n        x: torch.Tensor,\n        return_weights: bool = False,\n    ) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]:\n        if x.dim() != 3:\n            raise ValueError(f\"input must be (B, T, D), got shape {tuple(x.shape)}\")\n        b, t, d = x.shape\n        if d != self.d_model:\n            raise ValueError(f\"feature dim {d} != d_model {self.d_model}\")\n        if t > self.max_context_length:\n            raise ValueError(f\"seq_len {t} exceeds max_context_length {self.max_context_length}\")\n\n        qkv = self.qkv_proj(x)\n        q, k, v = qkv.chunk(3, dim=-1)\n\n        q = self._split_heads(q)\n        k = self._split_heads(k)\n        v = self._split_heads(v)\n\n        scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(self.d_head)\n        mask_slice = self.causal_mask[:t, :t]\n        scores = scores.masked_fill(mask_slice == 0, float(\"-inf\"))\n\n        weights = F.softmax(scores, dim=-1)\n        weights = self.attn_dropout(weights)\n\n        context = torch.matmul(weights, v)\n        context = self._merge_heads(context)\n","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/33-multihead-self-attention/code/main.py#L56-L92","documentation":"Error \"seq_len {t} exceeds max_context_length {self.max_context_length}\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/19-capstone-projects/33-multihead-self-attention/code/main.py:74 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"}