天天看點

chatglm2-6b在P40上做LORA微調

作者:京東雲開發者

背景:

目前,大模型的技術應用已經遍地開花。最快的應用方式無非是利用自有垂直領域的資料進行模型微調。chatglm2-6b在國内開源的大模型上,效果比較突出。本文章分享的内容是用chatglm2-6b模型在集團EA的P40機器上進行垂直領域的LORA微調。

一、chatglm2-6b介紹

github: https://github.com/THUDM/ChatGLM2-6B

chatglm2-6b相比于chatglm有幾方面的提升:

1. 性能提升: 相比初代模型,更新了 ChatGLM2-6B 的基座模型,同時在各項資料集評測上取得了不錯的成績;

2. 更長的上下文: 我們将基座模型的上下文長度(Context Length)由 ChatGLM-6B 的 2K 擴充到了 32K,并在對話階段使用 8K 的上下文長度訓練;

3. 更高效的推理: 基于 Multi-Query Attention 技術,ChatGLM2-6B 有更高效的推理速度和更低的顯存占用:在官方的模型實作下,推理速度相比初代提升了 42%;

4. 更開放的協定:ChatGLM2-6B 權重對學術研究完全開放,在填寫問卷進行登記後亦允許免費商業使用。

二、微調環境介紹

2.1 性能要求

推理這塊,chatglm2-6b在精度是fp16上隻需要14G的顯存,是以P40是可以cover的。

chatglm2-6b在P40上做LORA微調

EA上P40顯示卡的配置如下:

chatglm2-6b在P40上做LORA微調

2.2 鏡像環境

做微調之前,需要編譯環境進行配置,我這塊用的是docker鏡像的方式來加載鏡像環境,具體配置如下:

FROM base-clone-mamba-py37-cuda11.0-gpu

# mpich
RUN yum install mpich  

# create my own environment
RUN conda create -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/ --override --yes --name py39 python=3.9
# display my own environment in Launcher
RUN source activate py39 \
    && conda install --yes --quiet ipykernel \
    && python -m ipykernel install --name py39 --display-name "py39"

# install your own requirement package
RUN source activate py39 \
    && conda install -y -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/ \
    pytorch  torchvision torchaudio faiss-gpu \
    && pip install --no-cache-dir  --ignore-installed -i https://pypi.tuna.tsinghua.edu.cn/simple \
    protobuf \
    streamlit \
    transformers==4.29.1 \
    cpm_kernels \
    mdtex2html \
    gradio==3.28.3 \
	sentencepiece \
	accelerate \
	langchain \
    pymupdf \
	unstructured[local-inference] \
	layoutparser[layoutmodels,tesseract] \
	nltk~=3.8.1 \
	sentence-transformers \
	beautifulsoup4 \
	icetk \
	fastapi~=0.95.0 \
	uvicorn~=0.21.1 \
	pypinyin~=0.48.0 \
    click~=8.1.3 \
    tabulate \
    feedparser \
    azure-core \
    openai \
    pydantic~=1.10.7 \
    starlette~=0.26.1 \
    numpy~=1.23.5 \
    tqdm~=4.65.0 \
    requests~=2.28.2 \
    rouge_chinese \
    jieba \
    datasets \
    deepspeed \
	pdf2image \
	urllib3==1.26.15 \
    tenacity~=8.2.2 \
    autopep8 \
    paddleocr \
    mpi4py \
    tiktoken           

如果需要使用deepspeed方式來訓練, EA上缺少mpich資訊傳遞工具包,需要自己手動安裝。

2.3 模型下載下傳

huggingface位址: https://huggingface.co/THUDM/chatglm2-6b/tree/main

三、LORA微調

3.1 LORA介紹

paper: https://arxiv.org/pdf/2106.09685.pdf

LORA(Low-Rank Adaptation of Large Language Models)微調方法: 當機預訓練好的模型權重參數,在當機原模型參數的情況下,通過往模型中加入額外的網絡層,并隻訓練這些新增的網絡層參數。

chatglm2-6b在P40上做LORA微調

LoRA 的思想:

  • 在原始 PLM (Pre-trained Language Model) 旁邊增加一個旁路,做一個降維再升維的操作。
  • 訓練的時候固定 PLM 的參數,隻訓練降維矩陣A與升維矩B。而模型的輸入輸出次元不變,輸出時将BA與 PLM 的參數疊加。
  • 用随機高斯分布初始化A,用 0 矩陣初始化B,保證訓練的開始此旁路矩陣依然是 0 矩陣。

3.2 微調

huggingface提供的peft工具可以友善微調PLM模型,這裡也是采用的peft工具來建立LORA。

peft的github: https://gitcode.net/mirrors/huggingface/peft?utm_source=csdn_github_accelerator

加載模型和lora微調:

# load model
    tokenizer = AutoTokenizer.from_pretrained(args.model_dir, trust_remote_code=True)
    model = AutoModel.from_pretrained(args.model_dir, trust_remote_code=True)
    
    print("tokenizer:", tokenizer)
    
    # get LoRA model
    config = LoraConfig(
        r=args.lora_r,
        lora_alpha=32,
        lora_dropout=0.1,
        bias="none",)
    
    # 加載lora模型
    model = get_peft_model(model, config)
    # 半精度方式
    model = model.half().to(device)           

這裡需要注意的是,用huggingface加載本地模型,需要建立work檔案,EA上沒有權限在沒有在.cache建立,這裡需要自己先制定work路徑。

import os
os.environ['TRANSFORMERS_CACHE'] = os.path.dirname(os.path.abspath(__file__))+"/work/"
os.environ['HF_MODULES_CACHE'] = os.path.dirname(os.path.abspath(__file__))+"/work/"

           

如果需要用deepspeed方式訓練,選擇你需要的zero-stage方式:

conf = {"train_micro_batch_size_per_gpu": args.train_batch_size,
            "gradient_accumulation_steps": args.gradient_accumulation_steps,
            "optimizer": {
                "type": "Adam",
                "params": {
                    "lr": 1e-5,
                    "betas": [
                        0.9,
                        0.95
                    ],
                    "eps": 1e-8,
                    "weight_decay": 5e-4
                }
            },
            "fp16": {
                "enabled": True
            },
            "zero_optimization": {
                "stage": 1,
                "offload_optimizer": {
                    "device": "cpu",
                    "pin_memory": True
                },
                "allgather_partitions": True,
                "allgather_bucket_size": 2e8,
                "overlap_comm": True,
                "reduce_scatter": True,
                "reduce_bucket_size": 2e8,
                "contiguous_gradients": True
            },
            "steps_per_print": args.log_steps
            }           

其他都是資料處理處理方面的工作,需要關注的就是怎麼去建構prompt,個人認為在領域内做微調建構prompt非常重要,最終對模型的影響也比較大。

四、微調結果

目前模型還在finetune中,batch=1,epoch=3,已經疊代一輪。

chatglm2-6b在P40上做LORA微調

作者:京東零售 鄭少強

來源:京東雲開發者社群 轉載請注明來源