XTuner 微调个人小助手认知任务

微调前的模型对话

我们可以通过网页端的 Demo 来看看微调前 internlm2-chat-1_8b 的对话效果。

首先,我们需要准备一个Streamlit程序的脚本。

Streamlit程序的完整代码是:tools/xtuner_streamlit_demo.py

然后,我们可以直接启动应用。

conda activate xtuner0121

streamlit run /root/InternLM/Tutorial/tools/xtuner_streamlit_demo.py

运行后,在访问前,我们还需要做的就是将端口映射到本地。

完成了所有的准备工作后,我们就可以正式的开始我们下一阶段的旅程:XTuner 启动~!

当我们准备好了所有内容,我们只需要将使用 xtuner train 命令令即可开始训练。

xtuner train 命令用于启动模型微调进程。该命令需要一个参数:CONFIG 用于指定微调配置文件。这里我们使用修改好的配置文件 internlm2_chat_1_8b_qlora_alpaca_e3_copy.py
训练过程中产生的所有文件,包括日志、配置文件、检查点文件、微调后的模型等,默认保存在 work_dirs 目录下,我们也可以通过添加 --work-dir 指定特定的文件保存位置。

cd /root/InternLM/XTuner
conda activate xtuner0121

xtuner train ./internlm2_chat_1_8b_qlora_alpaca_e3_copy.py

import torch

from transformers import AutoTokenizer, AutoModelForCausalLM

def load_model(model_path, cache_dir, token=None):

    # Load tokenizer and model from a specific cache directory

    tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True, use_auth_token=token, cache_dir=cache_dir)

    model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.float16, trust_remote_code=True, use_auth_token=token, cache_dir=cache_dir).cuda()

    model = model.eval()

    return tokenizer, model

def generate(user_input, tokenizer, model):

    gen_kwargs = {"max_length": 128, "top_p": 0.8, "temperature": 0.8, "do_sample": True, "repetition_penalty": 1.0}

   

    inputs = tokenizer([user_input], return_tensors="pt")

    for k, v in inputs.items():

        inputs[k] = v.cuda()

    output = model.generate(**inputs, **gen_kwargs)

    output = tokenizer.decode(output[0].tolist(), skip_special_tokens=True)

    return output

# Replace with the actual model path and cache directory

model_path = "internlm/internlm2-1_8b"

cache_dir = "/root/InternLM/XTuner/Shanghai_AI_Laboratory"  # Specify your cache directory

token = None  # Set your token if needed

# Load model and tokenizer

tokenizer, model = load_model(model_path, cache_dir, token

# Generate text

print(generate("书生·浦语大模型实战营第三期是", tokenizer, model))

# Clean up

del tokenizer, model

torch.cuda.empty_cache()

运行这个test文件

模型格式转换

模型转换的本质其实就是将原本使用 Pytorch 训练出来的模型权重文件转换为目前通用的 HuggingFace 格式文件,那么我们可以通过以下命令来实现一键转换。

我们可以使用 xtuner convert pth_to_hf 命令来进行模型格式转换。

xtuner convert pth_to_hf 命令用于进行模型格式转换。该命令需要三个参数:CONFIG 表示微调的配置文件, PATH_TO_PTH_MODEL 表示微调的模型权重文件路径,即要转换的模型权重, SAVE_PATH_TO_HF_MODEL 表示转换后的 HuggingFace 格式文件的保存路径。

模型合并

对于 LoRA 或者 QLoRA 微调出来的模型其实并不是一个完整的模型,而是一个额外的层(Adapter),训练完的这个层最终还是要与原模型进行合并才能被正常的使用。

对于全量微调的模型(full)其实是不需要进行整合这一步的,因为全量微调修改的是原模型的权重而非微调一个新的 Adapter ,因此是不需要进行模型整合的。

在 XTuner 中提供了一键合并的命令 xtuner convert merge,在使用前我们需要准备好三个路径,包括原模型的路径、训练好的 Adapter 层的(模型格式转换后的)路径以及最终保存的路径。

xtuner convert merge命令用于合并模型。该命令需要三个参数:LLM 表示原模型路径,ADAPTER 表示 Adapter 层的路径, SAVE_PATH 表示合并后的模型最终的保存路径。

微调后的模型对话

微调完成后,我们可以再次运行xtuner_streamlit_demo.py脚本来观察微调后的对话效果,不过在运行之前,我们需要将脚本中的模型路径修改为微调后的模型的路径。

# 直接修改脚本文件第18行
- model_name_or_path = "/root/InternLM/XTuner/Shanghai_AI_Laboratory/internlm2-chat-1_8b"
+ model_name_or_path = "/root/InternLM/XTuner/merged"

然后,我们可以直接启动应用。

conda activate xtuner0121

streamlit run /root/InternLM/Tutorial/tools/xtuner_streamlit_demo.py

运行后,确保端口映射正常,如果映射已断开则需要重新做一次端口映射。

ssh -CNg -L 8501:127.0.0.1:8501 root@ssh.intern-ai.org.cn -p 43551

 

  • 30
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值