forked from openvinotoolkit/nncf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TORCH] Unwrap and wrap torch.return_type before and after posthooks (o…
…penvinotoolkit#2290) ### Changes * Post hooks inputs are being unwrapped from `torch.return_type` types to `torch.tensor` type. * Pre hooks outputs are being wrapped from `torch.tensor` to `torch.return_type` in case torch input were wrapped in the first place. ### Reason for changes To enable post hook insertion after torch operations which return `torch.tensor_type` values instead of `torch.tensor` values. ### Related tickets ### Tests * tests/torch/test_nncf_network.py is updated * tests/torch/test_return_types.py is introduced
- Loading branch information
1 parent
4c4aeac
commit db786a8
Showing
4 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Copyright (c) 2023 Intel Corporation | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import inspect | ||
from typing import Any, Optional, Tuple, Type, Union | ||
|
||
import torch | ||
|
||
|
||
def __get_supported_torch_return_types() -> Tuple[Type[tuple], ...]: | ||
""" | ||
Collects types from torch.return_type which can be wrapped/unwrapped by NNCF. | ||
NNCF can wrap/unwrap only return types that have two attributes, one of them | ||
should be the `values` attribute. | ||
:return: List of types from torch.return_type which can be wrapped/unwrapped by NNCF. | ||
""" | ||
retval = [t for _, t in inspect.getmembers(torch.return_types) if inspect.isclass(t) and hasattr(t, "values")] | ||
return tuple(t for t in retval if t.n_fields == 2) | ||
|
||
|
||
_TORCH_RETURN_TYPES = __get_supported_torch_return_types() | ||
|
||
|
||
def maybe_unwrap_from_torch_return_type(tensor: Any) -> torch.Tensor: | ||
""" | ||
Attempts to unwrap the tensor value from one of torch.return_types instances | ||
in case torch operation output is wrapped by a torch return_type. | ||
:param tensor: Torch tensor or torch return type instance to unwrap values from. | ||
:return: Unwrapped torch tensor. | ||
""" | ||
if isinstance(tensor, _TORCH_RETURN_TYPES): | ||
return tensor.values | ||
return tensor | ||
|
||
|
||
def maybe_wrap_to_torch_return_type(tensor: torch.Tensor, wrapped_input: Optional[Union[tuple, torch.Tensor]]) -> Any: | ||
""" | ||
Wraps tensor to wrapped_input wrapper in case wrapped_input is wrapped by a torch.return_value container. | ||
:param tensor: Torch tensor to wrap. | ||
:param wrapped_tensor: Instance of the tensor before it was unwrapped. | ||
:return: Wrapped tensor in case wrapped_input is wrapped by a torch.return_value container else the tensor. | ||
""" | ||
|
||
if isinstance(wrapped_input, _TORCH_RETURN_TYPES): | ||
# We assume that return_type has only two attributes, the first one is `value`. | ||
# This assumption is checked by `test_unwrap_wrap_torch_return_type`. | ||
return wrapped_input.__class__((tensor, wrapped_input[1])) | ||
return tensor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright (c) 2023 Intel Corporation | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import pytest | ||
import torch | ||
|
||
from nncf.torch.return_types import _TORCH_RETURN_TYPES | ||
from nncf.torch.return_types import maybe_unwrap_from_torch_return_type | ||
from nncf.torch.return_types import maybe_wrap_to_torch_return_type | ||
|
||
|
||
@pytest.mark.parametrize("return_type", _TORCH_RETURN_TYPES) | ||
def test_unwrap_wrap_torch_return_type(return_type): | ||
wrapped_tensor = return_type((torch.tensor(0), torch.tensor(1))) | ||
assert wrapped_tensor.values == torch.tensor(0) | ||
unwrapped_tensor = maybe_unwrap_from_torch_return_type(wrapped_tensor) | ||
assert unwrapped_tensor == torch.tensor(0) | ||
|
||
updated_wrapped_tensor = maybe_wrap_to_torch_return_type(unwrapped_tensor, wrapped_tensor) | ||
assert updated_wrapped_tensor == wrapped_tensor | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input_", [torch.tensor(0), [torch.tensor(0), torch.tensor(1)], (torch.tensor(0), torch.tensor(1))] | ||
) | ||
def test_wrap_unwrap_do_nothing_to_tensor(input_): | ||
wrapped_input = maybe_unwrap_from_torch_return_type(input_) | ||
assert wrapped_input is input_ | ||
unwrapped_input = maybe_wrap_to_torch_return_type(input_, wrapped_input) | ||
assert unwrapped_input is input_ |