diff --git a/optimum/exporters/openvino/__main__.py b/optimum/exporters/openvino/__main__.py index dba4628d7..3ac831488 100644 --- a/optimum/exporters/openvino/__main__.py +++ b/optimum/exporters/openvino/__main__.py @@ -432,6 +432,7 @@ class StoreAttr(object): for op in submodel.get_ops(): if op.get_type_name() == "Constant" and op.get_element_type() in [Type.f16, Type.f32, Type.bf16]: num_parameters += reduce(operator.mul, op.shape, 1) + del op if num_parameters >= _MAX_UNCOMPRESSED_SIZE: if is_nncf_available(): quantization_config = {"bits": 8, "sym": False} @@ -445,6 +446,8 @@ class StoreAttr(object): else: quantization_config = ov_config.quantization_config if quantization_config is None: + del submodel + gc.collect() continue if not is_nncf_available(): @@ -457,6 +460,7 @@ class StoreAttr(object): compressed_submodel_path = submodel_path.parent / f"{submodel_path.stem}_compressed.xml" save_model(submodel, compressed_submodel_path, compress_to_fp16=False) del submodel + gc.collect() submodel_path.unlink() submodel_path.with_suffix(".bin").unlink() diff --git a/optimum/exporters/openvino/convert.py b/optimum/exporters/openvino/convert.py index 0c0d5f59d..18f2efa7a 100644 --- a/optimum/exporters/openvino/convert.py +++ b/optimum/exporters/openvino/convert.py @@ -118,6 +118,8 @@ def _save_model( if hasattr(config, "runtime_options"): model = _add_runtime_options_to_rt_info(model, config.runtime_options) save_model(model, path, compress_to_fp16) + del model + gc.collect() def export( @@ -239,6 +241,7 @@ def export_tensorflow( config=config, ) del ov_model + gc.collect() return input_names, output_names, True @@ -303,6 +306,7 @@ def export_pytorch_via_onnx( config=config, ) del ov_model + gc.collect() return input_names, output_names, True diff --git a/optimum/intel/openvino/utils.py b/optimum/intel/openvino/utils.py index cf5060f42..1ba740c3b 100644 --- a/optimum/intel/openvino/utils.py +++ b/optimum/intel/openvino/utils.py @@ -22,6 +22,7 @@ from glob import glob from pathlib import Path from tempfile import TemporaryDirectory as OrigTemporaryDirectory +from tempfile import mkdtemp from typing import Tuple, Type, Union import numpy as np @@ -472,7 +473,7 @@ def onexc(*args): # to add behaviour that available only for python3.10+ for older python version class TemporaryDirectory(OrigTemporaryDirectory): def __init__(self, suffix=None, prefix=None, dir=None, ignore_cleanup_errors=True, *, delete=True): - super().__init__(suffix=suffix, prefix=prefix, dir=dir) + self.name = mkdtemp(suffix, prefix, dir) self._ignore_cleanup_errors = ignore_cleanup_errors self._delete = delete self._finalizer = weakref.finalize( @@ -485,13 +486,13 @@ def __init__(self, suffix=None, prefix=None, dir=None, ignore_cleanup_errors=Tru ) @classmethod - def _cleanup(cls, name, warn_message, ignore_errors=False, delete=True): + def _cleanup(cls, name, warn_message, ignore_errors=True, delete=True): if delete: cls._rmtree(name, ignore_errors=ignore_errors) warnings.warn(warn_message, ResourceWarning) @classmethod - def _rmtree(cls, name, ignore_errors=False, repeated=False): + def _rmtree(cls, name, ignore_errors=True, repeated=False): def _dont_follow_symlinks(func, path, *args): # Pass follow_symlinks=False, unless not supported on this platform. if func in os.supports_follow_symlinks: @@ -545,7 +546,7 @@ def onexc(func, path, exc): if not ignore_errors: raise - _rmtree(name, onexc=onexc) + _rmtree(name, onexc=onexc, ignore_errors=ignore_errors) def cleanup(self): if self._finalizer.detach() or os.path.exists(self.name):