Try   HackMD

Neural Network Compression Framework for fast model inference

Introduction

在傳統的 Deep neural networks (DNNs) 已經有著許多的突破,通過引入上百萬個參數來進行訓練以改善準確性,然而,為了得到正確的預測而需要進行大量的運算,會大大的影響到效能。

在模型分析 (Model analysis) 的研究上表示,大多數的DNN有著大量的冗餘,因為他們都被設計是用來得到最高的準確率,並未考慮到運算的時間。

另一方面,模型部署需要在準確度和裝置性能上權衡,因此時至今日發展出越來越多的方法來讓 Deep Learning (DL) 變得越來越有效率,讓模型得以部署到邊緣裝置 (Edge Device) 上。

壓縮的方法可以大致分為兩類,第一類方法為 Neural architecture search (NAS) 神經結構搜尋,其允許為特定數據集和用於模型推斷的特定硬體構建高效的神經網路,第二類方法旨在提高現有DL模型的性能,對其模型架構設計而沒有太大影響。

舉例來說,量化 (quantization),其用於將模型從浮點數轉換到定點表示,並允許以有效的方式使用硬體來加速定點算法。

本篇論文的貢獻如下

  • 具有微調的量化 (quantization) 、二值化 (binarization) 、稀疏性 (sparsity) 和過濾修剪演算法 (filter pruning algorithms)
  • Pytorch中的自動模型圖形轉換
  • 能夠堆疊壓縮的方式,並同時應用多個方法
  • 用於圖像分類,物體檢測和語義分段任務以及配置文件以壓縮一系列的模型。
  • 能夠將壓縮感知培訓集成到第三方存儲庫中,以最少的修改現有的訓練管道,將NNCF整合到大規模模型/管道的存儲庫(如MMDetection 或 Transformers)中。
  • 用於快速模型微調的硬體加速層和支援多個GPU訓練。
  • 兼容 OpenVINO Toolkit
  • Quantization-Aware Training (QAT)
    所謂的model quantization指的是將原本使用floating point計算的model (大多為float32),在推理時改為使用更小精準度的單位 (常見int8或int16)
    想當然,這樣的操作容易使網路的預測效果變差,與之對應的是在訓練時模擬int8的行為,實際上運行仍然是保持floating point的狀態,保證梯度的精準,這樣的訓練模式稱為training aware quantization (QAT)。
  • Network Sparsification
    進行模型壓縮,或者稀疏化權重矩陣的目標不外乎這幾個目標
    • 證明模型的引數存在冗餘,減少冗餘之後準確率沒有明顯下降。
    • 減少模型的引數,對模型進行壓縮。
    • 模型壓縮或者稀疏化之後就可以進一步實現模型預測的加速。

Framework Architecture

  • Compression Algorithm Builder
    對基礎模型進行更改以模擬當前的壓縮演算法的元件。

  • Compression Algorithm Controller
    提供對訓練期間的壓縮演算法參數和統計數據的元件(例如模型中某層的量化位元寬度,或某個層中的稀疏程度)。

  • Compression Loss
    在壓縮演算法中引入的額外損失函數,以便於壓縮。

  • Compression Scheduler
    定義為在訓練過程中自動控制壓縮方法的參數,在不使用 Compression Algorithm Controller 的情況下以每Epoch或每秒進行更新。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

在壓縮完畢後,NNCF會輸出ONNX格式的檔案,可以在OpenVINO進行使用。

Compression Methods Overview

  • Quantization
    Quantization是最常見也最常使用的壓縮技術
    基本的公式為
    q=rs+z

    s 和 z 是 Quantization 的參數,其中s會和輸入r有相同的type,而z會在q有相同的Type。

在對稱量化(Symmetric quantization)下,z會被設為0,其優點是簡單性,而在非對稱量化(Asymmetric quantization)下,充分的利用了量化的範圍,可能會導致更好的準確度。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

(W8/A8意思為 Weight 的Tpye為INT8/ Activation 的Type為INT8)

圖片中,比較的模型為MobileNet-V2,資料集為CIFAR-100,使用Top-1 accuracy進行衡量標準,在FP32下模型準確率為65.53%。

  • Binarization
    目前NNCF支援PyTorch的Conv2D的權重二值化
    二值化顧名思義就是透過一個特定的公式將輸入轉換至0或1的數值進而加速計算的過程。
    下圖為模型經由不同二值化的方式的比較。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

圖片中,使用的模型為ResNet-18,資料集為ImageNet,使用Top-1 accuracy進行衡量標準。

  • Sparsity
    在NNCF中稀疏化的方式分為兩種

    • Magnitude-based sparsity
      在Magnitude-based sparsity中,權重的大小被視為重要的程度,在NNCF中定義了稀疏化比例(sparsity level, SL,權重為0的比例),透過SL產生了特定的閥值(Threshold),如果權重低於特定的閥值,那權重將會被設為0。
    • Regularization-based sparsity
      在Regularization-based sparsity中,會定義出一個loss function為
      Lreg=(j=1|θ|[θj0]θ(1SL))2

      |θ|
      是模型的總參數量,SL是稀疏化的比例(權重為0的比例),透過定義損失函數來權重0的總和與SL來逼近。
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

      此圖片為模型經過Sparsification + quantization後的比較,RB為Regularization-based sparsity,Mag為Magnitude-based sparsity。
  • Filter pruning
    在NNCF中用來進行Filter pruning的演算法有三種
    l1-norm 和 l2-norm是透過各自對應的正規化,將較不重要的filter移除。

    • l1-norm
    • l2-norm
    • geometric median
      找出所有Filter的geometric median,將數值與所有filter的geometric median較相近的Filter移除,進而達成pruning。

在NNCF中,使用者也可以自行設定pruning rate,如圖所示,該pruning rate為30%。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Results

此篇研究中在最後實驗了一系列的模型,從影像分類、物件偵測、語意辨識和NLP,結果如圖所示

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

在壓縮的最後NNCF會輸出ONNX格式的檔案,可以用於OpenVINO,其加速效果和準確率的下降如圖所示

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Resourse

Neural Network Compression Framework for fast model inference