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

[TOPI][RELAY][TENSORFLOW]Math ops added #5502

Merged
merged 5 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions python/tvm/relay/frontend/tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1947,15 +1947,20 @@ def _impl(inputs, attr, params, mod):
# for N to 1 mapping, currently not supported(?)
_convert_map = {
'Abs' : AttrCvt('abs'),
'Acos' : AttrCvt('acos'),
'Acosh' : AttrCvt('acosh'),
'Add' : _elemwise('add'),
'AddN' : _add_n(),
'AddV2' : _elemwise('add'),
'All' : _reduce('all'),
'Any' : _reduce('any'),
'ArgMax' : _argx(_op.argmax, 'argmax'),
'ArgMin' : _argx(_op.argmin, 'argmin'),
'Asin' : AttrCvt('asin'),
'Asinh' : AttrCvt('asinh'),
'Assert' : _assert(),
'Atan' : AttrCvt('atan'),
'Atanh' : AttrCvt('atanh'),
'Atan2' : _atan2(),
'AvgPool' : _pooling('avg_pool'),
'AvgPool3D' : _pool3d('avg_pool3d'),
Expand All @@ -1975,6 +1980,7 @@ def _impl(inputs, attr, params, mod):
'Conv2DBackpropInput' : _conv('conv_transpose'),
'Conv3D' : _conv3d('conv'),
'Cos' : AttrCvt('cos'),
'Cosh' : AttrCvt('cosh'),
'CropAndResize' : _crop_and_resize(),
'DecodeJpeg' : _decode_image(),
'DepthToSpace' : _depth_to_space(),
Expand Down Expand Up @@ -2051,6 +2057,7 @@ def _impl(inputs, attr, params, mod):
'Sigmoid' : AttrCvt('sigmoid'),
'Sign' : AttrCvt('sign'),
'Sin' : AttrCvt('sin'),
'Sinh' : AttrCvt('sinh'),
'Size' : _size(),
'Slice' : _slice(),
'Softmax' : _softmax(),
Expand Down
7 changes: 6 additions & 1 deletion python/tvm/relay/op/_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
# under the License.
#pylint: disable=invalid-name, unused-argument, len-as-condition
"""Backend compiler related feature registration"""
import topi

from tvm.runtime import convert
from tvm.te.hybrid import script
import topi
from topi.util import get_const_tuple
from .op import register_compute, register_shape_func
from .op import register_broadcast_schedule, register_injective_schedule
Expand All @@ -34,7 +34,12 @@
register_broadcast_schedule("cosh")
register_broadcast_schedule("sin")
register_broadcast_schedule("sinh")
register_broadcast_schedule("acos")
register_broadcast_schedule("acosh")
register_broadcast_schedule("asin")
register_broadcast_schedule("asinh")
register_broadcast_schedule("atan")
register_broadcast_schedule("atanh")
register_broadcast_schedule("exp")
register_broadcast_schedule("erf")
register_broadcast_schedule("sqrt")
Expand Down
51 changes: 50 additions & 1 deletion python/tvm/relay/op/_tensor_grad.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
power,
sin,
sinh,
sqrt,
zeros_like,
equal,
shape_of,
Expand Down Expand Up @@ -116,12 +117,60 @@ def sinh_grad(orig, grad):
x = orig.args[0]
return [grad * cosh(x)]


@register_gradient("acos")
def acos_grad(orig, grad):
"""Returns [grad * -1/((1 - (x ^ 2)) ^ 1/2)]"""
x = orig.args[0]
a = const(2.0)
ones = ones_like(x)
return [grad * (-ones / sqrt(ones_like(x) - power(x, a)))]
siju-samuel marked this conversation as resolved.
Show resolved Hide resolved


@register_gradient("acosh")
def acosh_grad(orig, grad):
"""Returns [grad * 1/((x - 1) ^ 1/2 * (x + 1) ^ 1/2)]"""
x = orig.args[0]
a = const(2.0)
ones = ones_like(x)
return [grad * ones / sqrt(power(x, a) - ones)]
siju-samuel marked this conversation as resolved.
Show resolved Hide resolved


@register_gradient("asin")
def asin_grad(orig, grad):
"""Returns [grad * 1/((1 - (x ^ 2)) ^ (1/2))]"""
x = orig.args[0]
a = const(2.0)
ones = ones_like(x)
return [grad * ones / sqrt(ones - power(x, a))]
siju-samuel marked this conversation as resolved.
Show resolved Hide resolved


@register_gradient("asinh")
def asinh_grad(orig, grad):
"""Returns [grad * 1/((1 + (x ^ 2)) ^ (1/2))]"""
x = orig.args[0]
a = const(2.0)
ones = ones_like(x)
return [grad * ones / sqrt(ones + power(x, a))]
siju-samuel marked this conversation as resolved.
Show resolved Hide resolved


@register_gradient("atan")
def atan_grad(orig, grad):
"""Returns [grad * 1 / (1 + x ^ 2)]"""
x = orig.args[0]
a = const(2.0)
return [grad * ones_like(x) / (ones_like(x) + power(x, a))]
ones = ones_like(x)
return [grad * ones / (ones + power(x, a))]
siju-samuel marked this conversation as resolved.
Show resolved Hide resolved


@register_gradient("atanh")
def atanh_grad(orig, grad):
"""Returns xx[grad * 1 / (1 - x ^ 2)]"""
siju-samuel marked this conversation as resolved.
Show resolved Hide resolved
x = orig.args[0]
a = const(2.0)
ones = ones_like(x)
return [grad * ones / (ones - power(x, a))]
siju-samuel marked this conversation as resolved.
Show resolved Hide resolved


@register_gradient("exp")
def exp_grad(orig, grad):
Expand Down
75 changes: 75 additions & 0 deletions python/tvm/relay/op/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,66 @@ def sinh(data):
"""
return _make.sinh(data)

def acos(data):
"""Compute elementwise acos of data.

Parameters
----------
data : relay.Expr
The input data

Returns
-------
result : relay.Expr
The computed result.
"""
return _make.acos(data)

def acosh(data):
"""Compute elementwise acosh of data.

Parameters
----------
data : relay.Expr
The input data

Returns
-------
result : relay.Expr
The computed result.
"""
return _make.acosh(data)

def asin(data):
"""Compute elementwise asin of data.

Parameters
----------
data : relay.Expr
The input data

Returns
-------
result : relay.Expr
The computed result.
"""
return _make.asin(data)

def asinh(data):
"""Compute elementwise asinh of data.

Parameters
----------
data : relay.Expr
The input data

Returns
-------
result : relay.Expr
The computed result.
"""
return _make.asinh(data)

def atan(data):
"""Compute elementwise atan of data.

Expand All @@ -167,6 +227,21 @@ def atan(data):
"""
return _make.atan(data)

def atanh(data):
"""Compute elementwise atanh of data.

Parameters
----------
data : relay.Expr
The input data

Returns
-------
result : relay.Expr
The computed result.
"""
return _make.atanh(data)

def exp(data):
"""Compute elementwise exp of data.

Expand Down
3 changes: 2 additions & 1 deletion python/tvm/te/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
"""
# expose all operators in tvm tir.op
from tvm.tir import any, all, min_value, max_value, trace
from tvm.tir import exp, erf, tanh, sigmoid, log, tan, cos, sin, atan, sqrt, rsqrt, floor, ceil
from tvm.tir import exp, erf, tanh, sigmoid, log, tan, cos, sin, sqrt, rsqrt, floor, ceil
from tvm.tir import sinh, cosh, log2, log10
from tvm.tir import asin, asinh, acos, acosh, atan, atanh
from tvm.tir import trunc, abs, round, nearbyint, power, popcount, fmod, if_then_else
from tvm.tir import isnan, isfinite, isinf
from tvm.tir import div, indexdiv, indexmod, truncdiv, truncmod, floordiv, floormod
Expand Down
55 changes: 55 additions & 0 deletions src/relay/op/tensor/unary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,50 @@ RELAY_REGISTER_UNARY_OP("sinh")
.set_attr<FTVMCompute>("FTVMCompute", RELAY_UNARY_COMPUTE(topi::sinh));


RELAY_REGISTER_UNARY_OP("acos")
.describe(R"code(Returns the acos of input array, computed element-wise.

.. math::
Y = acos(X)

)code" TVM_ADD_FILELINE)
.set_support_level(1)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_UNARY_COMPUTE(topi::acos));


RELAY_REGISTER_UNARY_OP("acosh")
.describe(R"code(Returns the acosh of input array, computed element-wise.

.. math::
Y = acosh(X)

)code" TVM_ADD_FILELINE)
.set_support_level(1)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_UNARY_COMPUTE(topi::acosh));


RELAY_REGISTER_UNARY_OP("asin")
.describe(R"code(Returns the asin of input array, computed element-wise.

.. math::
Y = asin(X)

)code" TVM_ADD_FILELINE)
.set_support_level(1)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_UNARY_COMPUTE(topi::asin));


RELAY_REGISTER_UNARY_OP("asinh")
.describe(R"code(Returns the asinh of input array, computed element-wise.

.. math::
Y = asinh(X)

)code" TVM_ADD_FILELINE)
.set_support_level(1)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_UNARY_COMPUTE(topi::asinh));


RELAY_REGISTER_UNARY_OP("atan")
.describe(R"code(Returns the atan of input array, computed element-wise.

Expand All @@ -139,6 +183,17 @@ RELAY_REGISTER_UNARY_OP("atan")
.set_attr<FTVMCompute>("FTVMCompute", RELAY_UNARY_COMPUTE(topi::atan));


RELAY_REGISTER_UNARY_OP("atanh")
.describe(R"code(Returns the atanh of input array, computed element-wise.

.. math::
Y = atanh(X)

)code" TVM_ADD_FILELINE)
.set_support_level(1)
.set_attr<FTVMCompute>("FTVMCompute", RELAY_UNARY_COMPUTE(topi::atanh));


RELAY_REGISTER_UNARY_OP("exp")
.describe(R"code(Returns the exp input array, computed element-wise.

Expand Down
Loading