Skip to content

Commit 8233b01

Browse files
author
Alex Gladkov
committed
Add support for Tensorflow operators log1p and cos
The patch adds support for Tensorflow operators log1p and cos Tensorflow log1p is described at https://www.tensorflow.org/api_docs/python/tf/math/log1p Tensorflow cos is described at https://www.tensorflow.org/api_docs/python/tf/math/cos
1 parent 90eee08 commit 8233b01

File tree

13 files changed

+183
-1
lines changed

13 files changed

+183
-1
lines changed

include/tvm/expr_operator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ TVM_DECLARE_INTRIN_UNARY(sqrt);
517517
TVM_DECLARE_INTRIN_UNARY(rsqrt);
518518
TVM_DECLARE_INTRIN_UNARY(log);
519519
TVM_DECLARE_INTRIN_UNARY(popcount);
520-
520+
TVM_DECLARE_INTRIN_UNARY(cos);
521521

522522
// Implementation details after this
523523
inline bool is_const(const Expr& x) {

python/tvm/intrin.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,35 @@ def log(x):
258258
"""
259259
return call_pure_intrin(x.dtype, "log", x)
260260

261+
def log1p(x):
262+
"""Take log of input x.
263+
264+
Parameters
265+
----------
266+
x : Expr
267+
Input argument.
268+
269+
Returns
270+
-------
271+
y : Expr
272+
The result.
273+
"""
274+
return call_pure_intrin(x.dtype, "log1p", x)
275+
276+
def cos(x):
277+
"""Take cos of input x.
278+
279+
Parameters
280+
----------
281+
x : Expr
282+
Input argument.
283+
284+
Returns
285+
-------
286+
y : Expr
287+
The result.
288+
"""
289+
return call_pure_intrin(x.dtype, "cos", x)
261290

262291
def sqrt(x):
263292
"""Take square root of input x.

python/tvm/relay/frontend/tensorflow.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,8 @@ def _impl(inputs, attr, params):
13441344
'Less' : _broadcast('less'),
13451345
'LessEqual' : _broadcast('less_equal'),
13461346
'Log' : AttrCvt('log'),
1347+
'Log1p' : AttrCvt('log1p'),
1348+
'Cos' : AttrCvt('cos'),
13471349
'LogicalAnd' : _logical('logical_and'),
13481350
'LogicalOr' : _logical('logical_or'),
13491351
'LogicalNot' : _logical('logical_not'),

python/tvm/relay/op/_tensor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
schedule_elemwise = schedule_injective
2626

2727
register_schedule("log", schedule_broadcast)
28+
register_schedule("log1p", schedule_broadcast)
29+
register_schedule("cos", schedule_broadcast)
2830
register_schedule("exp", schedule_broadcast)
2931
register_schedule("sqrt", schedule_broadcast)
3032
register_schedule("rsqrt", schedule_broadcast)

python/tvm/relay/op/_tensor_grad.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ def log_grad(orig, grad):
3131
x = orig.args[0]
3232
return [grad * ones_like(x) / x]
3333

34+
@register_gradient("log1p")
35+
def log1p_grad(orig, grad):
36+
"""Returns [grad * (1 / (x+1))]"""
37+
x = orig.args[0]
38+
return [grad * ones_like(x) / (x + ones_like(x))]
39+
40+
@register_gradient("cos")
41+
def cos_grad(orig, grad):
42+
"""Returns [grad * (-sin(x))]"""
43+
x = orig.args[0]
44+
ones = ones_like(x)
45+
return [grad * (-ones * sin(x))]
3446

3547
@register_gradient("exp")
3648
def exp_grad(orig, grad):

python/tvm/relay/op/tensor.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,35 @@ def log(data):
4646
"""
4747
return _make.log(data)
4848

49+
def log1p(data):
50+
"""Compute elementwise log of data.
51+
52+
Parameters
53+
----------
54+
data : relay.Expr
55+
The input data
56+
57+
Returns
58+
-------
59+
result : relay.Expr
60+
The computed result.
61+
"""
62+
return _make.log1p(data)
63+
64+
def cos(data):
65+
"""Compute elementwise cos of data.
66+
67+
Parameters
68+
----------
69+
data : relay.Expr
70+
The input data
71+
72+
Returns
73+
-------
74+
result : relay.Expr
75+
The computed result.
76+
"""
77+
return _make.cos(data)
4978

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

src/codegen/intrin_rule.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,23 @@ TVM_REGISTER_GLOBAL("tvm.intrin.rule.default.exp")
3434
TVM_REGISTER_GLOBAL("tvm.intrin.rule.default.log")
3535
.set_body(DispatchExtern<FloatSuffix>);
3636

37+
TVM_REGISTER_GLOBAL("tvm.intrin.rule.default.log1p")
38+
.set_body([](const TVMArgs& args, TVMRetValue* rv){
39+
Expr e = args[0];
40+
const Call* call = e.as<Call>();
41+
CHECK(call != nullptr);
42+
43+
auto one = make_const(call->args[0].type(), 1);
44+
*rv = log(call->args[0] + one);
45+
});
46+
47+
3748
TVM_REGISTER_GLOBAL("tvm.intrin.rule.default.tanh")
3849
.set_body(DispatchExtern<FloatSuffix>);
3950

51+
TVM_REGISTER_GLOBAL("tvm.intrin.rule.default.cos")
52+
.set_body(DispatchExtern<FloatSuffix>);
53+
4054
TVM_REGISTER_GLOBAL("tvm.intrin.rule.default.sqrt")
4155
.set_body(DispatchExtern<FloatSuffix>);
4256

src/codegen/intrin_rule_cuda.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ TVM_REGISTER_GLOBAL("tvm.intrin.rule.cuda.exp")
9595
TVM_REGISTER_GLOBAL("tvm.intrin.rule.cuda.log")
9696
.set_body(DispatchExtern<CUDAFastMath>);
9797

98+
TVM_REGISTER_GLOBAL("tvm.intrin.rule.cuda.cos")
99+
.set_body(DispatchExtern<CUDAFastMath>);
100+
98101
TVM_REGISTER_GLOBAL("tvm.intrin.rule.cuda.tanh")
99102
.set_body(DispatchExtern<CUDAMath>);
100103

src/relay/op/tensor/unary.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,28 @@ RELAY_REGISTER_UNARY_OP("log")
5353
.set_attr<FTVMCompute>("FTVMCompute", RELAY_UNARY_COMPUTE(topi::log));
5454

5555

56+
RELAY_REGISTER_UNARY_OP("log1p")
57+
.describe(R"code(Returns the log1p input array, computed element-wise.
58+
59+
.. math::
60+
log(x+1)
61+
62+
)code" TVM_ADD_FILELINE)
63+
.set_support_level(1)
64+
.set_attr<FTVMCompute>("FTVMCompute", RELAY_UNARY_COMPUTE(topi::log1p));
65+
66+
67+
RELAY_REGISTER_UNARY_OP("cos")
68+
.describe(R"code(Returns the cos of input array, computed element-wise.
69+
70+
.. math::
71+
Y = cos(X)
72+
73+
)code" TVM_ADD_FILELINE)
74+
.set_support_level(1)
75+
.set_attr<FTVMCompute>("FTVMCompute", RELAY_UNARY_COMPUTE(topi::cos));
76+
77+
5678
RELAY_REGISTER_UNARY_OP("exp")
5779
.describe(R"code(Returns the exp input array, computed element-wise.
5880

tests/python/frontend/tensorflow/test_forward.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,6 +1850,22 @@ def test_forward_log():
18501850
tf.log(in_data, name="log")
18511851
compare_tf_with_tvm([np_data], ['in_data:0'], 'log:0')
18521852

1853+
def test_forward_log1p():
1854+
"""test operator Log1p """
1855+
np_data = np.random.uniform(1, 100, size=(2, 3, 5)).astype(np.float32)
1856+
tf.reset_default_graph()
1857+
in_data = tf.placeholder(tf.float32, (2, 3, 5), name="in_data")
1858+
tf.log1p(in_data, name="log1p")
1859+
compare_tf_with_tvm([np_data], ['in_data:0'], 'log1p:0')
1860+
1861+
def test_forward_cos():
1862+
"""test operator cos """
1863+
np_data = np.random.uniform(1, 100, size=(2, 3, 5)).astype(np.float32)
1864+
tf.reset_default_graph()
1865+
in_data = tf.placeholder(tf.float32, (2, 3, 5), name="in_data")
1866+
tf.cos(in_data, name="cos")
1867+
compare_tf_with_tvm([np_data], ['in_data:0'], 'cos:0')
1868+
18531869
def test_forward_negative():
18541870
"""test tf operator Neg """
18551871
np_data = np.random.uniform(-100, 255, size=(224, 224, 3)).astype(np.float32)
@@ -2097,6 +2113,7 @@ def test_placeholder():
20972113
# ----
20982114
if __name__ == '__main__':
20992115

2116+
21002117
# Transforms
21012118
test_forward_transpose()
21022119
test_forward_reshape()
@@ -2140,6 +2157,8 @@ def test_placeholder():
21402157
test_forward_pow_exp()
21412158
test_forward_sign()
21422159
test_forward_log()
2160+
test_forward_log1p()
2161+
test_forward_cos()
21432162
test_forward_negative()
21442163
test_forward_divide()
21452164
test_forward_abs()

0 commit comments

Comments
 (0)