Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
LLVMでHalideみたいな計算グラフ+JITを作りたい
Search
sonson
April 20, 2020
Programming
1.6k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
LLVMでHalideみたいな計算グラフ+JITを作りたい
sonson
April 20, 2020
Transcript
36 slides
HalideͬΆ͍Կ͔Λ࡞ͬͯΈΔςετ Programming Yuichi Yoshida Chief engineer, DENSO IT Laboratory, Inc.
#Θ͍Θ͍swift @sonson_twit © 2014 DENSO IT Laboratory, Inc., All rights reserved. Redistribution or public display not permitted without written permission from DENSO IT Laboratory, Inc. LLVMͰܭࢉάϥϑΛJITίϯύΠϧ
αϯϓϧίʔυ • https://github.com/sonsongithub/llvm_jit_compile • https://github.com/sonsongithub/llvm-tutorial • Ϗϧυʹllvm͕ඞཁ • macOSͷ߹ brew
install llvm • ubuntuͷ߹ apt install llvm
ܭࢉάϥϑ
Computational Graph • ܭࢉΛάϥϑͱͯ͠อ࣋͢Δ • ී௨ͷίʔυͰɼ݁Ռ͔͠ಘΒΕͳ͍ • ͳͥɼͦΕ͕ඞཁʁ • ඍ͍͔ͨ͠Β
• ͳͥɼඍ͍ͨ͠ͷ͔ʁ
χϡʔϥϧωοτϫʔΫ f1 y W1 W2 W3 W4 f2 f3 x
ֶशɿ ͷΛௐ͍ͯ͘͠ ͜ͷͨΊʹ Ͱඍ͢Δඞཁ͕͋Δ ·ͨ ඍ͢Δඞཁ͕͋Δ Back propagationͰݕࡧʂ y Wi f
ඍ • χϡʔϥϧωοτϫʔΫΛ࣮͢Δ߹ • ϑΥϫʔυ • ग़ྗΛܭࢉ͢Δ࣮ • όοΫϫʔυ •
ௐ͢Δͱ͖ʹܭࢉ͢Δ࣮ • ࣮ɼϑΥϫʔυͷܭࢉͷඍ • ैདྷɼ͜ΕΒΛผʑʹ࣮͍ͯͨ͠ • ࣮͕ඇৗʹ໘ɾɾɾɾඍखܭࢉ • όάͷԹচ
ࣗಈඍ • ඍɼ࣮ࣗಈతʹͰ͖Δʢࡶʹݴ͏ͱʣ • ֶͷڭՊॻΈ͍ͯͩ͘͞ • ੵͦͷݶΓͰ͋Γ·ͤΜ
ී௨ʹ࣮͢Δͱ // x=10ͷͱ͖ͷdy/dwͷ͕ཉ͍͠ double w = 1; double x =
10; // ͜͜Ͱଈ࣌ʹܭࢉ͞ΕΔɽ double y = sin(w * x); double temp_x = 10; // खͰతʹܭࢉͨࣜ͠Λ // ιʔείʔυ্Ͱ࣮͢Δඞཁ͕͋Δ double dy_dw = cos(w * temp_x) * temp_x; std::cout << dy_dw << std::endl;
ܭࢉάϥϑ͕ར༻Ͱ͖Ε // ܭࢉάϥϑͷ߹ Param w; Var x,y; // ͜͜Ͱܭࢉ͞Εͳ͍ɽܭࢉάϥϑ͕ߏங͞ΕΔɽ //
“define by run”ͱݺΕΔ y = math.sin(w * x); double temp_x = 10; // ඍɼܭࢉάϥϑʹج͖ͮɼࣗಈతʹܭࢉ͞ΕΔɽ double dy_dw = y.diff(temp_x); std::cout << dy_dw << std::endl;
ฒྻԽ
ฒྻԽͷͨΊͷख๏ • SIMD sin(a) sin(a) sin(a) sin(a)
ฒྻԽͷͨΊͷख๏ • ϚϧνεϨου sin(a) sin(a)
ฒྻԽͷͨΊͷख๏ • ϚϧνεϨου+SIMD sin(a) sin(a) sin(a) sin(a) sin(a) sin(a) sin(a)
sin(a)
LLVMͷԠ༻ྫɾɾɾɾHalide • εϨου • SIMD • GPU • ͳͲͳͲɾɾɾ
Halideͷ߹ HalideͳΒɼ͜ͷίʔυͰશͯͷڥʹϙʔτͰ͖Δ [10] • εϨου • SIMD • GPU •
ͳͲͳͲɾɾɾ
[11]
Halideͷίʔυ C++ͰίϯύΠϧՄೳ
Halideͷ࣮ࡍ $ DPEF )BMJEF'VOD )BMJEF%4- #JOBSZ "45 ී௨ʹ ίϯύΠϧ Ϗϧυʹ
LLVMͱ Halide͕ඞཁ ੩తʹίϯύΠϧՄೳ
Halideͷ࣮ࡍ $ DPEF )BMJEF'VOD )BMJEF%4- #JOBSZ )BMJEF*3 --7.*3 #JOBSZ Halide::Funcͷ
AST͔ΒIRΛੜ Halide IR͔Β LLVM IRΛੜ LLVMͰ όΠφϦΛੜ "45 ࣮ߦڥΛηοτ
$ DPEF )BMJEF'VOD )BMJEF%4- Halideͷ࣮ࡍ #JOBSZ )BMJEF*3 --7.*3 #JOBSZ ࣮ߦ
Halide::Funcͷ AST͔ΒIRΛੜ Halide IR͔Β LLVM IRΛੜ LLVMͰ όΠφϦΛੜ "45 ࣮ߦڥΛηοτ ࠷ۙ͜ͷลͰ QPMZIFESBMPQUJNJ[BUJPOͷͨΊʹ ͏Ұஈ֊*3Λు͍ͯ࠷దԽ͢ΔΒ͍͠
$ DPEF )BMJEF'VOD )BMJEF%4- Halideͷ࣮ࡍ "45 #JOBSZ )BMJEF*3 --7.*3 #JOBSZ
࣮ߦ Halide::Funcͷ AST͔ΒIRΛੜ Halide IR͔Β LLVM IRΛੜ LLVMͰ όΠφϦΛੜ "45 ࣮ߦڥΛηοτ Ϗϧυʹ LLVMͱ Halide͕ඞཁ Polyhederal IR
ܭࢉάϥϑ·ͱΊ • ࣗಈඍ • TensoFlow, PyTorchɾɾɾɾɾ • χϡʔϥϧωοτϫʔΫͷֶशʹඞਢͷٕज़ • ฒྻԽͳͲʹ
• Halide • ͭ·Γ • ܭࢉΛఆ͓͍ٛͯͯ͠ɼϥϯλΠϜͰͳΜͧ͢Δ • ࣗಈతʹͳΜͧ͢Δͷָ͕ʹͳΔ
LLVMͰؔΛ࡞ͬͯΈΑ͏
llvm::JIT • Ͳ͏ΒJITʹ͍͔ͭ͘API/࣮͕͋ΔͬΆ͍ • MCJIT • depracatedΒ͍͠ɾɾɾ • ͰγϯϓϧͰݟ௨͕͍͍͠ •
ORC JIT • KaleidoscopeJIT.hʹ࣮͞Ε͍ͯΔͷ • ·ͩ͜ͷลɾɾɾશવௐࠪͰ͖ͯͳ͍ • http://llvm.1065342.n5.nabble.com/llvm-dev- Questions-about-moving-from-MCJIT-to-Orc-JIT- td129151.html
ࠓͷྲྀΕ • LLVM API for C++ͰίʔυΛॻ͘ • IRBuilder • ίʔυΛ֬ೝɼLLVM
IRΛు͘ɼ • MCJIT API • ExecutionEngine • ίϯύΠϧ • C++ͷؔͷϙΠϯλΛήοτ͢Δ
LLVM IRΛίʔυ͔Β࡞Δ LLVMͷAPIͰLLVM IRΛΈཱͯΔ --7.͕ཧ͢Δ$POUFYU
LLVM IRΛίʔυ͔Β࡞Δ LLVMͷAPIͰLLVM IRΛΈཱͯΔ --7.͕ཧ͢Δ$POUFYU .PEVMF
LLVM IRΛίʔυ͔Β࡞Δ LLVMͷAPIͰLLVM IRΛΈཱͯΔ --7.͕ཧ͢Δ$POUFYU .PEVMF 'VODUJPO 'VODUJPO
LLVM IRΛίʔυ͔Β࡞Δ LLVMͷAPIͰLLVM IRΛΈཱͯΔ --7.͕ཧ͢Δ$POUFYU .PEVMF 'VODUJPO #MPDL FOUSZ 'VODUJPO
#MPDL UIFO SFUVSO
͠ࢉ • add.cpp • ೋ߲ԋࢉAPI͕༻ҙ͞Ε͍ͯΔͷͰͦΕΛ͏ • CreateFAddͳͲ
ؔݺͼग़͠ • call.cpp • ͷcosΛݺͼग़͢ • ࣗͰϩʔΧϧͰ࣮ͨؔ͠Λݺͼग़͢ • Cͷ໊લ •
C++ͷmangled name • खॱ • ؔΛϞδϡʔϧʹՃ • module͔ΒؔΛऔಘ • ݺͼग़͢
ϙΠϯλ • pointer.cpp • વϙΠϯλॲཧͰ͖Δ • StoreͱLoadͰΛॻ͖ࠐΊΔ
ؔΛ࣮ߦ͢Δ // Get pointer to a function which is built
by EngineBuilder. // ؔͷϙΠϯλͰड͚Δ auto f = reinterpret_cast<double(*)(double*)>( engineBuilder->getFunctionAddress(name); if (f == NULL) { cout << "error" << endl; return 1; } // Execution // a + b double a = 10; cout << f(&a) << endl;
ܭࢉάϥϑ • graph.cpp • ೋมݻఆͷγϯϓϧͳྫ • ΫϥεͱԋࢉࢠͰߏจΛ࡞Δ • VarΫϥεͱVarΫϥεΛͯ͠ɼExpressionΛɾɾɾ •
LLVM Tutorial KaleidoscopeϕʔεͰ։ൃ
ܭࢉάϥϑ • graph.cpp • ೋมݻఆͷγϯϓϧͳྫ • ΫϥεͱԋࢉࢠͰߏจΛ࡞Δ • VarΫϥεͱVarΫϥεΛͯ͠ɼExpressionΛɾɾɾ •
LLVM Tutorial KaleidoscopeϕʔεͰ։ൃ • ՝ • C++ͷϙΠϯλͰड͚ΔgetFunctionAddress • ҾͱΓΛࣗ༝ʹઃܭͰ͖ͳ͍
ࢀߟจݙ • ͖ͭͶ͞ΜͰΘ͔ΔLLVM • llvm.org • LLVM Tutorial, C++൛Λಡ͏ •
swift.org • SwiftݴޠαΠτ • https://halide-lang.org • ݁ہɾɾɾίʔυಡΉͷ͕खͬऔΓૣ͠ɾɾɾɾɾ • Halideͷͬ͟ͱͨ͠આ໌ɾɾɾFixStars͕৺ʹ׆ಈ͍ͯ͠Δɼ͢Β͍͠ • https://www.slideshare.net/fixstars/halide-82788728 • https://qiita.com/fukushima1981/items/fa3537234e19baffc761 • Polyhedral Model • εέδϡʔϧΛઢܗʹؼண͠ɼܭըͰղ͘Έ͍ͨͳ • ·ͩɼΑʔ͔ΘΒ͔Μ • Darkroomͱ͍͏ٕज़ͷதͬΆ͍ • https://www.slideshare.net/fixstars/prelude-to-halidepublic-108059122
More Decks by sonson
See All by sonson
計算グラフのJITコンパイラをLLVM on C++で作ろう
sonsongithub
2
650
LLVM Tutorial 02 - わいわいswiftc
sonsongithub
1
470
LLVM Tutorial - わいわいswiftc
sonsongithub
0
360
How to make and publish a Swift playground book for iPad
sonsongithub
5
19k
Swiftで実装するHTML特殊文字の高速処理
sonsongithub
3
8.1k
First step of 3D touch
sonsongithub
0
730
Getting started with 3D Touch
sonsongithub
0
840
SSLって必要ですか〜Let's Encryptを試してみよう
sonsongithub
3
610
Other Decks in Programming
See All in Programming
imagewebp()の向こう側: WebPはなぜ JPEG・PNGより軽いのか
yuzneri
0
160
Compiling Your Language to WasmGC & Friends
tanishiking
0
830
느린 네트워크와 저사양 디바이스 사용자를 위한 웹뷰 로딩 시간 개선기
kmkim
1
270
マルチプロダクトSaaSの アプリ一覧を速くする——複数プロダクトにまたがる「権限行脚」を減らすまで #techramen26conf
mktakuya
1
260
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
450
Proxmox x Talos Linux x Terraform で実現するなんかいい感じの Kubernetes クラスタ / A Sleek Kubernetes Cluster Powered by Proxmox, Talos Linux, and Terraform
nnaka2992
0
320
baseline으로 마이그레이션, 리팩토링 안전하게 맡기기
fornewid
1
1.1k
How SRE Grew Out of Server-Side Work and Became the Team's Own / サーバサイドから始まったSREを チームに渡すまで
maaaato
1
380
社内AIエージェント「Askワンバン」における コストチューニングの取り組み
t0yohei
0
180
AIが実装を担う時代の「技術設計」と「チームマネジメント」
kamina_zzz
2
470
SODA - FACT BOOK(EN)
sodainc
1
300
Agentic AI with the Power of Jakarta EE
ivargrimstad
0
240
Featured
See All Featured
Practical Orchestrator
shlominoach
192
12k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
1
320
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
12k
Build your cross-platform service in a week with App Engine
jlugia
234
19k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
33
5.9k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.9k
SEO for Brand Visibility & Recognition
aleyda
0
4.9k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
610
Docker and Python
trallard
47
4.3k
Skip the Path - Find Your Career Trail
mkilby
1
310
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.2k