KK
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 對深度學習模型整形的能力-量化(Quantization) 對經典神經網絡模型進行量化,以減少模型大小和延遲。此任務的目標如下: 實作 K-Means Quantization 及 Linear Quantization - 了解量化的基本概念 - 實現並應用 K-means 量化 - 實現並應用針對 K-means 量化的量化感知訓練 - 實現並應用線性量化 - 實現並應用線性量化的整數推斷 - 基本了解量化帶來的性能改善(如加速) - 了解這些量化方法之間的差異和權衡 1. K-Means Quantization 2. Linear Quantization --- 1. K-Means Quantization ``` 第一步:衡量目前模型 VGG fp32 有 accuracy = 92.95%, size = 35.2 MiB ``` ``` 第二步: 回顧 K-means 的原理 ``` ``` 第三步: 實作,將測試 tensor 做 2-bits k-means quantization 實作,將 VGG model 做 2/4/8-bits k-means quantization ``` ![image](https://hackmd.io/_uploads/Sy5Trjf06.png) ``` 描述上圖 ``` ``` k-means quantizing model into 8 bits 8-bit k-means quantized model has size=8.80 MiB 8-bit k-means quantized model has accuracy=92.79% k-means quantizing model into 4 bits 4-bit k-means quantized model has size=4.40 MiB 4-bit k-means quantized model has accuracy=83.03% k-means quantizing model into 2 bits 2-bit k-means quantized model has size=2.20 MiB 2-bit k-means quantized model has accuracy=10.00% ``` ``` 回顧一下模型經細粒度修剪後,取得 8.15 MiB 及 92.8% accuracy 的成績(此時精度是多少呢?) 8-bits k-means 量化竟然已經可以比擬了,但2/4-bits量化的精度大幅下降,retrain能夠拉回多少精度呢? ``` 第四步: quantization-aware training (QAT) 根據[1],The gradient for the centroids: $\frac{\partial \mathcal{L} }{\partial C_k} = \sum_{j} \frac{\partial \mathcal{L} }{\partial W_{j}} \frac{\partial W_{j} }{\partial C_k} = \sum_{j} \frac{\partial \mathcal{L} }{\partial W_{j}} \mathbf{1}(I_{j}=k)$ where $\mathcal{L}$ is the loss, $C_k$ is *k*-th centroid, $I_{j}$ is the label for weight $W_{j}$. $\mathbf{1}()$ is the indicator function, and $\mathbf{1}(I_{j}=k)$ means $1\;\mathrm{if}\;I_{j}=k\;\mathrm{else}\;0$, *i.e.*, $I_{j}==k$. 該如何實作呢? ``` 看一下效果,QAT 實在好用阿,模型已經壓到大小 2.2 MiB,accuracy = 91.68% k-means quantizing model into 8 bits 8-bit k-means quantized model has size=8.80 MiB 8-bit k-means quantized model has accuracy=92.79% before quantization-aware training No need for quantization-aware training since accuracy drop=0.16% is smaller than threshold=0.50% k-means quantizing model into 4 bits 4-bit k-means quantized model has size=4.40 MiB 4-bit k-means quantized model has accuracy=83.03% before quantization-aware training Quantization-aware training due to accuracy drop=9.92% is larger than threshold=0.50% Epoch 0 Accuracy 92.50% / Best Accuracy: 92.50% k-means quantizing model into 2 bits 2-bit k-means quantized model has size=2.20 MiB 2-bit k-means quantized model has accuracy=10.00% before quantization-aware training Quantization-aware training due to accuracy drop=82.95% is larger than threshold=0.50% Epoch 0 Accuracy 90.88% / Best Accuracy: 90.88% Epoch 1 Accuracy 91.22% / Best Accuracy: 91.22% Epoch 2 Accuracy 91.43% / Best Accuracy: 91.43% Epoch 3 Accuracy 91.68% / Best Accuracy: 91.68% Epoch 4 Accuracy 91.57% / Best Accuracy: 91.68% ``` Linear Quantization 線性量化可表示為 $r = S(q-Z)$ where $r$ is a floating point real number, $q$ is a *n*-bit integer, $Z$ is a *n*-bit integer, and $S$ is a floating point real number. $Z$ is quantization zero point and $S$ is quantization scaling factor. Both constant $Z$ and $S$ are quantization parameters. q 跟基準值 Z 進行比較,其比較結果即差值 (q-Z) 經過調節因子 S 轉換成一離散空間的新數值, 可將輸入的連續空間轉換為離散的輸出空間,實現了對輸入值的量化。 老規矩,寫函式 linear_quantize() 並測試之。 ![image](https://hackmd.io/_uploads/BJoOzBVAT.png) Special case: linear quantization on weight tensor 寫好函式能求出 quantized_tensor, scale, zero_point之後 看看實際例子,把線性量化用在權重上,下圖是 VGG 各層權重從 32-bits 到 4-bits以及 2-bits ![image](https://hackmd.io/_uploads/HyJnX8EA6.png) ![image](https://hackmd.io/_uploads/SJGhEIV0a.png) ![image](https://hackmd.io/_uploads/SJ5h4UVR6.png) ``` 另外需注意 密集的實驗表明,對於不同的輸出通道使用不同的縮放因子S和零點Z會有更好的表現。 因此,我們必須獨立地為每個輸出通道的子張量確定縮放因子S和零點Z "獨立輸出通道"是指權重張量中的每個輸出通道。例如,如果你有一個卷積層,它的權重張量可能具有形狀 [輸出通道, 輸入通道, 高度, 寬度]。在這種情況下,每個"獨立輸出通道"就是指這個四維張量中的每個一維切片,即每個輸出通道的權重。 ``` ``` 量化之後,卷積層和全連接層的推論也會發生變化 ``` --- Ref. [1] Deep Compression: Compressing Deep Neural Networks With Pruning, Trained Quantization And Huffman Coding.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully