rohitg00/ai-engineering-from-scratch · error · ValueError
hidden must divide heads
Error message
hidden must divide heads
What it means
Error "hidden must divide heads" thrown in rohitg00/ai-engineering-from-scratch.
Source
Thrown at phases/19-capstone-projects/41-eval-pipeline/code/main.py:77
if len(ids) > max_len:
ids = ids[:max_len]
return ids
def encode_text(self, text: str, max_len: int) -> List[int]:
ids = list(text.encode("utf-8", errors="ignore"))
if len(ids) > max_len:
ids = ids[:max_len]
return ids
def decode_response(self, ids: Sequence[int]) -> str:
return bytes(i for i in ids if i < 256).decode("utf-8", errors="replace")
class CausalSelfAttention(nn.Module):
def __init__(self, hidden: int, heads: int, max_len: int):
super().__init__()
if hidden % heads != 0:
raise ValueError("hidden must divide heads")
self.heads = heads
self.head_dim = hidden // heads
self.qkv = nn.Linear(hidden, hidden * 3, bias=False)
self.out = nn.Linear(hidden, hidden, bias=False)
mask = torch.tril(torch.ones(max_len, max_len, dtype=torch.bool))
self.register_buffer("causal_mask", mask, persistent=False)
def forward(self, x: torch.Tensor, key_pad_mask: Optional[torch.Tensor] = None) -> torch.Tensor:
B, T, D = x.shape
qkv = self.qkv(x).view(B, T, 3, self.heads, self.head_dim).permute(2, 0, 3, 1, 4)
q, k, v = qkv[0], qkv[1], qkv[2]
att = (q @ k.transpose(-2, -1)) / math.sqrt(self.head_dim)
causal = self.causal_mask[:T, :T].view(1, 1, T, T)
att = att.masked_fill(~causal, float("-inf"))
if key_pad_mask is not None:
km = key_pad_mask.view(B, 1, 1, T).to(torch.bool)
att = att.masked_fill(~km, float("-inf"))
weights = F.softmax(att, dim=-1)View on GitHub (pinned to 39ea8a1c6d)
When it happens
Trigger: Thrown at phases/19-capstone-projects/41-eval-pipeline/code/main.py:77 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26).
Data as JSON: /api/errors/357816e4866ff899.
Report an issue: GitHub.