已合并
[sync] PR-39678: revert: detect Ascend950 SoC for TORCH_NPU_USE_COMPATIBLE_IMPL default #39725
ascend-robot创建于 6月30日
[sync] PR-39678: revert: detect Ascend950 SoC for TORCH_NPU_USE_COMPATIBLE_IMPL default #39725
已合并
共 2 个文件变更+0-61
| @@ -1,42 +0,0 @@ | |||
| 1 | -# Owner(s): ["module: npu"] | ||
| 2 | - | ||
| 3 | -import os | ||
| 4 | -import unittest | ||
| 5 | - | ||
| 6 | -import torch_npu | ||
| 7 | -from torch_npu.testing.testcase import run_tests | ||
| 8 | - | ||
| 9 | - | ||
| 10 | -class TestCompatibleImplSocDetect(unittest.TestCase): | ||
| 11 | - | ||
| 12 | - def test_is_ascend950_returns_bool(self): | ||
| 13 | - result = torch_npu._is_ascend950() | ||
| 14 | - self.assertIsInstance(result, bool) | ||
| 15 | - | ||
| 16 | - def test_is_ascend950_matches_cpp_soc_version(self): | ||
| 17 | - # Cross-check with C++ GetSocVersion(): Ascend950 = 260 | ||
| 18 | - soc_version = torch_npu._C._npu_get_soc_version() | ||
| 19 | - expected = soc_version >= 260 | ||
| 20 | - actual = torch_npu._is_ascend950() | ||
| 21 | - self.assertEqual(actual, expected, | ||
| 22 | - f"_is_ascend950()={actual} but GetSocVersion()={soc_version} (>=260 is {expected})") | ||
| 23 | - | ||
| 24 | - def test_compatible_impl_default_matches_soc(self): | ||
| 25 | - # When env var not pre-set, TORCH_NPU_USE_COMPATIBLE_IMPL should match SoC | ||
| 26 | - if "TORCH_NPU_USE_COMPATIBLE_IMPL" in os.environ: | ||
| 27 | - self.skipTest("TORCH_NPU_USE_COMPATIBLE_IMPL was pre-set in environment") | ||
| 28 | - is_950 = torch_npu._is_ascend950() | ||
| 29 | - val = os.environ.get("TORCH_NPU_USE_COMPATIBLE_IMPL", "0") | ||
| 30 | - self.assertEqual(val == "1", is_950) | ||
| 31 | - | ||
| 32 | - def test_compatible_impl_respects_user_value(self): | ||
| 33 | - # When env var was pre-set, it should be preserved | ||
| 34 | - if "TORCH_NPU_USE_COMPATIBLE_IMPL" not in os.environ: | ||
| 35 | - self.skipTest("TORCH_NPU_USE_COMPATIBLE_IMPL was not pre-set") | ||
| 36 | - user_val = os.environ["TORCH_NPU_USE_COMPATIBLE_IMPL"] | ||
| 37 | - # Verify the env var wasn't overwritten | ||
| 38 | - self.assertIn(user_val, ("0", "1")) | ||
| 39 | - | ||
| 40 | - | ||
| 41 | -if __name__ == "__main__": | ||
| 42 | - run_tests() | ||
| @@ -5,25 +5,6 @@ import ctypes | |||
| 5 | import os | 5 | import os |
| 6 | 6 | ||
| 7 | 7 | ||
| 8 | -def _is_ascend950(): | ||
| 9 | - try: | ||
| 10 | - import ctypes | ||
| 11 | - lib = ctypes.CDLL("libascendcl.so") | ||
| 12 | - lib.aclrtGetSocName.restype = ctypes.c_char_p | ||
| 13 | - soc_name = lib.aclrtGetSocName() | ||
| 14 | - if soc_name and soc_name.decode().startswith("Ascend950"): | ||
| 15 | - return True | ||
| 16 | - except Exception as e: | ||
| 17 | - import logging | ||
| 18 | - logging.getLogger("torch_npu").debug( | ||
| 19 | - f"Failed to detect SoC version via aclrtGetSocName: {e}") | ||
| 20 | - return False | ||
| 21 | - | ||
| 22 | - | ||
| 23 | -# Set TORCH_NPU_USE_COMPATIBLE_IMPL default based on SoC version before C++ init | ||
| 24 | -if "TORCH_NPU_USE_COMPATIBLE_IMPL" not in os.environ: | ||
| 25 | - os.environ["TORCH_NPU_USE_COMPATIBLE_IMPL"] = "1" if _is_ascend950() else "0" | ||
| 26 | - | ||
| 27 | # Disable autoloading before running 'import torch' to avoid circular dependencies | 8 | # Disable autoloading before running 'import torch' to avoid circular dependencies |
| 28 | ORG_AUTOLOAD = os.getenv("TORCH_DEVICE_BACKEND_AUTOLOAD", "1") | 9 | ORG_AUTOLOAD = os.getenv("TORCH_DEVICE_BACKEND_AUTOLOAD", "1") |
| 29 | os.environ["TORCH_DEVICE_BACKEND_AUTOLOAD"] = "0" | 10 | os.environ["TORCH_DEVICE_BACKEND_AUTOLOAD"] = "0" |