24 #ifndef TVM_TOPI_NN_LAYER_NORM_H_
25 #define TVM_TOPI_NN_LAYER_NORM_H_
53 const ffi::Array<int64_t>& axis,
double epsilon,
54 std::string name =
"T_layer_norm", std::string tag =
kInjective) {
55 const auto& data_type = data->dtype;
56 const auto& gamma_type = gamma.defined() ? gamma->dtype : data_type;
57 const auto& beta_type = beta.defined() ? beta->dtype : data_type;
58 TVM_FFI_ICHECK(data_type == gamma_type && data_type == beta_type)
59 <<
"layer_norm: data, gamma and beta must have the same type";
61 <<
"layer_norm: only support float32 and float16 for now";
66 auto ndim = data->shape.size();
67 TVM_FFI_ICHECK_NE(ndim, 0) <<
"Cannot reduce a 0 dim Tensor";
68 auto real_axis =
GetRealAxis(
static_cast<int>(ndim), axis);
74 auto make_eval_range = [&real_axis, &reduce_axes,
75 ndim](
const ffi::Array<PrimVar>& non_reduce_indices) {
76 ffi::Array<PrimExpr> eval_range;
80 for (
size_t i = 0; i < ndim; ++i) {
81 if (std::find(real_axis.begin(), real_axis.end(), i) != real_axis.end()) {
83 eval_range.push_back(reduce_axes[red_counter]);
86 eval_range.push_back(non_reduce_indices[arg_counter]);
95 [is_float16, &data, &reduce_axes, &make_eval_range,
96 f32_ty](
const ffi::Array<PrimVar>& indices) {
97 auto eval_range = make_eval_range(indices);
102 return sum(x, reduce_axes);
108 for (
int i : real_axis) {
109 reduce_extent *= data->shape[i];
113 [&temp_sum, &reduce_extent](
const ffi::Array<PrimVar>& indices) {
114 return temp_sum(indices) / reduce_extent;
120 [is_float16, &data, &reduce_axes, &make_eval_range, &temp_mean,
121 f32_ty](
const ffi::Array<PrimVar>& indices) {
122 auto eval_range = make_eval_range(indices);
127 PrimExpr diff = x - temp_mean(indices);
128 return sum(diff * diff, reduce_axes);
132 auto layer_norm_func = [&](
const ffi::Array<PrimVar>& indices) {
133 ffi::Array<PrimVar> reduce_indices, non_reduce_indices;
134 for (
int i = 0, n =
static_cast<int>(indices.size()); i < n; ++i) {
135 if (std::find(real_axis.begin(), real_axis.end(), i) != real_axis.end()) {
136 reduce_indices.push_back(indices[i]);
138 non_reduce_indices.push_back(indices[i]);
141 auto mean = temp_mean(non_reduce_indices);
142 auto var = temp_var_sum(non_reduce_indices) / reduce_extent;
148 if (beta.defined()) {
153 return te::compute(data->shape, layer_norm_func, name, tag);
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition: base_expr.h:354
Definition: base_expr.h:113
static PrimType Float(int bits, int lanes=1)
Construct a floating-point type with fixed lanes.
ExpectedType ty() const
Definition: base_expr.h:333
Tensor structure representing a possible input, or intermediate computation result.
Definition: tensor.h:100
Managed reference to CastNode.
Definition: expr.h:96
Tensor expression language DSL.
Definition: extracted_task.h:32
PrimVar var(std::string name_hint, PrimType t=PrimType::Int(32))
Construct a new Var expression.
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 layer_norm(const Tensor &data, const Tensor &gamma, const Tensor &beta, const ffi::Array< int64_t > &axis, double epsilon, std::string name="T_layer_norm", std::string tag=kInjective)
Layer normalization.
Definition: layer_norm.h:52
Tensor sum(const Tensor &data, const ffi::Optional< ffi::Array< int64_t >> &axis, bool keepdims=false, bool atleast1d=false)
Creates an operation that sums array elements over a given axis.
Definition: reduction.h:337
ffi::Array< PrimExpr > MakeReduceTargetShape(const std::vector< int > &real_axis, const Tensor &data, bool keepdims, bool atleast1d)
Calculate the target shape for a reduce op.
Definition: reduction.h:99
constexpr auto kInjective
Definition: tags.h:33
ffi::Array< IterVar > MakeReduceAxes(const std::vector< int > &real_axis, const Tensor &data)
Enumerate the axes for a reduce op.
Definition: reduction.h:89
tvm::PrimExpr multiply(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:227
constexpr auto kCommReduce
Definition: tags.h:34
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:233
tvm::PrimExpr add(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:199
std::vector< int > GetRealAxis(int ndim, const ffi::Optional< ffi::Array< int64_t >> &axis)
Convert a reduction axis which could be empty or have negative elements into a real axis with valid d...
Definition: reduction.h:65
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:40
Operation node can generate one or multiple Tensors.
Reduction op constructors.