地獄貓貓旅行團
      • 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
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners 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

      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.
      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
    • 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 Help
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
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners 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

    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.
    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
    # LoRA ## Week 1B ---- ## 介紹 LoRA 為 Low-Rank Adaptation 的縮寫 在訓練模型時,不對整個模型所有參數都進行訓練 放一個小模型在旁邊,與大模型的輸出合併運算 從而減少反向梯度所需消耗的記憶體使用量 [Paper](https://arxiv.org/abs/2106.09685) ---- ## 運作流程 ![](https://huggingface.co/datasets/trl-internal-testing/example-images/resolve/main/blog/133_trl_peft/lora-animated.gif) ---- ## 原始參數量 若 Batch Size 為 $32$ 則我們的輸入是一個 $32 \times 100$ 的矩陣 $I$ 而模型的權重為 $100 \times 100$ 的矩陣 $W$ 最後輸出的結果為 $I \times W$ 為 $32\times 100\times 100$ 這樣更新參數量大小為 $100 \times 100=10000$ ---- ## LoRA 參數量 LoRA 將計算拆解為 $A,B$ 兩個矩陣 分別為 $100\times10$ 跟 $10\times100$ 的矩陣 然後計算 $I\times A\times B$ 得到 $32\times100\times100$ 但更新參數量只有 $100\times10+10\times100=2000$ 如此一來就減少了八成的參數量 ---- ## 心生疑問 1. LoRA 為何不會增加參數量呢? 2. 為何矩陣 $W,A,B$ 可以合併呢? ---- ## 合併權重 原本的計算為 $I\times W+I\times A\times B$ 根據分配律,可以改成 $I\times (W+A\times B)$ $A \in \mathbb{R}^{100 \times 10}\times B\in \mathbb{R}^{10 \times 100}=W' \in \mathbb{R}^{100\times 100}$ $W \in \mathbb{R}^{100\times 100}+W' \in \mathbb{R}^{100\times 100}=W'' \in \mathbb{R}^{100\times 100}$ $W''$ 的大小與原本的 $W$ 一樣 所以參數量並沒有增加,且三個矩陣能夠合併 --- # 實際操作 ---- ## 安裝套件 + Parameter-Efficient Fine-Tuning (PEFT) + 由 HuggingFace 維護的一個套件,包含 LoRA ``` pip install peft ``` ---- ## 使用方法 原本的訓練程式碼 ```python= model = ModelCls.from_pretrained("gpt2") args = TrainingArguments(...) trainer = Trainer(model, args) trainer.train() ``` 使用 LoRA 的訓練程式碼 ```python= model = ModelCls.from_pretrained("gpt2") model = get_peft_model(model, LoraConfig(r=8)) args = TrainingArguments(...) trainer = Trainer(model, args) trainer.train() ``` [Colab Demo](https://colab.research.google.com/drive/1DAJWDnSdWGxvwuptOpUaqAHK5KRngTv0?hl=en) --- # 實驗結果 ---- ## BERT-Tiny | Comparison | SFT | LoRA | Delta | |------------|---------:|---------:|-------:| | Parameters | 4M | 9K | 99.78% | | GPU Memory | 2560 MiB | 2380 MiB | 7.03% | | Time Cost | 150s | 120s | 20.00% | ---- ## BERT-Base | Comparison | SFT | LoRA | Delta | |------------|-------:|-------:|-------:| | Parameters | 109M | 300K | 99.72% | | GPU Memory | 22 GiB | 19 GiB | 13.64% | | Time Cost | ~45m | ~37m | 17.78% | ---- ## Performance | Epoch | SFT-Loss | SFT-Acc | LoRA-Loss | LoRA-Acc | |:-----:|---------:|--------:|----------:|---------:| | 1.0 | 0.3401 | 0.8574 | 0.4117 | 0.8134 | | 2.0 | 0.3080 | 0.8707 | 0.3968 | 0.8174 | | 3.0 | 0.3050 | 0.8762 | 0.3538 | 0.8442 | | 4.0 | 0.3056 | 0.8790 | 0.3457 | 0.8492 | | 5.0 | 0.3084 | 0.8776 | 0.3392 | 0.8523 | --- # Other ---- ## VSCode Server Install Path + 因為訓練機的 Home 目錄所在硬碟被塞爆了 + 所以需要幫 `.vscode-server` 換個位置 + 可以根據機器設置絕對路徑: ``` "remote.SSH.serverInstallPath": { "trainer57": "/mnt/ssd4t/.vscode-server", "trainer32": "/mnt/ssd2t/.vscode-server" } ``` [Reference](https://github.com/microsoft/vscode-remote-release/issues/472#issuecomment-975745519) ---- ## iThome 鐵人賽報名中 由 iThome 舉辦的鐵人賽 連續 30 天,每天寫一篇技術文章 完賽就會給你一個獎牌 [九月開賽,即刻報名!](https://ithelp.ithome.com.tw/2023ironman/event)

    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

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    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