沐曦玩转 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))
相关推荐
Martina_03213 分钟前
AI生成3D场景导入 Unity/Unreal 后,NPC 总串层?用6步检查导航高度与跨层连接
人工智能·游戏·数学建模·3d·unity·自然语言处理·aigc
Dawson Zhu26 分钟前
Multi-Agent架构与适用边界:何时应该使用Multi-Agent架构
人工智能·语言模型·架构·aigc·agi
JEECG官方1 小时前
让 Claude Code 学会做视频:180 个开源 Skill 分类盘点,107 个冷门宝藏不到 50 星
aigc
Maynor9963 小时前
Kling 4.0(可灵 4.0)怎么用?上线时间、Flash 版、价格与提示词全攻略(2026 最新)
人工智能·aigc·视频生成·ai视频·可灵
Dawson Zhu3 小时前
《Agentic Design Patterns》第 5 章导读:工具使用(Tool Use)
人工智能·语言模型·架构·aigc·agi
“AI国潮设计-小江”13 小时前
《Python+SDXL实战:用ControlNet精准控制“英歌舞翻糖吐司”构图,附批量生成脚本》
开发语言·人工智能·python·prompt·aigc
Dawson Zhu17 小时前
《Agentic Design Patterns》第 1 章导读:提示词链(Prompt Chaining)
人工智能·语言模型·架构·aigc·agi
feiyu_gao18 小时前
Mindcraft:从个人实践到可复用模式集
架构·aigc·ai编程
abigalexy18 小时前
Claude Code从零搭建新项目全流程AI实现-宠物生命周期管理App
架构·aigc·ai编程
“AI国潮设计-小江”18 小时前
《Python+SDXL实战:用ControlNet批量生成“英歌舞麻将糕”IP,附自动化脚本与商用思路》
开发语言·人工智能·python·prompt·aigc