llama微调数据:使用QLoRa微调Llama,2
上篇文章我们介绍了Llama 2的量化和部署,本篇文章将介绍使用PEFT库和QLoRa方法对Llama 27b预训练模型进行微调。我们将使用自定义数据集来构建情感分析模型。只有可以对数据进行微调我们才可以将这种大模型进行符合我们数据集的定制化。
一些前置的知识如果熟悉Google Colab、Weights & Biases (W&B)、HF库,可以跳过这一节。
虽然Google Colab(托管的Jupyter笔记本环境)不是真正的先决条件,但我们建议使用它来访问GPU并进行快速实验。如果是付费的用户,则可以使用高级GPU访问,比如A100这样的GPU。
W&B帐户的作用是记录进度和训练指标,这个如果不需要也可以用tensorboard替代,但是我们是演示Google Colab环境所以直接用它。
然后就是需要一个HF帐户。然后转到settings,创建至少具有读权限的API令牌。因为在训练脚本时将使用它下载预训练的Llama 2模型和数据集。
最后就是请求访问Llama 2模型。等待Meta AI和HF的邮件。这可能要1-2天。
准备数据集指令微调是一种常用技术,用于为特定的下游用例微调基本LLM。训练示例如下:
Below is an instruction that describes a sentiment analysis task...### Instruction:Analyze the following comment and classify the tone as...### Input:I love reading your articles...### Response:friendly & constructive
我们建议使用json,因为这样比较灵活。比如为每个示例创建一个JSON对象,其中只有一个文本字段。像这样:
{ "text": "Below is an instruction ... ### Instruction: Analyze the... ### Input: I love... ### Response: friendly" },{ "text": "Below is an instruction ... ### Instruction: ..." }
有很多很多方法可以提取原始数据、处理和创建训练数据集作为json文件。下面是一个简单的脚本:
with open('train.jsonl', 'a') as outfile:for example in raw_data:text = '<process_example>'# now append entry to the jsonl file.outfile.write('{"text": "' text '"}')outfile.write('\n')
如HF的Datasets库也是一个选择,但是我个人觉得他不好用。
在我们开始训练之前,我们要将文件作为数据集存储库推送到HF。可以直接使用huggingface-cli上传数据集。
训练Parameter-Efficient Fine-Tuning(PEFT)可以用于在不触及LLM的所有参数的情况下对LLM进行有效的微调。PEFT支持QLoRa方法,通过4位量化对LLM参数的一小部分进行微调。
Transformer Reinforcement Learning (TRL)是一个使用强化学习来训练语言模型的库。TRL也提供的监督微调(SFT)训练器API可以让我们快速的微调模型。
!pip install -q huggingface_hub!pip install -q -U trl transformers accelerate peft!pip install -q -U datasets bitsandbytes einops wandb# Uncomment to install new features that support latest models like Llama 2# !pip install git https://github.com/huggingface/peft.git# !pip install git https://github.com/huggingface/transformers.git# When prompted, paste the HF access token you created earlier.from huggingface_hub import notebook_loginnotebook_login()from datasets import load_datasetimport torchfrom transformers import AutoModelForCausalLM, BitsAndBytesConfig, AutoTokenizer, TrainingArgumentsfrom peft import LoraConfigfrom trl import SFTTrainerdataset_name = "<your_hf_dataset>"dataset = load_dataset(dataset_name, split="train")base_model_name = "meta-llama/Llama-2-7b-hf"bnb_config = BitsAndBytesConfig(load_in_4bit=True,bnb_4bit_quant_type="nf4",bnb_4bit_compute_dtype=torch.float16,)device_map = {"": 0}base_model = AutoModelForCausalLM.from_pretrained(base_model_name,quantization_config=bnb_config,device_map=device_map,trust_remote_code=True,use_auth_token=True)base_model.config.use_cache = False# More info: https://github.com/huggingface/transformers/pull/24906base_model.config.pretraining_tp = 1 peft_config = LoraConfig(lora_alpha=16,lora_dropout=0.1,r=64,bias="none",task_type="CAUSAL_LM",)tokenizer = AutoTokenizer.from_pretrained(base_model_name, trust_remote_code=True)tokenizer.pad_token = tokenizer.eos_tokenoutput_dir = "./results"training_args = TrainingArguments(output_dir=output_dir,per_device_train_batch_size=4,gradient_accumulation_steps=4,learning_rate=2e-4,logging_steps=10,max_steps=500)max_seq_length = 512trainer = SFTTrainer(model=base_model,train_dataset=dataset,peft_config=peft_config,dataset_text_field="text",max_seq_length=max_seq_length,tokenizer=tokenizer,args=training_args,)trainer.train()import osoutput_dir = os.path.join(output_dir, "final_checkpoint")trainer.model.save_pretrained(output_dir)
上面的脚本就是一个微调的简单代码,这里可以添加命令行参数解析器模块,如HfArgumentParser,这样就不必硬编码这些值
测试下面时一个简单的加载模型并进行完整性测试的快速方法。
from peft import AutoPeftModelForCausalLMmodel = AutoPeftModelForCausalLM.from_pretrained(output_dir, device_map=device_map, torch_dtype=torch.bfloat16)text = "..."inputs = tokenizer(text, return_tensors="pt").to(device)outputs = model.generate(input_ids=inputs["input_ids"].to("cuda"), attention_mask=inputs["attention_mask"], max_new_tokens=50, pad_token_id=tokenizer.eos_token_id)print(tokenizer.decode(outputs[0], skip_special_tokens=True))
这样就能够查看我们的结果了。
本文作者:UD
- 12-05二年级上语文课本生字书写 小学二年级语文汉字的正确书写顺序表
- 04-19我爱拼模型更新版游戏攻略:游戏推荐:我爱拼模型
- 04-20弃恶从善思想,反省从善,尤人成恶
- 04-08搞网络项目月入8000:有月收入上千的网络项目吗
- 02-07王牌对王牌5最新一期完整:200515王牌对王牌5新鲜剧照惊喜掉落
- 11-12冻结的时间冻结的画面是什么歌?16张被冻结而永远成为永恒的一幕
- 10-11大众七座家用车型有哪些好?大众家用大7座SUV配有四驱
- 04-19老师教学期末工作总结:区老师的学习课堂05期末总结三部曲
- 01-05家有儿女穿帮镜头牛仔裤:家有儿女中的穿帮镜头
- 11-11海底十万千米有多深:海底有多大多深多富有
- 02-09宾卑聚翻译:经典文言文赏析宾卑聚自杀
- 01-02推荐一款超级好用的软件 分享几款简单好用的软件给你
- 11-14许冠杰广州演唱会官方售票处:许冠杰个唱门票贵卖880元
- 02-26扬州湿地公园地址名称 扬州再添一处省级湿地公园
- 03-20剑网3新赛季上线时间,新礼贺岁剑网3重制版春节庆典今日开启
- 04-15宣城市2022年二手房:宣城房产中介,0608期
热门
推荐
- 1工程类公司的经营范围187
- 2简单的体育黑板报的设计图435
- 3以食品安全为话题作文166
- 4农村创业项目排行榜2017年最新的359
- 5除夕年夜饭养生汤有什么推荐461
- 6高中生档案个人鉴定总结292
- 7常见西洋参的食用方法推荐200
- 8考公务员难还是考研难哪个对未来发展更好460