[Fix] Transfer routed experts release ownership - #2064
Open
matrix72c wants to merge 1 commit into
Open
Conversation
matrix72c
force-pushed
the
fix/routed-experts-release-ownership
branch
2 times, most recently
from
September 7, 2026 03:52
47f6556 to
1979eb8
Compare
matrix72c
force-pushed
the
fix/routed-experts-release-ownership
branch
from
September 7, 2026 05:54
1979eb8 to
b68b221
Compare
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.
Motivation
When
enable_return_routed_experts=True, an RL rollout can attach RayObjectRefs to aRolloutState. The same reference may either be created by the rollout worker or be borrowed from aTraceStoresession. Before this change,RolloutStatedid not record which component owned the reference, while several cleanup helpers unconditionally freed references. This made retryable stale samples unsafe and could invalidate references that were still held by theTraceStore.Failure chain before this PR
The problematic path is easiest to see for a retryable stale sample:
ReplayBufferand is selected for retry.reset_rollout_response()to keep the prompt and clear the generated response.reset_rollout_response()also callsfree_object_refs()for every routed-expertsObjectRefit sees. It cannot distinguish a locally created rollout reference from a borrowedTraceStorereference.TraceStoresample, theTriestill contains the same reference because the session has not been released yet. The reset therefore frees a shared object too early.Trie. The reference is already invalid, which can surface as an object-fetch failure or as routed-experts/sequence-length validation errors.There were two related versions of the same ownership bug:
TraceStorestill owned it.TraceStoresession alive.Finally, replacing a value in the
TraceStoretrie did not release routed-experts references from the overwritten value, which could leak Ray objects during reroll/overwrite workloads.What changed
RolloutState.routed_experts_ownerwith the values"rollout"and"trace_store".reset_rollout_response()a pure state reset: it clears response fields and routed-experts fields, but never callsray.free.release_owned_routed_experts()for explicit caller-owned release, and makediscard_rollout_state(..., release_refs=...)opt in to releasing resources.RolloutWorker, vLLM rollout parsing, and the VERL tool loop) and at everyTraceStoreexport boundary."rollout"references before reset; borrowed"trace_store"references are detached but remain valid until their session is released.RolloutWorker.generate()opts in for direct rollout inputs; the handler default is non-releasing for safe reuse by other callers.TraceStoresession release as the final release point for its references, detach released routes before generic discard, and ensure the trainer performs session release in afinallyblock.Ownership after this change
The LMDeploy generation protocol is unchanged. Ownership is established at XTuner's producer/export boundaries, and old checkpoints remain loadable because the new field defaults to
None.Impact
This prevents premature freeing of shared
TraceStorereferences, makes direct-rollout cleanup explicit, and closes exception/overwrite cleanup gaps without introducing a global reference registry or changing stale reroll/session semantics.Tests
The following targeted checks pass locally:
ruff checkfor all changed source and test filesruff format --checkfor all changed source and test filespython -m py_compilefor all changed source and test filesThe full replay-buffer suite was also inspected; an existing async save/resume test does not complete in this shared test environment, so it was not used as a passing signal.
Related issue
Related to #2025. This PR fixes routed-experts
ObjectReflifetime leaks and premature frees on the XTuner RL path. It does not, by itself, bound learner-side materialization or change sequence-parallel transfer order; those memory-footprint issues remain separate follow-up work. The LMDeploy endpoint and generation protocol are unchanged.