-
Notifications
You must be signed in to change notification settings - Fork 3
/
convert_controlnet_to_onnx.py
166 lines (140 loc) · 5.39 KB
/
convert_controlnet_to_onnx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# Copyright 2022 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import os
import shutil
from pathlib import Path
import torch
from torch.onnx import export
import onnx
from onnxruntime.transformers.float16 import convert_float_to_float16
from diffusers import OnnxStableDiffusionPipeline, StableDiffusionControlNetPipeline, ControlNetModel, OnnxRuntimeModel
from packaging import version
#from transformers import AutoFeatureExtractor, BertTokenizerFast, CLIPTextModel, CLIPTokenizer
is_torch_less_than_1_11 = version.parse(version.parse(torch.__version__).base_version) < version.parse("1.11")
def convert_to_fp16(
model_path
):
'''Converts an ONNX model on disk to FP16'''
model_dir=os.path.dirname(model_path)
# Breaking down in steps due to Windows bug in convert_float_to_float16_model_path
onnx.shape_inference.infer_shapes_path(model_path)
fp16_model = onnx.load(model_path)
fp16_model = convert_float_to_float16(
fp16_model, keep_io_types=True, disable_shape_infer=True
)
# clean up existing tensor files
shutil.rmtree(model_dir)
os.mkdir(model_dir)
# save FP16 model
onnx.save(fp16_model, model_path)
def onnx_export(
model,
model_args: tuple,
output_path: Path,
ordered_input_names,
output_names,
dynamic_axes,
opset,
use_external_data_format=False,
):
output_path.parent.mkdir(parents=True, exist_ok=True)
# PyTorch deprecated the `enable_onnx_checker` and `use_external_data_format` arguments in v1.11,
# so we check the torch version for backwards compatibility
if is_torch_less_than_1_11:
export(
model,
model_args,
f=output_path.as_posix(),
input_names=ordered_input_names,
output_names=output_names,
dynamic_axes=dynamic_axes,
do_constant_folding=True,
use_external_data_format=use_external_data_format,
enable_onnx_checker=True,
opset_version=opset,
)
else:
export(
model,
model_args,
f=output_path.as_posix(),
input_names=ordered_input_names,
output_names=output_names,
dynamic_axes=dynamic_axes,
do_constant_folding=True,
opset_version=opset,
)
@torch.no_grad()
def convert_models(model_path: str, output_path: str, opset: int, fp16: bool, attention_slicing: str):
controlnet = ControlNetModel.from_pretrained(model_path)
output_path = Path(output_path)
if attention_slicing is not None:
print("Attention slicing: " + attention_slicing)
controlnet.set_attention_slice(attention_slicing)
dtype=torch.float32
device = "cpu"
cnet_path = output_path / "controlnet" / "model.onnx"
onnx_export(
controlnet,
model_args=(
torch.randn(2, 4, 64, 64).to(device=device, dtype=dtype),
torch.randn(2).to(device=device, dtype=dtype),
torch.randn(2, 77, 768).to(device=device, dtype=dtype),
torch.randn(2, 3, 512,512).to(device=device, dtype=dtype),
False,
),
output_path=cnet_path,
ordered_input_names=["sample", "timestep", "encoder_hidden_states", "controlnet_cond","return_dict"],
output_names=["down_block_res_samples", "mid_block_res_sample"], # has to be different from "sample" for correct tracing
dynamic_axes={
"sample": {0: "batch", 1: "channels", 2: "height", 3: "width"},
"timestep": {0: "batch"},
"encoder_hidden_states": {0: "batch", 1: "sequence"},
"controlnet_cond": {0: "batch", 2: "height", 3: "width"}
},
opset=opset,
)
if fp16:
cnet_path_model_path = str(cnet_path.absolute().as_posix())
convert_to_fp16(cnet_path_model_path)
print("ONNX controlnet saved to ", output_path)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--model_path",
type=str,
required=True,
help="Path to the `diffusers` checkpoint to convert (either a local directory or on the Hub).",
)
parser.add_argument("--output_path", type=str, required=True, help="Path to the output model.")
parser.add_argument(
"--opset",
default=15,
type=int,
help="The version of the ONNX operator set to use.",
)
parser.add_argument(
"--fp16",
action="store_true",
help="Export Controlnet in mixed `float16` mode"
)
parser.add_argument(
"--attention-slicing",
choices={"auto","max"},
type=str,
help="Attention slicing, off by default. Can be set to auto. Reduces amount of VRAM used."
)
args = parser.parse_args()
convert_models(args.model_path, args.output_path, args.opset, args.fp16 ,args.attention_slicing)