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
820
SSLって必要ですか〜Let's Encryptを試してみよう
sonsongithub
3
610
Other Decks in Programming
See All in Programming
月間100万人のサービス刷新で、 技術選定をHonoに振り切った話
yutobara
8
6.8k
Web Framework Benchmark Battle 2026, Any%
a_matsuda
2
2.7k
コードレビューのボトルネックを"する側"と"される側"の両面から解消する
yub0n
2
1.6k
Lambda durable functionsでHuman in the Loopを楽に…楽に……
har1101
0
150
R8 の設定見直しでアプリサイズ削減
andpad
0
220
Long Living Tokens, Die, Die, Die! (or: The One Ring is Banned in Modern)
sylph01
0
340
見えないものを探る要求要件定義に必要な基本的思考 / invisible-requirement-thinking
minodriven
15
8.6k
AI時代のコードレビューは人に向けるな、仕組みに向けろ
texmeijin
5
3.7k
ハーネス設計入門 〜 基礎知識の整理から実務へのステップアップ 〜
kinopeee
28
34k
sbt 2.0: takeoff
eed3si9n
0
160
Railsバージョンを4倍にした話
optim
0
500
作れる、でも消せるか?──安全に「枯らして消す」機能削除術 / Drain, Then Delete
chobishiba
2
860
Featured
See All Featured
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
500
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
74
43k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
530
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
730
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
2
950
The untapped power of vector embeddings
frankvandijk
2
1.9k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.4k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
500
Bash Introduction
62gerente
615
220k
AI: The stuff that nobody shows you
jnunemaker
PRO
10
1.2k
Fantastic passwords and where to find them - at NoRuKo
philnash
53
4k
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