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

[F2QI] Fix a rounding error on AvgPool when input and output affine scales differ #12577

Merged
merged 3 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
64 changes: 55 additions & 9 deletions python/tvm/relay/transform/fake_quantization_to_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,33 +114,79 @@ def adaptive_avgpool1d(expr, type_map):
"""Rewrite an adaptive avgpool op"""
arg = expr.args[0]
t = type_map[arg]
arg = relay.op.cast(arg, "int32")
out_t = type_map[expr]
if not (
approx_equal(t.scale, out_t.scale)
and approx_equal(t.zero_point, out_t.zero_point)
and tvm.ir.structural_equal(t.dtype, out_t.dtype)
):
arg = relay.qnn.op.requantize(
arg,
t.scale,
t.zero_point,
out_t.scale,
out_t.zero_point,
out_dtype="int32",
axis=t.axis,
)
else:
arg = relay.op.cast(arg, "int32")
output_size = expr.attrs.output_size
out = relay.op.nn.adaptive_avg_pool1d(arg, output_size)
out = relay.op.cast(out, t.dtype)
return [out, t]
return [out, TensorAffineType(out_t.scale, out_t.zero_point, "int32", out_t.axis)]


@register_fake_quantization_to_integer("nn.avg_pool2d")
def avgpool2d(expr, type_map):
"""Rewrite a avgpool op"""
arg = expr.args[0]
t = type_map[arg]
arg = relay.op.cast(arg, "int32")
out_t = type_map[expr]
if not (
approx_equal(t.scale, out_t.scale)
and approx_equal(t.zero_point, out_t.zero_point)
and tvm.ir.structural_equal(t.dtype, out_t.dtype)
):
arg = relay.qnn.op.requantize(
arg,
t.scale,
t.zero_point,
out_t.scale,
out_t.zero_point,
out_dtype="int32",
axis=t.axis,
)
else:
arg = relay.op.cast(arg, "int32")
out = relay.op.nn.avg_pool2d(arg, **expr.attrs)
out = relay.op.cast(out, t.dtype)
return [out, t]
return [out, TensorAffineType(out_t.scale, out_t.zero_point, "int32", out_t.axis)]


@register_fake_quantization_to_integer("nn.global_avg_pool2d")
def global_avgpool2d(expr, type_map):
"""Rewrite a global_avgpool op"""
arg = expr.args[0]
t = type_map[arg]
arg = relay.op.cast(arg, "int32")
out_t = type_map[expr]
out_t = type_map[expr]
if not (
approx_equal(t.scale, out_t.scale)
and approx_equal(t.zero_point, out_t.zero_point)
and tvm.ir.structural_equal(t.dtype, out_t.dtype)
):
arg = relay.qnn.op.requantize(
arg,
t.scale,
t.zero_point,
out_t.scale,
out_t.zero_point,
out_dtype="int32",
axis=t.axis,
)
else:
arg = relay.op.cast(arg, "int32")
out = relay.op.nn.global_avg_pool2d(arg)
out = relay.op.cast(out, t.dtype)
return [out, t]
return [out, TensorAffineType(out_t.scale, out_t.zero_point, "int32", out_t.axis)]


@register_fake_quantization_to_integer("broadcast_to")
Expand Down
15 changes: 6 additions & 9 deletions tests/python/relay/test_pass_fake_quantization_to_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,9 @@ def test_fake_quantize_maxpool():
def test_fake_quantize_adaptive_avgpool1d(output_size):
x = relay.var("x", shape=[1, 128, 768], dtype="int8")

zero = relay.const(0)
x = relay.qnn.op.dequantize(x, relay.const(2.0), zero)
x = relay.qnn.op.dequantize(x, relay.const(2.0), relay.const(-12))
op = relay.op.nn.adaptive_avg_pool1d(x, output_size)
op = relay.qnn.op.quantize(op, relay.const(2.0), zero)
op = relay.qnn.op.quantize(op, relay.const(0.5), relay.const(10))

x_np = np.random.randint(-128, 127, size=[1, 128, 768], dtype="int8")

Expand All @@ -294,10 +293,9 @@ def test_fake_quantize_adaptive_avgpool1d(output_size):
def test_fake_quantize_avgpool():
x = relay.var("x", shape=[1, 3, 224, 224], dtype="int8")

zero = relay.const(0)
x = relay.qnn.op.dequantize(x, relay.const(2.0), zero)
x = relay.qnn.op.dequantize(x, relay.const(2.0), relay.const(-12))
op = relay.op.nn.avg_pool2d(x, [3, 3])
op = relay.qnn.op.quantize(op, relay.const(2.0), zero)
op = relay.qnn.op.quantize(op, relay.const(0.5), relay.const(10))

x_np = np.random.randint(-128, 127, size=[1, 3, 224, 224], dtype="int8")

Expand All @@ -307,10 +305,9 @@ def test_fake_quantize_avgpool():
def test_fake_quantize_global_avg_pool():
x = relay.var("x", shape=[1, 3, 224, 224], dtype="int8")

zero = relay.const(0)
x = relay.qnn.op.dequantize(x, relay.const(2.0), zero)
x = relay.qnn.op.dequantize(x, relay.const(2.0), relay.const(-12))
op = relay.op.nn.global_avg_pool2d(x)
op = relay.qnn.op.quantize(op, relay.const(2.0), zero)
op = relay.qnn.op.quantize(op, relay.const(0.5), relay.const(10))

x_np = np.random.randint(-128, 127, size=[1, 3, 224, 224], dtype="int8")

Expand Down