ruvnet/ruflo · error · Error
FlashAttention: Keys and values must have same count. Got ${
Error message
FlashAttention: Keys and values must have same count. Got ${keys.length} keys, ${values.length} values What it means
Attention scores every query against every key and mixes the corresponding value, so keys and values must be parallel arrays of the same length; validateInputs() enforces keys.length === values.length and reports both counts when they differ. A mismatch means K and V were built from different sequences — an off-by-one, a bad slice, or caching one side from a previous step.
Source
Thrown at v3/@claude-flow/neural/src/flash-attention.ts:775
}
return vectors;
}
/**
* Validate input arrays
*/
private validateInputs(
queries: Float32Array[],
keys: Float32Array[],
values: Float32Array[],
): void {
if (!queries.length || !keys.length || !values.length) {
throw new Error('FlashAttention: Empty input arrays');
}
if (keys.length !== values.length) {
throw new Error(
`FlashAttention: Keys and values must have same count. Got ${keys.length} keys, ${values.length} values`,
);
}
const qDim = queries[0]?.length ?? 0;
const kDim = keys[0]?.length ?? 0;
const vDim = values[0]?.length ?? 0;
if (qDim !== kDim) {
throw new Error(
`FlashAttention: Query and key dimensions must match. Got Q=${qDim}, K=${kDim}`,
);
}
if (kDim !== vDim) {
throw new Error(
`FlashAttention: Key and value dimensions must match. Got K=${kDim}, V=${vDim}`,
);View on GitHub (pinned to fa13ee4ad6)
Solutions
- Derive keys and values from the same source array so lengths match by construction
- Assert keys.length === values.length before calling computeAttention
- Re-check slice bounds: both must use the same [i, i+n) window
Example fix
// before
const out = computeAttention(q, cachedKeys, freshValues);
// after
if (keys.length !== values.length) {
throw new Error(`K/V count mismatch: ${keys.length} vs ${values.length} - rebuild both from the same window`);
}
const out = computeAttention(q, keys, values); Defensive patterns
Strategy: validation
Validate before calling
if (keys.length !== values.length) {
throw new Error(
`K/V count mismatch: ${keys.length} keys vs ${values.length} values - rebuild both from the same window`,
);
}
const out = computeAttention(queries, keys, values); Prevention
- Derive keys and values from the same array so counts match by construction
- Keep KV-cache extensions symmetric: append to K and V together
- Use identical slice windows for K and V in sliding-window code
When it happens
Trigger: Building keys from one window and values from another (e.g. keys.slice(0, n) vs values.slice(1, n+1)); concatenating past-sequence keys for KV-cache without extending values the same way; example code adapted with different dummy lengths.
Common situations: Sliding-window/KV-cache attention implementations; inconsistent preprocessing of the same token stream; partial refactors that update one array's construction.
Understand the failure class
Background: Tensor shape mismatch errors ("must have shape", "expected shape ... got ..."): when tensor dimensions disagree with what an op or layer was told to expect — this error's family across 6 libraries.
Related errors
- FlashAttention: Empty input arrays
- FlashAttention: Query and key dimensions must match. Got Q=$
- FlashAttention: Key and value dimensions must match. Got K=$
- Expected embedding dimension ${INPUT_DIM}, got ${input.lengt
- Cannot judge incomplete trajectory
AI-assisted analysis of ruvnet/ruflo@fa13ee4ad6 (2026-08-18).
Data as JSON: /api/errors/fe368dcba780ce5c.
Report an issue: GitHub.