---
title: DeepSeek-V4-Flash-SI
canonical_url: "https://www.modelscope.cn/models/robinLua/DeepSeek-V4-Flash-SI"
md_url: "https://www.modelscope.cn/models/robinLua/DeepSeek-V4-Flash-SI.md"
repository: robinLua/DeepSeek-V4-Flash-SI
chinese_name: "流式推理引擎的DeepSeek-V4-Flash的权重文件"
last_updated: 2026-05-07
license: "MIT License"
pipeline_tag: text-generation
tasks:
  - text-generation
model_type:
  - deepseek_v4
architectures:
  - DeepseekV4ForCausalLM
base_model:
  - deepseek-ai/DeepSeek-V4-Flash
base_model_relation: repackage
parameters: 299.8B
tensor_type:
  - I64
  - BF16
  - F32
  - F4
  - F8_E8M0
  - F8_E4M3
library_name:
  - safetensors
  - pytorch
frameworks:
  - PyTorch
language:
  - zh
downloads: 1803
stars: 2
---

# DeepSeek-V4-Flash-SI

> DeepSeek-V4-Flash-SI - robinLua 在 ModelScope 开源的模型。DeepSeek-V4-Flash — MoE 大语言模型，284B 总参数，每 token 激活 ~13B。 43 层 Transformer，隐藏维度 4096，64 注意力头（MLA 低秩压缩）， 256 路由专家 + 1 共享专家（每 token 激活 6 个），词表 129,280。 支持 128 窗口滑动注意力 + KV 压缩（20 层 Indexer top-512 + 20 层密集），…

robinLua/DeepSeek-V4-Flash-SI 是 ModelScope 魔搭社区上的 299.8B 参数text-generation模型，采用 MIT License 许可，基于 deepseek-ai/DeepSeek-V4-Flash 构建。

- **Repository**: robinLua/DeepSeek-V4-Flash-SI
- **License**: MIT License
- **Tasks**: text-generation
- **Parameters**: 299.8B
- **Base model**: deepseek-ai/DeepSeek-V4-Flash
- **Downloads**: 1803
- **Stars**: 2
- **Last updated**: 2026-05-07

Source: https://www.modelscope.cn/models/robinLua/DeepSeek-V4-Flash-SI

---

# DeepSeek-V4-Flash · 流式推理模型

