tvm
local_response_norm.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_NN_LOCAL_RESPONSE_NORM_H_
25 #define TVM_TOPI_NN_LOCAL_RESPONSE_NORM_H_
26 
27 #include <tvm/te/operation.h>
28 #include <tvm/topi/tags.h>
29 
30 #include <string>
31 
32 namespace tvm {
33 namespace topi {
34 namespace nn {
35 
36 using namespace tvm::te;
37 
52 inline Tensor lrn(const Tensor& data, int size, int axis = 1, float alpha = 0.0001,
53  float beta = 0.75, float bias = 2, std::string name = "tensor",
54  std::string tag = kBroadcast) {
55  TVM_FFI_ICHECK_EQ(data->shape.size(), 4) << "LRN requires 4-D input";
56  TVM_FFI_ICHECK_EQ(size % 2, 1) << "size should be odd number";
57  TVM_FFI_ICHECK(axis == 1 || axis == 3) << "axis should be 1 or 3 for NCHW and NHWC";
58  // LRN only requires a floating-point element kind; lane encoding is irrelevant here.
59  TVM_FFI_ICHECK_EQ(data->dtype.code(), DLDataTypeCode::kDLFloat) << "datatype should be float";
60  auto input_shape = data->shape;
61  ffi::Array<PrimExpr> pad_before{0, 0, 0, 0};
62  ffi::Array<PrimExpr> pad_after{0, 0, 0, 0};
63  pad_before.Set(axis, static_cast<PrimExpr>(size / 2));
64  pad_after.Set(axis, static_cast<PrimExpr>(size / 2));
65  auto pad_data = pad(data, pad_before, pad_after, 0, "pad_data");
66  auto rxs = tvm::te::reduce_axis(Range(0, size), "rxs");
67  Tensor sqr_sum;
68  if (axis == 1) {
69  sqr_sum = tvm::te::compute(
70  input_shape,
71  [&](PrimVar i, PrimVar l, PrimVar j, PrimVar k) {
72  return tvm::sum(pad_data(i, l + rxs, j, k) * pad_data(i, l + rxs, j, k), {rxs});
73  },
74  "tensor", "sqr_sum");
75  } else if (axis == 3) {
76  sqr_sum = tvm::te::compute(
77  input_shape,
78  [&](PrimVar i, PrimVar l, PrimVar j, PrimVar k) {
79  return tvm::sum(pad_data(i, l, j, k + rxs) * pad_data(i, l, j, k + rxs), {rxs});
80  },
81  "tensor", "sqr_sum");
82  }
83  PrimExpr alpha_imm = tvm::te::MakeConst(PrimType(data->dtype), alpha);
84  PrimExpr beta_imm = tvm::te::MakeConst(PrimType(data->dtype), beta);
85  PrimExpr bias_imm = tvm::te::MakeConst(PrimType(data->dtype), bias);
86  auto sqrt_sum_up = tvm::te::compute(
87  input_shape,
88  [&](PrimVar i, PrimVar j, PrimVar k, PrimVar l) {
89  return tvm::pow(bias_imm + (div(alpha_imm * sqr_sum(i, j, k, l), size)), beta_imm);
90  },
91  "tensor", kElementWise);
92  return topi::divide(data, sqrt_sum_up);
93 }
94 } // namespace nn
95 } // namespace topi
96 } // namespace tvm
97 #endif // TVM_TOPI_NN_LOCAL_RESPONSE_NORM_H_
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition: base_expr.h:354
Definition: base_expr.h:113
Range container
Definition: expr.h:484
Tensor structure representing a possible input, or intermediate computation result.
Definition: tensor.h:100
Checked scalar view over a VarNode.
Definition: var.h:127
Tensor expression language DSL.
Definition: extracted_task.h:32
IterVar reduce_axis(Range dom, std::string name="rv")
Create a new IterVar for reduction operations.
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...
PrimExpr MakeConst(PrimType dtype, ValueType value, Span span=Span())
Make a const value with certain data type.
Definition: op.h:1012
Tensor lrn(const Tensor &data, int size, int axis=1, float alpha=0.0001, float beta=0.75, float bias=2, std::string name="tensor", std::string tag=kBroadcast)
Local response normalization inference operator.
Definition: local_response_norm.h:52
constexpr auto kElementWise
Definition: tags.h:32
constexpr auto kBroadcast
Definition: tags.h:36
tvm::PrimExpr divide(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:241
tvm::te::Tensor pad(const tvm::te::Tensor &t, const tvm::ffi::Array< tvm::PrimExpr > &pad_before, tvm::ffi::Array< tvm::PrimExpr > pad_after=tvm::ffi::Array< tvm::PrimExpr >(), PrimExpr pad_value=PrimExpr(), std::string name="T_pad", std::string tag=kElementWise, std::string pad_mode="constant", const ffi::Array< PrimExpr > *dyn_output_shape=nullptr)
Creates an operation that performs padding.
Definition: nn.h:156
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:40
PrimExpr div(PrimExpr a, PrimExpr b, Span span=Span())
compute division in C semantics.
PrimExpr pow(PrimExpr x, PrimExpr y, Span span=Span())
Calculate power(x, y)
PrimExpr sum(PrimExpr source, ffi::Array< tirx::IterVar > axis, ffi::Array< PrimExpr > init={}, Span span=Span())
sum of source expression over axis
Operation node can generate one or multiple Tensors.
Tag definitions.