ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

Triton Proton 命令行工具与性能分析查看器(proton / proton-viewer)完全指南

2026/9/13 6:15:09 拓冰建站 浏览量
Triton Proton 命令行工具与性能分析查看器(proton / proton-viewer)完全指南 Triton Proton 命令行工具与性能分析查看器proton / proton-viewer完全指南【免费下载链接】tritonDevelopment repository for the Triton language and compiler项目地址: https://gitcode.com/GitHub_Trending/tri/triton导读本文围绕 Triton 仓库中 Proton 性能分析子项目位于 third_party/proton的命令行工具展开系统讲解两条核心命令行链路用于零侵入式剖析 Python 脚本与 pytest 用例的proton命令以及用于终端内可视化、过滤、排序和对比 Hatchet 性能树的proton-viewer命令。阅读本文后你将掌握 Proton 命令行模式的全部参数语义、输出指标含派生指标的计算含义、正则过滤与格式化技巧、profile 差异对比以及 Chrome trace 的可视化方法并了解其背后的实现原理与测试佐证。一、proton命令零侵入式剖析入口1.1 设计动机不需要修改目标脚本proton命令的核心价值在于在不往目标脚本中插入任何proton.start/proton.finalize调用的情况下直接对 Python 脚本和 pytest 测试执行剖析。这在日常性能回归、命令行基准评测场景中非常实用——你不需要为了剖析而改动被测代码。其命令行入口为摘自 third_party/proton/docs/cli-and-viewer.mdproton [options] script.py [script_args] proton [options] pytest [pytest_args] python -m triton.profiler.proton [options] script.py [script_args]第三种形式通过 Python 模块方式调用与可执行文件proton等价。从 proton.py 的参数解析实现可以看出命令行选项之后的所有剩余参数target_args使用argparse.REMAINDER捕获会被原样透传给目标脚本或 pytest保证被测程序的参数不受影响。1.2 常用选项全解选项说明实现要点proton.py-n, --name剖析输出文件名不含后缀后缀由数据格式决定不指定时使用默认名proton若datatree则实际落盘为proton.hatchet见 profile.py 的DEFAULT_PROFILE_NAME-b, --backend剖析后端可选值来自libproton.get_available_profilers()不指定时自动选择见下文 1.4 节-c, --context剖析上下文shadow或python默认shadow使用 Proton 自定义 scope 名python则记录 Python 文件/函数/行调用路径-m, --mode后端专属模式如pcsampling、periodic_flushing等具体取决于后端-d, --data输出数据结构tree或trace默认treeHatchet 树trace输出 Chrome trace 格式-k, --hook剖析钩子当前支持triton用于采集 Triton kernel 启动元数据launch metadata1.3 命令行模式下的执行语义命令行模式有两条关键行为约定文档原文Proton 在目标运行之前启动、在目标退出之后完成finalize目标脚本内部的proton.start/proton.finalize调用会被忽略该模式下只有会话0是有效的。其实现机理在 proton.py 中清晰可见do_setup_and_execute首先执行flags.command_line True将进程级标志置位标志定义见 flags.py 的ProfilerFlags随后 profile.py 中的start()会检查flags.command_line若为真则直接返回 None忽略脚本内的一切start调用——这正是目标内 start 被忽略的底层实现run_profiling在do_setup_and_execute前后分别调用start(...)与finalize()实现运行前启动、运行后收尾目标脚本通过runpy.run_path(script, run_name__main__)在隔离环境中以__main__方式执行并在执行前把脚本所在目录追加到sys.path以支持相对导入proton.py。对于 pytest 场景is_pytest通过检查脚本基名是否为pytest来识别随后直接转调pytest.main(script_args)因此可以无缝对测试套件进行剖析。1.4 自动后端选择若未显式指定-bProton 会调用_select_backend()profile.py它读取当前 Triton runtime 活跃目标的后端triton.runtime.driver.active.get_current_target().backend再经libproton.select_profiler_from_triton_backend映射到对应的剖析器实现 NVIDIAcupti/ AMDrocprofiler、roctracer后端的透明选择。1.5 实操示例# 剖析一个 Python 脚本输出名为 dynamic_net proton -n dynamic_net tutorials/dynamic-net.py # 剖析 pytest 测试将输出写到 tests proton -n tests pytest third_party/proton/test/test_cmd.py -s --tbshort # 以 trace 数据模式剖析脚本输出 Chrome trace 文件 proton -n trace -d trace script.py # 使用 cupti 后端的 pcsampling 模式 proton -n samples -b cupti -m pcsampling script.py其中dynamic-net.py是仓库自带的 PyTorch 动态网络剖析示例位于 third_party/proton/tutorials/dynamic-net.py可作为命令行剖析的现成演练对象。二、proton-viewer终端内的 Hatchet 性能树查看器2.1 安装前提proton-viewer依赖 LLNL 的 Hatchet 库注意包名陷阱pip install llnl-hatchet文档特别强调必须使用llnl-hatchet因为pip install hatchet安装的是另一个 API 不兼容的包。这一点在 viewer.py 的导入逻辑中也有印证——导入失败时提示信息即为pip install llnl-hatchet。基本用法proton-viewer -m time/s profile_name.hatchet-m指定要显示的指标最多可同时显示两个指标用逗号分隔proton-viewer -m time/ns,time/% profile_name.hatchet第一个指标用于生成树形结构并参与阈值过滤第二个指标作为附加列展示。2.2 查看可用指标--listproton-viewer --list profile_name.hatchet该命令会打印输入文件中可用的原始raw指标分为 inclusive包含自身及子树打印为Available inclusive metrics和 exclusive仅当前帧打印为Available exclusive metrics两类实现见 viewer.py 的show_metrics。指标名大小写不敏感、可忽略单位后缀。除了原始指标proton-viewer还支持大量派生指标derived metrics即由原始指标实时计算所得常见的有时间类time/s、time/ms、time/us、time/ns平均时间类avg_time/s、avg_time/ms、avg_time/us、avg_time/ns即 time / count吞吐类flop/s、gflop/s、tflop/s以及按位宽细分的变体如tflop16/s对应 16 位浮点 FLOPs带宽类byte/s、gbyte/s、tbyte/s利用率util百分比metric/%即该帧指标值占总和的比例仅对 inclusive 指标可用如time/%这些派生指标的计算逻辑集中在 viewer.py 的derive_metrics中时间/带宽/FLOPs 指标通过单位换算因子time_factor_dict、bytes_factor_dict、default_flop_factor_dict等从原始指标推导util指标则综合了 FLOPS 与带宽两条瓶颈路径——先由 specs.py 的max_flops按设备架构、SM 数、时钟频率与位宽计算峰值算力和max_bps按总线位宽与显存时钟计算峰值带宽求出理论下界时间再取max(flops 耗时, bytes 耗时) / 实际耗时得到利用率其公式见 viewer.py。2.3 派生指标公式速查指标计算方式time/ns等原始 time 值 × 对应单位因子avg_time/ns等time / count次数flop16/s等flops16 / time再按单位换算byte/s等bytes / timeutilmax(Σflops_w / peak_flops_w 耗时, Σbytes / peak_bandwidth 耗时) / 实际耗时time/%帧 time / 总和 time × 100%三、过滤与格式化从海量帧中提取有效信息实际剖析结果往往包含大量框架与 warmup 帧proton-viewer提供了四类过滤/格式化手段实现见 viewer.py 的filter_frames底层使用 Hatchet 的 Cypher 风格查询3.1 包含过滤-i保留经过匹配帧的整条路径proton-viewer -m time/ns -i .*matmul.* profile_name.hatchet保留所有经过名字匹配该正则的帧的路径即只要路径上任一帧命中.*matmul.*整条路径都保留squash 为单一路径。3.2 排除过滤-e剔除匹配帧及其子树proton-viewer -m time/ns -e .*warmup.* profile_name.hatchet剔除匹配帧及其所有子节点常用于过滤预热warmup开销。注意-i与-e不能同时使用viewer.py 中对此有显式校验。3.3 阈值过滤-t隐藏低耗时帧proton-viewer -m time/ns -t 1000 profile_name.hatchet隐藏第一个显示指标低于阈值本例为 1000ns的帧。该过滤只作用于第一个指标且通过 Hatchet 的[*, {metric: threshold}]查询实现。3.4 限制树深度-dproton-viewer -m time/ns -d 4 profile_name.hatchet只显示前 4 层树深度默认深度为 100见 viewer.py。3.5 帧名格式化-fproton-viewer -m time/ns -f file_function_line profile_name.hatchet支持四种帧格式viewer.py 中通过choices限定格式含义full默认完整路径 文件名 函数名 行号file_function_line文件名 函数名 行号function_line仅函数名 行号file_function文件名 函数名格式化实现见 viewer.py 的format_frames例如file_function_line只取名称的最后一个/分隔段file_function则输出文件名函数名的紧凑形式。3.6 按指标值排序输出--print-sortedproton-viewer -m time/ns profile_name.hatchet --print-sorted默认输出按调用时序树遍历顺序排列加上该选项后会额外按第一个指标的值降序输出一份 kernel 排行表viewer.py便于快速定位热点 kernel——这是日常性能分析最常用的选项之一。四、Profile 对比--diff-profile性能优化离不开前后对比。proton-viewer内置差异分析proton-viewer -m time/ns --diff-profile before.hatchet after.hatchet其语义为current_profile[metric] - diff_profile[metric]即用主输入文件当前 profile减去--diff-profile指定的基准 profile逐帧计算指标差值正值表示该帧指标增加如耗时变长负值表示改善。实现上apply_diff_profile 会先对两个 profile 分别做相同的指标派生与过滤处理保持 derived 指标口径一致再调用 Hatchet 的gf.sub(gf2)完成树级相减。该选项也支持-diff作为短别名viewer.py。五、Trace 可视化Chrome trace 输出当需要时间线视图而非调用树时使用datatraceproton.start(profile_name, datatrace)剖析完成后会生成profile_name.chrome_trace文件可拖入chrome://tracing或 Perfetto UI 中查看各 kernel 与 scope 的时序分布。两点补充源自 profile.py 与文档finalize支持output_format参数可选hatchet、hatchet_msgpack、chrome_trace即也可以在 finalize 阶段显式指定输出格式当使用插桩后端instrumentation backend剖析 kernel 内部时间线时同样会输出 trace 格式数据相关说明见 intra-kernel.md。六、与 Python API 的关系及测试佐证6.1 命令行是 Python API 的外壳proton命令本质上是 Python 剖析 APIproton.start/finalize/activate/deactivate详见 python-api.md的命令行封装run_profiling内部就是依次调用start(...)与finalize()proton.py。因此理解命令行选项也就掌握了 Python API 的核心参数反之Python API 中的scope、hook、state等能力在命令行模式下同样可用例如-k triton对应 Python 侧的hooktriton启动元数据钩子。6.2 仓库内的验证命令行入口与查看器的行为均有对应测试proton的命令行解析与 pytest 透传由 third_party/proton/test/test_cmd.py 覆盖proton-viewer的指标派生、过滤与格式化由 third_party/proton/test/test_viewer.py 覆盖可运行示例位于 third_party/proton/tutorials/matmul.pyTriton matmul 剖析 launch metadata 指令采样与 third_party/proton/tutorials/dynamic-net.pyPyTorch 动态网络 scope。七、典型工作流总结一个完整的定位热点 → 过滤噪音 → 对比验证命令行工作流如下# 1. 剖析基线 proton -n before tutorials/matmul.py # 2. 剖析优化后版本 proton -n after tutorials/matmul.py # 3. 查看优化后 profile只保留 matmul 相关路径过滤 warmup # 按耗时排序并附带利用率 proton-viewer -m time/ns,util -i .*matmul.* -e .*warmup.* after.hatchet --print-sorted # 4. 对比优化前后差异 proton-viewer -m time/ns --diff-profile before.hatchet after.hatchet整个过程无需修改任何被测脚本输出文件.hatchet/.chrome_trace均为文本/JSON 格式可直接进入版本管理或 CI 性能回归流程。相关后端与模式细节可进一步参考 backends-and-modes.md 与 periodic-profiling.md。【免费下载链接】tritonDevelopment repository for the Triton language and compiler项目地址: https://gitcode.com/GitHub_Trending/tri/triton创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考