环境准备

虽然直接在windows下部署是可行的,但由于模型中依赖的很多安装包无法直接在windows下运行安装,需要做额外的兼容处理,所以建议使用Windows系统自带的wsl工具安装linux子系统,以便在linux环境下运行。

1. 安装 WSL 2(推荐)

WSL 2 提供了一个完整的 Linux 环境,可以避免 Windows 上的兼容性问题。

步骤

  1. 打开 PowerShell(管理员权限)并运行:

    wsl --install
  2. 安装完成后,重启系统。

  3. 安装 Ubuntu 或其他 Linux 发行版:

    wsl --list --online  # 查看可用发行版
    wsl --list --verbose # 查看已安装运行的版本
    wsl --install -d Ubuntu  # 安装 Ubuntu
  4. 启动 WSL:

    wsl

2.安装python和pip

在 Ubuntu 系统中安装 pip(Python 的包管理工具)可以通过多种方式完成,具体取决于你使用的 Python 版本(Python 2 或 Python 3)。

安装python

从 Ubuntu 18.04 开始,Python 3 已经是默认的 Python 版本。如果你的系统中尚未安装 Python 3,可以通过以下命令安装:

sudo apt update
sudo apt install python3
安装pip
sudo apt install python3-pip

安装完后,检查是否安装成功

pip --version
创建虚拟环境

由于deepseek需要安装一些非Debian 官方包,需要创建一个虚拟环境。虚拟环境是独立的 Python 环境,不会影响系统 Python 环境。以下是创建和使用虚拟环境的步骤:

1、安装 python3-venv 模块(如果尚未安装)

sudo apt update
sudo apt install python3-venv

2、创建虚拟环境

python3 -m venv path/to/venv

path/to/venv 替换为你希望创建虚拟环境的路径,例如 ~/myenv

3、激活虚拟环境

source path/to/venv/bin/activate

4、在虚拟环境中安装包

pip install xyz  ## deepseek 模型依赖安装 pip install -r requirements.txt

5、退出虚拟环境

deactivate

部署deepseek模型

克隆代码库

git clone https://github.com/deepseek-ai/DeepSeek-MoE.git
cd DeepSeek-MoE

安装依赖

这里安装依赖时,在上面创建的虚拟环境中安装,直接安装,可能不成功。

pip install -r requirements.txt

pip安装时,如果太慢,可以尝试更改镜像源,临时使用镜像源:

pip install 包名 -i https://pypi.tuna.tsinghua.edu.cn/simple

 永久更改镜像源:

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

下载模型权重

1. 下载模型权重

从官网或 Huggingface 下载模型权重文件(如 .bin 或 .pt)。注意:由于 Hugging Face 的主域名在国内可能无法访问,可以使用镜像站点来解决访问问题。你可以通过以下方法设置镜像站点:

export HF_ENDPOINT="https://hf-mirror.com"

示例(Huggingface):

# 使用 huggingface-cli 下载
pip install huggingface_hub
huggingface-cli download --resume-download deepseek-ai/deepseek-moe-16b --local-dir ./model

注意事项:大模型(如 175B)可能需要数百 GB 的存储空间,下载前请确保磁盘空间。访问huggingface 可查看对应的模型及其文件大小。

2. 下载配置文件
  • 模型配置文件(如 config.json)通常与权重文件一起提供。

  • 确保配置文件与权重文件匹配。

3. 下载分词器

分词器文件(如 tokenizer.json)用于将文本转换为模型输入。通常与权重文件一起提供。

示例:

huggingface-cli download deepseek-ai/deepseek-moe-16b --include="tokenizer/*"

启动服务

Text Completion

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig

model_name = "./model"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto")
model.generation_config = GenerationConfig.from_pretrained(model_name)
model.generation_config.pad_token_id = model.generation_config.eos_token_id

text = "An attention function can be described as mapping a query and a set of key-value pairs to an output, where the query, keys, values, and output are all vectors. The output is"
inputs = tokenizer(text, return_tensors="pt")
outputs = model.generate(**inputs.to(model.device), max_new_tokens=100)

result = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(result)

遇到的问题

wsl迁移

C盘磁盘空间不足,wsl默认安装子系统到C盘,一般C盘大小有限,可能无法再承载上百G的模型权重文件,此时可能需要把wsl子系统迁移到有空间的其他盘,这里采用的是从C盘导出并在其他盘重新导入 WSL 发行版。

## 1、关闭正在运行的wsl实例
wsl --shutdown

## 2、导出发行版
## 语法: wsl --export <发行版名称> <目标路径>.vhdx --vhd
## 发行版名称 通过 wsl --list --verbose 查看对应的Name字段。
## 目标路径 是想要迁移到的盘路径
wsl --export Ubuntu E:\wsl\export.vhdx --vhd

## 3、删除C盘原有发行版
## wsl --unregister <发行版名称>
wsl --unregister Ubuntu

## 4、将发行版导入到新位置
## wsl --import <发行版名称> <目标路径> <vhdx文件路径> --vhd
wsl --import Ubuntu E:\wsl\ubuntu E:\wsl\export.vhdx --vhd

## 5、导入成功后重新启动子系统,没问题后,删除export.vhdx即可

Logo

欢迎加入DeepSeek 技术社区。在这里,你可以找到志同道合的朋友,共同探索AI技术的奥秘。

更多推荐