LLM 研究方向(一): LLM Prompts--p-tuning、LoRA

目錄

1. prompt-tuning background

2. Prompt Tuning 模型介紹

2.1 2021 prefix-tuning?

2.2 2021 P-tuning v1

2.3 2021 Parameter-efficient prompt tuning (PET)

2.4 2022 P-tuning v2?

2.5 2019 Adapter?

?2.6 2021 LoRA (Low-Rank Adaptation)?

2.7 2024 DoRA (Weight-Decoupled Low-Rank Adaptation)

3. LoRA Implementation

3.1? LoRA 復現 01: MiniLoRA

3.1.1 core codes:torch.nn.utils.parametrize.register_parameterization?參數化應用函數

3.2 LoRA 復現 02: LoRA from Scratch on MNIST?

3.2.1 core codes: Lightning 深度學習框架?

3.3 LoRA 復現 03: Torch tutorial with torchtune

3.3.1 core codes:??torchtune package 介紹

3.4 LoRA 復現 04: peft implementation

3.4.1 core codes: AutoModelForSeq2SeqLM 介紹

3.4.2 code codes: peft package 介紹

3.5 *LoRA 05: Explanation

Reference:?


1. prompt-tuning background

problem: 之前的fune-tuning/model-tuning是對大模型進行下游任務re-training,即對whole模型參數進行微調!但由于LLM參數量太大,fine-tuning需要大量的數據、算力去更新學習參數,不夠實用

solution:prompt-tuning (p-tuning),是一種通過提示詞(prompt tokens)優化生成式預訓練模型(e.g. GPT)的技術旨在通過調整prompts而不是整個模型參數來提高模型在特定任務上的表現,達到節省計算開銷和資源消耗、保持甚至提升model performance的目的

按照時間順序,prompt-tuning演進過程分別是:prefix-tuning、p-tuning v1、parameter-efficient prompt tuning、p-tuning v2。

2. Prompt Tuning 模型介紹

2.1 2021 prefix-tuning?

prefix-tuning, paper: Optimizing Continuous Prompts for Generation, 就是在input tokens前面加上幾個與任務相關task-specific的tokens,并用MLP_{\theta}單獨訓練生成embeddings

Note:tokens不拼接!原有的input tokens依舊用transformer生成embeddings,并且保持transformer參數不變。The prefix tokens' embeddings?\vee _i \in P_{idx}, hi are drawn from a trainable matrix MLP~P_\theta. Then remaining tokens' embeddings are computed by the Transformer.

  • 優點:實現簡單、高效訓練、任務一致性。
  • 缺點:適用性有限,prefix-tuning在一些特定任務中效果不如p-tuning,e.g. 上下文限制,由于prefix embeddings始終位于序列前端,可能無法充分利用輸入序列的上下文信息。

2.2 2021 P-tuning v1

p-tuning v1, paper: GPT Understands, Too. 它通過在輸入層提示模板固定位置插入可訓練的提示詞向量trainable prompt tokens?embeddings,來提升模型性能。

problem: Previous prompts方法是離散discrete向量空間,主要是從詞庫V中選詞vi作為提示詞prompt來出入提示模板的第i個位置,并用prompt generator來生成提示詞向量prompt embeddings。這種固定的提示詞叫作hard prompt,只能用來微調整個模型的參數 pre-trained model parameters

solution: p-tuning v1是連續continuous向量空間,主要是通過prompt encoder生成trainable parameterized prompt embeddings來代替詞庫詞vi插入輸入層,這種generated trainable prompts稱為soft prompt

  • 初始化 initialize prompts: <T1> <T2> The movie was fantastic <T3> <T4>. -> 訓練優化 -> 推理 inference,這時不BP
  • 優點:少量參數、提高性能、通用性強。
  • 缺點:訓練復雜;依賴提示詞位置。

2.3 2021 Parameter-efficient prompt tuning (PET)

Parameter-efficient prompt tuning, paper: The power of scale for parameter-efficient prompt tuning, 可以在輸入序列的任意位置插入trianable prompt embeddings

2.4 2022 P-tuning v2?

