已合并
add_npu_backend_init_log #39399
cuiduo创建于 6月26日
add_npu_backend_init_log #39399
已合并
共 2 个文件变更+5-3
| @@ -1,7 +1,6 @@ | |||
| 1 | import torch | 1 | import torch |
| 2 | from torch.testing._internal.common_utils import run_tests, parametrize, instantiate_parametrized_tests | 2 | from torch.testing._internal.common_utils import run_tests, parametrize, instantiate_parametrized_tests |
| 3 | from testutils import TestUtils | 3 | from testutils import TestUtils |
| 4 | -import torch_npu | ||
| 5 | 4 | ||
| 6 | 5 | ||
| 7 | class TestSumAdd(TestUtils): | 6 | class TestSumAdd(TestUtils): |
| @@ -246,11 +246,14 @@ def patch_inductor_wrapper(): | |||
| 246 | def new_init(self, mode, options, dynamic): | 246 | def new_init(self, mode, options, dynamic): |
| 247 | src_init(self, mode, options, dynamic) | 247 | src_init(self, mode, options, dynamic) |
| 248 | backend = _resolve_npu_backend_from_wrapper(self) | 248 | backend = _resolve_npu_backend_from_wrapper(self) |
| 249 | - if backend=="mlir" or backend=="dvm": | 249 | + if backend=="mlir": |
| 250 | with _NpuBackendScope(backend): | 250 | with _NpuBackendScope(backend): |
| 251 | + log.info("Running MLIR backend") | ||
| 251 | device_id = torch_npu.npu.current_device() | 252 | device_id = torch_npu.npu.current_device() |
| 252 | torch_npu._C._recovery_all_npu_stream(device_id) | 253 | torch_npu._C._recovery_all_npu_stream(device_id) |
| 253 | - | 254 | + if backend=="dvm": |
| 255 | + with _NpuBackendScope(backend): | ||
| 256 | + log.info("Running dvm backend") | ||
| 254 | 257 | ||
| 255 | def new_call(self, model_, inputs_): | 258 | def new_call(self, model_, inputs_): |
| 256 | backend = _resolve_npu_backend_from_wrapper(self) | 259 | backend = _resolve_npu_backend_from_wrapper(self) |
🟡 Medium Priority
原始代码中,
backend=="mlir" or backend=="dvm"共用同一代码块,其中包含device_id = torch_npu.npu.current_device()和torch_npu._C._recovery_all_npu_stream(device_id)两条关键调用。本次变更将or条件拆分为两个独立if块以分别添加日志,但 dvm 分支中丢失了这两条调用,仅保留了_NpuBackendScope上下文和新增的log.info。_recovery_all_npu_stream的作用是重建当前 device 上所有 NPU stream(默认 stream、secondary stream 及 pool 中所有 stream)——这是设备初始化/恢复的必须步骤。该 PR 标题为 "add_npu_backend_init_log",描述中未提及要移除 dvm 的 stream recovery,且torch_npu/_inductor目录下也未找到 dvm 后端自行调用的替代逻辑。因此该移除高度疑似为拆分条件时意外遗漏。失败场景:当
backend=="dvm"且 device 此前已初始化时,dvm 编译流程将跳过 stream 重建,后续可能使用已失效的 stream 导致 NPU 运算异常或 hang。修复方向:在 dvm 分支的
with _NpuBackendScope(backend):块内补充回device_id = torch_npu.npu.current_device()和torch_npu._C._recovery_all_npu_stream(device_id)两行。建议:在 dvm 分支的
with _NpuBackendScope(backend):块内,于log.info("Running dvm backend")之后补充device_id = torch_npu.npu.current_device()和torch_npu._C._recovery_all_npu_stream(device_id)两行,与 mlir 分支保持一致。