Upload
ryos36
View
701
Download
3
Embed Size (px)
Citation preview
PolyphonyPython ではじめる FPGA
鈴木
2017/9/9
アジェンダ
• FPGA ってなに?
• Polyphony 入門
• 使ってみよう Polyphony
– Polyphony応用例(RISC-V)
–取り組み1(CNN)
–取り組み2(CV)
• 時間があればチュートリアル
• まとめ
自己紹介
• @ryos36
• ハッシュタグ#polyphony
著者です
FPGA てなに?
FPGAを使ってみよう!!
FPGAでハードウェアに自由度を!
http://www.ni.comより
Wikipedia より
FPGA=Field-Programmable Gate Array
使用例(OLED)
OLED もライブラリがあらかじめある
Jupyterからコントロール
使用例(キーパッド)
FPGAとWiFi接続例
ブレッドボードでプロトタイプの開発
FPGA の特徴
得意なこと
• 並列計算
• レイテンシを守ること
• ビット計算
• 柔軟性
不得意なこと
• 高速処理?
FPGA vs Raspberry PI
• FPGA • Raspberry PI
ARM SoCARM SoC FPGA
u-boot & Linuxu-boot & Linux
Linux Driver Linux Driver
Python Python
やりたいこと やりたいこと
値段とか消費電力とか入手性とかを無視して、ソフト的にざっくりと比較コミュニティの大きさ(情報の入りやすさ)もだいぶ違うけど、、、
HDL で何か実装してみよう!?
• 使う言語
– VHDL
– Verilog HDL
LED チカチカ=Lチカハードウェアの
Hello World 的存在
FPGAでハードウェアを自由に組む
module fulladder(input x,input y,input cin,
output A, output cout);
assign {cout,A} = cin + y + x;
endmodule
ハードウェア記述言語
VHDL を使った問題
signal x: std_logic…process (clk)
variable y : std_logic;begin
if clk’event and clk = 1 theny := x + 1x <= x + 1
end if;end process;
Q: x はいま 3 とします。左辺にある y と x はいくつになるでしょう?
VHDL を使った問題
signal x: std_logic…process (clk)
variable y : std_logic;begin
if clk’event and clk = 1 theny := x + 1x <= x + 1
end if;end process;
A: y は 4、x は 3のまま。次のクロックで 4。
何を意味しているのか?
FPGA でパイプライン処理
処理 処理 処理 処理 処理
処理を細分化することで高速化が可能
x + 1
x
x + 1
x頭の中でオーバラップする時間を考えながら設計する!!
何を意味しているのか?
FPGA の設計 = 頭の中にこんなのが思い浮かぶ必要あり
今でも検証には波形を見る
ここまでのまとめ
• FPGA なんだか楽しそう
• FPGA ちょっと難しそう
Polyphony入門
Python で FPGA
FPGA つかってみたいけど
Polyphonyを使おう!!
• Polyphony:Python でハードウェア設計!
What is Polyphony?(1/3)
Python for Hardware Design
Python PolyphonyVerilog HDL
(synthesizable)
What is Polyphony?(2/3)
Python PolyphonyVerilog HDL
(synthesizable)
● Bring higher level of abstraction to your design● Allow designers to focus on developing the algorithm● Reduce costs for program maintenance
● Open Source (https://github.com/ktok07b6/polyphony)
Polyphony はPythonコードのサブセットを合成可能
Python
Polyphony(is a subset of Python)
FunctionClassList(Fixed size)TupleFor/WhileIf/Else...
StringDictionarySetBuiltin funcs...
What is Polyphony?(3/3)
まずは Lチカ、、、
• こんな感じでできます
from polyphony import testbench, module, is_worker_runningfrom polyphony.io import Bit
@moduleclass Blink:
def __init__(self):self.led = Bit(0)self.append_worker(self.main, led)
def main(self):led = 1while is_worker_running():
self.led.wr(led)led = ~ledself._wait(10000000)
def _wait(self, interval):for i in range(interval):
pass
blink = Blink()
Fibonacci Number(フィボナッチ数列)#from polyphony import testbench
def fib(n):if n <= 0: return 0if n == 1: return 1r0 = 0r1 = 1for i in range(n-1):
prev_r1 = r1r1 = r0 + r1r0 = prev_r1
return r1
#@testbenchdef test():
expect = [0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610]for i in range(len(expect)):
result = fib(i)assert expect[i] == resultprint(i, "=>", result)
test()
ここまでのまとめ
• Polyphony で FPGA が簡単に使えそう
• Polyphony で何ができる?
• どこまでできる?
使ってみよう Polyphony
• 豊富なライブラリ
これから豊富になる(だろう)
Polyphonyライブラリ
• bitonic_sort : バイトニックソートのサンプル
• chenidct : 2次元IDCT(逆コサイン変換)
• adpcm : ADPCMのエンコーダ・デコーダ
• UART: printfデバッグ的なことが出来る
• SPI + A/D: センサーとの連携
ちょっと脱線
@polyphony.puredef bitonic_indices(size, blocks, offset):
for i in range(0, size):if (i % (offset << 1)) >= offset:
continuedirection = ASCENDING if (i // blocks) % 2 == 0 else DESCENDINGii = i + offsetyield i, ii, direction
• bitonic_sort : バイトニックソートのサンプル
特別なデコレータ
Python により“動的に”回路図を自動生成
Polyphony応用例
• RISC-V
• CNN
• CV(コンピュータ・ビジョン)
RISC-V
RISC-V:大手企業も注目するオープンな CPU の規格。すでに FPGA 上でも動いて、Linux も動作する模様。
RISC-V on Polyphony
パイプラインのシミュレート
CNN
Python で学ぶディープラーニングの本
CV(コンピュータ・ビジョン)
CV と呼ぶには程遠い。OpenCV との連携は“できません”。
あしからず。
エッジ検出
チュートリアル
• Hello World
• Mul and Add
• 実機!!
Hello World 1
• エディターで Hello World を写しましょう(写経)。
from polyphony import testbench
def hello():print("Hello World.")
@testbenchdef test():
hello()
test()
Hello World 2
• Python3 で実行
–必ず Python3 で実行するようにします
> python3 hello.pyHello World.
Hello World 3
• Polyphony でコンパイル
– polyphony でコンパイルします。
> polyphony hello.py> ls *.vhello.v polyphony_out.v test.v
verilogのファイル(.v のファイル) が生成されます。
Hello World 4
• iverilogでコンパイル&実行
> iverilog -o hello polyphony_out.v test.v[test-0.3.0] Persimmon:Tutorial_0> ls hellohello*> ./hello
0:Hello World.Hello World.Hello World.Hello World.
150:finishHello World
Hello World が5回実行されるのが気になる方は
Mul and Add1
• エディターで Hello World を写しましょう(写経)。
from polyphony import testbench
def mul_add(a, b, c, d):return a * b + c * d
@testbenchdef test():
assert 17 == mul_add(1, 2, 3, 4)assert 62 == mul_add(4, 5, 6, 7)
test()
Mul and Add 2
• Python3 で実行
–必ず Python3 で実行するようにします
> python3 mul_add.pyTraceback (most recent call last):
File "mul_add.py", line 11, in <module>test()
File "/lib/... .../polyphony/__init__.py", line 30, in _testbench_decoratorfunc()
File "mul_add.py", line 8, in testassert 17 == mul_add(1, 2, 3, 4)
AssertionError
AssertionErrorです。17 ではなく 14 に変えて再度チャレンジ
Mul and Add 3
• シミュレータで実行
> ../bin/simu.py mul_add.py0:mul_add_0_in_a= x, mul_add_0_in_b= x, mul_add_0_in_c= x,
mul_add_0_in_d= x, mul_add_0_out_0= x10:mul_add_0_in_a= 0, mul_add_0_in_b= 0, mul_add_0_in_c= 0,
mul_add_0_in_d= 0, mul_add_0_out_0= 0110:mul_add_0_in_a= 1, mul_add_0_in_b= 2, mul_add_0_in_c= 3,
mul_add_0_in_d= 4, mul_add_0_out_0= 0130:mul_add_0_in_a= 1, mul_add_0_in_b= 2, mul_add_0_in_c= 3,
mul_add_0_in_d= 4, mul_add_0_out_0= 14160:mul_add_0_in_a= 4, mul_add_0_in_b= 5, mul_add_0_in_c= 6,
mul_add_0_in_d= 7, mul_add_0_out_0= 14180:mul_add_0_in_a= 4, mul_add_0_in_b= 5, mul_add_0_in_c= 6,
mul_add_0_in_d= 7, mul_add_0_out_0= 62220:finish
実機では?
• 合成して
• IO 配線して
• 実装して
• ソフトも書いて
–起動するのにソフトとか必要かも
Python でフィルタ処理
SPI SPII/F
SPII/F
フレームワーク
#from polyphony import testbench
def filter(spi_in, spi_out):........
SPI
Python で書いたフィルターがFPGA 上で動く!!
高位合成ツール:
開発キット
フレームワーク
Murata(LoRa対応モジュールSX1276 + Cortex-M0)
Kiss4(Zynq = ARM + FPGA)
OR
Xilinx のツールへの対応もしました
ARM プロセッサ Polyphony のLチカ・モジュール
おまけ:ディープラーニング
ARMCortex-M4
モデルをぎゅっと圧縮
3 FPGA
Polyphony の今後
• HPC
–めざせ京対応!!
• ステレオビジョン
–視差画像
• Bayes
– メールの選別
• 数値計算
–精度保証付き?
まとめ
• FPGA つかってみよう!!
• Polyphony を使えばPython のコードを HDL に!!
• Happy Python Life!!
これからも Polyphony をよろしく!!
ご清聴ありがとうございました