p-tuning v2, paper: Prompt tuning can be comparable to fine-tuning universally across scale and tasks,? 多層提示prompt,在每一層加上prefix prompt embeddings。?

problem: 在模型參數量小于10B的訓練中,prompt training效果要低于fine-tuning。

solution:p-tuning v2在每一層都加上了layer prefix prompt embeddings,不同任務可以共享相同的網絡參數,支持多任務學習。?

  • 優點:可以更好地捕捉和利用上下文信息,進一步提高模型性能、更好泛化、靈活性強。
  • 缺點:實現復雜;計算開銷增加。

2.5 2019 Adapter?

paper: Parameter-Efficient transfer learning for NLP.

2.6 2021 LoRA (Low-Rank Adaptation)?

paper: Low-Rank Adaptation of Large Language Models.

W_{LoRA} = W_{orig} + \Delta W = W_{orig} + B*A?

LoRA保持pre-trained model參數凍結,只在原始矩陣中添加一個\Delta W參數,其參數比原始矩陣少。?

problem: 如果我們構造一個與Worig具有相同維度nxm的新\Delta W矩陣來對模型進行微調,模型performance沒有提升!還會將參數加倍!

solution:所以設計鬼才提出了低秩概念r,通過基于低秩r的低維矩陣乘法來構造\Delta W = B_{n\times r}A_{r\times m}, r << n和r << m,B和A相乘會產生一個與\Delta W具有相同維度的矩陣,但由更少的參數構成。因為我們希望訓練開始時增量為零,讓微調像原始模型一樣開始。因此,B通常被初始化為零矩陣,而A被初始化為隨機值(即正態分布)。

For example,input dim=1024,那origin W參數量=1024*1024\approx100萬,而低秩參數量=1024*4+4*1024?\approx8k。

優點:

  • 效率高,使用更少的參數。?
  • 提高泛化性能 《-- 通過限制模型復雜性,防止過擬合。
  • 可以無縫集成到現有的神經網絡中。

2.7 2024 DoRA (Weight-Decoupled Low-Rank Adaptation)

核心:每個權重矩陣W通過多個低秩矩陣Ai和Bi的乘積進行近似,可以表示為:W \approx \sum_{i=1}^k A_i B_i

3. LoRA Implementation

LoRA實現公式:W_{LoRA} = W_{orig} + \frac{\alpha}{r} \Delta W

my github link:?GitHub - yuyongsheng1990/LLM_Prompts

3.1? LoRA 復現 01: MiniLoRA

簡單、通俗、易懂、powerful

reference:minLoRA/demo.ipynb at main · cccntu/minLoRA · GitHub

3.1.1 core codes:torch.nn.utils.parametrize.register_parameterization?參數化應用函數
from functools import partial  # 用于固定某些函數的參數,從而創建一個新的函數。這個新函數會記住被固定的參數,并在調用時使用這些固定參數。
'''
simple example: torch.nn.utils.parametrize.register_parametrizationoutput: 原始參數(weight或bias)會被替換為一個通過指定參數模塊生成的參數。Linear((weight): ParametrizationList((0): MyParametrization())(bias): Parameter containing: [torch.FloatTensor of size 5])
'''
# -----------------single lora parameters---------------
linear = nn.Linear(5, 5)
print(linear)
class LowRankParametrization(nn.Module):def __init__(self, original_weight, rank=4):super().__init__()self.rank = rankself.U = nn.Parameter(torch.randn(original_weight.size(0), rank))self.V = nn.Parameter(torch.randn(rank, original_weight.size(1)))def forward(self, x):return self.U @ self.V# 注冊低秩參數化
'''torch.nn.utils.parametrize.register_parametrization函數用于在模型的參數上注冊新的參數化方法。這個功能允許你在現有參數layer.weight上應用一些變換LoRAParametrization,特別適用于LoRA
'''
parametrize.register_parametrization(linear, 'weight', LowRankParametrization(linear.weight))
# ----------------multiple lora parameters-------------------
# 可以順序應用多個參數化方法,繼續加就行 <--對應DoRA
# 定義第二個參數化方法
class MultiplyByTwoParametrization(nn.Module):def __init__(self, original_weight, rank=4):super().__init__()self.rank = rankself.U = nn.Parameter(torch.randn(original_weight.size(0), rank))self.V = nn.Parameter(torch.randn(rank, original_weight.size(1)))def forward(self, x):return self.U @ self.V
parametrize.register_parametrization(linear, 'weight', MultiplyByTwoParametrization(linear.weight, rank=3))# 打印線性層,查看參數化后的結果
print(linear)
'''
output:Linear(in_features=5, out_features=5, bias=True)  # 原始linear層-------------------------------------------------ParametrizedLinear(                          # 替換后的參數化線性層para linearin_features=5, out_features=5, bias=True   # 這表示layer原始參數original weight(parametrizations): ModuleDict(            # parametrizations表示應用參數化方法,新模型參數會存儲在ModuleDict中,ModuleDict是一個module容器,它像一個dict一樣工作。(weight): ParametrizationList(           # 這表示weight原始參數現在被替換/應用了ParametrizationList中一個或多個參數化方法.(0): LowRankParametrization()          # (0)表示ParametrizationList的第一個參數化方法。# (1): MultiplyByTwoParametrization()    # 順序應用:當ParametrizationList存儲多個參數化方法時,所有方法會按順序應用到weight參數上。)                                        ))
'''

