-
Notifications
You must be signed in to change notification settings - Fork 34
/
tensorrt_convert.py
650 lines (593 loc) · 21.2 KB
/
tensorrt_convert.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
import torch
import sys
import os
import time
import comfy.model_management
import tensorrt as trt
import folder_paths
from tqdm import tqdm
# TODO:
# Make it more generic: less model specific code
# add output directory to tensorrt search path
if "tensorrt" in folder_paths.folder_names_and_paths:
folder_paths.folder_names_and_paths["tensorrt"][0].append(
os.path.join(folder_paths.get_output_directory(), "tensorrt")
)
folder_paths.folder_names_and_paths["tensorrt"][1].add(".engine")
else:
folder_paths.folder_names_and_paths["tensorrt"] = (
[os.path.join(folder_paths.get_output_directory(), "tensorrt")],
{".engine"},
)
class TQDMProgressMonitor(trt.IProgressMonitor):
def __init__(self):
trt.IProgressMonitor.__init__(self)
self._active_phases = {}
self._step_result = True
self.max_indent = 5
def phase_start(self, phase_name, parent_phase, num_steps):
leave = False
try:
if parent_phase is not None:
nbIndents = (
self._active_phases.get(parent_phase, {}).get(
"nbIndents", self.max_indent
)
+ 1
)
if nbIndents >= self.max_indent:
return
else:
nbIndents = 0
leave = True
self._active_phases[phase_name] = {
"tq": tqdm(
total=num_steps, desc=phase_name, leave=leave, position=nbIndents
),
"nbIndents": nbIndents,
"parent_phase": parent_phase,
}
except KeyboardInterrupt:
# The phase_start callback cannot directly cancel the build, so request the cancellation from within step_complete.
_step_result = False
def phase_finish(self, phase_name):
try:
if phase_name in self._active_phases.keys():
self._active_phases[phase_name]["tq"].update(
self._active_phases[phase_name]["tq"].total
- self._active_phases[phase_name]["tq"].n
)
parent_phase = self._active_phases[phase_name].get("parent_phase", None)
while parent_phase is not None:
self._active_phases[parent_phase]["tq"].refresh()
parent_phase = self._active_phases[parent_phase].get(
"parent_phase", None
)
if (
self._active_phases[phase_name]["parent_phase"]
in self._active_phases.keys()
):
self._active_phases[
self._active_phases[phase_name]["parent_phase"]
]["tq"].refresh()
del self._active_phases[phase_name]
pass
except KeyboardInterrupt:
_step_result = False
def step_complete(self, phase_name, step):
try:
if phase_name in self._active_phases.keys():
self._active_phases[phase_name]["tq"].update(
step - self._active_phases[phase_name]["tq"].n
)
return self._step_result
except KeyboardInterrupt:
# There is no need to propagate this exception to TensorRT. We can simply cancel the build.
return False
class TRT_MODEL_CONVERSION_BASE:
def __init__(self):
self.output_dir = folder_paths.get_output_directory()
self.temp_dir = folder_paths.get_temp_directory()
self.timing_cache_path = os.path.normpath(
os.path.join(os.path.join(os.path.dirname(os.path.realpath(__file__)), "timing_cache.trt"))
)
RETURN_TYPES = ()
FUNCTION = "convert"
OUTPUT_NODE = True
CATEGORY = "TensorRT"
@classmethod
def INPUT_TYPES(s):
raise NotImplementedError
# Sets up the builder to use the timing cache file, and creates it if it does not already exist
def _setup_timing_cache(self, config: trt.IBuilderConfig):
buffer = b""
if os.path.exists(self.timing_cache_path):
with open(self.timing_cache_path, mode="rb") as timing_cache_file:
buffer = timing_cache_file.read()
print("Read {} bytes from timing cache.".format(len(buffer)))
else:
print("No timing cache found; Initializing a new one.")
timing_cache: trt.ITimingCache = config.create_timing_cache(buffer)
config.set_timing_cache(timing_cache, ignore_mismatch=True)
# Saves the config's timing cache to file
def _save_timing_cache(self, config: trt.IBuilderConfig):
timing_cache: trt.ITimingCache = config.get_timing_cache()
with open(self.timing_cache_path, "wb") as timing_cache_file:
timing_cache_file.write(memoryview(timing_cache.serialize()))
def _convert(
self,
model,
filename_prefix,
batch_size_min,
batch_size_opt,
batch_size_max,
height_min,
height_opt,
height_max,
width_min,
width_opt,
width_max,
context_min,
context_opt,
context_max,
num_video_frames,
is_static: bool,
):
output_onnx = os.path.normpath(
os.path.join(
os.path.join(self.temp_dir, "{}".format(time.time())), "model.onnx"
)
)
comfy.model_management.unload_all_models()
comfy.model_management.load_models_gpu([model], force_patch_weights=True, force_full_load=True)
unet = model.model.diffusion_model
context_dim = model.model.model_config.unet_config.get("context_dim", None)
context_len = 77
context_len_min = context_len
y_dim = model.model.adm_channels
extra_input = {}
dtype = torch.float16
if isinstance(model.model, comfy.model_base.SD3): #SD3
context_embedder_config = model.model.model_config.unet_config.get("context_embedder_config", None)
if context_embedder_config is not None:
context_dim = context_embedder_config.get("params", {}).get("in_features", None)
context_len = 154 #NOTE: SD3 can have 77 or 154 depending on which text encoders are used, this is why context_len_min stays 77
elif isinstance(model.model, comfy.model_base.AuraFlow):
context_dim = 2048
context_len_min = 256
context_len = 256
elif isinstance(model.model, comfy.model_base.Flux):
context_dim = model.model.model_config.unet_config.get("context_in_dim", None)
context_len_min = 256
context_len = 256
y_dim = model.model.model_config.unet_config.get("vec_in_dim", None)
extra_input = {"guidance": ()}
dtype = torch.bfloat16
if context_dim is not None:
input_names = ["x", "timesteps", "context"]
output_names = ["h"]
dynamic_axes = {
"x": {0: "batch", 2: "height", 3: "width"},
"timesteps": {0: "batch"},
"context": {0: "batch", 1: "num_embeds"},
}
transformer_options = model.model_options['transformer_options'].copy()
if model.model.model_config.unet_config.get(
"use_temporal_resblock", False
): # SVD
batch_size_min = num_video_frames * batch_size_min
batch_size_opt = num_video_frames * batch_size_opt
batch_size_max = num_video_frames * batch_size_max
class UNET(torch.nn.Module):
def forward(self, x, timesteps, context, y):
return self.unet(
x,
timesteps,
context,
y,
num_video_frames=self.num_video_frames,
transformer_options=self.transformer_options,
)
svd_unet = UNET()
svd_unet.num_video_frames = num_video_frames
svd_unet.unet = unet
svd_unet.transformer_options = transformer_options
unet = svd_unet
context_len_min = context_len = 1
else:
class UNET(torch.nn.Module):
def forward(self, x, timesteps, context, *args):
extras = input_names[3:]
extra_args = {}
for i in range(len(extras)):
extra_args[extras[i]] = args[i]
return self.unet(x, timesteps, context, transformer_options=self.transformer_options, **extra_args)
_unet = UNET()
_unet.unet = unet
_unet.transformer_options = transformer_options
unet = _unet
input_channels = model.model.model_config.unet_config.get("in_channels", 4)
inputs_shapes_min = (
(batch_size_min, input_channels, height_min // 8, width_min // 8),
(batch_size_min,),
(batch_size_min, context_len_min * context_min, context_dim),
)
inputs_shapes_opt = (
(batch_size_opt, input_channels, height_opt // 8, width_opt // 8),
(batch_size_opt,),
(batch_size_opt, context_len * context_opt, context_dim),
)
inputs_shapes_max = (
(batch_size_max, input_channels, height_max // 8, width_max // 8),
(batch_size_max,),
(batch_size_max, context_len * context_max, context_dim),
)
if y_dim > 0:
input_names.append("y")
dynamic_axes["y"] = {0: "batch"}
inputs_shapes_min += ((batch_size_min, y_dim),)
inputs_shapes_opt += ((batch_size_opt, y_dim),)
inputs_shapes_max += ((batch_size_max, y_dim),)
for k in extra_input:
input_names.append(k)
dynamic_axes[k] = {0: "batch"}
inputs_shapes_min += ((batch_size_min,) + extra_input[k],)
inputs_shapes_opt += ((batch_size_opt,) + extra_input[k],)
inputs_shapes_max += ((batch_size_max,) + extra_input[k],)
inputs = ()
for shape in inputs_shapes_opt:
inputs += (
torch.zeros(
shape,
device=comfy.model_management.get_torch_device(),
dtype=dtype,
),
)
else:
print("ERROR: model not supported.")
return ()
os.makedirs(os.path.dirname(output_onnx), exist_ok=True)
torch.onnx.export(
unet,
inputs,
output_onnx,
verbose=False,
input_names=input_names,
output_names=output_names,
opset_version=17,
dynamic_axes=dynamic_axes,
)
comfy.model_management.unload_all_models()
comfy.model_management.soft_empty_cache()
# TRT conversion starts here
logger = trt.Logger(trt.Logger.INFO)
builder = trt.Builder(logger)
network = builder.create_network(
1 << int(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH)
)
parser = trt.OnnxParser(network, logger)
success = parser.parse_from_file(output_onnx)
for idx in range(parser.num_errors):
print(parser.get_error(idx))
if not success:
print("ONNX load ERROR")
return ()
config = builder.create_builder_config()
profile = builder.create_optimization_profile()
self._setup_timing_cache(config)
config.progress_monitor = TQDMProgressMonitor()
prefix_encode = ""
for k in range(len(input_names)):
min_shape = inputs_shapes_min[k]
opt_shape = inputs_shapes_opt[k]
max_shape = inputs_shapes_max[k]
profile.set_shape(input_names[k], min_shape, opt_shape, max_shape)
# Encode shapes to filename
encode = lambda a: ".".join(map(lambda x: str(x), a))
prefix_encode += "{}#{}#{}#{};".format(
input_names[k], encode(min_shape), encode(opt_shape), encode(max_shape)
)
if dtype == torch.float16:
config.set_flag(trt.BuilderFlag.FP16)
if dtype == torch.bfloat16:
config.set_flag(trt.BuilderFlag.BF16)
config.add_optimization_profile(profile)
if is_static:
filename_prefix = "{}_${}".format(
filename_prefix,
"-".join(
(
"stat",
"b",
str(batch_size_opt),
"h",
str(height_opt),
"w",
str(width_opt),
)
),
)
else:
filename_prefix = "{}_${}".format(
filename_prefix,
"-".join(
(
"dyn",
"b",
str(batch_size_min),
str(batch_size_max),
str(batch_size_opt),
"h",
str(height_min),
str(height_max),
str(height_opt),
"w",
str(width_min),
str(width_max),
str(width_opt),
)
),
)
serialized_engine = builder.build_serialized_network(network, config)
full_output_folder, filename, counter, subfolder, filename_prefix = (
folder_paths.get_save_image_path(filename_prefix, self.output_dir)
)
output_trt_engine = os.path.join(
full_output_folder, f"{filename}_{counter:05}_.engine"
)
with open(output_trt_engine, "wb") as f:
f.write(serialized_engine)
self._save_timing_cache(config)
return ()
class DYNAMIC_TRT_MODEL_CONVERSION(TRT_MODEL_CONVERSION_BASE):
def __init__(self):
super(DYNAMIC_TRT_MODEL_CONVERSION, self).__init__()
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"model": ("MODEL",),
"filename_prefix": ("STRING", {"default": "tensorrt/ComfyUI_DYN"}),
"batch_size_min": (
"INT",
{
"default": 1,
"min": 1,
"max": 100,
"step": 1,
},
),
"batch_size_opt": (
"INT",
{
"default": 1,
"min": 1,
"max": 100,
"step": 1,
},
),
"batch_size_max": (
"INT",
{
"default": 1,
"min": 1,
"max": 100,
"step": 1,
},
),
"height_min": (
"INT",
{
"default": 512,
"min": 256,
"max": 4096,
"step": 64,
},
),
"height_opt": (
"INT",
{
"default": 512,
"min": 256,
"max": 4096,
"step": 64,
},
),
"height_max": (
"INT",
{
"default": 512,
"min": 256,
"max": 4096,
"step": 64,
},
),
"width_min": (
"INT",
{
"default": 512,
"min": 256,
"max": 4096,
"step": 64,
},
),
"width_opt": (
"INT",
{
"default": 512,
"min": 256,
"max": 4096,
"step": 64,
},
),
"width_max": (
"INT",
{
"default": 512,
"min": 256,
"max": 4096,
"step": 64,
},
),
"context_min": (
"INT",
{
"default": 1,
"min": 1,
"max": 128,
"step": 1,
},
),
"context_opt": (
"INT",
{
"default": 1,
"min": 1,
"max": 128,
"step": 1,
},
),
"context_max": (
"INT",
{
"default": 1,
"min": 1,
"max": 128,
"step": 1,
},
),
"num_video_frames": (
"INT",
{
"default": 14,
"min": 0,
"max": 1000,
"step": 1,
},
),
},
}
def convert(
self,
model,
filename_prefix,
batch_size_min,
batch_size_opt,
batch_size_max,
height_min,
height_opt,
height_max,
width_min,
width_opt,
width_max,
context_min,
context_opt,
context_max,
num_video_frames,
):
return super()._convert(
model,
filename_prefix,
batch_size_min,
batch_size_opt,
batch_size_max,
height_min,
height_opt,
height_max,
width_min,
width_opt,
width_max,
context_min,
context_opt,
context_max,
num_video_frames,
is_static=False,
)
class STATIC_TRT_MODEL_CONVERSION(TRT_MODEL_CONVERSION_BASE):
def __init__(self):
super(STATIC_TRT_MODEL_CONVERSION, self).__init__()
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"model": ("MODEL",),
"filename_prefix": ("STRING", {"default": "tensorrt/ComfyUI_STAT"}),
"batch_size_opt": (
"INT",
{
"default": 1,
"min": 1,
"max": 100,
"step": 1,
},
),
"height_opt": (
"INT",
{
"default": 512,
"min": 256,
"max": 4096,
"step": 64,
},
),
"width_opt": (
"INT",
{
"default": 512,
"min": 256,
"max": 4096,
"step": 64,
},
),
"context_opt": (
"INT",
{
"default": 1,
"min": 1,
"max": 128,
"step": 1,
},
),
"num_video_frames": (
"INT",
{
"default": 14,
"min": 0,
"max": 1000,
"step": 1,
},
),
},
}
def convert(
self,
model,
filename_prefix,
batch_size_opt,
height_opt,
width_opt,
context_opt,
num_video_frames,
):
return super()._convert(
model,
filename_prefix,
batch_size_opt,
batch_size_opt,
batch_size_opt,
height_opt,
height_opt,
height_opt,
width_opt,
width_opt,
width_opt,
context_opt,
context_opt,
context_opt,
num_video_frames,
is_static=True,
)
NODE_CLASS_MAPPINGS = {
"DYNAMIC_TRT_MODEL_CONVERSION": DYNAMIC_TRT_MODEL_CONVERSION,
"STATIC_TRT_MODEL_CONVERSION": STATIC_TRT_MODEL_CONVERSION,
}