rohitg00/ai-engineering-from-scratch · error · ValueError
kv_cache must be (B,H,Nv,hd)={expected}, got k={tuple(k.shap
Error message
kv_cache must be (B,H,Nv,hd)={expected}, got k={tuple(k.shape)} v={tuple(v.shape)} What it means
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.
Source
Thrown at phases/19-capstone-projects/61-cross-attention-fusion/code/main.py:124
kv_cache: tuple[torch.Tensor, torch.Tensor] | None = None
) -> torch.Tensor:
if x.dim() != 3:
raise ValueError(f"expected (B, Nt, hidden), got {tuple(x.shape)}")
if memory.shape[0] != x.shape[0]:
raise ValueError(
f"batch mismatch: text {x.shape[0]} vs memory {memory.shape[0]}"
)
b, nt, d = x.shape
h, hd = self.cfg.heads, self.cfg.head_dim
q = self.q_proj(x).reshape(b, nt, h, hd).transpose(1, 2)
if kv_cache is None:
k, v = self.project_memory(memory)
else:
k, v = kv_cache
expected = (b, h, memory.shape[1], hd)
if k.shape != expected or v.shape != expected:
raise ValueError(
f"kv_cache must be (B,H,Nv,hd)={expected}, got "
f"k={tuple(k.shape)} v={tuple(v.shape)}"
)
scores = (q @ k.transpose(-2, -1)) * self.scale
attn = F.softmax(scores, dim=-1)
out = (attn @ v).transpose(1, 2).reshape(b, nt, d)
return self.drop(self.out(out))
class FeedForward(nn.Module):
def __init__(self, cfg: DecoderConfig) -> None:
super().__init__()
inner = int(cfg.hidden * cfg.mlp_ratio)
self.fc1 = nn.Linear(cfg.hidden, inner)
self.fc2 = nn.Linear(inner, cfg.hidden)
def forward(self, x: torch.Tensor) -> torch.Tensor:View on GitHub (pinned to 39ea8a1c6d)
When it happens
Trigger: Thrown at phases/19-capstone-projects/61-cross-attention-fusion/code/main.py:124 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/4fe0ccb57fb4232c.
Report an issue: GitHub.