tvm
Loading...
Searching...
No Matches
rms_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_RMS_NORM_H_
25#define TVM_TOPI_NN_RMS_NORM_H_
26
27#include <tvm/te/operation.h>
28#include <tvm/topi/reduction.h>
29#include <tvm/topi/tags.h>
30
31#include <string>
32
33namespace tvm {
34namespace topi {
35namespace nn {
36
37using namespace tvm::te;
38
50inline Tensor rms_norm(const Tensor& data, const Tensor& weight, const ffi::Array<int64_t>& axis,
51 double epsilon, std::string name = "T_rms_norm",
52 std::string tag = kInjective) {
53 const auto& data_type = data->dtype;
54 const auto& weight_type = weight.defined() ? weight->dtype : data_type;
55 TVM_FFI_ICHECK(data_type == weight_type) << "rms_norm: data and weight must have the same type";
56
57 const auto& data_fp32 = cast(data, PrimType::Float(32));
58 const auto& weight_fp32 = cast(weight, PrimType::Float(32));
59
61 auto square_sum = sum(square, axis, /*keepdims=*/false, /*atleast1d=*/true);
62
63 auto ndim = data_fp32->shape.size();
64 TVM_FFI_ICHECK_NE(ndim, 0) << "Cannot reduce a 0 dim Tensor";
65 auto real_axis = GetRealAxis(static_cast<int>(ndim), axis);
66 auto reduce_extent = MakeConst(PrimType(data_fp32->dtype), 1);
67 for (int i : real_axis) {
68 reduce_extent *= data_fp32->shape[i];
69 }
70 auto rsqrt_func = [&](const ffi::Array<PrimVar>& indices) {
71 ffi::Array<PrimVar> non_reduce_indices;
72 for (int i = 0, n = static_cast<int>(indices.size()); i < n; ++i) {
73 if (std::find(real_axis.begin(), real_axis.end(), i) == real_axis.end()) {
74 non_reduce_indices.push_back(indices[i]);
75 }
76 }
78 MakeConst(PrimType(data_type), epsilon));
79 return output;
80 };
81 auto rsqrt_shape = ffi::Array<PrimExpr>();
82 for (int i = 0, n = static_cast<int>(data_fp32->shape.size()); i < n; ++i) {
83 if (std::find(real_axis.begin(), real_axis.end(), i) == real_axis.end()) {
84 rsqrt_shape.push_back(data_fp32->shape[i]);
85 }
86 }
87 auto rsqrt = tvm::te::compute(rsqrt_shape, rsqrt_func, "rsqrt", tag);
88
89 auto rms_norm_func = [&](const ffi::Array<PrimVar>& indices) {
90 ffi::Array<PrimVar> reduce_indices, non_reduce_indices;
91 for (int i = 0, n = static_cast<int>(indices.size()); i < n; ++i) {
92 if (std::find(real_axis.begin(), real_axis.end(), i) != real_axis.end()) {
93 reduce_indices.push_back(indices[i]);
94 } else {
95 non_reduce_indices.push_back(indices[i]);
96 }
97 }
98 auto output = rsqrt(non_reduce_indices) * data_fp32(indices) * weight_fp32(reduce_indices);
99 return output;
100 };
101 auto rms_norm = tvm::te::compute(data_fp32->shape, rms_norm_func, name, tag);
102
103 return cast(rms_norm, data_type);
104}
105
106} // namespace nn
107} // namespace topi
108} // namespace tvm
109
110#endif // TVM_TOPI_NN_RMS_NORM_H_
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
Tensor structure representing a possible input, or intermediate computation result.
Definition tensor.h:98
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 & cast()
See pesudo code below:
PrimExpr MakeConst(PrimType dtype, ValueType value, Span span=Span())
Make a const value with certain data type.
Definition op.h:1002
const Op & sum()
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:65
constexpr auto kInjective
Definition tags.h:33
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:234
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
PrimExpr rsqrt(PrimExpr x, Span span=Span())
Definition op.h:759
Operation node can generate one or multiple Tensors.
Reduction op constructors.
Tag definitions.