蝦想承為工程師
    • 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
    • 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
    • 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
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
  • 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
    1
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    ## 分散式訓練的類型:Data Parallel vs. Model Parallel 隨著深度學習模型的規模不斷擴大,單一計算設備已無法滿足訓練所需的記憶體和運算能力,分散式訓練技術因此成為解決這一問題的關鍵手段,在眾多分散式訓練方法中,數據並行(Data Parallelism)和模型並行(Model Parallelism)是最基礎且應用廣泛的兩種類型。 ## 什麼是 Data Parallel? 數據並行將訓練數據分成多份,分配到不同設備,每設備持有一份完整模型副本,獨立計算梯度後同步更新參數,其簡單高效,適合數據量大且模型較小的場景,梯度同步是數據並行的核心,常用方式包括 Parameter Server (PS) 和 Ring-All-Reduce。 ### Parameter Server(PS) Parameter Server 採用中心化架構,設有一個或多個參數伺服器(Server)負責存儲和管理全局模型參數,訓練過程中,各工作節點(Worker)計算本地梯度後發送至參數伺服器,伺服器聚合梯度並更新參數,再將更新後的參數廣播回各工作節點,這種方式易於擴展到大量節點,但在伺服器成為瓶頸時(如通信負載過高),效率可能下降。 ![image](https://hackmd.io/_uploads/SkX0hxNAye.png) ### Ring-All-Reduce Ring-All-Reduce 則是去中心化的同步方式,節點間形成環狀拓撲結構。各節點將梯度分塊,依次傳遞給下一個節點,通過多次傳遞與局部聚合完成全局梯度計算,最終所有節點獲得一致的更新結果。其優勢在於通信負載均勻分擔,無單點瓶頸,特別適合高帶寬環境,如 NVIDIA NCCL 支援的 GPU 集群,但隨著節點數增加,環內傳輸次數也增多,可能影響速度 ![image](https://hackmd.io/_uploads/rySdTgE0yl.png) ## 什麼是 Model Parallel? 當深度學習模型的參數量遠超單張 GPU 所能容納(例如 GPT-3、PaLM、LLaMA-3 等),傳統的數據並行已無法滿足訓練需求,此時就必須採用模型並行(Model Parallelism),也就是將模型本身拆解成數個部分,分布到多張 GPU 上運行。 ### Tensor slicing(張量切分) Tensor Slicing 是操作層內部的矩陣切分(例如線性層的權重矩陣),並將這些切分後的張量分配到不同 GPU 上計算,例如矩陣乘法 $X ⋅ A = Y$,其中 A 是權重矩陣, 𝑋 是輸入張量, 𝑌 是輸出張量,將 A 按行或列拆分,在多張 GPU 上平行處理,最後通過通信機制將結果整合。 ![image](https://hackmd.io/_uploads/SkmhNZNAJx.png) 以下圖片展示了 MLP 和 Self-Attention 對於Column-wise 和 Row-wise Slicing 的結合,分擔到多設備減輕記憶體壓力。 * Row-wise slicing:按列分割,需要通信來合併結果(加法操作),因為每個設備計算的結果是對最終輸出的部分貢獻,而不是獨立的部分。 * Column-wise slicing:按行分割,每個設備計算部分輸出,無需額外通信即可拼接結果。 ![image](https://hackmd.io/_uploads/B1O-wbV0yg.png) ### Layer-wise partitioning(層級切分) Layer-wise 是模型並行的傳統方式,將神經網路按層次分割到多個設備,例如,訓練 BERT-Large(3.4 億參數)時,單 16GB GPU 不足,可將 24 層 Transformer 分為前 12 層與後 12 層,部署至兩 GPU,利用 NVLink 高效傳輸中間結果完成訓練,但設備間因層間依賴可能導致閒置,效率低於 Tensor Slicing。 ![image](https://hackmd.io/_uploads/rkujVZNC1e.png) ## Pipeline Parallelism 在典型的深度學習模型中,尤其是 Transformer 架構,其模型由多層堆疊而成(如 GPT 的上百層 Transformer blocks),這些層的結構相對規則,非常適合沿模型深度方向進行切割,例如,若一個模型有 24 層,使用 4 張 GPU 訓練,則可以將每 6 層視為一個階段,分別交由不同的 GPU 負責。資料在訓練時被劃分成多個 micro-batches,第一個 micro-batch 從第一張 GPU 開始進行前向傳播,當它被傳遞至第二張 GPU 時,第一張 GPU 就可以立刻開始處理下一個 micro-batch。這樣一來,各 GPU 可以同時處理不同階段的不同資料,實現高度並行。 ### 優勢 1. 即使單張 GPU 記憶體不足以容納整個模型,也能透過模型切段的方式進行訓練,同時提升訓練吞吐量。 2. Pipeline Parallelism 是以「模型層的順序結構」為基礎進行切分,更貼近語義層次上的劃分,因此也常被視為 Layer-wise Model Parallelism 的實作之一。 ### 挑戰 1. Pipeline Bubble (管線空轉):當訓練開始時,前段 GPU 會先開始工作,而後段 GPU 必須等待前面的資料處理完成,才能接手運算,這造成初期與結尾的 GPU 利用率偏低。 2. 反向傳播的梯度也必須依序回傳,對訓練時序管理與記憶體調度提出更高要求。 為解決這些問題,研究人員提出了「微批次重疊(micro-batch interleaving)」與「梯度累積(gradient accumulation)」等技術,用以減少 idle time 並平衡 forward / backward 的流動。 ![image](https://hackmd.io/_uploads/rJ926ZV0kg.png) ### 實際案例 Google 在訓練 PaLM(5400 億參數)與 Gemini 模型時,就廣泛採用 GSPMD(General and Scalable Parallelism for ML)框架,結合了 pipeline、tensor、sharded、data 並行四種策略,才得以成功推動超兆級模型的訓練。 ## 總結 分散式平行化訓練技術並非單一方法,而是一組可根據硬體條件與模型特性靈活組合的策略工具箱,現今的SOTA模型往往採用 Data Parallel + Tensor Parallel + Pipeline Parallel 三重混合策略,配合進階通訊協定與記憶體管理機制,在數百張 GPU 甚至 TPU 上高效訓練超大規模模型。

    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 Google 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