perf(verl): reduce peak memory when decoding routed experts - #636
Open
Bozhen Peng (kiteretsu903) wants to merge 1 commit into
Open
Bozhen Peng (kiteretsu903) wants to merge 1 commit into
Bozhen Peng (kiteretsu903) wants to merge 1 commit into
Conversation
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
I noticed that R3 batch construction keeps every decoded routing array in memory while copying them into the final tensor. For MoE rollouts with long sequences, those temporary arrays can take up a lot of host memory.
This decodes one rollout at a time and releases its array after copying it into the batch. Padding, truncation and the output format stay the same.
In a local CPU benchmark with 64 synthetic rollouts, peak temporary decoding allocations fell from 126.7 MB to 4.7 MB, and process peak RSS fell from 660.3 MB to 543.4 MB. The final 128 MB tensor was the same.
Validation
I added focused tests for releasing each decoded array before loading the next one, padding and truncation with uint8 and int64 inputs, and rejecting routing data that's too short. The retention test fails on the original implementation; the compatibility tests pass on both.
Using Python 3.12 with the locked CPU dependencies:
python -m pytest -q tests/verl/test_rollout_adapter.py— 21 passed.python -m pytest -q --durations=10 tests— 142 passed.ruff check .,ruff format --check .,python scripts/check_headers.py, andpre-commit run --all-files --show-diff-on-failure— passed.pyright --venvpath /home/admin1/os-contributions/agent-lightning-research— no errors or warnings.uv build --no-sources --out-dir /mnt/d/Documents/os-contributions/verification/agent-lightning-r3/dist— passed; verified the updated module in both archives.