已合并
fix: 修复 inductor 编译子进程重复初始化 NPU 的问题 #44922
fix: 修复 inductor 编译子进程重复初始化 NPU 的问题 #44922
已合并
dingdairong创建于 13 天前
4 个文件变更+10-55
@@ -13,23 +13,6 @@ from torch.testing._internal.common_utils import (
13@unittest.skipIf(not torch.npu.is_available(), "requires an NPU device")13@unittest.skipIf(not torch.npu.is_available(), "requires an NPU device")
14class TestAscendcBasic(TestCase):14class TestAscendcBasic(TestCase):
15 15 
16- @classmethod
17- def setUpClass(cls):
18- super().setUpClass()
19- cls._ascendc_ok = False
20- try:
21- x = torch.randn(4, 4, device="npu")
22- torch.compile(lambda t: t + 1, backend="inductor",
23- options={"npu_backend": "ascendc"})(x)
24- cls._ascendc_ok = True
25- except Exception:
26- pass
27- 
28- def setUp(self):
29- super().setUp()
30- if not self._ascendc_ok:
31- self.skipTest("ascendc backend not available")
32- 
33 @parametrize("dtype", [torch.float32, torch.float16])16 @parametrize("dtype", [torch.float32, torch.float16])
34 def test_mul_sub(self, dtype):17 def test_mul_sub(self, dtype):
35 """Pointwise pattern: compiled output matches eager."""18 """Pointwise pattern: compiled output matches eager."""
@@ -62,20 +62,6 @@ def _temporary_env(name, value):
62 os.environ[name] = old_value62 os.environ[name] = old_value
63 63 
64 64 
65-def _is_ascendc_backend_available():
66- try:
67- x = torch.randn(4, 4, device="npu")
68- torch.compile(
69- lambda t: t + 1,
70- backend="inductor",
71- options={"npu_backend": "ascendc"},
72- )(x)
73- torch.npu.synchronize()
74- except Exception:
75- return False
76- return True
77- 
78- 
79def _run_dynamic_actual_seq_case():65def _run_dynamic_actual_seq_case():
80 import torch_npu._inductor.ascendc.config as ascendc_config66 import torch_npu._inductor.ascendc.config as ascendc_config
81 67 
@@ -166,15 +152,8 @@ def _assert_dynamic_actual_seq_key_reuse(test_case, logs):
166@unittest.skipIf(not torch.npu.is_available(), "requires an NPU device")152@unittest.skipIf(not torch.npu.is_available(), "requires an NPU device")
167class TestAscendcIFAv2DynamicActualSeq(TestCase):153class TestAscendcIFAv2DynamicActualSeq(TestCase):
168 154 
169- @classmethod
170- def setUpClass(cls):
171- super().setUpClass()
172- cls._ascendc_ok = _is_ascendc_backend_available()
173- 
174 def setUp(self):155 def setUp(self):
175 super().setUp()156 super().setUp()
176- if not self._ascendc_ok:
177- self.skipTest("ascendc backend not available")
178 torch._dynamo.reset()157 torch._dynamo.reset()
179 158 
180 def tearDown(self):159 def tearDown(self):
@@ -8,23 +8,6 @@ from torch.testing._internal.common_utils import run_tests, TestCase
8@unittest.skipIf(not torch.npu.is_available(), "requires an NPU device")8@unittest.skipIf(not torch.npu.is_available(), "requires an NPU device")
9class TestAscendcReduceOverhead(TestCase):9class TestAscendcReduceOverhead(TestCase):
10 10 
11- @classmethod
12- def setUpClass(cls):
13- super().setUpClass()
14- cls._ascendc_ok = False
15- try:
16- x = torch.randn(4, 4, device="npu")
17- torch.compile(lambda t: t + 1, backend="inductor",
18- options={"npu_backend": "ascendc"})(x)
19- cls._ascendc_ok = True
20- except Exception:
21- pass
22- 
23- def setUp(self):
24- super().setUp()
25- if not self._ascendc_ok:
26- self.skipTest("ascendc backend not available")
27- 
28 def test_capture_replay(self):11 def test_capture_replay(self):
29 """Verify reduce-overhead triggers graph capture and results are correct."""12 """Verify reduce-overhead triggers graph capture and results are correct."""
30 def fn(x, y):13 def fn(x, y):
@@ -1,4 +1,14 @@
1import os1import os
2+ 
3+ORG_AUTOLOAD = os.getenv("TORCH_DEVICE_BACKEND_AUTOLOAD", "1")
4+os.environ["TORCH_DEVICE_BACKEND_AUTOLOAD"] = "0"
5+from torch._inductor.async_compile import AsyncCompile
6+ 
7+ 
8+if os.environ.get("TORCH_WARM_POOL", "1") == "1":
9+ AsyncCompile.warm_pool()
10+os.environ["TORCH_DEVICE_BACKEND_AUTOLOAD"] = ORG_AUTOLOAD
11+ 
2from torch_npu.utils._dynamo import (12from torch_npu.utils._dynamo import (
3 _dynamo_register_interface_for_device,13 _dynamo_register_interface_for_device,
4 patch_SkipFunctionVariable,14 patch_SkipFunctionVariable,