3.2 LoRA 復現 02: LoRA from Scratch on MNIST?

reference:?lora_from_scratch/lora_on_mnist.ipynb at main · sunildkumar/lora_from_scratch · GitHub

3.2.1 core codes: Lightning 深度學習框架?
import lightning as L  # lightning是一個高層次的深度學習框架,建立在pytorch之上,用于簡化和加速模型的開發和訓練過程。
from lightning.pytorch.loggers import CSVLogger  # 用于將訓練日志記錄到csv文件中,便于之后的分析和可視化。
from lightning.pytorch.callbacks import LearningRateFinder  # 通過在training過程中調整學習率lr來找到最優的學習率,以提升模型性能
from lightning.pytorch.callbacks.early_stopping import EarlyStopping  # 用于在validation loss不再改善時提前停止,防止模型過擬合。from pytorch_lightning import Callback # 用于實現自定義的回調函數,在training過程中的特定時間點執行特定的操作,比如記錄日志、保存model、調整lr。

3.3 LoRA 復現 03: Torch tutorial with torchtune

reference:?Finetuning Llama2 with LoRA — TorchTune documentation

3.3.1 core codes:??torchtune package 介紹
from torchtune.models.llama2 import llama2_7b, lora_llama2_7b  # torchtune是一個torch庫,用于輕松創作、微調和試驗LLM。
'''torchtune, https://pytorch.org/torchtune/stable/index.html- Llama3 in torchtune- Finetuning with LoRA in torchtune- Understanding QLoRA in TorchTune- End-to-End Workflow with torchtune
'''

3.4 LoRA 復現 04: peft implementation

reference:??LoRA-Implementation/prepare_data.py at main · hahuyhoang411/LoRA-Implementation · GitHub

