此存儲庫包含Python軟件包loralib
的源代碼,以及如何將其與Pytorch模型(例如擁抱面孔的模型)集成在一起的幾個示例。我們目前僅支持Pytorch。有關洛拉的詳細說明,請參見我們的論文。
洛拉:大語言模型的低排名
Edward J. Hu*,Yelong Shen*,Phillip Wallis,Zeyuan Allen-Zhu,Yuanzhi Li,Shean Wang,Lu Wang,Weizhu Chen
論文:https://arxiv.org/abs/2106.09685
視頻解釋器:https://www.youtube.com/watch?v=dhrotoncyze
更新2/2023:現在,洛拉(Lora)由最新的參數效率微調(PEFT)庫支持,通過擁抱面孔。
洛拉通過學習秩定矩陣的一對,同時凍結原始權重,從而減少了可訓練的參數的數量。這大大減少了適合特定任務的大型語言模型的存儲要求,並在部署過程中啟用了有效的任務轉換,而無需引入推理潛伏期。洛拉還勝過其他幾種適應方法,包括適配器,前綴調整和微調。
我們獲得了使用Roberta(Liu等人,2019年)基礎和大型和Deberta(He等,2020)XXL 1.5B的膠水基准上的全面填充的結果可比性或優越。 。單擊下面的數字下載Roberta和Deberta Lora檢查點。
羅伯塔基地 微調 | 羅伯塔基地 洛拉 | Deberta XXL 微調 | Deberta XXL 洛拉 | ||
---|---|---|---|---|---|
可訓練的參數#。 | 125m | 0.8m | 1.5b | 4.7m | |
MNLI(M-ACC/MM-ACC) | 87.6 | 87.5 ±.3/86.9±.3 | 91.7/ 91.9 | 91.9 ±.1/ 91.9 ±.2 | |
SST2(ACC) | 94.8 | 95.1 ±.2 | 97.2 | 96.9±.2 | |
MRPC(ACC) | 90.2 | 89.7 ±.7 | 92.0 | 92.6 ±.6 | |
可樂(Matthew's Corr) | 63.6 | 63.4 ±1.2 | 72.0 | 72.4 ±1.1 | |
QNLI(ACC) | 92.8 | 93.3 ±.3 | 96.0 | 96.0 ±.1 | |
QQP(ACC) | 91.9 | 90.8±.1 | 92.7 | 92.9 ±.1 | |
RTE(ACC) | 78.7 | 86.6 ±.7 | 93.9 | 94.9 ±.4 | |
STSB(Pearson/Spearman Corr) | 91.2 | 91.5 ±.2/ 91.3 ±.2 | 92.9 /92.6 | 93.0 ±.2/ 92.9 ±.3 | |
平均的 | 86.40 | 87.24 | 91.06 | 91.32 |
注意:您仍然需要從擁抱臉部進行原始的預訓練檢查點才能使用LORA檢查點。
微調數字來自Liu等人。 (2019年)和他等。 (2020)。我們包括實驗結果的置信區間。請按照examples/NLU/
重現我們的結果。
在GPT-2上,Lora與完整的燃燒和其他有效的調整方法相比,例如適配器(Houlsby et al。,2019)和前綴調整(Li and Liang,2021)。我們評估了E2E NLG挑戰,DART和WebNLG:
方法 | 可訓練的參數# | e2e(bleu) | 飛鏢(bleu) | webnlg(bleu-u/s/a) | |
---|---|---|---|---|---|
GPT-2 m(微調) | 354.92m | 68.2 | 46.0 | 30.4 / 63.2 /47.6 | |
GPT-2 m(適配器) | 0.37m | 66.3 | 42.4 | 45.1/54.5/50.2 | |
GPT-2 m(前綴) | 0.35m | 69.7 | 45.7 | 44.1/63.1/54.4 | |
GPT-2 M(Lora) | 0.35m | 70.4 ±.1 | 47.1 ±.2 | 46.7 ±.4/ 62.1±.2/ 55.3 ±.2 | |
GPT-2 L(微調) | 774.03m | 68.5 | 46.5 | 41.7 / 64.6 /54.2 | |
GPT-2 L(適配器) | 88m | 69.1±.1 | 45.7±.1 | 49.8 ±.0/61.1±.0/56.0±.0 | |
GPT-2 L(前綴) | 1.77m | 70.3 | 46.5 | 47.0/64.2/56.4 | |
GPT-2 L(Lora) | 1.77m | 70.4 ±.1 | 47.5 ±.1 | 48.4±.3/ 64.0 ±.3/ 57.0 ±.1 |
除GPT-2較大的適配器外,非洛拉基線取自Li和Liang(2021)。我們包括實驗結果的置信區間。
下載GPT-2 LORA檢查點:
請按照examples/NLG/
中的說明來重現我們的結果。
(此存儲庫的初始版本已存檔在“快照-9-15-2021”的分支機構中)
此存儲庫中有幾個目錄:
loralib
的源代碼,需要安裝該軟件包以運行我們提供的示例;loralib
loralib
簡直就是pip install loralib
# Alternatively
# pip install git+https://github.com/microsoft/LoRA
loralib
中實現的對應物替換一些層來適應一些層。我們現在僅支持nn.Linear
, nn.Embedding
和nn.Conv2d
。我們還支持單個MergedLinear
代表多個層的情況,例如在註意qkv
投影的某些實現中(有關更多內容nn.Linear
請參見其他註釋)。 # ===== Before =====
# layer = nn.Linear(in_features, out_features)
# ===== After ======
import loralib as lora
# Add a pair of low-rank adaptation matrices with rank r=16
layer = lora . Linear ( in_features , out_features , r = 16 )
import loralib as lora
model = BigModel ()
# This sets requires_grad to False for all parameters without the string "lora_" in their names
lora . mark_only_lora_as_trainable ( model )
# Training loop
for batch in dataloader :
...
state_dict
。 # ===== Before =====
# torch.save(model.state_dict(), checkpoint_path)
# ===== After =====
torch . save ( lora . lora_state_dict ( model ), checkpoint_path )
load_state_dict
加載檢查點時,請確保設置strict=False
。 # Load the pretrained checkpoint first
model . load_state_dict ( torch . load ( 'ckpt_pretrained.pt' ), strict = False )
# Then load the LoRA checkpoint
model . load_state_dict ( torch . load ( 'ckpt_lora.pt' ), strict = False )
儘管我們專注於簡單而效果的設置,即僅適應變壓器中的q
和v
投影,但在我們的示例中,洛拉可以應用於任何預訓練的權重的子集。我們鼓勵您探索不同的配置,例如通過用nn.Embedding
和/或適應MLP層來調整嵌入層lora.Embedding
最佳配置在不同的模型體系結構和任務中很可能會有所不同。
一些變壓器實現使用單個nn.Linear
用於查詢,鍵和值的投影矩陣。如果一個人希望將更新的等級限制為單個矩陣,則必須將其分解為三個單獨的矩陣,或使用lora.MergedLinear
。如果選擇分解圖層,請確保相應地修改檢查點。
# ===== Before =====
# qkv_proj = nn.Linear(d_model, 3*d_model)
# ===== After =====
# Break it up (remember to modify the pretrained checkpoint accordingly)
q_proj = lora . Linear ( d_model , d_model , r = 8 )
k_proj = nn . Linear ( d_model , d_model )
v_proj = lora . Linear ( d_model , d_model , r = 8 )
# Alternatively, use lora.MergedLinear (recommended)
qkv_proj = lora . MergedLinear ( d_model , 3 * d_model , r = 8 , enable_lora = [ True , False , True ])
lora
嘗試。您可以通過將“全部”或“ lora_only”傳遞給bias=
mark_only_lora_as_trainable
時將一些偏差標記為可訓練的偏差。請記住,在保存檢查點時,將相應的bias=
參數傳遞給lora_state_dict
。 # ===== Before =====
# lora.mark_only_lora_as_trainable(model) # Not training any bias vectors
# ===== After =====
# Training all bias vectors associated with modules we apply LoRA to
lora . mark_only_lora_as_trainable ( model , bias = 'lora_only' )
# Alternatively, we can train *all* bias vectors in the model, including LayerNorm biases
lora . mark_only_lora_as_trainable ( model , bias = 'all' )
# When saving a checkpoint, use the same bias= ('all' or 'lora_only')
torch . save ( lora . lora_state_dict ( model , bias = 'all' ), checkpoint_path )
model.eval()
將觸發Lora參數與相應的預驗證參數的合併,從而消除了後續正向通行證的額外延遲。再次呼叫model.train()
將撤消合併。可以通過將merge_weights=False
to Lora層來禁用這一點。 如果您有任何疑問,請聯繫我們或發布問題。
對於與包裹loralib
有關的問題:
GPT-2示例:
羅伯塔/迪伯塔省的例子:
我們感謝按字母順序的命令Jianfeng Gao,Jade Huang,Jiayuan Huang,Lisa Xiang Li,Xiaodong Liu,Yabin Liu,Benjamin van Durme,Luis Vargas,Haoran Wei,Haoran Wei,Peter Welinder和Greg Yang和Greg Yang提供有價值的反饋。
@inproceedings {
hu2022lora,
title = { Lo{RA}: Low-Rank Adaptation of Large Language Models } ,
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 } ,
booktitle = { International Conference on Learning Representations } ,
year = { 2022 } ,
url = { https://openreview.net/forum?id=nZeVKeeFYf9 }
}
該項目歡迎貢獻和建議。大多數捐款要求您同意撰寫貢獻者許可協議(CLA),宣布您有權並實際上授予我們使用您的貢獻的權利。有關詳細信息,請訪問https://cla.opensource.microsoft.com。
當您提交拉動請求時,CLA機器人將自動確定您是否需要提供CLA並適當裝飾PR(例如狀態檢查,評論)。只需按照機器人提供的說明即可。您只需要使用我們的CLA在所有存儲庫中進行一次。
該項目採用了Microsoft開源的行為代碼。有關更多信息,請參見《行為守則常見問題守則》或與其他問題或評論聯繫[email protected]。