当前位置:   article > 正文

安装和微调大模型(基于LLaMA-Factory)_大模型微调 python包

大模型微调 python包
  1. 打开终端(在Unix或macOS上)或命令提示符/Anaconda Prompt(在Windows上)。

  2. 创建一个名为lora的虚拟环境并指定Python版本为3.9。

https://github.com/echonoshy/cgft-llm/blob/master/llama-factory/README.md
GitHub - hiyouga/LLaMA-Factory: Unify Efficient Fine-Tuning of 100+ LLMs

conda create --n lora python=3.9
  1. 激活新创建的虚拟环境。

conda activate lora
  1. 克隆项目。

  1. git clone https://github.com/hiyouga/LLaMA-Factory.git
  2. cd LLaMA-Factory
  1. 安装Python依赖项。由于您已经有了依赖项的列表,您可以使用pip来安装它们。

  1. pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
  2. pip install transformers_stream_generator bitsandbytes tiktoken auto-gptq optimum autoawq -i https://pypi.tuna.tsinghua.edu.cn/simple
  3. pip install --upgrade tensorflow -i https://pypi.tuna.tsinghua.edu.cn/simple
  1. 运行代码。

CUDA_VISIBLE_DEVICES=0 USE_MODELSCOPE_HUB=1 python src/webui.py
  1. 当您完成工作后,您可以停用虚拟环境。

llamafactory-cli train cust/train_llama3_lora_sft.yaml

conda deactivate

请确保您已经有了conda命令行工具,并且已经添加到您的系统环境变量中。如果您还没有安装conda,您可以从AnacondaMiniconda官网下载并安装。

请注意,如果您在安装过程中遇到任何依赖性问题,您可能需要根据错误信息调整包的版本或安装顺序。

 微调命令

(构建 cust/train_llama3_lora_sft.yaml)

(命令行执行:llamafactory-cli train cust/train_llama3_lora_sft.yaml)

(打开ui:  llamafactory-cli webchat cust/train_llama3_lora_sft.yaml)

  1. cutoff_len: 1024
  2. dataset: fintech,identity
  3. dataset_dir: data
  4. do_train: true
  5. finetuning_type: lora
  6. flash_attn: auto
  7. fp16: true
  8. gradient_accumulation_steps: 8
  9. learning_rate: 0.0002
  10. logging_steps: 5
  11. lora_alpha: 16
  12. lora_dropout: 0
  13. lora_rank: 8
  14. lora_target: q_proj,v_proj
  15. lr_scheduler_type: cosine
  16. max_grad_norm: 1.0
  17. max_samples: 1000
  18. model_name_or_path: /root/autodl-tmp/models/Llama3-8B-Chinese-Chat
  19. num_train_epochs: 10.0
  20. optim: adamw_torch
  21. output_dir: saves/LLaMA3-8B-Chinese-Chat/lora/train_2024-05-25-20-27-47
  22. packing: false
  23. per_device_train_batch_size: 2
  24. plot_loss: true
  25. preprocessing_num_workers: 16
  26. report_to: none
  27. save_steps: 100
  28. stage: sft
  29. template: llama3
  30. use_unsloth: true
  31. warmup_steps: 0

合并

llamafactory-cli export cust/merge_llama3_lora_sft.yaml
  1. ### 上面文件内容Note: DO NOT use quantized model or quantization_bit when merging lora adapters
  2. ### model
  3. model_name_or_path: /media/ldx/陈启的机械硬盘/models/Llama3-8B-Chinese-Chat1/
  4. adapter_name_or_path: /home/ldx/LLaMA-Factory/saves/LLaMA3-8B-Chinese-Chat/lora/train_2024-07-01-20-27-47
  5. template: llama3
  6. finetuning_type: lora
  7. ### export
  8. export_dir: /media/ldx/陈启的机械硬盘/models/Llama3-8B-Chinese-Chat-cq/
  9. export_size: 4
  10. export_device: cuda
  11. export_legacy_format:

API对话

  1. # 指定多卡和端口
  2. CUDA_VISIBLE_DEVICES=0,1 API_PORT=8000
  3. llamafactory-cli api cust/train_llama3_lora_sft.yaml
CUDA_VISIBLE_DEVICES=0 API_PORT=8000 llamafactory-cli api --model_name_or_path megred-model-path --template llama3 --infer_backend vllm --vllm_enforce_eager
  1. from openai import OpenAI
  2. # autodl 中指令
  3. # CUDA_VISIBLE_DEVICES=0 nohup python -m vllm.entrypoints.openai.api_server --model /autodl-tmp/LLM-Research/Meta-Llama-3-8B-Instruct --served-model-name Meta-Llama-3-8B-Instruct --dtype=half > vllm_test.out &
  4. # Set OpenAI's API key and API base to use vLLM's API server.
  5. openai_api_key = "EMPTY"
  6. openai_api_base = "http://localhost:8000/v1"
  7. client = OpenAI(
  8. api_key=openai_api_key,
  9. base_url=openai_api_base,
  10. )
  11. chat_response = client.chat.completions.create(
  12. model="Meta-Llama-3-8B-Instruct",
  13. messages=[
  14. {"role": "system", "content": "You are a helpful assistant."},
  15. {"role": "user", "content": "你是谁?"},
  16. ]
  17. )
  18. print("Chat response:", chat_response.choices)

终端对话

llamafactory-cli chat cust/train_llama3_lora_sft.yaml

UI对话

llamafactory-cli webchat cust/train_llama3_lora_sft.yaml

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/834064
推荐阅读
相关标签
  

闽ICP备14008679号