Learning-to-Cache: Accelerating Diffusion Transformer via Layer Caching

Xinyin Ma, Gongfan Fang, Michael Bi Mi, Xinchao Wang

TL;DR

Learning-to-Cache (L2C) is a novel acceleration strategy for Diffusion Transformers (DiTs) that identifies and skips redundant layer computations across denoising timesteps. By optimizing a differentiable "router" to decide which layers to cache, L2C achieves significant speedups—removing up to 93% of layer computations in certain steps—with virtually no impact on image quality.

The Challenge of Slow Diffusion Inference

Diffusion models have revolutionized image generation, but they are notoriously slow. Because they generate images through an iterative denoising process, a single image might require 50 or more passes through a massive transformer model.

While researchers have tried to speed this up by reducing the number of steps (via sampling solvers) or shrinking the model (via pruning or quantization), these methods often trade off significant image fidelity for speed. The authors of this paper noticed a different opportunity: temporal redundancy. In a transformer-based diffusion model, the outputs of many layers change very little between consecutive denoising steps. If we could identify these redundant layers and simply "cache" their previous results, we could skip a massive amount of computation. However, finding the optimal set of layers to cache is a "needle in a haystack" problem with an exponential search space.

The Learning-to-Cache (L2C) Framework

Instead of using manual heuristics to pick layers, the authors turn the selection into a differentiable optimization problem.

  1. Layer Interpolation: They define a "soft" version of a transformer layer that can smoothly transition between two states: a "slow" state (full computation) and a "fast" state (reusing the output from the previous timestep).
  2. The Router ($\beta$): They introduce a set of learnable parameters called a "router." This router is time-variant (it makes different decisions at step 10 vs. step 50) but input-invariant (it makes the same decision for every image at a given step).
  3. Differentiable Optimization: They train this router by minimizing a loss function that balances fidelity (the difference between the output of the full model and the cached model) and sparsity (a penalty on the number of active layers).
  4. Static Graph Generation: Once the router is trained, they apply a threshold to the continuous $\beta$ values to create a fixed, discrete computation graph used for high-speed inference.
The L2C mechanism uses a router to smoothly interpolate between full computation and cached states.

Key Insights

The authors evaluated L2C on the ImageNet dataset at 256×256 and 512×512 resolutions across architectures like DiT-XL/2, DiT-L/2, and U-ViT-H/2. They discovered several interesting properties of diffusion transformers:

  • Architecture-Specific Redundancy: The "cacheability" of a model depends heavily on its design. For example, U-ViT-H/2 (which uses U-Net-like skip connections) is incredibly redundant in its middle layers, allowing for much more aggressive caching than the standard DiT architecture.
  • Temporal Sensitivity: Redundancy is not constant throughout the denoising process. Earlier steps (when the image is mostly noise) are more tolerant of caching, while later steps (refining fine details) require more active computation.
  • FFN vs. MHSA: Multi-Head Self-Attention (MHSA) and Feed-Forward Networks (FFN) exhibit different error profiles, with errors tending to accumulate more significantly in the later layers of DiT models.

Performance and Results

L2C consistently outperforms standard samplers like DDIM and DPM-Solver, as well as prior caching methods like DeepCache, at the same inference speeds.

  • Extreme Efficiency: For U-ViT-H/2, L2C can remove 93.68% of layers in the "cache steps" (averaging 46.84% across all steps) with a negligible FID drop of less than 0.01.
  • Speedups: On DiT-XL/2, the method achieves a 1.74x speedup while maintaining a better FID (2.36) than a 30-step DPM-Solver (2.46).
  • Visual Fidelity: Qualitatively, the images produced by L2C are nearly indistinguishable from those produced by the full-computation baseline, avoiding the artifacts often seen with aggressive layer dropping.
The speed-quality tradeoff shows L2C (orange star) consistently achieving lower FID scores at faster latencies compared to manual heuristics.

Limitations and Future Work

  • 2x Ceiling: Currently, the implementation alternates between one "full" step and one "cache" step, which caps the theoretical speedup at 2x. Future work could explore caching across three or more steps to break this ceiling.
  • Training Requirement: While L2C doesn't require retraining the massive diffusion model itself, it does require a short "calibration" phase (about 1 epoch on ImageNet) to optimize the router for a specific model and resolution.
  • Resolution Sensitivity: Lossless caching is more difficult at higher resolutions (e.g., 512x512), where even small approximation errors can lead to a slight drop in FID.

Summary updated

BibTeX
@inproceedings{ma2024learning,
  author = {Xinyin Ma and Gongfan Fang and Michael Bi Mi and Xinchao Wang},
  title = {{Learning-to-Cache: Accelerating Diffusion Transformer via Layer Caching}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=ZupoMzMNrO},
}
Figure