Merged
feat(frontend): Sync compute determinism from torch_npu #5622
songyangyang18Add on Aug 24
feat(frontend): Sync compute determinism from torch_npu #5622
Merged
songyangyang18Add on Aug 24
Total: 6 files changed+83-2
@@ -146,6 +146,7 @@
146 "use_max_freq_label": true146 "use_max_freq_label": true
147 }147 }
148 }148 }
149- }149+ },
150+ "compute_determinism_level": 0
150 }151 }
151}152}
@@ -1125,6 +1125,15 @@
1125 }1125 }
1126 }1126 }
1127 }1127 }
1128+ },
1129+ "compute_determinism_level": {
1130+ "type": "integer",
1131+ "default": 0,
1132+ "enum": [0, 1, 2],
1133+ "label": {
1134+ "zh": "计算确定性等级,与 torch_npu set_deterministic_level 对齐:0 关闭,1 确定性,2 强一致",
1135+ "en": "Compute determinism level synced with torch_npu set_deterministic_level: 0 off, 1 deterministic, 2 strong consistency"
1136+ }
1128 }1137 }
1129 }1138 }
1130 }1139 }
@@ -278,3 +278,11 @@ TEST_F(TestConfigManager, InvalidValue)
278 EXPECT_NE(msg.find("Expected int64"), std::string::npos);278 EXPECT_NE(msg.find("Expected int64"), std::string::npos);
279 }279 }
280}280}
281+ 
282+TEST_F(TestConfigManager, ComputeDeterminismLevelGlobalConfig)
283+{
284+ for (int level : {0, 1, 2}) {
285+ ConfigManagerNg::SetGlobalConfig("compute_determinism_level", static_cast<int64_t>(level));
286+ EXPECT_EQ(ConfigManagerNg::GetGlobalConfig<int64_t>("compute_determinism_level"), level);
287+ }
288+}
@@ -61,6 +61,17 @@ def get_npu_tensor_format(tensor):
61 return "ND"61 return "ND"
62 62 
63 63 
64+def get_torch_npu_compute_determinism_level() -> int:
65+ """Return torch_npu deterministic level (0/1/2); 0 when torch_npu is unavailable."""
66+ torch_npu = get_torch_npu()
67+ if torch_npu is None:
68+ return 0
69+ level = int(torch_npu.npu._get_deterministic_level())
70+ if level < 0 or level > 2:
71+ return 0
72+ return level
73+ 
74+ 
64def to_sym(value) -> pypto_impl.SymbolicScalar:75def to_sym(value) -> pypto_impl.SymbolicScalar:
65 if isinstance(value, int):76 if isinstance(value, int):
66 return pypto_impl.SymbolicScalar(value)77 return pypto_impl.SymbolicScalar(value)
@@ -25,7 +25,12 @@ import torch
25 25 
26import pypto26import pypto
27from pypto import pypto_impl27from pypto import pypto_impl
28-from pypto._utils import get_dtensor_type, get_npu_tensor_format, get_torch_npu28+from pypto._utils import (
29+ get_dtensor_type,
30+ get_npu_tensor_format,
31+ get_torch_npu,
32+ get_torch_npu_compute_determinism_level,
33+)
29from pypto.cost_model import _cost_model_run_once_data_from_host34from pypto.cost_model import _cost_model_run_once_data_from_host
30from pypto.error import FeError, _catch_and_wrap_error35from pypto.error import FeError, _catch_and_wrap_error
31from pypto.frontend.parser.diagnostics import Source36from pypto.frontend.parser.diagnostics import Source
@@ -1017,6 +1022,10 @@ class JitCallableWrapper:
1017 pypto.set_verify_options(**self._verify_options)1022 pypto.set_verify_options(**self._verify_options)
1018 if self._debug_options:1023 if self._debug_options:
1019 pypto.set_debug_options(**self._debug_options)1024 pypto.set_debug_options(**self._debug_options)
1025+ # Follow torch_npu deterministic level 0/1/2 (same idea as TorchAir / aclnn).
1026+ pypto.set_global_config(
1027+ "compute_determinism_level", get_torch_npu_compute_determinism_level()
1028+ )
1020 1029 
1021 def _run(1030 def _run(
1022 self,1031 self,
@@ -0,0 +1,43 @@
1+#!/usr/bin/env python3
2+# coding: utf-8
3+# Copyright (c) 2026 Huawei Technologies Co., Ltd.
4+# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
5+# CANN Open Software License Agreement Version 2.0 (the "License").
6+# Please refer to the License for details. You may not use this file except in compliance with the License.
7+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8+# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9+# See LICENSE in the root of the software repository for the full text of the License.
10+# -----------------------------------------------------------------------------------------------------------
11+"""Prove torch_npu deterministic level 0/1/2 is synced to global config."""
12+ 
13+import pytest
14+import torch_npu
15+ 
16+import pypto
17+from pypto._utils import get_torch_npu_compute_determinism_level
18+from pypto.frontend.parser.entry import JitCallableWrapper
19+ 
20+ 
21+def _dummy_kernel(x):
22+ return x
23+ 
24+ 
25+@pytest.fixture
26+def restore_determinism():
27+ prev_level = int(torch_npu.npu._get_deterministic_level())
28+ try:
29+ yield
30+ finally:
31+ torch_npu.npu.set_deterministic_level(prev_level)
32+ pypto.set_global_config("compute_determinism_level", 0)
33+ 
34+ 
35+def test_torch_npu_determinism_level_synced_to_global_config(restore_determinism):
36+ """torch_npu.set_deterministic_level -> set_global_config -> get_global_config."""
37+ wrapper = JitCallableWrapper(None, _dummy_kernel, None)
38+ 
39+ for level in (0, 1, 2):
40+ torch_npu.npu.set_deterministic_level(level)
41+ assert get_torch_npu_compute_determinism_level() == level
42+ wrapper._set_config_option()
43+ assert pypto.get_global_config("compute_determinism_level") == level