---
title: Deepseek-V4-Flash-OPUS4.7-4.8-DeepReason-Fable5-CluadeCode-Qlora
canonical_url: "https://www.modelscope.cn/models/Seirin10086/Deepseek-V4-Flash-OPUS4.7-4.8-DeepReason-Fable5-CluadeCode-Qlora"
md_url: "https://www.modelscope.cn/models/Seirin10086/Deepseek-V4-Flash-OPUS4.7-4.8-DeepReason-Fable5-CluadeCode-Qlora.md"
repository: Seirin10086/Deepseek-V4-Flash-OPUS4.7-4.8-DeepReason-Fable5-CluadeCode-Qlora
last_updated: 2026-07-28
model_type:
  - deepseek_v4
architectures:
  - DeepseekV4ForCausalLM
base_model:
  - deepseek-ai/DeepSeek-V4-Flash
base_model_relation: adapter
parameters: 284.3B
tensor_type:
  - F32
  - I64
  - I8
  - F8_E8M0
  - BF16
library_name:
  - transformer
  - gguf
  - safetensors
downloads: 151
stars: 0
---

# Deepseek-V4-Flash-OPUS4.7-4.8-DeepReason-Fable5-CluadeCode-Qlora

> Deepseek-V4-Flash-OPUS4.7-4.8-DeepReason-Fable5-CluadeCode-Qlora - Seirin10086 在 ModelScope 开源的模型。DeepSeek-V4-Flash OPUS4.7-4.8-DeepReason-Fable5-CluadeCode QLoRA

Seirin10086/Deepseek-V4-Flash-OPUS4.7-4.8-DeepReason-Fable5-CluadeCode-Qlora 是 ModelScope 魔搭社区上的 284.3B 参数机器学习模型，基于 deepseek-ai/DeepSeek-V4-Flash 构建。

- **Repository**: Seirin10086/Deepseek-V4-Flash-OPUS4.7-4.8-DeepReason-Fable5-CluadeCode-Qlora
- **Parameters**: 284.3B
- **Base model**: deepseek-ai/DeepSeek-V4-Flash
- **Downloads**: 151
- **Stars**: 0
- **Last updated**: 2026-07-28

Source: https://www.modelscope.cn/models/Seirin10086/Deepseek-V4-Flash-OPUS4.7-4.8-DeepReason-Fable5-CluadeCode-Qlora

---

# DeepSeek-V4-Flash OPUS4.7-4.8-DeepReason-Fable5-CluadeCode QLoRA

基于 DeepSeek-V4-Flash (284B MoE, FP8+FP4 量化) 的 QLoRA 微调模型。
adapter.gguf已上传至adapter目录，llama.cpp可用的量化模型已上传

## 训练过程

### 基座模型
- **模型**: DeepSeek-V4-Flash (284B 参数, MoE 架构)
- **量化方式**: FP8 (非 expert 权重) + FP4 (expert 权重)
- **有效参数**: ~154.5B (量化压缩后), 显存占用 ~145.3GB (AMD MI308X 192GB)

### 数据集
数据集由两部分在 HuggingFace 上的数据集混合清洗得到：
1. **Claude-Opus-4.7-4.8-DeepReason-462x-105M** - Claude Opus 深度推理对话
2. **Claude-Fable-5-Claude-Code** - Claude Fable 模型代码生成对话

总计 55 个 session / 1970 条消息，清洗后分为 46 训练 + 9 评估 session。

### 训练配置
| 参数 | 值 |
|---|---|
| 框架 | PyTorch + PEFT LoRA + Transformers |
| 计算精度 | bfloat16 (autocast) |
| LoRA rank | 16 |
| LoRA alpha | 32 |
| LoRA target | q_a_proj, q_b_proj, kv_proj, o_b_proj, gate_proj, up_proj, down_proj |
| 可训练参数 | 63.5M (占总参数 0.04%) |
| Batch size | 1 (gradient accumulation 4) |
| 学习率 | 2e-4 (linear warmup + decay) |
| Epochs | 2 (early stopping) |
| 优化器 | AdamW (weight decay 0.1) |
| 梯度裁剪 | max norm 1.0 |
| 损失函数 | causal LM loss (仅 label assistant 回复) |

