TensorTonic
Problems
KV Cache Append in Triton
InferenceTransformersGPU Programming
Easy

Implement the KV cache append step used during autoregressive decoding. Given a single new key vector knewk_\text{new}knew​ and value vector vnewv_\text{new}vnew​ of shape (D,)(D,)(D,), write them into row pos\text{pos}pos of the pre-allocated caches in place:

Kcache[pos,:]←knew,Vcache[pos,:]←vnewK_\text{cache}[\text{pos}, :] \leftarrow k_\text{new}, \qquad V_\text{cache}[\text{pos}, :] \leftarrow v_\text{new}Kcache​[pos,:]←knew​,Vcache​[pos,:]←vnew​

This is the per-step scatter that grows the cache by one token during inference. There is no separate output tensor: the kernel mutates the caches in place. The pattern is a 1D map along DDD, partitioned into tiles so larger embedding dimensions stay safe with a tail mask.

Loading visualization...

Examples

Input: k_new = [1.0, 2.0], v_new = [3.0, 4.0], k_cache = [[0.0, 0.0], [0.0, 0.0]], v_cache = [[0.0, 0.0], [0.0, 0.0]], pos = 0

Output: k_cache = [[1.0, 2.0], [0.0, 0.0]], v_cache = [[3.0, 4.0], [0.0, 0.0]]

Input: k_new = [5.0, 6.0, 7.0], v_new = [8.0, 9.0, 10.0], k_cache = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], v_cache = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], pos = 1

Output: k_cache = [[0.0, 0.0, 0.0], [5.0, 6.0, 7.0], [0.0, 0.0, 0.0]], v_cache = [[0.0, 0.0, 0.0], [8.0, 9.0, 10.0], [0.0, 0.0, 0.0]]

Hint 1

Each program handles one tile along DDD. The row offset into the caches is pos⋅D+offsets\text{pos} \cdot D + \text{offsets}pos⋅D+offsets where offsets\text{offsets}offsets is the per-tile index vector.

Hint 2

Read knewk_\text{new}knew​ and vnewv_\text{new}vnew​ at the tile offsets and write them at the row-offset positions in KcacheK_\text{cache}Kcache​ and VcacheV_\text{cache}Vcache​. Both pairs of reads and writes share the same tail mask.

Hint 3

The kernel must not touch any row other than pos\text{pos}pos. The harness checks the whole cache, so unmodified rows must equal their original values.

Requirements

  • Use a 1D grid over the DDD axis; each program copies one tile of row pos\text{pos}pos
  • Mutate the caches in place; there is no separate output tensor
  • Mask the tail so a DDD not divisible by the block size is safe
  • Touch only row pos\text{pos}pos; leave every other row of the caches unchanged
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤Tmax≤40961 \le T_\text{max} \le 40961≤Tmax​≤4096, 1≤D≤40961 \le D \le 40961≤D≤4096, 0≤pos<Tmax0 \le \texttt{pos} < T_\text{max}0≤pos<Tmax​.
  • All tensors are torch.float32 on cuda.
  • Tolerance: atol=1e-3 on the full caches after the write.
  • The kernel mutates k_cache and v_cache in place; no out tensor is passed.
Try Similar Problems
Scaled Dot-Product Attention in TritonMediumRotary Position Embedding in TritonMediumOnline Softmax Kernel in TritonMediumFlashAttention Forward Kernel in TritonHardDropout (Inverted Scaling) in TritonEasyVector Sum Reduction in TritonEasyVector Addition in TritonEasyFused Multiply-Add in TritonEasyVector Max Reduction in TritonEasyGELU in TritonEasyReLU in TritonEasySiLU in TritonEasy

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: number

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

KV Cache Append in Triton

KV Cache Append in Triton

InferenceTransformersGPU Programming
Easy

Implement the KV cache append step used during autoregressive decoding. Given a single new key vector knewk_\text{new}knew​ and value vector vnewv_\text{new}vnew​ of shape (D,)(D,)(D,), write them into row pos\text{pos}pos of the pre-allocated caches in place:

Kcache[pos,:]←knew,Vcache[pos,:]←vnewK_\text{cache}[\text{pos}, :] \leftarrow k_\text{new}, \qquad V_\text{cache}[\text{pos}, :] \leftarrow v_\text{new}Kcache​[pos,:]←knew​,Vcache​[pos,:]←vnew​

This is the per-step scatter that grows the cache by one token during inference. There is no separate output tensor: the kernel mutates the caches in place. The pattern is a 1D map along DDD, partitioned into tiles so larger embedding dimensions stay safe with a tail mask.

Loading visualization...

Examples

Input: k_new = [1.0, 2.0], v_new = [3.0, 4.0], k_cache = [[0.0, 0.0], [0.0, 0.0]], v_cache = [[0.0, 0.0], [0.0, 0.0]], pos = 0

Output: k_cache = [[1.0, 2.0], [0.0, 0.0]], v_cache = [[3.0, 4.0], [0.0, 0.0]]

Input: k_new = [5.0, 6.0, 7.0], v_new = [8.0, 9.0, 10.0], k_cache = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], v_cache = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], pos = 1

Output: k_cache = [[0.0, 0.0, 0.0], [5.0, 6.0, 7.0], [0.0, 0.0, 0.0]], v_cache = [[0.0, 0.0, 0.0], [8.0, 9.0, 10.0], [0.0, 0.0, 0.0]]

Hint 1

Each program handles one tile along DDD. The row offset into the caches is pos⋅D+offsets\text{pos} \cdot D + \text{offsets}pos⋅D+offsets where offsets\text{offsets}offsets is the per-tile index vector.

Hint 2

Read knewk_\text{new}knew​ and vnewv_\text{new}vnew​ at the tile offsets and write them at the row-offset positions in KcacheK_\text{cache}Kcache​ and VcacheV_\text{cache}Vcache​. Both pairs of reads and writes share the same tail mask.

Hint 3

The kernel must not touch any row other than pos\text{pos}pos. The harness checks the whole cache, so unmodified rows must equal their original values.

Requirements

  • Use a 1D grid over the DDD axis; each program copies one tile of row pos\text{pos}pos
  • Mutate the caches in place; there is no separate output tensor
  • Mask the tail so a DDD not divisible by the block size is safe
  • Touch only row pos\text{pos}pos; leave every other row of the caches unchanged
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤Tmax≤40961 \le T_\text{max} \le 40961≤Tmax​≤4096, 1≤D≤40961 \le D \le 40961≤D≤4096, 0≤pos<Tmax0 \le \texttt{pos} < T_\text{max}0≤pos<Tmax​.
  • All tensors are torch.float32 on cuda.
  • Tolerance: atol=1e-3 on the full caches after the write.
  • The kernel mutates k_cache and v_cache in place; no out tensor is passed.
Try Similar Problems
Scaled Dot-Product Attention in TritonMediumRotary Position Embedding in TritonMediumOnline Softmax Kernel in TritonMediumFlashAttention Forward Kernel in TritonHardDropout (Inverted Scaling) in TritonEasyVector Sum Reduction in TritonEasyVector Addition in TritonEasyFused Multiply-Add in TritonEasyVector Max Reduction in TritonEasyGELU in TritonEasyReLU in TritonEasySiLU in TritonEasy

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: number

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1