vinx13 commented on code in PR #14512: URL: https://github.com/apache/tvm/pull/14512#discussion_r1160029376
########## src/relax/transform/decompose_ops.cc: ########## @@ -0,0 +1,267 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! \file src/relax/transform/decompose_ops.cc */ + +#include <tvm/relax/analysis.h> +#include <tvm/relax/attrs/nn.h> +#include <tvm/relax/struct_info.h> +#include <tvm/relax/transform.h> + +#include "utils.h" + +namespace tvm { +namespace relax { + +TensorStructInfo MatchTensorStructInfo(Expr data) { + auto _sinfo = MatchStructInfo<TensorStructInfo>(data); + ICHECK(_sinfo.defined()) << "Expect data to be a tensor, but get " << GetStructInfo(data); + return _sinfo.value(); +} + +Expr ExpandToMatchInput(Expr data, int ndim, Array<Integer> axes) { + axes = GetOrderedPositiveAxes(axes, ndim); + Array<Integer> expand_axes; + for (int i = 0, j = 0; i < ndim; ++i) { + if (j < static_cast<int>(axes.size()) && i == axes[j]->value) { + ++j; + } else { + expand_axes.push_back(i); + } + } + return expand_dims(data, expand_axes); +} + +Tuple SimplifyBatchNormInference(const Call& call) { + auto attrs = call->attrs.as<BatchNormAttrs>(); + ICHECK_NOTNULL(attrs); + + Expr data = call->args[0]; + TensorStructInfo sinfo = MatchTensorStructInfo(data); + Expr gamma = call->args[1]; + Expr beta = call->args[2]; + + Expr moving_mean = ExpandToMatchInput(call->args[3], sinfo->ndim, {attrs->axis}); + Expr moving_var = ExpandToMatchInput(call->args[4], sinfo->ndim, {attrs->axis}); + + // output = (x - mean) / sqrt(var + epsilon) * gamma + beta + Expr epsilon = MakeConstantScalar(attrs->epsilon, sinfo->dtype); + Expr sqrt_var = sqrt(add(moving_var, epsilon)); + Expr out = divide(subtract(data, moving_mean), sqrt_var); + + if (attrs->scale) { + out = multiply(out, ExpandToMatchInput(gamma, sinfo->ndim, {attrs->axis})); + } + if (attrs->center) { + out = add(out, ExpandToMatchInput(beta, sinfo->ndim, {attrs->axis})); + } + + return Tuple({out, call->args[3], call->args[4]}); +} + +Tuple SimplifyBatchNormTraining(const Call& call) { + auto attrs = call->attrs.as<BatchNormAttrs>(); + ICHECK_NOTNULL(attrs); + + Expr data = call->args[0]; + TensorStructInfo sinfo = MatchTensorStructInfo(data); + Expr gamma = call->args[1]; + Expr beta = call->args[2]; + + Array<Integer> reduce_axes; + for (int i = 0; i < sinfo->ndim; ++i) { + if (i != attrs->axis) { + reduce_axes.push_back(i); + } + } + + Expr data_mean = mean(data, reduce_axes, false); + Expr data_mean_rs = ExpandToMatchInput(data_mean, sinfo->ndim, {attrs->axis}); + Expr data_var = variance(data, reduce_axes, false); + Expr data_var_rs = ExpandToMatchInput(data_var, sinfo->ndim, {attrs->axis}); + + // output = (x - mean) / sqrt(var + epsilon) * gamma + beta + Expr epsilon = MakeConstantScalar(attrs->epsilon, sinfo->dtype); + Expr sqrt_var = sqrt(add(data_var_rs, epsilon)); + Expr out = divide(subtract(data, data_mean_rs), sqrt_var); + + if (attrs->scale) { + out = multiply(out, ExpandToMatchInput(gamma, sinfo->ndim, {attrs->axis})); + } + if (attrs->center) { + out = add(out, ExpandToMatchInput(beta, sinfo->ndim, {attrs->axis})); + } + + Expr moving_mean = call->args[3]; + Expr moving_var = call->args[4]; + Expr momentum = MakeConstantScalar(attrs->momentum, sinfo->dtype); + Expr one_minus_mom = MakeConstantScalar(1 - attrs->momentum, sinfo->dtype); + + return Tuple({ + out, + add(multiply(one_minus_mom, moving_mean), multiply(momentum, data_mean)), + add(multiply(one_minus_mom, moving_var), multiply(momentum, data_var)), + }); +} + +Expr SimplifyLayerNorm(const Call& call) { Review Comment: > Is there any complication for fusion if we have a composite ops in general? Yes, we need to be aware of the implementation when setting op type (`kOpaque` or `kInjective`). This will cause different fusion results. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@tvm.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org