tvm
group_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_GROUP_NORM_H_
25 #define TVM_TOPI_NN_GROUP_NORM_H_
26 
27 #include <tvm/te/operation.h>
28 
29 #include <algorithm>
30 #include <string>
31 #include <vector>
32 
33 namespace tvm {
34 namespace topi {
35 namespace nn {
36 
37 using namespace tvm::te;
38 
39 inline Tensor group_norm(const Tensor& data, const Tensor& gamma, const Tensor& beta,
40  int num_groups, int channel_axis, const Array<Integer>& axes,
41  double epsilon, std::string name = "T_group_norm",
42  std::string tag = kInjective) {
43  const auto& data_type = data->dtype;
44  const auto& gamma_type = gamma.defined() ? gamma->dtype : data_type;
45  const auto& beta_type = beta.defined() ? beta->dtype : data_type;
46  ICHECK(data_type == gamma_type && data_type == beta_type)
47  << "group_norm: data, gamma and beta must have the same type";
48  ICHECK(data_type == DataType::Float(32) || data_type == DataType::Float(16))
49  << "group_norm: only support float32 and float16 for now";
50  bool is_float16 = data_type == DataType::Float(16);
51  // reshape data C -> G, C/G
52  int ndim = data->shape.size();
53  channel_axis = GetRealAxis(static_cast<int>(ndim), {channel_axis})[0];
54 
55  auto shape = data->shape;
56  auto group_size = floordiv(shape[channel_axis], num_groups);
57  auto new_shape = Array<PrimExpr>();
58  for (int i = 0; i < ndim; ++i) {
59  if (i == channel_axis) {
60  new_shape.push_back(num_groups);
61  new_shape.push_back(group_size);
62  } else {
63  new_shape.push_back(shape[i]);
64  }
65  }
66  Tensor data_reshaped;
67  if (is_float16) {
68  data_reshaped = cast(reshape(data, new_shape), DataType::Float(32));
69  } else {
70  data_reshaped = reshape(data, new_shape);
71  }
72  // reshape gamma and beta, C -> G, C/G, cast to float32 if float16
73  Tensor gamma_reshaped;
74  if (gamma.defined()) {
75  gamma_reshaped = reshape(gamma, {num_groups, group_size});
76  }
77  Tensor beta_reshaped;
78  if (beta.defined()) {
79  beta_reshaped = reshape(beta, {num_groups, group_size});
80  }
81 
82  // get the new axes to normalize after reshape
83  std::vector<int> new_axes{channel_axis + 1};
84  for (auto axis : axes) {
85  int new_axis = GetRealAxis(static_cast<int>(ndim), {axis})[0];
86  if (new_axis < channel_axis) {
87  new_axes.push_back(new_axis);
88  } else if (new_axis > channel_axis) {
89  new_axes.push_back(new_axis + 1);
90  } else {
91  ICHECK(false) << "axes can not contain channel axis";
92  }
93  }
94  std::sort(new_axes.begin(), new_axes.end());
95 
96  // sum x and x^2, cast to float32 if float16
97  ndim = data_reshaped->shape.size();
98  auto reduce_axes = MakeReduceAxes(new_axes, data_reshaped);
99  auto target_shape =
100  MakeReduceTargetShape(new_axes, data_reshaped, /*keepdims=*/false, /*atleast1d=*/true);
101  auto func = MakeTupleSumReducer();
102 
103  auto compute = [ndim, &new_axes, &reduce_axes, &func, &data_reshaped](const Array<Var>& indices) {
104  Array<PrimExpr> eval_range;
105  int arg_counter = 0;
106  int red_counter = 0;
107 
108  for (int i = 0; i < ndim; ++i) {
109  if (std::find(new_axes.begin(), new_axes.end(), i) != new_axes.end()) {
110  // new_axes contains i
111  eval_range.push_back(reduce_axes[red_counter]);
112  red_counter++;
113  } else {
114  eval_range.push_back(indices[arg_counter]);
115  arg_counter++;
116  }
117  }
118  auto square = [](const PrimExpr& x) { return x * x; };
119  return func({data_reshaped(eval_range), square(data_reshaped(eval_range))}, reduce_axes,
120  nullptr);
121  };
122 
123  auto temp_x_x2 =
124  tvm::te::compute(target_shape, compute, data->op->name + "_red_temp", kCommReduce);
125 
126  auto temp_x = temp_x_x2[0];
127  auto temp_x2 = temp_x_x2[1];
128  auto reduce_extent = make_const(DataType::Float(32), 1);
129  for (auto axis : new_axes) {
130  reduce_extent *= data_reshaped->shape[axis];
131  }
132  auto group_norm_func = [&](const Array<Var>& indices) {
133  Array<Var> reduce_indices, non_reduce_indices, gamma_indices;
134  for (int i = 0, n = static_cast<int>(indices.size()); i < n; ++i) {
135  if (std::find(new_axes.begin(), new_axes.end(), i) != new_axes.end()) {
136  reduce_indices.push_back(indices[i]);
137  } else {
138  non_reduce_indices.push_back(indices[i]);
139  }
140  }
141  gamma_indices = {indices[channel_axis], indices[channel_axis + 1]};
142  auto mean = temp_x(non_reduce_indices) / reduce_extent;
143  auto var = temp_x2(non_reduce_indices) / reduce_extent - mean * mean;
145  (data_reshaped(indices) - mean) * tvm::rsqrt(var + make_const(data->dtype, epsilon));
146  if (is_float16) {
148  }
149  if (gamma.defined()) {
150  group_norm = topi::multiply(group_norm, gamma_reshaped(gamma_indices));
151  }
152  if (beta.defined()) {
153  group_norm = topi::add(group_norm, beta_reshaped(gamma_indices));
154  }
155  return group_norm;
156  };
157  auto group_norm_out = tvm::te::compute(data_reshaped->shape, group_norm_func, name, tag);
158  auto group_norm_out_reshaped = reshape(group_norm_out, shape);
159  return group_norm_out_reshaped;
160 }
161 
162 } // namespace nn
163 } // namespace topi
164 } // namespace tvm
165 
166 #endif // TVM_TOPI_NN_GROUP_NORM_H_
Reference to PrimExprNode.
Definition: expr.h:115
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
void push_back(const T &item)
push a new item to the back of the list
Definition: array.h:457
static DataType Float(int bits, int lanes=1)
Construct an float type.
Definition: data_type.h:236
bool defined() const
Definition: object.h:552
Tensor structure representing a possible input, or intermediate computation result.
Definition: tensor.h:102
Managed reference to CastNode.
Definition: expr.h:117
Tensor expression language DSL.
Definition: extracted_task.h:33
Var var(std::string name_hint, DataType t=DataType::Int(32))
Construct a new Var expression.
Tensor compute(Array< PrimExpr > shape, FCompute fcompute, std::string name="tensor", std::string tag="", Map< String, ObjectRef > attrs={})
Construct a new tensor by computing over shape, using the computation rule: result_tensor[axis] = fco...
PrimExpr make_const(DataType t, ValueType value, Span span=Span())
Make a const value with certain data type.
Definition: op.h:962
Tensor group_norm(const Tensor &data, const Tensor &gamma, const Tensor &beta, int num_groups, int channel_axis, const Array< Integer > &axes, double epsilon, std::string name="T_group_norm", std::string tag=kInjective)
Definition: group_norm.h:39
FCommReduce MakeTupleSumReducer()
Create communitive reducer summing over tuples.
Definition: reduction.h:587
constexpr auto kInjective
Definition: tags.h:33
Tensor reshape(const Tensor &x, Array< PrimExpr > newshape, std::string name="T_reshape", std::string tag=kInjective)
Reshape a tensor.
Definition: transform.h:327
tvm::PrimExpr multiply(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:225
constexpr auto kCommReduce
Definition: tags.h:34
Tensor cast(const Tensor &x, DataType type, std::string name="T_cast", std::string tag=kElementWise)
Cast each element of x to the given type. If expr is scalar and type is a corresponding vector type,...
Definition: elemwise.h:281
Array< IterVar > MakeReduceAxes(const std::vector< int > &real_axis, const Tensor &data)
Enumerate the axes for a reduce op.
Definition: reduction.h:89
std::vector< int > GetRealAxis(int ndim, const Array< Integer > &axis)
Convert a reduction axis which could be empty or have negative elements into a real axis with valid d...
Definition: reduction.h:65
tvm::PrimExpr add(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:197
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
Tensor shape(const Tensor &src, DataType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition: transform.h:1913
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
PrimExpr rsqrt(PrimExpr x, Span span=Span())
Definition: op.h:713
PrimExpr floordiv(PrimExpr a, PrimExpr b, Span span=Span())
compute floor(a / b)
Operation node can generate one or multiple Tensors.