Skip to content

Commit 20900b2

Browse files
authored
fix model removal from tmp dir windows (#1028)
* fix model removal from tmp dir windows * fix second case * delete explicit removal compressed_submodel obj as no affect
1 parent 8fcc0ec commit 20900b2

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

optimum/exporters/openvino/__main__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ class StoreAttr(object):
432432
for op in submodel.get_ops():
433433
if op.get_type_name() == "Constant" and op.get_element_type() in [Type.f16, Type.f32, Type.bf16]:
434434
num_parameters += reduce(operator.mul, op.shape, 1)
435+
del op
435436
if num_parameters >= _MAX_UNCOMPRESSED_SIZE:
436437
if is_nncf_available():
437438
quantization_config = {"bits": 8, "sym": False}
@@ -445,6 +446,8 @@ class StoreAttr(object):
445446
else:
446447
quantization_config = ov_config.quantization_config
447448
if quantization_config is None:
449+
del submodel
450+
gc.collect()
448451
continue
449452

450453
if not is_nncf_available():
@@ -457,6 +460,7 @@ class StoreAttr(object):
457460
compressed_submodel_path = submodel_path.parent / f"{submodel_path.stem}_compressed.xml"
458461
save_model(submodel, compressed_submodel_path, compress_to_fp16=False)
459462
del submodel
463+
gc.collect()
460464

461465
submodel_path.unlink()
462466
submodel_path.with_suffix(".bin").unlink()

optimum/exporters/openvino/convert.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ def _save_model(
118118
if hasattr(config, "runtime_options"):
119119
model = _add_runtime_options_to_rt_info(model, config.runtime_options)
120120
save_model(model, path, compress_to_fp16)
121+
del model
122+
gc.collect()
121123

122124

123125
def export(
@@ -239,6 +241,7 @@ def export_tensorflow(
239241
config=config,
240242
)
241243
del ov_model
244+
gc.collect()
242245
return input_names, output_names, True
243246

244247

@@ -303,6 +306,7 @@ def export_pytorch_via_onnx(
303306
config=config,
304307
)
305308
del ov_model
309+
gc.collect()
306310
return input_names, output_names, True
307311

308312

optimum/intel/openvino/utils.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from glob import glob
2323
from pathlib import Path
2424
from tempfile import TemporaryDirectory as OrigTemporaryDirectory
25+
from tempfile import mkdtemp
2526
from typing import Tuple, Type, Union
2627

2728
import numpy as np
@@ -472,7 +473,7 @@ def onexc(*args):
472473
# to add behaviour that available only for python3.10+ for older python version
473474
class TemporaryDirectory(OrigTemporaryDirectory):
474475
def __init__(self, suffix=None, prefix=None, dir=None, ignore_cleanup_errors=True, *, delete=True):
475-
super().__init__(suffix=suffix, prefix=prefix, dir=dir)
476+
self.name = mkdtemp(suffix, prefix, dir)
476477
self._ignore_cleanup_errors = ignore_cleanup_errors
477478
self._delete = delete
478479
self._finalizer = weakref.finalize(
@@ -485,13 +486,13 @@ def __init__(self, suffix=None, prefix=None, dir=None, ignore_cleanup_errors=Tru
485486
)
486487

487488
@classmethod
488-
def _cleanup(cls, name, warn_message, ignore_errors=False, delete=True):
489+
def _cleanup(cls, name, warn_message, ignore_errors=True, delete=True):
489490
if delete:
490491
cls._rmtree(name, ignore_errors=ignore_errors)
491492
warnings.warn(warn_message, ResourceWarning)
492493

493494
@classmethod
494-
def _rmtree(cls, name, ignore_errors=False, repeated=False):
495+
def _rmtree(cls, name, ignore_errors=True, repeated=False):
495496
def _dont_follow_symlinks(func, path, *args):
496497
# Pass follow_symlinks=False, unless not supported on this platform.
497498
if func in os.supports_follow_symlinks:
@@ -545,7 +546,7 @@ def onexc(func, path, exc):
545546
if not ignore_errors:
546547
raise
547548

548-
_rmtree(name, onexc=onexc)
549+
_rmtree(name, onexc=onexc, ignore_errors=ignore_errors)
549550

550551
def cleanup(self):
551552
if self._finalizer.detach() or os.path.exists(self.name):

0 commit comments

Comments
 (0)