-
Notifications
You must be signed in to change notification settings - Fork 102
[XPU] Support checkpoint-engine weight update on Intel XPU #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
siju-samuel
wants to merge
2
commits into
MoonshotAI:main
Choose a base branch
from
siju-samuel:feat/xpu-support-phase1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| """Pluggable device-buffer handoff between the ParameterServer and the worker. | ||
|
|
||
| The broadcast path shares a device buffer with the colocated worker. CUDA/NPU use | ||
| :class:`TorchIpcHandler` (``torch.multiprocessing`` CUDA IPC, wire-format | ||
| unchanged); XPU uses :class:`XpuIpcHandler` (native SYCL ``ipc_memory``). | ||
| The handle is always a picklable, self-contained value, so the producer's | ||
| ``export`` -> ZMQ ``send_pyobj`` -> consumer ``attach`` flow is identical for both; | ||
| each side calls ``detach`` on cleanup. | ||
| """ | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| import torch | ||
| from loguru import logger | ||
| from torch.multiprocessing.reductions import reduce_tensor | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Callable | ||
|
|
||
| from typing_extensions import Self | ||
|
|
||
| from checkpoint_engine.device_utils import DeviceManager | ||
|
|
||
|
|
||
| def _rebuild_ipc(handle: tuple["Callable", tuple], device_id: int | None = None) -> torch.Tensor: | ||
| func, args = handle | ||
| list_args = list(args) | ||
| if device_id is not None: | ||
| # the key is to change device id to the current device id | ||
| # in case two processes have different CUDA_VISIBLE_DEVICES | ||
| list_args[6] = device_id | ||
| return func(*list_args) | ||
|
|
||
|
|
||
| class IpcHandler(ABC): | ||
| """Hands a device buffer from the producer (ps) to the consumer (worker).""" | ||
|
|
||
| @abstractmethod | ||
| def export(self, buffer: torch.Tensor) -> Any: | ||
| """Producer: return the picklable handle to send over ZMQ.""" | ||
|
|
||
| @abstractmethod | ||
| def attach(self, handle: Any, device_id: int) -> torch.Tensor: | ||
| """Consumer: reconstruct the shared device buffer from ``handle``.""" | ||
|
|
||
| def detach(self) -> None: | ||
| """Release IPC resources on either side. No-op by default.""" | ||
|
|
||
| # Used as a context manager so the handle is always released, without the | ||
| # caller needing its own try/finally. | ||
| def __enter__(self) -> "Self": | ||
| return self | ||
|
|
||
| def __exit__(self, *exc_info: object) -> None: | ||
| self.detach() | ||
|
|
||
|
|
||
| class TorchIpcHandler(IpcHandler): | ||
| """CUDA/NPU zero-copy handoff via torch.multiprocessing CUDA IPC (unchanged).""" | ||
|
|
||
| def export(self, buffer: torch.Tensor) -> Any: | ||
| return reduce_tensor(buffer) | ||
|
|
||
| def attach(self, handle: Any, device_id: int) -> torch.Tensor: | ||
| assert isinstance(handle, tuple), f"expected reduce_tensor tuple, got {type(handle)}" | ||
| buffer = _rebuild_ipc(handle, device_id) | ||
| assert buffer.dtype == torch.uint8 | ||
| return buffer | ||
|
|
||
|
|
||
| class XpuIpcHandler(IpcHandler): | ||
| """Intel XPU zero-copy handoff via native SYCL ``ipc_memory`` (portable byte blob).""" | ||
|
|
||
| kind = "xpu_sycl" # wire tag: identifies this handle format to the consumer | ||
|
|
||
| def __init__(self) -> None: | ||
| self._opened_ptr: int | None = None # consumer: the mapping to unmap | ||
| self._exported_ptr: int | None = None # producer: the retained exporter handle | ||
|
|
||
| def export(self, buffer: torch.Tensor) -> Any: | ||
| from checkpoint_engine import xpu_ipc | ||
|
|
||
| ptr = buffer.data_ptr() | ||
| handle_bytes = xpu_ipc.get_handle(ptr) | ||
| # Release only in detach(): freeing before the consumer opens can drop the | ||
| # fd under the level-zero-v2 UR adapter. | ||
| self._exported_ptr = ptr | ||
| return { | ||
| "kind": self.kind, | ||
| "handle_bytes": handle_bytes, | ||
| "nbytes": buffer.nbytes, | ||
| } | ||
|
|
||
| def attach(self, handle: Any, device_id: int) -> torch.Tensor: | ||
| from checkpoint_engine import xpu_ipc | ||
|
|
||
| assert isinstance(handle, dict) and handle.get("kind") == self.kind, ( | ||
| f"expected {self.kind} handle dict, got {type(handle)}" | ||
| ) | ||
| ptr = xpu_ipc.open_handle(handle["handle_bytes"], device_id) | ||
| self._opened_ptr = ptr | ||
| buffer = xpu_ipc.wrap_tensor(ptr, handle["nbytes"], device_id) | ||
| assert buffer.dtype == torch.uint8 | ||
| return buffer | ||
|
|
||
| def detach(self) -> None: | ||
| # Consumer unmaps its opened pointer; producer releases its exported handle. | ||
| # At most one of the two is set on any given instance. | ||
| from checkpoint_engine import xpu_ipc | ||
|
|
||
| if self._opened_ptr is not None: | ||
| try: | ||
| torch.xpu.synchronize() # no in-flight reads before unmapping | ||
| xpu_ipc.close_handle(self._opened_ptr) | ||
| except Exception as e: # noqa: BLE001 | ||
| logger.debug(f"xpu ipc close_handle failed during detach: {e}") | ||
| self._opened_ptr = None | ||
| if self._exported_ptr is not None: | ||
| try: | ||
| xpu_ipc.release_handle(self._exported_ptr) | ||
| except Exception as e: # noqa: BLE001 | ||
| logger.debug(f"xpu ipc release_handle failed during detach: {e}") | ||
| self._exported_ptr = None | ||
|
|
||
|
|
||
| def build_ipc_handler(device_manager: "DeviceManager") -> IpcHandler: | ||
| """Select the IPC handler for the current device backend.""" | ||
| if device_manager.device_type == "xpu": | ||
| return XpuIpcHandler() | ||
| return TorchIpcHandler() | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about renaming WeightTransport to IPCHandler? This object transports nothing; it just hands an IPC handle for a device buffer across processes.