Share
Embed
モデルアーキテクチャ観点からのDeep Neural Network高速化
モデルアーキテクチャ観点からのDeep Neural Network高速化
-Factorization
-Efficient microarchitecture (module)
-Pruning
-Quantization
-Distillation
-Early termination
Deep Learning Acceleration勉強会発表資料
https://connpass.com/event/64632/
yu4u
September 03, 2017
Transcript
56 slides
-
Copyright © DeNA Co.,Ltd. All Rights Reserved. モデルアーキテクチャ観点からの Deep Neural
Network⾼速化 Yusuke Uchida (@yu4u) 1 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. 今⽇のおはなし(本発表⼀番のマサカリ場) 2 CUDA
cuDNN NVIDIA GPU GPU WebGL WebGPU TPU Video Core IV ??? ??? TensorFlow, Chainer, … deeplearn .js WebDNN ??? ??? @9_Jes さん アプリケーションレイヤ @Kiikurage さん @notogawa さん @kazunori_279 さん @Venginner さん @iwiwi さん @yu4u -
Copyright © DeNA Co.,Ltd. All Rights Reserved. モデルアーキテクチャ観点からの⾼速化 ! Factorization
! Efficient microarchitecture (module) ! Pruning ! Quantization ! Distillation ! Early termination 4 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Convolutionの計算量 5 W
H W H N M K K • ⼊⼒レイヤサイズ:H x W x N • 畳み込みカーネル:K x K x N x M [conv K x K, M]と表記 (e.g. [conv 3x3, 64]) • 出⼒レイヤサイズ:H x W x M • 畳み込みの計算量:H・W・N・K2・M ※バイアス項無視、padding = “same” -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Convolutionの計算量 6 W
H W H N M K K • ⼊⼒レイヤサイズ:H x W x N • 畳み込みカーネル:K x K x N x M [conv K x K, M]と表記 (e.g. [conv 3x3, 64]) • 出⼒レイヤサイズ:H x W x M • 畳み込みの計算量:H・W・N・K2・M ※バイアス項無視、padding = “same” 画像スケール、 チャネル数、 カーネルサイズの2乗に⽐例 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. モデルアーキテクチャ観点からの⾼速化 ! Factorization
! Efficient microarchitecture (module) ! Pruning ! Quantization ! Distillation ! Early termination 7 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Factorization ! Low
Rank Matrix Factorization ⾏列(=畳み込みカーネル)を低ランク近似することで計算量を削減 ! 学習によって得られた⾏列を分解するというよりは 最初から分解した形で重みを学習する (のでfactorizationと⾔って良いのか?) 8 ≈ -
Copyright © DeNA Co.,Ltd. All Rights Reserved. 畳み込みカーネルの空間的Factorization ! ⼤きな畳込みカーネルを複数の⼩さな畳込みカーネルに置き換える
⁃ conv 5x5 と conv 3x3 - conv 3x3 は同じreceptive fieldを持つ ⁃ 計算量は 25:18 ⁃ ⼩さな畳み込みを重ねるパターンはactivation層の数も多いため 表現能⼒の向上が期待できる ! 例 ⁃ AlexNet:conv 11x11 - conv 5x5 - conv 3x3 - … ↓ ⁃ VGGNet:conv 3x3 - conv 3x3 - conv 3x3 - … 9 conv 5x5 conv 3x3 - conv 3x3 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. 畳み込みカーネルの空間的Factorization ! conv
3x3 vs conv 1x3 - conv 3x1 ⁃ 計算量は 9:6 ! 例: Inception v6→v7 10 conv 3x3 conv 1x3 - conv 3x1 hap://lsun.cs.princeton.edu/slides/ChrisJan.pdf -
Copyright © DeNA Co.,Ltd. All Rights Reserved. 畳込みカーネルの空間⽅向とチャネル⽅向のFactorization ! 通常の畳込みカーネルをdepthwise
/ pointwise convolutionに分離 ! Depthwise (separable) convolution ⁃ Input tensorに対しチャネル毎に畳み込み ⁃ 計算量:H・W・N・K2・M (M=N) H・W・K2・N ! Pointwise convolution ⁃ ただのconv 1x1, M ⁃ 計算量:H・W・N・K2・M (K=1) H・W・N・M ! Depthwise + pointwise ⁃ 計算量:H・W・N・(K2 + M) ≒ H・W・N・M (M >> K2) 11 W H W H N 1 1 M W H W H N K K N -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Dilated convolution !
カーネルの重みに規則的に0を⼊れてカーネルサイズを拡張 ! ⼩さいカーネルと同じ計算量+パラメータで広いreceptive fieldを持つ ⁃ Feature mapの解像度を⾼く保ちたいsegmentationや超解像で 良く使われる ! 例 ⁃ Dilated Residual Networks, CVPRʼ17. ⁃ WaveNet - A Generative Model for Raw Audio 12 conv 5x5 conv 3x3, dilaJon = 2 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. 実際の処理速度は?? ! PyTorchとKeras
(TensorFlow backend) で簡単な実験 ⁃ 特定の種類のconvolutionのみを含む16層CNNを構築 • Batch normalizationやactivation層はなし ⁃ ⼊⼒tensorを 32x32x32x64(バッチサイズ32、チャネル数64) ⁃ ランダムな⼊⼒を100回forwardした時間を計測 ⁃ 環境:Ubuntu 16.04, CPU: i7-7700 3.60GHz、 GPU: GeForce GTX1080 (TensorFlowはAVX2等を使うようにコンパイルしていない) ! Summary 13 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. 実際の処理速度は?? ! PyTorchとKeras
(TensorFlow backend) で簡単な実験 ⁃ 特定の種類のconvolutionのみを含む16層CNNを構築 • Batch normalizationやactivation層はなし ⁃ ⼊⼒tensorを 32x32x32x64(バッチサイズ32、チャネル数64) ⁃ ランダムな⼊⼒を100回forwardした時間を計測 ⁃ 環境:Ubuntu 16.04, CPU: i7-7700 3.60GHz、 GPU: GeForce GTX1080 (TensorFlowはAVX2等を使うようにコンパイルしていない) ! Summary 14 Depthwise convoluJon Conv 3x3 dilaJon = 2 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. PyTorch CPU 15
Conv 3x3⽐の 理論計算量 Depthwise convoluJon Conv 3x3 dilaJon = 2 Conv 3x3⽐の 実処理時間 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. PyTorch CPU !
Depthwise convolutionとdilated convolution以外は 実処理時間と理論計算量が⼀致 ! どちらもメモリの局所性がないため? ⁃ 教えて中村さん! ! PyTorchのdepthwise convolutionはめっちゃ遅い ⁃ Grouped convolutionでグループ数=チャネル数でやってるため 16 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. PyTorch GPU !
GPUだとfactorizeしないほうが良さそう ⁃ でも5x5が遅いのは? 17 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Keras CPU !
PyTorchと傾向は同じ ⁃ Depthwise convolutionは専⽤の効率的な実装が存在 ⁃ Dilated convolutionも早い 18 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Keras GPU !
PyTorchとだいたい同じ? ! Dilated convolutionは早い 19 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. 中間的なFactorization ! Efficient
and Accurate Approximations of Nonlinear Convolutional Networks, CVPRʼ15. ! Conv k x k, d を Conv k x k, dʼ - Conv 1 x 1, d に分解 ⁃ 学習済みのカーネルを、出⼒feature mapの誤差を 最⼩に保つように最適化問題を解くことで分解 ⁃ 計算量:dk2c → dʼk2c + ddʼ = dʼ(k2c + d) ≒ dʼk2c 20 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. モデルアーキテクチャ観点からの⾼速化 ! Factorization
! Efficient microarchitecture (module) ! Pruning ! Quantization ! Distillation ! Early termination 21 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Efficient microarchitecture (module)
! Convをスタックするマクロアーキテクチャ (~VGGNet) から マイクロアーキテクチャをスタックする時代へ (GoogLeNet, ResNet~) ! 様々なマイクロアーキテクチャ (module or building block) ⁃ mlpconv (Network in Network) ⁃ Inception module (GoogLeNet) ⁃ Residual block, bottleneck block (ResNet) ⁃ ResNeXT ⁃ DenseBlock (DenseNet) ⁃ Fire module (SqueezeNet) ⁃ Separable conv (Xception, MobileNets) ⁃ Neural architecture search (meta learning) ! ダイレクトに計算量を減らすというよりは 精度と計算量のトレードオフを改善する ⁃ 実⽤上は精度を担保して、どれだけ計算量を減らせるか 22 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Residual Networks, ResNet(参考)
23 基本形 conv 3x3, 64 conv 3x3, 128 conv 3x3, 256 conv 3x3, 512 global average pooling FC x N1 x N2 x N3 x N4 56x56 28x28 14x14 7x7 出⼒ 画像サイズ Building block / Residual unit • パラメータの違うResidual unitを重ねていく • Batch normalizaJon • 画像サイズを半分+フィルタ数を倍に • 最後はglobal average pooling -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Residual block, bottleneck
block ! Bottleneckバージョンは、conv 3x3, 256を Factorizeしたものと⾒ることもできる ⁃ Receptive fieldは⼩さい 24 通常のresidual block “boaleneck” バージョン -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Fire module (SqueezeNet)
! 戦略 ⁃ 3x3の代わりに1x1のフィルタを利⽤する ⁃ 3x3への⼊⼒となるチャネル数を少なくする ! ちなみに⾮常に⼈気のあるモデルだと思われるが… ⁃ ResNetやInception後の論⽂なので、そこからの差異が⼩さい とのことで、ICLRʼ17でrejectとなっている 25 conv 1x1, s1x1 conv 3x3, e1x1 conv 3x3, e3x3 concat Fire module 32 128 128 256 256 Squeeze layer Expand layer conv 3x3, 256の Factorizeと⾒ることもできる -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Fire module (SqueezeNet)
26 Squeeze layerでの圧縮率 0.75程度だと精度低下なし 0.5でー0.7% Expand layerの3x3割合 0.75程度だと精度低下なし 0.5でー0.7% -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Depthwise separable convolution
! Xception ! MobileNets ! Xceptionはdepthwise/pointwise convの間にReLUがない ⁃ 実験でないほうが良いことを⽰している ! Xceptionはidentity mappingがある 27 Depthwise conv 3x3 Pointwise conv 3x3 Batch normalization ReLU Separable conv 3x3 ReLU (Maxpool 3x3, stride=2) Conv 1x1, stride=2 Separable conv 3x3 Depthwise conv 3x3 Batch normalization ReLU Batch normalization Pointwise conv 3x3 ReLU -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Learning Transferable Architectures
for Scalable Image Recognition, arXiv:1707.07012. ! moduleアーキテクチャをRNNで最適化 28 ! ⼤枠のアーキテクチャは決まっている • (ReducJon cell + Normal cell × N) × K ! ReducJon cell:feature mapをダウンサンプル ! 利⽤可能な要素は下記 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Learning Transferable Architectures
for Scalable Image Recognition, arXiv:1707.07012. ! できたmodule ! Sep多い 29 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Learning Transferable Architectures
for Scalable Image Recognition, arXiv:1707.07012. ! 結果 ! Mult-addとのトレードオフは良いが実速は? 30 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. モデルアーキテクチャ観点からの⾼速化 ! Factorization
! Efficient microarchitecture (module) ! Pruning ! Quantization ! Distillation ! Early termination 31 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Pruning ! 異なるレベルのpruning
⁃ 重みレベル ⁃ カーネルレベル ⁃ チャネルレベル ⁃ レイヤレベル 32 Structured (⾼速化しやすい 最適化しづらい) -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Deep Compression: Compressing
Deep Neural Networks with Pruning, Trained Quantization and Huffman Coding, ICLRʼ16 ! 重みレベルのpruning 33 • 通常通り重みを訓練 • しきい値以下の重みをprune(0にする) • 0の重みはそのままにfine-tuneする -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Deep Compression: Compressing
Deep Neural Networks with Pruning, Trained Quantization and Huffman Coding, ICLRʼ16 34 • ⾮ゼロ要素を7.5%にまで削減 • 但し、ほとんどFC層 ↑モダンなモデル (ResNet~) はglobal average poolingを使うので ここまでパラメータの多いFC層がない -
Copyright © DeNA Co.,Ltd. All Rights Reserved. (余談)Global average pooling
! Network In Network, ICLRʻ14. で提案された ! Feature mapサイズの average pooling ! オリジナルの論⽂では N をクラス数にすることで、 global average poolingの出⼒がそのままprediction結果となる ⁃ その後の使われ⽅としては、後段に出⼒がクラス数のFC層を使う ! 何れにせよ、パラメータ数の多いFC層が不要 35 W H N N 1 1 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Deep Compression: Compressing
Deep Neural Networks with Pruning, Trained Quantization and Huffman Coding, ICLRʼ16 ! んで早くなるの? ⁃ 早くなる、FCはね! (←cuSPARSE等のsparse⾏列演算ライブラリを利⽤) ! Sparseな重みのconvolutionは? 36 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Pruning Filters for
Efficient ConvNets, ICLRʼ17. ! チャネルレベルのpruning ! チャネル毎に、畳込みパラメータのL1ノルムが⼩さいものからprune ! 精度を保ったままpruningすると、計算量で10%台の削減 37 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. チャネル毎に⼊⼒を平均0分散1に正規化、γとβでscale & shit
Learning Efficient Convolutional Networks through Network Slimming, ICCV'17. ! チャネルレベルのpruning ! 各チャネルの出⼒をscalingする変数 γ を導⼊、Σi |γi | をロスとして学習 ⁃ γ はbatch normalizationに導⼊する(というか最初からある) ⁃ Batch normalizationの正規化が必須 ! 学習後、γi が⼩さいチャネルi を削除し、fine-tune 38 チャネルi Batch normalizaJon -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Learning Efficient Convolutional
Networks through Network Slimming, ICCV'17. ! 正則化の効果により、pruning後に精度も上がる! ! 感想 ⁃ 学習した後にpruningするより筋が良い ⁃ シンプル&Batch normalizationで実現するの、素敵 ⁃ SOTA精度のモデルをベースにして検証して欲しい ⁃ ResNetのようなskip connectionがあるとややこしい 39 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. L0 ではなくLasso に緩和して解く
Channel Pruning for Accelerating Very Deep Neural Networks, ICCVʼ17. ! チャネルレベルのpruning ! あるfeature mapのチャネルについて、次のレイヤのfeature mapを なるべく復元できるようなチャネルを選択し、残りを削除 ! VGG:5倍速, 精度0.3%↓、ResNet:2倍速, 精度1.4%↓ 40 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. モデルアーキテクチャ観点からの⾼速化 ! Factorization
! Efficient microarchitecture (module) ! Pruning ! Quantization ! Distillation ! Early termination 41 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Quantization ! 重みを量⼦化することでモデルのメモリを削減
⁃ 半精度を利⽤するとかでなければソフト的には⾼速化ではない (デモサイト等でのモデルのダウンロード速度は早くなるが) ! 量⼦化の種類 ⁃ Scalar quantization(各次元毎に量⼦化 • Binary • Uniform(centroidが⼀定間隔 • Adaptive(centroidの間隔をkmeans等で学習 ⁃ Product quantization(ベクトルを分割して個別に量⼦化 ⁃ Residual quantization(量⼦化→残差を量⼦化 ⁃ Hierarchical quantization(量⼦化→残差を量⼦化 ⁃ Vector quantization ! 量⼦化効率(centroid数 vs 量⼦化誤差) ⁃ binary < scalar < product < residual, hierarchical < vector 42 違いは後段の量⼦化コードブ ックが単⼀かどうか -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Quantization ! 重みを量⼦化することでモデルのメモリを削減
⁃ 半精度を利⽤するとかでなければソフト的には⾼速化ではない (デモサイト等でのモデルのダウンロード速度は早くなるが) ! 量⼦化の種類 ⁃ Scalar quantization(各次元毎に量⼦化 • Binary • Uniform(centroidが⼀定間隔 • Adaptive(centroidの間隔をkmeans等で学習 ⁃ Product quantization(ベクトルを分割して個別に量⼦化 ⁃ Residual quantization(量⼦化→残差を量⼦化 ⁃ Hierarchical quantization(量⼦化→残差を量⼦化 ⁃ Vector quantization ! 量⼦化効率(centroid数 vs 量⼦化誤差) ⁃ binary < scalar < product < residual, hierarchical < vector 43 Deep learningの コンテキストでは あまりない -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Deep Compression: Compressing
Deep Neural Networks with Pruning, Trained Quantization and Huffman Coding, ICLRʼ16 ! (再掲) 44 今度はココ -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Deep Compression: Compressing
Deep Neural Networks with Pruning, Trained Quantization and Huffman Coding, ICLRʼ16 ! 0でない重みをadaptive scalar quantization ! 重みを量⼦化した状態のままfine-tuneできる!! 45 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Deep Compression: Compressing
Deep Neural Networks with Pruning, Trained Quantization and Huffman Coding, ICLRʼ16 46 重みのIDを ハフマン符号化 ⾮ゼロ要素間のgapを ハフマン符号化 ! VGG16が552MB → 11.3MB ⁃ 伸張に時間は必要そう -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Binary neural networks
! 神資料があるのでまとめだけ… ⁃ https://www.slideshare.net/kentotajiri/ss-77136469 47 • ⼊⼒も重みもバイナリ化すること で⾼速化が可能 • ソフト的にも実装が可能 • サポートしているメジャーな フレームワークがない • 精度がまだ低い -
Copyright © DeNA Co.,Ltd. All Rights Reserved. モデルアーキテクチャ観点からの⾼速化 ! Factorization
! Efficient microarchitecture (module) ! Pruning ! Quantization ! Distillation ! Early termination 48 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Distilling the Knowledge
in a Neural Network, NIPSʼ14 Deep Learning Workshop. ! アンサンブルモデルや⼤きなモデルから、⼩さなモデルへの knowledge transfer ⁃ 精度を保ちながら⼩さなモデルを学習 49 1. アンサンブルモデル や⼤きなモデルを学習 2. 学習済みモデルを利⽤して ⼩さなモデルを学習 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Distilling the Knowledge
in a Neural Network, NIPSʼ14 Deep Learning Workshop. 50 … … 学習画像 学習済みモデル 学習するモデル … 正解ラベル (ハード ターゲット) 通常T = 1のsotmaxのTを⼤きくした ソフトターゲットを利⽤ … ソフトターゲット ソフト ターゲット ハード ターゲット 正解ラベルと 学習モデル出⼒の 両⽅を利⽤ 推論時はT=1とする(argmaxは同じだが… -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Distilling the Knowledge
in a Neural Network, NIPSʼ14 Deep Learning Workshop. ! 同⼀モデルでも、アンサンブルから学習したほうが良かったり ! ソフトターゲットだと⼩数のデータで効率的に学習できたりする 51 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. モデルアーキテクチャ観点からの⾼速化 ! Factorization
! Efficient microarchitecture (module) ! Pruning ! Quantization ! Distillation ! Early termination 52 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Spatially Adaptive Computation
Time for Residual Networks, CVPRʼ17. ! 各層のhalting scoreの合計が1を超えるとその層までの結果を出⼒ 53 計算量に関するロス を追加 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Spatially Adaptive Computation
Time for Residual Networks, CVPRʼ17. ! 空間(feature map)レベルに拡張 54 SACT=空間レベル ACT=res blockレベル -
Copyright © DeNA Co.,Ltd. All Rights Reserved. Adaptive Neural Networks
for Efficient Inference, ICMLʼ17. 55 -
Copyright © DeNA Co.,Ltd. All Rights Reserved. まとめ ! フレームワークより上のレイヤで意味があるのは
⁃ Factorization ⁃ Efficient microarchitecture (module) ⁃ Pruning • チャネルレベル(、レイヤレベル) ⁃ Quantization • 半精度(、バイナリ) ⁃ Distillation ⁃ Early termination ! 精度重視のモデルで学習し、効率的なmoduleにdistillationし、 pruningし、バイナリ化し、early terminationする → Efficient module + distillation + channel-level pruning 56