pytorch - ✅(Solved) Fix RFC: Add A Cross-Backend Device Name API: `torch.accelerator.current_device_name()` [1 pull requests, 6 comments, 4 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#177935Fetched 2026-04-08 01:02:55
View on GitHub
Comments
6
Participants
4
Timeline
41
Reactions
0
Author
Timeline (top)
mentioned ×14subscribed ×14commented ×6labeled ×5

This RFC proposes and documents the addition of torch.accelerator.current_device_name(), a new public Python API that returns the human-readable hardware name of the currently active accelerator device. The API is backend-agnostic and works uniformly across CUDA (NVIDIA) and XPU (Intel) devices, consistent with the existing torch.accelerator namespace design.

Root Cause

This RFC proposes and documents the addition of torch.accelerator.current_device_name(), a new public Python API that returns the human-readable hardware name of the currently active accelerator device. The API is backend-agnostic and works uniformly across CUDA (NVIDIA) and XPU (Intel) devices, consistent with the existing torch.accelerator namespace design.

Fix Action

Fixed

PR fix notes

PR #177928: [feat][accelerator] Add current_device_name() API

Description (problem / solution / changelog)

Adds cross-backend support for querying the name of the currently active accelerator device as proposed in RCF https://github.com/pytorch/pytorch/issues/177935.

Changes:

  • Add pure virtual getDeviceName() to DeviceGuardImplInterface and implement it across all backends:
    • CUDA: via cudaGetDeviceProperties(&props, ...).name
    • XPU: via sycl::info::device::name
    • NoOp, Fake, LTC, PythonDeviceGuard: return static string constants
    • VirtualGuardImpl: delegates to the underlying impl
  • Add current_device_name() to c10::cuda and c10::xpu namespaces
  • Add at::accelerator::getDeviceName() dispatching through VirtualGuardImpl
  • Expose as torch.accelerator.current_device_name() Python API, backed by torch._C._accelerator_getDeviceName
  • Add test_current_device_name test validating device name against vendor CLI tools (nvidia-smi / xpu-smi) via new get_gpu_name helper
  • Document current_device_name in docs/source/accelerator.md

Changed files

  • aten/src/ATen/DeviceAccelerator.cpp (modified, +7/-0)
  • aten/src/ATen/DeviceAccelerator.h (modified, +4/-0)
  • c10/core/impl/DeviceGuardImplInterface.h (modified, +10/-0)
  • c10/core/impl/FakeGuardImpl.h (modified, +5/-0)
  • c10/core/impl/VirtualGuardImpl.h (modified, +4/-0)
  • c10/cuda/CUDAFunctions.cpp (modified, +6/-0)
  • c10/cuda/CUDAFunctions.h (modified, +2/-0)
  • c10/cuda/impl/CUDAGuardImpl.h (modified, +3/-0)
  • c10/xpu/XPUFunctions.cpp (modified, +5/-0)
  • c10/xpu/XPUFunctions.h (modified, +2/-0)
  • c10/xpu/impl/XPUGuardImpl.h (modified, +4/-0)
  • docs/source/accelerator.md (modified, +1/-0)
  • test/test_accelerator.py (modified, +14/-0)
  • torch/_C/__init__.pyi.in (modified, +1/-0)
  • torch/accelerator/__init__.py (modified, +17/-0)
  • torch/csrc/DeviceAccelerator.cpp (modified, +6/-0)
  • torch/csrc/acc/Module.cpp (modified, +6/-0)
  • torch/csrc/lazy/core/tensor_impl.cpp (modified, +6/-0)
  • torch/testing/_internal/common_utils.py (modified, +46/-0)

Code Example

torch.accelerator.current_device_name() -> str

---

>>> import torch
>>> torch.accelerator.is_available()
True
>>> torch.accelerator.current_device_name()
'NVIDIA A100-SXM4-40GB'

---

>>> import torch
>>> torch.accelerator.is_available()
True
>>> torch.accelerator.current_device_name()
'Intel(R) Data Center GPU Max 1550'

---

// c10/core/impl/DeviceGuardImplInterface.h
virtual std::string getDeviceName() const = 0;

---

// aten/src/ATen/DeviceAccelerator.h
TORCH_API std::string getDeviceName();

---

std::string getDeviceName() {
  const auto device_type = getAccelerator(true).value();
  c10::impl::VirtualGuardImpl impl(device_type);
  return impl.getDeviceName();
}

---

m.def("_accelerator_getDeviceName", []() {
    const auto device_type = at::accelerator::getAccelerator(true).value();
    torch::utils::maybe_initialize_device(device_type);
    return at::accelerator::getDeviceName();
});

---

def current_device_name() -> str:
    return torch._C._accelerator_getDeviceName()
RAW_BUFFERClick to expand / collapse

🚀 The feature, motivation and pitch

Summary

This RFC proposes and documents the addition of torch.accelerator.current_device_name(), a new public Python API that returns the human-readable hardware name of the currently active accelerator device. The API is backend-agnostic and works uniformly across CUDA (NVIDIA) and XPU (Intel) devices, consistent with the existing torch.accelerator namespace design.

Motivation

PyTorch already exposes torch.accelerator.current_device_index() and torch.accelerator.get_device_capability(), but there is no way to programmatically retrieve name of the active device (e.g., "NVIDIA A100-SXM4-40GB" or "Intel(R) Data Center GPU Max 1550") without using backend-specific APIs such as torch.cuda.get_device_name() or torch.xpu.get_device_name().

This gap causes two problems:

  1. Backend lock-in in user code. Code that needs to log, assert, or branch on device name must import torch.cuda or torch.xpu directly, coupling otherwise backend-neutral code to a specific accelerator.

  2. Missing introspection for tooling and diagnostics. Training frameworks, profilers, and orchestration tools need to identify the hardware in use without writing vendor-specific branches. An example is distributed training launchers that emit hardware audit logs at job start.

Proposed API

torch.accelerator.current_device_name() -> str

Returns the name of the currently selected device for the active accelerator.

Examples

NVIDIA GPU (CUDA backend):

>>> import torch
>>> torch.accelerator.is_available()
True
>>> torch.accelerator.current_device_name()
'NVIDIA A100-SXM4-40GB'

Intel GPU (XPU backend):

>>> import torch
>>> torch.accelerator.is_available()
True
>>> torch.accelerator.current_device_name()
'Intel(R) Data Center GPU Max 1550'

Design

Interface Layer (DeviceGuardImplInterface)

A new pure virtual method is added to the device guard implementation interface, which is the existing extension point for all device backends:

// c10/core/impl/DeviceGuardImplInterface.h
virtual std::string getDeviceName() const = 0;

This follows the same pattern used by getDevice() and getDeviceCapability(). All concrete implementations must provide this method, ensuring the compiler enforces completeness across backends.

Backend Implementations

BackendImplementationSource
CUDAcudaGetDeviceProperties(&props, device).namec10/cuda/CUDAFunctions.cpp
XPUsycl::info::device::name via SYCL device poolc10/xpu/XPUFunctions.cpp
NoOp (CPU, Meta)Returns "NoOpDevice"DeviceGuardImplInterface.h
FakeGuardImpl (tests)Returns "FakeGuardDevice"c10/core/impl/FakeGuardImpl.h
LTC (Lazy Tensor Core)Returns "LazyDevice"torch/csrc/lazy/core/tensor_impl.cpp
PythonDeviceGuardReturns "PythonDeviceGuard"torch/csrc/acc/Module.cpp

C++ Accelerator Namespace

A new free function is added in at::accelerator:

// aten/src/ATen/DeviceAccelerator.h
TORCH_API std::string getDeviceName();

It resolves the active accelerator type, constructs a VirtualGuardImpl, and delegates:

std::string getDeviceName() {
  const auto device_type = getAccelerator(true).value();
  c10::impl::VirtualGuardImpl impl(device_type);
  return impl.getDeviceName();
}

VirtualGuardImpl itself is updated to forward getDeviceName() through its held DeviceGuardImplInterface*, following the same delegation pattern used for every other virtual method in that class.

Python Binding

A pybind11 lambda in torch/csrc/DeviceAccelerator.cpp exposes the C++ function:

m.def("_accelerator_getDeviceName", []() {
    const auto device_type = at::accelerator::getAccelerator(true).value();
    torch::utils::maybe_initialize_device(device_type);
    return at::accelerator::getDeviceName();
});

The call to maybe_initialize_device ensures the device context is initialized before querying properties, matching the pattern used by _accelerator_getDeviceIndex and _accelerator_getDeviceCapability.

The public Python API in torch/accelerator/__init__.py is a thin wrapper:

def current_device_name() -> str:
    return torch._C._accelerator_getDeviceName()

Testing

A new test test_current_device_name is added to test/test_accelerator.py. It:

  1. Iterates over known vendors ("nvidia", "intel"), skipping unavailable backends.
  2. Uses a new helper get_gpu_name(vendor, device_id) in torch/testing/_internal/common_utils.py that queries the device name from the system CLI tool (nvidia-smi or xpu-smi) and returns it as a ground-truth reference.
  3. Asserts that torch.accelerator.current_device_name() matches the CLI-reported name exactly.
  4. Skips on MPS (Apple Silicon), which does not support this API.

Backward Compatibility

This is a purely additive change. No existing APIs are modified.

cc @albanD @guangyey @EikanWang

extent analysis

Fix Plan

To implement the proposed torch.accelerator.current_device_name() API, follow these steps:

  • Step 1: Update the DeviceGuardImplInterface
    • Add a new pure virtual method getDeviceName() to the interface:

// c10/core/impl/DeviceGuardImplInterface.h virtual std::string getDeviceName() const = 0;

*   **Step 2: Implement backend-specific `getDeviceName()` methods**
    *   For CUDA backend:
        ```cpp
// c10/cuda/CUDAFunctions.cpp
std::string getDeviceName() const override {
    cudaDeviceProp props;
    cudaGetDeviceProperties(&props, device);
    return props.name;
}
*   For XPU backend:
    ```cpp

// c10/xpu/XPUFunctions.cpp std::string getDeviceName() const override { sycl::device device = /* get the SYCL device */; return device.get_infosycl::info::device::name(); }

*   **Step 3: Add a new free function to the `at::accelerator` namespace**
    *   Implement the `getDeviceName()` function:
        ```cpp
// aten/src/ATen/DeviceAccelerator.h
TORCH_API std::string getDeviceName();
    ```cpp

// aten/src/ATen/DeviceAccelerator.cpp std::string getDeviceName() { const auto device_type = getAccelerator(true).value(); c10::impl::VirtualGuardImpl impl(device_type); return impl.getDeviceName(); }

*   **Step 4: Create a Python binding for the `getDeviceName()` function**
    *   Use pybind11 to expose the C++ function:
        ```cpp
// torch/csrc/DeviceAccelerator.cpp
m.def("_accelerator_getDeviceName", []() {
    const auto device_type = at::accelerator::getAccelerator(true).value();
    torch::utils::maybe_initialize_device(device_type);
    return at::accelerator::getDeviceName();
});
  • Step 5: Add a public Python API
    • Create a thin wrapper in torch/accelerator/__init__.py:

def current_device_name() -> str: return torch._C._accelerator_getDeviceName()


### Verification
To verify the implementation, run the added test `test_current_device_name` in `test/test_accelerator.py`. This test checks that the `torch.accelerator.current_device_name()` API returns the correct device name for different vendors.

### Extra Tips
*   Ensure that the `getDeviceName()` method is implemented correctly for each backend.
*   Test the API thoroughly to catch any potential issues.
*   Consider adding more tests to cover different

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - ✅(Solved) Fix RFC: Add A Cross-Backend Device Name API: `torch.accelerator.current_device_name()` [1 pull requests, 6 comments, 4 participants]