如何运行 DeepSeek-R1

在本文件中,我们将向您展示如何在您的本地计算机上安装和运行 KTransformers。共有两个版本:

  • V0.2 是当前的主分支。
  • V0.3 是一个预览版本,目前仅提供二进制分发。
  • 要重现我们的DeepSeek-R1/V3结果,请参考Deepseek-R1/V3教程以获取安装后更多详细设置。

准备

一些准备:

  • CUDA 12.1 及以上版本,如果您还没有安装,可以从 这里 安装。

    # 将 CUDA 添加到 PATH
    if [ -d "/usr/local/cuda/bin" ]; then
        export PATH=$PATH:/usr/local/cuda/bin
    fi
    
    if [ -d "/usr/local/cuda/lib64" ]; then
        export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64
        # 或者您可以将其添加到 /etc/ld.so.conf 并以 root 身份运行 ldconfig:
        # echo "/usr/local/cuda-12.x/lib64" | sudo tee -a /etc/ld.so.conf
        # sudo ldconfig
    fi
    
    if [ -d "/usr/local/cuda" ]; then
        export CUDA_PATH=$CUDA_PATH:/usr/local/cuda
    fi
    
  • 使用gcc、g++和cmake的Linux-x86_64(以Ubuntu为例)

    sudo apt-get update
    sudo apt-get install build-essential cmake ninja-build
    
  • 我们推荐使用 Miniconda3Anaconda3 来创建一个 Python=3.11 的虚拟环境以运行我们的程序。假设您的 Anaconda 安装目录是 ~/anaconda3,您应该确保 Anaconda 使用的 GNU C++标准库的版本标识符包含 GLIBCXX-3.4.32

    conda create --name ktransformers python=3.11
    conda activate ktransformers # 您可能需要先运行 ‘conda init’ 并重新打开终端
    
    conda install -c conda-forge libstdcxx-ng # Anaconda 提供一个名为 `libstdcxx-ng` 的软件包,它包含更新版本的 `libstdc++`,可以通过 `conda-forge` 安装。
    
    strings ~/anaconda3/envs/ktransformers-0.3/lib/libstdc++.so.6 | grep GLIBCXX
    
  • 确保已安装 PyTorch、packaging 和 ninja。您也可以安装以前版本的 PyTorch

    pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
    pip3 install packaging ninja cpufeature numpy
    
  • 同时,您应该从 https://github.com/Dao-AILab/flash-attention/releases 下载并安装相应版本的 flash-attention。

安装

  • 下载源代码并编译:

    • 初始化源代码

      git clone https://github.com/kvcache-ai/ktransformers.git
      cd ktransformers
      git submodule init
      git submodule update
      
    • [可选] 如果您想在网站上运行,请在执行 bash install.sh 之前 编译网站

    • 对于Linux

      • 简单安装方法:

        bash install.sh
        
      • 对于拥有两个CPU和1T RAM的用户:

        # 确保您的系统具有双插槽,并且内存大小是模型大小的两倍(例如,512G模型需要1T RAM)
        export USE_NUMA=1
        bash install.sh # 或者 `make dev_install`
        
    • 适用于Windows

      install.bat
      
  • 如果您是开发者,您可以利用makefile来编译和格式化代码。
    makefile的详细用法在这里

本地聊天

We provide a simple command-line local chat Python script that you can run for testing.

注意:这是一个非常简单的测试工具,只支持一轮对话,不会记住上一条输入,如果您想尝试模型的全部功能,可以访问 RESTful API and Web UI

运行示例

# Begin from root of your cloned repo!
# Begin from root of your cloned repo!!
# Begin from root of your cloned repo!!! 

# Download mzwing/DeepSeek-V2-Lite-Chat-GGUF from huggingface
mkdir DeepSeek-V2-Lite-Chat-GGUF
cd DeepSeek-V2-Lite-Chat-GGUF

