ruvnet/ruflo · error · Error

FlashAttention: Empty input arrays

Error message

FlashAttention: Empty input arrays

What it means

FlashAttention.validateInputs() runs before every attention computation (attention(), blockAttention(), computeAttention()) and rejects calls where the queries, keys, or values array-of-vectors is empty (zero vectors). Attention over an empty sequence is mathematically undefined, so the library fails fast instead of returning garbage or NaNs.

Source

Thrown at v3/@claude-flow/neural/src/flash-attention.ts:771

        for (let d = 0; d < dimensions; d++) {
          vectors[i][d] /= norm;
        }
      }
    }

    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}`,
      );
    }

View on GitHub (pinned to fa13ee4ad6)

Solutions

  1. Skip the attention call when any input array is empty and return an empty result
  2. Fix the upstream producer so it cannot emit zero vectors
  3. Guard batch loops with `if (batch.length === 0) continue;` before attention

Example fix

// before
const out = computeAttention(queries, keys, values);

// after
if (queries.length === 0 || keys.length === 0 || values.length === 0) {
  return []; // nothing to attend over
}
const out = computeAttention(queries, keys, values);
Defensive patterns

Strategy: validation

Validate before calling

function hasVectors(...sets: Float32Array[][]): boolean {
  return sets.every(
    (s) => Array.isArray(s) && s.length > 0 && s.every((v) => v instanceof Float32Array && v.length > 0),
  );
}
if (!hasVectors(queries, keys, values)) {
  return []; // empty batch - nothing to attend over
}

Prevention

When it happens

Trigger: Passing [] for any of queries/keys/values — e.g. a batching layer that produced an empty batch; a pre-processing filter that removed all tokens/vectors; calling computeAttention() with placeholder arrays before real data has arrived asynchronously.

Common situations: Empty-batch edge cases in dataloaders; race conditions where attention runs before embedding finishes; slicing beyond array bounds yielding empty results.

Related errors


AI-assisted analysis of ruvnet/ruflo@fa13ee4ad6 (2026-08-18). Data as JSON: /api/errors/a98f09f96ffa79a5. Report an issue: GitHub.