TL;DR
This paper tackles the challenge of deploying large-scale diffusion models, like Stable Diffusion, onto resource-constrained mobile devices. They achieve this by adapting the model for TensorFlow Lite and implementing several optimization techniques, resulting in a "Mobile Stable Diffusion" that can generate a 512x512 image in under 7 seconds on Android GPUs.
What Problem Does This Paper Address?
Diffusion models have revolutionized image synthesis, producing impressive results. However, their sheer size (often billions of parameters) makes them impractical for direct deployment on mobile devices, which have limited computational power and memory. This creates a gap between the cutting-edge capabilities of these models and their accessibility for real-world, on-device applications. The paper aims to bridge this gap by making Stable Diffusion feasible for mobile use, enabling benefits like reduced server costs and enhanced user privacy.
Methods
The authors leverage Google's TensorFlow Lite (TFLite) framework as their deployment platform, opting for its accessibility and broad device support over custom kernel development. To overcome TFLite's limitations with large diffusion models, they introduce several key techniques:
- Converting Fully-Connected to Conv2D Layers: In certain parts of the model's architecture (specifically spatial transformer blocks), large fully-connected layers were found to hinder GPU delegation. The paper proposes converting these layers into equivalent Convolutional 2D (Conv2D) layers, which TFLite can more efficiently process on the GPU.
- Serializing Conv2D with Large Activations: Even after the conversion, some Conv2D layers with very large input/output activation sizes still posed delegation issues. The solution involves "serializing" these layers, effectively breaking them down into smaller, manageable chunks that can be processed sequentially. They found input serialization to be more efficient.
- Broadcast-Free Group Normalization: The standard implementation of group normalization in TFLite involves operations like
BroadcastTo, which are not supported by the TFLite GPU delegate. The authors reformat the group normalization layer to ensure activation tensors have at most 4 dimensions, thus avoiding the need forBroadcastTo. - Numerically Stable Approximation of GELU: Stable Diffusion uses
float16for faster operations, but this can lead to numerical instability on some mobile hardware, particularly with the GELU activation function. The paper proposes a more numerically stable approximation of the GELU function by clipping its cubic polynomial term, preventing floating-point exceptions without sacrificing image quality. - Pipelined Execution: Due to limited mobile memory, loading all components of Stable Diffusion (text encoder, denoising network, image decoder) simultaneously is often not feasible. They propose a pipelined execution strategy where components are loaded and unloaded as needed, orchestrated by a child thread.
- Model Compression (Quantization and Pruning): To further reduce memory footprint and computational load, they apply 8-bit weight quantization and structured pruning to the model. While
float16is used for activations, weights are cast tofloat16before computation.
Datasets
Not discussed in the paper. The paper focuses on optimizing an existing pre-trained model (Stable Diffusion v2.1) rather than training on new datasets.
Key Insights and Ideas
- TFLite as a viable, accessible platform: The paper demonstrates that TFLite, despite its limitations, can be effectively adapted for complex models like Stable Diffusion, promoting wider adoption.
- The importance of operator delegation: The core challenge lies in ensuring that as many operations as possible are delegated to the mobile GPU. Techniques like layer conversion and serialization are crucial for overcoming TFLite's inherent limitations.
- Subtle optimizations yield significant gains: Small modifications, like the GELU approximation or the reformatting of group normalization, are critical for achieving stable and efficient inference on diverse mobile hardware.
- Pipelining is essential for memory-constrained devices: The proposed pipelined execution strategy is a practical solution for devices that cannot hold the entire model in memory at once.
Key Results
The "Mobile Stable Diffusion" model, based on Stable Diffusion v2.1, achieves an inference latency of under 7 seconds for generating a 512x512 image on Android devices with mobile GPUs (specifically, a Samsung Galaxy S23 with Snapdragon 8 Gen 2). This is a significant improvement compared to previous approaches, which often relied on custom kernels or specialized hardware. The paper also qualitatively shows that the generated images are comparable to the baseline, with only minor differences after quantization and pruning.
Limitations and Future Work
- Limitations: The paper acknowledges that while their approach significantly reduces latency, it's still a trade-off. The serialization of Conv2D layers, for instance, introduces some kernel call overhead. The qualitative assessment of image quality after compression is noted as being less prominent than the differences seen in Fig. 3 (which highlights hardware-specific numerical instability).
- Future Work: The authors suggest that their optimization techniques can be extended to other diffusion models, facilitating their deployment on mobile devices. They also believe that optimizing for common and accessible inference frameworks like TFLite will enrich the ecosystem of real-world mobile applications powered by diffusion models.
Summary updated