一、linux 系统部署
准备工作
硬件要求:服务器需具备充足计算资源。推荐使用 nvidia gpu,如 a100、v100 等,能加快模型推理速度。内存至少 32gb,存储建议采用高速固态硬盘(ssd),保障数据读写高效。
软件环境:安装 linux 操作系统,如 ubuntu 20.04。同时,安装 python 3.8 及以上版本,以及相关依赖库,如 pytorch、transformers 等。以 cuda 11.7 为例,安装 pytorch 的命令如下:
pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cu117
安装 transformers 库:
pip install transformers
2.下载 deepseek 模型
访问 deepseek 官方模型下载地址,依据需求选择合适的模型版本。目前 deepseek 有不同参数规模的模型可选,如 deepseek-7b、deepseek-13b 等。
使用wget命令下载模型文件,示例如下:
wget https://download.deepseek.com/deepseek-7b.tar.gz
下载完成后,解压模型文件:
tar -zxvf deepseek-7b.tar.gz
3.部署步骤
创建项目目录:在本地创建新的项目目录,用于存放部署相关文件和脚本。
mkdir deepseek_deployment cd deepseek_deployment
编写推理脚本:使用 python 编写推理脚本,如inference.py。在脚本中导入必要库,加载 deepseek 模型和分词器,实现推理功能。示例代码如下:
import torch from transformers import autotokenizer, automodelforcausallm # 加载分词器和模型 tokenizer = autotokenizer.from_pretrained("path/to/deepseek-7b") model = automodelforcausallm.from_pretrained("path/to/deepseek-7b", torch_dtype=torch.float16).cuda() # 定义推理函数 def generate_text(prompt, max_length=100): input_ids = tokenizer.encode(prompt, return_tensors='pt').cuda() output = model.generate(input_ids, max_length=max_length, num_beams=5, early_stopping=true) return tokenizer.decode(output[0], skip_special_tokens=true) # 示例使用 prompt = "请介绍一下人工智能的发展趋势" generated_text = generate_text(prompt) print(generated_text) 请将path/to/deepseek-7b替换为实际的模型路径。 启动服务:若需将模型部署为服务,可使用 fastapi 等框架。首先安装 fastapi 和 uvicorn: pip install fastapi uvicorn 然后编写服务脚本,如app.py: from fastapi import fastapi from pydantic import basemodel import torch from transformers import autotokenizer, automodelforcausallm app = fastapi() # 加载分词器和模型 tokenizer = autotokenizer.from_pretrained("path/to/deepseek-7b") model = automodelforcausallm.from_pretrained("path/to/deepseek-7b", torch_dtype=torch.float16).cuda() class promptrequest(basemodel): prompt: str max_length: int = 100 @app.post("/generate") def generate_text(request: promptrequest): input_ids = tokenizer.encode(request.prompt, return_tensors='pt').cuda() output = model.generate(input_ids, max_length=request.max_length, num_beams=5, early_stopping=true) return {"generated_text": tokenizer.decode(output[0], skip_special_tokens=true)}
同样,将path/to/deepseek-7b替换为实际路径。
启动服务:
uvicorn app.py:app --host 0.0.0.0 --port 8000
二、windows 系统部署
1.准备工作
硬件要求:与 linux 系统类似,推荐配备 nvidia gpu,如 rtx 30 系列及以上,以获得较好的推理性能。内存建议 32gb 及以上,存储使用高速固态硬盘。
软件环境:安装 python 3.8 及以上版本,可从 python 官网下载安装包进行安装。安装时勾选 “add python to path” 选项,方便后续命令行操作。同时,安装 pytorch 和 transformers 库。由于 windows 下 cuda 安装较为复杂,建议使用 conda 进行环境管理。首先安装 anaconda,然后创建一个新的 conda 环境并安装依赖:
conda create -n deepseek_env python=3.8 conda activate deepseek_env conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia pip install transformers
2.下载 deepseek 模型
访问 deepseek 官方模型下载地址,选择合适的模型版本。
可使用浏览器直接下载模型文件,也可以在命令行中使用wget(需提前安装)或curl工具下载。例如,使用curl下载 deepseek-7b 模型:
curl -o https://download.deepseek.com/deepseek-7b.tar.gz
下载完成后,解压模型文件,可使用 7-zip 等解压工具。
3. 部署步骤
创建项目目录:在文件资源管理器中创建一个新的文件夹,例如 “deepseek_deployment”,用于存放部署相关文件。
编写推理脚本:使用文本编辑器(如 notepad++、vs code 等)编写 python 推理脚本inference.py,内容与 linux 版本类似:
import torch from transformers import autotokenizer, automodelforcausallm # 加载分词器和模型 tokenizer = autotokenizer.from_pretrained("path/to/deepseek-7b") model = automodelforcausallm.from_pretrained("path/to/deepseek-7b", torch_dtype=torch.float16).to('cuda') # 定义推理函数 def generate_text(prompt, max_length=100): input_ids = tokenizer.encode(prompt, return_tensors='pt').to('cuda') output = model.generate(input_ids, max_length=max_length, num_beams=5, early_stopping=true) return tokenizer.decode(output[0], skip_special_tokens=true) # 示例使用 prompt = "请介绍一下人工智能的发展趋势" generated_text = generate_text(prompt) print(generated_text) 请将path/to/deepseek-7b替换为实际的模型路径。 启动服务:若要部署为服务,同样可以使用 fastapi 和 uvicorn。在命令行中激活 conda 环境后安装相关库: pip install fastapi uvicorn 编写服务脚本app.py,内容与 linux 版本类似: from fastapi import fastapi from pydantic import basemodel import torch from transformers import autotokenizer, automodelforcausallm app = fastapi() # 加载分词器和模型 tokenizer = autotokenizer.from_pretrained("path/to/deepseek-7b") model = automodelforcausallm.from_pretrained("path/to/deepseek-7b", torch_dtype=torch.float16).to('cuda') class promptrequest(basemodel): prompt: str max_length: int = 100 @app.post("/generate") def generate_text(request: promptrequest): input_ids = tokenizer.encode(request.prompt, return_tensors='pt').to('cuda') output = model.generate(input_ids, max_length=request.max_length, num_beams=5, early_stopping=true) return {"generated_text": tokenizer.decode(output[0], skip_special_tokens=true)}
将path/to/deepseek-7b替换为实际路径。
启动服务:
uvicorn app.py:app --host 0.0.0.0 --port 8000
三、mac 系统部署
1.准备工作
硬件要求:如果是配备 m1 或 m2 芯片的 mac,可利用其强大的计算能力进行部署。对于 intel 芯片的 mac,建议配备较好的显卡(如果有独立显卡)。内存至少 16gb,存储使用高速固态硬盘。
软件环境:安装 python 3.8 及以上版本,可通过 homebrew 安装。首先安装 homebrew,然后安装 python 和相关依赖库:
/bin/bash -c "$(curl -fssl https://raw.githubusercontent.com/homebrew/install/head/install.sh)" brew install python pip install torch torchvision torchaudio pip install transformers
如果是 m1 或 m2 芯片的 mac,安装 pytorch 时需注意选择适配 arm 架构的版本:
pip install torch torchvision torchaudio -f https://download.pytorch.org/whl/torch_stable.html
2.下载 deepseek 模型
访问 deepseek 官方模型下载地址,选择合适的模型版本。
使用curl命令下载模型文件,例如:
curl -o https://download.deepseek.com/deepseek-7b.tar.gz
下载完成后,解压模型文件:
tar -zxvf deepseek-7b.tar.gz
3.部署步骤
创建项目目录:在终端中使用以下命令创建项目目录:
mkdir deepseek_deployment cd deepseek_deployment
编写推理脚本:使用文本编辑器(如 textedit、vs code 等)编写 python 推理脚本inference.py,内容与前面类似:
import torch from transformers import autotokenizer, automodelforcausallm # 加载分词器和模型 tokenizer = autotokenizer.from_pretrained("path/to/deepseek-7b") if torch.backends.mps.is_available(): model = automodelforcausallm.from_pretrained("path/to/deepseek-7b", torch_dtype=torch.float16).to('mps') else: model = automodelforcausallm.from_pretrained("path/to/deepseek-7b", torch_dtype=torch.float16).to('cuda' if torch.cuda.is_available() else 'cpu') # 定义推理函数 def generate_text(prompt, max_length=100): input_ids = tokenizer.encode(prompt, return_tensors='pt').to(model.device) output = model.generate(input_ids, max_length=max_length, num_beams=5, early_stopping=true) return tokenizer.decode(output[0], skip_special_tokens=true) # 示例使用 prompt = "请介绍一下人工智能的发展趋势" generated_text = generate_text(prompt) print(generated_text) 请将path/to/deepseek-7b替换为实际的模型路径。 启动服务:若要部署为服务,安装 fastapi 和 uvicorn: pip install fastapi uvicorn 编写服务脚本app.py,内容与前面类似: from fastapi import fastapi from pydantic import basemodel import torch from transformers import autotokenizer, automodelforcausallm app = fastapi() # 加载分词器和模型 tokenizer = autotokenizer.from_pretrained("path/to/deepseek-7b") if torch.backends.mps.is_available(): model = automodelforcausallm.from_pretrained("path/to/deepseek-7b", torch_dtype=torch.float16).to('mps') else: model = automodelforcausallm.from_pretrained("path/to/deepseek-7b", torch_dtype=torch.float16).to('cuda' if torch.cuda.is_available() else 'cpu') class promptrequest(basemodel): prompt: str max_length: int = 100 @app.post("/generate") def generate_text(request: promptrequest): input_ids = tokenizer.encode(request.prompt, return_tensors='pt').to(model.device) output = model.generate(input_ids, max_length=request.max_length, num_beams=5, early_stopping=true) return {"generated_text": tokenizer.decode(output[0], skip_special_tokens=true)}
将path/to/deepseek-7b替换为实际路径。
启动服务:
uvicorn app.py:app --host 0.0.0.0 --port 8000
四、优化与注意事项
模型量化:为减少内存占用和提高推理速度,可对模型进行量化处理,如使用 int8 量化。
安全设置:部署服务时,注意设置合理的访问权限和安全策略,防止模型被恶意调用。
性能监控:在 linux 和 windows 系统中,可使用 nvidia system management interface(nvidia-smi)监控 gpu 使用情况;在 mac 系统中,对于 m1/m2 芯片,可使用top命令等监控系统资源使用情况,确保模型运行在最佳状态。
到此这篇关于本地化部署 deepseek 全攻略的文章就介绍到这了,更多相关本地化部署 deepseek内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论