Skip to content

[Fix] Use Ray reference counting for routed-expert lifetime - #2263

Closed
matrix72c wants to merge 1 commit into
InternLM:mainfrom
matrix72c:fix/shared-routed-experts-lifetime
Closed

matrix72c wants to merge 1 commit into
InternLM:mainfrom
matrix72c:fix/shared-routed-experts-lifetime

Conversation

@matrix72c

@matrix72c matrix72c commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Independently based on main at c93eb15dc0b1e4fd371b33d79bec9e1321fe20f2, after the PPO stack merged. This preserves the fix from #2243, which was closed without merging when stack/rl-ppo was deleted.

Root Cause

Routed-expert Ray ObjectRefs are shared by partial rollouts, trace sessions, sibling samples, and repeated training passes. Explicitly freeing one holder's shared object invalidates other consumers. TrainBatch ownership does not make trainer/trace-store force-free safe.

Fix

  • Retire routed-expert holders through Ray reference counting in reset/discard, partial concatenation, session release, and trainer cleanup.
  • Preserve TrainBatch materialization/release ownership and existing explicit pixel-payload cleanup.
  • Reject serialized ObjectRefs in vLLM HTTP responses; array payload support remains. Out-of-band serialized refs cannot use this lifetime contract.

ProduceBatchResult impact: not affected; sample status, reward, and batch accounting are unchanged.

RoutedExperts impact: after transfer from LMDeploy, XTuner drops only each retiring holder; shared data remains readable until the final holder retires. Pre-transfer LMDeploy cleanup is unchanged.

Ray concurrency impact: dispatch groups and scheduling are unchanged; no blocking actor RPC or new concurrency lane.

Test Plan — main-based head

  • 100 passed: tests/rl/test_routed_experts_lifetime.py, test_trace_store.py, test_train_batch_contract.py, and test_rollout_logic.py in the development container.
  • Includes real local Ray ownership, competing consumers, TrainBatch materialization/repeated passes/release, final-reference retirement, and vLLM response handling.
  • Required pre-commit run --all-files executed: mypy passed on 430 source files; overall failure remains due to upstream autotest formatting/import-order violations. Unrelated formatter edits were not retained.
  • No GPU/NCCL training or live inference-service verification on this head. Target-environment validation and clean CI remain outstanding.
  • No throughput benchmark: local tests establish lifetime/final reclamation, not multi-node throughput or GPU memory peaks.

Retire routed-reference holders without forcing shared Ray objects free; preserve pixel cleanup and reject out-of-band serialized vLLM ObjectRefs. Exercise actual TrainBatch materialization and release across remote consumers, repeated passes and final holder retirement.
@matrix72c

Copy link
Copy Markdown
Contributor Author

2026-11-09 new-main compatibility check

Validated this PR independently against fixed upstream main d841a3feeba4565054ad773fce5335a4734def88, using PR head 6d2cb62fdafbb7f91f8baf37ffde351ac1d588fb. The PR head/history remains unchanged; the test-only local composition commit was 2f8b085849654d4ed53d79299de7ba13b7e982e4 (not another topic or the final integration).

Focused targets:

  • tests/rl/test_routed_experts_lifetime.py
  • tests/rl/test_rollout_logic.py
  • tests/rl/test_trace_store.py
  • tests/rl/test_train_batch_contract.py

Result: 100 passed, pytest exit 0. Python 3.12.12, Torch 2.10.0, Ray 2.56.1, Transformers 5.17.0, existing LMDeploy 0.18.0 environment. This does not validate main's all-extra Ray 2.58.0 environment.

ProduceBatchResult impact: batch lifecycle and ref-sharing tests pass; no new field changes in this refresh. RoutedExperts impact: checks exercise shared XTuner ref ownership and cleanup after transfer; they do not establish live LMDeploy transfer behavior. Ray concurrency impact: local tests are not cluster load validation.

These are targeted CPU/local-runtime checks, not a full-suite, GPU/PPO performance or CI-green claim. The unit-test check for this head was cancelled at the status inspection; cancellation is not a pass. Existing draft/readiness status is unchanged.

@matrix72c
matrix72c marked this pull request as ready for review November 9, 2026 09:46
@matrix72c

Copy link
Copy Markdown
Contributor Author

Closing: the shared routed-expert lifetime problem this PR addressed is now solved on main by #2297 (routed_experts_shared: shared refs are detached instead of force-freed, and trace-store / trainer cleanup skip them). The two approaches conflict, so this PR is superseded. The remaining small piece (rejecting out-of-band serialized ObjectRefs in vLLM HTTP responses) can be proposed separately if still wanted.

@matrix72c matrix72c closed this Nov 23, 2026
@matrix72c
matrix72c deleted the fix/shared-routed-experts-lifetime branch November 23, 2026 03:50
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.

1 participant