curlora
v4.0.0
穆罕默德·法維(Muhammad Fawi)
代碼doi:
研究預印本:
該回購包含庫洛拉研究論文的代碼,這是一種新型的大型語言模型(LLMS)的新方法,該方法在低級別適應性(LORA)的背景下利用CUR矩陣分解。我們的方法解決了LLM微調方面的兩個關鍵挑戰:減輕持續學習過程中的災難性遺忘並減少可訓練參數的數量。我們建議對CUR分解過程進行獨特的修改,以使更有效,更穩定的方法使LLM適應新任務,而不會損害任何現有知識。我們通過在多個數據集上進行的實驗證明,策展人在減輕災難性遺忘方面優於標準洛拉。它保持了跨任務的模型穩定性和性能,同時大大減少了可訓練的參數的數量。我們的結果表明,與洛拉相比,庫洛拉獲得了卓越的準確性和困惑得分,尤其是在數據有限的情況下。
CURLoRA.pdf :詳細介紹Curlora方法的研究論文。code/ :包含Curlora和實驗實現的目錄。code/curlora.py :包含Curlora類。code/utils.py :輔助功能。code/lora.py類。code/curlora_experiment.ipynb :Mistral 7b(MRPC上的微調,SST-2和Mentiment140)實驗。code/curlora_experiment-gpt.ipynb實驗gpt2-large(MRPC上的微調,SST-2和MENTIMENT140)。code/squad_gpt-curlora.ipynb和sfttrainer在小隊數據集上的Q&A進行微調GPT2-LARGE。 首先我們安裝要求
pip3 install -r code/requirements.txt所有Curlora輔助功能和類都在Code/Curlora.py和Code/Utils.py中定義。
加載模型並應用庫洛拉
from transformers import AutoTokenizer , AutoModelForCausalLM
from utils import *
model_name = "gpt2-large"
model = AutoModelForCausalLM . from_pretrained ( model_name )
model . to ( "cuda" ) # this will make all existing layers in CUDA
# turning off grad for all layers
for param in model . parameters ():
param . requires_grad = False
# replace original Q,K,V layers with CURLoRA (GPT2-Large specific)
# refer to utils.py for a more general way
for name , module in model . named_modules ():
if isinstance ( module , type ( model . transformer . h [ 0 ]. attn )):
# rank = 24, alpha = 1
module . c_attn = LinearWithCURLoRA ( module . c_attn , 24 , 1 )
# now look at how many CURLoRA parameters to be trained
total_params = sum ( p . numel () for p in model . parameters () if p . requires_grad )
print ( f"Total trainable parameters after: { total_params :, } " )
# making sure CURLoRA layers are on CUDA as well
model . to ( "cuda" )現在,您擁有將庫洛拉層應用於注意層(鑰匙,值和查詢)的模型,您可以將其用於微調或正常推理。
您可能需要知道如何調用該圖層,以便可以正確替換。例如,可以通過以下方式找到Mistral中的Q,K,V。
for name , module in model . named_children ():
if any ( l in name for l in [ "q_proj" , "v_proj" , "k_proj" ]):
setattr ( model , name , LinearWithCURLoRA ( module , rank , alpha ))請注意:
該項目是根據MIT許可證獲得許可的 - 有關詳細信息,請參見許可證文件。
如果您發現Curlora研究或編碼有幫助,請考慮引用它們。
@software { Fawi_CURLoRA_Leveraging_CUR_2024 ,
author = { Fawi, Muhammad } ,
title = { {CURLoRA: Leveraging CUR Matrix Decomposition for
Stable LLM Continual Fine-Tuning and Catastrophic
Forgetting Mitigation} } ,
month = jul,
year = 2024 ,
publisher = { Zenodo } ,
version = { v4.0.0 } ,
doi = { 10.5281/zenodo.12729738 } ,
url = { https://zenodo.org/doi/10.5281/zenodo.12729738 }
} Fawi, M. (2024). CURLoRA: Leveraging CUR Matrix Decomposition for Stable LLM Continual Fine-Tuning and Catastrophic Forgetting Mitigation (v4.0.0) [Computer software]. Zenodo. https://doi.org/10.5281/zenodo.12729738
@misc { fawi_2024_12730055 ,
author = { Fawi, Muhammad } ,
title = { {CURLoRA: Leveraging CUR Matrix Decomposition for
Stable LLM Continual Fine-Tuning and Catastrophic
Forgetting Mitigation} } ,
month = jul,
year = 2024 ,
publisher = { Zenodo } ,
doi = { 10.5281/zenodo.12730055 } ,
url = { https://doi.org/10.5281/zenodo.12730055 }
} Fawi, M. (2024). CURLoRA: Leveraging CUR Matrix Decomposition for Stable LLM Continual Fine-Tuning and Catastrophic Forgetting Mitigation. Zenodo. https://doi.org/10.5281/zenodo.12730055
貢獻和想法將不勝感激