-
Notifications
You must be signed in to change notification settings - Fork 2.1k
【GLM】subbatch performance and weight bug fix #2661
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
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8d84b93
add ep subbatch to reduce memory
danleifeng b79596d
fix
danleifeng c851a4e
add tensorwise_offload_optimizer and pre_alloc_memory
danleifeng efef08e
fix saveload bug;
danleifeng a05b963
remove offload optimizer and useless code
danleifeng 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -2788,7 +2788,7 @@ def _save_checkpoint(self, model, metrics=None): | |
optimizer_name = _add_variant(PADDLE_OPTIMIZER_NAME, self.args.optimizer_name_suffix) | ||
saved_signal_path = os.path.join(output_dir, f"saved_signal_{dist.get_rank()}") | ||
|
||
if self.args.unified_checkpoint and self.args.offload_optim: | ||
if self.args.unified_checkpoint and (self.args.offload_optim or self.args.tensorwise_offload_optimizer): | ||
|
||
self._reload_optimizer() | ||
|
||
if self.args.use_hybrid_parallel: | ||
|
This file contains hidden or 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 hidden or 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,81 @@ | ||
# Copyright (c) 2025 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 paddle | ||
from paddle import _C_ops | ||
from paddle.distributed.fleet.meta_optimizers.dygraph_optimizer.hybrid_parallel_optimizer import ( | ||
HybridParallelOptimizer, | ||
) | ||
from paddle.optimizer import Optimizer | ||
|
||
from .sharding_io import to_device | ||
|
||
|
||
def offload(tensor): | ||
if paddle.is_compiled_with_cuda(): | ||
place = paddle.CUDAPinnedPlace() | ||
else: | ||
place = paddle.CPUPlace() | ||
|
||
new_tensor = to_device(tensor, place) | ||
assert new_tensor is tensor, "to_device must be inplace operation" | ||
|
||
|
||
def reload(tensor): | ||
new_tensor = to_device(tensor) | ||
assert new_tensor is tensor, "to_device must be inplace operation" | ||
|
||
|
||
def hack_offload_optimizer(): | ||
# Step 1: mock _add_accumulator | ||
origin_add_accumulator = getattr(Optimizer, "_add_accumulator") | ||
|
||
def new_add_accumulator(self, *args, **kwargs): | ||
x = origin_add_accumulator(self, *args, **kwargs) | ||
offload(x) | ||
return x | ||
|
||
setattr(Optimizer, "_add_accumulator", new_add_accumulator) | ||
|
||
# Step 2: mock _C_ops.adamw_ and _C_ops.adamw | ||
for name in ["adam_", "adamw_"]: | ||
origin_op = getattr(_C_ops, name) | ||
|
||
def new_opt_op(*args): | ||
for arg in args: | ||
if isinstance(arg, paddle.Tensor): | ||
reload(arg) | ||
|
||
ret = origin_op(*args) | ||
|
||
for i, arg in enumerate(args): | ||
if i >= 2 and isinstance(arg, paddle.Tensor): # do not offload parameter and gradient | ||
offload(arg) | ||
return ret | ||
|
||
setattr(_C_ops, name, new_opt_op) | ||
|
||
# Step 3: mock _insert_sync | ||
opt_type = HybridParallelOptimizer | ||
origin_insert_sync = getattr(opt_type, "_insert_sync") | ||
|
||
def new_insert_sync(self, sync_var, *args, **kwargs): | ||
origin_place = sync_var.place | ||
reload(sync_var) | ||
ret = origin_insert_sync(self, sync_var, *args, **kwargs) | ||
new_sync_var = to_device(sync_var, origin_place) | ||
assert new_sync_var is sync_var, "to_device must be inplace operation" | ||
return ret | ||
|
||
setattr(opt_type, "_insert_sync", new_insert_sync) |
This file contains hidden or 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.
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.
unified_checkpoint_config: ignore_merge_optimizer
optim: adamw_custom
tensorwise_offload_optimizer: True
训练添加上述也可以做到offload optimizer降低显存,暂时先把optimizer相关修改删除,这个PR可以先合一版
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.
已删