已关闭
[Bug]: torch.accelerator.random APIs (initial_seed, get_rng_state, get_rng_state_all) does not work on NPU #3809
dwoai22创建于  8月6日关闭于  22 天前
dwoai22
dwoai22
8月6日 创建

欢迎加入社区,感谢您对社区的贡献 🎉!

欢迎加入社区,感谢您对社区的贡献 🎉!

环境信息

OS: Ubuntu 22.04.5 LTS (Jammy Jellyfish)
Hardware: 1x910B3
CANN: 9.0.0
python: 3.12.13
torch: 2.14.0a0+git97f01c2
torch_npu: 2.14.0+gite229e99

🐛 问题描述

Upstream torch serves 3 APIs in torch.accelerator namespace currently. When these APIs are used
an error is observed.

Reproduction and Error Log

>>> import torch
>>> torch.accelerator.random.initial_seed()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/workspace/pytorch/torch/accelerator/random.py", line 22, in initial_seed
    default_generator = torch._C._accelerator_getDefaultGenerator(device_index)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
NotImplementedError: You should register `PrivateUse1HooksInterface`by `RegisterPrivateUse1HooksInterface` and implement `getDefaultGenerator` at the same time for PrivateUse1.

Expected Behaviour

>>> import torch
>>> torch.accelerator.random.initial_seed()
1029742135505983

Root Cause

const at::Generator& getDefaultGenerator(c10::DeviceIndex device_index) {
getDefaultGenerator of torch_npu/csrc/core/npu/NPUHooksInterface.h cannot override the method in aten/src/ATen/detail/PrivateUse1HooksInterface.h because signatures dont match. NPU's getDefaultGenerator lacks trailing const expression.

Another problem is, returned generator is cached like this: static auto device_gen = .... When other device indexes given, they will get the generator of the first device that is cached.

欢迎加入社区,感谢您对社区的贡献 🎉!

欢迎加入社区,感谢您对社区的贡献 🎉!

likedislike
dwoai22dwoai22
8月6日 关联了看板:FrameworkPTAdapter 版本issue看板
ascend-robotascend-robot成员
8月6日 添加了label:bug
dwoai22dwoai22
8月7日 关联了pull request:fix: fix getDefaultGenerator in NPUHooksInterface.h so that torch.accelerator.random APIs can use NPU backend
TorchNPU-BotTorchNPU-Bot成员
8月7日 添加了label:bot-triaged
TorchNPU-Bot
TorchNPU-Bot成员
8月7日 评论:

检测到当前 issue 已关联 PR,自动添加标签:bot-triaged

likedislike
ascend-robotascend-robot成员
22 天前 关闭了 issue
ascend-robotascend-robot成员
22 天前 添加了label:resolved