Optimizing Stable Diffusion for Intel CPUs with NNCF and đ¤ Optimum
Overview
Hugging Face presented a method to optimize Stable Diffusion for Intel CPUs by combining Neural Network Compression Framework (NNCF) quantization-aware training with Token Merging, resulting in up to 5.1x faster inference and a model size reduced to 0.25x of the original PyTorch checkpoint.
Stable Diffusion Optimization
The UNet component of the Stable Diffusion pipeline is the most computationally expensive part, so optimizing it yields large speed gains. Traditional postâtraining 8âbit quantization does not work well for this model because pixelâlevel prediction tasks are highly sensitive to parameter changes and the model contains little redundancy due to training on hundreds of millions of samples. To preserve accuracy, more sophisticated quantization methods such as QuantizationâAware Training (QAT) are required.
Optimization Workflow
We started from a Stable Diffusion model fineâtuned on the Pokemon dataset (svjack/Stable-Diffusion-Pokemon-en). Using the Diffusers textâtoâimage fineâtuning example, we integrated NNCFâbased QAT into the training script, added a knowledgeâdistillation loss where the original model acts as a teacher, and applied Exponential Moving Average (EMA) to model parameters (excluding quantizers) for training stability. Gradient checkpointing and keeping the EMA model in RAM allowed the whole optimization to run on a single GPU with 24âŻGB VRAM in less than a day for 4096 iterations.
Going Beyond QuantizationâAware Training
Quantization alone reduces model footprint, load time, memory consumption and latency. We stacked 8âbit quantization with the Token Merging (ToME) method, which merges redundant tokens before the selfâattention block to cut computation. The combined workflow includes knowledge distillation, EMA, and gradient checkpointing as described above. Starting again from the Pokemonâfineâtuned model, we applied ToME with a merging ratio of 0.4 on top of quantization. The resulting model is intended for inference on client or edge CPUs.
Results
Converting the PyTorch baseline to OpenVINO FP32 gave a 1.9x speedup. Adding 8âbit quantization increased the speedup to 3.9x versus PyTorch and reduced the model footprint to 0.25x of the original checkpoint. Stacking Token Merging on top of quantization yielded a 5.1x inference speedup while keeping the footprint at the same 0.25x level. All measurements were performed with OpenVINO 2022.3 on a Hugging Face Spaces CPU upgrade instance using 3rd Generation IntelÂŽ XeonÂŽ Scalable processors with IntelÂŽ Deep Learning Boost technology, using the default 50 inference steps. The blog notes that fewer steps increase speed but may affect image quality, and recommends experimenting with step counts and schedulers.
An example inference pipeline is shown below:
from optimum.intel import OVStableDiffusionPipeline
# Load and compile the pipeline for performance.
name = "OpenVINO/stable-diffusion-pokemons-tome-quantized-aggressive"
pipe = OVStableDiffusionPipeline.from_pretrained(name, compile=False)
pipe.reshape(batch_size=1, height=512, width=512, num_images_per_prompt=1)
pipe.compile()
# Generate an image.
prompt = "a drawing of a green pokemon with red eyes"
output = pipe(prompt, num_inference_steps=50, output_type="pil\)).images[0]
output.save("image.png
The training and quantization code are available in the Optimum Intel repository, a demonstration notebook is provided, and optimized models can be found on the Hugging Face Hub under the OpenVINO organization. A live demo runs on Hugging Face Spaces.
What about the generalâpurpose Stable Diffusion model?
The workflow demonstrated on the Pokemon model shows that substantial optimization is achievable with modest training resources. Training a generalâpurpose Stable Diffusion model from scratch is expensive, but with sufficient budget and hardware the same approach can be applied. The caveat is that Token Merging reduces model capacity; therefore, for more complex datasets a lower merging ratio should be used during optimization.
For further reading on complementary approaches for 4thâgeneration Intel Xeon CPUs, see the related Hugging Face blog post on Stable Diffusion inference with Intel.
Sources
Related
- Dispatch
- Dispatch
- Dispatch
- Dispatch
- Dispatch