Skip to content

Commit 95c3e2c

Browse files
committed
apply review comments
1 parent 23e298c commit 95c3e2c

File tree

7 files changed

+34
-50
lines changed

7 files changed

+34
-50
lines changed

optimum/commands/export/openvino.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ def parse_args_openvino(parser: "ArgumentParser"):
106106
),
107107
)
108108
optional_group.add_argument(
109-
"--variant",
109+
"--weights-variant",
110110
type=str,
111111
default=None,
112-
help=("Select a variant of the model to export."),
112+
help=("If specified load weights from variant filename."),
113113
)
114114
optional_group.add_argument(
115115
"--ratio",
@@ -452,7 +452,7 @@ def run(self):
452452
quantization_config=quantization_config,
453453
stateful=not self.args.disable_stateful,
454454
trust_remote_code=self.args.trust_remote_code,
455-
variant=self.args.variant,
455+
variant=self.args.weights_variant,
456456
cache_dir=self.args.cache_dir,
457457
)
458458
model.save_pretrained(self.args.output)
@@ -475,6 +475,6 @@ def run(self):
475475
stateful=not self.args.disable_stateful,
476476
convert_tokenizer=not self.args.disable_convert_tokenizer,
477477
library_name=library_name,
478-
model_variant=self.args.variant,
478+
weights_variant=self.args.weights_variant,
479479
# **input_shapes,
480480
)

