Skip to content

[fsdp] fix: set _set_allocator_settings to True to avoid fsdp2 oom - #3020

Merged
chenhaiq merged 7 commits into
verl-project:mainfrom
chenhaiq:fix_fsdp2_memory
Aug 13, 2025
Merged

chenhaiq merged 7 commits into
verl-project:mainfrom
chenhaiq:fix_fsdp2_memory

Conversation

@chenhaiq

Copy link
Copy Markdown
Collaborator

What does this PR do?

Enable expandable_segments to avoid the increasing memory fragmentation caused by temporary variables during the training process of fsdp2, which may trigger probabilistic out-of-memory (OOM) errors.

Since both sglang and vllm can not work with expandable_segments:True, it has to be turn off during rollout.

Checklist Before Starting

  • Search for similar PRs. Paste at least one query link here: https://github.com/volcengine/verl/pulls?q=is%3Apr+is%3Aopen+fsdp2+
  • Format the PR title as [{modules}] {type}: {description} (This will be checked by the CI)
    • {modules} include fsdp, megatron, sglang, vllm, rollout, trainer, ci, training_utils, recipe, hardware, deployment, ray, worker, single_controller, misc, perf, model, algo, env, tool, ckpt, doc, data
    • If this PR involves multiple modules, separate them with , like [megatron, fsdp, doc]
    • {type} is in feat, fix, refactor, chore, test
    • If this PR breaks any API (CLI arguments, config, function signature, etc.), add [BREAKING] to the beginning of the title.
    • Example: [BREAKING][fsdp, megatron] feat: dynamic batching

Test

Without this fix, memory reserved could be very high after compute_log_prob or update_actor.

(WorkerDict pid=339320) [2025-08-11 17:43:01] dp actor After compute_log_prob, memory allocated (GB): 5.53, memory reserved (GB): 73.59, device memory used/total (GB): 77.47/79.15

With this fix, it stays low during training.

(WorkerDict pid=396879) [2025-08-12 07:39:42] dp actor After compute_log_prob, memory allocated (GB): 4.95, memory reserved (GB): 14.20, device memory used/total (GB): 17.72/79.15

API and Usage Example

Demonstrate how the API changes if any, and provide usage example(s) if possible.

# Add code snippet or script demonstrating how to use this

Design & Code Changes

Demonstrate the high-level design if this PR is complex, and list the specific changes.

Checklist Before Submitting

Important

Please check all the following items before requesting a review, otherwise the reviewer might deprioritize this PR for review.


Co-authored-by: Lu Hongyu luhongyu.4869@bytedance.com"

Co-authored-by: Lu Hongyu <luhongyu.4869@bytedance.com>"
@chenhaiq
chenhaiq requested a review from vermouth1992 August 12, 2025 07:56

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses a probabilistic out-of-memory (OOM) error with FSDP2 by dynamically toggling the expandable_segments setting in PyTorch's CUDA memory allocator. The changes correctly disable this setting for sglang and vllm during rollout and re-enable it for FSDP2 training.

My review focuses on the use of the private PyTorch function _set_allocator_settings. While this seems to be the only way to achieve the desired dynamic behavior, it introduces a maintainability risk as private APIs can change without notice. I've suggested wrapping these calls in try-except blocks to make the code more robust against future PyTorch updates and improve the comments to clarify why this approach is taken.

@zhaochenyang20

Copy link
Copy Markdown
Collaborator

@chenhaiq could you add @narutolhy as a co-author? He contributed the OOM scripts.

Comment thread verl/utils/device.py Resolved
@weifengpy

weifengpy commented Aug 13, 2025 •

Copy link
Copy Markdown
Contributor

thanks for the fix! for fsdp2, becuase we allocate many small tensors (model.parameters() sharded by dim-i), we almost should always use torch.cuda.memory._set_allocator_settings(f"expandable_segments:true")

with this PR, the outstanding fragmentation for fsdp2 is only inter-stream fragmentation: 1) all-gather stream has its own memory pool, 2) default stream has its own memory pool. This fragmentation is much less pronouced than expandable_segments. But in case we want to eliminate inter-stream fragmentation, it's possible to call model._set_unshard_async_op(unshard_async_op) and call layer.unshard() manually to to allocate all-gather buffer in the default stream at the cost of less overlapping:

https://github.com/pytorch/pytorch/blob/8d1cf529229dce7cd5ea04abb0faac83b87ca6d1/test/distributed/_composable/fsdp/test_fully_shard_training.py#L447-L448

Screenshot 2025-08-12 at 17 53 18

@chenhaiq chenhaiq closed this Aug 13, 2025
@chenhaiq

Copy link
Copy Markdown
Collaborator Author

@chenhaiq could you add @narutolhy as a co-author? He contributed the OOM scripts.

sure.

@chenhaiq chenhaiq reopened this Aug 13, 2025
@vermouth1992

Copy link
Copy Markdown
Collaborator

@chenhaiq could you add @narutolhy as a co-author? He contributed the OOM scripts.

sure.

I guess we can skip CI. How can we add @narutolhy as a co-author?

@chenhaiq
chenhaiq enabled auto-merge (squash) August 13, 2025 02:56
@chenhaiq

Copy link
Copy Markdown
Collaborator Author

I guess we can skip CI. How can we add @narutolhy as a co-author?

I have updated commit message with all co-authors. Please merge it.

@chenhaiq
chenhaiq merged commit e6843cc into verl-project:main Aug 13, 2025
yellowbee686 pushed a commit to yellowbee686/verl that referenced this pull request Aug 15, 2025
…erl-project#3020)

### What does this PR do?

Enable expandable_segments to avoid the increasing memory fragmentation
caused by temporary variables during the training process of fsdp2,
which may trigger probabilistic out-of-memory (OOM) errors.

Since both sglang and vllm can not work with expandable_segments:True,
it has to be turn off during rollout.


### Test

Without this fix, memory reserved could be very high after
compute_log_prob or update_actor.
```
(WorkerDict pid=339320) [2025-08-11 17:43:01] dp actor After compute_log_prob, memory allocated (GB): 5.53, memory reserved (GB): 73.59, device memory used/total (GB): 77.47/79.15
```

With this fix, it stays low during training.
```
(WorkerDict pid=396879) [2025-08-12 07:39:42] dp actor After compute_log_prob, memory allocated (GB): 4.95, memory reserved (GB): 14.20, device memory used/total (GB): 17.72/79.15
```

---------
Co-authored-by: narutolhy <luhongyu.4869@bytedance.com>"
Co-authored-by: Chi Zhang <zhangchi.usc1992@bytedance.com>
techkang pushed a commit to techkang/verl that referenced this pull request Aug 15, 2025
…erl-project#3020)

### What does this PR do?

Enable expandable_segments to avoid the increasing memory fragmentation
caused by temporary variables during the training process of fsdp2,
which may trigger probabilistic out-of-memory (OOM) errors.

Since both sglang and vllm can not work with expandable_segments:True,
it has to be turn off during rollout.


### Test

Without this fix, memory reserved could be very high after
compute_log_prob or update_actor.
```
(WorkerDict pid=339320) [2025-08-11 17:43:01] dp actor After compute_log_prob, memory allocated (GB): 5.53, memory reserved (GB): 73.59, device memory used/total (GB): 77.47/79.15
```

With this fix, it stays low during training.
```
(WorkerDict pid=396879) [2025-08-12 07:39:42] dp actor After compute_log_prob, memory allocated (GB): 4.95, memory reserved (GB): 14.20, device memory used/total (GB): 17.72/79.15
```

---------
Co-authored-by: narutolhy <luhongyu.4869@bytedance.com>"
Co-authored-by: Chi Zhang <zhangchi.usc1992@bytedance.com>
ChangyiYang pushed a commit to SwordFaith/verl that referenced this pull request Aug 16, 2025
…erl-project#3020)

### What does this PR do?

Enable expandable_segments to avoid the increasing memory fragmentation
caused by temporary variables during the training process of fsdp2,
which may trigger probabilistic out-of-memory (OOM) errors.

Since both sglang and vllm can not work with expandable_segments:True,
it has to be turn off during rollout.


### Test

Without this fix, memory reserved could be very high after
compute_log_prob or update_actor.
```
(WorkerDict pid=339320) [2025-08-11 17:43:01] dp actor After compute_log_prob, memory allocated (GB): 5.53, memory reserved (GB): 73.59, device memory used/total (GB): 77.47/79.15
```

With this fix, it stays low during training.
```
(WorkerDict pid=396879) [2025-08-12 07:39:42] dp actor After compute_log_prob, memory allocated (GB): 4.95, memory reserved (GB): 14.20, device memory used/total (GB): 17.72/79.15
```

---------
Co-authored-by: narutolhy <luhongyu.4869@bytedance.com>"
Co-authored-by: Chi Zhang <zhangchi.usc1992@bytedance.com>
@Edenzzzz

Copy link
Copy Markdown

Wonder why SGLang can't use expandable segments?

@zhaochenyang20

Copy link
Copy Markdown
Collaborator

Wonder why SGLang can't use expandable segments?

Torch memory savor can not use it. So we turn on in several stages.

whatadayG pushed a commit to whatadayG/verl that referenced this pull request Sep 5, 2025
…erl-project#3020)

### What does this PR do?

Enable expandable_segments to avoid the increasing memory fragmentation
caused by temporary variables during the training process of fsdp2,
which may trigger probabilistic out-of-memory (OOM) errors.

Since both sglang and vllm can not work with expandable_segments:True,
it has to be turn off during rollout.


### Test

Without this fix, memory reserved could be very high after
compute_log_prob or update_actor.
```
(WorkerDict pid=339320) [2025-08-11 17:43:01] dp actor After compute_log_prob, memory allocated (GB): 5.53, memory reserved (GB): 73.59, device memory used/total (GB): 77.47/79.15
```

