pytorch - 💡(How to fix) Fix torch.accelerator.device_index graph breaks [1 comments, 1 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#181540Fetched 2026-04-27 05:28:39
View on GitHub
Comments
1
Participants
1
Timeline
77
Reactions
0
Author
Participants
Timeline (top)
mentioned ×34subscribed ×34labeled ×8commented ×1

Fix Action

Fix / Workaround

print("\n=== torch.cuda.device ===") compiled_cuda = torch.compile(fn_cuda_device, backend="eager") compiled_cuda(x)

The workaround is to use e.g. ` torch.cuda.device(tensor.device.index)`

Code Example

import torch

def fn_accelerator(tensor):
    ctx = torch.accelerator.device_index(tensor.device.index)
    return tensor + 1

def fn_cuda_device(tensor):
    ctx = torch.cuda.device(tensor.device.index)
    return tensor + 1

x = torch.randn(4, device="cuda")

print("=== torch.accelerator.device_index ===")
compiled_accel = torch.compile(fn_accelerator, backend="eager")
compiled_accel(x)

print("\n=== torch.cuda.device ===")
compiled_cuda = torch.compile(fn_cuda_device, backend="eager")
compiled_cuda(x)
RAW_BUFFERClick to expand / collapse
import torch

def fn_accelerator(tensor):
    ctx = torch.accelerator.device_index(tensor.device.index)
    return tensor + 1

def fn_cuda_device(tensor):
    ctx = torch.cuda.device(tensor.device.index)
    return tensor + 1

x = torch.randn(4, device="cuda")

print("=== torch.accelerator.device_index ===")
compiled_accel = torch.compile(fn_accelerator, backend="eager")
compiled_accel(x)

print("\n=== torch.cuda.device ===")
compiled_cuda = torch.compile(fn_cuda_device, backend="eager")
compiled_cuda(x)

The workaround is to use e.g. torch.cuda.device(tensor.device.index)

cc @chauhang @penguinwu @voznesenskym @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @amjames @Lucaskabela @jataylo @azahed98 @albanD @guangyey

extent analysis

TL;DR

Use torch.cuda.device(tensor.device.index) instead of torch.accelerator.device_index(tensor.device.index) to resolve the issue.

Guidance

  • Replace torch.accelerator.device_index(tensor.device.index) with torch.cuda.device(tensor.device.index) in the fn_accelerator function.
  • Verify that the corrected function works as expected by running the compiled function with a sample tensor.
  • Check the PyTorch documentation for any updates or changes to the torch.accelerator module.
  • Test the workaround with different tensor devices to ensure it works consistently.

Example

def fn_accelerator(tensor):
    ctx = torch.cuda.device(tensor.device.index)
    return tensor + 1

Notes

The provided workaround suggests that torch.accelerator.device_index is not compatible with the eager backend, and using torch.cuda.device instead resolves the issue.

Recommendation

Apply workaround: use torch.cuda.device(tensor.device.index) instead of torch.accelerator.device_index(tensor.device.index), as it is a direct replacement suggested in the issue.

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 - 💡(How to fix) Fix torch.accelerator.device_index graph breaks [1 comments, 1 participants]