已合并
feat: Supports enabling environment variable log #28016
kuhn7创建于 2025年12月13日
feat: Supports enabling environment variable log #28016
已合并
kuhn7创建于 2025年12月13日
8 个文件变更+153-39
Atest/utils/test_patch_getenv.py+55-0
@@ -0,0 +1,55 @@
1+import os
2+import sys
3+import subprocess
4+ 
5+import torch_npu
6+from torch_npu.testing.testcase import TestCase, run_tests
7+ 
8+ 
9+def _run_in_subprocess(enable_env: bool):
10+ code = r"""
11+import os
12+ 
13+os.environ["FOO_TEST"] = "bar"
14+ 
15+import torch_npu._logging # 按 env 初始化
16+from torch_npu.utils import patch_getenv # 触发 patch
17+ 
18+_ = os.getenv("FOO_TEST")
19+_ = os.environ.get("FOO_TEST")
20+"""
21+ 
22+ env = os.environ.copy()
23+ env.pop("TORCH_NPU_LOGS", None)
24+ env.pop("TORCH_LOGS", None)
25+ env["FOO_TEST"] = "bar"
26+ 
27+ if enable_env:
28+ env["TORCH_NPU_LOGS"] = "env"
29+ 
30+ p = subprocess.run(
31+ [sys.executable, "-c", code],
32+ env=env,
33+ stdout=subprocess.PIPE,
34+ stderr=subprocess.PIPE,
35+ text=True,
36+ )
37+ out = (p.stdout or "") + (p.stderr or "")
38+ return p.returncode, out
39+ 
40+ 
41+class TestPatchGetenv(TestCase):
42+ def test_env_log_when_enabled_env(self):
43+ rc, out = _run_in_subprocess(enable_env=True)
44+ self.assertTrue(rc == 0, f"subprocess failed rc={rc}\n{out}")
45+ self.assertIn("get env FOO_TEST = bar", out)
46+ 
47+ def test_no_env_log_when_disabled_env(self):
48+ rc, out = _run_in_subprocess(enable_env=False)
49+ self.assertTrue(rc == 0, f"subprocess failed rc={rc}\n{out}")
50+ self.assertNotIn("FOO_TEST = bar", out)
51+ self.assertNotIn("get env", out)
52+ 
53+ 
54+if __name__ == "__main__":
55+ run_tests()
Mtorch_npu/__init__.py+1-0
@@ -65,6 +65,7 @@ import torch_npu.optim
65import torch_npu.dynamo65import torch_npu.dynamo
66import torch_npu._C66import torch_npu._C
67import torch_npu._logging67import torch_npu._logging
68+from torch_npu.utils import patch_getenv
68import torch_npu._afd69import torch_npu._afd
69from torch_npu import profiler70from torch_npu import profiler
70from torch_npu.npu.amp.sharded_grad_scaler import _ShardedGradScaler71from torch_npu.npu.amp.sharded_grad_scaler import _ShardedGradScaler
Mtorch_npu/_logging/_internal.py+1-0
@@ -39,3 +39,4 @@ def _add_logging_module():
39 torch._logging._internal.register_log("recovery", "torch_npu.recovery")39 torch._logging._internal.register_log("recovery", "torch_npu.recovery")
40 torch._logging._internal.register_log("op_plugin", "torch_npu.op_plugin")40 torch._logging._internal.register_log("op_plugin", "torch_npu.op_plugin")
41 torch._logging._internal.register_log("shmem", "torch_npu.symmetric_memory")41 torch._logging._internal.register_log("shmem", "torch_npu.symmetric_memory")
42+ torch._logging._internal.register_log("env", "torch_npu.env")
Mtorch_npu/csrc/core/npu/register/OptionsManager.cpp+43-31
@@ -16,12 +16,24 @@
16#include "torch_npu/csrc/npu/memory_snapshot.h"16#include "torch_npu/csrc/npu/memory_snapshot.h"
17#include "torch_npu/csrc/core/npu/NpuVariables.h"17#include "torch_npu/csrc/core/npu/NpuVariables.h"
18#include "torch_npu/csrc/core/npu/GetCANNInfo.h"18#include "torch_npu/csrc/core/npu/GetCANNInfo.h"
19+#include "torch_npu/csrc/logging/LogContext.h"
19 20 
20namespace c10_npu {21namespace c10_npu {
21namespace option {22namespace option {
22 23 
23using namespace std;24using namespace std;
24 25 
26+static std::shared_ptr<npu_logging::Logger> loggerEnv = npu_logging::logging().getLogger("torch_npu.env");
27+ 
28+char* get_and_log_env(const char* env_str)
29+{
30+ char* env_val = std::getenv(env_str);
31+ if (env_val != nullptr) {
32+ loggerEnv->info("get env %s = %s", env_str, env_val);
33+ }
34+ return env_val;
35+}
36+ 
25bool OptionsManager::IsHcclZeroCopyEnable()37bool OptionsManager::IsHcclZeroCopyEnable()
26{38{
27 const static bool isHcclZeroCopyEnable = []() -> bool {39 const static bool isHcclZeroCopyEnable = []() -> bool {
@@ -47,7 +59,7 @@ bool OptionsManager::IsResumeModeEnable()
47ReuseMode OptionsManager::GetMultiStreamMemoryReuse()59ReuseMode OptionsManager::GetMultiStreamMemoryReuse()
48{60{
49 const static ReuseMode reuseMode = []() -> ReuseMode {61 const static ReuseMode reuseMode = []() -> ReuseMode {
50- char *env_val = std::getenv("MULTI_STREAM_MEMORY_REUSE");62+ char *env_val = get_and_log_env("MULTI_STREAM_MEMORY_REUSE");
51 int64_t envFlag = (env_val != nullptr) ? strtol(env_val, nullptr, 10) : 1;63 int64_t envFlag = (env_val != nullptr) ? strtol(env_val, nullptr, 10) : 1;
52 ReuseMode mode = ERASE_RECORD_STREAM;64 ReuseMode mode = ERASE_RECORD_STREAM;
53 switch (envFlag) {65 switch (envFlag) {
@@ -139,21 +151,21 @@ bool OptionsManager::CheckAclDumpDateEnable()
139 151 
140int OptionsManager::GetBoolTypeOption(const char* env_str, int defaultVal)152int OptionsManager::GetBoolTypeOption(const char* env_str, int defaultVal)
141{153{
142- char* env_val = std::getenv(env_str);154+ char* env_val = get_and_log_env(env_str);
143 int64_t envFlag = (env_val != nullptr) ? strtol(env_val, nullptr, 10) : defaultVal;155 int64_t envFlag = (env_val != nullptr) ? strtol(env_val, nullptr, 10) : defaultVal;
144 return (envFlag != 0) ? 1 : 0;156 return (envFlag != 0) ? 1 : 0;
145}157}
146 158 
147uint32_t OptionsManager::GetHCCLConnectTimeout()159uint32_t OptionsManager::GetHCCLConnectTimeout()
148{160{
149- char* env_val = std::getenv("HCCL_CONNECT_TIMEOUT");161+ char* env_val = get_and_log_env("HCCL_CONNECT_TIMEOUT");
150 int64_t envFlag = (env_val != nullptr) ? strtol(env_val, nullptr, 10) : 0;162 int64_t envFlag = (env_val != nullptr) ? strtol(env_val, nullptr, 10) : 0;
151 return static_cast<uint32_t>(envFlag);163 return static_cast<uint32_t>(envFlag);
152}164}
153 165 
154int32_t OptionsManager::GetHCCLExecTimeout()166int32_t OptionsManager::GetHCCLExecTimeout()
155{167{
156- char* env_val = std::getenv("HCCL_EXEC_TIMEOUT");168+ char* env_val = get_and_log_env("HCCL_EXEC_TIMEOUT");
157 int64_t envFlag;169 int64_t envFlag;
158 if (env_val != nullptr) {170 if (env_val != nullptr) {
159 envFlag = strtol(env_val, nullptr, 10);171 envFlag = strtol(env_val, nullptr, 10);
@@ -169,7 +181,7 @@ int32_t OptionsManager::GetHCCLExecTimeout()
169 181 
170int32_t OptionsManager::GetHCCLEventTimeout()182int32_t OptionsManager::GetHCCLEventTimeout()
171{183{
172- char* env_val = std::getenv("HCCL_EVENT_TIMEOUT");184+ char* env_val = get_and_log_env("HCCL_EVENT_TIMEOUT");
173 int64_t envFlag;185 int64_t envFlag;
174 if (env_val != nullptr) {186 if (env_val != nullptr) {
175 envFlag = strtol(env_val, nullptr, 10);187 envFlag = strtol(env_val, nullptr, 10);
@@ -185,14 +197,14 @@ int32_t OptionsManager::GetHCCLEventTimeout()
185 197 
186int32_t OptionsManager::GetACLExecTimeout()198int32_t OptionsManager::GetACLExecTimeout()
187{199{
188- char* env_val = std::getenv("ACL_STREAM_TIMEOUT");200+ char* env_val = get_and_log_env("ACL_STREAM_TIMEOUT");
189 int64_t envFlag = (env_val != nullptr) ? strtol(env_val, nullptr, 10) : -1;201 int64_t envFlag = (env_val != nullptr) ? strtol(env_val, nullptr, 10) : -1;
190 return static_cast<int32_t>(envFlag);202 return static_cast<int32_t>(envFlag);
191}203}
192 204 
193int32_t OptionsManager::GetACLDeviceSyncTimeout()205int32_t OptionsManager::GetACLDeviceSyncTimeout()
194{206{
195- char* env_val = std::getenv("ACL_DEVICE_SYNC_TIMEOUT");207+ char* env_val = get_and_log_env("ACL_DEVICE_SYNC_TIMEOUT");
196 int64_t timeout = -1;208 int64_t timeout = -1;
197 if (env_val != nullptr) {209 if (env_val != nullptr) {
198 int64_t envFlag = strtol(env_val, nullptr, 10);210 int64_t envFlag = strtol(env_val, nullptr, 10);
@@ -205,7 +217,7 @@ int32_t OptionsManager::GetACLDeviceSyncTimeout()
205 217 
206uint32_t OptionsManager::CheckUseHcclAsyncErrorHandleEnable()218uint32_t OptionsManager::CheckUseHcclAsyncErrorHandleEnable()
207{219{
208- char* asyncErrorHandling_val = std::getenv("HCCL_ASYNC_ERROR_HANDLING");220+ char* asyncErrorHandling_val = get_and_log_env("HCCL_ASYNC_ERROR_HANDLING");
209 int64_t asyncErrorHandlingFlag =221 int64_t asyncErrorHandlingFlag =
210 (asyncErrorHandling_val != nullptr) ? strtol(asyncErrorHandling_val, nullptr, 10) : 1;222 (asyncErrorHandling_val != nullptr) ? strtol(asyncErrorHandling_val, nullptr, 10) : 1;
211 std::unordered_map<int32_t, std::string> asyncErrorHandlingMode = getAsyncErrorHandlingMode();223 std::unordered_map<int32_t, std::string> asyncErrorHandlingMode = getAsyncErrorHandlingMode();
@@ -217,7 +229,7 @@ uint32_t OptionsManager::CheckUseHcclAsyncErrorHandleEnable()
217 229 
218uint32_t OptionsManager::CheckUseDesyncDebugEnable()230uint32_t OptionsManager::CheckUseDesyncDebugEnable()
219{231{
220- char* desyncDebug_val = std::getenv("HCCL_DESYNC_DEBUG");232+ char* desyncDebug_val = get_and_log_env("HCCL_DESYNC_DEBUG");
221 int64_t desyncDebugFlag = (desyncDebug_val != nullptr) ? strtol(desyncDebug_val, nullptr, 10) : 0;233 int64_t desyncDebugFlag = (desyncDebug_val != nullptr) ? strtol(desyncDebug_val, nullptr, 10) : 0;
222 std::unordered_map<int32_t, std::string> desyncDebugMode = getDesyncDebugMode();234 std::unordered_map<int32_t, std::string> desyncDebugMode = getDesyncDebugMode();
223 if (desyncDebugMode.find(desyncDebugFlag) == desyncDebugMode.end()) {235 if (desyncDebugMode.find(desyncDebugFlag) == desyncDebugMode.end()) {
@@ -229,7 +241,7 @@ uint32_t OptionsManager::CheckUseDesyncDebugEnable()
229bool OptionsManager::isACLGlobalLogOn(aclLogLevel level)241bool OptionsManager::isACLGlobalLogOn(aclLogLevel level)
230{242{
231 const static int getACLGlobalLogLevel = []() -> int {243 const static int getACLGlobalLogLevel = []() -> int {
232- char* env_val = std::getenv("ASCEND_GLOBAL_LOG_LEVEL");244+ char* env_val = get_and_log_env("ASCEND_GLOBAL_LOG_LEVEL");
233 int64_t envFlag = (env_val != nullptr) ? strtol(env_val, nullptr, 10) : ACL_ERROR;245 int64_t envFlag = (env_val != nullptr) ? strtol(env_val, nullptr, 10) : ACL_ERROR;
234 std::unordered_map<int32_t, std::string> logLevelMode = getLogLevelMode();246 std::unordered_map<int32_t, std::string> logLevelMode = getLogLevelMode();
235 if (logLevelMode.find(envFlag) == logLevelMode.end()) {247 if (logLevelMode.find(envFlag) == logLevelMode.end()) {
@@ -242,14 +254,14 @@ bool OptionsManager::isACLGlobalLogOn(aclLogLevel level)
242 254 
243int64_t OptionsManager::GetRankId()255int64_t OptionsManager::GetRankId()
244{256{
245- char* rankId_val = std::getenv("RANK");257+ char* rankId_val = get_and_log_env("RANK");
246 int64_t rankId = (rankId_val != nullptr) ? strtol(rankId_val, nullptr, 10) : -1;258 int64_t rankId = (rankId_val != nullptr) ? strtol(rankId_val, nullptr, 10) : -1;
247 return rankId;259 return rankId;
248}260}
249 261 
250char *OptionsManager::GetNslbPath()262char *OptionsManager::GetNslbPath()
251{263{
252- return std::getenv("NSLB_CP");264+ return get_and_log_env("NSLB_CP");
253}265}
254 266 
255bool OptionsManager::CheckStatusSaveEnable()267bool OptionsManager::CheckStatusSaveEnable()
@@ -263,7 +275,7 @@ bool OptionsManager::CheckStatusSaveEnable()
263 275 
264std::string OptionsManager::GetStatusSavePath() noexcept276std::string OptionsManager::GetStatusSavePath() noexcept
265{277{
266- char* status_save_val = std::getenv("TORCH_HCCL_STATUS_SAVE_PATH");278+ char* status_save_val = get_and_log_env("TORCH_HCCL_STATUS_SAVE_PATH");
267 std::string status_save_path = (status_save_val != nullptr) ? std::string(status_save_val) : "/tmp";279 std::string status_save_path = (status_save_val != nullptr) ? std::string(status_save_val) : "/tmp";
268 return status_save_path;280 return status_save_path;
269}281}
@@ -271,7 +283,7 @@ std::string OptionsManager::GetStatusSavePath() noexcept
271uint32_t OptionsManager::GetStatusSaveInterval()283uint32_t OptionsManager::GetStatusSaveInterval()
272{284{
273 const static uint32_t status_save_interval = []() -> uint32_t {285 const static uint32_t status_save_interval = []() -> uint32_t {
274- char* env_val = std::getenv("TORCH_HCCL_STATUS_SAVE_INTERVAL");286+ char* env_val = get_and_log_env("TORCH_HCCL_STATUS_SAVE_INTERVAL");
275 int64_t envFlag = 2;287 int64_t envFlag = 2;
276 if (env_val != nullptr) {288 if (env_val != nullptr) {
277 envFlag = strtol(env_val, nullptr, 10);289 envFlag = strtol(env_val, nullptr, 10);
@@ -288,7 +300,7 @@ uint32_t OptionsManager::GetStatusSaveInterval()
288uint32_t OptionsManager::GetNslbCntVal()300uint32_t OptionsManager::GetNslbCntVal()
289{301{
290 const static uint32_t nslb_val = []() -> uint32_t {302 const static uint32_t nslb_val = []() -> uint32_t {
291- char* nslb_num = std::getenv("NSLB_MAX_RECORD_NUM");303+ char* nslb_num = get_and_log_env("NSLB_MAX_RECORD_NUM");
292 int64_t nslb_val = (nslb_num != nullptr) ? strtol(nslb_num, nullptr, 10) : 1000;304 int64_t nslb_val = (nslb_num != nullptr) ? strtol(nslb_num, nullptr, 10) : 1000;
293 return static_cast<uint32_t>(nslb_val);305 return static_cast<uint32_t>(nslb_val);
294 }();306 }();
@@ -341,7 +353,7 @@ std::unordered_map<std::string, std::string> OptionsManager::ParsePerfConfig(con
341 353 
342bool OptionsManager::CheckPerfDumpEnable()354bool OptionsManager::CheckPerfDumpEnable()
343{355{
344- char* perf_dump_config = std::getenv("PERF_DUMP_CONFIG");356+ char* perf_dump_config = get_and_log_env("PERF_DUMP_CONFIG");
345 if (perf_dump_config != nullptr) {357 if (perf_dump_config != nullptr) {
346 std::unordered_map<std::string, std::string> config_dict = ParsePerfConfig(perf_dump_config);358 std::unordered_map<std::string, std::string> config_dict = ParsePerfConfig(perf_dump_config);
347 auto it = config_dict.find("enable");359 auto it = config_dict.find("enable");
@@ -354,7 +366,7 @@ bool OptionsManager::CheckPerfDumpEnable()
354 366 
355std::string OptionsManager::GetPerfDumpPath()367std::string OptionsManager::GetPerfDumpPath()
356{368{
357- char* perf_dump_path = std::getenv("PERF_DUMP_PATH");369+ char* perf_dump_path = get_and_log_env("PERF_DUMP_PATH");
358 if (perf_dump_path != nullptr) {370 if (perf_dump_path != nullptr) {
359 return std::string(perf_dump_path);371 return std::string(perf_dump_path);
360 } else {372 } else {
@@ -364,7 +376,7 @@ std::string OptionsManager::GetPerfDumpPath()
364 376 
365std::string OptionsManager::GetRankTableFilePath()377std::string OptionsManager::GetRankTableFilePath()
366{378{
367- char* rank_table_file = std::getenv("RANK_TABLE_FILE");379+ char* rank_table_file = get_and_log_env("RANK_TABLE_FILE");
368 if (rank_table_file != nullptr) {380 if (rank_table_file != nullptr) {
369 return std::string(rank_table_file);381 return std::string(rank_table_file);
370 } else {382 } else {
@@ -375,7 +387,7 @@ std::string OptionsManager::GetRankTableFilePath()
375uint32_t OptionsManager::GetSilenceCheckFlag()387uint32_t OptionsManager::GetSilenceCheckFlag()
376{388{
377 const static uint32_t silence_check_flag = []() -> uint32_t {389 const static uint32_t silence_check_flag = []() -> uint32_t {
378- char* silence_check_flag_str = std::getenv("NPU_ASD_ENABLE");390+ char* silence_check_flag_str = get_and_log_env("NPU_ASD_ENABLE");
379 int64_t silence_check_flag = (silence_check_flag_str != nullptr) ? strtol(silence_check_flag_str, nullptr, 10) : 0;391 int64_t silence_check_flag = (silence_check_flag_str != nullptr) ? strtol(silence_check_flag_str, nullptr, 10) : 0;
380 SilenceCheckMode mode = CHECK_CLOSE;392 SilenceCheckMode mode = CHECK_CLOSE;
381 switch (silence_check_flag) {393 switch (silence_check_flag) {
@@ -421,7 +433,7 @@ std::vector<std::string> OptionsManager::Split(const std::string& input, char de
421std::pair<double, double> OptionsManager::GetSilenceThresh(const std::string& env_str,433std::pair<double, double> OptionsManager::GetSilenceThresh(const std::string& env_str,
422 std::pair<double, double> defaultThresh)434 std::pair<double, double> defaultThresh)
423{435{
424- char* upper_thresh_ptr = std::getenv(env_str.c_str());436+ char* upper_thresh_ptr = get_and_log_env(env_str.c_str());
425 std::string upper_thresh_str = (upper_thresh_ptr != nullptr) ? std::string(upper_thresh_ptr) : "";437 std::string upper_thresh_str = (upper_thresh_ptr != nullptr) ? std::string(upper_thresh_ptr) : "";
426 std::vector<std::string> split_result = Split(upper_thresh_str, ',');438 std::vector<std::string> split_result = Split(upper_thresh_str, ',');
427 if (split_result.size() != 2) {439 if (split_result.size() != 2) {
@@ -461,7 +473,7 @@ std::pair<double, double> OptionsManager::GetSilenceSigmaThresh()
461uint32_t OptionsManager::GetHcclBufferSize()473uint32_t OptionsManager::GetHcclBufferSize()
462{474{
463 const static uint32_t hccl_buf_size = []() -> uint32_t {475 const static uint32_t hccl_buf_size = []() -> uint32_t {
464- char* buf_val = std::getenv("HCCL_BUFFSIZE");476+ char* buf_val = get_and_log_env("HCCL_BUFFSIZE");
465 // Default 200M477 // Default 200M
466 int64_t buf_size = (buf_val != nullptr) ? strtol(buf_val, nullptr, 10) : 200;478 int64_t buf_size = (buf_val != nullptr) ? strtol(buf_val, nullptr, 10) : 200;
467 TORCH_CHECK(buf_size > 0, "HCCL_BUFFSIZE should be positive.", PTA_ERROR(ErrCode::VALUE));479 TORCH_CHECK(buf_size > 0, "HCCL_BUFFSIZE should be positive.", PTA_ERROR(ErrCode::VALUE));
@@ -473,7 +485,7 @@ uint32_t OptionsManager::GetHcclBufferSize()
473uint32_t OptionsManager::GetP2PBufferSize()485uint32_t OptionsManager::GetP2PBufferSize()
474{486{
475 const static uint32_t buf_size = []() -> uint32_t {487 const static uint32_t buf_size = []() -> uint32_t {
476- char* buf_val = std::getenv("P2P_HCCL_BUFFSIZE");488+ char* buf_val = get_and_log_env("P2P_HCCL_BUFFSIZE");
477 // Default 0M489 // Default 0M
478 int64_t buf_size_ = (buf_val != nullptr) ? strtol(buf_val, nullptr, 10) : 20;490 int64_t buf_size_ = (buf_val != nullptr) ? strtol(buf_val, nullptr, 10) : 20;
479 TORCH_CHECK(buf_size_ >= 0, "P2P_HCCL_BUFFSIZE cannot be negative.", PTA_ERROR(ErrCode::VALUE));491 TORCH_CHECK(buf_size_ >= 0, "P2P_HCCL_BUFFSIZE cannot be negative.", PTA_ERROR(ErrCode::VALUE));
@@ -485,7 +497,7 @@ uint32_t OptionsManager::GetP2PBufferSize()
485uint32_t OptionsManager::GetAclOpInitMode()497uint32_t OptionsManager::GetAclOpInitMode()
486{498{
487 const static uint32_t acl_op_init_mode = []() -> uint32_t {499 const static uint32_t acl_op_init_mode = []() -> uint32_t {
488- char* buf_val = std::getenv("ACL_OP_INIT_MODE");500+ char* buf_val = get_and_log_env("ACL_OP_INIT_MODE");
489 // Default 1 for A2/A3; Default 0 for others501 // Default 1 for A2/A3; Default 0 for others
490 static bool default_value_acl_mode = ((c10_npu::GetSocVersion() >= c10_npu::SocVersion::Ascend910B1) &&502 static bool default_value_acl_mode = ((c10_npu::GetSocVersion() >= c10_npu::SocVersion::Ascend910B1) &&
491 (c10_npu::GetSocVersion() < c10_npu::SocVersion::Ascend310B1)) ||503 (c10_npu::GetSocVersion() < c10_npu::SocVersion::Ascend310B1)) ||
@@ -520,7 +532,7 @@ uint32_t OptionsManager::GetAclOpInitMode()
520uint32_t OptionsManager::GetStreamsPerDevice()532uint32_t OptionsManager::GetStreamsPerDevice()
521{533{
522 const static uint32_t streams_per_device = []() -> uint32_t {534 const static uint32_t streams_per_device = []() -> uint32_t {
523- char* buf_val = std::getenv("STREAMS_PER_DEVICE");535+ char* buf_val = get_and_log_env("STREAMS_PER_DEVICE");
524 // Default 32536 // Default 32
525 int64_t streams_per_device = (buf_val != nullptr) ? strtol(buf_val, nullptr, 10) : 32;537 int64_t streams_per_device = (buf_val != nullptr) ? strtol(buf_val, nullptr, 10) : 32;
526 if (streams_per_device != 8 && streams_per_device != 32) {538 if (streams_per_device != 8 && streams_per_device != 32) {
@@ -534,7 +546,7 @@ uint32_t OptionsManager::GetStreamsPerDevice()
534 546 
535char* OptionsManager::GetCpuAffinityConf()547char* OptionsManager::GetCpuAffinityConf()
536{548{
537- return std::getenv("CPU_AFFINITY_CONF");549+ return get_and_log_env("CPU_AFFINITY_CONF");
538}550}
539 551 
540uint32_t OptionsManager::GetTaskQueueEnable()552uint32_t OptionsManager::GetTaskQueueEnable()
@@ -543,7 +555,7 @@ uint32_t OptionsManager::GetTaskQueueEnable()
543 return 0;555 return 0;
544 }556 }
545 const static uint32_t task_queue_enable = []() -> uint32_t {557 const static uint32_t task_queue_enable = []() -> uint32_t {
546- char* env_val = std::getenv("TASK_QUEUE_ENABLE");558+ char* env_val = get_and_log_env("TASK_QUEUE_ENABLE");
547 int64_t task_queue_enable = (env_val != nullptr) ? strtol(env_val, nullptr, 10) : 1;559 int64_t task_queue_enable = (env_val != nullptr) ? strtol(env_val, nullptr, 10) : 1;
548 std::unordered_map<int32_t, std::string> taskQueueEnableMode = getTaskQueueEnableMode();560 std::unordered_map<int32_t, std::string> taskQueueEnableMode = getTaskQueueEnableMode();
549 if (taskQueueEnableMode.find(task_queue_enable) == taskQueueEnableMode.end()) {561 if (taskQueueEnableMode.find(task_queue_enable) == taskQueueEnableMode.end()) {
@@ -561,7 +573,7 @@ uint32_t OptionsManager::GetPerStreamQueue()
561 }573 }
562 574 
563 const static uint32_t per_stream_queue = []() -> uint32_t {575 const static uint32_t per_stream_queue = []() -> uint32_t {
564- char* env_val = std::getenv("PER_STREAM_QUEUE");576+ char* env_val = get_and_log_env("PER_STREAM_QUEUE");
565 int64_t per_stream_queue = (env_val != nullptr) ? strtol(env_val, nullptr, 10) : 0;577 int64_t per_stream_queue = (env_val != nullptr) ? strtol(env_val, nullptr, 10) : 0;
566 return static_cast<uint32_t>(per_stream_queue);578 return static_cast<uint32_t>(per_stream_queue);
567 }();579 }();
@@ -583,7 +595,7 @@ bool OptionsManager::CheckForceUncached()
583 595 
584std::string OptionsManager::GetOomSnapshotDumpPath()596std::string OptionsManager::GetOomSnapshotDumpPath()
585{597{
586- char* sanpshot_dump_path = std::getenv("OOM_SNAPSHOT_PATH");598+ char* sanpshot_dump_path = get_and_log_env("OOM_SNAPSHOT_PATH");
587 std::string dump_path = "./";599 std::string dump_path = "./";
588 if (sanpshot_dump_path != nullptr) {600 if (sanpshot_dump_path != nullptr) {
589 dump_path = std::string(sanpshot_dump_path);601 dump_path = std::string(sanpshot_dump_path);
@@ -598,7 +610,7 @@ std::string OptionsManager::GetOomSnapshotDumpPath()
598bool OptionsManager::ShouldPrintWarning()610bool OptionsManager::ShouldPrintWarning()
599{611{
600 static bool should_print = []() {612 static bool should_print = []() {
601- char* disabled_warning = std::getenv("TORCH_NPU_DISABLED_WARNING");613+ char* disabled_warning = get_and_log_env("TORCH_NPU_DISABLED_WARNING");
602 if (disabled_warning != nullptr && strtol(disabled_warning, nullptr, 10) == 1) {614 if (disabled_warning != nullptr && strtol(disabled_warning, nullptr, 10) == 1) {
603 return false;615 return false;
604 }616 }
@@ -637,7 +649,7 @@ void oom_observer(int64_t device, int64_t allocated, int64_t device_total, int64
637bool OptionsManager::IsOomSnapshotEnable()649bool OptionsManager::IsOomSnapshotEnable()
638{650{
639 static bool isFirstCall = true;651 static bool isFirstCall = true;
640- const static char *env_val = std::getenv("OOM_SNAPSHOT_ENABLE");652+ const static char *env_val = get_and_log_env("OOM_SNAPSHOT_ENABLE");
641 int64_t envFlag = (env_val != nullptr) ? strtol(env_val, nullptr, 10) : 0;653 int64_t envFlag = (env_val != nullptr) ? strtol(env_val, nullptr, 10) : 0;
642#ifndef BUILD_LIBTORCH654#ifndef BUILD_LIBTORCH
643 if (isFirstCall) {655 if (isFirstCall) {
@@ -674,7 +686,7 @@ uint64_t OptionsManager::GetShmemSymmetricSize()
674 static uint64_t symmetricSize = []() -> uint64_t {686 static uint64_t symmetricSize = []() -> uint64_t {
675 static uint64_t defaultMemSize = 1024ULL * 1024 * 1024;687 static uint64_t defaultMemSize = 1024ULL * 1024 * 1024;
676 688 
677- char *env_val = std::getenv("NPU_SHMEM_SYMMETRIC_SIZE");689+ char *env_val = get_and_log_env("NPU_SHMEM_SYMMETRIC_SIZE");
678 if (env_val == nullptr) {690 if (env_val == nullptr) {
679 return defaultMemSize;691 return defaultMemSize;
680 }692 }
Mtorch_npu/csrc/core/npu/register/OptionsManager.h+1-0
@@ -146,6 +146,7 @@ private:
146};146};
147 147 
148void oom_observer(int64_t device = 0, int64_t allocated = 0, int64_t device_total = 0, int64_t device_free = 0);148void oom_observer(int64_t device = 0, int64_t allocated = 0, int64_t device_total = 0, int64_t device_free = 0);
149+char* get_and_log_env(const char* env_str);
149 150 
150} // namespace option151} // namespace option
151} // namespace c10_npu152} // namespace c10_npu
Mtorch_npu/csrc/distributed/ProcessGroupHCCL.cpp+2-0
@@ -89,6 +89,7 @@ bool force_stop_error_flag = false;
89const char* nslb_path = c10_npu::option::OptionsManager::GetNslbPath();89const char* nslb_path = c10_npu::option::OptionsManager::GetNslbPath();
90bool status_save_enable = c10_npu::option::OptionsManager::CheckStatusSaveEnable();90bool status_save_enable = c10_npu::option::OptionsManager::CheckStatusSaveEnable();
91std::string status_save_path = c10_npu::option::OptionsManager::GetStatusSavePath();91std::string status_save_path = c10_npu::option::OptionsManager::GetStatusSavePath();
92+std::shared_ptr<npu_logging::Logger> loggerEnv = npu_logging::logging().getLogger("torch_npu.env");
92 93 
93inline c10_npu::NPUStream getNPUStreamByCurrentType(c10::DeviceIndex device = -1)94inline c10_npu::NPUStream getNPUStreamByCurrentType(c10::DeviceIndex device = -1)
94{95{
@@ -268,6 +269,7 @@ bool getDeterministicState()
268 // The env variable has a higher priority.269 // The env variable has a higher priority.
269 const char* envValue = std::getenv("HCCL_DETERMINISTIC");270 const char* envValue = std::getenv("HCCL_DETERMINISTIC");
270 if (envValue != nullptr) {271 if (envValue != nullptr) {
272+ loggerEnv->info("get env HCCL_DETERMINISTIC = %s", envValue);
271 std::string valueStr(envValue);273 std::string valueStr(envValue);
272 std::transform(valueStr.begin(), valueStr.end(), valueStr.begin(), ::tolower);274 std::transform(valueStr.begin(), valueStr.end(), valueStr.begin(), ::tolower);
273 if (valueStr == "true") {275 if (valueStr == "true") {
Mtorch_npu/csrc/framework/interface/MstxInterface.cpp+14-8
@@ -3,6 +3,7 @@
3#include "torch_npu/csrc/core/npu/register/FunctionLoader.h"3#include "torch_npu/csrc/core/npu/register/FunctionLoader.h"
4#include "torch_npu/csrc/core/npu/npu_log.h"4#include "torch_npu/csrc/core/npu/npu_log.h"
5#include "torch_npu/csrc/toolkit/profiler/common/utils.h"5#include "torch_npu/csrc/toolkit/profiler/common/utils.h"
6+#include "torch_npu/csrc/logging/LogContext.h"
6 7 
7namespace at_npu {8namespace at_npu {
8namespace native {9namespace native {
@@ -32,19 +33,24 @@ LOAD_FUNCTION(mstxMemRegionsUnregister)
32// save python range id with cann mstx range id.33// save python range id with cann mstx range id.
33// when mstx.range_end(id) is called, we can check if this id is invalid34// when mstx.range_end(id) is called, we can check if this id is invalid
34static std::unordered_map<int, mstxRangeId> g_rangeIdMap;35static std::unordered_map<int, mstxRangeId> g_rangeIdMap;
36+static std::shared_ptr<npu_logging::Logger> loggerEnv = npu_logging::logging().getLogger("torch_npu.env");
35 37 
36static std::mutex g_mutex;38static std::mutex g_mutex;
37 39 
38static bool IsSupportMstxFuncImpl()40static bool IsSupportMstxFuncImpl()
39{41{
40- bool isSupport = false;42+ static auto checkSupport = []() -> bool {
41- char* path = std::getenv("ASCEND_HOME_PATH");43+ char* path = std::getenv("ASCEND_HOME_PATH");
42- if (path != nullptr) {44+ if (path != nullptr) {
43- std::string soPath = std::string(path) + "/lib64/libms_tools_ext.so";45+ loggerEnv->info("get env ASCEND_HOME_PATH = %s", path);
44- soPath = torch_npu::toolkit::profiler::Utils::RealPath(soPath);46+ std::string soPath = std::string(path) + "/lib64/libms_tools_ext.so";
45- isSupport = !soPath.empty();47+ soPath = torch_npu::toolkit::profiler::Utils::RealPath(soPath);
46- }48+ return !soPath.empty();
47- return isSupport;49+ }
50+ return false;
51+ };
52+ 
53+ return checkSupport();
48}54}
49 55 
50static bool IsSupportMstxDomainFuncImpl()56static bool IsSupportMstxDomainFuncImpl()
Atorch_npu/utils/patch_getenv.py+36-0
@@ -0,0 +1,36 @@
1+import os
2+import logging
3+ 
4+_seen = set()
5+ 
6+_orig_getenv = os.getenv
7+_orig_environ_get = os.environ.get
8+loggerEnv = logging.getLogger("torch_npu.env")
9+ 
10+ 
11+def _log_once(key: str, val):
12+ if key in _seen:
13+ return
14+ _seen.add(key)
15+ loggerEnv.info(f"get env {key} = {val}")
16+ 
17+ 
18+def _patched_getenv(key, default=None):
19+ hit = key in os.environ
20+ val = _orig_getenv(key, default)
21+ if hit and isinstance(val, str) and val != "":
22+ _log_once(key, val)
23+ return val
24+ 
25+ 
26+def _patched_environ_get(key, default=None):
27+ hit = key in os.environ
28+ val = _orig_environ_get(key, default)
29+ if hit and isinstance(val, str) and val != "":
30+ _log_once(key, val)
31+ return val
32+ 
33+ 
34+# patch on import
35+os.getenv = _patched_getenv
36+os.environ.get = _patched_environ_get