3.4.1 core codes: AutoModelForSeq2SeqLM 介紹
'''from transformers import AutoModelForSeq2SeqLM, AutoTokenizer# 指定模型名稱或路徑model_name = "t5-small"# 加載預訓練模型和分詞器model = AutoModelForSeq2SeqLM.from_pretrained(model_name)tokenizer = AutoTokenizer.from_pretrained(model_name)# 輸入文本input_text = "Translate English to French: How are you?"# 編碼文本--成模型可接受的輸入格式inputs = tokenizer(input_text, return_tensors="pt")# 生成輸出outputs = model.generate(**inputs)# 解碼輸出文本output_text = tokenizer.decode(outputs[0], skip_special_tokens=True)print(f"Input: {input_text}")print(f"Output: {output_text}")
'''
3.4.2 code codes: peft package 介紹
'''
peft (Parameter-Efficient Fine-Tuning) package introduction:Fine-tuning large pretrained models is often prohibitively costly due to their scale. PEFT methods enable efficient adaptation of large pretrained models to various downstream applications by only fine-tuning a 
small number of (extra) model parameters instead of all the model's parameters. This significantly decreases the computational and storage costs. Recent state-of-the-art PEFT techniques achieve performance comparable to fully fine-tuned models.PEFT is integrated with Transformers for easy model training and inference, 
peft簡化了LLM-finetuning 模型配置和加載功能,特別是使用LoRA等技術。- LoraConfig,用于配置LoRA參數。- TaskType,用于定義任務類型, e.g. task_type = TaskType.TEXT_GENERATION- get_peft_config,用于獲取peft配置- get_peft_model,用于獲取pretrained peft模型。
''''''
----------------peft翻譯模型---------------------
# 翻譯模型bigscience/mt0-large: English -> French
'''
# prepare a model for training with a PEFT method such as LoRA by wrapping the base model and PEFT configuration with get_peft_model.
# For the bigscience/mt0-large model, you are only training 0.19% of the parameters!
from transformers import AutoModelForSeq2SeqLM  # 用于加載和處理pre-trained seq2seq模型,用于處理nlp任務
from peft import get_peft_config, get_peft_model, LoraConfig, TaskType# 加載預訓練模型和分詞器 
model_name = 'bigscience/mt0-large'
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)# 定義lora配置
lora_config = LoraConfig(task_type = TaskType.SEQ_2_SEQ_LM, inference_mode=False, r=8, lora_alpha=32, lora_dropout=0.1
)# 獲取peft model
peft_model = get_peft_model(model, peft_config)
print(peft_model.print_trainable_parameters())  # 輸出peft mode可訓練參數# 準備輸入數據
input_text = "Translate English to French: How are you?"
inputs = tokenizer(input_text, return_tensors="pt")# 使用 PEFT 模型生成輸出
outputs = peft_model.generate(**inputs)
output_text = tokenizer.decode(outputs[0], skip_special_tokens=True)  # 解碼
print(outputs)
print(output_text)'''
------------peft因果推理模型----------------------
因果推理模型 ybelkada/opt-350m-lora; gpt2
'''
from peft import AutoPeftModelForCausalLM  # 用于加載和配置因果語言模型Causal LM,并進行高效微調參數
from transformers import AutoTokenizer
import torchdevice = 'cuda' if torch.cuda.is_available() else 'cpu'
model = AutoPeftModelForCausalLM.from_pretrained('ybelkada/opt-350m-lora').to(device) 
tokenizer = AutoTokenizer.from_pretrained('facebook/opt-350m')model.eval()
inputs = tokenizer('Preheat the oven to 350 degrees and place the cookie dough', return_tensors='pt')outputs = model.generate(input_ids=inputs['input_ids'].to(device), max_new_tokens=50)  # 生成輸出
outputs_text = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]  # tokenizer解碼輸出文本
print(outputs)
print(outputs_text)

3.5 *LoRA 05: Explanation

***選看:太難、太復雜,不做實現嘍

reference:?使用Pytorch從零開始構建LoRA_torch lora 使用 nn-CSDN博客

3.5 *LoRA 06: huanhuan chat

***選看:太難、太復雜,不做實現嘍

reference:?https://github.com/datawhalechina/self-llm/blob/master/GLM-4/05-GLM-4-9B-chat%20Lora%20%E5%BE%AE%E8%B0%83.ipynb

Reference:?

[1]?He J, Zhou C, Ma X, Berg-Kirkpatrick T, Neubig G. Towards a unified view of parameter-efficient transfer learning. arXiv preprint arXiv:2110.04366. 2021 Oct 8.

[2]?https://mltalks.medium.com/%E8%AF%A6%E8%A7%A3%E5%A4%A7%E6%A8%A1%E5%9E%8B%E5%BE%AE%E8%B0%83%E6%96%B9%E6%B3%95prompt-tuning-%E5%86%85%E9%99%84%E5%AE%9E%E7%8E%B0%E4%BB%A3%E7%A0%81-7e4276927729

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/44210.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/44210.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/44210.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

詳解[USACO07OPEN] Cheapest Palindrome G(洛谷PP2890)(區間DP經典題)

