diff --git a/nncf/quantization/algorithms/weight_compression/weight_lowering.py b/nncf/quantization/algorithms/weight_compression/weight_lowering.py index 1df00340fc8..6ed7501cc8f 100644 --- a/nncf/quantization/algorithms/weight_compression/weight_lowering.py +++ b/nncf/quantization/algorithms/weight_compression/weight_lowering.py @@ -154,7 +154,7 @@ def calculate_signed_scale(weight: Tensor, reduction_axes: ReductionAxes, num_bi w_max = fns.max(weight, axis=reduction_axes, keepdims=True) scale = fns.where(w_abs_min >= w_max, w_abs_min, -w_max) - fns.inplace_divide(scale, level_high) + fns.inplace_inverted_divide(scale, level_high) eps = fns.finfo(scale).eps scale = fns.where(fns.abs(scale) < eps, eps, scale) @@ -307,7 +307,7 @@ def calculate_quantized_weight( level_low = 0 if asym_quant else -(2 ** (num_bits - 1)) level_high = 2**num_bits - 1 if asym_quant else 2 ** (num_bits - 1) - 1 - compressed_weights = fns.divide(weight, scale) + compressed_weights = fns.inverted_divide(weight, scale) if zero_point is not None: compressed_weights += zero_point.astype(weight.dtype) compressed_weights = fns.round(compressed_weights) diff --git a/nncf/quantization/fake_quantize.py b/nncf/quantization/fake_quantize.py index 060a8b946cf..e27ee0c46d2 100644 --- a/nncf/quantization/fake_quantize.py +++ b/nncf/quantization/fake_quantize.py @@ -355,11 +355,11 @@ def calculate_scale_zero_point( :return: Scale and Zero point values. """ levels = level_high - level_low if narrow_range else level_high - level_low + 1 - scale = fns.divide((input_high - input_low), (levels - 1)).astype(TensorDataType.float32) + scale = fns.inverted_divide((input_high - input_low), (levels - 1)).astype(TensorDataType.float32) eps = fns.finfo(scale).eps # NOTE: adding machine epsilon to avoid division by zero scale = fns.where(fns.abs(scale) < eps, eps, scale) expected_level_low = level_low + 1 if narrow_range else level_low - zero_point = expected_level_low - fns.round(fns.divide(input_low, scale)) + zero_point = expected_level_low - fns.round(fns.inverted_divide(input_low, scale)) zero_point = fns.clip(zero_point.astype(TensorDataType.int32), level_low, level_high) return scale, zero_point diff --git a/nncf/tensor/functions/__init__.py b/nncf/tensor/functions/__init__.py index 9cfb8f105fb..3998ae371b3 100644 --- a/nncf/tensor/functions/__init__.py +++ b/nncf/tensor/functions/__init__.py @@ -24,14 +24,14 @@ from nncf.tensor.functions.numeric import count_nonzero as count_nonzero from nncf.tensor.functions.numeric import device as device from nncf.tensor.functions.numeric import diag as diag -from nncf.tensor.functions.numeric import divide as divide from nncf.tensor.functions.numeric import dtype as dtype from nncf.tensor.functions.numeric import expand_dims as expand_dims from nncf.tensor.functions.numeric import eye as eye from nncf.tensor.functions.numeric import finfo as finfo from nncf.tensor.functions.numeric import flatten as flatten from nncf.tensor.functions.numeric import from_numpy as from_numpy -from nncf.tensor.functions.numeric import inplace_divide as inplace_divide +from nncf.tensor.functions.numeric import inplace_inverted_divide as inplace_inverted_divide +from nncf.tensor.functions.numeric import inverted_divide as inverted_divide from nncf.tensor.functions.numeric import isclose as isclose from nncf.tensor.functions.numeric import isempty as isempty from nncf.tensor.functions.numeric import item as item diff --git a/nncf/tensor/functions/numeric.py b/nncf/tensor/functions/numeric.py index cdbdfde5d80..d1f1c68273f 100644 --- a/nncf/tensor/functions/numeric.py +++ b/nncf/tensor/functions/numeric.py @@ -909,7 +909,7 @@ def ceil(a: Tensor) -> Tensor: @functools.singledispatch @tensor_guard -def divide(a: Union[Tensor, float], b: Union[Tensor, float], invert: Optional[bool] = True) -> Tensor: +def inverted_divide(a: Union[Tensor, float], b: Union[Tensor, float], invert: Optional[bool] = True) -> Tensor: """ Divide two tensors or a tensor and a float. @@ -927,7 +927,7 @@ def divide(a: Union[Tensor, float], b: Union[Tensor, float], invert: Optional[bo @functools.singledispatch @tensor_guard -def inplace_divide(a: Union[Tensor, float], b: Union[Tensor, float], invert: Optional[bool] = True) -> None: +def inplace_inverted_divide(a: Union[Tensor, float], b: Union[Tensor, float], invert: Optional[bool] = True) -> None: """ In-place division of two tensors or a tensor and a float. diff --git a/tests/cross_fw/test_templates/template_test_nncf_tensor.py b/tests/cross_fw/test_templates/template_test_nncf_tensor.py index 13f2d6bc976..4ca345cc680 100644 --- a/tests/cross_fw/test_templates/template_test_nncf_tensor.py +++ b/tests/cross_fw/test_templates/template_test_nncf_tensor.py @@ -1695,3 +1695,17 @@ def test_svd(self, a, full_matrices, abs_res_ref): for act, abs_ref in zip(res, abs_res_ref): assert isinstance(act, Tensor) assert fns.allclose(fns.abs(act), abs_ref, atol=1e-7) + + def test_inverted_divide(self): + a = Tensor(self.to_tensor([1e8])).astype(TensorDataType.float32) + b = Tensor(self.to_tensor([7])).astype(TensorDataType.float32) + result = a / b + result_inverted_divide = fns.inverted_divide(a, b) + assert fns.allclose(result, Tensor(self.to_tensor([14285714])), atol=0, rtol=0) + assert fns.allclose(result_inverted_divide, Tensor(self.to_tensor([14285715])), atol=0, rtol=0) + + a /= b + a_copy = Tensor(self.to_tensor([1e8])).astype(TensorDataType.float32) + fns.inplace_inverted_divide(a_copy, b) + assert fns.allclose(a, result, atol=0, rtol=0) + assert fns.allclose(a_copy, result_inverted_divide, atol=0, rtol=0)