yujiepan commited on
Commit
c5752e3
·
verified ·
1 Parent(s): d53dcf4

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ tokenizer.json filter=lfs diff=lfs merge=lfs -text
.meta/create_model.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def create_model(source_model_id: str, source_revision: str, save_folder: str) -> str:
2
+ import json
3
+ from pathlib import Path
4
+
5
+ import torch
6
+ from huggingface_hub import hf_hub_download
7
+ from safetensors.torch import save_file
8
+ from transformers import AutoConfig, AutoModelForImageTextToText, set_seed
9
+
10
+ folder = Path(save_folder)
11
+ folder.mkdir(parents=True, exist_ok=True)
12
+ config = json.loads(
13
+ Path(hf_hub_download(source_model_id, "config.json", revision=source_revision)).read_text(encoding="utf-8")
14
+ )
15
+ text = config["text_config"]
16
+ text.update(
17
+ hidden_size=16,
18
+ intermediate_size=32,
19
+ head_dim=256,
20
+ num_hidden_layers=8,
21
+ num_attention_heads=8,
22
+ num_key_value_heads=2,
23
+ linear_key_head_dim=128,
24
+ linear_value_head_dim=128,
25
+ linear_num_key_heads=16,
26
+ linear_num_value_heads=16,
27
+ layer_types=["linear_attention", "linear_attention", "linear_attention", "full_attention"] * 2,
28
+ )
29
+ config["vision_config"].update(
30
+ hidden_size=128,
31
+ intermediate_size=256,
32
+ num_hidden_layers=4,
33
+ num_attention_heads=4,
34
+ )
35
+ config["insert_layer_id"] = 2
36
+ (folder / "config.json").write_text(json.dumps(config, indent=2) + "\n", encoding="utf-8")
37
+ for name in (
38
+ "chat_template.jinja",
39
+ "generation_config.json",
40
+ "preprocessor_config.json",
41
+ "processor_config.json",
42
+ "tokenizer.json",
43
+ "tokenizer_config.json",
44
+ ):
45
+ (folder / name).write_bytes(Path(hf_hub_download(source_model_id, name, revision=source_revision)).read_bytes())
46
+
47
+ previous_dtype = torch.get_default_dtype()
48
+ try:
49
+ torch.set_default_dtype(torch.bfloat16)
50
+ set_seed(42)
51
+ model = AutoModelForImageTextToText.from_config(AutoConfig.from_pretrained(folder))
52
+ with torch.no_grad():
53
+ for name, parameter in sorted(model.named_parameters()):
54
+ if name.endswith("A_log"):
55
+ parameter.zero_()
56
+ elif name.endswith("dt_bias"):
57
+ parameter.fill_(1)
58
+ elif name.endswith("weight") and "norm" in name:
59
+ parameter.fill_(1)
60
+ elif name.endswith("bias"):
61
+ parameter.zero_()
62
+ else:
63
+ parameter.normal_(mean=0.0, std=0.02)
64
+ state = {name: value.detach().cpu().contiguous() for name, value in model.state_dict().items()}
65
+ # The source ties the language-model head to the token table and does not
66
+ # serialize a separate `lm_head.weight`.
67
+ state.pop("lm_head.weight", None)
68
+ save_file(state, folder / "model.safetensors", metadata={"format": "pt"})
69
+ finally:
70
+ torch.set_default_dtype(previous_dtype)
71
+ (folder / "config.json").write_text(json.dumps(config, indent=2) + "\n", encoding="utf-8")
72
+ (folder / "generation_config.json").write_bytes(
73
+ Path(hf_hub_download(source_model_id, "generation_config.json", revision=source_revision)).read_bytes()
74
+ )
75
+ return str(model)
76
+
77
+
78
+ create_model(
79
+ source_model_id="openbmb/MiniCPM-OCR-1.0",
80
+ source_revision="c5833cddb363aa62f1cb60ddde48c832e8f1a92c",
81
+ save_folder="/tmp/minicpm-ocr-1.0",
82
+ )
.meta/env.json ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "source_model": {
3
+ "name": "openbmb/MiniCPM-OCR-1.0",
4
+ "commit_id": "c5833cddb363aa62f1cb60ddde48c832e8f1a92c"
5
+ },
6
+ "environments": {
7
+ "creation": {
8
+ "python": "3.12.13",
9
+ "packages": {
10
+ "huggingface_hub": "2.3.0",
11
+ "safetensors": "0.8.0",
12
+ "tokenizers": "0.23.3",
13
+ "torch": "2.13.0+cu126",
14
+ "transformers": "5.22.0.dev0"
15
+ },
16
+ "cuda": "12.6"
17
+ },
18
+ "transformers": {
19
+ "python": "3.11.2",
20
+ "packages": {
21
+ "torch": "2.7.1",
22
+ "transformers": "5.22.1",
23
+ "tokenizers": "0.23.3",
24
+ "huggingface_hub": "2.4.0",
25
+ "safetensors": "0.8.0",
26
+ "Pillow": "11.3.0",
27
+ "numpy": "2.3.2"
28
+ },
29
+ "cuda": "12.6",
30
+ "hardware": {
31
+ "gpu_model": "NVIDIA A800-SXM4-80GB"
32
+ }
33
+ }
34
+ }
35
+ }
.meta/model.log ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MiniCPMOcrForConditionalGeneration(
2
+ (model): MiniCPMOcrModel(
3
+ (vision_tower): MiniCPMOcrVisionModel(
4
+ (embeddings): MiniCPMOcrVisionEmbeddings(
5
+ (patch_embedding): Conv2d(3, 128, kernel_size=(14, 14), stride=(14, 14), padding=valid)
6
+ (position_embedding): Embedding(4900, 128)
7
+ )
8
+ (encoder): MiniCPMOcrVisionEncoder(
9
+ (layers): ModuleList(
10
+ (0-3): 4 x MiniCPMOcrVisionEncoderLayer(
11
+ (layer_norm1): LayerNorm((128,), eps=1e-06, elementwise_affine=True, bias=True)
12
+ (self_attn): MiniCPMOcrVisionAttention(
13
+ (k_proj): Linear(in_features=128, out_features=128, bias=True)
14
+ (v_proj): Linear(in_features=128, out_features=128, bias=True)
15
+ (q_proj): Linear(in_features=128, out_features=128, bias=True)
16
+ (out_proj): Linear(in_features=128, out_features=128, bias=True)
17
+ )
18
+ (layer_norm2): LayerNorm((128,), eps=1e-06, elementwise_affine=True, bias=True)
19
+ (mlp): MiniCPMOcrVisionMLP(
20
+ (activation_fn): GELUTanh()
21
+ (fc1): Linear(in_features=128, out_features=256, bias=True)
22
+ (fc2): Linear(in_features=256, out_features=128, bias=True)
23
+ )
24
+ )
25
+ )
26
+ )
27
+ (post_layernorm): LayerNorm((128,), eps=1e-06, elementwise_affine=True, bias=True)
28
+ (vit_merger): MiniCPMOcrViTWindowAttentionMerger(
29
+ (self_attn): MiniCPMOcrVisionAttention(
30
+ (k_proj): Linear(in_features=128, out_features=128, bias=True)
31
+ (v_proj): Linear(in_features=128, out_features=128, bias=True)
32
+ (q_proj): Linear(in_features=128, out_features=128, bias=True)
33
+ (out_proj): Linear(in_features=128, out_features=128, bias=True)
34
+ )
35
+ (layer_norm1): LayerNorm((128,), eps=1e-06, elementwise_affine=True, bias=True)
36
+ (pre_norm): LayerNorm((512,), eps=1e-06, elementwise_affine=True, bias=True)
37
+ (linear_1): Linear(in_features=512, out_features=1024, bias=True)
38
+ (act): GELUTanh()
39
+ (linear_2): Linear(in_features=1024, out_features=128, bias=True)
40
+ )
41
+ )
42
+ (language_model): Qwen3_5TextModel(
43
+ (embed_tokens): Embedding(248100, 16)
44
+ (layers): ModuleList(
45
+ (0-2): 3 x Qwen3_5DecoderLayer(
46
+ (linear_attn): Qwen3_5GatedDeltaNet(
47
+ (conv1d): Conv1d(6144, 6144, kernel_size=(4,), stride=(1,), padding=(3,), groups=6144, bias=False)
48
+ (norm): Qwen3_5RMSNormGated()
49
+ (out_proj): Linear(in_features=2048, out_features=16, bias=False)
50
+ (in_proj_qkv): Linear(in_features=16, out_features=6144, bias=False)
51
+ (in_proj_z): Linear(in_features=16, out_features=2048, bias=False)
52
+ (in_proj_b): Linear(in_features=16, out_features=16, bias=False)
53
+ (in_proj_a): Linear(in_features=16, out_features=16, bias=False)
54
+ )
55
+ (mlp): Qwen3_5MLP(
56
+ (gate_proj): Linear(in_features=16, out_features=32, bias=False)
57
+ (up_proj): Linear(in_features=16, out_features=32, bias=False)
58
+ (down_proj): Linear(in_features=32, out_features=16, bias=False)
59
+ (act_fn): SiLUActivation()
60
+ )
61
+ (input_layernorm): Qwen3_5RMSNorm((16,), eps=1e-06)
62
+ (post_attention_layernorm): Qwen3_5RMSNorm((16,), eps=1e-06)
63
+ )
64
+ (3): Qwen3_5DecoderLayer(
65
+ (self_attn): Qwen3_5Attention(
66
+ (q_proj): Linear(in_features=16, out_features=4096, bias=False)
67
+ (k_proj): Linear(in_features=16, out_features=512, bias=False)
68
+ (v_proj): Linear(in_features=16, out_features=512, bias=False)
69
+ (o_proj): Linear(in_features=2048, out_features=16, bias=False)
70
+ (q_norm): Qwen3_5RMSNorm((256,), eps=1e-06)
71
+ (k_norm): Qwen3_5RMSNorm((256,), eps=1e-06)
72
+ )
73
+ (mlp): Qwen3_5MLP(
74
+ (gate_proj): Linear(in_features=16, out_features=32, bias=False)
75
+ (up_proj): Linear(in_features=16, out_features=32, bias=False)
76
+ (down_proj): Linear(in_features=32, out_features=16, bias=False)
77
+ (act_fn): SiLUActivation()
78
+ )
79
+ (input_layernorm): Qwen3_5RMSNorm((16,), eps=1e-06)
80
+ (post_attention_layernorm): Qwen3_5RMSNorm((16,), eps=1e-06)
81
+ )
82
+ (4-6): 3 x Qwen3_5DecoderLayer(
83
+ (linear_attn): Qwen3_5GatedDeltaNet(
84
+ (conv1d): Conv1d(6144, 6144, kernel_size=(4,), stride=(1,), padding=(3,), groups=6144, bias=False)
85
+ (norm): Qwen3_5RMSNormGated()
86
+ (out_proj): Linear(in_features=2048, out_features=16, bias=False)
87
+ (in_proj_qkv): Linear(in_features=16, out_features=6144, bias=False)
88
+ (in_proj_z): Linear(in_features=16, out_features=2048, bias=False)
89
+ (in_proj_b): Linear(in_features=16, out_features=16, bias=False)
90
+ (in_proj_a): Linear(in_features=16, out_features=16, bias=False)
91
+ )
92
+ (mlp): Qwen3_5MLP(
93
+ (gate_proj): Linear(in_features=16, out_features=32, bias=False)
94
+ (up_proj): Linear(in_features=16, out_features=32, bias=False)
95
+ (down_proj): Linear(in_features=32, out_features=16, bias=False)
96
+ (act_fn): SiLUActivation()
97
+ )
98
+ (input_layernorm): Qwen3_5RMSNorm((16,), eps=1e-06)
99
+ (post_attention_layernorm): Qwen3_5RMSNorm((16,), eps=1e-06)
100
+ )
101
+ (7): Qwen3_5DecoderLayer(
102
+ (self_attn): Qwen3_5Attention(
103
+ (q_proj): Linear(in_features=16, out_features=4096, bias=False)
104
+ (k_proj): Linear(in_features=16, out_features=512, bias=False)
105
+ (v_proj): Linear(in_features=16, out_features=512, bias=False)
106
+ (o_proj): Linear(in_features=2048, out_features=16, bias=False)
107
+ (q_norm): Qwen3_5RMSNorm((256,), eps=1e-06)
108
+ (k_norm): Qwen3_5RMSNorm((256,), eps=1e-06)
109
+ )
110
+ (mlp): Qwen3_5MLP(
111
+ (gate_proj): Linear(in_features=16, out_features=32, bias=False)
112
+ (up_proj): Linear(in_features=16, out_features=32, bias=False)
113
+ (down_proj): Linear(in_features=32, out_features=16, bias=False)
114
+ (act_fn): SiLUActivation()
115
+ )
116
+ (input_layernorm): Qwen3_5RMSNorm((16,), eps=1e-06)
117
+ (post_attention_layernorm): Qwen3_5RMSNorm((16,), eps=1e-06)
118
+ )
119
+ )
120
+ (norm): Qwen3_5RMSNorm((16,), eps=1e-06)
121
+ (rotary_emb): Qwen3_5TextRotaryEmbedding()
122
+ )
123
+ (merger): MiniCPMOcrMerger(
124
+ (mlp): ModuleList(
125
+ (0): MiniCPMOcrDownsampleMLP(
126
+ (pre_norm): LayerNorm((512,), eps=1e-06, elementwise_affine=True, bias=True)
127
+ (linear_1): Linear(in_features=512, out_features=512, bias=True)
128
+ (act): GELU(approximate='none')
129
+ (linear_2): Linear(in_features=512, out_features=16, bias=True)
130
+ )
131
+ )
132
+ )
133
+ )
134
+ (lm_head): Linear(in_features=16, out_features=248100, bias=False)
135
+ )
README.md ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ pipeline_tag: image-text-to-text
4
+ base_model:
5
+ - openbmb/MiniCPM-OCR-1.0
6
+ ---
7
+
8
+ This is a randomly initialized tiny model based on [openbmb/MiniCPM-OCR-1.0](https://huggingface.co/openbmb/MiniCPM-OCR-1.0) for testing and debugging.
9
+
10
+ | Feature | Source | This repo |
11
+ |---|---|---|
12
+ | Checkpoint tensor elements | 1.30B BF16 | 7.58M BF16 |
13
+ | Safetensors weight files | 2.60 GB (model.safetensors) | 15.18 MB (model.safetensors) |
14
+ | Architecture / input | MiniCPM-OCR; text and document image | Same |
15
+ | Vocabulary rows / tied embeddings | 248,100 / tied | Same |
16
+ | Backbone layers / layer cadence | 24 / six 3-linear + 1-full cycles | 8 / two complete cycles |
17
+ | Hidden width / MLP width | 1,024 / 3,584 | 16 / 32 |
18
+ | Q heads / KV heads / head dimension | 8 / 2 / 256 | Same |
19
+ | Linear-attention key/value heads and dimensions | 16/16, 128/128 | Same |
20
+ | Vision layers / width / insert merger | 27 / 1,152 / after layer 6 | 4 / 128 / after layer 2 |
21
+ | Visual downsampling | 4x | Same |
22
+ | M-RoPE | sections 11/11/10; partial 0.25 | sections 11/11/10; partial 0.25 |
23
+ | MTP | disabled | Same |
24
+ | Checkpoint precision | BF16 | Same |
25
+
26
+ The tokenizer embedding and output head stay tied, so the checkpoint contains one BF16 token table. Two complete linear/linear/linear/full cycles are retained. The Qwen3.5 linear-attention path requires CUDA and Triton. The exact public snippet was verified on an NVIDIA A800-SXM4-80GB.
27
+
28
+ | File path | Size |
29
+ |------|------|
30
+ | model.safetensors | 15.2MB |
31
+
32
+ ### Example usage
33
+
34
+ #### Transformers (✅ verified)
35
+
36
+ The Qwen3.5 linear-attention path requires CUDA and Triton. Install `transformers[torch]>=5.21.0` and `Pillow`.
37
+
38
+ ```python
39
+ def run_transformers(model_id: str):
40
+ import torch
41
+ from PIL import Image, ImageDraw
42
+ from transformers import AutoModelForImageTextToText, AutoProcessor
43
+
44
+ model_id = str(model_id)
45
+ processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
46
+ model = AutoModelForImageTextToText.from_pretrained(
47
+ model_id,
48
+ dtype=torch.bfloat16,
49
+ device_map="cuda",
50
+ attn_implementation="sdpa",
51
+ )
52
+ image = Image.new("RGB", (448, 448), "white")
53
+ ImageDraw.Draw(image).text((20, 20), "Hello OCR", fill="black")
54
+ messages = [
55
+ {
56
+ "role": "user",
57
+ "content": [
58
+ {"type": "image", "image": image},
59
+ {"type": "text", "text": "Read this document."},
60
+ ],
61
+ }
62
+ ]
63
+ inputs = processor.apply_chat_template(
64
+ messages,
65
+ tokenize=True,
66
+ add_generation_prompt=True,
67
+ return_dict=True,
68
+ return_tensors="pt",
69
+ downsample_mode="4x",
70
+ max_slice_nums=1,
71
+ use_image_id=False,
72
+ ).to(model.device)
73
+ with torch.no_grad():
74
+ logits = model(**inputs).logits
75
+ assert torch.isfinite(logits).all()
76
+ output = model.generate(**inputs, downsample_mode="4x", do_sample=False, max_new_tokens=10)
77
+ print(processor.decode(output[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True))
78
+
79
+
80
+ if __name__ == "__main__":
81
+ run_transformers(model_id="tiny-random/minicpm-ocr-1.0")
82
+ ```
83
+
84
+ ### Creating this repo
85
+
86
+ - Code: [.meta/create_model.py](.meta/create_model.py)
87
+ - Model structure: [.meta/model.log](.meta/model.log)
88
+ - Environment: [.meta/env.json](.meta/env.json)
89
+
90
+ ### Change log
91
+
92
+ - 2026-11-01: Initial tiny MiniCPM-OCR 1.0 checkpoint.
93
+
94
+ *This repo is generated by DeepSeek-V4.1-Flash.*
chat_template.jinja ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- if enable_thinking is not defined -%}
2
+ {%- set enable_thinking = false -%}
3
+ {%- endif -%}
4
+ {%- macro render_content(content, is_system_content=false) -%}
5
+ {%- if content is string -%}
6
+ {{- content -}}
7
+ {%- elif content is iterable and content is not mapping -%}
8
+ {%- set ns = namespace(parts=[]) -%}
9
+ {%- for item in content -%}
10
+ {%- if 'image' in item or 'image_url' in item or item.type == 'image' -%}
11
+ {%- if is_system_content -%}
12
+ {{- raise_exception('System message cannot contain images.') -}}
13
+ {%- endif -%}
14
+ {%- set ns.parts = ns.parts + ['<|image_pad|>'] -%}
15
+ {%- elif 'video' in item or item.type == 'video' -%}
16
+ {%- if is_system_content -%}
17
+ {{- raise_exception('System message cannot contain videos.') -}}
18
+ {%- endif -%}
19
+ {%- set ns.parts = ns.parts + ['<|video_pad|>'] -%}
20
+ {%- elif 'text' in item -%}
21
+ {%- set ns.parts = ns.parts + [item.text] -%}
22
+ {%- else -%}
23
+ {{- raise_exception('Unexpected item type in content.') -}}
24
+ {%- endif -%}
25
+ {%- endfor -%}
26
+ {{- ns.parts | join('\n') -}}
27
+ {%- elif content is none or content is undefined -%}
28
+ {{- '' -}}
29
+ {%- else -%}
30
+ {{- raise_exception('Unexpected content type.') -}}
31
+ {%- endif -%}
32
+ {%- endmacro -%}
33
+ {%- if not messages %}
34
+ {{- raise_exception('No messages provided.') }}
35
+ {%- endif %}
36
+ {%- if tools and tools is iterable and tools is not mapping %}
37
+ {{- '<|im_start|>system\n' }}
38
+ {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
39
+ {%- for tool in tools %}
40
+ {{- "\n" }}
41
+ {{- tool | tojson }}
42
+ {%- endfor %}
43
+ {{- "\n</tools>" }}
44
+ {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
45
+ {%- if messages[0].role == 'system' %}
46
+ {%- set content = render_content(messages[0].content, true)|trim %}
47
+ {%- if content %}
48
+ {{- '\n\n' + content }}
49
+ {%- endif %}
50
+ {%- endif %}
51
+ {{- '<|im_end|>\n' }}
52
+ {%- else %}
53
+ {%- if messages[0].role == 'system' %}
54
+ {%- set content = render_content(messages[0].content, true)|trim %}
55
+ {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
56
+ {%- endif %}
57
+ {%- endif %}
58
+ {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
59
+ {%- for message in messages[::-1] %}
60
+ {%- set index = (messages|length - 1) - loop.index0 %}
61
+ {%- if ns.multi_step_tool and message.role == "user" %}
62
+ {%- set content = render_content(message.content)|trim %}
63
+ {%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
64
+ {%- set ns.multi_step_tool = false %}
65
+ {%- set ns.last_query_index = index %}
66
+ {%- endif %}
67
+ {%- endif %}
68
+ {%- endfor %}
69
+ {%- if ns.multi_step_tool %}
70
+ {{- raise_exception('No user query found in messages.') }}
71
+ {%- endif %}
72
+ {%- for message in messages %}
73
+ {%- set content = render_content(message.content)|trim %}
74
+ {%- if message.role == "system" %}
75
+ {%- if not loop.first %}
76
+ {{- raise_exception('System message must be at the beginning.') }}
77
+ {%- endif %}
78
+ {%- elif message.role == "user" %}
79
+ {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
80
+ {%- elif message.role == "assistant" %}
81
+ {%- set reasoning_content = '' %}
82
+ {%- if message.reasoning_content is string %}
83
+ {%- set reasoning_content = message.reasoning_content %}
84
+ {%- else %}
85
+ {%- if '</think>' in content %}
86
+ {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
87
+ {%- set content = content.split('</think>')[-1].lstrip('\n') %}
88
+ {%- endif %}
89
+ {%- endif %}
90
+ {%- set reasoning_content = reasoning_content|trim %}
91
+ {%- if loop.index0 > ns.last_query_index and reasoning_content %}
92
+ {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
93
+ {%- else %}
94
+ {{- '<|im_start|>' + message.role + '\n<think>\n\n</think>\n\n' + content }}
95
+ {%- endif %}
96
+ {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
97
+ {%- for tool_call in message.tool_calls %}
98
+ {%- if tool_call.function is defined %}
99
+ {%- set tool_call = tool_call.function %}
100
+ {%- endif %}
101
+ {%- if loop.first %}
102
+ {%- if content|trim %}
103
+ {{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
104
+ {%- else %}
105
+ {{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}
106
+ {%- endif %}
107
+ {%- else %}
108
+ {{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
109
+ {%- endif %}
110
+ {%- if tool_call.arguments is defined %}
111
+ {%- for args_name, args_value in tool_call.arguments|items %}
112
+ {{- '<parameter=' + args_name + '>\n' }}
113
+ {%- set args_value = args_value | tojson | safe if args_value is mapping or (args_value is sequence and args_value is not string) else args_value | string %}
114
+ {{- args_value }}
115
+ {{- '\n</parameter>\n' }}
116
+ {%- endfor %}
117
+ {%- endif %}
118
+ {{- '</function>\n</tool_call>' }}
119
+ {%- endfor %}
120
+ {%- endif %}
121
+ {{- '<|im_end|>\n' }}
122
+ {%- elif message.role == "tool" %}
123
+ {%- if loop.previtem and loop.previtem.role != "tool" %}
124
+ {{- '<|im_start|>user' }}
125
+ {%- endif %}
126
+ {{- '\n<tool_response>\n' }}
127
+ {{- content }}
128
+ {{- '\n</tool_response>' }}
129
+ {%- if not loop.last and loop.nextitem.role != "tool" %}
130
+ {{- '<|im_end|>\n' }}
131
+ {%- elif loop.last %}
132
+ {{- '<|im_end|>\n' }}
133
+ {%- endif %}
134
+ {%- else %}
135
+ {{- raise_exception('Unexpected message role.') }}
136
+ {%- endif %}
137
+ {%- endfor %}
138
+ {%- if add_generation_prompt %}
139
+ {{- '<|im_start|>assistant\n' }}
140
+ {%- if enable_thinking is defined and enable_thinking is true %}
141
+ {{- '<think>\n' }}
142
+ {%- else %}
143
+ {{- '<think>\n\n</think>\n\n' }}
144
+ {%- endif %}
145
+ {%- endif %}
config.json ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "MiniCPMOcrForConditionalGeneration"
4
+ ],
5
+ "downsample_mode": "4x",
6
+ "model_type": "minicpm_ocr",
7
+ "tie_word_embeddings": true,
8
+ "transformers_version": "5.21.0",
9
+ "vision_config": {
10
+ "attention_dropout": 0.0,
11
+ "hidden_act": "gelu_pytorch_tanh",
12
+ "hidden_size": 128,
13
+ "image_size": 980,
14
+ "intermediate_size": 256,
15
+ "layer_norm_eps": 1e-06,
16
+ "model_type": "minicpm_ocr_vision",
17
+ "num_attention_heads": 4,
18
+ "num_channels": 3,
19
+ "num_hidden_layers": 4,
20
+ "patch_size": 14
21
+ },
22
+ "insert_layer_id": 2,
23
+ "image_token_id": 248056,
24
+ "video_token_id": 248057,
25
+ "dtype": "bfloat16",
26
+ "text_config": {
27
+ "attention_bias": false,
28
+ "attention_dropout": 0.0,
29
+ "attn_output_gate": true,
30
+ "full_attention_interval": 4,
31
+ "head_dim": 256,
32
+ "hidden_act": "silu",
33
+ "hidden_size": 16,
34
+ "initializer_range": 0.02,
35
+ "intermediate_size": 32,
36
+ "layer_types": [
37
+ "linear_attention",
38
+ "linear_attention",
39
+ "linear_attention",
40
+ "full_attention",
41
+ "linear_attention",
42
+ "linear_attention",
43
+ "linear_attention",
44
+ "full_attention"
45
+ ],
46
+ "linear_conv_kernel_dim": 4,
47
+ "linear_key_head_dim": 128,
48
+ "linear_num_key_heads": 16,
49
+ "linear_num_value_heads": 16,
50
+ "linear_value_head_dim": 128,
51
+ "mamba_ssm_dtype": "float32",
52
+ "max_position_embeddings": 262144,
53
+ "mlp_only_layers": [],
54
+ "mtp_num_hidden_layers": 0,
55
+ "mtp_use_dedicated_embeddings": false,
56
+ "num_attention_heads": 8,
57
+ "num_hidden_layers": 8,
58
+ "num_key_value_heads": 2,
59
+ "partial_rotary_factor": 0.25,
60
+ "rms_norm_eps": 1e-06,
61
+ "rope_parameters": {
62
+ "partial_rotary_factor": 0.25,
63
+ "rope_theta": 10000000,
64
+ "rope_type": "default"
65
+ },
66
+ "vocab_size": 248100,
67
+ "model_type": "qwen3_5_text",
68
+ "tie_word_embeddings": true
69
+ }
70
+ }
generation_config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token_id": 248045,
3
+ "eos_token_id": [
4
+ 248044,
5
+ 248046
6
+ ],
7
+ "transformers_version": "5.21.0",
8
+ "use_cache": true
9
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2b776367cbaa20f17cab9341fcc42217f51e822f27f59d4a9b8af13907f64341
3
+ size 15175584
preprocessor_config.json ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "downsample_mode": "4x",
3
+ "max_slice_nums": 9,
4
+ "patch_size": 14,
5
+ "scale_resolution": 448,
6
+ "slice_mode": true,
7
+ "use_image_id": true,
8
+ "image_mean": [
9
+ 0.5,
10
+ 0.5,
11
+ 0.5
12
+ ],
13
+ "image_std": [
14
+ 0.5,
15
+ 0.5,
16
+ 0.5
17
+ ],
18
+ "image_processor_type": "MiniCPMOcrImageProcessor",
19
+ "processor_class": "MiniCPMOcrProcessor"
20
+ }
processor_config.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {
2
+ "processor_class": "MiniCPMOcrProcessor"
3
+ }
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6269eb38f25c20c0f2ba676577f2e5603f79eab3be573dfdaa727ca6eeb5522d
3
+ size 19993565
tokenizer_config.json ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_prefix_space": false,
3
+ "audio_bos_token": "<|audio_start|>",
4
+ "audio_eos_token": "<|audio_end|>",
5
+ "audio_token": "<|audio_pad|>",
6
+ "backend": "tokenizers",
7
+ "bos_token": "<|im_start|>",
8
+ "clean_up_tokenization_spaces": false,
9
+ "eos_token": "<|im_end|>",
10
+ "errors": "replace",
11
+ "extra_special_tokens": {
12
+ "image_token": "<|image_pad|>",
13
+ "video_token": "<|video_pad|>",
14
+ "image_start_token": "<image>",
15
+ "image_end_token": "</image>",
16
+ "slice_start_token": "<slice>",
17
+ "slice_end_token": "</slice>",
18
+ "image_id_start_token": "<image_id>",
19
+ "image_id_end_token": "</image_id>"
20
+ },
21
+ "image_token": "<|image_pad|>",
22
+ "model_max_length": 262144,
23
+ "model_specific_special_tokens": {
24
+ "audio_bos_token": "<|audio_start|>",
25
+ "audio_eos_token": "<|audio_end|>",
26
+ "audio_token": "<|audio_pad|>",
27
+ "image_token": "<|image_pad|>",
28
+ "video_token": "<|video_pad|>",
29
+ "vision_bos_token": "<|vision_start|>",
30
+ "vision_eos_token": "<|vision_end|>"
31
+ },
32
+ "pad_token": "<|endoftext|>",
33
+ "pretokenize_regex": "(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\\r\\n\\p{L}\\p{N}]?[\\p{L}\\p{M}]+|\\p{N}| ?[^\\s\\p{L}\\p{M}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+",
34
+ "processor_class": "MiniCPMOcrProcessor",
35
+ "split_special_tokens": false,
36
+ "tokenizer_class": "TokenizersBackend",
37
+ "unk_token": "<unk>",
38
+ "video_token": "<|video_pad|>",
39
+ "vision_bos_token": "<|vision_start|>",
40
+ "vision_eos_token": "<|vision_end|>"
41
+ }