Skip to content

Commit

Permalink
[FQ2I] Add log op to FQ2I (apache#10924)
Browse files Browse the repository at this point in the history
* unary op for resize2d and test

* renamed test

* added log in quantized form

* black'd some files

* changed suggested commentary
  • Loading branch information
CircleSpin authored Apr 21, 2022
1 parent a52b575 commit 97ae25c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions python/tvm/relay/qnn/op/legalizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def legalize_qnn_unary_op(attrs, inputs, types):
register_qnn_unary_op_legalize("qnn.erf", special.erf)
register_qnn_unary_op_legalize("qnn.sigmoid", lambda arr: 1 / (1 + np.exp(-arr)))
register_qnn_unary_op_legalize("qnn.tanh", np.tanh)
register_qnn_unary_op_legalize("qnn.log", np.log)


# Default to None. If overridden by target, this will not be run.
Expand Down
35 changes: 35 additions & 0 deletions python/tvm/relay/qnn/op/qnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,41 @@ def sigmoid(x, scale, zero_point, output_scale, output_zero_point):
)


def log(x, scale, zero_point, output_scale, output_zero_point):
"""Quantized log.
Parameters
----------
x : relay.Expr
The quantized input tensor.
scale: relay.Expr
The scale of the quantized expr.
zero_point: relay.Expr
The zero point of quantized expr.
output_scale: relay.Expr
The scale of the output quantized expr.
output_zero_point: relay.Expr
The zero point of output quantized expr.
Returns
-------
result : relay.Expr
The computed result.
"""
return _make.log(
x,
scale,
zero_point,
output_scale,
output_zero_point,
)


def subtract(
lhs,
rhs,
Expand Down
1 change: 1 addition & 0 deletions python/tvm/relay/transform/fake_quantization_to_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,4 @@ def unary(expr, type_map):
register_unary_qnn("erf", relay.qnn.op.erf)
register_unary_qnn("sigmoid", relay.qnn.op.sigmoid)
register_unary_qnn("tanh", relay.qnn.op.tanh)
register_unary_qnn("log", relay.qnn.op.log)
5 changes: 5 additions & 0 deletions src/relay/qnn/op/unary_elementwise_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,19 @@ QNN_CREATE_UNARY_ELEMENTWISE_OP("exp").set_attr<FTVMLegalize>(

QNN_CREATE_UNARY_ELEMENTWISE_OP("sqrt").set_attr<FTVMLegalize>(
"FTVMQnnCanonicalize", QNN_UNARY_OP_DEFAULT_CANONICALIZATION(Sqrt));

QNN_CREATE_UNARY_ELEMENTWISE_OP("rsqrt").set_attr<FTVMLegalize>(
"FTVMQnnCanonicalize", QNN_UNARY_OP_DEFAULT_CANONICALIZATION(Rsqrt));

QNN_CREATE_UNARY_ELEMENTWISE_OP("erf").set_attr<FTVMLegalize>(
"FTVMQnnCanonicalize", QNN_UNARY_OP_DEFAULT_CANONICALIZATION(Erf));

QNN_CREATE_UNARY_ELEMENTWISE_OP("sigmoid").set_attr<FTVMLegalize>(
"FTVMQnnCanonicalize", QNN_UNARY_OP_DEFAULT_CANONICALIZATION(Sigmoid));

QNN_CREATE_UNARY_ELEMENTWISE_OP("log").set_attr<FTVMLegalize>(
"FTVMQnnCanonicalize", QNN_UNARY_OP_DEFAULT_CANONICALIZATION(Log));

} // namespace qnn
} // namespace relay
} // namespace tvm
3 changes: 3 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 @@ -347,6 +347,9 @@ def test_sigmoid(self):
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)


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

0 comments on commit 97ae25c

Please sign in to comment.