Skip to content

Commit b3cbc95

Browse files
authored
Added a fix for FP16 overflow issue on GPU/NPU (#994)
* Added a fix for FP16 overflow issue on GPU/NPU * Style * Updated export test * Style
1 parent a8e69a3 commit b3cbc95

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

optimum/exporters/openvino/convert.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,31 @@
9191
from optimum.intel.openvino.configuration import OVConfig
9292

9393

94-
def _save_model(model, path: str, ov_config: Optional["OVConfig"] = None, library_name: Optional[str] = None):
94+
def _set_runtime_options(
95+
models_and_export_configs: Dict[
96+
str,
97+
Tuple[Union["PreTrainedModel", "TFPreTrainedModel", "ModelMixin", "DiffusionPipeline"], "OnnxConfig"],
98+
],
99+
task: str,
100+
):
101+
for model_name in models_and_export_configs.keys():
102+
_, sub_export_config = models_and_export_configs[model_name]
103+
if "vae_" in model_name or "text-generation" in task:
104+
sub_export_config.runtime_options = {"ACTIVATIONS_SCALE_FACTOR": "8.0"}
105+
106+
107+
def _save_model(
108+
model,
109+
path: str,
110+
ov_config: Optional["OVConfig"] = None,
111+
library_name: Optional[str] = None,
112+
config: OnnxConfig = None,
113+
):
95114
compress_to_fp16 = ov_config is not None and ov_config.dtype == "fp16"
96115
model = _add_version_info_to_model(model, library_name)
116+
117+
if hasattr(config, "runtime_options"):
118+
model = _add_runtime_options_to_rt_info(model, config.runtime_options)
97119
save_model(model, path, compress_to_fp16)
98120

99121

@@ -213,6 +235,7 @@ def export_tensorflow(
213235
output.parent / output,
214236
ov_config=ov_config,
215237
library_name=library_name,
238+
config=config,
216239
)
217240
del ov_model
218241
return input_names, output_names, True
@@ -276,6 +299,7 @@ def export_pytorch_via_onnx(
276299
output.parent / OV_XML_FILE_NAME if output.suffix != ".xml" else output,
277300
ov_config=ov_config,
278301
library_name=library_name,
302+
config=config,
279303
)
280304
del ov_model
281305
return input_names, output_names, True
@@ -450,6 +474,7 @@ def ts_patched_forward(*args, **kwargs):
450474
output,
451475
ov_config=ov_config,
452476
library_name=library_name,
477+
config=config,
453478
)
454479
clear_class_registry()
455480
del ov_model
@@ -718,6 +743,8 @@ def export_from_model(
718743

719744
model.save_config(output)
720745

746+
_set_runtime_options(models_and_export_configs, task)
747+
721748
export_models(
722749
models_and_export_configs=models_and_export_configs,
723750
output_dir=output,
@@ -792,6 +819,19 @@ def export_tokenizer(
792819
save_model(model, output / file_name.format(suffix))
793820

794821

822+
def _add_runtime_options_to_rt_info(model: Model, options: Dict):
823+
"""
824+
Add runtime optinos
825+
"""
826+
try:
827+
for name, value in options.items():
828+
model.set_rt_info(value, ["runtime_options", name])
829+
except Exception:
830+
pass
831+
832+
return model
833+
834+
795835
def _add_version_info_to_model(model: Model, library_name: Optional[str] = None):
796836
"""
797837
Add dependency versions to OpenVINO model

tests/openvino/test_export.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ def _openvino_export(
124124
self.assertEqual(
125125
ov_model.model.get_rt_info()["optimum"]["transformers_version"], _transformers_version
126126
)
127+
self.assertTrue(ov_model.model.has_rt_info(["runtime_options", "ACTIVATIONS_SCALE_FACTOR"]))
128+
129+
if library_name == "diffusers":
130+
self.assertTrue(
131+
ov_model.vae_encoder.model.has_rt_info(["runtime_options", "ACTIVATIONS_SCALE_FACTOR"])
132+
)
133+
self.assertTrue(
134+
ov_model.vae_decoder.model.has_rt_info(["runtime_options", "ACTIVATIONS_SCALE_FACTOR"])
135+
)
127136

128137
@parameterized.expand(SUPPORTED_ARCHITECTURES)
129138
def test_export(self, model_type: str):

0 commit comments

Comments
 (0)