|
| 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 | + |
| 20 | +/*! |
| 21 | + * \brief root mean square normalization op constructions |
| 22 | + * \file nn/rms_norm.h |
| 23 | + */ |
| 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 | + |
| 33 | +namespace tvm { |
| 34 | +namespace topi { |
| 35 | +namespace nn { |
| 36 | + |
| 37 | +using namespace tvm::te; |
| 38 | + |
| 39 | +/*! |
| 40 | + * \brief Root mean square normalization. |
| 41 | + * \param data N-D tensor with shape [d_0, d_1, ..., d_{N-1}] |
| 42 | + * \param weight K-D tensor with shape [r_0, r_1, ..., r_{K-1}] where K == len(axis) and |
| 43 | + * d_{axis_k} == r_k |
| 44 | + * \param bias Optional, K-D tensor with shape [r_0, r_1, ..., r_{K-1}] where |
| 45 | + * d_{axis_k} == r_k |
| 46 | + * \param axis The axis to normalize over. |
| 47 | + * \param epsilon The epsilon value to avoid division by zero. |
| 48 | + * \param name The name of the operation. |
| 49 | + * \param tag The tag to mark the operation. |
| 50 | + * \return The normalized tensor, with the same shape as data. |
| 51 | + */ |
| 52 | +inline Tensor rms_norm(const Tensor& data, const Tensor& weight, const Tensor& bias, |
| 53 | + const Array<Integer>& axis, double epsilon, std::string name = "T_rms_norm", |
| 54 | + std::string tag = kInjective) { |
| 55 | + const auto& data_type = data->dtype; |
| 56 | + const auto& weight_type = weight.defined() ? weight->dtype : data_type; |
| 57 | + ICHECK(data_type == weight_type) << "rms_norm: data and weight must have the same type"; |
| 58 | + const auto& bias_type = bias.defined() ? bias->dtype : data_type; |
| 59 | + ICHECK(data_type == bias_type) << "rms_norm: data and bias must have the same type"; |
| 60 | + |
| 61 | + auto square = multiply(data, data); |
| 62 | + auto square_sum = sum(square, axis, /*keepdims=*/false, /*atleast1d=*/true); |
| 63 | + |
| 64 | + auto ndim = data->shape.size(); |
| 65 | + ICHECK_NE(ndim, 0) << "Cannot reduce a 0 dim Tensor"; |
| 66 | + auto real_axis = GetRealAxis(static_cast<int>(ndim), axis); |
| 67 | + auto reduce_extent = make_const(data->dtype, 1); |
| 68 | + for (int i : real_axis) { |
| 69 | + reduce_extent *= data->shape[i]; |
| 70 | + } |
| 71 | + auto rms_norm_func = [&](const Array<Var>& indices) { |
| 72 | + Array<Var> reduce_indices, 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 | + reduce_indices.push_back(indices[i]); |
| 76 | + } else { |
| 77 | + non_reduce_indices.push_back(indices[i]); |
| 78 | + } |
| 79 | + } |
| 80 | + auto output = |
| 81 | + data(indices) * weight(reduce_indices) * |
| 82 | + tvm::rsqrt(square_sum(non_reduce_indices) / reduce_extent + make_const(data_type, epsilon)); |
| 83 | + if (bias.defined()) { |
| 84 | + output += bias(reduce_indices); |
| 85 | + } |
| 86 | + return output; |
| 87 | + }; |
| 88 | + auto rms_norm = tvm::te::compute(data->shape, rms_norm_func, name, tag); |
| 89 | + return rms_norm; |
| 90 | +} |
| 91 | + |
| 92 | +} // namespace nn |
| 93 | +} // namespace topi |
| 94 | +} // namespace tvm |
| 95 | + |
| 96 | +#endif // TVM_TOPI_NN_RMS_NORM_H_ |
0 commit comments