#include "op_plugin/AclOpsInterface.h"
#include "op_plugin/OpApiInterface.h"
#include "op_plugin/utils/op_api_common.h"
namespace op_api {
using npu_preparation = at_npu::native::OpPreparation;
std::tuple<at::Tensor, at::Tensor, at::Tensor, at::Tensor> batch_norm_backward_reduce(
const at::Tensor& grad_out, const at::Tensor& self, const at::Tensor& mean, const at::Tensor& invstd,
const c10::optional<at::Tensor>& weight_opt, bool input_g, bool weight_g, bool bias_g) {
DO_COMPATIBILITY(
aclnnBatchNormReduceBackward,
acl_op::batch_norm_backward_reduce(grad_out, self, mean, invstd, weight_opt, input_g, weight_g, bias_g));
int64_t n_input = self.size(1);
at::Tensor sum_dy;
at::Tensor sum_dy_xmu;
at::Tensor grad_weight;
at::Tensor grad_bias;
bool is_float16 = false;
if (self.scalar_type() == mean.scalar_type() && self.scalar_type() == at::kHalf) {
is_float16 = true;
}
auto out_dtype = (is_float16) ? at::kHalf : at::kFloat;
if (input_g) {
sum_dy = npu_preparation::apply_tensor_without_format(mean.sizes(), mean.options().dtype(out_dtype));
sum_dy_xmu = npu_preparation::apply_tensor_without_format(mean.sizes(), mean.options().dtype(out_dtype));
}
if (weight_g) {
grad_weight = npu_preparation::apply_tensor_without_format({n_input}, invstd.options().dtype(out_dtype));
}
if (bias_g) {
grad_bias = npu_preparation::apply_tensor_without_format({n_input}, grad_out.options().dtype(out_dtype));
}
EXEC_NPU_CMD(aclnnBatchNormReduceBackward, grad_out, self, mean, invstd, weight_opt, input_g, weight_g, bias_g,
sum_dy, sum_dy_xmu, grad_weight, grad_bias);
return std::make_tuple(sum_dy, sum_dy_xmu, grad_weight, grad_bias);
}
}