Pull Request已成功合入, 合并人@ascend-robot
(感谢 ilya_a 的贡献)变更摘要
本 PR 主要为 torch_npu profiler(基于 acl_prof)增加了对 PyTorch ExecutionTraceObserver 对象的支持:在 profile 类中新增 execution_trace_observer 参数,使 npu_profiler 在启动和停止时同步调用观测器的 start() 与 stop(),从而实现与 PyTorch 社区 profiler 一致的 Execution Trace 采集行为,并新增了相应的单元测试。
主要改动
- 新增
execution_trace_observer接口参数:在torch_npu/profiler/profiler.py的profile类构造函数中引入execution_trace_observer: _ITraceObserver | None = None,用于接收调用方传入的ExecutionTraceObserver对象,并将其保存到self.execution_trace_observer。 - 联动启动与停止 Execution Trace 观测:在
profile.start()中当execution_trace_observer存在时调用其start(),在profile.stop()中调用其stop(),使观测时间窗口与npu_profiler保持一致。 - 引入
_ITraceObserver类型:从torch.profiler.profiler导入_ITraceObserver作为新参数的类型标注。 - 新增 Execution Trace 单元测试:新增
test/profiler/test_npu_execution_trace.py,覆盖aten::gather的tensor_range记录,以及通过set_extra_resource_collection(True)和ENABLE_PYTORCH_EXECUTION_TRACE_SAVE_INTEGRAL_TENSOR_DATA验证 integral tensor 数据落盘(.dat文件)的功能。


代码审查
审查总结
已审查文件:
test/profiler/test_npu_execution_trace.py—— 发现 4 个问题(1 个 P2,3 个 P3)torch_npu/profiler/profiler.py—— 无问题(execution_trace_observer参数的传递、start()/stop()中 observer 的启停时序均与 PyTorch 社区 profiler 语义一致;类型注解_ITraceObserver | None与本仓库其它文件已使用的 PEP 604 语法一致,无兼容性问题)
问题计数(按优先级):
- P0:0
- P1:0
- P2:1
- P3:3
整体风险判断:
生产代码改动(torch_npu/profiler/profiler.py)逻辑正确、风险低。主要问题集中在新增测试文件:其中使用 torch.profiler.schedule 而非 torch_npu.profiler.schedule 会因枚举类型不匹配使 NPU profiler 采集静默不启动,导致第二个测试无法真正验证本 PR 的集成点(P2);另有环境变量泄漏、断言空转、死代码三个测试质量问题(P3)。建议至少修复 P2 的 schedule 误用,确保测试真正覆盖 NPU profiler 与 execution_trace_observer 的联动。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 0 |
| 🟡 建议 | 1 |
💬 仅评论


The following users do not have permission to comment /lgtm or /approve on any module in this PR:
eejiechu


Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.




【合入来源】
【修改方案】
Re-used existing PyTorch Execution Trace Observer object with torch_npu profiler (based on acl_prof).
Migrated relevant unit tests from original PyTorch test_execution_trace.py
【资料变更】
New parameter is added to npu_profiler - execution_trace_observer.
Execution Trace Observer implementation with torch_npu profiler must work the same way as with PyTorch community profiler.
【接口变更】
New parameter is added to npu_profiler - execution_trace_observer.
execution_trace_observer (ExecutionTraceObserver) : A PyTorch Execution Trace Observer object.
PyTorch Execution Traces <https://arxiv.org/pdf/2305.14516.pdf>__ offer a graph basedrepresentation of AI/ML workloads and enable replay benchmarks, simulators, and emulators.
When this argument is included the observer start() and stop() will be called for the
same time window as npu_profiler.
It is the same Execution Trace Observer as the one used by PyTorch community profiler.
【功能验证】
Build, install, run:
export MAX_JOBS=2
TORCH_DEVICE_BACKEND_AUTOLOAD=0 bash ci/build.sh --python=3.11
pip install --no-deps --force-reinstall dist/torch_npu-*.whl
ASCEND_LAUNCH_BLOCKING=1 pytest -s test_npu_execution_trace.py
Test results
【CheckList】