forked from openvinotoolkit/openvino.genai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
1529 lines (1316 loc) · 68.1 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) 2018-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import sys
import gc
import time
import logging as log
from argparse import ArgumentParser
from enum import Enum
from functools import wraps
from pathlib import Path
from typing import Tuple, Dict, Optional
import torch
from diffusers import StableDiffusionPipeline, StableDiffusionXLImg2ImgPipeline, LDMSuperResolutionPipeline, DiffusionPipeline
from diffusers import UNet2DConditionModel, AutoencoderTiny, LCMScheduler
from nncf import compress_weights
from openvino import Type, PartialShape, save_model, convert_model
from openvino.runtime import Core
from optimum.exporters import TasksManager
from optimum.exporters.tasks import make_backend_config_constructor_for_task
from optimum.exporters.onnx.config import TextDecoderOnnxConfig
from optimum.utils import (
NormalizedTextConfig, NormalizedConfigManager, DEFAULT_DUMMY_SHAPES,
DummyPastKeyValuesGenerator,
DummyTextInputGenerator,
DummyInputGenerator
)
from optimum.exporters.onnx import get_encoder_decoder_models_for_export
from optimum.exporters.openvino import export_models
from optimum.utils.save_utils import maybe_load_preprocessors
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.exporters.onnx import __main__ as optimum_main
try:
from optimum.exporters.openvino.__main__ import _get_submodels_and_export_configs
except ImportError:
from optimum.exporters.onnx.__main__ import _get_submodels_and_onnx_configs as _get_submodels_and_export_configs
from transformers import AutoTokenizer, AutoConfig, AutoModelForCausalLM, AutoModelForSeq2SeqLM, AutoModel
from utils.nncf_utils import COMPRESSION_OPTIONS, INT4_MODEL_CONFIGURATION, get_compressed_path
from utils.conversion_utils.convert_patch import patch_model_for_optimum_export
from utils.conversion_utils.better_transformer_patch import patch_model_with_bettertransformer
class BackendType(Enum):
PYTORCH = 'pytorch'
OPENVINO = 'openvino'
def save_tokenizer(tokenizer, out_dir):
try:
tokenizer.save_pretrained(out_dir)
except Exception as e:
log.error(f'tokenizer loading failed with {e}')
def compress_ov_model_weights_helper(ov_model, tok, config, out_path, compress_weights_format="INT8", fp16=False, args={}, model_name="openvino_model"):
compression_args = None
if "4BIT_DEFAULT" in compress_weights_format:
model_id = out_path.parents[3].name
if model_id in INT4_MODEL_CONFIGURATION:
compression_args = INT4_MODEL_CONFIGURATION[model_id]
else:
compression_args = COMPRESSION_OPTIONS["INT4_SYM"]
if compression_args is None:
compression_args = COMPRESSION_OPTIONS[compress_weights_format]
if args.ratio is not None:
compression_args["ratio"] = args.ratio
if args.group_size is not None:
compression_args["group_size"] = args.group_size
log.info("Compression options:")
log.info(compression_args)
compressed_ov_model = compress_weights(ov_model, **compression_args)
save_ov_model_helper(compressed_ov_model, out_path, model_name, fp16=fp16, tok=tok, config=config)
def save_ov_model_helper(ov_model, out_path, model_name='openvino_model', fp16=False, tok=None, config=None):
model_name = model_name or "openvino_model"
save_model(ov_model, Path(out_path) / f'{model_name}.xml', compress_to_fp16=fp16)
if tok is not None:
save_tokenizer(tok, out_path)
if config is not None:
config.save_pretrained(out_path)
def is_gptq(config):
config_dict = config.to_dict()
quantization_config = config_dict.get("quantization_config", None)
return quantization_config and quantization_config["quant_method"] == "gptq"
def patch_gptq(config):
do_gptq_patching = False
config_dict = config.to_dict()
quantization_config = config_dict.get("quantization_config", None)
do_gptq_patching = quantization_config and quantization_config["quant_method"] == "gptq"
orig_cuda_check = torch.cuda.is_available
orig_post_init_model = None
if do_gptq_patching:
torch.set_default_dtype(torch.float32)
torch.cuda.is_available = lambda: True
from optimum.gptq import GPTQQuantizer
orig_post_init_model = GPTQQuantizer.post_init_model
def post_init_model(self, model):
from auto_gptq import exllama_set_max_input_length
class StoreAttr(object):
pass
model.quantize_config = StoreAttr()
model.quantize_config.desc_act = self.desc_act
if self.desc_act and not self.disable_exllama and self.max_input_length is not None:
model = exllama_set_max_input_length(model, self.max_input_length)
return model
GPTQQuantizer.post_init_model = post_init_model
return orig_cuda_check, orig_post_init_model
def unpatch_gptq(orig_cuda_check, orig_post_init_model):
from optimum.gptq import GPTQQuantizer
torch.cuda.is_available = orig_cuda_check
GPTQQuantizer.post_init_model = orig_post_init_model
class TextDecoderWithPositionIdsOnnxConfig(TextDecoderOnnxConfig):
@property
def inputs(self) -> Dict[str, Dict[int, str]]:
common_inputs = super().inputs
# Decoders based on GPT2 require a position_ids input to avoid
# generating wrong position_ids in the model itself:
# https://github.com/huggingface/transformers/blob/v4.33.1/src/transformers/models/gpt2/modeling_gpt2.py#L802
if not self.no_position_ids and "text-generation" in self.task:
common_inputs["position_ids"] = {0: "batch_size", 1: "sequence_length"}
return common_inputs
def convert_optimum_causallm_base(model, args):
tok = AutoTokenizer.from_pretrained(args.model_id, trust_remote_code=True)
model = patch_model_for_optimum_export(model)
model_config = model.config
gptq_applied = is_gptq(model_config)
precision = args.precision if not gptq_applied else f"GPTQ_INT4-{args.precision}"
if gptq_applied and args.compress_weights:
log.info("Weights compression will be skipped for GPTQ models")
pt_compress_weights = args.compress_weights and BackendType.PYTORCH.value in args.compress_weights_backends
if args.save_orig:
pt_out_dir = Path(args.output_dir) / 'pytorch'
model.save_pretrained(pt_out_dir)
save_tokenizer(tok, pt_out_dir)
dummy_shapes = DEFAULT_DUMMY_SHAPES
onnx_config, models_and_onnx_configs = _get_submodels_and_export_configs(
model=model,
task="text-generation-with-past",
custom_onnx_configs={},
custom_architecture=None,
fn_get_submodels=None,
preprocessors=None,
_variant="default",
monolith=False
)
if "decoder_with_past_model" in models_and_onnx_configs:
models_and_onnx_configs = {"model": models_and_onnx_configs["decoder_with_past_model"]}
if args.bettertransformer:
models_and_onnx_configs["model"] = (patch_model_with_bettertransformer(*models_and_onnx_configs["model"]), models_and_onnx_configs["model"][1])
ov_out_dir = Path(args.output_dir) / 'pytorch/dldt' / precision
model.config.save_pretrained(ov_out_dir)
files_subpaths = ["openvino_" + model_name + ".xml" for model_name in models_and_onnx_configs.keys()]
export_models(
models_and_onnx_configs=models_and_onnx_configs,
output_dir=ov_out_dir,
output_names=files_subpaths,
input_shapes=dummy_shapes,
device="cpu",
fp16=args.precision == "FP16",
int8=False,
model_kwargs={},
)
save_tokenizer(tok, ov_out_dir)
if args.compress_weights and BackendType.OPENVINO.value in args.compress_weights_backends and not gptq_applied:
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_dir = ov_out_dir
ir_model = Core().read_model(fp_dir / files_subpaths[0])
compress_ov_model_weights_helper(ir_model, tok, model.config, optimized_dir, compress_option, args.precision == "FP16", args)
if pt_compress_weights and not gptq_applied:
compressed_model = compress_weights(model)
onnx_config, models_and_onnx_configs = _get_submodels_and_export_configs(
model=compressed_model,
task="text-generation-with-past",
custom_onnx_configs={},
custom_architecture=None,
fn_get_submodels=None,
preprocessors=None,
_variant="default",
monolith=False
)
pt_out_dir = Path(args.output_dir) / 'pytorch/dldt/compressed_weights' / f'PT_{args.precision}-INT8'
model.config.save_pretrained(pt_out_dir)
export_models(
models_and_onnx_configs=models_and_onnx_configs,
output_dir=pt_out_dir,
output_names=files_subpaths,
input_shapes=dummy_shapes,
device="cpu",
fp16=args.precision == "FP16",
int8=False,
model_kwargs={},
)
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)
model = AutoModelForCausalLM.from_pretrained(
args.model_id,
trust_remote_code=True,
config=config
)
convert_optimum_causallm_base(model, args)
if post_init is not None:
unpatch_gptq(cuda, post_init)
def convert_seq2seq(args):
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 = args.compress_weights and BackendType.PYTORCH.value in args.compress_weights_backends
start = time.perf_counter()
if args.save_orig or pt_compress_weights:
pt_model = AutoModelForSeq2SeqLM.from_pretrained(
args.model_id,
trust_remote_code=True,
config=AutoConfig.from_pretrained(args.model_id, trust_remote_code=True),
)
if args.save_orig:
pt_out_dir = Path(args.output_dir) / 'pytorch'
pt_model.save_pretrained(pt_out_dir)
save_tokenizer(tok, pt_out_dir)
if pt_compress_weights:
compressed_pt_model = compress_weights(pt_model)
onnx_config_constructor = TasksManager.get_exporter_config_constructor(model=pt_model, exporter='onnx', task='text2text-generation')
onnx_config = onnx_config_constructor(pt_model.config, use_past=True)
models_and_onnx_configs = get_encoder_decoder_models_for_export(compressed_pt_model, onnx_config)
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/dldt/compressed_weights' / f'PT_{args.precision}-INT8'
try:
export_models(
models_and_onnx_configs=models_and_onnx_configs,
opset=onnx_config.DEFAULT_ONNX_OPSET,
output_dir=save_dir_path,
output_names=output_names,
)
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()
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),
)
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/dldt' / 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)
if args.compress_weights and BackendType.OPENVINO.value in args.compress_weights_backends:
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)
compress_ov_model_weights_helper(
model.encoder.model, tok, model.config, optimized_dir, compress_option,
args.precision == "FP16", args, "openvino_encoder_model"
)
compress_ov_model_weights_helper(
model.decoder.model, tok, model.config, optimized_dir, compress_option,
args.precision == "FP16", args, "openvino_decoder_model"
)
if model.decoder_with_past:
compress_ov_model_weights_helper(
model.decoder_with_past.model, tok, model.config, optimized_dir, compress_option,
args.precision == "FP16", args, "openvino_decoder_with_past_model"
)
del model
gc.collect()
def convert_sd(args):
start = time.perf_counter()
pt_compress_weights = args.compress_weights and BackendType.PYTORCH.value in args.compress_weights_backends
if args.save_orig or pt_compress_weights:
pt_model = StableDiffusionPipeline.from_pretrained(args.model_id)
if args.save_orig:
pt_model.save_pretrained(Path(args.output_dir) / 'pytorch')
if pt_compress_weights:
wc_text_encoder = compress_weights(pt_model.text_encoder)
wc_unet = compress_weights(pt_model.unet)
wc_vae = compress_weights(pt_model.vae)
pt_model.text_encoder = wc_text_encoder
pt_model.unet = wc_unet
pt_model.vae = wc_vae
_, models_and_onnx_configs = optimum_main._get_submodels_and_onnx_configs(
model=pt_model,
task='stable-diffusion',
monolith=False,
custom_onnx_configs={},
custom_architecture=False,
_variant='default',
)
output = Path(args.output_dir) / 'pytorch/dldt/compressed_weights' / f'PT_{args.precision}-INT8'
for model_name in models_and_onnx_configs:
subcomponent = models_and_onnx_configs[model_name][0]
if hasattr(subcomponent, 'save_config'):
subcomponent.save_config(output / model_name)
elif hasattr(subcomponent, 'config') and hasattr(subcomponent.config, 'save_pretrained'):
subcomponent.config.save_pretrained(output / model_name)
files_subpaths = [Path(name_dir) / OV_XML_FILE_NAME for name_dir in models_and_onnx_configs]
# Saving the additional components needed to perform inference.
pt_model.scheduler.save_pretrained(output.joinpath('scheduler'))
feature_extractor = getattr(pt_model, 'feature_extractor', None)
if feature_extractor is not None:
feature_extractor.save_pretrained(output.joinpath('feature_extractor'))
tokenizer = getattr(pt_model, 'tokenizer', None)
if tokenizer is not None:
tokenizer.save_pretrained(output.joinpath('tokenizer'))
tokenizer_2 = getattr(pt_model, 'tokenizer_2', None)
if tokenizer_2 is not None:
tokenizer_2.save_pretrained(output.joinpath('tokenizer_2'))
pt_model.save_config(output)
export_models(
models_and_onnx_configs=models_and_onnx_configs,
output_dir=output,
output_names=files_subpaths,
)
del pt_model
gc.collect()
model = OVStableDiffusionPipeline.from_pretrained(args.model_id, export=True, compile=False)
end = time.perf_counter()
log.info(f'Conversion total time {end - start}s')
if args.precision == 'FP16':
model.half()
start1 = time.perf_counter()
model.save_pretrained(Path(args.output_dir) / 'pytorch/dldt' / args.precision)
end1 = time.perf_counter()
log.info(f'Serialization total time {end1 - start1}s')
if args.compress_weights and BackendType.OPENVINO.value in args.compress_weights_backends:
for weigths_compression_option in args.compress_weights:
if weigths_compression_option != "INT8":
log.warning("Weights compression {weigths_compression_option} does not supported for SD, will be ignored")
continue
ov_int8_dir = get_compressed_path(args.output_dir, args.precision, weigths_compression_option)
model.text_encoder.model = compress_weights(model.text_encoder.model)
model.unet.model = compress_weights(model.unet.model)
model.vae_decoder.model = compress_weights(model.vae_decoder.model)
model.save_pretrained(ov_int8_dir)
# Saving the additional components needed to perform inference.
model.scheduler.save_pretrained(ov_int8_dir.joinpath('scheduler'))
feature_extractor = getattr(model, 'feature_extractor', None)
if feature_extractor is not None:
feature_extractor.save_pretrained(ov_int8_dir.joinpath('feature_extractor'))
tokenizer = getattr(model, 'tokenizer', None)
if tokenizer is not None:
tokenizer.save_pretrained(ov_int8_dir.joinpath('tokenizer'))
tokenizer_2 = getattr(model, 'tokenizer_2', None)
if tokenizer_2 is not None:
tokenizer_2.save_pretrained(ov_int8_dir.joinpath('tokenizer_2'))
model.save_config(ov_int8_dir)
del model
gc.collect()
def convert_lcm(args):
start = time.perf_counter()
pt_compress_weights = args.compress_weights and BackendType.PYTORCH.value in args.compress_weights_backends
if args.save_orig or pt_compress_weights:
pt_model = DiffusionPipeline.from_pretrained(args.model_id)
if args.save_orig:
pt_model.save_pretrained(Path(args.output_dir) / 'pytorch')
if pt_compress_weights:
wc_text_encoder = compress_weights(pt_model.text_encoder)
wc_unet = compress_weights(pt_model.unet)
wc_vae = compress_weights(pt_model.vae)
pt_model.text_encoder = wc_text_encoder
pt_model.unet = wc_unet
pt_model.vae = wc_vae
_, models_and_onnx_configs = optimum_main._get_submodels_and_onnx_configs(
model=pt_model,
task='stable-diffusion',
monolith=False,
custom_onnx_configs={},
custom_architecture=False,
_variant='default',
)
output = Path(args.output_dir) / 'pytorch/dldt/compressed_weights' / f'PT_{args.precision}-INT8'
for model_name in models_and_onnx_configs:
subcomponent = models_and_onnx_configs[model_name][0]
if hasattr(subcomponent, 'save_config'):
subcomponent.save_config(output / model_name)
elif hasattr(subcomponent, 'config') and hasattr(subcomponent.config, 'save_pretrained'):
subcomponent.config.save_pretrained(output / model_name)
files_subpaths = [Path(name_dir) / OV_XML_FILE_NAME for name_dir in models_and_onnx_configs]
# Saving the additional components needed to perform inference.
pt_model.scheduler.save_pretrained(output.joinpath('scheduler'))
feature_extractor = getattr(pt_model, 'feature_extractor', None)
if feature_extractor is not None:
feature_extractor.save_pretrained(output.joinpath('feature_extractor'))
tokenizer = getattr(pt_model, 'tokenizer', None)
if tokenizer is not None:
tokenizer.save_pretrained(output.joinpath('tokenizer'))
tokenizer_2 = getattr(pt_model, 'tokenizer_2', None)
if tokenizer_2 is not None:
tokenizer_2.save_pretrained(output.joinpath('tokenizer_2'))
pt_model.save_config(output)
export_models(
models_and_onnx_configs=models_and_onnx_configs,
output_dir=output,
output_names=files_subpaths,
)
del pt_model
gc.collect()
model = OVLatentConsistencyModelPipeline.from_pretrained(args.model_id, export=True, compile=False)
end = time.perf_counter()
log.info(f'Conversion total time {end - start}s')
if args.precision == 'FP16':
model.half()
start1 = time.perf_counter()
model.save_pretrained(Path(args.output_dir) / 'pytorch/dldt' / args.precision)
end1 = time.perf_counter()
log.info(f'Serialization total time {end1 - start1}s')
if args.compress_weights and BackendType.OPENVINO.value in args.compress_weights_backends:
for weigths_compression_option in args.compress_weights:
if weigths_compression_option != "INT8":
log.warning("Weights compression {weigths_compression_option} does not supported for LCM, will be ignored")
continue
ov_int8_dir = get_compressed_path(args.output_dir, args.precision, weigths_compression_option)
model.text_encoder.model = compress_weights(model.text_encoder.model)
model.unet.model = compress_weights(model.unet.model)
model.vae_decoder.model = compress_weights(model.vae_decoder.model)
model.save_pretrained(ov_int8_dir)
# Saving the additional components needed to perform inference.
model.scheduler.save_pretrained(ov_int8_dir.joinpath('scheduler'))
feature_extractor = getattr(model, 'feature_extractor', None)
if feature_extractor is not None:
feature_extractor.save_pretrained(ov_int8_dir.joinpath('feature_extractor'))
tokenizer = getattr(model, 'tokenizer', None)
if tokenizer is not None:
tokenizer.save_pretrained(ov_int8_dir.joinpath('tokenizer'))
tokenizer_2 = getattr(model, 'tokenizer_2', None)
if tokenizer_2 is not None:
tokenizer_2.save_pretrained(ov_int8_dir.joinpath('tokenizer_2'))
model.save_config(ov_int8_dir)
del model
gc.collect()
def convert_sdxl(args):
pt_compress_weights = args.compress_weights and BackendType.PYTORCH.value in args.compress_weights_backends
def build_pt_model(model_id):
model_ids = [idx.replace(" ", "") for idx in model_id.split(',')]
pt_model = StableDiffusionXLImg2ImgPipeline.from_pretrained(model_ids[0])
tiny_vae = False
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:
tiny_vae = True
vae = AutoencoderTiny.from_pretrained(additional_model)
pt_model.vae = vae
continue
preprocessors = maybe_load_preprocessors(model_ids[0])
return pt_model, preprocessors, tiny_vae
def convert_pt_to_ov(pt_model, preprocessors, output_dir, fp16, tiny_vae):
_, models_and_onnx_configs = optimum_main._get_submodels_and_onnx_configs(
model=pt_model,
task='stable-diffusion-xl',
monolith=False,
custom_onnx_configs={},
custom_architecture=False,
_variant='default',
preprocessors=preprocessors,
legacy=False
)
if tiny_vae:
models_and_onnx_configs["vae_encoder"][0].forward = (
lambda sample: {"latent_sample": models_and_onnx_configs["vae_encoder"][0].encode(x=sample)["latents"]}
)
models_and_onnx_configs["vae_decoder"][0].forward = (
lambda latent_sample: models_and_onnx_configs["vae_decoder"][0].decode(latent_sample)
)
for model_name in models_and_onnx_configs:
subcomponent = models_and_onnx_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_onnx_configs]
# Saving the additional components needed to perform inference.
pt_model.scheduler.save_pretrained(output_dir.joinpath('scheduler'))
feature_extractor = getattr(pt_model, 'feature_extractor', None)
if feature_extractor is not None:
feature_extractor.save_pretrained(output_dir.joinpath('feature_extractor'))
tokenizer = getattr(pt_model, 'tokenizer', None)
if tokenizer is not None:
tokenizer.save_pretrained(output_dir.joinpath('tokenizer'))
tokenizer_2 = getattr(pt_model, 'tokenizer_2', None)
if tokenizer_2 is not None:
tokenizer_2.save_pretrained(output_dir.joinpath('tokenizer_2'))
pt_model.save_config(output_dir)
export_models(
models_and_onnx_configs=models_and_onnx_configs,
output_dir=output_dir,
output_names=files_subpaths,
fp16=fp16,
int8=False
)
pt_model, preprocessors, tiny_vae = build_pt_model(args.model_id)
if args.save_orig:
pt_model.save_pretrained(Path(args.output_dir) / 'pytorch')
if pt_compress_weights:
output = Path(args.output_dir) / 'pytorch/dldt/compressed_weights' / f'PT_{args.precision}-INT8'
pt_model.text_encoder = compress_weights(pt_model.text_encoder)
pt_model.unet = compress_weights(pt_model.unet)
pt_model.vae = compress_weights(pt_model.vae)
if getattr(pt_model, 'text_encoder_2', None) is not None:
pt_model.text_encoder_2 = compress_weights(pt_model.text_encoder_2)
convert_pt_to_ov(pt_model, output, args.precision == "FP16", tiny_vae)
del pt_model
gc.collect()
pt_model, preprocessors, tiny_vae = build_pt_model(args.model_id)
fp_out_dir = Path(args.output_dir) / 'pytorch/dldt' / args.precision
convert_pt_to_ov(pt_model, preprocessors, fp_out_dir, args.precision == "FP16", tiny_vae)
if args.compress_weights and BackendType.OPENVINO.value in args.compress_weights_backends:
for weigths_compression_option in args.compress_weights:
if weigths_compression_option != "INT8":
log.warning("Weights compression {weigths_compression_option} does not supported for SDXL, will be ignored")
continue
ov_int8_dir = get_compressed_path(args.output_dir, args.precision, weigths_compression_option)
model = OVStableDiffusionXLPipeline.from_pretrained(fp_out_dir, compile=False)
model.text_encoder.model = compress_weights(model.text_encoder.model)
if getattr(model, "text_encoder_2", None) is not None:
model.text_encoder_2.model = compress_weights(model.text_encoder_2.model)
model.unet.model = compress_weights(model.unet.model)
model.vae_decoder.model = compress_weights(model.vae_decoder.model)
if getattr(model, "vae_encoder", None) is not None:
model.vae_encoder.model = compress_weights(model.vae_encoder.model)
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')
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)
pt_compress_weights = args.compress_weights and BackendType.PYTORCH.value in args.compress_weights_backends
compress_to_fp16 = args.precision == 'FP16'
if pt_compress_weights:
compressed_unet = compress_weights(pipeline.unet)
ov_compressed_unet = convert_model(compressed_unet, example_input=unet_example_input)
ov_compressed_unet.inputs[1].get_node().set_element_type(Type.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/dldt/compressed_weights' / f'PT_{args.precision}-INT8'
save_model(ov_compressed_unet, pt_out_dir / 'unet.xml', compress_to_fp16=compress_to_fp16)
pipeline.scheduler.save_config(pt_out_dir)
# Couldn't compress decoder weights (RuntimeError: cdist only supports floating-point dtypes, X2 got: Byte)
ov_decoder = convert_model(decoder, example_input=torch.zeros((1, 3, 128, 128)))
save_model(ov_decoder, pt_out_dir / 'vqvae.xml', compress_to_fp16=compress_to_fp16)
# 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(Type.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/dldt' / 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)
if args.compress_weights and BackendType.OPENVINO.value in args.compress_weights_backends:
for weigths_compression_option in args.compress_weights:
if weigths_compression_option != "INT8":
log.warning("Weights compression {weigths_compression_option} does not supported for LDM, will be ignored")
continue
ov_int8_dir = get_compressed_path(args.output_dir, args.precision, weigths_compression_option)
compressed_ov_unet = compress_weights(ov_unet)
save_model(compressed_ov_unet, ov_int8_dir / 'unet.xml', compress_to_fp16=compress_to_fp16)
compressed_ov_decoder = 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):
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 == Type.dynamic:
m_input.get_node().set_element_type(Type.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)
pt_model = AutoModelForCausalLM.from_pretrained(
args.model_id,
trust_remote_code=True,
config=AutoConfig.from_pretrained(args.model_id, trust_remote_code=True),
)
tok = AutoTokenizer.from_pretrained(args.model_id, trust_remote_code=True)
pt_model.config.use_cache = True
pt_model.eval()
if args.save_orig:
pt_out_dir = Path(args.output_dir) / 'pytorch'
pt_model.save_pretrained(pt_out_dir)
save_tokenizer(tok, pt_out_dir)
ov_dir = Path(args.output_dir) / 'pytorch/dldt' / args.precision
compress_to_fp16 = args.precision == 'FP16'
convert_to_ov(pt_model, tok, ov_dir, compress_to_fp16)
if args.compress_weights:
if BackendType.PYTORCH.value in args.compress_weights_backends:
compressed_pt_model = compress_weights(pt_model)
pt_path = Path(args.output_dir) / 'pytorch/dldt/compressed_weights' / f'PT_{args.precision}-INT8'
convert_to_ov(compressed_pt_model, tok, pt_path, compress_to_fp16)
if BackendType.OPENVINO.value in args.compress_weights_backends:
for compress_option in args.compress_weights:
log.info(f"Compress model weights to {compress_option}")
ov_model = Core().read_model(ov_dir / 'openvino_model.xml')
ov_compressed_path = get_compressed_path(args.output_dir, args.precision, compress_option)
compress_ov_model_weights_helper(ov_model, tok, pt_model.config, ov_compressed_path, compress_option, compress_to_fp16, args)
def convert_stablelm(args):
config = AutoConfig.from_pretrained(args.model_id, trust_remote_code=True)
if not config.model_type.startswith('stablelm'):
return convert_causal_lm(args)
cuda, post_init = patch_gptq(config)
pt_model = AutoModelForCausalLM.from_pretrained(
args.model_id,
trust_remote_code=True,
config=AutoConfig.from_pretrained(args.model_id, trust_remote_code=True),
)
model_type = config.model_type.replace("_", "-")
NormalizedConfigManager._conf[model_type] = NormalizedTextConfig.with_args(
num_layers="num_hidden_layers", num_attention_heads="num_attention_heads"
)
TasksManager._SUPPORTED_MODEL_TYPE[model_type] = TasksManager._SUPPORTED_MODEL_TYPE['llama']
convert_optimum_causallm_base(pt_model, args)
if post_init is not None:
unpatch_gptq(cuda, post_init)
def convert_chatglm2(args):
class ChatGLM2NormalizedConfig(NormalizedTextConfig):
NUM_LAYERS = "num_layers"
VOCAB_SIZE = "padded_vocab_size"
class ChatGLM2DummyTextInputGenerator(DummyTextInputGenerator):
SUPPORTED_INPUT_NAMES = {
"input_ids",
"attention_mask",
"token_type_ids",
"position_ids",
}
def generate(self, input_name: str, framework: str = "pt", int_dtype: str = "int64", float_dtype: str = "fp32"):
input = super().generate(input_name, framework, int_dtype, float_dtype)
if input_name == "attention_mask":
input = torch.ones(input.shape, dtype=input.dtype)
if input_name == "position_ids":
bs = input.shape[0]
input = torch.range(0, input.shape[1], dtype=input.dtype).repeat(bs, 1)
return input
class ChatGLM2DummyPastKeyValuesGenerator(DummyPastKeyValuesGenerator):
def __init__(
self,
task: str,
normalized_config: NormalizedTextConfig,
batch_size: int = DEFAULT_DUMMY_SHAPES["batch_size"],
sequence_length: int = DEFAULT_DUMMY_SHAPES["sequence_length"],
random_batch_size_range: Optional[Tuple[int, int]] = None,
random_sequence_length_range: Optional[Tuple[int, int]] = None,
**kwargs,
):
super().__init__(
task=task,
normalized_config=normalized_config,
batch_size=batch_size,
sequence_length=sequence_length,
random_batch_size_range=random_batch_size_range,
random_sequence_length_range=random_sequence_length_range,
)
self.multi_query_group_num = normalized_config.multi_query_group_num
self.head_dim = self.hidden_size // self.num_attention_heads
def generate(self, input_name: str, framework: str = "pt", int_dtype: str = "int64", float_dtype: str = "fp32"):
past_key_shape = (
self.sequence_length,
self.batch_size,
self.multi_query_group_num,
self.head_dim,
)
past_value_shape = (
self.sequence_length,
self.batch_size,
self.multi_query_group_num,
self.head_dim,
)
return [
(
self.random_float_tensor(past_key_shape, framework=framework, dtype=float_dtype),
self.random_float_tensor(past_value_shape, framework=framework, dtype=float_dtype),
)
for _ in range(self.num_layers)
]
class ChatGLM2OpenVINOConfig(TextDecoderOnnxConfig):
NORMALIZED_CONFIG_CLASS = ChatGLM2NormalizedConfig
DUMMY_INPUT_GENERATOR_CLASSES = (ChatGLM2DummyTextInputGenerator, ChatGLM2DummyPastKeyValuesGenerator)
DUMMY_PKV_GENERATOR_CLASS = ChatGLM2DummyPastKeyValuesGenerator
no_position_ids = False
def generate_dummy_inputs(self, framework: str = "pt", **kwargs):
dummy_inputs_generators = self._create_dummy_input_generator_classes(**kwargs)
dummy_inputs = {}
input_names = [key for key in self.inputs.keys() if not key.startswith("past_key_values")]
if self.use_past_in_inputs and self.use_cache_branch is not False:
input_names.append("past_key_values")
for input_name in input_names:
input_was_inserted = False
for dummy_input_gen in dummy_inputs_generators:
if dummy_input_gen.supports_input(input_name):
dummy_inputs[input_name] = self.overwrite_shape_and_generate_input(
dummy_input_gen,
input_name,
framework,
input_shapes=kwargs,
)
input_was_inserted = True
break
if not input_was_inserted:
raise RuntimeError(
f'Could not generate dummy input for "{input_name}". Try adding a proper dummy input generator to the model ONNX config.'
)
# refer to https://github.com/huggingface/optimum/pull/764
cond1 = self.use_past_in_inputs
cond2 = self.PAD_ATTENTION_MASK_TO_PAST
cond3 = self.use_cache_branch is not False
cond4 = "attention_mask" in dummy_inputs
if (cond1 and cond2 and cond3 and cond4):
# Obtain the past sequence length from the value instead of the key (Bloom).
past_length = dummy_inputs["past_key_values"][0][1].shape[0]
for k, v in dummy_inputs.items():
if k not in ["attention_mask", "past_key_values"]:
dummy_inputs[k] = v[:, -1:]
dummy_inputs["attention_mask"] = DummyInputGenerator.pad_input_on_dim(
dummy_inputs["attention_mask"],
desired_length=past_length + 1,
dim=1,
dtype=dummy_inputs["attention_mask"].dtype,
)
return dummy_inputs
@property
def inputs(self) -> Dict[str, Dict[int, str]]:
common_inputs = super().inputs
if not self.no_position_ids and self.task == "text-generation":
common_inputs["position_ids"] = {0: "batch_size", 1: "sequence_length"}
return common_inputs
def add_past_key_values(self, inputs_or_outputs: Dict[str, Dict[int, str]], direction: str):
"""
Fills `input_or_outputs` mapping with past_key_values dynamic axes considering the direction.
Args:
inputs_or_outputs (`Dict[str, Dict[int, str]]`): The mapping to fill.
direction (`str`):
either "inputs" or "outputs", it specifies whether `input_or_outputs` is the input mapping or the
output mapping, this is important for axes naming.
"""
if direction not in ["inputs", "outputs"]:
raise ValueError(f'direction must either be "inputs" or "outputs", but {direction} was given')
if direction == "inputs":
decoder_sequence_name = "past_sequence_length"
name = "past_key_values"
else:
decoder_sequence_name = "past_sequence_length + 1"
name = "present"
for i in range(self._normalized_config.num_layers):
inputs_or_outputs[f"{name}.{i}.key"] = {1: "batch_size", 0: decoder_sequence_name}
inputs_or_outputs[f"{name}.{i}.value"] = {1: "batch_size", 0: decoder_sequence_name}
config = AutoConfig.from_pretrained(args.model_id, trust_remote_code=True)
cuda, post_init = patch_gptq(config)
pt_model = AutoModelForCausalLM.from_pretrained(
args.model_id,
trust_remote_code=True,
config=config,
torch_dtype=torch.float32
)
try:
pt_model.to(torch.float32)
except Exception:
pass
NormalizedConfigManager._conf[pt_model.config.model_type] = NormalizedTextConfig.with_args(
num_layers="num_hidden_layers", num_attention_heads="num_attention_heads"
)
export_config = ChatGLM2OpenVINOConfig
TasksManager._SUPPORTED_MODEL_TYPE[pt_model.config.model_type] = {
'onnx': {
'text-generation': make_backend_config_constructor_for_task(export_config, 'text-generation'),
'text-generation-with-past': make_backend_config_constructor_for_task(export_config, 'text-generation-with-past'),
},
'openvino': {
'text-generation': make_backend_config_constructor_for_task(export_config, 'text-generation'),
'text-generation-with-past': make_backend_config_constructor_for_task(export_config, 'text-generation-with-past'),
},
}
convert_optimum_causallm_base(pt_model, args)
if post_init is not None:
unpatch_gptq(cuda, post_init)
def convert_chatglm(args):
def convert_to_ov(pt_model, tok, out_path, compress_to_fp16=False):
pt_model.config.torchscript = True
last_token = torch.tensor([[130328]])
past = torch.zeros(28, 2, 5, 1, 32, 128)
position_ids = torch.tensor([[[2], [4]]])
dummy_input = {
'input_ids': last_token,
'past_key_values': past,
'position_ids': position_ids,
}
ov_model = convert_model(pt_model, example_input=dummy_input)
ov_model.outputs[0].get_tensor().set_names({'logits'})
for i in range(1, len(ov_model.outputs), 2):