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

Fix deepcopy of ov.Tensor #1146

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions optimum/intel/openvino/quantization.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
PREDEFINED_SD_DATASETS,
PREDEFINED_SPEECH_TO_TEXT_DATASETS,
PREDEFINED_VISUAL_LM_DATASETS,
deepcopy_data,
)


Expand Down Expand Up @@ -131,7 +132,7 @@ def __init__(

def collect_inputs(self, inputs):
if not self.apply_caching or not isinstance(inputs, dict):
self.collected_inputs.append(copy.deepcopy(inputs))
self.collected_inputs.append(deepcopy_data(inputs))
return

copied_inputs = {}
Expand All @@ -146,7 +147,7 @@ def collect_inputs(self, inputs):
# Avoid data copying if tensor contains data encountered earlier
self.tensor_cache.setdefault(k, {})
if data_hash not in self.tensor_cache[k]:
self.tensor_cache[k][data_hash] = copy.deepcopy(v)
self.tensor_cache[k][data_hash] = deepcopy_data(v)
copied_inputs[k] = self.tensor_cache[k][data_hash]
self.collected_inputs.append(copied_inputs)

Expand Down
24 changes: 21 additions & 3 deletions optimum/intel/openvino/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,22 @@
import stat
import warnings
import weakref
from copy import deepcopy
from glob import glob
from pathlib import Path
from tempfile import TemporaryDirectory as OrigTemporaryDirectory
from tempfile import mkdtemp
from typing import Tuple, Type, Union
from typing import Tuple, Type, Union, Any

import numpy as np
import torch
from huggingface_hub import model_info
from openvino.runtime import Core, Model, properties
from openvino.runtime import Core, Model, properties, Tensor
from openvino.runtime import Type as OVType
from packaging.version import Version
from transformers import AutoTokenizer, CLIPTokenizer, PreTrainedTokenizer, PreTrainedTokenizerFast
from transformers.onnx.utils import ParameterFormat, compute_serialized_parameters_size

import openvino
from optimum.intel.utils.import_utils import is_torch_version


Expand Down Expand Up @@ -586,3 +587,20 @@ def check_scale_available(model: Union[Model, str, Path]):
if runtime_options is None:
return False
return runtime_options.find("ACTIVATIONS_SCALE_FACTOR") is not None


def deepcopy_data(inputs: Any) -> Any:
if isinstance(inputs, dict):
new_inputs = {}
for k, v in inputs.items():
new_inputs[deepcopy_data(k)] = deepcopy_data(v)
elif isinstance(inputs, list):
new_inputs = [deepcopy_data(elem) for elem in inputs]
elif isinstance(inputs, tuple):
new_inputs = tuple(deepcopy_data(elem) for elem in inputs)
elif isinstance(inputs, openvino.Tensor):
new_inputs = openvino.Tensor(np.zeros(inputs.shape, dtype=inputs.element_type.to_dtype()))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
new_inputs = openvino.Tensor(np.zeros(inputs.shape, dtype=inputs.element_type.to_dtype()))
new_inputs = openvino.Tensor(np.empty(inputs.shape, dtype=inputs.element_type.to_dtype()))

Copy link
Collaborator

Choose a reason for hiding this comment

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

What will happen if inputs.element_type is bf16, u4 or i4?

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the comment!
I added a type argument to be sure that output tensor has a correct type.

new_inputs.copy_from(inputs)
else:
new_inputs = deepcopy(inputs)
return new_inputs
31 changes: 31 additions & 0 deletions tests/openvino/test_quantization.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
from transformers.testing_utils import slow
from transformers.utils.quantization_config import QuantizationMethod

from optimum.intel.openvino.utils import deepcopy_data


from optimum.intel import (
OVConfig,
OVFluxPipeline,
Expand Down Expand Up @@ -1354,6 +1357,34 @@ def test_calibration_data_uniqueness(self, model_name, apply_caching):
# Without caching, encoder hidden states tensors will be unique for each collected input
self.assertGreater(len(data_id_per_key["encoder_hidden_states"]), 2)

def test_deepcopy_data(self):
data = {
"a": torch.tensor([1, 2, 3]),
"b": np.array([1, 2, 3]),
"c": 1,
"d": "string",
"e": {"a": torch.tensor([1, 2, 3]), "b": np.array([1, 2, 3])},
"f": [ov.Tensor(np.ones((1, 2, 3))), ov.Tensor(np.ones((1, 2, 3)))],
}
copied_data = deepcopy_data(data)
assert copied_data["a"] is not data["a"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please use self.assertTrue(...) or similar instead of assert ...

Copy link
Author

Choose a reason for hiding this comment

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

updated

assert copied_data["b"] is not data["b"]
assert copied_data["e"]["a"] is not data["e"]["a"]
assert copied_data["e"]["b"] is not data["e"]["b"]
assert copied_data["f"][0] is not data["f"][0]
assert copied_data["f"][1] is not data["f"][1]

assert torch.equal(copied_data["a"], data["a"])
assert np.array_equal(copied_data["b"], data["b"])
assert copied_data["c"] == data["c"]
assert copied_data["d"] == data["d"]
assert torch.equal(copied_data["e"]["a"], data["e"]["a"])
assert np.array_equal(copied_data["e"]["b"], data["e"]["b"])
assert np.array_equal(copied_data["f"][0].data, data["f"][0].data)
assert np.array_equal(copied_data["f"][1].data, data["f"][1].data)

assert copied_data is not data


def check_optimization_not_applicable_to_optimized_model(model, quantization_config):
quantizer = OVQuantizer(model)
Expand Down
Loading