0% found this document useful (0 votes)
9 views5 pages

Optimizing Diffusion Models with Quantization

Uploaded by

anirudhashi007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Optimizing Diffusion Models with Quantization

Uploaded by

anirudhashi007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Effective Quantization for Diffusion Models on CPUs

Hanwen Chang Haihao Shen Yiyang Cai Xinyu Ye Zhenzhong Xu


Wenhua Cheng Kaokao lv Weiwei Zhang Yintong Lu Heng Guo
{[Link], [Link], [Link], [Link], [Link]
[Link], [Link], [Link], [Link], [Link]}@[Link]
arXiv:2311.16133v2 [[Link]] 29 Nov 2023

Abstract

Diffusion models have gained popularity for generating images from textual de-
scriptions. Nonetheless, the substantial need for computational resources continues
to present a noteworthy challenge, contributing to time-consuming processes. Quan-
tization, a technique employed to compress deep learning models for enhanced
efficiency, presents challenges when applied to diffusion models. These models are
notably more sensitive to quantization compared to other model types, potentially
resulting in a degradation of image quality. In this paper, we introduce a novel
approach to quantize the diffusion models by leveraging both quantization-aware
training and distillation. Our results show the quantized models can maintain the
high image quality while demonstrating the inference efficiency on CPUs. The code
is publicly available at: [Link]

1 Introduction

Diffusion models have demonstrated remarkable success in producing images characterized by both
high diversity and fidelity, e.g., Stable Diffusion Rombach et al. [2022], Imagen Saharia et al. [2022].
Nevertheless, their significant demand for computational resources remains a notable challenge.
Although the generated images are undeniably impressive and have captured people’s interest, a
significant challenge lies in their low performance or high computational costs. Users might find
themselves in a situation where generating images on a GPU incurs substantial expenses, and
attempting the same task on a CPU results in unacceptably long processing times.
Quantization represents a contemporary area of research aimed at optimizing and improving the
efficiency of diffusion methods. Post-training quantization (PTQ) as outlined in Shang et al.’s research
Shang et al. [2023] serves as a valuable reference for applying quantization to diffusion models
after the training process. Q-DiffusionLi et al. [2023b] divided weights and activations into distinct
groups, applying quantization separately to each group. These studies have achieved remarkable
Frechet Inception Distance (FID) Heusel et al. [2017a] scores on CIFAR-10, LSUN-Bedrooms, and
LSUN-Churches datasets, all while significantly reducing the model’s size. While these methods
have demonstrated success in terms of FID scores on certain datasets, generating visually appealing
images that meet human perception standards remains a persistent challenge.
This paper introduces innovative precision strategies specifically designed for enhancing the perfor-
mance of Diffusion models. By optimizing performance, we were able to generate images in less
than 6 seconds (50 steps) on an Intel CPU, producing output images at a resolution of 512x512 pixels.
The image quality has been assessed and confirmed as satisfactory by both human evaluators and
FID measurements. Our contributions can be summarized in three key aspects: 1) Introduce preci-
sion strategies/quantization recipes tailored for Diffusion models. 2) Develop an efficient inference
runtime equipped with high-performance kernels designed for CPUs. 3) Validate our approach across
various versions of Stable Diffusion, including 1.4, 1.5, and 2.1.

Neural Information Processing Systems (NeurIPS) 2023 Workshop on Diffusion Models.


2 Approach

We describe the quantization overview of diffusion models in Figure 1, which shows the precision is
selectively applied per timestamp.

Figure 1: Time-dependent Quantization: Different Precisions on Different Steps

2.1 Quantization on Unet

Numerous diffusion models feature the Unet architecture as a critical element. Throughout the de-
noising process, the Unet architecture is employed to predict the noise present in the noisy image and
subsequently enhance the image by iteratively utilizing this noise estimation across multiple iterations.
Profiling analysis reveals that the Unet operation represents the most computationally demanding
step in the entire image generation process. As a solution, we have introduced Quantization-Aware
Training (QAT) Jacob et al. [2018] specifically for the Unet component to alleviate this computational
burden. During QAT of Unet, Knowledge Distillation can be incorporated to improve the accuracy.
With original Unet as the teacher, its output functions as the guidance for the student, i.e. fake
quantized Unet. This quantization workflow is described in Algorithm 1.

