tvm
Loading...
Searching...
No Matches
elemwise.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
24#ifndef TVM_TOPI_ELEMWISE_H_
25#define TVM_TOPI_ELEMWISE_H_
26
27#include <tvm/ir/prim/builtin.h>
28#include <tvm/ir/prim/expr.h>
29#include <tvm/tirx/builtin.h>
30#include <tvm/tirx/op.h>
31#include <tvm/topi/tags.h>
32
33#include <algorithm>
34#include <string>
35
36#include "broadcast.h"
37
38namespace tvm {
39namespace topi {
40
41using namespace tvm::te;
42
43// Unary intrinsic operators
44#define TOPI_DECLARE_UNARY_OP(OpName) \
45 inline Tensor OpName(const Tensor& x, std::string name = "T_" #OpName, \
46 std::string tag = kElementWise) { \
47 return compute( \
48 x->shape, [&](const ffi::Array<PrimVar>& i) { return ::tvm::OpName(x(i)); }, name, tag); \
49 }
50
78
83inline Tensor fast_tanh_float(const Tensor& in, std::string name, std::string tag) {
84 // Clamp the inputs to the range [-9, 9] since anything outside
85 // this range is +/-1.0f in single-precision.
86 PrimType input_type = in->GetDataType();
88
89 // The monomial coefficients of the numerator polynomial (odd).
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);
97
98 // The monomial coefficients of the denominator polynomial (even).
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);
103
104 return compute(
105 x->shape,
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;
114 p = x(i) * p;
115
116 auto q = x2 * beta_6 + beta_4;
117 q = x2 * q + beta_2;
118 q = x2 * q + beta_0;
119 return p / q;
120 },
121 name, tag);
122}
123
133inline Tensor fast_tanh(const Tensor& x, std::string name = "T_fast_tanh",
134 std::string tag = kElementWise) {
135 if (x->GetDataType().MatchesElementType(DLDataTypeCode::kDLFloat, 32)) {
136 // invoke fast_tanh_float implementation
137 return fast_tanh_float(x, name, tag);
138 } else {
139 // fallback to default implementation
140 return compute(
141 x->shape, [&](const ffi::Array<PrimVar>& i) { return ::tvm::tanh(x(i)); }, name, tag);
142 }
143}
144
154inline Tensor identity(const Tensor& x, std::string name = "T_identity",
155 std::string tag = kElementWise) {
156 return compute(x->shape, [&](const ffi::Array<PrimVar>& i) { return x(i); }, name, tag);
157}
158
168inline Tensor negative(const Tensor& x, std::string name = "T_negative",
169 std::string tag = kElementWise) {
170 return compute(x->shape, [&](const ffi::Array<PrimVar>& i) { return -x(i); }, name, tag);
171}
172
182inline Tensor logical_not(const Tensor& x, std::string name = "T_logical_not",
183 std::string tag = kElementWise) {
184 return compute(x->shape, [&](const ffi::Array<PrimVar>& i) { return !x(i); }, name, tag);
185}
186
196inline Tensor bitwise_not(const Tensor& x, std::string name = "T_bitwise_not",
197 std::string tag = kElementWise) {
198 return compute(x->shape, [&](const ffi::Array<PrimVar>& i) { return ~x(i); }, name, tag);
199}
200
210inline Tensor sign(const Tensor& x, std::string name = "T_sign", std::string tag = kElementWise) {
211 return compute(
212 x->shape,
213 [&](const ffi::Array<PrimVar>& i) {
214 PrimType x_type(x->GetDataType());
215 PrimExpr zero = MakeConst(x_type, 0);
216 PrimExpr one = MakeConst(x_type, 1);
217 PrimExpr minus_one = MakeConst(x_type, -1);
218 auto s1 = tvm::prim::Select((x(i) < zero), minus_one, zero);
219 auto s2 = tvm::prim::Select((x(i) > zero), one, s1);
220 return s2;
221 },
222 name, tag);
223}
224
234inline Tensor rsqrt(const Tensor& x, std::string name = "tensor", std::string tag = kElementWise) {
235 return compute(
236 x->shape,
237 [&](const ffi::Array<PrimVar>& i) {
238 PrimExpr one = MakeConst(x->GetDataType(), 1);
239 return one / tvm::sqrt(x(i));
240 },
241 name, tag);
242}
243
256inline Tensor clip(const Tensor& x, const PrimExpr& a_min, const PrimExpr& a_max,
257 std::string name = "T_clip", std::string tag = kElementWise) {
258 return compute(
259 x->shape,
260 [&](const ffi::Array<PrimVar>& i) {
261 PrimType x_type(x->GetDataType());
262 auto min_val = tvm::cast(x_type, a_min);
263 auto max_val = tvm::cast(x_type, a_max);
264 return tvm::max(tvm::min(x(i), max_val), min_val); // NOLINT(*)
265 },
266 name, tag);
267}
268
281inline Tensor cast(const Tensor& x, PrimType type, std::string name, std::string tag);
282
283inline Tensor cast(const Tensor& x, DLDataType type, std::string name = "T_cast",
284 std::string tag = kElementWise) {
285 return cast(x, PrimType(type), std::move(name), std::move(tag));
286}
287
288inline Tensor cast(const Tensor& x, PrimType type, std::string name = "T_cast",
289 std::string tag = kElementWise) {
290 return compute(
291 x->shape,
292 [&](const ffi::Array<PrimVar>& i) -> PrimExpr {
293 auto expr = x(i);
294 PrimType expr_ty = expr.ty();
295 if (expr_ty.MatchesElementType(type.code(), type.bits())) {
296 if (expr_ty.lanes() == type.lanes()) {
297 return expr;
298 } else if (expr_ty.lanes() == 1 && type.IsFixedLengthVector()) {
299 return tvm::prim::Broadcast(expr, type.lanes());
300 }
301 }
302
303 return tvm::cast(type, x(i));
304 },
305 name, tag);
306}
307
318inline Tensor reinterpret(const Tensor& x, PrimType type, std::string name, std::string tag);
319
320inline Tensor reinterpret(const Tensor& x, DLDataType type, std::string name = "tensor",
321 std::string tag = kElementWise) {
322 return reinterpret(x, PrimType(type), std::move(name), std::move(tag));
323}
324
325inline Tensor reinterpret(const Tensor& x, PrimType type, std::string name = "tensor",
326 std::string tag = kElementWise) {
327 return compute(
328 x->shape, [&](const ffi::Array<PrimVar>& i) { return reinterpret(type, x(i)); }, name, tag);
329}
330
340inline Tensor elemwise_sum(const ffi::Array<Tensor>& xs, std::string name = "T_elemwise_sum",
341 std::string tag = kElementWise) {
342 TVM_FFI_ICHECK_GT(xs.size(), 0) << "elemwise sum must have at least one input tensor.";
343 return compute(
344 xs[0]->shape,
345 [&](const ffi::Array<PrimVar>& i) {
346 auto sum_expr = xs[0](i);
347 for (size_t j = 1; j < xs.size(); j++) {
348 sum_expr = sum_expr + xs[j](i);
349 }
350 return sum_expr;
351 },
352 name, tag);
353}
354
366inline Tensor full(const ffi::Array<PrimExpr>& shape, PrimType dtype, const PrimExpr fill_value,
367 std::string name, std::string tag);
368
369inline Tensor full(const ffi::Array<PrimExpr>& shape, DLDataType dtype, const PrimExpr fill_value,
370 std::string name = "T_full", std::string tag = kElementWise) {
371 return full(shape, PrimType(dtype), fill_value, std::move(name), std::move(tag));
372}
373
374inline Tensor full(const ffi::Array<PrimExpr>& shape, PrimType dtype, const PrimExpr fill_value,
375 std::string name = "T_full", std::string tag = kElementWise) {
376 PrimExpr ev = cast(dtype, fill_value);
377 if (!ev.defined()) {
378 LOG(ERROR) << "Can't cast fill_value to " << dtype;
379 }
380 return compute(shape, [&](const ffi::Array<PrimVar>& i) { return ev; }, name, tag);
381}
382
395 std::string name = "T_full_like", std::string tag = kElementWise) {
396 PrimExpr ev = cast(x->GetDataType(), fill_value);
397 return compute(x->shape, [&](const ffi::Array<PrimVar>& i) { return ev; }, name, tag);
398}
399
421inline Tensor fast_exp_float32(const Tensor& _x, std::string name, std::string tag) {
423 auto x_hi = FloatImm(f32_ty, 88.3762626647950f);
424 auto x_lo = FloatImm(f32_ty, -88.3762626647949f);
425 auto log2e = FloatImm(f32_ty, 1.44269504088896341f);
426 auto ln2 = FloatImm(f32_ty, 0.6931471805599453f);
427 PrimExpr p[6] = {FloatImm(f32_ty, 1.9875691500E-4f), FloatImm(f32_ty, 1.3981999507E-3f),
428 FloatImm(f32_ty, 8.3334519073E-3f), FloatImm(f32_ty, 4.1665795894E-2f),
429 FloatImm(f32_ty, 1.6666665459E-1f), FloatImm(f32_ty, 5.0000001201E-1f)};
430 auto one = FloatImm(f32_ty, 1.0f);
431 auto one_half = FloatImm(f32_ty, 0.5f);
432 auto b = FloatImm(f32_ty, 127.0f);
433
434 return compute(
435 _x->shape,
436 [&](const ffi::Array<PrimVar>& i) {
437 // clamp x
438 auto x = ::tvm::max(::tvm::min(_x(i), x_hi), x_lo);
439 // integer part
440 auto n = ::tvm::floor(x * log2e + one_half);
441 // fractional part
442 auto f = x - n * ln2;
443 auto y =
444 (((((p[0] * f + p[1]) * f + p[2]) * f + p[3]) * f + p[4]) * f + p[5]) * f * f + f + one;
445 // Return 2^m * exp(r).
446 auto ef =
447 tvm::reinterpret(PrimType::Float(32), ::tvm::cast(PrimType::Int(32), n + b) << 23);
448 return ::tvm::max(ef * y, _x(i)); // NOLINT(*)
449 },
450 name, tag);
451}
452
463inline Tensor fast_exp(const Tensor& x, std::string name = "T_fast_exp",
464 std::string tag = kElementWise) {
465 if (x->GetDataType().MatchesElementType(DLDataTypeCode::kDLFloat, 32)) {
466 auto ret = fast_exp_float32(x, name, tag);
467 return ret;
468 } else {
469 return compute(
470 x->shape, [&](const ffi::Array<PrimVar>& i) { return ::tvm::exp(x(i)); }, name, tag);
471 }
472}
473
477inline Tensor fast_erf_float32(const Tensor& data, std::string name, std::string tag) {
478 return compute(
479 data->shape, [&](const ffi::Array<PrimVar>& i) { return fast_erf_float_expr(data(i), 32); },
480 name, tag);
481}
482
486inline Tensor fast_erf_float16(const Tensor& data, std::string name, std::string tag) {
487 return compute(
488 data->shape, [&](const ffi::Array<PrimVar>& i) { return fast_erf_float_expr(data(i), 16); },
489 name, tag);
490}
491
501inline Tensor fast_erf(const Tensor& x, std::string name = "T_fast_erf",
502 std::string tag = kElementWise) {
503 PrimType x_type(x->GetDataType());
504 if (x_type.MatchesElementType(DLDataTypeCode::kDLFloat, 32)) {
505 auto ret = fast_erf_float32(x, name, tag);
506 return ret;
507 } else if (x_type.MatchesElementType(DLDataTypeCode::kDLFloat, 16)) {
508 auto ret = fast_erf_float16(x, name, tag);
509 return ret;
510 } else {
511 return topi::erf(x);
512 }
513}
514
515} // namespace topi
516} // namespace tvm
517#endif // TVM_TOPI_ELEMWISE_H_
Managed reference class to FloatImmNode.
Definition expr.h:567
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:401
Definition base_expr.h:137
static PrimType Float(int bits, int lanes=1)
Construct a floating-point type with fixed lanes.
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
Managed Tensor. The array is backed by reference counted blocks.
Definition tensor.h:49
Tensor structure representing a possible input, or intermediate computation result.
Definition tensor.h:98
Detail broadcast.
#define TOPI_DECLARE_UNARY_OP(OpName)
Definition elemwise.h:44
TIR expressions.
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...
const Op & sqrt()
const Op & minimum()
const Op & maximum()
const Op & cast()
See pesudo code below:
const Op & exp()
PrimExpr MakeConst(PrimType dtype, ValueType value, Span span=Span())
Make a const value with certain data type.
Definition op.h:1002
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:256
Tensor ceil(const Tensor &x, std::string name="T_" "ceil", std::string tag=kElementWise)
Definition elemwise.h:59
Tensor isinf(const Tensor &x, std::string name="T_" "isinf", std::string tag=kElementWise)
Definition elemwise.h:77
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:2009
Tensor round(const Tensor &x, std::string name="T_" "round", std::string tag=kElementWise)
Definition elemwise.h:60
Tensor tan(const Tensor &x, std::string name="T_" "tan", std::string tag=kElementWise)
Definition elemwise.h:65
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:477
Tensor floor(const Tensor &x, std::string name="T_" "floor", std::string tag=kElementWise)
Definition elemwise.h:58
Tensor trunc(const Tensor &x, std::string name="T_" "trunc", std::string tag=kElementWise)
Definition elemwise.h:61
Tensor isnan(const Tensor &x, std::string name="T_" "isnan", std::string tag=kElementWise)
Definition elemwise.h:74
Tensor fast_erf_float16(const Tensor &data, std::string name, std::string tag)
Fast_erf_float expression from Eigen for float16.
Definition elemwise.h:486
Tensor acos(const Tensor &x, std::string name="T_" "acos", std::string tag=kElementWise)
Definition elemwise.h:68
Tensor asinh(const Tensor &x, std::string name="T_" "asinh", std::string tag=kElementWise)
Definition elemwise.h:71
Tensor log(const Tensor &x, std::string name="T_" "log", std::string tag=kElementWise)
Definition elemwise.h:55
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:83
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:501
Tensor cos(const Tensor &x, std::string name="T_" "cos", std::string tag=kElementWise)
Definition elemwise.h:63
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 abs(const Tensor &x, std::string name="T_" "abs", 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:70
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:72
Tensor atanh(const Tensor &x, std::string name="T_" "atanh", std::string tag=kElementWise)
Definition elemwise.h:73
Tensor log10(const Tensor &x, std::string name="T_" "log10", std::string tag=kElementWise)
Definition elemwise.h:57
Tensor sigmoid(const Tensor &x, std::string name="T_" "sigmoid", std::string tag=kElementWise)
Definition elemwise.h:53
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:76
Tensor log2(const Tensor &x, std::string name="T_" "log2", std::string tag=kElementWise)
Definition elemwise.h:56
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:340
Tensor cosh(const Tensor &x, std::string name="T_" "cosh", std::string tag=kElementWise)
Definition elemwise.h:64
Tensor acosh(const Tensor &x, std::string name="T_" "acosh", std::string tag=kElementWise)
Definition elemwise.h:69
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:374
Tensor sin(const Tensor &x, std::string name="T_" "sin", std::string tag=kElementWise)
Definition elemwise.h:66
Tensor tanh(const Tensor &x, std::string name="T_" "tanh", std::string tag=kElementWise)
Definition elemwise.h:75
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:394
Tensor erf(const Tensor &x, std::string name="T_" "erf", std::string tag=kElementWise)
Definition elemwise.h:52
Tensor fast_exp(const Tensor &x, std::string name="T_fast_exp", std::string tag=kElementWise)
Fast exponential function implementation.
Definition elemwise.h:463
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:67
Tensor fast_exp_float32(const Tensor &_x, std::string name, std::string tag)
Fast exponential function implementation.
Definition elemwise.h:421
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
PrimExpr cast(PrimType t, PrimExpr value, Span span=Span())
cast value to type.
PrimExpr reinterpret(PrimType t, PrimExpr value, Span span=Span())
perform reinterpret cast value to type.
Tag definitions.
TIR builtin intrinsics.
Common operators defined for Expr.