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
More Decks by sonson
See All by sonson
計算グラフのJITコンパイラをLLVM on C++で作ろう
sonsongithub
2
640
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
830
SSLって必要ですか〜Let's Encryptを試してみよう
sonsongithub
3
610
Other Decks in Programming
See All in Programming
AI-Powered Domain-Driven Design
ziobrando
0
370
Next.js: The final boss of Web Performance
guaca
0
380
AI Augmentation Techniques with Liberty
ivargrimstad
0
240
コーディング自動化の次へ。人の介入をゼロにするファクトリーとサポートAIによる実践
timakin
5
5k
TS 7.0 で baseUrl が使えなくなった今、パスエイリアスをどうするか
bicstone
2
350
高橋メソッドでCVE解説シリーズ 〜CVE-2026-112816〜
hakkadaikon
0
170
AmebaブログはKMPから卒業します
kmars0605
0
310
Spec-Driven Development - Why Vaadin Is the Perfect Fit
simas
PRO
0
200
Vapor Mode でアクセシビリティは 壊れないか検証した話
fuqda
1
300
baseline으로 마이그레이션, 리팩토링 안전하게 맡기기
fornewid
1
800
社内AIエージェント「Askワンバン」における コストチューニングの取り組み
t0yohei
0
120
Web3Dライブラリを作ってVue Custom Rendererの仕組みを理解しよう / lets learn vue custom renderer by making 3d library
drumath2237
1
110
Featured
See All Featured
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.2k
Ethics towards AI in product and experience design
skipperchong
2
450
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
730
How Software Deployment tools have changed in the past 20 years
geshan
2
35k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.7k
Agile that works and the tools we love
rasmusluckow
331
22k
Building Adaptive Systems
keathley
44
3.3k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
4
4.4k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
960
Transcript
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