24 #ifndef TVM_TOPI_ELEMWISE_H_ 25 #define TVM_TOPI_ELEMWISE_H_ 42 #define TOPI_DECLARE_UNARY_OP(OpName) \ 43 inline Tensor OpName(const Tensor& x, std::string name = "T_" #OpName, \ 44 std::string tag = kElementWise) { \ 46 x->shape, [&](const Array<Var>& i) { return ::tvm::OpName(x(i)); }, name, tag); \ 87 auto alpha_1 =
make_const(in->dtype, 4.89352455891786e-03);
88 auto alpha_3 =
make_const(in->dtype, 6.37261928875436e-04);
89 auto alpha_5 =
make_const(in->dtype, 1.48572235717979e-05);
90 auto alpha_7 =
make_const(in->dtype, 5.12229709037114e-08);
91 auto alpha_9 =
make_const(in->dtype, -8.60467152213735e-11);
92 auto alpha_11 =
make_const(in->dtype, 2.00018790482477e-13);
93 auto alpha_13 =
make_const(in->dtype, -2.76076847742355e-16);
96 auto beta_0 =
make_const(in->dtype, 4.89352518554385e-03);
97 auto beta_2 =
make_const(in->dtype, 2.26843463243900e-03);
98 auto beta_4 =
make_const(in->dtype, 1.18534705686654e-04);
99 auto beta_6 =
make_const(in->dtype, 1.19825839466702e-06);
104 auto x2 = x(i) * x(i);
105 auto p = x2 * alpha_13 + alpha_11;
106 p = x2 * p + alpha_9;
107 p = x2 * p + alpha_7;
108 p = x2 * p + alpha_5;
109 p = x2 * p + alpha_3;
110 p = x2 * p + alpha_1;
113 auto q = x2 * beta_6 + beta_4;
138 x->shape, [&](
const Array<Var>& i) { return ::tvm::tanh(x(i)); }, name, tag);
154 x->shape, [&](
const Array<Var>& i) { return x(i); }, name, tag);
169 x->shape, [&](
const Array<Var>& i) { return -x(i); }, name, tag);
184 x->shape, [&](
const Array<Var>& i) { return !x(i); }, name, tag);
199 x->shape, [&](
const Array<Var>& i) { return ~x(i); }, name, tag);
215 PrimExpr zero = make_zero(x->dtype);
216 PrimExpr one = make_const(x->dtype, 1);
217 PrimExpr minus_one = make_const(x->dtype, -1);
218 auto s1 = tvm::tir::Select((x(i) < zero), minus_one, zero);
219 auto s2 = tvm::tir::Select((x(i) > zero), one, s1);
238 PrimExpr one = make_const(x->dtype, 1);
239 return one / tvm::sqrt(x(i));
257 std::string name =
"T_clip", std::string tag =
kElementWise) {
261 auto min_val = tvm::cast(x->dtype, a_min);
262 auto max_val = tvm::cast(x->dtype, a_max);
263 return tvm::max(tvm::min(x(i), max_val), min_val);
286 if (expr.dtype().code() == type.code() && expr.dtype().bits() == type.bits()) {
287 if (expr.dtype().lanes() == type.lanes()) {
289 } else if (expr.dtype().lanes() == 1 && type.lanes() > 1) {
290 return tvm::tir::Broadcast(expr, type.lanes());
314 return tvm::tir::Call(type, tvm::tir::builtin::reinterpret(), {x(i)});
330 ICHECK_GT(xs.
size(), 0) <<
"elemwise sum must have at least one input tensor.";
334 auto sum_expr = xs[0](i);
335 for (
size_t j = 1; j < xs.
size(); j++) {
336 sum_expr = sum_expr + xs[j](i);
355 std::string name =
"T_full", std::string tag =
kElementWise) {
358 LOG(ERROR) <<
"Can't cast fill_value to " << dtype;
361 shape, [&](
const Array<Var>& i) {
return ev; }, name, tag);
376 std::string name =
"T_full_like", std::string tag =
kElementWise) {
379 x->shape, [&](
const Array<Var>& i) { return ev; }, name, tag);
422 auto x = ::tvm::max(::tvm::min(_x(i), x_hi), x_lo);
424 auto n = ::tvm::floor(x * log2e + one_half);
426 auto f = x - n * ln2;
428 (((((p[0] * f + p[1]) * f + p[2]) * f + p[3]) * f + p[4]) * f + p[5]) * f * f + f + one;
431 tvm::reinterpret(DataType::Float(32), ::tvm::cast(DataType::Int(32), n + b) << 23);
432 return ::tvm::max(ef * y, _x(i));
454 x->shape, [&](
const Array<Var>& i) { return ::tvm::exp(x(i)); }, name, tag);
489 auto p = x2 * alpha_13 + alpha_11;
490 p = x2 * p + alpha_9;
491 p = x2 * p + alpha_7;
492 p = x2 * p + alpha_5;
493 p = x2 * p + alpha_3;
494 p = x2 * p + alpha_1;
498 auto q = x2 * beta_8 + beta_6;
511 data->shape, [&](
const Array<Var>& i) { return fast_erf_float_expr(data(i), 32); }, name,
536 #endif // TVM_TOPI_ELEMWISE_H_ Tensor full_like(const Tensor &x, const PrimExpr fill_value, std::string name="T_full_like", std::string tag=kElementWise)
Creates an operation that construct a tensor with same shape as input tensor, then fill a tensor with...
Definition: elemwise.h:375
Tensor negative(const Tensor &x, std::string name="T_negative", std::string tag=kElementWise)
Creates an operation that returns the negation of a given tensor.
Definition: elemwise.h:166
PrimExpr min(PrimExpr a, PrimExpr b, Span span=Span())
take minimum of two values
tvm::PrimExpr minimum(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:316
Tensor abs(const Tensor &x, std::string name="T_" "abs", std::string tag=kElementWise)
Definition: elemwise.h:60
Tensor isinf(const Tensor &x, std::string name="T_" "isinf", std::string tag=kElementWise)
Definition: elemwise.h:75
PrimExpr make_const(DataType t, ValueType value, Span span=Span())
Make a const value with certain data type.
Definition: op.h:1109
Tensor full(const Array< PrimExpr > &shape, DataType dtype, const PrimExpr fill_value, std::string name="T_full", std::string tag=kElementWise)
Creates an operation that fill a tensor with fill_value.
Definition: elemwise.h:354
Tensor sqrt(const Tensor &x, std::string name="T_" "sqrt", std::string tag=kElementWise)
Definition: elemwise.h:52
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
Tensor expression language DSL.
Definition: autodiff.h:35
Tensor elemwise_sum(const Array< Tensor > &xs, std::string name="T_elemwise_sum", std::string tag=kElementWise)
Creates an operation that sum each element of a tensor.
Definition: elemwise.h:328
Tensor tan(const Tensor &x, std::string name="T_" "tan", std::string tag=kElementWise)
Definition: elemwise.h:63
Tensor fast_tanh_float(const Tensor &in, std::string name, std::string tag)
Fast_tanh_float implementation from Eigen https://github.com/eigenteam/eigen-git-mirror/blob/master/E...
Definition: elemwise.h:81
Tensor floor(const Tensor &x, std::string name="T_" "floor", std::string tag=kElementWise)
Definition: elemwise.h:56
Tensor erf(const Tensor &x, std::string name="T_" "erf", std::string tag=kElementWise)
Definition: elemwise.h:50
Tensor round(const Tensor &x, std::string name="T_" "round", std::string tag=kElementWise)
Definition: elemwise.h:58
Tensor atan(const Tensor &x, std::string name="T_" "atan", std::string tag=kElementWise)
Definition: elemwise.h:70
Tensor asinh(const Tensor &x, std::string name="T_" "asinh", std::string tag=kElementWise)
Definition: elemwise.h:69
Tensor clip(const Tensor &x, const PrimExpr &a_min, const PrimExpr &a_max, std::string name="T_clip", std::string tag=kElementWise)
Creates an operation that clips each element of a tensor to the interval [a_min, a_max].
Definition: elemwise.h:256
Tensor acosh(const Tensor &x, std::string name="T_" "acosh", std::string tag=kElementWise)
Definition: elemwise.h:67
Tensor fast_exp_float32(const Tensor &_x, std::string name, std::string tag)
Fast exponential function implementation.
Definition: elemwise.h:403
Tensor sin(const Tensor &x, std::string name="T_" "sin", std::string tag=kElementWise)
Definition: elemwise.h:64
Tensor log10(const Tensor &x, std::string name="T_" "log10", std::string tag=kElementWise)
Definition: elemwise.h:55
PrimExpr cast(const DataType &t, PrimExpr value, Span span=Span())
cast value to type.
Tensor cos(const Tensor &x, std::string name="T_" "cos", std::string tag=kElementWise)
Definition: elemwise.h:61
Tensor sign(const Tensor &x, std::string name="T_sign", std::string tag=kElementWise)
Returns the sign of the tensor.
Definition: elemwise.h:211
Tensor isnan(const Tensor &x, std::string name="T_" "isnan", std::string tag=kElementWise)
Definition: elemwise.h:72
Tensor ceil(const Tensor &x, std::string name="T_" "ceil", std::string tag=kElementWise)
Definition: elemwise.h:57
size_t size() const
Definition: array.h:399
bool defined() const
Definition: object.h:537
Runtime primitive data type.
Definition: data_type.h:41
Tensor rsqrt(const Tensor &x, std::string name="tensor", std::string tag=kElementWise)
Creates an operation that returns rsqrt of a given tensor.
Definition: elemwise.h:234
Tensor identity(const Tensor &x, std::string name="T_identity", std::string tag=kElementWise)
Creates an operation that returns identity of a given tensor.
Definition: elemwise.h:151
static DataType Float(int bits, int lanes=1)
Construct an float type.
Definition: data_type.h:168
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:270
constexpr auto kElementWise
Definition: tags.h:32
Tensor log(const Tensor &x, std::string name="T_" "log", std::string tag=kElementWise)
Definition: elemwise.h:53
Tensor fast_exp(const Tensor &x, std::string name="T_fast_exp", std::string tag=kElementWise)
Fast exponential function implementation.
Definition: elemwise.h:447
Tensor sinh(const Tensor &x, std::string name="T_" "sinh", std::string tag=kElementWise)
Definition: elemwise.h:65
Tensor bitwise_not(const Tensor &x, std::string name="T_bitwise_not", std::string tag=kElementWise)
Creates an operation that returns the bitwise NOT of a given tensor.
Definition: elemwise.h:196
PrimExpr max(PrimExpr a, PrimExpr b, Span span=Span())
take maximum of two values
Tensor tanh(const Tensor &x, std::string name="T_" "tanh", std::string tag=kElementWise)
Definition: elemwise.h:73
Tensor shape(const Tensor &src, DataType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition: transform.h:1608
Tensor fast_erf_float32(const Tensor &data, std::string name, std::string tag)
Fast_erf_float expression from Eigen.
Definition: elemwise.h:509
Tensor acos(const Tensor &x, std::string name="T_" "acos", std::string tag=kElementWise)
Definition: elemwise.h:66
Tensor reinterpret(const Tensor &x, DataType type, std::string name="tensor", std::string tag=kElementWise)
Reinterpret each element of x to the given type.
Definition: elemwise.h:309
Tensor log2(const Tensor &x, std::string name="T_" "log2", std::string tag=kElementWise)
Definition: elemwise.h:54
Tensor sigmoid(const Tensor &x, std::string name="T_" "sigmoid", std::string tag=kElementWise)
Definition: elemwise.h:51
Tensor structure representing a possible input, or intermediate computation result.
Definition: tensor.h:102
Tensor trunc(const Tensor &x, std::string name="T_" "trunc", std::string tag=kElementWise)
Definition: elemwise.h:59
Tensor exp(const Tensor &x, std::string name="T_" "exp", std::string tag=kElementWise)
Definition: elemwise.h:49
Tensor fast_erf(const Tensor &x, std::string name="T_fast_erf", std::string tag=kElementWise)
Fast erf implementation.
Definition: elemwise.h:524
Tensor logical_not(const Tensor &x, std::string name="T_logical_not", std::string tag=kElementWise)
Creates an operation that returns the logical NOT of a given tensor.
Definition: elemwise.h:181
Tensor cosh(const Tensor &x, std::string name="T_" "cosh", std::string tag=kElementWise)
Definition: elemwise.h:62
Tensor asin(const Tensor &x, std::string name="T_" "asin", std::string tag=kElementWise)
Definition: elemwise.h:68
#define TOPI_DECLARE_UNARY_OP(OpName)
Definition: elemwise.h:42
Tensor isfinite(const Tensor &x, std::string name="T_" "isfinite", std::string tag=kElementWise)
Definition: elemwise.h:74
tvm::PrimExpr maximum(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:303
Tensor fast_tanh(const Tensor &x, std::string name="T_fast_tanh", std::string tag=kElementWise)
Creates an operation that returns hyperbolic tanh of a given tensor.
Definition: elemwise.h:130
PrimExpr ret(PrimExpr value, Span span=Span())
Return the value.
Tensor compute(Array< PrimExpr > shape, FCompute fcompute, std::string name="tensor", std::string tag="", Map< String, ObjectRef > attrs={})
Construct a new tensor by computing over shape, using the computation rule: result_tensor[axis] = fco...
Tensor cast(const Tensor &x, DataType type, std::string name="T_cast", std::string tag=kElementWise)
Cast each element of x to the given type. If expr is scalar and type is a corresponding vector type...
Definition: elemwise.h:280
Broadcast op constructions.
Reference to PrimExprNode.
Definition: expr.h:109
Tensor atanh(const Tensor &x, std::string name="T_" "atanh", std::string tag=kElementWise)
Definition: elemwise.h:71
PrimExpr fast_erf_float_expr(PrimExpr arg, int bits)
Fast_erf_float expression from Eigen https://github.com/eigenteam/eigen-git-mirror/blob/master/unsupp...
Definition: elemwise.h:464