沐曦玩转 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))
相关推荐
三道杠卷胡15 小时前
【AI News | 20250521】每日AI进展
人工智能·python·计算机视觉·语言模型·aigc
鸭鸭鸭进京赶烤19 小时前
第九届电子信息技术与计算机工程国际学术会议(EITCE 2025)
人工智能·计算机视觉·ai·云计算·aigc·mybatis·制造
视觉&物联智能19 小时前
【杂谈】-智领安全新篇:人工智能驱动现代物理安全防护体系
人工智能·深度学习·安全·aigc·agi
旺旺碎碎冰_1 天前
自回归图像编辑 EditAR: Unified Conditional Generation with Autoregressive Models
人工智能·计算机视觉·回归·aigc·图像生成
Mapmost2 天前
倾斜摄影已过时?3DGS能否重塑三维重建效率天花板
3d·信息可视化·aigc·3dgs
Him__2 天前
AI能源危机:人工智能发展与环境可持续性的矛盾与解决之道
人工智能·ai·chatgpt·aigc·llama
Him__2 天前
OpenAI Codex 加入Agent编程工具新阵营
人工智能·ai·aigc
Seeklike3 天前
本地部署Firecrawl+Dify调用踩坑记录
人工智能·aigc·dify·firecrawl
web150854159353 天前
AIGC与数字金融:人工智能金融创新的新纪元
人工智能·金融·aigc
摆烂仙君4 天前
南京邮电大学金工实习答案
人工智能·深度学习·aigc