|
| 1 | +/* Copyright (c) 2025 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 "glog/logging.h" |
| 16 | +#include "test/cpp/auto_parallel/spmd_rule_test_util.h" |
| 17 | +namespace paddle { |
| 18 | +namespace distributed { |
| 19 | +namespace auto_parallel { |
| 20 | + |
| 21 | +ProcessMesh CreateProcessMesh() { |
| 22 | + std::vector<int64_t> mesh_shape = {2, 3}; |
| 23 | + std::vector<int64_t> process_ids = {0, 1, 2, 3, 4, 5}; |
| 24 | + std::vector<std::string> dim_names = {"x", "y"}; |
| 25 | + return ProcessMesh(mesh_shape, process_ids, dim_names); |
| 26 | +} |
| 27 | + |
| 28 | +phi::distributed::DistMetaTensor CreateDistMetaTensor( |
| 29 | + const std::vector<int64_t>& shape, |
| 30 | + const std::vector<int64_t>& dims_mapping, |
| 31 | + const ProcessMesh& process_mesh) { |
| 32 | + TensorDistAttr dist_attr; |
| 33 | + dist_attr.set_process_mesh(process_mesh); |
| 34 | + dist_attr.set_dims_mapping(dims_mapping); |
| 35 | + return phi::distributed::DistMetaTensor(phi::make_ddim(shape), dist_attr); |
| 36 | +} |
| 37 | + |
| 38 | +TEST(ExpandInferSpmd, Ctor) { |
| 39 | + ProcessMesh process_mesh = CreateProcessMesh(); |
| 40 | + |
| 41 | + // Test case forward 1: Expand with shape {8, 2, 6, 1024, -1} |
| 42 | + auto x = CreateDistMetaTensor( |
| 43 | + {8, 2, 1, 1024, 128}, {0, -1, -1, 1, -1}, process_mesh); |
| 44 | + phi::IntArray shape = {8, 2, 6, 1024, -1}; |
| 45 | + auto spmdinfo = ExpandInferSpmd(x, shape); |
| 46 | + EXPECT_EQ(get_dims_mapping(spmdinfo.first[0]), |
| 47 | + std::vector<int64_t>({0, -1, -1, 1, -1})); |
| 48 | + EXPECT_EQ(get_dims_mapping(spmdinfo.second[0]), |
| 49 | + std::vector<int64_t>({0, -1, -1, 1, -1})); |
| 50 | + |
| 51 | + // Test case forward 2: Expand with shape {2, -1} |
| 52 | + auto x1 = CreateDistMetaTensor({8}, {1}, process_mesh); |
| 53 | + phi::IntArray shape1 = {2, -1}; |
| 54 | + auto spmdinfo1 = ExpandInferSpmd(x1, shape1); |
| 55 | + EXPECT_EQ(get_dims_mapping(spmdinfo1.first[0]), std::vector<int64_t>({1})); |
| 56 | + EXPECT_EQ(get_dims_mapping(spmdinfo1.second[0]), |
| 57 | + std::vector<int64_t>({-1, 1})); |
| 58 | + |
| 59 | + // Test case forward 3: Expand with shape {0, -1} |
| 60 | + auto x2 = CreateDistMetaTensor({8}, {1}, process_mesh); |
| 61 | + phi::IntArray shape2 = {0, -1}; |
| 62 | + auto spmdinfo2 = ExpandInferSpmd(x2, shape2); |
| 63 | + EXPECT_EQ(get_dims_mapping(spmdinfo2.first[0]), std::vector<int64_t>({1})); |
| 64 | + EXPECT_EQ(get_dims_mapping(spmdinfo2.second[0]), |
| 65 | + std::vector<int64_t>({-1, 1})); |
| 66 | + |
| 67 | + // Test case backward 1: ExpandGrad with shape {0, -1} |
| 68 | + auto x3 = CreateDistMetaTensor({8}, {1}, process_mesh); |
| 69 | + auto out3 = CreateDistMetaTensor({2, 8}, {-1, 1}, process_mesh); |
| 70 | + phi::IntArray shape3 = {0, -1}; |
| 71 | + auto spmdinfo3 = ExpandGradInferSpmd(x3, out3, shape3); |
| 72 | + EXPECT_EQ(get_dims_mapping(spmdinfo3.first[0]), std::vector<int64_t>({1})); |
| 73 | + EXPECT_EQ(get_dims_mapping(spmdinfo3.first[1]), |
| 74 | + std::vector<int64_t>({-1, 1})); |
| 75 | + EXPECT_EQ(get_dims_mapping(spmdinfo3.second[0]), std::vector<int64_t>({1})); |
| 76 | +} |
| 77 | + |
| 78 | +} // namespace auto_parallel |
| 79 | +} // namespace distributed |
| 80 | +} // namespace paddle |
0 commit comments