Skip to content

Commit 62cb0df

Browse files
Avoid import nncf in tests/cross_fw/shared/json.py (#3243)
### Changes - Remove import nncf in tests/cross_fw/shared/json.py - Remove install nncf in test_examples on windows - Add as_numpy_tensor for TORCH backend ### Reason for changes https://github.com/openvinotoolkit/nncf/actions/runs/13103780497 ### Tests https://github.com/openvinotoolkit/nncf/actions/runs/13121758899
1 parent 6c4b227 commit 62cb0df

File tree

6 files changed

+33
-21
lines changed

6 files changed

+33
-21
lines changed

.github/workflows/examples.yml

-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ jobs:
9898
- uses: ilammy/msvc-dev-cmd@ed94116c4d30d2091601b81f339a2eaa1c2ba0a6 # v1.4.1
9999
- name: Install NNCF and test requirements
100100
run: |
101-
pip install -e .
102101
pip install -r tests/cross_fw/examples/requirements.txt
103102
- name: Print installed modules
104103
run: pip list

nncf/tensor/functions/numeric.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -935,8 +935,11 @@ def tensor(
935935
@tensor_guard
936936
def as_numpy_tensor(a: Tensor) -> Tensor:
937937
"""
938-
Change backend of the tensor to numpy. Leads to data copying when tensor data type is bf16, u4 or i4. Otherwise,
939-
there is no data copying.
938+
Convert tensor to numpy.
939+
In certain cases, this conversion may involve data copying, depending on the
940+
data type or device. Specifically:
941+
- OV: if tensors data type is bf16, u4 or i4.
942+
- PT: if tensors on the GPU or data type is not supported on Numpy.
940943
941944
:param a: Tensor to change backend for.
942945
:return: Tensor in numpy backend.

nncf/tensor/functions/torch_numeric.py

+5
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,8 @@ def tensor(
484484
device = convert_to_torch_device(device)
485485
dtype = convert_to_torch_dtype(dtype)
486486
return torch.tensor(data, dtype=dtype, device=device)
487+
488+
489+
@numeric.as_numpy_tensor.register(torch.Tensor)
490+
def _(a: torch.Tensor) -> np.ndarray:
491+
return a.cpu().detach().numpy()

tests/cross_fw/shared/json.py

-11
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@
1515

1616
import numpy as np
1717

18-
from nncf.tensor import Tensor
19-
20-
try:
21-
import torch
22-
except ModuleNotFoundError:
23-
torch = None
24-
2518

2619
def load_json(stats_path: Path):
2720
with open(stats_path, encoding="utf8") as json_file:
@@ -32,16 +25,12 @@ class NumpyEncoder(json.JSONEncoder):
3225
"""Special json encoder for numpy types"""
3326

3427
def default(self, o):
35-
if isinstance(o, Tensor):
36-
o = o.data
3728
if isinstance(o, np.integer):
3829
return int(o)
3930
if isinstance(o, np.floating):
4031
return float(o)
4132
if isinstance(o, np.ndarray):
4233
return o.tolist()
43-
if isinstance(o, torch.Tensor):
44-
return o.cpu().detach().numpy().tolist()
4534
return json.JSONEncoder.default(self, o)
4635

4736

tests/cross_fw/test_templates/template_test_nncf_tensor.py

+9
Original file line numberDiff line numberDiff line change
@@ -1749,3 +1749,12 @@ def test_fn_tensor(self, data, dtype):
17491749
backend_tensor = backend_tensor.astype(dtype)
17501750
assert fns.allclose(nncf_tensor, backend_tensor)
17511751
assert nncf_tensor.dtype == backend_tensor.dtype
1752+
1753+
def test_as_numpy_tensor(self):
1754+
tensor1 = Tensor(self.to_tensor([1.0, 2.0]))
1755+
tensor2 = tensor1.as_numpy_tensor()
1756+
assert tensor2.backend == TensorBackend.numpy
1757+
assert tensor1.dtype == tensor2.dtype
1758+
assert tensor1.shape == tensor2.shape
1759+
assert tensor2.device == TensorDeviceType.CPU
1760+
assert fns.allclose(tensor1, tensor2)

tests/cross_fw/test_templates/test_calculate_quantizer_parameters.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,27 @@ def read_ref_fq_params(q_group, q_config, narrow_range, hf_range):
6262
return ref_quantize_params
6363

6464

65-
def dump_fq_params(fq_params, q_group, q_config, narrow_range, hf_range):
65+
def dump_fq_params(
66+
fq_params: FakeQuantizeParameters,
67+
q_group: QuantizerGroup,
68+
q_config: QuantizerConfig,
69+
narrow_range: bool,
70+
hf_range: bool,
71+
):
6672
key = get_test_reference_key(q_group, q_config, narrow_range, hf_range)
6773
all_fq_params = load_json(FQ_CALCULATED_PARAMETERS_PATH)
6874
fq_params_dict = parse_fq_params_to_dict(fq_params)
6975
all_fq_params[key] = fq_params_dict
7076
dump_to_json(FQ_CALCULATED_PARAMETERS_PATH, all_fq_params)
7177

7278

73-
def parse_fq_params_to_dict(fq_params):
79+
def parse_fq_params_to_dict(fq_params: FakeQuantizeParameters):
7480
return {
7581
"levels": fq_params.levels,
76-
"input_low": fq_params.input_low,
77-
"input_high": fq_params.input_high,
78-
"output_low": fq_params.output_low,
79-
"output_high": fq_params.output_high,
82+
"input_low": fq_params.input_low.as_numpy_tensor().data,
83+
"input_high": fq_params.input_high.as_numpy_tensor().data,
84+
"output_low": fq_params.output_low.as_numpy_tensor().data,
85+
"output_high": fq_params.output_high.as_numpy_tensor().data,
8086
}
8187

8288

@@ -227,7 +233,8 @@ def test_calculate_quantizer_parameters(self, case_to_test):
227233
)
228234
if not case_to_test.should_fail:
229235
fq_params = calculate_quantizer_parameters(statistics, q_config, quant_group, narrow_range, half_range)
230-
# Uncomment lines below to generate reference for new models.
236+
# Use OpenVINO backend to collect new reference files.
237+
# Uncomment lines below to generate reference for new parameters.
231238
# dump_fq_params(fq_params, quant_group, q_config, narrow_range, half_range)
232239
ref_fq_params = read_ref_fq_params(quant_group, q_config, narrow_range, half_range)
233240
compare_fq_parameters(fq_params, ref_fq_params)

0 commit comments

Comments
 (0)