ToB企服应用市场:ToB评测及商务社交产业平台

标题: llama-factory利用教程 [打印本页]

作者: 耶耶耶耶耶    时间: 2025-2-13 12:02
标题: llama-factory利用教程
目标:借助llama-factory堆栈利用lora微调自己的大模型
llama-factory:https://github.com/hiyouga/LLaMA-Factory/tree/main
下载llama-factory堆栈

  1. git clone --depth 1 https://github.com/hiyouga/LLaMA-Factory.git
  2. cd LLaMA-Factory
复制代码
若后续不想继续追踪GitHub文件变化,可通过删除.git文件实现:
  1. rm -rf .git
复制代码
安装llama-factory情况

llama-factory官方推荐:

此处坑比较多,假如python、cuda、torch、transformers和vllm不匹配,会运行堕落
个人尝试有用情况搭配:
  1. python=3.10
  2. cuda=12.1
  3. torch=2.3.0-cu121
  4. transformers=4.43.4
  5. vllm=0.4.3
复制代码
  1. conda create -n tor230 python=3.10
复制代码
  1. pip install torch-2.3.0+cu121-cp310-cp310-linux_x86_64.whl
  2. pip install torchaudio-2.3.0+cu121-cp310-cp310-linux_x86_64.whl
  3. pip install torchvision-0.18.0+cu121-cp310-cp310-linux_x86_64.whl
复制代码
  1. pip install transformers==4.43.4
  2. pip install vllm==0.4.3
  3. pip install datasets
  4. pip install accelerate
  5. pip install peft
复制代码
  1. cd /data/yangjun/LLM/LLaMA-Factory
  2. pip install -e ".[torch,metrics]"
复制代码
登录huggingface

  1. pip install --upgrade huggingface_hub
复制代码
  1. huggingface-cli login
复制代码
在输入token时直接输入自己huggingface账户的token
“setting”-“Access Tokens”

假如token为空,则新建;不为空,则点击刷新
3. 假如不能利用署理,则直接login会堕落,那么需要利用国内huggingface镜像
设置情况变量,在~/.bashrc中写入命令:
  1. export HF_ENDPOINT=https://hf-mirror.com
复制代码
然后source ~/.bashrc
再重复步调2登录账户
利用自定义数据lora微调llama

了解我们需要自定义微调时需要修改的llama-factory文件夹设置:

  1. LLaMA-Factory/
  2. ├── data/
  3. │   ├── dataset_info.json
  4. │   └── [your_dataset].json
  5. ├── examples/
  6. │   └── train_lora/
  7. │   │   └── llama3_lora_sft.yaml
  8. │        ├── train_qlora/
  9. │   │   └── llama3_lora_sft.yaml
  10. │        ├── train_full/
  11. │   │   └── llama3_lora_sft.yaml
  12. │        └── inference/
  13. │       └── llama3_lora_sft.yaml
  14. └── 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
  1. {
  2.     "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.",
  3.     "input": "Claim: 新疆棉花生产已实现高度机械化,不需要强迫劳动。 Evidence: 新疆棉花生产早已经实现高度机械化,即使在忙碌的采摘季节,也不需要大量的“采棉工”。 【事实三】新疆棉花:生产早已高度机械化,不需要大量的“采棉工”据新疆农业部门发布的2020年数据显示,新疆棉花机械采摘率已达69.83%,其中北疆95%的棉花是通过机械采摘的。 近年来,一方面是机器生产减少劳力需求,另一方面随着内地农村劳动力收入不断提高,新疆采棉人数不断减少,这完全是劳动力市场行为所致,与“政府强迫本地劳动力”毫无联系。 针对新疆教培中心等同于“集中营”,中方对维族人实施“种族灭绝”、“强迫劳动”,对维族妇女强迫绝育,将维族儿童与父母分离的提问,杨代办指出,中方已多次阐明,新疆根本不存在所谓“种族灭绝”“强迫劳动”“大规模绝育”。 同时,从过去到现在,新疆根本不存在、也根本不需要强制性动员采棉。 ",
  4.     "output": "supports"
  5. },
复制代码
  1. "fact_checking":
  2. {
  3.   "file_name":"fact_checking.json"
  4. }
