24#ifndef TVM_TOPI_ELEMWISE_H_
25#define TVM_TOPI_ELEMWISE_H_
43#define TOPI_DECLARE_UNARY_OP(OpName, ExprFunc) \
44 inline Tensor OpName(const Tensor& x, std::string name = "T_" #OpName, \
45 std::string tag = kElementWise) { \
47 x->shape, [&](const ffi::Array<PrimVar>& i) { return ExprFunc(x(i)); }, name, tag); \
86 PrimType input_type = in->GetDataType();
87 auto x =
maximum(MakeConst(input_type, -9.0),
minimum(MakeConst(input_type, 9.0), in));
90 auto alpha_1 = MakeConst(input_type, 4.89352455891786e-03);
91 auto alpha_3 = MakeConst(input_type, 6.37261928875436e-04);
92 auto alpha_5 = MakeConst(input_type, 1.48572235717979e-05);
93 auto alpha_7 = MakeConst(input_type, 5.12229709037114e-08);
94 auto alpha_9 = MakeConst(input_type, -8.60467152213735e-11);
95 auto alpha_11 = MakeConst(input_type, 2.00018790482477e-13);
96 auto alpha_13 = MakeConst(input_type, -2.76076847742355e-16);
99 auto beta_0 = MakeConst(input_type, 4.89352518554385e-03);
100 auto beta_2 = MakeConst(input_type, 2.26843463243900e-03);
101 auto beta_4 = MakeConst(input_type, 1.18534705686654e-04);
102 auto beta_6 = MakeConst(input_type, 1.19825839466702e-06);
106 [&](
const ffi::Array<PrimVar>& i) {
107 auto x2 = x(i) * x(i);
108 auto p = x2 * alpha_13 + alpha_11;
109 p = x2 * p + alpha_9;
110 p = x2 * p + alpha_7;
111 p = x2 * p + alpha_5;
112 p = x2 * p + alpha_3;
113 p = x2 * p + alpha_1;
116 auto q = x2 * beta_6 + beta_4;
135 if (x->GetDataType().MatchesElementType(DLDataTypeCode::kDLFloat, 32)) {
141 x->shape, [&](
const ffi::Array<PrimVar>& i) { return ::tvm::prim::tanh(x(i)); }, name, tag);
156 return compute(x->shape, [&](
const ffi::Array<PrimVar>& i) { return x(i); }, name, tag);
170 return compute(x->shape, [&](
const ffi::Array<PrimVar>& i) { return -x(i); }, name, tag);
184 return compute(x->shape, [&](
const ffi::Array<PrimVar>& i) { return !x(i); }, name, tag);
198 return compute(x->shape, [&](
const ffi::Array<PrimVar>& i) { return ~x(i); }, name, tag);
214 [&](
const ffi::Array<PrimVar>& i) {
215 PrimType x_type(x->GetDataType());
216 PrimExpr zero = MakeConst(x_type, 0);
217 PrimExpr one = MakeConst(x_type, 1);
218 PrimExpr minus_one = MakeConst(x_type, -1);
219 auto s1 = tvm::prim::Select((x(i) < zero), minus_one, zero);
220 auto s2 = tvm::prim::Select((x(i) > zero), one, s1);
239 [&](
const ffi::Array<PrimVar>& i) {
240 PrimExpr one = MakeConst(x->GetDataType(), 1);
241 return one / tvm::prim::sqrt(x(i));
259 std::string name =
"T_clip", std::string tag =
kElementWise) {
262 [&](
const ffi::Array<PrimVar>& i) {
263 PrimType x_type(x->GetDataType());
264 auto min_val = tvm::prim::cast(x_type, a_min);
265 auto max_val = tvm::prim::cast(x_type, a_max);
266 return tvm::max(tvm::min(x(i), max_val), min_val);
288 return cast(x,
PrimType(type), std::move(name), std::move(tag));
295 [&](
const ffi::Array<PrimVar>& i) ->
PrimExpr {
297 PrimType expr_ty = expr.ty();
298 if (expr_ty.MatchesElementType(type.code(), type.bits())) {
299 if (expr_ty.lanes() == type.lanes()) {
301 } else if (expr_ty.lanes() == 1 && type.IsFixedLengthVector()) {
302 return tvm::prim::Broadcast(expr, type.lanes());
324 std::string tag = kElementWise) {
330 std::string tag = kElementWise) {
332 x->shape, [&](
const ffi::Array<PrimVar>& i) { return tirx::reinterpret(type, x(i)); }, name,
346 std::string tag = kElementWise) {
347 TVM_FFI_ICHECK_GT(xs.size(), 0) <<
"elemwise sum must have at least one input tensor.";
350 [&](
const ffi::Array<PrimVar>& i) {
351 auto sum_expr = xs[0](i);
352 for (
size_t j = 1; j < xs.size(); j++) {
353 sum_expr = sum_expr + xs[j](i);
372 std::string name, std::string tag);
375 std::string name =
"T_full", std::string tag = kElementWise) {
376 return full(
shape,
PrimType(dtype), fill_value, std::move(name), std::move(tag));
380 std::string name =
"T_full", std::string tag = kElementWise) {
383 LOG(ERROR) <<
"Can't cast fill_value to " << dtype;
385 return compute(
shape, [&](
const ffi::Array<PrimVar>& i) {
return ev; }, name, tag);
400 std::string name =
"T_full_like", std::string tag = kElementWise) {
402 return compute(x->shape, [&](
const ffi::Array<PrimVar>& i) { return ev; }, name, tag);
428 auto x_hi =
FloatImm(f32_ty, 88.3762626647950f);
429 auto x_lo =
FloatImm(f32_ty, -88.3762626647949f);
430 auto log2e =
FloatImm(f32_ty, 1.44269504088896341f);
431 auto ln2 =
FloatImm(f32_ty, 0.6931471805599453f);
436 auto one_half =
FloatImm(f32_ty, 0.5f);
441 [&](
const ffi::Array<PrimVar>& i) {
443 auto x = ::tvm::max(::tvm::min(_x(i), x_hi), x_lo);
445 auto n = ::tvm::prim::floor(x * log2e + one_half);
447 auto f = x - n * ln2;
449 (((((p[0] * f + p[1]) * f + p[2]) * f + p[3]) * f + p[4]) * f + p[5]) * f * f + f + one;
451 auto ef = tvm::tirx::reinterpret(PrimType::Float(32),
452 ::tvm::prim::cast(PrimType::Int(32), n + b) << 23);
453 return ::tvm::max(ef * y, _x(i));
469 std::string tag = kElementWise) {
470 if (x->GetDataType().MatchesElementType(DLDataTypeCode::kDLFloat, 32)) {
475 x->shape, [&](
const ffi::Array<PrimVar>& i) { return ::tvm::prim::exp(x(i)); }, name, tag);
486 [&](
const ffi::Array<PrimVar>& i) { return tirx::fast_erf_float_expr(data(i), 32); }, name,
497 [&](
const ffi::Array<PrimVar>& i) { return tirx::fast_erf_float_expr(data(i), 16); }, name,
511 std::string tag = kElementWise) {
Managed reference class to FloatImmNode.
Definition expr.h:232
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:447
Definition base_expr.h:161
TVM_FFI_INLINE bool MatchesElementType(DLDataTypeCode code, int bits) const
Check the scalar element code and bit width.
Definition base_expr.h:222
static TVM_TIRX_DLL PrimType Float(int bits, int lanes=1)
Construct a floating-point type with fixed lanes.
Tensor structure representing a possible input, or intermediate computation result.
Definition tensor.h:111
#define TOPI_DECLARE_UNARY_OP(OpName, ExprFunc)
Definition elemwise.h:43
Shared primitive construction and constant helpers.
Specialized mathematical expression builders.
PrimExpr acos(PrimExpr x, Location loc=UnknownLoc())
Definition op.h:1021
PrimExpr sin(PrimExpr x, Location loc=UnknownLoc())
Definition op.h:1018
PrimExpr asin(PrimExpr x, Location loc=UnknownLoc())
Definition op.h:1020
PrimExpr cos(PrimExpr x, Location loc=UnknownLoc())
Definition op.h:1016
PrimExpr atanh(PrimExpr x, Location loc=UnknownLoc())
Definition op.h:1025
PrimExpr sinh(PrimExpr x, Location loc=UnknownLoc())
Definition op.h:1019
TVM_TIRX_DLL PrimExpr isfinite(PrimExpr x, Location loc=UnknownLoc())
Check if x is finite.
TVM_TIRX_DLL PrimExpr isnan(PrimExpr x, Location loc=UnknownLoc())
Check if x is NaN.
PrimExpr cosh(PrimExpr x, Location loc=UnknownLoc())
Definition op.h:1017
TVM_TIRX_DLL PrimExpr trunc(PrimExpr x, Location loc=UnknownLoc())
Calculate trunc(x)
TVM_TIRX_DLL PrimExpr isinf(PrimExpr x, Location loc=UnknownLoc())
Check if x is infinite.
TVM_TIRX_DLL PrimExpr floor(PrimExpr x, Location loc=UnknownLoc())
Calculate floor(x)
PrimExpr tan(PrimExpr x, Location loc=UnknownLoc())
Definition op.h:1015
PrimExpr asinh(PrimExpr x, Location loc=UnknownLoc())
Definition op.h:1024
PrimExpr erf(PrimExpr x, Location loc=UnknownLoc())
Definition op.h:1006
PrimExpr sqrt(PrimExpr x, Location loc=UnknownLoc())
Definition op.h:1009
PrimExpr log(PrimExpr x, Location loc=UnknownLoc())
Definition op.h:1011
PrimExpr exp(PrimExpr x, Location loc=UnknownLoc())
Definition op.h:1003
PrimExpr acosh(PrimExpr x, Location loc=UnknownLoc())
Definition op.h:1023
TVM_TIRX_DLL PrimExpr round(PrimExpr x, Location loc=UnknownLoc())
Round x to the nearest integer, ties to even.
TVM_TIRX_DLL PrimExpr abs(PrimExpr x, Location loc=UnknownLoc())
Calculate absolute value of x.
PrimExpr log10(PrimExpr x, Location loc=UnknownLoc())
Definition op.h:1012
TVM_TIRX_DLL PrimExpr cast(PrimType t, PrimExpr value, Location loc=UnknownLoc())
cast value to type.
PrimExpr sigmoid(PrimExpr x, Location loc=UnknownLoc())
Definition op.h:1008
PrimExpr atan(PrimExpr x, Location loc=UnknownLoc())
Definition op.h:1022
PrimExpr tanh(PrimExpr x, Location loc=UnknownLoc())
Definition op.h:1007
Tensor expression language DSL.
Definition extracted_task.h:33
Tensor compute(ffi::Array< PrimExpr > shape, FCompute fcompute, std::string name="tensor", std::string tag="", ffi::Map< ffi::String, ffi::Any > attrs={})
Construct a new tensor by computing over shape, using the computation rule: result_tensor[axis] = fco...
TVM_TIRX_DLL Expr reinterpret(Type target_ty, Expr value, Location loc=UnknownLoc())
perform reinterpret cast value to type.
constexpr auto kElementWise
Definition tags.h:32
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:258
Tensor ceil(const Tensor &x, std::string name="T_" "ceil", std::string tag=kElementWise)
Definition elemwise.h:58
Tensor isinf(const Tensor &x, std::string name="T_" "isinf", std::string tag=kElementWise)
Definition elemwise.h:76
Tensor shape(const Tensor &src, PrimType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition transform.h:2016
Tensor round(const Tensor &x, std::string name="T_" "round", std::string tag=kElementWise)
Definition elemwise.h:59
Tensor tan(const Tensor &x, std::string name="T_" "tan", std::string tag=kElementWise)
Definition elemwise.h:64
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:182
Tensor fast_erf_float32(const Tensor &data, std::string name, std::string tag)
Fast_erf_float expression from Eigen.
Definition elemwise.h:482
Tensor floor(const Tensor &x, std::string name="T_" "floor", std::string tag=kElementWise)
Definition elemwise.h:57
Tensor trunc(const Tensor &x, std::string name="T_" "trunc", std::string tag=kElementWise)
Definition elemwise.h:60
Tensor isnan(const Tensor &x, std::string name="T_" "isnan", std::string tag=kElementWise)
Definition elemwise.h:73
Tensor fast_erf_float16(const Tensor &data, std::string name, std::string tag)
Fast_erf_float expression from Eigen for float16.
Definition elemwise.h:493
Tensor exp(const Tensor &x, std::string name="T_" "exp", std::string tag=kElementWise)
Definition elemwise.h:50
Tensor acos(const Tensor &x, std::string name="T_" "acos", std::string tag=kElementWise)
Definition elemwise.h:67
Tensor asinh(const Tensor &x, std::string name="T_" "asinh", std::string tag=kElementWise)
Definition elemwise.h:70
Tensor log(const Tensor &x, std::string name="T_" "log", std::string tag=kElementWise)
Definition elemwise.h:54
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:82
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:133
Tensor fast_erf(const Tensor &x, std::string name="T_fast_erf", std::string tag=kElementWise)
Fast erf implementation.
Definition elemwise.h:510
Tensor cos(const Tensor &x, std::string name="T_" "cos", std::string tag=kElementWise)
Definition elemwise.h:62
tvm::PrimExpr minimum(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition broadcast.h:376
Tensor sqrt(const Tensor &x, std::string name="T_" "sqrt", std::string tag=kElementWise)
Definition elemwise.h:53
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:235
Tensor abs(const Tensor &x, std::string name="T_" "abs", std::string tag=kElementWise)
Definition elemwise.h:61
Tensor asin(const Tensor &x, std::string name="T_" "asin", std::string tag=kElementWise)
Definition elemwise.h:69
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
Tensor atan(const Tensor &x, std::string name="T_" "atan", std::string tag=kElementWise)
Definition elemwise.h:71
Tensor atanh(const Tensor &x, std::string name="T_" "atanh", std::string tag=kElementWise)
Definition elemwise.h:72
Tensor log10(const Tensor &x, std::string name="T_" "log10", std::string tag=kElementWise)
Definition elemwise.h:56
Tensor sigmoid(const Tensor &x, std::string name="T_" "sigmoid", std::string tag=kElementWise)
Definition elemwise.h:52
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:154
Tensor isfinite(const Tensor &x, std::string name="T_" "isfinite", std::string tag=kElementWise)
Definition elemwise.h:75
Tensor log2(const Tensor &x, std::string name="T_" "log2", std::string tag=kElementWise)
Definition elemwise.h:55
Tensor elemwise_sum(const ffi::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:345
Tensor cosh(const Tensor &x, std::string name="T_" "cosh", std::string tag=kElementWise)
Definition elemwise.h:63
Tensor acosh(const Tensor &x, std::string name="T_" "acosh", std::string tag=kElementWise)
Definition elemwise.h:68
Tensor full(const ffi::Array< PrimExpr > &shape, PrimType dtype, const PrimExpr fill_value, std::string name, std::string tag)
Creates an operation that fill a tensor with fill_value.
Definition elemwise.h:379
Tensor sin(const Tensor &x, std::string name="T_" "sin", std::string tag=kElementWise)
Definition elemwise.h:65
Tensor tanh(const Tensor &x, std::string name="T_" "tanh", std::string tag=kElementWise)
Definition elemwise.h:74
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:399
Tensor erf(const Tensor &x, std::string name="T_" "erf", std::string tag=kElementWise)
Definition elemwise.h:51
Tensor fast_exp(const Tensor &x, std::string name="T_fast_exp", std::string tag=kElementWise)
Fast exponential function implementation.
Definition elemwise.h:468
Tensor cast(const Tensor &x, PrimType type, std::string name, std::string tag)
Cast each element of x to the given type. If expr is scalar and type is a corresponding vector type,...
Definition elemwise.h:291
Tensor sign(const Tensor &x, std::string name="T_sign", std::string tag=kElementWise)
Returns the sign of the tensor.
Definition elemwise.h:210
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:168
Tensor sinh(const Tensor &x, std::string name="T_" "sinh", std::string tag=kElementWise)
Definition elemwise.h:66
tvm::PrimExpr maximum(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition broadcast.h:363
Tensor fast_exp_float32(const Tensor &_x, std::string name, std::string tag)
Fast exponential function implementation.
Definition elemwise.h:426
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
TVM_TIRX_DLL PrimExpr log2(PrimExpr x, Location loc=UnknownLoc())
Construct a base-two logarithm with the input's primitive type.
TVM_TIRX_DLL PrimExpr ceil(PrimExpr x, Location loc=UnknownLoc())
Round up to the nearest integral value, preserving the input type.
Memory operations for TIRx.