Algorithm 1 QAT with Knowledge Distillation for Unet


Require: Pretrained diffusion model, dataset, max train steps N
Copy Unet of pretrained diffusion model as teacher UT ;
Fake quantize Unet of pretrained diffusion model as student US ;
for k ← 1 to N do
Sample data from dataset randomly;
Run diffusion model training workflow until Unet’s forward;
Get UT ’s output oT and US ’s output oS ;
Compute loss between oT and oS as lKD ;
Add loss lKD to the original loss;
Update model’s weight with gradient w.r.t. this loss;
Run remaining diffusion model training workflow;
end for

2.2 Mixed Precision on Denoising Loop

The proposed time-dependent mixed precision framework applies mixed precision in a step-wise
fashion across the denoising process of the diffusion model. Specifically, within the denoising process
spanning ’n’ steps, the initial ’k’ steps and the final ’k’ steps employ a Unet model with higher
precision, such as BFloat16, for noise estimation. In contrast, the intervening steps utilize a Unet
model with lower precision, like INT8, for noise estimation.

2
3 Software Acceleration
While low precision can reduce the inference overhead, we still need to optimize GroupNorm operator.
Figure 2 illustrates the data layout, while Figure 3 demonstrates the data division across various cores.
The primary issue lies in the fact that the number of groups is fewer than the available CPU cores,
resulting in a low CPU utilization rate. To address this issue, we have restructured our approach
by computation parallelism across dimensions for channels rather than groups. In the initial step,
each core calculates the mean and variance for its respective channels. The subsequent step involves
computing group-level values from the channels within each group. Finally, each core performs
channel normalization independently. The whole flow is in Figure4. You can find these optimizations
in Intel Extension for Transformers Intel [2023].

Figure 2: Data Layout Figure 3: Divide Computing Tasks by


Group

Beyond enhancing GroupNorm, we also fuse the Multi-Head Attention (MHA) and introduce an
advanced memory allocator to further optimize performance.

Figure 4: Optimized GroupNorm

4 Experimental setup
We select Stable Diffusion as the representative model in our experiment, given it’s the most prevalent
and widely-used open-source diffusion model. As mentioned in Section 2, we apply quantization to
Unet which is performance critical to the entire model. We use the default 50 iterations for latent
denoising. Note that there are potential accuracy discrepancies between our model and the others due
to configuration differences.

4.1 Accuracy & Performance

On accuracy, we use MS-COCOLin et al. [2014] 2017 validation dataset to evaluate the FID of the
Stable Diffusion. The dataset has 5,000 images, and each image has a few captions that describe the
image in natural language. We choose 5,000 images and their first caption as the test dataset. Five

3
experimental sets are selected for comparing the FID of Stable Diffusion: 1. 50 steps on FP32 Unet;
2. 50 steps on BF16 Unet; 3. 50 steps on INT8 Unet; 4. 6 steps (first and last 3 steps) on BF16 Unet
and 44 steps on INT8 Unet, and 5. 10 steps (first and last 5 steps) on BF16 Unet and 40 steps on
INT8 Unet.
On performance, we leverage Intel Extensions for Transformers Intel [2023] to measure the per-
formance of various Stable Diffusion versions (1.4, v1.5, and 2.1) on Intel’s 4th Generation Xeon
Scalable Processors (Sapphire Rapids). The image size 512x512 is used. The code is publicly
available at: [Link]

5 Results
Table1 shows the accuracy as measured by FID Heusel et al. [2017b] using the pre-
definedconfigurations.

Table 1: FID of each precision

Precision FP32 BF16 INT8 BF16 (6 Steps)/INT8 BF16 (10 Steps)/INT8


FID 30.48 30.58 35.46 31.07 30.63

You can explore the output images instead of metrics. From Figure 5, the image quality looks
promising and very close to full precision results. This approach demonstrates its feasibility, with
results that are visually indistinguishable to the human eye.

Figure 5: output images of mixed precision and full precision.

We validated the performance of mixed precision in v1.5, as demonstrated in Table 2, providing


