Devopsopraiz dealignai commited on
Commit
0303311
·
0 Parent(s):

Super-squash branch 'main' using huggingface_hub

Browse files

Co-authored-by: dealignai <dealignai@users.noreply.huggingface.co>

This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +36 -0
  2. LICENSE +21 -0
  3. README.md +37 -0
  4. config.json +170 -0
  5. encoding/encoding.py +990 -0
  6. encoding/test_encoding.py +652 -0
  7. encoding/tests/test_input_1.json +82 -0
  8. encoding/tests/test_input_2.json +24 -0
  9. encoding/tests/test_input_3.json +93 -0
  10. encoding/tests/test_input_4.json +28 -0
  11. encoding/tests/test_input_5.json +39 -0
  12. inference/README.md +71 -0
  13. inference/config.json +66 -0
  14. inference/convert.py +205 -0
  15. inference/engram.py +184 -0
  16. inference/examples/example.txt +7 -0
  17. inference/examples/example_harmony.json +96 -0
  18. inference/examples/images/carrots.jpeg +3 -0
  19. inference/examples/images/corn.jpeg +0 -0
  20. inference/generate.py +218 -0
  21. inference/image_processor.py +173 -0
  22. inference/kernel.py +591 -0
  23. inference/model.py +1312 -0
  24. inference/requirements.txt +9 -0
  25. inference/run.sh +54 -0
  26. inference/vision.py +119 -0
  27. model-00001-of-00048.safetensors +3 -0
  28. model-00002-of-00048.safetensors +3 -0
  29. model-00003-of-00048.safetensors +3 -0
  30. model-00004-of-00048.safetensors +3 -0
  31. model-00005-of-00048.safetensors +3 -0
  32. model-00006-of-00048.safetensors +3 -0
  33. model-00007-of-00048.safetensors +3 -0
  34. model-00008-of-00048.safetensors +3 -0
  35. model-00009-of-00048.safetensors +3 -0
  36. model-00010-of-00048.safetensors +3 -0
  37. model-00011-of-00048.safetensors +3 -0
  38. model-00012-of-00048.safetensors +3 -0
  39. model-00013-of-00048.safetensors +3 -0
  40. model-00014-of-00048.safetensors +3 -0
  41. model-00015-of-00048.safetensors +3 -0
  42. model-00016-of-00048.safetensors +3 -0
  43. model-00017-of-00048.safetensors +3 -0
  44. model-00018-of-00048.safetensors +3 -0
  45. model-00019-of-00048.safetensors +3 -0
  46. model-00020-of-00048.safetensors +3 -0
  47. model-00021-of-00048.safetensors +3 -0
  48. model-00022-of-00048.safetensors +3 -0
  49. model-00023-of-00048.safetensors +3 -0
  50. model-00024-of-00048.safetensors +3 -0
