TVM概述
TVM
TVM是陳天奇領導的一個DL加速框架項目。它處于DL框架(如tensorflow、pytorch)和硬件后端(如CUDA、OpenCL)之間,兼顧了前者的易用性和后者的執行效率。
官網:
https://tvm.apache.org/
代碼:
https://github.com/apache/tvm
論文:
《TVM: End-to-End Optimization Stack for Deep Learning》
和同類項目的差異:
-
TFLite和ONNXRuntime只能接收特定格式的模型。而TVM這些都能接收。
-
NCNN、MACE之類的項目,一般只考慮了ARM CPU的優化,對于異構計算做的比較少。
-
TVM強化了圖優化的部分,使之更類似于編譯器的架構。
架構
官網:
https://tvm.apache.org/docs/arch/index.html
安裝
TVM暫時不支持pip安裝,pip install tvm安裝的是另一個同名軟件。。。
TVM Runtime
TVM采用C/S模式進行部署,其中在target機器上的部分,被稱為TVM Runtime。
TVM Runtime的代碼通常比較薄,只需要把host發過來的優化結果執行即可。
官方文檔:
http://tvm.apache.org/docs/dev/runtime.html
TVM Runtime也是BYOC的重要組成部分。
BYOC:Bring Your Own Codegen
https://tvm.apache.org/2020/07/15/how-to-bring-your-own-codegen-to-tvm
How to Bring Your Own Codegen to TVM
這是中文翻譯版:
https://zhuanlan.zhihu.com/p/337033822
如何在TVM上集成Codegen(上)
https://zhuanlan.zhihu.com/p/337037547
如何在TVM上集成Codegen(下)
示例:
https://github.com/antkillerfarm/antkillerfarm_crazy/blob/master/python/ml/tvm/pytorch2tvm.py
該示例包含以下內容:
1.如何導入pytorch和tflite的模型。
2.local執行和remote執行。
3.使用print(mod.astext(show_meta_data=False))可以打印相關IR的內容。meta data有的時候包含了權重,打印出來意義不大,反而導致其他有意義的部分,淹沒在大量的log中,沒法看了。
參考:
https://zhuanlan.zhihu.com/p/369981405
部署TVM Runtime
https://zhuanlan.zhihu.com/p/352988283
TVM學習記錄——pytorch
NNVM
NNVM是一個類似于ONNX、NNEF的中間表示。
官網:
https://github.com/dmlc/nnvm
參考:
https://mp.weixin.qq.com/s/qkvX0rmEe0yQ-BhCmWAXSQ
李沐:AWS開源端到端AI框架編譯器NNVM
Relay
Relay是TVM中用來替代NNVM的模塊,其本身被認為是NNVM第二代。
官網:
https://tvm.apache.org/docs/arch/relay_intro.html
NNVM本質上只能描述傳統的計算圖,這屬于Data Flow的范疇。但是現在的DL框架越來越靈活,不僅能對數據進行計算,還能對數據進行一定的控制處理,也就是所謂的Control Flow(if-else/ADT matching/遞歸調用)。
論文:
《The Deep Learning Compiler: A Comprehensive Survey》
參考:
https://zhuanlan.zhihu.com/p/91283238
TVM圖編譯器Relay簡單探究
https://zhuanlan.zhihu.com/p/390087648
Relay IR與Relay Pass
https://mp.weixin.qq.com/s/Kt4xDLo-NRui8Whl0DqcSA
Data Flow和Control Flow
Pass
https://tvm.apache.org/docs/how_to/extend_tvm/use_pass_infra.html
和LLVM類似,TVM的Pass也可分為兩類:
ModulePass:將整個程序視作一個單元處理的pass。
FunctionPass:以單個函數為作用域的pass, 每個函數間是相互獨立的。
FunctionPass包括了Relay層的tvm.relay.transform.FunctionPass和TIR層的tvm.tir.transform.PrimFuncPass。
部分pass:
-
FoldConstant: src/relay/transforms/fold_constant.cc
-
AlterOpLayout: src/relay/transforms/alter_op_layout.cc
-
Legalize: src/relay/transforms/legalize.cc
-
MergeComposite: src/relay/transforms/merge_composite.cc
參考:
https://zhuanlan.zhihu.com/p/378739411
萬字長文入門TVM Pass
https://www.cnblogs.com/wujianming-110117/p/14580172.html
TVM Pass IR如何使用
https://zhuanlan.zhihu.com/p/112813859
Relay Pass in TVM
https://zhuanlan.zhihu.com/p/358437531
pass總結
Schedule Primitives
https://tvm.apache.org/docs/how_to/work_with_schedules/schedule_primitives.html
Layout
TVM默認的input layout: NCHW,kernel layout: OIHW。
Backend
python層面:
python/tvm/relay/op/contrib/ethosn.py
@register_pattern_table("ethos-n") def pattern_table(): @tvm.ir.register_op_attr("nn.max_pool2d", "target.ethos-n") def max_pool2d(attrs, args):relay層面:
src/relay/backend/contrib/ethosn
TVM_REGISTER_GLOBAL("relay.ext.ethos-n").set_body_typed(CompileEthosn);runtime層面:
src/runtime/contrib/ethosn
test:
tests/python/contrib/test_ethosn
代碼分析
TVM建立了一套類型系統:
class BaseExprNode : public Object; class BaseExpr : public ObjectRef;根據基類,查看實際類型:
XX->checked_type()
Quantize
microTVM
microTVM可用于那些沒有OS的單片機。
官網:
https://tvm.apache.org/docs/arch/microtvm_design.html
從上面的圖來看,microTVM只要在單片機的main函數中啟動即可,同時參數也可以放到FLASH上。
PS:這種能直接尋址的FLASH,多半是NOR FLASH。
參考:
https://zhuanlan.zhihu.com/p/337085225
TinyML-TVM是如何馴服Tiny的(上)
https://zhuanlan.zhihu.com/p/337087273
TinyML-TVM是如何馴服Tiny的(下)
參考
https://zhuanlan.zhihu.com/p/139552817
一篇關于深度學習編譯器架構的綜述論文
https://www.zhihu.com/question/396105855
針對神經網絡的編譯器和傳統編譯器的區別和聯系是什么?
https://mp.weixin.qq.com/s/8bXwxYyNjdThlGQQ70cgWQ
TVM:端到端自動深度學習編譯器,244頁ppt
https://zhuanlan.zhihu.com/p/333706468
TVM學習系列blog
https://zhuanlan.zhihu.com/p/163717035
AI編譯優化
https://www.zhihu.com/question/267167829
如何看待Tensor Comprehensions?與TVM有何異同?(這個問題下的答案不多,但基本都是陳天奇、賈揚清之類的大佬)
https://mp.weixin.qq.com/s/irvBbPKENiZX9G_6wh5c-Q
陳天奇等人提出TVM:深度學習自動優化代碼生成器
https://mp.weixin.qq.com/s/28n8g_epHsYB0I9GVc_lww
陳天奇團隊TVM重磅更新:直接在瀏覽器使用GPU
https://mp.weixin.qq.com/s/7JGLm-hkCZBNDLA98qvWNA
自動生成硬件優化內核:陳天奇等人發布深度學習編譯器TVM
https://mp.weixin.qq.com/s/YVIvdMznb3oatIXqD5a5_A
陳天奇等人提出AutoTVM:讓AI來編譯優化AI系統底層算子
https://mp.weixin.qq.com/s/HquT_mKm7x_rbDGz4Voqpw
阿里巴巴最新實踐:TVM+TensorFlow提高神經機器翻譯性能
https://zhuanlan.zhihu.com/p/50529704
手把手帶你遨游TVM
https://mp.weixin.qq.com/s/z5rsU_uAAaRxgD9YAxDkZA
陳天奇:深度學習編譯技術的現狀和未來
https://zhuanlan.zhihu.com/p/75203171
如何利用TVM快速實現超越Numpy(MKL)的GEMM
https://zhuanlan.zhihu.com/p/58918363
TVM: Deep Learning模型的優化編譯器
https://zhuanlan.zhihu.com/p/87664838
也談TVM和深度學習編譯器
https://mp.weixin.qq.com/s/VE3CySjjS2rTpUDPnKcLTg
陳天奇最新研究:遞歸模型編譯器CORTEX
https://zhuanlan.zhihu.com/p/358585143
深度學習編譯器及TVM介紹
https://zhuanlan.zhihu.com/p/360385060
TVM中的scheduler
https://zhuanlan.zhihu.com/p/388452164
tvm or mlir?
總結