TensorTonic
Study Plans
Study Plan
Find Reusable Prefixes in a KV Trie
Language ModelsInference
Medium

Find the longest reusable KV-cache prefix for each requested token sequence. The root has node ID zero. Cached sequences are inserted in input order, with tokens processed from left to right. Each previously unseen token edge receives the next node ID, starting at one; duplicate sequences and shared prefixes reuse existing nodes.

A request matches consecutive edges from the root until its first missing token, even if the matched prefix ends inside a longer cached sequence. With no match, the cache node is zero and the entire request is uncached. An empty cached sequence adds no nodes; an empty request matches zero tokens at the root.

Return a Python dictionary with matches, a list in request order. Each match is a dictionary containing matched_length and cache_node as Python integers, plus uncached_suffix as a list of the remaining token integers.

Loading visualization...

Examples

Input: cached_sequences = [[1,2],[1,2,3],[4]], requests = [[1,2,9],[4,5],[5]]

Output: {"matches":[{"matched_length":2,"cache_node":2,"uncached_suffix":[9]},{"matched_length":1,"cache_node":4,"uncached_suffix":[5]},{"matched_length":0,"cache_node":0,"uncached_suffix":[5]}]}

Explanation: Insertion creates nodes 1 and 2 on the first branch, node 3 below it, and node 4 for token 4.

Input: cached_sequences = [[7],[7]], requests = [[7,8]]

Output: {"matches":[{"matched_length":1,"cache_node":1,"uncached_suffix":[8]}]}

Input: cached_sequences = [[9]], requests = [[2,2,2]]

Output: {"matches":[{"matched_length":0,"cache_node":0,"uncached_suffix":[2,2,2]}]}

Hint 1

A dictionary can map a token to a child node ID.

Hint 2

List length can supply the next sequential node ID.

Hint 3

List slicing extracts the unmatched suffix.

Requirements

  • Use Python lists, dictionaries, and integer node and token IDs.
  • Preserve deterministic node allocation and request order.
  • Do not modify cached sequences or requests.

Constraints

  • cached_sequences and requests are Python lists of lists of nonnegative integer tokens.
  • Either outer list and any individual sequence may be empty.
  • Repeated tokens, duplicate sequences, and nested prefixes are valid.
Try Similar Problems
Route Cache-Aware Prefill RequestsHardSimulate Continuous Decode BatchingHardPlan KV-Cache CapacityMediumMeasure Kernel Timeline UtilizationMediumDecode Attention with a KV CacheHardPerform Exact Speculative SamplingHardTrain a BPE Tokenizer From Scratch in PythonMediumCanonicalize Pairwise Preference DataMediumImplement Prefix Cache Matching and ReuseMediumBeam Search DecodingMediumText Generation SamplingMediumImplement Temperature, Top-k, and Top-p SamplingMedium

Accepts: array

Accepts: array

You must run your code first.

Find Reusable Prefixes in a KV Trie

Language ModelsInference
Medium

Find the longest reusable KV-cache prefix for each requested token sequence. The root has node ID zero. Cached sequences are inserted in input order, with tokens processed from left to right. Each previously unseen token edge receives the next node ID, starting at one; duplicate sequences and shared prefixes reuse existing nodes.

A request matches consecutive edges from the root until its first missing token, even if the matched prefix ends inside a longer cached sequence. With no match, the cache node is zero and the entire request is uncached. An empty cached sequence adds no nodes; an empty request matches zero tokens at the root.

Return a Python dictionary with matches, a list in request order. Each match is a dictionary containing matched_length and cache_node as Python integers, plus uncached_suffix as a list of the remaining token integers.

Loading visualization...

Examples

Input: cached_sequences = [[1,2],[1,2,3],[4]], requests = [[1,2,9],[4,5],[5]]

Output: {"matches":[{"matched_length":2,"cache_node":2,"uncached_suffix":[9]},{"matched_length":1,"cache_node":4,"uncached_suffix":[5]},{"matched_length":0,"cache_node":0,"uncached_suffix":[5]}]}

Explanation: Insertion creates nodes 1 and 2 on the first branch, node 3 below it, and node 4 for token 4.

Input: cached_sequences = [[7],[7]], requests = [[7,8]]

Output: {"matches":[{"matched_length":1,"cache_node":1,"uncached_suffix":[8]}]}

Input: cached_sequences = [[9]], requests = [[2,2,2]]

Output: {"matches":[{"matched_length":0,"cache_node":0,"uncached_suffix":[2,2,2]}]}

Hint 1

A dictionary can map a token to a child node ID.

Hint 2

List length can supply the next sequential node ID.

Hint 3

List slicing extracts the unmatched suffix.

Requirements

  • Use Python lists, dictionaries, and integer node and token IDs.
  • Preserve deterministic node allocation and request order.
  • Do not modify cached sequences or requests.

Constraints

  • cached_sequences and requests are Python lists of lists of nonnegative integer tokens.
  • Either outer list and any individual sequence may be empty.
  • Repeated tokens, duplicate sequences, and nested prefixes are valid.
Try Similar Problems
Route Cache-Aware Prefill RequestsHardSimulate Continuous Decode BatchingHardPlan KV-Cache CapacityMediumMeasure Kernel Timeline UtilizationMediumDecode Attention with a KV CacheHardPerform Exact Speculative SamplingHardTrain a BPE Tokenizer From Scratch in PythonMediumCanonicalize Pairwise Preference DataMediumImplement Prefix Cache Matching and ReuseMediumBeam Search DecodingMediumText Generation SamplingMediumImplement Temperature, Top-k, and Top-p SamplingMedium

Accepts: array

Accepts: array

You must run your code first.