Safetensors
deepseek_ocr2
pnevskaiaan commited on
Commit
305824b
·
verified ·
1 Parent(s): 4134eaa

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +77 -0
README.md CHANGED
@@ -1,3 +1,80 @@
1
  ---
2
  license: apache-2.0
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: apache-2.0
3
  ---
4
+
5
+ ```python
6
+
7
+ import os
8
+
9
+ import torch
10
+
11
+
12
+ torch.set_default_dtype(torch.float32)
13
+
14
+ from transformers import AutoConfig, AutoProcessor
15
+ from transformers.models.deepseek_ocr2 import DeepseekOcr2ForConditionalGeneration
16
+
17
+
18
+ # === Step 1: Load and shrink the original config ===
19
+ MODEL_ID = "deepseek-community/DeepSeek-OCR-2"
20
+ OUTPUT_DIR = "./tiny-deepseek-ocr-2"
21
+
22
+ print(f"Loading config from {MODEL_ID}...")
23
+ config = AutoConfig.from_pretrained(MODEL_ID)
24
+
25
+ # Tiny DeepseekV2 (MoE) text decoder (reduced from hidden_size=1280, 12 layers, 10 heads).
26
+ text_config = config.text_config
27
+ text_config.hidden_size = 128 # from 1280
28
+ text_config.intermediate_size = 256 # from 6848 (dense MLP)
29
+ text_config.moe_intermediate_size = 128 # from 896 (per-expert MLP)
30
+ text_config.num_hidden_layers = 2 # from 12
31
+ text_config.num_attention_heads = 4 # from 10
32
+ text_config.num_key_value_heads = 4 # from 10
33
+ text_config.head_dim = 32 # from 128
34
+ text_config.n_routed_experts = 4 # from 64
35
+ text_config.num_experts_per_tok = 2 # from 6
36
+ text_config.n_shared_experts = 1 # from 2
37
+ # First layer stays dense, the rest are MoE ("sparse"); length must equal num_hidden_layers.
38
+ text_config.mlp_layer_types = ["dense", "sparse"]
39
+
40
+ # Tiny CLIP-style vision encoder (reduced from hidden_size=896, 24 layers, 14 heads).
41
+ encoder_config = config.vision_config.encoder_config
42
+ encoder_config.hidden_size = 128 # from 896 (must match sam downsample_channels[1] below)
43
+ encoder_config.intermediate_size = 256 # from 4864
44
+ encoder_config.num_hidden_layers = 2 # from 24
45
+ encoder_config.num_attention_heads = 4 # from 14
46
+ encoder_config.num_key_value_heads = 2 # from 2
47
+ encoder_config.layer_types = ["full_attention"] * encoder_config.num_hidden_layers
48
+
49
+ # Tiny SAM ViT vision encoder (reduced from hidden_size=768, 12 layers, 12 heads).
50
+ sam_config = config.vision_config.sam_config
51
+ sam_config.hidden_size = 64 # from 768
52
+ sam_config.num_hidden_layers = 2 # from 12
53
+ sam_config.num_attention_heads = 4 # from 12
54
+ sam_config.mlp_dim = 128 # from 3072
55
+ sam_config.output_channels = 64 # from 256
56
+ sam_config.downsample_channels = [64, 128] # from [512, 896]; last must == encoder hidden_size
57
+ sam_config.global_attn_indexes = [] # windowed attention only, to keep compute small
58
+ # image_size (1024), patch_size (16) and window_size (14) are kept so the preprocessing
59
+ # (global 1024px view + 768px crop tiles) stays compatible with the real model.
60
+
61
+ # === Step 2: Create model from config ===
62
+ print("Creating tiny DeepSeek-OCR-2 model...")
63
+ model = DeepseekOcr2ForConditionalGeneration(config)
64
+ model.eval()
65
+
66
+ total_params = sum(p.numel() for p in model.parameters())
67
+ print(f"Total parameters: {total_params:,} ({total_params * 4 / 1024 / 1024:.2f} MB in float32)")
68
+
69
+ # === Step 3: Load processor (image processor + tokenizer) from the original model ===
70
+ print(f"Loading processor from {MODEL_ID}...")
71
+ processor = AutoProcessor.from_pretrained(MODEL_ID)
72
+
73
+ # === Step 4: Save model and processor ===
74
+ os.makedirs(OUTPUT_DIR, exist_ok=True)
75
+ print(f"Saving tiny model to {OUTPUT_DIR}...")
76
+ model.save_pretrained(OUTPUT_DIR, safe_serialization=False)
77
+ processor.save_pretrained(OUTPUT_DIR)
78
+
79
+ print(f"Done! Tiny DeepSeek-OCR-2 model saved to {OUTPUT_DIR}")
80
+ ```