赞
踩
目录:
一. 前言
二. 系统环境
三. 新建虚拟环境
四. 安装依赖库
五. 下载模型文件
六. 运行大模型
本次介绍如何在Ubuntu子系统下,使用python代码运行Qwen2-7B开源大模型。
PowerShell执行wsl,进入子系统,子系统内执行:
- conda create -n qwen2-7b python=3.10
- conda activate qwen2-7b
conda安装请参考:3.Windows11 with WSL2 Ubuntu 子系统下安装minicoda-CSDN博客
先设置pip镜像源为清华源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
安装依赖
- pip install torch torchvision torchaudio
- pip install transformers
- pip install modelscope
- pip install accelerate
比如:/mnt/e/llm/qwen2-7b
子系统中的 /mnt/e/ 目录指向的是Windows11中的E盘
代码如下:
- # 模型下载
- from modelscope import snapshot_download
- model_dir = snapshot_download('qwen/Qwen2-7B-Instruct', cache_dir='/mnt/e/llm/qwen2-7b/models')
注意:修改cache_dir参数为你本地的真实路径
python download.py
输出:
注意:代码中有两处模型目录: /mnt/e/...,替换为你自己的
新建run.py,代码如下:
- from modelscope import AutoModelForCausalLM, AutoTokenizer
- device = "cuda" # the device to load the model onto
-
- model = AutoModelForCausalLM.from_pretrained(
- "/mnt/e/llm/qwen2-7b/models/Qwen/Qwen2-7B-Instruct",
- torch_dtype="auto",
- device_map="auto"
- )
- tokenizer = AutoTokenizer.from_pretrained("/mnt/e/llm/qwen2-7b/models/Qwen/Qwen2-7B-Instruct")
-
- prompt = "请介绍一下钱学森"
- messages = [
- {"role": "system", "content": "You are a helpful assistant."},
- {"role": "user", "content": prompt}
- ]
- text = tokenizer.apply_chat_template(
- messages,
- tokenize=False,
- add_generation_prompt=True
- )
- model_inputs = tokenizer([text], return_tensors="pt").to(device)
-
- generated_ids = model.generate(
- model_inputs.input_ids,
- max_new_tokens=512
- )
- generated_ids = [
- output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
- ]
-
- response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
- print(response)
运行:
python run.py
代码会先加载模型,然后推理,需要等待一段时间,如图输出,成功了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。