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
贡献和想法将不胜感激