// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. // // Licensed 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. #include "paddle/phi/kernels/reduce_mean_grad_kernel.h" #include "paddle/phi/backends/xpu/enforce_xpu.h" #include "paddle/phi/backends/xpu/xpu_context.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/xpu/reduce.h" namespace phi { template void ReduceMeanGradKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& out_grad, const IntArray& dims, bool keep_dim, bool reduce_all, DenseTensor* x_grad) { if (x_grad && x_grad->numel() == 0) { dev_ctx.template Alloc(x_grad); return; } using XPUType = typename XPUTypeTrait::Type; reduce_all = recompute_reduce_all(x, dims, reduce_all); dev_ctx.template Alloc(x_grad); const XPUType* dy_data = reinterpret_cast(out_grad.data()); XPUType* x_data = reinterpret_cast(x_grad->data()); auto reduce_dims = dims.GetData(); std::vector xdims = vectorize(x.dims()); std::vector ydims = vectorize(out_grad.dims()); int64_t reduce_numel = 1; if (reduce_all) { reduce_dims.clear(); for (size_t d = 0; d < xdims.size(); ++d) { reduce_dims.push_back(d); } } for (auto& d : reduce_dims) { if (d < 0) { d = d + xdims.size(); } reduce_numel *= xdims[d]; } if (keep_dim != true) { sort(reduce_dims.begin(), reduce_dims.end()); for (auto& d : reduce_dims) { ydims.insert(ydims.begin() + d, 1); } } float val = 1.0f / static_cast(reduce_numel); int r = xpu::constant( dev_ctx.x_context(), x_data, x.numel(), static_cast(val)); PADDLE_ENFORCE_XDNN_SUCCESS(r, "constant"); // use [1] to replace [], because xpu not support [] if (xdims.size() == 0) { xdims = std::vector({1}); } if (ydims.size() == 0) { ydims = std::vector({1}); } r = xpu::broadcast_mul( dev_ctx.x_context(), x_data, dy_data, x_data, xdims, ydims); PADDLE_ENFORCE_XDNN_SUCCESS(r, "broadcast_mul"); } } // namespace phi PD_REGISTER_KERNEL(mean_grad, XPU, ALL_LAYOUT, phi::ReduceMeanGradKernel, float, phi::float16, phi::bfloat16) {}