Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR targets Ray-related memory leaks in the PyTorch engine’s distributed (Ray) execution path by changing how input ObjectRefs are handled and introducing a Ray actor–backed store for transferring large routed_experts outputs across process boundaries.
Changes:
- Replace
ray.dagexecution with direct per-workerforward_async.remote(...)calls to avoid DAG-retained input ObjectRefs. - Add a detached named Ray actor (
SharedStore) to own/stagerouted_expertsand return an opaque key instead of embedding an ObjectRef in the response.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
lmdeploy/pytorch/engine/executor/ray_executor.py |
Switches async forward dispatch from dag.execute to direct remote calls, aiming to prevent input ObjectRef retention. |
lmdeploy/pytorch/engine/engine_instance.py |
Adds a Ray actor-based shared store and changes routed_experts extra output to return a store key when Ray transfer is enabled. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| return key | ||
|
|
||
| def get(self, key): | ||
| import ray |
Comment on lines
141
to
+145
| if routed_experts is not None and resp.type in [ResponseType.FINISH, ResponseType.CANCEL]: | ||
| if self._enable_transfer_obj_ref: | ||
| import pybase64 | ||
| import ray | ||
|
|
||
| ref = ray.put(routed_experts) | ||
| data = ray.cloudpickle.dumps(ref) | ||
| outputs['routed_experts'] = pybase64.b64encode(data).decode('utf-8') | ||
| key = ray.get(_SHARED_STORE.put.remote(routed_experts)) | ||
| outputs['routed_experts'] = key |
Comment on lines
+94
to
+95
| if len(all_data) > 0: | ||
| ray.internal.free(all_data, local_only=False) |
| _SHARED_STORE = ray.remote(num_cpus=0,)(SharedStore).options( | ||
| name=name, | ||
| namespace='lmdeploy', | ||
| lifetime='detached', |
Comment on lines
+109
to
+113
| _SHARED_STORE = ray.remote(num_cpus=0,)(SharedStore).options( | ||
| name=name, | ||
| namespace='lmdeploy', | ||
| lifetime='detached', | ||
| ).remote() |
Comment on lines
143
to
+145
| import ray | ||
|
|
||
| ref = ray.put(routed_experts) | ||
| data = ray.cloudpickle.dumps(ref) | ||
| outputs['routed_experts'] = pybase64.b64encode(data).decode('utf-8') | ||
| key = ray.get(_SHARED_STORE.put.remote(routed_experts)) | ||
| outputs['routed_experts'] = key |
Comment on lines
508
to
+512
| self._prev_inputs = ray.put(inputs) | ||
| # make sure in order | ||
| self._prev_out = self.dag.execute(self._prev_inputs) | ||
| # non-compiled dag would add input object ref, and the ref can not be released in python | ||
| self._prev_out = [ | ||
| worker.forward_async.remote(self._prev_inputs) for worker in self.workers | ||
| ] |
lvhan028
approved these changes
Apr 10, 2026
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.
RayEngineWorker might keep the stream output if the stream is cancelled or breaked before finish.