-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConvert_PyTorch_ONNX.py
263 lines (226 loc) · 10.4 KB
/
Convert_PyTorch_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#
# Convert PyTorch Stable Diffusion models to ONNX format, which then can be converted to OpenVINO IR format using Model Optimizer tool
#
# Based on OpenVINO toolkit notebook:
# https://github.com/openvinotoolkit/openvino_notebooks/blob/main/notebooks/225-stable-diffusion-text-to-image/225-stable-diffusion-text-to-image.ipynb
#
# Resolution
res_v = 512
res_h = 512
opset = 16
from diffusers import StableDiffusionPipeline
import torch
from pathlib import Path
import torch
import numpy as np
# Load the pre-trained weights of all components of the model.
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float32, low_cpu_mem_usage=True)
UNET_ONNX_PATH = Path('unet/unet.onnx')
UNET_OV_PATH = UNET_ONNX_PATH.parents[1] / 'unet.xml'
@torch.no_grad()
def convert_unet_onnx(pipe:StableDiffusionPipeline, onnx_path:Path):
"""
Convert Unet model to ONNX, then IR format.
Function accepts pipeline, prepares example inputs for ONNX conversion via torch.export,
Parameters:
pipe (StableDiffusionPipeline): Stable Diffusion pipeline
onnx_path (Path): File for storing onnx model
Returns:
None
"""
if not onnx_path.exists():
# prepare inputs
text = 'a photo of an astronaut riding a horse on mars'
text_encoder = pipe.text_encoder
input_ids = pipe.tokenizer(
text,
padding="max_length",
max_length=pipe.tokenizer.model_max_length,
truncation=True,
return_tensors="pt",
).input_ids
with torch.no_grad():
text_encoder_output = text_encoder(input_ids)
latents_shape = (2, 4, res_v // 8, res_h // 8)
latents = torch.randn(latents_shape)
t = torch.from_numpy(np.array(1, dtype=float))
# model size > 2Gb, it will be represented as onnx with external data files, we will store it in separated directory for avoid a lot of files in current directory
onnx_path.parent.mkdir(exist_ok=True, parents=True)
max_length = input_ids.shape[-1]
# we plan to use unet with classificator free guidence, in this cace conditionaly generated text embeddings should be concatenated with uncoditional
uncond_input = pipe.tokenizer([""], padding="max_length", max_length=max_length, return_tensors="pt")
uncond_embeddings = pipe.text_encoder(uncond_input.input_ids)[0]
encoder_hidden_state = torch.cat([uncond_embeddings, text_encoder_output[0]])
encoder_hidden_state = torch.cat([encoder_hidden_state, encoder_hidden_state], axis=1)
# to make sure that model works
pipe.unet(latents, t, encoder_hidden_state)[0]
with torch.no_grad():
torch.onnx.export(
pipe.unet,
(latents, t, encoder_hidden_state), str(onnx_path),
input_names = ['sample', 'timestep', 'encoder_hidden_state'],
output_names = ['out_sample'],
dynamic_axes = {
"sample": {0: "batch", 1: "channels", 2: "height", 3: "width"},
"encoder_hidden_state": {0: "batch", 1: "sequence"},
},
opset_version = opset
)
print('Unet successfully converted to ONNX')
if not UNET_OV_PATH.exists():
convert_unet_onnx(pipe, UNET_ONNX_PATH)
print(f"mo --input_model {UNET_ONNX_PATH} --compress_to_fp16")
print('Unet successfully converted to IR')
TEXT_ENCODER_ONNX_PATH = Path('text_encoder.onnx')
TEXT_ENCODER_OV_PATH = TEXT_ENCODER_ONNX_PATH.with_suffix('.xml')
@torch.no_grad()
def convert_encoder_onnx(pipe: StableDiffusionPipeline, onnx_path:Path):
"""
Convert Text Encoder model to ONNX.
Function accepts pipeline, prepares example inputs for ONNX conversion via torch.export,
Parameters:
pipe (StableDiffusionPipeline): Stable Diffusion pipeline
onnx_path (Path): File for storing onnx model
Returns:
None
"""
if not onnx_path.exists():
text = 'a photo of an astronaut riding a horse on mars'
text_encoder = pipe.text_encoder
input_ids = pipe.tokenizer(
text,
padding="max_length",
max_length=pipe.tokenizer.model_max_length,
truncation=True,
return_tensors="pt",
).input_ids
# switch model to inference mode
text_encoder.eval()
# disable gradients calculation for reducing memory consumption
with torch.no_grad():
# infer model, just to make sure that it works
text_encoder(input_ids)
# export model to ONNX format
torch.onnx.export(
text_encoder, # model instance
input_ids, # inputs for model tracing
onnx_path, # output file for saving result
input_names = ['input_ids'], # model input name for onnx representation
output_names = ['last_hidden_state', 'pooler_out'], # model output names for onnx representation
dynamic_axes = {'input_ids': {0: 'batch', 1: 'sequence'}},
opset_version = opset # onnx opset version for export
)
print('Text Encoder successfully converted to ONNX')
if not TEXT_ENCODER_OV_PATH.exists():
convert_encoder_onnx(pipe, TEXT_ENCODER_ONNX_PATH)
print(f"mo --input_model {TEXT_ENCODER_ONNX_PATH} --compress_to_fp16")
print('Text Encoder successfully converted to IR')
@torch.no_grad()
def convert_vae_decoder_onnx(pipe:StableDiffusionPipeline, onnx_path:Path):
"""
Convert VAE model to ONNX, then IR format.
Function accepts pipeline, creates wrapper class for export only necessary for inference part,
prepares example inputs for ONNX conversion via torch.export,
Parameters:
pipe (StableDiffusionPipeline): Stable Diffusion pipeline
onnx_path (Path): File for storing onnx model
Returns:
None
"""
class VAEDecoderWrapper(torch.nn.Module):
def __init__(self, vae):
super().__init__()
self.vae = vae
def forward(self, latents):
return self.vae.decode(latents)
if not onnx_path.exists():
vae_decoder = VAEDecoderWrapper(pipe.vae)
text = 'a photo of an astronaut riding a horse on mars'
text_encoder = pipe.text_encoder
input_ids = pipe.tokenizer(
text,
padding="max_length",
max_length=pipe.tokenizer.model_max_length,
truncation=True,
return_tensors="pt",
).input_ids
with torch.no_grad():
text_encoder_output = text_encoder(input_ids)
latents_shape = (2, 4, res_v // 8, res_h // 8)
latents = torch.randn(latents_shape)
t = torch.from_numpy(np.array(1, dtype=float))
max_length = input_ids.shape[-1]
uncond_input = pipe.tokenizer([""], padding="max_length", max_length=max_length, return_tensors="pt")
uncond_embeddings = pipe.text_encoder(uncond_input.input_ids)[0]
encoder_hidden_state = torch.cat([uncond_embeddings, text_encoder_output[0]])
output_latents = pipe.unet(latents, t, encoder_hidden_state)[0]
latents_uncond, latents_text = output_latents[0].unsqueeze(0), output_latents[1].unsqueeze(0)
latents = latents_uncond + 7.5 * (latents_text - latents_uncond)
vae_decoder.eval()
with torch.no_grad():
torch.onnx.export(
vae_decoder, latents, onnx_path, input_names=['latents'], output_names=['sample'],
dynamic_axes={"latents": {0: "batch", 1: "channels", 2: "height", 3: "width"}},
opset_version = opset # onnx opset version for export
)
print('VAE decoder successfully converted to ONNX')
VAE_ONNX_PATH = Path('vae_decoder.onnx')
VAE_OV_PATH = VAE_ONNX_PATH.with_suffix('.xml')
if not VAE_OV_PATH.exists():
convert_vae_decoder_onnx(pipe, VAE_ONNX_PATH)
print(f"mo --input_model {VAE_ONNX_PATH} --compress_to_fp16")
print('VAE successfully converted to IR')
@torch.no_grad()
def convert_vae_encoder_onnx(pipe:StableDiffusionPipeline, onnx_path:Path):
"""
Convert VAE model to ONNX, then IR format.
Function accepts pipeline, creates wrapper class for export only necessary for inference part,
prepares example inputs for ONNX conversion via torch.export,
Parameters:
pipe (StableDiffusionPipeline): Stable Diffusion pipeline
onnx_path (Path): File for storing onnx model
Returns:
None
"""
class VAEEncoderWrapper(torch.nn.Module):
def __init__(self, vae):
super().__init__()
self.vae = vae
def forward(self, sample):
latent = self.vae.encode(sample)[0].sample() #, return_dict
return latent
if not onnx_path.exists():
vae_encoder = VAEEncoderWrapper(pipe.vae)
text = 'a photo of an astronaut riding a horse on mars'
text_encoder = pipe.text_encoder
input_ids = pipe.tokenizer(
text,
padding="max_length",
max_length=pipe.tokenizer.model_max_length,
truncation=True,
return_tensors="pt",
).input_ids
with torch.no_grad():
text_encoder_output = text_encoder(input_ids)
image_shape = (1, 3, res_v, res_h)
image = torch.randn(image_shape)
t = torch.from_numpy(np.array(1, dtype=float))
max_length = input_ids.shape[-1]
uncond_input = pipe.tokenizer([""], padding="max_length", max_length=max_length, return_tensors="pt")
uncond_embeddings = pipe.text_encoder(uncond_input.input_ids)[0]
encoder_hidden_state = torch.cat([uncond_embeddings, text_encoder_output[0]])
vae_encoder.eval()
with torch.no_grad():
torch.onnx.export(
vae_encoder, (image,), onnx_path, input_names=['init_image'], output_names=['sample'],
dynamic_axes={"init_image": {0: "batch", 1: "channels", 2: "height", 3: "width"}},
opset_version = opset # onnx opset version for export
)
print('VAE encoder successfully converted to ONNX')
VAEE_ONNX_PATH = Path('vae_encoder.onnx')
VAEE_OV_PATH = VAEE_ONNX_PATH.with_suffix('.xml')
if not VAEE_OV_PATH.exists():
convert_vae_encoder_onnx(pipe, VAEE_ONNX_PATH)
print(f"mo --input_model {VAEE_ONNX_PATH} --compress_to_fp16")
print('VAE successfully converted to IR')
del pipe