Skip to main content

Module hash

Module hash 

Source
Expand description

Stable BLAKE3 prefix hashing used by the router and KV daemon.

Two flavours are exposed:

  • hash_chunks — independent per-chunk digests. Cheap. Correct only when KV reuse cares about content overlap and not position.
  • hash_seq_chunks — sequence-chained digests. Each block is salted with the prior block’s digest so that the hash for chunk N encodes the entire prefix tokens[0..N*TOKENS_PER_CHUNK]. This matches what actual KV caches require: the K/V tensors at position P depend on every token at positions <P, so two requests with the same suffix but different prefixes must produce different digests at every position past the divergence point. The router uses this variant to compute longest-prefix overlap; the resulting score is what determines whether a node can skip a prefill.

Both variants are model-salted.

Constants§

TOKENS_PER_CHUNK
Number of token IDs hashed per chunk. Keeps the prefix-trie depth bounded while still letting cache hits accumulate at sub-prompt granularity.

Functions§

hash_chunk
Hash a window of token ids into a deterministic 32-byte digest.
hash_chunks
Walk a full token sequence and yield one digest per TOKENS_PER_CHUNK rolling chunk. The last (partial) chunk is also emitted because it is often the most cache-relevant.
hash_seq_chunk
Sequence-chained digest for one chunk. parent is the digest of the prior chunk, or all-zero for the first chunk.
hash_seq_chunks
Walk a full token sequence and yield one sequence-chained digest per chunk. Chunk N’s digest depends on chunks 0..=N, so the digest at position N uniquely identifies the prefix tokens[0..(N+1)*TOKENS_PER_CHUNK].
short
Format a digest as lowercase hex (16 chars of prefix → 8 bytes is plenty).