raspberry pi deep learning のvideocore gpu 推論の高速化 …raspberry piのvideocore...

33
Raspberry Pi VideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

Upload: others

Post on 20-Feb-2020

19 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

Raspberry PiのVideoCore GPUを用いたDeep Learning推論の高速化

Idein株式会社代表取締役 中村晃一

Page 2: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

自己紹介

• 中村晃一• Idein Inc. 創業者・代表取締役• https://nineties.github.io/

• 東京大学情報理工学系理学部情報科学科卒• 最適化コンパイラ技術を研究• 2015年3月に後期博士課程を退学しIdeinを創業

2

Page 3: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

©2019, Idein Inc. All rights reserved.

社名 Idein Inc. (読み: イデイン)

設立日 2015年4月7日

所在地 東京都千代田区

ウェブサイト https://idein.jp

資本金(資本剰余金含む)

約12.5億円

役員

CEO/創業者 中村晃一CTO/創業者 山田康之CFO 小林大悟社外取締役 上前田直樹社外監査役 村岡竜介

従業員数33名(アルバイトを除く)46名(アルバイトを含む)(2019年8月時点)

主な外部株主

グローバル・ブレインアイシン精機博報堂DYベンチャーズSony Innovation Fund by IGVソニーフィナンシャルベンチャーズDG Lab

会社概要

•ビジョン

実世界のあらゆる情報をソフトウェアで扱えるようにする

•技術

組み込みデバイス向けのDeep Learningの高速化技術

•事業

1. エッジコンピューティングプラットフォーム, Actcast

2. 自動車開発での協業

3. (コンパイラのライセンス販売)

3

Page 4: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

©2019, Idein Inc. All rights reserved.

エッジコンピューティングアーキテクチャに基づくシステム開発と運用の為の開発者向けクラウドサービス1. 安価なデバイスへの深層学習モデルの搭載2. 多数のデバイスとソフトウェアの遠隔運用

{;}

4

{;}

https://actcast.io

Page 5: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

youtube.com/c/IdeinInc

Raspberry PiでのDeep Learning推論高速化

Page 6: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

弊社の取組の特長

●変換後モデルは元のモデルと数学的に等価● 32-bit floatのまま

●量子化なし

● Pruningなし

●従って精度低下なし✔ 今あるモデルをそのままデバイスに載せることが出来る

Page 7: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

速度・消費電力

Task Resolution Model Device Frames/sec(includes display rendering time)

Power consumption(excludes power for display)

1000-classImage Classification 224x224

MobileNet V2 α=1.0 trained on ImageNet datasetTop-1: 71.8%Top-5: 91.0%

Pi 3 10.1fps 2.8W

Pi 0 8.5fps 2.1W

Face Detection 192x192

Our custom model based on MobileNet V2 SSD trained on Open Images V4.mAP: 66.3%

Pi 3 12.5fps 3.6W

Pi 0 7.8fps 1.9W

Multi PersonPose Estimation 224x224

Our custom model based on Pose Proposal Network trained on MPII dataset.

Pi 3 9.5fps 2.7W

Pi 0 6.6fps 2.0W

Style Transfer 256x256 Our custom model based on Fast Neural Style

Pi 3 4.3fps 3.2W

Pi 0 3.3fps 2.0W

The speed of face detection and pose estimation depends on the number of objects in a frame.This benchmark is taken with one object.

7

Page 8: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

VideoCoreとは

Broadcom製のマルチメディアプロセッサ。Raspberry Piシリーズ等に搭載

• 初代Raspberry Pi~Raspberry Pi 3 Model B+: VideoCore IV• Raspberry Pi 4~: VideoCore VI

• Pi 4はまだ日本では利用できないので未検証。本日は触れません。

Page 9: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

性能

• Pi Zeroの場合、GPUの方が約14.4倍も理論性能が高い• Pi 3の場合CPUの方が性能が高いが、GPUの方が熱対策が易しい、CPUを他処理に空けたい等

の理由で、GPUも活用したい。

BCM2835 BCM2837

Device Pi Zeroなど Pi 3など

CPU 1GHz single-coreARM1176JZF-S

1.2GHz 64-bitquad-core ARM

Cortex-A53

GPU VideoCore IV

CPU peak performance 2Gflops 38.4Gflops

GPU peak performance 28.8Gflops 28.8Gflops

Page 10: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

ソフトウェアスタック

nnoir

py-videocore

Writing CNN kernelsDoing tests and optimization

qdnn

libnn

Graph Compiler

VC4 NEONAVX/SSE CPU

Computation Graph Representation

Neural Network APIPure-GPUCode Generation No-runtime

Code Generation

runtime library

Model Converters

Designed and Developerd by Deep leaning frameworks

Page 11: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

VideoCoreを使う為のSW

拙作のpy-videocoreを使ってVideoCore用のプログラムを書く事が出来ます。

Page 12: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

VideoCore IVのドキュメントは公開されている

● BroadcomからRaspbery Pi財団への誕生日プレゼント

Page 13: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

行列乗算のみGPU化

pure GPU化

もろもろのGPU化や特定計算用の特化コード用意など

レイヤー融合やメモリ転送最適化など golf, メモリ最適化

タイリング最適化などいろいろ

googlenettop1:68.7%, top5:88.9%

mobilenet v1top1:70.9%, top5:88.9%

mobilenet v2top1:71.8%, top5:91.0%

depthwise, pointwise改善メモリ管理改善などいろいろ

※ラズパイのGPU周波数に対する誤解があり、ここ以前はオーバークロック状態 (400MHz)で測っているケースがありました。ここ以後はデフォルト周波数 (300MHz)での値です。

