-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
[HybridParallel]Support fp16 in dygraph hybrid parallel #36420
Merged
ForFishes
merged 10 commits into
PaddlePaddle:develop
from
haohongxiang:pure_fp16_for_hp
Oct 18, 2021
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
37c6e64
[HybridParallel]Support fp16 in dygraph hybrid parallel
haohongxiang 285eff3
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
haohongxiang 6a637a3
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
haohongxiang 2cf4707
update
haohongxiang dfdc02e
update
haohongxiang 6177ba1
update for recompute
haohongxiang 190b15a
add unittest of pp+fp16
haohongxiang fc70624
add unittest of recompute+fp16
haohongxiang 36acffc
update
haohongxiang c454e00
modify ut
haohongxiang 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
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
138 changes: 138 additions & 0 deletions
138
python/paddle/fluid/tests/unittests/hybrid_parallel_pp_fp16.py
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,138 @@ | ||
# Copyright (c) 2021 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. | ||
|
||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
import unittest | ||
import paddle | ||
import numpy as np | ||
import random | ||
import paddle | ||
import paddle.distributed as dist | ||
import paddle.distributed.fleet as fleet | ||
from hybrid_parallel_pp_layer import AlexNetPipeDesc, AlexNet | ||
|
||
|
||
def set_random_seed(seed, dp_id, rank_id): | ||
"""Set random seed for reproducability.""" | ||
random.seed(seed) | ||
np.random.seed(seed + dp_id) | ||
paddle.seed(seed + dp_id) | ||
|
||
|
||
batch_size = 4 | ||
micro_batch_size = 2 | ||
|
||
|
||
class TestDistPPTraning(unittest.TestCase): | ||
def setUp(self): | ||
strategy = fleet.DistributedStrategy() | ||
self.model_parallel_size = 1 | ||
self.data_parallel_size = 1 | ||
self.pipeline_parallel_size = 2 | ||
strategy.hybrid_configs = { | ||
"dp_degree": self.data_parallel_size, | ||
"mp_degree": self.model_parallel_size, | ||
"pp_degree": self.pipeline_parallel_size, | ||
} | ||
strategy.pipeline_configs = { | ||
"accumulate_steps": batch_size // micro_batch_size, | ||
"micro_batch_size": micro_batch_size | ||
} | ||
fleet.init(is_collective=True, strategy=strategy) | ||
|
||
def test_pp_model(self): | ||
hcg = fleet.get_hybrid_communicate_group() | ||
word_size = hcg.get_model_parallel_world_size() | ||
dp_id = hcg.get_data_parallel_rank() | ||
pp_id = hcg.get_stage_id() | ||
rank_id = dist.get_rank() | ||
set_random_seed(1024, dp_id, rank_id) | ||
|
||
#construct model a | ||
model_a = AlexNet(10) | ||
scheduler_a = paddle.optimizer.lr.PiecewiseDecay( | ||
boundaries=[2], values=[0.001, 0.002], verbose=True) | ||
optimizer_a = paddle.optimizer.SGD(learning_rate=scheduler_a, | ||
parameters=model_a.parameters()) | ||
|
||
scaler_a = paddle.amp.GradScaler(init_loss_scaling=2**5) | ||
|
||
# construct model b | ||
model_b = AlexNetPipeDesc(num_stages=self.pipeline_parallel_size) | ||
scheduler_b = paddle.optimizer.lr.PiecewiseDecay( | ||
boundaries=[2], values=[0.001, 0.002], verbose=True) | ||
optimizer_b = paddle.optimizer.SGD(learning_rate=scheduler_b, | ||
parameters=model_b.parameters()) | ||
|
||
param_len = len(model_a.parameters()) | ||
parameters = [] | ||
for param in model_a.parameters(): | ||
parameters.append(param.numpy()) | ||
|
||
for idx, param in enumerate(model_b.parameters()): | ||
param.set_value(parameters[idx + pp_id * (param_len // 2)]) | ||
|
||
model_a, optimizer_a = paddle.amp.decorate( | ||
models=model_a, | ||
optimizers=optimizer_a, | ||
level='O2', | ||
save_dtype='float32') | ||
model_b, optimizer_b = paddle.amp.decorate( | ||
models=model_b, | ||
optimizers=optimizer_b, | ||
level='O2', | ||
save_dtype='float32') | ||
|
||
model_b = fleet.distributed_model(model_b) | ||
optimizer_b = fleet.distributed_optimizer(optimizer_b) | ||
scaler_b = paddle.amp.GradScaler(init_loss_scaling=2**5) | ||
scaler_b = fleet.distributed_scaler(scaler_b) | ||
|
||
# construct reader | ||
train_reader = paddle.batch( | ||
paddle.dataset.mnist.train(), batch_size=batch_size, drop_last=True) | ||
|
||
for step_id, data in enumerate(train_reader()): | ||
x_data = np.array([x[0] for x in data]).astype('float32').reshape( | ||
batch_size, 1, 28, 28) | ||
y_data = np.array([x[1] for x in data]).astype('int64').reshape( | ||
batch_size, 1) | ||
img = paddle.to_tensor(x_data) | ||
label = paddle.to_tensor(y_data) | ||
img.stop_gradient = True | ||
label.stop_gradient = True | ||
|
||
if step_id >= 5: | ||
return True | ||
|
||
with paddle.amp.auto_cast(enable=True, level='O2'): | ||
loss_a = model_a(img, label) | ||
scaler_a.scale(loss_a).backward() | ||
with paddle.amp.auto_cast(enable=False): | ||
scaler_a.minimize(optimizer_a, loss_a) | ||
optimizer_a.clear_grad() | ||
scheduler_a.step() | ||
|
||
loss_b = model_b.train_batch( | ||
[img, label], optimizer_b, scheduler_b, scaler=scaler_b) | ||
|
||
print("loss: ", loss_a.numpy(), loss_b.numpy()) | ||
np.testing.assert_allclose( | ||
loss_a.numpy(), loss_b.numpy(), rtol=5e-3) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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.
It is ok to put guard here, but I wonder if it is needed.
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.
Removing guard will cause diff of precision while broadcasting train_loss. So it is necessary to put guard here.