TensorTonic
Study Plans
Study Plan
Decode Attention with a KV Cache
Language ModelsTransformersInference
Hard

Compute one grouped-query attention decode step and return the extended key-value (KV) caches. Let BBB be batch size, HqH_qHq​ the query-head count, HkvH_{kv}Hkv​ the KV-head count, DDD the head width, and SSS the cached sequence length. Query has shape (B,Hq,D)(B,H_q,D)(B,Hq​,D), each new key or value has shape (B,Hkv,D)(B,H_{kv},D)(B,Hkv​,D), and each cache has shape (B,Hkv,S,D)(B,H_{kv},S,D)(B,Hkv​,S,D). Each KV head serves a contiguous group of query heads of size:

G=HqHkv.G=\frac{H_q}{H_{kv}}.G=Hkv​Hq​​.

The new key and value occupy position SSS after all cached positions. Let bbb index a batch item, hhh a KV head, ggg a query within its group, and sss a cache position. Query qb,h,gq_{b,h,g}qb,h,g​ is original query head hG+ghG+ghG+g; kb,h,sk_{b,h,s}kb,h,s​ and vb,h,sv_{b,h,s}vb,h,s​ are the extended key and value vectors. Attention weights aaa and output vectors yyy are:

ab,h,g,s=softmax⁡s(qb,h,gTkb,h,sD).a_{b,h,g,s}=\operatorname{softmax}_s\left(\frac{q_{b,h,g}^{\mathsf T}k_{b,h,s}}{\sqrt D}\right).ab,h,g,s​=softmaxs​(D​qb,h,gT​kb,h,s​​). yb,h,g=∑s=0Sab,h,g,svb,h,s.y_{b,h,g}=\sum_{s=0}^{S}a_{b,h,g,s}v_{b,h,s}.yb,h,g​=s=0∑S​ab,h,g,s​vb,h,s​.

Softmax is over all cache positions, including the new token. Return a Python dictionary with output, a PyTorch tensor of shape (B, Hq, D), and new_key_cache and new_value_cache, PyTorch tensors of shape (B, Hkv, S + 1, D). All three preserve the input dtype and device.

Loading visualization...

Examples

Input: query = [[[0.1,0.2]]], new_key = [[[0.2,0.4]]], new_value = [[[0.3,0.6]]], key_cache = [[[[0.2,0.4]]]], value_cache = [[[[0.1,0.2]]]], num_query_heads = 1, num_kv_heads = 1

Output: {"output":[[[0.2,0.4]]],"new_key_cache":[[[[0.2,0.4],[0.2,0.4]]]],"new_value_cache":[[[[0.1,0.2],[0.3,0.6]]]]}

Explanation: The two identical keys receive equal attention weights, so the output is the average of their values.

Input: query = [[[0]]], new_key = [[[2]]], new_value = [[[6]]], key_cache = [[[[1]]]], value_cache = [[[[2]]]], num_query_heads = 1, num_kv_heads = 1

Output: {"output":[[[4]]],"new_key_cache":[[[[1],[2]]]],"new_value_cache":[[[[2],[6]]]]}

Input: query = [[[0],[1]]], new_key = [[[1.09861229]]], new_value = [[[6]]], key_cache = [[[[0]]]], value_cache = [[[[2]]]], num_query_heads = 2, num_kv_heads = 1

Output: {"output":[[[4],[5]]],"new_key_cache":[[[[0],[1.098612]]]],"new_value_cache":[[[[2],[6]]]]}

Hint 1

torch.cat joins tensors along an existing dimension.

Hint 2

torch.matmul broadcasts leading batch dimensions.

Hint 3

torch.softmax accepts the axis to normalize.

Requirements

  • Use PyTorch, with float32 attention arithmetic before restoring the input dtype.
  • Share each KV head across its contiguous query-head group without constructing repeated key or value heads.
  • Preserve existing cache order and do not modify input tensors.

Constraints

  • query has shape (B, Hq, D); new_key and new_value have shape (B, Hkv, D).
  • key_cache and value_cache have shape (B, Hkv, S, D), where S may be zero.
  • num_query_heads equals Hq, num_kv_heads equals Hkv, and Hkv divides Hq.
  • All tensors share dtype and device; supported dtypes are float32, float64, and bfloat16.
  • Inputs contain finite values and may be noncontiguous.
Try Similar Problems
Perform Exact Speculative SamplingHardPlan KV-Cache CapacityMediumRoute Cache-Aware Prefill RequestsHardCausal Grouped-Query AttentionHardSimulate Continuous Decode BatchingHardFind Reusable Prefixes in a KV TrieMediumAssign Multimodal RoPE CoordinatesMediumRoPE: Rotary Query and Key EmbeddingsMediumImplement Speculative Decoding and Token VerificationHardImplement PagedAttention Block AllocationHardImplement FlashAttention with Online SoftmaxHardBeam Search DecodingHard

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.

Decode Attention with a KV Cache

Language ModelsTransformersInference
Hard

Compute one grouped-query attention decode step and return the extended key-value (KV) caches. Let BBB be batch size, HqH_qHq​ the query-head count, HkvH_{kv}Hkv​ the KV-head count, DDD the head width, and SSS the cached sequence length. Query has shape (B,Hq,D)(B,H_q,D)(B,Hq​,D), each new key or value has shape (B,Hkv,D)(B,H_{kv},D)(B,Hkv​,D), and each cache has shape (B,Hkv,S,D)(B,H_{kv},S,D)(B,Hkv​,S,D). Each KV head serves a contiguous group of query heads of size:

G=HqHkv.G=\frac{H_q}{H_{kv}}.G=Hkv​Hq​​.

The new key and value occupy position SSS after all cached positions. Let bbb index a batch item, hhh a KV head, ggg a query within its group, and sss a cache position. Query qb,h,gq_{b,h,g}qb,h,g​ is original query head hG+ghG+ghG+g; kb,h,sk_{b,h,s}kb,h,s​ and vb,h,sv_{b,h,s}vb,h,s​ are the extended key and value vectors. Attention weights aaa and output vectors yyy are:

ab,h,g,s=softmax⁡s(qb,h,gTkb,h,sD).a_{b,h,g,s}=\operatorname{softmax}_s\left(\frac{q_{b,h,g}^{\mathsf T}k_{b,h,s}}{\sqrt D}\right).ab,h,g,s​=softmaxs​(D​qb,h,gT​kb,h,s​​). yb,h,g=∑s=0Sab,h,g,svb,h,s.y_{b,h,g}=\sum_{s=0}^{S}a_{b,h,g,s}v_{b,h,s}.yb,h,g​=s=0∑S​ab,h,g,s​vb,h,s​.

Softmax is over all cache positions, including the new token. Return a Python dictionary with output, a PyTorch tensor of shape (B, Hq, D), and new_key_cache and new_value_cache, PyTorch tensors of shape (B, Hkv, S + 1, D). All three preserve the input dtype and device.

Loading visualization...

Examples

Input: query = [[[0.1,0.2]]], new_key = [[[0.2,0.4]]], new_value = [[[0.3,0.6]]], key_cache = [[[[0.2,0.4]]]], value_cache = [[[[0.1,0.2]]]], num_query_heads = 1, num_kv_heads = 1

Output: {"output":[[[0.2,0.4]]],"new_key_cache":[[[[0.2,0.4],[0.2,0.4]]]],"new_value_cache":[[[[0.1,0.2],[0.3,0.6]]]]}

Explanation: The two identical keys receive equal attention weights, so the output is the average of their values.

Input: query = [[[0]]], new_key = [[[2]]], new_value = [[[6]]], key_cache = [[[[1]]]], value_cache = [[[[2]]]], num_query_heads = 1, num_kv_heads = 1

Output: {"output":[[[4]]],"new_key_cache":[[[[1],[2]]]],"new_value_cache":[[[[2],[6]]]]}

Input: query = [[[0],[1]]], new_key = [[[1.09861229]]], new_value = [[[6]]], key_cache = [[[[0]]]], value_cache = [[[[2]]]], num_query_heads = 2, num_kv_heads = 1

Output: {"output":[[[4],[5]]],"new_key_cache":[[[[0],[1.098612]]]],"new_value_cache":[[[[2],[6]]]]}

Hint 1

torch.cat joins tensors along an existing dimension.

Hint 2

torch.matmul broadcasts leading batch dimensions.

Hint 3

torch.softmax accepts the axis to normalize.

Requirements

  • Use PyTorch, with float32 attention arithmetic before restoring the input dtype.
  • Share each KV head across its contiguous query-head group without constructing repeated key or value heads.
  • Preserve existing cache order and do not modify input tensors.

Constraints

  • query has shape (B, Hq, D); new_key and new_value have shape (B, Hkv, D).
  • key_cache and value_cache have shape (B, Hkv, S, D), where S may be zero.
  • num_query_heads equals Hq, num_kv_heads equals Hkv, and Hkv divides Hq.
  • All tensors share dtype and device; supported dtypes are float32, float64, and bfloat16.
  • Inputs contain finite values and may be noncontiguous.
Try Similar Problems
Perform Exact Speculative SamplingHardPlan KV-Cache CapacityMediumRoute Cache-Aware Prefill RequestsHardCausal Grouped-Query AttentionHardSimulate Continuous Decode BatchingHardFind Reusable Prefixes in a KV TrieMediumAssign Multimodal RoPE CoordinatesMediumRoPE: Rotary Query and Key EmbeddingsMediumImplement Speculative Decoding and Token VerificationHardImplement PagedAttention Block AllocationHardImplement FlashAttention with Online SoftmaxHardBeam Search DecodingHard

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.