wget https://huggingface.co/mradermacher/DeepSeek-V2-Lite-GGUF/resolve/main/DeepSeek-V2-Lite.Q4_K_M.gguf -O DeepSeek-V2-Lite-Chat.Q4_K_M.gguf

cd .. # Move to repo's root dir

# Start local chat
python -m ktransformers.local_chat --model_path deepseek-ai/DeepSeek-V2-Lite-Chat --gguf_path ./DeepSeek-V2-Lite-Chat-GGUF

# If you see “OSError: We couldn't connect to 'https://huggingface.co' to load this file”, try:
# GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/deepseek-ai/DeepSeek-V2-Lite
# python  ktransformers.local_chat --model_path ./DeepSeek-V2-Lite --gguf_path ./DeepSeek-V2-Lite-Chat-GGUF

它具有以下参数:

  • --model_path(必需):模型名称(例如“deepseek-ai/DeepSeek-V2-Lite-Chat”,将自动从Hugging Face下载配置)。或者如果您已经有本地文件,您可以直接使用该路径来初始化模型。

    注意:目录中不需要.safetensors文件。我们只需要配置文件来构建模型和分词器。

  • --gguf_path(必需):包含可以从 Hugging Face 下载的 GGUF 文件的目录路径。请注意,该目录应仅包含当前模型的 GGUF,这意味着每个模型需要一个单独的目录。

  • --optimize_config_path(除Qwen2Moe和DeepSeek-V2外为必需): 包含优化规则的YAML文件路径。ktransformers/optimize/optimize_rules目录中预编写了两个规则文件,用于优化DeepSeek-V2和Qwen2-57B-A14这两个最先进的MoE模型。

  • --max_new_tokens: 整数(默认=1000)。生成的新标记的最大数量。

  • --cpu_infer: 整数(默认为10)。用于推断的CPU数量。理想情况下应设置为(总核心数 - 2)。

Supported Models/quantization

支持的模型包括:

支持的模型不再支持的模型
DeepSeek-R1InternLM2.5-7B-Chat-1M
深度搜索-V3
深度寻宝-V2
DeepSeek-V2.5
Qwen2-57B
DeepSeek-V2-Lite
Mixtral-8x7B
Mixtral-8x22B

支持量化格式:

支持的格式不再支持的格式
Q2_K_LIQ2_XXS
Q2_K_XS
Q3_K_M
Q4_K_M
Q5_K_M
Q6_K
Q8_0
Suggested Model
模型名称模型大小显存最低内存推荐内存
DeepSeek-R1-q4_k_m377G14G382G512G
DeepSeek-V3-q4_k_m377G14G382G512G
DeepSeek-V2-q4_k_m133G11G136G192G
DeepSeek-V2.5-q4_k_m133G11G136G192G
DeepSeek-V2.5-IQ4_XS117G10G107G128G
Qwen2-57B-A14B-Instruct-q4_k_m33G8G34G64G
DeepSeek-V2-Lite-q4_k_m9.7G3G13G16G
Mixtral-8x7B-q4_k_m25G1.6G51G64G
Mixtral-8x22B-q4_k_m80G4G86.1G96G
InternLM2.5-7B-Chat-1M15.5G15.5G8G(32K 上下文)150G (1M 上下文)

更多内容将很快发布。请告诉我们您最感兴趣的模型。

请注意,在使用 DeepSeekQWen 时,您需要遵守它们对应的模型许可证。

