home/ atoms/ kv-cache-precomputation

Pre-computing KV-cache for the text prompt eliminates repeated text-encoder work across frames

In a standard diffusion UNet, the cross-attention keys and values derived from the text prompt embedding are recomputed every denoising step. When the prompt is constant across frames—the common case in live performance—this is redundant. StreamDiffusion’s prepare() method encodes the prompt once, stores prompt_embeds, and repeats it across the full batch dimension (repeat(self.batch_size, 1, 1)). The stored embeddings are reused for every subsequent __call__, so text-encoder inference only runs when update_prompt() is explicitly called. This caching is the Pre-Computation for KV-Caches feature listed in the README and contributes to the overall throughput budget.

Examples

stream.prepare('1girl with brown dog hair')  # text-encoder runs once
for frame in video_source:
    result = stream(frame)          # reuses cached prompt_embeds

stream.update_prompt('cat in hat')  # re-encodes only on change

Assessment

Explain why calling stream.prepare() on every frame would defeat the purpose of KV-cache pre-computation. Then identify the method to call when the prompt changes mid-performance.

“self.prompt_embeds = encoder_output[0].repeat(self.batch_size, 1, 1)”
corpus · streamdiffusion-pipeline-for-real-time-interactive-image-gen · chunk 56