-
Notifications
You must be signed in to change notification settings - Fork 2.4k
[fsdp, sglang] fix: Using Agreesive Empty Cache instead #3136
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
wuxibin89
merged 6 commits into
volcengine:main
from
zhaochenyang20:aggressive_empty_cache
Aug 20, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
993c3d8
Merge branch 'main' of https://github.com/zhaochenyang20/verl
zhaochen20 557a72c
Merge branch 'main' of https://github.com/zhaochenyang20/verl
zhaochen20 47f0f71
use aggressive_empty_cache
zhaochen20 60e5ecb
fix lint
zhaochen20 2101c72
in megatorn
zhaochen20 be59f7f
use fsdp2
zhaochen20 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
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 |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |
| from verl.protocol import all_gather_data_proto | ||
| from verl.utils.device import get_device_id, get_torch_device, set_expandable_segments | ||
| from verl.utils.fsdp_utils import fsdp_version, load_fsdp_model_to_gpu, offload_fsdp_model_to_cpu | ||
| from verl.utils.memory_utils import aggressive_empty_cache | ||
| from verl.utils.model import convert_weight_keys | ||
| from verl.utils.profiler import GPUMemoryLogger, log_gpu_memory_usage, simple_timer | ||
| from verl.utils.torch_functional import check_device_is_available | ||
|
|
@@ -124,7 +125,7 @@ async def release_memory(self): | |
|
|
||
| @GPUMemoryLogger(role="FSDPSGLangShardingManager enter", logger=logger) | ||
| async def wake_up(self): | ||
| get_torch_device().empty_cache() | ||
| aggressive_empty_cache(force_sync=True) | ||
|
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. Should we also modify 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. Let me do it |
||
|
|
||
| log_gpu_memory_usage("Before state_dict() in sharding manager memory", logger=logger) | ||
| if self.offload_param: | ||
|
|
@@ -146,6 +147,8 @@ async def wake_up(self): | |
|
|
||
| # sglang need to set _set_allocator_settings to False | ||
| logger.debug("fsdp sglang sharding_manager _set_allocator_settings to False") | ||
| # Note(chenyang): SGLang is using torch memory pool to manage memory | ||
| # which is incompatible with expandable segments | ||
| set_expandable_segments(False) | ||
|
|
||
| if self.device_mesh["infer_tp"].get_local_rank() == 0 and self.rollout_config.free_cache_engine: | ||
|
|
@@ -161,7 +164,7 @@ async def wake_up(self): | |
| log_gpu_memory_usage("After sync model weights in sharding manager", logger=logger) | ||
|
|
||
| del params | ||
| get_torch_device().empty_cache() | ||
| aggressive_empty_cache(force_sync=True) | ||
| log_gpu_memory_usage("After del state_dict and empty_cache in sharding manager", logger=logger) | ||
|
|
||
| if ( | ||
|
|
@@ -187,7 +190,7 @@ async def sleep(self): | |
| self.module.train() | ||
|
|
||
| # add empty cache after each compute | ||
| get_torch_device().empty_cache() | ||
| aggressive_empty_cache(force_sync=True) | ||
|
|
||
| # always set _set_allocator_settings to True when using sglang | ||
| # it is required by fsdp2 to avoid oom | ||
|
|
||
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.
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.
While using a more aggressive cache clearing function is a good approach to mitigate OOM errors, the implementation of
aggressive_empty_cacheinverl/utils/memory_utils.pyappears to have a potential flaw in its loop termination logic.The loop terminates if the amount of freed reserved memory in an iteration is less than 1GB (
reserved_freed < 1024**3). This has two potential drawbacks:max_retriestimes. This could introduce unnecessary latency if a single pass was sufficient.A more robust approach would be to continue looping as long as a significant amount of memory is being freed, and stop only when an iteration frees little to no memory. This would make the function both more effective at preventing OOMs and more efficient.
Since
aggressive_empty_cacheis a new core utility for this fix, it would be beneficial to refine its logic to be more reliable before merging.