Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,24 @@ bool AminOpInferSymbolicShape(pir::Operation *op,
axis.size() == 0 /*reduce_all*/);
}

bool AminmaxOpInferSymbolicShape(
pir::Operation *op, pir::InferSymbolicShapeContext *infer_context) {
const auto &axis = details::GetVectorAttr(op, "axis");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个打印个信息调试下吧,显示没覆盖到

bool keepdim = GetBoolAttr(op, "keepdim");
bool reduce_all = axis.size() == 0;

// ReduceInferDim only sets result(0). We need the same shape for both
// outputs, so call it for result(0) then copy to result(1).
bool ret =
details::ReduceInferDim(op, infer_context, axis, keepdim, reduce_all);
if (ret) {
const auto &out_shape =
infer_context->GetShapeOrDataForValue(op->result(0));
infer_context->SetShapeOrDataForValue(op->result(1), out_shape);
}
return ret;
}

bool AnyOpInferSymbolicShape(pir::Operation *op,
pir::InferSymbolicShapeContext *infer_context) {
const auto &axis = details::GetVectorAttr(op, "axis");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ OP_DECLARE_INFER_SYMBOLIC_SHAPE(AffineGrid)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(All)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Amax)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Amin)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Aminmax)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Any)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Argmax)
OP_DECLARE_INFER_SYMBOLIC_SHAPE(Argmin)
Expand Down
18 changes: 18 additions & 0 deletions paddle/phi/infermeta/unary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4287,6 +4287,24 @@ void ReduceInferMeta(const MetaTensor& x,
ReduceInferMetaBase(x, axis, keep_dim, reduce_all, out);
}

void AMinMaxInferMeta(const MetaTensor& x,
const std::vector<int64_t>& axis,
bool keep_dim,
MetaTensor* min,
MetaTensor* max) {
bool reduce_all = false;
if (axis.empty()) {
reduce_all = true;
}
DDim out_dim = ReduceInferDim(x, axis, keep_dim, reduce_all);
min->set_dims(out_dim);
min->set_dtype(x.dtype());
min->set_layout(x.layout());
max->set_dims(out_dim);
max->set_dtype(x.dtype());
max->set_layout(x.layout());
}

