24#ifndef TVM_TOPI_NN_RMS_NORM_H_
25#define TVM_TOPI_NN_RMS_NORM_H_
51 double epsilon, std::string name =
"T_rms_norm",
54 const auto& data_type = data->dtype;
55 const auto& weight_type = weight.defined() ? weight->dtype : data_type;
56 TVM_FFI_ICHECK(data_type == weight_type) <<
"rms_norm: data and weight must have the same type";
61 auto square =
multiply(data_fp32, data_fp32);
62 auto square_sum =
sum(square, axis,
false,
true);
64 auto ndim = data_fp32->shape.size();
65 TVM_FFI_ICHECK_NE(ndim, 0) <<
"Cannot reduce a 0 dim Tensor";
66 auto real_axis =
GetRealAxis(
static_cast<int>(ndim), axis);
67 auto reduce_extent = MakeConst(
PrimType(data_fp32->dtype), 1);
68 for (
int i : real_axis) {
69 reduce_extent *= data_fp32->shape[i];
71 auto rsqrt_func = [&](
const ffi::Array<PrimVar>& indices) {
72 ffi::Array<PrimVar> non_reduce_indices;
73 for (
int i = 0, n =
static_cast<int>(indices.size()); i < n; ++i) {
74 if (std::find(real_axis.begin(), real_axis.end(), i) == real_axis.end()) {
75 non_reduce_indices.push_back(indices[i]);
78 auto output =
tvm::prim::rsqrt(square_sum(non_reduce_indices) / reduce_extent +
79 MakeConst(
PrimType(data_type), epsilon));
82 auto rsqrt_shape = ffi::Array<PrimExpr>();
83 for (
int i = 0, n =
static_cast<int>(data_fp32->shape.size()); i < n; ++i) {
84 if (std::find(real_axis.begin(), real_axis.end(), i) == real_axis.end()) {
85 rsqrt_shape.push_back(data_fp32->shape[i]);
90 auto rms_norm_func = [&](
const ffi::Array<PrimVar>& indices) {
91 ffi::Array<PrimVar> reduce_indices, non_reduce_indices;
92 for (
int i = 0, n =
static_cast<int>(indices.size()); i < n; ++i) {
93 if (std::find(real_axis.begin(), real_axis.end(), i) != real_axis.end()) {
94 reduce_indices.push_back(indices[i]);
96 non_reduce_indices.push_back(indices[i]);
99 auto output =
rsqrt(non_reduce_indices) * data_fp32(indices) * weight_fp32(reduce_indices);
Definition base_expr.h:161
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
PrimExpr rsqrt(PrimExpr x, Location loc=UnknownLoc())
Definition op.h:1010
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...
Tensor rms_norm(const Tensor &data, const Tensor &weight, const ffi::Array< int64_t > &axis, double epsilon, std::string name="T_rms_norm", std::string tag=kInjective)
Root mean square normalization.
Definition rms_norm.h:50
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:64
constexpr auto kInjective
Definition tags.h:33
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:336
tvm::PrimExpr multiply(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition broadcast.h:227
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 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
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
Operation node can generate one or multiple Tensors.
Reduction op constructors.