KEMBAR78
Polyphony: Python ではじめる FPGA | PPTX
Polyphony
Python ではじめる 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 then
y := x + 1
x <= 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 then
y := x + 1
x <= 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 Polyphony
Verilog HDL
(synthesizable)
What is Polyphony?(2/3)
Python Polyphony
Verilog 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)
Function
Class
List(Fixed
size)
Tuple
For/While
If/Else
...
String
Dictionary
Set
Builtin funcs
...
What is Polyphony?(3/3)
まずは Lチカ、、、
• こんな感じでできます
from polyphony import testbench, module, is_worker_running
from polyphony.io import Bit
@module
class Blink:
def __init__(self):
self.led = Bit(0)
self.append_worker(self.main, led)
def main(self):
led = 1
while is_worker_running():
self.led.wr(led)
led = ~led
self._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 0
if n == 1: return 1
r0 = 0
r1 = 1
for i in range(n-1):
prev_r1 = r1
r1 = r0 + r1
r0 = prev_r1
return r1
#@testbench
def 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] == result
print(i, "=>", result)
test()
ここまでのまとめ
• Polyphony で FPGA が簡単に使えそう
• Polyphony で何ができる?
• どこまでできる?
使ってみよう Polyphony
• 豊富なライブラリ
これから豊富になる(だろう)
Polyphony ライブラリ
• bitonic_sort : バイトニックソートのサンプル
• chenidct : 2次元IDCT(逆コサイン変換)
• adpcm : ADPCMのエンコーダ・デコーダ
• UART: printf デバッグ的なことが出来る
• SPI + A/D: センサーとの連携
ちょっと脱線
@polyphony.pure
def bitonic_indices(size, blocks, offset):
for i in range(0, size):
if (i % (offset << 1)) >= offset:
continue
direction = ASCENDING if (i // blocks) % 2 == 0 else DESCENDING
ii = i + offset
yield 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.")
@testbench
def test():
hello()
test()
Hello World 2
• Python3 で実行
– 必ず Python3 で実行するようにします
> python3 hello.py
Hello World.
Hello World 3
• Polyphony でコンパイル
– polyphony でコンパイルします。
> polyphony hello.py
> ls *.v
hello.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 hello
hello*
> ./hello
0:
Hello World.
Hello World.
Hello World.
Hello World.
150:finish
Hello 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
@testbench
def 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.py
Traceback (most recent call last):
File "mul_add.py", line 11, in <module>
test()
File "/lib/... .../polyphony/__init__.py", line 30, in _testbench_decorator
func()
File "mul_add.py", line 8, in test
assert 17 == mul_add(1, 2, 3, 4)
AssertionError
AssertionError です。17 ではなく 14 に変えて再度チャレンジ
Mul and Add 3
• シミュレータで実行
> ../bin/simu.py mul_add.py
0: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= x
10: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= 0
110: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= 0
130: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= 14
160: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= 14
180: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= 62
220:finish
実機では?
• 合成して
• IO 配線して
• 実装して
• ソフトも書いて
– 起動するのにソフトとか必要かも
Python でフィルタ処理
SPI SPI
I/F
SPI
I/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チカ・モジュール
おまけ:ディープラーニング
ARM
Cortex-M4
モデルをぎゅっと
圧縮
3 FPGA
Polyphony の今後
• HPC
– めざせ京 対応!!
• ステレオビジョン
– 視差画像
• Bayes
– メールの選別
• 数値計算
– 精度保証付き?
まとめ
• FPGA つかってみよう!!
• Polyphony を使えば
Python のコードを HDL に!!
• Happy Python Life!!
これからも Polyphony をよろしく!!
ご清聴ありがとうございました

Polyphony: Python ではじめる FPGA