← 返回 nvidia 的题目列表Implement a Decoder KV Cache
类型:online_judge
Implement a Decoder KV Cache
Implement a KV cache for a single-layer, single-batch Transformer decoder. The key and value of each token are integer vectors of length D.
The cache is initially empty. Process Q operations:
APPEND k1 ... kD v1 ... vD
Append one token's key vector [k1, ..., kD] and value vector [v1, ..., vD] to the end of the KV cache.
GET
Print all cached keys and values.
For every cached token, in insertion order, print one line in the form k1 ... kD | v1 ... vD.
Print EMPTY if the cache is empty.
Input Format
D Q
<operation 1>
<operation 2>
...
<operation Q>
1 <= D <= 64
1 <= Q <= 2 * 10^5
Integers in APPEND are in [-10^9, 10^9].
There is at least one GET operation.
Output Format
For each GET, print the complete KV cache at that point. Print an additional line containing --- between outputs of consecutive GET operations.
Example
Input:
2 4
APPEND 1 2 10 20
APPEND 3 4 30 40
GET
GET
Output:
1 2 | 10 20
3 4 | 30 40
---
1 2 | 10 20
3 4 | 30 40
Implement efficient append and retrieval logic.
Example
Input
1 3
GET
APPEND 5 9
GET
Output
EMPTY
---
5 | 9