Click To Show how to run other examples
  • Qwen2-57B

    pip install flash_attn # 针对 Qwen2
    
    mkdir Qwen2-57B-GGUF && cd Qwen2-57B-GGUF
    
    wget https://huggingface.co/Qwen/Qwen2-57B-A14B-Instruct-GGUF/resolve/main/qwen2-57b-a14b-instruct-q4_k_m.gguf?download=true -O qwen2-57b-a14b-instruct-q4_k_m.gguf
    
    cd ..
    
    python -m ktransformers.local_chat --model_name Qwen/Qwen2-57B-A14B-Instruct --gguf_path ./Qwen2-57B-GGUF
    
    # 如果你看到 “OSError: 我们无法连接到 'https://huggingface.co' 来加载此文件”,请尝试:
    # GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/Qwen/Qwen2-57B-A14B-Instruct
    # python  ktransformers/local_chat.py --model_path ./Qwen2-57B-A14B-Instruct --gguf_path ./DeepSeek-V2-Lite-Chat-GGUF
    
  • Deepseek-V2

    mkdir DeepSeek-V2-Chat-0628-GGUF && cd DeepSeek-V2-Chat-0628-GGUF
    # 下载权重
    wget https://huggingface.co/bartowski/DeepSeek-V2-Chat-0628-GGUF/resolve/main/DeepSeek-V2-Chat-0628-Q4_K_M/DeepSeek-V2-Chat-0628-Q4_K_M-00001-of-00004.gguf -o DeepSeek-V2-Chat-0628-Q4_K_M-00001-of-00004.gguf
    wget https://huggingface.co/bartowski/DeepSeek-V2-Chat-0628-GGUF/resolve/main/DeepSeek-V2-Chat-0628-Q4_K_M/DeepSeek-V2-Chat-0628-Q4_K_M-00002-of-00004.gguf -o DeepSeek-V2-Chat-0628-Q4_K_M-00002-of-00004.gguf
    wget https://huggingface.co/bartowski/DeepSeek-V2-Chat-0628-GGUF/resolve/main/DeepSeek-V2-Chat-0628-Q4_K_M/DeepSeek-V2-Chat-0628-Q4_K_M-00003-of-00004.gguf -o DeepSeek-V2-Chat-0628-Q4_K_M-00003-of-00004.gguf
    wget https://huggingface.co/bartowski/DeepSeek-V2-Chat-0628-GGUF/resolve/main/DeepSeek-V2-Chat-0628-Q4_K_M/DeepSeek-V2-Chat-0628-Q4_K_M-00004-of-00004.gguf -o DeepSeek-V2-Chat-0628-Q4_K_M-00004-of-00004.gguf
    
    cd ..
    
    python -m ktransformers.local_chat --model_name deepseek-ai/DeepSeek-V2-Chat-0628 --gguf_path ./DeepSeek-V2-Chat-0628-GGUF
    
    # 如果你看到“OSError: We couldn't connect to 'https://huggingface.co' to load this file”,请尝试:
    
    # GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/deepseek-ai/DeepSeek-V2-Chat-0628
    
    # python -m ktransformers.local_chat --model_path ./DeepSeek-V2-Chat-0628 --gguf_path ./DeepSeek-V2-Chat-0628-GGUF
    
模型名称权重下载链接
Qwen2-57BQwen2-57B-A14B-gguf-Q4K-M
DeepseekV2-coderDeepSeek-Coder-V2-Instruct-gguf-Q4K-M
DeepseekV2-chatDeepSeek-V2-Chat-gguf-Q4K-M
DeepseekV2-liteDeepSeek-V2-Lite-Chat-GGUF-Q4K-M
DeepSeek-R1DeepSeek-R1-gguf-Q4K-M

RESTful API 和 Web UI

无需网站启动:

ktransformers --model_path deepseek-ai/DeepSeek-V2-Lite-Chat --gguf_path /path/to/DeepSeek-V2-Lite-Chat-GGUF --port 10002

使用网站启动:

ktransformers --model_path deepseek-ai/DeepSeek-V2-Lite-Chat --gguf_path /path/to/DeepSeek-V2-Lite-Chat-GGUF  --port 10002 --web True

或者您希望使用 transformers 启动服务器,model_path 应该包含 safetensors

ktransformers --type transformers --model_path /mnt/data/model/Qwen2-0.5B-Instruct --port 10002 --web True

通过网址 http://localhost:10002/web/index.html#/chat 访问网站:

Web UI

有关 RESTful API 服务器的更多信息,请在 这里 查找。您还可以在 这里 找到与 Tabby 集成的示例。