掘金 人工智能 前天 10:44
【TVM 教程】PAPI 入门
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文介绍了如何在 Apache TVM 中使用 PAPI(Performance Application Programming Interface)进行性能分析。PAPI 库提供了对 CPU、GPU 等硬件的底层性能指标的访问,如缓存未命中、循环计数和浮点运算等。文章详细阐述了 PAPI 的安装、配置,以及如何在 TVM 的编译和运行时环境中集成和使用 PAPI 进行性能数据收集和分析。通过示例代码展示了如何使用 PAPIMetricCollector 收集特定指标,帮助开发者优化深度学习模型的性能。

🚀 PAPI 简介:PAPI 是一个库,用于在各种平台上提供性能计数器,它能提供处理器行为的底层信息,如总循环计数、缓存未命中等,这些指标在性能分析时非常有用。

⚙️ PAPI 安装:PAPI 可以通过包管理器或从源代码安装。推荐从标记版本(如 papi-6-0-0-1-t)安装,以避免构建问题。安装完成后,需要配置 TVM 以使用 PAPI。

🛠️ TVM 中使用 PAPI:在 TVM 构建过程中,通过设置 `USE_PAPI` 选项为 `ON` 来启用 PAPI。如果 PAPI 安装在非标准位置,可以通过 `USE_PAPI` 指定其位置。然后,可以使用 `tvm.runtime.profiling.PAPIMetricCollector` 收集性能指标。

📊 性能指标收集:通过 `tvm.runtime.GraphModule.profile()` 函数,结合 `PAPIMetricCollector`,可以收集 TVM 中各个函数的性能数据。可以指定要收集的指标,例如浮点运算 (PAPI_FP_OPS),`papi_avail` 和 `papi_native_avail` 命令可以列出可用的指标。

Apache TVM 是一个深度的深度学习编译框架,适用于 CPU、GPU 和各种机器学习加速芯片。更多 TVM 中文文档可访问 →tvm.hyper.ai/

性能应用程序编程接口(Performance Application Programming Interface,简称 PAPI)是一个可在各种平台上提供性能计数器的库。在指定的运行期间,性能计数器提供处理器行为的准确底层信息,包含简单的指标,如总循环计数、缓存未命中和执行的指令,以及更高级的信息(如总 FLOPS 和 warp 占用率)。PAPI 的这些指标在 profiling 时可用。

安装 PAPI

PAPI 可以用包管理器(Ubuntu 上用 apt-get install libpapi-dev 命令)来安装,也可以从 源代码 安装。

由于之前从源代码 pull 最新版本的 PAPI 导致了构建问题,因此推荐 checkout 标记版本 papi-6-0-0-1-t

使用 PAPI 构建 TVM

若要在 TVM 构建中包含 PAPI,在 config.cmake 中设置:

set(USE_PAPI ON)

若 PAPI 安装在非标准位置,可指定它的位置:

set(USE_PAPI path/to/papi.pc)

在 Profiling 时使用 PAPI

若 TVM 是用 PAPI 构建的(见上文),可将 tvm.runtime.profiling.PAPIMetricCollector 传给 tvm.runtime.GraphModule.profile() 来收集性能指标:

import tvmfrom tvm import relayfrom tvm.relay.testing import mlpfrom tvm.runtime import profiler_vmimport numpy as nptarget = "llvm"dev = tvm.cpu()mod, params = mlp.get_workload(1)exe = relay.vm.compile(mod, target, params=params)vm = profiler_vm.VirtualMachineProfiler(exe, dev)data = tvm.nd.array(np.random.rand(1, 1, 28, 28).astype("float32"), device=dev)report = vm.profile(    data,    func_name="main",    collectors=[tvm.runtime.profiling.PAPIMetricCollector()],)print(report)
Name                                    perf::CACHE-MISSES   perf::CYCLES  perf::STALLED-CYCLES-BACKEND  perf::INSTRUCTIONS  perf::STALLED-CYCLES-FRONTENDfused_nn_dense_nn_bias_add_nn_relu                   2,494      1,570,698                        85,608             675,564                         39,583fused_nn_dense_nn_bias_add_nn_relu_1                 1,149        655,101                        13,278             202,297                         21,380fused_nn_dense_nn_bias_add                             288        600,184                         8,321             163,446                         19,513fused_nn_batch_flatten                                 301        587,049                         4,636             158,636                         18,565fused_nn_softmax                                       154        575,143                         8,018             160,738                         18,995----------Sum                                                  4,386      3,988,175                       119,861           1,360,681                        118,036Total                                               10,644      8,327,360                       179,310           2,660,569                        270,044

还可以指定收集哪些指标:

report = vm.profile(    data,    func_name="main",    collectors=[tvm.runtime.profiling.PAPIMetricCollector({dev: ["PAPI_FP_OPS"])],)
Name                                  PAPI_FP_OPSfused_nn_dense_nn_bias_add_nn_relu        200,832fused_nn_dense_nn_bias_add_nn_relu_1       16,448fused_nn_dense_nn_bias_add                  1,548fused_nn_softmax                              160fused_nn_batch_flatten                          0----------Sum                                       218,988Total                                     218,988

运行 papi_avail 和 papi_native_avail 命令可得到可用指标列表。

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

Apache TVM PAPI 性能分析 深度学习
相关文章