40 const ffi::Optional<Tensor>& beta,
int num_groups,
int channel_axis,
41 const ffi::Array<int64_t>& axes,
double epsilon,
42 std::string name =
"T_group_norm", std::string tag =
kInjective) {
44 const auto& data_type = data->dtype;
45 const auto& gamma_type = gamma.has_value() ? gamma.value()->dtype : data_type;
46 const auto& beta_type = beta.has_value() ? beta.value()->dtype : data_type;
47 TVM_FFI_ICHECK(data_type == gamma_type && data_type == beta_type)
48 <<
"group_norm: data, gamma and beta must have the same type";
50 <<
"group_norm: only support float32 and float16 for now";
53 int ndim = data->shape.size();
54 channel_axis =
GetRealAxis(
static_cast<int>(ndim), ffi::Array<int64_t>({channel_axis}))[0];
56 auto shape = data->shape;
58 auto new_shape = ffi::Array<PrimExpr>();
59 for (
int i = 0; i < ndim; ++i) {
60 if (i == channel_axis) {
61 new_shape.push_back(num_groups);
62 new_shape.push_back(group_size);
64 new_shape.push_back(
shape[i]);
72 ffi::Optional<Tensor> gamma_reshaped;
73 if (gamma.has_value()) {
74 gamma_reshaped =
reshape(gamma.value(), {num_groups, group_size});
76 ffi::Optional<Tensor> beta_reshaped;
77 if (beta.has_value()) {
78 beta_reshaped =
reshape(beta.value(), {num_groups, group_size});
82 std::vector<int> new_axes{channel_axis + 1};
83 for (
auto axis : axes) {
84 int new_axis =
GetRealAxis(
static_cast<int>(ndim), ffi::Array<int64_t>({axis}))[0];
85 if (new_axis < channel_axis) {
86 new_axes.push_back(new_axis);
87 }
else if (new_axis > channel_axis) {
88 new_axes.push_back(new_axis + 1);
90 TVM_FFI_ICHECK(
false) <<
"axes can not contain channel axis";
93 std::sort(new_axes.begin(), new_axes.end());
96 ndim = data_reshaped->shape.size();
102 auto compute = [ndim, &new_axes, &reduce_axes, &func,
103 &data_reshaped](
const ffi::Array<PrimVar>& indices) {
104 ffi::Array<PrimExpr> eval_range;
108 for (
int i = 0; i < ndim; ++i) {
109 if (std::find(new_axes.begin(), new_axes.end(), i) != new_axes.end()) {
111 eval_range.push_back(reduce_axes[red_counter]);
114 eval_range.push_back(indices[arg_counter]);
118 auto square = [](
const PrimExpr& x) {
return x * x; };
119 return func({data_reshaped(eval_range), square(data_reshaped(eval_range))}, reduce_axes,
126 auto temp_x = temp_x_x2[0];
127 auto temp_x2 = temp_x_x2[1];
129 for (
auto axis : new_axes) {
130 reduce_extent *= data_reshaped->shape[axis];
132 auto group_norm_func = [&](
const ffi::Array<PrimVar>& indices) {
133 ffi::Array<PrimVar> 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]);
138 non_reduce_indices.push_back(indices[i]);
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;
149 if (gamma.has_value()) {
152 if (beta.has_value()) {
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;
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 group_norm(const Tensor &data, const ffi::Optional< Tensor > &gamma, const ffi::Optional< Tensor > &beta, int num_groups, int channel_axis, const ffi::Array< int64_t > &axes, double epsilon, std::string name="T_group_norm", std::string tag=kInjective)
Definition group_norm.h:39
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