{"record":{"id":"1cae374e13f342c0","repo":"rohitg00/ai-engineering-from-scratch","slug":"d-model-d-model-must-be-divisible-by-n-heads","errorCode":null,"errorMessage":"d_model ({d_model}) must be divisible by n_heads ({n_heads})","messagePattern":"d_model \\((.+?)\\) must be divisible by n_heads \\((.+?)\\)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/33-multihead-self-attention/code/main.py","lineNumber":37,"sourceCode":"\nclass MultiHeadSelfAttention(nn.Module):\n    \"\"\"Multi-head self-attention with a single QKV linear and a causal mask.\"\"\"\n\n    def __init__(\n        self,\n        d_model: int,\n        n_heads: int,\n        max_context_length: int,\n        attn_dropout: float = 0.0,\n        out_dropout: float = 0.0,\n    ) -> None:\n        super().__init__()\n        if d_model < 1:\n            raise ValueError(f\"d_model must be >= 1, got {d_model}\")\n        if n_heads < 1:\n            raise ValueError(f\"n_heads must be >= 1, got {n_heads}\")\n        if d_model % n_heads != 0:\n            raise ValueError(\n                f\"d_model ({d_model}) must be divisible by n_heads ({n_heads})\"\n            )\n        if max_context_length < 1:\n            raise ValueError(f\"max_context_length must be >= 1, got {max_context_length}\")\n        self.d_model = d_model\n        self.n_heads = n_heads\n        self.d_head = d_model // n_heads\n        self.max_context_length = max_context_length\n\n        self.qkv_proj = nn.Linear(d_model, 3 * d_model, bias=True)\n        self.out_proj = nn.Linear(d_model, d_model, bias=True)\n        self.attn_dropout = nn.Dropout(attn_dropout)\n        self.out_dropout = nn.Dropout(out_dropout)\n\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:","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/33-multihead-self-attention/code/main.py#L19-L55","documentation":"Error \"d_model ({d_model}) must be divisible by n_heads ({n_heads})\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/19-capstone-projects/33-multihead-self-attention/code/main.py:37 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"}