Skip to content

Commit

Permalink
[TOPI][RELAY][TENSORFLOW]Math ops added (apache#5502)
Browse files Browse the repository at this point in the history
* [TOPI][RELAY][TENSORFLOW]Math ops added

* Extra newline removed

* CI fix

* Review comments fixed

* Review comments fixed
  • Loading branch information
siju-samuel authored and Trevor Morris committed Jun 18, 2020
1 parent 1071831 commit 1695971
Show file tree
Hide file tree
Showing 11 changed files with 340 additions and 80 deletions.
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
53 changes: 48 additions & 5 deletions 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 @@ -98,10 +99,9 @@ def cos_grad(orig, grad):

@register_gradient("cosh")
def cosh_grad(orig, grad):
"""Returns [grad * (-sinh(x))]"""
"""Returns [grad * sinh(x)]"""
x = orig.args[0]
ones = ones_like(x)
return [grad * (-ones * sinh(x))]
return [grad * sinh(x)]


@register_gradient("sin")
Expand All @@ -110,18 +110,61 @@ def sin_grad(orig, grad):
x = orig.args[0]
return [grad * cos(x)]


@register_gradient("sinh")
def sinh_grad(orig, grad):
"""Returns [grad * cosh(x)]"""
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]
ones = ones_like(x)
return [grad * (-ones / sqrt(ones - (x * x)))]


@register_gradient("acosh")
def acosh_grad(orig, grad):
"""Returns [grad * 1/((x - 1) ^ 1/2 * (x + 1) ^ 1/2)]"""
x = orig.args[0]
ones = ones_like(x)
return [grad * ones / sqrt((x * x) - ones)]


@register_gradient("asin")
def asin_grad(orig, grad):
"""Returns [grad * 1/((1 - (x ^ 2)) ^ (1/2))]"""
x = orig.args[0]
ones = ones_like(x)
return [grad * ones / sqrt(ones - (x * x))]


@register_gradient("asinh")
def asinh_grad(orig, grad):
"""Returns [grad * 1/((1 + (x ^ 2)) ^ (1/2))]"""
x = orig.args[0]
ones = ones_like(x)
return [grad * ones / sqrt(ones + (x * x))]


@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 + (x * x))]


@register_gradient("atanh")
def atanh_grad(orig, grad):
"""Returns [grad * 1 / (1 - x ^ 2)]"""
x = orig.args[0]
ones = ones_like(x)
return [grad * ones / (ones - (x * x))]


@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

0 comments on commit 1695971

Please sign in to comment.