Upload folder using huggingface_hub
Browse files- .gitattributes +4 -0
- LICENSE +202 -0
- README.md +152 -1
- assets/intern-s2-preview-397b-memdec-arch.pdf +3 -0
- assets/intern-s2-preview-397b-memdec-arch.png +3 -0
- assets/radar_memory_decoder_combined.png +3 -0
- chat_template.jinja +154 -0
- config.json +104 -0
- memory_fusion/config.json +11 -0
- memory_fusion/routers/intern-s2-397b/router_config.json +8 -0
- memory_fusion/routers/intern-s2-397b/router_epoch_1.pt +3 -0
- memory_fusion/routers/intern-s2-397b/router_epoch_1.safetensors +3 -0
- merges.txt +0 -0
- model.safetensors +3 -0
- preprocessor_config.json +21 -0
- tokenization_interns1.py +1008 -0
- tokenizer.json +3 -0
- tokenizer_PROT.model +3 -0
- tokenizer_SMILES.model +3 -0
- tokenizer_XNA.model +3 -0
- tokenizer_config.json +514 -0
- video_preprocessor_config.json +21 -0
- vocab.json +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,7 @@ 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 |
+
assets/intern-s2-preview-397b-memdec-arch.pdf filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
assets/intern-s2-preview-397b-memdec-arch.png filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
assets/radar_memory_decoder_combined.png filter=lfs diff=lfs merge=lfs -text
|
LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
Apache License
|
| 3 |
+
Version 2.0, January 2004
|
| 4 |
+
http://www.apache.org/licenses/
|
| 5 |
+
|
| 6 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
| 7 |
+
|
| 8 |
+
1. Definitions.
|
| 9 |
+
|
| 10 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
| 11 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
| 12 |
+
|
| 13 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
| 14 |
+
the copyright owner that is granting the License.
|
| 15 |
+
|
| 16 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
| 17 |
+
other entities that control, are controlled by, or are under common
|
| 18 |
+
control with that entity. For the purposes of this definition,
|
| 19 |
+
"control" means (i) the power, direct or indirect, to cause the
|
| 20 |
+
direction or management of such entity, whether by contract or
|
| 21 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
| 22 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
| 23 |
+
|
| 24 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
| 25 |
+
exercising permissions granted by this License.
|
| 26 |
+
|
| 27 |
+
"Source" form shall mean the preferred form for making modifications,
|
| 28 |
+
including but not limited to software source code, documentation
|
| 29 |
+
source, and configuration files.
|
| 30 |
+
|
| 31 |
+
"Object" form shall mean any form resulting from mechanical
|
| 32 |
+
transformation or translation of a Source form, including but
|
| 33 |
+
not limited to compiled object code, generated documentation,
|
| 34 |
+
and conversions to other media types.
|
| 35 |
+
|
| 36 |
+
"Work" shall mean the work of authorship, whether in Source or
|
| 37 |
+
Object form, made available under the License, as indicated by a
|
| 38 |
+
copyright notice that is included in or attached to the work
|
| 39 |
+
(an example is provided in the Appendix below).
|
| 40 |
+
|
| 41 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
| 42 |
+
form, that is based on (or derived from) the Work and for which the
|
| 43 |
+
editorial revisions, annotations, elaborations, or other modifications
|
| 44 |
+
represent, as a whole, an original work of authorship. For the purposes
|
| 45 |
+
of this License, Derivative Works shall not include works that remain
|
| 46 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
| 47 |
+
the Work and Derivative Works thereof.
|
| 48 |
+
|
| 49 |
+
"Contribution" shall mean any work of authorship, including
|
| 50 |
+
the original version of the Work and any modifications or additions
|
| 51 |
+
to that Work or Derivative Works thereof, that is intentionally
|
| 52 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
| 53 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
| 54 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
| 55 |
+
means any form of electronic, verbal, or written communication sent
|
| 56 |
+
to the Licensor or its representatives, including but not limited to
|
| 57 |
+
communication on electronic mailing lists, source code control systems,
|
| 58 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
| 59 |
+
Licensor for the purpose of discussing and improving the Work, but
|
| 60 |
+
excluding communication that is conspicuously marked or otherwise
|
| 61 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
| 62 |
+
|
| 63 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
| 64 |
+
on behalf of whom a Contribution has been received by Licensor and
|
| 65 |
+
subsequently incorporated within the Work.
|
| 66 |
+
|
| 67 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
| 68 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 69 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 70 |
+
copyright license to reproduce, prepare Derivative Works of,
|
| 71 |
+
publicly display, publicly perform, sublicense, and distribute the
|
| 72 |
+
Work and such Derivative Works in Source or Object form.
|
| 73 |
+
|
| 74 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
| 75 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 76 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 77 |
+
(except as stated in this section) patent license to make, have made,
|
| 78 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
| 79 |
+
where such license applies only to those patent claims licensable
|
| 80 |
+
by such Contributor that are necessarily infringed by their
|
| 81 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
| 82 |
+
with the Work to which such Contribution(s) was submitted. If You
|
| 83 |
+
institute patent litigation against any entity (including a
|
| 84 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
| 85 |
+
or a Contribution incorporated within the Work constitutes direct
|
| 86 |
+
or contributory patent infringement, then any patent licenses
|
| 87 |
+
granted to You under this License for that Work shall terminate
|
| 88 |
+
as of the date such litigation is filed.
|
| 89 |
+
|
| 90 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
| 91 |
+
Work or Derivative Works thereof in any medium, with or without
|
| 92 |
+
modifications, and in Source or Object form, provided that You
|
| 93 |
+
meet the following conditions:
|
| 94 |
+
|
| 95 |
+
(a) You must give any other recipients of the Work or
|
| 96 |
+
Derivative Works a copy of this License; and
|
| 97 |
+
|
| 98 |
+
(b) You must cause any modified files to carry prominent notices
|
| 99 |
+
stating that You changed the files; and
|
| 100 |
+
|
| 101 |
+
(c) You must retain, in the Source form of any Derivative Works
|
| 102 |
+
that You distribute, all copyright, patent, trademark, and
|
| 103 |
+
attribution notices from the Source form of the Work,
|
| 104 |
+
excluding those notices that do not pertain to any part of
|
| 105 |
+
the Derivative Works; and
|
| 106 |
+
|
| 107 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
| 108 |
+
distribution, then any Derivative Works that You distribute must
|
| 109 |
+
include a readable copy of the attribution notices contained
|
| 110 |
+
within such NOTICE file, excluding those notices that do not
|
| 111 |
+
pertain to any part of the Derivative Works, in at least one
|
| 112 |
+
of the following places: within a NOTICE text file distributed
|
| 113 |
+
as part of the Derivative Works; within the Source form or
|
| 114 |
+
documentation, if provided along with the Derivative Works; or,
|
| 115 |
+
within a display generated by the Derivative Works, if and
|
| 116 |
+
wherever such third-party notices normally appear. The contents
|
| 117 |
+
of the NOTICE file are for informational purposes only and
|
| 118 |
+
do not modify the License. You may add Your own attribution
|
| 119 |
+
notices within Derivative Works that You distribute, alongside
|
| 120 |
+
or as an addendum to the NOTICE text from the Work, provided
|
| 121 |
+
that such additional attribution notices cannot be construed
|
| 122 |
+
as modifying the License.
|
| 123 |
+
|
| 124 |
+
You may add Your own copyright statement to Your modifications and
|
| 125 |
+
may provide additional or different license terms and conditions
|
| 126 |
+
for use, reproduction, or distribution of Your modifications, or
|
| 127 |
+
for any such Derivative Works as a whole, provided Your use,
|
| 128 |
+
reproduction, and distribution of the Work otherwise complies with
|
| 129 |
+
the conditions stated in this License.
|
| 130 |
+
|
| 131 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
| 132 |
+
any Contribution intentionally submitted for inclusion in the Work
|
| 133 |
+
by You to the Licensor shall be under the terms and conditions of
|
| 134 |
+
this License, without any additional terms or conditions.
|
| 135 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
| 136 |
+
the terms of any separate license agreement you may have executed
|
| 137 |
+
with Licensor regarding such Contributions.
|
| 138 |
+
|
| 139 |
+
6. Trademarks. This License does not grant permission to use the trade
|
| 140 |
+
names, trademarks, service marks, or product names of the Licensor,
|
| 141 |
+
except as required for reasonable and customary use in describing the
|
| 142 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
| 143 |
+
|
| 144 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
| 145 |
+
agreed to in writing, Licensor provides the Work (and each
|
| 146 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
| 147 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 148 |
+
implied, including, without limitation, any warranties or conditions
|
| 149 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
| 150 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
| 151 |
+
appropriateness of using or redistributing the Work and assume any
|
| 152 |
+
risks associated with Your exercise of permissions under this License.
|
| 153 |
+
|
| 154 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
| 155 |
+
whether in tort (including negligence), contract, or otherwise,
|
| 156 |
+
unless required by applicable law (such as deliberate and grossly
|
| 157 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
| 158 |
+
liable to You for damages, including any direct, indirect, special,
|
| 159 |
+
incidental, or consequential damages of any character arising as a
|
| 160 |
+
result of this License or out of the use or inability to use the
|
| 161 |
+
Work (including but not limited to damages for loss of goodwill,
|
| 162 |
+
work stoppage, computer failure or malfunction, or any and all
|
| 163 |
+
other commercial damages or losses), even if such Contributor
|
| 164 |
+
has been advised of the possibility of such damages.
|
| 165 |
+
|
| 166 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
| 167 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
| 168 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
| 169 |
+
or other liability obligations and/or rights consistent with this
|
| 170 |
+
License. However, in accepting such obligations, You may act only
|
| 171 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
| 172 |
+
of any other Contributor, and only if You agree to indemnify,
|
| 173 |
+
defend, and hold each Contributor harmless for any liability
|
| 174 |
+
incurred by, or claims asserted against, such Contributor by reason
|
| 175 |
+
of your accepting any such warranty or additional liability.
|
| 176 |
+
|
| 177 |
+
END OF TERMS AND CONDITIONS
|
| 178 |
+
|
| 179 |
+
APPENDIX: How to apply the Apache License to your work.
|
| 180 |
+
|
| 181 |
+
To apply the Apache License to your work, attach the following
|
| 182 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
| 183 |
+
replaced with your own identifying information. (Don't include
|
| 184 |
+
the brackets!) The text should be enclosed in the appropriate
|
| 185 |
+
comment syntax for the file format. We also recommend that a
|
| 186 |
+
file or class name and description of purpose be included on the
|
| 187 |
+
same "printed page" as the copyright notice for easier
|
| 188 |
+
identification within third-party archives.
|
| 189 |
+
|
| 190 |
+
Copyright 2026 Alibaba Cloud
|
| 191 |
+
|
| 192 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 193 |
+
you may not use this file except in compliance with the License.
|
| 194 |
+
You may obtain a copy of the License at
|
| 195 |
+
|
| 196 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 197 |
+
|
| 198 |
+
Unless required by applicable law or agreed to in writing, software
|
| 199 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 200 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 201 |
+
See the License for the specific language governing permissions and
|
| 202 |
+
limitations under the License.
|
README.md
CHANGED
|
@@ -1,3 +1,154 @@
|
|
| 1 |
---
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
tags:
|
| 3 |
+
- intern-s2
|
| 4 |
+
- memory-decoder
|
| 5 |
+
- biology
|
| 6 |
+
- scientific-ai
|
| 7 |
+
- domain-adaptation
|
| 8 |
---
|
| 9 |
+
|
| 10 |
+
# Intern-MemDec-4B
|
| 11 |
+
|
| 12 |
+
<p align="center">
|
| 13 |
+
<img src="https://huggingface.co/internlm/Intern-S2-Preview-397B/resolve/main/figs/title.png" alt="Intern-S2" width="100%">
|
| 14 |
+
</p>
|
| 15 |
+
|
| 16 |
+
<p align="center">
|
| 17 |
+
<a href="https://github.com/InternLM/Intern-S1">💻 GitHub Repo</a> •
|
| 18 |
+
<a href="https://huggingface.co/collections/internlm/intern-s2">🤗 Model Collections</a> •
|
| 19 |
+
<a href="https://arxiv.org/abs/2607.25614">📖 MemSFT Paper</a>
|
| 20 |
+
</p>
|
| 21 |
+
|
| 22 |
+
## Introduction
|
| 23 |
+
|
| 24 |
+
We introduce Intern-MemDec-4B, a memory decoder designed to extend Intern-S2 backbone models with specialized domain knowledge and capabilities. It is an auxiliary model component rather than a standalone language model: during inference, an Intern-S2 backbone and the memory decoder process the same context in parallel, while a lightweight token-level router dynamically combines their predictions.
|
| 25 |
+
|
| 26 |
+
Scientific knowledge is long-tailed and continuously evolving. A fixed post-trained foundation model cannot fully cover every specialized subfield, task protocol, or newly emerging discovery. Updating the entire backbone for each domain can also disturb its general reasoning, instruction-following, multimodal, and agentic capabilities. Intern-MemDec-4B instead provides a modular domain-extension mechanism. The Intern-S2 backbone remains intact and serves as the primary reasoning engine, while the attached memory supplies additional biological knowledge and recurring task patterns when they are useful.
|
| 27 |
+
|
| 28 |
+
Intern-MemDec-4B is trained by compressing retrieval-based evidence from domain data into a reusable parametric module. At inference time, it does not require access to the original retrieval datastore.
|
| 29 |
+
|
| 30 |
+
> **Important:** Intern-MemDec-4B is not intended to be loaded or used as a standalone chat model. It must be deployed together with a compatible Intern-S2 backbone and the corresponding fusion configuration or router.
|
| 31 |
+
|
| 32 |
+
## Features
|
| 33 |
+
|
| 34 |
+
- **Modular Domain Extension.** Intern-MemDec-4B adds specialized domain knowledge and task capabilities without updating the Intern-S2 backbone parameters. By turning domain adaptation into memory attachment rather than backbone rewriting, it provides a practical way to extend a general scientific foundation model with expertise in DNA, RNA, proteins, and biomolecular interactions while keeping the original backbone unchanged.
|
| 35 |
+
- **Adaptive Fusion While Preserving General Capabilities.** The Intern-S2 backbone and the biological memory process the same decoding context in parallel, and a lightweight token-level router dynamically determines their contributions to each next-token prediction. The memory branch can strengthen specialized predictions on biology-related inputs, while the backbone remains dominant for general reasoning, instruction following, factual knowledge, multimodal understanding, and agentic tasks, reducing interference with the model's original capabilities.
|
| 36 |
+
- **Reusable and Extensible Parametric Memory.** Domain evidence is compressed into a reusable parametric module, so online inference does not require the original retrieval datastore. Once trained, the memory can be attached to compatible Intern-S2 backbones using a matching router or fusion configuration. The same approach can also be applied to up-to-date data from other target domains, enabling independently trained memories to continually introduce new knowledge and capabilities as scientific fields evolve.
|
| 37 |
+
|
| 38 |
+
## How It Works
|
| 39 |
+
|
| 40 |
+
For every decoding step, the Intern-S2 backbone and Intern-MemDec-4B receive the same input context and independently produce next-token predictions. A lightweight router examines signals from both branches and determines how much each should contribute to the final prediction.
|
| 41 |
+
|
| 42 |
+
On biology-related inputs, the router can increase the contribution of the memory branch to strengthen specialized predictions. On general or unrelated inputs, it can keep the backbone dominant so that the original reasoning, instruction-following, factual, multimodal, and agentic behavior is retained. New domain capabilities can therefore be added as independently trained, plug-and-play memories rather than through repeated full-model fine-tuning.
|
| 43 |
+
|
| 44 |
+

|
| 45 |
+
|
| 46 |
+
## Performance
|
| 47 |
+
|
| 48 |
+
We evaluate Intern-MemDec-4B with Intern-S2-Preview-397B on all 21 tasks in Biology-Instructions. The attached biological memory improves the average score from **56.92** to **60.32** (**+3.40** points). The evaluation in the Technical Report also shows a broadly comparable capability profile on general and multimodal benchmarks, supporting the intended use of Memory Decoder as a domain-extension interface that preserves the foundation model's general behavior.
|
| 49 |
+
|
| 50 |
+

|
| 51 |
+
|
| 52 |
+
<table width="100%">
|
| 53 |
+
<tr>
|
| 54 |
+
<td width="50%" align="center"><b>(a) Biology-Instructions category performance</b></td>
|
| 55 |
+
<td width="50%" align="center"><b>(b) Cross-domain capability profile</b></td>
|
| 56 |
+
</tr>
|
| 57 |
+
</table>
|
| 58 |
+
|
| 59 |
+
### Biology-Instructions
|
| 60 |
+
|
| 61 |
+
All scores are reported on a 0–100 scale.
|
| 62 |
+
|
| 63 |
+
| Biology-Instructions task | Intern-S2-Preview-397B | + Intern-MemDec-4B |
|
| 64 |
+
|---|---:|---:|
|
| 65 |
+
| DNA-cpd | 63.11 | 72.57 |
|
| 66 |
+
| DNA-emp | 19.95 | 27.25 |
|
| 67 |
+
| DNA-enhancer activity | 53.68 | 60.71 |
|
| 68 |
+
| DNA-pd | 84.40 | 89.12 |
|
| 69 |
+
| DNA-tf-h | 56.57 | 55.99 |
|
| 70 |
+
| DNA-tf-m | 56.96 | 67.09 |
|
| 71 |
+
| Multi-sequence antibody-antigen | 40.24 | 36.44 |
|
| 72 |
+
| Multi-sequence promoter-enhancer interaction | 22.46 | 38.47 |
|
| 73 |
+
| Multi-sequence RNA-protein interaction | 84.74 | 87.34 |
|
| 74 |
+
| Multi-sequence siRNA efficiency | 63.05 | 60.63 |
|
| 75 |
+
| Protein-Fluorescence | 70.48 | 72.23 |
|
| 76 |
+
| Protein-FunctionEC | 61.88 | 60.10 |
|
| 77 |
+
| Protein-Solubility | 68.60 | 68.00 |
|
| 78 |
+
| Protein-Stability | 69.67 | 67.80 |
|
| 79 |
+
| Protein-Thermostability | 58.44 | 53.97 |
|
| 80 |
+
| RNA-CRISPROnTarget | 6.61 | 17.18 |
|
| 81 |
+
| RNA-Isoform | 82.65 | 84.81 |
|
| 82 |
+
| RNA-MeanRibosomeLoading | 56.20 | 59.71 |
|
| 83 |
+
| RNA-Modification | 59.64 | 60.48 |
|
| 84 |
+
| RNA-NoncodingRNAFamily | 78.80 | 85.70 |
|
| 85 |
+
| RNA-ProgrammableRNASwitches | 37.13 | 41.23 |
|
| 86 |
+
| **Average score** | **56.92** | **60.32** |
|
| 87 |
+
|
| 88 |
+
## Domain Coverage
|
| 89 |
+
|
| 90 |
+
This release covers the **biology domain only**. Its training and evaluation focus on biological sequence understanding and prediction tasks involving DNA, RNA, proteins, and biomolecular interactions. Representative task families include:
|
| 91 |
+
|
| 92 |
+
- DNA property, enhancer activity, and transcription-factor prediction;
|
| 93 |
+
- RNA modification, isoform, regulatory, and interaction prediction;
|
| 94 |
+
- protein function, solubility, stability, fluorescence, and thermostability prediction;
|
| 95 |
+
- antibody-antigen and other multi-sequence biomolecular interaction tasks.
|
| 96 |
+
|
| 97 |
+
Intern-MemDec-4B should not be interpreted as a general memory covering every scientific discipline. To extend Intern-S2 to another domain, a separate memory decoder can be trained on up-to-date, high-quality data from that target domain. The resulting memory can inject new domain knowledge and specialized capabilities into the backbone without rewriting the backbone parameters. This makes the memory interface suitable for continual domain extension as scientific data and task requirements evolve.
|
| 98 |
+
|
| 99 |
+
## Quick Start
|
| 100 |
+
|
| 101 |
+
We recommend using [LMDeploy](https://github.com/InternLM/lmdeploy) (`>= 0.15.0`) to deploy the model suite — the [internlm/Intern-S2-Preview-397B-FP8](https://huggingface.co/internlm/Intern-S2-Preview-397B-FP8) LLM and the [internlm/Intern-MemDec-4B](https://huggingface.co/internlm/Intern-MemDec-4B) memory model.
|
| 102 |
+
```shell
|
| 103 |
+
pip install "lmdeploy>=0.15.0"
|
| 104 |
+
```
|
| 105 |
+
On 4× H200 GPUs, launch the service with:
|
| 106 |
+
```shell
|
| 107 |
+
lmdeploy serve api_server internlm/Intern-S2-Preview-397B-FP8 \
|
| 108 |
+
--backend pytorch \
|
| 109 |
+
--tp 4 \
|
| 110 |
+
--trust-remote-code \
|
| 111 |
+
--hf-overrides '{"memory_model_path": "internlm/Intern-MemDec-4B"}'
|
| 112 |
+
```
|
| 113 |
+
The service exposes OpenAI-compatible APIs at `http://0.0.0.0:23333` by default. Query it with:
|
| 114 |
+
```shell
|
| 115 |
+
curl http://localhost:23333/v1/chat/completions \
|
| 116 |
+
-H "Content-Type: application/json" \
|
| 117 |
+
-d '{
|
| 118 |
+
"model": "internlm/Intern-S2-Preview-397B-FP8",
|
| 119 |
+
"messages": [{"role": "user", "content": "<PROTEIN> GSSGSSGPSKFIEGLRNEEATEGDTATLWCELSKAAPVEWRKGHETLRDGDRHSLRQDGSRCELQIRGLAVVDAGEYSCVCGQERTSATLTVRALPARFIESGPSSG </PROTEIN> What is the EC number associated with the enzymatic function of this protein? Please put the final enzyme within \\boxed{} using an EC number such as ECx.x.x.x, and separate multiple entries with commas."}]
|
| 120 |
+
}'
|
| 121 |
+
```
|
| 122 |
+
|
| 123 |
+
For this example, the model returns the predicted enzyme classes:
|
| 124 |
+
|
| 125 |
+
```text
|
| 126 |
+
\boxed{EC2.7.11.-,EC2.7.11.1}
|
| 127 |
+
```
|
| 128 |
+
|
| 129 |
+
## Compatibility
|
| 130 |
+
|
| 131 |
+
- Intern-MemDec-4B must be used with a supported Intern-S2 backbone through a Memory Decoder-enabled inference runtime. Currently, **Intern-S2-Preview-397B is the only supported backbone**.
|
| 132 |
+
- Although the memory checkpoint is designed to be reusable across compatible backbone scales, adaptive routers depend on the selected backbone-memory pair. Use only a router or fusion configuration explicitly released for that pair.
|
| 133 |
+
|
| 134 |
+
## Citation
|
| 135 |
+
|
| 136 |
+
```bibtex
|
| 137 |
+
@misc{wang2026memsftmitigatingalignmenttax,
|
| 138 |
+
title={MemSFT: Mitigating Alignment Tax with an External Parametric Memory},
|
| 139 |
+
author={Jiarui Wang and Xiang Shi and Jiaqi Cao and Rubin Wei and Xiquan Wang and Hao Sun and Jingzhi Wang and Zhiqi Yang and Qipeng Guo and Bowen Zhou and Zhouhan Lin},
|
| 140 |
+
year={2026},
|
| 141 |
+
eprint={2607.25614},
|
| 142 |
+
archivePrefix={arXiv},
|
| 143 |
+
primaryClass={cs.LG},
|
| 144 |
+
url={https://arxiv.org/abs/2607.25614},
|
| 145 |
+
}
|
| 146 |
+
@article{cao2026memory,
|
| 147 |
+
title={Memory decoder: A pretrained, plug-and-play memory for large language models},
|
| 148 |
+
author={Cao, Jiaqi and Wang, Jiarui and Wei, Rubin and Guo, Qipeng and Chen, Kai and Zhou, Bowen and Lin, Zhouhan},
|
| 149 |
+
journal={Advances in Neural Information Processing Systems},
|
| 150 |
+
volume={38},
|
| 151 |
+
pages={115487--115510},
|
| 152 |
+
year={2026}
|
| 153 |
+
}
|
| 154 |
+
```
|
assets/intern-s2-preview-397b-memdec-arch.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:29c2e8f236956f9afc6ec734b5cc5b056e90e75f814cf70a9f85a6501700485d
|
| 3 |
+
size 157493
|
assets/intern-s2-preview-397b-memdec-arch.png
ADDED
|
Git LFS Details
|
assets/radar_memory_decoder_combined.png
ADDED
|
Git LFS Details
|
chat_template.jinja
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 'text' in item %}
|
| 31 |
+
{{- item.text }}
|
| 32 |
+
{%- else %}
|
| 33 |
+
{{- raise_exception('Unexpected item type in content.') }}
|
| 34 |
+
{%- endif %}
|
| 35 |
+
{%- endfor %}
|
| 36 |
+
{%- elif content is none or content is undefined %}
|
| 37 |
+
{{- '' }}
|
| 38 |
+
{%- else %}
|
| 39 |
+
{{- raise_exception('Unexpected content type.') }}
|
| 40 |
+
{%- endif %}
|
| 41 |
+
{%- endmacro %}
|
| 42 |
+
{%- if not messages %}
|
| 43 |
+
{{- raise_exception('No messages provided.') }}
|
| 44 |
+
{%- endif %}
|
| 45 |
+
{%- if tools and tools is iterable and tools is not mapping %}
|
| 46 |
+
{{- '<|im_start|>system\n' }}
|
| 47 |
+
{{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
|
| 48 |
+
{%- for tool in tools %}
|
| 49 |
+
{{- "\n" }}
|
| 50 |
+
{{- tool | tojson }}
|
| 51 |
+
{%- endfor %}
|
| 52 |
+
{{- "\n</tools>" }}
|
| 53 |
+
{{- '\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>' }}
|
| 54 |
+
{%- if messages[0].role == 'system' %}
|
| 55 |
+
{%- set content = render_content(messages[0].content, false, true)|trim %}
|
| 56 |
+
{%- if content %}
|
| 57 |
+
{{- '\n\n' + content }}
|
| 58 |
+
{%- endif %}
|
| 59 |
+
{%- endif %}
|
| 60 |
+
{{- '<|im_end|>\n' }}
|
| 61 |
+
{%- else %}
|
| 62 |
+
{%- if messages[0].role == 'system' %}
|
| 63 |
+
{%- set content = render_content(messages[0].content, false, true)|trim %}
|
| 64 |
+
{{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
|
| 65 |
+
{%- endif %}
|
| 66 |
+
{%- endif %}
|
| 67 |
+
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
|
| 68 |
+
{%- for message in messages[::-1] %}
|
| 69 |
+
{%- set index = (messages|length - 1) - loop.index0 %}
|
| 70 |
+
{%- if ns.multi_step_tool and message.role == "user" %}
|
| 71 |
+
{%- set content = render_content(message.content, false)|trim %}
|
| 72 |
+
{%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
|
| 73 |
+
{%- set ns.multi_step_tool = false %}
|
| 74 |
+
{%- set ns.last_query_index = index %}
|
| 75 |
+
{%- endif %}
|
| 76 |
+
{%- endif %}
|
| 77 |
+
{%- endfor %}
|
| 78 |
+
{%- if ns.multi_step_tool %}
|
| 79 |
+
{{- raise_exception('No user query found in messages.') }}
|
| 80 |
+
{%- endif %}
|
| 81 |
+
{%- for message in messages %}
|
| 82 |
+
{%- set content = render_content(message.content, true)|trim %}
|
| 83 |
+
{%- if message.role == "system" %}
|
| 84 |
+
{%- if not loop.first %}
|
| 85 |
+
{{- raise_exception('System message must be at the beginning.') }}
|
| 86 |
+
{%- endif %}
|
| 87 |
+
{%- elif message.role == "user" %}
|
| 88 |
+
{{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
|
| 89 |
+
{%- elif message.role == "assistant" %}
|
| 90 |
+
{%- set reasoning_content = '' %}
|
| 91 |
+
{%- if message.reasoning_content is string %}
|
| 92 |
+
{%- set reasoning_content = message.reasoning_content %}
|
| 93 |
+
{%- else %}
|
| 94 |
+
{%- if '</think>' in content %}
|
| 95 |
+
{%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
|
| 96 |
+
{%- set content = content.split('</think>')[-1].lstrip('\n') %}
|
| 97 |
+
{%- endif %}
|
| 98 |
+
{%- endif %}
|
| 99 |
+
{%- set reasoning_content = reasoning_content|trim %}
|
| 100 |
+
{%- if loop.index0 > ns.last_query_index %}
|
| 101 |
+
{{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
|
| 102 |
+
{%- else %}
|
| 103 |
+
{{- '<|im_start|>' + message.role + '\n' + content }}
|
| 104 |
+
{%- endif %}
|
| 105 |
+
{%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
|
| 106 |
+
{%- for tool_call in message.tool_calls %}
|
| 107 |
+
{%- if tool_call.function is defined %}
|
| 108 |
+
{%- set tool_call = tool_call.function %}
|
| 109 |
+
{%- endif %}
|
| 110 |
+
{%- if loop.first %}
|
| 111 |
+
{%- if content|trim %}
|
| 112 |
+
{{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
|
| 113 |
+
{%- else %}
|
| 114 |
+
{{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}
|
| 115 |
+
{%- endif %}
|
| 116 |
+
{%- else %}
|
| 117 |
+
{{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
|
| 118 |
+
{%- endif %}
|
| 119 |
+
{%- if tool_call.arguments is defined %}
|
| 120 |
+
{%- for args_name, args_value in tool_call.arguments|items %}
|
| 121 |
+
{{- '<parameter=' + args_name + '>\n' }}
|
| 122 |
+
{%- 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 %}
|
| 123 |
+
{{- args_value }}
|
| 124 |
+
{{- '\n</parameter>\n' }}
|
| 125 |
+
{%- endfor %}
|
| 126 |
+
{%- endif %}
|
| 127 |
+
{{- '</function>\n</tool_call>' }}
|
| 128 |
+
{%- endfor %}
|
| 129 |
+
{%- endif %}
|
| 130 |
+
{{- '<|im_end|>\n' }}
|
| 131 |
+
{%- elif message.role == "tool" %}
|
| 132 |
+
{%- if loop.previtem and loop.previtem.role != "tool" %}
|
| 133 |
+
{{- '<|im_start|>user' }}
|
| 134 |
+
{%- endif %}
|
| 135 |
+
{{- '\n<tool_response>\n' }}
|
| 136 |
+
{{- content }}
|
| 137 |
+
{{- '\n</tool_response>' }}
|
| 138 |
+
{%- if not loop.last and loop.nextitem.role != "tool" %}
|
| 139 |
+
{{- '<|im_end|>\n' }}
|
| 140 |
+
{%- elif loop.last %}
|
| 141 |
+
{{- '<|im_end|>\n' }}
|
| 142 |
+
{%- endif %}
|
| 143 |
+
{%- else %}
|
| 144 |
+
{{- raise_exception('Unexpected message role.') }}
|
| 145 |
+
{%- endif %}
|
| 146 |
+
{%- endfor %}
|
| 147 |
+
{%- if add_generation_prompt %}
|
| 148 |
+
{{- '<|im_start|>assistant\n' }}
|
| 149 |
+
{%- if enable_thinking is defined and enable_thinking is false %}
|
| 150 |
+
{{- '<think>\n\n</think>\n\n' }}
|
| 151 |
+
{%- else %}
|
| 152 |
+
{{- '<think>\n' }}
|
| 153 |
+
{%- endif %}
|
| 154 |
+
{%- endif %}
|
config.json
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"Qwen3_5ForConditionalGeneration"
|
| 4 |
+
],
|
| 5 |
+
"image_token_id": 248056,
|
| 6 |
+
"model_type": "qwen3_5",
|
| 7 |
+
"text_config": {
|
| 8 |
+
"attention_bias": false,
|
| 9 |
+
"attention_dropout": 0.0,
|
| 10 |
+
"attn_output_gate": true,
|
| 11 |
+
"dtype": "bfloat16",
|
| 12 |
+
"eos_token_id": 248044,
|
| 13 |
+
"full_attention_interval": 4,
|
| 14 |
+
"head_dim": 256,
|
| 15 |
+
"hidden_act": "silu",
|
| 16 |
+
"hidden_size": 2560,
|
| 17 |
+
"initializer_range": 0.02,
|
| 18 |
+
"intermediate_size": 9216,
|
| 19 |
+
"layer_types": [
|
| 20 |
+
"linear_attention",
|
| 21 |
+
"linear_attention",
|
| 22 |
+
"linear_attention",
|
| 23 |
+
"full_attention",
|
| 24 |
+
"linear_attention",
|
| 25 |
+
"linear_attention",
|
| 26 |
+
"linear_attention",
|
| 27 |
+
"full_attention",
|
| 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 |
+
],
|
| 53 |
+
"linear_conv_kernel_dim": 4,
|
| 54 |
+
"linear_key_head_dim": 128,
|
| 55 |
+
"linear_num_key_heads": 16,
|
| 56 |
+
"linear_num_value_heads": 32,
|
| 57 |
+
"linear_value_head_dim": 128,
|
| 58 |
+
"max_position_embeddings": 262144,
|
| 59 |
+
"mlp_only_layers": [],
|
| 60 |
+
"model_type": "qwen3_5_text",
|
| 61 |
+
"mtp_num_hidden_layers": 1,
|
| 62 |
+
"mtp_use_dedicated_embeddings": false,
|
| 63 |
+
"num_attention_heads": 16,
|
| 64 |
+
"num_hidden_layers": 32,
|
| 65 |
+
"num_key_value_heads": 4,
|
| 66 |
+
"rms_norm_eps": 1e-06,
|
| 67 |
+
"tie_word_embeddings": true,
|
| 68 |
+
"use_cache": true,
|
| 69 |
+
"vocab_size": 251392,
|
| 70 |
+
"mamba_ssm_dtype": "float32",
|
| 71 |
+
"rope_parameters": {
|
| 72 |
+
"mrope_interleaved": true,
|
| 73 |
+
"mrope_section": [
|
| 74 |
+
11,
|
| 75 |
+
11,
|
| 76 |
+
10
|
| 77 |
+
],
|
| 78 |
+
"rope_type": "default",
|
| 79 |
+
"rope_theta": 10000000,
|
| 80 |
+
"partial_rotary_factor": 0.25
|
| 81 |
+
}
|
| 82 |
+
},
|
| 83 |
+
"tie_word_embeddings": true,
|
| 84 |
+
"transformers_version": "4.57.0.dev0",
|
| 85 |
+
"video_token_id": 248057,
|
| 86 |
+
"vision_config": {
|
| 87 |
+
"deepstack_visual_indexes": [],
|
| 88 |
+
"depth": 24,
|
| 89 |
+
"hidden_act": "gelu_pytorch_tanh",
|
| 90 |
+
"hidden_size": 1024,
|
| 91 |
+
"in_channels": 3,
|
| 92 |
+
"initializer_range": 0.02,
|
| 93 |
+
"intermediate_size": 4096,
|
| 94 |
+
"model_type": "qwen3_5",
|
| 95 |
+
"num_heads": 16,
|
| 96 |
+
"num_position_embeddings": 2304,
|
| 97 |
+
"out_hidden_size": 2560,
|
| 98 |
+
"patch_size": 16,
|
| 99 |
+
"spatial_merge_size": 2,
|
| 100 |
+
"temporal_patch_size": 2
|
| 101 |
+
},
|
| 102 |
+
"vision_end_token_id": 248054,
|
| 103 |
+
"vision_start_token_id": 248053
|
| 104 |
+
}
|
memory_fusion/config.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"lambda_value": 1.0,
|
| 3 |
+
"adaptive_router": true,
|
| 4 |
+
"default_router": "intern-s2-397b",
|
| 5 |
+
"routers": {
|
| 6 |
+
"intern-s2-397b": {
|
| 7 |
+
"path": "routers/intern-s2-397b"
|
| 8 |
+
}
|
| 9 |
+
},
|
| 10 |
+
"lambda_base_only_threshold": -1.0
|
| 11 |
+
}
|
memory_fusion/routers/intern-s2-397b/router_config.json
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"num_layers": 4,
|
| 3 |
+
"input_mode": "both",
|
| 4 |
+
"use_scalars": true,
|
| 5 |
+
"scalar_proj_dim": 64,
|
| 6 |
+
"hidden_dim": 128,
|
| 7 |
+
"dropout": 0.2
|
| 8 |
+
}
|
memory_fusion/routers/intern-s2-397b/router_epoch_1.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:37acfa081f0f0ed38f44459847b967c37184ad8f43a3a561a0db96161d01713f
|
| 3 |
+
size 1843789
|
memory_fusion/routers/intern-s2-397b/router_epoch_1.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a8a439e6cc3dd4007b02190e66f4b25ad8234006eb6958e6f0c255525e478fef
|
| 3 |
+
size 1838852
|
merges.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1800e47c7143697785a473515ec09474154c16ed0bd2d954048fa0b8937629a3
|
| 3 |
+
size 9094349144
|
preprocessor_config.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
}
|
tokenization_interns1.py
ADDED
|
@@ -0,0 +1,1008 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
**kwargs,
|
| 513 |
+
):
|
| 514 |
+
bos_token = (
|
| 515 |
+
AddedToken(bos_token, lstrip=False, rstrip=False, special=True, normalized=False)
|
| 516 |
+
if isinstance(bos_token, str)
|
| 517 |
+
else bos_token
|
| 518 |
+
)
|
| 519 |
+
eos_token = (
|
| 520 |
+
AddedToken(eos_token, lstrip=False, rstrip=False, special=True, normalized=False)
|
| 521 |
+
if isinstance(eos_token, str)
|
| 522 |
+
else eos_token
|
| 523 |
+
)
|
| 524 |
+
unk_token = (
|
| 525 |
+
AddedToken(unk_token, lstrip=False, rstrip=False, special=True, normalized=False)
|
| 526 |
+
if isinstance(unk_token, str)
|
| 527 |
+
else unk_token
|
| 528 |
+
)
|
| 529 |
+
pad_token = (
|
| 530 |
+
AddedToken(pad_token, lstrip=False, rstrip=False, special=True, normalized=False)
|
| 531 |
+
if isinstance(pad_token, str)
|
| 532 |
+
else pad_token
|
| 533 |
+
)
|
| 534 |
+
|
| 535 |
+
with open(vocab_file, encoding="utf-8") as vocab_handle:
|
| 536 |
+
self.encoder = json.load(vocab_handle)
|
| 537 |
+
self.decoder = {v: k for k, v in self.encoder.items()}
|
| 538 |
+
self.errors = errors # how to handle errors in decoding
|
| 539 |
+
self.byte_encoder = bytes_to_unicode()
|
| 540 |
+
self.byte_decoder = {v: k for k, v in self.byte_encoder.items()}
|
| 541 |
+
bpe_merges = []
|
| 542 |
+
with open(merges_file, encoding="utf-8") as merges_handle:
|
| 543 |
+
for i, line in enumerate(merges_handle):
|
| 544 |
+
line = line.strip()
|
| 545 |
+
if (i == 0 and line.startswith("#version:")) or not line:
|
| 546 |
+
continue
|
| 547 |
+
bpe_merges.append(tuple(line.split()))
|
| 548 |
+
self.bpe_ranks = dict(zip(bpe_merges, range(len(bpe_merges))))
|
| 549 |
+
# NOTE: the cache can grow without bound and will get really large for long running processes
|
| 550 |
+
# (esp. for texts of language that do not use space between word, e.g. Chinese); technically
|
| 551 |
+
# not a memory leak but appears as one.
|
| 552 |
+
# GPT2Tokenizer has the same problem, so let's be consistent.
|
| 553 |
+
self.cache = {}
|
| 554 |
+
|
| 555 |
+
self.pat = re.compile(PRETOKENIZE_REGEX)
|
| 556 |
+
|
| 557 |
+
if kwargs.get("add_prefix_space", False):
|
| 558 |
+
logger.warning_once(
|
| 559 |
+
f"{self.__class__.__name} does not support `add_prefix_space`, setting it to True has no effect."
|
| 560 |
+
)
|
| 561 |
+
|
| 562 |
+
super().__init__(
|
| 563 |
+
vocab_file=vocab_file,
|
| 564 |
+
merges_file=merges_file,
|
| 565 |
+
errors=errors,
|
| 566 |
+
unk_token=unk_token,
|
| 567 |
+
bos_token=bos_token,
|
| 568 |
+
eos_token=eos_token,
|
| 569 |
+
pad_token=pad_token,
|
| 570 |
+
clean_up_tokenization_spaces=clean_up_tokenization_spaces,
|
| 571 |
+
split_special_tokens=split_special_tokens,
|
| 572 |
+
special_tokens_pattern="none",
|
| 573 |
+
**kwargs,
|
| 574 |
+
)
|
| 575 |
+
|
| 576 |
+
self.prepare_extra_tokenizers(vocab_file)
|
| 577 |
+
|
| 578 |
+
@property
|
| 579 |
+
def vocab_size(self) -> int:
|
| 580 |
+
return len(self.encoder)
|
| 581 |
+
|
| 582 |
+
# Copied from transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer.get_vocab
|
| 583 |
+
def get_vocab(self):
|
| 584 |
+
return dict(self.encoder, **self.added_tokens_encoder)
|
| 585 |
+
|
| 586 |
+
# Copied from transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer.bpe
|
| 587 |
+
def bpe(self, token):
|
| 588 |
+
if token in self.cache:
|
| 589 |
+
return self.cache[token]
|
| 590 |
+
word = tuple(token)
|
| 591 |
+
pairs = get_pairs(word)
|
| 592 |
+
|
| 593 |
+
if not pairs:
|
| 594 |
+
return token
|
| 595 |
+
|
| 596 |
+
while True:
|
| 597 |
+
bigram = min(pairs, key=lambda pair: self.bpe_ranks.get(pair, float("inf")))
|
| 598 |
+
if bigram not in self.bpe_ranks:
|
| 599 |
+
break
|
| 600 |
+
first, second = bigram
|
| 601 |
+
new_word = []
|
| 602 |
+
i = 0
|
| 603 |
+
while i < len(word):
|
| 604 |
+
try:
|
| 605 |
+
j = word.index(first, i)
|
| 606 |
+
except ValueError:
|
| 607 |
+
new_word.extend(word[i:])
|
| 608 |
+
break
|
| 609 |
+
else:
|
| 610 |
+
new_word.extend(word[i:j])
|
| 611 |
+
i = j
|
| 612 |
+
|
| 613 |
+
if word[i] == first and i < len(word) - 1 and word[i + 1] == second:
|
| 614 |
+
new_word.append(first + second)
|
| 615 |
+
i += 2
|
| 616 |
+
else:
|
| 617 |
+
new_word.append(word[i])
|
| 618 |
+
i += 1
|
| 619 |
+
new_word = tuple(new_word)
|
| 620 |
+
word = new_word
|
| 621 |
+
if len(word) == 1:
|
| 622 |
+
break
|
| 623 |
+
else:
|
| 624 |
+
pairs = get_pairs(word)
|
| 625 |
+
word = " ".join(word)
|
| 626 |
+
self.cache[token] = word
|
| 627 |
+
return word
|
| 628 |
+
|
| 629 |
+
def prepare_extra_tokenizers(self, vocab_file: str) -> None:
|
| 630 |
+
"""
|
| 631 |
+
Prepare domain-specific tokenizers.
|
| 632 |
+
|
| 633 |
+
Define variables/maps here which guide domain-specific tokenization later.
|
| 634 |
+
"""
|
| 635 |
+
# Load extra tokenizers with SentencePiece model
|
| 636 |
+
dir_name = os.path.dirname(vocab_file)
|
| 637 |
+
|
| 638 |
+
self.sp_model_SMILES = spm.SentencePieceProcessor()
|
| 639 |
+
self.sp_model_SMILES.Load(os.path.join(dir_name, "tokenizer_SMILES.model"))
|
| 640 |
+
self.sp_model_SMILES.offset = self.init_kwargs["offset_SMILES"]
|
| 641 |
+
|
| 642 |
+
self.sp_model_PROT = spm.SentencePieceProcessor()
|
| 643 |
+
self.sp_model_PROT.Load(os.path.join(dir_name, "tokenizer_PROT.model"))
|
| 644 |
+
self.sp_model_PROT.offset = self.init_kwargs["offset_PROT"]
|
| 645 |
+
|
| 646 |
+
self.sp_model_XNA = spm.SentencePieceProcessor()
|
| 647 |
+
self.sp_model_XNA.Load(os.path.join(dir_name, "tokenizer_XNA.model"))
|
| 648 |
+
self.sp_model_XNA.offset = self.init_kwargs["offset_XNA"]
|
| 649 |
+
|
| 650 |
+
base_mapping = {
|
| 651 |
+
"SMILES": self.sp_model_SMILES,
|
| 652 |
+
"protein": self.sp_model_PROT,
|
| 653 |
+
"dna": self.sp_model_XNA,
|
| 654 |
+
"rna": self.sp_model_XNA,
|
| 655 |
+
}
|
| 656 |
+
auto_detect_mapping = {
|
| 657 |
+
"SMILES": self.sp_model_SMILES,
|
| 658 |
+
"PROT": self.sp_model_PROT,
|
| 659 |
+
"XNA": self.sp_model_XNA,
|
| 660 |
+
}
|
| 661 |
+
# Guiding tokens of domain-specific tokenization
|
| 662 |
+
self.ex_begin_mapping = {f"<{key}>": value for key, value in base_mapping.items()}
|
| 663 |
+
self.ex_end_mapping = {f"</{key}>": value for key, value in base_mapping.items()}
|
| 664 |
+
# Transient markers for auto-detection, these tokens will not be assigned token ids
|
| 665 |
+
self.ex_auto_begin_mapping = {f"<{key}_AUTO_DETECT>": value for key, value in auto_detect_mapping.items()}
|
| 666 |
+
self.ex_auto_end_mapping = {f"</{key}_AUTO_DETECT>": value for key, value in auto_detect_mapping.items()}
|
| 667 |
+
# Token markers to prevent unwanted auto-detection
|
| 668 |
+
self.ex_protect_begin_tokens = ["<MOLFORMULA>"]
|
| 669 |
+
self.ex_protect_end_tokens = ["</MOLFORMULA>"]
|
| 670 |
+
# For simplicity
|
| 671 |
+
self.ex_protect_tokens = self.ex_protect_begin_tokens + self.ex_protect_end_tokens
|
| 672 |
+
self.ex_all_begin_mapping = self.ex_begin_mapping | self.ex_auto_begin_mapping
|
| 673 |
+
self.ex_all_end_mapping = self.ex_end_mapping | self.ex_auto_end_mapping
|
| 674 |
+
|
| 675 |
+
# Update encoder & decoder with extra tokenizers
|
| 676 |
+
for tokenizer_name, sp_model in [
|
| 677 |
+
("SMILES", self.sp_model_SMILES),
|
| 678 |
+
("PROT", self.sp_model_PROT),
|
| 679 |
+
("XNA", self.sp_model_XNA),
|
| 680 |
+
]:
|
| 681 |
+
self.decoder.update(
|
| 682 |
+
{i + sp_model.offset: sp_model.id_to_piece(i) for i in range(sp_model.get_piece_size())}
|
| 683 |
+
)
|
| 684 |
+
# Not really used, only to fill holes in encoder, to keep methods like `add_tokens` working
|
| 685 |
+
self.encoder.update(
|
| 686 |
+
{
|
| 687 |
+
f"<|{tokenizer_name}_{sp_model.id_to_piece(i)}|>": i + sp_model.offset
|
| 688 |
+
for i in range(sp_model.get_piece_size())
|
| 689 |
+
}
|
| 690 |
+
)
|
| 691 |
+
|
| 692 |
+
# protect-tokens should keep complete temporarily to guide later tokenization
|
| 693 |
+
# it will be segmented later
|
| 694 |
+
for token in self.ex_protect_tokens:
|
| 695 |
+
self.tokens_trie.add(token)
|
| 696 |
+
|
| 697 |
+
self._unk_token = "<unk>" # Fall-back
|
| 698 |
+
self.check_module_list = [SmilesCheckModule(), ProtCheckModule(), XnaCheckModule()]
|
| 699 |
+
|
| 700 |
+
def _pop_logical_sp_token(self, extra_tokenizer_stack: list, mapping_name: str) -> None:
|
| 701 |
+
"""Switch tokenizer when it comes to an end sp token"""
|
| 702 |
+
extra_tokenizer = extra_tokenizer_stack.pop()
|
| 703 |
+
if extra_tokenizer != self.ex_all_end_mapping[mapping_name]:
|
| 704 |
+
logger.warning_once(
|
| 705 |
+
f"Encounter incorrect nesting of extra tokenizer: {self.ex_all_end_mapping[mapping_name]} and {extra_tokenizer}"
|
| 706 |
+
)
|
| 707 |
+
logger.warning_once("This may lead to unexpected behaviour of the tokenizer, please check your input.")
|
| 708 |
+
|
| 709 |
+
def tokenize(self, text: TextInput, **kwargs) -> list[str]:
|
| 710 |
+
"""
|
| 711 |
+
Converts a string into a sequence of tokens, using the tokenizer.
|
| 712 |
+
|
| 713 |
+
It will switch to domain-specific tokenizer once encountering extra/logical sp tokens.
|
| 714 |
+
|
| 715 |
+
Args:
|
| 716 |
+
text: TextInput
|
| 717 |
+
"""
|
| 718 |
+
split_special_tokens = kwargs.pop("split_special_tokens", self.split_special_tokens)
|
| 719 |
+
|
| 720 |
+
text, kwargs = self.prepare_for_tokenization(text, **kwargs)
|
| 721 |
+
|
| 722 |
+
if hasattr(self, "do_lower_case") and self.do_lower_case:
|
| 723 |
+
# convert non-special tokens to lowercase. Might be super slow as well?
|
| 724 |
+
escaped_special_toks = [re.escape(s_tok) for s_tok in (self.all_special_tokens)]
|
| 725 |
+
escaped_special_toks += [
|
| 726 |
+
re.escape(s_tok.content)
|
| 727 |
+
for s_tok in (self._added_tokens_decoder.values())
|
| 728 |
+
if not s_tok.special and s_tok.normalized
|
| 729 |
+
]
|
| 730 |
+
pattern = r"(" + r"|".join(escaped_special_toks) + r")|" + r"(.+?)"
|
| 731 |
+
text = re.sub(pattern, lambda m: m.groups()[0] or m.groups()[1].lower(), text)
|
| 732 |
+
|
| 733 |
+
if split_special_tokens:
|
| 734 |
+
no_split_token = []
|
| 735 |
+
tokens = [text]
|
| 736 |
+
else:
|
| 737 |
+
no_split_token = self._added_tokens_encoder.keys() # don't split on any of the added tokens
|
| 738 |
+
# "This is something<special_token_1> else"
|
| 739 |
+
tokens = self.tokens_trie.split(text)
|
| 740 |
+
|
| 741 |
+
# ["This is something", "<special_token_1>", " else"]
|
| 742 |
+
for i, token in enumerate(tokens):
|
| 743 |
+
if token in no_split_token:
|
| 744 |
+
tok_extended = self._added_tokens_decoder.get(self._added_tokens_encoder[token], None)
|
| 745 |
+
left = tokens[i - 1] if i > 0 else None
|
| 746 |
+
right = tokens[i + 1] if i < len(tokens) - 1 else None
|
| 747 |
+
if isinstance(tok_extended, AddedToken):
|
| 748 |
+
if tok_extended.rstrip and right:
|
| 749 |
+
# A bit counter-intuitive but we strip the left of the string
|
| 750 |
+
# since tok_extended.rstrip means the special token is eating all white spaces on its right
|
| 751 |
+
tokens[i + 1] = right.lstrip()
|
| 752 |
+
# Strip white spaces on the left
|
| 753 |
+
if tok_extended.lstrip and left:
|
| 754 |
+
tokens[i - 1] = left.rstrip() # Opposite here
|
| 755 |
+
if tok_extended.single_word and left and left[-1] != " ":
|
| 756 |
+
tokens[i - 1] += token
|
| 757 |
+
tokens[i] = ""
|
| 758 |
+
elif tok_extended.single_word and right and right[0] != " ":
|
| 759 |
+
tokens[i + 1] = token + tokens[i + 1]
|
| 760 |
+
tokens[i] = ""
|
| 761 |
+
else:
|
| 762 |
+
raise ValueError(
|
| 763 |
+
f"{tok_extended} cannot be tokenized because it was not properly added"
|
| 764 |
+
f" to the tokenizer. This means that it is not an `AddedToken` but a {type(tok_extended)}"
|
| 765 |
+
)
|
| 766 |
+
|
| 767 |
+
# ["This is something", "<special_token_1>", "else"]
|
| 768 |
+
tokenized_text = []
|
| 769 |
+
|
| 770 |
+
# Codes for automatically detecting domain-specific content
|
| 771 |
+
# All parts that have been marked by domain-specific or protection tokens will not be subject to auto detection
|
| 772 |
+
# See transformers/tests/models/intern_s1/test_tokenization_intern_s1.py::test_auto_detection() for more details
|
| 773 |
+
new_tokens = []
|
| 774 |
+
not_split_flag = 0
|
| 775 |
+
for token in tokens:
|
| 776 |
+
if not token:
|
| 777 |
+
continue
|
| 778 |
+
if token in no_split_token or token in self.ex_protect_tokens:
|
| 779 |
+
new_tokens.append(token)
|
| 780 |
+
if token in self.ex_begin_mapping or token in self.ex_protect_begin_tokens:
|
| 781 |
+
not_split_flag += 1 # In case nested sp tokens
|
| 782 |
+
elif token in self.ex_end_mapping or token in self.ex_protect_end_tokens:
|
| 783 |
+
not_split_flag = max(0, not_split_flag - 1)
|
| 784 |
+
else:
|
| 785 |
+
if not_split_flag:
|
| 786 |
+
new_tokens.append(token)
|
| 787 |
+
else:
|
| 788 |
+
for check_module in self.check_module_list:
|
| 789 |
+
token = check_module.re_split(token)
|
| 790 |
+
|
| 791 |
+
new_tokens.extend(token)
|
| 792 |
+
tokens = new_tokens
|
| 793 |
+
|
| 794 |
+
# Use stack to maintain which tokenizer should be used, considering the possibility of nested extra tokenizer
|
| 795 |
+
extra_tokenizer_stack = []
|
| 796 |
+
for token in tokens:
|
| 797 |
+
# Need to skip eventual empty (fully stripped) tokens
|
| 798 |
+
if not token:
|
| 799 |
+
continue
|
| 800 |
+
# protect-tokens are not assigned token ids, should be segmented here
|
| 801 |
+
if token in self.ex_protect_tokens:
|
| 802 |
+
tokenized_text.extend(self._tokenize(token))
|
| 803 |
+
# push tokenizer to stack when encountering begin token
|
| 804 |
+
elif token in self.ex_all_begin_mapping:
|
| 805 |
+
tokenized_text.append(token)
|
| 806 |
+
extra_tokenizer_stack.append(self.ex_all_begin_mapping[token])
|
| 807 |
+
# pop tokenizer from stack when encountering end token
|
| 808 |
+
elif token in self.ex_all_end_mapping:
|
| 809 |
+
tokenized_text.append(token)
|
| 810 |
+
if extra_tokenizer_stack:
|
| 811 |
+
self._pop_logical_sp_token(extra_tokenizer_stack, token)
|
| 812 |
+
# other special tokens
|
| 813 |
+
elif token in no_split_token:
|
| 814 |
+
tokenized_text.append(token)
|
| 815 |
+
else:
|
| 816 |
+
tokenized_text.extend(self._tokenize(token, extra_tokenizer_stack=extra_tokenizer_stack))
|
| 817 |
+
|
| 818 |
+
# ["This", " is", " something", "<special_token_1>", "else"]
|
| 819 |
+
return tokenized_text
|
| 820 |
+
|
| 821 |
+
def _tokenize(self, text, **kwargs):
|
| 822 |
+
"""
|
| 823 |
+
Modified from `transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer._tokenize`.
|
| 824 |
+
|
| 825 |
+
This adaptation supports domain-specific tokenizers.
|
| 826 |
+
"""
|
| 827 |
+
extra_tokenizer_stack = kwargs.pop("extra_tokenizer_stack", False)
|
| 828 |
+
if extra_tokenizer_stack:
|
| 829 |
+
tokenized_text = extra_tokenizer_stack[-1].encode(text, out_type=str)
|
| 830 |
+
tokenized_id = extra_tokenizer_stack[-1].encode(text, out_type=int)
|
| 831 |
+
final_tokenized_text = []
|
| 832 |
+
for text_piece, id_piece in zip(tokenized_text, tokenized_id):
|
| 833 |
+
if id_piece == 0:
|
| 834 |
+
final_tokenized_text.extend(self._bpe_tokenize(text_piece))
|
| 835 |
+
else:
|
| 836 |
+
final_tokenized_text.append(text_piece)
|
| 837 |
+
return final_tokenized_text
|
| 838 |
+
else:
|
| 839 |
+
return self._bpe_tokenize(text)
|
| 840 |
+
|
| 841 |
+
def _bpe_tokenize(self, text, **kwargs):
|
| 842 |
+
text = text.replace(
|
| 843 |
+
"▁", " "
|
| 844 |
+
) # This discrepancy stems from differing whitespace treatment in SentencePiece versus BPE tokenization.
|
| 845 |
+
bpe_tokens = []
|
| 846 |
+
for token in re.findall(self.pat, text):
|
| 847 |
+
token = "".join(
|
| 848 |
+
self.byte_encoder[b] for b in token.encode("utf-8")
|
| 849 |
+
) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case)
|
| 850 |
+
bpe_tokens.extend(bpe_token for bpe_token in self.bpe(token).split(" "))
|
| 851 |
+
return bpe_tokens
|
| 852 |
+
|
| 853 |
+
def convert_tokens_to_ids(self, tokens: Union[str, list[str]]) -> Union[int, list[int]]:
|
| 854 |
+
"""
|
| 855 |
+
Modified from `transformers.tokenization_utils.PreTrainedTokenzier.convert_tokens_to_ids`.
|
| 856 |
+
|
| 857 |
+
Converts a token string (or a sequence of tokens) in a single integer id (or a sequence of ids), using the
|
| 858 |
+
vocabulary.
|
| 859 |
+
|
| 860 |
+
This adaptation supports domain-specific tokenizers.
|
| 861 |
+
|
| 862 |
+
Args:
|
| 863 |
+
tokens (`str` or `List[str]`): One or several token(s) to convert to token id(s).
|
| 864 |
+
|
| 865 |
+
Returns:
|
| 866 |
+
`int` or `List[int]`: The token id or list of token ids.
|
| 867 |
+
"""
|
| 868 |
+
if tokens is None:
|
| 869 |
+
return None
|
| 870 |
+
|
| 871 |
+
if isinstance(tokens, str):
|
| 872 |
+
return self._convert_token_to_id_with_added_voc(tokens)
|
| 873 |
+
|
| 874 |
+
ids = []
|
| 875 |
+
extra_tokenizer_stack = []
|
| 876 |
+
|
| 877 |
+
for token in tokens:
|
| 878 |
+
if token not in self.ex_auto_begin_mapping and token not in self.ex_auto_end_mapping:
|
| 879 |
+
ids.append(
|
| 880 |
+
self._convert_token_to_id_with_added_voc(token, extra_tokenizer_stack=extra_tokenizer_stack)
|
| 881 |
+
)
|
| 882 |
+
if token in self.ex_all_begin_mapping:
|
| 883 |
+
extra_tokenizer_stack.append(self.ex_all_begin_mapping[token])
|
| 884 |
+
elif token in self.ex_all_end_mapping:
|
| 885 |
+
if extra_tokenizer_stack:
|
| 886 |
+
self._pop_logical_sp_token(extra_tokenizer_stack, token)
|
| 887 |
+
return ids
|
| 888 |
+
|
| 889 |
+
def _convert_token_to_id_with_added_voc(self, token, **kwargs):
|
| 890 |
+
"""
|
| 891 |
+
Modified from `transformers.tokenization_utils.PreTrainedTokenzier._convert_token_to_id_with_added_voc`.
|
| 892 |
+
|
| 893 |
+
This adaptation supports domain-specific tokenizers.
|
| 894 |
+
"""
|
| 895 |
+
if token is None:
|
| 896 |
+
return None
|
| 897 |
+
|
| 898 |
+
if token in self._added_tokens_encoder:
|
| 899 |
+
return self._added_tokens_encoder[token]
|
| 900 |
+
return self._convert_token_to_id(token, **kwargs)
|
| 901 |
+
|
| 902 |
+
def _convert_token_to_id(self, token, **kwargs):
|
| 903 |
+
"""
|
| 904 |
+
Modified from `transformers.tokenization_utils.PreTrainedTokenzier._convert_token_to_id`.
|
| 905 |
+
|
| 906 |
+
Converts a token (str) in an id using the vocab.
|
| 907 |
+
|
| 908 |
+
Fall back to original tokenizer once OOV.
|
| 909 |
+
"""
|
| 910 |
+
extra_tokenizer_stack = kwargs.pop("extra_tokenizer_stack", False)
|
| 911 |
+
if extra_tokenizer_stack:
|
| 912 |
+
token_id = extra_tokenizer_stack[-1].piece_to_id(token)
|
| 913 |
+
if token_id == extra_tokenizer_stack[-1].unk_id():
|
| 914 |
+
return self.encoder.get(token, self.encoder.get(self._unk_token))
|
| 915 |
+
else:
|
| 916 |
+
return token_id + extra_tokenizer_stack[-1].offset
|
| 917 |
+
else:
|
| 918 |
+
return self.encoder.get(token, self.encoder.get(self._unk_token))
|
| 919 |
+
|
| 920 |
+
# Copied from transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer._convert_id_to_token
|
| 921 |
+
def _convert_id_to_token(self, index):
|
| 922 |
+
"""Converts an index (integer) in a token (str) using the vocab."""
|
| 923 |
+
return self.decoder.get(index)
|
| 924 |
+
|
| 925 |
+
def convert_tokens_to_string(self, tokens):
|
| 926 |
+
"""Converts a sequence of tokens (string) in a single string."""
|
| 927 |
+
text = "".join(tokens)
|
| 928 |
+
text = text.replace(
|
| 929 |
+
"▁", "Ġ"
|
| 930 |
+
) # This discrepancy stems from differing whitespace treatment in SentencePiece versus BPE tokenization.
|
| 931 |
+
text = text.replace("\n", "Ċ")
|
| 932 |
+
text = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8", errors=self.errors)
|
| 933 |
+
return text
|
| 934 |
+
|
| 935 |
+
def decode(
|
| 936 |
+
self,
|
| 937 |
+
token_ids,
|
| 938 |
+
skip_special_tokens: bool = False,
|
| 939 |
+
clean_up_tokenization_spaces: Optional[bool] = False,
|
| 940 |
+
spaces_between_special_tokens: bool = False,
|
| 941 |
+
**kwargs,
|
| 942 |
+
) -> str:
|
| 943 |
+
# `spaces_between_special_tokens` defaults to True for _decode in slow tokenizers
|
| 944 |
+
# and cannot be configured elsewhere, but it should default to False for InternS1Tokenizer
|
| 945 |
+
return super().decode(
|
| 946 |
+
token_ids,
|
| 947 |
+
skip_special_tokens=skip_special_tokens,
|
| 948 |
+
clean_up_tokenization_spaces=clean_up_tokenization_spaces,
|
| 949 |
+
spaces_between_special_tokens=spaces_between_special_tokens,
|
| 950 |
+
**kwargs,
|
| 951 |
+
)
|
| 952 |
+
|
| 953 |
+
def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]:
|
| 954 |
+
"""
|
| 955 |
+
Modified from `transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer.save_vocabulary` to support saving custom extension.
|
| 956 |
+
"""
|
| 957 |
+
if not os.path.isdir(save_directory):
|
| 958 |
+
logger.error(f"Vocabulary path ({save_directory}) should be a directory")
|
| 959 |
+
return
|
| 960 |
+
vocab_file = os.path.join(
|
| 961 |
+
save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
|
| 962 |
+
)
|
| 963 |
+
merge_file = os.path.join(
|
| 964 |
+
save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"]
|
| 965 |
+
)
|
| 966 |
+
sp_model_smiles = os.path.join(
|
| 967 |
+
save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["sp_model_SMILES"]
|
| 968 |
+
)
|
| 969 |
+
sp_model_prot = os.path.join(
|
| 970 |
+
save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["sp_model_PROT"]
|
| 971 |
+
)
|
| 972 |
+
sp_model_xna = os.path.join(
|
| 973 |
+
save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["sp_model_XNA"]
|
| 974 |
+
)
|
| 975 |
+
|
| 976 |
+
with open(vocab_file, "w", encoding="utf-8") as f:
|
| 977 |
+
f.write(json.dumps(self.encoder, indent=2, sort_keys=True, ensure_ascii=False) + "\n")
|
| 978 |
+
|
| 979 |
+
index = 0
|
| 980 |
+
with open(merge_file, "w", encoding="utf-8") as writer:
|
| 981 |
+
writer.write("#version: 0.2\n")
|
| 982 |
+
for bpe_tokens, token_index in sorted(self.bpe_ranks.items(), key=lambda kv: kv[1]):
|
| 983 |
+
if index != token_index:
|
| 984 |
+
logger.warning(
|
| 985 |
+
f"Saving vocabulary to {merge_file}: BPE merge indices are not consecutive."
|
| 986 |
+
" Please check that the tokenizer is not corrupted!"
|
| 987 |
+
)
|
| 988 |
+
index = token_index
|
| 989 |
+
writer.write(" ".join(bpe_tokens) + "\n")
|
| 990 |
+
index += 1
|
| 991 |
+
|
| 992 |
+
with open(sp_model_smiles, "wb") as f:
|
| 993 |
+
f.write(self.sp_model_SMILES.serialized_model_proto())
|
| 994 |
+
|
| 995 |
+
with open(sp_model_prot, "wb") as f:
|
| 996 |
+
f.write(self.sp_model_PROT.serialized_model_proto())
|
| 997 |
+
|
| 998 |
+
with open(sp_model_xna, "wb") as f:
|
| 999 |
+
f.write(self.sp_model_XNA.serialized_model_proto())
|
| 1000 |
+
|
| 1001 |
+
return vocab_file, merge_file
|
| 1002 |
+
|
| 1003 |
+
def prepare_for_tokenization(self, text, **kwargs):
|
| 1004 |
+
text = unicodedata.normalize("NFC", text)
|
| 1005 |
+
return (text, kwargs)
|
| 1006 |
+
|
| 1007 |
+
|
| 1008 |
+
__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,514 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": false,
|
| 3 |
+
"added_tokens_decoder": {
|
| 4 |
+
"248044": {
|
| 5 |
+
"content": "<|endoftext|>",
|
| 6 |
+
"lstrip": false,
|
| 7 |
+
"normalized": false,
|
| 8 |
+
"rstrip": false,
|
| 9 |
+
"single_word": false,
|
| 10 |
+
"special": true
|
| 11 |
+
},
|
| 12 |
+
"248045": {
|
| 13 |
+
"content": "<|im_start|>",
|
| 14 |
+
"lstrip": false,
|
| 15 |
+
"normalized": false,
|
| 16 |
+
"rstrip": false,
|
| 17 |
+
"single_word": false,
|
| 18 |
+
"special": true
|
| 19 |
+
},
|
| 20 |
+
"248046": {
|
| 21 |
+
"content": "<|im_end|>",
|
| 22 |
+
"lstrip": false,
|
| 23 |
+
"normalized": false,
|
| 24 |
+
"rstrip": false,
|
| 25 |
+
"single_word": false,
|
| 26 |
+
"special": true
|
| 27 |
+
},
|
| 28 |
+
"248047": {
|
| 29 |
+
"content": "<|object_ref_start|>",
|
| 30 |
+
"lstrip": false,
|
| 31 |
+
"normalized": false,
|
| 32 |
+
"rstrip": false,
|
| 33 |
+
"single_word": false,
|
| 34 |
+
"special": true
|
| 35 |
+
},
|
| 36 |
+
"248048": {
|
| 37 |
+
"content": "<|object_ref_end|>",
|
| 38 |
+
"lstrip": false,
|
| 39 |
+
"normalized": false,
|
| 40 |
+
"rstrip": false,
|
| 41 |
+
"single_word": false,
|
| 42 |
+
"special": true
|
| 43 |
+
},
|
| 44 |
+
"248049": {
|
| 45 |
+
"content": "<|box_start|>",
|
| 46 |
+
"lstrip": false,
|
| 47 |
+
"normalized": false,
|
| 48 |
+
"rstrip": false,
|
| 49 |
+
"single_word": false,
|
| 50 |
+
"special": true
|
| 51 |
+
},
|
| 52 |
+
"248050": {
|
| 53 |
+
"content": "<|box_end|>",
|
| 54 |
+
"lstrip": false,
|
| 55 |
+
"normalized": false,
|
| 56 |
+
"rstrip": false,
|
| 57 |
+
"single_word": false,
|
| 58 |
+
"special": true
|
| 59 |
+
},
|
| 60 |
+
"248051": {
|
| 61 |
+
"content": "<|quad_start|>",
|
| 62 |
+
"lstrip": false,
|
| 63 |
+
"normalized": false,
|
| 64 |
+
"rstrip": false,
|
| 65 |
+
"single_word": false,
|
| 66 |
+
"special": true
|
| 67 |
+
},
|
| 68 |
+
"248052": {
|
| 69 |
+
"content": "<|quad_end|>",
|
| 70 |
+
"lstrip": false,
|
| 71 |
+
"normalized": false,
|
| 72 |
+
"rstrip": false,
|
| 73 |
+
"single_word": false,
|
| 74 |
+
"special": true
|
| 75 |
+
},
|
| 76 |
+
"248053": {
|
| 77 |
+
"content": "<|vision_start|>",
|
| 78 |
+
"lstrip": false,
|
| 79 |
+
"normalized": false,
|
| 80 |
+
"rstrip": false,
|
| 81 |
+
"single_word": false,
|
| 82 |
+
"special": true
|
| 83 |
+
},
|
| 84 |
+
"248054": {
|
| 85 |
+
"content": "<|vision_end|>",
|
| 86 |
+
"lstrip": false,
|
| 87 |
+
"normalized": false,
|
| 88 |
+
"rstrip": false,
|
| 89 |
+
"single_word": false,
|
| 90 |
+
"special": true
|
| 91 |
+
},
|
| 92 |
+
"248055": {
|
| 93 |
+
"content": "<|vision_pad|>",
|
| 94 |
+
"lstrip": false,
|
| 95 |
+
"normalized": false,
|
| 96 |
+
"rstrip": false,
|
| 97 |
+
"single_word": false,
|
| 98 |
+
"special": true
|
| 99 |
+
},
|
| 100 |
+
"248056": {
|
| 101 |
+
"content": "<|image_pad|>",
|
| 102 |
+
"lstrip": false,
|
| 103 |
+
"normalized": false,
|
| 104 |
+
"rstrip": false,
|
| 105 |
+
"single_word": false,
|
| 106 |
+
"special": true
|
| 107 |
+
},
|
| 108 |
+
"248057": {
|
| 109 |
+
"content": "<|video_pad|>",
|
| 110 |
+
"lstrip": false,
|
| 111 |
+
"normalized": false,
|
| 112 |
+
"rstrip": false,
|
| 113 |
+
"single_word": false,
|
| 114 |
+
"special": true
|
| 115 |
+
},
|
| 116 |
+
"248058": {
|
| 117 |
+
"content": "<tool_call>",
|
| 118 |
+
"lstrip": false,
|
| 119 |
+
"normalized": false,
|
| 120 |
+
"rstrip": false,
|
| 121 |
+
"single_word": false,
|
| 122 |
+
"special": false
|
| 123 |
+
},
|
| 124 |
+
"248059": {
|
| 125 |
+
"content": "</tool_call>",
|
| 126 |
+
"lstrip": false,
|
| 127 |
+
"normalized": false,
|
| 128 |
+
"rstrip": false,
|
| 129 |
+
"single_word": false,
|
| 130 |
+
"special": false
|
| 131 |
+
},
|
| 132 |
+
"248060": {
|
| 133 |
+
"content": "<|fim_prefix|>",
|
| 134 |
+
"lstrip": false,
|
| 135 |
+
"normalized": false,
|
| 136 |
+
"rstrip": false,
|
| 137 |
+
"single_word": false,
|
| 138 |
+
"special": false
|
| 139 |
+
},
|
| 140 |
+
"248061": {
|
| 141 |
+
"content": "<|fim_middle|>",
|
| 142 |
+
"lstrip": false,
|
| 143 |
+
"normalized": false,
|
| 144 |
+
"rstrip": false,
|
| 145 |
+
"single_word": false,
|
| 146 |
+
"special": false
|
| 147 |
+
},
|
| 148 |
+
"248062": {
|
| 149 |
+
"content": "<|fim_suffix|>",
|
| 150 |
+
"lstrip": false,
|
| 151 |
+
"normalized": false,
|
| 152 |
+
"rstrip": false,
|
| 153 |
+
"single_word": false,
|
| 154 |
+
"special": false
|
| 155 |
+
},
|
| 156 |
+
"248063": {
|
| 157 |
+
"content": "<|fim_pad|>",
|
| 158 |
+
"lstrip": false,
|
| 159 |
+
"normalized": false,
|
| 160 |
+
"rstrip": false,
|
| 161 |
+
"single_word": false,
|
| 162 |
+
"special": false
|
| 163 |
+
},
|
| 164 |
+
"248064": {
|
| 165 |
+
"content": "<|repo_name|>",
|
| 166 |
+
"lstrip": false,
|
| 167 |
+
"normalized": false,
|
| 168 |
+
"rstrip": false,
|
| 169 |
+
"single_word": false,
|
| 170 |
+
"special": false
|
| 171 |
+
},
|
| 172 |
+
"248065": {
|
| 173 |
+
"content": "<|file_sep|>",
|
| 174 |
+
"lstrip": false,
|
| 175 |
+
"normalized": false,
|
| 176 |
+
"rstrip": false,
|
| 177 |
+
"single_word": false,
|
| 178 |
+
"special": false
|
| 179 |
+
},
|
| 180 |
+
"248066": {
|
| 181 |
+
"content": "<tool_response>",
|
| 182 |
+
"lstrip": false,
|
| 183 |
+
"normalized": false,
|
| 184 |
+
"rstrip": false,
|
| 185 |
+
"single_word": false,
|
| 186 |
+
"special": false
|
| 187 |
+
},
|
| 188 |
+
"248067": {
|
| 189 |
+
"content": "</tool_response>",
|
| 190 |
+
"lstrip": false,
|
| 191 |
+
"normalized": false,
|
| 192 |
+
"rstrip": false,
|
| 193 |
+
"single_word": false,
|
| 194 |
+
"special": false
|
| 195 |
+
},
|
| 196 |
+
"248068": {
|
| 197 |
+
"content": "<think>",
|
| 198 |
+
"lstrip": false,
|
| 199 |
+
"normalized": false,
|
| 200 |
+
"rstrip": false,
|
| 201 |
+
"single_word": false,
|
| 202 |
+
"special": false
|
| 203 |
+
},
|
| 204 |
+
"248069": {
|
| 205 |
+
"content": "</think>",
|
| 206 |
+
"lstrip": false,
|
| 207 |
+
"normalized": false,
|
| 208 |
+
"rstrip": false,
|
| 209 |
+
"single_word": false,
|
| 210 |
+
"special": false
|
| 211 |
+
},
|
| 212 |
+
"248070": {
|
| 213 |
+
"content": "<|audio_start|>",
|
| 214 |
+
"lstrip": false,
|
| 215 |
+
"normalized": false,
|
| 216 |
+
"rstrip": false,
|
| 217 |
+
"single_word": false,
|
| 218 |
+
"special": true
|
| 219 |
+
},
|
| 220 |
+
"248071": {
|
| 221 |
+
"content": "<|audio_end|>",
|
| 222 |
+
"lstrip": false,
|
| 223 |
+
"normalized": false,
|
| 224 |
+
"rstrip": false,
|
| 225 |
+
"single_word": false,
|
| 226 |
+
"special": true
|
| 227 |
+
},
|
| 228 |
+
"248072": {
|
| 229 |
+
"content": "<tts_pad>",
|
| 230 |
+
"lstrip": false,
|
| 231 |
+
"normalized": false,
|
| 232 |
+
"rstrip": false,
|
| 233 |
+
"single_word": false,
|
| 234 |
+
"special": true
|
| 235 |
+
},
|
| 236 |
+
"248073": {
|
| 237 |
+
"content": "<tts_text_bos>",
|
| 238 |
+
"lstrip": false,
|
| 239 |
+
"normalized": false,
|
| 240 |
+
"rstrip": false,
|
| 241 |
+
"single_word": false,
|
| 242 |
+
"special": true
|
| 243 |
+
},
|
| 244 |
+
"248074": {
|
| 245 |
+
"content": "<tts_text_eod>",
|
| 246 |
+
"lstrip": false,
|
| 247 |
+
"normalized": false,
|
| 248 |
+
"rstrip": false,
|
| 249 |
+
"single_word": false,
|
| 250 |
+
"special": true
|
| 251 |
+
},
|
| 252 |
+
"248075": {
|
| 253 |
+
"content": "<tts_text_bos_single>",
|
| 254 |
+
"lstrip": false,
|
| 255 |
+
"normalized": false,
|
| 256 |
+
"rstrip": false,
|
| 257 |
+
"single_word": false,
|
| 258 |
+
"special": true
|
| 259 |
+
},
|
| 260 |
+
"248076": {
|
| 261 |
+
"content": "<|audio_pad|>",
|
| 262 |
+
"lstrip": false,
|
| 263 |
+
"normalized": false,
|
| 264 |
+
"rstrip": false,
|
| 265 |
+
"single_word": false,
|
| 266 |
+
"special": true
|
| 267 |
+
},
|
| 268 |
+
"248077": {
|
| 269 |
+
"content": "<IMG_CONTEXT>",
|
| 270 |
+
"lstrip": false,
|
| 271 |
+
"normalized": false,
|
| 272 |
+
"rstrip": false,
|
| 273 |
+
"single_word": false,
|
| 274 |
+
"special": true
|
| 275 |
+
},
|
| 276 |
+
"248078": {
|
| 277 |
+
"content": "<img>",
|
| 278 |
+
"lstrip": false,
|
| 279 |
+
"normalized": false,
|
| 280 |
+
"rstrip": false,
|
| 281 |
+
"single_word": false,
|
| 282 |
+
"special": true
|
| 283 |
+
},
|
| 284 |
+
"248079": {
|
| 285 |
+
"content": "</img>",
|
| 286 |
+
"lstrip": false,
|
| 287 |
+
"normalized": false,
|
| 288 |
+
"rstrip": false,
|
| 289 |
+
"single_word": false,
|
| 290 |
+
"special": true
|
| 291 |
+
},
|
| 292 |
+
"248080": {
|
| 293 |
+
"content": "<quad>",
|
| 294 |
+
"lstrip": false,
|
| 295 |
+
"normalized": false,
|
| 296 |
+
"rstrip": false,
|
| 297 |
+
"single_word": false,
|
| 298 |
+
"special": true
|
| 299 |
+
},
|
| 300 |
+
"248081": {
|
| 301 |
+
"content": "</quad>",
|
| 302 |
+
"lstrip": false,
|
| 303 |
+
"normalized": false,
|
| 304 |
+
"rstrip": false,
|
| 305 |
+
"single_word": false,
|
| 306 |
+
"special": true
|
| 307 |
+
},
|
| 308 |
+
"248082": {
|
| 309 |
+
"content": "<ref>",
|
| 310 |
+
"lstrip": false,
|
| 311 |
+
"normalized": false,
|
| 312 |
+
"rstrip": false,
|
| 313 |
+
"single_word": false,
|
| 314 |
+
"special": true
|
| 315 |
+
},
|
| 316 |
+
"248083": {
|
| 317 |
+
"content": "</ref>",
|
| 318 |
+
"lstrip": false,
|
| 319 |
+
"normalized": false,
|
| 320 |
+
"rstrip": false,
|
| 321 |
+
"single_word": false,
|
| 322 |
+
"special": true
|
| 323 |
+
},
|
| 324 |
+
"248084": {
|
| 325 |
+
"content": "<box>",
|
| 326 |
+
"lstrip": false,
|
| 327 |
+
"normalized": false,
|
| 328 |
+
"rstrip": false,
|
| 329 |
+
"single_word": false,
|
| 330 |
+
"special": true
|
| 331 |
+
},
|
| 332 |
+
"248085": {
|
| 333 |
+
"content": "</box>",
|
| 334 |
+
"lstrip": false,
|
| 335 |
+
"normalized": false,
|
| 336 |
+
"rstrip": false,
|
| 337 |
+
"single_word": false,
|
| 338 |
+
"special": true
|
| 339 |
+
},
|
| 340 |
+
"248086": {
|
| 341 |
+
"content": "<|action_start|>",
|
| 342 |
+
"lstrip": false,
|
| 343 |
+
"normalized": false,
|
| 344 |
+
"rstrip": false,
|
| 345 |
+
"single_word": false,
|
| 346 |
+
"special": true
|
| 347 |
+
},
|
| 348 |
+
"248087": {
|
| 349 |
+
"content": "<|action_end|>",
|
| 350 |
+
"lstrip": false,
|
| 351 |
+
"normalized": false,
|
| 352 |
+
"rstrip": false,
|
| 353 |
+
"single_word": false,
|
| 354 |
+
"special": true
|
| 355 |
+
},
|
| 356 |
+
"248088": {
|
| 357 |
+
"content": "<|interpreter|>",
|
| 358 |
+
"lstrip": false,
|
| 359 |
+
"normalized": false,
|
| 360 |
+
"rstrip": false,
|
| 361 |
+
"single_word": false,
|
| 362 |
+
"special": true
|
| 363 |
+
},
|
| 364 |
+
"248089": {
|
| 365 |
+
"content": "<|plugin|>",
|
| 366 |
+
"lstrip": false,
|
| 367 |
+
"normalized": false,
|
| 368 |
+
"rstrip": false,
|
| 369 |
+
"single_word": false,
|
| 370 |
+
"special": true
|
| 371 |
+
},
|
| 372 |
+
"248090": {
|
| 373 |
+
"content": "<video>",
|
| 374 |
+
"lstrip": false,
|
| 375 |
+
"normalized": false,
|
| 376 |
+
"rstrip": false,
|
| 377 |
+
"single_word": false,
|
| 378 |
+
"special": true
|
| 379 |
+
},
|
| 380 |
+
"248091": {
|
| 381 |
+
"content": "<|ts|>",
|
| 382 |
+
"lstrip": false,
|
| 383 |
+
"normalized": false,
|
| 384 |
+
"rstrip": false,
|
| 385 |
+
"single_word": false,
|
| 386 |
+
"special": true
|
| 387 |
+
},
|
| 388 |
+
"248092": {
|
| 389 |
+
"content": "<|/ts|>",
|
| 390 |
+
"lstrip": false,
|
| 391 |
+
"normalized": false,
|
| 392 |
+
"rstrip": false,
|
| 393 |
+
"single_word": false,
|
| 394 |
+
"special": true
|
| 395 |
+
},
|
| 396 |
+
"248093": {
|
| 397 |
+
"content": "<TS_CONTEXT>",
|
| 398 |
+
"lstrip": false,
|
| 399 |
+
"normalized": false,
|
| 400 |
+
"rstrip": false,
|
| 401 |
+
"single_word": false,
|
| 402 |
+
"special": true
|
| 403 |
+
},
|
| 404 |
+
"248094": {
|
| 405 |
+
"content": "<TS_GEN>",
|
| 406 |
+
"lstrip": false,
|
| 407 |
+
"normalized": false,
|
| 408 |
+
"rstrip": false,
|
| 409 |
+
"single_word": false,
|
| 410 |
+
"special": true
|
| 411 |
+
},
|
| 412 |
+
"248095": {
|
| 413 |
+
"content": "<SMILES>",
|
| 414 |
+
"lstrip": false,
|
| 415 |
+
"normalized": false,
|
| 416 |
+
"rstrip": false,
|
| 417 |
+
"single_word": false,
|
| 418 |
+
"special": false
|
| 419 |
+
},
|
| 420 |
+
"248096": {
|
| 421 |
+
"content": "</SMILES>",
|
| 422 |
+
"lstrip": false,
|
| 423 |
+
"normalized": false,
|
| 424 |
+
"rstrip": false,
|
| 425 |
+
"single_word": false,
|
| 426 |
+
"special": false
|
| 427 |
+
},
|
| 428 |
+
"248097": {
|
| 429 |
+
"content": "<protein>",
|
| 430 |
+
"lstrip": false,
|
| 431 |
+
"normalized": false,
|
| 432 |
+
"rstrip": false,
|
| 433 |
+
"single_word": false,
|
| 434 |
+
"special": false
|
| 435 |
+
},
|
| 436 |
+
"248098": {
|
| 437 |
+
"content": "</protein>",
|
| 438 |
+
"lstrip": false,
|
| 439 |
+
"normalized": false,
|
| 440 |
+
"rstrip": false,
|
| 441 |
+
"single_word": false,
|
| 442 |
+
"special": false
|
| 443 |
+
},
|
| 444 |
+
"248099": {
|
| 445 |
+
"content": "<dna>",
|
| 446 |
+
"lstrip": false,
|
| 447 |
+
"normalized": false,
|
| 448 |
+
"rstrip": false,
|
| 449 |
+
"single_word": false,
|
| 450 |
+
"special": false
|
| 451 |
+
},
|
| 452 |
+
"248100": {
|
| 453 |
+
"content": "</dna>",
|
| 454 |
+
"lstrip": false,
|
| 455 |
+
"normalized": false,
|
| 456 |
+
"rstrip": false,
|
| 457 |
+
"single_word": false,
|
| 458 |
+
"special": false
|
| 459 |
+
},
|
| 460 |
+
"248101": {
|
| 461 |
+
"content": "<rna>",
|
| 462 |
+
"lstrip": false,
|
| 463 |
+
"normalized": false,
|
| 464 |
+
"rstrip": false,
|
| 465 |
+
"single_word": false,
|
| 466 |
+
"special": false
|
| 467 |
+
},
|
| 468 |
+
"248102": {
|
| 469 |
+
"content": "</rna>",
|
| 470 |
+
"lstrip": false,
|
| 471 |
+
"normalized": false,
|
| 472 |
+
"rstrip": false,
|
| 473 |
+
"single_word": false,
|
| 474 |
+
"special": false
|
| 475 |
+
}
|
| 476 |
+
},
|
| 477 |
+
"audio_bos_token": "<|audio_start|>",
|
| 478 |
+
"audio_eos_token": "<|audio_end|>",
|
| 479 |
+
"audio_token": "<|audio_pad|>",
|
| 480 |
+
"auto_map": {
|
| 481 |
+
"AutoTokenizer": [
|
| 482 |
+
"tokenization_interns1.InternS1Tokenizer",
|
| 483 |
+
null
|
| 484 |
+
]
|
| 485 |
+
},
|
| 486 |
+
"backend": "custom",
|
| 487 |
+
"bos_token": "<|im_start|>",
|
| 488 |
+
"clean_up_tokenization_spaces": false,
|
| 489 |
+
"eos_token": "<|im_end|>",
|
| 490 |
+
"errors": "replace",
|
| 491 |
+
"image_token": "<|image_pad|>",
|
| 492 |
+
"is_local": true,
|
| 493 |
+
"model_max_length": 262144,
|
| 494 |
+
"model_specific_special_tokens": {
|
| 495 |
+
"audio_bos_token": "<|audio_start|>",
|
| 496 |
+
"audio_eos_token": "<|audio_end|>",
|
| 497 |
+
"audio_token": "<|audio_pad|>",
|
| 498 |
+
"image_token": "<|image_pad|>",
|
| 499 |
+
"video_token": "<|video_pad|>",
|
| 500 |
+
"vision_bos_token": "<|vision_start|>",
|
| 501 |
+
"vision_eos_token": "<|vision_end|>"
|
| 502 |
+
},
|
| 503 |
+
"offset_PROT": 249126,
|
| 504 |
+
"offset_SMILES": 248102,
|
| 505 |
+
"offset_XNA": 250150,
|
| 506 |
+
"pad_token": "<|endoftext|>",
|
| 507 |
+
"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+",
|
| 508 |
+
"split_special_tokens": false,
|
| 509 |
+
"tokenizer_class": "InternS1Tokenizer",
|
| 510 |
+
"unk_token": null,
|
| 511 |
+
"video_token": "<|video_pad|>",
|
| 512 |
+
"vision_bos_token": "<|vision_start|>",
|
| 513 |
+
"vision_eos_token": "<|vision_end|>"
|
| 514 |
+
}
|
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
|
|
|