Optimizing Stable Diffusion for Intel CPUs with NNCF and 🤗 Optimum
Post-training 8-bit quantization breaks Stable Diffusion's UNet, so this workflow uses NNCF quantization-aware training with knowledge distillation from the original model as teacher and EMA on non-quantizer weights. It is stacked with an OpenVINO-compatible Token Merging (merge ratio 0.4) to speed up inference on Intel CPUs. Fine-tuning a Pokemon-tuned model for 4096 iterations on a single 24 GB GPU takes under a day. Against PyTorch FP32 on a Xeon CPU, OpenVINO FP32 gives 1.9x, 8-bit QAT gives 3.9x with a 4x smaller footprint, and ToMe plus 8-bit reaches 5.1x. The resulting pipeline loads through Optimum Intel's OVStableDiffusionPipeline, and more complex datasets call for lower merging ratios.
Related posts
- Accelerating Stable Diffusion Inference on Intel CPUs
Hugging Face mediumAdeptStable Diffusion v1.5 inference on Sapphire Rapids Xeon CPUs (EC2 r7iz) can be cut from a 32.3s baseline to a few seconds per image with a stack of optimizations. OpenVINO via Optimum Intel gives a 2x speedup through automatic bfloat16, and reshaping the pipeline to a static 512x512 resolution brings it to 4.7s. Without OpenVINO, jemalloc, Intel OpenMP and numactl core pinning reach 11.8s with no code change. Adding IPEX with channels-last, bfloat16 and AMX gets to 5.4s, and the DPMSolverMultistep scheduler reaches 5.05s, about 6.5x faster than the baseline.
- Accelerate your models with 🤗 Optimum Intel and OpenVINO
Hugging Face mediumAdeptOptimum Intel now integrates OpenVINO, letting Transformers models run on OpenVINO Runtime across Intel processors and be quantized with the Neural Network Compression Framework (NNCF). A walkthrough applies post-training static quantization to a ViT image classifier fine-tuned on food101, using 300 calibration samples, then exports to OpenVINO IR and loads the result via OVModelForImageClassification pipelines. The quantized model shrinks 3.8x (344MB to 90MB) and cuts per-sample latency 2.4x (98ms to 41ms) with no measurable accuracy loss (87.6). Warm up the model before benchmarking, because compilation happens on the first inference. Encoder-decoder quantization is not yet supported.
- Exploring Quantization Backends in Diffusers
Hugging Face mediumAdeptHugging Face Diffusers supports several quantization backends (bitsandbytes, torchao, Quanto, GGUF and native FP8 layerwise casting) that shrink large diffusion models such as Flux-dev, whose full BF16 pipeline needs about 31 GB. Benchmarks on an H100 put BnB 4-bit NF4 at about 12.6 GB after loading with no inference slowdown, while torchao and Quanto INT4 cut memory further but take roughly 109 seconds per image versus 12 seconds for BF16. Code examples apply PipelineQuantizationConfig to the transformer and T5 encoder, load pre-quantized GGUF files, and combine quantization with CPU offloading, group offloading and torch.compile. The 8-bit quality differences are usually hard to see, and NF4 is suggested as the best tradeoff.
- Fine-tuning Stable Diffusion models on Intel CPUs
Hugging Face mediumAdeptStable Diffusion v1.5 can be fine-tuned with textual inversion on a four-node Intel Sapphire Rapids Xeon cluster, using AMX acceleration via Intel Extension for PyTorch, oneCCL for distributed communication, and Accelerate launched under mpirun with 16 tasks. With only five example images and bf16 mixed precision, 200 training steps take about five minutes; 3,000 steps take about an hour. The walkthrough covers environment setup, the IPEX patch to the diffusers script, oneCCL/tcmalloc environment variables, per-node troubleshooting, and exporting the result to OpenVINO via Optimum Intel for sub-5-second CPU inference.
- Blazing Fast SetFit Inference with 🤗 Optimum Intel on Xeon
Hugging Face mediumAdeptPost-training static int8 quantization of a SetFit model body with Optimum Intel and Intel Neural Compressor speeds up inference on Xeon CPUs by up to 7.8x in throughput with negligible accuracy loss. The walkthrough calibrates on 100 rotten_tomatoes samples, quantizes a bge-small SetFit model with the IPEX backend, and benchmarks it against fp32 Transformers and IPEX bf16 with TorchScript. At batch size 1, latency drops from 15.69 ms to 4.55 ms (3.45x), model size shrinks from 127 MB to 45 MB, and sst2 accuracy moves only from 88.4% to 88.1%.