已合并
profiler: set the msmonitor status to uninitialized when the polling subprocess ends #27273
xfeng创建于 2025年11月29日
profiler: set the msmonitor status to uninitialized when the polling subprocess ends #27273
已合并
xfeng创建于 2025年11月29日
共 2 个文件变更+11-1
@@ -244,4 +244,12 @@ def worker_dyno_func(params_dict):
244 except Exception as ex:244 except Exception as ex:
245 dynamic_profiler_utils.out_log("Dynamic profiler cfg bytes write failed, {} has occur!".format(str(ex)),245 dynamic_profiler_utils.out_log("Dynamic profiler cfg bytes write failed, {} has occur!".format(str(ex)),
246 dynamic_profiler_utils.LoggerLevelEnum.ERROR)246 dynamic_profiler_utils.LoggerLevelEnum.ERROR)
247+ if hasattr(py_dyno_monitor, "update_profiler_status"):
248+ profiler_status = {
O
OopenLiBingCI2025年11月29日

代码逻辑和结构: 在worker_dyno_func函数中,profiler_status字典的创建存在硬编码问题。该字典的结构与DynamicProfilerMonitor类中第26-31行定义的_profiler_status字典结构相同,但代码中重复定义了相同的字典结构,违反了DRY原则。

问题类型: 代码逻辑和结构 文件路径: torch_npu/profiler/_dynamic_profiler/_dynamic_profiler_monitor.py 行号: 248 问题代码:

profiler_status = {
    DynamicProfilerUtils.PROFILER_STATUS: str(DynamicProfilerUtils.ProfilerStatus.UNINITIALIZED.value),
    DynamicProfilerUtils.CURRENT_STEP: "-1",
    DynamicProfilerUtils.START_STEP: "-1",
    DynamicProfilerUtils.STOP_STEP: "-1",
}

修改建议:

建议将profiler_status的默认结构定义为一个常量或类方法,在DynamicProfilerMonitor类和worker_dyno_func函数中共享使用。例如在DynamicProfilerUtils类中添加一个静态方法来返回默认的profiler状态字典。

此评论由代码审查工具自动生成

likedislike
249+ DynamicProfilerUtils.PROFILER_STATUS: str(DynamicProfilerUtils.ProfilerStatus.UNINITIALIZED.value),
250+ DynamicProfilerUtils.CURRENT_STEP: "-1",
251+ DynamicProfilerUtils.START_STEP: "-1",
252+ DynamicProfilerUtils.STOP_STEP: "-1",
253+ }
254+ py_dyno_monitor.update_profiler_status(profiler_status)
247 dynamic_profiler_utils.out_log("Dynolog profiler process done", dynamic_profiler_utils.LoggerLevelEnum.INFO)255 dynamic_profiler_utils.out_log("Dynolog profiler process done", dynamic_profiler_utils.LoggerLevelEnum.INFO)
@@ -40,6 +40,7 @@ class _DynamicProfile:
40 self._step_record_time = None40 self._step_record_time = None
41 self._step_time = 041 self._step_time = 0
42 self._min_poll_interval = 142 self._min_poll_interval = 1
43+ self._max_poll_interval = 50
43 self._step_mstx_range_id = 044 self._step_mstx_range_id = 0
44 45 
45 def init(self):46 def init(self):
@@ -78,7 +79,8 @@ class _DynamicProfile:
78 if self.cur_step == self.RECORD_TIME_STEP:79 if self.cur_step == self.RECORD_TIME_STEP:
79 self._step_record_time = time.time()80 self._step_record_time = time.time()
80 elif self.cur_step - self.RECORD_TIME_STEP == 1:81 elif self.cur_step - self.RECORD_TIME_STEP == 1:
81- self._step_time = max(self._min_poll_interval, int(time.time() - self._step_record_time))82+ self._step_time = min(self._max_poll_interval,
83+ max(self._min_poll_interval, int(time.time() - self._step_record_time)))
82 self._dynamic_monitor.modify_step_time(self._step_time)84 self._dynamic_monitor.modify_step_time(self._step_time)
83 if self._step_mstx_range_id:85 if self._step_mstx_range_id:
84 mstx.range_end(self._step_mstx_range_id)86 mstx.range_end(self._step_mstx_range_id)