Skip to content

Commit

Permalink
Renamed to 'inverted_divide'. Add a test.
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-savelyevv committed Nov 18, 2024
1 parent 213c990 commit 1b4e986
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions nncf/quantization/fake_quantize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions nncf/tensor/functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions nncf/tensor/functions/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions tests/cross_fw/test_templates/template_test_nncf_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 1b4e986

Please sign in to comment.