{"record":{"id":"4fe0ccb57fb4232c","repo":"rohitg00/ai-engineering-from-scratch","slug":"kv-cache-must-be-b-h-nv-hd-expected-got-k-tu","errorCode":null,"errorMessage":"kv_cache must be (B,H,Nv,hd)={expected}, got k={tuple(k.shape)} v={tuple(v.shape)}","messagePattern":"kv_cache must be \\(B,H,Nv,hd\\)=(.+?), got k=(.+?) v=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/61-cross-attention-fusion/code/main.py","lineNumber":124,"sourceCode":"                kv_cache: tuple[torch.Tensor, torch.Tensor] | None = None\n                ) -> torch.Tensor:\n        if x.dim() != 3:\n            raise ValueError(f\"expected (B, Nt, hidden), got {tuple(x.shape)}\")\n        if memory.shape[0] != x.shape[0]:\n            raise ValueError(\n                f\"batch mismatch: text {x.shape[0]} vs memory {memory.shape[0]}\"\n            )\n        b, nt, d = x.shape\n        h, hd = self.cfg.heads, self.cfg.head_dim\n\n        q = self.q_proj(x).reshape(b, nt, h, hd).transpose(1, 2)\n        if kv_cache is None:\n            k, v = self.project_memory(memory)\n        else:\n            k, v = kv_cache\n            expected = (b, h, memory.shape[1], hd)\n            if k.shape != expected or v.shape != expected:\n                raise ValueError(\n                    f\"kv_cache must be (B,H,Nv,hd)={expected}, got \"\n                    f\"k={tuple(k.shape)} v={tuple(v.shape)}\"\n                )\n\n        scores = (q @ k.transpose(-2, -1)) * self.scale\n        attn = F.softmax(scores, dim=-1)\n        out = (attn @ v).transpose(1, 2).reshape(b, nt, d)\n        return self.drop(self.out(out))\n\n\nclass FeedForward(nn.Module):\n    def __init__(self, cfg: DecoderConfig) -> None:\n        super().__init__()\n        inner = int(cfg.hidden * cfg.mlp_ratio)\n        self.fc1 = nn.Linear(cfg.hidden, inner)\n        self.fc2 = nn.Linear(inner, cfg.hidden)\n\n    def forward(self, x: torch.Tensor) -> torch.Tensor:","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/61-cross-attention-fusion/code/main.py#L106-L142","documentation":"Error \"kv_cache must be (B,H,Nv,hd)={expected}, got k={tuple(k.shape)} v={tuple(v.shape)}\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/19-capstone-projects/61-cross-attention-fusion/code/main.py:124 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"}