Page 14: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

VideoCore IVのアーキテクチャ

● 16wayのSIMDコアが4x3個○ fp32の乗算,加算

○ Special function unit● HostからGPUへは

L2 Cache経由とDMA● GPUからHostへはDMAのみ

● Register AllocationやScheduling等のハードウェア機能はない

● よくある感じの(ちょっと昔の)アーキテクチャ

Using Raspberry Pi GPU for Deep Neural Networkより

Page 15: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

CNNライブラリ: qdnn

● py-videocoreを用いてCNNのカーネル群を実装● 数学的には1つでも、無数の実装のバリエーションがあり、最適実装は1つではない

アルゴリズム

DirectIm2col

WinogradFFT

テンソルのレイアウト

NHWC ?NCHW ?NHCW ?

ブロック化

処理単位の形状をどうするのか?

Page 16: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

非常に多くのカーネル

● こんな感じに(一番多いところを取ってますが)

※python利用はカーネルのコード生成の為。

コンパイラ自体はHaskell実装。

Page 17: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

Graph Compiler

●複数のレイヤをマージする

●アルゴリズムを選択する● Tensorのshapeなどによって最適な実装が変わる

●あらかじめweightの変換等も必要

●一部はruntimeに決定

●など

Page 18: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

レイヤー融合

Convolution

Batch Normalization

Convolution

Convolution

ReLU

Convolution+ReLU

事前にWeight同士の積、和を計算しておいて一つにまとめてしまう

Convolutionの最内ループにReLUをインライン化する

など

Page 19: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

アルゴリズム

● 2D Convolutionのアルゴリズム○ 入力サイズ=HxW, フィルタサイズ=3x3の場合

手法 概要 演算回数 メモリ使用量 備考

Direct 定義通り - - メモリが弱いプロセッサだとこれが強い場合がある

im2col 行列乗算 同じ 入力が9倍 非常によく使われる

winograd 高速乗算 4/9程度に減少 Weightが16/9倍 よく使われる。Separable Convに弱い。

FFT 高速乗算 最大2/9程度に減少 WeightがHW/9倍 複素演算が必要。あまり使われない。

● 詳しくはConvolutionの数理とアルゴリズム

Page 20: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

ループ最適化

● 古典的な最適化手法。良く効く。奥が深い。○ ループアンローリング

○ ループ交換

○ ソフトウェアパイプライニング

○ など

● ハードウェア特性を意識する事が必要○ アクセス順序

○ アラインメント

○ レジスタの数

○ ハードウェアパイプラインの構造

○ など

Page 21: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

● テンソルをメモリ上にどう配置するか?

○ 以下は2Dイメージの場合の絵

○ 実際はもう一次元(N,バッチ)ある

メモリレイアウト

1 2 3 45 6 7 8

1 2 3 4 5 6 7 8

15

26

37

48

1 2 3 45 6 7 8H

W

Cアドレス

CHW

HWC

HCW

など

(講演時、以下の図が誤っていましたので修正しました)

Page 22: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

並列化

● SIMD● マルチコア

● …N

C

H

W

● どこでどう並列化するのか?

Page 23: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

テンソルのshapeによって最適実装は変わる

● 長さ16のSIMD演算器があったとして、どう使う?

224

あるレイヤ: 224x224の3チャンネル

例えばこの辺の16pixelを計算

あるレイヤ: 7x7の1280チャンネル

7

同じコードだとここの計算が無駄

16

Page 24: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

フィルタサイズでも最適実装は変わる

3

31

1

Page 25: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

Pure-GPU化

●弊社史上最も異常な最適化

●普通● 1 layerずつカーネルを呼び出す

●Pure-GPU実装● 1度ioctlを呼び出すと、CNNを全部実行してから戻ってくる

● GPU上でコントロールフローを実行する方法を発明

Page 26: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

なぜラズパイ?

Page 27: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

実は産業用で使われ始めている

“2018年6月の時点で、月産される50万~60万台のうち、35万~40万台、つまり約60%が産業用途向けとなっているという。”

EETimes Japan, 「ラズパイ」最初の10年、今後の10年http://eetimes.jp/ee/articles/1807/11/news042.html

Page 28: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

安価である

Raspberry Pi 3 Model B+ Raspberry Pi Zero W

$35 $10

Page 29: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

SoMがある

● SO-DIMM 200pin ソケットに差して利用

SOC 価格

Compute Module 1 Pi Zeroと同じ $30Compute Module 3 Pi 3と同じ $30Compute Module 3 Lite Pi 3と同じ $25

Ideinでも過去に利用

Page 30: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

供給保証がある

✔ 以下は、少なくとも2023年まで入手可能

Raspberry Pi 3 Model B+Compute Module 1Compute Module 3Compute Module 3 Lite

Page 31: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

PoEが可能

● PoE Hatを使うとLANケーブル1本で給電・通信が可能✔ LANケーブルは最長100m✔ 配線工事に資格が不要

Page 32: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

カメラが使える

•公式カメラモジュール ✔ 800万画素

✔ 赤外線カメラモジュールもある

•市販のUSBカメラも使える

$30 $30

Page 33: Raspberry Pi Deep Learning のVideoCore GPU 推論の高速化 …Raspberry PiのVideoCore GPUを用いた Deep Learning推論の高速化 Idein株式会社 代表取締役 中村晃一

ハードウェアエコシステム

●様々なディスプレイ

●様々なケース

● HAT● 様々なセンサー

● LTE接続

● GPS● 太陽光発電

● などなど

Revolution Pi

NEC Display Solutions