华为昇腾Qwen2-VL-7B-Instruct模型训练微调推理

1 准备环境

检查设备驱动:

npu-smi info

在这里插入图片描述
可以看到设备的驱动版本是24.1.rc3。

2 下载模型

进入ModelScope魔搭社区下载通义千问2-VL-7B-Instruct模型
在这里插入图片描述
安装ModelScope包:

pip install modelscope

在这里插入图片描述
创建download.py文件:

touch download.py

或者编辑download.py文件:

vim download.py
# 模型下载
from modelscope import snapshot_download
# 可以根据需求修改下载模型的路径
model_dir = snapshot_download('Qwen/Qwen2-VL-7B-Instruct',cache_dir="/home/HwHiAiUser/")

运行download.py,将模型下载到指定目录/home/HwHiAiUser/。

python3 download.py

在这里插入图片描述

3 构建昇腾mindie环境

3.1 获取800I A2镜像

方法1:从昇腾镜像仓库的官方路径获取800I A2镜像。
在这里插入图片描述
方法2:从docker获取800I A2镜像。

  1. 安装docker
yun install docker
  1. 拉取镜像
docker pull swr.cn-east-317.qdrgznjszx.com/sxj731533730/mindie:1.0.T71-800I-A2-py311-ubuntu22.04-arm64

报错:
在这里插入图片描述
修改配置文件/ect/docker/daemon.json,修改配置源,并添加mindie的镜像源,同时将下载路径修改在/home/HwHiAiUser/docker中。

{
   
        "data-root": "/home/HwHiAiUser/docker",
        "insecure-registries": ["https://swr.cn-east-317.qdrgznjszx.com"],
        "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}

重新启动docker服务,后继续拉取镜像:

systemctl restart docker.service

在这里插入图片描述

3.2 检查并编辑镜像启动脚本

查看docker的本地镜像,获取其中的REPOSITORY和TAG字段。
在这里插入图片描述
编辑快速启动Docker容器并配置运行环境的Shell脚本docker_run.sh,将其中的REPOSITORY和TAG字段以冒号的形式分隔,并填写到docker_images字段。(以下启动的是8卡容器)

#!/bin/bash
docker_images=swr.cn-east-317.qdrgznjszx.com/sxj731533730/mindie:1.0.T71-800I-A2-py311-ubuntu22.04-arm64
model_dir=/home/HwHiAiUser   # 可自定义挂载目录
docker run -it -u root --ipc=host --net=host \
        --name mindie_1 \
        --device=/dev/davinci0 \
        --device=/dev/davinci1 \
        --device=/dev/davinci2 \
        --device=/dev/davinci3 \
        --device=/dev/davinci4 \
        --device=/dev/davinci5 \
        --device=/dev/davinci6 \
        --device=/dev/davinci7 \
        --device=/dev/davinci_manager \
        --device=/dev/devmm_svm \
        --device=/dev/hisi_hdc \
        -v /usr/bin/hccn_tool:/usr/bin/hccn_tool \
        -v /usr/local/Ascend/driver:/usr/local/Ascend/driver \
        -v /usr/local/Ascend/add-ons/:/usr/local/Ascend/add-ons \
        -v /usr/local/sbin/npu-smi:/usr/local/sbin/npu-smi \
        -v /usr/local/dcmi:/usr/local/dcmi \
        -v /usr/local/Ascend/driver/lib64/common:/usr/local/Ascend/driver/lib64/common \
        -v /usr/local/Ascend/driver/lib64/driver:/usr/local/Ascend/driver/lib64/driver \
        -v /etc/ascend_install.info:/etc/ascend_install.info \
        -v /etc/vnpu.cfg:/etc/vnpu.cfg \
        -v /usr/local/Ascend/driver/version.info:/usr/local/Ascend/driver/version.info \
        -v ${model_dir}:${model_dir} \
        -v /var/log/npu:/usr/slog \
        ${docker_images} /bin/bash

3.3 启动镜像并进入容器查看环境是否可用

bash docker_run.sh

在这里插入图片描述
在这里插入图片描述
查看所有容器:

docker ps -a

在这里插入图片描述
启动容器:

docker start 00c

进入容器:

docker exec -it 00c /bin/bash

退出容器:

exit

停止容器:

docker stop 00c

在这里插入图片描述

4 使用LLaMa-Factory训练

4.1 下载并配置LLaMa-Factory环境

git clone https://github.com/hiyouga/LLaMA-Factory.git
cd LLaMA-Factory
pip install -e '.[torch,metrics]'

在这里插入图片描述
在这里插入图片描述

4.2 编辑配置文件

  1. 从模板中拷贝配置文件:
cp examples/train_lora/llama3_lora_sft.yaml examples/train_lora/qwen2_VL_7B_Instruct_lora_sft.yaml

在这里插入图片描述
2. 修改配置文件:
LLaMA-Factory官网查找template。
在这里插入图片描述

### model
model_name_or_path: /home/HwHiAiUser/Qwen/Qwen2-VL-7B-Instruct
trust_remote_code: true
### method
stage: sft
do_train: true
finetuning_type: lora
lora_rank: 8
lora_target: all
### dataset
dataset: identity,alpaca_en_demo
template: qwen2_vl
cutoff_len: 2048
max_samples: 1000
ove
### Huawei Qwen2.5-VL-7B-Instruct Model Information The Huawei Qwen2.5-VL-7B-Instruct model is a large multimodal language model designed to understand and generate text based on both textual and visual inputs. This model integrates advanced capabilities from natural language processing (NLP) and computer vision, enabling it to interpret features across different modalities effectively[^1]. #### Key Characteristics of the Model - **Architecture**: The architecture leverages transformer-based models optimized for handling multi-modal data, combining deep learning techniques with efficient computational strategies. - **Training Data**: Trained on extensive datasets that include paired image-text examples, ensuring robust performance when dealing with diverse content types. - **Parameter Size**: With approximately 7 billion parameters, this version strikes a balance between complexity and efficiency while maintaining high accuracy. #### Usage Examples To utilize the Huawei Qwen2.5-VL-7B-Instruct model within Python applications or research projects, one can employ libraries such as Hugging Face's `transformers`. Below demonstrates how to load and interact with the model using PyTorch: ```python from transformers import AutoModelForVision2Seq, AutoProcessor processor = AutoProcessor.from_pretrained("huawei/Qwen2.5-VL-7B-Instruct") model = AutoModelForVision2Seq.from_pretrained("huawei/Qwen2.5-VL-7B-Instruct") image_path = "path_to_image.jpg" text_input = "Describe what you see." inputs = processor(image=image_path, text=text_input, return_tensors="pt") outputs = model.generate(**inputs) print(processor.decode(outputs[0], skip_special_tokens=True)) ``` This code snippet initializes the necessary components for interacting with images and texts through the specified pretrained weights provided by Huawei under their designated repository name.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值