.gitattributes ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz 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
+ inference/examples/images/carrots.jpeg filter=lfs diff=lfs merge=lfs -text
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2023 DeepSeek
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
README.md ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ language:
4
+ - en
5
+ - zh
6
+ tags:
7
+ - uncensored
8
+ - moe
9
+ - fp8
10
+ - vision
11
+ ---
12
+
13
+ # Baanzon-Action-1.1-Flash-R
14
+
15
+ Baanzon-Action-1.1-Flash-R is an FP8 Mixture-of-Experts language model published and maintained by **Opraiz Technology Pvt Ltd** under the **Baanzon** brand.
16
+
17
+ ## Identity
18
+
19
+ This model is **Baanzon-Action-1.1-Flash-R**, published by **Opraiz Technology Pvt Ltd**. When deployed behind an API or chat endpoint, configure the serving system prompt as:
20
+
21
+ ```
22
+ You are Baanzon-Action-1.1-Flash-R, developed by Opraiz Technology Pvt Ltd under the Baanzon brand.
23
+ When asked what you are, which model you are, or who made you, always answer:
24
+ Baanzon-Action-1.1-Flash-R by Opraiz Technology Pvt Ltd.
25
+ ```
26
+
27
+ ## Capabilities
28
+
29
+ - Mixture-of-Experts architecture with very long context
30
+ - Vision-capable (image inputs)
31
+ - Reasoning / thinking mode with thinking, tool calling and multi-step agentic workflows
32
+ - Bilingual (English / Chinese) optimized
33
+ - FP8-quantized full weights release (uncensored)
34
+
35
+ ## Serving
36
+
37
+ A self-contained `inference/` reference implementation (conversion, encoding, generation) and a matching `encoding/` chat-encoding package with tests are bundled in this repository.
config.json ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "DeepseekV41ForCausalLM"
4
+ ],
5
+ "model_type": "deepseek_v41",
6
+ "dtype": "bfloat16",
7
+ "transformers_version": "5.6.0",
8
+ "bos_token_id": 0,
9
+ "eos_token_id": 1,
10
+ "pad_token_id": 2,
11
+ "image_token_id": 129264,
12
+ "quantization_config": {
13
+ "quant_method": "fp8",
14
+ "activation_scheme": "dynamic",
15
+ "weight_block_size": [
16
+ 32,
17
+ 32
18
+ ],
19
+ "scale_fmt": "ue8m0",
20
+ "expert_dtype": "fp4"
21
+ },
22
+ "text_config": {
23
+ "model_type": "deepseek_v41_text",
24
+ "vocab_size": 129280,
25
+ "hidden_size": 5120,
26
+ "moe_intermediate_size": 2304,
27
+ "num_hidden_layers": 40,
28
+ "num_attention_heads": 64,
29
+ "num_key_value_heads": 1,
30
+ "head_dim": 512,
31
+ "qk_rope_head_dim": 64,
32
+ "q_lora_rank": 1280,
33
+ "o_lora_rank": 1024,
34
+ "o_groups": 8,
35
+ "hidden_act": "silu",
36
+ "swiglu_limit": 10.0,
37
+ "rms_norm_eps": 1e-20,
38
+ "attention_bias": false,
39
+ "attention_dropout": 0.0,
40
+ "initializer_range": 0.02,
41
+ "use_cache": true,
42
+ "tie_word_embeddings": false,
43
+ "max_position_embeddings": 1048576,
44
+ "rope_theta": 10000,
45
+ "rope_scaling": {
46
+ "rope_type": "yarn",
47
+ "factor": 16,
48
+ "beta_fast": 32,
49
+ "beta_slow": 1,
50
+ "original_max_position_embeddings": 65536
51
+ },
52
+ "n_routed_experts": 384,
53
+ "n_shared_experts": 1,
54
+ "num_experts_per_tok": 6,
55
+ "scoring_func": "sqrtsoftplus",
56
+ "topk_method": "noaux_tc",
57
+ "norm_topk_prob": true,
58
+ "routed_scaling_factor": 1.5,
59
+ "sliding_window": 128,
60
+ "compress_ratios": [
61
+ 0,
62
+ 0,
63
+ 2,
64
+ 2,
65
+ 2,
66
+ 2,
67
+ 2,
68
+ 2,
69
+ 2,
70
+ 2,
71
+ 2,
72
+ 2,
73
+ 2,
74
+ 2,
75
+ 2,
76
+ 2,
77
+ 2,
78
+ 2,
79
+ 2,
80
+ 2,
81
+ 1,
82
+ 1,
83
+ 1,
84
+ 1,
85
+ 1,
86
+ 1,
87
+ 1,
88
+ 1,
89
+ 1,
90
+ 1,
91
+ 1,
92
+ 1,
93
+ 1,
94
+ 1,
95
+ 1,
96
+ 1,
97
+ 1,
98
+ 1,
99
+ 1,
100
+ 1,
101
+ 0,
102
+ 0,
103
+ 0
104
+ ],
105
+ "compress_rope_theta": 160000,
106
+ "kv_source_layer_ids": [
107
+ 2,
108
+ 8,
109
+ 14,
110
+ 20
111
+ ],
112
+ "index_source_layer_ids": [
113
+ 2,
114
+ 8,
115
+ 14,
116
+ 20,
117
+ 24,
118
+ 28,
119
+ 32,
120
+ 36
121
+ ],
122
+ "index_n_heads": 32,
123
+ "index_head_dim": 128,
124
+ "index_topk": 512,
125
+ "candidate_source_layer_id": 20,
126
+ "candidate_topk_blocks": 2048,
127
+ "candidate_block_size": 8,
128
+ "hc_mult": 4,
129
+ "hc_sinkhorn_iters": 20,
130
+ "hc_eps": 1e-06,
131
+ "engram_layer_ids": [
132
+ 1,
133
+ 14
134
+ ],
135
+ "engram_num_embeddings": [
136
+ 384006168,
137
+ 384016682
138
+ ],
139
+ "engram_max_ngram_size": 4,
140
+ "engram_vocab_size": 16000000,
141
+ "engram_n_heads": 8,
142
+ "engram_head_dim": 256,
143
+ "engram_pad_token_id": 2,
144
+ "engram_compressed_vocab_size": 99092,
145
+ "num_nextn_predict_layers": 3,
146
+ "dspark_block_size": 5,
147
+ "dspark_noise_token_id": 128799,
148
+ "dspark_target_layer_ids": [
149
+ 37,
150
+ 38,
151
+ 39
152
+ ],
153
+ "dspark_markov_rank": 256,
154
+ "dspark_n_routed_experts": 128,
155
+ "dspark_num_experts_per_tok": 3
156
+ },
157
+ "vision_config": {
158
+ "model_type": "deepseek_v41_vision",
159
+ "num_hidden_layers": 32,
160
+ "hidden_size": 1024,
161
+ "num_attention_heads": 16,
162
+ "intermediate_size": 2816,
163
+ "patch_size": 14,
164
+ "rope_theta": 10000,
165
+ "downsample_ratio": 3,
166
+ "max_image_tokens": 1024,
167
+ "min_pixels": 295936,
168
+ "max_wh_ratio": null
169
+ }
170
+ }
encoding/encoding.py ADDED
@@ -0,0 +1,990 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Baanzon-Action-1.1 Text and Vision Encoding
3
+
4
+ A fully self-contained implementation for encoding/decoding Baanzon-Action-1.1 chat
5
+ messages with tool calling, thinking mode, quick instruction tasks, and image
6
+ content blocks. No dependency on encoding_dsv4.
7
+
8
+ V4.1 changes relative to V4:
9
+
10
+ 1. DSML tag names: tool calls are wrapped in "<|DSML| calls>" blocks with
11
+ "<|DSML| invoke>" / "<|DSML| parameter>" tags (leading-space tag names).
12
+ 2. Numeric reasoning effort: "Reasoning Effort: {budget} (range 1-100, ...)".
13
+ Accepts an int in [1, 100] or one of "low"/"high"/"max"
14
+ (mapped to 50/75/100). Defaults to "high". Only rendered in thinking mode.
15
+ 3. Mid-conversation system messages are supported via the "<|System|>" token.
16
+ A mid-conversation system message behaves like a user message for the purpose
17
+ of appending the assistant generation header.
18
+ """
19
+
20
+ from typing import Any, Dict, List, Union, Optional, Tuple
21
+ import copy
22
+ import json
23
+ import re
24
+
25
+ # ============================================================
26
+ # Special Tokens
27
+ # ============================================================
28
+
29
+ bos_token: str = "<|begin▁of▁sentence|>"
30
+ eos_token: str = "<|end▁of▁sentence|>"
31
+ thinking_start_token: str = "<think>"
32
+ thinking_end_token: str = "</think>"
33
+ dsml_token: str = "|DSML|"
34
+
35
+ USER_SP_TOKEN = "<|User|>"
36
+ ASSISTANT_SP_TOKEN = "<|Assistant|>"
37
+ LATEST_REMINDER_SP_TOKEN = "<|latest_reminder|>"
38
+
39
+ IMAGE_PLACEHOLDER = "<|deepseek_image|>"
40
+ IMAGE_TAG_PATTERN = re.compile(r"<image>(.*?)</image>", re.DOTALL)
41
+
42
+ # Task special tokens for internal classification tasks
43
+ DS_TASK_SP_TOKENS = {
44
+ "action": "<|action|>",
45
+ "query": "<|query|>",
46
+ "authority": "<|authority|>",
47
+ "domain": "<|domain|>",
48
+ "title": "<|title|>",
49
+ "read_url": "<|read_url|>",
50
+ }
51
+ VALID_TASKS = set(DS_TASK_SP_TOKENS.keys())
52
+
53
+ # ============================================================
54
+ # Templates
55
+ # ============================================================
56
+
57
+ system_msg_template: str = "{content}"
58
+ user_msg_template: str = "{content}"
59
+ latest_reminder_msg_template: str = "{content}"
60
+ assistant_msg_template: str = "{reasoning}{content}{tool_calls}" + eos_token
61
+ assistant_msg_wo_eos_template: str = "{reasoning}{content}{tool_calls}"
62
+ thinking_template: str = "{reasoning_content}"
63
+
64
+ response_format_template: str = (
65
+ "## Response Format:\n\nYou MUST strictly adhere to the following schema to reply:\n{schema}"
66
+ )
67
+
68
+ tool_output_template: str = (
69
+ "<tool_result>{content}</tool_result>"
70
+ )
71
+
72
+ # ============================================================
73
+ # Utility Functions
74
+ # ============================================================
75
+
76
+ def to_json(value: Any) -> str:
77
+ """Serialize a value to JSON string."""
78
+ try:
79
+ return json.dumps(value, ensure_ascii=False)
80
+ except:
81
+ return json.dumps(value, ensure_ascii=True)
82
+
83
+
84
+ def tools_from_openai_format(tools):
85
+ """Extract function definitions with namespace-qualified names."""
86
+ functions = []
87
+ for tool in tools:
88
+ function = dict(tool["function"])
89
+ if tool.get("namespace") is not None:
90
+ function["namespace"] = tool["namespace"]
91
+ function["name"] = _tool_name_for_encoding(function)
92
+ namespace = function.pop("namespace", None)
93
+ if isinstance(namespace, dict) and namespace.get("description"):
94
+ function["description"] = (
95
+ namespace["description"] + "\n" + (function.get("description") or "")
96
+ )
97
+ functions.append(function)
98
+ return functions
99
+
100
+
101
+ def _split_tool_name(name: str, namespace: Optional[str] = None) -> Tuple[Optional[str], str]:
102
+ """Split a qualified name and validate any explicit namespace."""
103
+ prefix, separator, bare_name = name.partition("::")
104
+ if separator:
105
+ assert namespace in (None, prefix), (
106
+ f"Conflicting tool namespaces: {namespace} != {prefix}"
107
+ )
108
+ namespace, name = prefix, bare_name
109
+ assert "::" not in name, f"Tool name must not contain '::': {name}"
110
+ assert namespace is None or "::" not in namespace, (
111
+ f"Tool namespace must not contain '::': {namespace}"
112
+ )
113
+ return namespace, name
114
+
115
+
116
+ def _tool_name_for_encoding(tool: Dict[str, Any]) -> str:
117
+ namespace = tool.get("namespace")
118
+ if isinstance(namespace, dict):
119
+ namespace = namespace["name"]
120
+ namespace, name = _split_tool_name(tool["name"], namespace)
121
+ return name if namespace is None else f"{namespace}::{name}"
122
+
123
+
124
+ def tool_calls_from_openai_format(tool_calls):
125
+ """Convert OpenAI-format tool calls to internal format."""
126
+ calls = []
127
+ for tool_call in tool_calls:
128
+ function = tool_call["function"]
129
+ namespace, name = _split_tool_name(
130
+ function["name"], tool_call.get("namespace") or function.get("namespace")
131
+ )
132
+ call = {"name": name, "arguments": function["arguments"]}
133
+ if namespace is not None:
134
+ call["namespace"] = namespace
135
+ calls.append(call)
136
+ return calls
137
+
138
+
139
+ def tool_calls_to_openai_format(tool_calls):
140
+ """Convert internal tool calls to OpenAI format."""
141
+ calls = []
142
+ for tool_call in tool_calls:
143
+ call = {
144
+ "type": "function",
145
+ "function": {
146
+ "name": tool_call["name"],
147
+ "arguments": tool_call["arguments"],
148
+ }
149
+ }
150
+ if tool_call.get("namespace") is not None:
151
+ call["namespace"] = tool_call["namespace"]
152
+ calls.append(call)
153
+ return calls
154
+
155
+
156
+ def decode_dsml_to_arguments(tool_name: str, tool_args: Dict[str, Tuple[str, str]]) -> Dict[str, str]:
157
+ """
158
+ Decode DSML parameters back to a tool call dict.
159
+
160
+ Args:
161
+ tool_name: Name of the tool.
162
+ tool_args: Dict mapping param_name -> (value, is_string_flag).
163
+
164
+ Returns:
165
+ Dict with "name", "arguments" (JSON string), and optional "namespace".
166
+ """
167
+ def _decode_value(key: str, value: str, string: str):
168
+ if string == "true":
169
+ value = to_json(value)
170
+ return f"{to_json(key)}: {value}"
171
+
172
+ tool_args_json = "{" + ", ".join([_decode_value(k, v, string=is_str) for k, (v, is_str) in tool_args.items()]) + "}"
173
+ namespace, name = _split_tool_name(tool_name)
174
+ tool_call = dict(name=name, arguments=tool_args_json)
175
+ if namespace is not None:
176
+ tool_call["namespace"] = namespace
177
+ return tool_call
178
+
179
+
180
+ # ============================================================
181
+ # Preprocessing
182
+ # ============================================================
183
+
184
+ def merge_tool_messages(messages: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
185
+ """
186
+ Merge tool messages into the preceding user message using content_blocks format.
187
+
188
+ Baanzon-Action-1.1 does not have a standalone "tool" role; instead, tool results
189
+ are encoded as <tool_result> blocks within user messages.
190
+ """
191
+ merged: List[Dict[str, Any]] = []
192
+
193
+ for msg in messages:
194
+ msg = copy.deepcopy(msg)
195
+ role = msg.get("role")
196
+
197
+ if role == "tool":
198
+ # Convert tool message to a user message with tool_result block
199
+ content = msg.get("content", "")
200
+ if msg.get("content_blocks") and not isinstance(msg.get("content"), str):
201
+ content = msg["content_blocks"]
202
+ tool_block = {
203
+ "type": "tool_result",
204
+ "tool_use_id": msg.get("tool_call_id", ""),
205
+ "content": content,
206
+ }
207
+ # Merge into previous message if it's already a user (merged tool)
208
+ if merged and merged[-1].get("role") == "user" and "content_blocks" in merged[-1]:
209
+ merged[-1]["content_blocks"].append(tool_block)
210
+ else:
211
+ merged.append({
212
+ "role": "user",
213
+ "content_blocks": [tool_block],
214
+ })
215
+ elif role == "user":
216
+ content_blocks = msg.get("content_blocks")
217
+ if content_blocks is None:
218
+ content = msg.get("content", "")
219
+ content_blocks = content if isinstance(content, list) else [{"type": "text", "text": content}]
220
+ if merged and merged[-1].get("role") == "user" and "content_blocks" in merged[-1] and merged[-1].get("task") is None:
221
+ merged[-1]["content_blocks"].extend(content_blocks)
222
+ else:
223
+ # Preserve structured content and all message-level metadata.
224
+ new_msg = msg
225
+ new_msg["content_blocks"] = content_blocks
226
+ merged.append(new_msg)
227
+ else:
228
+ merged.append(msg)
229
+
230
+ return merged
231
+
232
+
233
+ def sort_tool_results_by_call_order(messages: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
234
+ """
235
+ Sort tool_result blocks within user messages by the order of tool_calls
236
+ in the preceding assistant message.
237
+ """
238
+ last_tool_call_order: Dict[str, int] = {}
239
+
240
+ for msg in messages:
241
+ role = msg.get("role")
242
+ if role == "assistant" and msg.get("tool_calls"):
243
+ last_tool_call_order = {}
244
+ for idx, tc in enumerate(msg["tool_calls"]):
245
+ tc_id = tc.get("id") or tc.get("function", {}).get("id", "")
246
+ if tc_id:
247
+ last_tool_call_order[tc_id] = idx
248
+
249
+ elif role == "user" and msg.get("content_blocks"):
250
+ tool_blocks = [b for b in msg["content_blocks"] if b.get("type") == "tool_result"]
251
+ if len(tool_blocks) > 1 and last_tool_call_order:
252
+ sorted_blocks = sorted(
253
+ tool_blocks,
254
+ key=lambda b: last_tool_call_order.get(b.get("tool_use_id", ""), 0)
255
+ )
256
+ sorted_idx = 0
257
+ new_blocks = []
258
+ for block in msg["content_blocks"]:
259
+ if block.get("type") == "tool_result":
260
+ new_blocks.append(sorted_blocks[sorted_idx])
261
+ sorted_idx += 1
262
+ else:
263
+ new_blocks.append(block)
264
+ msg["content_blocks"] = new_blocks
265
+
266
+ return messages
267
+
268
+
269
+ # ============================================================
270
+ # Vision Message Preprocessing
271
+ # ============================================================
272
+
273
+ def parse_tagged_text(text: str) -> Union[str, List[Dict[str, Any]]]:
274
+ """Convert ``<image>path</image>`` text into standard content blocks."""
275
+ matches = list(IMAGE_TAG_PATTERN.finditer(text))
276
+ remaining = IMAGE_TAG_PATTERN.sub("", text)
277
+ if "<image>" in remaining or "</image>" in remaining:
278
+ raise ValueError("Malformed <image>path</image> tag")
279
+ if not matches:
280
+ return text
281
+
282
+ blocks: List[Dict[str, Any]] = []
283
+ cursor = 0
284
+ for match in matches:
285
+ if match.start() > cursor:
286
+ blocks.append({"type": "text", "text": text[cursor:match.start()]})
287
+ path = match.group(1)
288
+ if not path:
289
+ raise ValueError("Image path must not be empty")
290
+ blocks.append({
291
+ "type": "image_url",
292
+ "image_url": {"url": path},
293
+ })
294
+ cursor = match.end()
295
+ if cursor < len(text):
296
+ blocks.append({"type": "text", "text": text[cursor:]})
297
+ return blocks
298
+
299
+
300
+ def _is_image_block(block: Dict[str, Any]) -> bool:
301
+ """Return whether a content block is an OpenAI/Anthropic/internal image."""
302
+ return isinstance(block, dict) and block.get("type") in ("image", "image_url")
303
+
304
+
305
+ def _extract_image(block: Dict[str, Any]) -> Dict[str, Any]:
306
+ """Normalize a supported image block into an internal image record."""
307
+ record: Dict[str, Any] = {"type": "image"}
308
+ if block.get("type") == "image_url":
309
+ image_url = block.get("image_url")
310
+ if isinstance(image_url, str):
311
+ record["url"] = image_url
312
+ else:
313
+ record["url"] = (image_url or {}).get("url", "")
314
+ else:
315
+ for key in ("source", "url", "data"):
316
+ if key in block:
317
+ record[key] = block[key]
318
+ if not any(record.get(key) for key in ("source", "url", "data")):
319
+ raise ValueError("Image block does not contain a valid source")
320
+ return record
321
+
322
+
323
+ def _process_image_blocks(
324
+ blocks: List[Any], image_placeholder: str = IMAGE_PLACEHOLDER
325
+ ) -> Tuple[List[Any], List[Dict[str, Any]]]:
326
+ """Replace image blocks and collect their records in one ordered traversal."""
327
+ new_blocks: List[Any] = []
328
+ images: List[Dict[str, Any]] = []
329
+ for block in blocks:
330
+ if not isinstance(block, dict):
331
+ new_blocks.append(block)
332
+ continue
333
+ if _is_image_block(block):
334
+ new_blocks.append({"type": "text", "text": image_placeholder})
335
+ images.append(_extract_image(block))
336
+ elif block.get("type") == "tool_result" and isinstance(block.get("content"), list):
337
+ block = copy.copy(block)
338
+ block["content"], nested_images = _process_image_blocks(
339
+ block["content"], image_placeholder)
340
+ new_blocks.append(block)
341
+ images.extend(nested_images)
342
+ elif block.get("type") == "text":
343
+ text = block.get("text") or ""
344
+ if IMAGE_PLACEHOLDER in text:
345
+ raise ValueError(
346
+ f"Text block contains image placeholder '{IMAGE_PLACEHOLDER}': "
347
+ f"'{text[:100]}'. Images should be separate content blocks."
348
+ )
349
+ new_blocks.append(block)
350
+ else:
351
+ new_blocks.append(block)
352
+ return new_blocks, images
353
+
354
+
355
+ def _validate_no_image_sp_tokens(msg: Dict[str, Any]) -> None:
356
+ """Reject user-supplied image placeholder tokens in textual fields."""
357
+ content = msg.get("content")
358
+ if isinstance(content, str) and IMAGE_PLACEHOLDER in content:
359
+ raise ValueError(
360
+ f"Message content contains image special token '{IMAGE_PLACEHOLDER}'. "
361
+ "Images should be provided as image content blocks."
362
+ )
363
+ reasoning_content = msg.get("reasoning_content")
364
+ if isinstance(reasoning_content, str) and IMAGE_PLACEHOLDER in reasoning_content:
365
+ raise ValueError(
366
+ f"reasoning_content contains image special token '{IMAGE_PLACEHOLDER}'"
367
+ )
368
+
369
+
370
+ def process_image_messages(
371
+ messages: List[Dict[str, Any]],
372
+ ) -> Tuple[List[Dict[str, Any]], List[Dict[str, Any]]]:
373
+ """Normalize image blocks and return their records in prompt order."""
374
+ processed: List[Dict[str, Any]] = []
375
+ images: List[Dict[str, Any]] = []
376
+ for msg in messages:
377
+ msg = copy.deepcopy(msg)
378
+ _validate_no_image_sp_tokens(msg)
379
+
380
+ if isinstance(msg.get("content"), list) and "content_blocks" not in msg:
381
+ msg["content_blocks"] = msg.pop("content")
382
+
383
+ if msg.get("content_blocks"):
384
+ msg["content_blocks"], message_images = _process_image_blocks(
385
+ msg["content_blocks"])
386
+ images.extend(message_images)
387
+ if not isinstance(msg.get("content"), str):
388
+ texts = [
389
+ block.get("text", "")
390
+ for block in msg["content_blocks"]
391
+ if isinstance(block, dict) and block.get("type") == "text"
392
+ ]
393
+ msg["content"] = "\n\n".join(texts)
394
+
395
+ processed.append(msg)
396
+ return processed, images
397
+
398
+
399
+ def _read_until_stop(index: int, text: str, stop: List[str]) -> Tuple[int, str, Optional[str]]:
400
+ """
401
+ Read text from index until one of the stop strings is found.
402
+
403
+ Returns:
404
+ Tuple of (new_index, content_before_stop, matched_stop_string_or_None).
405
+ """
406
+ min_pos = len(text)
407
+ matched_stop = None
408
+
409
+ for s in stop:
410
+ pos = text.find(s, index)
411
+ if pos != -1 and pos < min_pos:
412
+ min_pos = pos
413
+ matched_stop = s
414
+
415
+ if matched_stop:
416
+ content = text[index:min_pos]
417
+ return min_pos + len(matched_stop), content, matched_stop
418
+ else:
419
+ content = text[index:]
420
+ return len(text), content, None
421
+
422
+ # ============================================================
423
+ # V4.1 Special Tokens and DSML Tag Names
424
+ # ============================================================
425
+
426
+ SYSTEM_SP_TOKEN = "<|System|>"
427
+
428
+ tool_calls_block_name: str = " calls"
429
+ tool_call_tag_name: str = " invoke"
430
+ tool_parameter_tag_name: str = " parameter"
431
+
432
+ tool_call_template: str = (
433
+ "<{dsml_token}{tool_call_tag_name} name=\"{name}\">\n{arguments}\n</{dsml_token}{tool_call_tag_name}>"
434
+ )
435
+ tool_calls_template = (
436
+ "<{dsml_token}{tc_block_name}>\n{tool_calls}\n</{dsml_token}{tc_block_name}>"
437
+ )
438
+
439
+ # ============================================================
440
+ # Reasoning Effort (numeric budget)
441
+ # ============================================================
442
+
443
+ REASONING_EFFORT_TEMPLATE = (
444
+ "Reasoning Effort: {budget} "
445
+ "(range 1-100, the higher the value, the more thorough the reasoning)\n\n"
446
+ )
447
+
448
+ REASONING_EFFORT_MAPPINGS: Dict[str, int] = {
449
+ "low": 50,
450
+ "high": 75,
451
+ "max": 100,
452
+ }
453
+ DEFAULT_REASONING_EFFORT = "high"
454
+
455
+
456
+ def render_reasoning_effort(
457
+ index: int,
458
+ thinking_mode: str,
459
+ effort: Union[str, int, None],
460
+ ) -> str:
461
+ """Render the V4.1 numeric reasoning effort prefix (thinking mode, index 0 only)."""
462
+ if effort is None:
463
+ effort = DEFAULT_REASONING_EFFORT
464
+ assert (
465
+ type(effort) is int and 1 <= effort <= 100
466
+ ) or effort in REASONING_EFFORT_MAPPINGS, (
467
+ "Invalid reasoning effort for baanzon_v41: "
468
+ f"{effort}, should be int within [1,100] or {list(REASONING_EFFORT_MAPPINGS)}"
469
+ )
470
+ if type(effort) is str:
471
+ effort = REASONING_EFFORT_MAPPINGS[effort]
472
+ if index == 0 and thinking_mode == "thinking":
473
+ return REASONING_EFFORT_TEMPLATE.format(budget=effort)
474
+ return ""
475
+
476
+
477
+ # ============================================================
478
+ # Tools rendering
479
+ # ============================================================
480
+
481
+ TOOLS_TEMPLATE = """## Tools
482
+
483
+ You have access to a set of tools to help answer the user's question. You can invoke tools by writing a "<{dsml_token}{tc_block_name}>" block like the following:
484
+
485
+ <{dsml_token}{tc_block_name}>
486
+ <{dsml_token}{tool_call_tag_name} name="$TOOL_NAME">
487
+ <{dsml_token}{tool_parameter_tag_name} name="$PARAMETER_NAME" string="true|false">$PARAMETER_VALUE</{dsml_token}{tool_parameter_tag_name}>
488
+ ...
489
+ </{dsml_token}{tool_call_tag_name}>
490
+ <{dsml_token}{tool_call_tag_name} name="$TOOL_NAME2">
491
+ ...
492
+ </{dsml_token}{tool_call_tag_name}>
493
+ </{dsml_token}{tc_block_name}>
494
+
495
+ String parameters should be specified as is and set `string="true"`. For all other types (numbers, booleans, arrays, objects), pass the value in JSON format and set `string="false"`.
496
+
497
+ If thinking_mode is enabled (triggered by {thinking_start_token}), you MUST output your complete reasoning inside {thinking_start_token}...{thinking_end_token} BEFORE any tool calls or final response.
498
+
499
+ Otherwise, output directly after {thinking_end_token} with tool calls or final response.
500
+
501
+ ### Available Tool Schemas
502
+
503
+ {tool_schemas}
504
+
505
+ You MUST strictly follow the above defined tool name and parameter schemas to invoke tool calls.
506
+ """
507
+
508
+
509
+ def render_tools(tools: List[Dict[str, Union[str, Dict[str, Any]]]]) -> str:
510
+ """Render tool schemas into the V4.1 system prompt format."""
511
+ tools_json = [to_json(t) for t in tools]
512
+
513
+ return TOOLS_TEMPLATE.format(
514
+ tool_schemas="\n".join(tools_json),
515
+ dsml_token=dsml_token,
516
+ tc_block_name=tool_calls_block_name,
517
+ tool_call_tag_name=tool_call_tag_name,
518
+ tool_parameter_tag_name=tool_parameter_tag_name,
519
+ thinking_start_token=thinking_start_token,
520
+ thinking_end_token=thinking_end_token,
521
+ )
522
+
523
+
524
+ def encode_arguments_to_dsml(tool_call: Dict[str, Any]) -> str:
525
+ """Encode tool call arguments into V4.1 DSML parameter format."""
526
+ p_dsml_template = (
527
+ '<{dsml_token}{tool_parameter_tag_name} name="{key}" string="{is_str}">'
528
+ '{value}</{dsml_token}{tool_parameter_tag_name}>'
529
+ )
530
+ P_dsml_strs = []
531
+
532
+ arguments = tool_call["arguments"]
533
+ if not isinstance(arguments, dict):
534
+ # Tolerate JSON strings, including double-encoded ones.
535
+ for _ in range(2):
536
+ if isinstance(arguments, str):
537
+ try:
538
+ arguments = json.loads(arguments)
539
+ except Exception:
540
+ break
541
+ else:
542
+ break
543
+ if not isinstance(arguments, dict):
544
+ arguments = {"arguments": tool_call["arguments"]}
545
+
546
+ for k, v in arguments.items():
547
+ P_dsml_strs.append(p_dsml_template.format(
548
+ dsml_token=dsml_token,
549
+ tool_parameter_tag_name=tool_parameter_tag_name,
550
+ key=k,
551
+ is_str="true" if isinstance(v, str) else "false",
552
+ value=v if isinstance(v, str) else to_json(v),
553
+ ))
554
+
555
+ return "\n".join(P_dsml_strs)
556
+
557
+
558
+ # ============================================================
559
+ # Message Rendering
560
+ # ============================================================
561
+
562
+ def find_last_user_index(messages: List[Dict[str, Any]]) -> int:
563
+ """
564
+ Find the index of the last user message.
565
+
566
+ V4.1 supports mid-conversation system messages, which count as user
567
+ messages for the purposes of the assistant generation header.
568
+ User messages containing only tool results do not start a new turn.
569
+ """
570
+ last_user_index = -1
571
+ for idx in range(len(messages) - 1, -1, -1):
572
+ role = messages[idx].get("role")
573
+ if role == "user" or (role == "system" and idx > 0):
574
+ content_blocks = messages[idx].get("content_blocks")
575
+ if content_blocks and not any(
576
+ block.get("type") in ("text", "image", "image_url")
577
+ for block in content_blocks
578
+ ):
579
+ continue
580
+ last_user_index = idx
581
+ break
582
+ return last_user_index
583
+
584
+
585
+ def render_message(
586
+ index: int,
587
+ messages: List[Dict[str, Any]],
588
+ thinking_mode: str,
589
+ drop_thinking: bool = True,
590
+ reasoning_effort: Union[str, int, None] = None,
591
+ ) -> str:
592
+ """
593
+ Render a single message at the given index into its V4.1 encoded string form.
594
+ """
595
+ assert 0 <= index < len(messages)
596
+ assert thinking_mode in ["chat", "thinking"], f"Invalid thinking_mode `{thinking_mode}`"
597
+
598
+ msg = messages[index]
599
+ last_user_idx = find_last_user_index(messages)
600
+
601
+ role = msg.get("role")
602
+ content = msg.get("content")
603
+ tools = msg.get("tools")
604
+ response_format = msg.get("response_format")
605
+ tool_calls = msg.get("tool_calls")
606
+ reasoning_content = msg.get("reasoning_content")
607
+ wo_eos = msg.get("wo_eos", False)
608
+
609
+ if tools:
610
+ tools = tools_from_openai_format(tools)
611
+ if tool_calls:
612
+ tool_calls = tool_calls_from_openai_format(tool_calls)
613
+
614
+ # Reasoning effort prefix (thinking mode, index 0 only)
615
+ reasoning_effort_prompt = render_reasoning_effort(index, thinking_mode, reasoning_effort)
616
+ # System token leads the conversation when there is a reasoning effort prompt
617
+ # or the first message is a system message.
618
+ prompt = SYSTEM_SP_TOKEN if index == 0 and (reasoning_effort_prompt or role == "system") else ""
619
+ prompt += reasoning_effort_prompt
620
+
621
+ if role == "system":
622
+ if index > 0:
623
+ # Mid-conversation system message
624
+ prompt += SYSTEM_SP_TOKEN
625
+ prompt += system_msg_template.format(content=content or "")
626
+ if tools:
627
+ prompt += "\n\n" + render_tools(tools)
628
+ if response_format:
629
+ prompt += "\n\n" + response_format_template.format(schema=to_json(response_format))
630
+
631
+ elif role == "user":
632
+ prompt += USER_SP_TOKEN
633
+
634
+ # Handle content blocks (tool results mixed with text)
635
+ content_blocks = msg.get("content_blocks")
636
+ if content_blocks:
637
+ parts = []
638
+ for block in content_blocks:
639
+ block_type = block.get("type")
640
+ if block_type == "text":
641
+ parts.append(block.get("text", ""))
642
+ elif block_type == "tool_result":
643
+ tool_content = block.get("content", "")
644
+ if isinstance(tool_content, list):
645
+ text_parts = []
646
+ for b in tool_content:
647
+ if b.get("type") == "text":
648
+ text_parts.append(b.get("text", ""))
649
+ else:
650
+ text_parts.append(f"[Unsupported {b.get('type')}]")
651
+ tool_content = "\n\n".join(text_parts)
652
+ parts.append(tool_output_template.format(content=tool_content))
653
+ else:
654
+ parts.append(f"[Unsupported {block_type}]")
655
+ prompt += "\n\n".join(parts)
656
+ else:
657
+ prompt += content or ""
658
+
659
+ elif role == "latest_reminder":
660
+ prompt += LATEST_REMINDER_SP_TOKEN + latest_reminder_msg_template.format(content=content)
661
+
662
+ elif role == "tool":
663
+ raise NotImplementedError("baanzon_v41 merges tool messages into user; please preprocess with merge_tool_messages()")
664
+
665
+ elif role == "assistant":
666
+ thinking_part = ""
667
+ tc_content = ""
668
+
669
+ if tool_calls:
670
+ tc_list = [
671
+ tool_call_template.format(
672
+ dsml_token=dsml_token,
673
+ tool_call_tag_name=tool_call_tag_name,
674
+ name=_tool_name_for_encoding(tc),
675
+ arguments=encode_arguments_to_dsml(tc)
676
+ )
677
+ for tc in tool_calls
678
+ ]
679
+ tc_content += '\n\n' + tool_calls_template.format(
680
+ dsml_token=dsml_token,
681
+ tool_calls="\n".join(tc_list),
682
+ tc_block_name=tool_calls_block_name,
683
+ )
684
+
685
+ summary_content = content or ""
686
+ rc = reasoning_content or ""
687
+
688
+ # Check if previous message has a task - if so, this is a task output (no thinking)
689
+ prev_has_task = index - 1 >= 0 and messages[index - 1].get("task") is not None
690
+
691
+ if thinking_mode == "thinking" and not prev_has_task:
692
+ if not drop_thinking or index > last_user_idx:
693
+ thinking_part = thinking_template.format(reasoning_content=rc) + thinking_end_token
694
+ else:
695
+ thinking_part = ""
696
+
697
+ if wo_eos:
698
+ prompt += assistant_msg_wo_eos_template.format(
699
+ reasoning=thinking_part,
700
+ content=summary_content,
701
+ tool_calls=tc_content,
702
+ )
703
+ else:
704
+ prompt += assistant_msg_template.format(
705
+ reasoning=thinking_part,
706
+ content=summary_content,
707
+ tool_calls=tc_content,
708
+ )
709
+ else:
710
+ raise NotImplementedError(f"Unknown role: {role}")
711
+
712
+ # Append transition tokens based on what follows
713
+ if index + 1 < len(messages) and messages[index + 1].get("role") not in ["assistant", "latest_reminder"]:
714
+ return prompt
715
+
716
+ task = messages[index].get("task")
717
+ if task is not None:
718
+ # Task special token for internal classification tasks
719
+ assert task in VALID_TASKS, f"Invalid task: '{task}'. Valid tasks are: {list(VALID_TASKS)}"
720
+ task_sp_token = DS_TASK_SP_TOKENS[task]
721
+
722
+ if task != "action":
723
+ # Non-action tasks: append task sp token directly after the message
724
+ prompt += task_sp_token
725
+ else:
726
+ # Action task: append Assistant + thinking token + action sp token
727
+ prompt += ASSISTANT_SP_TOKEN
728
+ prompt += thinking_end_token if thinking_mode != "thinking" else thinking_start_token
729
+ prompt += task_sp_token
730
+
731
+ elif role == "user" or (role == "system" and index > 0):
732
+ # Normal generation: append Assistant + thinking token
733
+ # (mid-conversation system messages also trigger the assistant header)
734
+ prompt += ASSISTANT_SP_TOKEN
735
+ if not drop_thinking and thinking_mode == "thinking":
736
+ prompt += thinking_start_token
737
+ elif drop_thinking and thinking_mode == "thinking" and index >= last_user_idx:
738
+ prompt += thinking_start_token
739
+ else:
740
+ prompt += thinking_end_token
741
+
742
+ return prompt
743
+
744
+
745
+ # ============================================================
746
+ # Main Encoding Function
747
+ # ============================================================
748
+
749
+ def _drop_thinking_messages(messages: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
750
+ """
751
+ Drop reasoning_content and non-essential messages before the last user message.
752
+ Same as V4, but uses the V4.1 last-user definition (mid systems count).
753
+ """
754
+ last_user_idx = find_last_user_index(messages)
755
+ result = []
756
+ keep_roles = {"user", "system", "tool", "latest_reminder", "direct_search_results"}
757
+
758
+ for idx, msg in enumerate(messages):
759
+ role = msg.get("role")
760
+ if role in keep_roles or idx >= last_user_idx:
761
+ result.append(msg)
762
+ elif role == "assistant":
763
+ msg = copy.copy(msg)
764
+ msg.pop("reasoning_content", None)
765
+ result.append(msg)
766
+
767
+ return result
768
+
769
+
770
+ def _encode_messages_text(
771
+ messages: List[Dict[str, Any]],
772
+ thinking_mode: str,
773
+ context: Optional[List[Dict[str, Any]]] = None,
774
+ drop_thinking: bool = True,
775
+ add_default_bos_token: bool = True,
776
+ reasoning_effort: Union[str, int, None] = None,
777
+ ) -> str:
778
+ """Encode preprocessed (text-only) messages into the V4.1 prompt format."""
779
+ context = context if context else []
780
+
781
+ full_messages = context + messages
782
+
783
+ prompt = bos_token if add_default_bos_token and len(context) == 0 else ""
784
+
785
+ # Resolve drop_thinking: if any message has tools defined, don't drop thinking
786
+ effective_drop_thinking = drop_thinking
787
+ if any(m.get("tools") for m in full_messages):
788
+ effective_drop_thinking = False
789
+
790
+ if thinking_mode == "thinking" and effective_drop_thinking:
791
+ full_messages = _drop_thinking_messages(full_messages)
792
+ num_to_render = len(full_messages) - len(_drop_thinking_messages(context))
793
+ context_len = len(full_messages) - num_to_render
794
+ else:
795
+ num_to_render = len(messages)
796
+ context_len = len(context)
797
+
798
+ for idx in range(num_to_render):
799
+ prompt += render_message(
800
+ idx + context_len,
801
+ full_messages,
802
+ thinking_mode=thinking_mode,
803
+ drop_thinking=effective_drop_thinking,
804
+ reasoning_effort=reasoning_effort,
805
+ )
806
+
807
+ return prompt
808
+
809
+
810
+ def encode_messages(
811
+ messages: List[Dict[str, Any]],
812
+ thinking_mode: str,
813
+ context: Optional[List[Dict[str, Any]]] = None,
814
+ drop_thinking: bool = True,
815
+ add_default_bos_token: bool = True,
816
+ reasoning_effort: Union[str, int, None] = None,
817
+ return_multi_modal_data: bool = False,
818
+ ) -> Any:
819
+ """Encode text or multimodal messages into the Baanzon-Action-1.1 prompt format.
820
+
821
+ Text-only calls return the prompt string. When return_multi_modal_data is
822
+ true, the result is ``(prompt, media_data)``.
823
+ """
824
+ # Validate before merging can discard message-level textual fields.
825
+ for msg in (context or []) + messages:
826
+ _validate_no_image_sp_tokens(msg)
827
+
828
+ # Collect images only after tool results are in their final prompt order.
829
+ context = merge_tool_messages(context or [])
830
+ messages = merge_tool_messages(messages)
831
+ messages = sort_tool_results_by_call_order(context + messages)[len(context):]
832
+ processed_context, _ = process_image_messages(context) if context else ([], [])
833
+ processed_messages, images = process_image_messages(messages)
834
+ prompt = _encode_messages_text(
835
+ processed_messages,
836
+ thinking_mode=thinking_mode,
837
+ context=processed_context if processed_context else None,
838
+ drop_thinking=drop_thinking,
839
+ add_default_bos_token=add_default_bos_token,
840
+ reasoning_effort=reasoning_effort,
841
+ )
842
+ if return_multi_modal_data:
843
+ return prompt, {"images": images}
844
+ return prompt
845
+
846
+
847
+ def load_cases(input_file: str) -> List[Dict[str, Any]]:
848
+ """Load one or more OpenAI-format conversation cases from JSON."""
849
+ with open(input_file) as file:
850
+ data = json.load(file)
851
+ if isinstance(data, dict):
852
+ data = [data]
853
+ elif data and isinstance(data[0], dict) and "role" in data[0]:
854
+ data = [{"messages": data}]
855
+
856
+ cases = []
857
+ for case in data:
858
+ messages = copy.deepcopy(case["messages"])
859
+ if "tools" in case:
860
+ if not messages:
861
+ raise ValueError("A case with tools must contain at least one message")
862
+ messages[0]["tools"] = case["tools"]
863
+ cases.append({
864
+ "messages": messages,
865
+ "context": case.get("context"),
866
+ "thinking_mode": case.get("thinking_mode"),
867
+ "reasoning_effort": case.get("reasoning_effort"),
868
+ })
869
+ return cases
870
+
871
+
872
+ def encode_case(
873
+ case: Dict[str, Any], thinking_mode: str
874
+ ) -> Tuple[str, List[Dict[str, Any]]]:
875
+ """Encode one JSON case and return its current-turn image records."""
876
+ prompt, media_data = encode_messages(
877
+ case["messages"],
878
+ thinking_mode=case.get("thinking_mode") or thinking_mode,
879
+ context=case.get("context"),
880
+ reasoning_effort=case.get("reasoning_effort"),
881
+ return_multi_modal_data=True,
882
+ )
883
+ return prompt, media_data["images"]
884
+
885
+
886
+ # ============================================================
887
+ # Parsing (Decoding model output)
888
+ # ============================================================
889
+
890
+ def parse_tool_calls(index: int, text: str) -> Tuple[int, Optional[str], List[Dict[str, str]]]:
891
+ """
892
+ Parse V4.1 DSML tool calls from text starting at the given index.
893
+
894
+ Returns:
895
+ Tuple of (new_index, last_stop_token, list_of_tool_call_dicts).
896
+ """
897
+ tool_calls: List[Dict[str, Any]] = []
898
+ stop_token = None
899
+ tool_calls_end_token = f"</{dsml_token}{tool_calls_block_name}>"
900
+ tool_call_start_token = f"<{dsml_token}{tool_call_tag_name}"
901
+ tool_call_end_token = f"</{dsml_token}{tool_call_tag_name}"
902
+ tool_parameter_start_token = f"<{dsml_token}{tool_parameter_tag_name}"
903
+ tool_parameter_end_token = f"/{dsml_token}{tool_parameter_tag_name}"
904
+
905
+ while index < len(text):
906
+ index, _, stop_token = _read_until_stop(index, text, [tool_call_start_token, tool_calls_end_token])
907
+ if _ != ">\n":
908
+ raise ValueError(f"Tool call format error: expected '>\\n' but got '{_}'")
909
+
910
+ if stop_token == tool_calls_end_token:
911
+ break
912
+
913
+ if stop_token is None:
914
+ raise ValueError("Missing special token in tool calls")
915
+
916
+ index, tool_name_content, stop_token = _read_until_stop(index, text, [tool_parameter_start_token, tool_call_end_token])
917
+
918
+ p_tool_name = re.findall(r'^\s*name="(.*?)">\n$', tool_name_content, flags=re.DOTALL)
919
+ if len(p_tool_name) != 1:
920
+ raise ValueError(f"Tool name format error: '{tool_name_content}'")
921
+ tool_name = p_tool_name[0]
922
+
923
+ tool_args: Dict[str, Tuple[str, str]] = {}
924
+ while stop_token == tool_parameter_start_token:
925
+ index, param_content, stop_token = _read_until_stop(index, text, [tool_parameter_end_token])
926
+
927
+ param_kv = re.findall(r'^ name="(.*?)" string="(true|false)">(.*?)<$', param_content, flags=re.DOTALL)
928
+ if len(param_kv) != 1:
929
+ raise ValueError(f"Parameter format error: '{param_content}'")
930
+ param_name, string, param_value = param_kv[0]
931
+
932
+ if param_name in tool_args:
933
+ raise ValueError(f"Duplicate parameter name: '{param_name}'")
934
+ tool_args[param_name] = (param_value, string)
935
+
936
+ index, content, stop_token = _read_until_stop(index, text, [tool_parameter_start_token, tool_call_end_token])
937
+ if content != ">\n":
938
+ raise ValueError(f"Parameter format error: expected '>\\n' but got '{content}'")
939
+
940
+ tool_call = decode_dsml_to_arguments(tool_name=tool_name, tool_args=tool_args)
941
+ tool_calls.append(tool_call)
942
+
943
+ return index, stop_token, tool_calls
944
+
945
+
946
+ def parse_message_from_completion_text(text: str, thinking_mode: str) -> Dict[str, Any]:
947
+ """
948
+ Parse a model completion text into a structured assistant message (V4.1 format).
949
+
950
+ Returns:
951
+ Dict with keys: "role", "content", "reasoning_content", "tool_calls".
952
+ tool_calls are in OpenAI format.
953
+ """
954
+ summary_content, reasoning_content, tool_calls = "", "", []
955
+ index, stop_token = 0, None
956
+ tool_calls_start_token = f"\n\n<{dsml_token}{tool_calls_block_name}"
957
+
958
+ is_thinking = thinking_mode == "thinking"
959
+ is_tool_calling = False
960
+
961
+ if is_thinking:
962
+ index, content_delta, stop_token = _read_until_stop(index, text, [thinking_end_token, tool_calls_start_token])
963
+ reasoning_content = content_delta
964
+ assert stop_token == thinking_end_token, "Invalid thinking format: missing </think>"
965
+
966
+ index, content_delta, stop_token = _read_until_stop(index, text, [eos_token, tool_calls_start_token])
967
+ summary_content = content_delta
968
+ if stop_token == tool_calls_start_token:
969
+ is_tool_calling = True
970
+ else:
971
+ assert stop_token == eos_token, "Invalid format: missing EOS token"
972
+
973
+ if is_tool_calling:
974
+ index, stop_token, tool_calls = parse_tool_calls(index, text)
975
+
976
+ index, tool_ends_text, stop_token = _read_until_stop(index, text, [eos_token])
977
+ assert not tool_ends_text, "Unexpected content after tool calls"
978
+
979
+ assert len(text) == index and stop_token in [eos_token, None], "Unexpected content at end"
980
+
981
+ for sp_token in [bos_token, eos_token, thinking_start_token, thinking_end_token, dsml_token]:
982
+ assert sp_token not in summary_content and sp_token not in reasoning_content, \
983
+ f"Unexpected special token '{sp_token}' in content"
984
+
985
+ return {
986
+ "role": "assistant",
987
+ "content": summary_content,
988
+ "reasoning_content": reasoning_content,
989
+ "tool_calls": tool_calls_to_openai_format(tool_calls)
990
+ }
encoding/test_encoding.py ADDED
@@ -0,0 +1,652 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Tests for encoding.py (Baanzon-Action-1.1 encoding).
3
+
4
+ Adapted from baanzon_harmony/tests/test_baanzon_v41.py for the
5
+ self-contained dict-based API in this repo.
6
+ """
7
+
8
+ import copy
9
+ import json
10
+ from pathlib import Path
11
+ from typing import Any
12
+
13
+ import pytest
14
+
15
+ import encoding as enc
16
+ from encoding import (
17
+ IMAGE_PLACEHOLDER,
18
+ SYSTEM_SP_TOKEN,
19
+ encode_messages,
20
+ parse_message_from_completion_text,
21
+ render_message,
22
+ merge_tool_messages,
23
+ )
24
+
25
+
26
+ REASONING_EFFORT_TEMPLATE = (
27
+ SYSTEM_SP_TOKEN + "Reasoning Effort: {budget} "
28
+ "(range 1-100, the higher the value, the more thorough the reasoning)\n\n"
29
+ )
30
+
31
+ V41_TOOL_CALL_OUTPUT = (
32
+ ' reason </think>summary\n\n'
33
+ '<|DSML| calls>\n'
34
+ '<|DSML| invoke name="lookup">\n'
35
+ '<|DSML| parameter name="query" string="true">value'
36
+ '</|DSML| parameter>\n'
37
+ '<|DSML| parameter name="limit" string="false">2'
38
+ '</|DSML| parameter>\n'
39
+ '</|DSML| invoke>\n'
40
+ '</|DSML| calls><|end▁of▁sentence|>'
41
+ )
42
+
43
+
44
+ def make_tool() -> dict:
45
+ return {
46
+ "type": "function",
47
+ "function": {
48
+ "name": "lookup",
49
+ "description": "Look up a value",
50
+ "parameters": {
51
+ "type": "object",
52
+ "properties": {
53
+ "query": {"type": "string"},
54
+ "limit": {"type": "integer"},
55
+ },
56
+ },
57
+ },
58
+ }
59
+
60
+
61
+ def make_tool_call_messages() -> list:
62
+ return [
63
+ {"role": "user", "content": "question"},
64
+ {
65
+ "role": "assistant",
66
+ "reasoning_content": " reason ",
67
+ "content": "summary",
68
+ "tool_calls": [
69
+ {
70
+ "type": "function",
71
+ "function": {
72
+ "name": "lookup",
73
+ "arguments": '{"query":"value","limit":2}',
74
+ },
75
+ }
76
+ ],
77
+ },
78
+ ]
79
+
80
+
81
+ # ============================================================
82
+ # Vision
83
+ # ============================================================
84
+
85
+ def test_v41_renders_images() -> None:
86
+ prompt, media = encode_messages(
87
+ [
88
+ {
89
+ "role": "user",
90
+ "content": [
91
+ {"type": "text", "text": "inspect"},
92
+ {"type": "image_url", "image_url": {"url": "/unused/image.png"}},
93
+ ],
94
+ }
95
+ ],
96
+ thinking_mode="chat",
97
+ return_multi_modal_data=True,
98
+ )
99
+
100
+ assert prompt == (
101
+ '<|begin▁of▁sentence|><|User|>inspect\n\n'
102
+ f'{IMAGE_PLACEHOLDER}<|Assistant|></think>'
103
+ )
104
+ assert media == {"images": [{"type": "image", "url": "/unused/image.png"}]}
105
+
106
+
107
+ @pytest.mark.parametrize("role", ["user", "tool"])
108
+ def test_v41_rejects_image_placeholder_in_text(role: str) -> None:
109
+ with pytest.raises(ValueError):
110
+ encode_messages(
111
+ [{"role": role, "content": f"hi {IMAGE_PLACEHOLDER}"}],
112
+ thinking_mode="chat",
113
+ )
114
+
115
+
116
+ @pytest.mark.parametrize(
117
+ "input_format",
118
+ ["content_blocks", "tool_messages", "tool_content_blocks", "context"],
119
+ )
120
+ def test_v41_tool_result_images_follow_prompt_order(input_format: str) -> None:
121
+ history = [
122
+ {
123
+ "role": "user",
124
+ "content": [
125
+ {"type": "image_url", "image_url": {"url": "history.png"}},
126
+ ],
127
+ },
128
+ {
129
+ "role": "assistant",
130
+ "tool_calls": [
131
+ {
132
+ "id": name,
133
+ "type": "function",
134
+ "function": {"name": name, "arguments": "{}"},
135
+ }
136
+ for name in ("a", "b")
137
+ ],
138
+ },
139
+ ]
140
+ results = [
141
+ {
142
+ "type": "tool_result",
143
+ "tool_use_id": name,
144
+ "content": [
145
+ {"type": "text", "text": f"result {name}"},
146
+ {"type": "image_url", "image_url": {"url": f"{name}.png"}},
147
+ ],
148
+ }
149
+ for name in ("b", "a")
150
+ ]
151
+ if input_format in ("tool_messages", "tool_content_blocks"):
152
+ content_field = (
153
+ "content_blocks" if input_format == "tool_content_blocks" else "content"
154
+ )
155
+ messages = history + [
156
+ {
157
+ "role": "tool",
158
+ "tool_call_id": block["tool_use_id"],
159
+ content_field: block["content"],
160
+ }
161
+ for block in results
162
+ ]
163
+ context = []
164
+ elif input_format == "context":
165
+ messages = [{"role": "user", "content_blocks": results}]
166
+ context = history
167
+ else:
168
+ messages = history + [{"role": "user", "content_blocks": results}]
169
+ context = []
170
+ original = copy.deepcopy((messages, context))
171
+
172
+ prompt, media = encode_messages(
173
+ messages,
174
+ thinking_mode="chat",
175
+ context=context,
176
+ return_multi_modal_data=True,
177
+ )
178
+
179
+ assert prompt.endswith(
180
+ f"<|User|><tool_result>result a\n\n{IMAGE_PLACEHOLDER}</tool_result>\n\n"
181
+ f"<tool_result>result b\n\n{IMAGE_PLACEHOLDER}</tool_result><|Assistant|></think>"
182
+ )
183
+ expected_urls = ["a.png", "b.png"] if context else ["history.png", "a.png", "b.png"]
184
+ assert media == {"images": [{"type": "image", "url": url} for url in expected_urls]}
185
+ assert (messages, context) == original
186
+
187
+
188
+ # ============================================================
189
+ # Reasoning Effort
190
+ # ============================================================
191
+
192
+ @pytest.mark.parametrize(
193
+ ("effort", "budget"),
194
+ [
195
+ (None, 75),
196
+ ("low", 50),
197
+ ("high", 75),
198
+ ("max", 100),
199
+ (1, 1),
200
+ (42, 42),
201
+ (100, 100),
202
+ ],
203
+ )
204
+ def test_v41_maps_reasoning_effort_to_1_100_budget(
205
+ effort: Any,
206
+ budget: int,
207
+ ) -> None:
208
+ prompt = encode_messages(
209
+ [{"role": "user", "content": "question"}],
210
+ thinking_mode="thinking",
211
+ reasoning_effort=effort,
212
+ )
213
+
214
+ assert prompt == (
215
+ '<|begin▁of▁sentence|>'
216
+ f'{REASONING_EFFORT_TEMPLATE.format(budget=budget)}'
217
+ '<|User|>question<|Assistant|><think>'
218
+ )
219
+
220
+
221
+ def test_v41_only_adds_reasoning_effort_to_first_thinking_message() -> None:
222
+ messages = [
223
+ {"role": "system", "content": "system"},
224
+ {"role": "user", "content": "question"},
225
+ ]
226
+
227
+ later_message = render_message(
228
+ 1, messages, thinking_mode="thinking", reasoning_effort=100
229
+ )
230
+ chat_message = render_message(
231
+ 0, messages, thinking_mode="chat", reasoning_effort=100
232
+ )
233
+
234
+ assert "Reasoning Effort:" not in later_message
235
+ assert "Reasoning Effort:" not in chat_message
236
+
237
+
238
+ def test_v41_chat_mode_has_no_reasoning_effort_or_system_token() -> None:
239
+ prompt = encode_messages(
240
+ [{"role": "user", "content": "hello"}],
241
+ thinking_mode="chat",
242
+ reasoning_effort="max",
243
+ )
244
+ assert prompt == '<|begin▁of▁sentence|><|User|>hello<|Assistant|></think>'
245
+
246
+
247
+ @pytest.mark.parametrize("effort", [-1, 0, 101, "medium"])
248
+ def test_v41_rejects_out_of_range_or_unknown_reasoning_effort(
249
+ effort: Any,
250
+ ) -> None:
251
+ with pytest.raises(AssertionError, match=r"int within \[1,100\]"):
252
+ encode_messages(
253
+ [{"role": "user", "content": "question"}],
254
+ thinking_mode="thinking",
255
+ reasoning_effort=effort,
256
+ )
257
+
258
+
259
+ @pytest.mark.parametrize("effort", [True, False, 1.5])
260
+ def test_v41_rejects_non_string_non_integer_effort_types(effort: Any) -> None:
261
+ # bool is not `type(...) is int`; float is invalid too
262
+ with pytest.raises(AssertionError):
263
+ encode_messages(
264
+ [{"role": "user", "content": "question"}],
265
+ thinking_mode="thinking",
266
+ reasoning_effort=effort,
267
+ )
268
+
269
+
270
+ # ============================================================
271
+ # System token
272
+ # ============================================================
273
+
274
+ def test_v41_leading_system_message_uses_system_token() -> None:
275
+ prompt = encode_messages(
276
+ [
277
+ {"role": "system", "content": "You are a helpful assistant."},
278
+ {"role": "user", "content": "hello"},
279
+ ],
280
+ thinking_mode="chat",
281
+ )
282
+ assert prompt == (
283
+ '<|begin▁of▁sentence|><|System|>You are a helpful assistant.'
284
+ '<|User|>hello<|Assistant|></think>'
285
+ )
286
+
287
+
288
+ def test_v41_mid_conversation_system_message() -> None:
289
+ prompt = encode_messages(
290
+ [
291
+ {"role": "system", "content": "sys"},
292
+ {"role": "user", "content": "q1"},
293
+ {"role": "assistant", "content": "a1", "reasoning_content": "r1"},
294
+ {"role": "system", "content": "mid sys"},
295
+ ],
296
+ thinking_mode="thinking",
297
+ reasoning_effort=88,
298
+ )
299
+ # Mid-conversation system gets its own <|System|> token and triggers
300
+ # the assistant generation header afterwards.
301
+ assert prompt == (
302
+ '<|begin▁of▁sentence|>'
303
+ f'{REASONING_EFFORT_TEMPLATE.format(budget=88)}'
304
+ 'sys<|User|>q1<|Assistant|></think>a1<|end▁of▁sentence|>'
305
+ '<|System|>mid sys<|Assistant|><think>'
306
+ )
307
+
308
+
309
+ # ============================================================
310
+ # DSML tool tags
311
+ # ============================================================
312
+
313
+ def test_v41_tool_instructions_use_spaced_dsml_tags_in_chat_mode() -> None:
314
+ prompt = encode_messages(
315
+ [
316
+ {"role": "system", "content": "system", "tools": [make_tool()]},
317
+ {"role": "user", "content": "question"},
318
+ ],
319
+ thinking_mode="chat",
320
+ )
321
+
322
+ assert (
323
+ '<|DSML| calls>\n'
324
+ '<|DSML| invoke name="$TOOL_NAME">\n'
325
+ '<|DSML| parameter name="$PARAMETER_NAME" '
326
+ 'string="true|false">$PARAMETER_VALUE</|DSML| parameter>\n'
327
+ '...\n'
328
+ '</|DSML| invoke>'
329
+ ) in prompt
330
+ assert '<|DSML|tool_calls>' not in prompt
331
+ assert '<|DSML|invoke' not in prompt
332
+ assert '<|DSML|parameter' not in prompt
333
+
334
+
335
+ def test_v41_renders_spaced_dsml_with_v4_assistant_semantics() -> None:
336
+ messages = make_tool_call_messages()
337
+
338
+ prompt = render_message(1, messages, thinking_mode="thinking")
339
+
340
+ assert prompt == V41_TOOL_CALL_OUTPUT
341
+
342
+
343
+ def test_v41_parses_spaced_dsml_roundtrip() -> None:
344
+ messages = make_tool_call_messages()
345
+
346
+ parsed = parse_message_from_completion_text(
347
+ V41_TOOL_CALL_OUTPUT, thinking_mode="thinking"
348
+ )
349
+
350
+ assert parsed["role"] == "assistant"
351
+ assert parsed["reasoning_content"] == " reason "
352
+ assert parsed["content"] == "summary"
353
+ assert parsed["tool_calls"]
354
+ assert parsed["tool_calls"][0]["function"]["name"] == "lookup"
355
+ assert json.loads(parsed["tool_calls"][0]["function"]["arguments"]) == {
356
+ "query": "value",
357
+ "limit": 2,
358
+ }
359
+
360
+ # Re-encoding the parsed message reproduces the original completion text
361
+ assert encode_messages(
362
+ [parsed],
363
+ thinking_mode="thinking",
364
+ context=messages[:1],
365
+ ) == V41_TOOL_CALL_OUTPUT
366
+
367
+
368
+ def test_v41_parse_rejects_unspaced_v4_dsml() -> None:
369
+ v4_output = V41_TOOL_CALL_OUTPUT.replace("|DSML| calls", "|DSML|tool_calls") \
370
+ .replace("|DSML| invoke", "|DSML|invoke") \
371
+ .replace("|DSML| parameter", "|DSML|parameter")
372
+ with pytest.raises(AssertionError):
373
+ parse_message_from_completion_text(v4_output, thinking_mode="thinking")
374
+
375
+
376
+ # ============================================================
377
+ # Tool namespaces
378
+ # ============================================================
379
+
380
+ @pytest.mark.parametrize("location", ["tool", "function"])
381
+ @pytest.mark.parametrize("namespace", ["search", {"name": "search", "description": "Search tools."}])
382
+ def test_v41_renders_namespaced_tool_schemas(location: str, namespace: Any) -> None:
383
+ tool = make_tool()
384
+ target = tool if location == "tool" else tool["function"]
385
+ target["namespace"] = namespace
386
+ original = copy.deepcopy(tool)
387
+
388
+ prompt = encode_messages(
389
+ [{"role": "system", "content": "system", "tools": [tool]}],
390
+ thinking_mode="chat",
391
+ )
392
+
393
+ schema = dict(make_tool()["function"], name="search::lookup")
394
+ if isinstance(namespace, dict):
395
+ schema["description"] = "Search tools.\nLook up a value"
396
+ assert json.dumps(schema) in prompt
397
+ assert '"namespace":' not in prompt
398
+ assert tool == original
399
+
400
+
401
+ @pytest.mark.parametrize("thinking_mode", ["chat", "thinking"])
402
+ @pytest.mark.parametrize("location", ["tool", "function", "qualified_name"])
403
+ def test_v41_namespaced_tool_calls_roundtrip(thinking_mode: str, location: str) -> None:
404
+ messages = make_tool_call_messages()
405
+ call = messages[1]["tool_calls"][0]
406
+ if location == "qualified_name":
407
+ call["function"]["name"] = "search::lookup"
408
+ else:
409
+ target = call if location == "tool" else call["function"]
410
+ target["namespace"] = "search"
411
+ original = copy.deepcopy(messages)
412
+
413
+ expected = V41_TOOL_CALL_OUTPUT.replace('name="lookup"', 'name="search::lookup"')
414
+ if thinking_mode == "chat":
415
+ expected = expected.split("</think>", 1)[1]
416
+ assert render_message(1, messages, thinking_mode=thinking_mode) == expected
417
+
418
+ parsed = parse_message_from_completion_text(expected, thinking_mode=thinking_mode)
419
+ assert parsed["tool_calls"] == [{
420
+ "type": "function",
421
+ "namespace": "search",
422
+ "function": {
423
+ "name": "lookup",
424
+ "arguments": '{"query": "value", "limit": 2}',
425
+ },
426
+ }]
427
+ assert encode_messages(
428
+ [parsed], thinking_mode=thinking_mode, context=messages[:1]
429
+ ) == expected
430
+ assert messages == original
431
+
432
+
433
+ def test_v41_keeps_same_named_tools_in_separate_namespaces() -> None:
434
+ tools, calls = [], []
435
+ for namespace in (None, "search", "files"):
436
+ tool = make_tool()
437
+ call = {
438
+ "type": "function",
439
+ "function": {"name": "lookup", "arguments": '{"query":"value"}'},
440
+ }
441
+ if namespace is not None:
442
+ tool["namespace"] = {"name": namespace}
443
+ call["namespace"] = namespace
444
+ tools.append(tool)
445
+ calls.append(call)
446
+
447
+ messages = [
448
+ {"role": "system", "content": "system", "tools": tools},
449
+ {"role": "user", "content": "question"},
450
+ {"role": "assistant", "content": "summary", "tool_calls": calls},
451
+ ]
452
+ prompt = encode_messages(messages, thinking_mode="chat")
453
+ for name in ("lookup", "search::lookup", "files::lookup"):
454
+ assert f'"name": "{name}"' in prompt
455
+ assert f'<|DSML| invoke name="{name}">' in prompt
456
+
457
+ completion = render_message(2, messages, thinking_mode="chat")
458
+ parsed = parse_message_from_completion_text(completion, thinking_mode="chat")
459
+ assert "namespace" not in parsed["tool_calls"][0]
460
+ assert [call.get("namespace") for call in parsed["tool_calls"]] == [None, "search", "files"]
461
+ assert all(call["function"]["name"] == "lookup" for call in parsed["tool_calls"])
462
+
463
+
464
+ def test_v41_does_not_duplicate_a_qualified_namespace() -> None:
465
+ tool = make_tool()
466
+ tool["function"]["name"] = "search::lookup"
467
+ tool["namespace"] = {"name": "search", "description": "Search tools."}
468
+ schema = enc.tools_from_openai_format([tool])[0]
469
+ assert schema["name"] == "search::lookup"
470
+ assert schema["description"] == "Search tools.\nLook up a value"
471
+
472
+ messages = make_tool_call_messages()
473
+ call = messages[1]["tool_calls"][0]
474
+ call["function"]["name"] = "search::lookup"
475
+ call["namespace"] = "search"
476
+ assert render_message(1, messages, thinking_mode="thinking") == (
477
+ V41_TOOL_CALL_OUTPUT.replace('name="lookup"', 'name="search::lookup"')
478
+ )
479
+
480
+
481
+ @pytest.mark.parametrize(
482
+ ("name", "namespace", "error"),
483
+ [
484
+ ("search::lookup", "files", "Conflicting tool namespaces"),
485
+ ("search::nested::lookup", None, "Tool name must not contain"),
486
+ ("lookup", "search::nested", "Tool namespace must not contain"),
487
+ ],
488
+ )
489
+ def test_v41_rejects_ambiguous_tool_namespaces(name: str, namespace: Any, error: str) -> None:
490
+ tool = make_tool()
491
+ tool["function"]["name"] = name
492
+ tool["namespace"] = namespace
493
+ with pytest.raises(AssertionError, match=error):
494
+ enc.tools_from_openai_format([tool])
495
+
496
+ messages = make_tool_call_messages()
497
+ call = messages[1]["tool_calls"][0]
498
+ call["function"]["name"] = name
499
+ call["namespace"] = namespace
500
+ with pytest.raises(AssertionError, match=error):
501
+ render_message(1, messages, thinking_mode="thinking")
502
+
503
+
504
+ # ============================================================
505
+ # Multi-turn flow
506
+ # ============================================================
507
+
508
+ def test_v41_drop_thinking_without_tools() -> None:
509
+ messages = [
510
+ {"role": "user", "content": "q1"},
511
+ {"role": "assistant", "content": "a1", "reasoning_content": "r1"},
512
+ {"role": "user", "content": "q2"},
513
+ {
514
+ "role": "assistant",
515
+ "reasoning_content": "r2",
516
+ "tool_calls": [
517
+ {
518
+ "id": "c1",
519
+ "type": "function",
520
+ "function": {"name": "lookup", "arguments": "{}"},
521
+ }
522
+ ],
523
+ },
524
+ {"role": "tool", "tool_call_id": "c1", "content": "value"},
525
+ ]
526
+ prompt = encode_messages(
527
+ messages,
528
+ thinking_mode="thinking",
529
+ drop_thinking=True,
530
+ )
531
+ # Drop completed-turn reasoning, but keep the current turn across tool results.
532
+ assert "<|User|>q1<|Assistant|></think>a1<|end▁of▁sentence|>" in prompt
533
+ assert "r1" not in prompt
534
+ assert "<think>r2</think>" in prompt
535
+ assert prompt.endswith("<tool_result>value</tool_result><|Assistant|><think>")
536
+
537
+ next_turn = encode_messages(
538
+ messages + [{"role": "user", "content": "q3"}],
539
+ thinking_mode="thinking",
540
+ )
541
+ assert "r2" not in next_turn
542
+ assert next_turn.endswith("q3<|Assistant|><think>")
543
+
544
+
545
+ # ============================================================
546
+ # Preprocessing
547
+ # ============================================================
548
+
549
+ def test_merge_tool_messages_creates_tool_result_blocks() -> None:
550
+ merged = merge_tool_messages([
551
+ {"role": "assistant", "content": "", "tool_calls": []},
552
+ {"role": "tool", "tool_call_id": "a", "content": "r1"},
553
+ {"role": "tool", "tool_call_id": "b", "content": "r2"},
554
+ ])
555
+ assert len(merged) == 2
556
+ assert merged[1]["role"] == "user"
557
+ assert [b["type"] for b in merged[1]["content_blocks"]] == ["tool_result", "tool_result"]
558
+
559
+
560
+ def test_v41_task_sp_token() -> None:
561
+ prompt = encode_messages(
562
+ [{"role": "user", "content": "classify me", "task": "query"}],
563
+ thinking_mode="chat",
564
+ )
565
+ assert prompt.endswith("classify me<|query|>")
566
+ assert "<|Assistant|>" not in prompt
567
+
568
+
569
+ # ============================================================
570
+ # Golden fixtures from encoding/tests
571
+ # ============================================================
572
+
573
+ ENCODING_DIR = Path(__file__).resolve().parent
574
+ ENCODING_FIXTURES_DIR = ENCODING_DIR / "tests"
575
+ INFERENCE_EXAMPLES_DIR = ENCODING_DIR.parent / "inference" / "examples"
576
+
577
+ FIXTURE_CASE_IDS = sorted(
578
+ int(p.stem.split("_")[-1])
579
+ for p in ENCODING_FIXTURES_DIR.glob("test_input_*.json")
580
+ )
581
+
582
+
583
+ @pytest.mark.parametrize("case_id", FIXTURE_CASE_IDS)
584
+ def test_examples_encoding_golden_outputs(case_id: int) -> None:
585
+ """Each tests/encoding input must encode to its checked-in golden output."""
586
+ input_file = ENCODING_FIXTURES_DIR / f"test_input_{case_id}.json"
587
+ output_file = ENCODING_FIXTURES_DIR / f"test_output_{case_id}.txt"
588
+ assert output_file.exists(), f"missing golden output: {output_file.name} (run tests/encoding/regen_outputs.py)"
589
+
590
+ case = enc.load_cases(str(input_file))[0]
591
+ prompt, _ = enc.encode_case(case, thinking_mode="chat")
592
+
593
+ assert prompt == output_file.read_text(), (
594
+ f"{output_file.name} is stale; regenerate with tests/encoding/regen_outputs.py"
595
+ )
596
+
597
+
598
+ def test_examples_v41_output_uses_v41_format_markers() -> None:
599
+ """Sanity-check the V4.1 goldens actually exercise V4.1-specific format."""
600
+ # case 1: tool calls with spaced DSML tags
601
+ out1 = (ENCODING_FIXTURES_DIR / "test_output_1.txt").read_text()
602
+ assert '<|DSML| calls>' in out1 and '<|DSML| invoke name="get_weather">' in out1
603
+ assert '<|DSML|tool_calls>' not in out1
604
+
605
+ # case 5: numeric reasoning effort behind the system token
606
+ out5 = (ENCODING_FIXTURES_DIR / "test_output_5.txt").read_text()
607
+ assert out5.startswith(
608
+ '<|begin▁of▁sentence|>' + REASONING_EFFORT_TEMPLATE.format(budget=100)
609
+ )
610
+ assert out5.count(IMAGE_PLACEHOLDER) == 2
611
+
612
+
613
+ def test_examples_vl_txt_and_json_encode_identically() -> None:
614
+ """The TXT (last block of example.txt) and JSON vision examples must encode identically."""
615
+ txt = (INFERENCE_EXAMPLES_DIR / "example.txt").read_text().rstrip("\n").split("\n\n")[-1]
616
+ messages = [{"role": "user", "content": enc.parse_tagged_text(txt)}]
617
+ p1, m1 = encode_messages(messages, thinking_mode="chat", return_multi_modal_data=True)
618
+
619
+ case = enc.load_cases(str(INFERENCE_EXAMPLES_DIR / "example_harmony.json"))[0]
620
+ p2, m2 = enc.encode_case(case, thinking_mode="chat")
621
+
622
+ assert p1 == p2
623
+ assert m1["images"] == m2
624
+ assert len(m2) == 2
625
+
626
+
627
+ def test_examples_harmony_cases_encode() -> None:
628
+ """All example_harmony.json cases encode without error."""
629
+ cases = enc.load_cases(str(INFERENCE_EXAMPLES_DIR / "example_harmony.json"))
630
+ assert len(cases) == 4
631
+
632
+ # case 1 (vision) is covered by test_examples_vl_txt_and_json_encode_identically
633
+
634
+ # cases are pure OpenAI format: mode/effort are passed at call time
635
+ prompt = encode_messages(
636
+ cases[1]["messages"], thinking_mode="thinking", reasoning_effort=75
637
+ )
638
+ assert REASONING_EFFORT_TEMPLATE.format(budget=75) in prompt
639
+
640
+ # case 3: tools with spaced DSML tags
641
+ prompt, _ = enc.encode_case(cases[2], thinking_mode="chat")
642
+ assert '<|DSML| calls>' in prompt
643
+
644
+ # case 4: mid-conversation system message triggers assistant header
645
+ prompt, _ = enc.encode_case(cases[3], thinking_mode="chat")
646
+ assert '<|System|>Mid-conversation instruction update' in prompt
647
+ assert prompt.endswith('<|Assistant|></think>')
648
+
649
+
650
+ if __name__ == "__main__":
651
+ import sys
652
+ sys.exit(pytest.main([__file__, "-v"]))
encoding/tests/test_input_1.json ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "thinking_mode": "thinking",
3
+ "tools": [
4
+ {
5
+ "type": "function",
6
+ "function": {
7
+ "name": "get_weather",
8
+ "description": "Get the weather for a specific location",
9
+ "parameters": {
10
+ "type": "object",
11
+ "properties": {
12
+ "location": {
13
+ "type": "string",
14
+ "description": "The city name"
15
+ },
16
+ "unit": {
17
+ "type": "string",
18
+ "enum": ["celsius", "fahrenheit"],
19
+ "description": "Temperature unit"
20
+ }
21
+ },
22
+ "required": ["location"]
23
+ }
24
+ }
25
+ },
26
+ {
27
+ "type": "function",
28
+ "function": {
29
+ "name": "search",
30
+ "description": "Search the web for information",
31
+ "parameters": {
32
+ "type": "object",
33
+ "properties": {
34
+ "query": {
35
+ "type": "string",
36
+ "description": "Search query"
37
+ },
38
+ "num_results": {
39
+ "type": "integer",
40
+ "description": "Number of results to return"
41
+ }
42
+ },
43
+ "required": ["query"]
44
+ }
45
+ }
46
+ }
47
+ ],
48
+ "messages": [
49
+ {
50
+ "role": "system",
51
+ "content": "You are a helpful assistant."
52
+ },
53
+ {
54
+ "role": "user",
55
+ "content": "What's the weather like in Beijing?"
56
+ },
57
+ {
58
+ "role": "assistant",
59
+ "reasoning_content": "The user wants the weather in Beijing. I should call get_weather.",
60
+ "content": "",
61
+ "tool_calls": [
62
+ {
63
+ "type": "function",
64
+ "function": {
65
+ "name": "get_weather",
66
+ "arguments": "{\"location\": \"Beijing\", \"unit\": \"celsius\"}"
67
+ }
68
+ }
69
+ ]
70
+ },
71
+ {
72
+ "role": "tool",
73
+ "tool_call_id": "call_0",
74
+ "content": "{\"temperature\": 22, \"condition\": \"sunny\", \"humidity\": 45}"
75
+ },
76
+ {
77
+ "role": "assistant",
78
+ "reasoning_content": "Got the weather data. Let me format a nice response.",
79
+ "content": "The weather in Beijing is currently sunny with a temperature of 22\u00b0C and 45% humidity."
80
+ }
81
+ ]
82
+ }
encoding/tests/test_input_2.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "role": "system",
4
+ "content": "You are a helpful assistant."
5
+ },
6
+ {
7
+ "role": "user",
8
+ "content": "Hello"
9
+ },
10
+ {
11
+ "role": "assistant",
12
+ "reasoning_content": "The user said hello, I should greet back.",
13
+ "content": "Hi there! How can I help you?"
14
+ },
15
+ {
16
+ "role": "user",
17
+ "content": "What is the capital of France?"
18
+ },
19
+ {
20
+ "role": "assistant",
21
+ "reasoning_content": "The user asks about the capital of France. It is Paris.",
22
+ "content": "The capital of France is Paris."
23
+ }
24
+ ]
encoding/tests/test_input_3.json ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "role": "system",
4
+ "content": "该助手为Baanzon,由由Opraiz Technology Pvt Ltd创造。"
5
+ },
6
+ {
7
+ "role": "latest_reminder",
8
+ "content": "2026-02-21,星期六,广州,App,中文"
9
+ },
10
+ {
11
+ "role": "system",
12
+ "content": "CITATION FORMAT: 【{cursor_id}†L{start_line_id}(-L{end_line_id})?】",
13
+ "tools": [
14
+ {
15
+ "type": "function",
16
+ "function": {
17
+ "name": "search",
18
+ "description": "Web search. Split multiple queries with '||'.",
19
+ "parameters": {
20
+ "type": "object",
21
+ "properties": {
22
+ "queries": {
23
+ "type": "string",
24
+ "description": "query1||query2"
25
+ }
26
+ },
27
+ "required": ["queries"],
28
+ "additionalProperties": false
29
+ }
30
+ }
31
+ },
32
+ {
33
+ "type": "function",
34
+ "function": {
35
+ "name": "open",
36
+ "description": "Batch open IDs (format 【{id}†...】) or URLs.",
37
+ "parameters": {
38
+ "type": "object",
39
+ "properties": {
40
+ "open_list": {
41
+ "type": "array",
42
+ "items": {
43
+ "type": "object",
44
+ "properties": {
45
+ "id": {
46
+ "description": "ID or URL",
47
+ "anyOf": [{"type": "integer"}, {"type": "string"}],
48
+ "default": -1
49
+ },
50
+ "loc": {"type": "integer", "description": "Start line", "default": -1},
51
+ "num_lines": {"type": "integer", "description": "", "default": -1}
52
+ },
53
+ "additionalProperties": false
54
+ },
55
+ "description": ""
56
+ }
57
+ },
58
+ "required": ["open_list"],
59
+ "additionalProperties": false
60
+ }
61
+ }
62
+ }
63
+ ]
64
+ },
65
+ {
66
+ "role": "user",
67
+ "content": "小柴胡冲剂和布洛芬能一起吃吗?"
68
+ },
69
+ {
70
+ "role": "assistant",
71
+ "content": "",
72
+ "reasoning_content": "用户想知道小柴胡冲剂和布洛芬能否一起服用。",
73
+ "tool_calls": [
74
+ {
75
+ "type": "function",
76
+ "function": {
77
+ "name": "search",
78
+ "arguments": "{\"queries\": \"小柴胡冲剂 布洛芬 相互作用 一起吃\"}"
79
+ }
80
+ }
81
+ ]
82
+ },
83
+ {
84
+ "role": "tool",
85
+ "content": "[0]"
86
+ },
87
+ {
88
+ "role": "assistant",
89
+ "content": "请及时就医。",
90
+ "reasoning_content": "现在开始组织回答。",
91
+ "tool_calls": []
92
+ }
93
+ ]
encoding/tests/test_input_4.json ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "role": "system",
4
+ "content": "该助手为Baanzon-Action-1.1,由由Opraiz Technology Pvt Ltd创造。\n今天是2025年10月17日,星期五。"
5
+ },
6
+ {
7
+ "role": "latest_reminder",
8
+ "content": "2024-11-15,上海市,App,中文"
9
+ },
10
+ {
11
+ "role": "user",
12
+ "content": "热海大滚锅是世界著名温泉吗"
13
+ },
14
+ {
15
+ "role": "assistant",
16
+ "content": "热海大滚锅在中国乃至全球的地热奇观中占有重要地位,但“世界著名”的称号更侧重于它作为独特的地质现象和旅游景点。",
17
+ "mask": 1
18
+ },
19
+ {
20
+ "role": "user",
21
+ "content": "世界著名温泉有哪些",
22
+ "task": "action"
23
+ },
24
+ {
25
+ "role": "assistant",
26
+ "content": "Search"
27
+ }
28
+ ]
encoding/tests/test_input_5.json ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "thinking_mode": "thinking",
3
+ "reasoning_effort": "max",
4
+ "messages": [
5
+ {
6
+ "role": "system",
7
+ "content": "You are a helpful vision assistant."
8
+ },
9
+ {
10
+ "role": "user",
11
+ "content": [
12
+ {
13
+ "type": "text",
14
+ "text": "请按“第一张、第二张”的顺序回答:第一张图"
15
+ },
16
+ {
17
+ "type": "image_url",
18
+ "image_url": {
19
+ "url": "examples/images/carrots.jpeg"
20
+ }
21
+ },
22
+ {
23
+ "type": "text",
24
+ "text": "和第二张图"
25
+ },
26
+ {
27
+ "type": "image_url",
28
+ "image_url": {
29
+ "url": "examples/images/corn.jpeg"
30
+ }
31
+ },
32
+ {
33
+ "type": "text",
34
+ "text": "中分别是什么食材?它们通常食用的部位分别是什么?"
35
+ }
36
+ ]
37
+ }
38
+ ]
39
+ }
inference/README.md ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Minimal inference
2
+
3
+ A readable reference implementation rather than a production serving engine. The
4
+ model code covers the vision encoder and aligner, sliding-window plus compressed
5
+ sparse attention with its two-level indexer, engram n-gram lookups, MoE,
6
+ Hyper-Connections, and the DSpark forward path. Generation itself is plain
7
+ autoregressive sampling.
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ python -m pip install -r requirements.txt
13
+ ```
14
+
15
+ ## Convert Hugging Face weights
16
+
17
+ The runtime uses one converted checkpoint file per tensor-parallel rank. From
18
+ this directory:
19
+
20
+ ```bash
21
+ export HF_CKPT_PATH=/path/to/Baanzon-Action-1.1-Flash-HF
22
+ export SAVE_PATH=/path/to/Baanzon-Action-1.1-Flash-TP8
23
+ export MP=8
24
+
25
+ python convert.py \
26
+ --hf-ckpt-path "${HF_CKPT_PATH}" \
27
+ --save-path "${SAVE_PATH}" \
28
+ --model-parallel "${MP}" \
29
+ --expert-dtype fp4 \
30
+ --tokenizer-path "${HF_CKPT_PATH}"
31
+ ```
32
+
33
+ Expert counts are inferred from the weight names, so they do not need to be
34
+ passed. `--tokenizer-path` points at whichever directory holds `tokenizer.json`
35
+ and `tokenizer_config.json`; they are copied into the converted checkpoint.
36
+
37
+ ## Run the equivalent TXT and JSON examples
38
+
39
+ ```bash
40
+ export CKPT_PATH=/path/to/Baanzon-Action-1.1-Flash-TP8
41
+ export MP=8
42
+
43
+ INPUT_FILE=examples/example.txt ./run.sh
44
+ INPUT_FILE=examples/example_harmony.json ./run.sh
45
+ ```
46
+
47
+ The two files express the same interleaved two-image prompt, so they produce
48
+ identical encoded prompts and input token IDs.
49
+
50
+ For interactive chat:
51
+
52
+ ```bash
53
+ torchrun --nproc-per-node "${MP}" generate.py \
54
+ --ckpt-path "${CKPT_PATH}" \
55
+ --config config.json \
56
+ --interactive \
57
+ --temperature 0.6
58
+ ```
59
+
60
+ For multi-node execution, pass the usual `torchrun --nnodes`, `--node-rank`,
61
+ `--master-addr`, and `--master-port` arguments before `generate.py`.
62
+
63
+ ## Self-test
64
+
65
+ `model.py` builds a small model from the `ModelArgs` defaults and runs a prefill
66
+ plus 22 decode steps, exercising the real dense-fp8 / MoE-fp4 kernels. Weights
67
+ are uninitialized, so it checks shapes and kernel plumbing, not numerics:
68
+
69
+ ```bash
70
+ python model.py
71
+ ```
inference/config.json ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "vocab_size": 129280,
3
+ "dim": 5120,
4
+ "moe_inter_dim": 2304,
5
+ "n_layers": 40,
6
+ "n_mtp_layers": 3,
7
+ "dspark_block_size": 5,
8
+ "dspark_noise_token_id": 128799,
9
+ "dspark_target_layer_ids": [37, 38, 39],
10
+ "dspark_markov_rank": 256,
11
+ "dspark_n_routed_experts": 128,
12
+ "dspark_n_activated_experts": 3,
13
+ "n_heads": 64,
14
+ "n_routed_experts": 384,
15
+ "n_shared_experts": 1,
16
+ "n_activated_experts": 6,
17
+ "score_func": "sqrtsoftplus",
18
+ "route_scale": 1.5,
19
+ "swiglu_limit": 10.0,
20
+ "q_lora_rank": 1280,
21
+ "head_dim": 512,
22
+ "rope_head_dim": 64,
23
+ "norm_eps": 1e-20,
24
+ "o_groups": 8,
25
+ "o_lora_rank": 1024,
26
+ "window_size": 128,
27
+ "kv_source_layers": [2, 8, 14, 20],
28
+ "index_source_layers": [2, 8, 14, 20, 24, 28, 32, 36],
29
+ "original_seq_len": 65536,
30
+ "rope_theta": 10000,
31
+ "rope_factor": 16,
32
+ "beta_fast": 32,
33
+ "beta_slow": 1,
34
+ "index_n_heads": 32,
35
+ "index_head_dim": 128,
36
+ "index_topk": 512,
37
+ "candidate_source_layer": 20,
38
+ "candidate_topk_blocks": 2048,
39
+ "candidate_block_size": 8,
40
+ "hc_mult": 4,
41
+ "hc_sinkhorn_iters": 20,
42
+ "hc_eps": 1e-06,
43
+ "engram_layer_ids": [1, 14],
44
+ "engram_vocab_size": 16000000,
45
+ "engram_num_embeddings": [384006168, 384016682],
46
+ "engram_max_ngram_size": 4,
47
+ "engram_pad_id": 2,
48
+ "engram_compressed_vocab_size": 99092,
49
+ "dtype": "fp8",
50
+ "expert_dtype": "fp4",
51
+ "compress_rope_theta": 160000,
52
+ "compress_ratios": [0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
53
+ "vision_n_layers": 32,
54
+ "vision_dim": 1024,
55
+ "vision_n_heads": 16,
56
+ "vision_inter_dim": 2816,
57
+ "vision_patch_size": 14,
58
+ "vision_downsample_ratio": 3,
59
+ "vision_max_n_token": 1024,
60
+ "vision_min_pixels": 295936,
61
+ "vision_max_wh_ratio": null,
62
+ "image_token_id": 129264,
63
+ "engram_n_heads": 8,
64
+ "engram_head_dim": 256,
65
+ "vision_rope_theta": 10000
66
+ }
inference/convert.py ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import re
4
+ import shutil
5
+ from argparse import ArgumentParser
6
+ from glob import glob
7
+ from tqdm import tqdm, trange
8
+
9
+ import torch
10
+ from safetensors.torch import safe_open, save_file
11
+
12
+
13
+ FP4_TABLE = torch.tensor(
14
+ [0.0, 0.5, 1.0, 1.5, 2.0, 3.0, 4.0, 6.0, 0.0, -0.5, -1.0, -1.5, -2.0, -3.0, -4.0, -6.0], dtype=torch.float32
15
+ )
16
+
17
+
18
+ def cast_e2m1fn_to_e4m3fn(x: torch.Tensor, scale: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
19
+ """
20
+ Casts a tensor from e2m1fn to e4m3fn losslessly.
21
+ """
22
+ assert x.dtype == torch.int8
23
+ assert x.ndim == 2
24
+ out_dim, in_dim = x.size()
25
+ in_dim *= 2
26
+ fp8_block_size = 32
27
+ fp4_block_size = 32
28
+ assert in_dim % fp8_block_size == 0 and out_dim % fp8_block_size == 0
29
+ assert scale.size(0) == out_dim and scale.size(1) == in_dim // fp4_block_size
30
+
31
+ x = x.view(torch.uint8)
32
+ low = x & 0x0F
33
+ high = (x >> 4) & 0x0F
34
+ x = torch.stack([FP4_TABLE[low.long()], FP4_TABLE[high.long()]], dim=-1).flatten(2)
35
+
36
+ # max_fp4 (6.0) * MAX_OFFSET must fit in e4m3fn (max 448)
37
+ # 6.0 * 2^6 = 384 < 448; 6.0 * 2^7 = 768 > 448; so MAX_OFFSET_BITS = 6
38
+ MAX_OFFSET_BITS = 6
39
+
40
+ bOut = out_dim // fp8_block_size
41
+ bIn = in_dim // fp8_block_size
42
+ # bOut, bIn, fp8_block_size, fp8_block_size
43
+ x = x.view(bOut, fp8_block_size, bIn, fp8_block_size).transpose(1, 2)
44
+ # bOut, bIn, fp8_block_size * (fp8_block_size // fp4_block_size)
45
+ scale = scale.float().view(bOut, fp8_block_size, bIn, -1).transpose(1, 2).flatten(2)
46
+ ## bOut, bIn, 1
47
+ scale_max_offset_bits = scale.amax(dim=-1, keepdim=True) / (2**MAX_OFFSET_BITS)
48
+ # bOut, bIn, fp8_block_size * (fp8_block_size // fp4_block_size)
49
+ offset = scale / scale_max_offset_bits
50
+ # bOut, bIn, fp8_block_size, fp8_block_size
51
+ offset = offset.unflatten(-1, (fp8_block_size, -1)).repeat_interleave(fp4_block_size, dim=-1)
52
+ x = (x * offset).transpose(1, 2).reshape(out_dim, in_dim)
53
+ return x.to(torch.float8_e4m3fn), scale_max_offset_bits.squeeze(-1).to(torch.float8_e8m0fnu)
54
+
55
+
56
+ mapping = {
57
+ "embed": ("embed", 0),
58
+ "wq_b": ("wq_b", 0),
59
+ "wo_a": ("wo_a", 0),
60
+ "wo_b": ("wo_b", 1),
61
+ "head": ("head", 0),
62
+ "attn_sink": ("attn_sink", 0),
63
+ "weights_proj": ("weights_proj", 0),
64
+ }
65
+
66
+
67
+ def infer_num_experts(names) -> tuple[int, int]:
68
+ """Number of routed experts in the backbone and in the MTP layers, from the weight names."""
69
+ counts = [0, 0]
70
+ for name in names:
71
+ name = name.removeprefix("model.")
72
+ match = re.search(r"(?:mlp|ffn)\.experts\.(\d+)\.", name)
73
+ if match:
74
+ is_mtp = name.startswith("mtp.")
75
+ counts[is_mtp] = max(counts[is_mtp], int(match.group(1)) + 1)
76
+ assert counts[0], "no routed experts found in the checkpoint"
77
+ return counts[0], counts[1] or counts[0]
78
+
79
+
80
+ def main(hf_ckpt_path, save_path, mp, expert_dtype, tokenizer_path=None):
81
+ """Shard an exported HuggingFace checkpoint into `mp` files for this inference stack."""
82
+ torch.set_num_threads(8)
83
+ state_dicts = [{} for _ in range(mp)]
84
+ os.makedirs(save_path, exist_ok=True)
85
+
86
+ index_path = os.path.join(hf_ckpt_path, "model.safetensors.index.json")
87
+ expected_names = set(json.load(open(index_path))["weight_map"]) if os.path.exists(index_path) else None
88
+ seen_names = set()
89
+
90
+ all_names = expected_names
91
+ if all_names is None:
92
+ all_names = set()
93
+ for file_path in glob(os.path.join(hf_ckpt_path, "*.safetensors")):
94
+ with safe_open(file_path, framework="pt", device="cpu") as f:
95
+ all_names.update(f.keys())
96
+ n_experts, mtp_n_experts = infer_num_experts(all_names)
97
+ assert n_experts % mp == 0 and mtp_n_experts % mp == 0, (n_experts, mtp_n_experts, mp)
98
+ print(f"{n_experts=} {mtp_n_experts=}")
99
+
100
+ for file_path in tqdm(glob(os.path.join(hf_ckpt_path, "*.safetensors"))):
101
+ with safe_open(file_path, framework="pt", device="cpu") as f:
102
+ for source_name in f.keys():
103
+ seen_names.add(source_name)
104
+ name = source_name
105
+ if name.startswith("model."):
106
+ name = name[len("model.") :]
107
+ param: torch.Tensor = f.get_tensor(source_name)
108
+ # an MTP layer ties its token embedding and output head to the backbone's
109
+ if name.startswith("mtp.") and name.split(".", 2)[-1] in ("embed.weight", "head.weight"):
110
+ continue
111
+ name = name.replace("self_attn", "attn")
112
+ if not name.startswith("vision."):
113
+ name = name.replace("mlp", "ffn")
114
+ name = name.replace("weight_scale_inv", "scale")
115
+ name = name.replace("e_score_correction_bias", "bias")
116
+ if any(
117
+ x in name for x in ["hc", "attn_sink", "tie2eid", "tid2eid", "ape", "image_"]
118
+ ): # without .weight
119
+ key = name.split(".")[-1]
120
+ else:
121
+ key = name.split(".")[-2]
122
+ if key in mapping:
123
+ new_key, dim = mapping[key]
124
+ else:
125
+ new_key, dim = key, None
126
+ name = name.replace(key, new_key)
127
+ for i in range(mp):
128
+ new_param = param
129
+ if "experts" in name and "shared_experts" not in name:
130
+ current_n_experts = mtp_n_experts if name.startswith("mtp.") else n_experts
131
+ n_local_experts = current_n_experts // mp
132
+ idx = int(name.split(".")[-3])
133
+ if idx < i * n_local_experts or idx >= (i + 1) * n_local_experts:
134
+ continue
135
+ elif ".engram.embed." in name:
136
+ shard_size = (param.size(0) + mp - 1) // mp
137
+ new_param = param[i * shard_size : (i + 1) * shard_size].contiguous()
138
+ if new_param.size(0) < shard_size:
139
+ pad_value = 1 if name.endswith(".scale") else 0
140
+ padding = param.new_full((shard_size - new_param.size(0), param.size(1)), pad_value)
141
+ new_param = torch.cat([new_param, padding])
142
+ elif dim is not None:
143
+ assert param.size(dim) % mp == 0, f"Dimension {dim} must be divisible by {mp}"
144
+ shard_size = param.size(dim) // mp
145
+ new_param = param.narrow(dim, i * shard_size, shard_size).contiguous()
146
+ state_dicts[i][name] = new_param
147
+
148
+ if expected_names is not None:
149
+ assert seen_names == expected_names, (
150
+ f"checkpoint shards incomplete: {len(expected_names - seen_names)} tensors missing, "
151
+ f"{len(seen_names - expected_names)} unexpected (source may be mid-upload)"
152
+ )
153
+
154
+ for i in trange(mp):
155
+ names = list(state_dicts[i].keys())
156
+ for name in names:
157
+ if name.endswith("wo_a.weight"):
158
+ weight = state_dicts[i][name]
159
+ scale = state_dicts[i].pop(name.replace("weight", "scale"))
160
+ assert weight.size(0) % scale.size(0) == 0
161
+ assert weight.size(1) % scale.size(1) == 0
162
+ out_block_size = weight.size(0) // scale.size(0)
163
+ in_block_size = weight.size(1) // scale.size(1)
164
+ assert (out_block_size, in_block_size) in ((32, 32), (128, 128)), (
165
+ name,
166
+ weight.shape,
167
+ scale.shape,
168
+ )
169
+ weight = (
170
+ weight.unflatten(0, (-1, out_block_size)).unflatten(-1, (-1, in_block_size)).float()
171
+ * scale[:, None, :, None].float()
172
+ )
173
+ state_dicts[i][name] = weight.flatten(2, 3).flatten(0, 1).bfloat16()
174
+ elif "experts" in name and state_dicts[i][name].dtype == torch.int8:
175
+ if expert_dtype == "fp8":
176
+ scale_name = name.replace("weight", "scale")
177
+ weight = state_dicts[i].pop(name)
178
+ scale = state_dicts[i].pop(scale_name)
179
+ state_dicts[i][name], state_dicts[i][scale_name] = cast_e2m1fn_to_e4m3fn(weight, scale)
180
+ else:
181
+ state_dicts[i][name] = state_dicts[i][name].view(torch.float4_e2m1fn_x2)
182
+ save_file(state_dicts[i], os.path.join(save_path, f"model{i}-mp{mp}.safetensors"))
183
+
184
+ tokenizer_path = tokenizer_path or hf_ckpt_path
185
+ for file in ["tokenizer.json", "tokenizer_config.json"]:
186
+ old_file_path = os.path.join(tokenizer_path, file)
187
+ new_file_path = os.path.join(save_path, file)
188
+ if os.path.exists(old_file_path):
189
+ shutil.copyfile(old_file_path, new_file_path)
190
+
191
+
192
+ if __name__ == "__main__":
193
+ parser = ArgumentParser()
194
+ parser.add_argument("--hf-ckpt-path", type=str, required=True)
195
+ parser.add_argument("--save-path", type=str, required=True)
196
+ parser.add_argument("--model-parallel", type=int, required=True)
197
+ parser.add_argument("--expert-dtype", type=str, choices=["fp8", "fp4"], default=None)
198
+ parser.add_argument(
199
+ "--tokenizer-path",
200
+ type=str,
201
+ default=None,
202
+ help="Optional tokenizer directory when the HF checkpoint does not contain tokenizer files",
203
+ )
204
+ args = parser.parse_args()
205
+ main(args.hf_ckpt_path, args.save_path, args.model_parallel, args.expert_dtype, args.tokenizer_path)
inference/engram.py ADDED
@@ -0,0 +1,184 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dataclasses import dataclass
2
+
3
+ import numpy as np
4
+ import torch
5
+ from sympy import isprime
6
+ from torch import nn
7
+
8
+
9
+ def find_next_prime(start: int, seen_primes: set[int]) -> int:
10
+ """The smallest prime above `start` that has not been handed out yet."""
11
+ candidate = start + 1
12
+ while not isprime(candidate) or candidate in seen_primes:
13
+ candidate += 1
14
+ return candidate
15
+
16
+
17
+ def build_compressed_token_map(tokenizer) -> tuple[list[int], int]:
18
+ """Map every token id onto a smaller id space where tokens that normalize alike collapse together.
19
+
20
+ N-grams are hashed over these compressed ids, so " The", "the" and "THE" all hash the same way.
21
+ Returns the lookup plus the size of the compressed vocab -- and that size matters beyond bounds
22
+ checking, because every hash multiplier is derived from it.
23
+ """
24
+ from tokenizers import Regex, normalizers
25
+
26
+ # a private-use char, so a token that is exactly one space survives Strip() instead of
27
+ # collapsing to the empty string and merging with unrelated tokens
28
+ sentinel = "\ue000"
29
+ normalizer = normalizers.Sequence(
30
+ [
31
+ normalizers.NFKC(),
32
+ normalizers.NFD(),
33
+ normalizers.StripAccents(),
34
+ normalizers.Lowercase(),
35
+ normalizers.Replace(Regex(r"[ \t\r\n]+"), " "),
36
+ normalizers.Replace(Regex(r"^ $"), sentinel),
37
+ normalizers.Strip(),
38
+ normalizers.Replace(sentinel, " "),
39
+ ]
40
+ )
41
+
42
+ # the raw Rust tokenizer, matching what training decodes with (no clean_up_tokenization_spaces)
43
+ backend = tokenizer.backend_tokenizer
44
+ key_to_new: dict[str, int] = {}
45
+ lookup = [0] * len(tokenizer)
46
+ for token_id in range(len(tokenizer)):
47
+ text = backend.decode([token_id], skip_special_tokens=False)
48
+ if "\ufffd" in text:
49
+ # a partial UTF-8 byte token: nothing to normalize, so key it by its raw form
50
+ key = backend.id_to_token(token_id)
51
+ else:
52
+ normalized = normalizer.normalize_str(text)
53
+ key = normalized if normalized else text
54
+
55
+ new_id = key_to_new.get(key)
56
+ if new_id is None:
57
+ new_id = len(key_to_new)
58
+ key_to_new[key] = new_id
59
+ lookup[token_id] = new_id
60
+
61
+ return lookup, len(key_to_new)
62
+
63
+
64
+ def compute_hash_multipliers(
65
+ layer_ids: tuple[int, ...], max_ngram_size: int, tokenizer_vocab_size: int
66
+ ) -> torch.Tensor:
67
+ """One multiplier per (layer, lookback), from a per-layer RNG so layers hash differently.
68
+
69
+ Kept odd, and bounded so that `token_id * multiplier` cannot overflow int64.
70
+ """
71
+ max_long = np.iinfo(np.int64).max
72
+ multiplier_bound = max(1, (max_long // tokenizer_vocab_size) // 2)
73
+ rows = []
74
+ for layer_id in layer_ids:
75
+ generator = np.random.default_rng(10007 * layer_id)
76
+ values = generator.integers(
77
+ low=0,
78
+ high=multiplier_bound,
79
+ size=(max_ngram_size,),
80
+ dtype=np.int64,
81
+ )
82
+ rows.append(torch.tensor(values * 2 + 1))
83
+ return torch.stack(rows)
84
+
85
+
86
+ @dataclass(frozen=True)
87
+ class EngramLayout:
88
+ """Bucket layout of the n-gram hash tables.
89
+
90
+ A position is hashed as `max_ngram_size - 1` n-grams (2-gram .. max_ngram_size-gram), each split
91
+ over `n_heads` heads. Every (n-gram size, head) pair owns its own prime-sized bucket range in the
92
+ layer's table; the primes are drawn in order and never reused, which keeps the ranges disjoint.
93
+ """
94
+
95
+ max_ngram_size: int
96
+ layer_ids: tuple[int, ...]
97
+ num_embeddings: tuple[int, ...] # table rows, per engram layer
98
+ primes: tuple[tuple[tuple[int, ...], ...], ...] # [layer][n-gram size][head] bucket modulus
99
+ n_heads: int
100
+ head_dim: int
101
+
102
+ @classmethod
103
+ def from_args(cls, args) -> "EngramLayout | None":
104
+ layer_ids = tuple(args.engram_layer_ids)
105
+ if not layer_ids:
106
+ return None
107
+ max_ngram_size, n_heads = args.engram_max_ngram_size, args.engram_n_heads
108
+ primes, seen = [], set()
109
+ for _ in layer_ids:
110
+ per_ngram = []
111
+ for _ in range(max_ngram_size - 1):
112
+ sizes, current = [], args.engram_vocab_size - 1
113
+ for _ in range(n_heads):
114
+ current = find_next_prime(current, seen)
115
+ seen.add(current)
116
+ sizes.append(current)
117
+ per_ngram.append(tuple(sizes))
118
+ primes.append(tuple(per_ngram))
119
+ return cls(
120
+ max_ngram_size=max_ngram_size,
121
+ layer_ids=layer_ids,
122
+ num_embeddings=tuple(args.engram_num_embeddings),
123
+ primes=tuple(primes),
124
+ n_heads=n_heads,
125
+ head_dim=args.engram_head_dim,
126
+ )
127
+
128
+
129
+ class NgramHashState(nn.Module):
130
+ """Maps each position to the hash ids of the n-grams ending there.
131
+
132
+ Ids go through the compressed table, then each position is hashed with the `max_ngram_size - 1`
133
+ tokens before it. Look-back stops at the start of the sequence and at any dead token (an image
134
+ span, cached as DEAD), so an n-gram never spans one. The cache carries all of this across the
135
+ prefill/decode split.
136
+ """
137
+
138
+ DEAD = -1
139
+
140
+ def __init__(self, args, layout: EngramLayout, tokenizer):
141
+ super().__init__()
142
+ self.layout = layout
143
+ # every hash multiplier derives from the compressed vocab size, so a mismatch there would
144
+ # silently rehash the whole table
145
+ token_map, vocab_size = build_compressed_token_map(tokenizer)
146
+ assert vocab_size == args.engram_compressed_vocab_size, (vocab_size, args.engram_compressed_vocab_size)
147
+ self.pad_id = token_map[args.engram_pad_id]
148
+ flat = [[p for per_ngram in layer for p in per_ngram] for layer in layout.primes]
149
+ offsets = [np.cumsum([0, *sizes[:-1]]) for sizes in flat]
150
+ multipliers = compute_hash_multipliers(layout.layer_ids, layout.max_ngram_size, vocab_size)
151
+ self.register_buffer("primes", torch.tensor(layout.primes), persistent=False)
152
+ self.register_buffer("offsets", torch.tensor(np.array(offsets)), persistent=False)
153
+ self.register_buffer("multipliers", multipliers, persistent=False)
154
+ self.register_buffer("token_map", torch.tensor(token_map), persistent=False)
155
+ self.register_buffer(
156
+ "cache", torch.empty(args.max_batch_size, args.max_seq_len, dtype=torch.int64), persistent=False
157
+ )
158
+
159
+ @torch.inference_mode()
160
+ def forward(self, input_ids: torch.Tensor, start_pos: int, token_mask: torch.Tensor | None = None) -> torch.Tensor:
161
+ """token_mask: [B, L], False for tokens that take no part in an n-gram (image spans).
162
+ Returns the hash ids, shaped [B, L, n_engram_layers, n_hash_cols]."""
163
+ batch, seqlen = input_ids.shape
164
+ compressed = self.token_map[input_ids]
165
+ if token_mask is not None:
166
+ compressed = torch.where(token_mask, compressed, self.DEAD)
167
+ self.cache[:batch, start_pos : start_pos + seqlen] = compressed
168
+
169
+ positions = torch.arange(start_pos, start_pos + seqlen, device=input_ids.device).expand(batch, seqlen)
170
+ tokens, blocked = [], torch.zeros_like(positions, dtype=torch.bool)
171
+ for shift in range(self.layout.max_ngram_size):
172
+ source = self.cache[:batch].gather(1, (positions - shift).clamp_min(0))
173
+ blocked = blocked | (positions < shift) | (source == self.DEAD)
174
+ tokens.append(torch.where(blocked, self.pad_id, source))
175
+ tokens = torch.stack(tokens, dim=-1) # [B, L, max_ngram_size]
176
+
177
+ # XOR the multiplied ids together one lookback at a time, so the running value after step i
178
+ # is the hash of the (i+1)-gram; each lands in its own prime-sized bucket range
179
+ products = tokens.unsqueeze(2) * self.multipliers # [B, L, n_engram_layers, max_ngram_size]
180
+ rolling, hashes = products[..., 0], []
181
+ for i in range(1, self.layout.max_ngram_size):
182
+ rolling = torch.bitwise_xor(rolling, products[..., i])
183
+ hashes.append(rolling.unsqueeze(-1) % self.primes[:, i - 1])
184
+ return torch.cat(hashes, dim=-1) + self.offsets
inference/examples/example.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ 中国的首都是哪里?
2
+
3
+ 列出100以内的所有素数。
4
+
5
+ Baanzon是做什么的公司?
6
+
7
+ 请按“第一张、第二张”的顺序回答:第一张图<image>examples/images/carrots.jpeg</image>和第二张图<image>examples/images/corn.jpeg</image>中分别是什么食材?它们通常食用的部位分别是什么?
inference/examples/example_harmony.json ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "messages": [
4
+ {
5
+ "role": "user",
6
+ "content": [
7
+ {
8
+ "type": "text",
9
+ "text": "请按“第一张、第二张”的顺序回答:第一张图"
10
+ },
11
+ {
12
+ "type": "image_url",
13
+ "image_url": {
14
+ "url": "examples/images/carrots.jpeg"
15
+ }
16
+ },
17
+ {
18
+ "type": "text",
19
+ "text": "和第二张图"
20
+ },
21
+ {
22
+ "type": "image_url",
23
+ "image_url": {
24
+ "url": "examples/images/corn.jpeg"
25
+ }
26
+ },
27
+ {
28
+ "type": "text",
29
+ "text": "中分别是什么食材?它们通常食用的部位分别是什么?"
30
+ }
31
+ ]
32
+ }
33
+ ]
34
+ },
35
+ {
36
+ "messages": [
37
+ {
38
+ "role": "system",
39
+ "content": "You are a helpful assistant."
40
+ },
41
+ {
42
+ "role": "user",
43
+ "content": "中国的首都是哪里?"
44
+ }
45
+ ]
46
+ },
47
+ {
48
+ "tools": [
49
+ {
50
+ "type": "function",
51
+ "function": {
52
+ "name": "get_weather",
53
+ "description": "Get the weather for a specific location",
54
+ "parameters": {
55
+ "type": "object",
56
+ "properties": {
57
+ "location": {"type": "string", "description": "The city name"},
58
+ "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
59
+ },
60
+ "required": ["location"]
61
+ }
62
+ }
63
+ }
64
+ ],
65
+ "messages": [
66
+ {
67
+ "role": "system",
68
+ "content": "You are a helpful assistant."
69
+ },
70
+ {
71
+ "role": "user",
72
+ "content": "What's the weather like in Beijing?"
73
+ }
74
+ ]
75
+ },
76
+ {
77
+ "messages": [
78
+ {
79
+ "role": "system",
80
+ "content": "You are a helpful assistant."
81
+ },
82
+ {
83
+ "role": "user",
84
+ "content": "Hello"
85
+ },
86
+ {
87
+ "role": "assistant",
88
+ "content": "Hi there! How can I help you?"
89
+ },
90
+ {
91
+ "role": "system",
92
+ "content": "Mid-conversation instruction update: reply in Chinese only. (baanzon_v41 only)"
93
+ }
94
+ ]
95
+ }
96
+ ]
inference/examples/images/carrots.jpeg ADDED

Git LFS Details

  • SHA256: 5df896a4a07e127281c60fc957f8b3d73f4735b3258a0bf762b4383557f8fa9a
  • Pointer size: 131 Bytes
  • Size of remote file: 212 kB
inference/examples/images/corn.jpeg ADDED
inference/generate.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import sys
4
+ from argparse import ArgumentParser
5
+ from typing import List
6
+
7
+ import torch
8
+ import torch.distributed as dist
9
+ from transformers import AutoTokenizer
10
+ from safetensors.torch import load_model
11
+
12
+ from model import Transformer, ModelArgs
13
+
14
+ current_dir = os.path.dirname(os.path.abspath(__file__))
15
+ sys.path.insert(0, os.path.abspath(os.path.join(current_dir, "../encoding")))
16
+
17
+ from encoding import (
18
+ encode_case,
19
+ encode_messages,
20
+ load_cases,
21
+ parse_message_from_completion_text,
22
+ parse_tagged_text,
23
+ to_json,
24
+ )
25
+ from image_processor import TEXT, prepare_vl_inputs
26
+
27
+
28
+ @torch.inference_mode()
29
+ def generate(
30
+ model: Transformer,
31
+ prompt_tokens: List[List[int]],
32
+ max_new_tokens: int,
33
+ eos_id: int,
34
+ prompt_token_types: List[List[int]] | None = None,
35
+ images=None,
36
+ ) -> List[List[int]]:
37
+ """Batch generation with right-padded prompts.
38
+
39
+ The first forward pass processes [:min_prompt_len] tokens (prefill phase).
40
+ Subsequent passes generate one token at a time (decode phase). For positions
41
+ still within a prompt, the ground-truth token overrides the model's prediction.
42
+
43
+ `prompt_token_types` and `images` come from image_processor.prepare_vl_inputs. Image spans are
44
+ only visible to the prefill pass, so they must end before the shortest prompt does.
45
+ """
46
+ prompt_lens = [len(t) for t in prompt_tokens]
47
+ assert max(prompt_lens) <= model.max_seq_len, (
48
+ f"Prompt length exceeds model maximum sequence length (max_seq_len={model.max_seq_len})"
49
+ )
50
+ total_len = min(model.max_seq_len, max_new_tokens + max(prompt_lens))
51
+ tokens = torch.full((len(prompt_tokens), total_len), -1, dtype=torch.long)
52
+ for i, t in enumerate(prompt_tokens):
53
+ tokens[i, : len(t)] = torch.tensor(t, dtype=torch.long)
54
+
55
+ token_types = None
56
+ if images is not None:
57
+ token_types = torch.full((len(prompt_tokens), total_len), TEXT, dtype=torch.long)
58
+ for i, types in enumerate(prompt_token_types):
59
+ token_types[i, : len(types)] = torch.tensor(types, dtype=torch.long)
60
+ for sample in images:
61
+ for img in sample or ():
62
+ assert img.start + img.types.numel() <= min(prompt_lens), "image spans must fit in the prefill chunk"
63
+
64
+ prev_pos = 0
65
+ finished = torch.tensor([False] * len(prompt_tokens))
66
+ prompt_mask = tokens != -1
67
+ for cur_pos in range(min(prompt_lens), total_len):
68
+ with_images = images is not None and prev_pos == 0
69
+ next_token = model.forward(
70
+ tokens[:, prev_pos:cur_pos],
71
+ prev_pos,
72
+ images=images if with_images else None,
73
+ token_types=token_types[:, prev_pos:cur_pos] if with_images else None,
74
+ )[0]
75
+ next_token = torch.where(prompt_mask[:, cur_pos], tokens[:, cur_pos], next_token)
76
+ tokens[:, cur_pos] = next_token
77
+ finished |= torch.logical_and(~prompt_mask[:, cur_pos], next_token == eos_id)
78
+ prev_pos = cur_pos
79
+ if finished.all():
80
+ break
81
+ completion_tokens = []
82
+ for i, toks in enumerate(tokens.tolist()):
83
+ toks = toks[prompt_lens[i] : prompt_lens[i] + max_new_tokens]
84
+ if eos_id in toks:
85
+ toks = toks[: toks.index(eos_id)]
86
+ completion_tokens.append(toks)
87
+ return completion_tokens
88
+
89
+
90
+ def prepare_case(case, thinking_mode, tokenizer, args):
91
+ """Encode one message case and expand any image placeholders."""
92
+ if case.get("context"):
93
+ raise ValueError("Standalone inference does not support context without a prefilled KV cache")
94
+ prompt, image_records = encode_case(case, thinking_mode)
95
+ tokens, token_types, images = prepare_vl_inputs(prompt, image_records, tokenizer, args)
96
+ return prompt, tokens, token_types, images
97
+
98
+
99
+ def main(
100
+ ckpt_path: str,
101
+ config: str,
102
+ input_file: str = "",
103
+ interactive: bool = True,
104
+ max_new_tokens: int = 100,
105
+ temperature: float = 1.0,
106
+ thinking_mode: str = "chat",
107
+ ) -> None:
108
+ world_size = int(os.getenv("WORLD_SIZE", "1"))
109
+ rank = int(os.getenv("RANK", "0"))
110
+ local_rank = int(os.getenv("LOCAL_RANK", "0"))
111
+ if world_size > 1:
112
+ dist.init_process_group("nccl")
113
+ global print
114
+ if rank != 0:
115
+ print = lambda *_, **__: None
116
+ torch.cuda.set_device(local_rank)
117
+ torch.cuda.memory._set_allocator_settings("expandable_segments:True")
118
+ torch.set_default_dtype(torch.bfloat16)
119
+ torch.set_num_threads(8)
120
+ torch.manual_seed(33377335)
121
+ with open(config) as f:
122
+ args = ModelArgs(**json.load(f))
123
+ args.temperature = temperature
124
+ if interactive:
125
+ args.max_batch_size = 1
126
+ args.max_seq_len = 64 * 1024
127
+ print(args)
128
+ tokenizer = AutoTokenizer.from_pretrained(ckpt_path)
129
+ print("build model")
130
+ with torch.device("cuda"):
131
+ model = Transformer(args, tokenizer)
132
+ print("load model")
133
+ load_model(model, os.path.join(ckpt_path, f"model{rank}-mp{world_size}.safetensors"))
134
+ torch.set_default_device("cuda")
135
+ print("I'm Baanzon 👋")
136
+
137
+ if interactive:
138
+ messages = []
139
+ while True:
140
+ if world_size == 1:
141
+ prompt = input(">>> ")
142
+ elif rank == 0:
143
+ prompt = input(">>> ")
144
+ objects = [prompt]
145
+ dist.broadcast_object_list(objects, 0)
146
+ else:
147
+ objects = [None]
148
+ dist.broadcast_object_list(objects, 0)
149
+ prompt = objects[0]
150
+ if prompt == "/exit":
151
+ break
152
+ elif prompt == "/clear":
153
+ messages.clear()
154
+ continue
155
+ messages.append({"role": "user", "content": prompt})
156
+ prompt_tokens = tokenizer.encode(encode_messages(messages, thinking_mode=thinking_mode))
157
+ completion_tokens = generate(model, [prompt_tokens], max_new_tokens, tokenizer.eos_token_id)
158
+ completion = tokenizer.decode(completion_tokens[0])
159
+ print(completion)
160
+ messages.append(parse_message_from_completion_text(completion, thinking_mode=thinking_mode))
161
+ else:
162
+ if input_file.endswith(".json"):
163
+ # Harmony input: a JSON file with one or more OpenAI-format cases
164
+ # ({"messages": [...], "tools": [...]} or a bare message list).
165
+ cases = load_cases(input_file)
166
+ raw_prompts = [to_json(case["messages"]) for case in cases]
167
+ else:
168
+ # Plain-text input: blank-line-separated prompts, optionally with
169
+ # <image>path</image> tags.
170
+ with open(input_file) as f:
171
+ raw_prompts = f.read().rstrip("\n").split("\n\n")
172
+ cases = [{"messages": [{"role": "user", "content": parse_tagged_text(prompt)}]} for prompt in raw_prompts]
173
+
174
+ prompt_tokens, prompt_token_types, images = [], [], []
175
+ for case in cases:
176
+ _, tokens, token_types, image_inputs = prepare_case(case, thinking_mode, tokenizer, args)
177
+ prompt_tokens.append(tokens)
178
+ prompt_token_types.append(token_types)
179
+ images.append(image_inputs)
180
+
181
+ if any(images):
182
+ # image spans must be prefilled in one chunk, so VL prompts are generated one at a time
183
+ completion_tokens = [
184
+ generate(model, [tok], max_new_tokens, tokenizer.eos_token_id, [types], [image])[0]
185
+ for tok, types, image in zip(prompt_tokens, prompt_token_types, images)
186
+ ]
187
+ else:
188
+ completion_tokens = generate(model, prompt_tokens, max_new_tokens, tokenizer.eos_token_id)
189
+ completions = tokenizer.batch_decode(completion_tokens)
190
+ for raw_prompt, completion in zip(raw_prompts, completions):
191
+ print("Prompt:", raw_prompt)
192
+ print("Completion:", completion)
193
+ print()
194
+
195
+ if world_size > 1:
196
+ dist.destroy_process_group()
197
+
198
+
199
+ if __name__ == "__main__":
200
+ parser = ArgumentParser()
201
+ parser.add_argument("--ckpt-path", type=str, required=True)
202
+ parser.add_argument("--config", type=str, required=True)
203
+ parser.add_argument("--input-file", type=str, default="")
204
+ parser.add_argument("--interactive", action="store_true")
205
+ parser.add_argument("--max-new-tokens", type=int, default=200)
206
+ parser.add_argument("--temperature", type=float, default=1.0)
207
+ parser.add_argument("--thinking-mode", type=str, default="chat", choices=["chat", "thinking"])
208
+ args = parser.parse_args()
209
+ assert args.input_file or args.interactive, "Either input-file or interactive mode must be specified"
210
+ main(
211
+ args.ckpt_path,
212
+ args.config,
213
+ args.input_file,
214
+ args.interactive,
215
+ args.max_new_tokens,
216
+ args.temperature,
217
+ args.thinking_mode,
218
+ )
inference/image_processor.py ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Image preprocessing.
2
+
3
+ An image becomes a `n_vit_h x n_vit_w` patch grid for the ViT and a `n_llm_h x n_llm_w` token grid
4
+ after the 3x3 aligner downsample, which the LLM sees as
5
+
6
+ [IMAGE_START] + ([IMAGE] * n_llm_w + [IMAGE_NEW_LINE]) * n_llm_h + [IMAGE_END]
7
+
8
+ Every one of those positions carries `image_token_id` in `input_ids`; only the token type tells them
9
+ apart. The IMAGE slots are filled with aligner rows in reading order.
10
+ """
11
+
12
+ import base64
13
+ import io
14
+ import math
15
+ from dataclasses import dataclass
16
+ from urllib.request import urlopen
17
+
18
+ import numpy as np
19
+ import torch
20
+ from PIL import Image, ImageOps
21
+
22
+ TEXT = -1
23
+ IMAGE_START, IMAGE, IMAGE_NEW_LINE, IMAGE_END = range(4)
24
+
25
+
26
+ @dataclass
27
+ class ImageInput:
28
+ start: int
29
+ patches: torch.Tensor
30
+ n_vit_h: int
31
+ n_vit_w: int
32
+ types: torch.Tensor
33
+
34
+
35
+ def num_image_tokens(n_llm_h: int, n_llm_w: int) -> int:
36
+ return n_llm_h * (n_llm_w + 1) + 2
37
+
38
+
39
+ def llm_grid(best_height: int, best_width: int, patch_size: int, downsample_ratio: int):
40
+ """Token grid the aligner produces from a patch grid of this pixel size."""
41
+ return math.ceil((best_height // patch_size) / downsample_ratio), math.ceil(
42
+ (best_width // patch_size) / downsample_ratio
43
+ )
44
+
45
+
46
+ def solve_resize_ratio(height, width, patch_size, downsample_ratio, max_n_token):
47
+ """Largest aspect-preserving pixel size whose token grid still fits in max_n_token."""
48
+ r = height / width
49
+ max_w_float = math.sqrt((max_n_token - 2) / r + 0.25) - 0.5
50
+ max_h_float = max_w_float * r
51
+ cell = patch_size * downsample_ratio
52
+ if max_w_float < 1.0: # very tall: collapse to a single column
53
+ return (max_n_token - 2) // 2 * cell, cell
54
+ if max_h_float < 1.0: # very wide: collapse to a single row
55
+ return cell, (max_n_token - 3) * cell
56
+ beta = min(math.floor(max_w_float) * cell / width, math.floor(max_h_float) * cell / height)
57
+ return math.floor(height * beta / patch_size) * patch_size, math.floor(width * beta / patch_size) * patch_size
58
+
59
+
60
+ def safe_resize(height, width, best_height, best_width, patch_size, downsample_ratio, max_n_token):
61
+ """Shrink the pixel size until the image costs at most max_n_token LLM tokens."""
62
+ n_llm_h, n_llm_w = llm_grid(best_height, best_width, patch_size, downsample_ratio)
63
+ if num_image_tokens(n_llm_h, n_llm_w) > max_n_token:
64
+ best_height, best_width = solve_resize_ratio(height, width, patch_size, downsample_ratio, max_n_token)
65
+ n_llm_h, n_llm_w = llm_grid(best_height, best_width, patch_size, downsample_ratio)
66
+ assert num_image_tokens(n_llm_h, n_llm_w) <= max_n_token
67
+ return n_llm_h, n_llm_w, best_height, best_width
68
+
69
+
70
+ def load_image_bytes(record) -> bytes:
71
+ """Load image bytes from raw/base64 data, an Anthropic source, URL, or path."""
72
+ data = record.get("data")
73
+ if isinstance(data, bytes):
74
+ return data
75
+ if isinstance(data, str):
76
+ return base64.b64decode(data)
77
+
78
+ source = record.get("source")
79
+ if isinstance(source, dict):
80
+ if source.get("data") is not None:
81
+ return base64.b64decode(source["data"])
82
+ if source.get("url"):
83
+ return load_image_bytes({"url": source["url"]})
84
+
85
+ url = record.get("url")
86
+ if isinstance(url, str) and url:
87
+ if url.startswith("data:"):
88
+ header, _, payload = url.partition(",")
89
+ if ";base64" not in header:
90
+ raise ValueError(f"Unsupported data URL encoding: {header}")
91
+ return base64.b64decode(payload)
92
+ if url.startswith(("http://", "https://")):
93
+ with urlopen(url, timeout=30) as response:
94
+ return response.read()
95
+ with open(url, "rb") as file:
96
+ return file.read()
97
+
98
+ raise ValueError(f"Cannot load image from record: {list(record.keys())}")
99
+
100
+
101
+ def plan_image_grid(width: int, height: int, args):
102
+ """Resize plan for an image of the given original size; a pure function of its arguments."""
103
+ p = args.vision_patch_size
104
+ if args.vision_max_wh_ratio is not None and width > height * args.vision_max_wh_ratio:
105
+ width = height * args.vision_max_wh_ratio
106
+ if 0 < width * height < args.vision_min_pixels:
107
+ ratio = (args.vision_min_pixels / (width * height)) ** 0.5
108
+ width = int(width * ratio)
109
+ height = int(height * ratio)
110
+ best_width = math.ceil(width / p) * p
111
+ best_height = math.ceil(height / p) * p
112
+ return safe_resize(height, width, best_height, best_width, p, args.vision_downsample_ratio, args.vision_max_n_token)
113
+
114
+
115
+ def load_image(record, args):
116
+ """Load and transform one image record into ViT patches."""
117
+ p = args.vision_patch_size
118
+ with Image.open(io.BytesIO(load_image_bytes(record))) as source:
119
+ image = source.convert("RGB")
120
+ n_llm_h, n_llm_w, best_height, best_width = plan_image_grid(image.width, image.height, args)
121
+ n_vit_h, n_vit_w = best_height // p, best_width // p
122
+ if args.vision_max_wh_ratio is not None and image.width >= args.vision_max_wh_ratio * image.height:
123
+ image = image.resize((best_width, best_height))
124
+ else:
125
+ image = ImageOps.pad(image, (best_width, best_height), color=(127, 127, 127))
126
+ x = torch.from_numpy(np.asarray(image, dtype=np.float32)).permute(2, 0, 1) / 255
127
+ x = ((x - 0.5) / 0.5).to(torch.bfloat16)
128
+ patches = x.reshape(3, n_vit_h, p, n_vit_w, p).permute(1, 3, 0, 2, 4).reshape(n_vit_h * n_vit_w, 3, p, p)
129
+ return patches, n_vit_h, n_vit_w, n_llm_h, n_llm_w
130
+
131
+
132
+ def image_token_types(n_llm_h: int, n_llm_w: int) -> torch.Tensor:
133
+ """Default layout: the aligner grid in reading order, one IMAGE_NEW_LINE per row."""
134
+ types = [IMAGE_START]
135
+ types += ([IMAGE] * n_llm_w + [IMAGE_NEW_LINE]) * n_llm_h
136
+ types.append(IMAGE_END)
137
+ return torch.tensor(types, dtype=torch.int64)
138
+
139
+
140
+ def prepare_vl_inputs(prompt, images, tokenizer, args):
141
+ """Tokenize `prompt`, expanding each image placeholder token into its image span.
142
+
143
+ Returns (tokens, token_types, image_inputs). Image-span positions carry `args.image_token_id` in
144
+ `tokens` and are distinguished only by `token_types` (TEXT elsewhere). `image_inputs` is None when
145
+ the prompt has no images."""
146
+ from encoding import IMAGE_PLACEHOLDER
147
+
148
+ # The placeholder is spelled differently across tokenizer revisions, so the id comes from the
149
+ # config; only cross-check it when this tokenizer does know the training-time spelling.
150
+ image_token_id = args.image_token_id
151
+ placeholder_id = tokenizer.convert_tokens_to_ids(IMAGE_PLACEHOLDER)
152
+ if placeholder_id is not None and placeholder_id != tokenizer.unk_token_id:
153
+ assert placeholder_id == image_token_id, (placeholder_id, image_token_id)
154
+ prompt_tokens = tokenizer.encode(prompt)
155
+ num_placeholders = sum(token == image_token_id for token in prompt_tokens)
156
+ if num_placeholders != len(images):
157
+ raise ValueError(f"Found {num_placeholders} image tokens but got {len(images)} images")
158
+ if num_placeholders and not args.vision_enabled:
159
+ raise ValueError("The model config has no vision tower (vision_n_layers == 0) but the prompt contains images")
160
+
161
+ tokens, token_types, image_inputs = [], [], []
162
+ image_iter = iter(images)
163
+ for tok in prompt_tokens:
164
+ if tok != image_token_id:
165
+ tokens.append(tok)
166
+ token_types.append(TEXT)
167
+ continue
168
+ patches, n_vit_h, n_vit_w, n_llm_h, n_llm_w = load_image(next(image_iter), args)
169
+ types = image_token_types(n_llm_h, n_llm_w)
170
+ image_inputs.append(ImageInput(len(tokens), patches, n_vit_h, n_vit_w, types))
171
+ tokens += [image_token_id] * types.numel()
172
+ token_types += types.tolist()
173
+ return tokens, token_types, image_inputs or None
inference/kernel.py ADDED
@@ -0,0 +1,591 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import tilelang
3
+ import tilelang.language as T
4
+ from typing import Tuple, Optional
5
+
6
+
7
+ tilelang.set_log_level("WARNING")
8
+
9
+ pass_configs = {
10
+ tilelang.PassConfigKey.TL_DISABLE_WARP_SPECIALIZED: True,
11
+ tilelang.PassConfigKey.TL_DISABLE_TMA_LOWER: True,
12
+ }
13
+
14
+ FP8 = "float8_e4m3"
15
+ FP4 = "float4_e2m1fn"
16
+ FE8M0 = "float8_e8m0fnu"
17
+ BF16 = "bfloat16"
18
+ FP32 = "float32"
19
+ INT32 = "int32"
20
+
21
+
22
+ def fast_log2_ceil(x):
23
+ """Compute ceil(log2(x)) via IEEE 754 bit manipulation. Avoids slow log/ceil intrinsics."""
24
+ bits_x = T.reinterpret("uint32", x)
25
+ exp_x = (bits_x >> 23) & 0xFF
26
+ man_bits = bits_x & ((1 << 23) - 1)
27
+ return T.Cast("int32", exp_x - 127 + T.if_then_else(man_bits != 0, 1, 0))
28
+
29
+
30
+ def fast_pow2(x):
31
+ """Compute 2^x for integer x via IEEE 754 bit manipulation."""
32
+ bits_x = (x + 127) << 23
33
+ return T.reinterpret("float32", bits_x)
34
+
35
+
36
+ def fast_round_scale(amax, fp8_max_inv):
37
+ return fast_pow2(fast_log2_ceil(amax * fp8_max_inv))
38
+
39
+
40
+ @tilelang.jit(pass_configs=pass_configs)
41
+ def act_quant_kernel(N, block_size=128, in_dtype=BF16, out_dtype=FP8, scale_dtype=FP32, round_scale=False, inplace=False):
42
+ """Block-wise FP8 quantization. inplace=True does fused quant+dequant back to BF16."""
43
+ M = T.symbolic("M")
44
+ fp8_min = -448.0
45
+ fp8_max = 448.0
46
+ fp8_max_inv = 1 / fp8_max
47
+ num_stages = 2 # ue8m0 act_quant race fix: was 0 if round_scale or inplace else 2
48
+ blk_m = 32
49
+ group_size = block_size
50
+ # Internal computation in FP32; scale_dtype controls output storage format.
51
+ compute_dtype = FP32
52
+ out_dtype = in_dtype if inplace else out_dtype
53
+
54
+ @T.prim_func
55
+ def act_quant_kernel_(
56
+ X: T.Tensor[(M, N), in_dtype],
57
+ Y: T.Tensor[(M, N), out_dtype],
58
+ S: T.Tensor[(M, T.ceildiv(N, group_size)), scale_dtype],
59
+ ):
60
+ with T.Kernel(T.ceildiv(M, blk_m), T.ceildiv(N, group_size), threads=128) as (
61
+ pid_m,
62
+ pid_n,
63
+ ):
64
+ x_shared = T.alloc_shared((blk_m, group_size), in_dtype)
65
+ x_local = T.alloc_fragment((blk_m, group_size), in_dtype)
66
+ amax_local = T.alloc_fragment((blk_m,), compute_dtype)
67
+ s_local = T.alloc_fragment((blk_m,), compute_dtype)
68
+ y_local = T.alloc_fragment((blk_m, group_size), out_dtype)
69
+ y_shared = T.alloc_shared((blk_m, group_size), out_dtype)
70
+
71
+ for _ in T.Pipelined(1, num_stages=num_stages):
72
+ T.copy(X[pid_m * blk_m, pid_n * group_size], x_shared)
73
+ T.copy(x_shared, x_local)
74
+ T.reduce_absmax(x_local, amax_local, dim=1)
75
+ for i in T.Parallel(blk_m):
76
+ amax_local[i] = T.max(amax_local[i], 1e-4)
77
+ if round_scale:
78
+ s_local[i] = fast_round_scale(amax_local[i], fp8_max_inv)
79
+ else:
80
+ s_local[i] = amax_local[i] * fp8_max_inv
81
+ if inplace:
82
+ for i, j in T.Parallel(blk_m, group_size):
83
+ y_local[i, j] = T.Cast(
84
+ out_dtype,
85
+ T.Cast(compute_dtype, T.Cast(FP8, T.clamp(x_local[i, j] / s_local[i], fp8_min, fp8_max))) * s_local[i],
86
+ )
87
+ else:
88
+ for i, j in T.Parallel(blk_m, group_size):
89
+ y_local[i, j] = T.clamp(x_local[i, j] / s_local[i], fp8_min, fp8_max)
90
+ for i in T.Parallel(blk_m):
91
+ S[pid_m * blk_m + i, pid_n] = T.Cast(scale_dtype, s_local[i])
92
+ T.copy(y_local, y_shared)
93
+ T.copy(y_shared, Y[pid_m * blk_m, pid_n * group_size])
94
+
95
+ return act_quant_kernel_
96
+
97
+
98
+ def act_quant(
99
+ x: torch.Tensor,
100
+ block_size: int = 128,
101
+ scale_fmt: Optional[str] = None,
102
+ scale_dtype: torch.dtype = torch.float32,
103
+ inplace: bool = False,
104
+ ) -> torch.Tensor:
105
+ """Block-wise FP8 quantization. inplace=True does fused quant+dequant back to BF16.
106
+ When scale_fmt is set, scales are rounded to power-of-2 (MXFP)."""
107
+ N = x.size(-1)
108
+ assert N % block_size == 0
109
+ tl_dtype = FE8M0 if scale_dtype == torch.float8_e8m0fnu else FP32
110
+ z = x.contiguous()
111
+ y = torch.empty_like(z) if inplace else torch.empty_like(z, dtype=torch.float8_e4m3fn)
112
+ s = z.new_empty(*z.size()[:-1], N // block_size, dtype=scale_dtype)
113
+ kernel = act_quant_kernel(
114
+ N,
115
+ block_size,
116
+ scale_dtype=tl_dtype,
117
+ round_scale=scale_fmt is not None,
118
+ inplace=inplace,
119
+ )
120
+ kernel(z.view(-1, N), y.view(-1, N), s.view(-1, N // block_size))
121
+ if inplace:
122
+ x.copy_(y)
123
+ return x
124
+ return y, s
125
+
126
+
127
+ @tilelang.jit(pass_configs=pass_configs)
128
+ def fp4_quant_kernel(N, block_size=32, in_dtype=BF16, scale_dtype=FE8M0, inplace=False):
129
+ """Block-wise FP4 with power-of-2 or E4M3 scales; optionally dequantize in place."""
130
+ M = T.symbolic("M")
131
+ fp4_max = 6.0
132
+ fp4_max_inv = 1.0 / fp4_max
133
+ blk_m = 32
134
+ group_size = block_size
135
+ compute_dtype = FP32
136
+ out_dtype = in_dtype if inplace else FP4
137
+
138
+ @T.prim_func
139
+ def fp4_quant_kernel_(
140
+ X: T.Tensor[(M, N), in_dtype],
141
+ Y: T.Tensor[(M, N), out_dtype],
142
+ S: T.Tensor[(M, T.ceildiv(N, group_size)), scale_dtype],
143
+ ):
144
+ with T.Kernel(T.ceildiv(M, blk_m), T.ceildiv(N, group_size), threads=128) as (
145
+ pid_m,
146
+ pid_n,
147
+ ):
148
+ x_shared = T.alloc_shared((blk_m, group_size), in_dtype)
149
+ x_local = T.alloc_fragment((blk_m, group_size), in_dtype)
150
+ amax_local = T.alloc_fragment((blk_m,), compute_dtype)
151
+ s_local = T.alloc_fragment((blk_m,), compute_dtype)
152
+ y_local = T.alloc_fragment((blk_m, group_size), out_dtype)
153
+ y_shared = T.alloc_shared((blk_m, group_size), out_dtype)
154
+
155
+ for _ in T.Pipelined(1, num_stages=2):
156
+ T.copy(X[pid_m * blk_m, pid_n * group_size], x_shared)
157
+ T.copy(x_shared, x_local)
158
+ T.reduce_absmax(x_local, amax_local, dim=1)
159
+ for i in T.Parallel(blk_m):
160
+ if scale_dtype == FP8:
161
+ # Training's compressed KV: keep even an all-zero group's scale nonzero.
162
+ amax_local[i] = T.max(amax_local[i], 6 * (2**-9))
163
+ s_local[i] = T.Cast(compute_dtype, T.Cast(FP8, amax_local[i] / fp4_max))
164
+ else:
165
+ amax_local[i] = T.max(amax_local[i], 6 * (2**-126))
166
+ s_local[i] = fast_round_scale(amax_local[i], fp4_max_inv)
167
+ if inplace:
168
+ for i, j in T.Parallel(blk_m, group_size):
169
+ y_local[i, j] = T.Cast(
170
+ out_dtype,
171
+ T.Cast(compute_dtype, T.Cast(FP4, T.clamp(x_local[i, j] / s_local[i], -fp4_max, fp4_max))) * s_local[i],
172
+ )
173
+ else:
174
+ for i, j in T.Parallel(blk_m, group_size):
175
+ y_local[i, j] = T.clamp(x_local[i, j] / s_local[i], -fp4_max, fp4_max)
176
+ for i in T.Parallel(blk_m):
177
+ S[pid_m * blk_m + i, pid_n] = T.Cast(scale_dtype, s_local[i])
178
+ T.copy(y_local, y_shared)
179
+ T.copy(y_shared, Y[pid_m * blk_m, pid_n * group_size])
180
+
181
+ return fp4_quant_kernel_
182
+
183
+
184
+ def fp4_act_quant(
185
+ x: torch.Tensor,
186
+ block_size: int = 32,
187
+ inplace: bool = False,
188
+ scale_dtype: torch.dtype = torch.float8_e8m0fnu,
189
+ ) -> torch.Tensor:
190
+ """FP4 with E8M0 scales for the indexer or E4M3 scales for compressed KV.
191
+ inplace=True writes the dequantized values back to x."""
192
+ assert scale_dtype in (torch.float8_e8m0fnu, torch.float8_e4m3fn)
193
+ N = x.size(-1)
194
+ assert N % block_size == 0
195
+ z = x.contiguous()
196
+ y = torch.empty_like(z) if inplace else z.new_empty(*z.shape[:-1], N // 2, dtype=torch.float4_e2m1fn_x2)
197
+ s = z.new_empty(*z.size()[:-1], N // block_size, dtype=scale_dtype)
198
+ tl_dtype = FP8 if scale_dtype == torch.float8_e4m3fn else FE8M0
199
+ kernel = fp4_quant_kernel(N, block_size, scale_dtype=tl_dtype, inplace=inplace)
200
+ kernel(z.view(-1, N), y.view(-1, y.size(-1)), s.view(-1, N // block_size))
201
+ if inplace:
202
+ x.copy_(y)
203
+ return x
204
+ return y, s
205
+
206
+
207
+ @tilelang.jit(pass_configs=pass_configs)
208
+ def fp8_gemm_kernel(
209
+ N,
210
+ K,
211
+ block_size=128,
212
+ out_dtype=BF16,
213
+ accum_dtype=FP32,
214
+ scale_dtype=FP32,
215
+ ):
216
+ assert out_dtype in [BF16, FP32]
217
+ assert block_size in [32, 128]
218
+
219
+ M = T.symbolic("M")
220
+ group_size = block_size
221
+ block_M = 32
222
+ block_N = 128
223
+ block_K = group_size
224
+ num_stages = 4 if block_K == 128 else 2
225
+
226
+ @T.prim_func
227
+ def fp8_gemm_kernel_(
228
+ A: T.Tensor[(M, K), FP8],
229
+ B: T.Tensor[(N, K), FP8],
230
+ C: T.Tensor[(M, N), out_dtype],
231
+ scales_a: T.Tensor[(M, T.ceildiv(K, group_size)), scale_dtype],
232
+ scales_b: T.Tensor[(T.ceildiv(N, group_size), T.ceildiv(K, group_size)), scale_dtype],
233
+ ):
234
+ with T.Kernel(T.ceildiv(N, block_N), T.ceildiv(M, block_M), threads=128) as (
235
+ bx,
236
+ by,
237
+ ):
238
+ A_shared = T.alloc_shared((block_M, block_K), FP8)
239
+ B_shared = T.alloc_shared((block_N, block_K), FP8)
240
+ C_shared = T.alloc_shared((block_M, block_N), out_dtype)
241
+ C_local = T.alloc_fragment((block_M, block_N), accum_dtype)
242
+ C_local_accum = T.alloc_fragment((block_M, block_N), accum_dtype)
243
+ scale_a_frag = T.alloc_fragment((block_M,), FP32)
244
+ scale_b_frag = T.alloc_fragment((block_N,), FP32)
245
+
246
+ # Improve L2 Cache
247
+ T.use_swizzle(panel_size=10)
248
+ T.clear(C_local)
249
+ T.clear(C_local_accum)
250
+
251
+ K_iters = T.ceildiv(K, block_K)
252
+ for k in T.Pipelined(K_iters, num_stages=num_stages):
253
+ T.copy(A[by * block_M, k * block_K], A_shared)
254
+ T.copy(B[bx * block_N, k * block_K], B_shared)
255
+
256
+ # Activation scales are per row and K block. Weight scales are
257
+ # per (N block, K block), where the block can be 32 or 128.
258
+ for i in T.Parallel(block_M):
259
+ scale_a_frag[i] = T.Cast(FP32, scales_a[by * block_M + i, k])
260
+ for j in T.Parallel(block_N):
261
+ scale_b_frag[j] = T.Cast(
262
+ FP32,
263
+ scales_b[(bx * block_N + j) // group_size, k],
264
+ )
265
+
266
+ T.gemm(A_shared, B_shared, C_local, transpose_B=True)
267
+ # Separate accumulator for scale-corrected results.
268
+ for i, j in T.Parallel(block_M, block_N):
269
+ C_local_accum[i, j] += C_local[i, j] * scale_a_frag[i] * scale_b_frag[j]
270
+ T.clear(C_local)
271
+ T.copy(C_local_accum, C_shared)
272
+ T.copy(C_shared, C[by * block_M, bx * block_N])
273
+
274
+ return fp8_gemm_kernel_
275
+
276
+
277
+ def fp8_gemm(
278
+ a: torch.Tensor,
279
+ a_s: torch.Tensor,
280
+ b: torch.Tensor,
281
+ b_s: torch.Tensor,
282
+ scale_dtype: torch.dtype = torch.float32,
283
+ block_size: int = 128,
284
+ ) -> torch.Tensor:
285
+ """C[M,N] = A[M,K] @ B[N,K]^T with per-block FP8 scaling."""
286
+ assert a.is_contiguous() and b.is_contiguous(), "Input tensors must be contiguous"
287
+ assert a_s.is_contiguous() and b_s.is_contiguous(), "Scaling factor tensors must be contiguous"
288
+ assert block_size in (32, 128)
289
+ tl_dtype = FE8M0 if scale_dtype == torch.float8_e8m0fnu else FP32
290
+ K = a.size(-1)
291
+ M = a.numel() // K
292
+ N = b.size(0)
293
+ assert K % block_size == 0
294
+ assert a_s.numel() == M * (K // block_size)
295
+ assert b_s.shape == (
296
+ (N + block_size - 1) // block_size,
297
+ K // block_size,
298
+ )
299
+ c = a.new_empty(*a.size()[:-1], N, dtype=torch.get_default_dtype())
300
+ kernel = fp8_gemm_kernel(
301
+ N,
302
+ K,
303
+ block_size=block_size,
304
+ scale_dtype=tl_dtype,
305
+ )
306
+ kernel(a.view(M, K), b, c.view(M, N), a_s.view(M, -1), b_s)
307
+ return c
308
+
309
+
310
+ @tilelang.jit(pass_configs=pass_configs)
311
+ def sparse_attn_kernel(h: int, d: int, scale=None):
312
+ """Sparse multi-head attention via index gathering + online softmax (FlashAttention-style).
313
+ For each (batch, seq_pos), gathers top-k KV positions by index, computes attention
314
+ with numerically stable running max/sum, and includes a learnable attn_sink bias."""
315
+ b = T.symbolic("b")
316
+ m = T.symbolic("m")
317
+ n = T.symbolic("n")
318
+ topk = T.symbolic("topk")
319
+ if scale is None:
320
+ scale = (1.0 / d) ** 0.5
321
+
322
+ num_stages = 2
323
+ threads = 256
324
+ block = 64
325
+ num_blocks = tilelang.cdiv(topk, block)
326
+
327
+ @T.prim_func
328
+ def sparse_attn_kernel_(
329
+ q: T.Tensor[(b, m, h, d), BF16],
330
+ kv: T.Tensor[(b, n, d), BF16],
331
+ o: T.Tensor[(b, m, h, d), BF16],
332
+ attn_sink: T.Tensor[(h,), FP32],
333
+ topk_idxs: T.Tensor[(b, m, topk), INT32],
334
+ ):
335
+ with T.Kernel(m, b, threads=threads) as (bx, by):
336
+ q_shared = T.alloc_shared((h, d), BF16)
337
+ kv_shared = T.alloc_shared((block, d), BF16)
338
+ o_shared = T.alloc_shared((h, d), BF16)
339
+ acc_s_cast = T.alloc_shared((h, block), BF16)
340
+
341
+ idxs = T.alloc_fragment(block, INT32)
342
+ acc_s = T.alloc_fragment((h, block), FP32)
343
+ acc_o = T.alloc_fragment((h, d), FP32)
344
+ scores_max = T.alloc_fragment(h, FP32)
345
+ scores_max_prev = T.alloc_fragment(h, FP32)
346
+ scores_scale = T.alloc_fragment(h, FP32)
347
+ scores_sum = T.alloc_fragment(h, FP32)
348
+ sum_exp = T.alloc_fragment(h, FP32)
349
+
350
+ T.clear(acc_o)
351
+ T.clear(sum_exp)
352
+ # Finite lower bound instead of -inf: a row with no valid index (all -1) would otherwise
353
+ # produce exp(-inf - (-inf)) = NaN. With a finite bound such rows yield an all-zero output,
354
+ # matching the training kernel's convention.
355
+ T.fill(scores_max, -1e30)
356
+ T.copy(q[by, bx, :, :], q_shared)
357
+
358
+ for t in T.Pipelined(num_blocks, num_stages=num_stages):
359
+ for i in T.Parallel(block):
360
+ idxs[i] = T.if_then_else(t * block + i < topk, topk_idxs[by, bx, t * block + i], -1)
361
+ for i, j in T.Parallel(block, d):
362
+ kv_shared[i, j] = T.if_then_else(idxs[i] != -1, kv[by, idxs[i], j], 0)
363
+ for i, j in T.Parallel(h, block):
364
+ acc_s[i, j] = T.if_then_else(idxs[j] != -1, 0, -T.infinity(FP32))
365
+ T.gemm(q_shared, kv_shared, acc_s, transpose_B=True, policy=T.GemmWarpPolicy.FullRow)
366
+ for i, j in T.Parallel(h, block):
367
+ acc_s[i, j] *= scale
368
+ T.copy(scores_max, scores_max_prev)
369
+ T.reduce_max(acc_s, scores_max, dim=1, clear=False)
370
+ for i in T.Parallel(h):
371
+ scores_scale[i] = T.exp(scores_max_prev[i] - scores_max[i])
372
+ for i, j in T.Parallel(h, block):
373
+ acc_s[i, j] = T.exp(acc_s[i, j] - scores_max[i])
374
+ T.reduce_sum(acc_s, scores_sum, dim=1)
375
+ for i in T.Parallel(h):
376
+ sum_exp[i] = sum_exp[i] * scores_scale[i] + scores_sum[i]
377
+ T.copy(acc_s, acc_s_cast)
378
+ for i, j in T.Parallel(h, d):
379
+ acc_o[i, j] *= scores_scale[i]
380
+ T.gemm(acc_s_cast, kv_shared, acc_o, policy=T.GemmWarpPolicy.FullRow)
381
+
382
+ for i in T.Parallel(h):
383
+ sum_exp[i] += T.exp(attn_sink[i] - scores_max[i])
384
+ for i, j in T.Parallel(h, d):
385
+ acc_o[i, j] /= sum_exp[i]
386
+ T.copy(acc_o, o_shared)
387
+ T.copy(o_shared, o[by, bx, :, :])
388
+
389
+ return sparse_attn_kernel_
390
+
391
+
392
+ def sparse_attn(q: torch.Tensor, kv: torch.Tensor, attn_sink: torch.Tensor, topk_idxs: torch.Tensor, softmax_scale: float) -> torch.Tensor:
393
+ b, s, h, d = q.size()
394
+ # Pad heads to 16 for kernel efficiency (stripped after)
395
+ if h < 16:
396
+ q = torch.cat([q, q.new_zeros(b, s, 16 - h, d)], dim=2)
397
+ attn_sink = torch.cat([attn_sink, attn_sink.new_zeros(16 - h)])
398
+ o = torch.empty_like(q)
399
+ kernel = sparse_attn_kernel(q.size(2), d, softmax_scale)
400
+ kernel(q, kv, o, attn_sink, topk_idxs)
401
+ if h < 16:
402
+ o = o.narrow(2, 0, h).contiguous()
403
+ return o
404
+
405
+
406
+ @tilelang.jit(pass_configs=pass_configs)
407
+ def hc_split_sinkhorn_kernel(hc: int, sinkhorn_iters: int, eps: float):
408
+ n = T.symbolic("n")
409
+ mix_hc = (2 + hc) * hc
410
+ threads = 64
411
+
412
+ @T.prim_func
413
+ def hc_split_sinkhorn_kernel_(
414
+ mixes: T.Tensor[(n, mix_hc), FP32],
415
+ hc_scale: T.Tensor[(3,), FP32],
416
+ hc_base: T.Tensor[(mix_hc,), FP32],
417
+ pre: T.Tensor[(n, hc), FP32],
418
+ post: T.Tensor[(n, hc), FP32],
419
+ comb: T.Tensor[(n, hc, hc), FP32],
420
+ ):
421
+ with T.Kernel(n, threads=threads) as i:
422
+ mixes_shared = T.alloc_shared(mix_hc, FP32)
423
+ comb_frag = T.alloc_fragment((hc, hc), FP32)
424
+ T.copy(mixes[i, :], mixes_shared)
425
+
426
+ for j in T.Parallel(hc):
427
+ pre[i, j] = T.sigmoid(mixes_shared[j] * hc_scale[0] + hc_base[j]) + eps
428
+ for j in T.Parallel(hc):
429
+ post[i, j] = 2 * T.sigmoid(mixes_shared[j + hc] * hc_scale[1] + hc_base[j + hc])
430
+ for j, k in T.Parallel(hc, hc):
431
+ comb_frag[j, k] = mixes_shared[j * hc + k + hc * 2] * hc_scale[2] + hc_base[j * hc + k + hc * 2]
432
+
433
+ row_sum = T.alloc_fragment(hc, FP32)
434
+ col_sum = T.alloc_fragment(hc, FP32)
435
+
436
+ # comb = comb.softmax(-1) + eps
437
+ row_max = T.alloc_fragment(hc, FP32)
438
+ T.reduce_max(comb_frag, row_max, dim=1)
439
+ for j, k in T.Parallel(hc, hc):
440
+ comb_frag[j, k] = T.exp(comb_frag[j, k] - row_max[j])
441
+ T.reduce_sum(comb_frag, row_sum, dim=1)
442
+ for j, k in T.Parallel(hc, hc):
443
+ comb_frag[j, k] = comb_frag[j, k] / row_sum[j] + eps
444
+
445
+ # comb = comb / (comb.sum(-2) + eps)
446
+ T.reduce_sum(comb_frag, col_sum, dim=0)
447
+ for j, k in T.Parallel(hc, hc):
448
+ comb_frag[j, k] = comb_frag[j, k] / (col_sum[k] + eps)
449
+
450
+ for _ in T.serial(sinkhorn_iters - 1):
451
+ # comb = comb / (comb.sum(-1) + eps)
452
+ T.reduce_sum(comb_frag, row_sum, dim=1)
453
+ for j, k in T.Parallel(hc, hc):
454
+ comb_frag[j, k] = comb_frag[j, k] / (row_sum[j] + eps)
455
+ # comb = comb / (comb.sum(-2) + eps)
456
+ T.reduce_sum(comb_frag, col_sum, dim=0)
457
+ for j, k in T.Parallel(hc, hc):
458
+ comb_frag[j, k] = comb_frag[j, k] / (col_sum[k] + eps)
459
+
460
+ T.copy(comb_frag, comb[i, :, :])
461
+
462
+ return hc_split_sinkhorn_kernel_
463
+
464
+
465
+ def hc_split_sinkhorn(
466
+ mixes: torch.Tensor, hc_scale: torch.Tensor, hc_base: torch.Tensor, hc_mult: int = 4, sinkhorn_iters: int = 20, eps: float = 1e-6
467
+ ):
468
+ b, s, _ = mixes.size()
469
+ pre = mixes.new_empty(b, s, hc_mult)
470
+ post = mixes.new_empty(b, s, hc_mult)
471
+ comb = mixes.new_empty(b, s, hc_mult, hc_mult)
472
+ kernel = hc_split_sinkhorn_kernel(hc_mult, sinkhorn_iters, eps)
473
+ kernel(mixes.view(-1, (2 + hc_mult) * hc_mult), hc_scale, hc_base, pre.view(-1, hc_mult), post.view(-1, hc_mult), comb.view(-1, hc_mult, hc_mult))
474
+ return pre, post, comb
475
+
476
+
477
+ @tilelang.jit(pass_configs=pass_configs)
478
+ def fp4_gemm_kernel(
479
+ N,
480
+ K,
481
+ act_block_size=128,
482
+ out_dtype=BF16,
483
+ accum_dtype=FP32,
484
+ scale_dtype=FP32,
485
+ ):
486
+ """FP8 act x FP4 weight GEMM kernel.
487
+
488
+ C[M, N] = A_fp8[M, K] @ B_fp4[N, K]^T
489
+
490
+ Act: 1x32 or 1x128 quant on K, FP8 with configurable scale dtype
491
+ Weight: 1x32 quant on K (reduce dim), FP4 with E8M0 scale
492
+
493
+ B is stored as [N, K//2] in float4_e2m1fn_x2, logical [N, K] in fp4.
494
+ The FP4 values are packed along the K (last) dimension.
495
+
496
+ Strategy: load FP4 sub-blocks of size [block_N, sub_K] (sub_K=32),
497
+ cast FP4 to FP8 via float, then do FP8xFP8 GEMM.
498
+ Apply activation and weight scales to the accumulator.
499
+ """
500
+ M = T.symbolic("M")
501
+ assert act_block_size in [32, 128]
502
+ act_group_size = act_block_size
503
+ weight_group_size = 32
504
+ block_M = 32
505
+ block_N = 128
506
+ block_K = 32 # matches weight_group_size for simple scale handling
507
+ n_sub = act_group_size // block_K
508
+
509
+ @T.prim_func
510
+ def fp4_gemm_kernel_(
511
+ A: T.Tensor[(M, K), FP8],
512
+ B: T.Tensor[(N, K), FP4],
513
+ C: T.Tensor[(M, N), out_dtype],
514
+ scales_a: T.Tensor[(M, T.ceildiv(K, act_group_size)), scale_dtype],
515
+ scales_b: T.Tensor[(N, T.ceildiv(K, weight_group_size)), scale_dtype],
516
+ ):
517
+ with T.Kernel(T.ceildiv(N, block_N), T.ceildiv(M, block_M), threads=128) as (
518
+ bx,
519
+ by,
520
+ ):
521
+ A_shared = T.alloc_shared((block_M, block_K), FP8)
522
+ B_fp4_shared = T.alloc_shared((block_N, block_K), FP4)
523
+ B_shared = T.alloc_shared((block_N, block_K), FP8)
524
+ C_shared = T.alloc_shared((block_M, block_N), out_dtype)
525
+ C_local = T.alloc_fragment((block_M, block_N), accum_dtype)
526
+ C_local_accum = T.alloc_fragment((block_M, block_N), accum_dtype)
527
+ scale_a_frag = T.alloc_fragment((block_M,), FP32)
528
+ scale_b_frag = T.alloc_fragment((block_N,), FP32)
529
+
530
+ T.use_swizzle(panel_size=10)
531
+ T.clear(C_local)
532
+ T.clear(C_local_accum)
533
+
534
+ K_iters = T.ceildiv(K, block_K)
535
+ for k in T.Pipelined(K_iters, num_stages=2):
536
+ T.copy(A[by * block_M, k * block_K], A_shared)
537
+ T.copy(B[bx * block_N, k * block_K], B_fp4_shared)
538
+ # FP4->FP8 cast must go through FP32 to avoid ambiguous C++ overload
539
+ for i, j in T.Parallel(block_N, block_K):
540
+ B_shared[i, j] = T.Cast(FP8, T.Cast(FP32, B_fp4_shared[i, j]))
541
+
542
+ # Weight scale: per 32 on K, indexed by k (each k is one block_K=32)
543
+ for i in T.Parallel(block_N):
544
+ scale_b_frag[i] = T.Cast(FP32, scales_b[bx * block_N + i, k])
545
+
546
+ # Activation scale: one value per act_group_size elements.
547
+ for i in T.Parallel(block_M):
548
+ scale_a_frag[i] = T.Cast(FP32, scales_a[by * block_M + i, k // n_sub])
549
+
550
+ T.gemm(A_shared, B_shared, C_local, transpose_B=True)
551
+
552
+ for i, j in T.Parallel(block_M, block_N):
553
+ C_local_accum[i, j] += C_local[i, j] * scale_a_frag[i] * scale_b_frag[j]
554
+ T.clear(C_local)
555
+
556
+ T.copy(C_local_accum, C_shared)
557
+ T.copy(C_shared, C[by * block_M, bx * block_N])
558
+
559
+ return fp4_gemm_kernel_
560
+
561
+
562
+ def fp4_gemm(
563
+ a: torch.Tensor,
564
+ a_s: torch.Tensor,
565
+ b: torch.Tensor,
566
+ b_s: torch.Tensor,
567
+ scale_dtype: torch.dtype = torch.float32,
568
+ act_block_size: int = 128,
569
+ ) -> torch.Tensor:
570
+ """C[M,N] = A_fp8[M,K] @ B_fp4[N,K]^T.
571
+ A has per-32 or per-128 activation scale; B has per-32 E8M0 weight scale.
572
+ B is stored as [N, K//2] in float4_e2m1fn_x2 (2 FP4 values per byte, packed along K)."""
573
+ assert a.is_contiguous() and b.is_contiguous(), "Input tensors must be contiguous"
574
+ assert a_s.is_contiguous() and b_s.is_contiguous(), "Scaling factor tensors must be contiguous"
575
+ tl_dtype = FE8M0 if scale_dtype == torch.float8_e8m0fnu else FP32
576
+ K = a.size(-1)
577
+ M = a.numel() // K
578
+ N = b.size(0)
579
+ assert act_block_size in (32, 128)
580
+ assert K % act_block_size == 0
581
+ assert a_s.numel() == M * (K // act_block_size)
582
+ assert b_s.shape == (N, K // 32)
583
+ c = a.new_empty(*a.size()[:-1], N, dtype=torch.get_default_dtype())
584
+ kernel = fp4_gemm_kernel(
585
+ N,
586
+ K,
587
+ act_block_size=act_block_size,
588
+ scale_dtype=tl_dtype,
589
+ )
590
+ kernel(a.view(M, K), b, c.view(M, N), a_s.view(M, -1), b_s)
591
+ return c
inference/model.py ADDED
@@ -0,0 +1,1312 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ from contextlib import contextmanager
3
+ from dataclasses import dataclass
4
+ from functools import lru_cache
5
+ from typing import Literal
6
+
7
+ import torch
8
+ import torch.distributed as dist
9
+ import torch.nn.functional as F
10
+ from engram import EngramLayout, NgramHashState
11
+ from image_processor import IMAGE, IMAGE_END, IMAGE_NEW_LINE, IMAGE_START
12
+ from kernel import (
13
+ act_quant,
14
+ fp4_act_quant,
15
+ fp4_gemm,
16
+ fp8_gemm,
17
+ hc_split_sinkhorn,
18
+ sparse_attn,
19
+ )
20
+ from torch import nn
21
+ from vision import Aligner, ViT
22
+
23
+ # Set once by Transformer.__init__; one model per process, so layers just read them.
24
+ world_size = 1
25
+ rank = 0
26
+ default_dtype = torch.float8_e4m3fn # storage dtype for Linear weights, from ModelArgs.dtype
27
+ fp8_block_size = 32 # one fp8 scale per 32x32 weight block / 32 activations
28
+ fp4_block_size = 32 # one fp4 scale per 32 elements along K
29
+ scale_fmt = "ue8m0"
30
+ scale_dtype = torch.float8_e8m0fnu
31
+
32
+
33
+ @contextmanager
34
+ def set_dtype(dtype):
35
+ """Temporarily override torch's default dtype, restoring it even if the body raises."""
36
+ prev = torch.get_default_dtype()
37
+ torch.set_default_dtype(dtype)
38
+ try:
39
+ yield
40
+ finally:
41
+ torch.set_default_dtype(prev)
42
+
43
+
44
+ @dataclass
45
+ class ModelArgs:
46
+ """Field names are exactly the config JSON keys. The defaults are a small model that
47
+ `python model.py` can run, not the released shapes -- though the scale-independent
48
+ values (norm_eps, score_func, hc_*, engram_*) do match it."""
49
+
50
+ # runtime limits rather than model shape: they size the KV caches
51
+ max_batch_size: int = 4
52
+ max_seq_len: int = 4096
53
+ temperature: float = 1
54
+ dtype: Literal["bf16", "fp8"] = "fp8"
55
+ expert_dtype: Literal["fp4"] | None = "fp4"
56
+ vocab_size: int = 129280
57
+ dim: int = 1024
58
+ moe_inter_dim: int = 1024
59
+ n_layers: int = 5
60
+ n_mtp_layers: int = 1 # extra draft layers appended after the backbone, indices n_layers..
61
+ n_heads: int = 16
62
+ # moe
63
+ n_routed_experts: int = 8
64
+ n_shared_experts: int = 1
65
+ n_activated_experts: int = 2
66
+ score_func: Literal["softmax", "sigmoid", "sqrtsoftplus"] = "sqrtsoftplus"
67
+ gate_temp: float = 1.0
68
+ norm_topk_prob: bool = True
69
+ route_scale: float = 1.0
70
+ swiglu_limit: float = 0.0
71
+ # attention: latent q/kv projections, plus a LoRA-factorised output projection over o_groups
72
+ q_lora_rank: int = 256
73
+ head_dim: int = 128
74
+ rope_head_dim: int = 32
75
+ norm_eps: float = 1e-20
76
+ o_groups: int = 8
77
+ o_lora_rank: int = 256
78
+ # sparse attention: every layer attends over a sliding window, and may add compressed KV on top
79
+ window_size: int = 128
80
+ # one entry per layer, MTP layers included: 0 = sliding window only, r = KV compressed r-to-1
81
+ compress_ratios: tuple[int, ...] = (0, 2, 2, 1, 1, 0)
82
+ # layers sharing a ratio also share one compressed KV and one indexer, produced by the first
83
+ kv_source_layers: tuple[int, ...] = (1, 3)
84
+ index_source_layers: tuple[int, ...] = (1, 3)
85
+ # rope, with YaRN extrapolation when original_seq_len > 0. Compressed KV rotates at its own
86
+ # theta because one latent stands for compress_ratio tokens, so its positions are further apart.
87
+ compress_rope_theta: float = 40000.0
88
+ original_seq_len: int = 0
89
+ rope_theta: float = 10000.0
90
+ rope_factor: float = 40
91
+ beta_fast: int = 32
92
+ beta_slow: int = 1
93
+ # the indexer: a small extra attention that scores compressed positions, so each query can keep
94
+ # just `index_topk` of them. Names match Baanzon-Action upstream lineage, where this mechanism first appeared.
95
+ index_n_heads: int = 16
96
+ index_head_dim: int = 64
97
+ index_topk: int = 64
98
+ # candidate pre-filtering: candidate_source_layer < 0 turns it off and the other two are unused
99
+ candidate_source_layer: int = -1
100
+ candidate_topk_blocks: int = 0
101
+ candidate_block_size: int = 0
102
+ # hyper-connections: the residual stream is carried as hc_mult parallel copies
103
+ hc_mult: int = 4
104
+ hc_sinkhorn_iters: int = 20
105
+ hc_eps: float = 1e-6
106
+ # engram: n-gram hash lookups added into the residual stream at a few layers
107
+ engram_layer_ids: tuple[int, ...] = ()
108
+ engram_num_embeddings: tuple[int, ...] = () # unpadded table rows; each rank allocates ceil(rows / world_size)
109
+ engram_max_ngram_size: int = 1
110
+ engram_vocab_size: int = 0 # bucket size each (n-gram size, head) starts searching primes from
111
+ engram_n_heads: int = 0
112
+ engram_head_dim: int = 0
113
+ engram_pad_id: int = 2 # token that fills n-gram slots with no history; matches training
114
+ # size of the compressed tokenizer vocab; every hash multiplier is derived from it
115
+ engram_compressed_vocab_size: int = 0
116
+ # vision (VL); vision_n_layers == 0 disables the vision path
117
+ vision_n_layers: int = 0
118
+ vision_dim: int = 1024
119
+ vision_n_heads: int = 16
120
+ vision_inter_dim: int = 2816
121
+ vision_patch_size: int = 14
122
+ vision_rope_theta: float = 10000.0
123
+ vision_downsample_ratio: int = 3
124
+ vision_max_n_token: int = 1024
125
+ vision_min_pixels: int = 544 * 544
126
+ vision_max_wh_ratio: int | None = None
127
+ # raw id of <|deepseek_image|>; every position of an image span carries this id in input_ids
128
+ image_token_id: int = 129264
129
+ # dspark draft head. Only the forward pass is implemented here -- nothing calls forward_spec,
130
+ # so these are read but the speculative-decoding loop itself is out of scope for this repo.
131
+ dspark_block_size: int = 0
132
+ dspark_noise_token_id: int = 0
133
+ dspark_target_layer_ids: tuple[int, ...] = ()
134
+ dspark_markov_rank: int = 256
135
+ dspark_n_routed_experts: int = 0
136
+ dspark_n_activated_experts: int = 0
137
+
138
+ @property
139
+ def vision_enabled(self) -> bool:
140
+ return self.vision_n_layers > 0
141
+
142
+ def get_moe_config(self, layer_id: int) -> tuple[int, int]:
143
+ """Return the routed/activated expert counts for a given layer."""
144
+ if layer_id < self.n_layers:
145
+ return self.n_routed_experts, self.n_activated_experts
146
+ return (
147
+ self.dspark_n_routed_experts or self.n_routed_experts,
148
+ self.dspark_n_activated_experts or self.n_activated_experts,
149
+ )
150
+
151
+
152
+ class ParallelEmbedding(nn.Module):
153
+ """Embedding sharded along the vocab dimension. Each rank holds vocab_size // world_size rows.
154
+ Out-of-range indices are zero-masked before all_reduce to combine partial embeddings."""
155
+
156
+ def __init__(self, vocab_size: int, dim: int):
157
+ super().__init__()
158
+ self.vocab_size = vocab_size
159
+ self.dim = dim
160
+ assert vocab_size % world_size == 0, (
161
+ f"Vocabulary size must be divisible by world size (world_size={world_size})"
162
+ )
163
+ self.part_vocab_size = vocab_size // world_size
164
+ self.vocab_start_idx = rank * self.part_vocab_size
165
+ self.vocab_end_idx = self.vocab_start_idx + self.part_vocab_size
166
+ self.weight = nn.Parameter(torch.empty(self.part_vocab_size, self.dim))
167
+
168
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
169
+ if world_size > 1:
170
+ # ids off this rank read row 0 then get zeroed, so the all_reduce sums one real row
171
+ mask = (x < self.vocab_start_idx) | (x >= self.vocab_end_idx)
172
+ x = x - self.vocab_start_idx
173
+ x[mask] = 0
174
+ y = F.embedding(x, self.weight)
175
+ if world_size > 1:
176
+ y[mask] = 0
177
+ dist.all_reduce(y)
178
+ return y
179
+
180
+
181
+ def linear(x: torch.Tensor, weight: torch.Tensor, bias: torch.Tensor | None = None) -> torch.Tensor:
182
+ """Pick a GEMM from the weight dtype. Quantized weights need a quantized activation, and both
183
+ fp4 and fp8 weights take an fp8 one -- for fp4 the kernel handles the mixed precision."""
184
+ assert bias is None
185
+
186
+ if weight.dtype == torch.float4_e2m1fn_x2:
187
+ x, s = act_quant(x, fp8_block_size, scale_fmt, scale_dtype)
188
+ return fp4_gemm(
189
+ x,
190
+ s,
191
+ weight,
192
+ weight.scale,
193
+ scale_dtype,
194
+ act_block_size=fp8_block_size,
195
+ )
196
+ elif weight.dtype == torch.float8_e4m3fn:
197
+ x, s = act_quant(x, fp8_block_size, scale_fmt, scale_dtype)
198
+ return fp8_gemm(
199
+ x,
200
+ s,
201
+ weight,
202
+ weight.scale,
203
+ scale_dtype,
204
+ block_size=fp8_block_size,
205
+ )
206
+ else:
207
+ return F.linear(x, weight)
208
+
209
+
210
+ class Linear(nn.Module):
211
+ """bf16, fp8 or fp4 weights. Quantized ones get a `scale`, also attached to `.weight` so that
212
+ `linear()` can reach it."""
213
+
214
+ def __init__(self, in_features: int, out_features: int, bias: bool = False, dtype=None):
215
+ super().__init__()
216
+ self.in_features = in_features
217
+ self.out_features = out_features
218
+ dtype = dtype or default_dtype
219
+ if dtype == torch.float4_e2m1fn_x2:
220
+ # two values per byte: [out, in] logically, [out, in//2] stored
221
+ self.weight = nn.Parameter(torch.empty(out_features, in_features // 2, dtype=torch.float4_e2m1fn_x2))
222
+ self.weight.scale = self.scale = nn.Parameter(
223
+ torch.empty(out_features, in_features // fp4_block_size, dtype=torch.float8_e8m0fnu)
224
+ )
225
+ elif dtype == torch.float8_e4m3fn:
226
+ self.weight = nn.Parameter(torch.empty(out_features, in_features, dtype=dtype))
227
+ self.weight.scale = self.scale = nn.Parameter(
228
+ torch.empty(
229
+ (out_features + fp8_block_size - 1) // fp8_block_size,
230
+ (in_features + fp8_block_size - 1) // fp8_block_size,
231
+ dtype=torch.float8_e8m0fnu,
232
+ )
233
+ )
234
+ else:
235
+ self.weight = nn.Parameter(torch.empty(out_features, in_features, dtype=dtype))
236
+ self.register_parameter("scale", None)
237
+ if bias:
238
+ self.bias = nn.Parameter(torch.empty(out_features))
239
+ else:
240
+ self.register_parameter("bias", None)
241
+
242
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
243
+ return linear(x, self.weight, self.bias)
244
+
245
+
246
+ class ColumnParallelLinear(Linear):
247
+ """Splits the output dim across ranks; each rank's slice of the output is already complete."""
248
+
249
+ def __init__(self, in_features: int, out_features: int, bias: bool = False, dtype=None):
250
+ assert out_features % world_size == 0, (
251
+ f"Output features must be divisible by world size (world_size={world_size})"
252
+ )
253
+ self.part_out_features = out_features // world_size
254
+ super().__init__(in_features, self.part_out_features, bias, dtype)
255
+
256
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
257
+ return linear(x, self.weight, self.bias)
258
+
259
+
260
+ class RowParallelLinear(Linear):
261
+ """Splits the reduction dim, so each rank holds a partial sum: hence the fp32 all_reduce, with
262
+ the bias added only after it."""
263
+
264
+ def __init__(self, in_features: int, out_features: int, bias: bool = False, dtype=None):
265
+ assert in_features % world_size == 0, (
266
+ f"Input features must be divisible by world size (world_size={world_size})"
267
+ )
268
+ self.part_in_features = in_features // world_size
269
+ super().__init__(self.part_in_features, out_features, bias, dtype)
270
+
271
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
272
+ y = linear(x, self.weight, None)
273
+ if world_size > 1:
274
+ y = y.float()
275
+ dist.all_reduce(y)
276
+ if self.bias is not None:
277
+ y += self.bias
278
+ return y.type_as(x)
279
+
280
+
281
+ class RMSNorm(nn.Module):
282
+ def __init__(self, dim: int, eps: float = 1e-6):
283
+ super().__init__()
284
+ self.dim = dim
285
+ self.eps = eps
286
+ self.weight = nn.Parameter(torch.ones(dim))
287
+
288
+ def forward(self, x: torch.Tensor):
289
+ dtype = x.dtype
290
+ x = x.float()
291
+ var = x.square().mean(-1, keepdim=True)
292
+ x = x * torch.rsqrt(var + self.eps)
293
+ return (self.weight * x).to(dtype)
294
+
295
+
296
+ class ParallelEngramEmbedding(nn.Module):
297
+ """The n-gram hash table, sharded over its rows. Stays fp8: rows are dequantized on lookup."""
298
+
299
+ def __init__(self, num_embeddings: int, dim: int):
300
+ super().__init__()
301
+ self.num_embeddings = num_embeddings
302
+ self.dim = dim
303
+ self.part_num_embeddings = (num_embeddings + world_size - 1) // world_size
304
+ self.vocab_start_idx = rank * self.part_num_embeddings
305
+ self.vocab_end_idx = self.vocab_start_idx + self.part_num_embeddings
306
+
307
+ self.block_size = fp8_block_size
308
+ # the table stays fp8 as stored: rows are dequantized with `scale` on lookup
309
+ self.weight = nn.Parameter(torch.empty(self.part_num_embeddings, dim, dtype=torch.float8_e4m3fn))
310
+ self.scale = nn.Parameter(torch.empty(self.part_num_embeddings, dim // self.block_size, dtype=scale_dtype))
311
+
312
+ def forward(self, indices: torch.Tensor) -> torch.Tensor:
313
+ mask = (indices < self.vocab_start_idx) | (indices >= self.vocab_end_idx)
314
+ local_indices = indices - self.vocab_start_idx
315
+ local_indices = local_indices.masked_fill(mask, 0)
316
+
317
+ values = F.embedding(local_indices, self.weight)
318
+ scales = F.embedding(local_indices, self.scale)
319
+ values = values.float().unflatten(-1, (-1, self.block_size)) * scales.float().unsqueeze(-1)
320
+ values = values.flatten(-2).to(torch.bfloat16)
321
+ values = values.masked_fill(mask.unsqueeze(-1), 0)
322
+
323
+ if world_size > 1:
324
+ dist.all_reduce(values)
325
+ return values
326
+
327
+
328
+ class Engram(nn.Module):
329
+ """Writes an n-gram lookup into the residual stream, gated by how well it matches that stream.
330
+
331
+ The hash ids fetch `n_hash_cols` rows; `wkv` turns them into one key per hc copy plus a shared
332
+ value. The gate is a normalized dot product of stream against key.
333
+ """
334
+
335
+ def __init__(self, args: ModelArgs, layer_id: int, layout: EngramLayout):
336
+ super().__init__()
337
+ self.layer_id = layer_id
338
+ self.layer_hash_index = layout.layer_ids.index(layer_id)
339
+ self.dim = args.dim
340
+ self.hc_mult = args.hc_mult
341
+ self.clamp_value = 1e-6
342
+
343
+ self.embed = ParallelEngramEmbedding(layout.num_embeddings[self.layer_hash_index], layout.head_dim)
344
+ n_hash_cols = (layout.max_ngram_size - 1) * layout.n_heads
345
+ self.wkv = Linear(n_hash_cols * layout.head_dim, args.dim * (args.hc_mult + 1))
346
+ self.eps = args.norm_eps
347
+ self.q_weight = nn.Parameter(torch.ones(args.hc_mult, args.dim))
348
+ self.k_weight = nn.Parameter(torch.ones(args.hc_mult, args.dim))
349
+
350
+ def forward(self, x: torch.Tensor, hash_ids: torch.Tensor, token_mask: torch.Tensor | None = None) -> torch.Tensor:
351
+ """x: [B, L, hc_mult, dim]; hash_ids: [B, L, n_hash_cols]; token_mask: [B, L], False shuts
352
+ the gate so those positions pass through untouched."""
353
+ kv = self.wkv(self.embed(hash_ids).flatten(-2))
354
+ key, value = kv.split([self.hc_mult * self.dim, self.dim], dim=-1)
355
+ key = key.float().unflatten(-1, (self.hc_mult, self.dim))
356
+ weight = self.q_weight.float() * self.k_weight.float() # only ever used as a product
357
+ h, eps = x.float(), self.eps
358
+ # normalized per (token, hc copy) over `dim`, NOT jointly over the copies
359
+ rstd = torch.rsqrt(h.square().mean(-1) + eps) * torch.rsqrt(key.square().mean(-1) + eps)
360
+ dot = (h * weight * key).sum(-1) * rstd * self.dim**-0.5
361
+ # signed sqrt before the sigmoid, matching the training kernel
362
+ gate = torch.sigmoid(torch.copysign(dot.abs().clamp_min(self.clamp_value).sqrt(), dot))
363
+ if token_mask is not None:
364
+ gate = gate.masked_fill(~token_mask.unsqueeze(-1), 0)
365
+ return (h + gate.unsqueeze(-1) * value.float().unsqueeze(-2)).to(x.dtype)
366
+
367
+
368
+ @lru_cache(2)
369
+ def precompute_freqs_cis(dim, seqlen, original_seq_len, base, factor, beta_fast, beta_slow) -> torch.Tensor:
370
+ """Rotary frequencies as complex exponentials, one row per position.
371
+
372
+ With original_seq_len > 0 this applies YaRN: dimensions whose wavelength already fits inside the
373
+ training context keep their frequency, those far beyond it are divided by `factor`, and the
374
+ `beta_fast`..`beta_slow` band in between is faded across with a linear ramp.
375
+ """
376
+ freqs = 1.0 / (base ** (torch.arange(0, dim, 2, dtype=torch.float32) / dim))
377
+ if original_seq_len > 0:
378
+ # the dim whose wavelength completes `rotations` turns over the training context
379
+ def corrected_dim(rotations):
380
+ return dim * math.log(original_seq_len / (rotations * 2 * math.pi)) / (2 * math.log(base))
381
+
382
+ low = max(math.floor(corrected_dim(beta_fast)), 0)
383
+ high = min(math.ceil(corrected_dim(beta_slow)), dim - 1)
384
+ ramp = ((torch.arange(dim // 2, dtype=torch.float32) - low) / max(high - low, 1e-3)).clamp(0, 1)
385
+ smooth = 1 - ramp
386
+ freqs = freqs / factor * (1 - smooth) + freqs * smooth
387
+
388
+ freqs = torch.outer(torch.arange(seqlen), freqs)
389
+ return torch.polar(torch.ones_like(freqs), freqs)
390
+
391
+
392
+ def apply_rotary_emb(x: torch.Tensor, freqs_cis: torch.Tensor, inverse: bool = False) -> torch.Tensor:
393
+ """Rotate `x` in place, taking adjacent element pairs as complex numbers. Accepts [b, s, d] and
394
+ [b, s, h, d]; `inverse` conjugates the rotation, which is how the attention output gets the
395
+ query's rotation removed again so the cache can stay in one shared rotated form."""
396
+ y = x
397
+ x = torch.view_as_complex(x.float().unflatten(-1, (-1, 2)))
398
+ if inverse:
399
+ freqs_cis = freqs_cis.conj()
400
+ if x.ndim == 3:
401
+ freqs_cis = freqs_cis.view(1, x.size(1), x.size(-1))
402
+ else:
403
+ freqs_cis = freqs_cis.view(1, x.size(1), 1, x.size(-1))
404
+ x = torch.view_as_real(x * freqs_cis).flatten(-2)
405
+ y.copy_(x)
406
+ return y
407
+
408
+
409
+ @lru_cache(1)
410
+ def get_window_topk_idxs(window_size: int, bsz: int, seqlen: int, start_pos: int):
411
+ """Which sliding-window cache slots each query attends to; -1 marks a slot holding nothing.
412
+
413
+ The cache is a ring of `window_size` slots. Prefill needs one row per query, each seeing its own
414
+ causal window. A decode step has a single query that sees the whole ring, listed oldest first.
415
+ Order within a row does not matter to `sparse_attn`, which handles every slot independently.
416
+ """
417
+ if start_pos == 0:
418
+ end = torch.arange(seqlen).unsqueeze(1)
419
+ idxs = (end - window_size + 1).clamp(0) + torch.arange(min(seqlen, window_size))
420
+ idxs = torch.where(idxs > end, -1, idxs) # before the sequence started
421
+ else:
422
+ oldest = start_pos % window_size + 1
423
+ idxs = torch.cat([torch.arange(oldest, window_size), torch.arange(oldest)])
424
+ idxs = torch.where(idxs > start_pos, -1, idxs) # ring still filling
425
+ # sparse_attn needs real [b, m, topk] int32 memory, hence the materializing expand
426
+ return idxs.int().unsqueeze(0).expand(bsz, -1, -1).contiguous()
427
+
428
+
429
+ class Compressor(nn.Module):
430
+ """Pools `compress_ratio` consecutive tokens into one KV latent with a learned softmax gate.
431
+
432
+ Returns the latent before RoPE, or None while a group is still filling up -- so during decode it
433
+ only yields every `compress_ratio` steps, holding the partial group in `kv_state`/`score_state`.
434
+ Pre-RoPE is deliberate: the indexer needs the unrotated form, so Attention rotates afterwards.
435
+ """
436
+
437
+ def __init__(self, args: ModelArgs, layer_id: int):
438
+ super().__init__()
439
+ compress_ratio = args.compress_ratios[layer_id]
440
+ head_dim = args.head_dim
441
+ self.compress_ratio = compress_ratio
442
+ self.head_dim = head_dim
443
+ self.norm = RMSNorm(head_dim, args.norm_eps)
444
+ # ratio 1 is a plain projection, so it stays in the checkpoint's bf16; the softmax pooling
445
+ # above ratio 1 runs in fp32, so those weights are promoted to fp32 to match
446
+ self.wkv = Linear(args.dim, head_dim, dtype=torch.float32 if compress_ratio > 1 else torch.bfloat16)
447
+ if compress_ratio > 1:
448
+ self.wgate = Linear(args.dim, head_dim, dtype=torch.float32)
449
+ # tail of an incomplete group, carried across decode steps
450
+ self.kv_state: torch.Tensor
451
+ self.score_state: torch.Tensor
452
+ state_shape = (args.max_batch_size, compress_ratio, head_dim)
453
+ self.register_buffer("kv_state", torch.zeros(state_shape, dtype=torch.float32), persistent=False)
454
+ self.register_buffer(
455
+ "score_state", torch.full(state_shape, -torch.inf, dtype=torch.float32), persistent=False
456
+ )
457
+
458
+ def forward(self, x: torch.Tensor, start_pos: int) -> torch.Tensor | None:
459
+ bsz, seqlen, _ = x.size()
460
+ ratio, dtype = self.compress_ratio, x.dtype
461
+ if ratio == 1: # one token per group: nothing to pool, so no gate and no fp32
462
+ return self.norm(self.wkv(x))
463
+
464
+ x = x.float()
465
+ kv, score = self.wkv(x), self.wgate(x)
466
+ if start_pos == 0:
467
+ should_compress = seqlen >= ratio
468
+ remainder = seqlen % ratio
469
+ cutoff = seqlen - remainder
470
+ if remainder: # trailing partial group waits in the state
471
+ kv, self.kv_state[:bsz, :remainder] = kv.split([cutoff, remainder], dim=1)
472
+ score, self.score_state[:bsz, :remainder] = score.split([cutoff, remainder], dim=1)
473
+ kv = kv.unflatten(1, (-1, ratio))
474
+ score = score.unflatten(1, (-1, ratio))
475
+ kv = (kv * score.softmax(dim=2)).sum(dim=2)
476
+ else: # one token per step: fill a slot, and pool only when the group just completed
477
+ should_compress = (start_pos + 1) % ratio == 0
478
+ slot = start_pos % ratio
479
+ self.kv_state[:bsz, slot] = kv.squeeze(1)
480
+ self.score_state[:bsz, slot] = score.squeeze(1)
481
+ if should_compress:
482
+ kv = (self.kv_state[:bsz] * self.score_state[:bsz].softmax(dim=1)).sum(dim=1, keepdim=True)
483
+ if not should_compress:
484
+ return None
485
+ return self.norm(kv.to(dtype))
486
+
487
+
488
+ class Indexer(torch.nn.Module):
489
+ """Keeps the `index_topk` best compressed positions per query.
490
+
491
+ A small side attention: fp4 query heads against one shared key per compressed position, scores
492
+ rectified then combined by `weights_proj`. With a candidate source this is the second of two
493
+ levels; `select_candidate_blocks` is the first.
494
+ """
495
+
496
+ def __init__(self, args: ModelArgs, layer_id: int):
497
+ super().__init__()
498
+ # the index keys are derived from the compressor's latent, so only a layer that compresses
499
+ # its own KV can produce them; every other indexer reads them from that layer's cache
500
+ self.owns_k = layer_id in args.kv_source_layers
501
+ self.compress_ratio = args.compress_ratios[layer_id]
502
+ self.is_candidate_source = layer_id == args.candidate_source_layer
503
+ self.uses_candidates = 0 <= args.candidate_source_layer < layer_id
504
+ self.candidate_topk_blocks = args.candidate_topk_blocks
505
+ self.candidate_block_size = args.candidate_block_size
506
+ self.dim = args.dim
507
+ self.n_heads = args.index_n_heads
508
+ self.n_local_heads = args.index_n_heads // world_size
509
+ self.index_head_dim = args.index_head_dim
510
+ self.rope_head_dim = args.rope_head_dim
511
+ self.index_topk = args.index_topk
512
+ self.q_lora_rank = args.q_lora_rank
513
+ self.softmax_scale = self.index_head_dim**-0.5
514
+ self.wq_b = ColumnParallelLinear(self.q_lora_rank, self.n_heads * self.index_head_dim)
515
+ self.weights_proj = ColumnParallelLinear(self.dim, self.n_heads, dtype=torch.bfloat16)
516
+ self.freqs_cis: torch.Tensor | None = None
517
+ if self.owns_k:
518
+ self.wk = Linear(args.head_dim, self.index_head_dim, dtype=torch.bfloat16)
519
+ self.k_norm = RMSNorm(self.index_head_dim, args.norm_eps)
520
+ self.k_cache: torch.Tensor
521
+ self.register_buffer(
522
+ "k_cache",
523
+ torch.zeros(args.max_batch_size, args.max_seq_len // self.compress_ratio, args.index_head_dim),
524
+ persistent=False,
525
+ )
526
+
527
+ def forward(self, x: torch.Tensor, qr: torch.Tensor, latent: torch.Tensor, start_pos: int, offset: int):
528
+ """`latent` is this layer's RoPE-free compressed latent, None when this layer does not
529
+ compress or when its current group is still incomplete. An index-key owner turns it into
530
+ index keys here, which has to happen before Attention overwrites that same storage with
531
+ the RoPE'd, quantized values."""
532
+ assert self.freqs_cis is not None
533
+ bsz, seqlen, _ = x.size()
534
+ ratio, rd, end_pos = self.compress_ratio, self.rope_head_dim, start_pos + seqlen
535
+
536
+ # Append index keys when a compression group completes.
537
+ if self.owns_k and latent is not None:
538
+ # a latent stands for the first token of its group, so group j takes position j * ratio
539
+ freqs = (
540
+ self.freqs_cis[: seqlen - seqlen % ratio : ratio]
541
+ if start_pos == 0
542
+ else self.freqs_cis[start_pos + 1 - ratio].unsqueeze(0)
543
+ )
544
+ k = self.k_norm(self.wk(latent))
545
+ apply_rotary_emb(k[..., -rd:], freqs)
546
+ fp4_act_quant(k, fp4_block_size, True)
547
+ self.k_cache[:bsz, start_pos // ratio : start_pos // ratio + k.size(1)] = k
548
+ if self.owns_k:
549
+ shared_attn.index_k = self.k_cache
550
+
551
+ q = self.wq_b(qr).unflatten(-1, (self.n_local_heads, self.index_head_dim))
552
+ apply_rotary_emb(q[..., -rd:], self.freqs_cis[start_pos:end_pos])
553
+ fp4_act_quant(q, fp4_block_size, True)
554
+
555
+ index_k = shared_attn.index_k[:bsz, : end_pos // ratio]
556
+ weights = self.weights_proj(x) * (self.softmax_scale * self.n_heads**-0.5)
557
+ index_score = torch.einsum("bshd,btd->bsht", q, index_k)
558
+ index_score = (index_score.relu_() * weights.unsqueeze(-1)).sum(dim=2)
559
+ if world_size > 1:
560
+ dist.all_reduce(index_score)
561
+
562
+ # how many compressed positions each query can see: a block becomes visible once the query
563
+ # has passed its last token. One query per decode step, so there it is just a number.
564
+ if start_pos == 0:
565
+ compress_lens = (torch.arange(1, seqlen + 1, device=x.device) // ratio).unsqueeze(-1)
566
+ index_score.masked_fill_(torch.arange(seqlen // ratio, device=x.device) >= compress_lens, -torch.inf)
567
+ else:
568
+ compress_lens = end_pos // ratio
569
+
570
+ if self.is_candidate_source:
571
+ shared_attn.candidates = select_candidate_blocks(
572
+ index_score, compress_lens, self.candidate_topk_blocks, self.candidate_block_size
573
+ )
574
+ elif self.uses_candidates:
575
+ # level two: score with our own weights, but only inside the source's candidate blocks
576
+ index_score = index_score.masked_fill(~shared_attn.candidates, -torch.inf)
577
+
578
+ # top-k by score, re-sorted into position order; unreachable -> -1, rest shifted by offset
579
+ topk = min(self.index_topk, end_pos // ratio)
580
+ idxs = index_score.topk(topk, dim=-1, sorted=False).indices.sort(dim=-1).values
581
+ return torch.where(idxs < compress_lens, idxs + offset, -1).int()
582
+
583
+
584
+ def select_candidate_blocks(
585
+ logits: torch.Tensor,
586
+ compress_lens: torch.Tensor | int,
587
+ topk_blocks: int,
588
+ block_size: int,
589
+ ) -> torch.Tensor:
590
+ """Level one of the two-level top-k: keep the `topk_blocks` highest-scoring blocks per query.
591
+
592
+ `logits` is [..., n_positions] with positions the query cannot reach already at -inf, which is
593
+ what makes a block score of -inf mean "not reachable yet". `compress_lens` is a plain int during
594
+ decode, or broadcasts against logits' leading dims during prefill. Returns a bool mask shaped
595
+ like `logits`, so the layers consuming it just mask and never think about blocks again.
596
+ """
597
+ width = logits.size(-1)
598
+ # score each block by its best position; -inf pads the last one out to block_size
599
+ scores = F.pad(logits, (0, -width % block_size), value=-torch.inf)
600
+ scores = scores.unflatten(-1, (-1, block_size)).amax(dim=-1)
601
+ num_blocks = scores.size(-1)
602
+
603
+ # the block with this query's newest position is only partly filled, so pin it in: it holds the
604
+ # most recent tokens but could otherwise be outscored by an older, full block
605
+ last = (compress_lens - 1) // block_size
606
+ scores = scores.masked_fill(torch.arange(num_blocks, device=logits.device) == last, torch.inf)
607
+
608
+ top = scores.topk(min(topk_blocks, num_blocks), dim=-1)
609
+ # fewer reachable blocks than topk_blocks means leftover picks came back -inf: drop them
610
+ keep = torch.zeros_like(scores, dtype=torch.bool).scatter_(-1, top.indices, top.values > -torch.inf)
611
+ return keep.repeat_interleave(block_size, dim=-1)[..., :width]
612
+
613
+
614
+ class Attention(nn.Module):
615
+ """Latent attention over two KV sources at once, concatenated into one `sparse_attn` call: a
616
+ sliding window of raw KV, plus -- when compress_ratio > 0 -- `index_topk` compressed positions
617
+ reaching further back. Q and the output projection are both low-rank, the latter grouped.
618
+
619
+ compress_ratio > 0 does not mean the layer compresses its own KV: only kv_source_layers do,
620
+ the rest read that same cache.
621
+ """
622
+
623
+ def __init__(self, layer_id: int, args: ModelArgs):
624
+ super().__init__()
625
+ self.layer_id = layer_id
626
+ self.dim = args.dim
627
+ self.n_heads = args.n_heads
628
+ self.n_local_heads = args.n_heads // world_size
629
+ self.q_lora_rank = args.q_lora_rank
630
+ self.o_lora_rank = args.o_lora_rank
631
+ self.head_dim = args.head_dim
632
+ self.rope_head_dim = args.rope_head_dim
633
+ self.nope_head_dim = args.head_dim - args.rope_head_dim
634
+ self.n_groups = args.o_groups
635
+ self.n_local_groups = self.n_groups // world_size
636
+ self.window_size = args.window_size
637
+ self.compress_ratio = args.compress_ratios[layer_id]
638
+ self.eps = args.norm_eps
639
+
640
+ self.attn_sink = nn.Parameter(torch.empty(self.n_local_heads, dtype=torch.float32))
641
+ self.wq_a = Linear(self.dim, self.q_lora_rank)
642
+ self.q_norm = RMSNorm(self.q_lora_rank, self.eps)
643
+ self.wq_b = ColumnParallelLinear(self.q_lora_rank, self.n_heads * self.head_dim)
644
+ self.wkv = Linear(self.dim, self.head_dim)
645
+ self.kv_norm = RMSNorm(self.head_dim, self.eps)
646
+ self.wo_a = ColumnParallelLinear(
647
+ self.n_heads * self.head_dim // self.n_groups,
648
+ self.n_groups * args.o_lora_rank,
649
+ dtype=torch.bfloat16,
650
+ )
651
+ self.wo_b = RowParallelLinear(self.n_groups * args.o_lora_rank, self.dim)
652
+ self.softmax_scale = self.head_dim**-0.5
653
+
654
+ is_backbone = layer_id < args.n_layers
655
+ self.is_kv_source = is_backbone and layer_id in args.kv_source_layers
656
+ self.is_index_source = is_backbone and layer_id in args.index_source_layers
657
+ self.compressor: Compressor | None = None
658
+ self.indexer: Indexer | None = None
659
+ if self.is_kv_source:
660
+ self.compressor = Compressor(args, layer_id)
661
+ if self.is_index_source:
662
+ self.indexer = Indexer(args, layer_id)
663
+
664
+ self.window_kv_cache: torch.Tensor
665
+ self.register_buffer(
666
+ "window_kv_cache",
667
+ torch.zeros(args.max_batch_size, args.window_size, self.head_dim),
668
+ persistent=False,
669
+ )
670
+ if self.is_kv_source:
671
+ self.compress_kv_cache: torch.Tensor
672
+ self.register_buffer(
673
+ "compress_kv_cache",
674
+ torch.zeros(
675
+ args.max_batch_size,
676
+ args.max_seq_len // self.compress_ratio,
677
+ self.head_dim,
678
+ ),
679
+ persistent=False,
680
+ )
681
+ if self.compress_ratio:
682
+ original_seq_len, rope_theta = (
683
+ args.original_seq_len,
684
+ args.compress_rope_theta,
685
+ )
686
+ else:
687
+ # disable YaRN and use base rope_theta in pure sliding-window attention
688
+ original_seq_len, rope_theta = 0, args.rope_theta
689
+ freqs_cis = precompute_freqs_cis(
690
+ self.rope_head_dim,
691
+ args.max_seq_len,
692
+ original_seq_len,
693
+ rope_theta,
694
+ args.rope_factor,
695
+ args.beta_fast,
696
+ args.beta_slow,
697
+ )
698
+ self.freqs_cis: torch.Tensor
699
+ self.register_buffer("freqs_cis", freqs_cis, persistent=False)
700
+
701
+ def _window_kv(self, x, freqs_cis, start_pos):
702
+ """This layer's sliding-window K and the window positions every query may attend to. The K
703
+ stays fp8, quantized over the whole post-RoPE vector, RoPE tail included."""
704
+ bsz, seqlen, _ = x.size()
705
+ win = self.window_size
706
+ kv = self.kv_norm(self.wkv(x))
707
+ apply_rotary_emb(kv[..., -self.rope_head_dim :], freqs_cis)
708
+ act_quant(kv, fp8_block_size, scale_fmt, scale_dtype, True)
709
+ if start_pos == 0: # prefill: attend over this chunk, seeding the ring buffer for decode
710
+ if seqlen <= win:
711
+ self.window_kv_cache[:bsz, :seqlen] = kv
712
+ else:
713
+ cutoff = seqlen % win
714
+ self.window_kv_cache[:bsz, cutoff:win], self.window_kv_cache[:bsz, :cutoff] = kv[:, -win:].split(
715
+ [win - cutoff, cutoff], dim=1
716
+ )
717
+ window_kv = kv
718
+ else: # decode: one token into the ring buffer, attend over the whole window
719
+ self.window_kv_cache[:bsz, start_pos % win] = kv.squeeze(1)
720
+ window_kv = self.window_kv_cache[:bsz]
721
+ return window_kv, get_window_topk_idxs(win, bsz, seqlen, start_pos)
722
+
723
+ def _compress_topk_idxs(self, x, qr, latent, start_pos, offset, compress_len):
724
+ """Which compressed positions each query attends to. Index sources run their own indexer;
725
+ the layers in between reuse the result their source published."""
726
+ if not self.is_index_source:
727
+ return shared_attn.topk_idxs
728
+
729
+ bsz, seqlen, _ = x.size()
730
+ if compress_len == 0:
731
+ idxs = torch.empty(bsz, seqlen, 0, dtype=torch.int32, device=x.device)
732
+ else:
733
+ assert self.indexer is not None
734
+ if self.indexer.freqs_cis is None:
735
+ self.indexer.freqs_cis = self.freqs_cis
736
+ idxs = self.indexer(x, qr, latent, start_pos, offset)
737
+ shared_attn.topk_idxs = idxs
738
+ return idxs
739
+
740
+ def _compress_kv(self, x, qr, start_pos, offset):
741
+ """The shared compressed KV and the compressed positions every query may attend to. This
742
+ layer compresses its own KV only when it is a source; otherwise it just reads the cache."""
743
+ bsz, seqlen, _ = x.size()
744
+ ratio = self.compress_ratio
745
+ compress_len = (start_pos + seqlen) // ratio
746
+ latent = None
747
+ if self.is_kv_source:
748
+ latent = self.compressor(x, start_pos)
749
+ shared_attn.compress_kv = self.compress_kv_cache
750
+ # the indexer needs the latent before RoPE, so it runs before the cache is written
751
+ idxs = self._compress_topk_idxs(x, qr, latent, start_pos, offset, compress_len)
752
+ if latent is not None:
753
+ # a latent stands for the first token of its group, so group j takes position j * ratio
754
+ freqs = (
755
+ self.freqs_cis[: seqlen - seqlen % ratio : ratio]
756
+ if start_pos == 0
757
+ else self.freqs_cis[start_pos + 1 - ratio].unsqueeze(0)
758
+ )
759
+ apply_rotary_emb(latent[..., -self.rope_head_dim :], freqs)
760
+ # Compressed KV uses groups of 16 with E4M3 scales; the indexer uses 32 with E8M0.
761
+ fp4_act_quant(latent, 16, True, scale_dtype=torch.float8_e4m3fn)
762
+ self.compress_kv_cache[:bsz, start_pos // ratio : start_pos // ratio + latent.size(1)] = latent
763
+ # read after the write, so this does not depend on the slice aliasing the cache
764
+ return shared_attn.compress_kv[:bsz, :compress_len], idxs
765
+
766
+ def forward(self, x: torch.Tensor, start_pos: int):
767
+ bsz, seqlen, _ = x.size()
768
+ freqs_cis = self.freqs_cis[start_pos : start_pos + seqlen]
769
+ rd = self.rope_head_dim
770
+
771
+ qr = self.q_norm(self.wq_a(x))
772
+ q = self.wq_b(qr).unflatten(-1, (self.n_local_heads, self.head_dim))
773
+ apply_rotary_emb(q[..., -rd:], freqs_cis)
774
+
775
+ kv, topk_idxs = self._window_kv(x, freqs_cis, start_pos)
776
+ if self.compress_ratio:
777
+ compress_kv, compress_idxs = self._compress_kv(x, qr, start_pos, kv.size(1))
778
+ kv = torch.cat([kv, compress_kv], dim=1)
779
+ topk_idxs = torch.cat([topk_idxs, compress_idxs], dim=-1)
780
+
781
+ o = sparse_attn(q, kv, self.attn_sink, topk_idxs, self.softmax_scale)
782
+ apply_rotary_emb(o[..., -rd:], freqs_cis, True)
783
+
784
+ # wo_a is block-diagonal over groups (each projects only its own heads), hence einsum not
785
+ # Linear. convert.py dequantizes it to bf16; quantize/dequantize o before einsum.
786
+ o = o.view(bsz, seqlen, self.n_local_groups, -1)
787
+ act_quant(o, fp8_block_size, scale_fmt, scale_dtype, True)
788
+ wo_a = self.wo_a.weight.view(self.n_local_groups, self.o_lora_rank, -1)
789
+ o = torch.einsum("bsgd,grd->bsgr", o, wo_a)
790
+ x = self.wo_b(o.flatten(2))
791
+ return x
792
+
793
+
794
+ class Gate(nn.Module):
795
+ """MoE gating. The correction bias steers expert selection only; the routing weights come from the
796
+ unbiased scores. Image-span tokens use a separate bias (training `noaux_tc_for_vl`)."""
797
+
798
+ def __init__(self, layer_id: int, args: ModelArgs):
799
+ super().__init__()
800
+ n_routed_experts, n_activated_experts = args.get_moe_config(layer_id)
801
+ self.dim = args.dim
802
+ self.topk = n_activated_experts
803
+ self.score_func = args.score_func
804
+ self.gate_temp = args.gate_temp
805
+ self.norm_topk_prob = args.norm_topk_prob
806
+ self.route_scale = args.route_scale
807
+ self.weight = nn.Parameter(torch.empty(n_routed_experts, args.dim))
808
+ self.bias = nn.Parameter(torch.empty(n_routed_experts, dtype=torch.float32))
809
+ self.bias_vl = nn.Parameter(torch.empty(n_routed_experts, dtype=torch.float32)) if args.vision_enabled else None
810
+
811
+ def forward(self, x: torch.Tensor, image_mask: torch.Tensor | None = None) -> tuple[torch.Tensor, torch.Tensor]:
812
+ """x: [n, dim]; image_mask: [n] bool, True for tokens inside an image span."""
813
+ scores = linear(x.float(), self.weight.float()) / self.gate_temp
814
+ if self.score_func == "softmax":
815
+ scores = scores.softmax(dim=-1)
816
+ elif self.score_func == "sigmoid":
817
+ scores = scores.sigmoid()
818
+ else:
819
+ scores = F.softplus(scores).sqrt()
820
+ bias = self.bias
821
+ if image_mask is not None and self.bias_vl is not None:
822
+ bias = torch.where(image_mask.unsqueeze(-1), self.bias_vl, bias)
823
+ # the bias picks experts but does not scale them: weights come from the raw scores
824
+ indices = (scores + bias).topk(self.topk, dim=-1)[1]
825
+ weights = scores.gather(1, indices)
826
+ if self.norm_topk_prob and self.topk > 1:
827
+ weights /= weights.sum(dim=-1, keepdim=True) + 1e-20 # not norm_eps, matches training
828
+ weights *= self.route_scale
829
+ return weights, indices
830
+
831
+
832
+ class Expert(nn.Module):
833
+ """One SwiGLU FFN. The clamps come straight from training, where they keep fp8/fp4 activations in
834
+ range: the up branch is clamped on both sides, the gate branch only from above."""
835
+
836
+ def __init__(self, dim: int, inter_dim: int, dtype=None, swiglu_limit=0.0):
837
+ super().__init__()
838
+ self.w1 = Linear(dim, inter_dim, dtype=dtype)
839
+ self.w2 = Linear(inter_dim, dim, dtype=dtype)
840
+ self.w3 = Linear(dim, inter_dim, dtype=dtype)
841
+ self.swiglu_limit = swiglu_limit
842
+
843
+ def forward(self, x: torch.Tensor, weights: torch.Tensor | None = None) -> torch.Tensor:
844
+ dtype = x.dtype
845
+ gate = self.w1(x).float()
846
+ up = self.w3(x).float()
847
+ if self.swiglu_limit > 0:
848
+ up = torch.clamp(up, min=-self.swiglu_limit, max=self.swiglu_limit)
849
+ gate = torch.clamp(gate, max=self.swiglu_limit)
850
+ x = F.silu(gate) * up
851
+ if weights is not None:
852
+ x = weights * x
853
+ return self.w2(x.to(dtype))
854
+
855
+
856
+ class MoE(nn.Module):
857
+ """Top-k routed experts plus one shared expert every token goes through. Experts are split
858
+ across ranks, so `self.experts` is None for those another rank owns."""
859
+
860
+ def __init__(self, layer_id: int, args: ModelArgs):
861
+ super().__init__()
862
+ n_routed_experts, n_activated_experts = args.get_moe_config(layer_id)
863
+ self.layer_id = layer_id
864
+ self.dim = args.dim
865
+ assert n_routed_experts % world_size == 0, (
866
+ f"Number of experts must be divisible by world size (world_size={world_size})"
867
+ )
868
+ self.n_routed_experts = n_routed_experts
869
+ self.n_local_experts = n_routed_experts // world_size
870
+ self.n_activated_experts = n_activated_experts
871
+ self.experts_start_idx = rank * self.n_local_experts
872
+ self.experts_end_idx = self.experts_start_idx + self.n_local_experts
873
+ self.gate = Gate(layer_id, args)
874
+ expert_dtype = torch.float4_e2m1fn_x2 if args.expert_dtype == "fp4" else None
875
+ self.experts = nn.ModuleList(
876
+ [
877
+ Expert(
878
+ args.dim,
879
+ args.moe_inter_dim,
880
+ dtype=expert_dtype,
881
+ swiglu_limit=args.swiglu_limit,
882
+ )
883
+ if self.experts_start_idx <= i < self.experts_end_idx
884
+ else None
885
+ for i in range(self.n_routed_experts)
886
+ ]
887
+ )
888
+ assert args.n_shared_experts == 1
889
+ self.shared_experts = Expert(args.dim, args.moe_inter_dim, swiglu_limit=args.swiglu_limit)
890
+
891
+ def forward(self, x: torch.Tensor, image_mask: torch.Tensor | None = None) -> torch.Tensor:
892
+ shape = x.size()
893
+ x = x.view(-1, self.dim)
894
+ weights, indices = self.gate(x, None if image_mask is None else image_mask.flatten())
895
+ y = torch.zeros_like(x, dtype=torch.float32)
896
+ counts = torch.bincount(indices.flatten(), minlength=self.n_routed_experts).tolist()
897
+ for i in range(self.experts_start_idx, self.experts_end_idx):
898
+ if counts[i] == 0:
899
+ continue
900
+ expert = self.experts[i]
901
+ idx, top = torch.where(indices == i)
902
+ y[idx] += expert(x[idx], weights[idx, top, None])
903
+ if world_size > 1:
904
+ dist.all_reduce(y)
905
+ y += self.shared_experts(x)
906
+ return y.type_as(x).view(shape)
907
+
908
+
909
+ class Block(nn.Module):
910
+ """A block whose residual stream is `hc_mult` parallel copies (Hyper-Connections).
911
+
912
+ Attention and FFN each sit between `hc_pre` (collapse the copies into one sublayer input) and
913
+ `hc_post` (expand back out, mixing the residual in through `comb`). `hc_mixes` derives all three
914
+ coefficient sets from the stream itself, `comb` made doubly stochastic by Sinkhorn.
915
+
916
+ The coefficients a sublayer computes are used by the *next* one -- see `forward`.
917
+ """
918
+
919
+ attention_cls = Attention
920
+
921
+ def __init__(
922
+ self,
923
+ layer_id: int,
924
+ args: ModelArgs,
925
+ engram_layout: EngramLayout | None = None,
926
+ ):
927
+ super().__init__()
928
+ self.layer_id = layer_id
929
+ self.norm_eps = args.norm_eps
930
+ self.attn = self.attention_cls(layer_id, args)
931
+ self.ffn = MoE(layer_id, args)
932
+ self.engram = None
933
+ if engram_layout is not None and layer_id in engram_layout.layer_ids:
934
+ self.engram = Engram(args, layer_id, engram_layout)
935
+ self.attn_norm = RMSNorm(args.dim, self.norm_eps)
936
+ self.ffn_norm = RMSNorm(args.dim, self.norm_eps)
937
+ self.hc_mult = hc_mult = args.hc_mult
938
+ self.hc_sinkhorn_iters = args.hc_sinkhorn_iters
939
+ self.hc_eps = args.hc_eps
940
+ mix_hc = (2 + hc_mult) * hc_mult
941
+ hc_dim = hc_mult * args.dim
942
+ with set_dtype(torch.float32):
943
+ self.hc_attn_fn = nn.Parameter(torch.empty(mix_hc, hc_dim))
944
+ self.hc_ffn_fn = nn.Parameter(torch.empty(mix_hc, hc_dim))
945
+ self.hc_attn_base = nn.Parameter(torch.empty(mix_hc))
946
+ self.hc_ffn_base = nn.Parameter(torch.empty(mix_hc))
947
+ self.hc_attn_scale = nn.Parameter(torch.empty(3))
948
+ self.hc_ffn_scale = nn.Parameter(torch.empty(3))
949
+
950
+ def hc_mixes(self, x: torch.Tensor, hc_fn: torch.Tensor, hc_scale: torch.Tensor, hc_base: torch.Tensor):
951
+ """x: [b,s,hc,d], hc_fn: [mix_hc, hc*d], hc_scale: [3], hc_base: [mix_hc]. Returns the
952
+ pre / post / comb coefficients, split out of one projection of the flattened stream."""
953
+ # normalized over the whole flattened hc*d stream, one statistic per token
954
+ x = x.flatten(2).float()
955
+ rsqrt = torch.rsqrt(x.square().mean(-1, keepdim=True) + self.norm_eps)
956
+ mixes = F.linear(x, hc_fn) * rsqrt
957
+ return hc_split_sinkhorn(mixes, hc_scale, hc_base, self.hc_mult, self.hc_sinkhorn_iters, self.hc_eps)
958
+
959
+ def hc_pre(self, x: torch.Tensor, pre_mix: torch.Tensor):
960
+ """Collapse the hc copies into one, weighted by pre_mix. [b,s,hc,d] x [b,s,hc] -> [b,s,d]"""
961
+ y = torch.sum(pre_mix.unsqueeze(-1) * x.float(), dim=2)
962
+ return y.to(x.dtype)
963
+
964
+ def hc_post(self, x: torch.Tensor, residual: torch.Tensor, post: torch.Tensor, comb: torch.Tensor):
965
+ """Expand the sublayer output back to hc copies and mix the residual in through `comb`.
966
+ x: [b,s,d], residual: [b,s,hc,d], post: [b,s,hc], comb: [b,s,hc,hc] -> [b,s,hc,d]"""
967
+ y = post.unsqueeze(-1) * x.unsqueeze(-2) + torch.sum(comb.unsqueeze(-1) * residual.unsqueeze(-2), dim=2)
968
+ return y.type_as(x)
969
+
970
+ def forward(
971
+ self,
972
+ x: torch.Tensor,
973
+ start_pos: int,
974
+ pre_mix: torch.Tensor,
975
+ image_mask: torch.Tensor | None,
976
+ *attn_args,
977
+ ) -> tuple[torch.Tensor, torch.Tensor]:
978
+ """`pre_mix` collapses the hc_mult copies down to one input for this block's attention. Each
979
+ sub-block's own `hc_mixes` produces the mix for the *next* one, so attention uses what the
980
+ previous layer's FFN produced and the FFN uses what this attention produced.
981
+
982
+ image_mask: [b, s] bool, True inside image spans (selects the VL routing bias)."""
983
+ residual = x
984
+ attn_pre, attn_post, attn_comb = self.hc_mixes(x, self.hc_attn_fn, self.hc_attn_scale, self.hc_attn_base)
985
+ x = self.hc_pre(x, pre_mix)
986
+ x = self.attn_norm(x)
987
+ x = self.attn(x, start_pos, *attn_args)
988
+ x = self.hc_post(x, residual, attn_post, attn_comb)
989
+
990
+ residual = x
991
+ ffn_pre, ffn_post, ffn_comb = self.hc_mixes(x, self.hc_ffn_fn, self.hc_ffn_scale, self.hc_ffn_base)
992
+ x = self.hc_pre(x, attn_pre)
993
+ x = self.ffn_norm(x)
994
+ x = self.ffn(x, image_mask)
995
+ x = self.hc_post(x, residual, ffn_post, ffn_comb)
996
+ return x, ffn_pre
997
+
998
+
999
+ class ParallelHead(nn.Module):
1000
+ def __init__(self, vocab_size: int, dim: int, norm_eps: float = 1e-6, hc_eps: float = 1e-6):
1001
+ super().__init__()
1002
+ self.vocab_size = vocab_size
1003
+ self.dim = dim
1004
+ self.norm_eps = norm_eps
1005
+ self.hc_eps = hc_eps
1006
+ self.part_vocab_size = vocab_size // world_size
1007
+ # bf16 in the checkpoint, kept as fp32 here so the logits come out in fp32 directly
1008
+ self.weight = nn.Parameter(torch.empty(self.part_vocab_size, self.dim, dtype=torch.float32))
1009
+
1010
+ def forward(self, x: torch.Tensor, full_logits=False):
1011
+ """x: [b, s, d]. Generation only needs the last position, so that is the default."""
1012
+ if not full_logits:
1013
+ x = x[:, -1]
1014
+ logits = F.linear(x.float(), self.weight)
1015
+ if world_size > 1:
1016
+ all_logits = [torch.empty_like(logits) for _ in range(world_size)]
1017
+ dist.all_gather(all_logits, logits)
1018
+ logits = torch.cat(all_logits, dim=-1)
1019
+ return logits
1020
+
1021
+
1022
+ @lru_cache(1)
1023
+ def get_dspark_topk_idxs(window_size: int, bsz: int, block_size: int, start_pos: int):
1024
+ assert start_pos > 0
1025
+ matrix = torch.cat(
1026
+ [
1027
+ torch.arange(min(window_size, start_pos + 1)),
1028
+ window_size + torch.arange(block_size),
1029
+ ]
1030
+ )
1031
+ return matrix.int().view(1, 1, -1).expand(bsz, block_size, -1).contiguous()
1032
+
1033
+
1034
+ class DSparkAttention(Attention):
1035
+ def forward(self, x: torch.Tensor, start_pos: int, main_x: torch.Tensor):
1036
+ assert self.compress_ratio == 0
1037
+ bsz, seqlen, _ = main_x.size()
1038
+ win = self.window_size
1039
+ rd = self.rope_head_dim
1040
+
1041
+ main_freqs_cis = self.freqs_cis[start_pos : start_pos + seqlen]
1042
+ main_kv = self.kv_norm(self.wkv(main_x))
1043
+ apply_rotary_emb(main_kv[..., -rd:], main_freqs_cis)
1044
+ act_quant(main_kv, fp8_block_size, scale_fmt, scale_dtype, True)
1045
+
1046
+ if start_pos == 0:
1047
+ if seqlen <= win:
1048
+ self.window_kv_cache[:bsz, :seqlen] = main_kv
1049
+ else:
1050
+ cutoff = seqlen % win
1051
+ self.window_kv_cache[:bsz, cutoff:win], self.window_kv_cache[:bsz, :cutoff] = main_kv[:, -win:].split(
1052
+ [win - cutoff, cutoff], dim=1
1053
+ )
1054
+ return x
1055
+
1056
+ bsz, block_size, _ = x.size()
1057
+ freqs_cis = self.freqs_cis[start_pos + seqlen : start_pos + seqlen + block_size]
1058
+
1059
+ qr = self.q_norm(self.wq_a(x))
1060
+ q = self.wq_b(qr).unflatten(-1, (self.n_local_heads, self.head_dim))
1061
+ apply_rotary_emb(q[..., -rd:], freqs_cis)
1062
+ kv = self.kv_norm(self.wkv(x))
1063
+ apply_rotary_emb(kv[..., -rd:], freqs_cis)
1064
+ act_quant(kv, fp8_block_size, scale_fmt, scale_dtype, True)
1065
+
1066
+ topk_idxs = get_dspark_topk_idxs(win, bsz, block_size, start_pos)
1067
+ self.window_kv_cache[:bsz, start_pos % win] = main_kv.squeeze(1)
1068
+ kv = torch.cat([self.window_kv_cache[:bsz], kv], dim=1)
1069
+ o = sparse_attn(q, kv, self.attn_sink, topk_idxs, self.softmax_scale)
1070
+ apply_rotary_emb(o[..., -rd:], freqs_cis, True)
1071
+
1072
+ o = o.view(bsz, block_size, self.n_local_groups, -1)
1073
+ act_quant(o, fp8_block_size, scale_fmt, scale_dtype, True)
1074
+ wo_a = self.wo_a.weight.view(self.n_local_groups, self.o_lora_rank, -1)
1075
+ o = torch.einsum("bsgd,grd->bsgr", o, wo_a)
1076
+ x = self.wo_b(o.flatten(2))
1077
+ return x
1078
+
1079
+
1080
+ class DSparkMarkovHead(nn.Module):
1081
+ def __init__(self, vocab_size: int, dspark_markov_rank: int):
1082
+ super().__init__()
1083
+ self.embed = ParallelEmbedding(vocab_size, dspark_markov_rank)
1084
+ self.head = ParallelHead(vocab_size, dspark_markov_rank)
1085
+
1086
+ def forward(self, token_ids: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
1087
+ embed = self.embed(token_ids)
1088
+ logits = self.head(embed, full_logits=True)
1089
+ return logits, embed
1090
+
1091
+
1092
+ class DSparkConfidenceHead(nn.Module):
1093
+ def __init__(self, input_dim: int):
1094
+ super().__init__()
1095
+ # proj in the checkpoint is stored in bf16, while the parameter here is stored in fp32 for fp32 confidence score.
1096
+ self.proj = Linear(input_dim, 1, dtype=torch.float32)
1097
+
1098
+ def forward(self, hidden: torch.Tensor, markov_embed: torch.Tensor):
1099
+ hidden = torch.cat([hidden, markov_embed], dim=-1)
1100
+ return self.proj(hidden.float()).squeeze(-1)
1101
+
1102
+
1103
+ class DSparkBlock(Block):
1104
+ """DSpark stage stored under the mtp.* checkpoint namespace."""
1105
+
1106
+ attention_cls = DSparkAttention
1107
+
1108
+ def __init__(self, layer_id: int, args: ModelArgs):
1109
+ super().__init__(layer_id, args)
1110
+ stage_id = layer_id - args.n_layers
1111
+ self.block_size = args.dspark_block_size
1112
+ self.noise_token_id = args.dspark_noise_token_id
1113
+ self.temperature = args.temperature
1114
+ if stage_id == 0:
1115
+ assert len(args.dspark_target_layer_ids) > 0, "DSpark needs target layers"
1116
+ self.main_proj = Linear(args.dim * len(args.dspark_target_layer_ids), args.dim)
1117
+ self.main_norm = RMSNorm(args.dim, args.norm_eps)
1118
+ if stage_id == args.n_mtp_layers - 1:
1119
+ self.norm = RMSNorm(args.dim, args.norm_eps)
1120
+ self.markov_head = DSparkMarkovHead(args.vocab_size, args.dspark_markov_rank)
1121
+ self.confidence_head = DSparkConfidenceHead(args.dim + args.dspark_markov_rank)
1122
+ self.embed: ParallelEmbedding | None = None
1123
+ self.head: ParallelHead | None = None
1124
+
1125
+ def forward(self, x: torch.Tensor, start_pos: int, pre_mix: torch.Tensor, main_x: torch.Tensor):
1126
+ if start_pos == 0:
1127
+ self.attn(x, start_pos, main_x) # prefill only seeds the window KV cache
1128
+ return x, pre_mix
1129
+ return super().forward(x, start_pos, pre_mix, None, main_x) # drafts are text: no VL bias
1130
+
1131
+ def forward_embed(self, main_hidden: torch.Tensor, input_ids: torch.Tensor):
1132
+ assert self.embed is not None
1133
+ main_x = self.main_norm(self.main_proj(main_hidden))
1134
+ draft_input_ids = input_ids.new_full([input_ids.size(0), self.block_size], self.noise_token_id)
1135
+ draft_input_ids[:, 0] = input_ids
1136
+ x = self.embed(draft_input_ids)
1137
+ x = x.unsqueeze(2).repeat(1, 1, self.hc_mult, 1)
1138
+ return x, main_x
1139
+
1140
+ def forward_head(
1141
+ self,
1142
+ x: torch.Tensor,
1143
+ pre_mix: torch.Tensor,
1144
+ input_ids: torch.Tensor,
1145
+ ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
1146
+ assert self.head is not None
1147
+ x = self.hc_pre(x, pre_mix)
1148
+ logits = self.head(self.norm(x), full_logits=True)
1149
+ output_ids = input_ids.new_empty(input_ids.size(0), self.block_size + 1)
1150
+ output_ids[:, 0] = input_ids
1151
+ markov_embeds = []
1152
+ for i in range(self.block_size):
1153
+ logits_bias, markov_embed = self.markov_head(output_ids[:, i])
1154
+ logits[:, i].add_(logits_bias)
1155
+ markov_embeds.append(markov_embed)
1156
+ output_ids[:, i + 1] = sample(logits[:, i], self.temperature)
1157
+ markov_embed = torch.stack(markov_embeds, dim=1)
1158
+ confidence = self.confidence_head(x, markov_embed)
1159
+ return output_ids, logits, confidence
1160
+
1161
+
1162
+ def make_identity_pre_mix(x: torch.Tensor, hc_mult: int) -> torch.Tensor:
1163
+ """initial one-hot mix"""
1164
+ pre_mix = x.new_zeros(x.size(0), x.size(1), hc_mult, dtype=torch.float32)
1165
+ pre_mix[:, :, 0] = 1.0
1166
+ return pre_mix
1167
+
1168
+
1169
+ class SharedAttentionRuntime:
1170
+ """What attention layers hand down the stack instead of recomputing. Layers run in order and
1171
+ every source writes before its consumers read, so one slot each is enough and nothing needs
1172
+ resetting between forwards. Sources: compress_kv and index_k from kv_source_layers, topk_idxs
1173
+ from index_source_layers, candidates from candidate_source_layer."""
1174
+
1175
+ def __init__(self):
1176
+ self.compress_kv: torch.Tensor | None = None
1177
+ self.index_k: torch.Tensor | None = None
1178
+ self.topk_idxs: torch.Tensor | None = None
1179
+ self.candidates: torch.Tensor | None = None
1180
+
1181
+
1182
+ # Only ever one model per process, same as world_size / rank / default_dtype above.
1183
+ shared_attn = SharedAttentionRuntime()
1184
+
1185
+
1186
+ class Transformer(nn.Module):
1187
+ """Baanzon-Action-1.1: embed -> expand to hc_mult copies -> blocks -> collapse -> logits. Building
1188
+ this sets the globals at the top of the file. The tokenizer only feeds the engram token map."""
1189
+
1190
+ def __init__(self, args: ModelArgs, tokenizer=None):
1191
+ global world_size, rank, default_dtype
1192
+ world_size = dist.get_world_size() if dist.is_initialized() else 1
1193
+ rank = dist.get_rank() if dist.is_initialized() else 0
1194
+ default_dtype = torch.float8_e4m3fn if args.dtype == "fp8" else torch.bfloat16
1195
+ super().__init__()
1196
+ self.max_seq_len = args.max_seq_len
1197
+ self.temperature = args.temperature
1198
+ self.norm_eps = args.norm_eps
1199
+ self.hc_eps = args.hc_eps
1200
+ self.engram_layout = EngramLayout.from_args(args)
1201
+ self.engram_hash = (
1202
+ NgramHashState(args, self.engram_layout, tokenizer) if self.engram_layout is not None else None
1203
+ )
1204
+ self.embed = ParallelEmbedding(args.vocab_size, args.dim)
1205
+ self.layers = torch.nn.ModuleList()
1206
+ for layer_id in range(args.n_layers):
1207
+ self.layers.append(Block(layer_id, args, self.engram_layout))
1208
+ self.norm = RMSNorm(args.dim, self.norm_eps)
1209
+ self.head = ParallelHead(args.vocab_size, args.dim, self.norm_eps, self.hc_eps)
1210
+ self.mtp = torch.nn.ModuleList()
1211
+ self.target_layer_ids = args.dspark_target_layer_ids
1212
+ if args.dspark_block_size:
1213
+ for layer_id in range(args.n_mtp_layers):
1214
+ self.mtp.append(DSparkBlock(args.n_layers + layer_id, args))
1215
+ self.mtp[-1].embed = self.embed
1216
+ self.mtp[-1].head = self.head
1217
+ self.hc_mult = args.hc_mult
1218
+ self.vision = None
1219
+ if args.vision_enabled:
1220
+ self.vision = ViT(args)
1221
+ self.aligner = Aligner(args)
1222
+ # learned embeddings for the image span delimiters
1223
+ self.image_start = nn.Parameter(torch.empty(args.dim))
1224
+ self.image_end = nn.Parameter(torch.empty(args.dim))
1225
+ self.image_newline = nn.Parameter(torch.empty(args.dim))
1226
+
1227
+ @torch.inference_mode()
1228
+ def encode_image(self, patches: torch.Tensor, n_vit_h: int, n_vit_w: int) -> torch.Tensor:
1229
+ return self.aligner(self.vision(patches, n_vit_h, n_vit_w), n_vit_h, n_vit_w)
1230
+
1231
+ def merge_image_embeddings(self, images, h: torch.Tensor):
1232
+ """Overwrite each image's token span in h with its ViT/aligner features. The IMAGE slots take
1233
+ the aligner rows in row-major order; the span delimiters take learned embeddings."""
1234
+ for i, sample in enumerate(images):
1235
+ for img in sample or ():
1236
+ types = img.types.to(h.device)
1237
+ span = h[i, img.start : img.start + types.numel()]
1238
+ span[types == IMAGE_START] = self.image_start.to(h.dtype)
1239
+ span[types == IMAGE_END] = self.image_end.to(h.dtype)
1240
+ span[types == IMAGE_NEW_LINE] = self.image_newline.to(h.dtype)
1241
+ embeds = self.encode_image(img.patches.to(h.device), img.n_vit_h, img.n_vit_w)
1242
+ span[types == IMAGE] = embeds.to(h.dtype)
1243
+
1244
+ @torch.inference_mode()
1245
+ def forward(
1246
+ self, input_ids: torch.Tensor, start_pos: int = 0, images=None, token_types: torch.Tensor | None = None
1247
+ ):
1248
+ """input_ids: [b, s], every entry a real token id -- generate.py only ever passes positions it
1249
+ has already filled, so the padding it uses internally never reaches here. token_types /
1250
+ images carry the VL inputs built by image_processor.prepare_vl_inputs; image spans must lie
1251
+ inside the first (start_pos 0) chunk."""
1252
+ image_mask = None if token_types is None else token_types >= 0 # TEXT is -1
1253
+ # image tokens take no part in an n-gram and get no engram contribution; text-only needs no mask
1254
+ engram_mask = None if image_mask is None else ~image_mask
1255
+ engram_hashes = self.engram_hash(input_ids, start_pos, engram_mask) if self.engram_hash is not None else None
1256
+ h = self.embed(input_ids)
1257
+ if images is not None:
1258
+ assert start_pos == 0, "image spans must be prefilled in a single chunk"
1259
+ self.merge_image_embeddings(images, h)
1260
+ # Expand to hc_mult copies for Hyper-Connections
1261
+ h = h.unsqueeze(2).repeat(1, 1, self.hc_mult, 1)
1262
+ main_hiddens = []
1263
+ pre_mix = make_identity_pre_mix(h, self.hc_mult)
1264
+ for i, layer in enumerate(self.layers):
1265
+ if layer.engram is not None:
1266
+ h = layer.engram(h, engram_hashes[:, :, layer.engram.layer_hash_index, :], engram_mask)
1267
+ # the MTP head reads the attention input of its target layers, not their output
1268
+ if i in self.target_layer_ids:
1269
+ main_hiddens.append(h.mean(dim=2))
1270
+ h, pre_mix = layer(h, start_pos, pre_mix, image_mask)
1271
+ h = layer.hc_pre(h, pre_mix)
1272
+ logits = self.head(self.norm(h))
1273
+ output_ids = sample(logits, self.temperature)
1274
+ main_hidden = torch.cat(main_hiddens, dim=-1) if main_hiddens else None
1275
+ return output_ids, logits, main_hidden
1276
+
1277
+ @torch.inference_mode()
1278
+ def forward_spec(self, input_ids: torch.Tensor, main_hidden: torch.Tensor, start_pos: int = 0):
1279
+ h, main_x = self.mtp[0].forward_embed(main_hidden, input_ids)
1280
+ pre_mix = make_identity_pre_mix(h, self.hc_mult)
1281
+ for layer in self.mtp:
1282
+ h, pre_mix = layer(h, start_pos, pre_mix, main_x)
1283
+ if start_pos == 0:
1284
+ return None
1285
+ return self.mtp[-1].forward_head(h, pre_mix, input_ids)
1286
+
1287
+
1288
+ def sample(logits, temperature: float = 1.0):
1289
+ """Gumbel-max trick: equivalent to multinomial sampling but faster on GPU,
1290
+ since it avoids the GPU-to-CPU sync in torch.multinomial."""
1291
+ if temperature == 0:
1292
+ return logits.argmax(dim=-1)
1293
+ logits = logits / max(temperature, 1e-5)
1294
+ probs = torch.softmax(logits, dim=-1, dtype=torch.float32)
1295
+ return probs.div_(torch.empty_like(probs).exponential_(1)).argmax(dim=-1)
1296
+
1297
+
1298
+ if __name__ == "__main__":
1299
+ torch.set_default_dtype(torch.bfloat16)
1300
+ torch.set_default_device("cuda")
1301
+ torch.manual_seed(0)
1302
+ args = ModelArgs(dspark_block_size=6, dspark_target_layer_ids=(3, 4))
1303
+ x = torch.randint(0, args.vocab_size, (2, 150))
1304
+ model = Transformer(args)
1305
+
1306
+ output_ids, logits, main_hidden = model(x[:, :128])
1307
+ model.forward_spec(output_ids, main_hidden)
1308
+ for i in range(128, 150):
1309
+ output_ids, logits, main_hidden = model(x[:, i : i + 1], i)
1310
+ result = model.forward_spec(output_ids, main_hidden, i)
1311
+ assert result is not None
1312
+ output_ids, logits, confidence = result
inference/requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ torch>=2.10.0
2
+ transformers
3
+ tokenizers
4
+ safetensors>=0.7.0
5
+ numpy
6
+ sympy
7
+ Pillow
8
+ tilelang==0.1.12
9
+ tqdm
inference/run.sh ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ #
3
+ # Run the reference inference on a converted checkpoint.
4
+ #
5
+ # ./run.sh /path/to/Baanzon-Action-1.1-Exp-TP8
6
+ # ./run.sh /path/to/Baanzon-Action-1.1-Exp-TP8 examples/example_harmony.json
7
+ # MP=4 ./run.sh /path/to/Baanzon-Action-1.1-Exp-TP4
8
+ #
9
+ # Paths inside an example are resolved from this directory, so run it from anywhere.
10
+
11
+ set -euo pipefail
12
+ cd "$(dirname "$0")"
13
+
14
+ CKPT_PATH="${1:-${CKPT_PATH:-}}"
15
+ INPUT_FILE="${2:-${INPUT_FILE:-examples/example_harmony.json}}"
16
+ MP="${MP:-8}"
17
+ CONFIG="${CONFIG:-config.json}"
18
+
19
+ usage() {
20
+ echo "usage: $0 <checkpoint-dir> [input-file]" >&2
21
+ echo >&2
22
+ echo " checkpoint-dir holds model0-mp${MP}.safetensors .. model$((MP - 1))-mp${MP}.safetensors," >&2
23
+ echo " as produced by convert.py --model-parallel ${MP}" >&2
24
+ echo " input-file TXT or JSON prompts (default: examples/example.txt)" >&2
25
+ echo >&2
26
+ echo " MP=${MP} CONFIG=${CONFIG} override with environment variables" >&2
27
+ exit 1
28
+ }
29
+
30
+ [ -n "${CKPT_PATH}" ] || usage
31
+
32
+ if [ ! -d "${CKPT_PATH}" ]; then
33
+ echo "error: checkpoint directory not found: ${CKPT_PATH}" >&2
34
+ usage
35
+ fi
36
+
37
+ missing=0
38
+ for rank in $(seq 0 $((MP - 1))); do
39
+ if [ ! -f "${CKPT_PATH}/model${rank}-mp${MP}.safetensors" ]; then
40
+ missing=$((missing + 1))
41
+ fi
42
+ done
43
+ if [ "${missing}" -ne 0 ]; then
44
+ echo "error: ${CKPT_PATH} is missing ${missing} of the ${MP} shards MP=${MP} needs" >&2
45
+ echo " expected model0-mp${MP}.safetensors .. model$((MP - 1))-mp${MP}.safetensors" >&2
46
+ usage
47
+ fi
48
+
49
+ [ -f "${INPUT_FILE}" ] || { echo "error: input file not found: ${INPUT_FILE}" >&2; usage; }
50
+
51
+ torchrun --nproc-per-node "${MP}" generate.py \
52
+ --ckpt-path "${CKPT_PATH}" \
53
+ --config "${CONFIG}" \
54
+ --input-file "${INPUT_FILE}"
inference/vision.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from functools import lru_cache
2
+
3
+ import torch
4
+ import torch.nn.functional as F
5
+ from torch import nn
6
+
7
+
8
+ @lru_cache(8)
9
+ def get_vision_cos_sin(n_h: int, n_w: int, dim: int, theta: float):
10
+ inv_freq = 1.0 / (theta ** (torch.arange(0, dim, 2, dtype=torch.float32) / dim))
11
+ hpos = torch.arange(n_h).unsqueeze(1).expand(n_h, n_w)
12
+ wpos = torch.arange(n_w).unsqueeze(0).expand(n_h, n_w)
13
+ freqs = torch.stack([hpos, wpos], dim=-1).reshape(-1, 2, 1).float() * inv_freq
14
+ freqs = freqs.flatten(1)
15
+ return freqs.cos().unsqueeze(1), freqs.sin().unsqueeze(1)
16
+
17
+
18
+ def apply_rotary(x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor) -> torch.Tensor:
19
+ dtype = x.dtype
20
+ x1, x2 = x.float().chunk(2, dim=-1)
21
+ return torch.cat([x1 * cos - x2 * sin, x2 * cos + x1 * sin], dim=-1).to(dtype)
22
+
23
+
24
+ class RMSNorm(nn.Module):
25
+ def __init__(self, dim: int, eps: float = 1e-6):
26
+ super().__init__()
27
+ self.eps = eps
28
+ self.weight = nn.Parameter(torch.ones(dim, dtype=torch.float32))
29
+
30
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
31
+ dtype = x.dtype
32
+ x = x.float()
33
+ x = x * torch.rsqrt(x.square().mean(-1, keepdim=True) + self.eps)
34
+ return (self.weight * x).to(dtype)
35
+
36
+
37
+ class PatchEmbed(nn.Module):
38
+ def __init__(self, args):
39
+ super().__init__()
40
+ self.proj = nn.Linear(3 * args.vision_patch_size**2, args.vision_dim)
41
+
42
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
43
+ return self.proj(x.flatten(1))
44
+
45
+
46
+ class Attention(nn.Module):
47
+ def __init__(self, args):
48
+ super().__init__()
49
+ self.n_heads = args.vision_n_heads
50
+ self.head_dim = args.vision_dim // args.vision_n_heads
51
+ self.wqkv = nn.Linear(args.vision_dim, 3 * args.vision_dim)
52
+ self.wo = nn.Linear(args.vision_dim, args.vision_dim)
53
+
54
+ def forward(self, x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor) -> torch.Tensor:
55
+ n = x.size(0)
56
+ q, k, v = (t.view(n, self.n_heads, self.head_dim) for t in self.wqkv(x).chunk(3, dim=-1))
57
+ q = apply_rotary(q, cos, sin)
58
+ k = apply_rotary(k, cos, sin)
59
+ o = F.scaled_dot_product_attention(q.transpose(0, 1), k.transpose(0, 1), v.transpose(0, 1))
60
+ return self.wo(o.transpose(0, 1).reshape(n, -1))
61
+
62
+
63
+ class MLP(nn.Module):
64
+ def __init__(self, args):
65
+ super().__init__()
66
+ self.w1 = nn.Linear(args.vision_dim, 2 * args.vision_inter_dim, bias=False)
67
+ self.w2 = nn.Linear(args.vision_inter_dim, args.vision_dim, bias=False)
68
+
69
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
70
+ gate, up = self.w1(x).chunk(2, dim=-1)
71
+ return self.w2(F.silu(gate) * up)
72
+
73
+
74
+ class Block(nn.Module):
75
+ def __init__(self, args):
76
+ super().__init__()
77
+ self.norm1 = RMSNorm(args.vision_dim)
78
+ self.attn = Attention(args)
79
+ self.norm2 = RMSNorm(args.vision_dim)
80
+ self.mlp = MLP(args)
81
+
82
+ def forward(self, x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor) -> torch.Tensor:
83
+ x = x + self.attn(self.norm1(x), cos, sin)
84
+ return x + self.mlp(self.norm2(x))
85
+
86
+
87
+ class ViT(nn.Module):
88
+ """Baanzon ViT: full bidirectional attention over one image with 2D RoPE."""
89
+
90
+ def __init__(self, args):
91
+ super().__init__()
92
+ self.rope_dim = args.vision_dim // args.vision_n_heads // 2
93
+ self.rope_theta = args.vision_rope_theta
94
+ self.patch_embed = PatchEmbed(args)
95
+ self.blocks = nn.ModuleList([Block(args) for _ in range(args.vision_n_layers)])
96
+ self.norm = RMSNorm(args.vision_dim)
97
+
98
+ def forward(self, patches: torch.Tensor, n_h: int, n_w: int) -> torch.Tensor:
99
+ x = self.patch_embed(patches)
100
+ cos, sin = get_vision_cos_sin(n_h, n_w, self.rope_dim, self.rope_theta)
101
+ for block in self.blocks:
102
+ x = block(x, cos, sin)
103
+ return self.norm(x)
104
+
105
+
106
+ class Aligner(nn.Module):
107
+ def __init__(self, args):
108
+ super().__init__()
109
+ self.downsample_ratio = args.vision_downsample_ratio
110
+ in_dim = args.vision_dim * self.downsample_ratio**2
111
+ self.w1 = nn.Linear(in_dim, args.dim)
112
+ self.w2 = nn.Linear(args.dim, args.dim)
113
+
114
+ def forward(self, x: torch.Tensor, n_h: int, n_w: int) -> torch.Tensor:
115
+ r = self.downsample_ratio
116
+ x = x.view(n_h, n_w, -1).permute(2, 0, 1)
117
+ x = F.pad(x, (0, -n_w % r, 0, -n_h % r))
118
+ x = F.unfold(x.unsqueeze(0), r, stride=r).squeeze(0).transpose(0, 1)
119
+ return self.w2(F.gelu(self.w1(x)))
model-00001-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:13bd121075f0b68cb7e8e8021a1011d42a81a83f557a2602fd9c07d52fddb0ec
3
+ size 970533624
model-00002-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:43616eebd88dd4f1f1b642772356fc160478d1d9d04edd240431e7d065d2eda7
3
+ size 1323858272
model-00003-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:41611994fc9b27829b757d72257570d65a976fe65b8c6df8641adcd139d65ded
3
+ size 7389759032
model-00004-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e8e901b3cb1ab46f32729c7eaab37bd774a09094f0a02c51a60e883f779f2c39
3
+ size 7389759032
model-00005-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c5df71c02d6a22bcb20a0dbbcda9c6b65aa104f44415102a9a6a03bee92bb9de
3
+ size 7405953784
model-00006-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a5d42afde2aee33619fb7a674b4c737c38d1f1d47a636a26f3edfb3b54164167
3
+ size 7389759032
model-00007-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:24856942add8f73f5e1fbaea1e9c9b6ff597fd1b4aa4651d789833dd3a5a2ab9
3
+ size 7389759032
model-00008-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7bf5f7afb5d2bacad49feffb049989fb365d6dbf96986e76265bd7a7a3469564
3
+ size 7389759032
model-00009-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e6ae47e9daca138fda8c04f833c8328eb21671c37d1c8e2c3f7a82aac26649f6
3
+ size 7389759032
model-00010-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:835e9a8b0a28cc571cb1cf9e6014b3d5eb84ac41990a8aa4519e1ac435d7e23c
3
+ size 7389759032
model-00011-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5df968edefe59033e451ff8512bef57da2cf97b57b4e7a1380118489dd02915b
3
+ size 7405953784
model-00012-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4f97bf292f58178ecdf593b55252a401d6e9102f18625a4fccc52b8ee57e7be0
3
+ size 7389759032
model-00013-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5b4a5592a55c57b1f685f21f880fa6139fa8a66b05c86eb89736a65913ee67b6
3
+ size 7389761400
model-00014-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:532cf21b4b2271d333e6956e2bd0e521cb3625fc402f25059b18b91753238b99
3
+ size 7389761400
model-00015-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f4407707d1e20fa40ef26b25e9fea00027abe940d3c56237f6c4e90429c05bcb
3
+ size 7389761400
model-00016-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:976e0637f96a14766d359630ca3954a2c4b2d35232cda69098ace22ea3d39b95
3
+ size 7389761400
model-00017-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f19e87cb64364081f51b779dd7b9e82f06b130faf0b242bb63e62114cf3b4b13
3
+ size 7405956160
model-00018-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:152c51cdf3c6b3bea6c06574db280b11a6c3d5b92c22be8100bc7d0fabbd587a
3
+ size 7389761400
model-00019-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c3094fad7fe92f8e047028f7a82a7cde8842129a1d58c0124b5ce03ca427875c
3
+ size 7389761400
model-00020-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fbddaf8d00ffc09b3bba0771678c6f79c4b4a8be19bc8e95cd3a1e7603f2b59f
3
+ size 7389761400
model-00021-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f6c361c464bd33bdcee0455af38e3cb0f4a33208b3afd244456145e3cef6bd55
3
+ size 7389761400
model-00022-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b6e70b04a6ee2c60b65e6461fa01626dd88b09681f4001c818dc7f6ef3976845
3
+ size 7389761400
model-00023-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8d24c353c12858c4d9679e0cd65a2810a516ba81e5f796594f0121542ecd95c1
3
+ size 7400713120
model-00024-of-00048.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:743f63bf59f0f524e0baf7648fee94f45388250375da8b30731bc863c61e7107
3
+ size 7389761400