Grouped-Query Attention with a KV Cache
Problem statement
Compute one projection-free grouped-query scaled dot-product attention pass using an existing key-value cache plus newly generated key and value rows.
query has width queryHeads * headDim. Every cached or new key and value row has width kvHeads * headDim. Append the new rows after the cached rows for the attention calculation. queryHeads is divisible by kvHeads; each consecutive group of queryHeads / kvHeads query heads shares one key-value head.
Examples
Example 1
query = [[1]]cachedKey = []cachedValue = []newKey = [[0],[1]]newValue = [[10],[20]]queryHeads = 1kvHeads = 1return = ["17.310586"]With one head and no cache, this is ordinary scaled dot-product attention.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 2 more worked examples, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.