Skip to content

Commit

Permalink
Move Tensor to nncf/__init__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderDokuchaev committed Oct 2, 2023
1 parent 35ee9a4 commit 04cd1dc
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 103 deletions.
7 changes: 6 additions & 1 deletion nncf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
from nncf.common.strip import strip
from nncf.config import NNCFConfig
from nncf.data import Dataset
from nncf.experimental.tensor.enums import TensorBackendType
from nncf.experimental.tensor.enums import TensorDataType
from nncf.experimental.tensor.enums import TensorDeviceType
from nncf.experimental.tensor.functions import *
from nncf.experimental.tensor.tensor import Tensor
from nncf.parameters import DropType
from nncf.parameters import ModelType
from nncf.parameters import TargetDevice
Expand Down Expand Up @@ -49,7 +54,7 @@
framework_present = True
_AVAILABLE_FRAMEWORKS[fw_name] = framework_present

if not any(_AVAILABLE_FRAMEWORKS.values()):
if not sum(_AVAILABLE_FRAMEWORKS.values()):
nncf_logger.error(
"Neither PyTorch, TensorFlow, ONNX or OpenVINO Python packages have been found in your Python "
"environment.\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from nncf.experimental.tensor import Tensor
from nncf.experimental.tensor import functions as fns
import nncf
from nncf import Tensor


def mean_per_channel(x: Tensor, axis: int) -> Tensor:
Expand All @@ -22,9 +22,9 @@ def mean_per_channel(x: Tensor, axis: int) -> Tensor:
:return: Reduced Tensor.
"""
if len(x.shape) < 3:
return fns.mean(x, axis=0)
return nncf.mean(x, axis=0)
pos_axis = axis + x.ndim if axis < 0 else axis
if pos_axis < 0 or pos_axis >= x.ndim:
raise ValueError(f"axis {axis} is out of bounds for array of dimension {x.ndim}")
axis = tuple(i for i in range(x.ndim) if i != pos_axis)
return fns.mean(x, axis=axis)
return nncf.mean(x, axis=axis)
10 changes: 5 additions & 5 deletions nncf/experimental/tensor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Common algorithms should use wrapped tensors and provide the unwrapped tensor to
### Initialization Tensor

```python
from nncf.experimental.tensor import Tensor
from nncf import Tensor

import numpy as np
numpy_array = np.array([1,2])
Expand Down Expand Up @@ -57,16 +57,16 @@ nncf_tensor.max() # Tensor(2)
All available functions you can found in [functions.py](functions.py).

```python
from nncf.experimental.tensor import functions as fns
fns.max(nncf_tensor) # Tensor(2)
import nncf
nncf.max(nncf_tensor) # Tensor(2)
```

**NOTE** A function requires at least one positional argument, which is used to dispatch the function
to the appropriate implementation depending on the type of argument.

```python
fns.max(nncf_tensor) # Correct
fns.max(a=nncf_tensor) # TypeError: wrapper requires at least 1 positional argument
nncf.max(nncf_tensor) # Correct
nncf.max(a=nncf_tensor) # TypeError: wrapper requires at least 1 positional argument
```

### Loop over Tensor
Expand Down
4 changes: 2 additions & 2 deletions nncf/experimental/tensor/torch_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

import torch

from nncf.experimental.tensor import TensorDataType
from nncf.experimental.tensor import TensorDeviceType
from nncf.experimental.tensor import functions as fns
from nncf.experimental.tensor.enums import TensorDataType
from nncf.experimental.tensor.enums import TensorDeviceType

DTYPE_MAP = {
TensorDataType.float16: torch.float16,
Expand Down
14 changes: 7 additions & 7 deletions nncf/quantization/algorithms/fast_bias_correction/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
from math import inf
from typing import Any, Dict, List, Optional, Tuple, TypeVar, Union

import nncf
from nncf import Dataset
from nncf import Tensor
from nncf.common.factory import EngineFactory
from nncf.common.factory import ModelTransformerFactory
from nncf.common.graph.graph import NNCFGraph
Expand All @@ -26,9 +28,7 @@
from nncf.common.tensor_statistics.statistic_point import StatisticPointsContainer
from nncf.common.utils.backend import BackendType
from nncf.common.utils.backend import get_backend
from nncf.experimental.common.tensor_statistics import statistical_functions as s_fns
from nncf.experimental.tensor import Tensor
from nncf.experimental.tensor import functions as fns
from nncf.experimental.common.tensor_statistics.statistical_functions import mean_per_channel
from nncf.quantization.algorithms.algorithm import Algorithm
from nncf.quantization.algorithms.fast_bias_correction.backend import ALGO_BACKENDS

Expand Down Expand Up @@ -199,8 +199,8 @@ def _get_bias_shift_magnitude(current_bias_value: Tensor, updated_bias_value: Te
:return: Magnitude between original and updated bias values.
"""
bias_shift_magnitude = inf
if fns.count_nonzero(current_bias_value == 0) == 0:
bias_shift_magnitude = fns.max(fns.abs((updated_bias_value - current_bias_value) / current_bias_value))
if nncf.count_nonzero(current_bias_value == 0) == 0:
bias_shift_magnitude = nncf.max(nncf.abs((updated_bias_value - current_bias_value) / current_bias_value))
return bias_shift_magnitude

@staticmethod
Expand Down Expand Up @@ -318,8 +318,8 @@ def _get_bias_shift(
engine = EngineFactory.create(model)
raw_output = engine.infer(input_blob)
q_outputs = self._backend_entity.process_model_output(raw_output, output_name)
q_outputs = s_fns.mean_per_channel(q_outputs, channel_axis)
bias_shift = fns.stack(output_fp) - q_outputs
q_outputs = mean_per_channel(q_outputs, channel_axis)
bias_shift = nncf.stack(output_fp) - q_outputs
return bias_shift

def get_statistic_points(self, model: TModel, graph: NNCFGraph) -> StatisticPointsContainer:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
import numpy as np
import onnx

from nncf import Tensor
from nncf.common.graph import NNCFGraph
from nncf.common.graph import NNCFNode
from nncf.common.graph.transformations.commands import TargetType
from nncf.common.tensor_statistics.collectors import ReductionShape
from nncf.common.utils.backend import BackendType
from nncf.experimental.tensor import Tensor
from nncf.onnx.graph.node_utils import get_bias_value
from nncf.onnx.graph.node_utils import is_any_weight_quantized
from nncf.onnx.graph.node_utils import is_node_with_bias
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
import numpy as np
import openvino.runtime as ov

from nncf import Tensor
from nncf.common.graph import NNCFGraph
from nncf.common.graph import NNCFNode
from nncf.common.graph.transformations.commands import TargetType
from nncf.common.tensor_statistics.collectors import ReductionShape
from nncf.common.utils.backend import BackendType
from nncf.experimental.common.tensor_statistics.collectors import TensorCollector
from nncf.experimental.tensor import Tensor
from nncf.openvino.graph.metatypes.groups import FAKE_QUANTIZE_OPERATIONS
from nncf.openvino.graph.node_utils import get_bias_value
from nncf.openvino.graph.node_utils import is_node_with_bias
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
import numpy as np
import torch

from nncf import Tensor
from nncf.common.graph import NNCFGraph
from nncf.common.graph import NNCFNode
from nncf.common.graph.definitions import NNCFGraphNodeType
from nncf.common.graph.transformations.commands import TargetType
from nncf.common.tensor_statistics.collectors import ReductionShape
from nncf.common.utils.backend import BackendType
from nncf.experimental.tensor import Tensor
from nncf.quantization.algorithms.fast_bias_correction.backend import ALGO_BACKENDS
from nncf.quantization.algorithms.fast_bias_correction.backend import FastBiasCorrectionAlgoBackend
from nncf.torch.graph.transformations.command_creation import create_bias_correction_command
Expand Down
42 changes: 22 additions & 20 deletions nncf/quantization/fake_quantize.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@

import numpy as np

import nncf
from nncf import Tensor
from nncf import TensorDataType
from nncf.common.quantization.quantizers import calculate_asymmetric_level_ranges
from nncf.common.quantization.quantizers import calculate_symmetric_level_ranges
from nncf.common.quantization.quantizers import get_num_levels
from nncf.common.quantization.structs import QuantizationMode
from nncf.common.quantization.structs import QuantizerConfig
from nncf.common.quantization.structs import QuantizerGroup
from nncf.common.tensor_statistics.statistics import MinMaxTensorStatistic
from nncf.experimental.tensor import Tensor
from nncf.experimental.tensor import TensorDataType
from nncf.experimental.tensor import functions as fns


@dataclass
Expand Down Expand Up @@ -53,9 +53,9 @@ def fix_zero_filters_symmetric(max_values: Tensor, eps: float = 0.01) -> Tensor:
:param eps: Correction coefficient.
:return: Fixed the high quant number.
"""
max_range = fns.max(max_values)
lower_threshold = fns.maximum(max_range * eps, 8e-5)
return fns.maximum(lower_threshold, max_values)
max_range = nncf.max(max_values)
lower_threshold = nncf.maximum(max_range * eps, 8e-5)
return nncf.maximum(lower_threshold, max_values)


def fix_zero_filters_asymmetric(min_values: Tensor, max_values: Tensor, eps: float = 1e-8) -> Tuple[Tensor, Tensor]:
Expand All @@ -71,7 +71,9 @@ def fix_zero_filters_asymmetric(min_values: Tensor, max_values: Tensor, eps: flo
"""
ranges = max_values - min_values
min_correction = 8e-4
corrections = fns.where(ranges > min_correction, (fns.maximum(eps * ranges, ranges) - ranges) * 0.5, min_correction)
corrections = nncf.where(
ranges > min_correction, (nncf.maximum(eps * ranges, ranges) - ranges) * 0.5, min_correction
)

level_low = min_values - corrections
level_high = max_values + corrections
Expand Down Expand Up @@ -99,21 +101,21 @@ def tune_range(
if unify_zp:
scale = (right_border - left_border) / level_high
zero_point = -left_border / scale
avg_zpts = fns.round(fns.mean(zero_point))
qval = fns.ones_like(left_border) * avg_zpts
avg_zpts = nncf.round(nncf.mean(zero_point))
qval = nncf.ones_like(left_border) * avg_zpts
else:
s = level_high / (right_border - left_border)
fval = -left_border * s
qval = fns.round(fval)
qval = nncf.round(fval)

ra = fns.where(qval < level_high, qval / (qval - level_high) * right_border, left_border)
rb = fns.where(qval > 0.0, (qval - level_high) / qval * left_border, right_border)
ra = nncf.where(qval < level_high, qval / (qval - level_high) * right_border, left_border)
rb = nncf.where(qval > 0.0, (qval - level_high) / qval * left_border, right_border)

range_a = right_border - ra
range_b = rb - left_border

mask = fns.where(range_a > range_b, 1.0, 0.0)
inv_mask = fns.abs(1.0 - mask)
mask = nncf.where(range_a > range_b, 1.0, 0.0)
inv_mask = nncf.abs(1.0 - mask)

ra = mask * ra + inv_mask * left_border
rb = inv_mask * rb + mask * right_border
Expand Down Expand Up @@ -145,8 +147,8 @@ def symmetric_range(
else:
signed = quantizer_config.signedness_to_force is True
level_low = (
fns.zeros_like(level_high)
if fns.all(min_values >= 0) and not signed
nncf.zeros_like(level_high)
if nncf.all(min_values >= 0) and not signed
else -level_high * levels / (levels - 2)
)

Expand Down Expand Up @@ -175,8 +177,8 @@ def asymmetric_range(
level_high - the high quant number
"""
level_low, level_high = fix_zero_filters_asymmetric(min_values, max_values)
level_low = fns.where(level_low < 0.0, level_low, 0.0)
level_high = fns.where(level_high > 0.0, level_high, 0.0)
level_low = nncf.where(level_low < 0.0, level_low, 0.0)
level_high = nncf.where(level_high > 0.0, level_high, 0.0)

if unify_zp and q_group == QuantizerGroup.ACTIVATIONS:
raise NotImplementedError("Unified zero point is not supported for activations.")
Expand Down Expand Up @@ -239,8 +241,8 @@ def calculate_quantizer_parameters(
input_low, input_high = asymmetric_range(min_values, max_values, quantizer_config, quant_group)

if not quantizer_config.per_channel:
input_low = fns.squeeze(input_low)
input_high = fns.squeeze(input_high)
input_low = nncf.squeeze(input_low)
input_high = nncf.squeeze(input_high)

output_low, output_high = input_low, input_high
return FakeQuantizeParameters(input_low, input_high, output_low, output_high, levels)
Expand Down
2 changes: 1 addition & 1 deletion tests/onnx/quantization/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import onnx

from nncf import Dataset
from nncf.experimental.tensor import Tensor
from nncf import Tensor
from nncf.onnx.graph.nncf_graph_builder import GraphConverter
from nncf.onnx.graph.onnx_graph import ONNXGraph
from nncf.onnx.statistics.statistics import ONNXMinMaxTensorStatistic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
import numpy as np
import pytest

import nncf
from nncf.common.quantization.structs import QuantizationMode
from nncf.common.quantization.structs import QuantizerConfig
from nncf.common.quantization.structs import QuantizerGroup
from nncf.experimental.tensor import functions as fns
from nncf.quantization.fake_quantize import FakeQuantizeParameters
from nncf.quantization.fake_quantize import calculate_quantizer_parameters
from tests.post_training.conftest import FQ_CALCULATED_PARAMETERS_PATH
Expand All @@ -33,10 +33,10 @@ def compare_fq_parameters(ref_params, params):
assert ref_params.input_high.shape == params.input_high.shape
assert ref_params.output_low.shape == params.output_low.shape
assert ref_params.output_high.shape == params.output_high.shape
assert fns.allclose(ref_params.input_low, params.input_low)
assert fns.allclose(ref_params.input_high, params.input_high)
assert fns.allclose(ref_params.output_low, params.output_low)
assert fns.allclose(ref_params.output_high, params.output_high)
assert nncf.allclose(ref_params.input_low, params.input_low)
assert nncf.allclose(ref_params.input_high, params.input_high)
assert nncf.allclose(ref_params.output_low, params.output_low)
assert nncf.allclose(ref_params.output_high, params.output_high)


def get_test_reference_key(q_group, q_config, narrow_range, hf_range):
Expand Down
Loading

0 comments on commit 04cd1dc

Please sign in to comment.