compelling evidence that mixed precision can significantly enhance overall performance. In fact, we
discovered that employing 20 steps can yield comparable results to using 50 steps. Therefore, we
conducted a performance benchmark with the 20-step approach. The latency for BF16 in version 1.5
is 2.74 seconds, while for INT8, it is 2.14 seconds. We hold the belief that a mixed approach could
also prove effective. Low precision also works for version 1.4 and version 2.1, their FP32 latency are
11.39 seconds and 16.98 seconds while BF16 latency are both 2.83 seconds.

Table 2: Inference Performance (50 Steps)

Precision BF16 BF16 (10 Steps)/INT8 INT8


Latency 6.32 5.5s 5.2s

6 Summary and future work


We presented an effective quantization approach for diffusion models, allowing the mixed preci-
sion on Unet to achieve a well-balanced trade-off between accuracy and performance. The next
step is to explore other compression techniques such as 4-bits quantization Frantar et al. [2022],
Cheng et al. [2023] or sparse Li et al. [2023a]. We plan to try early exit with INT8 model
initialized and subsequently perform inference using mixed precision to improve the quality in
[Link]

4
References
W. Cheng, W. Zhang, H. Shen, Y. Cai, X. He, and K. Lv. Optimize weight rounding via signed
gradient descent for the quantization of llms. arXiv preprint arXiv:2309.05516, 2023.
E. Frantar, S. Ashkboos, T. Hoefler, and D. Alistarh. Gptq: Accurate post-training quantization for
generative pre-trained transformers. arXiv preprint arXiv:2210.17323, 2022.
M. Heusel, H. Ramsauer, T. Unterthiner, B. Nessler, and S. Hochreiter. Gans trained by a two
time-scale update rule converge to a local nash equilibrium. Advances in neural information
processing systems, 30, 2017a.
M. Heusel, H. Ramsauer, T. Unterthiner, B. Nessler, G. Klambauer, and S. Hochreiter. Gans trained
by a two time-scale update rule converge to a nash equilibrium. CoRR, abs/1706.08500, 2017b.
URL [Link]
Intel. Intel® extension for transformers, 2023. URL "[Link]
intel-extension-for-transformers. [Link]
transformers.
B. Jacob, S. Kligys, B. Chen, M. Zhu, M. Tang, A. Howard, H. Adam, and D. Kalenichenko.
Quantization and training of neural networks for efficient integer-arithmetic-only inference. In
Proceedings of the IEEE conference on computer vision and pattern recognition, pages 2704–2713,
2018.
M. Li, J. Lin, C. Meng, S. Ermon, S. Han, and J.-Y. Zhu. Efficient spatially sparse inference for
conditional gans and diffusion models. IEEE Transactions on Pattern Analysis and Machine
Intelligence, 2023a.
X. Li, L. Lian, Y. Liu, H. Yang, Z. Dong, D. Kang, S. Zhang, and K. Keutzer. Q-diffusion: Quantizing
diffusion models. arXiv preprint arXiv:2302.04304, 2023b.
T.-Y. Lin, M. Maire, S. J. Belongie, J. Hays, P. Perona, D. Ramanan, P. Dollár, and C. L. Zitnick.
Microsoft coco: Common objects in context. In European Conference on Computer Vision, 2014.
URL [Link]
R. Rombach, A. Blattmann, D. Lorenz, P. Esser, and B. Ommer. High-resolution image synthesis
with latent diffusion models. In Proceedings of the IEEE/CVF conference on computer vision and
pattern recognition, pages 10684–10695, 2022.
C. Saharia, W. Chan, S. Saxena, L. Li, J. Whang, E. L. Denton, K. Ghasemipour, R. Gontijo Lopes,
B. Karagol Ayan, T. Salimans, et al. Photorealistic text-to-image diffusion models with deep
language understanding. Advances in Neural Information Processing Systems, 35:36479–36494,
2022.
Y. Shang, Z. Yuan, B. Xie, B. Wu, and Y. Yan. Post-training quantization on diffusion models. In
Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition, pages
1972–1981, 2023.

Common questions

Powered by AI

Frechet Inception Distance (FID) is considered a suitable metric for evaluating the quality of images generated by diffusion models because it quantitatively evaluates the diversity and fidelity of generated images against real images. FID measures the difference between the feature distributions of generated and real images, as calculated with an Inception neural network. Lower FID scores indicate closer similarity to real images, hence better image quality. It effectively balances the trade-offs between realism and diversity, providing a reliable numerical assessment for model outputs in generative tasks such as image generation .

Knowledge distillation complements quantization-aware training by providing a learning framework where a simpler model (student) imitates a more complex one (teacher). During quantization-aware training in diffusion models, the student model, which is quantized, is guided by the output of the teacher model, which uses full precision. This process helps the student model learn meaningful representations and improve accuracy despite the lower precision constraints. It allows quantized models to maintain high performance levels in terms of image quality and inference efficiency on CPUs, bridging the gap created by reduced numerical precision .

The potential benefits of employing 4-bits quantization for diffusion models include even greater reductions in model size and further improvement in computational efficiency, which would make large-scale models feasible on more limited hardware. Such low-bit quantization could lead to faster inference times and reduced resource consumption. However, the challenges would likely involve maintaining model accuracy and image quality, as the aggressive reduction in numerical precision could lead to significant quantization noise. Practical implementation would also require the development of more sophisticated training and optimization techniques to satisfactorily compensate for these low precision levels without degrading model performance beyond usability .

Utilizing Intel's Extensions for Transformers in diffusion model performance provides significant quantitative improvements, particularly in processing speed. The optimizations directly enhance operations such as GroupNorm and Multi-Head Attention, which contribute to reduced inference times on Intel Xeon processors. For instance, using BF16 precision combined with these extensions results in latency of just 2.74 seconds per image output on certain models. Such hardware-specific optimizations enable faster computations with minimal impact on model accuracy, allowing effective scaling of the diffusion processes to CPU environments .

Mixed precision in the denoising process of diffusion models involves using varying levels of numerical precision at different stages of denoising. Initially, higher precision (BFloat16) is used for the first and last steps, where accurate noise estimation is crucial, while the intermediate stages use lower precision (INT8). This time-dependent strategy maintains image quality while reducing computational load, resulting in faster processing times without significantly sacrificing visual fidelity. By balancing precision needs across denoising steps, mixed precision optimizes performance while ensuring reasonable accuracy and satisfies human perceptual standards .

Enhanced CPU utilization in diffusion models' inference can be achieved by restructuring computation parallelism. This restructuring involves performing channel-wise rather than group-wise computations, as the number of available CPU cores exceeds the number of groups. By assigning work to channels instead, each core can independently calculate mean and variance, leading to more efficient simultaneous processing per core. This approach maximizes resource use on CPUs and reduces idling, thus increasing throughput and improving the overall speed of operations within diffusion model inference tasks .

The UNet architecture is performance-critical in diffusion models because it constitutes the most computationally demanding part of the image generation process, focusing on denoising iterations. Quantization-aware training (QAT) specifically for UNet helps lessen this computational demand by preparing the model for lower precision arithmetic during training. This optimization allows the preservation of model accuracy post-quantization and enhances efficiency by allowing deployment on less powerful hardware such as CPUs .

Optimized GroupNorm enhances performance by improving CPU utilization during the normalization process. By dividing tasks by channels instead of groups, all available CPU cores are better utilized, leading to more efficient data processing. Similarly, fusing Multi-Head Attention (MHA) reduces the overhead from managing separate components and helps streamline computations. These optimizations collectively enable faster inference and more efficient use of resources, thus boosting the overall performance of diffusion models on CPU environments .

Quantization often leads to a degradation of image quality in diffusion models because these models are more sensitive to the quantization process. To mitigate these effects, techniques such as quantization-aware training (QAT) and knowledge distillation are employed. QAT allows the model to adjust to the lower precision during training, while knowledge distillation uses a teacher-student model framework, with the original model (teacher) guiding the quantized model (student). This method improves accuracy while reducing computational burdens, allowing the quantized models to maintain high image quality and inference efficiency on CPUs .

Inference performance in stable diffusion models is measured primarily by latency and accuracy metrics such as Frechet Inception Distance (FID). In experimental setups using mixed precision approaches, latency was reduced by employing different precisions at various steps (e.g., using BF16 and INT8 precision stages). These methods allowed for significant reductions in processing time while keeping FID scores close to those of fully accurate (FP32) models, thus demonstrating comparable image quality at a faster inference rate. Results showed dramatic improvements, with latencies notably lower for lower precision mixed models, demonstrating the effectiveness of mixed precision in boosting performance .

You might also like