Skip to content

[xpu][feature] [2/2] Introduce XPUPluggableAllocator in frontend part - #169043

Closed
guangyey wants to merge 25 commits into
gh/guangyey/251/basefrom
gh/guangyey/251/head
Closed

guangyey wants to merge 25 commits into
gh/guangyey/251/basefrom
gh/guangyey/251/head

Conversation

@guangyey

@guangyey guangyey commented Nov 25, 2025 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

Motivation

This PR aims to introduce torch.xpu.memory.XPUPluggableAllocator and focus on the frontend part. Meanwhile, we introduce an API torch.xpu.memory.change_current_allocator that used to change the current XPUAllocator. Both APIs has the CUDA counterpart.

Additional Context

This API would be used in popular repos such as https://github.com/search?q=repo%3Avllm-project%2Fvllm%20cudapluggableallocator&type=code

@pytorch-bot

pytorch-bot Bot commented Nov 25, 2025 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/169043

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 Cancelled Job, 1 Unrelated Failure

As of commit 3a38261 with merge base c6ae757 (image):

CANCELLED JOB - The following job was cancelled. Please retry:

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

guangyey added a commit that referenced this pull request Nov 25, 2025
guangyey added a commit that referenced this pull request Nov 25, 2025

@EikanWang EikanWang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Any test plan?

@guangyey

Copy link
Copy Markdown
Collaborator Author

Any test plan?

I am working on it.

guangyey added a commit that referenced this pull request Nov 25, 2025
guangyey added a commit that referenced this pull request Nov 25, 2025
@guangyey guangyey added ciflow/xpu Run XPU CI tasks release notes: xpu release notes category ciflow/trunk Trigger trunk jobs on your pull request labels Nov 25, 2025
guangyey added a commit that referenced this pull request Nov 25, 2025
guangyey added a commit that referenced this pull request Nov 25, 2025
guangyey added a commit that referenced this pull request Nov 25, 2025
guangyey added a commit that referenced this pull request Nov 25, 2025
guangyey added a commit that referenced this pull request Nov 25, 2025
guangyey added a commit that referenced this pull request Nov 25, 2025
guangyey added a commit that referenced this pull request Nov 25, 2025
@guangyey
guangyey requested a review from albanD November 25, 2025 11:18
guangyey added a commit that referenced this pull request Nov 25, 2025
[ghstack-poisoned]
guangyey added a commit that referenced this pull request Nov 25, 2025
guangyey added a commit that referenced this pull request Dec 3, 2025
guangyey added a commit that referenced this pull request Dec 3, 2025
guangyey added a commit that referenced this pull request Dec 3, 2025
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
guangyey added a commit that referenced this pull request Dec 3, 2025
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]

@eellison eellison 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.

Seems reasonable to me - maybe @albanD to do another check

Comment thread torch/csrc/xpu/Module.cpp
});
}

static void registerXpuPluggableAllocator(PyObject* module) {

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.

cc @albanD on advice if this is the right device extension api for allocator. seems reasonable to me

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@albanD This is the same as CUDA.

static void registerCudaPluggableAllocator(PyObject* module) {
auto m = py::handle(module).cast<py::module>();


include_dirs = kwargs.get("include_dirs", [])
include_dirs += include_paths()
include_dirs += include_paths(device_type="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.

same comment @albanD , maybe @malfet

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Hi @albanD @malfet , please refer to

def include_paths(device_type: str = "cpu", torch_include_dirs=True) -> list[str]:
"""
Get the include paths required to build a C++ or CUDA or SYCL extension.
Args:
device_type: Defaults to "cpu".
Returns:
A list of include path strings.
"""
paths = []
lib_include = os.path.join(_TORCH_PATH, 'include')
if torch_include_dirs:
paths.extend([
lib_include,
# Remove this once torch/torch.h is officially no longer supported for C++ extensions.
os.path.join(lib_include, 'torch', 'csrc', 'api', 'include'),
])
if device_type == "cuda" and IS_HIP_EXTENSION:
paths.append(os.path.join(lib_include, 'THH'))
paths.append(_join_rocm_home('include'))
elif device_type == "cuda":
cuda_home_include = _join_cuda_home('include')
# if we have the Debian/Ubuntu packages for cuda, we get /usr as cuda home.
# but gcc doesn't like having /usr/include passed explicitly
if cuda_home_include != '/usr/include':
paths.append(cuda_home_include)
# Support CUDA_INC_PATH env variable supported by CMake files
if (cuda_inc_path := os.environ.get("CUDA_INC_PATH", None)) and \
cuda_inc_path != '/usr/include':
paths.append(cuda_inc_path)
if CUDNN_HOME is not None:
paths.append(os.path.join(CUDNN_HOME, 'include'))
elif device_type == "xpu":
paths.append(_join_sycl_home('include'))
paths.append(_join_sycl_home('include', 'sycl'))
return paths

include_paths defaults to using cpu as the device type. However, in SyclExtension, we need to include the SYCL headers. To ensure the SYCL include path is added, I explicitly set device_type to xpu, which triggers inclusion of the SYCL-specific include directories.

[ghstack-poisoned]
@guangyey

guangyey commented Dec 4, 2025

Copy link
Copy Markdown
Collaborator Author

Let's land this PR first. If there are any additional concerns, we can address them in a follow-up PR.

@guangyey

guangyey commented Dec 4, 2025

Copy link
Copy Markdown
Collaborator Author

Thanks @eellison
@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: xpu / linux-noble-xpu-n-py3.10 / test (default, 6, 12, linux.idc.xpu)

Details for Dev Infra team Raised by workflow job

@guangyey

guangyey commented Dec 4, 2025

Copy link
Copy Markdown
Collaborator Author

"log upload timeout"
@pytorchbot merge -i

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged while ignoring the following 2 checks: trunk / linux-jammy-rocm-py3.10 / test (default, 1, 6, linux.rocm.gpu.gfx942.1, unstable), xpu / linux-noble-xpu-n-py3.10 / test (default, 6, 12, linux.idc.xpu)

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

JacobSzwejbka pushed a commit that referenced this pull request Dec 8, 2025
…#169043)

# Motivation
This PR aims to introduce `torch.xpu.memory.XPUPluggableAllocator` and focus on the frontend part. Meanwhile, we introduce an API `torch.xpu.memory.change_current_allocator` that used to change the current `XPUAllocator`. Both APIs has the CUDA counterpart.

# Additional Context
This API would be used in popular repos such as https://github.com/search?q=repo%3Avllm-project%2Fvllm%20cudapluggableallocator&type=code

Pull Request resolved: #169043
Approved by: https://github.com/EikanWang, https://github.com/gujinghui, https://github.com/eellison
ghstack dependencies: #168966
@github-actions
github-actions Bot deleted the gh/guangyey/251/head branch January 4, 2026 02:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request ciflow/xpu Run XPU CI tasks Merged open source release notes: xpu release notes category

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

6 participants