paulwong

          微調llama3大模型(1) - 使用Llama Factory微調llama3大模型

          對于象META的開源大模型,如llama3,由于都是用通用數據進行預訓練,對想使用其模型的公司來說,可能會不適用,因為這大模型對公司的數據不熟悉,因此引入微調(Fine-Tunning)。
          通過喂給大模型大量數據,1萬條起步,使得大模型也能對公司的數據熟悉,進而用于各種對話場景。

          1.克隆并安裝LLAMA FACTORY庫,install-llamafactory.sh

          BIN_PATH=$(cd `dirname $0`; pwd)
          cd $BIN_PATH/../
          pwd
          git clone --depth 
          1 https://github.com/hiyouga/LLaMA-Factory.git
          cd LLaMA-Factory
          pip install -e 
          ".[torch,metrics,bitsandbytes,modelscope]"

          2.設置環境變量

          export USE_MODELSCOPE_HUB=1 #使用modelscop模型庫,非huggingface的
          export CUDA_VISIBLE_DEVICES
          =0 #設置使用GPU
          export HF_ENDPOINT
          =https://hf-mirror.com #設置huggingface的替代地址
          export MODELSCOPE_CACHE
          =/root/autodl-tmp/models/modelscope #設置modelscope中的大模型保存路徑
          export LLAMAFACTORY_HOME=/root/autodl-tmp/LLaMA-Factory

          3.準備數據

          #在data/dataset_info.json中加入此數據

          "docker_command_NL": {
              
          "hf_hub_url""MattCoddity/dockerNLcommands"
            }
          ,
          在data目錄中加入訓練數據,MattCoddity/dockerNLcommands.json
          數據格式為:
          [
            {
              
          "input""Give me a list of containers that have the Ubuntu image as their ancestor.",
              
          "instruction""translate this sentence in docker command",
              
          "output""docker ps --filter 'ancestor=ubuntu'"
            }
          ,

          ]

          4.訓練大模型

          訓練的參數文件:llama3_lora_sft_docker_command.yaml
          ### model
          #md model id
          model_name_or_path: LLM-Research/Meta-Llama-
          3-8B-Instruct
          #huggingface model id
          #model_name_or_path: meta-llama/Meta-Llama-
          3-8B-Instruct

          ### method
          stage: sft
          do_train: true
          finetuning_type: lora
          lora_target: all

          ### dataset
          dataset: docker_command_NL
          template: llama3
          cutoff_len: 
          1024
          max_samples: 
          1000
          overwrite_cache: true
          preprocessing_num_workers: 
          16

          ### output
          output_dir: /root/autodl-tmp/my-test/saves/llama3-8b/lora/sft/docker-commnad-nlp/sft
          logging_steps: 
          10
          save_steps: 
          500
          plot_loss: true
          overwrite_output_dir: true

          ### train
          per_device_train_batch_size: 
          4
          gradient_accumulation_steps: 
          8
          learning_rate: 
          1.0e-4
          num_train_epochs: 
          3.0
          lr_scheduler_type: cosine
          warmup_ratio: 
          0.1
          bf16: true
          ddp_timeout: 
          180000000

          ### eval
          val_size: 
          0.1
          per_device_eval_batch_size: 
          1
          eval_strategy: steps
          eval_steps: 
          500
          訓練命令:lora-train-docker-command.sh
          BIN_PATH=$(cd `dirname $0`; pwd)
          cd $BIN_PATH/
          pwd
          cd $LLAMAFACTORY_HOME
          pwd
          llamafactory-cli train $BIN_PATH/conf/llama3_lora_sft_docker_command.yaml
          執行此命令即可開始訓練大模型。

          5.合并大模型

          合并用的參數文件,llama3_lora_export_docker_command.yaml
          ### model
          #md model id
          model_name_or_path: LLM-Research/Meta-Llama-
          3-8B-Instruct
          #huggingface model id
          #model_name_or_path: meta-llama/Meta-Llama-
          3-8B-Instruct

          adapter_name_or_path: /root/autodl-tmp/my-test/saves/llama3-8b/lora/docker-commnad-nlp/sft
          template: llama3
          export_dir: /root/autodl-tmp/my-test/saves/llama3-8b/lora/docker-commnad-nlp/export
          finetuning_type: lora
          export_size: 
          2
          export_device: gpu
          export_legacy_format: False
          合并命令,lora-export-docker-command.sh
          BIN_PATH=$(cd `dirname $0`; pwd)
          cd $BIN_PATH/
          pwd
          llamafactory-cli export conf/llama3_lora_export_docker_command.yaml

          posted on 2024-07-08 18:44 paulwong 閱讀(130) 評論(0)  編輯  收藏 所屬分類: AI-LLMAI-FINE-TUNNING

          主站蜘蛛池模板: 和田市| 都兰县| 鸡泽县| 花垣县| 阳原县| 宁武县| 富宁县| 乐都县| 南郑县| 武宣县| 罗田县| 五大连池市| 涿鹿县| 安新县| 黄平县| 富锦市| 克什克腾旗| 韶山市| 连平县| 汝城县| 南投市| 彰化市| 吴旗县| 中西区| 清丰县| 莆田市| 郑州市| 田东县| 铜梁县| 民勤县| 金秀| 湛江市| 海口市| 神木县| 怀集县| 六安市| 无为县| 贵德县| 诸暨市| 天长市| 汪清县|