DDim ReduceInferDimForIntArrayAxis(const MetaTensor& x,
const IntArray& axis,
bool keep_dim,
Expand Down
6 changes: 6 additions & 0 deletions paddle/phi/infermeta/unary.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ PADDLE_API void ArgMinMaxInferMeta(const MetaTensor& x,
MetaTensor* out,
MetaConfig config = MetaConfig());

PADDLE_API void AMinMaxInferMeta(const MetaTensor& x,
const std::vector<int64_t>& axis,
bool keep_dim,
MetaTensor* min,
MetaTensor* max);

PADDLE_API void MinMaxWithIndexInferMeta(const MetaTensor& x,
const Scalar& axis,
bool keepdims,
Expand Down
77 changes: 77 additions & 0 deletions paddle/phi/kernels/aminmax_grad_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) 2026 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/aminmax_grad_kernel.h"

#include "paddle/phi/backends/all_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/elementwise_add_kernel.h"
#include "paddle/phi/kernels/reduce_amax_grad_kernel.h"
#include "paddle/phi/kernels/reduce_amin_grad_kernel.h"

namespace phi {

template <typename T, typename Context>
void AMinMaxGradKernel(const Context& dev_ctx,
const DenseTensor& x,
const DenseTensor& min,
const DenseTensor& max,
const DenseTensor& min_grad,
const DenseTensor& max_grad,
const std::vector<int64_t>& dims,
bool keep_dim,
bool reduce_all,
DenseTensor* x_grad) {
if (x_grad && x_grad->numel() == 0) {
dev_ctx.template Alloc<T>(x_grad);
return;
}
reduce_all = recompute_reduce_all(x, dims, reduce_all);

// Compute amax grad contribution into x_grad
ReduceAMaxGradKernel<T, Context>(
dev_ctx, x, max, max_grad, dims, keep_dim, reduce_all, x_grad);

// Compute amin grad contribution into a temporary tensor
DenseTensor amin_x_grad;
amin_x_grad.Resize(x_grad->dims());
dev_ctx.template Alloc<T>(&amin_x_grad);
ReduceAMinGradKernel<T, Context>(
dev_ctx, x, min, min_grad, dims, keep_dim, reduce_all, &amin_x_grad);

// x_grad = amax_grad_result + amin_grad_result
Add<T, Context>(dev_ctx, *x_grad, amin_x_grad, x_grad);
}

} // namespace phi

PD_REGISTER_KERNEL(aminmax_grad,
CPU,
ALL_LAYOUT,
phi::AMinMaxGradKernel,
float,
double,
int,
int64_t) {}

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
PD_REGISTER_KERNEL(aminmax_grad,
GPU,
ALL_LAYOUT,
phi::AMinMaxGradKernel,
float,
double,
int,
int64_t) {}
#endif
31 changes: 31 additions & 0 deletions paddle/phi/kernels/aminmax_grad_kernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2026 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.

#pragma once

#include "paddle/phi/core/dense_tensor.h"

namespace phi {
template <typename T, typename Context>
void AMinMaxGradKernel(const Context& dev_ctx,
const DenseTensor& x,
const DenseTensor& min,
const DenseTensor& max,
const DenseTensor& min_grad,
const DenseTensor& max_grad,
const std::vector<int64_t>& axis,
bool keep_dim,
bool reduce_all,
DenseTensor* x_grad);
} // namespace phi
50 changes: 50 additions & 0 deletions paddle/phi/kernels/aminmax_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2025 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/aminmax_kernel.h"

#include "paddle/phi/backends/all_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/reduce_amax_kernel.h"
#include "paddle/phi/kernels/reduce_amin_kernel.h"

namespace phi {

template <typename T, typename Context>
void AMinMaxKernel(const Context& dev_ctx,
const DenseTensor& x,
const std::vector<int64_t>& dims,
bool keep_dim,
DenseTensor* min,
DenseTensor* max) {
bool reduce_all = recompute_reduce_all(x, dims);
AMinRawKernel<T>(dev_ctx, x, dims, keep_dim, reduce_all, min);
AMaxRawKernel<T>(dev_ctx, x, dims, keep_dim, reduce_all, max);
}

} // namespace phi

PD_REGISTER_KERNEL(
aminmax, CPU, ALL_LAYOUT, phi::AMinMaxKernel, float, double, int, int64_t) {
}

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
PD_REGISTER_KERNEL(
aminmax, GPU, ALL_LAYOUT, phi::AMinMaxKernel, float, double, int, int64_t) {
}
#endif

#if defined(PADDLE_WITH_XPU_KP)
PD_REGISTER_KERNEL(aminmax, KPS, ALL_LAYOUT, phi::AMinMaxKernel, float) {}
#endif
29 changes: 29 additions & 0 deletions paddle/phi/kernels/aminmax_kernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2025 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.

#pragma once

#include "paddle/phi/core/dense_tensor.h"

namespace phi {

template <typename T, typename Context>
void AMinMaxKernel(const Context& dev_ctx,
const DenseTensor& x,
const std::vector<int64_t>& dims,
bool keep_dim,
DenseTensor* min,
DenseTensor* max);

} // namespace phi
10 changes: 10 additions & 0 deletions paddle/phi/ops/yaml/backward.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@
kernel :
func : amin_grad

- backward_op : aminmax_grad
forward : aminmax (Tensor x, int64_t[] axis={}, bool keepdim=false) -> Tensor(min), Tensor(max)
args : (Tensor x, Tensor min, Tensor max, Tensor min_grad, Tensor max_grad, int64_t[] axis={}, bool keepdim=false, bool reduce_all=false)
output : Tensor(x_grad)
infer_meta :
func : UnchangedInferMeta
param : [x]
kernel :
func : aminmax_grad

- backward_op : angle_grad
forward : angle (Tensor x) -> Tensor(out)
args : (Tensor x, Tensor out_grad)
Expand Down
8 changes: 8 additions & 0 deletions paddle/phi/ops/yaml/op_compat.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@
amin_grad : GetReduceGradExpectedKernelType
manual_signature : [amin]

- op : aminmax

@zhwesky2010 zhwesky2010 Mar 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个应该是一些不兼容历史问题的适配,这个需要改吗,不报错可以不改

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是否可以不改?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的定义用于映射旧框架大写命名(X、Min、Max)到新 phi 内核小写命名(x、min、max)。OpTest 类依赖此映射来构建算子,移除后 check_output 和 check_grad 会报 Missing x as input 或 out_dtype not found 错误,已通过验证不能去掉

backward : aminmax_grad
inputs :
x : X
outputs :
{min : Min, max : Max}
manual_signature : [aminmax]

- op : anchor_generator
inputs:
input : Input
Expand Down
10 changes: 10 additions & 0 deletions paddle/phi/ops/yaml/ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@
backward : amin_grad
interfaces : paddle::dialect::InferSymbolicShapeInterface, paddle::dialect::LayoutTransformationInterface

- op : aminmax
args : (Tensor x, int64_t[] axis={}, bool keepdim=false)
output : Tensor(min), Tensor(max)
infer_meta :
func : AMinMaxInferMeta
kernel :
func : aminmax
backward : aminmax_grad
interfaces : paddle::dialect::InferSymbolicShapeInterface

- op : angle
args : (Tensor x)
output : Tensor
Expand Down
5 changes: 5 additions & 0 deletions paddle/phi/ops/yaml/python_api_info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
args_alias :
use_default_mapping : True

- op : aminmax
name : [paddle.aminmax, paddle.Tensor.aminmax]
args_alias :
use_default_mapping : True

- op : angle
name : [paddle.angle, paddle.Tensor.angle]
args_alias :
Expand Down
1 change: 1 addition & 0 deletions python/paddle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,7 @@ def __dir__(self):
'min',
'narrow',
'amin',
'aminmax',
'any',
'slice',
'slice_scatter',
Expand Down
Loading
Loading