### 硬件
- GPU: AMD MI308X (gfx942) × 1, 192GB VRAM

### 关键实现细节
1. 移除了 `o_a_proj`（FP8GroupedLinear）以防止与 LoRA 不兼容
2. 对 `load_finegrained_fp8_kernel` 进行了 monkey-patch，用纯 PyTorch bf16 matmul 替代 Triton kernel
3. 自动检测 FP4/FP8 weight 布局方向，动态选择 `x @ w` 或 `F.linear`

### 训练曲线
- Epoch 1: Train 18.94 → Eval 17.63 (loss 下降)
- Epoch 2: Train 17.16 → Eval 17.59 (loss 继续下降)
- Epoch 3: Train 17.52 → Eval 17.81 (开始过拟合, 触发 early stopping)

最终选择 Epoch 2 的 checkpoint 进行 merge 和发布。

## 文件结构

```
/
├── model-00001-of-00004.safetensors  # 合并模型 shard 1
├── model-00002-of-00004.safetensors  # 合并模型 shard 2
├── model-00003-of-00004.safetensors  # 合并模型 shard 3
├── model-00004-of-00004.safetensors  # 合并模型 shard 4
├── model.safetensors.index.json      # 权重索引
├── config.json                       # 模型配置
├── tokenizer.json                    # 分词器
├── tokenizer_config.json             # 分词器配置
├── generation_config.json            # 生成配置
├── lora_adapter/                     # LoRA adapter (独立)
│   ├── adapter_model.safetensors
│   ├── adapter_config.json
│   ├── adapter.gguf              # GGUF 格式 LoRA adapter (llama.cpp 直接使用)
│   └── README.md
├── lora_adapter.zip                  # LoRA adapter 压缩包
├── combined_scored.jsonl             # 训练数据集
└── README.md                         # 本文件
```

## 使用方法

### 加载合并模型
```python
from transformers import AutoModelForCausalLM, AutoTokenizer

model = AutoModelForCausalLM.from_pretrained(
    "Seirin10086/Deepseek-V4-Flash-OPUS4.7-4.8-DeepReason-Fable5-CluadeCode-Qlora",
    trust_remote_code=True,
    device_map="auto",
)
tokenizer = AutoTokenizer.from_pretrained(
    "Seirin10086/Deepseek-V4-Flash-OPUS4.7-4.8-DeepReason-Fable5-CluadeCode-Qlora",
    trust_remote_code=True,
)
```

### 加载 LoRA Adapter
```python
from peft import PeftModel

model = PeftModel.from_pretrained(
    model,
    "Seirin10086/Deepseek-V4-Flash-OPUS4.7-4.8-DeepReason-Fable5-CluadeCode-Qlora/lora_adapter",
)
```


## GGUF LoRA Adapter（llama.cpp 直接使用）

除 HuggingFace 格式的 `lora_adapter/`（PEFT）外，仓库额外提供 **GGUF 格式**的 LoRA adapter：

- 路径：`lora_adapter/adapter.gguf`
- 大小：约 127 MB
- 精度：BF16
- 生成方式：由 `convert_lora_to_gguf.py` 从 `lora_adapter/` 的 HF adapter 转换而来

> 说明：DeepSeek-V4 基座 GGUF 使用短命名（如 `attn.wq_a`、`ffn.shared_experts.w2`），而 HF adapter 使用标准命名（如 `self_attn.q_a_proj`、`mlp.shared_experts.down_proj`）。转换时已自动做名称映射，使 `adapter.gguf` 能正确匹配基座 GGUF 的张量名。

### 使用方式（llama.cpp）

无需 PyTorch / PEFT，推理时直接挂载 adapter：

```bash
# 服务端（OpenAI 兼容 API）
llama-server -m <base_model>.gguf --lora lora_adapter/adapter.gguf

# 命令行
llama-cli -m <base_model>.gguf --lora lora_adapter/adapter.gguf
```

