已合并
test: adapt debug_set_autodiff_subgraph_inlining for NPU #41603
yulin520创建于 7月14日
test: adapt debug_set_autodiff_subgraph_inlining for NPU #41603
已合并
共 1 个文件变更+62-7
| @@ -1,11 +1,66 @@ | |||
| 1 | diff --git a/test/jit/test_autodiff_subgraph_slicing.py b/test/jit/test_autodiff_subgraph_slicing.py | 1 | diff --git a/test/jit/test_autodiff_subgraph_slicing.py b/test/jit/test_autodiff_subgraph_slicing.py |
| 2 | -index ea36710..b5fcca2 100644 | 2 | +index ea36710..7b52227 100644 |
| 3 | --- a/test/jit/test_autodiff_subgraph_slicing.py | 3 | --- a/test/jit/test_autodiff_subgraph_slicing.py |
| 4 | +++ b/test/jit/test_autodiff_subgraph_slicing.py | 4 | +++ b/test/jit/test_autodiff_subgraph_slicing.py |
| 5 | -@@ -1,3 +1,6 @@ | 5 | +@@ -24,6 +24,7 @@ from torch.testing._internal.jit_utils import ( |
| 6 | -+import torch_npu | 6 | + disable_autodiff_subgraph_inlining, |
| 7 | -+from torch_npu.contrib import transfer_to_npu | 7 | + JitTestCase, |
| 8 | -+ | 8 | + ) |
| 9 | - # Owner(s): ["oncall: jit"] | 9 | ++import torch_npu # noqa: F401 |
| 10 | + | ||
| 11 | + | ||
| 12 | + if __name__ == "__main__": | ||
| 13 | +@@ -44,7 +45,7 @@ class TestAutodiffSubgraphSlicing(JitTestCase): | ||
| 14 | + with disable_autodiff_subgraph_inlining(): | ||
| 15 | + with enable_profiling_mode_for_profiling_tests(): | ||
| 16 | + ge = torch.jit.script(fn) | ||
| 17 | +- inputs = [torch.randn(size, requires_grad=True) for size in input_sizes] | ||
| 18 | ++ inputs = [torch.randn(size).npu().requires_grad_() for size in input_sizes] | ||
| 19 | + ge(*inputs, profile_and_replay=True) | ||
| 20 | + return ge.graph_for(*inputs) | ||
| 21 | + | ||
| 22 | +@@ -68,7 +69,7 @@ class TestAutodiffSubgraphSlicing(JitTestCase): | ||
| 23 | + x1, x2 = torch.chunk(x, 2) | ||
| 24 | + return (x1, x2) | ||
| 25 | + | ||
| 26 | +- input = torch.rand(6, 10).requires_grad_() | ||
| 27 | ++ input = torch.rand(6, 10).npu().requires_grad_() | ||
| 28 | + with disable_autodiff_subgraph_inlining(): | ||
| 29 | + with enable_profiling_mode_for_profiling_tests(): | ||
| 30 | + func(input, profile_and_replay=True) | ||
| 31 | +@@ -322,7 +323,7 @@ class TestAutodiffSubgraphSlicing(JitTestCase): | ||
| 32 | + # o --> x | ||
| 33 | + def fn(x, y, z): | ||
| 34 | + a = x * y | ||
| 35 | +- b = torch.zeros([abs(int(y))]) | ||
| 36 | ++ b = torch.zeros([abs(int(y))], device=y.device) | ||
| 37 | + return a, b | ||
| 38 | + | ||
| 39 | + graph = self._perform_ad_subgraph_slicing(fn, 1, 1, 1) | ||
| 40 | +@@ -382,7 +383,7 @@ class TestAutodiffSubgraphSlicing(JitTestCase): | ||
| 41 | + # \_________/ | ||
| 42 | + def fn(w, x, y): | ||
| 43 | + a = w * x | ||
| 44 | +- b = torch.zeros(abs(int(a))) | ||
| 45 | ++ b = torch.zeros(abs(int(a)), device=a.device) | ||
| 46 | + c = a * b | ||
| 47 | + return c | ||
| 48 | + | ||
| 49 | +@@ -395,7 +396,7 @@ class TestAutodiffSubgraphSlicing(JitTestCase): | ||
| 50 | + # \_________/ | ||
| 51 | + def fn(w, x, y, z): | ||
| 52 | + a = w * x | ||
| 53 | +- b = torch.zeros(abs(int(y))) | ||
| 54 | ++ b = torch.zeros(abs(int(y)), device=y.device) | ||
| 55 | + c = a * z | ||
| 56 | + return b, c | ||
| 57 | + | ||
| 58 | +@@ -410,7 +411,7 @@ class TestAutodiffSubgraphSlicing(JitTestCase): | ||
| 59 | + # \_________/ | ||
| 60 | + def fn(v, w, x, y): | ||
| 61 | + a = v * w | ||
| 62 | +- b = torch.ones(int(y)) | ||
| 63 | ++ b = torch.ones(int(y), device=y.device) | ||
| 64 | + c = b * a | ||
| 65 | + return a, c | ||
| 10 | 66 | ||
| 11 | - import os | ||
🟠 High Priority
在 4 个测试方法中(patch 第 38、48、58、68 行),
device_type = acc.type if (acc := torch.accelerator.current_accelerator()) else "cpu"被放置在fn函数体内部。这些fn随后被传入self._perform_ad_subgraph_slicing(fn, ...),该方法(patch 第 16 行)调用torch.jit.script(fn)对fn进行 TorchScript 编译。torch.accelerator.current_accelerator()是 Python 运行时 API,用于查询当前加速器状态。TorchScript 编译器无法编译此类动态 Python 调用,会在torch.jit.script(fn)阶段抛出类似torch.jit.Error或RuntimeError: Python call 'current_accelerator' is not supported in TorchScript的错误,导致测试用例直接失败。受影响的 4 处(均为相同根因):
对照:
_perform_ad_subgraph_slicing方法自身(第 18 行)采用了正确做法——在fn外部计算device_type,使 TorchScript 将其作为闭包常量捕获。4 处问题 hunk 应将device_type移至fn外部,与该正确模式保持一致。