{"record":{"id":"4ee9aad5fff479a5","repo":"rohitg00/ai-engineering-from-scratch","slug":"d-model-must-be-1-got-d-model-4ee9aa","errorCode":null,"errorMessage":"d_model must be >= 1, got {d_model}","messagePattern":"d_model must be >= 1, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/33-multihead-self-attention/code/main.py","lineNumber":33,"sourceCode":"import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\n\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","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/33-multihead-self-attention/code/main.py#L15-L51","documentation":"Error \"d_model must be >= 1, got {d_model}\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/19-capstone-projects/33-multihead-self-attention/code/main.py:33 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"}