Upload code quantize int8 ONNX weight.
Browse files- quantize_int8_test.py +113 -0
quantize_int8_test.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
import onnx
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from diffusers import DiffusionPipeline, StableDiffusionPipeline
|
| 6 |
+
import torch
|
| 7 |
+
from utilities import load_calib_prompts
|
| 8 |
+
from utilities import get_smoothquant_config
|
| 9 |
+
import ammo.torch.quantization as atq
|
| 10 |
+
import ammo.torch.opt as ato
|
| 11 |
+
from utilities import filter_func, quantize_lvl
|
| 12 |
+
|
| 13 |
+
# pipeline = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0",
|
| 14 |
+
# torch_dtype=torch.float16,
|
| 15 |
+
# use_safetensors=True,
|
| 16 |
+
# variant="fp16")
|
| 17 |
+
|
| 18 |
+
pipeline = StableDiffusionPipeline.from_pretrained("wyyadd/sd-1.5", torch_dtype=torch.float16)
|
| 19 |
+
|
| 20 |
+
pipeline.to("cuda")
|
| 21 |
+
# pipeline.enable_xformers_memory_efficient_attention()
|
| 22 |
+
# pipeline.enable_vae_slicing()
|
| 23 |
+
|
| 24 |
+
BATCH_SIZE = 4
|
| 25 |
+
cali_prompts = load_calib_prompts(batch_size=BATCH_SIZE, calib_data_path="./calibration-prompts.txt")
|
| 26 |
+
|
| 27 |
+
quant_config = get_smoothquant_config(pipeline.unet, quant_level=3.0)
|
| 28 |
+
|
| 29 |
+
def do_calibrate(base, calibration_prompts, **kwargs):
|
| 30 |
+
for i_th, prompts in enumerate(calibration_prompts):
|
| 31 |
+
print(prompts)
|
| 32 |
+
if i_th >= kwargs["calib_size"]:
|
| 33 |
+
return
|
| 34 |
+
base(
|
| 35 |
+
prompt=prompts,
|
| 36 |
+
num_inference_steps=kwargs["n_steps"],
|
| 37 |
+
negative_prompt=[
|
| 38 |
+
"normal quality, low quality, worst quality, low res, blurry, nsfw, nude"
|
| 39 |
+
]
|
| 40 |
+
* len(prompts),
|
| 41 |
+
).images
|
| 42 |
+
|
| 43 |
+
def calibration_loop():
|
| 44 |
+
do_calibrate(
|
| 45 |
+
base=pipeline,
|
| 46 |
+
calibration_prompts=cali_prompts,
|
| 47 |
+
calib_size=384,
|
| 48 |
+
n_steps=50,
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
quantized_model = atq.quantize(pipeline.unet, quant_config, forward_loop = calibration_loop)
|
| 53 |
+
ato.save(quantized_model, 'base.unet15_2.int8.pt')
|
| 54 |
+
|
| 55 |
+
quantize_lvl(quantized_model, quant_level=3.0)
|
| 56 |
+
atq.disable_quantizer(quantized_model, filter_func)
|
| 57 |
+
|
| 58 |
+
device1 = "cpu"
|
| 59 |
+
quantized_model = quantized_model.to(torch.float32).to(device1)
|
| 60 |
+
|
| 61 |
+
#Export model
|
| 62 |
+
sample = torch.randn((1, 4, 128, 128), dtype=torch.float32, device=device1)
|
| 63 |
+
timestep = torch.rand(1, dtype=torch.float32, device=device1)
|
| 64 |
+
encoder_hidden_state = torch.randn((1, 77, 768), dtype=torch.float32, device=device1)
|
| 65 |
+
|
| 66 |
+
import onnx
|
| 67 |
+
from pathlib import Path
|
| 68 |
+
|
| 69 |
+
output_path = Path('/home/tiennv/trang/Convert-_Unet_int8_Rebuild/Diffusion/onnx_unet15')
|
| 70 |
+
output_path.mkdir(parents=True, exist_ok=True)
|
| 71 |
+
|
| 72 |
+
dummy_inputs = (sample, timestep, encoder_hidden_state)
|
| 73 |
+
|
| 74 |
+
onnx_output_path = output_path / "unet" / "model.onnx"
|
| 75 |
+
onnx_output_path.parent.mkdir(parents=True, exist_ok=True)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
# to cpu to export onnx
|
| 79 |
+
# from onnx_utils import ammo_export_sd
|
| 80 |
+
# base.unet.to(torch.float32).to("cpu")
|
| 81 |
+
# ammo_export_sd(base, 'onnx_dir', 'stabilityai/stable-diffusion-xl-base-1.0')
|
| 82 |
+
|
| 83 |
+
torch.onnx.export(
|
| 84 |
+
quantized_model,
|
| 85 |
+
dummy_inputs,
|
| 86 |
+
str(onnx_output_path),
|
| 87 |
+
export_params=True,
|
| 88 |
+
opset_version=18,
|
| 89 |
+
do_constant_folding=True,
|
| 90 |
+
input_names=['sample', 'timestep', 'encoder_hidden_state'],
|
| 91 |
+
output_names=['predict_noise'],
|
| 92 |
+
dynamic_axes={
|
| 93 |
+
"sample": {0: "B", 2: "W", 3: 'H'},
|
| 94 |
+
"encoder_hidden_state": {0: "B", 1: "S", 2: 'D'},
|
| 95 |
+
"predict_noise": {0: 'B', 2: "W", 3: 'H'}
|
| 96 |
+
}
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# T峄慽 瓢u h贸a v脿 l瓢u m么 h矛nh ONNX
|
| 100 |
+
unet_opt_graph = onnx.load(str(onnx_output_path))
|
| 101 |
+
unet_optimize_path = output_path / "unet_optimize"
|
| 102 |
+
unet_optimize_path.mkdir(parents=True, exist_ok=True)
|
| 103 |
+
unet_optimize_file = unet_optimize_path / "model.onnx"
|
| 104 |
+
|
| 105 |
+
onnx.save_model(
|
| 106 |
+
unet_opt_graph,
|
| 107 |
+
str(unet_optimize_file),
|
| 108 |
+
save_as_external_data=True,
|
| 109 |
+
all_tensors_to_one_file=True,
|
| 110 |
+
location="weights.pb",
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
|