Update README.md
Browse files
README.md
CHANGED
|
@@ -1,21 +1,108 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
base_model:
|
| 4 |
+
- deepseek-ai/DeepSeek-OCR
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
# Model Overview
|
| 8 |
+
|
| 9 |
+
- **Model Architecture:** DeepSeek-OCR
|
| 10 |
+
- **Input:** Text
|
| 11 |
+
- **Output:** Text
|
| 12 |
+
- **Supported Hardware Microarchitecture:** AMD MI350/MI355
|
| 13 |
+
- **ROCm:** 7.1.0
|
| 14 |
+
- **Operating System(s):** Linux
|
| 15 |
+
- **Inference Engine:** [vLLM](https://docs.vllm.ai/en/latest/)
|
| 16 |
+
- **Model Optimizer:** [AMD-Quark](https://quark.docs.amd.com/latest/index.html) (V0.11)
|
| 17 |
+
- **Weight quantization:** Language model, MoE only, OCP MXFP4, Static
|
| 18 |
+
- **Activation quantization:** Language model, MoE only, OCP MXFP4, Dynamic
|
| 19 |
+
- **Calibration Dataset:** [Pile](https://huggingface.co/datasets/mit-han-lab/pile-val-backup)
|
| 20 |
+
|
| 21 |
+
This model was built with DeepSeek-OCR model by applying AMD-Quark for MXFP4 quantization.
|
| 22 |
+
|
| 23 |
+
# Model Quantization
|
| 24 |
+
|
| 25 |
+
The model was quantized from [amd/DeepSeek-OCR](https://huggingface.co/amd/DeepSeek-OCR) using [AMD-Quark](https://quark.docs.amd.com/latest/index.html). The weights and activations are quantized to MXFP4.
|
| 26 |
+
|
| 27 |
+
**Quantization scripts:**
|
| 28 |
+
|
| 29 |
+
Note that deepseek_vl_v2 is not in the built-in model template list in Quark V0.11, it has to be registered before quantization.
|
| 30 |
+
|
| 31 |
+
```python
|
| 32 |
+
import torch
|
| 33 |
+
from transformers import AutoModel, AutoTokenizer, AutoProcessor
|
| 34 |
+
from quark.torch import LLMTemplate, ModelQuantizer, export_safetensors
|
| 35 |
+
from datasets import load_dataset
|
| 36 |
+
from quark.contrib.llm_eval import ppl_eval
|
| 37 |
+
|
| 38 |
+
# Register DeepSeek-OCR template
|
| 39 |
+
deepseek_ocr_template = LLMTemplate(
|
| 40 |
+
model_type="deepseek_vl_v2",
|
| 41 |
+
kv_layers_name=["*k_proj", "*v_proj"],
|
| 42 |
+
q_layer_name="*q_proj",
|
| 43 |
+
exclude_layers_name=["lm_head", "model.sam_model*", "model.vision_model*", "model.projector*"],
|
| 44 |
+
)
|
| 45 |
+
LLMTemplate.register_template(deepseek_ocr_template)
|
| 46 |
+
|
| 47 |
+
# Configuration
|
| 48 |
+
ckpt_path = "amd/DeepSeek-OCR"
|
| 49 |
+
output_dir = "amd/DeepSeek-OCR-MXFP4"
|
| 50 |
+
quant_scheme = "mxfp4"
|
| 51 |
+
exclude_layers = ["*self_attn*", "*mlp.gate", "lm_head", "*mlp.gate_proj", "*mlp.up_proj",
|
| 52 |
+
"*mlp.down_proj", "*shared_experts.*", "*sam_model*", "*vision_model*", "*projector*"]
|
| 53 |
+
|
| 54 |
+
# Load model
|
| 55 |
+
model = AutoModel.from_pretrained(ckpt_path, use_safetensors=True, trust_remote_code=True,
|
| 56 |
+
_attn_implementation='flash_attention_2', device_map="cuda:0", torch_dtype=torch.bfloat16)
|
| 57 |
+
model.eval()
|
| 58 |
+
tokenizer = AutoTokenizer.from_pretrained(ckpt_path, trust_remote_code=True)
|
| 59 |
+
processor = AutoProcessor.from_pretrained(ckpt_path, trust_remote_code=True)
|
| 60 |
+
|
| 61 |
+
# Get quant config from template
|
| 62 |
+
template = LLMTemplate.get(model.config.model_type)
|
| 63 |
+
quant_config = template.get_config(scheme=quant_scheme, exclude_layers=exclude_layers)
|
| 64 |
+
|
| 65 |
+
# Quantize
|
| 66 |
+
quantizer = ModelQuantizer(quant_config)
|
| 67 |
+
model = quantizer.quantize_model(model)
|
| 68 |
+
model = quantizer.freeze(model)
|
| 69 |
+
|
| 70 |
+
# Export hf_format
|
| 71 |
+
export_safetensors(model, output_dir, custom_mode="quark")
|
| 72 |
+
tokenizer.save_pretrained(output_dir)
|
| 73 |
+
processor.save_pretrained(output_dir)
|
| 74 |
+
|
| 75 |
+
# Evaluate PPL (optional)
|
| 76 |
+
testdata = load_dataset("wikitext", "wikitext-2-raw-v1", split="test")
|
| 77 |
+
testenc = tokenizer("\n\n".join(testdata["text"]), return_tensors="pt")
|
| 78 |
+
ppl = ppl_eval(model, testenc, model.device)
|
| 79 |
+
print(f"Perplexity: {ppl.item()}")
|
| 80 |
+
```
|
| 81 |
+
|
| 82 |
+
### Perplexity
|
| 83 |
+
|
| 84 |
+
<table>
|
| 85 |
+
<tr>
|
| 86 |
+
<td><strong>Benchmark</strong>
|
| 87 |
+
</td>
|
| 88 |
+
<td><strong>DeepSeek-OCR </strong>
|
| 89 |
+
</td>
|
| 90 |
+
<td><strong>DeepSeek-OCR-MXFP4(this model) </strong>
|
| 91 |
+
</td>
|
| 92 |
+
<td><strong>Recovery</strong>
|
| 93 |
+
</td>
|
| 94 |
+
</tr>
|
| 95 |
+
<tr>
|
| 96 |
+
<td>ppl
|
| 97 |
+
</td>
|
| 98 |
+
<td>11.178650856018066
|
| 99 |
+
</td>
|
| 100 |
+
<td>11.88680648803711
|
| 101 |
+
</td>
|
| 102 |
+
<td>94.04%
|
| 103 |
+
</td>
|
| 104 |
+
</tr>
|
| 105 |
+
</table>
|
| 106 |
+
|
| 107 |
+
# License
|
| 108 |
+
Modifications Copyright(c) 2025 Advanced Micro Devices, Inc. All rights reserved.
|