顺晟科技
2023-02-23 10:04:25
184
这个回答来自TVM官方英语文件Lianmin Zheng,Chengfan Jia。更多TVM中文文档可用 https://tvm.hyper.ai/
本教程将演示TVM的自动计划功能。这是在不创建自定义模板的情况下找到最佳计划的方法。
与基于模板的AutoTVM使用手动模板定义搜索空间不同,auto-scheduler不需要模板。用户只需编写计算声明,无需schedule命令或模板。自动调度程序可以自动生成较大的搜索空间,并在空间中找到最佳调度程序。
本教程使用矩阵乘法作为示例。
本教程不能在Windows或更高版本的macOS上运行。要执行,请将本教程的正文设置为if name==& amp# 039;_ _ main _ _ & amp# 039;放置在代码块中。import OS import numpy as NP import tvm from tvm import te,auto _ scheduler定义矩阵乘法,首先使用偏移加法定义矩阵乘法。这里使用TVM张量表达式语言的标准操作。主要区别在于,在函数定义之上使用register_workload装饰器。此函数必须返回输入/输出张量列表。通过这些张量,auto-scheduler可以得到完整的计算图。
@ auto _ scheduler . register _ workload # note the auto _ scheduler decorator def mat mul _ add(n,l,m Dtype# 039;b & amp# 039;dtype=dtype)c=te . placer Name=& amp;# 039;k & amp# 039;)matmul=te.compute ((n,m),Lamba I,j: te.sum (a [I,k] * b * # enable自动布局轨迹)例如,大小为1024x1024的两个矩阵乘法。然后N=L=M=1024和dtype=& amp# 039;float32 & amp# 039;创建搜索任务
使用自定义目标提高性能
为了使TVM能够充分利用特定硬件平台,必须手动指定CPU功能。例如:
使用AVX2:将下面的llvm替换为llvm -mcpu=core-avx2
使用AVX-512:以下llvm为llvm-mcpu=sky lake-AVX 512 target=tvm . target . target(& amp;# 039;llvm & amp# 039;)替换为N=l=m=1024 task=tvm . auto _ scheduler . search task(func=matmul _ add,args=(n,l)
computational Dag:a=placeholder[1024,1024] b=placeholder [1024,1024] matmul (I,j)=J)
Num_measure_trials表示搜索期间可用的测试尝试次数。在本教程中,为了快速演示,只进行了10次实验。实际上,1000是搜索收敛的最佳数量。可以根据自己的时间预算进行更多的实验。
此外,还使用RecordToFile将测试记录写入matmul.json文件。测试记录可用于优化查询历史记录、恢复搜索和以后的进一步分析。
有关其他参数,请参阅TuningOptions
Log _ file=& amp# 039;matmul.json & amp# 039;tune _ option=auto _ scheduler . tuning options(num _ measure _;Verbose=2,)开始搜索准备好所有输入后,将开始搜索,自动调度程序将发挥其作用。经过一些测试实验后,可以从日志文件中加载和应用最佳计划。
#运行自动教学(搜索)task.tune(tune_option)#应用最佳计划sch,args=task.apply _ best(自动教学)
print(& amp;# 039;loweredtir:& amp;# 039;)print (tvm.lower (sch,args,simple _ mode=true))输出结果:
lowered TIR:@ main=prim fn(a _ 1:handle,b _ 1: handle,c _ 1: handle,out _ 1: handle)# 039;& amp# 039;main & amp# 039;& amp# 039;tir.no alias & amp# 039;true } buffers={ a:buffer(a _ 2:pool float 32,[1048576],[]),c: buffer (c _)# 039;parallel & amp# 039;4) {for (ax7: int32,0,8){ auto _ scheduler _ layout _ transform _ 1:buffer(auto \# 039;parallel & amp# 039;{ allocate(For(I . outer . inner:int 32,0,2)){ mat mul _ 1:buffer(mat mul,float32x8,[4],[4]4){ letcse _ var _ 2:int 32=((floor mod(I . outer . outer . j . outer . fused)128)* 8192)128)* 8192)(I . outer . inner * 4096)(k . outer * 4))k . inner){ mat mul _ 1[;8)* auto _ scheduler _ layout _ transform _ 1[ramp(CSE _ var _ 2、1、8)]) matmut 8 8)]} } } 4){ letcse _ var _ 3:int 32=(((floor div(I . outer . outer . j . outer . fuse)
Func=tvm.build (sch、args、target)a _ NP=NP . random . uniform(size=(n,l))Mastype(NP . float 32)c _ NP=NP . random . uniform(size=(n,m))。astype(Device=dev)B _ tvm=tvm . nd . array(B _ NP,Device=dev)c _ tvm=tvm . nd . array B \# 039;execution time of this operator:% . 3 f ms & amp;# 039;% (np.median (evaluan))
Execution time of this operator:在93.286ms日志文件使用情况搜索期间,所有测试记录都存储在日志文件matmul.json中。测试记录可用于重新应用搜索结果、恢复搜索和执行其他分析。
以下是从文件加载最佳时间表和打印相应Python时间表API的示例。可用于调试和学习自动调度器的行为。
print(& amp;# 039;equivalent python schedule:& amp;# 039;)print (task.print _ best (log _ file))输出结果:
equi valent python schedule:mat mul _ I、matmul _ j、mat mul _ k=tuple(mat mul . op . axis)tuple Out _split (matmul _ I _ o _ I,factor=1)mat mul _ I mat mul _ j _ I=s[mat mul]。split (matmul _ j,factor=8)mat mul _ j _ o _ o _ I mat mul _ j _ o _ o _ o _ I=ssplit (matmul _ j _ o _ o _ I,Factor=1)matmul Factor=4)s[matmul]。reorder (matmul _ I _ o _ o _ o、matmul _ j _ o _ o、)Matmul _ I _ I、matmul _ jsplit ( Out _ j _ I=s [out])。split (out _ j,factor=8) out _ j _ o _ o,out _ j _;Out _ j _ o _ I、out _ I _ I、out _ j _ I) s [matmul]。compute _ at(s[Out])Out _ j _ o _ o)s[Out]。parallel(out _ I _ o _ o _ j _ o _ o _ fused)s[;Unroll _ explicit & amp# 039;true) s [matmul]。vectorize (matmul _ j _ I) s [out]。vectorize在以下示例中,为了恢复状态,进行了5次实验。
Def resume_search(task,Log _ file):print(& amp;# 039;resume search:& amp;# 039;)cost _ model=auto _ scheduler . xgb model()cost _ Search _ policy=auto _ scheduler . sketch policy
resume search:/usr/local/lib/python 3.7/dist-packages/xgboost/training . py:17:user warning# 039;old style callback is deprecated . see:{ link } & amp;# 039;从用户警告(user warning)的最终说明和摘要张量表达式(TE)语言开始,演示了TVM如何优化计算任务的一系列示例。
下载python源代码:auto_scheduler_matmul_x86.py
下载jupyter notebook:auto _ scheduler _ matmul _ x86 . ipynb
这是本文的全部内容。要查看更多TVM中文文档,请访问 https://tvm.hyper.ai/。
29
2024-02
23
2023-02
23
2023-02
04
2022-05
04
2022-05
27
2022-03