|
| 1 | +// Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "paddle/phi/kernels/aminmax_grad_kernel.h" |
| 16 | + |
| 17 | +#include "paddle/phi/backends/all_context.h" |
| 18 | +#include "paddle/phi/core/kernel_registry.h" |
| 19 | +#include "paddle/phi/kernels/elementwise_add_kernel.h" |
| 20 | +#include "paddle/phi/kernels/reduce_amax_grad_kernel.h" |
| 21 | +#include "paddle/phi/kernels/reduce_amin_grad_kernel.h" |
| 22 | + |
| 23 | +namespace phi { |
| 24 | + |
| 25 | +template <typename T, typename Context> |
| 26 | +void AMinMaxGradKernel(const Context& dev_ctx, |
| 27 | + const DenseTensor& x, |
| 28 | + const DenseTensor& min, |
| 29 | + const DenseTensor& max, |
| 30 | + const DenseTensor& min_grad, |
| 31 | + const DenseTensor& max_grad, |
| 32 | + const std::vector<int64_t>& dims, |
| 33 | + bool keep_dim, |
| 34 | + bool reduce_all, |
| 35 | + DenseTensor* x_grad) { |
| 36 | + if (x_grad && x_grad->numel() == 0) { |
| 37 | + dev_ctx.template Alloc<T>(x_grad); |
| 38 | + return; |
| 39 | + } |
| 40 | + reduce_all = recompute_reduce_all(x, dims, reduce_all); |
| 41 | + |
| 42 | + // Compute amax grad contribution into x_grad |
| 43 | + ReduceAMaxGradKernel<T, Context>( |
| 44 | + dev_ctx, x, max, max_grad, dims, keep_dim, reduce_all, x_grad); |
| 45 | + |
| 46 | + // Compute amin grad contribution into a temporary tensor |
| 47 | + DenseTensor amin_x_grad; |
| 48 | + amin_x_grad.Resize(x_grad->dims()); |
| 49 | + dev_ctx.template Alloc<T>(&amin_x_grad); |
| 50 | + ReduceAMinGradKernel<T, Context>( |
| 51 | + dev_ctx, x, min, min_grad, dims, keep_dim, reduce_all, &amin_x_grad); |
| 52 | + |
| 53 | + // x_grad = amax_grad_result + amin_grad_result |
| 54 | + Add<T, Context>(dev_ctx, *x_grad, amin_x_grad, x_grad); |
| 55 | +} |
| 56 | + |
| 57 | +} // namespace phi |
| 58 | + |
| 59 | +PD_REGISTER_KERNEL(aminmax_grad, |
| 60 | + CPU, |
| 61 | + ALL_LAYOUT, |
| 62 | + phi::AMinMaxGradKernel, |
| 63 | + float, |
| 64 | + double, |
| 65 | + int, |
| 66 | + int64_t) {} |
| 67 | + |
| 68 | +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) |
| 69 | +PD_REGISTER_KERNEL(aminmax_grad, |
| 70 | + GPU, |
| 71 | + ALL_LAYOUT, |
| 72 | + phi::AMinMaxGradKernel, |
| 73 | + float, |
| 74 | + double, |
| 75 | + int, |
| 76 | + int64_t) {} |
| 77 | +#endif |
0 commit comments