題目 思路 考慮區間DP。 設dp[i][j]為從i到j這段區間被修正為回文串的最小花費 c[cc][1]為添加字符cc的花費 c[cc][2]為刪去字符cc的花費 s為題目給出的字符串。 用[i 1,j]區間轉移&#xff1a;這種轉移相當于在[i1,j]區間的左邊加入一個字符&#xff0c;讓[i,j]變為回文的方…

SQL 游標

關系數據庫中的操作會對整個行集起作用。 例如&#xff0c;由 SELECT 語句返回的行集包括滿足該語句的 WHERE 子句中條件的所有行。 這種由語句返回的完整行集稱為結果集。 應用程序&#xff0c;特別是交互式聯機應用程序&#xff0c;并不總能將整個結果集作為一個單元來有效地…

安裝Go語言常用工具

好的&#xff0c;這里是常用的Go工具&#xff0c;包括下載地址、在線安裝指令和離線安裝步驟。 1. gopls (Go language server) 功能: 提供代碼補全、跳轉定義、文檔提示等IDE功能。 下載地址: golang.org/x/tools/gopls 在線安裝命令 : sh 復制代碼 go install golang.org/…

云倉適合什么商家?

1、電商平臺和網店&#xff1a; 對于擁有大量在線訂單但沒有自建倉儲和物流能力的小型和中型電商企業&#xff0c;云倉可以在成本和效率上提供顯著優勢&#xff0c;幫助快速處理訂單并縮短配送時間。 —————————————————————————————————— …

根據關鍵詞query獲取google_img(api方式)

文章目錄 說明代碼第一部分&#xff1a;鏈接保存為Json第二部分&#xff1a;鏈接轉換為img 說明 根據關鍵詞query獲取google_img USERNAME “xxx” PASSWORD “xxx” 官網申請。 代碼 首先獲取圖片鏈接&#xff0c;保存為json之后下載。 第一部分&#xff1a;鏈接保存為…

.net 8 使用 quic 協議通訊

debian環境安裝 quic支持 # 1. 添加unstable倉庫&#xff08;如果您使用的是Debian的不穩定分支&#xff09; sudo apt install apt-transport-https ca-certificates sudo wget -O /etc/apt/trusted.gpg.d/microsoft.gpg https://packages.microsoft.com/keys/microsoft.asc …

【獨家揭秘】視頻號矩陣系統火爆上線,一鍵式多平臺管理,你的內容營銷神器!

在信息爆炸的時代&#xff0c;內容創作者們面臨著前所未有的挑戰與機遇。如何讓自己的內容在眾多平臺中脫穎而出&#xff0c;快速傳播并吸引大量觀眾&#xff0c;成為了每個創作者關注的焦點。近日&#xff0c;一款名為“迅狐視頻號矩陣系統”的神器震撼來襲&#xff0c;它以其…

UV膠,它是否有毒?如同那些隱藏在黑暗中的危險之物?

UV膠&#xff0c;它是否有毒&#xff1f;如同那些隱藏在黑暗中的危險之物&#xff1f; 關于uv膠的毒性問題&#xff0c;或許我們可以這樣深入探討。UV膠&#xff0c;如同一位戴著神秘面紗的訪客&#xff0c;在我們的生活中悄然出現&#xff0c;卻帶著諸多疑問。那么&#xff0…

二維碼生成需知:名片二維碼尺寸多少合適?電子名片二維碼制作方法?

隨著數字化時代的到來&#xff0c;二維碼在各個領域的應用越來越廣泛&#xff0c;名片作為商業交流的重要工具之一&#xff0c;也開始逐漸融入二維碼的元素。通過在名片上添加二維碼&#xff0c;我們可以輕松實現信息的快速傳遞和分享。然而&#xff0c;名片二維碼的尺寸選擇成…

Monorepo倉庫管理策略之 Lerna

這里寫目錄標題 前言&#xff1a;一、簡介二、新建項目使用安裝生成結構 三、復用現有項目執行命令查看包 四、配置package相互引用導入現有的包 五、發布包確定項目版本發布項目添加項目到到git發布包到NPM包發布出錯解決方案 五、實例代碼 前言&#xff1a; 將大型代碼倉庫分…

