Transcript
Page 1: Polyphony: Python ではじめる FPGA

PolyphonyPython ではじめる FPGA

鈴木

2017/9/9

Page 2: Polyphony: Python ではじめる FPGA

アジェンダ

• FPGA ってなに?

• Polyphony 入門

• 使ってみよう Polyphony

– Polyphony応用例(RISC-V)

–取り組み1(CNN)

–取り組み2(CV)

• 時間があればチュートリアル

• まとめ

Page 3: Polyphony: Python ではじめる FPGA

自己紹介

• @ryos36

• ハッシュタグ#polyphony

著者です

Page 4: Polyphony: Python ではじめる FPGA

FPGA てなに?

FPGAを使ってみよう!!

Page 5: Polyphony: Python ではじめる FPGA

FPGAでハードウェアに自由度を!

http://www.ni.comより

Wikipedia より

FPGA=Field-Programmable Gate Array

Page 6: Polyphony: Python ではじめる FPGA

使用例(OLED)

OLED もライブラリがあらかじめある

Jupyterからコントロール

Page 7: Polyphony: Python ではじめる FPGA

使用例(キーパッド)

Page 8: Polyphony: Python ではじめる FPGA

FPGAとWiFi接続例

ブレッドボードでプロトタイプの開発

Page 9: Polyphony: Python ではじめる FPGA

FPGA の特徴

得意なこと

• 並列計算

• レイテンシを守ること

• ビット計算

• 柔軟性

不得意なこと

• 高速処理?

Page 10: Polyphony: Python ではじめる FPGA

FPGA vs Raspberry PI

• FPGA • Raspberry PI

ARM SoCARM SoC FPGA

u-boot & Linuxu-boot & Linux

Linux Driver Linux Driver

Python Python

やりたいこと やりたいこと

値段とか消費電力とか入手性とかを無視して、ソフト的にざっくりと比較コミュニティの大きさ(情報の入りやすさ)もだいぶ違うけど、、、

Page 11: Polyphony: Python ではじめる FPGA

HDL で何か実装してみよう!?

• 使う言語

– VHDL

– Verilog HDL

LED チカチカ=Lチカハードウェアの

Hello World 的存在

Page 12: Polyphony: Python ではじめる FPGA

FPGAでハードウェアを自由に組む

module fulladder(input x,input y,input cin,

output A, output cout);

assign {cout,A} = cin + y + x;

endmodule

ハードウェア記述言語

Page 13: Polyphony: Python ではじめる FPGA

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 はいくつになるでしょう?

Page 14: Polyphony: Python ではじめる FPGA

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。

何を意味しているのか?

Page 15: Polyphony: Python ではじめる FPGA

FPGA でパイプライン処理

処理 処理 処理 処理 処理

処理を細分化することで高速化が可能

x + 1

x

x + 1

x頭の中でオーバラップする時間を考えながら設計する!!

何を意味しているのか?

Page 16: Polyphony: Python ではじめる FPGA

FPGA の設計 = 頭の中にこんなのが思い浮かぶ必要あり

Page 17: Polyphony: Python ではじめる FPGA

今でも検証には波形を見る

Page 18: Polyphony: Python ではじめる FPGA

ここまでのまとめ

• FPGA なんだか楽しそう

• FPGA ちょっと難しそう

Page 19: Polyphony: Python ではじめる FPGA

Polyphony入門

Python で FPGA

Page 20: Polyphony: Python ではじめる FPGA

FPGA つかってみたいけど

Page 21: Polyphony: Python ではじめる FPGA

Polyphonyを使おう!!

• Polyphony:Python でハードウェア設計!

Page 22: Polyphony: Python ではじめる FPGA

What is Polyphony?(1/3)

Python for Hardware Design

Python PolyphonyVerilog HDL

(synthesizable)

Page 23: Polyphony: Python ではじめる FPGA

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)

Page 24: Polyphony: Python ではじめる FPGA

Polyphony はPythonコードのサブセットを合成可能

Python

Polyphony(is a subset of Python)

FunctionClassList(Fixed size)TupleFor/WhileIf/Else...

StringDictionarySetBuiltin funcs...

What is Polyphony?(3/3)

Page 25: Polyphony: Python ではじめる FPGA

まずは 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()

Page 26: Polyphony: Python ではじめる FPGA

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()