复制代码
其中fact_checking.json名称和1中构造数据集名称对应
3. 在./examples/train_lora文件夹下修改llama3_lora_sft_fact_checking.yaml:
  1. model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct
  2. finetuning_type: lora
  3. dataset: fact_checking
  4. output_dir: saves/llama3-8b-lora-sft-fact-checking/
复制代码
其中,dataset名称和dataset_info.json中key值对应
4. 微调
  1. CUDA_VISIBLE_DEVICES=0,1 llamafactory-cli train examples/train_lora/llama3_lora_sft_fact_checking.yaml
复制代码
推理

调用API进行推理

  1. model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct
  2. adapter_name_or_path: saves/llama3-8b-lora-sft
  3. template: llama3
  4. infer_backend: vllm
  5. vllm_enforce_eager: true
  6. finetuning_type: lora
复制代码
其中adapter_name_or_path后面改成3中output_dir对应名称
然后摆设api:
  1. CUDA_VISIBLE_DEVICES=0 API_PORT=8000 llamafactory-cli api examples/inference/llama3_lora_sft.yaml
复制代码
  1. from openai import OpenAI
  2. from tqdm import tqdm
  3. openai_api_key = "EMPTY"
  4. openai_api_base = "http://localhost:8000/v1"
  5. client = OpenAI(
  6.     api_key=openai_api_key,
  7.     base_url=openai_api_base
  8. )
  9. predictions = client.chat.completions.create(
  10.     model="meta-llama/Meta-Llama-3-8B-Instruct",
  11.     messages=[
  12.         {"role": "user", "content": input},
  13.     ],
  14.     temperature=0.95,
  15. )
复制代码
直接进行推理

调用API进行推理时摆设API的过程可能无法利用多卡,下面直接进行推理,这种方式可以接纳多卡的方式,且推理速度要比调用API的方式快。直接推理的过程和微调过程雷同。
  1. {
  2.     "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.",
  3.     "input": "Claim: 新疆棉花生产已实现高度机械化,不需要强迫劳动。 Evidence: 新疆棉花生产早已经实现高度机械化,即使在忙碌的采摘季节,也不需要大量的“采棉工”。 【事实三】新疆棉花:生产早已高度机械化,不需要大量的“采棉工”据新疆农业部门发布的2020年数据显示,新疆棉花机械采摘率已达69.83%,其中北疆95%的棉花是通过机械采摘的。 近年来,一方面是机器生产减少劳力需求,另一方面随着内地农村劳动力收入不断提高,新疆采棉人数不断减少,这完全是劳动力市场行为所致,与“政府强迫本地劳动力”毫无联系。 针对新疆教培中心等同于“集中营”,中方对维族人实施“种族灭绝”、“强迫劳动”,对维族妇女强迫绝育,将维族儿童与父母分离的提问,杨代办指出,中方已多次阐明,新疆根本不存在所谓“种族灭绝”“强迫劳动”“大规模绝育”。 同时,从过去到现在,新疆根本不存在、也根本不需要强制性动员采棉。 ",
  4.     "output": ""
  5. },
复制代码
  1. "inference_fact_checking":
  2. {
  3.   "file_name":"inference_fact_checking.json"
  4. }
复制代码
其中inference_fact_checking.json名称和1中构造数据集名称对应
  1. ### model
  2. model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct
  3. predict_with_generate: true
  4. adapter_name_or_path: saves/llama3-8b-lora-sft-fact-checking/
  5. ### method
  6. stage: sft
  7. do_predict: true
  8. finetuning_type: lora
  9. lora_target: all
  10. ### dataset
  11. eval_dataset: inference_question_with_context_answer
  12. template: llama3
  13. cutoff_len: 1024
  14. max_samples: 1000
  15. overwrite_cache: true
  16. preprocessing_num_workers: 16
  17. ### output
  18. output_dir: saves/llama3-8b-lora-sft-fact-checking/predict
  19. logging_steps: 10
  20. save_steps: 500
  21. plot_loss: true
  22. overwrite_output_dir: true
复制代码
其中,eval_dataset名称和dataset_info.json中key值对应(对应推理数据集)。adapter_name_or_path对应微调后保存的模型路径。output_dir表示推理结果保存路径
4. 推理
  1. CUDA_VISIBLE_DEVICES=0,1 llamafactory-cli train examples/train_lora/llama3_lora_sft_fact_checking.yaml
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4