Skip to content

Commit

Permalink
[FQ2I] Add abs to FQ2I (apache#10922)
Browse files Browse the repository at this point in the history
* add abs to fq2i

* lint

* special case for fq2i

* np iinfo
  • Loading branch information
anwang2009 authored and Lucien0 committed Apr 19, 2022
1 parent 199c3eb commit a7b2db8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions python/tvm/relay/transform/fake_quantization_to_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ def identity(expr, type_map):
register_unary_identity("image.resize2d")


@register_fake_quantization_to_integer("abs")
def abs_(expr, type_map):
"""Rewrite an abs op"""
assert len(expr.args) == 1
arg = expr.args[0]
t = type_map[arg]

min_value = relay.const(np.iinfo(t.dtype).min, t.dtype)
one = relay.const(1, t.dtype)
out = relay.op.where(relay.op.equal(min_value, arg), arg + one, arg)
out = relay.op.abs(out)
return [out, t]


@register_fake_quantization_to_integer("nn.adaptive_avg_pool1d")
def adaptive_avgpool1d(expr, type_map):
"""Rewrite an adaptive avgpool op"""
Expand Down
13 changes: 13 additions & 0 deletions tests/python/relay/test_pass_fake_quantization_to_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,19 @@ def test_fake_quantize_image_resize_bilinear():
compare_fq_to_int(op, [x_np], allow_rounding_error=True)


def test_fake_quantize_abs():
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)
op = relay.op.abs(x)
op = relay.qnn.op.quantize(op, relay.const(2.0), zero)

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

compare_fq_to_int(op, [x_np])


def test_fake_quantize_expand_dims():
x = relay.var("x", shape=[1, 3, 224, 224], dtype="int8")

Expand Down

0 comments on commit a7b2db8

Please sign in to comment.