Messi-Hua commited on
Commit
2fd8ae7
·
verified ·
1 Parent(s): 06cd0e0

Upload Intern-S2-Mobius model

Browse files
.gitattributes CHANGED
@@ -33,3 +33,8 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ tokenizer.json filter=lfs diff=lfs merge=lfs -text
37
+ figs/efficiency.jpg filter=lfs diff=lfs merge=lfs -text
38
+ figs/title.png filter=lfs diff=lfs merge=lfs -text
39
+ figs/performance.png filter=lfs diff=lfs merge=lfs -text
40
+ figs/case-study.png filter=lfs diff=lfs merge=lfs -text
0092638_seism.npy ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c2b94653c6964b630038897a27cb6d276ff866d9ecd1f6419358b9407f0df62e
3
+ size 72128
LICENSE ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2025-2026 Shanghai AI Laboratory
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
README.md CHANGED
@@ -1,3 +1,78 @@
1
  ---
 
2
  license: apache-2.0
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ library_name: transformers
3
  license: apache-2.0
4
+ license_link: https://huggingface.co/internlm/Intern-S2-Mobius/blob/main/LICENSE
5
+ pipeline_tag: image-text-to-text
6
  ---
7
+
8
+ ## Intern-S2-Mobius
9
+
10
+ <div align="center">
11
+ <img src="./figs/title.png" />
12
+
13
+ <div>&nbsp;</div>
14
+
15
+ [💻Github Repo](https://github.com/InternLM/Intern-S2-Mobius) • [🤗Model Collections](https://huggingface.co/collections/internlm/intern-s2) • [💬Online Chat](https://chat.intern-ai.org.cn/)
16
+
17
+ </div>
18
+
19
+
20
+ ## Introduction
21
+
22
+ We introduce **Intern-S2-Mobius**, a 35B foundation model built on the Mobius-v0 architecture, which decouples knowledge vectors from reasoning operators inside the base model. Instead of binding knowledge storage and reasoning computation layer by layer as in conventional Transformer models, Mobius organizes knowledge into a globally shared **Memory** and lets multiple **Reasoners** iteratively query and refine hidden states against this shared repository.
23
+
24
+ This knowledge-reasoning separation gives Intern-S2-Mobius two native capabilities: **Backward Residual Connection**, where reasoning stages can access knowledge beyond their local layer hierarchy, and **Dynamic Latent Reasoning**, where deliberation, refinement, and multi-token prediction are internalized into high-density continuous states. Continual-pretrained from Qwen3.5-35B and further post-trained with SFT and RL, Intern-S2-Mobius preserves strong downstream capability while achieving substantially higher end-to-end inference efficiency, with nearly 4x speedup reported in the technical report.
25
+
26
+ ### Features
27
+
28
+ - **Knowledge-reasoning decoupled architecture.** Intern-S2-Mobius separates knowledge vectors from reasoning operators by replacing layer-bound FFN knowledge storage with a globally shared Memory. This gives each Reasoner access to a broader knowledge space and improves knowledge compression compared with a standard Transformer layout.
29
+
30
+ - **Backward Residual Connection.** Through shared Memory, shallow and deep reasoning stages can access knowledge across the model rather than relying only on forward layer-wise information flow. This enables more flexible cross-layer knowledge composition and helps the model synthesize useful information in fewer reasoning steps.
31
+
32
+ - **Dynamic Latent Reasoning.** Mobius refines continuous hidden states through recurrent latent iteration before decoding. This internalizes part of the deliberation process, reduces reliance on long visible chain-of-thought, and dynamically allocates computation to different tokens.
33
+
34
+ - **Higher inference efficiency with concise reasoning.** On reasoning benchmarks, Intern-S2-Mobius reaches comparable or stronger scores than the Qwen3.5-35B baseline while producing markedly shorter reasoning traces and higher request throughput, leading to nearly 4x end-to-end inference speedup in the reported evaluation.
35
+
36
+ - **Strong general and scientific performance.** Intern-S2-Mobius improves the reported average score over Qwen3.5-35B on general reasoning benchmarks, and shows large gains on scientific tasks such as Biology-Instructions, Mol-Instructions, and MolecularIQ.
37
+
38
+ <figure>
39
+ <img src="./figs/efficiency.png" alt="Mobius inference efficiency">
40
+ <figcaption>Fig1: Inference efficiency on reasoning benchmarks. Intern-S2-Mobius improves request throughput over the Transformer baseline while maintaining strong reasoning performance, with gains largely coming from shorter, more compact reasoning traces.</figcaption>
41
+ </figure>
42
+
43
+ ### Performance
44
+
45
+ We evaluate the Intern-S2-Mobius on various benchmarks, including general datasets and scientific datasets. We report the performance comparison with Qwen3.5-35B below.
46
+
47
+ ![performance](./figs/performance.png)
48
+
49
+
50
+ > **Note**: **Bold** indicates the best performance among all models.
51
+
52
+ We use the [OpenCompass](https://github.com/open-compass/OpenCompass/) and [VLMEvalKit](https://github.com/open-compass/vlmevalkit) to evaluate all models. For text benchmarks, Intern-S2-Mobius is evaluated with a maximum inference length of 64K tokens on MMLU Pro, SimpleQA, and HLE, and 128K tokens on the remaining text benchmarks. For multimodal benchmarks, Intern-S2-Mobius is evaluated with a maximum inference length of 64K tokens.
53
+
54
+
55
+ ## Quick Start
56
+
57
+ ### Sampling Parameters
58
+
59
+ We recommend using the following hyperparameters to ensure better results
60
+
61
+ ```python
62
+ top_p = 0.95
63
+ top_k = 50
64
+ min_p = 0.0
65
+ temperature = 0.8
66
+ ```
67
+
68
+ ### Serving
69
+
70
+ Intern-S2-Mobius can be deployed using any of the following LLM inference frameworks:
71
+
72
+ - LMDeploy
73
+ - Transformer
74
+ - vLLM
75
+ - SGLang
76
+
77
+ Detailed deployment examples for these frameworks are available in the [Model Deployment Guide](./deployment_guide.md).
78
+
chat_template.jinja ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- set image_count = namespace(value=0) %}
2
+ {%- set video_count = namespace(value=0) %}
3
+ {%- macro render_content(content, do_vision_count, is_system_content=false) %}
4
+ {%- if content is string %}
5
+ {{- content }}
6
+ {%- elif content is iterable and content is not mapping %}
7
+ {%- for item in content %}
8
+ {%- if 'image' in item or 'image_url' in item or item.type == 'image' %}
9
+ {%- if is_system_content %}
10
+ {{- raise_exception('System message cannot contain images.') }}
11
+ {%- endif %}
12
+ {%- if do_vision_count %}
13
+ {%- set image_count.value = image_count.value + 1 %}
14
+ {%- endif %}
15
+ {%- if add_vision_id %}
16
+ {{- 'Picture ' ~ image_count.value ~ ': ' }}
17
+ {%- endif %}
18
+ {{- '<|vision_start|><|image_pad|><|vision_end|>' }}
19
+ {%- elif 'video' in item or item.type == 'video' %}
20
+ {%- if is_system_content %}
21
+ {{- raise_exception('System message cannot contain videos.') }}
22
+ {%- endif %}
23
+ {%- if do_vision_count %}
24
+ {%- set video_count.value = video_count.value + 1 %}
25
+ {%- endif %}
26
+ {%- if add_vision_id %}
27
+ {{- 'Video ' ~ video_count.value ~ ': ' }}
28
+ {%- endif %}
29
+ {{- '<|vision_start|><|video_pad|><|vision_end|>' }}
30
+ {%- elif 'time_series' in item or item.type == 'time_series' %}
31
+ {%- if is_system_content %}
32
+ {{- raise_exception('System message cannot contain time series.') }}
33
+ {%- endif %}
34
+ {{- '<|ts|><TS_CONTEXT><|/ts|>' }}
35
+ {%- elif 'text' in item %}
36
+ {{- item.text }}
37
+ {%- else %}
38
+ {{- raise_exception('Unexpected item type in content.') }}
39
+ {%- endif %}
40
+ {%- endfor %}
41
+ {%- elif content is none or content is undefined %}
42
+ {{- '' }}
43
+ {%- else %}
44
+ {{- raise_exception('Unexpected content type.') }}
45
+ {%- endif %}
46
+ {%- endmacro %}
47
+ {%- if not messages %}
48
+ {{- raise_exception('No messages provided.') }}
49
+ {%- endif %}
50
+ {%- if tools and tools is iterable and tools is not mapping %}
51
+ {{- '<|im_start|>system\n' }}
52
+ {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
53
+ {%- for tool in tools %}
54
+ {{- "\n" }}
55
+ {{- tool | tojson }}
56
+ {%- endfor %}
57
+ {{- "\n</tools>" }}
58
+ {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
59
+ {%- if messages[0].role == 'system' %}
60
+ {%- set content = render_content(messages[0].content, false, true)|trim %}
61
+ {%- if content %}
62
+ {{- '\n\n' + content }}
63
+ {%- endif %}
64
+ {%- endif %}
65
+ {{- '<|im_end|>\n' }}
66
+ {%- else %}
67
+ {%- if messages[0].role == 'system' %}
68
+ {%- set content = render_content(messages[0].content, false, true)|trim %}
69
+ {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
70
+ {%- endif %}
71
+ {%- endif %}
72
+ {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
73
+ {%- for message in messages[::-1] %}
74
+ {%- set index = (messages|length - 1) - loop.index0 %}
75
+ {%- if ns.multi_step_tool and message.role == "user" %}
76
+ {%- set content = render_content(message.content, false)|trim %}
77
+ {%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
78
+ {%- set ns.multi_step_tool = false %}
79
+ {%- set ns.last_query_index = index %}
80
+ {%- endif %}
81
+ {%- endif %}
82
+ {%- endfor %}
83
+ {%- if ns.multi_step_tool %}
84
+ {{- raise_exception('No user query found in messages.') }}
85
+ {%- endif %}
86
+ {%- for message in messages %}
87
+ {%- set content = render_content(message.content, true)|trim %}
88
+ {%- if message.role == "system" %}
89
+ {%- if not loop.first %}
90
+ {{- raise_exception('System message must be at the beginning.') }}
91
+ {%- endif %}
92
+ {%- elif message.role == "user" %}
93
+ {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
94
+ {%- elif message.role == "assistant" %}
95
+ {%- set reasoning_content = '' %}
96
+ {%- if message.reasoning_content is string %}
97
+ {%- set reasoning_content = message.reasoning_content %}
98
+ {%- else %}
99
+ {%- if '</think>' in content %}
100
+ {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
101
+ {%- set content = content.split('</think>')[-1].lstrip('\n') %}
102
+ {%- endif %}
103
+ {%- endif %}
104
+ {%- set reasoning_content = reasoning_content|trim %}
105
+ {%- if loop.index0 > ns.last_query_index %}
106
+ {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
107
+ {%- else %}
108
+ {{- '<|im_start|>' + message.role + '\n' + content }}
109
+ {%- endif %}
110
+ {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
111
+ {%- for tool_call in message.tool_calls %}
112
+ {%- if tool_call.function is defined %}
113
+ {%- set tool_call = tool_call.function %}
114
+ {%- endif %}
115
+ {%- if loop.first %}
116
+ {%- if content|trim %}
117
+ {{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
118
+ {%- else %}
119
+ {{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}
120
+ {%- endif %}
121
+ {%- else %}
122
+ {{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
123
+ {%- endif %}
124
+ {%- if tool_call.arguments is defined %}
125
+ {%- for args_name, args_value in tool_call.arguments|items %}
126
+ {{- '<parameter=' + args_name + '>\n' }}
127
+ {%- set args_value = args_value | tojson | safe if args_value is mapping or (args_value is sequence and args_value is not string) else args_value | string %}
128
+ {{- args_value }}
129
+ {{- '\n</parameter>\n' }}
130
+ {%- endfor %}
131
+ {%- endif %}
132
+ {{- '</function>\n</tool_call>' }}
133
+ {%- endfor %}
134
+ {%- endif %}
135
+ {{- '<|im_end|>\n' }}
136
+ {%- elif message.role == "tool" %}
137
+ {%- if loop.previtem and loop.previtem.role != "tool" %}
138
+ {{- '<|im_start|>user' }}
139
+ {%- endif %}
140
+ {{- '\n<tool_response>\n' }}
141
+ {{- content }}
142
+ {{- '\n</tool_response>' }}
143
+ {%- if not loop.last and loop.nextitem.role != "tool" %}
144
+ {{- '<|im_end|>\n' }}
145
+ {%- elif loop.last %}
146
+ {{- '<|im_end|>\n' }}
147
+ {%- endif %}
148
+ {%- else %}
149
+ {{- raise_exception('Unexpected message role.') }}
150
+ {%- endif %}
151
+ {%- endfor %}
152
+ {%- if add_generation_prompt %}
153
+ {{- '<|im_start|>assistant\n' }}
154
+ {%- if enable_thinking is defined and enable_thinking is false %}
155
+ {{- '<think>\n\n</think>\n\n' }}
156
+ {%- else %}
157
+ {{- '<think>\n' }}
158
+ {%- endif %}
159
+ {%- endif %}
config.json ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_type": "interns2_mobius",
3
+ "architectures": [
4
+ "InternS2MobiusForConditionalGeneration"
5
+ ],
6
+ "transformers_version": "5.2.0",
7
+ "auto_map": {
8
+ "AutoConfig": "configuration_interns2_mobius.InternS2MobiusConfig",
9
+ "AutoModelForCausalLM": "modeling_interns2_mobius.InternS2MobiusForCausalLM",
10
+ "AutoModel": "modeling_interns2_mobius.InternS2MobiusModel",
11
+ "AutoModelForImageTextToText": "modeling_interns2_mobius.InternS2MobiusForConditionalGeneration",
12
+ "AutoModelForMultimodalLM": "modeling_interns2_mobius.InternS2MobiusForConditionalGeneration"
13
+ },
14
+ "image_token_id": 248056,
15
+ "text_config": {
16
+ "model_type": "interns2_mobius_text",
17
+ "attention_bias": false,
18
+ "attention_dropout": 0.0,
19
+ "attn_output_gate": true,
20
+ "dtype": "bfloat16",
21
+ "eos_token_id": 248044,
22
+ "full_attention_interval": 4,
23
+ "head_dim": 256,
24
+ "hidden_act": "silu",
25
+ "hidden_size": 2048,
26
+ "initializer_range": 0.02,
27
+ "layer_types": [
28
+ "linear_attention",
29
+ "linear_attention",
30
+ "linear_attention",
31
+ "full_attention",
32
+ "linear_attention",
33
+ "linear_attention",
34
+ "linear_attention",
35
+ "full_attention",
36
+ "linear_attention",
37
+ "linear_attention",
38
+ "linear_attention",
39
+ "full_attention",
40
+ "linear_attention",
41
+ "linear_attention",
42
+ "linear_attention",
43
+ "full_attention",
44
+ "linear_attention",
45
+ "linear_attention",
46
+ "linear_attention",
47
+ "full_attention",
48
+ "linear_attention",
49
+ "linear_attention",
50
+ "linear_attention",
51
+ "full_attention",
52
+ "linear_attention",
53
+ "linear_attention",
54
+ "linear_attention",
55
+ "full_attention",
56
+ "linear_attention",
57
+ "linear_attention",
58
+ "linear_attention",
59
+ "full_attention",
60
+ "linear_attention",
61
+ "linear_attention",
62
+ "linear_attention",
63
+ "full_attention",
64
+ "linear_attention",
65
+ "linear_attention",
66
+ "linear_attention",
67
+ "full_attention"
68
+ ],
69
+ "linear_conv_kernel_dim": 4,
70
+ "linear_key_head_dim": 128,
71
+ "linear_num_key_heads": 16,
72
+ "linear_num_value_heads": 32,
73
+ "linear_value_head_dim": 128,
74
+ "max_position_embeddings": 262144,
75
+ "mlp_only_layers": [],
76
+ "moe_intermediate_size": 512,
77
+ "mtp_num_hidden_layers": 1,
78
+ "mtp_use_dedicated_embeddings": false,
79
+ "mtp_num_experts": 256,
80
+ "mtp_num_experts_per_tok": 8,
81
+ "num_attention_heads": 16,
82
+ "num_blocks": 4,
83
+ "num_experts": 2560,
84
+ "num_experts_per_tok": 8,
85
+ "num_hidden_layers": 40,
86
+ "num_key_value_heads": 2,
87
+ "rms_norm_eps": 1e-06,
88
+ "router_aux_loss_coef": 0.001,
89
+ "shared_expert_intermediate_size": 512,
90
+ "use_cache": true,
91
+ "vocab_size": 251392,
92
+ "mamba_ssm_dtype": "float32",
93
+ "rope_parameters": {
94
+ "mrope_interleaved": true,
95
+ "mrope_section": [
96
+ 11,
97
+ 11,
98
+ 10
99
+ ],
100
+ "rope_type": "default",
101
+ "rope_theta": 10000000,
102
+ "partial_rotary_factor": 0.25
103
+ },
104
+ "pad_token_id": null,
105
+ "bos_token_id": null,
106
+ "tie_word_embeddings": false,
107
+ "output_router_logits": false,
108
+ "partial_rotary_factor": 0.25
109
+ },
110
+ "tie_word_embeddings": false,
111
+ "video_token_id": 248057,
112
+ "vision_config": {
113
+ "model_type": "interns2_mobius",
114
+ "deepstack_visual_indexes": [],
115
+ "depth": 27,
116
+ "hidden_act": "gelu_pytorch_tanh",
117
+ "hidden_size": 1152,
118
+ "in_channels": 3,
119
+ "initializer_range": 0.02,
120
+ "intermediate_size": 4304,
121
+ "num_heads": 16,
122
+ "num_position_embeddings": 2304,
123
+ "out_hidden_size": 2048,
124
+ "patch_size": 16,
125
+ "spatial_merge_size": 2,
126
+ "temporal_patch_size": 2
127
+ },
128
+ "vision_end_token_id": 248054,
129
+ "vision_start_token_id": 248053
130
+ }
configuration_interns2_mobius.py ADDED
@@ -0,0 +1,342 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2025 InternS2Mobius Team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ from transformers.configuration_utils import PreTrainedConfig, layer_type_validation
15
+ from transformers.modeling_rope_utils import RopeParameters
16
+
17
+
18
+ class InternS2MobiusVisionConfig(PreTrainedConfig):
19
+ model_type = "interns2_mobius"
20
+ base_config_key = "vision_config"
21
+
22
+ def __init__(
23
+ self,
24
+ depth=27,
25
+ hidden_size=1152,
26
+ hidden_act="gelu_pytorch_tanh",
27
+ intermediate_size=4304,
28
+ num_heads=16,
29
+ in_channels=3,
30
+ patch_size=16,
31
+ spatial_merge_size=2,
32
+ temporal_patch_size=2,
33
+ out_hidden_size=3584,
34
+ num_position_embeddings=2304,
35
+ initializer_range=0.02,
36
+ deepstack_visual_indexes=None,
37
+ **kwargs,
38
+ ):
39
+ super().__init__(**kwargs)
40
+
41
+ self.depth = depth
42
+ self.hidden_size = hidden_size
43
+ self.hidden_act = hidden_act
44
+ self.intermediate_size = intermediate_size
45
+ self.num_heads = num_heads
46
+ self.in_channels = in_channels
47
+ self.patch_size = patch_size
48
+ self.spatial_merge_size = spatial_merge_size
49
+ self.temporal_patch_size = temporal_patch_size
50
+ self.out_hidden_size = out_hidden_size
51
+ self.num_position_embeddings = num_position_embeddings
52
+ self.initializer_range = initializer_range
53
+ self.deepstack_visual_indexes = deepstack_visual_indexes if deepstack_visual_indexes is not None else []
54
+
55
+
56
+ class InternS2MobiusTextConfig(PreTrainedConfig):
57
+ r"""
58
+ This is the configuration class to store the configuration of a [`InternS2MobiusTextModel`]. It is used to instantiate a
59
+ InternS2Mobius model according to the specified arguments, defining the model architecture.
60
+ Instantiating a configuration with the defaults will yield a similar configuration to that of
61
+ InternS2Mobius.
62
+
63
+ Configuration objects inherit from [`PreTrainedConfig`] and can be used to control the model outputs. Read the
64
+ documentation from [`PreTrainedConfig`] for more information.
65
+
66
+
67
+ Args:
68
+ vocab_size (`int`, *optional*, defaults to 248320):
69
+ Vocabulary size of the model. Defines the number of different tokens that can be represented by the
70
+ `inputs_ids`.
71
+ hidden_size (`int`, *optional*, defaults to 2048):
72
+ Dimension of the hidden representations.
73
+ num_hidden_layers (`int`, *optional*, defaults to 40):
74
+ Number of hidden layers in the Transformer encoder.
75
+ num_attention_heads (`int`, *optional*, defaults to 16):
76
+ Number of attention heads for each attention layer in the Transformer encoder.
77
+ num_key_value_heads (`int`, *optional*, defaults to 2):
78
+ This is the number of key_value heads that should be used to implement Grouped Query Attention. If
79
+ `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
80
+ `num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When
81
+ converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
82
+ by meanpooling all the original heads within that group. For more details checkout [this
83
+ paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to `32`.
84
+ hidden_act (`str`, *optional*, defaults to `"silu"`):
85
+ The non-linear activation function in the decoder.
86
+ max_position_embeddings (`int`, *optional*, defaults to 32768):
87
+ The maximum sequence length that this model might ever be used with.
88
+ initializer_range (`float`, *optional*, defaults to 0.02):
89
+ The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
90
+ rms_norm_eps (`float`, *optional*, defaults to 1e-06):
91
+ The epsilon used by the rms normalization layers.
92
+ use_cache (`bool`, *optional*, defaults to `True`):
93
+ Whether or not the model should return the last key/values attentions (not used by all models). Only
94
+ relevant if `config.is_decoder=True`.
95
+ tie_word_embeddings (`bool`, *optional*, defaults to `False`):
96
+ Whether the model's input and output word embeddings should be tied.
97
+ rope_parameters (`RopeParameters`, *optional*):
98
+ Dictionary containing the configuration parameters for the RoPE embeddings. The dictionary should contain
99
+ a value for `rope_theta` and optionally parameters used for scaling in case you want to use RoPE
100
+ with longer `max_position_embeddings`.
101
+ attention_bias (`bool`, *optional*, defaults to `False`):
102
+ Whether to use a bias in the query, key, value and output projection layers during self-attention.
103
+ attention_dropout (`float`, *optional*, defaults to 0.0):
104
+ The dropout ratio for the attention probabilities.
105
+ head_dim (`int`, *optional*, defaults to 256):
106
+ Projection weights dimension in multi-head attention.
107
+ linear_conv_kernel_dim (`int`, *optional*, defaults to 4):
108
+ Kernel size of the convolution used in linear attention layers.
109
+ linear_key_head_dim (`int`, *optional*, defaults to 128):
110
+ Dimension of each key head in linear attention.
111
+ linear_value_head_dim (`int`, *optional*, defaults to 128):
112
+ Dimension of each value head in linear attention.
113
+ linear_num_key_heads (`int`, *optional*, defaults to 16):
114
+ Number of key heads used in linear attention layers.
115
+ linear_num_value_heads (`int`, *optional*, defaults to 32):
116
+ Number of value heads used in linear attention layers.
117
+ moe_intermediate_size (`int`, *optional*, defaults to 512):
118
+ Intermediate size of the routed expert.
119
+ shared_expert_intermediate_size (`int`, *optional*, defaults to 512):
120
+ Intermediate size of the shared expert.
121
+ num_experts_per_tok (`int`, *optional*, defaults to 8):
122
+ Number of selected experts.
123
+ num_experts (`int`, *optional*, defaults to 256):
124
+ Number of routed experts.
125
+ output_router_logits (`bool`, *optional*, defaults to `False`):
126
+ Whether or not the router logits should be returned by the model. Enabling this will also
127
+ allow the model to output the auxiliary loss, including load balancing loss and router z-loss.
128
+ router_aux_loss_coef (`float`, *optional*, defaults to 0.001):
129
+ The aux loss factor for the total loss.
130
+ layer_types (`list[str]`, *optional*):
131
+ Types of each layer (attention or linear).
132
+ pad_token_id (`int`, *optional*):
133
+ Padding token id.
134
+ bos_token_id (`int`, *optional*):
135
+ Beginning of stream token id.
136
+ eos_token_id (`int`, *optional*):
137
+ End of stream token id.
138
+
139
+ ```python
140
+ >>> from transformers import InternS2MobiusTextModel, InternS2MobiusTextConfig
141
+
142
+ >>> # Initializing a InternS2Mobius style configuration
143
+ >>> configuration = InternS2MobiusTextConfig()
144
+
145
+ >>> # Initializing a model from the InternS2Mobius style configuration
146
+ >>> model = InternS2MobiusTextModel(configuration)
147
+
148
+ >>> # Accessing the model configuration
149
+ >>> configuration = model.config
150
+ ```
151
+ """
152
+
153
+ # NOTE: `model_type` is kept as `interns2_mobius_text` to match config.json.
154
+ model_type = "interns2_mobius_text"
155
+ keys_to_ignore_at_inference = ["past_key_values"]
156
+
157
+ base_model_tp_plan = {
158
+ "layers.*.self_attn.q_proj": "colwise",
159
+ "layers.*.self_attn.k_proj": "colwise",
160
+ "layers.*.self_attn.v_proj": "colwise",
161
+ "layers.*.self_attn.o_proj": "rowwise",
162
+ "meta_mlp.*.experts.gate_up_proj": "packed_colwise",
163
+ "meta_mlp.*.experts.down_proj": "rowwise",
164
+ "layers.*.mlp.shared_expert.gate_proj": "colwise",
165
+ "layers.*.mlp.shared_expert.up_proj": "colwise",
166
+ "layers.*.mlp.shared_expert.down_proj": "rowwise",
167
+ }
168
+ base_model_pp_plan = {
169
+ "embed_tokens": (["input_ids"], ["inputs_embeds"]),
170
+ "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
171
+ "norm": (["hidden_states"], ["hidden_states"]),
172
+ }
173
+ base_config_key = "text_config"
174
+
175
+ def __init__(
176
+ self,
177
+ vocab_size=248320,
178
+ hidden_size=2048,
179
+ num_hidden_layers=40,
180
+ num_attention_heads=16,
181
+ num_key_value_heads=2,
182
+ hidden_act="silu",
183
+ max_position_embeddings=32768,
184
+ initializer_range=0.02,
185
+ rms_norm_eps=1e-6,
186
+ use_cache=True,
187
+ tie_word_embeddings=False,
188
+ rope_parameters: RopeParameters | dict[str, RopeParameters] | None = None,
189
+ attention_bias=False,
190
+ attention_dropout=0.0,
191
+ head_dim=256,
192
+ linear_conv_kernel_dim=4,
193
+ linear_key_head_dim=128,
194
+ linear_value_head_dim=128,
195
+ linear_num_key_heads=16,
196
+ linear_num_value_heads=32,
197
+ moe_intermediate_size=512,
198
+ shared_expert_intermediate_size=512,
199
+ num_experts_per_tok=8,
200
+ num_experts=256,
201
+ output_router_logits=False,
202
+ router_aux_loss_coef=0.001,
203
+ num_blocks=4,
204
+ dtype=None,
205
+ layer_types=None,
206
+ pad_token_id: int | None = None,
207
+ bos_token_id: int | None = None,
208
+ eos_token_id: int | None = None,
209
+ **kwargs,
210
+ ):
211
+ kwargs["ignore_keys_at_rope_validation"] = {"mrope_section", "mrope_interleaved"}
212
+ self.num_blocks = num_blocks
213
+ self.dtype = dtype
214
+ self.pad_token_id = pad_token_id
215
+ self.bos_token_id = bos_token_id
216
+ self.eos_token_id = eos_token_id
217
+ self.tie_word_embeddings = tie_word_embeddings
218
+ self.vocab_size = vocab_size
219
+ self.max_position_embeddings = max_position_embeddings
220
+ self.hidden_size = hidden_size
221
+ self.num_hidden_layers = num_hidden_layers
222
+ self.num_attention_heads = num_attention_heads
223
+ self.num_key_value_heads = num_key_value_heads
224
+ self.hidden_act = hidden_act
225
+ self.initializer_range = initializer_range
226
+ self.rms_norm_eps = rms_norm_eps
227
+ self.use_cache = use_cache
228
+ self.attention_bias = attention_bias
229
+ self.attention_dropout = attention_dropout
230
+ self.head_dim = head_dim
231
+ self.rope_parameters = rope_parameters
232
+ kwargs.setdefault("partial_rotary_factor", 0.25) # assign default for BC
233
+
234
+ self.layer_types = layer_types
235
+ if self.layer_types is None:
236
+ interval_pattern = kwargs.get("full_attention_interval", 4)
237
+ self.layer_types = [
238
+ "linear_attention" if bool((i + 1) % interval_pattern) else "full_attention"
239
+ for i in range(self.num_hidden_layers)
240
+ ]
241
+ layer_type_validation(self.layer_types, self.num_hidden_layers)
242
+
243
+ # linear attention part
244
+ self.linear_conv_kernel_dim = linear_conv_kernel_dim
245
+ self.linear_key_head_dim = linear_key_head_dim
246
+ self.linear_value_head_dim = linear_value_head_dim
247
+ self.linear_num_key_heads = linear_num_key_heads
248
+ self.linear_num_value_heads = linear_num_value_heads
249
+ self.moe_intermediate_size = moe_intermediate_size
250
+ self.shared_expert_intermediate_size = shared_expert_intermediate_size
251
+ self.num_experts_per_tok = num_experts_per_tok
252
+ self.num_experts = num_experts
253
+ self.output_router_logits = output_router_logits
254
+ self.router_aux_loss_coef = router_aux_loss_coef
255
+ super().__init__(**kwargs)
256
+
257
+
258
+ class InternS2MobiusConfig(PreTrainedConfig):
259
+ r"""
260
+ This is the configuration class to store the configuration of a [`InternS2MobiusModel`]. It is used to instantiate a
261
+ InternS2Mobius model according to the specified arguments, defining the model architecture. Instantiating a configuration
262
+ with the defaults will yield a similar configuration to that of
263
+ InternS2Mobius.
264
+
265
+ Configuration objects inherit from [`PreTrainedConfig`] and can be used to control the model outputs. Read the
266
+ documentation from [`PreTrainedConfig`] for more information.
267
+
268
+
269
+ Args:
270
+ text_config (`Union[PreTrainedConfig, dict]`, *optional*, defaults to `InternS2MobiusTextConfig`):
271
+ The config object or dictionary of the text backbone.
272
+ vision_config (`Union[PreTrainedConfig, dict]`, *optional*, defaults to `InternS2MobiusVisionConfig`):
273
+ The config object or dictionary of the vision backbone.
274
+ image_token_id (`int`, *optional*, defaults to 248056):
275
+ The image token index to encode the image prompt.
276
+ video_token_id (`int`, *optional*, defaults to 248057):
277
+ The video token index to encode the image prompt.
278
+ vision_start_token_id (`int`, *optional*, defaults to 248053):
279
+ The start token index to encode the image prompt.
280
+ vision_end_token_id (`int`, *optional*, defaults to 248054):
281
+ The end token index to encode the image prompt.
282
+ tie_word_embeddings (`bool`, *optional*, defaults to `False`):
283
+ Whether to tie the word embeddings.
284
+
285
+ ```python
286
+ >>> from transformers import InternS2MobiusForConditionalGeneration, InternS2MobiusConfig
287
+
288
+ >>> # Initializing a InternS2Mobius style configuration
289
+ >>> configuration = InternS2MobiusConfig()
290
+
291
+ >>> # Initializing a model from the InternS2Mobius style configuration
292
+ >>> model = InternS2MobiusForConditionalGeneration(configuration)
293
+
294
+ >>> # Accessing the model configuration
295
+ >>> configuration = model.config
296
+ ```"""
297
+
298
+ model_type = "interns2_mobius"
299
+ sub_configs = {
300
+ "vision_config": InternS2MobiusVisionConfig,
301
+ "text_config": InternS2MobiusTextConfig,
302
+ }
303
+ keys_to_ignore_at_inference = ["past_key_values"]
304
+
305
+ def __init__(
306
+ self,
307
+ text_config=None,
308
+ vision_config=None,
309
+ image_token_id=248056,
310
+ video_token_id=248057,
311
+ vision_start_token_id=248053,
312
+ vision_end_token_id=248054,
313
+ tie_word_embeddings=False,
314
+ **kwargs,
315
+ ):
316
+ if isinstance(vision_config, dict):
317
+ self.vision_config = self.sub_configs["vision_config"](**vision_config)
318
+ elif vision_config is None:
319
+ self.vision_config = self.sub_configs["vision_config"]()
320
+
321
+ if isinstance(text_config, dict):
322
+ self.text_config = self.sub_configs["text_config"](**text_config)
323
+ elif text_config is None:
324
+ self.text_config = self.sub_configs["text_config"]()
325
+
326
+ self.image_token_id = image_token_id
327
+ self.video_token_id = video_token_id
328
+ self.vision_start_token_id = vision_start_token_id
329
+ self.vision_end_token_id = vision_end_token_id
330
+ self.tie_word_embeddings = tie_word_embeddings
331
+ super().__init__(**kwargs)
332
+ self.auto_map = {
333
+ "AutoConfig": "configuration_interns2_mobius.InternS2MobiusConfig",
334
+ "AutoModelForCausalLM": "modeling_interns2_mobius.InternS2MobiusForCausalLM",
335
+ "AutoModel": "modeling_interns2_mobius.InternS2MobiusModel",
336
+ "AutoModelForImageTextToText": "modeling_interns2_mobius.InternS2MobiusForConditionalGeneration",
337
+ "AutoModelForMultimodalLM": "modeling_interns2_mobius.InternS2MobiusForConditionalGeneration",
338
+ }
339
+ self.architectures = ["InternS2MobiusForConditionalGeneration"]
340
+
341
+
342
+ __all__ = ["InternS2MobiusConfig", "InternS2MobiusTextConfig"]
deployment_guide.md ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Intern-S2-Mobius Deployment Guide
2
+
3
+ The Intern-S2-Mobius release is a 35B-A3B model stored in bfloat16 weight format. This guide provides deployment examples for the following configurations:
4
+
5
+ - MTP speculative decoding (Recommended)
6
+ - Basic serving without MTP
7
+ - Long-context inference with YaRN RoPE configuration
8
+
9
+ > NOTE: The commands below are reference configurations. Inference frameworks are under active development, so use the latest framework documentation and your local validation results when tuning production deployments.
10
+
11
+ ## LMDeploy
12
+
13
+ Use the latest LMDeploy with Intern-S2-Mobius support. The examples below use single-GPU serving.
14
+
15
+ - Serving With MTP (Recommended)
16
+
17
+ ```bash
18
+ lmdeploy serve api_server \
19
+ internlm/Intern-S2-Mobius \
20
+ --trust-remote-code \
21
+ --backend pytorch \
22
+ --tp 1 \
23
+ --speculative-algorithm qwen3_5_mtp \
24
+ --speculative-num-draft-tokens 4 \
25
+ --dtype bfloat16 \
26
+ --max-batch-size 64
27
+ ```
28
+
29
+ - Basic Serving Without MTP
30
+
31
+ ```bash
32
+ lmdeploy serve api_server \
33
+ internlm/Intern-S2-Mobius \
34
+ --trust-remote-code \
35
+ --backend pytorch \
36
+ --dtype bfloat16 \
37
+ --tp 1
38
+ ```
39
+
40
+
41
+ ## Transformers
42
+
43
+ Use a recent Transformers version with remote-code loading enabled.
44
+
45
+ - Installation
46
+
47
+ ```bash
48
+ pip install -U transformers accelerate safetensors
49
+ ```
50
+
51
+ - Basic Inference
52
+
53
+ ```python
54
+ import torch
55
+ from transformers import AutoModelForImageTextToText, AutoTokenizer
56
+
57
+ model_path = "internlm/Intern-S2-Mobius"
58
+
59
+ tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
60
+ model = AutoModelForImageTextToText.from_pretrained(
61
+ model_path,
62
+ trust_remote_code=True,
63
+ torch_dtype=torch.bfloat16,
64
+ device_map="auto",
65
+ ).eval()
66
+
67
+ messages = [
68
+ {"role": "user", "content": "Give me a short introduction to Intern-S2-Mobius."}
69
+ ]
70
+
71
+ text = tokenizer.apply_chat_template(
72
+ messages,
73
+ tokenize=False,
74
+ add_generation_prompt=True,
75
+ )
76
+ inputs = tokenizer(text, return_tensors="pt").to(model.device)
77
+
78
+ with torch.no_grad():
79
+ output_ids = model.generate(
80
+ **inputs,
81
+ max_new_tokens=512,
82
+ do_sample=True,
83
+ temperature=0.8,
84
+ top_p=0.95,
85
+ )
86
+
87
+ response_ids = output_ids[0][inputs["input_ids"].shape[-1]:]
88
+ print(tokenizer.decode(response_ids, skip_special_tokens=True))
89
+ ```
90
+
91
+ ## vLLM
92
+
93
+ Use the latest vLLM Docker image or source build with Intern-S2-Preview support.
94
+
95
+ For example, you can use the vLLM nightly Docker image `docker pull vllm/vllm-openai:cu129-nightly-a6682d1d259cca69a9ae737ea5608fbbe7520031`
96
+
97
+ - Serving With MTP (Recommended)
98
+
99
+ ```bash
100
+ vllm serve internlm/Intern-S2-Preview \
101
+ --trust-remote-code \
102
+ --tensor-parallel-size 2 \
103
+ --reasoning-parser qwen3 \
104
+ --enable-auto-tool-choice \
105
+ --tool-call-parser qwen3_coder \
106
+ --speculative-config '{"method":"mtp","num_speculative_tokens":4}'
107
+ ```
108
+
109
+ - Basic Serving Without MTP
110
+
111
+ ```bash
112
+ vllm serve internlm/Intern-S2-Preview \
113
+ --trust-remote-code \
114
+ --tensor-parallel-size 2 \
115
+ --reasoning-parser qwen3 \
116
+ --enable-auto-tool-choice \
117
+ --tool-call-parser qwen3_coder
118
+ ```
119
+
120
+ ## SGLang
121
+
122
+ Use the latest SGLang Docker image or source build with Intern-S2-Preview support.
123
+
124
+ For example, you can use the SGLang nightly Docker image `docker pull lmsysorg/sglang:nightly-dev-cu12-20260520-425dffbd`
125
+
126
+ - Serving With MTP (Recommended)
127
+
128
+ ```bash
129
+ SGLANG_ENABLE_SPEC_V2=1 \
130
+ python3 -m sglang.launch_server \
131
+ --model-path internLM/Intern-S2-Preview \
132
+ --trust-remote-code \
133
+ --tp-size 2 \
134
+ --reasoning-parser qwen3 \
135
+ --tool-call-parser qwen3_coder \
136
+ --mamba-scheduler-strategy extra_buffer \
137
+ --speculative-algo 'NEXTN' \
138
+ --speculative-eagle-topk 1 \
139
+ --speculative-num-steps 3 \
140
+ --speculative-num-draft-tokens 4
141
+ ```
142
+
143
+ - Basic Serving Without MTP
144
+
145
+ ```bash
146
+ python3 -m sglang.launch_server \
147
+ --model-path internlm/Intern-S2-Preview \
148
+ --trust-remote-code \
149
+ --tp-size 2 \
150
+ --reasoning-parser qwen3 \
151
+ --tool-call-parser qwen3_coder
152
+ ```
figs/case-study.png ADDED

Git LFS Details

  • SHA256: e265f57c09f198e7b2c7543e0de456ecb1ab3ba5a736829b7dcfed6b3c84e5ca
  • Pointer size: 131 Bytes
  • Size of remote file: 208 kB
figs/chain-of-thought.png ADDED
figs/efficiency.png ADDED
figs/performance.png ADDED
figs/title.png ADDED

Git LFS Details

  • SHA256: 1e0080637b1009715c78ad8fb9b00f2355282b79e9e332100b8943f1a17eb33c
  • Pointer size: 132 Bytes
  • Size of remote file: 1.3 MB
generation_config.json ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token_id": 248044,
3
+ "do_sample": true,
4
+ "eos_token_id": [
5
+ 248046,
6
+ 248044
7
+ ],
8
+ "pad_token_id": 248044,
9
+ "temperature": 1.0,
10
+ "top_k": 20,
11
+ "top_p": 0.95,
12
+ "transformers_version": "4.57.0.dev0"
13
+ }
merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
model-00001-of-00005.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cf10fc827c1099ef541e26f811af5499b560f55d7f691697c078a672423c3723
3
+ size 8492270416
model-00002-of-00005.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a193cf4f28e95752b1cda6d26303e067c0e64ad389b2e302c5b3b9819352ab60
3
+ size 16116613520
model-00003-of-00005.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:659d71fdc93902561b5b294c09801e078be1afba2508c50cbb1753011aee93b7
3
+ size 16116613520
model-00004-of-00005.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:675ad48ee9a35a92c7d55d12a73c70e8f5b2d141c56e29529df165e62fa524ed
3
+ size 16116613520
model-00005-of-00005.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7e791a06933ee438a86f5d8e331cdcfc2905a5a2af21f9cb6b2d76125832f49e
3
+ size 16116613520
model.safetensors.index.json ADDED
The diff for this file is too large to render. See raw diff
 
modeling_interns2_mobius.py ADDED
The diff for this file is too large to render. See raw diff
 
preprocessor_config.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "size": {
3
+ "longest_edge": 16777216,
4
+ "shortest_edge": 65536
5
+ },
6
+ "patch_size": 16,
7
+ "temporal_patch_size": 2,
8
+ "merge_size": 2,
9
+ "image_mean": [
10
+ 0.5,
11
+ 0.5,
12
+ 0.5
13
+ ],
14
+ "image_std": [
15
+ 0.5,
16
+ 0.5,
17
+ 0.5
18
+ ],
19
+ "processor_class": "Qwen3VLProcessor",
20
+ "image_processor_type": "Qwen2VLImageProcessorFast",
21
+ "auto_map": {
22
+ "AutoProcessor": "processing_interns2_mobius.InternS2MobiusProcessor"
23
+ }
24
+ }
processing_interns2_mobius.py ADDED
@@ -0,0 +1,265 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2025 InternS2Mobius Team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ import numpy as np
15
+
16
+ from transformers.feature_extraction_utils import BatchFeature
17
+ from transformers.image_utils import ImageInput
18
+ from transformers.processing_utils import MultiModalData, ProcessingKwargs, ProcessorMixin, Unpack
19
+ from transformers.tokenization_utils_base import PreTokenizedInput, TextInput
20
+ from transformers.utils import auto_docstring, logging
21
+ from transformers.video_utils import VideoInput
22
+
23
+
24
+ logger = logging.get_logger(__name__)
25
+
26
+
27
+ class InternS2MobiusProcessorKwargs(ProcessingKwargs, total=False):
28
+ _defaults = {
29
+ "text_kwargs": {
30
+ "padding": False,
31
+ "return_token_type_ids": False,
32
+ "return_mm_token_type_ids": False,
33
+ },
34
+ "videos_kwargs": {"return_metadata": True},
35
+ }
36
+
37
+
38
+ @auto_docstring
39
+ class InternS2MobiusProcessor(ProcessorMixin):
40
+ def __init__(self, image_processor=None, tokenizer=None, video_processor=None, chat_template=None, **kwargs):
41
+ self.image_token = "<|image_pad|>" if not hasattr(tokenizer, "image_token") else tokenizer.image_token
42
+ self.video_token = "<|video_pad|>" if not hasattr(tokenizer, "video_token") else tokenizer.video_token
43
+ self.image_token_id = (
44
+ tokenizer.image_token_id
45
+ if getattr(tokenizer, "image_token_id", None)
46
+ else tokenizer.convert_tokens_to_ids(self.image_token)
47
+ )
48
+ self.video_token_id = (
49
+ tokenizer.video_token_id
50
+ if getattr(tokenizer, "video_token_id", None)
51
+ else tokenizer.convert_tokens_to_ids(self.video_token)
52
+ )
53
+ super().__init__(image_processor, tokenizer, video_processor, chat_template=chat_template)
54
+ self.vision_start_token = (
55
+ "<|vision_start|>" if not hasattr(tokenizer, "vision_start_token") else tokenizer.vision_start_token
56
+ )
57
+ self.vision_end_token = (
58
+ "<|vision_end|>" if not hasattr(tokenizer, "vision_end_token") else tokenizer.vision_end_token
59
+ )
60
+ self.vision_start_token_id = (
61
+ tokenizer.vision_start_token_id
62
+ if getattr(tokenizer, "vision_start_token_id", None)
63
+ else tokenizer.convert_tokens_to_ids(self.vision_start_token)
64
+ )
65
+ self.vision_end_token_id = (
66
+ tokenizer.vision_end_token_id
67
+ if getattr(tokenizer, "vision_end_token_id", None)
68
+ else tokenizer.convert_tokens_to_ids(self.vision_end_token)
69
+ )
70
+
71
+ @auto_docstring
72
+ def __call__(
73
+ self,
74
+ images: ImageInput = None,
75
+ text: TextInput | PreTokenizedInput | list[TextInput] | list[PreTokenizedInput] = None,
76
+ videos: VideoInput = None,
77
+ **kwargs: Unpack[InternS2MobiusProcessorKwargs],
78
+ ) -> BatchFeature:
79
+ r"""
80
+ Returns:
81
+ [`BatchFeature`]: A [`BatchFeature`] with the following fields:
82
+
83
+ - **input_ids** -- List of token ids to be fed to a model. Returned when `text` is not `None`.
84
+ - **attention_mask** -- List of indices specifying which tokens should be attended to by the model (when
85
+ `return_attention_mask=True` or if *"attention_mask"* is in `self.model_input_names` and if `text` is not
86
+ `None`).
87
+ - **pixel_values** -- Pixel values to be fed to a model. Returned when `images` is not `None`.
88
+ - **pixel_values_videos** -- Pixel values of videos to be fed to a model. Returned when `videos` is not `None`.
89
+ - **image_grid_thw** -- List of image 3D grid in LLM. Returned when `images` is not `None`.
90
+ - **video_grid_thw** -- List of video 3D grid in LLM. Returned when `videos` is not `None`.
91
+ """
92
+ output_kwargs = self._merge_kwargs(
93
+ InternS2MobiusProcessorKwargs,
94
+ tokenizer_init_kwargs=self.tokenizer.init_kwargs,
95
+ **kwargs,
96
+ )
97
+ if images is not None:
98
+ image_inputs = self.image_processor(images=images, **output_kwargs["images_kwargs"])
99
+ image_grid_thw = image_inputs["image_grid_thw"]
100
+ else:
101
+ image_inputs = {}
102
+ image_grid_thw = None
103
+
104
+ if videos is not None:
105
+ videos_inputs = self.video_processor(videos=videos, **output_kwargs["videos_kwargs"])
106
+ video_grid_thw = videos_inputs["video_grid_thw"]
107
+ # If user has not requested video metadata, pop it
108
+ if not kwargs.get("return_metadata"):
109
+ video_metadata = videos_inputs.pop("video_metadata")
110
+ else:
111
+ video_metadata = videos_inputs["video_metadata"]
112
+ else:
113
+ videos_inputs = {}
114
+ video_grid_thw = None
115
+
116
+ if not isinstance(text, list):
117
+ text = [text]
118
+
119
+ text = text.copy() # below lines change text in-place
120
+
121
+ if image_grid_thw is not None:
122
+ merge_length = self.image_processor.merge_size**2
123
+ index = 0
124
+ for i in range(len(text)):
125
+ while self.image_token in text[i]:
126
+ num_image_tokens = image_grid_thw[index].prod() // merge_length
127
+ text[i] = text[i].replace(self.image_token, "<|placeholder|>" * num_image_tokens, 1)
128
+ index += 1
129
+ text[i] = text[i].replace("<|placeholder|>", self.image_token)
130
+
131
+ if video_grid_thw is not None:
132
+ merge_length = self.video_processor.merge_size**2
133
+ index = 0
134
+ for i in range(len(text)):
135
+ while self.video_token in text[i]:
136
+ metadata = video_metadata[index]
137
+ if metadata.fps is None:
138
+ logger.warning_once(
139
+ "Qwen3VL requires frame timestamps to construct prompts, but the `fps` of the input video could not be inferred. "
140
+ "Probably `video_metadata` was missing from inputs and you passed pre-sampled frames. "
141
+ "Defaulting to `fps=24`. Please provide `video_metadata` for more accurate results."
142
+ )
143
+ metadata.fps = 24 if metadata.fps is None else metadata.fps
144
+
145
+ # if timestamps are not provided, calculate them
146
+ curr_timestamp = self._calculate_timestamps(
147
+ metadata.frames_indices,
148
+ metadata.fps,
149
+ self.video_processor.temporal_patch_size,
150
+ )
151
+
152
+ video_placeholder = ""
153
+ frame_seqlen = video_grid_thw[index][1:].prod() // merge_length
154
+ for frame_idx in range(video_grid_thw[index][0]):
155
+ curr_time = curr_timestamp[frame_idx]
156
+ video_placeholder += f"<{curr_time:.1f} seconds>"
157
+ video_placeholder += (
158
+ self.vision_start_token + "<|placeholder|>" * frame_seqlen + self.vision_end_token
159
+ )
160
+ if f"{self.vision_start_token}{self.video_token}{self.vision_end_token}" in text[i]:
161
+ text[i] = text[i].replace(
162
+ f"{self.vision_start_token}{self.video_token}{self.vision_end_token}", video_placeholder, 1
163
+ )
164
+ else:
165
+ # vllm may input video token directly
166
+ text[i] = text[i].replace(self.video_token, video_placeholder, 1)
167
+ index += 1
168
+
169
+ text[i] = text[i].replace("<|placeholder|>", self.video_token)
170
+
171
+ return_tensors = output_kwargs["text_kwargs"].pop("return_tensors", None)
172
+ return_mm_token_type_ids = output_kwargs["text_kwargs"].pop("return_mm_token_type_ids", None)
173
+ text_inputs = self.tokenizer(text, **output_kwargs["text_kwargs"])
174
+ self._check_special_mm_tokens(text, text_inputs, modalities=["image", "video"])
175
+
176
+ if return_mm_token_type_ids:
177
+ array_ids = np.array(text_inputs["input_ids"])
178
+ mm_token_type_ids = np.zeros_like(text_inputs["input_ids"])
179
+ mm_token_type_ids[array_ids == self.image_token_id] = 1
180
+ text_inputs["mm_token_type_ids"] = mm_token_type_ids.tolist()
181
+
182
+ return BatchFeature(
183
+ data={**text_inputs, **image_inputs, **videos_inputs}, tensor_type=return_tensors
184
+ )
185
+
186
+ def _get_num_multimodal_tokens(self, image_sizes=None, video_sizes=None, **kwargs):
187
+ """
188
+ Computes the number of placeholder tokens needed for multimodal inputs with the given sizes.
189
+ Args:
190
+ image_sizes (`list[list[int]]`, *optional*):
191
+ The input sizes formatted as (height, width) per each image.
192
+ video_sizes (`list[list[int]]`, *optional*):
193
+ The input sizes formatted as (num_frames, height, width) per each video.
194
+ Returns:
195
+ `MultiModalData`: A `MultiModalData` object holding number of tokens per each of the provided
196
+ input modalities, along with other useful data.
197
+ """
198
+
199
+ vision_data = {}
200
+ if image_sizes is not None:
201
+ images_kwargs = InternS2MobiusProcessorKwargs._defaults.get("images_kwargs", {})
202
+ images_kwargs.update(kwargs)
203
+ merge_size = images_kwargs.get("merge_size", None) or self.image_processor.merge_size
204
+
205
+ num_image_patches = [
206
+ self.image_processor.get_number_of_image_patches(*image_size, images_kwargs)
207
+ for image_size in image_sizes
208
+ ]
209
+ num_image_tokens = [(num_patches // merge_size**2) for num_patches in num_image_patches]
210
+ vision_data.update({"num_image_tokens": num_image_tokens, "num_image_patches": num_image_patches})
211
+
212
+ if video_sizes is not None:
213
+ videos_kwargs = InternS2MobiusProcessorKwargs._defaults.get("videos_kwargs", {})
214
+ videos_kwargs.update(kwargs)
215
+ num_video_patches = [
216
+ self.video_processor.get_number_of_video_patches(*video_size, videos_kwargs)
217
+ for video_size in video_sizes
218
+ ]
219
+ num_video_tokens = [(num_patches // merge_size**2) for num_patches in num_video_patches]
220
+ vision_data["num_video_tokens"] = num_video_tokens
221
+
222
+ return MultiModalData(**vision_data)
223
+
224
+ def post_process_image_text_to_text(
225
+ self, generated_outputs, skip_special_tokens=True, clean_up_tokenization_spaces=False, **kwargs
226
+ ):
227
+ """
228
+ Post-process the output of the model to decode the text.
229
+
230
+ Args:
231
+ generated_outputs (`torch.Tensor` or `np.ndarray`):
232
+ The output of the model `generate` function. The output is expected to be a tensor of shape `(batch_size, sequence_length)`
233
+ or `(sequence_length,)`.
234
+ skip_special_tokens (`bool`, *optional*, defaults to `True`):
235
+ Whether or not to remove special tokens in the output. Argument passed to the tokenizer's `batch_decode` method.
236
+ clean_up_tokenization_spaces (`bool`, *optional*, defaults to `False`):
237
+ Whether or not to clean up the tokenization spaces. Argument passed to the tokenizer's `batch_decode` method.
238
+ **kwargs:
239
+ Additional arguments to be passed to the tokenizer's `batch_decode method`.
240
+
241
+ Returns:
242
+ `list[str]`: The decoded text.
243
+ """
244
+ return self.tokenizer.batch_decode(
245
+ generated_outputs,
246
+ skip_special_tokens=skip_special_tokens,
247
+ clean_up_tokenization_spaces=clean_up_tokenization_spaces,
248
+ **kwargs,
249
+ )
250
+
251
+ def _calculate_timestamps(self, indices: list[int] | np.ndarray, video_fps: float, merge_size: int = 2):
252
+ if not isinstance(indices, list):
253
+ indices = indices.tolist()
254
+ if len(indices) % merge_size != 0:
255
+ indices.extend(indices[-1] for _ in range(merge_size - len(indices) % merge_size))
256
+ timestamps = [idx / video_fps for idx in indices]
257
+ # @JJJYmmm frames are merged by self.merge_size, \
258
+ # so we need to average the timestamps between the first/last frame within the temporal patch
259
+ timestamps = [
260
+ (timestamps[i] + timestamps[i + merge_size - 1]) / 2 for i in range(0, len(timestamps), merge_size)
261
+ ]
262
+ return timestamps
263
+
264
+
265
+ __all__ = ["InternS2MobiusProcessor"]
special_tokens_map.json ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "additional_special_tokens": [
3
+ "<|im_start|>",
4
+ "<|im_end|>",
5
+ "<|object_ref_start|>",
6
+ "<|object_ref_end|>",
7
+ "<|box_start|>",
8
+ "<|box_end|>",
9
+ "<|quad_start|>",
10
+ "<|quad_end|>",
11
+ "<|vision_start|>",
12
+ "<|vision_end|>",
13
+ "<|vision_pad|>",
14
+ "<|image_pad|>",
15
+ "<|video_pad|>"
16
+ ],
17
+ "audio_bos_token": "<|audio_start|>",
18
+ "audio_eos_token": "<|audio_end|>",
19
+ "audio_token": "<|audio_pad|>",
20
+ "bos_token": {
21
+ "content": "<|im_start|>",
22
+ "lstrip": false,
23
+ "normalized": false,
24
+ "rstrip": false,
25
+ "single_word": false
26
+ },
27
+ "eos_token": {
28
+ "content": "<|im_end|>",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false
33
+ },
34
+ "image_token": "<|image_pad|>",
35
+ "pad_token": {
36
+ "content": "<|endoftext|>",
37
+ "lstrip": false,
38
+ "normalized": false,
39
+ "rstrip": false,
40
+ "single_word": false
41
+ },
42
+ "video_token": "<|video_pad|>",
43
+ "vision_bos_token": "<|vision_start|>",
44
+ "vision_eos_token": "<|vision_end|>"
45
+ }
tokenization_interns1.py ADDED
@@ -0,0 +1,1009 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2025 The Intern team and Shanghai AI Lab team. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ """Tokenization classes for InternS1."""
16
+
17
+ import json
18
+ import os
19
+ import unicodedata
20
+ from abc import ABC, abstractmethod
21
+ from typing import Optional, Union
22
+ from functools import lru_cache
23
+
24
+ import regex as re
25
+ import sentencepiece as spm
26
+
27
+ from transformers.tokenization_utils_base import AddedToken, TextInput
28
+ from transformers.utils import logging
29
+ from packaging import version
30
+ import transformers
31
+ if version.parse(transformers.__version__) >= version.parse("5.0.0"):
32
+ from transformers.tokenization_python import PreTrainedTokenizer
33
+ else:
34
+ from transformers.tokenization_utils import PreTrainedTokenizer
35
+
36
+ logger = logging.get_logger(__name__)
37
+
38
+ try:
39
+ from rdkit import Chem, RDLogger
40
+
41
+ RDLogger.DisableLog("rdApp.error")
42
+ RDLogger.DisableLog("rdApp.*")
43
+ RDKIT_AVAILABLE = True
44
+ except ImportError:
45
+ logger.warning_once(
46
+ "If tokenization with SMILES formula is of necessity, please 'pip install RDKit' for better tokenization quality."
47
+ )
48
+ RDKIT_AVAILABLE = False
49
+
50
+ VOCAB_FILES_NAMES = {
51
+ "vocab_file": "vocab.json",
52
+ "merges_file": "merges.txt",
53
+ "sp_model_SMILES": "tokenizer_SMILES.model",
54
+ "sp_model_PROT": "tokenizer_PROT.model",
55
+ "sp_model_XNA": "tokenizer_XNA.model",
56
+ }
57
+
58
+ PRETOKENIZE_REGEX = r"""(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+"""
59
+
60
+
61
+ class InternS1CheckModuleMixin(ABC):
62
+ """
63
+ Basic auto-detection module.
64
+
65
+ Note that short strings are ignored by this module.
66
+ """
67
+
68
+ def __init__(self, *, min_length: int):
69
+ self.min_length = min_length
70
+ self.REGEX = self._build_regex()
71
+ self.all_auto_detect_token_start = ["<SMILES_AUTO_DETECT>", "<PROT_AUTO_DETECT>", "<XNA_AUTO_DETECT>"]
72
+ self.all_auto_detect_token_end = ["</SMILES_AUTO_DETECT>", "</PROT_AUTO_DETECT>", "</XNA_AUTO_DETECT>"]
73
+ self.auto_detect_token = []
74
+ self.truncation = False
75
+
76
+ @abstractmethod
77
+ def _build_regex(self):
78
+ pass
79
+
80
+ @abstractmethod
81
+ def check_legitimacy(self, candidate: str) -> bool:
82
+ pass
83
+
84
+ def re_split(self, texts: Union[str, list[str]]) -> list[str]:
85
+ if isinstance(texts, str):
86
+ texts = [texts]
87
+
88
+ total_results = []
89
+
90
+ no_split_flag = 0
91
+
92
+ for text in texts:
93
+ if text in self.all_auto_detect_token_start:
94
+ total_results.append(text)
95
+ no_split_flag += 1
96
+ continue
97
+ elif text in self.all_auto_detect_token_end:
98
+ total_results.append(text)
99
+ no_split_flag = max(0, no_split_flag - 1)
100
+ continue
101
+
102
+ if no_split_flag > 0:
103
+ total_results.append(text)
104
+ continue
105
+
106
+ results = []
107
+ current_pos = 0
108
+ for match in self.REGEX.finditer(text):
109
+ candidate = match.group(1)
110
+
111
+ if len(candidate) >= self.min_length:
112
+ match_start, match_end = match.span(1)
113
+
114
+ if not self.check_legitimacy(candidate):
115
+ continue
116
+
117
+ if not self.truncation:
118
+ if match_start > 0 and text[match_start - 1].encode("UTF-8").isalpha():
119
+ continue
120
+ if match_end < len(text) and text[match_end].encode("UTF-8").isalpha():
121
+ continue
122
+
123
+ if match_start > current_pos:
124
+ non_candidate_part = text[current_pos:match_start]
125
+ results.append(non_candidate_part)
126
+ else:
127
+ continue
128
+
129
+ results.extend([self.auto_detect_token[0], candidate, self.auto_detect_token[1]])
130
+ current_pos = match_end
131
+
132
+ if current_pos < len(text):
133
+ remaining_part = text[current_pos:]
134
+ results.append(remaining_part)
135
+
136
+ total_results.extend(results)
137
+
138
+ return total_results
139
+
140
+
141
+ class XnaCheckModule(InternS1CheckModuleMixin):
142
+ """
143
+ XNA sequence auto-detection module.
144
+
145
+ Automatically detects XNA sequence using regex patterns.
146
+ """
147
+ def __init__(self, *, min_length: int = 27):
148
+ super().__init__(min_length=min_length)
149
+ self.auto_detect_token = ["<XNA_AUTO_DETECT>", "</XNA_AUTO_DETECT>"]
150
+ self.truncation = True
151
+
152
+ def _build_regex(self):
153
+ return re.compile(r"([ATCGU]{" + str(self.min_length) + r",})")
154
+
155
+ def check_legitimacy(self, candidate: str):
156
+ return True
157
+
158
+
159
+ class ProtCheckModule(InternS1CheckModuleMixin):
160
+ """
161
+ Protein sequence auto-detection module.
162
+
163
+ Automatically detects protein sequence using regex patterns.
164
+ """
165
+ def __init__(self, *, min_length: int = 27):
166
+ super().__init__(min_length=min_length)
167
+ self.auto_detect_token = ["<PROT_AUTO_DETECT>", "</PROT_AUTO_DETECT>"]
168
+ self.truncation = True
169
+ self._xna_pattern = re.compile(r"^[ATCGU]+$")
170
+
171
+ def _build_regex(self):
172
+ return re.compile(r"([A-Z]{" + str(self.min_length) + r",})")
173
+
174
+ def check_legitimacy(self, candidate: str):
175
+ if self._xna_pattern.match(candidate):
176
+ return False
177
+ return True
178
+
179
+
180
+ # fmt: off
181
+ bonds = ["-", "=", "#", ":", "/", "\\", ".", "$"]
182
+ organic_symbols = ["B", "C", "N", "O", "P", "S", "F", "Cl", "Br", "I"]
183
+ other_allows = bonds + ["[", "]", "(", ")", ";"]
184
+ aromatic_symbols = ["b", "c", "n", "o", "s", "p"]
185
+ elements = [
186
+ "H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne",
187
+ "Na", "Mg", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca",
188
+ "Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn",
189
+ "Ga", "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y", "Zr",
190
+ "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd", "In", "Sn",
191
+ "Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd",
192
+ "Pm", "Sm", "Eu", "Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb",
193
+ "Lu", "Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg",
194
+ "Tl", "Pb", "Bi", "Po", "At", "Rn", "Fr", "Ra", "Ac", "Th",
195
+ "Pa", "U", "Np", "Pu", "Am", "Cm", "Bk", "Cf", "Es", "Fm",
196
+ "Md", "No", "Lr", "Rf", "Db", "Sg", "Bh", "Hs", "Mt", "Ds",
197
+ "Rg", "Cn", "Nh", "Fl", "Mc", "Lv", "Ts", "Og"
198
+ ]
199
+ # fmt: on
200
+
201
+
202
+ class SmilesCheckModule(InternS1CheckModuleMixin):
203
+ """
204
+ SMILES molecular sequence auto-detection module.
205
+
206
+ Automatically detects and validates SMILES strings in text using regex patterns
207
+ or chemical syntax rules. Uses RDKit for precise validation when available,
208
+ otherwise falls back to rule-based validation.
209
+ """
210
+
211
+ def __init__(self, *, min_length: int = 10):
212
+ super().__init__(min_length=min_length)
213
+ self.auto_detect_token = ["<SMILES_AUTO_DETECT>", "</SMILES_AUTO_DETECT>"]
214
+ self._SQ_BRACKET_BAN_1 = re.compile(r"(?:[A-GI-Z]|[a-z]){3,}")
215
+ self._SQ_BRACKET_BAN_2 = re.compile(r"\d{4,}")
216
+
217
+ def _build_regex(self):
218
+ # fmt: off
219
+ _two_letter_elements = [
220
+ 'Ac', 'Ag', 'Al', 'Am', 'Ar', 'As', 'At', 'Au', 'Ba', 'Be', 'Bh', 'Bi', 'Bk', 'Br', 'Ca', 'Cd',
221
+ 'Ce', 'Cf', 'Cl', 'Cm', 'Cn', 'Co', 'Cr', 'Cs', 'Cu', 'Db', 'Ds', 'Dy', 'Er', 'Es', 'Eu', 'Fe',
222
+ 'Fl', 'Fm', 'Fr', 'Ga', 'Gd', 'Ge', 'He', 'Hf', 'Hg', 'Ho', 'Hs', 'In', 'Ir', 'Kr', 'La', 'Li',
223
+ 'Lr', 'Lu', 'Lv', 'Mc', 'Md', 'Mg', 'Mn', 'Mo', 'Mt', 'Na', 'Nb', 'Nd', 'Ne', 'Nh', 'Ni', 'No',
224
+ 'Np', 'Og', 'Os', 'Pa', 'Pb', 'Pd', 'Pm', 'Po', 'Pr', 'Pt', 'Pu', 'Ra', 'Rb', 'Re', 'Rf', 'Rg',
225
+ 'Rh', 'Rn', 'Ru', 'Sb', 'Sc', 'Se', 'Sg', 'Si', 'Sm', 'Sn', 'Sr', 'Ta', 'Tb', 'Tc', 'Te', 'Th',
226
+ 'Ti', 'Tl', 'Tm', 'Ts', 'Xe', 'Yb', 'Zn', 'Zr'
227
+ ]
228
+ _single_letter_elements = [
229
+ "B", "C", "F", "H", "I", "K", "N", "O", "P", "S", "U", "V", "W", "Y", 'b', 'c', 'n', 'o', 'p', 's'
230
+ ]
231
+ # fmt: on
232
+ all_elements_sorted = sorted(_two_letter_elements + _single_letter_elements, key=lambda x: (-len(x), x))
233
+ elements_pattern_str = "|".join(all_elements_sorted)
234
+
235
+ bracket_atom_pattern_str = r"\[[^\]]+\]"
236
+ other_single_chars_pattern_str = r"[\(\)\.=\-#@\d\$\%\*:\+\-\/\\]"
237
+ smiles_unit_pattern = (
238
+ r"(?:"
239
+ + bracket_atom_pattern_str
240
+ + r"|"
241
+ + elements_pattern_str
242
+ + r"|"
243
+ + other_single_chars_pattern_str
244
+ + r")"
245
+ )
246
+ core_sequence_pattern = rf"(?>{smiles_unit_pattern}){{10,}}"
247
+ constrained_core_sequence_pattern = rf"(?![:.=]){core_sequence_pattern}(?<![:.=])"
248
+
249
+ final_regex_str = rf"({constrained_core_sequence_pattern})"
250
+
251
+ COMPILED_REGEX = re.compile(final_regex_str)
252
+ return COMPILED_REGEX
253
+
254
+ def check_legitimacy_slow(self, candidate: str) -> bool:
255
+ """Check legitimacy with RDKit"""
256
+ if sum(1 for char in candidate if char.encode("UTF-8").isalpha()) < 5:
257
+ return False
258
+
259
+ mol = Chem.MolFromSmiles(candidate)
260
+ if mol is None:
261
+ return False
262
+ else:
263
+ return True
264
+
265
+ def check_legitimacy_fast(self, candidate: str) -> bool:
266
+ """Check legitimacy with hard rules"""
267
+ if sum(1 for char in candidate if char.encode("UTF-8").isalpha()) < 5:
268
+ return False
269
+
270
+ if not self.check_rings_and_brackets(candidate):
271
+ return False
272
+ else:
273
+ return True
274
+
275
+ def check_legitimacy(self, candidate: str) -> bool:
276
+ if RDKIT_AVAILABLE:
277
+ return self.check_legitimacy_slow(candidate)
278
+ else:
279
+ return self.check_legitimacy_fast(candidate)
280
+
281
+ def check_brackets(self, text):
282
+ matches = re.findall(r"\[([^\[\]]*)\]", text)
283
+ for part in matches:
284
+ if "(" in part or ")" in part:
285
+ return False
286
+ if len(part) == 0:
287
+ return False
288
+ if part[0] in elements or part[0] in aromatic_symbols or part[:2] in elements:
289
+ return True
290
+ return True
291
+
292
+ def check_rings_and_brackets(self, text):
293
+ rings = {}
294
+ left_sq_bracket, right_sq_bracket = 0, 0
295
+ left_pt_bracket, right_pt_bracket = 0, 0
296
+ all_lower = True
297
+ digits_cnt = 0
298
+ pos = 0
299
+ while pos < len(text):
300
+ step = 0
301
+ c = text[pos]
302
+ if ord(c) >= 65 and ord(c) <= 90:
303
+ all_lower = False
304
+ if (pos == len(text) - 1 or pos == 0) and c in bonds:
305
+ return False
306
+ if pos > 0 and text[pos - 1] in bonds and text[pos] in bonds:
307
+ return False
308
+ if c == "[":
309
+ step = 1
310
+ left_sq_bracket += 1
311
+ if left_sq_bracket > right_sq_bracket + 1:
312
+ return False
313
+ if pos == len(text) - 1:
314
+ return False
315
+ if "]" not in text[pos + 1 :]:
316
+ return False
317
+ bracket_span = text[pos + 1 : text.find("]")]
318
+
319
+ if self._SQ_BRACKET_BAN_1.search(bracket_span) or self._SQ_BRACKET_BAN_2.search(bracket_span):
320
+ return False
321
+
322
+ matches = re.findall(r"\d+", bracket_span)
323
+ if len(matches) > 2:
324
+ return False
325
+ if c == "]":
326
+ step = 1
327
+ right_sq_bracket += 1
328
+ if right_sq_bracket > left_sq_bracket:
329
+ return False
330
+
331
+ if c == "(":
332
+ step = 1
333
+ left_pt_bracket += 1
334
+ if c == ")":
335
+ step = 1
336
+ right_pt_bracket += 1
337
+ if right_pt_bracket > left_pt_bracket:
338
+ return False
339
+
340
+ if left_sq_bracket == right_sq_bracket:
341
+ if c.isdigit():
342
+ digits_cnt += 1
343
+ step = 1
344
+ if (
345
+ pos == 0
346
+ or (pos == 1 and text[pos - 1] != "%")
347
+ or (pos > 1 and text[pos - 1] != "%" and text[pos - 2] != "%")
348
+ ):
349
+ if c in rings:
350
+ if rings[c] == "unclosed":
351
+ rings[c] = "closed"
352
+ else:
353
+ rings[c] = "unclosed"
354
+ else:
355
+ rings[c] = "unclosed"
356
+ if c == "%":
357
+ if pos >= len(text) - 2 or not text[pos + 1].isdigit() or not text[pos + 2].isdigit():
358
+ return False
359
+ step = 3
360
+ digits_cnt += 1
361
+ num = text[pos + 1 : pos + 3]
362
+ if num in rings:
363
+ if rings[num] == "unclosed":
364
+ rings[num] = "closed"
365
+ else:
366
+ rings[num] = "unclosed"
367
+ else:
368
+ rings[num] = "unclosed"
369
+ if step == 0:
370
+ if (
371
+ pos < len(text) - 1
372
+ and text[pos : pos + 2] in organic_symbols + aromatic_symbols + other_allows
373
+ ):
374
+ step = 2
375
+ elif c in organic_symbols + aromatic_symbols + other_allows:
376
+ step = 1
377
+ else:
378
+ return False
379
+
380
+ if step == 0:
381
+ step = 1
382
+ pos += step
383
+
384
+ if left_sq_bracket != right_sq_bracket or any(v == "unclosed" for v in rings.values()):
385
+ return False
386
+ if all_lower and digits_cnt < 2:
387
+ return False
388
+ return self.check_brackets(text)
389
+
390
+
391
+ @lru_cache
392
+ # Copied from transformers.models.gpt2.tokenization_gpt2.bytes_to_unicode
393
+ def bytes_to_unicode():
394
+ """
395
+ Returns list of utf-8 byte and a mapping to unicode strings. We specifically avoids mapping to whitespace/control
396
+ characters the bpe code barfs on.
397
+
398
+ The reversible bpe codes work on unicode strings. This means you need a large # of unicode characters in your vocab
399
+ if you want to avoid UNKs. When you're at something like a 10B token dataset you end up needing around 5K for
400
+ decent coverage. This is a significant percentage of your normal, say, 32K bpe vocab. To avoid that, we want lookup
401
+ tables between utf-8 bytes and unicode strings.
402
+ """
403
+ bs = (
404
+ list(range(ord("!"), ord("~") + 1)) + list(range(ord("¡"), ord("¬") + 1)) + list(range(ord("®"), ord("ÿ") + 1))
405
+ )
406
+ cs = bs[:]
407
+ n = 0
408
+ for b in range(2**8):
409
+ if b not in bs:
410
+ bs.append(b)
411
+ cs.append(2**8 + n)
412
+ n += 1
413
+ cs = [chr(n) for n in cs]
414
+ return dict(zip(bs, cs))
415
+
416
+
417
+ # Copied from transformers.models.gpt2.tokenization_gpt2.get_pairs
418
+ def get_pairs(word):
419
+ """
420
+ Return set of symbol pairs in a word.
421
+
422
+ Word is represented as tuple of symbols (symbols being variable-length strings).
423
+ """
424
+ pairs = set()
425
+ prev_char = word[0]
426
+ for char in word[1:]:
427
+ pairs.add((prev_char, char))
428
+ prev_char = char
429
+ return pairs
430
+
431
+
432
+ # @requires(backends=("sentencepiece",))
433
+ class InternS1Tokenizer(PreTrainedTokenizer):
434
+ """
435
+ Construct an InternS1 tokenizer. Based on byte-level Byte-Pair-Encoding.
436
+
437
+ Same with GPT2Tokenizer, this tokenizer has been trained to treat spaces like parts of the tokens so a word will
438
+ be encoded differently whether it is at the beginning of the sentence (without space) or not:
439
+
440
+ ```python
441
+ >>> from transformers import AutoTokenizer
442
+
443
+ >>> tokenizer = AutoTokenizer.from_pretrained("InternS1Tokenizer", trust_remote_code=True)
444
+ >>> tokenizer("Hello world")["input_ids"]
445
+ [9707, 1879]
446
+
447
+ >>> tokenizer(" Hello world")["input_ids"]
448
+ [21927, 1879]
449
+ ```
450
+ This is expected.
451
+
452
+ Include custom extension to support better domain-specific text tokenization, leveraging a separately trained tokenizer model.
453
+
454
+ ```python
455
+ >>> from transformers import AutoTokenizer
456
+
457
+ >>> tokenizer = AutoTokenizer.from_pretrained("InternS1Tokenizer", trust_remote_code=True)
458
+ >>> tokenizer.tokenize("Describe <SMILES>C1=CC=C(C=C1)C=O</SMILES> and CC1=CC=CC=C1C=O")
459
+ ["Describe ", "<SMILES>", "C1=CC=C(C=C1)C=O", "</SMILES>", " and ", "<SMILES_AUTO_DETECT>",
460
+ "CC1=CC=CC=C1C=O", "</SMILES_AUTO_DETECT>"]
461
+ >>> token_ids = tokenizer("Describe <SMILES>C1=CC=C(C=C1)C=O</SMILES> and CC1=CC=CC=C1C=O")["input_ids"]
462
+ >>> token_ids
463
+ [74785, 220, 151925, 151854, 151860, 151698, 151707, 151860, 151690, 151726, 151926, 323, 220, 151672, 151860, 151701, 151860, 151854, 151726]
464
+
465
+ >>> tokenizer.convert_ids_to_tokens(token_ids)
466
+ ['Describe', 'Ġ', '<SMILES>', 'C', '1', '=CC=C(', 'C=C', '1', ')C', '=O', '</SMILES>', 'Ġand', 'Ġ', 'CC', '1', '=CC=CC=C', '1', 'C', '=O']
467
+ ```
468
+
469
+ Users should refer to this superclass [`PreTrainedTokenizer`] for more information regarding those overloaded methods
470
+
471
+ Args:
472
+ vocab_file (`str`):
473
+ Path to the vocabulary file.
474
+ merges_file (`str`):
475
+ Path to the merges file.
476
+ errors (`str`, *optional*, defaults to `"replace"`):
477
+ Paradigm to follow when decoding bytes to UTF-8. See
478
+ [bytes.decode](https://docs.python.org/3/library/stdtypes.html#bytes.decode) for more information.
479
+ unk_token (`str`, *optional*, defaults to `"<|endoftext|>"`):
480
+ The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
481
+ token instead.
482
+ bos_token (`str`, *optional*):
483
+ The beginning of sequence token. Not applicable for this tokenizer.
484
+ eos_token (`str`, *optional*, defaults to `"<|endoftext|>"`):
485
+ The end of sequence token.
486
+ pad_token (`str`, *optional*, defaults to `"<|endoftext|>"`):
487
+ The token used for padding, for example when batching sequences of different lengths.
488
+ clean_up_tokenization_spaces (`bool`, *optional*, defaults to `False`):
489
+ Whether or not the model should cleanup the spaces that were added when splitting the input text during the
490
+ tokenization process. Not applicable to this tokenizer, since tokenization does not add spaces.
491
+ split_special_tokens (`bool`, *optional*, defaults to `False`):
492
+ Whether or not the special tokens should be split during the tokenization process. The default behavior is
493
+ to not split special tokens. This means that if `<|endoftext|>` is the `eos_token`, then `tokenizer.tokenize("<|endoftext|>") =
494
+ ['<|endoftext|>`]. Otherwise, if `split_special_tokens=True`, then `tokenizer.tokenize("<|endoftext|>")` will be give `['<',
495
+ '|', 'endo', 'ft', 'ext', '|', '>']`. This argument is only supported for `slow` tokenizers for the moment.
496
+ """
497
+
498
+ vocab_files_names = VOCAB_FILES_NAMES
499
+ model_input_names = ["input_ids", "attention_mask"]
500
+
501
+ def __init__(
502
+ self,
503
+ vocab_file,
504
+ merges_file,
505
+ errors="replace",
506
+ unk_token="<|endoftext|>",
507
+ bos_token=None,
508
+ eos_token="<|endoftext|>",
509
+ pad_token="<|endoftext|>",
510
+ clean_up_tokenization_spaces=False,
511
+ split_special_tokens=False,
512
+ special_tokens_pattern="none",
513
+ **kwargs,
514
+ ):
515
+ bos_token = (
516
+ AddedToken(bos_token, lstrip=False, rstrip=False, special=True, normalized=False)
517
+ if isinstance(bos_token, str)
518
+ else bos_token
519
+ )
520
+ eos_token = (
521
+ AddedToken(eos_token, lstrip=False, rstrip=False, special=True, normalized=False)
522
+ if isinstance(eos_token, str)
523
+ else eos_token
524
+ )
525
+ unk_token = (
526
+ AddedToken(unk_token, lstrip=False, rstrip=False, special=True, normalized=False)
527
+ if isinstance(unk_token, str)
528
+ else unk_token
529
+ )
530
+ pad_token = (
531
+ AddedToken(pad_token, lstrip=False, rstrip=False, special=True, normalized=False)
532
+ if isinstance(pad_token, str)
533
+ else pad_token
534
+ )
535
+
536
+ with open(vocab_file, encoding="utf-8") as vocab_handle:
537
+ self.encoder = json.load(vocab_handle)
538
+ self.decoder = {v: k for k, v in self.encoder.items()}
539
+ self.errors = errors # how to handle errors in decoding
540
+ self.byte_encoder = bytes_to_unicode()
541
+ self.byte_decoder = {v: k for k, v in self.byte_encoder.items()}
542
+ bpe_merges = []
543
+ with open(merges_file, encoding="utf-8") as merges_handle:
544
+ for i, line in enumerate(merges_handle):
545
+ line = line.strip()
546
+ if (i == 0 and line.startswith("#version:")) or not line:
547
+ continue
548
+ bpe_merges.append(tuple(line.split()))
549
+ self.bpe_ranks = dict(zip(bpe_merges, range(len(bpe_merges))))
550
+ # NOTE: the cache can grow without bound and will get really large for long running processes
551
+ # (esp. for texts of language that do not use space between word, e.g. Chinese); technically
552
+ # not a memory leak but appears as one.
553
+ # GPT2Tokenizer has the same problem, so let's be consistent.
554
+ self.cache = {}
555
+
556
+ self.pat = re.compile(PRETOKENIZE_REGEX)
557
+
558
+ if kwargs.get("add_prefix_space", False):
559
+ logger.warning_once(
560
+ f"{self.__class__.__name} does not support `add_prefix_space`, setting it to True has no effect."
561
+ )
562
+
563
+ super().__init__(
564
+ vocab_file=vocab_file,
565
+ merges_file=merges_file,
566
+ errors=errors,
567
+ unk_token=unk_token,
568
+ bos_token=bos_token,
569
+ eos_token=eos_token,
570
+ pad_token=pad_token,
571
+ clean_up_tokenization_spaces=clean_up_tokenization_spaces,
572
+ split_special_tokens=split_special_tokens,
573
+ special_tokens_pattern=special_tokens_pattern,
574
+ **kwargs,
575
+ )
576
+
577
+ self.prepare_extra_tokenizers(vocab_file)
578
+
579
+ @property
580
+ def vocab_size(self) -> int:
581
+ return len(self.encoder)
582
+
583
+ # Copied from transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer.get_vocab
584
+ def get_vocab(self):
585
+ return dict(self.encoder, **self.added_tokens_encoder)
586
+
587
+ # Copied from transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer.bpe
588
+ def bpe(self, token):
589
+ if token in self.cache:
590
+ return self.cache[token]
591
+ word = tuple(token)
592
+ pairs = get_pairs(word)
593
+
594
+ if not pairs:
595
+ return token
596
+
597
+ while True:
598
+ bigram = min(pairs, key=lambda pair: self.bpe_ranks.get(pair, float("inf")))
599
+ if bigram not in self.bpe_ranks:
600
+ break
601
+ first, second = bigram
602
+ new_word = []
603
+ i = 0
604
+ while i < len(word):
605
+ try:
606
+ j = word.index(first, i)
607
+ except ValueError:
608
+ new_word.extend(word[i:])
609
+ break
610
+ else:
611
+ new_word.extend(word[i:j])
612
+ i = j
613
+
614
+ if word[i] == first and i < len(word) - 1 and word[i + 1] == second:
615
+ new_word.append(first + second)
616
+ i += 2
617
+ else:
618
+ new_word.append(word[i])
619
+ i += 1
620
+ new_word = tuple(new_word)
621
+ word = new_word
622
+ if len(word) == 1:
623
+ break
624
+ else:
625
+ pairs = get_pairs(word)
626
+ word = " ".join(word)
627
+ self.cache[token] = word
628
+ return word
629
+
630
+ def prepare_extra_tokenizers(self, vocab_file: str) -> None:
631
+ """
632
+ Prepare domain-specific tokenizers.
633
+
634
+ Define variables/maps here which guide domain-specific tokenization later.
635
+ """
636
+ # Load extra tokenizers with SentencePiece model
637
+ dir_name = os.path.dirname(vocab_file)
638
+
639
+ self.sp_model_SMILES = spm.SentencePieceProcessor()
640
+ self.sp_model_SMILES.Load(os.path.join(dir_name, "tokenizer_SMILES.model"))
641
+ self.sp_model_SMILES.offset = self.init_kwargs["offset_SMILES"]
642
+
643
+ self.sp_model_PROT = spm.SentencePieceProcessor()
644
+ self.sp_model_PROT.Load(os.path.join(dir_name, "tokenizer_PROT.model"))
645
+ self.sp_model_PROT.offset = self.init_kwargs["offset_PROT"]
646
+
647
+ self.sp_model_XNA = spm.SentencePieceProcessor()
648
+ self.sp_model_XNA.Load(os.path.join(dir_name, "tokenizer_XNA.model"))
649
+ self.sp_model_XNA.offset = self.init_kwargs["offset_XNA"]
650
+
651
+ base_mapping = {
652
+ "SMILES": self.sp_model_SMILES,
653
+ "protein": self.sp_model_PROT,
654
+ "dna": self.sp_model_XNA,
655
+ "rna": self.sp_model_XNA,
656
+ }
657
+ auto_detect_mapping = {
658
+ "SMILES": self.sp_model_SMILES,
659
+ "PROT": self.sp_model_PROT,
660
+ "XNA": self.sp_model_XNA,
661
+ }
662
+ # Guiding tokens of domain-specific tokenization
663
+ self.ex_begin_mapping = {f"<{key}>": value for key, value in base_mapping.items()}
664
+ self.ex_end_mapping = {f"</{key}>": value for key, value in base_mapping.items()}
665
+ # Transient markers for auto-detection, these tokens will not be assigned token ids
666
+ self.ex_auto_begin_mapping = {f"<{key}_AUTO_DETECT>": value for key, value in auto_detect_mapping.items()}
667
+ self.ex_auto_end_mapping = {f"</{key}_AUTO_DETECT>": value for key, value in auto_detect_mapping.items()}
668
+ # Token markers to prevent unwanted auto-detection
669
+ self.ex_protect_begin_tokens = ["<MOLFORMULA>"]
670
+ self.ex_protect_end_tokens = ["</MOLFORMULA>"]
671
+ # For simplicity
672
+ self.ex_protect_tokens = self.ex_protect_begin_tokens + self.ex_protect_end_tokens
673
+ self.ex_all_begin_mapping = self.ex_begin_mapping | self.ex_auto_begin_mapping
674
+ self.ex_all_end_mapping = self.ex_end_mapping | self.ex_auto_end_mapping
675
+
676
+ # Update encoder & decoder with extra tokenizers
677
+ for tokenizer_name, sp_model in [
678
+ ("SMILES", self.sp_model_SMILES),
679
+ ("PROT", self.sp_model_PROT),
680
+ ("XNA", self.sp_model_XNA),
681
+ ]:
682
+ self.decoder.update(
683
+ {i + sp_model.offset: sp_model.id_to_piece(i) for i in range(sp_model.get_piece_size())}
684
+ )
685
+ # Not really used, only to fill holes in encoder, to keep methods like `add_tokens` working
686
+ self.encoder.update(
687
+ {
688
+ f"<|{tokenizer_name}_{sp_model.id_to_piece(i)}|>": i + sp_model.offset
689
+ for i in range(sp_model.get_piece_size())
690
+ }
691
+ )
692
+
693
+ # protect-tokens should keep complete temporarily to guide later tokenization
694
+ # it will be segmented later
695
+ for token in self.ex_protect_tokens:
696
+ self.tokens_trie.add(token)
697
+
698
+ self._unk_token = "<unk>" # Fall-back
699
+ self.check_module_list = [SmilesCheckModule(), ProtCheckModule(), XnaCheckModule()]
700
+
701
+ def _pop_logical_sp_token(self, extra_tokenizer_stack: list, mapping_name: str) -> None:
702
+ """Switch tokenizer when it comes to an end sp token"""
703
+ extra_tokenizer = extra_tokenizer_stack.pop()
704
+ if extra_tokenizer != self.ex_all_end_mapping[mapping_name]:
705
+ logger.warning_once(
706
+ f"Encounter incorrect nesting of extra tokenizer: {self.ex_all_end_mapping[mapping_name]} and {extra_tokenizer}"
707
+ )
708
+ logger.warning_once("This may lead to unexpected behaviour of the tokenizer, please check your input.")
709
+
710
+ def tokenize(self, text: TextInput, **kwargs) -> list[str]:
711
+ """
712
+ Converts a string into a sequence of tokens, using the tokenizer.
713
+
714
+ It will switch to domain-specific tokenizer once encountering extra/logical sp tokens.
715
+
716
+ Args:
717
+ text: TextInput
718
+ """
719
+ split_special_tokens = kwargs.pop("split_special_tokens", self.split_special_tokens)
720
+
721
+ text, kwargs = self.prepare_for_tokenization(text, **kwargs)
722
+
723
+ if hasattr(self, "do_lower_case") and self.do_lower_case:
724
+ # convert non-special tokens to lowercase. Might be super slow as well?
725
+ escaped_special_toks = [re.escape(s_tok) for s_tok in (self.all_special_tokens)]
726
+ escaped_special_toks += [
727
+ re.escape(s_tok.content)
728
+ for s_tok in (self._added_tokens_decoder.values())
729
+ if not s_tok.special and s_tok.normalized
730
+ ]
731
+ pattern = r"(" + r"|".join(escaped_special_toks) + r")|" + r"(.+?)"
732
+ text = re.sub(pattern, lambda m: m.groups()[0] or m.groups()[1].lower(), text)
733
+
734
+ if split_special_tokens:
735
+ no_split_token = []
736
+ tokens = [text]
737
+ else:
738
+ no_split_token = self._added_tokens_encoder.keys() # don't split on any of the added tokens
739
+ # "This is something<special_token_1> else"
740
+ tokens = self.tokens_trie.split(text)
741
+
742
+ # ["This is something", "<special_token_1>", " else"]
743
+ for i, token in enumerate(tokens):
744
+ if token in no_split_token:
745
+ tok_extended = self._added_tokens_decoder.get(self._added_tokens_encoder[token], None)
746
+ left = tokens[i - 1] if i > 0 else None
747
+ right = tokens[i + 1] if i < len(tokens) - 1 else None
748
+ if isinstance(tok_extended, AddedToken):
749
+ if tok_extended.rstrip and right:
750
+ # A bit counter-intuitive but we strip the left of the string
751
+ # since tok_extended.rstrip means the special token is eating all white spaces on its right
752
+ tokens[i + 1] = right.lstrip()
753
+ # Strip white spaces on the left
754
+ if tok_extended.lstrip and left:
755
+ tokens[i - 1] = left.rstrip() # Opposite here
756
+ if tok_extended.single_word and left and left[-1] != " ":
757
+ tokens[i - 1] += token
758
+ tokens[i] = ""
759
+ elif tok_extended.single_word and right and right[0] != " ":
760
+ tokens[i + 1] = token + tokens[i + 1]
761
+ tokens[i] = ""
762
+ else:
763
+ raise ValueError(
764
+ f"{tok_extended} cannot be tokenized because it was not properly added"
765
+ f" to the tokenizer. This means that it is not an `AddedToken` but a {type(tok_extended)}"
766
+ )
767
+
768
+ # ["This is something", "<special_token_1>", "else"]
769
+ tokenized_text = []
770
+
771
+ # Codes for automatically detecting domain-specific content
772
+ # All parts that have been marked by domain-specific or protection tokens will not be subject to auto detection
773
+ # See transformers/tests/models/intern_s1/test_tokenization_intern_s1.py::test_auto_detection() for more details
774
+ new_tokens = []
775
+ not_split_flag = 0
776
+ for token in tokens:
777
+ if not token:
778
+ continue
779
+ if token in no_split_token or token in self.ex_protect_tokens:
780
+ new_tokens.append(token)
781
+ if token in self.ex_begin_mapping or token in self.ex_protect_begin_tokens:
782
+ not_split_flag += 1 # In case nested sp tokens
783
+ elif token in self.ex_end_mapping or token in self.ex_protect_end_tokens:
784
+ not_split_flag = max(0, not_split_flag - 1)
785
+ else:
786
+ if not_split_flag:
787
+ new_tokens.append(token)
788
+ else:
789
+ for check_module in self.check_module_list:
790
+ token = check_module.re_split(token)
791
+
792
+ new_tokens.extend(token)
793
+ tokens = new_tokens
794
+
795
+ # Use stack to maintain which tokenizer should be used, considering the possibility of nested extra tokenizer
796
+ extra_tokenizer_stack = []
797
+ for token in tokens:
798
+ # Need to skip eventual empty (fully stripped) tokens
799
+ if not token:
800
+ continue
801
+ # protect-tokens are not assigned token ids, should be segmented here
802
+ if token in self.ex_protect_tokens:
803
+ tokenized_text.extend(self._tokenize(token))
804
+ # push tokenizer to stack when encountering begin token
805
+ elif token in self.ex_all_begin_mapping:
806
+ tokenized_text.append(token)
807
+ extra_tokenizer_stack.append(self.ex_all_begin_mapping[token])
808
+ # pop tokenizer from stack when encountering end token
809
+ elif token in self.ex_all_end_mapping:
810
+ tokenized_text.append(token)
811
+ if extra_tokenizer_stack:
812
+ self._pop_logical_sp_token(extra_tokenizer_stack, token)
813
+ # other special tokens
814
+ elif token in no_split_token:
815
+ tokenized_text.append(token)
816
+ else:
817
+ tokenized_text.extend(self._tokenize(token, extra_tokenizer_stack=extra_tokenizer_stack))
818
+
819
+ # ["This", " is", " something", "<special_token_1>", "else"]
820
+ return tokenized_text
821
+
822
+ def _tokenize(self, text, **kwargs):
823
+ """
824
+ Modified from `transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer._tokenize`.
825
+
826
+ This adaptation supports domain-specific tokenizers.
827
+ """
828
+ extra_tokenizer_stack = kwargs.pop("extra_tokenizer_stack", False)
829
+ if extra_tokenizer_stack:
830
+ tokenized_text = extra_tokenizer_stack[-1].encode(text, out_type=str)
831
+ tokenized_id = extra_tokenizer_stack[-1].encode(text, out_type=int)
832
+ final_tokenized_text = []
833
+ for text_piece, id_piece in zip(tokenized_text, tokenized_id):
834
+ if id_piece == 0:
835
+ final_tokenized_text.extend(self._bpe_tokenize(text_piece))
836
+ else:
837
+ final_tokenized_text.append(text_piece)
838
+ return final_tokenized_text
839
+ else:
840
+ return self._bpe_tokenize(text)
841
+
842
+ def _bpe_tokenize(self, text, **kwargs):
843
+ text = text.replace(
844
+ "▁", " "
845
+ ) # This discrepancy stems from differing whitespace treatment in SentencePiece versus BPE tokenization.
846
+ bpe_tokens = []
847
+ for token in re.findall(self.pat, text):
848
+ token = "".join(
849
+ self.byte_encoder[b] for b in token.encode("utf-8")
850
+ ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case)
851
+ bpe_tokens.extend(bpe_token for bpe_token in self.bpe(token).split(" "))
852
+ return bpe_tokens
853
+
854
+ def convert_tokens_to_ids(self, tokens: Union[str, list[str]]) -> Union[int, list[int]]:
855
+ """
856
+ Modified from `transformers.tokenization_utils.PreTrainedTokenzier.convert_tokens_to_ids`.
857
+
858
+ Converts a token string (or a sequence of tokens) in a single integer id (or a sequence of ids), using the
859
+ vocabulary.
860
+
861
+ This adaptation supports domain-specific tokenizers.
862
+
863
+ Args:
864
+ tokens (`str` or `List[str]`): One or several token(s) to convert to token id(s).
865
+
866
+ Returns:
867
+ `int` or `List[int]`: The token id or list of token ids.
868
+ """
869
+ if tokens is None:
870
+ return None
871
+
872
+ if isinstance(tokens, str):
873
+ return self._convert_token_to_id_with_added_voc(tokens)
874
+
875
+ ids = []
876
+ extra_tokenizer_stack = []
877
+
878
+ for token in tokens:
879
+ if token not in self.ex_auto_begin_mapping and token not in self.ex_auto_end_mapping:
880
+ ids.append(
881
+ self._convert_token_to_id_with_added_voc(token, extra_tokenizer_stack=extra_tokenizer_stack)
882
+ )
883
+ if token in self.ex_all_begin_mapping:
884
+ extra_tokenizer_stack.append(self.ex_all_begin_mapping[token])
885
+ elif token in self.ex_all_end_mapping:
886
+ if extra_tokenizer_stack:
887
+ self._pop_logical_sp_token(extra_tokenizer_stack, token)
888
+ return ids
889
+
890
+ def _convert_token_to_id_with_added_voc(self, token, **kwargs):
891
+ """
892
+ Modified from `transformers.tokenization_utils.PreTrainedTokenzier._convert_token_to_id_with_added_voc`.
893
+
894
+ This adaptation supports domain-specific tokenizers.
895
+ """
896
+ if token is None:
897
+ return None
898
+
899
+ if token in self._added_tokens_encoder:
900
+ return self._added_tokens_encoder[token]
901
+ return self._convert_token_to_id(token, **kwargs)
902
+
903
+ def _convert_token_to_id(self, token, **kwargs):
904
+ """
905
+ Modified from `transformers.tokenization_utils.PreTrainedTokenzier._convert_token_to_id`.
906
+
907
+ Converts a token (str) in an id using the vocab.
908
+
909
+ Fall back to original tokenizer once OOV.
910
+ """
911
+ extra_tokenizer_stack = kwargs.pop("extra_tokenizer_stack", False)
912
+ if extra_tokenizer_stack:
913
+ token_id = extra_tokenizer_stack[-1].piece_to_id(token)
914
+ if token_id == extra_tokenizer_stack[-1].unk_id():
915
+ return self.encoder.get(token, self.encoder.get(self._unk_token))
916
+ else:
917
+ return token_id + extra_tokenizer_stack[-1].offset
918
+ else:
919
+ return self.encoder.get(token, self.encoder.get(self._unk_token))
920
+
921
+ # Copied from transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer._convert_id_to_token
922
+ def _convert_id_to_token(self, index):
923
+ """Converts an index (integer) in a token (str) using the vocab."""
924
+ return self.decoder.get(index)
925
+
926
+ def convert_tokens_to_string(self, tokens):
927
+ """Converts a sequence of tokens (string) in a single string."""
928
+ text = "".join(tokens)
929
+ text = text.replace(
930
+ "▁", "Ġ"
931
+ ) # This discrepancy stems from differing whitespace treatment in SentencePiece versus BPE tokenization.
932
+ text = text.replace("\n", "Ċ")
933
+ text = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8", errors=self.errors)
934
+ return text
935
+
936
+ def decode(
937
+ self,
938
+ token_ids,
939
+ skip_special_tokens: bool = False,
940
+ clean_up_tokenization_spaces: Optional[bool] = False,
941
+ spaces_between_special_tokens: bool = False,
942
+ **kwargs,
943
+ ) -> str:
944
+ # `spaces_between_special_tokens` defaults to True for _decode in slow tokenizers
945
+ # and cannot be configured elsewhere, but it should default to False for InternS1Tokenizer
946
+ return super().decode(
947
+ token_ids,
948
+ skip_special_tokens=skip_special_tokens,
949
+ clean_up_tokenization_spaces=clean_up_tokenization_spaces,
950
+ spaces_between_special_tokens=spaces_between_special_tokens,
951
+ **kwargs,
952
+ )
953
+
954
+ def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]:
955
+ """
956
+ Modified from `transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer.save_vocabulary` to support saving custom extension.
957
+ """
958
+ if not os.path.isdir(save_directory):
959
+ logger.error(f"Vocabulary path ({save_directory}) should be a directory")
960
+ return
961
+ vocab_file = os.path.join(
962
+ save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
963
+ )
964
+ merge_file = os.path.join(
965
+ save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"]
966
+ )
967
+ sp_model_smiles = os.path.join(
968
+ save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["sp_model_SMILES"]
969
+ )
970
+ sp_model_prot = os.path.join(
971
+ save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["sp_model_PROT"]
972
+ )
973
+ sp_model_xna = os.path.join(
974
+ save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["sp_model_XNA"]
975
+ )
976
+
977
+ with open(vocab_file, "w", encoding="utf-8") as f:
978
+ f.write(json.dumps(self.encoder, indent=2, sort_keys=True, ensure_ascii=False) + "\n")
979
+
980
+ index = 0
981
+ with open(merge_file, "w", encoding="utf-8") as writer:
982
+ writer.write("#version: 0.2\n")
983
+ for bpe_tokens, token_index in sorted(self.bpe_ranks.items(), key=lambda kv: kv[1]):
984
+ if index != token_index:
985
+ logger.warning(
986
+ f"Saving vocabulary to {merge_file}: BPE merge indices are not consecutive."
987
+ " Please check that the tokenizer is not corrupted!"
988
+ )
989
+ index = token_index
990
+ writer.write(" ".join(bpe_tokens) + "\n")
991
+ index += 1
992
+
993
+ with open(sp_model_smiles, "wb") as f:
994
+ f.write(self.sp_model_SMILES.serialized_model_proto())
995
+
996
+ with open(sp_model_prot, "wb") as f:
997
+ f.write(self.sp_model_PROT.serialized_model_proto())
998
+
999
+ with open(sp_model_xna, "wb") as f:
1000
+ f.write(self.sp_model_XNA.serialized_model_proto())
1001
+
1002
+ return vocab_file, merge_file
1003
+
1004
+ def prepare_for_tokenization(self, text, **kwargs):
1005
+ text = unicodedata.normalize("NFC", text)
1006
+ return (text, kwargs)
1007
+
1008
+
1009
+ __all__ = ["InternS1Tokenizer"]
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5f9e4d4901a92b997e463c1f46055088b6cca5ca61a6522d1b9f64c4bb81cb42
3
+ size 12807982
tokenizer_PROT.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1144f52f86f3ca5a29940d69b037e508c05a89e6eedbe42bea641e226b20dbe0
3
+ size 12118
tokenizer_SMILES.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fba1c97da0353ccbffd368ae78e311ccbc762aa5ba74f9aff8bf2ab363c4d37d
3
+ size 14775
tokenizer_XNA.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:58fc8bfb2af3dfe936a13dad8a9cb28dab7850b70b358db19605d867c133fb35
3
+ size 15451
tokenizer_config.json ADDED
@@ -0,0 +1,521 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": false,
3
+ "add_prefix_space": false,
4
+ "added_tokens_decoder": {
5
+ "248044": {
6
+ "content": "<|endoftext|>",
7
+ "lstrip": false,
8
+ "normalized": false,
9
+ "rstrip": false,
10
+ "single_word": false,
11
+ "special": true
12
+ },
13
+ "248045": {
14
+ "content": "<|im_start|>",
15
+ "lstrip": false,
16
+ "normalized": false,
17
+ "rstrip": false,
18
+ "single_word": false,
19
+ "special": true
20
+ },
21
+ "248046": {
22
+ "content": "<|im_end|>",
23
+ "lstrip": false,
24
+ "normalized": false,
25
+ "rstrip": false,
26
+ "single_word": false,
27
+ "special": true
28
+ },
29
+ "248047": {
30
+ "content": "<|object_ref_start|>",
31
+ "lstrip": false,
32
+ "normalized": false,
33
+ "rstrip": false,
34
+ "single_word": false,
35
+ "special": true
36
+ },
37
+ "248048": {
38
+ "content": "<|object_ref_end|>",
39
+ "lstrip": false,
40
+ "normalized": false,
41
+ "rstrip": false,
42
+ "single_word": false,
43
+ "special": true
44
+ },
45
+ "248049": {
46
+ "content": "<|box_start|>",
47
+ "lstrip": false,
48
+ "normalized": false,
49
+ "rstrip": false,
50
+ "single_word": false,
51
+ "special": true
52
+ },
53
+ "248050": {
54
+ "content": "<|box_end|>",
55
+ "lstrip": false,
56
+ "normalized": false,
57
+ "rstrip": false,
58
+ "single_word": false,
59
+ "special": true
60
+ },
61
+ "248051": {
62
+ "content": "<|quad_start|>",
63
+ "lstrip": false,
64
+ "normalized": false,
65
+ "rstrip": false,
66
+ "single_word": false,
67
+ "special": true
68
+ },
69
+ "248052": {
70
+ "content": "<|quad_end|>",
71
+ "lstrip": false,
72
+ "normalized": false,
73
+ "rstrip": false,
74
+ "single_word": false,
75
+ "special": true
76
+ },
77
+ "248053": {
78
+ "content": "<|vision_start|>",
79
+ "lstrip": false,
80
+ "normalized": false,
81
+ "rstrip": false,
82
+ "single_word": false,
83
+ "special": true
84
+ },
85
+ "248054": {
86
+ "content": "<|vision_end|>",
87
+ "lstrip": false,
88
+ "normalized": false,
89
+ "rstrip": false,
90
+ "single_word": false,
91
+ "special": true
92
+ },
93
+ "248055": {
94
+ "content": "<|vision_pad|>",
95
+ "lstrip": false,
96
+ "normalized": false,
97
+ "rstrip": false,
98
+ "single_word": false,
99
+ "special": true
100
+ },
101
+ "248056": {
102
+ "content": "<|image_pad|>",
103
+ "lstrip": false,
104
+ "normalized": false,
105
+ "rstrip": false,
106
+ "single_word": false,
107
+ "special": true
108
+ },
109
+ "248057": {
110
+ "content": "<|video_pad|>",
111
+ "lstrip": false,
112
+ "normalized": false,
113
+ "rstrip": false,
114
+ "single_word": false,
115
+ "special": true
116
+ },
117
+ "248058": {
118
+ "content": "<tool_call>",
119
+ "lstrip": false,
120
+ "normalized": false,
121
+ "rstrip": false,
122
+ "single_word": false,
123
+ "special": false
124
+ },
125
+ "248059": {
126
+ "content": "</tool_call>",
127
+ "lstrip": false,
128
+ "normalized": false,
129
+ "rstrip": false,
130
+ "single_word": false,
131
+ "special": false
132
+ },
133
+ "248060": {
134
+ "content": "<|fim_prefix|>",
135
+ "lstrip": false,
136
+ "normalized": false,
137
+ "rstrip": false,
138
+ "single_word": false,
139
+ "special": false
140
+ },
141
+ "248061": {
142
+ "content": "<|fim_middle|>",
143
+ "lstrip": false,
144
+ "normalized": false,
145
+ "rstrip": false,
146
+ "single_word": false,
147
+ "special": false
148
+ },
149
+ "248062": {
150
+ "content": "<|fim_suffix|>",
151
+ "lstrip": false,
152
+ "normalized": false,
153
+ "rstrip": false,
154
+ "single_word": false,
155
+ "special": false
156
+ },
157
+ "248063": {
158
+ "content": "<|fim_pad|>",
159
+ "lstrip": false,
160
+ "normalized": false,
161
+ "rstrip": false,
162
+ "single_word": false,
163
+ "special": false
164
+ },
165
+ "248064": {
166
+ "content": "<|repo_name|>",
167
+ "lstrip": false,
168
+ "normalized": false,
169
+ "rstrip": false,
170
+ "single_word": false,
171
+ "special": false
172
+ },
173
+ "248065": {
174
+ "content": "<|file_sep|>",
175
+ "lstrip": false,
176
+ "normalized": false,
177
+ "rstrip": false,
178
+ "single_word": false,
179
+ "special": false
180
+ },
181
+ "248066": {
182
+ "content": "<tool_response>",
183
+ "lstrip": false,
184
+ "normalized": false,
185
+ "rstrip": false,
186
+ "single_word": false,
187
+ "special": false
188
+ },
189
+ "248067": {
190
+ "content": "</tool_response>",
191
+ "lstrip": false,
192
+ "normalized": false,
193
+ "rstrip": false,
194
+ "single_word": false,
195
+ "special": false
196
+ },
197
+ "248068": {
198
+ "content": "<think>",
199
+ "lstrip": false,
200
+ "normalized": false,
201
+ "rstrip": false,
202
+ "single_word": false,
203
+ "special": false
204
+ },
205
+ "248069": {
206
+ "content": "</think>",
207
+ "lstrip": false,
208
+ "normalized": false,
209
+ "rstrip": false,
210
+ "single_word": false,
211
+ "special": false
212
+ },
213
+ "248070": {
214
+ "content": "<|audio_start|>",
215
+ "lstrip": false,
216
+ "normalized": false,
217
+ "rstrip": false,
218
+ "single_word": false,
219
+ "special": true
220
+ },
221
+ "248071": {
222
+ "content": "<|audio_end|>",
223
+ "lstrip": false,
224
+ "normalized": false,
225
+ "rstrip": false,
226
+ "single_word": false,
227
+ "special": true
228
+ },
229
+ "248072": {
230
+ "content": "<tts_pad>",
231
+ "lstrip": false,
232
+ "normalized": false,
233
+ "rstrip": false,
234
+ "single_word": false,
235
+ "special": true
236
+ },
237
+ "248073": {
238
+ "content": "<tts_text_bos>",
239
+ "lstrip": false,
240
+ "normalized": false,
241
+ "rstrip": false,
242
+ "single_word": false,
243
+ "special": true
244
+ },
245
+ "248074": {
246
+ "content": "<tts_text_eod>",
247
+ "lstrip": false,
248
+ "normalized": false,
249
+ "rstrip": false,
250
+ "single_word": false,
251
+ "special": true
252
+ },
253
+ "248075": {
254
+ "content": "<tts_text_bos_single>",
255
+ "lstrip": false,
256
+ "normalized": false,
257
+ "rstrip": false,
258
+ "single_word": false,
259
+ "special": true
260
+ },
261
+ "248076": {
262
+ "content": "<|audio_pad|>",
263
+ "lstrip": false,
264
+ "normalized": false,
265
+ "rstrip": false,
266
+ "single_word": false,
267
+ "special": true
268
+ },
269
+ "248077": {
270
+ "content": "<IMG_CONTEXT>",
271
+ "lstrip": false,
272
+ "normalized": false,
273
+ "rstrip": false,
274
+ "single_word": false,
275
+ "special": true
276
+ },
277
+ "248078": {
278
+ "content": "<img>",
279
+ "lstrip": false,
280
+ "normalized": false,
281
+ "rstrip": false,
282
+ "single_word": false,
283
+ "special": true
284
+ },
285
+ "248079": {
286
+ "content": "</img>",
287
+ "lstrip": false,
288
+ "normalized": false,
289
+ "rstrip": false,
290
+ "single_word": false,
291
+ "special": true
292
+ },
293
+ "248080": {
294
+ "content": "<quad>",
295
+ "lstrip": false,
296
+ "normalized": false,
297
+ "rstrip": false,
298
+ "single_word": false,
299
+ "special": true
300
+ },
301
+ "248081": {
302
+ "content": "</quad>",
303
+ "lstrip": false,
304
+ "normalized": false,
305
+ "rstrip": false,
306
+ "single_word": false,
307
+ "special": true
308
+ },
309
+ "248082": {
310
+ "content": "<ref>",
311
+ "lstrip": false,
312
+ "normalized": false,
313
+ "rstrip": false,
314
+ "single_word": false,
315
+ "special": true
316
+ },
317
+ "248083": {
318
+ "content": "</ref>",
319
+ "lstrip": false,
320
+ "normalized": false,
321
+ "rstrip": false,
322
+ "single_word": false,
323
+ "special": true
324
+ },
325
+ "248084": {
326
+ "content": "<box>",
327
+ "lstrip": false,
328
+ "normalized": false,
329
+ "rstrip": false,
330
+ "single_word": false,
331
+ "special": true
332
+ },
333
+ "248085": {
334
+ "content": "</box>",
335
+ "lstrip": false,
336
+ "normalized": false,
337
+ "rstrip": false,
338
+ "single_word": false,
339
+ "special": true
340
+ },
341
+ "248086": {
342
+ "content": "<|action_start|>",
343
+ "lstrip": false,
344
+ "normalized": false,
345
+ "rstrip": false,
346
+ "single_word": false,
347
+ "special": true
348
+ },
349
+ "248087": {
350
+ "content": "<|action_end|>",
351
+ "lstrip": false,
352
+ "normalized": false,
353
+ "rstrip": false,
354
+ "single_word": false,
355
+ "special": true
356
+ },
357
+ "248088": {
358
+ "content": "<|interpreter|>",
359
+ "lstrip": false,
360
+ "normalized": false,
361
+ "rstrip": false,
362
+ "single_word": false,
363
+ "special": true
364
+ },
365
+ "248089": {
366
+ "content": "<|plugin|>",
367
+ "lstrip": false,
368
+ "normalized": false,
369
+ "rstrip": false,
370
+ "single_word": false,
371
+ "special": true
372
+ },
373
+ "248090": {
374
+ "content": "<video>",
375
+ "lstrip": false,
376
+ "normalized": false,
377
+ "rstrip": false,
378
+ "single_word": false,
379
+ "special": true
380
+ },
381
+ "248091": {
382
+ "content": "<|ts|>",
383
+ "lstrip": false,
384
+ "normalized": false,
385
+ "rstrip": false,
386
+ "single_word": false,
387
+ "special": true
388
+ },
389
+ "248092": {
390
+ "content": "<|/ts|>",
391
+ "lstrip": false,
392
+ "normalized": false,
393
+ "rstrip": false,
394
+ "single_word": false,
395
+ "special": true
396
+ },
397
+ "248093": {
398
+ "content": "<TS_CONTEXT>",
399
+ "lstrip": false,
400
+ "normalized": false,
401
+ "rstrip": false,
402
+ "single_word": false,
403
+ "special": true
404
+ },
405
+ "248094": {
406
+ "content": "<SMILES>",
407
+ "lstrip": false,
408
+ "normalized": false,
409
+ "rstrip": false,
410
+ "single_word": false,
411
+ "special": false
412
+ },
413
+ "248095": {
414
+ "content": "</SMILES>",
415
+ "lstrip": false,
416
+ "normalized": false,
417
+ "rstrip": false,
418
+ "single_word": false,
419
+ "special": false
420
+ },
421
+ "248096": {
422
+ "content": "<protein>",
423
+ "lstrip": false,
424
+ "normalized": false,
425
+ "rstrip": false,
426
+ "single_word": false,
427
+ "special": false
428
+ },
429
+ "248097": {
430
+ "content": "</protein>",
431
+ "lstrip": false,
432
+ "normalized": false,
433
+ "rstrip": false,
434
+ "single_word": false,
435
+ "special": false
436
+ },
437
+ "248098": {
438
+ "content": "<dna>",
439
+ "lstrip": false,
440
+ "normalized": false,
441
+ "rstrip": false,
442
+ "single_word": false,
443
+ "special": false
444
+ },
445
+ "248099": {
446
+ "content": "</dna>",
447
+ "lstrip": false,
448
+ "normalized": false,
449
+ "rstrip": false,
450
+ "single_word": false,
451
+ "special": false
452
+ },
453
+ "248100": {
454
+ "content": "<rna>",
455
+ "lstrip": false,
456
+ "normalized": false,
457
+ "rstrip": false,
458
+ "single_word": false,
459
+ "special": false
460
+ },
461
+ "248101": {
462
+ "content": "</rna>",
463
+ "lstrip": false,
464
+ "normalized": false,
465
+ "rstrip": false,
466
+ "single_word": false,
467
+ "special": false
468
+ }
469
+ },
470
+ "additional_special_tokens": [
471
+ "<|im_start|>",
472
+ "<|im_end|>",
473
+ "<|object_ref_start|>",
474
+ "<|object_ref_end|>",
475
+ "<|box_start|>",
476
+ "<|box_end|>",
477
+ "<|quad_start|>",
478
+ "<|quad_end|>",
479
+ "<|vision_start|>",
480
+ "<|vision_end|>",
481
+ "<|vision_pad|>",
482
+ "<|image_pad|>",
483
+ "<|video_pad|>"
484
+ ],
485
+ "audio_bos_token": "<|audio_start|>",
486
+ "audio_eos_token": "<|audio_end|>",
487
+ "audio_token": "<|audio_pad|>",
488
+ "auto_map": {
489
+ "AutoTokenizer": [
490
+ "tokenization_interns1.InternS1Tokenizer",
491
+ null
492
+ ]
493
+ },
494
+ "bos_token": "<|im_start|>",
495
+ "clean_up_tokenization_spaces": false,
496
+ "eos_token": "<|im_end|>",
497
+ "errors": "replace",
498
+ "extra_special_tokens": {
499
+ "audio_bos_token": "<|audio_start|>",
500
+ "audio_eos_token": "<|audio_end|>",
501
+ "audio_token": "<|audio_pad|>",
502
+ "image_token": "<|image_pad|>",
503
+ "video_token": "<|video_pad|>",
504
+ "vision_bos_token": "<|vision_start|>",
505
+ "vision_eos_token": "<|vision_end|>"
506
+ },
507
+ "image_token": "<|image_pad|>",
508
+ "model_max_length": 262144,
509
+ "offset_PROT": 249126,
510
+ "offset_SMILES": 248102,
511
+ "offset_XNA": 250150,
512
+ "pad_token": "<|endoftext|>",
513
+ "pretokenize_regex": "(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\\r\\n\\p{L}\\p{N}]?[\\p{L}\\p{M}]+|\\p{N}| ?[^\\s\\p{L}\\p{M}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+",
514
+ "special_tokens_pattern": "none",
515
+ "split_special_tokens": false,
516
+ "tokenizer_class": "InternS1Tokenizer",
517
+ "unk_token": null,
518
+ "video_token": "<|video_pad|>",
519
+ "vision_bos_token": "<|vision_start|>",
520
+ "vision_eos_token": "<|vision_end|>"
521
+ }
video_preprocessor_config.json ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "size": {
3
+ "longest_edge": 25165824,
4
+ "shortest_edge": 4096
5
+ },
6
+ "patch_size": 16,
7
+ "temporal_patch_size": 2,
8
+ "merge_size": 2,
9
+ "image_mean": [
10
+ 0.5,
11
+ 0.5,
12
+ 0.5
13
+ ],
14
+ "image_std": [
15
+ 0.5,
16
+ 0.5,
17
+ 0.5
18
+ ],
19
+ "processor_class": "Qwen3VLProcessor",
20
+ "video_processor_type": "Qwen3VLVideoProcessor"
21
+ }
vocab.json ADDED
The diff for this file is too large to render. See raw diff