目标:借助llama-factory堆栈利用lora微调自己的大模型
llama-factory:https://github.com/hiyouga/LLaMA-Factory/tree/main
下载llama-factory堆栈
- git clone --depth 1 https://github.com/hiyouga/LLaMA-Factory.git
- cd LLaMA-Factory
复制代码 若后续不想继续追踪GitHub文件变化,可通过删除.git文件实现:
安装llama-factory情况
llama-factory官方推荐:
此处坑比较多,假如python、cuda、torch、transformers和vllm不匹配,会运行堕落
个人尝试有用情况搭配:
- python=3.10
- cuda=12.1
- torch=2.3.0-cu121
- transformers=4.43.4
- vllm=0.4.3
复制代码- conda create -n tor230 python=3.10
复制代码
- 下载torch相关离线包,然后本地安装 torch离线包下载
- pip install torch-2.3.0+cu121-cp310-cp310-linux_x86_64.whl
- pip install torchaudio-2.3.0+cu121-cp310-cp310-linux_x86_64.whl
- pip install torchvision-0.18.0+cu121-cp310-cp310-linux_x86_64.whl
复制代码- pip install transformers==4.43.4
- pip install vllm==0.4.3
- pip install datasets
- pip install accelerate
- pip install peft
复制代码- cd /data/yangjun/LLM/LLaMA-Factory
- pip install -e ".[torch,metrics]"
复制代码 登录huggingface
- pip install --upgrade huggingface_hub
复制代码
- 假如能利用署理,直接登录huggingface账号:
在输入token时直接输入自己huggingface账户的token
“setting”-“Access Tokens”
假如token为空,则新建;不为空,则点击刷新
3. 假如不能利用署理,则直接login会堕落,那么需要利用国内huggingface镜像
设置情况变量,在~/.bashrc中写入命令:
- export HF_ENDPOINT=https://hf-mirror.com
复制代码 然后source ~/.bashrc
再重复步调2登录账户
利用自定义数据lora微调llama
了解我们需要自定义微调时需要修改的llama-factory文件夹设置:
- LLaMA-Factory/
- │
- ├── data/
- │ ├── dataset_info.json
- │ └── [your_dataset].json
- │
- ├── examples/
- │ └── train_lora/
- │ │ └── llama3_lora_sft.yaml
- │ ├── train_qlora/
- │ │ └── llama3_lora_sft.yaml
- │ ├── train_full/
- │ │ └── llama3_lora_sft.yaml
- │ └── inference/
- │ └── llama3_lora_sft.yaml
- └── saves/
复制代码 其中data文件夹下面的 [your_dataset].json 是自定义微调任务中练习集,dataset_info.json用于定义数据集名称
examples文件夹下面的train_lora、train_qlora和train_full表示差别的微调方式,相应文件夹下面存在微调任务的设置文件;inference表示利用练习好的模型来进行推理
saves文件夹需要自己新建,用于保存微调后的模型
详细先容微调大模型的利用方法
微调
以究竟核查(英文名:fact-checking,根据claim和evidence判定claim的真实性,其标签包括:supports(证据支持声明),refutes(证据反驳声明)和not enough information(证据对于判定声明真实性信息不敷))任务为例,利用lora微调LLaMA-3-8B
- 构造练习数据,保存在 ./data/fact_checking.json
数据格式:
- {
- "instruction": "Analyze whether the evidence supports the claim, refutes the statement, or is not enough information to verify the claim's truthfulness. Provide your final conclusion.",
- "input": "Claim: 新疆棉花生产已实现高度机械化,不需要强迫劳动。 Evidence: 新疆棉花生产早已经实现高度机械化,即使在忙碌的采摘季节,也不需要大量的“采棉工”。 【事实三】新疆棉花:生产早已高度机械化,不需要大量的“采棉工”据新疆农业部门发布的2020年数据显示,新疆棉花机械采摘率已达69.83%,其中北疆95%的棉花是通过机械采摘的。 近年来,一方面是机器生产减少劳力需求,另一方面随着内地农村劳动力收入不断提高,新疆采棉人数不断减少,这完全是劳动力市场行为所致,与“政府强迫本地劳动力”毫无联系。 针对新疆教培中心等同于“集中营”,中方对维族人实施“种族灭绝”、“强迫劳动”,对维族妇女强迫绝育,将维族儿童与父母分离的提问,杨代办指出,中方已多次阐明,新疆根本不存在所谓“种族灭绝”“强迫劳动”“大规模绝育”。 同时,从过去到现在,新疆根本不存在、也根本不需要强制性动员采棉。 ",
- "output": "supports"
- },
复制代码
- 在dataset_info.json文件中新增一项
- "fact_checking":
- {
- "file_name":"fact_checking.json"
- }
复制代码 其中fact_checking.json名称和1中构造数据集名称对应
3. 在./examples/train_lora文件夹下修改llama3_lora_sft_fact_checking.yaml:
- model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct
- finetuning_type: lora
- dataset: fact_checking
- output_dir: saves/llama3-8b-lora-sft-fact-checking/
复制代码 其中,dataset名称和dataset_info.json中key值对应
4. 微调
- CUDA_VISIBLE_DEVICES=0,1 llamafactory-cli train examples/train_lora/llama3_lora_sft_fact_checking.yaml
复制代码 推理
调用API进行推理
- 在./examples/inference文件夹下修改llama3_lora_sft.yaml构造推理api:
- model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct
- adapter_name_or_path: saves/llama3-8b-lora-sft
- template: llama3
- infer_backend: vllm
- vllm_enforce_eager: true
- finetuning_type: lora
复制代码 其中adapter_name_or_path后面改成3中output_dir对应名称
然后摆设api:
- CUDA_VISIBLE_DEVICES=0 API_PORT=8000 llamafactory-cli api examples/inference/llama3_lora_sft.yaml
复制代码- from openai import OpenAI
- from tqdm import tqdm
- openai_api_key = "EMPTY"
- openai_api_base = "http://localhost:8000/v1"
- client = OpenAI(
- api_key=openai_api_key,
- base_url=openai_api_base
- )
- predictions = client.chat.completions.create(
- model="meta-llama/Meta-Llama-3-8B-Instruct",
- messages=[
- {"role": "user", "content": input},
- ],
- temperature=0.95,
- )
复制代码 直接进行推理
调用API进行推理时摆设API的过程可能无法利用多卡,下面直接进行推理,这种方式可以接纳多卡的方式,且推理速度要比调用API的方式快。直接推理的过程和微调过程雷同。
- 构造推理数据,构造方式和构造微调数据集雷同。构造推理数据,保存在 ./data/inference_fact_checking.json
数据格式:
- {
- "instruction": "Analyze whether the evidence supports the claim, refutes the statement, or is not enough information to verify the claim's truthfulness. Provide your final conclusion.",
- "input": "Claim: 新疆棉花生产已实现高度机械化,不需要强迫劳动。 Evidence: 新疆棉花生产早已经实现高度机械化,即使在忙碌的采摘季节,也不需要大量的“采棉工”。 【事实三】新疆棉花:生产早已高度机械化,不需要大量的“采棉工”据新疆农业部门发布的2020年数据显示,新疆棉花机械采摘率已达69.83%,其中北疆95%的棉花是通过机械采摘的。 近年来,一方面是机器生产减少劳力需求,另一方面随着内地农村劳动力收入不断提高,新疆采棉人数不断减少,这完全是劳动力市场行为所致,与“政府强迫本地劳动力”毫无联系。 针对新疆教培中心等同于“集中营”,中方对维族人实施“种族灭绝”、“强迫劳动”,对维族妇女强迫绝育,将维族儿童与父母分离的提问,杨代办指出,中方已多次阐明,新疆根本不存在所谓“种族灭绝”“强迫劳动”“大规模绝育”。 同时,从过去到现在,新疆根本不存在、也根本不需要强制性动员采棉。 ",
- "output": ""
- },
复制代码
- 在dataset_info.json文件中新增一项
- "inference_fact_checking":
- {
- "file_name":"inference_fact_checking.json"
- }
复制代码 其中inference_fact_checking.json名称和1中构造数据集名称对应
- 在./examples/train_lora文件夹下新增llama3_lora_sft_inference_fact_checking.yaml:
- ### model
- model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct
- predict_with_generate: true
- adapter_name_or_path: saves/llama3-8b-lora-sft-fact-checking/
- ### method
- stage: sft
- do_predict: true
- finetuning_type: lora
- lora_target: all
- ### dataset
- eval_dataset: inference_question_with_context_answer
- template: llama3
- cutoff_len: 1024
- max_samples: 1000
- overwrite_cache: true
- preprocessing_num_workers: 16
- ### output
- output_dir: saves/llama3-8b-lora-sft-fact-checking/predict
- logging_steps: 10
- save_steps: 500
- plot_loss: true
- overwrite_output_dir: true
复制代码 其中,eval_dataset名称和dataset_info.json中key值对应(对应推理数据集)。adapter_name_or_path对应微调后保存的模型路径。output_dir表示推理结果保存路径
4. 推理
- CUDA_VISIBLE_DEVICES=0,1 llamafactory-cli train examples/train_lora/llama3_lora_sft_fact_checking.yaml
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |