Ascend profiler collect adaption

From kali20gakki/msAgent · View interactive version

Ascend profiler collect adaption is an AI agent skill from the GitHub repository kali20gakki/msAgent. Adapt torch_npu.profiler interface for AI frameworks and training/inference code on Ascend NPU. Use this skill whenever users need to integrate profiling, add performance data collection, or instrument PyTorch code with torch_npu.profiler on Huawei Ascend NPU platforms. Triggers on: "torch_npu profi

How to use this skill

Skill file

torch_npu.profiler 接口适配

将 torch_npu.profiler 采集接口适配到 AI 框架或训练/推理代码中,采集 NPU 算子、kernel 及内存等性能数据。按以下三步完成,每步向用户确认后继续。

第一步:`torch_npu.profiler` 先验知识

torch_npu.profiler 是昇腾 NPU 平台上的性能分析工具,用于采集 PyTorch 训练或在线推理场景中的 PyTorch 算子、CANN 算子、底层 NPU 算子及内存占用等性能数据。

参数说明、使用方式(with 语句 / start/stop)详见 CODE2,关键约束见下方"规则"。

第二步:定位注入点

普通单卡场景

无封装层,适合快速验证或单机调试。配置直接写在脚本中,运行即采集,无外部控制机制。

import torch_npu

experimental_config = torch_npu.profiler._ExperimentalConfig(
    export_type=[torch_npu.profiler.ExportType.Text],
    profiler_level=torch_npu.profiler.ProfilerLevel.Level0,
    aic_metrics=torch_npu.profiler.AiCMetrics.AiCoreNone,
)

prof = torch_npu.profiler.profile(
    activities=[ProfilerActivity.CPU, ProfilerActivity.NPU],
    schedule=torch_npu.profiler.schedule(wait=0, warmup=0, active=1, repeat=1, skip_first=1),
    on_trace_ready=torch_npu.profiler.tensorboard_trace_handler("./result"),
    experimental_config=experimental_config,
)

prof.start()
for _ in range(10):
    train_one_step()
    prof.step()
prof.stop()

训练场景

推理场景

强化学习场景

第三步:验证输出(可选阶段)

先向用户确认是否需要验证 torch_npu.profiler 接口适配后的实际采集效果。

AI 任务拉起脚本/启动命令适配 profiler

在用户提供的 AI 任务拉起脚本、启动命令或对应封装入口中补充 torch_npu.profiler 相关配置参数,并确保这些参数能够透传到 torch_npu.profiler 的核心注入位置。

需要注意的是:

profiler 交付件校验

告知用户运行完成后,按实际导出类型检查输出目录中的关键文件,text类型交付件与DB类型交付件不要求同时存在:

{OUTPUTDIR}/*/ASCEND_PROFILER_OUTPUT/
├── trace_view.json      # 文本类交付件:全量 trace
├── op_statistic.csv     # 文本类交付件:算子统计
├── kernel_details.csv   # 文本类交付件:kernel 详情
├── ascend_pytorch_profiler_*.db  # DB 类交付件
└── ...

text类型交付件校验示例:

ls {OUTPUTDIR}/*/ASCEND_PROFILER_OUTPUT/trace_view.json
ls {OUTPUTDIR}/*/ASCEND_PROFILER_OUTPUT/op_statistic.csv
ls {OUTPUTDIR}/*/ASCEND_PROFILER_OUTPUT/kernel_details.csv

DB类型交付件校验示例:

ls {OUTPUTDIR}/*/ASCEND_PROFILER_OUTPUT/ascend_pytorch_profiler_*.db

具体的 profiler 交付件完整性校验方法,可参考 ascend-profiler-data-validation。

规则

Reference 说明

| reference 文件 | 主要内容 | 何时按需读取 |

|---|---|---|

| CODE0 | 训练场景适配范例,说明训练流程中的典型注入点、参数组织方式和调用链路。 | 当任务属于训练框架、训练脚本或单机/分布式训练适配时读取。 |

| CODE0 | 推理场景适配范例,说明服务化推理中的参数透传、封装层调用和控制链路。 | 当任务属于推理框架、在线服务或多进程推理适配时读取。 |

| CODE0 | 强化学习场景适配范例,说明分阶段解耦控制、阶段内参数配置和 Trainer / Rollout / Actor 协同方式。 | 当任务属于强化学习场景,或涉及 rollout、actor、ref 等阶段化 profiler 控制时读取。 |

| CODE0 | torch_npu.profiler 关键 API、常用参数、_ExperimentalConfig 字段及基础使用方式。 | 当需要确认接口定义、参数含义、默认值、with / start-stop 用法时读取。 |

按需读取原则:先根据任务场景选择对应 reference;仅在需要确认接口或参数细节时补充读取 references/api_reference.md,避免无关 reference 混读。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.