With this fix, it stays low during training.
```
(WorkerDict pid=396879) [2025-08-12 07:39:42] dp actor After compute_log_prob, memory allocated (GB): 4.95, memory reserved (GB): 14.20, device memory used/total (GB): 17.72/79.15
```

---------
Co-authored-by: narutolhy <luhongyu.4869@bytedance.com>"
Co-authored-by: Chi Zhang <zhangchi.usc1992@bytedance.com>
techkang pushed a commit to techkang/verl that referenced this pull request Oct 31, 2025
…erl-project#3020)

### What does this PR do?

Enable expandable_segments to avoid the increasing memory fragmentation
caused by temporary variables during the training process of fsdp2,
which may trigger probabilistic out-of-memory (OOM) errors.

Since both sglang and vllm can not work with expandable_segments:True,
it has to be turn off during rollout.


### Test

Without this fix, memory reserved could be very high after
compute_log_prob or update_actor.
```
(WorkerDict pid=339320) [2025-08-11 17:43:01] dp actor After compute_log_prob, memory allocated (GB): 5.53, memory reserved (GB): 73.59, device memory used/total (GB): 77.47/79.15
```

With this fix, it stays low during training.
```
(WorkerDict pid=396879) [2025-08-12 07:39:42] dp actor After compute_log_prob, memory allocated (GB): 4.95, memory reserved (GB): 14.20, device memory used/total (GB): 17.72/79.15
```

---------
Co-authored-by: narutolhy <luhongyu.4869@bytedance.com>"
Co-authored-by: Chi Zhang <zhangchi.usc1992@bytedance.com>
chenjiaoAngel added a commit to chenjiaoAngel/verl that referenced this pull request Nov 14, 2025
…erl-project#3020)

### What does this PR do?

Enable expandable_segments to avoid the increasing memory fragmentation
caused by temporary variables during the training process of fsdp2,
which may trigger probabilistic out-of-memory (OOM) errors.

Since both sglang and vllm can not work with expandable_segments:True,
it has to be turn off during rollout.


### Test

Without this fix, memory reserved could be very high after
compute_log_prob or update_actor.
```
(WorkerDict pid=339320) [2025-08-11 17:43:01] dp actor After compute_log_prob, memory allocated (GB): 5.53, memory reserved (GB): 73.59, device memory used/total (GB): 77.47/79.15
```

With this fix, it stays low during training.
```
(WorkerDict pid=396879) [2025-08-12 07:39:42] dp actor After compute_log_prob, memory allocated (GB): 4.95, memory reserved (GB): 14.20, device memory used/total (GB): 17.72/79.15
```

---------
Co-authored-by: narutolhy <luhongyu.4869@bytedance.com>"
Co-authored-by: Chi Zhang <zhangchi.usc1992@bytedance.com>
TimurTaepov pushed a commit to giorgossideris/verl that referenced this pull request Dec 20, 2025
…erl-project#3020)

### What does this PR do?

Enable expandable_segments to avoid the increasing memory fragmentation
caused by temporary variables during the training process of fsdp2,
which may trigger probabilistic out-of-memory (OOM) errors.

Since both sglang and vllm can not work with expandable_segments:True,
it has to be turn off during rollout.


### Test

Without this fix, memory reserved could be very high after
compute_log_prob or update_actor.
```
(WorkerDict pid=339320) [2025-08-11 17:43:01] dp actor After compute_log_prob, memory allocated (GB): 5.53, memory reserved (GB): 73.59, device memory used/total (GB): 77.47/79.15
```

With this fix, it stays low during training.
```
(WorkerDict pid=396879) [2025-08-12 07:39:42] dp actor After compute_log_prob, memory allocated (GB): 4.95, memory reserved (GB): 14.20, device memory used/total (GB): 17.72/79.15
```

---------
Co-authored-by: narutolhy <luhongyu.4869@bytedance.com>"
Co-authored-by: Chi Zhang <zhangchi.usc1992@bytedance.com>
vyomakesh0728 added a commit to vyomakesh0728/verl that referenced this pull request Jan 22, 2026
…erl-project#3020)

### What does this PR do?

Enable expandable_segments to avoid the increasing memory fragmentation
caused by temporary variables during the training process of fsdp2,
which may trigger probabilistic out-of-memory (OOM) errors.

Since both sglang and vllm can not work with expandable_segments:True,
it has to be turn off during rollout.


### Test

Without this fix, memory reserved could be very high after
compute_log_prob or update_actor.
```
(WorkerDict pid=339320) [2025-08-11 17:43:01] dp actor After compute_log_prob, memory allocated (GB): 5.53, memory reserved (GB): 73.59, device memory used/total (GB): 77.47/79.15
```

With this fix, it stays low during training.
```
(WorkerDict pid=396879) [2025-08-12 07:39:42] dp actor After compute_log_prob, memory allocated (GB): 4.95, memory reserved (GB): 14.20, device memory used/total (GB): 17.72/79.15
```

---------
Co-authored-by: narutolhy <luhongyu.4869@bytedance.com>"
Co-authored-by: Chi Zhang <zhangchi.usc1992@bytedance.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants