Train your own large language model quickly: fine-tuning of lora instructions based on LLAMA-7B

Time:2023-11-15


Preface:

  • System: ubuntu 18.04
  • Graphics card: A100-80G (dabble, hehehe~)
    (This time the main record is how to quickly fine-tune commands for large models)

1. Selected works: lit-llama

  • Address: https://github.com/Lightning-AI/lit-llama
    Train your own large language model quickly: fine-tuning of lora instructions based on LLAMA-7B

2. Downloading the project

git clone https://github.com/Lightning-AI/lit-llama.git

3. Installation environment

  1. Switch to the project directory
cd ./lit-llama
  1. Installation of dependent libraries using pip
pip install -r requirements.txt

(Of course, there may be network issues here that prevent the installation of lightning)
It can be installed using the following method:

  • Download lightning project
    Train your own large language model quickly: fine-tuning of lora instructions based on LLAMA-7B

  • Unzip into the project directory and install using the following command

    python setup.py install
  • Check if lightning was installed successfully:

    pip list|grep lightning

    (Note here that lightning version is 2.1.0)
    Train your own large language model quickly: fine-tuning of lora instructions based on LLAMA-7B

4. Download the LLAMA-7B model

Here we are going to do instruction fine-tuning based on LLAMA-7B, so we have to download the model weights first and do a series of transformations.

  • Switch to the location of lit-llama and use the following command to download the weights:
python scripts/download.py --repo_id openlm-research/open_llama_7b --local_dir checkpoints/open-llama/7B

(The file is a bit large and needs to wait some time)

  • After downloading, you will get the following file:
    Train your own large language model quickly: fine-tuning of lora instructions based on LLAMA-7B

5. Doing model conversions

  • Still switching to the location of lit-llama, use the following command to convert it
    python scripts/convert_hf_checkpoint.py --checkpoint_dir checkpoints/open-llama/7B --model_size 7B
    
  • After the conversion, you will get a lit-llama.pth file of about 26G in lit-llama/checkpoints/lit-llama/7B, and a tokenizer.pth file in the previous directory (lit-llama/checkpoints/lit-llama). model file in the upper directory (lit-llama/checkpoints/lit-llama).
    Train your own large language model quickly: fine-tuning of lora instructions based on LLAMA-7B

6. Preliminary testing

  • At the command line, run with the following command:

    python generate.py --prompt "Today is a"

    Train your own large language model quickly: fine-tuning of lora instructions based on LLAMA-7B

  • Tested with Chinese and found it didn’t work well (started talking nonsense~)
    Train your own large language model quickly: fine-tuning of lora instructions based on LLAMA-7B

7. Why is instruction fine-tuning necessary?

  • Because the original pre-trained LLAMA model is a big language model (crap~) that predicts the next word based on the previous word, if you ask it a question, it won’t answer you correctly, and if you ask it a question, it might continue some sentence that is the same as yours, e.g.
  • Prompt for “What is the capital of China?” and its response is shown below:
    Train your own large language model quickly: fine-tuning of lora instructions based on LLAMA-7B

8. Commencement of instruction fine-tuning

8.1 Data preparation

  • The 52k instruction data from alpaca is used here for fine-tuning, using the following instructions:

    python scripts/prepare_alpaca.py
    
  • If the download doesn’t work, we’ll just open the scripts/prepare_alpaca.py file as follows:
    Train your own large language model quickly: fine-tuning of lora instructions based on LLAMA-7B

  • Open the link https://raw.githubusercontent.com/tloen/alpaca-lora/main/alpaca_data_cleaned_archive.json directly, then select the page in its entirety to copy it, then save it to a newly created file.
    Train your own large language model quickly: fine-tuning of lora instructions based on LLAMA-7B

  • get alpaca_data_cleaned_archive.json (see the name of the clean should be cleaned), each instruction contains “instruction”, “input”, “output” three keywords, this time not to specifically expand the explanation. As shown below:
    Train your own large language model quickly: fine-tuning of lora instructions based on LLAMA-7B

  • After running the command, data division will also be done. train-49759, val-2000 is shown below:
    Train your own large language model quickly: fine-tuning of lora instructions based on LLAMA-7B

  • Finally, you will get the following file under the path lit-llama/data/alpaca:
    Train your own large language model quickly: fine-tuning of lora instructions based on LLAMA-7B

8.2 Starting Model Training

  • Use the following command:

    python finetune/lora.py
    
  • The training process is shown below.
    Train your own large language model quickly: fine-tuning of lora instructions based on LLAMA-7B

  • The whole training process takes about 1.5 hours (16:51 ~ 18:22)

  • After training, a series of lora weight files are obtained at out/lora/alpaca as shown below:
    Train your own large language model quickly: fine-tuning of lora instructions based on LLAMA-7B

8.3 Model Testing

  • Use the following instructions to ask a question where the prompt is “what is the capital of China?”.

    python generate/lora.py --prompt "what is the capital of China?"
  • The results obtained are shown below
    Train your own large language model quickly: fine-tuning of lora instructions based on LLAMA-7B

  • As you can see, after fine-tuning the commands, the model is ready to answer our questions properly.

(ps: you can refer to the README of the project for other details, it’s written very clearly~)

The end.

Recommended Today

uniapp and applet set tabBar and show and hide tabBar

(1) Set the tabBar: uni.setTabberItem({}); wx.setTabberItem({}); indexnumberisWhich item of the tabBar, counting from the left, is indexed from 0.textstringnoButton text on tabiconPathstringnoImage PathselectedIconPathstringnoImage path when selectedpagePathstringnoPage absolute pathvisiblebooleannotab Whether to display uni.setTabBarItem({ index: 0, text: ‘text’, iconPath: ‘/path/to/iconPath’, selectedIconPath: ‘/path/to/selectedIconPath’, pagePath: ‘pages/home/home’ }) wx.setTabBarItem({ index: 0, text: ‘text’, iconPath: ‘/path/to/iconPath’, selectedIconPath: ‘/path/to/selectedIconPath’, pagePath: ‘pages/home/home’ }) […]