Skip to content

[Auto Parallel]Add spmd_rule for min and min_grad ops. #72269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion paddle/phi/infermeta/spmd_rules/reduction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@ SpmdInfo ReductionMaxInferSpmdDynamic(const DistMetaTensor& x,
return ReductionInferSpmdBase(
x, axis.GetData(), keep_dim, static_cast<int>(ReduceType::kRedMax));
}

SpmdInfo ReductionMinInferSpmdDynamic(const DistMetaTensor& x,
const IntArray& axis,
bool keep_dim) {
return ReductionInferSpmdBase(
x, axis.GetData(), keep_dim, static_cast<int>(ReduceType::kRedMin));
}
SpmdInfo ReductionAllInferSpmdDynamic(const DistMetaTensor& x,
const IntArray& axis,
bool keep_dim) {
Expand Down
3 changes: 3 additions & 0 deletions paddle/phi/infermeta/spmd_rules/reduction.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ SpmdInfo ReductionMaxInferSpmdDynamic(const DistMetaTensor& x,
const IntArray& axis,
bool keep_dim);

SpmdInfo ReductionMinInferSpmdDynamic(const DistMetaTensor& x,
const IntArray& axis,
bool keep_dim);
SpmdInfo ReductionAllInferSpmdDynamic(const DistMetaTensor& x,
const IntArray& axis,
bool keep_dim);
Expand Down
1 change: 1 addition & 0 deletions paddle/phi/ops/yaml/inconsistent/dygraph_backward.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@
infer_meta :
func : UnchangedInferMeta
param: [x]
spmd_rule : ReductionGradInferSpmd
kernel :
func : min_grad
composite : min_grad(x, out, out_grad, axis, keepdim, reduce_all, x_grad)
Expand Down
1 change: 1 addition & 0 deletions paddle/phi/ops/yaml/inconsistent/dygraph_ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@
output : Tensor(out)
infer_meta :
func : ReduceIntArrayAxisInferMeta
spmd_rule : ReductionMinInferSpmdDynamic
kernel :
func : min
backward : min_grad
Expand Down
1 change: 1 addition & 0 deletions paddle/phi/ops/yaml/inconsistent/static_backward.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@
infer_meta :
func : UnchangedInferMeta
param: [x]
spmd_rule : ReductionGradInferSpmd
kernel :
func : min_grad

Expand Down
1 change: 1 addition & 0 deletions paddle/phi/ops/yaml/inconsistent/static_ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@
output : Tensor(out)
infer_meta :
func : ReduceIntArrayAxisInferMeta
spmd_rule : ReductionMinInferSpmdDynamic
kernel :
func : min
backward : min_grad
Expand Down
21 changes: 21 additions & 0 deletions test/cpp/auto_parallel/spmd_rule_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,27 @@ TEST(ReduceMaxRule, Ctor) {
check_partial_dims(backward_info.second[0], {});
}

TEST(ReduceMinRule, Ctor) {
std::vector<int64_t> mesh_shape = {2};
std::vector<int64_t> process_ids = {0, 1};
std::vector<std::string> dim_names = {"x"};
ProcessMesh process_mesh(mesh_shape, process_ids, dim_names);

// test forward
auto t_dist_attr = TensorDistAttr();
t_dist_attr.set_process_mesh(process_mesh);
t_dist_attr.set_dims_mapping({-1, 0, -1});
t_dist_attr.set_dynamic_dims({false, false, false});
phi::distributed::DistMetaTensor x = phi::distributed::DistMetaTensor(
common::make_ddim({4, 6, 8}), t_dist_attr);
phi::IntArray axis = {1};
bool keep_dim = false;
phi::distributed::SpmdInfo forward_info =
phi::distributed::ReductionMinInferSpmdDynamic(x, axis, keep_dim);
check_dim_mapping(forward_info.second[0], {-1, -1});
check_partial_dims(forward_info.second[0], {0});
Copy link
Contributor

Choose a reason for hiding this comment

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

shall we test backward?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The spmd_rule function of min_grad reuses ReductionGradInferSpmd, which has already been validated through previous CI tests.

}

TEST(ReduceAllRule, Ctor) {
std::vector<int64_t> mesh_shape = {2};
std::vector<int64_t> process_ids = {0, 1};
Expand Down
Loading