-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
add spmd rule for amp ops #64202
Merged
zhiqiu
merged 10 commits into
PaddlePaddle:develop
from
zhiqiu:dev/add_spmd_for_amp_ops
May 24, 2024
Merged
add spmd rule for amp ops #64202
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a336333
add spmd for check_finite_and_unscale
zhiqiu 0b51436
add spmd for update_loss_scaling
zhiqiu 351a871
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zhiqiu e4026d0
fix partial
zhiqiu ac771ee
fix ut
zhiqiu b3828bf
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zhiqiu 970f561
fix spmd
zhiqiu 5474a05
fix custom_relu test
zhiqiu 200bf8c
fix hybrid test
zhiqiu b1cc300
fix ut
zhiqiu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// 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/amp_ops.h" | ||
|
||
#include <vector> | ||
#include "glog/logging.h" | ||
|
||
#include "paddle/phi/infermeta/spmd_rules/utils.h" | ||
|
||
namespace phi { | ||
namespace distributed { | ||
// TODO(zhiqiu): support xs on different mesh. | ||
SpmdInfo CheckFiniteAndUnscaleSpmd(const std::vector<DistMetaTensor>& xs, | ||
const DistMetaTensor& scale) { | ||
std::vector<TensorDistAttr> xs_attrs; | ||
paddle::flat_hash_map<int64_t, ReduceType> partial_on_dims; | ||
for (auto& x : xs) { | ||
auto dist_attr = x.dist_attr(); | ||
dist_attr.clean_partial_status(); | ||
xs_attrs.emplace_back(dist_attr); | ||
auto dims_mapping = dist_attr.dims_mapping(); | ||
for (auto& m : dims_mapping) { | ||
if (m != -1 && partial_on_dims.count(m) == 0) { | ||
partial_on_dims[m] = ReduceType::kRedMax; | ||
} | ||
} | ||
} | ||
TensorDistAttr found_infinite_attr = | ||
CopyTensorDistAttrForOutput(scale.dist_attr()); | ||
found_infinite_attr.set_partial_status(partial_on_dims); | ||
found_infinite_attr.set_dims_mapping({-1}); | ||
return {{xs_attrs, scale.dist_attr()}, {xs_attrs, found_infinite_attr}}; | ||
} | ||
|
||
SpmdInfo UpdateLossScalingSpmd(const std::vector<DistMetaTensor>& xs, | ||
const DistMetaTensor& found_infinite, | ||
const DistMetaTensor& prev_loss_scaling, | ||
const DistMetaTensor& in_good_steps, | ||
const DistMetaTensor& in_bad_steps, | ||
int incr_every_n_steps, | ||
int decr_every_n_nan_or_inf, | ||
float incr_ratio, | ||
float decr_ratio, | ||
Scalar stop_update) { | ||
std::vector<TensorDistAttr> xs_attrs; | ||
for (auto& x : xs) { | ||
auto dist_attr = x.dist_attr(); | ||
dist_attr.clean_partial_status(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the same as above |
||
xs_attrs.emplace_back(dist_attr); | ||
} | ||
TensorDistAttr found_infinite_attr = | ||
CopyTensorDistAttrForOutput(found_infinite.dist_attr()); | ||
return {{xs_attrs, | ||
found_infinite_attr, | ||
prev_loss_scaling.dist_attr(), | ||
in_good_steps.dist_attr(), | ||
in_bad_steps.dist_attr()}, | ||
{xs_attrs, | ||
found_infinite_attr, | ||
in_good_steps.dist_attr(), | ||
in_bad_steps.dist_attr()}}; | ||
} | ||
|
||
} // 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,39 @@ | ||
// 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 <vector> | ||
|
||
#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 CheckFiniteAndUnscaleSpmd(const std::vector<DistMetaTensor>& xs, | ||
const DistMetaTensor& scale); | ||
|
||
SpmdInfo UpdateLossScalingSpmd(const std::vector<DistMetaTensor>& xs, | ||
const DistMetaTensor& found_infinite, | ||
const DistMetaTensor& prev_loss_scaling, | ||
const DistMetaTensor& in_good_steps, | ||
const DistMetaTensor& in_bad_steps, | ||
int incr_every_n_steps, | ||
int decr_every_n_nan_or_inf, | ||
float incr_ratio, | ||
float decr_ratio, | ||
Scalar stop_update = false); | ||
} // 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could allow the partial status pass through to have better performance:
if not allow partial to get through, allreduce will be conducted before this operation, no matter checkfinite return False or True.
BUT if we allow the partial to get through, allreduce will be triggered by the later operation(e.g. adam) and if the checkfinite return True, not adam would be call and therefore not allreduce is conduct