Page 27: Polyphony: Python ではじめる FPGA

ここまでのまとめ

• Polyphony で FPGA が簡単に使えそう

• Polyphony で何ができる?

• どこまでできる?

Page 28: Polyphony: Python ではじめる FPGA

使ってみよう Polyphony

• 豊富なライブラリ

これから豊富になる(だろう)

Page 29: Polyphony: Python ではじめる FPGA

Polyphonyライブラリ

• bitonic_sort : バイトニックソートのサンプル

• chenidct : 2次元IDCT(逆コサイン変換)

• adpcm : ADPCMのエンコーダ・デコーダ

• UART: printfデバッグ的なことが出来る

• SPI + A/D: センサーとの連携

Page 30: Polyphony: Python ではじめる FPGA

ちょっと脱線

@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 により“動的に”回路図を自動生成

Page 31: Polyphony: Python ではじめる FPGA

Polyphony応用例

• RISC-V

• CNN

• CV(コンピュータ・ビジョン)

Page 32: Polyphony: Python ではじめる FPGA

RISC-V

RISC-V:大手企業も注目するオープンな CPU の規格。すでに FPGA 上でも動いて、Linux も動作する模様。

Page 33: Polyphony: Python ではじめる FPGA

RISC-V on Polyphony

パイプラインのシミュレート

Page 34: Polyphony: Python ではじめる FPGA

CNN

Python で学ぶディープラーニングの本

Page 35: Polyphony: Python ではじめる FPGA

CV(コンピュータ・ビジョン)

CV と呼ぶには程遠い。OpenCV との連携は“できません”。

あしからず。

エッジ検出

Page 36: Polyphony: Python ではじめる FPGA

チュートリアル

• Hello World

• Mul and Add

• 実機!!

Page 37: Polyphony: Python ではじめる FPGA

Hello World 1

• エディターで Hello World を写しましょう(写経)。

from polyphony import testbench

def hello():print("Hello World.")

@testbenchdef test():

hello()

test()

Page 38: Polyphony: Python ではじめる FPGA

Hello World 2

• Python3 で実行

–必ず Python3 で実行するようにします

> python3 hello.pyHello World.

Page 39: Polyphony: Python ではじめる FPGA

Hello World 3

• Polyphony でコンパイル

– polyphony でコンパイルします。

> polyphony hello.py> ls *.vhello.v polyphony_out.v test.v

verilogのファイル(.v のファイル) が生成されます。

Page 40: Polyphony: Python ではじめる FPGA

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回実行されるのが気になる方は

Page 41: Polyphony: Python ではじめる FPGA

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()

Page 42: Polyphony: Python ではじめる FPGA

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 に変えて再度チャレンジ

Page 43: Polyphony: Python ではじめる FPGA

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

Page 44: Polyphony: Python ではじめる FPGA

実機では?

• 合成して

• IO 配線して

• 実装して

• ソフトも書いて

–起動するのにソフトとか必要かも

Page 45: Polyphony: Python ではじめる FPGA

Python でフィルタ処理

SPI SPII/F

SPII/F

フレームワーク

#from polyphony import testbench

def filter(spi_in, spi_out):........

SPI

Python で書いたフィルターがFPGA 上で動く!!

高位合成ツール:

Page 46: Polyphony: Python ではじめる FPGA

開発キット

フレームワーク

Murata(LoRa対応モジュールSX1276 + Cortex-M0)

Kiss4(Zynq = ARM + FPGA)

OR

Page 47: Polyphony: Python ではじめる FPGA

Xilinx のツールへの対応もしました

ARM プロセッサ Polyphony のLチカ・モジュール

Page 48: Polyphony: Python ではじめる FPGA

おまけ:ディープラーニング

ARMCortex-M4

モデルをぎゅっと圧縮

3 FPGA

Page 49: Polyphony: Python ではじめる FPGA

Polyphony の今後

• HPC

–めざせ京対応!!

• ステレオビジョン

–視差画像

• Bayes

– メールの選別

• 数値計算

–精度保証付き?

Page 50: Polyphony: Python ではじめる FPGA

まとめ

• FPGA つかってみよう!!

• Polyphony を使えばPython のコードを HDL に!!

• Happy Python Life!!

Page 51: Polyphony: Python ではじめる FPGA

これからも Polyphony をよろしく!!

ご清聴ありがとうございました