> 模型来源：[DeepSeek-V4-Flash](https://www.modelscope.cn/models/deepseek-ai/DeepSeek-V4-Flash)（284B 参数），经权重转换（FP4 量化专家 + per-layer safetensors）后，供本项目的流式推理引擎使用。
参见[源代码](https://atomgit.com/robrui/ds4si)、[技术文章](https://blog.csdn.net/redhat77/article/details/160771630)。
## 模型概述

DeepSeek-V4-Flash 是 DeepSeek 系列的高效大语言模型，采用 MoE（Mixture of Experts）架构。模型总参数量 284B，每个 token 激活约 13B 参数。本目录为转换后的权重文件，专为流式推理引擎加载。

| 属性 | 值 |
|------|-----|
| 模型架构 | DeepseekV4ForCausalLM |
| 总参数量 | 284B |
| 激活参数量 | ~13B / token |
| 隐藏维度 | 4096 |
| 层数 | 43 |
| 注意力头数 | 64 |
| 头维度 | 512 |
| 词表大小 | 129,280 |
| 最大位置编码 | 1,048,576 |
| 上下文长度 | 4096（本推理实现） |
| 量化格式 | 非专家 BF16 + 专家 FP4 E2M1 |
| 权重文件大小 | ~150 GB（45 个 safetensors） |

## 模型介绍

### 架构特性

**Multi-head Latent Attention (MLA)**：Q 投影采用低秩分解（`wq_a → q_norm → wq_b`），秩 1024。KV 压缩为单一头维度 512 的向量，经 RoPE（YaRN 外推）编码后存入 cache。O 投影分 8 组低秩输出。MLA 将 KV cache 压缩至极低的 ~0.5 GB（43 层合计）。

**Mixture of Experts (MoE)**：每层 256 个 routed expert + 1 个 shared expert，每个 token 经由 gate 网络选择 top-6 专家。前 3 层使用 hash routing（基于 token ID 确定性选择），其余 40 层使用 sqrtsoftplus 评分 top-k。routing scale 1.5，SwiGLU 激活限幅 ±10.0。

**Hyper-Connections (HC)**：以 hc_mult=4 维护 4 份隐藏状态副本。每层通过 Sinkhorn 归一化（20 次迭代）对 hc 副本加权组合（hc_pre），attention/FFN 处理后扩展回 4 副本（hc_post）。替代传统残差连接，增强梯度流动。

**稀疏注意力 + KV 压缩**：

| 层 | 压缩比 | 策略 |
|----|:---:|------|
| 0, 1, 42 | 0 | 纯滑动窗口（128 tokens） |
| 20 层 | 4 | Indexer 评分 top-512 压缩 KV |
| 20 层 | 128 | 密集压缩（gated pooling） |

**低精度计算**：非专家权重保持 BF16（9.77 GB），专家权重 FP4 E2M1 + F8_E8M0 block scale（每 32 个元素一组）。激活值在线 FP8 E4M3 量化（每 128 个元素一组，power-of-2 scale）。

### 推理架构（本实现）

本目录包含从 HuggingFace 格式转换而来的 per-layer safetensors 文件，专为 DGX Spark（128GB 统一内存）流式推理优化：

- `global.safetensors`（2.0 GB）：embed、head、norm、hc_head 参数
- `model_L{00-42}.safetensors`（43 × ~3.4 GB）：每层全部张量（attention、gate、shared expert、HC、256 expert）
- `mtp.safetensors`（3.4 GB）：MTP 推测解码头 + 256 expert

每个 `model_L{xx}.safetensors` 自包含一层的完整数据——流式推理时只需 mmap 当前层文件，避免跨文件寻址。

## 模型训练

DeepSeek-V4-Flash 由 DeepSeek 团队训练，官方发布在 [ModelScope](https://www.modelscope.cn/models/deepseek-ai/DeepSeek-V4-Flash)。

关键训练配置（从 config 推断）：

| 配置项 | 值 |
|------|-----|
| 训练精度 | BF16 + FP8 激活量化 |
| 激活量化 | 动态 FP8 E4M3，ue8m0 scale |
| 权重 block size | [128, 128] |
| 优化器 | 未公开 |
| 上下文长度（训练） | 65,536（经 YaRN 外推至 1M） |
| 词表大小 | 129,280 |
| BOS token | `<｜begin▁of▁sentence｜>` (id=0) |
| EOS token | `<｜end▁of▁sentence｜>` (id=1) |

## 模型推理

### 运行环境

| 依赖 | 版本要求 |
|------|---------|
| PyTorch | ≥ 2.11.0 |
| TileLang | == 0.1.9 |
| transformers | ≥ 5.0.0 |
| CUDA | 12.x（Blackwell SM100+） |
| 推荐硬件 | NVIDIA DGX Spark（128GB 统一内存） |
| 最低内存 | ~110 GB（含 90GB 专家缓存） |

### 推理命令

```bash
cd /home/admin/ds4/src/inference

# 单次推理
python streaming_inference.py \
    --ckpt-path ../converted_model \
    --prompt "你好" --max-tokens 100

# CUDA Graph 加速
python streaming_inference.py \
    --capturable --cudagraph \
    --ckpt-path ../converted_model \
    --prompt "你好" --max-tokens 100

# 交互式对话
python streaming_inference.py \
    --ckpt-path ../converted_model \
    --interactive

# 思考模式
python streaming_inference.py \
    --ckpt-path ../converted_model \
    --interactive --thinking thinking
```

### 推理模式

| 模式 | 命令 | 说明 |
|------|------|------|
| Eager | 默认 | 三阶段异步流水线，每步实时加载专家 |
| CUDA Graph | `--capturable --cudagraph` | MoE GEMM 路径图捕获，降低 kernel launch 开销 |
| 推测解码 | `--speculative` | MTP 头草稿 + 验证（实验性） |

### 资源需求

| 指标 | 数值 |
|------|------|
| 非专家权重（常驻） | 9.77 GB |
| 专家缓存（LFU，默认 90 GB） | 按需填充 |
| KV Cache（43 层 MLA） | ~0.5 GB |
| 激活值 + 临时缓冲 | ~5 GB |
| PyTorch/CUDA 框架 | ~5 GB |
| **总内存** | **~110 GB** |

## 模型效果

### 性能基准（DGX Spark, 2026-05-05）

| 指标 | 数值 |
|------|------|
| 热缓存解码速度 | 2.31 tok/s |
| 冷启动首 token 延迟 | 3.6s |
| 专家缓存热命中率 | 96.1% |
| 上下文长度 | 4096 tokens |
| CUDA Graph 加速 | ~2.16 tok/s（eager 的 93.5%） |

### 解码瓶颈（433ms/步）

```
433ms 解码步
├── Python/工程开销        ~270ms (62%)
├── Attention（43 层）      ~108ms (25%)
├── MoE GEMM（43×6 专家）   ~43ms (10%)
├── Head 投影               ~5ms  (1%)
└── 采样/其他               ~7ms  (2%)
```

Python 开销为主要瓶颈。CUDA Graph 部分缓解了 kernel launch 开销，后续优化方向为减少 Python 循环和 dict 操作。

### 质量标准

- 输出质量经端到端验证，与 eager 模式 diff=0.0
- Gate 每步实时计算（无 one-step-lag），专家选择精度与 StreamingMoE 等价
- 支持多轮对话：首轮 warmup + capture graph，后续轮次直接图解码
- 支持思考模式（thinking/max）、top-p 核采样、温度调节

## 文件清单

```
converted_model/
├── config.json                # 模型超参数（HuggingFace 格式）
├── inference_config.json      # 推理引擎配置
├── tokenizer.json             # BPE 分词器
├── tokenizer_config.json      # 分词器配置
├── global.safetensors         # 全局权重（2.0 GB, 6 张量）
├── model_L{00-42}.safetensors # 逐层权重（43×3.4 GB, ~1565 张量/层）
└── mtp.safetensors            # MTP 推测解码权重（3.4 GB）
```

转换工具链位于 `src/cleanup_and_convert.sh`，支持从 ModelScope HuggingFace 格式转换。