`<base_model>.gguf` 为 DeepSeek-V4-Flash 的 GGUF 基座（或下方量化后的模型）。

---

## 量化方案（llama.cpp）

合并后的 F16 模型（`Ds-256x8.4B-MXFP4_MOE-lora-f16.gguf`）或基座可用 `llama-quantize` 量化回小体积 GGUF。下方方案已通过 `--dry-run` 验证。

### 命令

```bash
llama-quantize \
  --imatrix "DeepSeek-V4-Flash-chat-v2-routed-moe-ds4-aligned.dat" \
  --allow-requantize \
  --exclude-weights attn_q_a.weight \
  --exclude-weights attn_q_b.weight \
  --exclude-weights attn_kv.weight \
  --exclude-weights attn_output_b.weight \
  --exclude-weights ffn_gate_shexp.weight \
  --exclude-weights ffn_down_shexp.weight \
  --exclude-weights ffn_up_shexp.weight \
  --exclude-weights attn_compressor_gate.weight \
  --exclude-weights attn_compressor_kv.weight \
  --exclude-weights indexer_compressor_gate.weight \
  --exclude-weights indexer_compressor_kv.weight \
  --exclude-weights indexer.attn_q_b.weight \
  --tensor-type "token_embd\.weight=Q6_K" \
  --tensor-type "output\.weight=Q4_K" \
  --tensor-type "attn_kv\.weight=Q6_K" \
  --tensor-type "attn_q_a\.weight=Q4_K" \
  --tensor-type "attn_q_b\.weight=Q4_K" \
  --tensor-type "attn_output_b\.weight=Q5_K" \
  --tensor-type "ffn_down_shexp\.weight=Q6_K" \
  --tensor-type "ffn_up_shexp\.weight=Q4_K" \
  --tensor-type "ffn_gate_shexp\.weight=Q4_K" \
  --tensor-type "ffn_down_exps\.weight=MXFP4" \
  --tensor-type "ffn_up_exps\.weight=Q2_K" \
  --tensor-type "ffn_gate_exps\.weight=Q2_K" \
  "Ds-256x8.4B-MXFP4_MOE-lora-f16.gguf" "DeepSeekV4-Flash-Flash-Opus-Fable-QLora.gguf" Q4_K_M
```

> Windows 命令行请将续行符 `\` 替换为 `^`。

### 逐模块量化类型

| 张量模块 | 量化类型 | 说明 |
|---|---|---|
| `token_embd` | Q6_K | 词嵌入 |
| `output` | Q4_K | 输出层（lm_head） |
| `attn_kv` | Q6_K | MLA 合并 KV 投影（单张量，K/V 不可分） |
| `attn_q_a` / `attn_q_b` | Q4_K | attention Q 投影 |
| `attn_output_b` | Q5_K | attention 输出 |
| `ffn_down_shexp` | Q6_K | 共享专家 down |
| `ffn_up_shexp` / `ffn_gate_shexp` | Q4_K | 共享专家 up / gate |
| `ffn_down_exps` | MXFP4 | 路由专家 down（保持原生 MXFP4 不变） |
| `ffn_up_exps` / `ffn_gate_exps` | Q2_K | 路由专家 up / gate |
| 其余张量（含 12 类 LoRA 改动层） | Q4_K_M（默认） | 全局混合类型 |

### 说明

- `--imatrix` 使用合并前模型训练的 `DeepSeek-V4-Flash-chat-v2-routed-moe-ds4-aligned.dat`，其条目仅覆盖路由专家（`ffn_*_exps`）；因此 LoRA 改动的 attention / 共享专家 / indexer 等层不会经过 imatrix 校准。
- `--allow-requantize` 必填：路由专家的 up/gate 原生为 MXFP4，需允许重新量化才能改为 Q2_K。
- `--tensor-type` 使用正则匹配（`.` 需转义为 `\.`），否则会误伤 `attn_q_a_norm` 等归一化层。
- `ffn_down_exps` 必须显式指定 `MXFP4`，否则默认会被量化成其他类型，破坏原生 FP4 权重。

## License
Same as DeepSeek-V4-Flash.
