Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 309d160

Browse files
committedMay 22, 2024·
dump model before compress cli
1 parent c69fe32 commit 309d160

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed
 

‎optimum/exporters/openvino/convert.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
import os
2020
from pathlib import Path
2121
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union
22+
import tempfile
2223

2324
import onnx
2425
from transformers.utils import is_tf_available, is_torch_available
2526

26-
from openvino.runtime import Model, PartialShape, save_model
27+
from openvino.runtime import Model, PartialShape, save_model, Core
2728
from openvino.runtime.exceptions import OVTypeError
2829
from openvino.runtime.utils.types import get_element_type
2930
from openvino.tools.ovc import convert_model
@@ -58,6 +59,7 @@
5859

5960

6061
logger = logging.getLogger(__name__)
62+
core = Core()
6163

6264
if is_torch_available():
6365
import torch.nn as nn
@@ -412,11 +414,24 @@ def ts_patched_forward(*args, **kwargs):
412414

413415
if stateful:
414416
patch_stateful(model.config, ov_model)
417+
if ov_config.quantization_config:
418+
with tempfile.TemporaryDirectory() as temp_dir:
419+
tmp_output = Path(temp_dir) / output.name
420+
_save_model(ov_model, tmp_output, ov_config=None)
421+
clear_class_registry()
422+
del ov_model
423+
del model
424+
gc.collect()
425+
426+
ov_model = core.read_model(tmp_output)
427+
_save_model(ov_model, output, ov_config)
428+
else:
429+
_save_model(ov_model, output, ov_config)
430+
clear_class_registry()
431+
del ov_model
432+
del model
433+
gc.collect()
415434

416-
_save_model(ov_model, output, ov_config=ov_config)
417-
clear_class_registry()
418-
del model
419-
gc.collect()
420435
return input_names, output_names, False
421436

422437

0 commit comments

Comments
 (0)
Please sign in to comment.