Skip to content
This repository has been archived by the owner on Nov 25, 2022. It is now read-only.

Commit

Permalink
[FQ2I] fix unary op output affine type in fq2i (apache#12224)
Browse files Browse the repository at this point in the history
* fix unary op output affine type in fq2i

* better names

* add option to force to positive values for ops that are undefined on negative values
  • Loading branch information
Matthew Brookhart authored and xinetzone committed Nov 25, 2022
1 parent fb49291 commit bb7f48f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion python/tvm/relay/transform/fake_quantization_to_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ def unary(expr, type_map):
out_t.scale,
out_t.zero_point,
)
return [out, x_t]
return [out, out_t]

return register_fake_quantization_to_integer(op_name, unary)

Expand Down
35 changes: 24 additions & 11 deletions tests/python/relay/test_pass_fake_quantization_to_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,23 +318,36 @@ def test_fake_quantize_global_avg_pool():


class TestUnaryQNNOp:
def helper_test_fake_quantize_unary_op(self, fp32_op, scale=0.125):
x = relay.var("x", shape=[1, 3, 3, 3], dtype="int8")
mid_point = relay.const(-128)
def helper_test_fake_quantize_unary_op(self, fp32_op, pos_values=False):
for dtype in ["int8", "uint8"]:
x = relay.var("x", shape=[1, 3, 3, 3], dtype=dtype)

x = relay.qnn.op.dequantize(x, relay.const(scale), mid_point)
op = fp32_op(x)
op = relay.qnn.op.quantize(op, relay.const(scale), mid_point)
zero = -128 if dtype == "int8" else 0
if pos_values:
# Use a positive range for quanitzed ops that only work on positive values
input_mid_point = relay.const(zero)
output_mid_point = relay.const(zero)
else:
input_mid_point = relay.const(np.random.randint(0, 255) + zero)
output_mid_point = relay.const(np.random.randint(0, 255) + zero)

x_np = np.random.randint(-128, 127, size=[1, 3, 3, 3], dtype="int8")
input_scale = relay.const(np.random.rand())
output_scale = relay.const(np.random.rand())

compare_fq_to_int(op, [x_np], True)
x = relay.qnn.op.dequantize(x, input_scale, input_mid_point)
op = fp32_op(x)

op = relay.qnn.op.quantize(op, output_scale, output_mid_point, out_dtype=dtype)

x_np = np.random.randint(0 + zero, 255 + zero, size=[1, 3, 3, 3], dtype=dtype)

compare_fq_to_int(op, [x_np], True)

def test_sqrt(self):
self.helper_test_fake_quantize_unary_op(fp32_op=relay.sqrt)
self.helper_test_fake_quantize_unary_op(fp32_op=relay.sqrt, pos_values=True)

def test_rsqrt(self):
self.helper_test_fake_quantize_unary_op(fp32_op=relay.rsqrt)
self.helper_test_fake_quantize_unary_op(fp32_op=relay.rsqrt, pos_values=True)

def test_exp(self):
self.helper_test_fake_quantize_unary_op(fp32_op=relay.exp)
Expand All @@ -349,7 +362,7 @@ def test_tanh(self):
self.helper_test_fake_quantize_unary_op(fp32_op=relay.tanh)

def test_log(self):
self.helper_test_fake_quantize_unary_op(fp32_op=relay.log)
self.helper_test_fake_quantize_unary_op(fp32_op=relay.log, pos_values=True)


def test_fake_quantize_reshape():
Expand Down

0 comments on commit bb7f48f

Please sign in to comment.