Python 與扣子 API的鏈接

當 Python 與各種 API 進行鏈接時&#xff0c;更是能碰撞出無數精彩的火花&#xff0c;為我們的開發工作帶來極大的便利和創新。今天&#xff0c;咱們就來聊聊 Python 與扣子 API 的鏈接那些事兒。 扣子 API 作為一種新興的技術接口&#xff0c;為我們提供了豐富的數據和功能。…

文心一言的流式接口數據進行處理 增加屬性

需求&#xff1a;需要對文心一言的流式接口數據進行處理 增加屬性 return ResponseEntity.ok().header("Access-Control-Allow-Origin", "*").contentType(org.springframework.http.MediaType.TEXT_EVENT_STREAM).cacheControl(org.springframework.http…

python調用串口收發數據

1、確認串口信息 2、安裝pyserial庫 打開終端或命令行&#xff0c;敲入這行命令&#xff1a;pip install pyserial 3、python編程 import serial def main(): #創建串口對象 ser serial.Serial(COM4, 9600, timeout1) if not ser.isOpen(): print("串…

飛睿智能6公里WiFi圖傳接收模塊,低延遲、抗干擾、高速穩定傳輸數據,無人機、農田遠距離WiFi模塊

在科技日新月異的今天&#xff0c;無線通信技術正以前所未有的速度發展&#xff0c;不僅改變了我們的生活方式&#xff0c;還為企業帶來了前所未有的商業機遇。今天&#xff0c;我要向大家介紹一款飛睿智能的產品——6公里WiFi圖傳接收模塊&#xff0c;它以其高性能、穩定的傳輸…

【常見的設計模式】單例模式

參考&#xff1a;【設計模式專題之單例模式】1.小明的購物車 【設計模式專題之單例模式】 1.小明的購物車 時間限制&#xff1a;1.000S 空間限制&#xff1a;256MB ? 題目描述 小明去了一家大型商場&#xff0c;拿到了一個購物車&#xff0c;并開始購物。請你設計一個購物車管…

【React】基礎數據回填--useForm與setFieldsValue詳解

相關屬性 1.form 2.setFieldsValue 代碼 import{Form }from"antd";const Publish =

體積大的快遞怎么寄便宜?如何寄件寄包裹更省錢?

大學畢業了&#xff0c;面對即將到來的工作生活&#xff0c;小李不得不把宿舍里的大包小包打包寄回家。可是&#xff0c;當他真正開始打包行李時&#xff0c;才發現這可不是一件簡單的事&#xff1a;衣服、被子、書籍、雜物……這些東西加起來體積不小&#xff0c;想要省錢寄快…

虛擬化技術 DeskV(或Desktop Virtualization)

DeskV&#xff08;或Desktop Virtualization&#xff09;&#xff0c;即桌面虛擬化技術&#xff0c;是一種將計算機的桌面系統&#xff08;包括操作系統、應用程序和用戶數據&#xff09;進行虛擬化&#xff0c;以實現桌面使用的安全性和靈活性的技術。以下是關于DeskV&#xf…

基于stm32單片機的智能手環的設計

摘 要 隨著科技的飛速發展和人們生活水平的提高&#xff0c;健康與科技日益融合&#xff0c;智能可穿戴設備已成為現代人生活中不可或缺的一部分。智能手環&#xff0c;作為一種便攜、實用且功能豐富的可穿戴設備&#xff0c;受到越來越多用戶的喜愛。它不僅能夠實時監測用戶的…

簡化嵌入式Linux開發:在Ubuntu上安裝和配置交叉編譯環境的高效方法

在嵌入式Linux開發中&#xff0c;我們通常需要在Ubuntu上安裝交叉編譯工具鏈&#xff0c;并配置相關文件。編譯過程中&#xff0c;如果遇到依賴庫問題&#xff0c;還需要手動查找并編譯開源源碼。這些步驟較為繁瑣&#xff0c;為了簡化操作&#xff0c;我們可以嘗試以下方案&am…