optimum/exporters/openvino/__main__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def main_export(
122122
convert_tokenizer: bool = False,
123123
library_name: Optional[str] = None,
124124
model_loading_kwargs: Optional[Dict[str, Any]] = None,
125-
model_variant: Optional[str] = None,
125+
weights_variant: Optional[str] = None,
126126
**kwargs_shapes,
127127
):
128128
"""
@@ -238,8 +238,8 @@ def main_export(
238238
custom_architecture = False
239239
patch_16bit = False
240240
loading_kwargs = model_loading_kwargs or {}
241-
if model_variant is not None:
242-
loading_kwargs["variant"] = model_variant
241+
if weights_variant is not None:
242+
loading_kwargs["variant"] = weights_variant
243243
if library_name == "transformers":
244244
config = AutoConfig.from_pretrained(
245245
model_name_or_path,
@@ -350,7 +350,7 @@ class StoreAttr(object):
350350

351351
GPTQQuantizer.post_init_model = post_init_model
352352
elif library_name == "diffusers" and is_openvino_version(">=", "2024.6"):
353-
_loading_kwargs = {} if model_variant is None else {"variant": model_variant}
353+
_loading_kwargs = {} if weights_variant is None else {"variant": weights_variant}
354354
dtype = deduce_diffusers_dtype(
355355
model_name_or_path,
356356
revision=revision,

optimum/intel/openvino/modeling_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ def _from_transformers(
609609
trust_remote_code=trust_remote_code,
610610
ov_config=ov_config,
611611
library_name=cls._library_name,
612-
model_variant=variant,
612+
weights_variant=variant,
613613
)
614614

615615
return cls._from_pretrained(

optimum/intel/openvino/modeling_decoder.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ def _from_transformers(
327327
stateful=stateful,
328328
model_loading_kwargs=model_loading_kwargs,
329329
library_name=cls._library_name,
330-
model_variant=variant,
330+
weughts_variant=variant,
331331
)
332332

333333
if config.model_type == "phi3" and config.max_position_embeddings != getattr(

optimum/intel/openvino/modeling_diffusion.py

+10-14
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ def _from_transformers(
590590
force_download=force_download,
591591
ov_config=ov_config,
592592
library_name=cls._library_name,
593-
model_variant=variant,
593+
weights_variant=variant,
594594
)
595595

596596
return cls._from_pretrained(
@@ -767,7 +767,7 @@ def _reshape_text_encoder(
767767
self, model: openvino.runtime.Model, batch_size: int = -1, tokenizer_max_length: int = -1
768768
):
769769
if batch_size != -1:
770-
shapes = {model.inputs[0]: [batch_size, tokenizer_max_length]}
770+
shapes = {input_tensor: [batch_size, tokenizer_max_length] for input_tensor in model.inputs}
771771
model.reshape(shapes)
772772
return model
773773

@@ -824,9 +824,9 @@ def reshape(
824824
tokenizer_max_len = -1
825825
else:
826826
tokenizer_max_len = (
827-
self.tokenizer.model_max_length
827+
getattr(self.tokenizer, "model_max_length", -1)
828828
if self.tokenizer is not None
829-
else self.tokenizer_2.model_max_length
829+
else getattr(self.tokenizer_2, "model_max_length", -1)
830830
)
831831

832832
if self.unet is not None:
@@ -848,21 +848,19 @@ def reshape(
848848
self.text_encoder.model = self._reshape_text_encoder(
849849
self.text_encoder.model,
850850
batch_size,
851-
self.tokenizer.model_max_length if "Gemma" not in self.tokenizer.__class__.__name__ else -1,
851+
getattr(self.tokenizer, "model_max_length", -1)
852+
if "Gemma" not in self.tokenizer.__class__.__name__
853+
else -1,
852854
)
853855

854856
if self.text_encoder_2 is not None:
855857
self.text_encoder_2.model = self._reshape_text_encoder(
856-
self.text_encoder_2.model,
857-
batch_size,
858-
self.tokenizer_2.model_max_length if "Gemma" not in self.tokenizer.__class__.__name__ else -1,
858+
self.text_encoder_2.model, batch_size, getattr(self.tokenizer_2, "model_max_length", -1)
859859
)
860860

861861
if self.text_encoder_3 is not None:
862862
self.text_encoder_3.model = self._reshape_text_encoder(
863-
self.text_encoder_3.model,
864-
batch_size,
865-
self.tokenizer_3.model_max_length if "Gemma" not in self.tokenizer.__class__.__name__ else -1,
863+
self.text_encoder_3.model, batch_size, getattr(self.tokenizer_3, "model_max_length", -1)
866864
)
867865

868866
self.clear_requests()
@@ -1068,9 +1066,7 @@ def forward(
10681066
model_inputs = {"input_ids": input_ids}
10691067

10701068
if "attention_mask" in self.input_names:
1071-
model_inputs["attention_mask"] = (
1072-
attention_mask if attention_mask is not None else torch.ones(input_ids.shape, dtype=torch.long)
1073-
)
1069+
model_inputs["attention_mask"] = attention_mask
10741070

10751071
ov_outputs = self.request(model_inputs, share_inputs=True)
10761072
main_out = ov_outputs[0]

optimum/intel/openvino/modeling_visual_language.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ def _from_transformers(
630630
trust_remote_code=trust_remote_code,
631631
ov_config=ov_config,
632632
stateful=stateful,
633-
model_variant=variant,
633+
weights_variant=variant,
634634
)
635635
config = AutoConfig.from_pretrained(save_dir_path, trust_remote_code=trust_remote_code)
636636
return cls._from_pretrained(

tests/openvino/test_diffusion.py

+13-25
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class OVPipelineForText2ImageTest(unittest.TestCase):
7878
NEGATIVE_PROMPT_SUPPORT_ARCHITECTURES = ["stable-diffusion", "stable-diffusion-xl", "latent-consistency"]
7979
if is_transformers_version(">=", "4.40.0"):
8080
SUPPORTED_ARCHITECTURES.extend(["stable-diffusion-3", "flux", "sana"])
81-
NEGATIVE_PROMPT_SUPPORT_ARCHITECTURES.append(["stable-diffusion-3"])
81+
NEGATIVE_PROMPT_SUPPORT_ARCHITECTURES.extend(["stable-diffusion-3"])
8282
CALLBACK_SUPPORT_ARCHITECTURES = ["stable-diffusion", "stable-diffusion-xl", "latent-consistency"]
8383

8484
OVMODEL_CLASS = OVPipelineForText2Image
@@ -94,13 +94,6 @@ def generate_inputs(self, height=128, width=128, batch_size=1):
9494

9595
return inputs
9696

97-
def get_auto_cls(self, model_arch):
98-
if model_arch == "sana":
99-
from diffusers import SanaPipeline
100-
101-
return SanaPipeline
102-
return self.AUTOMODEL_CLASS
103-
10497
@require_diffusers
10598
def test_load_vanilla_model_which_is_not_supported(self):
10699
with self.assertRaises(Exception) as context:
@@ -111,9 +104,7 @@ def test_load_vanilla_model_which_is_not_supported(self):
111104
@parameterized.expand(SUPPORTED_ARCHITECTURES)
112105
@require_diffusers
113106
def test_ov_pipeline_class_dispatch(self, model_arch: str):
114-
auto_cls = self.get_auto_cls(model_arch)
115-
auto_pipeline = DiffusionPipeline if model_arch != "sana" else auto_cls
116-
auto_pipeline = auto_cls.from_pretrained(MODEL_NAMES[model_arch])
107+
auto_pipeline = DiffusionPipeline.from_pretrained(MODEL_NAMES[model_arch])
117108
ov_pipeline = self.OVMODEL_CLASS.from_pretrained(MODEL_NAMES[model_arch])
118109

119110
self.assertEqual(ov_pipeline.auto_model_class, auto_pipeline.__class__)
@@ -141,21 +132,19 @@ def test_num_images_per_prompt(self, model_arch: str):
141132
def test_compare_to_diffusers_pipeline(self, model_arch: str):
142133
height, width, batch_size = 64, 64, 1
143134
inputs = self.generate_inputs(height=height, width=width, batch_size=batch_size)
144-
auto_cls = self.get_auto_cls(model_arch)
145135
ov_pipeline = self.OVMODEL_CLASS.from_pretrained(MODEL_NAMES[model_arch])
146-
diffusers_pipeline = auto_cls.from_pretrained(MODEL_NAMES[model_arch])
136+
diffusers_pipeline = DiffusionPipeline.from_pretrained(MODEL_NAMES[model_arch])
147137

148-
with torch.no_grad():
149-
for output_type in ["latent", "np", "pt"]:
150-
inputs["output_type"] = output_type
151-
if model_arch == "sana":
152-
# resolution binning will lead to resize output to standard resolution and back that can interpolate floating-point deviations
153-
inputs["use_resolution_binning"] = False
154-
atol = 1e-4
138+
for output_type in ["latent", "np", "pt"]:
139+
inputs["output_type"] = output_type
140+
if model_arch == "sana":
141+
# resolution binning will lead to resize output to standard resolution and back that can interpolate floating-point deviations
142+
inputs["use_resolution_binning"] = False
143+
atol = 1e-4
155144

156-
ov_output = ov_pipeline(**inputs, generator=get_generator("pt", SEED)).images
157-
diffusers_output = diffusers_pipeline(**inputs, generator=get_generator("pt", SEED)).images
158-
np.testing.assert_allclose(ov_output, diffusers_output, atol=atol, rtol=1e-2)
145+
ov_output = ov_pipeline(**inputs, generator=get_generator("pt", SEED)).images
146+
diffusers_output = diffusers_pipeline(**inputs, generator=get_generator("pt", SEED)).images
147+
np.testing.assert_allclose(ov_output, diffusers_output, atol=atol, rtol=1e-2)
159148

160149
# test on inputs nondivisible on 64
161150
height, width, batch_size = 96, 96, 1
@@ -191,8 +180,7 @@ def __call__(self, *args, **kwargs) -> None:
191180
auto_callback = Callback()
192181

193182
ov_pipe = self.OVMODEL_CLASS.from_pretrained(MODEL_NAMES[model_arch])
194-
auto_cls = self.get_auto_cls(model_arch)
195-
auto_pipe = auto_cls.from_pretrained(MODEL_NAMES[model_arch])
183+
auto_pipe = DiffusionPipeline.from_pretrained(MODEL_NAMES[model_arch])
196184

197185
# callback_steps=1 to trigger callback every step
198186
ov_pipe(**inputs, callback=ov_callback, callback_steps=1)

0 commit comments

Comments
 (0)