Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FQ2I] fix unary op output affine type in fq2i #12224

Merged
merged 3 commits into from
Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 17 additions & 8 deletions tests/python/relay/test_pass_fake_quantization_to_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,26 @@ 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):
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

x_np = np.random.randint(-128, 127, size=[1, 3, 3, 3], dtype="int8")
mid_point = relay.const(np.random.randint(0, 255) + zero)
mbrookhart marked this conversation as resolved.
Show resolved Hide resolved
scale = relay.const(np.random.rand())

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

mid_point = relay.const(np.random.randint(0, 255) + zero)
scale = relay.const(np.random.rand())

op = relay.qnn.op.quantize(op, scale, 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)
Expand Down