Skip to content

Commit 09c543a

Browse files
author
Yikai Zhao
committed
chore: convert device_uuid and zmq_ctx to cached_property
1 parent 63b782d commit 09c543a

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

checkpoint_engine/worker.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import gc
22
import traceback
33
from collections.abc import Callable
4+
from functools import cached_property
45
from typing import TypedDict
56

67
import torch
@@ -117,6 +118,21 @@ class VllmColocateWorkerExtension:
117118
`worker_extension_cls` argument when initializing the vLLM worker.
118119
"""
119120

121+
@cached_property
122+
def _device_uuid(self) -> str:
123+
from vllm.platforms import current_platform
124+
125+
if current_platform.device_type == "cuda":
126+
return current_platform.get_device_uuid(self.device.index)
127+
elif current_platform.device_type == "npu":
128+
return f"NPU-{npu_generate_uuid()}"
129+
else:
130+
raise ValueError(f"Unsupported device type: {current_platform.device_type}")
131+
132+
@cached_property
133+
def _zmq_ctx(self) -> zmq.Context:
134+
return zmq.Context()
135+
120136
def update_weights_from_ipc(self, zmq_handles: dict[str, str]):
121137
"""
122138
Update model weights from checkpoint-engine via IPC communication.
@@ -149,16 +165,6 @@ def update_weights_from_ipc(self, zmq_handles: dict[str, str]):
149165
if current_platform.device_type == "npu" and self.device is None:
150166
self.device = torch.device(f"npu:{self.local_rank}")
151167
assert self.device is not None
152-
if not hasattr(self, "_zmq_ctx") or self._zmq_ctx is None:
153-
self._zmq_ctx = zmq.Context()
154-
155-
if not hasattr(self, "_device_uuid") or self._device_uuid is None:
156-
if current_platform.device_type == "cuda":
157-
self._device_uuid = current_platform.get_device_uuid(self.device.index)
158-
elif current_platform.device_type == "npu":
159-
self._device_uuid = f"NPU-{npu_generate_uuid()}"
160-
else:
161-
raise ValueError(f"Unsupported device type: {current_platform.device_type}")
162168

163169
update_weights_from_ipc(
164170
self._zmq_ctx,

0 commit comments

Comments
 (0)