Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dump model before compress cli #723

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions optimum/exporters/openvino/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
import os
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union
import tempfile

import onnx
from transformers.utils import is_tf_available, is_torch_available

from openvino.runtime import Model, PartialShape, save_model
from openvino.runtime import Model, PartialShape, save_model, Core
from openvino.runtime.exceptions import OVTypeError
from openvino.runtime.utils.types import get_element_type
from openvino.tools.ovc import convert_model
Expand Down Expand Up @@ -58,6 +59,7 @@


logger = logging.getLogger(__name__)
core = Core()

if is_torch_available():
import torch.nn as nn
Expand Down Expand Up @@ -412,11 +414,24 @@ def ts_patched_forward(*args, **kwargs):

if stateful:
patch_stateful(model.config, ov_model)
if ov_config.quantization_config:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is another PR where we probably solve this problem: https://github.com/huggingface/optimum-intel/pull/721/files

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikita-savelyevv PR, as I understand it works only for quantization with dataset for specific cases, when you need to infer model. The goal of my changes remove pytorch model before any weights compression process started for free memory (IR before saving on disk shares weights with pytorch model and additionally may requires own memory on top of that. When we use API for exporting model, weights compression happens after conversion step finished and we already removed pyotrch model from RAM, but when optimum-cli used conversion and compression combined in one step and pytorch model is still alive at compression step)

I open it for experiment only, both changes can be useful, I think we can combine them

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, seems like the changes are independent.

@eaidova Just out of curiosity, do you have numbers for how much memory can be saved with this approach?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, @eaidova. @nikita-savelyevv, can you please adopt changes from this PR?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlexKoff88 I don't think this applies to my case because I don't have access to the PyTorch or OpenVINO model objects after the main_export call. So I can't delete them.

Or do you mean to copy changes from this PR to my PR? If so, in my opinion these should be added separately.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I didn't notice that this is for torch models only. Then, it makes sense to keep these changes separately.

with tempfile.TemporaryDirectory() as temp_dir:
tmp_output = Path(temp_dir) / output.name
_save_model(ov_model, tmp_output, ov_config=None)
clear_class_registry()
del ov_model
del model
gc.collect()

ov_model = core.read_model(tmp_output)
_save_model(ov_model, output, ov_config)
else:
_save_model(ov_model, output, ov_config)
clear_class_registry()
del ov_model
del model
gc.collect()

_save_model(ov_model, output, ov_config=ov_config)
clear_class_registry()
del model
gc.collect()
return input_names, output_names, False


Expand Down
Loading