REAL PYTORCH · CPU TRAINING AND AUTOGRAD
Decode chunks with an owned, offset-aware KV cache
A cached query begins after the keys already stored.
Run this Lab in Pythilo
Open Labs, choose its topic and tap Download entire topic, or Download entire category for all its Labs, including the required Python packages, then load the starter or worked answer and tap Trace. Progress, pause, resume and removal are in Downloads. Independent checks execute real CPU training and gradients.
No GPU, external model weights or API key is required. After downloading the topic and packages, this Lab runs offline.
PyTorch is unavailable in this browser runtime. The code and lessons are readable here; execution and checks run in the app.
Pythilo on the App Store →Read lesson: Decode chunks with an owned, offset-aware KV cache →
Your task
Inference-only q,k,v have the same nonempty [B,H,T,D] shape. Optional cache has exactly k/v with matching batch/head/width/dtype/device. Append current keys and values and admit key position <= past_length+query_index. Return output and a detached cloned k/v cache; do not mutate or alias old cache or new input chunks. Full, tokenwise and multi-token chunked execution must agree. No dropout or positional transform is applied here.
Starter
import torch
def cached_attention(q, k, v, cache=None):
return NoneInspect the worked answer
import math
import torch
@torch.no_grad()
def cached_attention(q, k, v, cache=None):
if q.ndim != 4 or q.shape != k.shape or k.shape != v.shape or min(q.shape) < 1:
raise ValueError('aligned nonempty [B,H,T,D] chunks required')
past = 0
if cache is not None:
if set(cache) != {'k', 'v'} or cache['k'].shape != cache['v'].shape or cache['k'].ndim != 4:
raise ValueError('owned key/value cache required')
old = cache['k']
if old.shape[:2] != k.shape[:2] or old.shape[-1] != k.shape[-1] or old.dtype != k.dtype or old.device != k.device:
raise ValueError('cache and chunk dimensions must agree')
past = old.shape[2]
keys = torch.cat((cache['k'], k), dim=2)
values = torch.cat((cache['v'], v), dim=2)
else:
keys, values = k, v
query_positions = torch.arange(past, past + q.shape[2], device=q.device)
key_positions = torch.arange(keys.shape[2], device=q.device)
allow = key_positions[None, :] <= query_positions[:, None]
scores = q @ keys.transpose(-2, -1) / math.sqrt(q.shape[-1])
weights = torch.softmax(scores.masked_fill(~allow, float('-inf')), dim=-1)
return weights @ values, {'k': keys.detach().clone(), 'v': values.detach().clone()}