In “TorchTPU Performance: Execution Modes, XProf and Pallas Kernels,” Omotayo Aina uses a growing key-value cache to explain why autoregressive decoding can spend time compiling between tokens. The original DEV Community article is at https://dev.to/gde/torchtpu-performance-execution-modes-xprof-and-pallas-kernels-5995
Watch the shape as the cache grows
Each decode step adds key and value states for a new token. If the cache grows by concatenation, the sequence dimension of the input to the next step changes too. Aina compares this pattern with a preallocated cache in the context of TorchTPU 0.2.0. In the first case, compiling for exact shapes can make a new sequence length trigger another compilation. In the second, the buffer shape stays fixed while the active length changes. The decoding code still has to keep track of which positions contain valid state.
TorchTPU also documents bounded dynamism for supported workloads. Several active lengths within a compiled bucket can reuse its executable. The compiler has a fixed upper bound for the bucket, and the execution tracks the actual length. That gives teams a way to test a growing cache before taking on the work of changing how the model stores its state. The versioned reference explains the bucket behavior and its limitations at https://torchtpu.readthedocs.io/en/0.2.0/dynamic_shapes/
Separate first-run cost from repeated stalls
Aina discusses using XProf alongside the execution-mode comparisons. A practical comparison would keep the prompt and generated token count fixed across runs, then look at the first call and later calls separately. Inside one trace, compare steps with the same input shape against steps where the shape changes. Record any compile events alongside the host and device timelines. A long gap could warrant a closer look at synchronization as well as compilation.
For a discussion based on this example, it helps to report which shapes changed and which execution mode produced the trace. Those details give readers something they can compare against their own traces when their decoding loop slows down.