Skip to content

Commit

Permalink
fix model removal from tmp dir windows (#1028)
Browse files Browse the repository at this point in the history
* fix model removal from tmp dir windows

* fix second case

* delete  explicit removal compressed_submodel obj as no affect
  • Loading branch information
eaidova authored Nov 25, 2024
1 parent 8fcc0ec commit 20900b2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 4 additions & 0 deletions optimum/exporters/openvino/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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():
Expand All @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions optimum/exporters/openvino/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -239,6 +241,7 @@ def export_tensorflow(
config=config,
)
del ov_model
gc.collect()
return input_names, output_names, True


Expand Down Expand Up @@ -303,6 +306,7 @@ def export_pytorch_via_onnx(
config=config,
)
del ov_model
gc.collect()
return input_names, output_names, True


Expand Down
9 changes: 5 additions & 4 deletions optimum/intel/openvino/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 20900b2

Please sign in to comment.