{"notes":[{"id":"model-tuning/adapter","category":"model-tuning","legacyPath":"model-tuning/1902-adapter","legacySlug":"1902-adapter","legacyPaths":[],"title":"Adapter","subtitle":"參數節省九成六","author":"Z. Yuan","paperDate":"2019-02-02","publishedOn":"2024-05-16","paperTitle":"Parameter-Efficient Transfer Learning for NLP","paperUrl":"https://arxiv.org/abs/1902.00751","bibliography":{"title":"Parameter-Efficient Transfer Learning for NLP","authors":["Neil Houlsby","Andrei Giurgiu","Stanislaw Jastrzebski","Bruna Morrone","Quentin de Laroussilhe","Andrea Gesmundo","Mona Attariyan","Sylvain Gelly"],"url":"https://arxiv.org/abs/1902.00751","pdfUrl":"https://arxiv.org/pdf/1902.00751","codeUrl":null,"preprint":{"id":"1902.00751","submittedOn":"2019-02-02","url":"https://arxiv.org/abs/1902.00751"},"publication":{"venue":"ICML","year":2019,"kind":"conference","url":"https://proceedings.mlr.press/v97/houlsby19a.html"},"citation":"@misc{modeltuningadapter2019,\n  title = {{Parameter-Efficient Transfer Learning for NLP}},\n  author = {Neil Houlsby and Andrei Giurgiu and Stanislaw Jastrzebski and Bruna Morrone and Quentin de Laroussilhe and Andrea Gesmundo and Mona Attariyan and Sylvain Gelly},\n  year = {2019},\n  eprint = {1902.00751},\n  archivePrefix = {arXiv},\n  url = {https://arxiv.org/abs/1902.00751}\n}\n","citationPath":"/content/papers/citations/model-tuning/adapter.fa08e84b43d7.bib"},"thumbnail":{"src":"/content/papers/assets/8bca77531dbddb82e18f6dedd27d962405dc5c7a0f976c6c0d09a501c918c122.jpg","width":980,"height":712},"locale":"zh-hant","path":"/content/papers/zh-hant/model-tuning/adapter.8c970e76a643.md","headings":[{"id":"定義問題","text":"定義問題","depth":2},{"id":"浪費","text":"浪費！","depth":3},{"id":"解決問題","text":"解決問題","depth":2},{"id":"討論","text":"討論","depth":2},{"id":"glue-評價","text":"GLUE 評價","depth":3},{"id":"消融實驗","text":"消融實驗","depth":3},{"id":"結論","text":"結論","depth":2}],"anchorAliases":{},"guide":null,"relatedReading":[{"id":"model-tuning/lora","relation":"comparison","topic":"用少量可訓練參數調整模型"},{"id":"model-tuning/prefix-tuning","relation":"comparison","topic":"用少量可訓練參數調整模型"}]},{"id":"model-tuning/lora","category":"model-tuning","legacyPath":"model-tuning/2106-lora","legacySlug":"2106-lora","legacyPaths":[],"title":"LoRA","subtitle":"萬分之一的 LLM","author":"Z. Yuan","paperDate":"2021-06-17","publishedOn":"2024-10-26","paperTitle":"LoRA: Low-Rank Adaptation of Large Language Models","paperUrl":"https://arxiv.org/abs/2106.09685","bibliography":{"title":"LoRA: Low-Rank Adaptation of Large Language Models","authors":["Edward J. Hu","Yelong Shen","Phillip Wallis","Zeyuan Allen-Zhu","Yuanzhi Li","Shean Wang","Lu Wang","Weizhu Chen"],"url":"https://arxiv.org/abs/2106.09685","pdfUrl":"https://arxiv.org/pdf/2106.09685","codeUrl":null,"preprint":{"id":"2106.09685","submittedOn":"2021-06-17","url":"https://arxiv.org/abs/2106.09685"},"publication":{"venue":"ICLR","year":2022,"kind":"conference","url":"https://openreview.net/pdf?id=nZeVKeeFYf9"},"citation":"@misc{modeltuninglora2021,\n  title = {{LoRA: Low-Rank Adaptation of Large Language Models}},\n  author = {Edward J. Hu and Yelong Shen and Phillip Wallis and Zeyuan Allen-Zhu and Yuanzhi Li and Shean Wang and Lu Wang and Weizhu Chen},\n  year = {2021},\n  eprint = {2106.09685},\n  archivePrefix = {arXiv},\n  url = {https://arxiv.org/abs/2106.09685}\n}\n","citationPath":"/content/papers/citations/model-tuning/lora.e23eb465372e.bib"},"thumbnail":{"src":"/content/papers/assets/547204d875b324a959dc2fb7c92601337563d9262cd3612847a3a772d092c53f.jpg","width":516,"height":472},"locale":"zh-hant","path":"/content/papers/zh-hant/model-tuning/lora.c13fd6295cb6.md","headings":[{"id":"定義問題","text":"定義問題","depth":2},{"id":"解決問題","text":"解決問題","depth":2},{"id":"秩全秩低秩","text":"秩、全秩、低秩","depth":3},{"id":"為什麼低秩矩陣有用","text":"為什麼低秩矩陣有用？","depth":3},{"id":"低秩分解lora","text":"低秩分解：LoRA","depth":3},{"id":"lora-on-transformer","text":"LoRA on Transformer","depth":3},{"id":"討論","text":"討論","depth":2},{"id":"實驗基準","text":"實驗基準","depth":3},{"id":"和其他微調方法的比較","text":"和其他微調方法的比較","depth":3},{"id":"微調-gpt-3","text":"微調 GPT-3","depth":3},{"id":"在-transformer-中應該對哪些權重矩陣應用-lora","text":"在 Transformer 中應該對哪些權重矩陣應用 LoRA？","depth":3},{"id":"lora-的最佳秩-r-是多少","text":"LoRA 的最佳秩 r 是多少？","depth":3},{"id":"子空間相似性分析","text":"子空間相似性分析","depth":3},{"id":"結論","text":"結論","depth":2}],"anchorAliases":{},"guide":null,"relatedReading":[{"id":"model-tuning/adapter","relation":"comparison","topic":"用少量可訓練參數調整模型"},{"id":"model-tuning/prefix-tuning","relation":"comparison","topic":"用少量可訓練參數調整模型"}]},{"id":"model-tuning/prefix-tuning","category":"model-tuning","legacyPath":"model-tuning/2101-prefix-tuning","legacySlug":"2101-prefix-tuning","legacyPaths":[],"title":"Prefix-Tuning","subtitle":"是他？不是他？","author":"Z. Yuan","paperDate":"2021-01-01","publishedOn":"2024-10-30","paperTitle":"Prefix-Tuning: Optimizing Continuous Prompts for Generation","paperUrl":"https://arxiv.org/abs/2101.00190","bibliography":{"title":"Prefix-Tuning: Optimizing Continuous Prompts for Generation","authors":["Xiang Lisa Li","Percy Liang"],"url":"https://arxiv.org/abs/2101.00190","pdfUrl":"https://arxiv.org/pdf/2101.00190","codeUrl":null,"preprint":{"id":"2101.00190","submittedOn":"2021-01-01","url":"https://arxiv.org/abs/2101.00190"},"publication":{"venue":"ACL-IJCNLP","year":2021,"kind":"conference","url":"https://doi.org/10.18653/v1/2021.acl-long.353"},"citation":"@misc{modeltuningprefixtuning2021,\n  title = {{Prefix-Tuning: Optimizing Continuous Prompts for Generation}},\n  author = {Xiang Lisa Li and Percy Liang},\n  year = {2021},\n  eprint = {2101.00190},\n  archivePrefix = {arXiv},\n  url = {https://arxiv.org/abs/2101.00190}\n}\n","citationPath":"/content/papers/citations/model-tuning/prefix-tuning.fda64a808b55.bib"},"thumbnail":{"src":"/content/papers/assets/21f9834f78ef22b11e57cdbfd5c39edca8f15b7ba5ce59a699bd30d0a6c1bc7c.jpg","width":1834,"height":620},"locale":"zh-hant","path":"/content/papers/zh-hant/model-tuning/prefix-tuning.02f5b48465e6.md","headings":[{"id":"定義問題","text":"定義問題","depth":2},{"id":"解決問題","text":"解決問題","depth":2},{"id":"模型架構","text":"模型架構","depth":3},{"id":"實作細節","text":"實作細節","depth":3},{"id":"資料集設置","text":"資料集設置","depth":3},{"id":"討論","text":"討論","depth":2},{"id":"表格轉文字生成性能","text":"表格轉文字生成性能","depth":3},{"id":"摘要生成性能","text":"摘要生成性能","depth":3},{"id":"小樣本配置","text":"小樣本配置","depth":3},{"id":"外推能力","text":"外推能力","depth":3},{"id":"前綴要多長","text":"前綴要多長？","depth":3},{"id":"初始化策略","text":"初始化策略","depth":3},{"id":"適用場景","text":"適用場景","depth":3},{"id":"為何在未見主題上表現優異","text":"為何在未見主題上表現優異？","depth":3},{"id":"結論","text":"結論","depth":2}],"anchorAliases":{},"guide":null,"relatedReading":[{"id":"model-tuning/adapter","relation":"comparison","topic":"用少量可訓練參數調整模型"},{"id":"model-tuning/lora","relation":"comparison","topic":"用少量可訓練參數調整模型"},{"id":"model-tuning/autoprompt","relation":"comparison","topic":"離散提示、軟提示與前綴"}]}],"text":"BERT 提出後，定調的 NLP 的學習範式：\n\n- **我們必須先用「大量」資料訓練一個「大型」模型，然後再進行微調。**\n\n只要你遵守這個學習範式，你就能在各種 NLP 任務上取得最佳表現。\n\n## 定義問題\n\n本論文的作者認為這件事有個顯而易見的問題：\n\n### 浪費！\n\n回想一下，在目標任務上，我們是不是必須拿整個預訓練模型來微調？\n\n- 即使我們只需要改變其中一小部分。\n\n所以，如果我們想解決十個不同的任務，我們就必須訓練十個不同的模型？\n\n- 即使這些任務之間有很多共通之處。\n\n明明有很多共通之處，為什麼我們還要浪費這麼多資源？\n\n## 解決問題\n\n![Adapter](/content/papers/assets/8bca77531dbddb82e18f6dedd27d962405dc5c7a0f976c6c0d09a501c918c122.jpg)\n\n這裡，我們需要的東西不多，只是給每個任務一個小小的「Adapter」。\n\n作者在現有的 Transformer 架構中加入了一些小但高效的 Adapter 模組，這些 Adapter 的目的是在不增加太多額外參數的情況下，提高模型的表現。\n\n這裡請搭配上圖看一下，作者在 Transformer 的每一層中加入兩個 Adapter 模組。\n\n- **第一個加在 Self-Attention 的輸出投影之後、原有殘差相加與 LayerNorm 之前。**\n- **第二個加在 Feed-Forward 的輸出投影之後、原有殘差相加與 LayerNorm 之前。**\n\n每個 Adapter 模組的工作方式就像一個小型的神經網路，先將原始的高維特徵壓縮到較低維度，再應用非線性轉換，最後再轉換回原來的高維度。\n\n這樣的設計就是我們以前常在 MobileNet 或 SqueezeNet 等輕量級模型中看到的「瓶頸」的設計，這種設計有兩個好處：\n\n- **節省參數：** 這樣的設計只需要原模型大約 0.5% - 8% 的參數量。\n- **調整特徵：** 在較窄的瓶頸中學習任務相關的變換，再投影回原本維度。\n\n另外，為了確保每個 Adapter 模組能夠順利運作，作者還為每個 Adapter 模組內部加入了一個「跳躍連接」，這樣一來，如果 Adapter 模組初始化的參數接近於零，它們就會像恆等函數一樣不會影響原來的輸出，保證了穩定性。\n\n在模型訓練時，預訓練的 Transformer 權重保持凍結；除了 Adapter，作者也訓練每個任務的 LayerNorm 參數和最後的分類層。\n\n## 討論\n\n### GLUE 評價\n\n![benchmark](/content/papers/assets/b9b5229899d20a9c7260137a63debf7626df15c79c41caf38ed41e7ad7ab5050.jpg)\n\n作者用 GLUE 驗證集選擇超參數與模型，上表報告的是 GLUE 測試集成績。\n\n結果顯示：這種 Adapter 模組的設計只增加少量任務專屬參數，就能接近完整微調的表現。\n\n上面表格中有分兩種實驗設計：\n\n- **第一組實驗 (8-256)**：這個設計是針對不同任務設計不同的 Adapter 模組，其特徵維度（瓶頸中的單元數量）在 「8, 64, 256」 三選一。\n- **第二組實驗 (64)**：每個任務仍有各自的 Adapter，但瓶頸維度都固定為 64。\n\n最後，由於訓練不穩定，作者使用不同的隨機種子重新運行 5 次，並在驗證集上選擇最佳模型。\n\n從上面的表格中，Adapter (8–256) 的綜合 GLUE 得分為 80.0，而「完全微調」為 80.4。雖然 Adapter 的表現略低，但每個任務訓練的參數約為 BERT-Large 的 3.6%；解決表中所有任務時，總參數量約為基底模型的 1.3 倍，而完整微調需要 9 倍。這樣的效果已經非常驚人了。\n\n\n> [!TIP]\n> \n> 每個任務不用再訓練整個模型，好棒！至於小顯卡能否放得下基底模型與訓練時的中間張量，仍要看實際設定。\n\n\n### 消融實驗\n\n為了檢驗 Adapter 模組的有效性，作者進行了一系列的消融實驗。\n\n![ablation](/content/papers/assets/24aa3bb40755256dde53a7200fe5e559c1f60c3b6e081a7fe6ffafbe7fa90aa7.jpg)\n\n這是熱圖，顯示已訓練好的 BERT-Base 模型從連續層跨度中移除 Adapter、且不重新訓練時，驗證集準確率的相對變化；左、右圖分別是 MNLI 和 CoLA。\n\n\n> [!TIP]\n> \n> 這個圖第一次看可能有點複雜，我們帶你看一下：\n> \n> - **看 Row0**\n>   - 這時 First ablated layer 等於 0，也就是從第 0 層開始移除 Adapter。\n> - **接著看 Row0，和 Column0**\n>   - 此時 Last ablated layer 等於 0，也就是只移除第 0 層的 Adapter。\n> - **接著看 Row0，和 Column1**\n>   - 此時 Last ablated layer 等於 1，也就是移除第 0 層「至」第 1 層的 Adapter。\n> - **接著看 Row0，和 Column11**\n>   - 此時 Last ablated layer 等於 11，也就是移除第 0 層「至」第 11 層的 Adapter。\n> - **接著看 Row5，和 Column8**\n>   - 此時 First ablated layer 等於 5；Last ablated layer 等於 8，也就是移除第 5 層「至」第 8 層的 Adapter。\n> \n> 相信你已經看懂了，這個圖就是用來看從不同層次移除 Adapter 時效能的變化，每個格子的顏色代表相對準確率下降幅度，愈深色代表降得愈多。\n> \n> 這張圖看上三角的部分即可，下三角沒有意義。\n\n\n實驗結果展示了 Adapter 整體結果：\n\n1. **移除層影響**：\n\n   - 移除單層的 Adapter 對整體效能影響不大，驗證集準確率的相對下降最多約 2%。\n   - 但如果移除所有 Adapter，驗證集準確率會大幅下降，在 MNLI 上降到約 37%，在 CoLA 上降到約 69%，相當於預測多數類別的基線；這裡的 CoLA 準確率不同於上表使用的 Matthews correlation。\n\n2. **高低層影響**：\n\n   - 在 MNLI 上，低層 Adapter（例如 0–4 層）的影響較小，移除它們幾乎不影響效能。\n   - 高層 Adapter 對效能影響更大；作者推測高層較常建構特定任務的特徵。\n\n3. **初始化尺度**：\n\n   - Adapter 的初始化標準差低於 0.01 時，效能穩健。\n   - 初始化標準差過大會導致效能下降，尤其在 CoLA 數據集上。\n\n4. **瓶頸特徵大小**：8、64、256 三種維度在該組分類任務的平均驗證準確率分別為 86.2%、85.8%、85.7%，效能相對穩定。\n\n5. **架構擴展**：添加 BatchNorm 或 LayerNorm、增加 Adapter 層數、更換啟動函數（例如：tanh）、僅在注意層內插入 Adapter 等方法沒有顯著提升效能。\n\n因此，作者推薦最原始的 Adapter 的設計，因為它既簡單又有效，不用傷腦筋。\n\n## 結論\n\nAdapter 模組的設計理念，就像是在模型中加了一些小而高效的插件，只需很少的參數，大約 0.5% 至 8%，就能讓模型效能遷移到我們的目標任務上。例如一個 30B 參數量的模型，只需要 0.15 ~ 2.4 B 的參數量就能達到相同的效果。\n\n能夠讓沒有算力的普通人也能輕鬆微調大型 Transformer 模型，從各方面來說，都是個非常好的消息！\n","imageSizes":{"/content/papers/assets/8bca77531dbddb82e18f6dedd27d962405dc5c7a0f976c6c0d09a501c918c122.jpg":{"width":980,"height":712},"/content/papers/assets/b9b5229899d20a9c7260137a63debf7626df15c79c41caf38ed41e7ad7ab5050.jpg":{"width":1224,"height":232},"/content/papers/assets/24aa3bb40755256dde53a7200fe5e559c1f60c3b6e081a7fe6ffafbe7fa90aa7.jpg":{"width":1522,"height":632}}}
