Skip to content

Commit 0c7f3de

Browse files
authored
Add Stable Diffusion int8 example (#2259)
Signed-off-by: Kaihui-intel <kaihui.tang@intel.com>
1 parent 263911a commit 0c7f3de

File tree

7 files changed

+636
-0
lines changed

7 files changed

+636
-0
lines changed

examples/.config/model_params_pytorch_3x.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,22 @@
228228
"main_script": "main.py",
229229
"batch_size": 1
230230
},
231+
"sd21_static_int8":{
232+
"model_src_dir": "diffusion_model/diffusers/stable_diffusion/static_quant",
233+
"dataset_location": "/tf_dataset2/datasets/coco2017/coco/",
234+
"input_model": "",
235+
"main_script": "main.py",
236+
"batch_size": 1,
237+
"iters": 10
238+
},
239+
"lcm_static_int8":{
240+
"model_src_dir": "diffusion_model/diffusers/stable_diffusion/static_quant",
241+
"dataset_location": "/tf_dataset2/datasets/coco2017/coco/",
242+
"input_model": "",
243+
"main_script": "main.py",
244+
"batch_size": 1,
245+
"iters": 10
246+
},
231247
"resnet18_mixed_precision": {
232248
"model_src_dir": "cv/mixed_precision",
233249
"dataset_location": "/tf_dataset/pytorch/ImageNet/raw",
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Stable Diffusion
2+
3+
Stable Diffusion quantization and inference best known configurations with static quant.
4+
5+
## Model Information
6+
7+
| **Framework** | **Model Repo** |
8+
|:-------------:|:-------------------------------------------------------------------:|
9+
| PyTorch | https://huggingface.co/stabilityai/stable-diffusion-2-1 |
10+
| PyTorch | https://huggingface.co/SimianLuo/LCM_Dreamshaper_v7 |
11+
12+
# Pre-Requisite
13+
14+
### Datasets
15+
16+
Download the 2017 [COCO dataset](https://cocodataset.org) using the `download_dataset.sh` script.
17+
Export the `DATASET_DIR` environment variable to specify the directory where the dataset will be downloaded. This environment variable will be used again when running training scripts.
18+
```
19+
export DATASET_DIR=<directory where the dataset will be saved>
20+
bash download_dataset.sh
21+
```
22+
23+
# Quantization and Inference
24+
quantization
25+
```shell
26+
python main.py \
27+
--model_name_or_path stabilityai/stable-diffusion-2-1 \
28+
--dataset_path=${DATASET_DIR} \
29+
--quantized_model_path=${INT8_MODEL} \
30+
--compile_inductor \
31+
--precision=int8-bf16 \
32+
--calibration
33+
```
34+
inference
35+
```shell
36+
python main.py \
37+
--model_name_or_path stabilityai/stable-diffusion-2-1 \
38+
--dataset_path=${DATASET_DIR} \
39+
--precision=int8-bf16 \
40+
--benchmark \
41+
-w 1 \
42+
-i 10 \
43+
--quantized_model_path=${INT8_MODEL} \
44+
--compile_inductor
45+
```
46+
## FID evaluation
47+
We have also evaluated FID scores on COCO2017 validation dataset for BF16 model, mixture of BF16 and INT8 model. FID results are listed below.
48+
49+
| Model | BF16 | INT8+BF16 |
50+
|----------------------|-------|-----------|
51+
| stable-diffusion-2-1 | 27.8630 | 27.8618 |
52+
| SimianLuo/LCM_Dreamshaper_v7|42.1710| 42.3138|
53+
54+
To evaluated FID score on COCO2017 validation dataset for mixture of BF16 and INT8 model, you can use below command.
55+
56+
```bash
57+
python main.py \
58+
--dataset_path=${DATASET_DIR} \
59+
--precision=int8-bf16 \
60+
--accuracy \
61+
--quantized_model_path=${INT8_MODEL} \
62+
--compile_inductor
63+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright (c) 2025 Intel Corporation
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
DATASET_DIR=${DATASET_DIR-$PWD}
19+
20+
dir=$(pwd)
21+
mkdir ${DATASET_DIR}; cd ${DATASET_DIR}
22+
curl -O http://images.cocodataset.org/zips/val2017.zip; unzip val2017.zip
23+
curl -O http://images.cocodataset.org/annotations/annotations_trainval2017.zip; unzip annotations_trainval2017.zip
24+
cd $dir

0 commit comments

Comments
 (0)