Skip to content

[Feature][Intel XPU] Add memory saver support for Intel XPU via upstream torch_memory_saver - #29935

Merged
mingfeima merged 5 commits into
sgl-project:mainfrom
siju-samuel:xpu-memory-saver
Sep 8, 2026
Merged

mingfeima merged 5 commits into
sgl-project:mainfrom
siju-samuel:xpu-memory-saver

Conversation

@siju-samuel

@siju-samuel siju-samuel commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Motivation

Enable release/resume_memory_occupation on Intel XPU using the upstream torch_memory_saver package (Level Zero VMM backend). Replaces in-repo implementation with pip-installable upstream dependency.

Features:

  • Pause/resume physical GPU memory while preserving virtual addresses
  • Tag-based selective memory management (kv_cache, weights, cuda_graph)
  • Optional CPU backup for content preservation
  • Multi-device support (verified via the package's multi-device test)
  • Sysman-based memory verification (torch accounting doesn't reflect unmapped pages)

Modifications

Changes:

  • Add TorchMemorySaverAdapter for unified memory saver API
  • XPU uses hook_mode='torch' (in-process pluggable allocator)
  • configure_subprocess() and cuda_graph() are no-ops on XPU
  • Update Dockerfile to build the upstream package for XPU
  • Add test suite (10 tests, all passing)

Installation (XPU builds from source; TMS_PLATFORM=xpu forces the XPU backend and --no-build-isolation lets the build match libsycl to the installed torch):
source /opt/intel/oneapi/setvars.sh
TMS_PLATFORM=xpu pip install --no-build-isolation
git+https://github.com/fzyzcjy/torch_memory_saver.git

Usage:
python -m sglang.launch_server --model-path --device xpu --enable-memory-saver

Depends on: fzyzcjy/torch_memory_saver#78

Accuracy Tests

Speed Tests and Profiling

Checklist

Review and Merge Process

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

cc @mingfeima @fzyzcjy @habaohaba @yueming-yuan @jianan-gu @hnyls2002 @merrymercy


CI States

Latest PR Test (Base): ❌ Run #34083643346
Latest PR Test (Extra): ❌ Run #34083643187
Latest PR Test (AMD ROCm 7.2): ❌ Run #34083643322

@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Jul 2, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds support for the experimental Memory Saver feature on Intel XPU, allowing the engine to temporarily release and reclaim GPU memory. The changes include updating the Dockerfile to install torch_memory_saver from source, adding documentation, adapting the TorchMemorySaverAdapter for XPU specifics (such as using the in-process pluggable allocator), updating memory utility functions, and introducing a comprehensive suite of unit tests. The reviewer feedback suggests pinning the torch_memory_saver repository to a specific commit hash in the Dockerfile to ensure reproducible builds, and removing an unused variable cur in the test setup.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread docker/xpu.Dockerfile Outdated
Comment thread test/registered/xpu/test_xpu_memory_saver.py
siju-samuel added a commit to siju-samuel/sglang that referenced this pull request Jul 2, 2026
Addresses review on PR sgl-project#29935: torch.xpu.current_device() was assigned to
'cur' but never used (mem_get_info(i) reads a specific device without changing
the current one). Drop it and correct the comment.
Comment thread python/sglang/test/test_utils.py Outdated
Comment thread test/registered/xpu/test_xpu_memory_saver.py
return _memory_saver.region(tag=tag, enable_cpu_backup=enable_cpu_backup)

def cuda_graph(self, **kwargs):
if is_xpu():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why can't we support this ?

siju-samuel added a commit to siju-samuel/sglang that referenced this pull request Aug 10, 2026
Addresses review on PR sgl-project#29935: torch.xpu.current_device() was assigned to
'cur' but never used (mem_get_info(i) reads a specific device without changing
the current one). Drop it and correct the comment.
@siju-samuel
siju-samuel force-pushed the xpu-memory-saver branch 2 times, most recently from 64d10f5 to 6150746 Compare August 10, 2026 15:09
siju-samuel added a commit to siju-samuel/sglang that referenced this pull request Aug 10, 2026
Addresses review on PR sgl-project#29935: torch.xpu.current_device() was assigned to
'cur' but never used (mem_get_info(i) reads a specific device without changing
the current one). Drop it and correct the comment.
@siju-samuel

Copy link
Copy Markdown
Contributor Author

/tag-run-ci-label

@mingfeima

Copy link
Copy Markdown
Collaborator

https://github.com/sgl-project/sglang/actions/runs/32100083362/job/95598837541 xpu ci failed. seems to be related to this pr, where torch_memory_saver import in torch_memory_saver_adapter.py:L7-L20, even without --enable-memory-saver.

please check.

@mingfeima mingfeima added intel xpu intel gpu with device `torch.xpu` labels Sep 7, 2026
Enable release/resume_memory_occupation on Intel XPU using the upstream
torch_memory_saver package (Level Zero VMM backend). Replaces in-repo
implementation with pip-installable upstream dependency.

Features:
- Pause/resume physical GPU memory while preserving virtual addresses
- Tag-based selective memory management (kv_cache, weights, cuda_graph)
- Optional CPU backup for content preservation
- Multi-device support (verified via the package's multi-device test)
- Sysman-based memory verification (torch accounting doesn't reflect unmapped pages)

Changes:
- Add TorchMemorySaverAdapter for unified memory saver API
- XPU uses hook_mode='torch' (in-process pluggable allocator, not LD_PRELOAD)
- configure_subprocess() and cuda_graph() are no-ops on XPU
- Update Dockerfile to build the upstream package for XPU
- Add test suite (10 tests, all passing)

Installation (XPU builds from source; TMS_PLATFORM=xpu forces the XPU backend
and --no-build-isolation lets the build match libsycl to the installed torch):
  source /opt/intel/oneapi/setvars.sh
  TMS_PLATFORM=xpu pip install --no-build-isolation \
    git+https://github.com/fzyzcjy/torch_memory_saver.git

Usage:
  python -m sglang.launch_server --model-path <model> --device xpu --enable-memory-saver

Depends on: fzyzcjy/torch_memory_saver#78
Addresses review on PR sgl-project#29935: torch.xpu.current_device() was assigned to
'cur' but never used (mem_get_info(i) reads a specific device without changing
the current one). Drop it and correct the comment.
torch_memory_saver was installed from the tip of master, so an upstream change
could break the XPU image and the documented setup without any change here.

Pin to a5c99f11b18ebb8e9fda71a68812e476ae49e417 (tag v0.0.10b2). XPU still has
to build from source -- the published wheels are CUDA-only -- so pinning the ref
is what makes the build reproducible. The Dockerfile exposes it as the
TORCH_MEMORY_SAVER_REF build arg so the ref can be bumped with --build-arg.
The test asserted physical release through the saver's sysman-backed
tms_xpu_device_free_bytes, which newer Intel drivers report frozen: it stayed
constant across alloc/pause/resume, so 5 of the 10 cases read a 0.0 GiB delta
and failed even though pause/resume worked correctly.

Use tms_xpu_committed_bytes instead — the physical bytes the saver holds ACTIVE,
which it only decrements after zeVirtualMemUnmap and zePhysicalMemDestroy
succeed. That keeps the assertions tied to real Level Zero release while being
driver-independent. Also pin the install ref quoted in the docstring and skip
message, and refresh the moved docs path.
…-ops

Revert python/sglang/test/test_utils.py to main. get_gpu_memory_gb() is a
pre-existing helper with no callers in the tree, and switching its XPU branch to
torch.xpu.mem_get_info() also cut against the platform layer, which deliberately
uses memory_allocated() until mem_get_info is adopted there
(platforms/xpu.py:get_available_memory). Out of scope for this PR either way.

torch_memory_saver_adapter: state why the XPU paths are no-ops rather than just
that they are. Pauseable graph capture is gated on hook_mode="preload" upstream
while XPU requires hook_mode="torch", and configure_subprocess() LD_PRELOADs a
binary upstream's setup.py does not build for XPU. cuda_graph() is unreachable on
XPU today (runner_backend routes XPU to FullXPUGraphBackend, which takes no memory
saver), so warn once instead of raising: a future XPU graph backend that does route
here surfaces that graph memory is not pauseable instead of failing to launch.

Docs: mem_get_info() is also unusable for confirming a release -- it stays flat
because torch keeps the freed block cached (measured: free unchanged across pause
while sysman moved 1.00 GiB) -- so name it alongside memory_allocated().
@mingfeima
mingfeima merged commit 2358916 into sgl-project:main Sep 8, 2026
144 of 175 checks passed
mqhc2020 pushed a commit to mqhc2020/sglang that referenced this pull request Sep 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation intel run-ci xpu intel gpu with device `torch.xpu`

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants