{"record":{"id":"8fd680413bf0134a","repo":"rohitg00/ai-engineering-from-scratch","slug":"input-must-be-b-t-d-got-shape-tuple-x-shape","errorCode":null,"errorMessage":"input must be (B, T, D), got shape {tuple(x.shape)}","messagePattern":"input must be \\(B, T, D\\), got shape (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/33-multihead-self-attention/code/main.py","lineNumber":69,"sourceCode":"\n        causal_mask = torch.tril(torch.ones(max_context_length, max_context_length))\n        self.register_buffer(\"causal_mask\", causal_mask, persistent=False)\n\n    def _split_heads(self, x: torch.Tensor) -> torch.Tensor:\n        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)","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/33-multihead-self-attention/code/main.py#L51-L87","documentation":"Error \"input must be (B, T, D), got shape {tuple(x.shape)}\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/19-capstone-projects/33-multihead-self-attention/code/main.py:69 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"}