-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AutoParallel] Add pad spmd rules (#68304)
* update * Update backward.yaml * Update backward.yaml * Update backward.yaml
- Loading branch information
Showing
10 changed files
with
255 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* Copyright (c) 2024 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/infermeta/spmd_rules/pad.h" | ||
#include <numeric> | ||
|
||
#include "glog/logging.h" | ||
|
||
#include "paddle/phi/core/distributed/auto_parallel/dist_attr.h" | ||
#include "paddle/phi/core/distributed/auto_parallel/inferspmd_utils.h" | ||
#include "paddle/phi/core/distributed/auto_parallel/utils.h" | ||
#include "paddle/phi/infermeta/spmd_rules/rules.h" | ||
#include "paddle/phi/infermeta/spmd_rules/spmd_rule_macro_define.h" | ||
#include "paddle/phi/infermeta/spmd_rules/utils.h" | ||
|
||
namespace phi { | ||
namespace distributed { | ||
|
||
SpmdInfo PadInferSpmd(const DistMetaTensor& x, | ||
const std::vector<int>& paddings, | ||
int pad_value) { | ||
auto x_shape = phi::vectorize(x.dims()); | ||
int x_ndim = x_shape.size(); | ||
auto x_dist_attr_src = x.dist_attr(); | ||
std::vector<int64_t> x_dims_mapping = x_dist_attr_src.dims_mapping(); | ||
PADDLE_ENFORCE_EQ( | ||
x_ndim, | ||
x_dims_mapping.size(), | ||
phi::errors::InvalidArgument("The Tensor X's rank [%d] and X's " | ||
"dims_mapping size [%d] are not matched.", | ||
x_ndim, | ||
x_dims_mapping.size())); | ||
std::vector<int64_t> dims_to_unshard; | ||
for (size_t i = 0; i < paddings.size(); i += 2) { | ||
if (paddings[i] != 0 || paddings[i + 1] != 0) { | ||
dims_to_unshard.push_back(i / 2); | ||
} | ||
} | ||
auto x_dist_attr = UnShardTensorDims(x_dist_attr_src, dims_to_unshard); | ||
TensorDistAttr out_dist_attr = CopyTensorDistAttrForOutput(x_dist_attr); | ||
out_dist_attr.set_dims_mapping(x_dist_attr.dims_mapping()); | ||
|
||
VLOG(4) << "PadInferSpmd: X shape: [" << str_join(x_shape) << "]"; | ||
VLOG(4) << "X dims_mapping: [" << str_join(x_dist_attr.dims_mapping()) | ||
<< "] Out dims_mapping: [" << str_join(out_dist_attr.dims_mapping()) | ||
<< "]"; | ||
|
||
return {{x_dist_attr}, {out_dist_attr}}; | ||
} | ||
|
||
SpmdInfo PadGradInferSpmd(const DistMetaTensor& x, | ||
const DistMetaTensor& out, | ||
const std::vector<int>& paddings, | ||
int pad_value) { | ||
auto out_shape = phi::vectorize(out.dims()); | ||
int out_ndim = out_shape.size(); | ||
auto out_dist_attr_src = out.dist_attr(); | ||
std::vector<int64_t> out_dims_mapping = out_dist_attr_src.dims_mapping(); | ||
PADDLE_ENFORCE_EQ( | ||
out_ndim, | ||
out_dims_mapping.size(), | ||
phi::errors::InvalidArgument("The Tensor Out's rank [%d] and Out's " | ||
"dims_mapping size [%d] are not matched.", | ||
out_ndim, | ||
out_dims_mapping.size())); | ||
|
||
std::vector<int64_t> dims_to_unshard; | ||
for (size_t i = 0; i < paddings.size(); i += 2) { | ||
if (paddings[i] != 0 || paddings[i + 1] != 0) { | ||
dims_to_unshard.push_back(i / 2); | ||
} | ||
} | ||
auto out_dist_attr = UnShardTensorDims(out_dist_attr_src, dims_to_unshard); | ||
TensorDistAttr x_dist_attr = CopyTensorDistAttrForOutput(out_dist_attr); | ||
x_dist_attr.set_dims_mapping(out_dist_attr.dims_mapping()); | ||
|
||
VLOG(4) << "PadInferSpmdReverse: Out shape: [" << str_join(out_shape) << "]"; | ||
VLOG(4) << "Out dims_mapping: [" << str_join(x_dist_attr.dims_mapping()) | ||
<< "] X dims_mapping: [" << str_join(x_dist_attr.dims_mapping()) | ||
<< "]"; | ||
|
||
return {{x_dist_attr}, {out_dist_attr}}; | ||
} | ||
|
||
SpmdInfo PadInferSpmdDynamic(const DistMetaTensor& x, | ||
const std::vector<int>& paddings, | ||
const Scalar& pad_value) { | ||
return PadInferSpmd(x, paddings, pad_value.to<int32_t>()); | ||
} | ||
|
||
SpmdInfo PadGradInferSpmdDynamic(const DistMetaTensor& out_grad, | ||
const std::vector<int>& paddings, | ||
const Scalar& pad_value) { | ||
return PadInferSpmd(out_grad, paddings, pad_value.to<int32_t>()); | ||
} | ||
|
||
} // namespace distributed | ||
} // namespace phi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* Copyright (c) 2024 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/common/scalar.h" | ||
#include "paddle/phi/core/distributed/auto_parallel/dist_meta_tensor.h" | ||
#include "paddle/phi/core/distributed/type_defs.h" | ||
|
||
namespace phi { | ||
namespace distributed { | ||
|
||
SpmdInfo PadInferSpmd(const DistMetaTensor& x, | ||
const std::vector<int>& paddings, | ||
int pad_value); | ||
|
||
SpmdInfo PadGradInferSpmd(const DistMetaTensor& x, | ||
const DistMetaTensor& out, | ||
const std::vector<int>& paddings, | ||
int pad_value); | ||
|
||
SpmdInfo PadInferSpmdDynamic(const DistMetaTensor& x, | ||
const std::vector<int>& paddings, | ||
const Scalar& pad_value); | ||
|
||
SpmdInfo PadGradInferSpmdDynamic(const DistMetaTensor& out_grad, | ||
const std::vector<int>& paddings, | ||
const Scalar& pad_value); | ||
} // namespace distributed | ||
} // namespace phi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Copyright (c) 2024 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. | ||
|
||
import unittest | ||
|
||
from paddle.distributed.auto_parallel.static.dist_attribute import ( | ||
DistTensorSpec, | ||
TensorDistAttr, | ||
) | ||
from paddle.distributed.fleet import auto | ||
from paddle.framework import core | ||
|
||
|
||
class TestPadSPMDRule(unittest.TestCase): | ||
def setUp(self): | ||
self.process_mesh = auto.ProcessMesh(mesh=[[0, 1, 2, 3], [4, 5, 6, 7]]) | ||
self.shapes = [[8, 16, 16]] | ||
self.dim_mappings = [[0, 1, -1]] | ||
self.paddings = [0, 0, 0, 1, 2, 3] | ||
|
||
def build_inputs(self): | ||
inputs = [] | ||
for shape, dim_mapping in zip(self.shapes, self.dim_mappings): | ||
tensor_dist_attr = TensorDistAttr() | ||
tensor_dist_attr.dims_mapping = dim_mapping | ||
tensor_dist_attr.process_mesh = self.process_mesh | ||
inputs.append(DistTensorSpec(shape, tensor_dist_attr)) | ||
return inputs | ||
|
||
def test_infer_forward(self): | ||
inputs = self.build_inputs() | ||
rule = core.get_phi_spmd_rule("pad") | ||
infered_dist_attrs = rule.infer_forward(inputs, self.paddings, 0) | ||
|
||
infered_output_dist_attrs = infered_dist_attrs[1] | ||
self.assertEqual(len(infered_output_dist_attrs), 1) | ||
self.assertEqual(infered_output_dist_attrs[0].dims_mapping, [0, -1, -1]) | ||
|
||
def test_infer_backward(self): | ||
inputs = self.build_inputs() | ||
rule = core.get_phi_spmd_rule("pad") | ||
infered_dist_attrs = rule.infer_backward( | ||
inputs, inputs, self.paddings, 0 | ||
) | ||
|
||
infered_input_dist_attrs = infered_dist_attrs[0] | ||
self.assertEqual(len(infered_input_dist_attrs), 1) | ||
self.assertEqual(infered_input_dist_attrs[0].dims_mapping, [0, -1, -1]) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters