沐曦玩转 LMDeploy、XTuner 和 InternLM3

学习链接: https://aicarrier.feishu.cn/wiki/O84LwkiBriUU0NkDwurcSufhnVb

一 LMDeploy推理及验证

1.1 下载LMDeploy

bash 复制代码
# 安装addict软件包
pip install addict mmengine mmengine-lite fire accelerate==0.32.1 nvidia-ml-py

# 解决LMDeploy对tranformers版本要求的Iusse:
pip install transformers==4.47.1

# 下载lmdeploy,并进入目录
git clone https://github.com/InternLM/lmdeploy.git
cd lmdeploy

1.2 部署验证结果

bash 复制代码
import lmdeploy
from lmdeploy import PytorchEngineConfig

if __name__ == "__main__":
    pipe = lmdeploy.pipeline("/root/public-model/models/internlm/internlm3-8b-instruct",
                             backend_config = PytorchEngineConfig(tp=1,
                                                                  cache_max_entry_count=0.8, device_type="maca",
                                                                  block_size=16)
                            )
    
    question = ["Shanghai is", "Please introduce China", "How are you?"]
    response = pipe(question, request_output_len=256, do_preprocess=False)
    for idx, r in enumerate(response):
        print(f"Q: {question[idx]}")
        print(f"A: {r.text}")
        print()

运行结果

二 XTune微调及验证

2.1 安装必要软件包

1. 进入容器

复制代码
`conda activate base`

2. 安装git

复制代码
执行命令`apt -y install git`

3. 安装mpi4py

复制代码
`pip install mpi4py`

4. 验证xtuner是否正确安装

复制代码
执行xtuner list-cfg|grep internlm,出现上图所示

5. 下载Tutorial

复制代码
`git clone https://github.com/InternLM/Tutorial.git -b camp4`

6. 创建finetune文件

复制代码
`mkdir -p /root/finetune && cd /root/finetune`

2.2 修改提供的数据集

1. 创建文件用于存储微调数据

复制代码
`mkdir -p /root/finetune/data && cd /root/finetune/data`
`cp -r /root/Tutorial/data/assistant_Tuner.jsonl /root/finetune/data`

2. 创建修改脚本

plain 复制代码
# 创建 change_script.py 文件
touch /root/finetune/data/change_script.py

根据教程将代码复制进change_script.py文件

并按照教程将44行代码修改

3. 执行脚本

Plain 复制代码
# usage:python change_script.py {input_file.jsonl} {output_file.jsonl}
cd ~/finetune/data
python change_script.py ./assistant_Tuner.jsonl ./assistant_Tuner_change.jsonl

assistant_Tuner_change.jsonl 是修改后符合 XTuner 格式的训练数据

4. 查看数据

cat assistant_Tuner_change.jsonl | head -n 3

得到数据如下:

2.3 准备训练脚本

根据教程获取官方写好的配置文件

2.4 修改微调参数

根据教程对internlm2_5_1.8b_lora_alpaca_e3_copy.py修改

2.4 启动微调

Plain 复制代码
cd /root/finetune
xtuner train ./config/internlm2_5_1.8b_lora_alpaca_e3_copy.py --deepspeed deepspeed_zero2 --work-dir ./work_dirs/assistTuner

2.5 权重转换

根据教学文档执行代码
xtuner convert pth_to_hf internlm2_5_1.8b_lora_alpaca_e3_copy.py /root/finetune/work_dirs/assistTuner/iter_864.pth ./hf

2.6 模型合并

根据教学文档微调模型合并

2.7 验证结果

python 复制代码
from transformers import AutoTokenizer, AutoModelForCausalLM

# 使用GPU
model_name = "/root/finetune/work_dirs/assistTuner/merged"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name).to('cuda')

# 处理输入
prompt = "请介绍一下你自己"
inputs = tokenizer(prompt, return_tensors="pt").to('cuda')

# 生成配置
outputs = model.generate(
    input_ids=inputs.input_ids,
    max_new_tokens=100,
    do_sample=True,
    temperature=0.8,
    top_p=0.95,
    pad_token_id=tokenizer.eos_token_id
)

# 解码结果
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
相关推荐
win4r7 小时前
🚀企业级最强开源大模型Qwen3震撼发布!本地部署+全面客观测评!Qwen3-235B-A22B+Qwen3-32B+Qwen3-14B谁是王者?ollama
llm·aigc·openai
ImAlex8 小时前
深入解析模型上下文协议(MCP):AI与数据源无缝集成的标准化之道
aigc·mcp
Blossom.11810 小时前
可解释人工智能(XAI):让机器决策透明化
人工智能·驱动开发·深度学习·目标检测·机器学习·aigc·硬件架构
康斯坦丁师傅10 小时前
深夜突袭,阿里Qwen3登顶全球开源王座!暴击DeepSeek-R1
aigc·openai
渭雨轻尘_学习计算机ing12 小时前
手把手玩转MCP:从入门到实战,解锁AI的“万能插头”
aigc·mcp
拳打南山敬老院15 小时前
从零搭建MCP服务:基于Stdio的实践指南
前端·javascript·aigc
墨风如雪15 小时前
DeepSeek-R1T-Chimera:当R1的智慧,遇上V3的速度!开源AI新物种驾到!
aigc
CF14年老兵17 小时前
别被忽悠!从入门到年薪百万 AI 工程师的真实成长路径
python·aigc·openai