forked from openvinotoolkit/openvino.genai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
1461 lines (1295 loc) · 59.2 KB
/
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# Copyright (C) 2023-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import sys
import gc
import time
import copy
import logging as log
from argparse import ArgumentParser
from functools import wraps
from pathlib import Path
from typing import Tuple, Union, Dict, Optional, TYPE_CHECKING
import nncf
import torch
from diffusers import (
StableDiffusionPipeline,
StableDiffusionXLImg2ImgPipeline,
LDMSuperResolutionPipeline,
)
from diffusers import UNet2DConditionModel, AutoencoderTiny, LCMScheduler
from nncf.torch.model_creation import is_wrapped_model
from openvino import Type as OVType, PartialShape, save_model, convert_model
from openvino.runtime import Core, get_version
from optimum.exporters import TasksManager
from optimum.utils import DEFAULT_DUMMY_SHAPES
from optimum.intel.openvino.configuration import OVConfig
from optimum.exporters.utils import get_encoder_decoder_models_for_export
from optimum.exporters.openvino import export_models
from optimum.exporters.openvino.model_patcher import patch_model_with_bettertransformer
from optimum.intel.openvino import (
OVModelForSeq2SeqLM,
OVStableDiffusionPipeline,
OVStableDiffusionXLPipeline,
OVLatentConsistencyModelPipeline,
OV_XML_FILE_NAME,
OV_DECODER_NAME,
OV_DECODER_WITH_PAST_NAME,
OV_ENCODER_NAME,
)
from optimum.utils.import_utils import is_torch_available, is_diffusers_available
from optimum.exporters.utils import _get_submodels_and_export_configs
from transformers import (
AutoTokenizer,
AutoConfig,
AutoModelForCausalLM,
AutoModelForSeq2SeqLM,
AutoModel,
)
from utils.nncf_utils import get_compressed_path
from utils.model_utils import add_stateful_model_arguments
from optimum.exporters.openvino.utils import flattenize_inputs
from utils.conversion_utils.convert_patch import patch_model_for_optimum_export
from utils.conversion_utils.better_transformer_patch import (
register_bettertransformer_config,
)
import utils.conversion_utils.export_configs # noqa: F401,F403
from utils.conversion_utils.helpers import (
PYTORCH_DIR,
OV_DIR,
GPTQ_DIR,
PYTORCH_COMPRESS_WEIGHTS_DIR,
is_torch_compression,
is_ov_compression,
is_gptq,
is_fp16,
patch_gptq,
unpatch_gptq,
save_tokenizer,
compress_ov_model_weights_helper,
save_ov_model_helper,
get_fp_path,
is_ov_model_provided,
is_int8_compression,
BackendType,
)
from utils.nncf_utils import COMPRESSION_OPTIONS
if TYPE_CHECKING:
from optimum.onnx.configuration import OnnxConfig
if is_torch_available():
from transformers.modeling_utils import PreTrainedModel
if is_diffusers_available():
from diffusers import ModelMixin
register_bettertransformer_config()
def compress_torchmodels(
models_and_export_configs,
stateful: bool = True,
dummy_shapes: Optional[Dict] = None,
compression_options: Optional[Dict] = None,
):
if dummy_shapes is None:
dummy_shapes = {}
if compression_options is None:
compression_options = {}
for model_name in models_and_export_configs.keys():
submodel, sub_export_config = models_and_export_configs[model_name]
if stateful:
submodel = patch_model_with_bettertransformer(submodel)
if is_wrapped_model(submodel):
dataset = None
else:
dummy_inputs = sub_export_config.generate_dummy_inputs(framework="pt", **dummy_shapes)
dataset = nncf.Dataset([dummy_inputs])
compressed_submodel = nncf.compress_weights(submodel, dataset=dataset, **compression_options)
models_and_export_configs[model_name] = (compressed_submodel, sub_export_config)
return models_and_export_configs
def convert_optimum_causallm_base(model, args, model_config=None, compress_only=False):
tokenizer_id = args.tokenizer_id or args.model_id
tok = AutoTokenizer.from_pretrained(tokenizer_id, trust_remote_code=True)
precision = args.precision
gptq_applied = is_gptq(model_config)
pt_compress_weights = is_torch_compression(args)
if args.stateful:
log.warning(
"usage --stateful flag is deprecated and will be removed in future, default behaviour is export stateful model"
" please use --disable_stateful if you need model without state"
)
if not compress_only:
model_config = model.config
model = patch_model_for_optimum_export(model)
precision = precision if not gptq_applied else GPTQ_DIR.format(precision=args.precision)
ov_out_dir = Path(args.output_dir) / PYTORCH_DIR / OV_DIR / precision
if gptq_applied and args.compress_weights:
log.info("Weights compression will be skipped for GPTQ models")
if args.save_orig:
pt_out_dir = Path(args.output_dir) / PYTORCH_DIR
model.save_pretrained(pt_out_dir)
save_tokenizer(tok, pt_out_dir)
dummy_shapes = DEFAULT_DUMMY_SHAPES
export_config, models_and_export_configs = _get_submodels_and_export_configs(
model=model,
task="text-generation-with-past",
exporter="openvino",
custom_export_configs={},
custom_architecture=None,
fn_get_submodels=None,
preprocessors=None,
_variant="default",
monolith=False,
library_name="transformers"
)
if "decoder_with_past_model" in models_and_export_configs:
models_and_export_configs = {"model": models_and_export_configs["decoder_with_past_model"]}
model.config.save_pretrained(ov_out_dir)
files_subpaths = ["openvino_" + model_name + ".xml" for model_name in models_and_export_configs.keys()]
export_models(
models_and_export_configs=models_and_export_configs,
output_dir=ov_out_dir,
output_names=files_subpaths,
input_shapes=dummy_shapes,
device="cpu",
ov_config=OVConfig(dtype="fp16") if args.precision == "FP16" else None,
model_kwargs={},
stateful=not args.disable_stateful,
)
save_tokenizer(tok, ov_out_dir)
if is_ov_compression(args) and not gptq_applied:
if compress_only:
fp_path = get_fp_path(args, "openvino_model.xml")
log.info(
f"Model conversion to {args.precision} will be skipped as found converted model {fp_path}."
"If it is not expected behaviour, please remove previously converted model or use --force_convert option"
)
for compress_option in args.compress_weights:
log.info(f"Compress model weights to {compress_option}")
optimized_dir = get_compressed_path(args.output_dir, args.precision, compress_option)
model_config.save_pretrained(optimized_dir)
fp_path = get_fp_path(args, "openvino_model.xml")
ir_model = Core().read_model(fp_path)
compress_ov_model_weights_helper(
ir_model,
tok,
model_config,
optimized_dir,
compress_option,
is_fp16(args),
args,
)
if pt_compress_weights and not gptq_applied:
compression_modes = []
for cw in args.compress_weights:
if is_int8_compression(cw):
compression_modes.append(cw)
assert compression_modes, "Only INT8 compression supported for PyTorch backend"
number_compression_modes = len(compression_modes)
original_model = model
for idx, compress_mode in enumerate(compression_modes):
if number_compression_modes - idx > 1:
model = copy.deepcopy(original_model)
else:
model = original_model
_, models_and_export_configs = _get_submodels_and_export_configs(
model=model,
exporter="openvino",
task="text-generation-with-past",
custom_export_configs={},
custom_architecture=None,
fn_get_submodels=None,
preprocessors=None,
_variant="default",
monolith=False,
library_name="transformers"
)
compression_options = COMPRESSION_OPTIONS[compress_mode]
models_and_export_configs = compress_torchmodels(
models_and_export_configs,
stateful=not args.disable_stateful,
dummy_shapes=dummy_shapes,
compression_options=compression_options,
)
pt_out_dir = (
Path(args.output_dir)
/ PYTORCH_DIR
/ OV_DIR
/ PYTORCH_COMPRESS_WEIGHTS_DIR.format(precision=precision, compression=compress_mode)
)
model.config.save_pretrained(pt_out_dir)
export_models(
models_and_export_configs=models_and_export_configs,
output_dir=pt_out_dir,
output_names=files_subpaths,
input_shapes=dummy_shapes,
device="cpu",
ov_config=OVConfig(dtype="fp16") if args.precision == "FP16" else None,
model_kwargs={},
stateful=not args.disable_stateful,
)
save_tokenizer(tok, pt_out_dir)
return
def convert_causal_lm(args):
config = AutoConfig.from_pretrained(args.model_id, trust_remote_code=True)
cuda, post_init = patch_gptq(config)
ov_out_dir = Path(args.output_dir) / PYTORCH_DIR / OV_DIR
precision = args.precision
compression_only = (
is_ov_compression(args)
and not is_torch_compression(args)
and is_ov_model_provided(args.model_id, ov_out_dir, precision)
)
model_kwargs = {}
if post_init is not None:
model_kwargs["torch_dtype"] = torch.float32
model = None
if not compression_only:
model = AutoModelForCausalLM.from_pretrained(
args.model_id, trust_remote_code=True, config=config, **model_kwargs
)
try:
model.to(torch.float32)
except Exception:
pass
convert_optimum_causallm_base(model, args, config, compression_only)
if post_init is not None:
unpatch_gptq(cuda, post_init)
def convert_seq2seq(args):
config = AutoConfig.from_pretrained(args.model_id, trust_remote_code=True)
tokenizer_id = args.model_id if "blenderbot-9B" not in args.model_id else "facebook/blenderbot-3B"
tok = AutoTokenizer.from_pretrained(tokenizer_id, trust_remote_code=True)
pt_compress_weights = is_torch_compression(args)
if args.save_orig or pt_compress_weights:
pt_model = AutoModelForSeq2SeqLM.from_pretrained(
args.model_id,
trust_remote_code=True,
config=config,
)
if args.save_orig:
pt_out_dir = Path(args.output_dir) / PYTORCH_DIR
pt_model.save_pretrained(pt_out_dir)
save_tokenizer(tok, pt_out_dir)
if pt_compress_weights:
compression_modes = []
for cw in args.compress_weights:
if is_int8_compression(cw):
compression_modes.append(cw)
assert compression_modes, "Only INT8 compression supported for PyTorch backend"
for idx, compress_mode in enumerate(compression_modes):
if idx > 0:
pt_model = AutoModelForSeq2SeqLM.from_pretrained(
args.model_id,
trust_remote_code=True,
config=config,
)
export_config_constructor = TasksManager.get_exporter_config_constructor(
model=pt_model, exporter="openvino", task="text2text-generation"
)
export_config = export_config_constructor(pt_model.config, use_past=True)
models_and_export_configs = get_encoder_decoder_models_for_export(pt_model, export_config)
compression_options = COMPRESSION_OPTIONS[compress_mode]
models_and_export_configs = compress_torchmodels(
models_and_export_configs, compression_options=compression_options
)
encoder_file_name = Path("encoder") / OV_ENCODER_NAME
decoder_file_name = Path("decoder") / OV_DECODER_NAME
decoder_with_past_file_name = Path("decoder_with_past") / OV_DECODER_WITH_PAST_NAME
output_names = [
encoder_file_name,
decoder_file_name,
decoder_with_past_file_name,
]
save_dir_path = (
Path(args.output_dir)
/ PYTORCH_DIR
/ OV_DIR
/ PYTORCH_COMPRESS_WEIGHTS_DIR.format(precision=args.precision, compression=compress_mode)
)
try:
export_models(
models_and_export_configs=models_and_export_configs,
opset=export_config.DEFAULT_ONNX_OPSET,
output_dir=save_dir_path,
output_names=output_names,
ov_config=OVConfig(dtype="fp16") if args.precision == "FP16" else None,
stateful=False
)
save_tokenizer(tok, save_dir_path)
except Exception as ex:
log.warning(f"PT weights compression failed with {ex}, please use OpenVINO backend instead")
del pt_model
gc.collect()
# skip openvino compression pipeline if pytorch compression pipeline was used
if pt_compress_weights:
return
ov_compression = is_ov_compression(args)
ov_encoder = is_ov_model_provided(args.model_id, args.output_dir, args.precision, "openvino_encoder_model.xml")
ov_decoder = is_ov_model_provided(args.model_id, args.output_dir, args.precision, "openvino_decoder_model.xml")
compress_only = ov_compression and not args.force_convert and ov_encoder and ov_decoder
if not compress_only:
start = time.perf_counter()
model = OVModelForSeq2SeqLM.from_pretrained(
args.model_id,
export=True,
compile=False,
trust_remote_code=True,
config=AutoConfig.from_pretrained(args.model_id, trust_remote_code=True),
load_in_8bit=False
)
if is_fp16(args):
model.half()
end = time.perf_counter()
log.info(f"Conversion total time {end - start}s")
start1 = time.perf_counter()
ov_out_dir = Path(args.output_dir) / PYTORCH_DIR / OV_DIR / args.precision
model.save_pretrained(ov_out_dir)
end1 = time.perf_counter()
log.info(f"Serialization total time {end1 - start1}s")
save_tokenizer(tok, ov_out_dir)
del model
gc.collect()
if ov_compression:
if compress_only:
log.info(
f"Model conversion to {args.precision} will be skipped as found converted model. "
"If it is not expected behaviour, please remove previously converted model or use --force_convert option"
)
for compress_option in args.compress_weights:
log.info(f"Compress model weights to {compress_option}")
optimized_dir = get_compressed_path(args.output_dir, args.precision, compress_option)
fp_enc_path = get_fp_path(args, "openvino_encoder_model.xml")
enc_model = Core().read_model(fp_enc_path)
compress_ov_model_weights_helper(
enc_model,
tok,
config,
optimized_dir,
compress_option,
is_fp16(args),
args,
"openvino_encoder_model",
)
fp_dec_path = get_fp_path(args, "openvino_decoder_model.xml")
dec_model = Core().read_model(fp_dec_path)
compress_ov_model_weights_helper(
dec_model,
tok,
config,
optimized_dir,
compress_option,
is_fp16(args),
args,
"openvino_decoder_model",
)
fp_dec_path = get_fp_path(args, "openvino_decoder_with_past_model.xml")
if fp_dec_path is not None:
dec_model = Core().read_model(fp_dec_path)
compress_ov_model_weights_helper(
dec_model,
tok,
config,
optimized_dir,
compress_option,
is_fp16(args),
args,
"openvino_decoder_with_past_model",
)
def _get_submodels_for_export_stable_diffusion(
pipeline: "StableDiffusionPipeline",
) -> Dict[str, Union["PreTrainedModel", "ModelMixin"]]:
"""
Returns the components of a Stable Diffusion model.
"""
from diffusers import StableDiffusionXLImg2ImgPipeline
models_for_export = {}
if isinstance(pipeline, StableDiffusionXLImg2ImgPipeline):
projection_dim = pipeline.text_encoder_2.config.projection_dim
else:
projection_dim = pipeline.text_encoder.config.projection_dim
# Text encoder
if pipeline.text_encoder is not None:
if isinstance(pipeline, StableDiffusionXLImg2ImgPipeline):
pipeline.text_encoder.config.output_hidden_states = True
models_for_export["text_encoder"] = pipeline.text_encoder
# U-NET
pipeline.unet.config.text_encoder_projection_dim = projection_dim
# The U-NET time_ids inputs shapes depends on the value of `requires_aesthetics_score`
# https://github.com/huggingface/diffusers/blob/v0.18.2/src/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_img2img.py#L571
pipeline.unet.config.requires_aesthetics_score = getattr(pipeline.config, "requires_aesthetics_score", False)
models_for_export["unet"] = pipeline.unet
# VAE Encoder https://github.com/huggingface/diffusers/blob/v0.11.1/src/diffusers/models/vae.py#L565
vae_encoder = copy.deepcopy(pipeline.vae)
if isinstance(vae_encoder, AutoencoderTiny):
vae_encoder.forward = lambda sample: {"latent_sample": vae_encoder.encode(x=sample)["latents"]}
else:
vae_encoder.forward = lambda sample: {"latent_sample": vae_encoder.encode(x=sample)["latent_dist"].sample()}
models_for_export["vae_encoder"] = vae_encoder
# VAE Decoder https://github.com/huggingface/diffusers/blob/v0.11.1/src/diffusers/models/vae.py#L600
vae_decoder = copy.deepcopy(pipeline.vae)
if isinstance(vae_encoder, AutoencoderTiny):
vae_decoder.forward = lambda latent_sample: vae_decoder.decode(latent_sample)
else:
vae_decoder.forward = lambda latent_sample: vae_decoder.decode(z=latent_sample)
models_for_export["vae_decoder"] = vae_decoder
text_encoder_2 = getattr(pipeline, "text_encoder_2", None)
if text_encoder_2 is not None:
text_encoder_2.config.output_hidden_states = True
models_for_export["text_encoder_2"] = text_encoder_2
return models_for_export
def get_stable_diffusion_models_for_export(
pipeline: "StableDiffusionPipeline",
int_dtype: str = "int64",
float_dtype: str = "fp32",
) -> Dict[str, Tuple[Union["PreTrainedModel", "ModelMixin"], "OnnxConfig"]]:
"""
Returns the components of a Stable Diffusion model and their subsequent onnx configs.
Args:
pipeline ([`StableDiffusionPipeline`]):
The model to export.
int_dtype (`str`, defaults to `"int64"`):
The data type of integer tensors, could be ["int64", "int32", "int8"], default to "int64".
float_dtype (`str`, defaults to `"fp32"`):
The data type of float tensors, could be ["fp32", "fp16", "bf16"], default to "fp32".
Returns:
`Dict[str, Tuple[Union[`PreTrainedModel`, `TFPreTrainedModel`], `OnnxConfig`]: A Dict containing the model and
onnx configs for the different components of the model.
"""
models_for_export = _get_submodels_for_export_stable_diffusion(pipeline)
# Text encoder
if "text_encoder" in models_for_export:
text_encoder_config_constructor = TasksManager.get_exporter_config_constructor(
model=pipeline.text_encoder,
exporter="openvino",
task="feature-extraction",
library_name="diffusers",
)
text_encoder_export_config = text_encoder_config_constructor(
pipeline.text_encoder.config, int_dtype=int_dtype, float_dtype=float_dtype
)
models_for_export["text_encoder"] = (models_for_export["text_encoder"], text_encoder_export_config)
# U-NET
export_config_constructor = TasksManager.get_exporter_config_constructor(
model=pipeline.unet,
exporter="openvino",
task="semantic-segmentation",
model_type="unet",
library_name="diffusers",
)
unet_export_config = export_config_constructor(pipeline.unet.config, int_dtype=int_dtype, float_dtype=float_dtype)
models_for_export["unet"] = (models_for_export["unet"], unet_export_config)
# VAE Encoder https://github.com/huggingface/diffusers/blob/v0.11.1/src/diffusers/models/vae.py#L565
vae_encoder = models_for_export["vae_encoder"]
vae_config_constructor = TasksManager.get_exporter_config_constructor(
model=vae_encoder,
exporter="openvino",
task="semantic-segmentation",
model_type="vae-encoder",
library_name="diffusers",
)
vae_export_config = vae_config_constructor(vae_encoder.config, int_dtype=int_dtype, float_dtype=float_dtype)
models_for_export["vae_encoder"] = (vae_encoder, vae_export_config)
# VAE Decoder https://github.com/huggingface/diffusers/blob/v0.11.1/src/diffusers/models/vae.py#L600
vae_decoder = models_for_export["vae_decoder"]
vae_config_constructor = TasksManager.get_exporter_config_constructor(
model=vae_decoder,
exporter="openvino",
task="semantic-segmentation",
model_type="vae-decoder",
library_name="diffusers",
)
vae_export_config = vae_config_constructor(vae_decoder.config, int_dtype=int_dtype, float_dtype=float_dtype)
models_for_export["vae_decoder"] = (vae_decoder, vae_export_config)
if "text_encoder_2" in models_for_export:
export_config_constructor = TasksManager.get_exporter_config_constructor(
model=pipeline.text_encoder_2,
exporter="openvino",
task="feature-extraction",
model_type="clip-text-with-projection",
library_name="diffusers",
)
export_config = export_config_constructor(
pipeline.text_encoder_2.config, int_dtype=int_dtype, float_dtype=float_dtype
)
models_for_export["text_encoder_2"] = (models_for_export["text_encoder_2"], export_config)
return models_for_export
def convert_sd_prepared_for_export_common(pipeline, models_and_export_configs, output_dir, args):
for model_name in models_and_export_configs:
subcomponent = models_and_export_configs[model_name][0]
if hasattr(subcomponent, "save_config"):
subcomponent.save_config(output_dir / model_name)
elif hasattr(subcomponent, "config") and hasattr(subcomponent.config, "save_pretrained"):
subcomponent.config.save_pretrained(output_dir / model_name)
files_subpaths = [Path(name_dir) / OV_XML_FILE_NAME for name_dir in models_and_export_configs]
# Saving the additional components needed to perform inference.
pipeline.scheduler.save_pretrained(output_dir.joinpath("scheduler"))
feature_extractor = getattr(pipeline, "feature_extractor", None)
if feature_extractor is not None:
feature_extractor.save_pretrained(output_dir.joinpath("feature_extractor"))
tokenizer = getattr(pipeline, "tokenizer", None)
if tokenizer is not None:
tokenizer.save_pretrained(output_dir.joinpath("tokenizer"))
tokenizer_2 = getattr(pipeline, "tokenizer_2", None)
if tokenizer_2 is not None:
tokenizer_2.save_pretrained(output_dir.joinpath("tokenizer_2"))
pipeline.save_config(output_dir)
export_models(
models_and_export_configs=models_and_export_configs,
output_dir=output_dir,
output_names=files_subpaths,
ov_config=OVConfig(dtype="fp16") if args.precision == "FP16" else None,
stateful=False
)
def convert_sd_common(pipeline, output_dir, args):
models_and_export_configs = get_stable_diffusion_models_for_export(pipeline)
convert_sd_prepared_for_export_common(pipeline, models_and_export_configs, output_dir, args)
def convert_sd(args):
pt_compress_weights = is_torch_compression(args)
pt_model = StableDiffusionPipeline.from_pretrained(args.model_id)
if args.save_orig:
pt_model.save_pretrained(Path(args.output_dir) / PYTORCH_DIR)
output_dir = Path(args.output_dir) / PYTORCH_DIR / OV_DIR / args.precision
models_and_export_configs = get_stable_diffusion_models_for_export(pt_model)
convert_sd_prepared_for_export_common(pt_model, models_and_export_configs, output_dir, args)
if pt_compress_weights:
compression_modes = []
for cw in args.compress_weights:
if is_int8_compression(cw):
compression_modes.append(cw)
assert compression_modes, "Only INT8 compression supported for PyTorch backend"
for idx, compress_mode in enumerate(compression_modes):
if idx > 0:
pt_model = StableDiffusionPipeline.from_pretrained(args.model_id)
models_and_export_configs = get_stable_diffusion_models_for_export(pt_model)
target_models_and_export_configs = {
k: models_and_export_configs[k] for k in ("text_encoder", "unet", "vae_decoder")
}
compression_options = COMPRESSION_OPTIONS[compress_mode]
models_and_export_configs.update(
compress_torchmodels(target_models_and_export_configs, compression_options=compression_options)
)
output = (
Path(args.output_dir)
/ PYTORCH_DIR
/ OV_DIR
/ PYTORCH_COMPRESS_WEIGHTS_DIR.format(precision=args.precision, compression=compress_mode)
)
convert_sd_prepared_for_export_common(pt_model, models_and_export_configs, output, args)
del pt_model
gc.collect()
if is_ov_compression(args):
for weigths_compression_option in args.compress_weights:
if not is_int8_compression(weigths_compression_option):
log.warning(
f"Weights compression {weigths_compression_option} is not supported for SD, will be ignored"
)
continue
model = OVStableDiffusionPipeline.from_pretrained(output_dir, compile=False)
ov_int8_dir = get_compressed_path(args.output_dir, args.precision, weigths_compression_option)
model.text_encoder.model = nncf.compress_weights(model.text_encoder.model)
model.unet.model = nncf.compress_weights(model.unet.model)
model.vae_decoder.model = nncf.compress_weights(model.vae_decoder.model)
model.save_pretrained(ov_int8_dir)
del model
gc.collect()
def convert_lcm(args):
pt_compress_weights = is_torch_compression(args)
pt_model = StableDiffusionPipeline.from_pretrained(args.model_id)
if args.save_orig:
pt_model.save_pretrained(Path(args.output_dir) / PYTORCH_DIR)
output_dir = Path(args.output_dir) / PYTORCH_DIR / OV_DIR / args.precision
models_and_export_configs = get_stable_diffusion_models_for_export(pt_model)
convert_sd_prepared_for_export_common(pt_model, models_and_export_configs, output_dir, args)
if pt_compress_weights:
compression_modes = []
for cw in args.compress_weights:
if is_int8_compression(cw):
compression_modes.append(cw)
assert compression_modes, "Only INT8 compression supported for PyTorch backend"
for idx, compress_mode in enumerate(compression_modes):
if idx > 0:
pt_model = StableDiffusionPipeline.from_pretrained(args.model_id)
models_and_export_configs = get_stable_diffusion_models_for_export(pt_model)
target_models_and_export_configs = {
k: models_and_export_configs[k] for k in ("text_encoder", "unet", "vae_decoder")
}
compression_options = COMPRESSION_OPTIONS[compress_mode]
models_and_export_configs.update(
compress_torchmodels(target_models_and_export_configs, compression_options=compression_options)
)
output = (
Path(args.output_dir)
/ PYTORCH_DIR
/ OV_DIR
/ PYTORCH_COMPRESS_WEIGHTS_DIR.format(precision=args.precision, compression=compress_mode)
)
convert_sd_prepared_for_export_common(pt_model, models_and_export_configs, output, args)
del pt_model
gc.collect()
if is_ov_compression(args):
for weigths_compression_option in args.compress_weights:
if not is_int8_compression(weigths_compression_option):
log.warning(
f"Weights compression {weigths_compression_option} is not supported for LCM, will be ignored"
)
continue
model = OVLatentConsistencyModelPipeline.from_pretrained(output_dir, compile=False)
ov_int8_dir = get_compressed_path(args.output_dir, args.precision, weigths_compression_option)
model.text_encoder.model = nncf.compress_weights(model.text_encoder.model)
model.unet.model = nncf.compress_weights(model.unet.model)
model.vae_decoder.model = nncf.compress_weights(model.vae_decoder.model)
model.save_pretrained(ov_int8_dir)
del model
gc.collect()
def convert_sdxl(args):
pt_compress_weights = is_torch_compression(args)
def build_pt_model(model_id):
model_ids = [idx.replace(" ", "") for idx in model_id.split(",")]
pt_model = StableDiffusionXLImg2ImgPipeline.from_pretrained(model_ids[0])
if len(model_ids) > 1:
for additional_model in model_ids[1:]:
if "lora" in additional_model:
pt_model.load_lora_weights(additional_model)
pt_model.fuse_lora()
if "lcm" in additional_model:
pt_model.scheduler = LCMScheduler.from_config(pt_model.scheduler.config)
continue
if "lcm" in additional_model and "lora" not in additional_model:
unet = UNet2DConditionModel.from_pretrained(additional_model)
pt_model.unet = unet
pt_model.scheduler = LCMScheduler.from_config(pt_model.scheduler.config)
continue
if "tae" in additional_model:
vae = AutoencoderTiny.from_pretrained(additional_model)
pt_model.vae = vae
continue
return pt_model
pt_model = build_pt_model(args.model_id)
if args.save_orig:
pt_model.save_pretrained(Path(args.output_dir) / PYTORCH_DIR)
del pt_model
gc.collect()
pt_model = build_pt_model(args.model_id)
fp_out_dir = Path(args.output_dir) / PYTORCH_DIR / OV_DIR / args.precision
models_and_export_configs = get_stable_diffusion_models_for_export(pt_model)
convert_sd_prepared_for_export_common(pt_model, models_and_export_configs, fp_out_dir, args)
if pt_compress_weights:
compression_modes = []
for cw in args.compress_weights:
if is_int8_compression(cw):
compression_modes.append(cw)
assert compression_modes, "Only INT8 compression supported for PyTorch backend"
for idx, compress_mode in enumerate(compression_modes):
if idx > 0:
pt_model = build_pt_model(args.model_id)
models_and_export_configs = get_stable_diffusion_models_for_export(pt_model)
compression_options = COMPRESSION_OPTIONS[compress_mode]
models_and_export_configs = compress_torchmodels(
models_and_export_configs, compression_options=compression_options
)
output = (
Path(args.output_dir)
/ PYTORCH_DIR
/ OV_DIR
/ PYTORCH_COMPRESS_WEIGHTS_DIR.format(precision=args.precision, compression=compress_mode)
)
convert_sd_prepared_for_export_common(pt_model, models_and_export_configs, output, args)
del pt_model
gc.collect()
if is_ov_compression(args):
for weigths_compression_option in args.compress_weights:
if not is_int8_compression(weigths_compression_option):
log.warning(
f"Weights compression {weigths_compression_option} is not supported for SDXL, will be ignored"
)
continue
ov_int8_dir = get_compressed_path(args.output_dir, args.precision, weigths_compression_option)
compression_options = COMPRESSION_OPTIONS[weigths_compression_option]
model = OVStableDiffusionXLPipeline.from_pretrained(fp_out_dir, compile=False)
model.text_encoder.model = nncf.compress_weights(model.text_encoder.model, **compression_options)
if getattr(model, "text_encoder_2", None) is not None:
model.text_encoder_2.model = nncf.compress_weights(model.text_encoder_2.model, **compression_options)
model.unet.model = nncf.compress_weights(model.unet.model)
model.vae_decoder.model = nncf.compress_weights(model.vae_decoder.model, **compression_options)
if getattr(model, "vae_encoder", None) is not None:
model.vae_encoder.model = nncf.compress_weights(model.vae_encoder.model, **compression_options)
model.save_pretrained(ov_int8_dir)
del model
gc.collect()
def convert_ldm_super_res(args):
pipeline = LDMSuperResolutionPipeline.from_pretrained(args.model_id)
if args.save_orig:
pipeline.save_pretrained(Path(args.output_dir) / PYTORCH_DIR)
unet_example_input = (
torch.zeros((1, 6, 128, 128)),
torch.tensor(1, dtype=torch.int32),
)
class Decoder(torch.nn.Module):
def __init__(self, model):
super().__init__()
self.model = model
def forward(self, latents):
return self.model.decode(latents)
decoder = Decoder(pipeline.vqvae)
compress_to_fp16 = is_fp16(args)
# convert model to OpenVINO IR
ov_unet = convert_model(pipeline.unet, example_input=unet_example_input)
ov_unet.inputs[1].get_node().set_element_type(OVType.i32)
ov_unet.inputs[1].get_node().set_partial_shape(PartialShape([]))
ov_unet.validate_nodes_and_infer_types()
save_dir = Path(args.output_dir) / PYTORCH_DIR / OV_DIR / args.precision
save_model(ov_unet, save_dir / "unet.xml", compress_to_fp16=compress_to_fp16)
ov_decoder = convert_model(decoder, example_input=torch.zeros((1, 3, 128, 128)))
save_model(ov_decoder, save_dir / "vqvae.xml", compress_to_fp16=compress_to_fp16)
pipeline.scheduler.save_config(save_dir)
del ov_unet, ov_decoder
gc.collect()
pt_compress_weights = is_torch_compression(args)
if pt_compress_weights:
compression_modes = []
for cw in args.compress_weights:
if is_int8_compression(cw):
compression_modes.append(cw)
assert compression_modes, "Only INT8 compression supported for PyTorch backend"
for idx, compress_mode in enumerate(compression_modes):
if idx > 0:
pipeline = LDMSuperResolutionPipeline.from_pretrained(args.model_id)
decoder = Decoder(pipeline.vqvae)
compression_options = COMPRESSION_OPTIONS[compress_mode]
compressed_unet = nncf.compress_weights(
pipeline.unet, dataset=nncf.Dataset([unet_example_input]), **compression_options
)
ov_compressed_unet = convert_model(compressed_unet, example_input=unet_example_input)
ov_compressed_unet.inputs[1].get_node().set_element_type(OVType.i32)
ov_compressed_unet.inputs[1].get_node().set_partial_shape(PartialShape([]))
ov_compressed_unet.validate_nodes_and_infer_types()
pt_out_dir = (
Path(args.output_dir)
/ PYTORCH_DIR
/ OV_DIR
/ PYTORCH_COMPRESS_WEIGHTS_DIR.format(precision=args.precision, compression=compress_mode)
)
save_model(
ov_compressed_unet,
pt_out_dir / "unet.xml",
compress_to_fp16=compress_to_fp16,
)
pipeline.scheduler.save_config(pt_out_dir)
decoder_example_input = torch.zeros(1, 3, 128, 128)
compressed_decoder = nncf.compress_weights(
decoder, dataset=nncf.Dataset([decoder_example_input]), **compression_options
)
ov_compressed_decoder = convert_model(compressed_decoder, example_input=decoder_example_input)
save_model(ov_compressed_decoder, pt_out_dir / "vqvae.xml", compress_to_fp16=compress_to_fp16)
if is_ov_compression(args):
for weigths_compression_option in args.compress_weights:
if not is_int8_compression(weigths_compression_option):
log.warning(
f"Weights compression {weigths_compression_option} is not supported for LDM, will be ignored"
)
continue
ov_int8_dir = get_compressed_path(args.output_dir, args.precision, weigths_compression_option)
ov_unet = Core().read_model(save_dir / "unet.xml")
compressed_ov_unet = nncf.compress_weights(ov_unet)
save_model(
compressed_ov_unet,
ov_int8_dir / "unet.xml",
compress_to_fp16=compress_to_fp16,
)
ov_decoder = Core().read_model(save_dir / "vqvae.xml")
compressed_ov_decoder = nncf.compress_weights(ov_decoder)
save_model(
compressed_ov_decoder,
ov_int8_dir / "vqvae.xml",
compress_to_fp16=compress_to_fp16,
)
pipeline.scheduler.save_config(ov_int8_dir)
def convert_mpt(args):
@torch.no_grad
def convert_to_ov(pt_model, tok, out_path, compress_to_fp16=False):
pt_model.config.use_cache = True
outs = pt_model(
input_ids=torch.ones((1, 10), dtype=torch.long),
attention_mask=torch.ones((1, 10), dtype=torch.long),
)
old = outs.past_key_values[0][0].ndim == 3
inputs = ["input_ids"]
outputs = ["logits"]
dynamic_shapes = {"input_ids": {1: "seq_len"}, "attention_mask": {1: "seq_len"}}
for idx in range(len(outs.past_key_values)):
inputs.extend([f"past_key_values.{idx}.key", f"past_key_values.{idx}.value"])
dynamic_shapes[inputs[-1]] = {2: "past_sequence + sequence"}
dynamic_shapes[inputs[-2]] = {3 if not old else 2: "past_sequence + sequence"}
outputs.extend([f"present.{idx}.key", f"present.{idx}.value"])
inputs.append("attention_mask")
dummy_inputs = {
"input_ids": torch.ones((1, 2), dtype=torch.long),
"past_key_values": outs.past_key_values,
"attention_mask": torch.ones((1, 12), dtype=torch.long),
}
pt_model.config.torchscript = True
orig_forward = pt_model.forward
@wraps(orig_forward)
def ts_patched_forward(
input_ids: torch.Tensor,
past_key_values: Tuple[Tuple[torch.Tensor]],
attention_mask: torch.Tensor,
):
pkv_list = list(past_key_values)
outs = orig_forward(
input_ids=input_ids,
past_key_values=pkv_list,
attention_mask=attention_mask,
)
return (outs.logits, tuple(outs.past_key_values))
pt_model.forward = ts_patched_forward
ov_model = convert_model(pt_model, example_input=dummy_inputs)
pt_model.forward = orig_forward
for inp_name, m_input, input_data in zip(inputs, ov_model.inputs, flattenize_inputs(dummy_inputs.values())):
input_node = m_input.get_node()
if input_node.element_type == OVType.dynamic:
m_input.get_node().set_element_type(OVType.f32)
shape = list(input_data.shape)
if inp_name in dynamic_shapes:
for k in dynamic_shapes[inp_name]:
shape[k] = -1
input_node.set_partial_shape(PartialShape(shape))
m_input.get_tensor().set_names({inp_name})
for out, out_name in zip(ov_model.outputs, outputs):
out.get_tensor().set_names({out_name})
save_ov_model_helper(ov_model, out_path, fp16=compress_to_fp16, tok=tok, config=pt_model.config)
remote_code = False
pt_model = None
try:
config = AutoConfig.from_pretrained(args.model_id, trust_remote_code=False)
except Exception:
config = AutoConfig.from_pretrained(args.model_id, trust_remote_code=True)
remote_code = True
cuda, post_init = patch_gptq(config)
model_kwargs = {}
precision = args.precision
compression_only = (
args.compress_weights
and not args.force_convert
and not is_torch_compression(args)
and is_ov_model_provided(args.model_id, args.output_dir, args.precision)
)
gptq_applied = is_gptq(config)
precision = precision if not gptq_applied else GPTQ_DIR.format(precision=args.precision)
if post_init is not None:
model_kwargs = {"torch_dtype": torch.float32}
pt_model = None
tokenizer_id = args.tokenizer_id or args.model_id
tok = AutoTokenizer.from_pretrained(tokenizer_id, trust_remote_code=True)
compress_to_fp16 = is_fp16(args)
if not compression_only:
def create_model(model_id, config, model_kwargs):
pt_model = AutoModelForCausalLM.from_pretrained(
model_id, trust_remote_code=remote_code, config=config, **model_kwargs
)
pt_model.config.use_cache = True