-
Notifications
You must be signed in to change notification settings - Fork 7.6k
[Feature][Intel XPU] Add memory saver support for Intel XPU via upstream torch_memory_saver #29935
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
Draft
siju-samuel
wants to merge
3
commits into
sgl-project:main
Choose a base branch
from
siju-samuel:xpu-memory-saver
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.
Draft
Changes from all commits
Commits
Show all changes
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,17 @@ | |
| from abc import ABC | ||
| from contextlib import contextmanager | ||
|
|
||
| from sglang.srt.utils.common import is_xpu | ||
|
|
||
| try: | ||
| import torch_memory_saver | ||
|
|
||
| # Intel XPU requires hook_mode="torch" (in-process pluggable allocator); | ||
| # the LD_PRELOAD-based preload mode is CUDA/HIP-only. Set it before the | ||
| # singleton is initialized on first use. | ||
| if is_xpu(): | ||
| torch_memory_saver.torch_memory_saver.hook_mode = "torch" | ||
|
|
||
| _memory_saver = torch_memory_saver.torch_memory_saver | ||
| import_error = None | ||
| except ImportError as e: | ||
|
|
@@ -18,11 +26,24 @@ class TorchMemorySaverAdapter(ABC): | |
| @staticmethod | ||
| def create(enable: bool): | ||
| if enable and import_error is not None: | ||
| logger.warning( | ||
| "enable_memory_saver is enabled, but " | ||
| "torch-memory-saver is not installed. Please install it " | ||
| "via `pip3 install torch-memory-saver`. " | ||
| ) | ||
| if is_xpu(): | ||
| # XPU ships no prebuilt wheel; it is built from source against the | ||
| # local oneAPI + torch-XPU runtime. TMS_PLATFORM=xpu forces the XPU | ||
| # backend; --no-build-isolation lets the build see torch and match | ||
| # the libsycl ABI to it. | ||
| logger.warning( | ||
| "enable_memory_saver is enabled, but torch-memory-saver is " | ||
| "not installed. On Intel XPU, build it from source with Intel " | ||
| "oneAPI on PATH: `TMS_PLATFORM=xpu pip3 install " | ||
| "--no-build-isolation " | ||
| "git+https://github.com/fzyzcjy/torch_memory_saver.git`." | ||
| ) | ||
| else: | ||
| logger.warning( | ||
| "enable_memory_saver is enabled, but " | ||
| "torch-memory-saver is not installed. Please install it " | ||
| "via `pip3 install torch-memory-saver`. " | ||
| ) | ||
| raise import_error | ||
| return ( | ||
| _TorchMemorySaverAdapterReal() if enable else _TorchMemorySaverAdapterNoop() | ||
|
|
@@ -59,17 +80,34 @@ def enabled(self): | |
|
|
||
|
|
||
| class _TorchMemorySaverAdapterReal(TorchMemorySaverAdapter): | ||
| """Adapter for TorchMemorySaver with tag-based control""" | ||
| """Adapter for TorchMemorySaver with tag-based control. | ||
|
|
||
| Backed by the upstream torch_memory_saver package (CUDA VMM, and Intel XPU | ||
| via Level Zero). On XPU the package uses an in-process pluggable allocator | ||
| (hook_mode="torch") rather than the CUDA LD_PRELOAD path, so | ||
| configure_subprocess() (nothing to preload) and cuda_graph() (no pauseable | ||
| graph-capture path) are no-ops there. | ||
| """ | ||
|
|
||
| def configure_subprocess(self): | ||
| if is_xpu(): | ||
| # XPU uses an in-process pluggable allocator; nothing to preload. | ||
| return self._noop_context() | ||
| return torch_memory_saver.configure_subprocess() | ||
|
|
||
| def region(self, tag: str, enable_cpu_backup: bool = False): | ||
| return _memory_saver.region(tag=tag, enable_cpu_backup=enable_cpu_backup) | ||
|
|
||
| def cuda_graph(self, **kwargs): | ||
| if is_xpu(): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why can't we support this ? |
||
| # XPU does not support the memory-saver pauseable graph-capture path. | ||
| return self._noop_context() | ||
| return _memory_saver.cuda_graph(**kwargs) | ||
|
|
||
| @contextmanager | ||
| def _noop_context(self, **kwargs): | ||
| yield | ||
|
|
||
| def disable(self): | ||
| return _memory_saver.disable() | ||
|
|
||
|
|
||
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 |
|---|---|---|
|
|
@@ -2479,7 +2479,11 @@ def get_gpu_memory_gb(): | |
| if is_cuda(): | ||
| return torch.cuda.device_memory_used() / 1024**3 | ||
| elif is_xpu(): | ||
| return torch.xpu.memory_allocated() / 1024**3 | ||
| # Use mem_get_info (real device free/total), NOT memory_allocated(): the | ||
| # latter is the allocator's bookkeeping and does not drop when the XPU | ||
| # memory saver releases physical pages via zeVirtualMemUnmap. | ||
| free, total = torch.xpu.mem_get_info() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this api working ? |
||
| return (total - free) / 1024**3 | ||
| else: | ||
| return 0 | ||
|
|
||
|
|
||
Oops, something went wrong.
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.
To ensure reproducible builds and prevent future breaking changes on the
mainbranch oftorch_memory_saverfrom breaking the Docker image build, it is highly recommended to pin the installation to a specific commit hash or tag.