SwagMessiah100 Zhiding commited on
Commit
fcb976c
·
0 Parent(s):

Duplicate from nvidia/LocateAnything-3B

Browse files

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

.gitattributes ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ teaser.jpg filter=lfs diff=lfs merge=lfs -text
37
+ assets/decoding_demo.mp4 filter=lfs diff=lfs merge=lfs -text
38
+ assets/demo.mp4 filter=lfs diff=lfs merge=lfs -text
39
+ assets/coco_lvis.png filter=lfs diff=lfs merge=lfs -text
40
+ assets/dense_object_detection.png filter=lfs diff=lfs merge=lfs -text
41
+ assets/layout_ocr.png filter=lfs diff=lfs merge=lfs -text
42
+ assets/referring.png filter=lfs diff=lfs merge=lfs -text
43
+ assets/sspro.png filter=lfs diff=lfs merge=lfs -text
LICENSE ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ NVIDIA License
2
+
3
+ 1. Definitions
4
+
5
+ “Licensor” means any person or entity that distributes its Work.
6
+
7
+ “Work” means (a) the original work of authorship made available under this license,
8
+ which may include software, documentation, or other files, and (b) any additions to or
9
+ derivative works thereof that are made available under this license.
10
+
11
+ The terms “reproduce,” “reproduction,” “derivative works,” and “distribution” have the
12
+ meaning as provided under U.S. copyright law; provided, however, that for the purposes
13
+ of this license, derivative works shall not include works that remain separable from, or
14
+ merely link (or bind by name) to the interfaces of, the Work.
15
+
16
+ Works are “made available” under this license by including in or with the Work either
17
+ (a) a copyright notice referencing the applicability of this license to the Work, or
18
+ (b) a copy of this license.
19
+
20
+ 2. License Grant
21
+
22
+ 2.1 Copyright Grant. Subject to the terms and conditions of this license, each Licensor
23
+ grants to you a perpetual, worldwide, non-exclusive, royalty-free, copyright license to
24
+ use, reproduce, prepare derivative works of, publicly display, publicly perform,
25
+ sublicense and distribute its Work and any resulting derivative works in any form.
26
+
27
+ 3. Limitations
28
+
29
+ 3.1 Redistribution. You may reproduce or distribute the Work only if (a) you do so under
30
+ this license, (b) you include a complete copy of this license with your distribution,
31
+ and (c) you retain without modification any copyright, patent, trademark, or attribution
32
+ notices that are present in the Work.
33
+
34
+ 3.2 Derivative Works. You may specify that additional or different terms apply to the
35
+ use, reproduction, and distribution of your derivative works of the Work (“Your Terms”)
36
+ only if (a) Your Terms provide that the use limitation in Section 3.3 applies to your
37
+ derivative works, and (b) you identify the specific derivative works that are subject
38
+ to Your Terms. Notwithstanding Your Terms, this license (including the redistribution
39
+ requirements in Section 3.1) will continue to apply to the Work itself.
40
+
41
+ 3.3 Use Limitation. The Work and any derivative works thereof only may be used or
42
+ intended for use non-commercially. Notwithstanding the foregoing, NVIDIA Corporation
43
+ and its affiliates may use the Work and any derivative works commercially. As used
44
+ herein, “non-commercially” means for research or evaluation purposes only.
45
+
46
+ 3.4 Patent Claims. If you bring or threaten to bring a patent claim against any
47
+ Licensor (including any claim, cross-claim or counterclaim in a lawsuit) to enforce any
48
+ patents that you allege are infringed by any Work, then your rights under this license
49
+ from such Licensor (including the grant in Section 2.1) will terminate immediately.
50
+
51
+ 3.5 Trademarks. This license does not grant any rights to use any Licensor’s or its
52
+ affiliates’ names, logos, or trademarks, except as necessary to reproduce the notices
53
+ described in this license.
54
+
55
+ 3.6 Termination. If you violate any term of this license, then your rights under this
56
+ license (including the grant in Section 2.1) will terminate immediately.
57
+
58
+ 4. Disclaimer of Warranty.
59
+
60
+ THE WORK IS PROVIDED “AS IS” WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER
61
+ EXPRESS OR IMPLIED, INCLUDING WARRANTIES OR CONDITIONS OF MERCHANTABILITY, FITNESS FOR
62
+ A PARTICULAR PURPOSE, TITLE OR NON-INFRINGEMENT. YOU BEAR THE RISK OF UNDERTAKING ANY
63
+ ACTIVITIES UNDER THIS LICENSE.
64
+
65
+ 5. Limitation of Liability.
66
+
67
+ EXCEPT AS PROHIBITED BY APPLICABLE LAW, IN NO EVENT AND UNDER NO LEGAL THEORY, WHETHER
68
+ IN TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE SHALL ANY LICENSOR BE LIABLE TO
69
+ YOU FOR DAMAGES, INCLUDING ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
70
+ DAMAGES ARISING OUT OF OR RELATED TO THIS LICENSE, THE USE OR INABILITY TO USE THE WORK
71
+ (INCLUDING BUT NOT LIMITED TO LOSS OF GOODWILL, BUSINESS INTERRUPTION, LOST PROFITS OR
72
+ DATA, COMPUTER FAILURE OR MALFUNCTION, OR ANY OTHER DAMAGES OR LOSSES), EVEN IF THE
73
+ LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
74
+
README.md ADDED
@@ -0,0 +1,556 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: other
3
+ license_name: nvidia-license
4
+ license_link: https://huggingface.co/nvidia/LocateAnything-3B/blob/main/LICENSE
5
+ language:
6
+ - en
7
+ tags:
8
+ - nvidia
9
+ - eagle
10
+ - vision
11
+ - object-detection
12
+ - grounding
13
+ - locateanything
14
+ - arxiv:2605.27365
15
+ demo: https://huggingface.co/spaces/nvidia/LocateAnything
16
+ github: https://github.com/NVlabs/Eagle/tree/main/Embodied
17
+ library_name: transformers
18
+ pipeline_tag: image-text-to-text
19
+ base_model:
20
+ - Qwen/Qwen2.5-3B-Instruct
21
+ ---
22
+
23
+ # LocateAnything: Fast and High-Quality Vision-Language Grounding with Parallel Box Decoding
24
+
25
+ <p align="center">
26
+ <img src="assets/teaser.jpg" alt="LocateAnything teaser" width="100%">
27
+ </p>
28
+
29
+ ## 🔗 Quick Links
30
+
31
+ * 🚀 **Online Demo**: [LocateAnything (Hugging Face Spaces)](https://huggingface.co/spaces/nvidia/LocateAnything)
32
+ * 💻 **GitHub Code**: [NVlabs/Eagle/Embodied](https://github.com/NVlabs/Eagle/tree/main/Embodied)
33
+ * 📄 **Paper**: [arXiv:2605.27365](https://arxiv.org/abs/2605.27365)
34
+
35
+
36
+ # Model Overview
37
+
38
+ ### Description:
39
+
40
+ LocateAnything is a vision-language model for fast and high-quality visual grounding, enabling precise object localization, dense detection, and point-based localization across diverse domains in both Enterprise Intelligence and Physical AI. The model adopts a generalist design, supporting tasks such as referring expression grounding, multi-object detection, GUI element grounding, and text localization, with strong performance in complex and cluttered scenes.
41
+
42
+ Its core innovation, Parallel Box Decoding (PBD), predicts complete bounding box coordinates in a single parallel step rather than autoregressive token-by-token decoding, improving efficiency while preserving geometric consistency. This enables up to 2.5× higher throughput compared to prior approaches.
43
+
44
+ The model is trained on a large-scale multi-domain dataset (12M images, 138M+ queries, 785M bounding boxes) spanning natural scenes, robotics, driving, GUI interaction, and document understanding. It serves as a foundation for generalist multimodal perception and has been integrated into NVIDIA’s frontier production-grade vision-language models, such as Nemotron 3 Nano Omni, supporting grounding, GUI understanding, and multimodal agentic capabilities.
45
+
46
+ LocateAnything is developed as part of the [Eagle VLM](https://github.com/NVlabs/EAGLE) model family. This released model is for research and development only. In addition, LocateAnything contributed to [Nemotron](https://www.nvidia.com/en-us/ai-data-science/foundation-models/nemotron/) and [Cosmos](https://www.nvidia.com/en-us/ai/cosmos/) as part of the Computer Use and Visual Grounding features. We give special thanks the Nemotron and Cosmos Teams for time and efforts in product integration.
47
+
48
+ ### Demo Videos
49
+
50
+ <p align="left">
51
+ <video src="https://huggingface.co/nvidia/LocateAnything-3B/resolve/main/assets/demo.mp4" controls="controls" width="80%">
52
+ Your browser does not support the video tag.
53
+ </video>
54
+ </p>
55
+
56
+ <p align="left">
57
+ <video src="https://huggingface.co/nvidia/LocateAnything-3B/resolve/main/assets/decoding_demo.mp4" controls="controls" width="80%">
58
+ Your browser does not support the video tag.
59
+ </video>
60
+ </p>
61
+
62
+ ### License/Terms of Use:
63
+
64
+ This model is released under the [NVIDIA License](https://huggingface.co/nvidia/LocateAnything-3B/blob/main/LICENSE) for non-commercial use, which permits use, reproduction, and modification for **academic and non-profit research purposes only**. Commercial use is **not permitted**, except by NVIDIA and its affiliates. Redistribution must retain the license and all applicable copyright and attribution notices. The model is provided **“as is” without warranty of any kind**, and users assume all associated risks.
65
+
66
+ This model is built using components from third-party models with their respective licenses:
67
+ - Language model: [Qwen2.5-3B-Instruct](https://huggingface.co/Qwen/Qwen2.5-3B-Instruct) (Qwen Research License)
68
+ - Vision encoder: [MoonViT-SO-400M](https://huggingface.co/moonshotai/MoonViT-SO-400M) (MIT License)
69
+
70
+ Models are improved using Qwen.
71
+
72
+ ### Deployment Geography:
73
+
74
+ Global
75
+
76
+ ### Use Case:
77
+
78
+ LocateAnything-3B is intended for developers and researchers building vision-language models and applications that require fast and precise visual localization from natural language instructions.
79
+
80
+ Supported use cases include:
81
+ - Open-set, common, and long-tail object detection
82
+ - Dense multi-object detection in cluttered scenes
83
+ - Phrase and referring-expression grounding
84
+ - Automated dataset labeling and annotation (e.g., detection, grounding, pointing)
85
+ - GUI element grounding for interactive and agentic systems
86
+ - Robotics and autonomous driving perception
87
+ - Document understanding, layout grounding, and OCR localization
88
+ - Industrial inspection, surveillance, and remote sensing applications
89
+ - Point-based localization and fine-grained spatial reasoning
90
+
91
+ ### Release Date [Insert the expected release date below]:
92
+
93
+ - Github [05/26/2026] via https://github.com/NVlabs/Eagle/tree/main/Embodied.
94
+ - Hugging Face [05/26/2026] via https://huggingface.co/nvidia/LocateAnything-3B.
95
+ - Demo [05/26/2026] via https://huggingface.co/spaces/nvidia/LocateAnything.
96
+ - Webpage [05/26/2026] via https://research.nvidia.com/labs/lpr/locate-anything/.
97
+ - Tech Report [05/26/2026] via https://research.nvidia.com/labs/lpr/locate-anything/LocateAnything.pdf
98
+
99
+ ## References(s):
100
+ - Wang et al., [LocateAnything: Fast and High-Quality Vision-Language Grounding with Parallel Box Decoding](https://research.nvidia.com/labs/lpr/locate-anything/LocateAnything.pdf), NVIDIA Tech Report, 2026
101
+ - Kimi Team, [Kimi-VL Technical Report](https://arxiv.org/abs/2504.07491), arXiv:2504.07491, 2025.
102
+ - Qwen Team, [Qwen2.5: A Party of Foundation Models](https://qwen.ai/blog?id=qwen2.5), Qwen Blog, 2024.
103
+ - Chen et al., [Pix2Seq: A Language Modeling Framework for Object Detection](https://arxiv.org/abs/2109.10852), ICLR, 2022.
104
+ - Jiang et al., [Detect Anything via Next Point Prediction](https://arxiv.org/abs/2510.12798), arXiv:2510.12798, 2025.
105
+ - Liu et al., [Grounding DINO: Marrying DINO with Grounded Pre-Training for Open-Set Object Detection](https://arxiv.org/abs/2303.05499), arXiv:2303.05499, 2023.
106
+ - Lin et al., [Microsoft COCO: Common Objects in Context](https://arxiv.org/abs/1405.0312), ECCV, 2014.
107
+ - Gupta et al., [LVIS: A Dataset for Large Vocabulary Instance Segmentation](https://arxiv.org/abs/1908.03195), CVPR, 2019.
108
+ - Li et al., [ScreenSpot-Pro: GUI Grounding for Professional High-Resolution Computer Use](https://arxiv.org/abs/2504.07981), ACM MM, 2025.
109
+
110
+ ## Model Architecture:
111
+
112
+ **Architecture Type:** Transformer-based vision-language model (VLM).
113
+
114
+ **Network Architecture:** Native-resolution VLM with the following components:
115
+ - Vision encoder: MoonViT
116
+ - Language model: Qwen2.5-3B-Instruct
117
+ - Multimodal projector: MLP projector
118
+ - Output formulation: Block-based structure for visual grounding
119
+
120
+ **Number of model parameters:** 3B.
121
+
122
+ LocateAnything extends a vision-language model with Parallel Box Decoding (PBD), a block-wise multi-token prediction framework for efficient visual grounding. Instead of autoregressive coordinate generation, the model predicts complete bounding boxes and points in parallel structured units, improving decoding efficiency while preserving geometric consistency. The architecture jointly optimizes next-token prediction and multi-token prediction to balance reasoning ability and parallel inference. Training follows a four-stage pipeline: initial multimodal knowledge adaptation using captioning, VQA, OCR, and related data, followed by grounding and dense-scene localization fine-tuning.
123
+
124
+ ## Input(s):
125
+
126
+ **Input Type(s):** Image and Text.
127
+
128
+ **Input Format(s):**
129
+ - Image: RGB image input with original source resolution.
130
+ - Text: Natural-language prompt or task template, such as object categories, referring expressions, GUI instructions, OCR/layout requests, or pointing queries.
131
+
132
+ **Input Parameters:**
133
+ - Image: Two-Dimensional (2D)
134
+ - Text: One-Dimensional (1D)
135
+
136
+ **Other Properties Related to Input:**
137
+ - Production image resolution supports up to 2.5K.
138
+ - Prompt length supports up to 24K tokens.
139
+ - Training detection and grounding stages use a maximum sequence length of 25,600 tokens.
140
+ - Inference supports up to 8,192 newly generated tokens.
141
+
142
+ ## Output(s):
143
+
144
+ **Output Type(s):** Text.
145
+
146
+ **Output Format(s):**
147
+ - Text: Model-generated token sequence containing semantic labels and structured coordinate tokens, such as bounding boxes (`<box> x1, y1, x2, y2 </box>`) and points (`<box> x, y </box>`).
148
+
149
+ **Output Parameters:**
150
+ - Text: One-Dimensional (1D)
151
+ - Bounding boxes/points: Two-Dimensional (2D) spatial coordinates
152
+
153
+ **Other Properties Related to Output:**
154
+ - Outputs are organized into fixed-length blocks (length 6), including Semantic, Box, Negative, and End blocks.
155
+ - A Box block encodes quantized spatial coordinates with structural tokens; unused positions are padded with `<null>`.
156
+ - Fast Mode predicts box-aligned blocks in parallel; Slow Mode uses autoregressive decoding; Hybrid Mode defaults to parallel decoding with fallback to autoregressive decoding for format irregularity or spatial ambiguity.
157
+
158
+ Our AI models are designed and optimized to run on NVIDIA GPU-accelerated systems. By leveraging NVIDIA hardware (e.g., GPU cores) and software frameworks (e.g., CUDA libraries), the model achieves improved training and inference performance compared to CPU-only solutions.
159
+
160
+ ## Software Integration:
161
+ **Runtime Engine(s):**
162
+ * Transformers. The inference setup uses standard VLM generation with BF16 precision and KV cache. TensorRT, TensorRT-LLM, and Triton are not yet supported.
163
+
164
+ **Supported Hardware Microarchitecture Compatibility:**
165
+
166
+ * NVIDIA Ampere (e.g., A100)
167
+ * NVIDIA Blackwell
168
+ * NVIDIA Hopper (e.g., H100)
169
+ * NVIDIA Lovelace (e.g., L40, RTX 4090)
170
+
171
+ Deployment on embedded platforms such as NVIDIA Thor is possible with additional model optimization, including quantization, compression, or distillation. Other architectures may be supported depending on available memory, precision support, and software configuration.
172
+
173
+ **Supported Operating System(s):**
174
+ * Linux
175
+
176
+ The integration of foundation and fine-tuned models into AI systems requires additional testing using use-case-specific data to ensure safe and effective deployment. Following the V-model methodology, iterative testing and validation at both unit and system levels are essential to mitigate risks, meet technical and functional requirements, and ensure compliance with safety and ethical standards before deployment.
177
+
178
+ ## Model Version(s):
179
+ LocateAnything-3B: 3B-parameter research model variant evaluated in Hybrid Mode by default. Fast, Hybrid, and Slow inference modes are supported by the same model formulation.
180
+
181
+ LocateAnything-3B can be integrated into systems that require spatial grounding from natural language, such as GUI agents, robotics/embodied agents, document-understanding pipelines, OCR/text localization, and open-world detection workflows.
182
+
183
+ ## Training, Testing, and Evaluation Datasets:
184
+
185
+ ### Data Modality:
186
+ Image and Text. <br>
187
+ * Image <br>
188
+ * Text <br>
189
+
190
+ ### Training Data Size:
191
+ **Image Training Data Size:** <br>
192
+ * 1 Million to 1 Billion Images - 12M unique images. <br>
193
+
194
+ **Text Training Data Size:** <br>
195
+ * 1 Billion to 10 Trillion Tokens - Derived from approximately 140M natural-language queries. <br>
196
+
197
+ **Data Collection Method by dataset:** <br>
198
+ - Hybrid: Human, Automated <br>
199
+ Data is collected from human-curated and open-source datasets, as well as automated ingestion of publicly available data sources.
200
+
201
+ **Labeling Method by dataset:** <br>
202
+ - Hybrid: Human, Synthetic, Automated <br>
203
+ Labeling includes original human or open-source annotations, along with model-assisted and synthetic annotation generation using Qwen3-VL, Molmo, SAM 3, and Rex-Omni, with automated post-verification.
204
+
205
+ **Properties:** The training data consists of supervised fine-tuning (SFT) datasets with multimodal inputs, primarily image-text pairs and structured annotations such as bounding boxes, points, and negative samples.
206
+
207
+ The data spans multiple domains, including grounding, open-world grounding, general and dense object detection, scene text detection, GUI understanding and grounding, document layout understanding, and OCR.
208
+
209
+ Modalities include visual inputs (images) and natural-language queries or instructions. The dataset is derived from a mixture of publicly available academic datasets, along with model-assisted and synthetic annotations. It may include publicly available and potentially copyrighted content; users are responsible for ensuring compliance with applicable usage rights.
210
+
211
+ The linguistic content primarily consists of short, task-oriented natural-language expressions, such as object categories, referring expressions, GUI instructions, OCR queries, and grounding prompts, typically in English.
212
+
213
+ ## Evaluation Dataset:
214
+
215
+ **Data Collection Method by dataset:**
216
+ - Hybrid: Human, Automated
217
+
218
+ **Labeling Method by dataset:**
219
+ - Hybrid: Human, Synthetic, Automated
220
+
221
+ **Properties:** The evaluation datasets consist of publicly available benchmarks spanning visual grounding, object detection, document understanding, scene text detection, and GUI-related tasks. Modalities include image inputs paired with natural-language queries and structured annotations such as bounding boxes and points.
222
+
223
+ The evaluation suite covers both box-level and point-level grounding tasks, with approximately 48K images for box evaluation and 35K images for point evaluation across multiple datasets. These datasets span diverse domains including natural scenes, documents, aerial imagery, and human-centric interactions, enabling comprehensive assessment of localization accuracy and robustness.
224
+
225
+ Evaluation queries are typically short, task-oriented natural-language expressions such as referring phrases, object categories, and grounding prompts.
226
+
227
+ Performance is measured using box-based F1 at IoU thresholds of 0.5 and 0.95, as well as mean IoU for detection, layout, and OCR tasks. Point-based localization is evaluated based on whether predicted points fall within ground-truth segmentation masks or bounding boxes. Inference efficiency is reported in boxes per second (BPS) on a single NVIDIA H100 GPU with batch size 1.
228
+
229
+ ## Quantitative Evaluation Benchmarks
230
+
231
+ ### General Object Detection
232
+ <p align="left">
233
+ <img src="assets/coco_lvis.png" width="700">
234
+ </p>
235
+
236
+ ### Dense Object Detection
237
+ <p align="left">
238
+ <img src="assets/dense_object_detection.png" width="700">
239
+ </p>
240
+
241
+ ### GUI Understanding
242
+ <p align="left">
243
+ <img src="assets/sspro.png" width="700">
244
+ </p>
245
+
246
+ ### Layout Grounding and OCR
247
+ <p align="left">
248
+ <img src="assets/layout_ocr.png" width="700">
249
+ </p>
250
+
251
+ ### Referring Expression Grounding
252
+ <p align="left">
253
+ <img src="assets/referring.png" width="700">
254
+ </p>
255
+
256
+ ### Pointing
257
+ <p align="left">
258
+ <img src="assets/pointing.png" width="700">
259
+ </p>
260
+
261
+ ## Inference:
262
+
263
+ Test Hardware: H100 & A100
264
+
265
+ We suggest using `max_new_tokens=8192` and `generation_mode="hybrid"` to avoid truncated response and balance speed with accuracy.
266
+
267
+ ### Batch Hybrid Inference
268
+
269
+ This release includes `batch_infer.py`, `batch_utils`, and `kernel_utils` for
270
+ high-throughput detection and grounding. The `la_flash` backend is a pure
271
+ FlashAttention-varlen sparse range executor: it keeps LocateAnything's hybrid
272
+ MTP decoding path, avoids dense `[B,H,Q,K]` SDPA masks, and does not require a
273
+ custom CUDA extension build.
274
+
275
+ Use it with:
276
+
277
+ ```bash
278
+ python batch_infer.py \
279
+ --model . \
280
+ --attn la_flash \
281
+ --scheduler pipeline \
282
+ --batch-size 4 \
283
+ --image /path/to/image.jpg \
284
+ --query "person</c>car"
285
+ ```
286
+
287
+ A100 4K probe, real 3840x2160 street image, `query=vehicle`,
288
+ `batch_size=4`, raw PIL input, `in_token_limit=25600`, hybrid MTP inference:
289
+
290
+ | Backend | Attention Path | Time | Peak Reserved Memory |
291
+ | --- | --- | ---: | ---: |
292
+ | `sdpa` | Dense SDPA masks | 8.2600 s | 35.12 GB |
293
+ | `la_flash` | FlashAttention sparse range plan | 8.0314 s | 11.71 GB |
294
+
295
+ See `batch_utils/README.md` and `kernel_utils/README.md` for runtime knobs and
296
+ implementation details.
297
+
298
+ ### Installation
299
+
300
+ ```bash
301
+ pip install opencv-python-headless==4.11.0.86 transformers==4.57.1 numpy==1.25.0 Pillow==11.1.0 peft torchvision decord==0.6.0 lmdb==1.7.5
302
+ ```
303
+
304
+ > PyTorch (`torch`) must be installed separately according to your CUDA version. See [pytorch.org/get-started](https://pytorch.org/get-started/locally/).
305
+
306
+ Optional — [MagiAttention](https://sandai-org.github.io/MagiAttention/docs/main/user_guide/install.html) (Hopper / Blackwell GPUs only, recommended for faster MTP inference):
307
+
308
+ ```bash
309
+ git clone https://github.com/SandAI-org/MagiAttention.git
310
+ cd MagiAttention
311
+ git checkout v1.0.5
312
+ git submodule update --init --recursive
313
+ pip install -r requirements.txt
314
+ pip install --no-build-isolation .
315
+ ```
316
+
317
+ If MagiAttention is installed, the model will automatically use it for efficient MTP block-diffusion attention. If not installed, it will fall back to PyTorch SDPA — fully functional but slower for MTP decoding.
318
+
319
+ ### Worker (recommended)
320
+
321
+ Below is a self-contained worker that loads the model once and serves perception queries via a unified `predict()` plus task-specific convenience methods. You can drop this class into any FastAPI / gRPC / Triton serving framework.
322
+
323
+ ```python
324
+ import re
325
+ import torch
326
+ from PIL import Image
327
+ from transformers import AutoModel, AutoTokenizer, AutoProcessor
328
+
329
+
330
+ class LocateAnythingWorker:
331
+ """Stateful worker that loads the model once and serves perception queries."""
332
+
333
+ def __init__(self, model_path: str, device: str = "cuda", dtype=torch.bfloat16):
334
+ self.device = device
335
+ self.dtype = dtype
336
+
337
+ self.tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
338
+ self.processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
339
+ self.model = AutoModel.from_pretrained(
340
+ model_path,
341
+ torch_dtype=dtype,
342
+ trust_remote_code=True,
343
+ ).to(device).eval()
344
+
345
+ @torch.no_grad()
346
+ def predict(
347
+ self,
348
+ image: Image.Image,
349
+ question: str,
350
+ generation_mode: str = "hybrid", # "fast" (MTP) | "slow" (NTP/AR) | "hybrid"
351
+ max_new_tokens: int = 2048,
352
+ temperature: float = 0.7,
353
+ verbose: bool = True,
354
+ ) -> dict:
355
+ messages = [
356
+ {"role": "user", "content": [
357
+ {"type": "image", "image": image},
358
+ {"type": "text", "text": question},
359
+ ]}
360
+ ]
361
+
362
+ text = self.processor.py_apply_chat_template(
363
+ messages, tokenize=False, add_generation_prompt=True
364
+ )
365
+ images, videos = self.processor.process_vision_info(messages)
366
+ inputs = self.processor(
367
+ text=[text], images=images, videos=videos, return_tensors="pt"
368
+ ).to(self.device)
369
+
370
+ pixel_values = inputs["pixel_values"].to(self.dtype)
371
+ input_ids = inputs["input_ids"]
372
+ image_grid_hws = inputs.get("image_grid_hws", None)
373
+
374
+ response = self.model.generate(
375
+ pixel_values=pixel_values,
376
+ input_ids=input_ids,
377
+ attention_mask=inputs["attention_mask"],
378
+ image_grid_hws=image_grid_hws,
379
+ tokenizer=self.tokenizer,
380
+ max_new_tokens=max_new_tokens,
381
+ use_cache=True,
382
+ generation_mode=generation_mode,
383
+ temperature=temperature,
384
+ do_sample=True,
385
+ top_p=0.9,
386
+ repetition_penalty=1.1,
387
+ verbose=verbose,
388
+ )
389
+
390
+ result = {"answer": response[0] if isinstance(response, tuple) else response}
391
+ if isinstance(response, tuple) and len(response) >= 3:
392
+ result["history"] = response[1]
393
+ result["stats"] = response[2]
394
+ return result
395
+
396
+ # ---- Convenience methods for each task ----
397
+
398
+ def detect(self, image: Image.Image, categories: list[str], **kwargs) -> dict:
399
+ """Object detection / document layout analysis."""
400
+ cats = "</c>".join(categories)
401
+ prompt = f"Locate all the instances that matches the following description: {cats}."
402
+ return self.predict(image, prompt, **kwargs)
403
+
404
+ def ground_single(self, image: Image.Image, phrase: str, **kwargs) -> dict:
405
+ """Phrase grounding — single instance."""
406
+ prompt = f"Locate a single instance that matches the following description: {phrase}."
407
+ return self.predict(image, prompt, **kwargs)
408
+
409
+ def ground_multi(self, image: Image.Image, phrase: str, **kwargs) -> dict:
410
+ """Phrase grounding — multiple instances."""
411
+ prompt = f"Locate all the instances that match the following description: {phrase}."
412
+ return self.predict(image, prompt, **kwargs)
413
+
414
+ def ground_text(self, image: Image.Image, phrase: str, **kwargs) -> dict:
415
+ """Text grounding."""
416
+ prompt = f"Please locate the text referred as {phrase}."
417
+ return self.predict(image, prompt, **kwargs)
418
+
419
+ def detect_text(self, image: Image.Image, **kwargs) -> dict:
420
+ """Scene text detection."""
421
+ prompt = "Detect all the text in box format."
422
+ return self.predict(image, prompt, **kwargs)
423
+
424
+ def ground_gui(self, image: Image.Image, phrase: str, output_type: str = "box", **kwargs) -> dict:
425
+ """GUI grounding (box or point)."""
426
+ if output_type == "point":
427
+ prompt = f"Point to: {phrase}."
428
+ else:
429
+ prompt = f"Locate the region that matches the following description: {phrase}."
430
+ return self.predict(image, prompt, **kwargs)
431
+
432
+ def point(self, image: Image.Image, phrase: str, **kwargs) -> dict:
433
+ """Pointing."""
434
+ prompt = f"Point to: {phrase}."
435
+ return self.predict(image, prompt, **kwargs)
436
+
437
+ # ---- Utility: parse model output ----
438
+
439
+ @staticmethod
440
+ def parse_boxes(answer: str, image_width: int, image_height: int) -> list[dict]:
441
+ """Parse model output into pixel-coordinate bounding boxes.
442
+
443
+ Coordinates in model output are normalized integers in [0, 1000].
444
+ """
445
+ boxes = []
446
+ for m in re.finditer(r"<box><(\d+)><(\d+)><(\d+)><(\d+)></box>", answer):
447
+ x1, y1, x2, y2 = [int(g) for g in m.groups()]
448
+ boxes.append({
449
+ "x1": x1 / 1000 * image_width,
450
+ "y1": y1 / 1000 * image_height,
451
+ "x2": x2 / 1000 * image_width,
452
+ "y2": y2 / 1000 * image_height,
453
+ })
454
+ return boxes
455
+
456
+ @staticmethod
457
+ def parse_points(answer: str, image_width: int, image_height: int) -> list[dict]:
458
+ """Parse model output into pixel-coordinate points."""
459
+ points = []
460
+ for m in re.finditer(r"<box><(\d+)><(\d+)></box>", answer):
461
+ x, y = int(m.group(1)), int(m.group(2))
462
+ points.append({
463
+ "x": x / 1000 * image_width,
464
+ "y": y / 1000 * image_height,
465
+ })
466
+ return points
467
+ ```
468
+
469
+ ### Usage Example
470
+
471
+ ```python
472
+ worker = LocateAnythingWorker("nvidia/LocateAnything-3B")
473
+ img = Image.open("example.jpg").convert("RGB")
474
+
475
+ # Object Detection
476
+ result = worker.detect(img, ["person", "car", "bicycle"])
477
+ print("Detection:", result["answer"])
478
+
479
+ # Phrase Grounding (multiple)
480
+ result = worker.ground_multi(img, "people wearing red shirts")
481
+ print("Grounding:", result["answer"])
482
+
483
+ # Scene Text Detection
484
+ result = worker.detect_text(img)
485
+ print("Text Detection:", result["answer"])
486
+
487
+ # Pointing
488
+ result = worker.point(img, "the traffic light")
489
+ print("Pointing:", result["answer"])
490
+
491
+ # GUI Grounding (point)
492
+ result = worker.ground_gui(img, "the search button", output_type="point")
493
+ print("GUI Point:", result["answer"])
494
+
495
+ # Parse structured output into pixel coordinates
496
+ w, h = img.size
497
+ boxes = LocateAnythingWorker.parse_boxes(result["answer"], w, h)
498
+ points = LocateAnythingWorker.parse_points(result["answer"], w, h)
499
+ ```
500
+
501
+ ### Supported Tasks & Prompt Templates
502
+
503
+ | Task | Worker Method | Output | Prompt Template |
504
+ | --- | --- | --- | --- |
505
+ | Object Detection | `worker.detect(img, [...])` | Box | `Locate all the instances that matches the following description: [CATEGORIES].` |
506
+ | Phrase Grounding (single) | `worker.ground_single(img, phrase)` | Single Box | `Locate a single instance that matches the following description: [PHRASE].` |
507
+ | Phrase Grounding (multi) | `worker.ground_multi(img, phrase)` | Multiple Boxes | `Locate all the instances that match the following description: [PHRASE].` |
508
+ | Text Grounding | `worker.ground_text(img, phrase)` | Box | `Please locate the text referred as [PHRASE].` |
509
+ | Scene Text Detection | `worker.detect_text(img)` | Box | `Detect all the text in box format.` |
510
+ | Document Layout Analysis | `worker.detect(img, [...])` | Box | `Locate all the instances that matches the following description: [CATEGORIES].` |
511
+ | GUI Grounding (box) | `worker.ground_gui(img, phrase, "box")` | Box | `Locate the region that matches the following description: [PHRASE].` |
512
+ | GUI Grounding (point) / Pointing | `worker.ground_gui(img, phrase, "point")` / `worker.point(img, phrase)` | Point | `Point to: [PHRASE].` |
513
+
514
+ `[PHRASE]` is a free-form natural-language description; `[CATEGORIES]` is a comma-separated list (multiple categories may also be joined with `</c>`).
515
+
516
+ ### Generation Modes
517
+
518
+ | Mode | Description | Speed | Accuracy |
519
+ | --- | --- | --- | --- |
520
+ | `fast` | MTP only, never falls back to AR | Fastest | Good for simple scenes |
521
+ | `slow` | Pure auto-regressive decoding | Slowest | Most robust |
522
+ | `hybrid` (default) | MTP first, falls back to AR on uncertain boxes, switches back after box boundary | Balanced | Best overall |
523
+
524
+ ## Batch Utils and Kernel Utils
525
+
526
+ This repository also includes optional utilities for high-throughput detection
527
+ runs:
528
+
529
+ - `batch_infer.py`: JSONL/image-query batch inference CLI.
530
+ - `batch_utils/`: batched hybrid generation runtime. See
531
+ `batch_utils/README.md`.
532
+ - `kernel_utils/`: LA Flash sparse range utilities. See
533
+ `kernel_utils/README.md`.
534
+
535
+ Run a small batch inference job:
536
+
537
+ ```bash
538
+ python batch_infer.py \
539
+ --model . \
540
+ --attn la_flash \
541
+ --scheduler pipeline \
542
+ --batch-size 4 \
543
+ --image assets/pointing.png \
544
+ --query "the object being pointed at"
545
+ ```
546
+
547
+ The batched sparse-plan decode runtime is intended for inference/evaluation and
548
+ does not support the training `labels` path. Training remains on the
549
+ MagiAttention backend.
550
+
551
+ ## Ethical Considerations:
552
+ NVIDIA believes Trustworthy AI is a shared responsibility and we have established policies and practices to enable development for a wide array of AI applications. When downloaded or used in accordance with our terms of service, developers should work with their internal model team to ensure this model meets requirements for the relevant industry and use case and addresses unforeseen product misuse.
553
+
554
+ Please make sure you have proper rights and permissions for all input image and video content; if image or video includes people, personal health information, or intellectual property, the image or video generated will not blur or maintain proportions of image subjects included.
555
+
556
+ Please report model quality, risk, security vulnerabilities or NVIDIA AI Concerns [here](https://app.intigriti.com/programs/nvidia/nvidiavdp/detail).
added_tokens.json ADDED
@@ -0,0 +1,1040 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "</box>": 151669,
3
+ "</c>": 152680,
4
+ "</img>": 151667,
5
+ "</interval>": 151675,
6
+ "</quad>": 151671,
7
+ "</ref>": 151673,
8
+ "</tool_call>": 151658,
9
+ "<0>": 151677,
10
+ "<1000>": 152677,
11
+ "<100>": 151777,
12
+ "<101>": 151778,
13
+ "<102>": 151779,
14
+ "<103>": 151780,
15
+ "<104>": 151781,
16
+ "<105>": 151782,
17
+ "<106>": 151783,
18
+ "<107>": 151784,
19
+ "<108>": 151785,
20
+ "<109>": 151786,
21
+ "<10>": 151687,
22
+ "<110>": 151787,
23
+ "<111>": 151788,
24
+ "<112>": 151789,
25
+ "<113>": 151790,
26
+ "<114>": 151791,
27
+ "<115>": 151792,
28
+ "<116>": 151793,
29
+ "<117>": 151794,
30
+ "<118>": 151795,
31
+ "<119>": 151796,
32
+ "<11>": 151688,
33
+ "<120>": 151797,
34
+ "<121>": 151798,
35
+ "<122>": 151799,
36
+ "<123>": 151800,
37
+ "<124>": 151801,
38
+ "<125>": 151802,
39
+ "<126>": 151803,
40
+ "<127>": 151804,
41
+ "<128>": 151805,
42
+ "<129>": 151806,
43
+ "<12>": 151689,
44
+ "<130>": 151807,
45
+ "<131>": 151808,
46
+ "<132>": 151809,
47
+ "<133>": 151810,
48
+ "<134>": 151811,
49
+ "<135>": 151812,
50
+ "<136>": 151813,
51
+ "<137>": 151814,
52
+ "<138>": 151815,
53
+ "<139>": 151816,
54
+ "<13>": 151690,
55
+ "<140>": 151817,
56
+ "<141>": 151818,
57
+ "<142>": 151819,
58
+ "<143>": 151820,
59
+ "<144>": 151821,
60
+ "<145>": 151822,
61
+ "<146>": 151823,
62
+ "<147>": 151824,
63
+ "<148>": 151825,
64
+ "<149>": 151826,
65
+ "<14>": 151691,
66
+ "<150>": 151827,
67
+ "<151>": 151828,
68
+ "<152>": 151829,
69
+ "<153>": 151830,
70
+ "<154>": 151831,
71
+ "<155>": 151832,
72
+ "<156>": 151833,
73
+ "<157>": 151834,
74
+ "<158>": 151835,
75
+ "<159>": 151836,
76
+ "<15>": 151692,
77
+ "<160>": 151837,
78
+ "<161>": 151838,
79
+ "<162>": 151839,
80
+ "<163>": 151840,
81
+ "<164>": 151841,
82
+ "<165>": 151842,
83
+ "<166>": 151843,
84
+ "<167>": 151844,
85
+ "<168>": 151845,
86
+ "<169>": 151846,
87
+ "<16>": 151693,
88
+ "<170>": 151847,
89
+ "<171>": 151848,
90
+ "<172>": 151849,
91
+ "<173>": 151850,
92
+ "<174>": 151851,
93
+ "<175>": 151852,
94
+ "<176>": 151853,
95
+ "<177>": 151854,
96
+ "<178>": 151855,
97
+ "<179>": 151856,
98
+ "<17>": 151694,
99
+ "<180>": 151857,
100
+ "<181>": 151858,
101
+ "<182>": 151859,
102
+ "<183>": 151860,
103
+ "<184>": 151861,
104
+ "<185>": 151862,
105
+ "<186>": 151863,
106
+ "<187>": 151864,
107
+ "<188>": 151865,
108
+ "<189>": 151866,
109
+ "<18>": 151695,
110
+ "<190>": 151867,
111
+ "<191>": 151868,
112
+ "<192>": 151869,
113
+ "<193>": 151870,
114
+ "<194>": 151871,
115
+ "<195>": 151872,
116
+ "<196>": 151873,
117
+ "<197>": 151874,
118
+ "<198>": 151875,
119
+ "<199>": 151876,
120
+ "<19>": 151696,
121
+ "<1>": 151678,
122
+ "<200>": 151877,
123
+ "<201>": 151878,
124
+ "<202>": 151879,
125
+ "<203>": 151880,
126
+ "<204>": 151881,
127
+ "<205>": 151882,
128
+ "<206>": 151883,
129
+ "<207>": 151884,
130
+ "<208>": 151885,
131
+ "<209>": 151886,
132
+ "<20>": 151697,
133
+ "<210>": 151887,
134
+ "<211>": 151888,
135
+ "<212>": 151889,
136
+ "<213>": 151890,
137
+ "<214>": 151891,
138
+ "<215>": 151892,
139
+ "<216>": 151893,
140
+ "<217>": 151894,
141
+ "<218>": 151895,
142
+ "<219>": 151896,
143
+ "<21>": 151698,
144
+ "<220>": 151897,
145
+ "<221>": 151898,
146
+ "<222>": 151899,
147
+ "<223>": 151900,
148
+ "<224>": 151901,
149
+ "<225>": 151902,
150
+ "<226>": 151903,
151
+ "<227>": 151904,
152
+ "<228>": 151905,
153
+ "<229>": 151906,
154
+ "<22>": 151699,
155
+ "<230>": 151907,
156
+ "<231>": 151908,
157
+ "<232>": 151909,
158
+ "<233>": 151910,
159
+ "<234>": 151911,
160
+ "<235>": 151912,
161
+ "<236>": 151913,
162
+ "<237>": 151914,
163
+ "<238>": 151915,
164
+ "<239>": 151916,
165
+ "<23>": 151700,
166
+ "<240>": 151917,
167
+ "<241>": 151918,
168
+ "<242>": 151919,
169
+ "<243>": 151920,
170
+ "<244>": 151921,
171
+ "<245>": 151922,
172
+ "<246>": 151923,
173
+ "<247>": 151924,
174
+ "<248>": 151925,
175
+ "<249>": 151926,
176
+ "<24>": 151701,
177
+ "<250>": 151927,
178
+ "<251>": 151928,
179
+ "<252>": 151929,
180
+ "<253>": 151930,
181
+ "<254>": 151931,
182
+ "<255>": 151932,
183
+ "<256>": 151933,
184
+ "<257>": 151934,
185
+ "<258>": 151935,
186
+ "<259>": 151936,
187
+ "<25>": 151702,
188
+ "<260>": 151937,
189
+ "<261>": 151938,
190
+ "<262>": 151939,
191
+ "<263>": 151940,
192
+ "<264>": 151941,
193
+ "<265>": 151942,
194
+ "<266>": 151943,
195
+ "<267>": 151944,
196
+ "<268>": 151945,
197
+ "<269>": 151946,
198
+ "<26>": 151703,
199
+ "<270>": 151947,
200
+ "<271>": 151948,
201
+ "<272>": 151949,
202
+ "<273>": 151950,
203
+ "<274>": 151951,
204
+ "<275>": 151952,
205
+ "<276>": 151953,
206
+ "<277>": 151954,
207
+ "<278>": 151955,
208
+ "<279>": 151956,
209
+ "<27>": 151704,
210
+ "<280>": 151957,
211
+ "<281>": 151958,
212
+ "<282>": 151959,
213
+ "<283>": 151960,
214
+ "<284>": 151961,
215
+ "<285>": 151962,
216
+ "<286>": 151963,
217
+ "<287>": 151964,
218
+ "<288>": 151965,
219
+ "<289>": 151966,
220
+ "<28>": 151705,
221
+ "<290>": 151967,
222
+ "<291>": 151968,
223
+ "<292>": 151969,
224
+ "<293>": 151970,
225
+ "<294>": 151971,
226
+ "<295>": 151972,
227
+ "<296>": 151973,
228
+ "<297>": 151974,
229
+ "<298>": 151975,
230
+ "<299>": 151976,
231
+ "<29>": 151706,
232
+ "<2>": 151679,
233
+ "<300>": 151977,
234
+ "<301>": 151978,
235
+ "<302>": 151979,
236
+ "<303>": 151980,
237
+ "<304>": 151981,
238
+ "<305>": 151982,
239
+ "<306>": 151983,
240
+ "<307>": 151984,
241
+ "<308>": 151985,
242
+ "<309>": 151986,
243
+ "<30>": 151707,
244
+ "<310>": 151987,
245
+ "<311>": 151988,
246
+ "<312>": 151989,
247
+ "<313>": 151990,
248
+ "<314>": 151991,
249
+ "<315>": 151992,
250
+ "<316>": 151993,
251
+ "<317>": 151994,
252
+ "<318>": 151995,
253
+ "<319>": 151996,
254
+ "<31>": 151708,
255
+ "<320>": 151997,
256
+ "<321>": 151998,
257
+ "<322>": 151999,
258
+ "<323>": 152000,
259
+ "<324>": 152001,
260
+ "<325>": 152002,
261
+ "<326>": 152003,
262
+ "<327>": 152004,
263
+ "<328>": 152005,
264
+ "<329>": 152006,
265
+ "<32>": 151709,
266
+ "<330>": 152007,
267
+ "<331>": 152008,
268
+ "<332>": 152009,
269
+ "<333>": 152010,
270
+ "<334>": 152011,
271
+ "<335>": 152012,
272
+ "<336>": 152013,
273
+ "<337>": 152014,
274
+ "<338>": 152015,
275
+ "<339>": 152016,
276
+ "<33>": 151710,
277
+ "<340>": 152017,
278
+ "<341>": 152018,
279
+ "<342>": 152019,
280
+ "<343>": 152020,
281
+ "<344>": 152021,
282
+ "<345>": 152022,
283
+ "<346>": 152023,
284
+ "<347>": 152024,
285
+ "<348>": 152025,
286
+ "<349>": 152026,
287
+ "<34>": 151711,
288
+ "<350>": 152027,
289
+ "<351>": 152028,
290
+ "<352>": 152029,
291
+ "<353>": 152030,
292
+ "<354>": 152031,
293
+ "<355>": 152032,
294
+ "<356>": 152033,
295
+ "<357>": 152034,
296
+ "<358>": 152035,
297
+ "<359>": 152036,
298
+ "<35>": 151712,
299
+ "<360>": 152037,
300
+ "<361>": 152038,
301
+ "<362>": 152039,
302
+ "<363>": 152040,
303
+ "<364>": 152041,
304
+ "<365>": 152042,
305
+ "<366>": 152043,
306
+ "<367>": 152044,
307
+ "<368>": 152045,
308
+ "<369>": 152046,
309
+ "<36>": 151713,
310
+ "<370>": 152047,
311
+ "<371>": 152048,
312
+ "<372>": 152049,
313
+ "<373>": 152050,
314
+ "<374>": 152051,
315
+ "<375>": 152052,
316
+ "<376>": 152053,
317
+ "<377>": 152054,
318
+ "<378>": 152055,
319
+ "<379>": 152056,
320
+ "<37>": 151714,
321
+ "<380>": 152057,
322
+ "<381>": 152058,
323
+ "<382>": 152059,
324
+ "<383>": 152060,
325
+ "<384>": 152061,
326
+ "<385>": 152062,
327
+ "<386>": 152063,
328
+ "<387>": 152064,
329
+ "<388>": 152065,
330
+ "<389>": 152066,
331
+ "<38>": 151715,
332
+ "<390>": 152067,
333
+ "<391>": 152068,
334
+ "<392>": 152069,
335
+ "<393>": 152070,
336
+ "<394>": 152071,
337
+ "<395>": 152072,
338
+ "<396>": 152073,
339
+ "<397>": 152074,
340
+ "<398>": 152075,
341
+ "<399>": 152076,
342
+ "<39>": 151716,
343
+ "<3>": 151680,
344
+ "<400>": 152077,
345
+ "<401>": 152078,
346
+ "<402>": 152079,
347
+ "<403>": 152080,
348
+ "<404>": 152081,
349
+ "<405>": 152082,
350
+ "<406>": 152083,
351
+ "<407>": 152084,
352
+ "<408>": 152085,
353
+ "<409>": 152086,
354
+ "<40>": 151717,
355
+ "<410>": 152087,
356
+ "<411>": 152088,
357
+ "<412>": 152089,
358
+ "<413>": 152090,
359
+ "<414>": 152091,
360
+ "<415>": 152092,
361
+ "<416>": 152093,
362
+ "<417>": 152094,
363
+ "<418>": 152095,
364
+ "<419>": 152096,
365
+ "<41>": 151718,
366
+ "<420>": 152097,
367
+ "<421>": 152098,
368
+ "<422>": 152099,
369
+ "<423>": 152100,
370
+ "<424>": 152101,
371
+ "<425>": 152102,
372
+ "<426>": 152103,
373
+ "<427>": 152104,
374
+ "<428>": 152105,
375
+ "<429>": 152106,
376
+ "<42>": 151719,
377
+ "<430>": 152107,
378
+ "<431>": 152108,
379
+ "<432>": 152109,
380
+ "<433>": 152110,
381
+ "<434>": 152111,
382
+ "<435>": 152112,
383
+ "<436>": 152113,
384
+ "<437>": 152114,
385
+ "<438>": 152115,
386
+ "<439>": 152116,
387
+ "<43>": 151720,
388
+ "<440>": 152117,
389
+ "<441>": 152118,
390
+ "<442>": 152119,
391
+ "<443>": 152120,
392
+ "<444>": 152121,
393
+ "<445>": 152122,
394
+ "<446>": 152123,
395
+ "<447>": 152124,
396
+ "<448>": 152125,
397
+ "<449>": 152126,
398
+ "<44>": 151721,
399
+ "<450>": 152127,
400
+ "<451>": 152128,
401
+ "<452>": 152129,
402
+ "<453>": 152130,
403
+ "<454>": 152131,
404
+ "<455>": 152132,
405
+ "<456>": 152133,
406
+ "<457>": 152134,
407
+ "<458>": 152135,
408
+ "<459>": 152136,
409
+ "<45>": 151722,
410
+ "<460>": 152137,
411
+ "<461>": 152138,
412
+ "<462>": 152139,
413
+ "<463>": 152140,
414
+ "<464>": 152141,
415
+ "<465>": 152142,
416
+ "<466>": 152143,
417
+ "<467>": 152144,
418
+ "<468>": 152145,
419
+ "<469>": 152146,
420
+ "<46>": 151723,
421
+ "<470>": 152147,
422
+ "<471>": 152148,
423
+ "<472>": 152149,
424
+ "<473>": 152150,
425
+ "<474>": 152151,
426
+ "<475>": 152152,
427
+ "<476>": 152153,
428
+ "<477>": 152154,
429
+ "<478>": 152155,
430
+ "<479>": 152156,
431
+ "<47>": 151724,
432
+ "<480>": 152157,
433
+ "<481>": 152158,
434
+ "<482>": 152159,
435
+ "<483>": 152160,
436
+ "<484>": 152161,
437
+ "<485>": 152162,
438
+ "<486>": 152163,
439
+ "<487>": 152164,
440
+ "<488>": 152165,
441
+ "<489>": 152166,
442
+ "<48>": 151725,
443
+ "<490>": 152167,
444
+ "<491>": 152168,
445
+ "<492>": 152169,
446
+ "<493>": 152170,
447
+ "<494>": 152171,
448
+ "<495>": 152172,
449
+ "<496>": 152173,
450
+ "<497>": 152174,
451
+ "<498>": 152175,
452
+ "<499>": 152176,
453
+ "<49>": 151726,
454
+ "<4>": 151681,
455
+ "<500>": 152177,
456
+ "<501>": 152178,
457
+ "<502>": 152179,
458
+ "<503>": 152180,
459
+ "<504>": 152181,
460
+ "<505>": 152182,
461
+ "<506>": 152183,
462
+ "<507>": 152184,
463
+ "<508>": 152185,
464
+ "<509>": 152186,
465
+ "<50>": 151727,
466
+ "<510>": 152187,
467
+ "<511>": 152188,
468
+ "<512>": 152189,
469
+ "<513>": 152190,
470
+ "<514>": 152191,
471
+ "<515>": 152192,
472
+ "<516>": 152193,
473
+ "<517>": 152194,
474
+ "<518>": 152195,
475
+ "<519>": 152196,
476
+ "<51>": 151728,
477
+ "<520>": 152197,
478
+ "<521>": 152198,
479
+ "<522>": 152199,
480
+ "<523>": 152200,
481
+ "<524>": 152201,
482
+ "<525>": 152202,
483
+ "<526>": 152203,
484
+ "<527>": 152204,
485
+ "<528>": 152205,
486
+ "<529>": 152206,
487
+ "<52>": 151729,
488
+ "<530>": 152207,
489
+ "<531>": 152208,
490
+ "<532>": 152209,
491
+ "<533>": 152210,
492
+ "<534>": 152211,
493
+ "<535>": 152212,
494
+ "<536>": 152213,
495
+ "<537>": 152214,
496
+ "<538>": 152215,
497
+ "<539>": 152216,
498
+ "<53>": 151730,
499
+ "<540>": 152217,
500
+ "<541>": 152218,
501
+ "<542>": 152219,
502
+ "<543>": 152220,
503
+ "<544>": 152221,
504
+ "<545>": 152222,
505
+ "<546>": 152223,
506
+ "<547>": 152224,
507
+ "<548>": 152225,
508
+ "<549>": 152226,
509
+ "<54>": 151731,
510
+ "<550>": 152227,
511
+ "<551>": 152228,
512
+ "<552>": 152229,
513
+ "<553>": 152230,
514
+ "<554>": 152231,
515
+ "<555>": 152232,
516
+ "<556>": 152233,
517
+ "<557>": 152234,
518
+ "<558>": 152235,
519
+ "<559>": 152236,
520
+ "<55>": 151732,
521
+ "<560>": 152237,
522
+ "<561>": 152238,
523
+ "<562>": 152239,
524
+ "<563>": 152240,
525
+ "<564>": 152241,
526
+ "<565>": 152242,
527
+ "<566>": 152243,
528
+ "<567>": 152244,
529
+ "<568>": 152245,
530
+ "<569>": 152246,
531
+ "<56>": 151733,
532
+ "<570>": 152247,
533
+ "<571>": 152248,
534
+ "<572>": 152249,
535
+ "<573>": 152250,
536
+ "<574>": 152251,
537
+ "<575>": 152252,
538
+ "<576>": 152253,
539
+ "<577>": 152254,
540
+ "<578>": 152255,
541
+ "<579>": 152256,
542
+ "<57>": 151734,
543
+ "<580>": 152257,
544
+ "<581>": 152258,
545
+ "<582>": 152259,
546
+ "<583>": 152260,
547
+ "<584>": 152261,
548
+ "<585>": 152262,
549
+ "<586>": 152263,
550
+ "<587>": 152264,
551
+ "<588>": 152265,
552
+ "<589>": 152266,
553
+ "<58>": 151735,
554
+ "<590>": 152267,
555
+ "<591>": 152268,
556
+ "<592>": 152269,
557
+ "<593>": 152270,
558
+ "<594>": 152271,
559
+ "<595>": 152272,
560
+ "<596>": 152273,
561
+ "<597>": 152274,
562
+ "<598>": 152275,
563
+ "<599>": 152276,
564
+ "<59>": 151736,
565
+ "<5>": 151682,
566
+ "<600>": 152277,
567
+ "<601>": 152278,
568
+ "<602>": 152279,
569
+ "<603>": 152280,
570
+ "<604>": 152281,
571
+ "<605>": 152282,
572
+ "<606>": 152283,
573
+ "<607>": 152284,
574
+ "<608>": 152285,
575
+ "<609>": 152286,
576
+ "<60>": 151737,
577
+ "<610>": 152287,
578
+ "<611>": 152288,
579
+ "<612>": 152289,
580
+ "<613>": 152290,
581
+ "<614>": 152291,
582
+ "<615>": 152292,
583
+ "<616>": 152293,
584
+ "<617>": 152294,
585
+ "<618>": 152295,
586
+ "<619>": 152296,
587
+ "<61>": 151738,
588
+ "<620>": 152297,
589
+ "<621>": 152298,
590
+ "<622>": 152299,
591
+ "<623>": 152300,
592
+ "<624>": 152301,
593
+ "<625>": 152302,
594
+ "<626>": 152303,
595
+ "<627>": 152304,
596
+ "<628>": 152305,
597
+ "<629>": 152306,
598
+ "<62>": 151739,
599
+ "<630>": 152307,
600
+ "<631>": 152308,
601
+ "<632>": 152309,
602
+ "<633>": 152310,
603
+ "<634>": 152311,
604
+ "<635>": 152312,
605
+ "<636>": 152313,
606
+ "<637>": 152314,
607
+ "<638>": 152315,
608
+ "<639>": 152316,
609
+ "<63>": 151740,
610
+ "<640>": 152317,
611
+ "<641>": 152318,
612
+ "<642>": 152319,
613
+ "<643>": 152320,
614
+ "<644>": 152321,
615
+ "<645>": 152322,
616
+ "<646>": 152323,
617
+ "<647>": 152324,
618
+ "<648>": 152325,
619
+ "<649>": 152326,
620
+ "<64>": 151741,
621
+ "<650>": 152327,
622
+ "<651>": 152328,
623
+ "<652>": 152329,
624
+ "<653>": 152330,
625
+ "<654>": 152331,
626
+ "<655>": 152332,
627
+ "<656>": 152333,
628
+ "<657>": 152334,
629
+ "<658>": 152335,
630
+ "<659>": 152336,
631
+ "<65>": 151742,
632
+ "<660>": 152337,
633
+ "<661>": 152338,
634
+ "<662>": 152339,
635
+ "<663>": 152340,
636
+ "<664>": 152341,
637
+ "<665>": 152342,
638
+ "<666>": 152343,
639
+ "<667>": 152344,
640
+ "<668>": 152345,
641
+ "<669>": 152346,
642
+ "<66>": 151743,
643
+ "<670>": 152347,
644
+ "<671>": 152348,
645
+ "<672>": 152349,
646
+ "<673>": 152350,
647
+ "<674>": 152351,
648
+ "<675>": 152352,
649
+ "<676>": 152353,
650
+ "<677>": 152354,
651
+ "<678>": 152355,
652
+ "<679>": 152356,
653
+ "<67>": 151744,
654
+ "<680>": 152357,
655
+ "<681>": 152358,
656
+ "<682>": 152359,
657
+ "<683>": 152360,
658
+ "<684>": 152361,
659
+ "<685>": 152362,
660
+ "<686>": 152363,
661
+ "<687>": 152364,
662
+ "<688>": 152365,
663
+ "<689>": 152366,
664
+ "<68>": 151745,
665
+ "<690>": 152367,
666
+ "<691>": 152368,
667
+ "<692>": 152369,
668
+ "<693>": 152370,
669
+ "<694>": 152371,
670
+ "<695>": 152372,
671
+ "<696>": 152373,
672
+ "<697>": 152374,
673
+ "<698>": 152375,
674
+ "<699>": 152376,
675
+ "<69>": 151746,
676
+ "<6>": 151683,
677
+ "<700>": 152377,
678
+ "<701>": 152378,
679
+ "<702>": 152379,
680
+ "<703>": 152380,
681
+ "<704>": 152381,
682
+ "<705>": 152382,
683
+ "<706>": 152383,
684
+ "<707>": 152384,
685
+ "<708>": 152385,
686
+ "<709>": 152386,
687
+ "<70>": 151747,
688
+ "<710>": 152387,
689
+ "<711>": 152388,
690
+ "<712>": 152389,
691
+ "<713>": 152390,
692
+ "<714>": 152391,
693
+ "<715>": 152392,
694
+ "<716>": 152393,
695
+ "<717>": 152394,
696
+ "<718>": 152395,
697
+ "<719>": 152396,
698
+ "<71>": 151748,
699
+ "<720>": 152397,
700
+ "<721>": 152398,
701
+ "<722>": 152399,
702
+ "<723>": 152400,
703
+ "<724>": 152401,
704
+ "<725>": 152402,
705
+ "<726>": 152403,
706
+ "<727>": 152404,
707
+ "<728>": 152405,
708
+ "<729>": 152406,
709
+ "<72>": 151749,
710
+ "<730>": 152407,
711
+ "<731>": 152408,
712
+ "<732>": 152409,
713
+ "<733>": 152410,
714
+ "<734>": 152411,
715
+ "<735>": 152412,
716
+ "<736>": 152413,
717
+ "<737>": 152414,
718
+ "<738>": 152415,
719
+ "<739>": 152416,
720
+ "<73>": 151750,
721
+ "<740>": 152417,
722
+ "<741>": 152418,
723
+ "<742>": 152419,
724
+ "<743>": 152420,
725
+ "<744>": 152421,
726
+ "<745>": 152422,
727
+ "<746>": 152423,
728
+ "<747>": 152424,
729
+ "<748>": 152425,
730
+ "<749>": 152426,
731
+ "<74>": 151751,
732
+ "<750>": 152427,
733
+ "<751>": 152428,
734
+ "<752>": 152429,
735
+ "<753>": 152430,
736
+ "<754>": 152431,
737
+ "<755>": 152432,
738
+ "<756>": 152433,
739
+ "<757>": 152434,
740
+ "<758>": 152435,
741
+ "<759>": 152436,
742
+ "<75>": 151752,
743
+ "<760>": 152437,
744
+ "<761>": 152438,
745
+ "<762>": 152439,
746
+ "<763>": 152440,
747
+ "<764>": 152441,
748
+ "<765>": 152442,
749
+ "<766>": 152443,
750
+ "<767>": 152444,
751
+ "<768>": 152445,
752
+ "<769>": 152446,
753
+ "<76>": 151753,
754
+ "<770>": 152447,
755
+ "<771>": 152448,
756
+ "<772>": 152449,
757
+ "<773>": 152450,
758
+ "<774>": 152451,
759
+ "<775>": 152452,
760
+ "<776>": 152453,
761
+ "<777>": 152454,
762
+ "<778>": 152455,
763
+ "<779>": 152456,
764
+ "<77>": 151754,
765
+ "<780>": 152457,
766
+ "<781>": 152458,
767
+ "<782>": 152459,
768
+ "<783>": 152460,
769
+ "<784>": 152461,
770
+ "<785>": 152462,
771
+ "<786>": 152463,
772
+ "<787>": 152464,
773
+ "<788>": 152465,
774
+ "<789>": 152466,
775
+ "<78>": 151755,
776
+ "<790>": 152467,
777
+ "<791>": 152468,
778
+ "<792>": 152469,
779
+ "<793>": 152470,
780
+ "<794>": 152471,
781
+ "<795>": 152472,
782
+ "<796>": 152473,
783
+ "<797>": 152474,
784
+ "<798>": 152475,
785
+ "<799>": 152476,
786
+ "<79>": 151756,
787
+ "<7>": 151684,
788
+ "<800>": 152477,
789
+ "<801>": 152478,
790
+ "<802>": 152479,
791
+ "<803>": 152480,
792
+ "<804>": 152481,
793
+ "<805>": 152482,
794
+ "<806>": 152483,
795
+ "<807>": 152484,
796
+ "<808>": 152485,
797
+ "<809>": 152486,
798
+ "<80>": 151757,
799
+ "<810>": 152487,
800
+ "<811>": 152488,
801
+ "<812>": 152489,
802
+ "<813>": 152490,
803
+ "<814>": 152491,
804
+ "<815>": 152492,
805
+ "<816>": 152493,
806
+ "<817>": 152494,
807
+ "<818>": 152495,
808
+ "<819>": 152496,
809
+ "<81>": 151758,
810
+ "<820>": 152497,
811
+ "<821>": 152498,
812
+ "<822>": 152499,
813
+ "<823>": 152500,
814
+ "<824>": 152501,
815
+ "<825>": 152502,
816
+ "<826>": 152503,
817
+ "<827>": 152504,
818
+ "<828>": 152505,
819
+ "<829>": 152506,
820
+ "<82>": 151759,
821
+ "<830>": 152507,
822
+ "<831>": 152508,
823
+ "<832>": 152509,
824
+ "<833>": 152510,
825
+ "<834>": 152511,
826
+ "<835>": 152512,
827
+ "<836>": 152513,
828
+ "<837>": 152514,
829
+ "<838>": 152515,
830
+ "<839>": 152516,
831
+ "<83>": 151760,
832
+ "<840>": 152517,
833
+ "<841>": 152518,
834
+ "<842>": 152519,
835
+ "<843>": 152520,
836
+ "<844>": 152521,
837
+ "<845>": 152522,
838
+ "<846>": 152523,
839
+ "<847>": 152524,
840
+ "<848>": 152525,
841
+ "<849>": 152526,
842
+ "<84>": 151761,
843
+ "<850>": 152527,
844
+ "<851>": 152528,
845
+ "<852>": 152529,
846
+ "<853>": 152530,
847
+ "<854>": 152531,
848
+ "<855>": 152532,
849
+ "<856>": 152533,
850
+ "<857>": 152534,
851
+ "<858>": 152535,
852
+ "<859>": 152536,
853
+ "<85>": 151762,
854
+ "<860>": 152537,
855
+ "<861>": 152538,
856
+ "<862>": 152539,
857
+ "<863>": 152540,
858
+ "<864>": 152541,
859
+ "<865>": 152542,
860
+ "<866>": 152543,
861
+ "<867>": 152544,
862
+ "<868>": 152545,
863
+ "<869>": 152546,
864
+ "<86>": 151763,
865
+ "<870>": 152547,
866
+ "<871>": 152548,
867
+ "<872>": 152549,
868
+ "<873>": 152550,
869
+ "<874>": 152551,
870
+ "<875>": 152552,
871
+ "<876>": 152553,
872
+ "<877>": 152554,
873
+ "<878>": 152555,
874
+ "<879>": 152556,
875
+ "<87>": 151764,
876
+ "<880>": 152557,
877
+ "<881>": 152558,
878
+ "<882>": 152559,
879
+ "<883>": 152560,
880
+ "<884>": 152561,
881
+ "<885>": 152562,
882
+ "<886>": 152563,
883
+ "<887>": 152564,
884
+ "<888>": 152565,
885
+ "<889>": 152566,
886
+ "<88>": 151765,
887
+ "<890>": 152567,
888
+ "<891>": 152568,
889
+ "<892>": 152569,
890
+ "<893>": 152570,
891
+ "<894>": 152571,
892
+ "<895>": 152572,
893
+ "<896>": 152573,
894
+ "<897>": 152574,
895
+ "<898>": 152575,
896
+ "<899>": 152576,
897
+ "<89>": 151766,
898
+ "<8>": 151685,
899
+ "<900>": 152577,
900
+ "<901>": 152578,
901
+ "<902>": 152579,
902
+ "<903>": 152580,
903
+ "<904>": 152581,
904
+ "<905>": 152582,
905
+ "<906>": 152583,
906
+ "<907>": 152584,
907
+ "<908>": 152585,
908
+ "<909>": 152586,
909
+ "<90>": 151767,
910
+ "<910>": 152587,
911
+ "<911>": 152588,
912
+ "<912>": 152589,
913
+ "<913>": 152590,
914
+ "<914>": 152591,
915
+ "<915>": 152592,
916
+ "<916>": 152593,
917
+ "<917>": 152594,
918
+ "<918>": 152595,
919
+ "<919>": 152596,
920
+ "<91>": 151768,
921
+ "<920>": 152597,
922
+ "<921>": 152598,
923
+ "<922>": 152599,
924
+ "<923>": 152600,
925
+ "<924>": 152601,
926
+ "<925>": 152602,
927
+ "<926>": 152603,
928
+ "<927>": 152604,
929
+ "<928>": 152605,
930
+ "<929>": 152606,
931
+ "<92>": 151769,
932
+ "<930>": 152607,
933
+ "<931>": 152608,
934
+ "<932>": 152609,
935
+ "<933>": 152610,
936
+ "<934>": 152611,
937
+ "<935>": 152612,
938
+ "<936>": 152613,
939
+ "<937>": 152614,
940
+ "<938>": 152615,
941
+ "<939>": 152616,
942
+ "<93>": 151770,
943
+ "<940>": 152617,
944
+ "<941>": 152618,
945
+ "<942>": 152619,
946
+ "<943>": 152620,
947
+ "<944>": 152621,
948
+ "<945>": 152622,
949
+ "<946>": 152623,
950
+ "<947>": 152624,
951
+ "<948>": 152625,
952
+ "<949>": 152626,
953
+ "<94>": 151771,
954
+ "<950>": 152627,
955
+ "<951>": 152628,
956
+ "<952>": 152629,
957
+ "<953>": 152630,
958
+ "<954>": 152631,
959
+ "<955>": 152632,
960
+ "<956>": 152633,
961
+ "<957>": 152634,
962
+ "<958>": 152635,
963
+ "<959>": 152636,
964
+ "<95>": 151772,
965
+ "<960>": 152637,
966
+ "<961>": 152638,
967
+ "<962>": 152639,
968
+ "<963>": 152640,
969
+ "<964>": 152641,
970
+ "<965>": 152642,
971
+ "<966>": 152643,
972
+ "<967>": 152644,
973
+ "<968>": 152645,
974
+ "<969>": 152646,
975
+ "<96>": 151773,
976
+ "<970>": 152647,
977
+ "<971>": 152648,
978
+ "<972>": 152649,
979
+ "<973>": 152650,
980
+ "<974>": 152651,
981
+ "<975>": 152652,
982
+ "<976>": 152653,
983
+ "<977>": 152654,
984
+ "<978>": 152655,
985
+ "<979>": 152656,
986
+ "<97>": 151774,
987
+ "<980>": 152657,
988
+ "<981>": 152658,
989
+ "<982>": 152659,
990
+ "<983>": 152660,
991
+ "<984>": 152661,
992
+ "<985>": 152662,
993
+ "<986>": 152663,
994
+ "<987>": 152664,
995
+ "<988>": 152665,
996
+ "<989>": 152666,
997
+ "<98>": 151775,
998
+ "<990>": 152667,
999
+ "<991>": 152668,
1000
+ "<992>": 152669,
1001
+ "<993>": 152670,
1002
+ "<994>": 152671,
1003
+ "<995>": 152672,
1004
+ "<996>": 152673,
1005
+ "<997>": 152674,
1006
+ "<998>": 152675,
1007
+ "<999>": 152676,
1008
+ "<99>": 151776,
1009
+ "<9>": 151686,
1010
+ "<IMG_CONTEXT>": 151665,
1011
+ "<box>": 151668,
1012
+ "<img>": 151666,
1013
+ "<interval>": 151674,
1014
+ "<null>": 152678,
1015
+ "<quad>": 151670,
1016
+ "<ref>": 151672,
1017
+ "<switch>": 152679,
1018
+ "<text_mask>": 151676,
1019
+ "<tool_call>": 151657,
1020
+ "<|box_end|>": 151649,
1021
+ "<|box_start|>": 151648,
1022
+ "<|endoftext|>": 151643,
1023
+ "<|file_sep|>": 151664,
1024
+ "<|fim_middle|>": 151660,
1025
+ "<|fim_pad|>": 151662,
1026
+ "<|fim_prefix|>": 151659,
1027
+ "<|fim_suffix|>": 151661,
1028
+ "<|im_end|>": 151645,
1029
+ "<|im_start|>": 151644,
1030
+ "<|image_pad|>": 151655,
1031
+ "<|object_ref_end|>": 151647,
1032
+ "<|object_ref_start|>": 151646,
1033
+ "<|quad_end|>": 151651,
1034
+ "<|quad_start|>": 151650,
1035
+ "<|repo_name|>": 151663,
1036
+ "<|video_pad|>": 151656,
1037
+ "<|vision_end|>": 151653,
1038
+ "<|vision_pad|>": 151654,
1039
+ "<|vision_start|>": 151652
1040
+ }
all_results.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "epoch": 0.069,
3
+ "total_flos": 5.6478980657499236e+20,
4
+ "train_loss": 0.02277738807797432,
5
+ "train_runtime": 3535.7374,
6
+ "train_samples": "streaming",
7
+ "train_samples_per_second": 362.018,
8
+ "train_steps_per_second": 1.414
9
+ }
assets/coco_lvis.png ADDED

Git LFS Details

  • SHA256: 8b75aa2122f9bc32c3449dfb6ac483bbed27d1f81de30c57aba36c47eb845baa
  • Pointer size: 131 Bytes
  • Size of remote file: 235 kB
assets/decoding_demo.mp4 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3f4083674bacb26f43d1f68f5529dac7ed4c713dca724d90b718358253453aba
3
+ size 24955186
assets/demo.mp4 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:56e5c216d26741be466562e90bf6f3d576f6e3c7ac64341a9101cccf94af2335
3
+ size 101601160
assets/dense_object_detection.png ADDED

Git LFS Details

  • SHA256: 5a656f3ec1d1931531a03380ccbf12db5f442027e519748c537d8d112a6cd371
  • Pointer size: 131 Bytes
  • Size of remote file: 161 kB
assets/layout_ocr.png ADDED

Git LFS Details

  • SHA256: ecccc9ebcdf453f5ff0094a785d5c771d34740b8690f471017c3756b0eb0887d
  • Pointer size: 131 Bytes
  • Size of remote file: 104 kB
assets/pointing.png ADDED
assets/referring.png ADDED

Git LFS Details

  • SHA256: 1a814ab0d9254962ee76d5928774b852540e235d6fc3eb6847840953a166b658
  • Pointer size: 131 Bytes
  • Size of remote file: 151 kB
assets/sspro.png ADDED

Git LFS Details

  • SHA256: 485bb3f9f49f413ee7d4dba852066184f2498a5067ce50055f29094e8f5b7acd
  • Pointer size: 131 Bytes
  • Size of remote file: 163 kB
assets/teaser.jpg ADDED

Git LFS Details

  • SHA256: b5af7f67594ce5fd519d47b1ad86e23c175ca50c1af1c5014e1d7bd48a60c14f
  • Pointer size: 131 Bytes
  • Size of remote file: 580 kB
batch_infer.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Minimal batch inference CLI for the LocateAnything-3B release code.
3
+
4
+ Examples:
5
+ python batch_infer.py --model /path/to/LocateAnything-3B --attn sdpa \
6
+ --image demo.jpg --query "person</c>car"
7
+
8
+ python batch_infer.py --requests requests.jsonl --batch-size 16 --attn la_flash
9
+
10
+ Each JSONL request should contain {"image": "/path/to.jpg", "query": "person</c>car"}.
11
+ """
12
+ import argparse
13
+ import json
14
+ import os
15
+ from pathlib import Path
16
+
17
+ from PIL import Image
18
+
19
+
20
+ def _attn_arg(value):
21
+ mode = (value or "sdpa").strip().lower().replace("-", "_")
22
+ aliases = {
23
+ "": "sdpa",
24
+ "manual": "eager",
25
+ "torch": "eager",
26
+ "torch_eager": "eager",
27
+ "torch_sdpa": "sdpa",
28
+ "flash": "la_flash",
29
+ "la_flash": "la_flash",
30
+ "kernel": "la_flash",
31
+ "cuda": "la_flash",
32
+ "range": "la_flash",
33
+ "range_attention": "la_flash",
34
+ }
35
+ mode = aliases.get(mode, mode)
36
+ if mode not in {"sdpa", "eager", "magi", "la_flash"}:
37
+ raise argparse.ArgumentTypeError(
38
+ f"--attn must be one of sdpa, eager, magi, la_flash; got {value!r}"
39
+ )
40
+ return mode
41
+
42
+
43
+ def _load_requests(args):
44
+ requests = []
45
+ if args.requests:
46
+ with open(args.requests, "r", encoding="utf-8") as f:
47
+ for line in f:
48
+ if not line.strip():
49
+ continue
50
+ row = json.loads(line)
51
+ requests.append((row["image"], row["query"]))
52
+ if args.image or args.query:
53
+ if len(args.image or []) != len(args.query or []):
54
+ raise ValueError("--image and --query must appear the same number of times")
55
+ requests.extend(zip(args.image, args.query))
56
+ if not requests:
57
+ raise ValueError("provide --requests JSONL or at least one --image/--query pair")
58
+ return requests
59
+
60
+
61
+ def main():
62
+ ap = argparse.ArgumentParser()
63
+ ap.add_argument("--requests", help="JSONL file with image/query fields")
64
+ ap.add_argument("--image", action="append", help="Image path; repeat with --query")
65
+ ap.add_argument("--query", action="append", help="Category query, e.g. person</c>car")
66
+ ap.add_argument("--model", default=os.environ.get("LA_FLASH_MODEL", "nvidia/LocateAnything-3B"))
67
+ ap.add_argument("--attn", type=_attn_arg, default=os.environ.get("LA_FLASH_ATTN", "sdpa"),
68
+ help="LLM attention backend: sdpa, eager, magi, or la_flash")
69
+ ap.add_argument("--vision-attn", default=os.environ.get("LA_FLASH_VISION_ATTN", "auto"),
70
+ choices=["auto", "flash_attention_2", "sdpa", "eager"])
71
+ ap.add_argument("--batch-size", type=int, default=1)
72
+ ap.add_argument("--scheduler", default=os.environ.get("LA_FLASH_HYBRID_SCHEDULER", "eager"),
73
+ choices=["eager", "hold_ar", "ar_first", "pipeline", "adaptive"])
74
+ ap.add_argument("--group-size", type=int, default=int(os.environ.get("LA_FLASH_HYBRID_GROUP_SIZE", "0")))
75
+ ap.add_argument("--max-new-tokens", type=int, default=2048)
76
+ ap.add_argument("--temperature", type=float, default=0.7)
77
+ ap.add_argument("--top-p", type=float, default=0.9)
78
+ ap.add_argument("--top-k", type=int, default=0)
79
+ ap.add_argument("--repetition-penalty", type=float, default=1.1)
80
+ ap.add_argument("--strict-attn", action="store_true",
81
+ help="Fail instead of falling back to SDPA if magi/la_flash is unavailable")
82
+ ap.add_argument("--out", default="", help="Optional output JSONL path; stdout if omitted")
83
+ args = ap.parse_args()
84
+ args.attn = _attn_arg(args.attn)
85
+
86
+ os.environ["LA_FLASH_MODEL"] = args.model
87
+ os.environ["LA_FLASH_ATTN"] = args.attn
88
+ os.environ["LA_FLASH_VISION_ATTN"] = args.vision_attn
89
+ os.environ["LA_FLASH_HYBRID_SCHEDULER"] = args.scheduler
90
+ os.environ["LA_FLASH_HYBRID_GROUP_SIZE"] = str(args.group_size)
91
+ if args.strict_attn:
92
+ os.environ["LA_FLASH_STRICT_ATTN"] = "1"
93
+
94
+ from batch_utils import generate_batch_hybrid, get_last_hybrid_stats, load
95
+ from batch_utils.hybrid_runtime import load_pil
96
+
97
+ requests = _load_requests(args)
98
+ load()
99
+
100
+ writer = open(args.out, "w", encoding="utf-8") if args.out else None
101
+ try:
102
+ for start in range(0, len(requests), max(1, args.batch_size)):
103
+ chunk = requests[start:start + max(1, args.batch_size)]
104
+ pairs = [(load_pil(image), query) for image, query in chunk]
105
+ texts = generate_batch_hybrid(
106
+ pairs,
107
+ temperature=args.temperature,
108
+ top_p=None if args.top_p < 0 else args.top_p,
109
+ top_k=None if args.top_k <= 0 else args.top_k,
110
+ repetition_penalty=args.repetition_penalty,
111
+ max_new_tokens=args.max_new_tokens,
112
+ scheduler=args.scheduler,
113
+ group_size=args.group_size,
114
+ )
115
+ stats = get_last_hybrid_stats()
116
+ for (image, query), text in zip(chunk, texts):
117
+ row = {"image": str(Path(image)), "query": query, "raw_response": text, "stats": stats}
118
+ line = json.dumps(row, ensure_ascii=False)
119
+ if writer:
120
+ writer.write(line + "\n")
121
+ else:
122
+ print(line, flush=True)
123
+ finally:
124
+ if writer:
125
+ writer.close()
126
+
127
+
128
+ if __name__ == "__main__":
129
+ main()
batch_utils/README.md ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Batch Utils
2
+
3
+ `batch_utils` contains the optional batched hybrid generation runtime for
4
+ LocateAnything. It keeps the model loading, tokenization, image feature caching,
5
+ sampling, and scheduler code used by `batch_infer.py` and the detection
6
+ experiments.
7
+
8
+ ## Runtime Modes
9
+
10
+ - `LA_FLASH_ATTN=sdpa`: stock PyTorch SDPA path.
11
+ - `LA_FLASH_ATTN=eager`: eager attention path for debugging.
12
+ - `LA_FLASH_ATTN=magi`: MagiAttention path when MagiAttention is installed.
13
+ - `LA_FLASH_ATTN=la_flash`: LA Flash sparse range backend
14
+ from `kernel_utils`.
15
+
16
+ ## Common Knobs
17
+
18
+ | Variable | Default | Meaning |
19
+ | --- | --- | --- |
20
+ | `LA_FLASH_MODEL` | `nvidia/LocateAnything-3B` | HF model id or local model directory. |
21
+ | `LA_FLASH_ATTN` | `sdpa` | LLM attention backend. |
22
+ | `LA_FLASH_VISION_ATTN` | `auto` | Vision encoder attention: `auto`, `flash_attention_2`, `sdpa`, or `eager`. |
23
+ | `LA_FLASH_STRICT_ATTN` | `0` | Set `1` to fail instead of falling back to SDPA. |
24
+ | `LA_FLASH_HYBRID_SCHEDULER` | `eager` | Hybrid decode scheduler. |
25
+ | `LA_FLASH_HYBRID_GROUP_SIZE` | `0` | Scheduler group size; `0` lets the runtime decide. |
26
+ | `LA_FLASH_VISION_ENCODE_BATCH_SIZE` | `8` | Maximum images per MoonViT encode micro-batch. |
27
+ | `LA_FLASH_KV_PACK_TOKEN_BUDGET` | `0` | Optional KV packing memory cap for long-tail batches. |
28
+ | `LA_FLASH_DENSE_BACKEND` | `sdpa` | Dense worker/prefill attention backend. Keep this as `sdpa`; LA Flash is used for sparse range plans. |
29
+ | `LA_FLASH_SEGMENT_FASTPATH` | `auto` | Sparse MTP decode uses FlashAttention varlen multi-segment merge by default. |
30
+
31
+ ## CLI Example
32
+
33
+ ```bash
34
+ python batch_infer.py \
35
+ --model nvidia/LocateAnything-3B \
36
+ --attn la_flash \
37
+ --scheduler pipeline \
38
+ --batch-size 4 \
39
+ --image /path/to/image.jpg \
40
+ --query "person</c>car"
41
+ ```
42
+
43
+ For JSONL input, each row should contain:
44
+
45
+ ```json
46
+ {"image": "/path/to/image.jpg", "query": "person</c>car"}
47
+ ```
48
+
49
+ ## Training Boundary
50
+
51
+ This package is for inference and evaluation. Training remains on the
52
+ MagiAttention backend; the batched sparse-plan decode runtime does not support
53
+ the `labels` training path.
batch_utils/__init__.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Hybrid batched inference helpers for NVIDIA LocateAnything-3B."""
2
+ from .hybrid_runtime import load, load_pil
3
+ from .engine_hybrid import generate_batch_hybrid, generate_batch_grouped_hybrid, get_last_hybrid_stats
4
+
5
+ __all__ = [
6
+ "load",
7
+ "load_pil",
8
+ "generate_batch_hybrid",
9
+ "generate_batch_grouped_hybrid",
10
+ "get_last_hybrid_stats",
11
+ ]
12
+ __version__ = "0.1.0"
batch_utils/engine_hybrid.py ADDED
@@ -0,0 +1,1357 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Batched hybrid-mode generation for LocateAnything-3B.
2
+
3
+ This module keeps the stock hybrid state machine:
4
+
5
+ MTP -> error_box -> AR
6
+ AR -> box_end_ar -> MTP
7
+
8
+ Rows in a batch may be in different modes. The decode loop therefore stores
9
+ per-row KV caches, packs rows with the same mode for one forward call, then
10
+ unpacks the clean KV back per row.
11
+ """
12
+ import copy
13
+ import importlib
14
+ import os
15
+
16
+ import torch
17
+
18
+
19
+ from .hybrid_runtime import (
20
+ ATTN_MODE,
21
+ AR_BATCH_SAN,
22
+ BATCH_SAN,
23
+ DEV,
24
+ N_FUTURE,
25
+ _encode_images,
26
+ _helpers,
27
+ _pad_generated,
28
+ _set_llm_mode,
29
+ _tokenize,
30
+ _tokenize_cached_image,
31
+ build_magi_scheduler_ranges,
32
+ language_model_forward,
33
+ load,
34
+ sample_next_tokens_batched,
35
+ sample_tokens_batched,
36
+ )
37
+
38
+
39
+ README_MAX_NEW_TOKENS = 2048
40
+ README_TEMPERATURE = 0.7
41
+ README_TOP_P = 0.9
42
+ README_REPETITION_PENALTY = 1.1
43
+
44
+ _LAST_HYBRID_STATS = None
45
+
46
+
47
+ def _row_len(kv):
48
+ return kv[0][0].shape[2]
49
+
50
+
51
+ def _pack_stock_kv_rows(kv_rows, rows, dev):
52
+ """Left-pad per-row real-token KV caches for stock-style decoding."""
53
+ lengths = [0 if kv_rows[r] is None else _row_len(kv_rows[r]) for r in rows]
54
+ kmax = max(lengths) if lengths else 0
55
+ if kmax == 0:
56
+ return None, torch.zeros((len(rows), 0), dtype=torch.long, device=dev), lengths, 0
57
+
58
+ ref = next(kv_rows[r] for r in rows if kv_rows[r] is not None)
59
+ packed = []
60
+ for layer in range(len(ref)):
61
+ ref_k, ref_v = ref[layer]
62
+ ks, vs = [], []
63
+ for r, length in zip(rows, lengths):
64
+ if length == 0:
65
+ k = ref_k.new_zeros((1, ref_k.shape[1], kmax, ref_k.shape[3]))
66
+ v = ref_v.new_zeros((1, ref_v.shape[1], kmax, ref_v.shape[3]))
67
+ else:
68
+ k, v = kv_rows[r][layer]
69
+ if length < kmax:
70
+ pad_shape = (1, k.shape[1], kmax - length, k.shape[3])
71
+ k = torch.cat([k.new_zeros(pad_shape), k], dim=2)
72
+ v = torch.cat([v.new_zeros(pad_shape), v], dim=2)
73
+ ks.append(k)
74
+ vs.append(v)
75
+ packed.append((torch.cat(ks, dim=0), torch.cat(vs, dim=0)))
76
+
77
+ kvalid = torch.zeros((len(rows), kmax), dtype=torch.long, device=dev)
78
+ for i, length in enumerate(lengths):
79
+ if length:
80
+ kvalid[i, kmax - length :] = 1
81
+ return tuple(packed), kvalid, lengths, kmax
82
+
83
+
84
+ def _unpack_stock_after_forward(out_kv, local_row, old_len, uncached_len, kmax, umax):
85
+ """Keep old real KV plus the right-aligned uncached real tokens; drop pads/window."""
86
+ out = []
87
+ u0 = kmax + (umax - uncached_len)
88
+ u1 = kmax + umax
89
+ for k, v in out_kv:
90
+ parts_k, parts_v = [], []
91
+ if old_len:
92
+ parts_k.append(k[local_row : local_row + 1, :, kmax - old_len : kmax, :])
93
+ parts_v.append(v[local_row : local_row + 1, :, kmax - old_len : kmax, :])
94
+ if uncached_len:
95
+ parts_k.append(k[local_row : local_row + 1, :, u0:u1, :])
96
+ parts_v.append(v[local_row : local_row + 1, :, u0:u1, :])
97
+ out.append((torch.cat(parts_k, dim=2).contiguous(),
98
+ torch.cat(parts_v, dim=2).contiguous()))
99
+ return tuple(out)
100
+
101
+
102
+ def _mk_generate_kwargs(temperature, top_p, top_k, repetition_penalty, row_temp=None):
103
+ t = temperature if row_temp is None else row_temp
104
+ gk = {"repetition_penalty": repetition_penalty, "generation_mode": "hybrid"}
105
+ if t and t > 0:
106
+ gk["temperature"] = t
107
+ if top_p is not None:
108
+ gk["top_p"] = top_p
109
+ if top_k is not None:
110
+ gk["top_k"] = top_k
111
+ return gk
112
+
113
+
114
+ def _classify_ar_token(token_val, tids):
115
+ if token_val == tids["box_end_token_id"]:
116
+ return "box_end_ar"
117
+ if tids["coord_start_token_id"] <= token_val <= tids["coord_end_token_id"]:
118
+ return "coord_ar"
119
+ if token_val == tids["none_token_id"]:
120
+ return "coord_ar"
121
+ return "im_end"
122
+
123
+
124
+ def _env_flag(name, default=False):
125
+ val = os.environ.get(name)
126
+ if val is None:
127
+ return default
128
+ return val.lower() not in {"0", "false", "no", "off", ""}
129
+
130
+
131
+ def _env_int(name, default):
132
+ val = os.environ.get(name)
133
+ if val is None or val == "":
134
+ return default
135
+ return int(val)
136
+
137
+
138
+ def _kv_pack_token_budget():
139
+ return max(0, _env_int("LA_FLASH_KV_PACK_TOKEN_BUDGET", 0))
140
+
141
+
142
+ def _debug_enabled(debug):
143
+ return _env_flag("LA_FLASH_DEBUG", False) if debug is None else bool(debug)
144
+
145
+
146
+ def _new_hybrid_stats(total_rows, scheduler, group_size, hold_max_steps, adaptive_hold_mtp_max=0):
147
+ return {
148
+ "scheduler": scheduler,
149
+ "requested_group_size": int(group_size or 0),
150
+ "hold_max_steps": int(hold_max_steps),
151
+ "adaptive_hold_mtp_max": int(adaptive_hold_mtp_max),
152
+ "input_batches": 1,
153
+ "input_rows": int(total_rows),
154
+ "groups": 0,
155
+ "group_sizes": [],
156
+ "decode_loops": 0,
157
+ "mixed_mode_cycles": 0,
158
+ "eager_mtp_then_ar_cycles": 0,
159
+ "ar_first_cycles": 0,
160
+ "pipeline_ar_after_mtp_cycles": 0,
161
+ "adaptive_hold_cycles": 0,
162
+ "adaptive_ar_first_cycles": 0,
163
+ "hold_ar_steps": 0,
164
+ "hold_ar_held_mtp_rows": 0,
165
+ "hold_ar_limit_mtp_forwards": 0,
166
+ "mtp_forwards": 0,
167
+ "ar_forwards": 0,
168
+ "mtp_forward_rows": 0,
169
+ "ar_forward_rows": 0,
170
+ "mtp_forward_query_tokens": 0,
171
+ "ar_forward_query_tokens": 0,
172
+ "max_mtp_forward_rows": 0,
173
+ "max_ar_forward_rows": 0,
174
+ "mtp_max_uncached_len": 0,
175
+ "ar_max_uncached_len": 0,
176
+ "mtp_forward_row_hist": {},
177
+ "ar_forward_row_hist": {},
178
+ "prompt_prefill_mode": _hybrid_prefill_mode(),
179
+ "prompt_prefill_forwards": 0,
180
+ "prompt_prefill_forward_rows": 0,
181
+ "prompt_prefill_forward_query_tokens": 0,
182
+ "prompt_prefill_real_tokens": 0,
183
+ "prompt_prefill_shared_groups": 0,
184
+ "prompt_prefill_shared_rows": 0,
185
+ "prompt_prefill_shared_saved_tokens": 0,
186
+ "kv_bucket_splits": 0,
187
+ "kv_bucket_groups": 0,
188
+ "kv_bucket_max_packed_tokens": 0,
189
+ }
190
+
191
+
192
+ def _set_last_hybrid_stats(stats):
193
+ global _LAST_HYBRID_STATS
194
+ _LAST_HYBRID_STATS = copy.deepcopy(stats) if stats is not None else None
195
+
196
+
197
+ def get_last_hybrid_stats():
198
+ """Return scheduler/forward statistics from the most recent hybrid batch."""
199
+ return copy.deepcopy(_LAST_HYBRID_STATS)
200
+
201
+
202
+ def _record_group_stats(stats, bsz):
203
+ if stats is None:
204
+ return
205
+ stats["groups"] += 1
206
+ stats["group_sizes"].append(int(bsz))
207
+
208
+
209
+ def _bump_hist(hist, val):
210
+ key = str(int(val))
211
+ hist[key] = int(hist.get(key, 0)) + 1
212
+
213
+
214
+ def _record_forward_stats(stats, kind, rows, q_len, uncached_lens):
215
+ if stats is None:
216
+ return
217
+ prefix = "mtp" if kind == "mtp" else "ar"
218
+ nrows = int(len(rows))
219
+ q_len = int(q_len)
220
+ stats[f"{prefix}_forwards"] += 1
221
+ stats[f"{prefix}_forward_rows"] += nrows
222
+ stats[f"{prefix}_forward_query_tokens"] += nrows * q_len
223
+ stats[f"max_{prefix}_forward_rows"] = max(stats[f"max_{prefix}_forward_rows"], nrows)
224
+ stats[f"{prefix}_max_uncached_len"] = max(
225
+ stats[f"{prefix}_max_uncached_len"],
226
+ max((int(x) for x in uncached_lens), default=0),
227
+ )
228
+ _bump_hist(stats[f"{prefix}_forward_row_hist"], nrows)
229
+
230
+
231
+ def _record_prefill_stats(stats, rows, q_len, real_tokens, shared_groups=0, shared_rows=0, saved_tokens=0):
232
+ if stats is None:
233
+ return
234
+ nrows = int(rows)
235
+ stats["prompt_prefill_forwards"] += 1
236
+ stats["prompt_prefill_forward_rows"] += nrows
237
+ stats["prompt_prefill_forward_query_tokens"] += nrows * int(q_len)
238
+ stats["prompt_prefill_real_tokens"] += int(real_tokens)
239
+ stats["prompt_prefill_shared_groups"] += int(shared_groups)
240
+ stats["prompt_prefill_shared_rows"] += int(shared_rows)
241
+ stats["prompt_prefill_shared_saved_tokens"] += int(saved_tokens)
242
+
243
+
244
+ def _split_rows_by_kv_budget(rows, kv_rows):
245
+ """Keep dense left-padded KV packs bounded when a few rows become long tails."""
246
+ budget = _kv_pack_token_budget()
247
+ if budget <= 0 or len(rows) <= 1:
248
+ return [rows]
249
+ lengths = [0 if kv_rows[r] is None else _row_len(kv_rows[r]) for r in rows]
250
+ if not lengths or max(lengths) * len(rows) <= budget:
251
+ return [rows]
252
+
253
+ groups = []
254
+ current = []
255
+ current_max = 0
256
+ for row, length in sorted(zip(rows, lengths), key=lambda item: item[1]):
257
+ next_max = max(current_max, int(length))
258
+ if current and next_max * (len(current) + 1) > budget:
259
+ groups.append(current)
260
+ current = [row]
261
+ current_max = int(length)
262
+ else:
263
+ current.append(row)
264
+ current_max = next_max
265
+ if current:
266
+ groups.append(current)
267
+ return groups or [rows]
268
+
269
+
270
+ def _record_kv_bucket_stats(stats, groups, kv_rows):
271
+ if stats is None:
272
+ return
273
+ max_packed = 0
274
+ for group in groups:
275
+ if not group:
276
+ continue
277
+ kmax = max((0 if kv_rows[r] is None else _row_len(kv_rows[r])) for r in group)
278
+ max_packed = max(max_packed, int(kmax) * len(group))
279
+ stats["kv_bucket_max_packed_tokens"] = max(stats["kv_bucket_max_packed_tokens"], max_packed)
280
+ if len(groups) > 1:
281
+ stats["kv_bucket_splits"] += 1
282
+ stats["kv_bucket_groups"] += len(groups)
283
+
284
+
285
+ def _hybrid_scheduler(scheduler):
286
+ val = os.environ.get("LA_FLASH_HYBRID_SCHEDULER", "eager") if scheduler is None else scheduler
287
+ val = str(val).strip().lower()
288
+ aliases = {
289
+ "": "eager",
290
+ "default": "eager",
291
+ "normal": "eager",
292
+ "hold": "hold_ar",
293
+ "hold-ar": "hold_ar",
294
+ "hold_mtp": "hold_ar",
295
+ "hold-mtp": "hold_ar",
296
+ "repair_first": "ar_first",
297
+ "repair-first": "ar_first",
298
+ "ar-first": "ar_first",
299
+ }
300
+ val = aliases.get(val, val)
301
+ if val not in {"eager", "hold_ar", "ar_first", "pipeline", "adaptive"}:
302
+ raise ValueError("scheduler must be one of: eager, hold_ar, ar_first, pipeline, adaptive")
303
+ return val
304
+
305
+
306
+ def _hybrid_group_size(group_size):
307
+ if group_size is None:
308
+ return max(0, _env_int("LA_FLASH_HYBRID_GROUP_SIZE", 0))
309
+ return max(0, int(group_size))
310
+
311
+
312
+ def _hybrid_prefill_mode():
313
+ val = os.environ.get("LA_FLASH_HYBRID_PREFILL", "shared").strip().lower()
314
+ aliases = {
315
+ "0": "none",
316
+ "false": "none",
317
+ "off": "none",
318
+ "legacy": "none",
319
+ "1": "per_row",
320
+ "true": "per_row",
321
+ "on": "per_row",
322
+ "single": "per_row",
323
+ "row": "per_row",
324
+ "rows": "per_row",
325
+ "batched": "batch",
326
+ "prefix": "shared",
327
+ "shared_prefix": "shared",
328
+ "shared-image": "shared",
329
+ "shared_image": "shared",
330
+ "vision": "shared",
331
+ }
332
+ val = aliases.get(val, val)
333
+ if val not in {"none", "per_row", "batch", "shared"}:
334
+ raise ValueError("LA_FLASH_HYBRID_PREFILL must be one of none, per_row, batch, shared")
335
+ return val
336
+
337
+
338
+ def _tolist(t):
339
+ return t.detach().cpu().tolist()
340
+
341
+
342
+ def _safe_decode_rows(tok, input_ids):
343
+ rows = []
344
+ for row in _tolist(input_ids):
345
+ try:
346
+ rows.append(tok.decode(torch.tensor(row), skip_special_tokens=False))
347
+ except Exception:
348
+ rows.append("<decode failed>")
349
+ return rows
350
+
351
+
352
+ def _safe_decode_row(tok, row):
353
+ try:
354
+ return tok.decode(torch.tensor(row), skip_special_tokens=False)
355
+ except Exception:
356
+ return "<decode failed>"
357
+
358
+
359
+ def _effective_allowed_mask(mask2d, q_len, past_len, mtp_window=False):
360
+ """Readable 1/0 q-by-k mask derived from the 2D key-valid mask.
361
+
362
+ This mirrors the model path at a high level:
363
+ causal + padding columns, then the MTP window update
364
+ attn[-block:, -block:] = visible and attn[-block:, -block-1] = masked.
365
+ """
366
+ rows = []
367
+ key_valid = mask2d.detach().cpu().bool()
368
+ total_len = int(key_valid.numel())
369
+ for qi in range(q_len):
370
+ q_abs = past_len + qi
371
+ row = []
372
+ for ki in range(total_len):
373
+ row.append(1 if bool(key_valid[ki]) and ki <= q_abs else 0)
374
+ rows.append(row)
375
+
376
+ if mtp_window and q_len >= N_FUTURE and total_len >= N_FUTURE:
377
+ q0 = q_len - N_FUTURE
378
+ k0 = total_len - N_FUTURE
379
+ for qi in range(q0, q_len):
380
+ for ki in range(k0, total_len):
381
+ rows[qi][ki] = 1
382
+ if k0 - 1 >= 0:
383
+ rows[qi][k0 - 1] = 0
384
+ return rows
385
+
386
+
387
+ def _tail_matrix(mat, rows=None, cols=None):
388
+ if rows is not None:
389
+ mat = mat[-rows:]
390
+ if cols is not None:
391
+ mat = [row[-cols:] for row in mat]
392
+ return mat
393
+
394
+
395
+ def _format_01_matrix(mat):
396
+ return "\n".join(" " + " ".join(str(int(v)) for v in row) for row in mat)
397
+
398
+
399
+ def _safe_sdpa_mask_enabled():
400
+ return _env_flag("LA_FLASH_SDPA_SAFE_4D_MASK", True)
401
+
402
+
403
+ def _build_safe_sdpa_visible_mask(attention_mask_2d, input_ids, past_len, mtp_window=False):
404
+ """Build a 4D 1/0 visible mask, with harmless visibility for all-masked pad queries.
405
+
406
+ The remote Qwen2 SDPA path uses a 2D key-valid mask and can create fully
407
+ masked query rows for left-padded, no-cache prefill. Those rows can produce
408
+ NaNs inside SDPA and later contaminate real tokens through masked K columns.
409
+ This 4D mask keeps real-token visibility identical, and only gives otherwise
410
+ all-masked query rows one valid fallback key so their activations stay finite.
411
+ """
412
+ bsz, q_len = int(input_ids.shape[0]), int(input_ids.shape[1])
413
+ key_len = int(attention_mask_2d.shape[1])
414
+ dev = input_ids.device
415
+ key_valid = attention_mask_2d.to(dtype=torch.bool, device=dev)
416
+ key_idx = torch.arange(key_len, device=dev).view(1, 1, key_len)
417
+ q_abs = (past_len + torch.arange(q_len, device=dev)).view(1, q_len, 1)
418
+ visible = key_valid[:, None, :] & (key_idx <= q_abs)
419
+
420
+ if mtp_window and q_len >= N_FUTURE and key_len >= N_FUTURE:
421
+ k0 = key_len - N_FUTURE
422
+ visible[:, -N_FUTURE:, k0:key_len] = key_valid[:, None, k0:key_len]
423
+ blocked_k = k0 - 1
424
+ if blocked_k >= 0:
425
+ visible[:, -N_FUTURE:, blocked_k] = False
426
+
427
+ row_has_key = visible.any(dim=-1)
428
+ fallback_rows = int((~row_has_key).sum().item())
429
+ if fallback_rows:
430
+ for b in range(bsz):
431
+ valid = torch.nonzero(key_valid[b], as_tuple=False).flatten()
432
+ fallback = int(valid[0].item()) if valid.numel() else 0
433
+ missing = torch.nonzero(~row_has_key[b], as_tuple=False).flatten()
434
+ if missing.numel():
435
+ visible[b, missing, fallback] = True
436
+
437
+ mask = visible[:, None, :, :].to(dtype=torch.bfloat16)
438
+ try:
439
+ mask._la_flash_visible_mask = True
440
+ except Exception:
441
+ pass
442
+ return mask, fallback_rows
443
+
444
+
445
+ def _mask_desc(mask):
446
+ if mask is None:
447
+ return "none"
448
+ if isinstance(mask, dict):
449
+ return "magi_ranges"
450
+ if hasattr(mask, "dim"):
451
+ return "4d_safe_sdpa" if mask.dim() == 4 else "2d_key_valid"
452
+ return type(mask).__name__
453
+
454
+
455
+ def _forward_attention_mask(model, input_ids, attention_mask_2d, past_len, mtp_window=False, range_plan=False):
456
+ llm = model.language_model.model
457
+ if getattr(model, "_la_flash_requested_attn", ATTN_MODE) in {"magi", "la_flash"}:
458
+ range_plan = build_magi_scheduler_ranges(
459
+ model, attention_mask_2d, input_ids, past_len, mtp_window=mtp_window)
460
+ if range_plan is not None:
461
+ return range_plan, 0
462
+ needs_safe_pad = (
463
+ past_len == 0
464
+ and attention_mask_2d is not None
465
+ and attention_mask_2d.dim() == 2
466
+ and input_ids.shape[0] > 1
467
+ )
468
+ if (
469
+ getattr(llm, "_attn_implementation", None) == "sdpa"
470
+ and _safe_sdpa_mask_enabled()
471
+ and needs_safe_pad
472
+ and attention_mask_2d is not None
473
+ and attention_mask_2d.dim() == 2
474
+ ):
475
+ return _build_safe_sdpa_visible_mask(attention_mask_2d, input_ids, past_len, mtp_window)
476
+ return attention_mask_2d, 0
477
+
478
+
479
+ def _actual_sdpa_allowed_masks(model, input_ids, attention_mask, past_len):
480
+ """Recreate the remote Qwen2 SDPA 4D additive mask and return a 0/1 view."""
481
+ llm = model.language_model.model
482
+ mod = importlib.import_module(type(llm).__module__)
483
+ bsz, q_len = int(input_ids.shape[0]), int(input_ids.shape[1])
484
+ dummy = torch.empty(
485
+ (bsz, q_len, 1),
486
+ dtype=torch.bfloat16,
487
+ device=input_ids.device,
488
+ )
489
+ mask4 = mod._prepare_4d_causal_attention_mask(
490
+ attention_mask,
491
+ (bsz, q_len),
492
+ dummy,
493
+ past_len,
494
+ sliding_window=getattr(llm.config, "sliding_window", None),
495
+ )
496
+ remote_ar_decode = q_len == 1 or (
497
+ input_ids is not None and int(input_ids[0, -1].item()) != int(llm.text_mask_token_id)
498
+ )
499
+ if not remote_ar_decode and mask4 is not None and mask4.dim() == 4:
500
+ rows = []
501
+ for b in range(bsz):
502
+ rows.append(
503
+ mod.update_causal_mask_for_one_gen_window_2d(
504
+ input_ids[b],
505
+ mask4[b][0].clone(),
506
+ block_size=int(llm.block_size),
507
+ use_cache=True,
508
+ causal_attn=bool(getattr(llm, "causal_attn", False)),
509
+ ).unsqueeze(0)
510
+ )
511
+ mask4 = torch.stack(rows, dim=0)
512
+ allowed = (mask4[:, 0] >= 0).to(torch.int8).detach().cpu().tolist()
513
+ return allowed, tuple(mask4.shape), remote_ar_decode
514
+
515
+
516
+ def _debug_magi_ranges(q_len, past_len, mtp_window=False):
517
+ kv_len = past_len + q_len
518
+ ar_decode = not mtp_window
519
+ if ar_decode:
520
+ return {
521
+ "q_ranges": [[0, q_len]],
522
+ "k_ranges": [[0, kv_len]],
523
+ "attn_type_map": ["CAUSAL"],
524
+ }
525
+
526
+ block = N_FUTURE
527
+ if not (0 < block <= q_len <= kv_len):
528
+ return {"error": f"invalid magi MTP shape: block={block}, q_len={q_len}, kv_len={kv_len}"}
529
+
530
+ prefix_len = kv_len - block
531
+ blocked_k = prefix_len - 1
532
+ q_ranges, k_ranges, attn_types = [], [], []
533
+ if q_len == kv_len:
534
+ if prefix_len > 0:
535
+ q_ranges.append([0, prefix_len])
536
+ k_ranges.append([0, prefix_len])
537
+ attn_types.append("CAUSAL")
538
+ if prefix_len > 0 and blocked_k > 0:
539
+ q_ranges.append([prefix_len, kv_len])
540
+ k_ranges.append([0, blocked_k])
541
+ attn_types.append("FULL")
542
+ q_ranges.append([prefix_len, kv_len])
543
+ k_ranges.append([prefix_len, kv_len])
544
+ attn_types.append("FULL")
545
+ else:
546
+ recompute = q_len - block
547
+ q_global_start = kv_len - q_len
548
+ for i in range(recompute):
549
+ g = q_global_start + i
550
+ q_ranges.append([i, i + 1])
551
+ k_ranges.append([0, g + 1])
552
+ attn_types.append("FULL")
553
+ q_win = [recompute, q_len]
554
+ if blocked_k > 0:
555
+ q_ranges.append(q_win)
556
+ k_ranges.append([0, blocked_k])
557
+ attn_types.append("FULL")
558
+ q_ranges.append(q_win)
559
+ k_ranges.append([prefix_len, kv_len])
560
+ attn_types.append("FULL")
561
+
562
+ return {"q_ranges": q_ranges, "k_ranges": k_ranges, "attn_type_map": attn_types}
563
+
564
+
565
+ def _print_debug_forward(label, model, tok, input_ids, attention_mask, position_ids,
566
+ past_len, mtp_window=False, extra=None, attention_impl="sdpa"):
567
+ print(f"\n========== LA Flash DEBUG {label} ==========", flush=True)
568
+ if extra:
569
+ for k, v in extra.items():
570
+ print(f"{k}: {v}", flush=True)
571
+ tail = int(os.environ.get("LA_FLASH_DEBUG_TAIL", "15"))
572
+ bsz, q_len = int(input_ids.shape[0]), int(input_ids.shape[1])
573
+ key_len = int(attention_mask.shape[1])
574
+ q_tail, k_tail = min(tail, q_len), min(tail, key_len)
575
+ print(
576
+ "shapes: "
577
+ f"input_ids={tuple(input_ids.shape)} "
578
+ f"position_ids={tuple(position_ids.shape)} "
579
+ f"attention_mask_key_valid={tuple(attention_mask.shape)} "
580
+ f"mask_2d_q_by_k=({bsz}, {q_len}, {key_len}) "
581
+ f"mask_2d_tail=({bsz}, {q_tail}, {k_tail}) "
582
+ f"past_len={past_len} q_len={q_len} "
583
+ f"mtp_window={mtp_window} ar_decode={not mtp_window}",
584
+ flush=True,
585
+ )
586
+ print(f"dtypes/devices: input_ids={input_ids.dtype}@{input_ids.device} position_ids={position_ids.dtype}@{position_ids.device} attention_mask={attention_mask.dtype}@{attention_mask.device}", flush=True)
587
+ print(f"attention_impl={attention_impl}", flush=True)
588
+ input_rows = _tolist(input_ids)
589
+ pos_rows = _tolist(position_ids)
590
+ print(f"tail_window_last={tail}", flush=True)
591
+ print(f"input_ids_tail.shape=({bsz}, {q_tail})", flush=True)
592
+ print(f"position_ids_tail.shape=({bsz}, {q_tail})", flush=True)
593
+ actual_sdpa = None
594
+ if attention_impl in {"sdpa", "eager", "la_flash"}:
595
+ try:
596
+ actual_sdpa = _actual_sdpa_allowed_masks(model, input_ids, attention_mask, past_len)
597
+ print(
598
+ f"actual_sdpa_4d_mask_shape={actual_sdpa[1]} "
599
+ f"remote_ar_decode={actual_sdpa[2]}",
600
+ flush=True,
601
+ )
602
+ except Exception as e:
603
+ print(f"actual_sdpa_4d_mask_debug_failed={type(e).__name__}: {e}", flush=True)
604
+
605
+ for b in range(input_ids.shape[0]):
606
+ ids_tail = input_rows[b][-tail:]
607
+ pos_tail = pos_rows[b][-tail:]
608
+ allowed = _effective_allowed_mask(attention_mask[b], input_ids.shape[1], past_len, mtp_window)
609
+ q_tail = min(tail, len(allowed))
610
+ k_tail = min(tail, len(allowed[0]) if allowed else 0)
611
+ allowed_tail = _tail_matrix(allowed, rows=q_tail, cols=k_tail)
612
+ print(f"batch_row={b} ar_decode={not mtp_window}", flush=True)
613
+ print(f"input_ids_tail[-{tail}:]: {ids_tail}", flush=True)
614
+ print(f"decoded_tail[-{tail}:]: {_safe_decode_row(tok, ids_tail)}", flush=True)
615
+ print(f"position_ids_tail[-{tail}:]: {pos_tail}", flush=True)
616
+ print(f"expected_mask_2d_tail[-{q_tail}:,-{k_tail}:].shape=({q_tail}, {k_tail})", flush=True)
617
+ print(_format_01_matrix(allowed_tail), flush=True)
618
+ if actual_sdpa is not None:
619
+ actual = actual_sdpa[0][b]
620
+ actual_tail = _tail_matrix(actual, rows=q_tail, cols=k_tail)
621
+ mismatch = sum(
622
+ int(allowed[qi][ki] != actual[qi][ki])
623
+ for qi in range(len(allowed))
624
+ for ki in range(len(allowed[qi]))
625
+ )
626
+ print(
627
+ f"actual_sdpa_mask_2d_tail[-{q_tail}:,-{k_tail}:].shape=({q_tail}, {k_tail})",
628
+ flush=True,
629
+ )
630
+ print(_format_01_matrix(actual_tail), flush=True)
631
+ print(f"expected_vs_actual_sdpa_mismatch_count={mismatch}", flush=True)
632
+
633
+ if _env_flag("LA_FLASH_DEBUG_FULL_MASK", False):
634
+ masks = [
635
+ _effective_allowed_mask(attention_mask[b], input_ids.shape[1], past_len, mtp_window)
636
+ for b in range(input_ids.shape[0])
637
+ ]
638
+ print("effective_allowed_mask_q_by_k_FULL:", masks, flush=True)
639
+ if attention_impl == "magi":
640
+ if bsz == 1:
641
+ print(
642
+ "magi_ranges:",
643
+ _debug_magi_ranges(input_ids.shape[1], past_len, mtp_window),
644
+ flush=True,
645
+ )
646
+ else:
647
+ print(
648
+ "magi_ranges: built once per forward from the batched scheduler mask",
649
+ flush=True,
650
+ )
651
+ print(
652
+ "magi_ranges_single_row_template:",
653
+ _debug_magi_ranges(input_ids.shape[1], past_len, mtp_window),
654
+ flush=True,
655
+ )
656
+
657
+
658
+ def _common_prefix_len(prompt_ids, rows):
659
+ if not rows:
660
+ return 0
661
+ first = prompt_ids[rows[0]]
662
+ max_len = min(int(prompt_ids[r].numel()) for r in rows)
663
+ prefix_len = 0
664
+ for idx in range(max_len):
665
+ val = int(first[idx].item())
666
+ if all(int(prompt_ids[r][idx].item()) == val for r in rows[1:]):
667
+ prefix_len += 1
668
+ else:
669
+ break
670
+ return prefix_len
671
+
672
+
673
+ def _prefill_shared_prefix_kv_rows(model, prompt_ids, vit_list, img_tok, pad, dev, stats=None, debug=False):
674
+ """Cache one common prompt prefix per image-feature group.
675
+
676
+ Multi-category split repeats the same image feature tensor for each
677
+ category prompt. Token ids are identical through the image tokens and the
678
+ fixed prompt prefix, so we prefill that shared prefix once and let each
679
+ category row forward only its text suffix.
680
+ """
681
+ bsz = len(prompt_ids)
682
+ kv_rows = [None] * bsz
683
+ cached_lens = [0] * bsz
684
+ groups = {}
685
+ for row, vit in enumerate(vit_list):
686
+ groups.setdefault(id(vit), []).append(row)
687
+
688
+ items = []
689
+ min_prefix_len = max(1, _env_int("LA_FLASH_SHARED_PREFILL_MIN_PREFIX", 64))
690
+ for rows in groups.values():
691
+ if len(rows) < 2:
692
+ continue
693
+ prefix_len = _common_prefix_len(prompt_ids, rows)
694
+ if prefix_len < min_prefix_len:
695
+ continue
696
+ prefix_ids = prompt_ids[rows[0]][:prefix_len]
697
+ image_token_count = int((prefix_ids == img_tok).sum().item())
698
+ if image_token_count != int(vit_list[rows[0]].shape[0]):
699
+ if debug:
700
+ print(
701
+ "LA Flash shared prefill skip group: "
702
+ f"rows={rows} prefix_len={prefix_len} "
703
+ f"image_tokens={image_token_count} visual_rows={int(vit_list[rows[0]].shape[0])}",
704
+ flush=True,
705
+ )
706
+ continue
707
+ items.append((rows, prefix_ids, vit_list[rows[0]]))
708
+
709
+ if not items:
710
+ return kv_rows, cached_lens
711
+
712
+ lengths = [int(ids.numel()) for _rows, ids, _vit in items]
713
+ pmax = max(lengths)
714
+ input_ids = torch.full((len(items), pmax), pad, dtype=torch.long, device=dev)
715
+ amask = torch.zeros((len(items), pmax), dtype=torch.long, device=dev)
716
+ pos = torch.ones((len(items), pmax), dtype=torch.long, device=dev)
717
+ for item_idx, (_rows, ids, _vit) in enumerate(items):
718
+ length = lengths[item_idx]
719
+ left = pmax - length
720
+ input_ids[item_idx, left:] = ids.to(dev)
721
+ amask[item_idx, left:] = 1
722
+ pos[item_idx, left:] = torch.arange(length, dtype=torch.long, device=dev)
723
+
724
+ visual_features = torch.cat([vit for _rows, _ids, vit in items], dim=0)
725
+ assert int((input_ids == img_tok).sum().item()) == visual_features.shape[0], \
726
+ "shared-prefix image-token count != supplied visual_features rows"
727
+
728
+ if debug:
729
+ group_sizes = [len(rows) for rows, _ids, _vit in items]
730
+ print(
731
+ "LA Flash hybrid shared prompt prefill "
732
+ f"groups={len(items)} group_sizes={group_sizes} prefix_lens={lengths}",
733
+ flush=True,
734
+ )
735
+
736
+ forward_mask, fallback_rows = _forward_attention_mask(
737
+ model, input_ids, amask, 0, mtp_window=False)
738
+ if debug and fallback_rows:
739
+ print(
740
+ "LA Flash hybrid shared prefill safe SDPA fallback "
741
+ f"query_rows={fallback_rows}",
742
+ flush=True,
743
+ )
744
+ forward_kwargs = dict(
745
+ input_ids=input_ids,
746
+ visual_features=visual_features,
747
+ image_token_index=img_tok,
748
+ attention_mask=forward_mask,
749
+ position_ids=pos,
750
+ past_key_values=None,
751
+ use_cache=True,
752
+ )
753
+ if isinstance(forward_mask, dict):
754
+ out = language_model_forward(model, **forward_kwargs, return_logits=False)
755
+ else:
756
+ out = model.language_model.model(**forward_kwargs)
757
+
758
+ real_tokens = sum(lengths)
759
+ shared_rows = sum(len(rows) for rows, _ids, _vit in items)
760
+ saved_tokens = sum((len(rows) - 1) * length for (rows, _ids, _vit), length in zip(items, lengths))
761
+ _record_prefill_stats(
762
+ stats,
763
+ rows=len(items),
764
+ q_len=pmax,
765
+ real_tokens=real_tokens,
766
+ shared_groups=len(items),
767
+ shared_rows=shared_rows,
768
+ saved_tokens=saved_tokens,
769
+ )
770
+
771
+ for item_idx, (rows, _ids, _vit) in enumerate(items):
772
+ prefix_len = lengths[item_idx]
773
+ prefix_kv = _unpack_stock_after_forward(out.past_key_values, item_idx, 0, prefix_len, 0, pmax)
774
+ for row in rows:
775
+ kv_rows[row] = prefix_kv
776
+ cached_lens[row] = prefix_len
777
+
778
+ return kv_rows, cached_lens
779
+
780
+
781
+ @torch.no_grad()
782
+ def _prefill_prompt_kv_rows(model, prompt_ids, vit_list, img_tok, pad, dev, mode, debug=False, stats=None):
783
+ """Return per-row prompt KV caches and cached lengths.
784
+
785
+ ``mode='none'`` preserves the legacy stock-like first MTP forward where the
786
+ whole prompt and the 6-token MTP window are forwarded together. The split
787
+ prefill modes keep prompt KV clean before the scheduler batches only short
788
+ suffix/window forwards, which avoids ragged prompt+window masking in the
789
+ first decode step.
790
+ """
791
+ bsz = len(prompt_ids)
792
+ lengths = [int(p.numel()) for p in prompt_ids]
793
+ if mode == "none":
794
+ return [None] * bsz, [0] * bsz
795
+
796
+ base = model.language_model.model
797
+ if debug:
798
+ print(f"LA Flash hybrid prompt prefill mode={mode} rows={bsz} lengths={lengths}", flush=True)
799
+
800
+ if mode == "shared":
801
+ return _prefill_shared_prefix_kv_rows(
802
+ model, prompt_ids, vit_list, img_tok, pad, dev, stats=stats, debug=debug)
803
+
804
+ if mode == "per_row":
805
+ kv_rows = []
806
+ for b, ids in enumerate(prompt_ids):
807
+ ids = ids.to(dev).unsqueeze(0)
808
+ pos = torch.arange(ids.shape[1], dtype=torch.long, device=dev).unsqueeze(0)
809
+ out = base(
810
+ input_ids=ids,
811
+ visual_features=vit_list[b],
812
+ image_token_index=img_tok,
813
+ attention_mask=None,
814
+ position_ids=pos,
815
+ past_key_values=None,
816
+ use_cache=True,
817
+ )
818
+ kv_rows.append(out.past_key_values)
819
+ _record_prefill_stats(stats, rows=1, q_len=ids.shape[1], real_tokens=ids.shape[1])
820
+ return kv_rows, lengths
821
+
822
+ pmax = max(lengths)
823
+ input_ids = torch.full((bsz, pmax), pad, dtype=torch.long, device=dev)
824
+ amask = torch.zeros((bsz, pmax), dtype=torch.long, device=dev)
825
+ pos = torch.ones((bsz, pmax), dtype=torch.long, device=dev)
826
+ for b, ids in enumerate(prompt_ids):
827
+ left = pmax - lengths[b]
828
+ input_ids[b, left:] = ids.to(dev)
829
+ amask[b, left:] = 1
830
+ pos[b, left:] = torch.arange(lengths[b], dtype=torch.long, device=dev)
831
+
832
+ visual_features = torch.cat(vit_list, dim=0)
833
+ assert int((input_ids == img_tok).sum().item()) == visual_features.shape[0], \
834
+ "image-token count != supplied visual_features rows"
835
+ forward_mask, fallback_rows = _forward_attention_mask(
836
+ model, input_ids, amask, 0, mtp_window=False)
837
+ if debug and fallback_rows:
838
+ print(
839
+ "LA Flash hybrid batch prefill safe SDPA fallback "
840
+ f"query_rows={fallback_rows}",
841
+ flush=True,
842
+ )
843
+ forward_kwargs = dict(
844
+ input_ids=input_ids,
845
+ visual_features=visual_features,
846
+ image_token_index=img_tok,
847
+ attention_mask=forward_mask,
848
+ position_ids=pos,
849
+ past_key_values=None,
850
+ use_cache=True,
851
+ )
852
+ if isinstance(forward_mask, dict):
853
+ out = language_model_forward(model, **forward_kwargs, return_logits=False)
854
+ else:
855
+ out = base(**forward_kwargs)
856
+ _record_prefill_stats(stats, rows=bsz, q_len=pmax, real_tokens=sum(lengths))
857
+ kv_rows = [
858
+ _unpack_stock_after_forward(out.past_key_values, b, 0, lengths[b], 0, pmax)
859
+ for b in range(bsz)
860
+ ]
861
+ return kv_rows, lengths
862
+
863
+
864
+ @torch.no_grad()
865
+ def generate_batch_hybrid(pairs, temperature=README_TEMPERATURE, top_p=README_TOP_P, top_k=None,
866
+ repetition_penalty=README_REPETITION_PENALTY,
867
+ max_new_tokens=README_MAX_NEW_TOKENS, temps=None,
868
+ debug=None, scheduler=None, group_size=None,
869
+ vision_features=None, _stats=None):
870
+ """Batched stock-style LocateAnything-3B hybrid generation.
871
+
872
+ This mirrors ``model.generate(..., generation_mode='hybrid')``: each row
873
+ owns a full ``generated`` token stream plus a KV cache truncated to real
874
+ generated tokens before sampling. MTP forwards
875
+ ``generated[cached_len:] + duplicate-last + mask*5``; AR forwards
876
+ ``generated[cached_len:]``.
877
+ """
878
+ tok, _, model = load()
879
+ san, hpat = _helpers()
880
+ tids = model.token_ids
881
+ img_tok = model.config.image_token_index
882
+ mask_tok = tids["default_mask_token_id"]
883
+ im_end = tids["im_end_token_id"]
884
+ pad = tok.pad_token_id if tok.pad_token_id is not None else im_end
885
+ dev = DEV
886
+
887
+ if not pairs:
888
+ return []
889
+ if temps is not None and len(temps) != len(pairs):
890
+ raise ValueError("temps must have the same length as pairs")
891
+ if vision_features is not None and len(vision_features) != len(pairs):
892
+ raise ValueError("vision_features must have the same length as pairs")
893
+ debug = _debug_enabled(debug)
894
+ scheduler = _hybrid_scheduler(scheduler)
895
+ group_size = _hybrid_group_size(group_size)
896
+ requested_attn = getattr(model, "_la_flash_requested_attn", ATTN_MODE)
897
+ use_magi = requested_attn == "magi"
898
+ prefill_mode = _hybrid_prefill_mode()
899
+ hold_max_steps = max(0, _env_int("LA_FLASH_HYBRID_HOLD_MAX_STEPS", 5))
900
+ adaptive_hold_mtp_max = max(0, _env_int("LA_FLASH_HYBRID_ADAPTIVE_HOLD_MTP_MAX", 3))
901
+ top_level_stats = _stats is None
902
+ if top_level_stats:
903
+ _stats = _new_hybrid_stats(
904
+ len(pairs), scheduler, group_size, hold_max_steps, adaptive_hold_mtp_max)
905
+ if os.environ.get("LA_FLASH_PLAN_STATS", "0") == "1":
906
+ model._la_flash_sparse_plan_stats = None
907
+ if group_size and len(pairs) > group_size:
908
+ outs = []
909
+ if debug:
910
+ print(
911
+ f"LA Flash hybrid grouped scheduling: total_rows={len(pairs)} "
912
+ f"group_size={group_size} scheduler={scheduler} hold_max_steps={hold_max_steps} "
913
+ f"adaptive_hold_mtp_max={adaptive_hold_mtp_max}",
914
+ flush=True,
915
+ )
916
+ for start in range(0, len(pairs), group_size):
917
+ end = min(start + group_size, len(pairs))
918
+ chunk_temps = temps[start:end] if temps is not None else None
919
+ chunk_vision_features = (
920
+ vision_features[start:end] if vision_features is not None else None
921
+ )
922
+ if debug:
923
+ print(f"LA Flash hybrid group rows=[{start}:{end}]", flush=True)
924
+ outs.extend(generate_batch_hybrid(
925
+ pairs[start:end],
926
+ temperature=temperature,
927
+ top_p=top_p,
928
+ top_k=top_k,
929
+ repetition_penalty=repetition_penalty,
930
+ max_new_tokens=max_new_tokens,
931
+ temps=chunk_temps,
932
+ debug=debug,
933
+ scheduler=scheduler,
934
+ group_size=0,
935
+ vision_features=chunk_vision_features,
936
+ _stats=_stats,
937
+ ))
938
+ if top_level_stats:
939
+ _set_last_hybrid_stats(_stats)
940
+ return outs
941
+
942
+ use_cached_tokenize = (
943
+ vision_features is not None
944
+ and os.environ.get("LA_FLASH_CACHE_TOKENIZE", "1") != "0"
945
+ )
946
+ if use_cached_tokenize:
947
+ try:
948
+ prompt_ids = [
949
+ _tokenize_cached_image(q, int(v.shape[0]), im=im)
950
+ for (im, q), v in zip(pairs, vision_features)
951
+ ]
952
+ except Exception as exc:
953
+ if os.environ.get("LA_FLASH_CACHE_TOKENIZE_STRICT", "0") == "1":
954
+ raise
955
+ if debug:
956
+ print(f"LA Flash cached tokenize fallback: {exc}", flush=True)
957
+ prompt_ids = [_tokenize(im, q) for im, q in pairs]
958
+ else:
959
+ prompt_ids = [_tokenize(im, q) for im, q in pairs]
960
+ vit_list = (
961
+ list(vision_features)
962
+ if vision_features is not None
963
+ else _encode_images([im for im, _ in pairs])
964
+ )
965
+ lengths = [int(p.numel()) for p in prompt_ids]
966
+ bsz = len(pairs)
967
+ _record_group_stats(_stats, bsz)
968
+
969
+ _set_llm_mode(model, requested_attn)
970
+
971
+ modes = ["mtp"] * bsz
972
+ finished = [False] * bsz
973
+ gen_ids = [[] for _ in range(bsz)]
974
+ full_ids = [list(ids.detach().cpu().tolist()) for ids in prompt_ids]
975
+ kv_rows, cached_lens = _prefill_prompt_kv_rows(
976
+ model, prompt_ids, vit_list, img_tok, pad, dev, prefill_mode, debug=debug, stats=_stats)
977
+ total_limits = [lengths[b] + max_new_tokens for b in range(bsz)]
978
+
979
+ row_temps = [float(temperature or 0.0)] * bsz if temps is None else [float(t or 0.0) for t in temps]
980
+
981
+ def run_ar(ar_rows, step_idx):
982
+ row_groups = _split_rows_by_kv_budget(ar_rows, kv_rows)
983
+ _record_kv_bucket_stats(_stats, row_groups, kv_rows)
984
+ for row_group in row_groups:
985
+ _step_stock_ar_rows(
986
+ model, san, tids, prompt_ids, kv_rows, row_group,
987
+ cached_lens, full_ids, gen_ids, modes, finished, total_limits,
988
+ pad, img_tok, row_temps, temperature, top_p, top_k,
989
+ repetition_penalty, dev, tok, debug, step_idx, use_magi, _stats,
990
+ )
991
+
992
+ def run_mtp(mtp_rows, step_idx):
993
+ if any(cached_lens[r] == 0 for r in mtp_rows) and any(cached_lens[r] > 0 for r in mtp_rows):
994
+ first_rows = [r for r in mtp_rows if cached_lens[r] == 0]
995
+ cached_rows = [r for r in mtp_rows if cached_lens[r] > 0]
996
+ if first_rows:
997
+ run_mtp(first_rows, step_idx)
998
+ if cached_rows:
999
+ run_mtp(cached_rows, step_idx)
1000
+ return
1001
+ row_groups = _split_rows_by_kv_budget(mtp_rows, kv_rows)
1002
+ _record_kv_bucket_stats(_stats, row_groups, kv_rows)
1003
+ if len(row_groups) > 1:
1004
+ for row_group in row_groups:
1005
+ run_mtp(row_group, step_idx)
1006
+ return
1007
+ _step_stock_mtp_rows(
1008
+ model, san, hpat, tids, prompt_ids, kv_rows, mtp_rows,
1009
+ cached_lens, full_ids, gen_ids, modes, finished, total_limits,
1010
+ vit_list, pad, mask_tok, img_tok, row_temps, top_p, top_k,
1011
+ repetition_penalty, dev, tok, debug, step_idx, use_magi, _stats,
1012
+ )
1013
+
1014
+ def live_rows(mode):
1015
+ return [b for b in range(bsz) if not finished[b] and modes[b] == mode]
1016
+
1017
+ step = 0
1018
+ hold_steps = 0
1019
+ while not all(finished) and step <= max_new_tokens:
1020
+ step += 1
1021
+ if _stats is not None:
1022
+ _stats["decode_loops"] += 1
1023
+ if scheduler == "hold_ar" and hold_max_steps > 0:
1024
+ ar_rows = live_rows("ar")
1025
+ mtp_rows = live_rows("mtp")
1026
+ if ar_rows and mtp_rows and _stats is not None:
1027
+ _stats["mixed_mode_cycles"] += 1
1028
+ if ar_rows and (hold_steps < hold_max_steps or not mtp_rows):
1029
+ if mtp_rows and _stats is not None:
1030
+ _stats["hold_ar_steps"] += 1
1031
+ _stats["hold_ar_held_mtp_rows"] += len(mtp_rows)
1032
+ run_ar(ar_rows, step)
1033
+ hold_steps += 1
1034
+ continue
1035
+ if mtp_rows:
1036
+ if ar_rows and _stats is not None:
1037
+ _stats["hold_ar_limit_mtp_forwards"] += 1
1038
+ run_mtp(mtp_rows, step)
1039
+ hold_steps = 0
1040
+ continue
1041
+
1042
+ if scheduler in {"ar_first", "pipeline", "adaptive"}:
1043
+ ar_rows_at_loop_start = live_rows("ar")
1044
+ mtp_rows_at_loop_start = live_rows("mtp")
1045
+ mixed = bool(ar_rows_at_loop_start and mtp_rows_at_loop_start)
1046
+ if mixed and _stats is not None:
1047
+ _stats["mixed_mode_cycles"] += 1
1048
+
1049
+ if scheduler == "adaptive" and mixed and hold_max_steps > 0:
1050
+ should_hold = len(mtp_rows_at_loop_start) <= adaptive_hold_mtp_max
1051
+ if should_hold and hold_steps < hold_max_steps:
1052
+ if _stats is not None:
1053
+ _stats["adaptive_hold_cycles"] += 1
1054
+ _stats["hold_ar_steps"] += 1
1055
+ _stats["hold_ar_held_mtp_rows"] += len(mtp_rows_at_loop_start)
1056
+ run_ar(ar_rows_at_loop_start, step)
1057
+ hold_steps += 1
1058
+ continue
1059
+
1060
+ if ar_rows_at_loop_start:
1061
+ if mixed and _stats is not None:
1062
+ if scheduler == "adaptive":
1063
+ _stats["adaptive_ar_first_cycles"] += 1
1064
+ else:
1065
+ _stats["ar_first_cycles"] += 1
1066
+ run_ar(ar_rows_at_loop_start, step)
1067
+
1068
+ mtp_rows = live_rows("mtp")
1069
+ if mtp_rows:
1070
+ run_mtp(mtp_rows, step)
1071
+ hold_steps = 0
1072
+
1073
+ if scheduler == "pipeline" and mtp_rows:
1074
+ old_ar = set(ar_rows_at_loop_start)
1075
+ new_ar_rows = [b for b in live_rows("ar") if b not in old_ar]
1076
+ if new_ar_rows:
1077
+ if _stats is not None:
1078
+ _stats["pipeline_ar_after_mtp_cycles"] += 1
1079
+ run_ar(new_ar_rows, step)
1080
+ continue
1081
+
1082
+ mtp_rows = live_rows("mtp")
1083
+ ar_rows_at_loop_start = live_rows("ar")
1084
+ if mtp_rows and ar_rows_at_loop_start and _stats is not None:
1085
+ _stats["mixed_mode_cycles"] += 1
1086
+ if mtp_rows:
1087
+ run_mtp(mtp_rows, step)
1088
+
1089
+ ar_rows = [b for b in range(bsz) if not finished[b] and modes[b] == "ar"]
1090
+ if mtp_rows and ar_rows and _stats is not None:
1091
+ _stats["eager_mtp_then_ar_cycles"] += 1
1092
+ if ar_rows:
1093
+ run_ar(ar_rows, step)
1094
+
1095
+ outs = [
1096
+ tok.decode(torch.tensor(gen_ids[b], dtype=torch.long, device=dev),
1097
+ skip_special_tokens=False) if gen_ids[b] else ""
1098
+ for b in range(bsz)
1099
+ ]
1100
+ if top_level_stats:
1101
+ if os.environ.get("LA_FLASH_PLAN_STATS", "0") == "1":
1102
+ _stats["sparse_plan_stats"] = copy.deepcopy(
1103
+ getattr(model, "_la_flash_sparse_plan_stats", None) or {}
1104
+ )
1105
+ _set_last_hybrid_stats(_stats)
1106
+ return outs
1107
+
1108
+
1109
+ @torch.no_grad()
1110
+ def _step_stock_mtp_rows(model, san, hpat, tids, prompt_ids, kv_rows, rows,
1111
+ cached_lens, full_ids, gen_ids, modes, finished, total_limits,
1112
+ vit_list, pad, mask_tok, img_tok, row_temps, top_p, top_k,
1113
+ repetition_penalty, dev, tok, debug, step_idx, use_magi, stats=None):
1114
+ kv, kvalid, old_lens, kmax = _pack_stock_kv_rows(kv_rows, rows, dev)
1115
+ uncached_lens = [len(full_ids[r]) - cached_lens[r] for r in rows]
1116
+ umax = max(uncached_lens)
1117
+ seq_len = umax + N_FUTURE
1118
+ _record_forward_stats(stats, "mtp", rows, seq_len, uncached_lens)
1119
+
1120
+ suf_ids = torch.full((len(rows), seq_len), pad, dtype=torch.long, device=dev)
1121
+ suf_pos = torch.ones((len(rows), seq_len), dtype=torch.long, device=dev)
1122
+ q_valid = torch.zeros((len(rows), seq_len), dtype=torch.long, device=dev)
1123
+
1124
+ for i, r in enumerate(rows):
1125
+ uncached = full_ids[r][cached_lens[r] :]
1126
+ left = umax - len(uncached)
1127
+ if uncached:
1128
+ suf_ids[i, left : left + len(uncached)] = torch.tensor(uncached, dtype=torch.long, device=dev)
1129
+ suf_pos[i, left : left + len(uncached)] = torch.arange(
1130
+ cached_lens[r], len(full_ids[r]), dtype=torch.long, device=dev)
1131
+ q_valid[i, left : left + len(uncached)] = 1
1132
+
1133
+ rep = full_ids[r][-1]
1134
+ cur_len = len(full_ids[r])
1135
+ suf_ids[i, umax] = rep
1136
+ suf_pos[i, umax] = cur_len - 1
1137
+ q_valid[i, umax] = 1
1138
+ for j in range(1, N_FUTURE):
1139
+ suf_ids[i, umax + j] = mask_tok
1140
+ suf_pos[i, umax + j] = cur_len + (j - 1)
1141
+ q_valid[i, umax + j] = 1
1142
+
1143
+ full_mask = torch.cat([kvalid, q_valid], dim=1)
1144
+
1145
+ if debug:
1146
+ forward_mask, fallback_rows = _forward_attention_mask(
1147
+ model, suf_ids, full_mask, kmax, mtp_window=True, range_plan=True)
1148
+ _print_debug_forward(
1149
+ f"MTP step={step_idx}",
1150
+ model,
1151
+ tok,
1152
+ suf_ids,
1153
+ full_mask,
1154
+ suf_pos,
1155
+ past_len=kmax,
1156
+ mtp_window=True,
1157
+ extra={
1158
+ "global_rows": rows,
1159
+ "old_kv_lens": old_lens,
1160
+ "cached_lens": [cached_lens[r] for r in rows],
1161
+ "full_lens": [len(full_ids[r]) for r in rows],
1162
+ "uncached_lens": uncached_lens,
1163
+ "forward_attention_mask": _mask_desc(forward_mask),
1164
+ "safe_sdpa_fallback_query_rows": fallback_rows,
1165
+ },
1166
+ attention_impl="magi" if use_magi else ATTN_MODE,
1167
+ )
1168
+ else:
1169
+ forward_mask, _ = _forward_attention_mask(
1170
+ model, suf_ids, full_mask, kmax, mtp_window=True, range_plan=True)
1171
+ first_rows = [r for r in rows if cached_lens[r] == 0]
1172
+ visual_features = None
1173
+ if first_rows:
1174
+ if first_rows != rows:
1175
+ raise RuntimeError("mixed first/non-first MTP rows are not supported")
1176
+ visual_features = torch.cat([vit_list[r] for r in rows], dim=0)
1177
+ assert int((suf_ids == img_tok).sum().item()) == visual_features.shape[0], \
1178
+ "image-token count != supplied visual_features rows"
1179
+ out = language_model_forward(
1180
+ model, input_ids=suf_ids, attention_mask=forward_mask,
1181
+ position_ids=suf_pos, past_key_values=kv, use_cache=True,
1182
+ visual_features=visual_features,
1183
+ image_token_index=img_tok if visual_features is not None else None,
1184
+ logits_slice=slice(-N_FUTURE, None))
1185
+
1186
+ for i, r in enumerate(rows):
1187
+ kv_rows[r] = _unpack_stock_after_forward(
1188
+ out.past_key_values, i, old_lens[i], uncached_lens[i], kmax, umax)
1189
+ cached_lens[r] = len(full_ids[r])
1190
+
1191
+ wlogits = out.logits[:, -N_FUTURE:, :]
1192
+ local_prompts = [prompt_ids[r] for r in rows]
1193
+ local_gen = [gen_ids[r] for r in rows]
1194
+ gen_pad = _pad_generated(local_prompts, local_gen, img_tok, dev)
1195
+ per_row_temp = torch.tensor([row_temps[r] for r in rows], dtype=torch.float32, device=dev)
1196
+
1197
+ if BATCH_SAN:
1198
+ x0_all, boxes_all = sample_tokens_batched(
1199
+ wlogits, gen_pad, tids, per_row_temp,
1200
+ repetition_penalty=repetition_penalty, top_p=top_p, top_k=top_k,
1201
+ keep_k_avg=4, generation_mode="hybrid")
1202
+
1203
+ for i, r in enumerate(rows):
1204
+ if finished[r]:
1205
+ continue
1206
+ if BATCH_SAN:
1207
+ x0b, boxb = x0_all[i], boxes_all[i]
1208
+ else:
1209
+ gk = _mk_generate_kwargs(row_temps[r], top_p, top_k, repetition_penalty)
1210
+ _, _, x0, box_avg = san(wlogits[i : i + 1], gen_pad[i : i + 1], tids, keep_k=5, **gk)
1211
+ x0b, boxb = x0[0], box_avg[0]
1212
+ nt = x0b if bool((boxb == 0).all()) else boxb
1213
+ op = hpat(nt, tids, "hybrid")
1214
+
1215
+ toks = [int(t) for t in op["tokens"]]
1216
+ for t in toks:
1217
+ gen_ids[r].append(t)
1218
+ full_ids[r].append(t)
1219
+
1220
+ if op["type"] == "im_end":
1221
+ finished[r] = True
1222
+ elif op["type"] == "error_box":
1223
+ modes[r] = "ar"
1224
+ if len(full_ids[r]) >= total_limits[r]:
1225
+ finished[r] = True
1226
+
1227
+
1228
+ @torch.no_grad()
1229
+ def _step_stock_ar_rows(model, san, tids, prompt_ids, kv_rows, rows,
1230
+ cached_lens, full_ids, gen_ids, modes, finished, total_limits,
1231
+ pad, img_tok, row_temps, temperature, top_p, top_k,
1232
+ repetition_penalty, dev, tok, debug, step_idx, use_magi, stats=None):
1233
+ kv, kvalid, old_lens, kmax = _pack_stock_kv_rows(kv_rows, rows, dev)
1234
+ uncached_lens = [len(full_ids[r]) - cached_lens[r] for r in rows]
1235
+ if any(n <= 0 for n in uncached_lens):
1236
+ raise RuntimeError(f"AR rows have no uncached tokens: {rows}")
1237
+ umax = max(uncached_lens)
1238
+ _record_forward_stats(stats, "ar", rows, umax, uncached_lens)
1239
+
1240
+ suf_ids = torch.full((len(rows), umax), pad, dtype=torch.long, device=dev)
1241
+ suf_pos = torch.ones((len(rows), umax), dtype=torch.long, device=dev)
1242
+ q_valid = torch.zeros((len(rows), umax), dtype=torch.long, device=dev)
1243
+
1244
+ for i, r in enumerate(rows):
1245
+ uncached = full_ids[r][cached_lens[r] :]
1246
+ left = umax - len(uncached)
1247
+ suf_ids[i, left:] = torch.tensor(uncached, dtype=torch.long, device=dev)
1248
+ suf_pos[i, left:] = torch.arange(cached_lens[r], len(full_ids[r]), dtype=torch.long, device=dev)
1249
+ q_valid[i, left:] = 1
1250
+
1251
+ full_mask = torch.cat([kvalid, q_valid], dim=1)
1252
+ if debug:
1253
+ forward_mask, fallback_rows = _forward_attention_mask(
1254
+ model, suf_ids, full_mask, kmax, mtp_window=False, range_plan=True)
1255
+ _print_debug_forward(
1256
+ f"AR step={step_idx}",
1257
+ model,
1258
+ tok,
1259
+ suf_ids,
1260
+ full_mask,
1261
+ suf_pos,
1262
+ past_len=kmax,
1263
+ mtp_window=False,
1264
+ extra={
1265
+ "global_rows": rows,
1266
+ "old_kv_lens": old_lens,
1267
+ "cached_lens": [cached_lens[r] for r in rows],
1268
+ "full_lens": [len(full_ids[r]) for r in rows],
1269
+ "uncached_lens": uncached_lens,
1270
+ "forward_attention_mask": _mask_desc(forward_mask),
1271
+ "safe_sdpa_fallback_query_rows": fallback_rows,
1272
+ },
1273
+ attention_impl="magi" if use_magi else ATTN_MODE,
1274
+ )
1275
+ else:
1276
+ forward_mask, _ = _forward_attention_mask(
1277
+ model, suf_ids, full_mask, kmax, mtp_window=False, range_plan=True)
1278
+
1279
+ out = language_model_forward(
1280
+ model, input_ids=suf_ids, attention_mask=forward_mask,
1281
+ position_ids=suf_pos, past_key_values=kv, use_cache=True,
1282
+ logits_slice=slice(-1, None))
1283
+
1284
+ for i, r in enumerate(rows):
1285
+ kv_rows[r] = _unpack_stock_after_forward(
1286
+ out.past_key_values, i, old_lens[i], uncached_lens[i], kmax, umax)
1287
+ cached_lens[r] = len(full_ids[r])
1288
+
1289
+ if AR_BATCH_SAN:
1290
+ local_prompts = [prompt_ids[r] for r in rows]
1291
+ local_gen = [gen_ids[r] for r in rows]
1292
+ gen_pad = _pad_generated(local_prompts, local_gen, img_tok, dev)
1293
+ per_row_temp = torch.tensor([row_temps[r] for r in rows], dtype=torch.float32, device=dev)
1294
+ x0_all = sample_next_tokens_batched(
1295
+ out.logits[:, -1:, :],
1296
+ gen_pad,
1297
+ per_row_temp,
1298
+ repetition_penalty=repetition_penalty,
1299
+ top_p=top_p,
1300
+ top_k=top_k,
1301
+ )
1302
+
1303
+ for i, r in enumerate(rows):
1304
+ if AR_BATCH_SAN:
1305
+ token_val = int(x0_all[i, 0].item())
1306
+ else:
1307
+ logits = out.logits[i : i + 1, -1:, :]
1308
+ gen_pad = _pad_generated([prompt_ids[r]], [gen_ids[r]], img_tok, dev)
1309
+ gk = _mk_generate_kwargs(temperature, top_p, top_k, repetition_penalty, row_temp=row_temps[r])
1310
+ _, _, x0, _ = san(logits, gen_pad, tids, **gk)
1311
+ token_val = int(x0[0, 0].item())
1312
+ out_type = _classify_ar_token(token_val, tids)
1313
+
1314
+ gen_ids[r].append(token_val)
1315
+ full_ids[r].append(token_val)
1316
+
1317
+ if out_type == "im_end":
1318
+ finished[r] = True
1319
+ elif out_type == "box_end_ar":
1320
+ modes[r] = "mtp"
1321
+
1322
+ if len(full_ids[r]) >= total_limits[r]:
1323
+ finished[r] = True
1324
+
1325
+
1326
+ def generate_batch_grouped_hybrid(groups, temperature=README_TEMPERATURE, top_p=README_TOP_P,
1327
+ top_k=None, repetition_penalty=README_REPETITION_PENALTY,
1328
+ max_new_tokens=README_MAX_NEW_TOKENS, temps=None,
1329
+ debug=None, scheduler=None, group_size=None,
1330
+ vision_features=None):
1331
+ """Hybrid grouped API shape.
1332
+
1333
+ This preserves grouped return shape, but intentionally uses the generic
1334
+ hybrid decoder rather than the fast engine's shared-prefix optimization.
1335
+ """
1336
+ flat = []
1337
+ flat_vision_features = [] if vision_features is not None else None
1338
+ counts = []
1339
+ for group_idx, (im, queries) in enumerate(groups):
1340
+ counts.append(len(queries))
1341
+ flat.extend((im, q) for q in queries)
1342
+ if flat_vision_features is not None:
1343
+ flat_vision_features.extend([vision_features[group_idx]] * len(queries))
1344
+
1345
+ outs = generate_batch_hybrid(
1346
+ flat, temperature=temperature, top_p=top_p, top_k=top_k,
1347
+ repetition_penalty=repetition_penalty, max_new_tokens=max_new_tokens,
1348
+ temps=temps, debug=debug, scheduler=scheduler, group_size=group_size,
1349
+ vision_features=flat_vision_features)
1350
+ res, offset = [], 0
1351
+ for n in counts:
1352
+ res.append(outs[offset : offset + n])
1353
+ offset += n
1354
+ return res
1355
+
1356
+
1357
+ __all__ = ["generate_batch_hybrid", "generate_batch_grouped_hybrid", "get_last_hybrid_stats"]
batch_utils/hybrid_runtime.py ADDED
@@ -0,0 +1,1842 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Internal runtime support for the LocateAnything-3B hybrid batch decoder.
2
+
3
+ This file keeps only the model-loading, tokenization, image-encoding, stock
4
+ processor, and sample-token helpers that ``engine_hybrid.py`` needs.
5
+
6
+ Important env knobs:
7
+ LA_FLASH_MODEL HF repo id / local path of the model (default nvidia/LocateAnything-3B)
8
+ HF_HUB_OFFLINE=1 read the local HF cache only (no network); unset -> download on first use
9
+ LA_FLASH_ATTN sdpa, eager, magi, or la_flash; la_flash uses FlashAttention sparse ranges
10
+ LA_FLASH_STRICT_ATTN 1 -> fail if the requested backend is unavailable;
11
+ default 0 falls back to sdpa
12
+ LA_FLASH_VISION_ATTN auto, flash_attention_2, sdpa, or eager (default auto)
13
+ LA_FLASH_HYBRID_PREFILL shared, none, per_row, or batch prompt KV prefill (default shared)
14
+ MTP_BATCH_VISION 0 -> per-image vision encode (default 1: batched when flash is present)
15
+ LA_FLASH_VISION_ENCODE_BATCH_SIZE
16
+ max images per MoonViT encode micro-batch (default 8; <=0 disables limit)
17
+ MTP_BATCH_SAN 0 -> per-row logits/sample pipeline (default 1: batched over [B,6,V])
18
+ AR_BATCH_SAN 0 -> per-row AR sample pipeline (default 1: batched over [B,1,V])
19
+ """
20
+ import inspect
21
+ import os, warnings, importlib, torch
22
+ from types import SimpleNamespace
23
+ import numpy as np
24
+ from transformers import AutoModel, AutoTokenizer, AutoProcessor
25
+
26
+
27
+ # By default let transformers fetch the model on first use; set HF_HUB_OFFLINE=1 yourself
28
+ # to read the local HF cache only (e.g. air-gapped / already-downloaded runs).
29
+ MODEL = os.environ.get("LA_FLASH_MODEL", "nvidia/LocateAnything-3B")
30
+
31
+
32
+ LLM_ATTN_MODES = ("sdpa", "eager", "magi", "la_flash")
33
+ VISION_ATTN_MODES = ("auto", "flash_attention_2", "sdpa", "eager")
34
+
35
+
36
+ def _normalize_attn_mode(value):
37
+ mode = (value or "sdpa").strip().lower().replace("-", "_")
38
+ aliases = {
39
+ "": "sdpa",
40
+ "manual": "eager",
41
+ "torch": "eager",
42
+ "torch_eager": "eager",
43
+ "torch_sdpa": "sdpa",
44
+ "scaled_dot_product_attention": "sdpa",
45
+ "flash": "la_flash",
46
+ "la_flash": "la_flash",
47
+ "kernel": "la_flash",
48
+ "cuda": "la_flash",
49
+ "range": "la_flash",
50
+ "range_attention": "la_flash",
51
+ "flex_flash": "magi",
52
+ "flex_flash_attention": "magi",
53
+ "flex_flash_attn": "magi",
54
+ }
55
+ mode = aliases.get(mode, mode)
56
+ if mode not in LLM_ATTN_MODES:
57
+ raise ValueError(
58
+ f"LA_FLASH_ATTN must be one of {', '.join(LLM_ATTN_MODES)}; got {value!r}"
59
+ )
60
+ return mode
61
+
62
+
63
+ def _normalize_vision_attn_mode(value):
64
+ mode = (value or "auto").strip().lower().replace("-", "_")
65
+ aliases = {
66
+ "": "auto",
67
+ "flash": "flash_attention_2",
68
+ "flash_attention2": "flash_attention_2",
69
+ "fa2": "flash_attention_2",
70
+ "manual": "eager",
71
+ }
72
+ mode = aliases.get(mode, mode)
73
+ if mode not in VISION_ATTN_MODES:
74
+ raise ValueError(
75
+ f"LA_FLASH_VISION_ATTN must be one of {', '.join(VISION_ATTN_MODES)}; got {value!r}"
76
+ )
77
+ return mode
78
+
79
+
80
+ ATTN_MODE = _normalize_attn_mode(os.environ.get("LA_FLASH_ATTN", "sdpa"))
81
+ REMOTE_ATTN_MODE = "sdpa" if ATTN_MODE in {"la_flash", "magi"} else ATTN_MODE
82
+ VISION_ATTN_MODE = _normalize_vision_attn_mode(os.environ.get("LA_FLASH_VISION_ATTN", "auto"))
83
+ MAX_DIM = 1024
84
+ DEV, DT = "cuda", torch.bfloat16
85
+ N_FUTURE = 6 # = config.block_size (MTP window)
86
+ _PROMPT = "Locate all the instances that matches the following description: "
87
+
88
+
89
+ def _env_flag(name, default=False):
90
+ val = os.environ.get(name)
91
+ if val is None:
92
+ return default
93
+ return val.strip().lower() not in {"0", "false", "no", "off"}
94
+
95
+
96
+ def _env_int(name):
97
+ val = os.environ.get(name)
98
+ if val is None or val.strip() == "":
99
+ return None
100
+ return int(val)
101
+
102
+
103
+ def _strict_attn():
104
+ return _env_flag("LA_FLASH_STRICT_ATTN", False)
105
+
106
+
107
+ def _fallback_to_sdpa(model, requested, reason):
108
+ if requested == "sdpa":
109
+ raise RuntimeError(f"LA_FLASH_ATTN=sdpa failed: {reason}") from reason
110
+ message = f"LA_FLASH_ATTN={requested} is unavailable; falling back to sdpa. Reason: {reason}"
111
+ if _strict_attn():
112
+ raise RuntimeError(message) from reason
113
+ warnings.warn(message)
114
+ _set_llm_mode(model, "sdpa")
115
+ model._la_flash_requested_attn_original = requested
116
+ model._la_flash_attn_fallback_reason = str(reason)
117
+ return "sdpa"
118
+
119
+
120
+ # Optional compile for the shared Qwen2 core. This is off by default because the
121
+ # hybrid scheduler already varies query/cache shapes and first-call compile cost is high.
122
+ MTP_COMPILE = os.environ.get("MTP_COMPILE", "0") == "1"
123
+
124
+ # Batch the MoonViT vision encode across a micro-batch's images: pack N images into ONE
125
+ # extract_feature. With flash present, MoonViT's varlen cu_seqlens path is block-diagonal per
126
+ # image and equivalent to per-image encode.
127
+ # Without flash, sdpa builds a dense [1,S,S] mask -> O(S^2) N^2 -> per-image fallback (auto, see
128
+ # _vision_is_flash). Default ON; set MTP_BATCH_VISION=0 to force per-image.
129
+ BATCH_VISION = os.environ.get("MTP_BATCH_VISION", "1") == "1"
130
+ _vision_encode_batch_size = _env_int("LA_FLASH_VISION_ENCODE_BATCH_SIZE")
131
+ VISION_ENCODE_BATCH_SIZE = 8 if _vision_encode_batch_size is None else max(0, _vision_encode_batch_size)
132
+
133
+ # Batch the per-row box-decode (sample_tokens): run the row-independent logits pipeline
134
+ # (rep-penalty / per-row temperature / top_p / top_k / softmax / sample) ONCE over the whole
135
+ # [B,6,V] step instead of B times on [1,6,V]; only the variable-length box assembly stays per-row.
136
+ # Greedy is BIT-IDENTICAL to the per-row san (argmax, no RNG). Default ON; MTP_BATCH_SAN=0 -> per-row.
137
+ BATCH_SAN = os.environ.get("MTP_BATCH_SAN", "1") == "1"
138
+
139
+ # Batch the AR repair sampler over [B,1,V]. This shares the exact filtering
140
+ # helpers with MTP batching but skips box/ref decoding, so it only replaces the
141
+ # repeated stock one-token sample calls. Sampling itself stays row-ordered by
142
+ # default to preserve the stock RNG consumption pattern for AR repair.
143
+ AR_BATCH_SAN = os.environ.get("AR_BATCH_SAN", "1") == "1"
144
+
145
+ _tok = _proc = _model = None
146
+
147
+ def _magi_diag():
148
+ lines = []
149
+ try:
150
+ import magi_attention
151
+ lines.append(f"magi_attention: OK file={getattr(magi_attention, '__file__', None)}")
152
+ lines.append(f"magi_attention.__version__={getattr(magi_attention, '__version__', '<missing>')}")
153
+ except Exception as e:
154
+ lines.append(f"magi_attention: FAIL {type(e).__name__}: {e}")
155
+ return "\n".join(lines)
156
+ try:
157
+ from magi_attention.functional.flex_flash_attn import flex_flash_attn_func
158
+ lines.append(f"magi_attention.functional.flex_flash_attn: OK func={flex_flash_attn_func}")
159
+ except Exception as e:
160
+ lines.append(f"magi_attention.functional.flex_flash_attn: FAIL {type(e).__name__}: {e}")
161
+ return "\n".join(lines)
162
+
163
+ def _remote_magi_diag(model=None):
164
+ lines = []
165
+ try:
166
+ if model is not None:
167
+ mod = importlib.import_module(type(model.language_model.model).__module__)
168
+ else:
169
+ # Best effort: if the dynamic module is not imported yet this may fail;
170
+ # the post-load diagnostic below will still work.
171
+ mod = importlib.import_module("transformers_modules.LocateAnything-3B.modeling_qwen2")
172
+ lines.append(f"remote_qwen2_module={getattr(mod, '__file__', None)}")
173
+ lines.append(f"remote_qwen2._MAGI_AVAILABLE={getattr(mod, '_MAGI_AVAILABLE', '<missing>')!r}")
174
+ lines.append(f"remote_qwen2.flex_flash_attn_func={getattr(mod, 'flex_flash_attn_func', '<missing>')}")
175
+ except Exception as e:
176
+ lines.append(f"remote_qwen2: diagnostic failed {type(e).__name__}: {e}")
177
+ return "\n".join(lines)
178
+
179
+ def _attn_class_diag(model):
180
+ try:
181
+ llm = model.language_model.model
182
+ classes = [type(layer.self_attn).__name__ for layer in llm.layers[:4]]
183
+ return (
184
+ f"llm._attn_implementation={getattr(llm, '_attn_implementation', None)!r}\n"
185
+ f"config._attn_implementation={getattr(llm.config, '_attn_implementation', None)!r}\n"
186
+ f"first_attn_classes={classes}"
187
+ )
188
+ except Exception as e:
189
+ return f"attention class diagnostic failed {type(e).__name__}: {e}"
190
+
191
+
192
+ def _set_vision_attention_mode(model):
193
+ """Match HF's MoonViT policy: prefer flash_attention_2, then sdpa, then eager."""
194
+ vm = getattr(model, "vision_model", None)
195
+ if vm is None:
196
+ return None
197
+ mod = importlib.import_module(type(vm).__module__)
198
+ funcs = getattr(mod, "VL_VISION_ATTENTION_FUNCTIONS", {})
199
+ has_flash = getattr(mod, "flash_attn_varlen_func", None) is not None
200
+ requested = VISION_ATTN_MODE
201
+
202
+ if requested == "auto":
203
+ candidates = ("flash_attention_2", "sdpa", "eager")
204
+ else:
205
+ candidates = (requested, "flash_attention_2", "sdpa", "eager")
206
+
207
+ chosen = None
208
+ for candidate in candidates:
209
+ if candidate == "flash_attention_2" and not has_flash:
210
+ continue
211
+ if candidate in funcs:
212
+ chosen = candidate
213
+ break
214
+ if chosen is None:
215
+ raise RuntimeError("MoonViT has no supported attention implementation.")
216
+
217
+ if requested == "flash_attention_2" and chosen != "flash_attention_2":
218
+ warnings.warn("LA_FLASH_VISION_ATTN=flash_attention_2 requested but flash-attn is unavailable; "
219
+ f"using {chosen}.")
220
+ elif requested not in {"auto", chosen}:
221
+ warnings.warn(f"LA_FLASH_VISION_ATTN={requested} is unavailable; using {chosen}.")
222
+
223
+ if hasattr(model.config, "vision_config"):
224
+ model.config.vision_config._attn_implementation = chosen
225
+ try:
226
+ vm.config._attn_implementation = chosen
227
+ except Exception:
228
+ pass
229
+ try:
230
+ for block in vm.encoder.blocks:
231
+ block.attn_implementation = chosen
232
+ except Exception as exc:
233
+ raise RuntimeError("Failed to configure MoonViT attention implementation.") from exc
234
+ model._la_flash_vision_attn = chosen
235
+ return chosen
236
+
237
+
238
+ def load():
239
+ """Lazy model load with HF remote-code semantics plus release backends.
240
+
241
+ The text decoder is pinned to one of sdpa/eager/magi/la_flash. MoonViT is
242
+ configured independently and follows the HF policy: flash_attention_2 when
243
+ flash-attn is importable, otherwise sdpa, otherwise eager.
244
+ """
245
+ global _tok, _proc, _model
246
+ if _model is None:
247
+ _tok = AutoTokenizer.from_pretrained(MODEL, trust_remote_code=True)
248
+ _proc = AutoProcessor.from_pretrained(MODEL, trust_remote_code=True)
249
+ attn_impl = REMOTE_ATTN_MODE
250
+ if ATTN_MODE == "magi" and os.environ.get("LA_FLASH_DEBUG", "0") != "0":
251
+ print("LA Flash magi pre-load diagnostic:", flush=True)
252
+ print(_magi_diag(), flush=True)
253
+ _model = AutoModel.from_pretrained(MODEL, torch_dtype=DT, trust_remote_code=True,
254
+ attn_implementation=attn_impl).to(DEV).eval()
255
+ _set_vision_attention_mode(_model)
256
+ actual_attn = getattr(_model.language_model.model, "_attn_implementation", None)
257
+ if ATTN_MODE == "magi" and os.environ.get("LA_FLASH_DEBUG", "0") != "0":
258
+ print("LA Flash magi post-load diagnostic:", flush=True)
259
+ print(_remote_magi_diag(_model), flush=True)
260
+ print(_attn_class_diag(_model), flush=True)
261
+ if ATTN_MODE == "magi":
262
+ try:
263
+ qwen2_mod = importlib.import_module(type(_model.language_model.model).__module__)
264
+ if not getattr(qwen2_mod, "_MAGI_AVAILABLE", False):
265
+ raise RuntimeError(
266
+ "remote module reports _MAGI_AVAILABLE=False.\n"
267
+ f"{_remote_magi_diag(_model)}\n{_magi_diag()}"
268
+ )
269
+ first_attn = type(_model.language_model.model.layers[0].self_attn).__name__
270
+ if actual_attn != "sdpa" or first_attn != "_BatchedMagiAttention":
271
+ _set_llm_mode(_model, "magi")
272
+ actual_attn = getattr(_model.language_model.model, "_attn_implementation", None)
273
+ first_attn = type(_model.language_model.model.layers[0].self_attn).__name__
274
+ if os.environ.get("LA_FLASH_DEBUG", "0") != "0":
275
+ print("LA Flash magi post-swap diagnostic:", flush=True)
276
+ print(_attn_class_diag(_model), flush=True)
277
+ if actual_attn != "sdpa" or first_attn != "_BatchedMagiAttention":
278
+ raise RuntimeError(
279
+ "batched magi attention did not activate. "
280
+ f"actual_attn={actual_attn!r}; first_attn={first_attn!r}; "
281
+ f"{_remote_magi_diag(_model)}; {_attn_class_diag(_model)}"
282
+ )
283
+ _model._la_flash_requested_attn = "magi"
284
+ except Exception as exc:
285
+ _fallback_to_sdpa(_model, "magi", exc)
286
+ else:
287
+ try:
288
+ _set_llm_mode(_model, ATTN_MODE) # decode-safe mask plumbing for sdpa/eager/la_flash
289
+ except Exception as exc:
290
+ _fallback_to_sdpa(_model, ATTN_MODE, exc)
291
+ if MTP_COMPILE:
292
+ _maybe_compile(_model)
293
+ return _tok, _proc, _model
294
+
295
+
296
+ def _maybe_compile(model):
297
+ """Compile the shared Qwen2Model core (base.forward). It backs BOTH prefill (called directly)
298
+ and decode (language_model.forward -> self.model). lm_head + MoonViT left eager. dynamic=True
299
+ so the varying decode S/kvlen don't trigger a recompile storm. No-op + warning if triton is
300
+ missing (inductor needs it on GPU). First call pays the compile cost (~42s warm / ~187s cold)."""
301
+ try:
302
+ import triton # noqa: F401
303
+ except Exception:
304
+ warnings.warn("MTP_COMPILE set but triton is unavailable; running without torch.compile.")
305
+ return
306
+ import torch._dynamo as _dyn
307
+ _dyn.config.cache_size_limit = max(_dyn.config.cache_size_limit, 64)
308
+ base = model.language_model.model
309
+ if not getattr(base, "_mtp_compiled", False):
310
+ base.forward = torch.compile(base.forward, dynamic=True)
311
+ base._mtp_compiled = True
312
+
313
+
314
+ def build_batched_magi_attention_class(mod):
315
+ """Build a Qwen2 attention subclass backed by Magi's flex_flash_attn.
316
+
317
+ The official LocateAnything ``Qwen2MagiAttention`` asserts ``bsz == 1`` and
318
+ relies on ``Qwen2Model._attn_implementation == "magi"`` to build a single
319
+ sample range plan. For release batch inference the hybrid scheduler passes
320
+ a batched Magi range plan directly to this layer; a 4D-mask conversion path
321
+ remains as a compatibility fallback.
322
+ """
323
+ flex_flash_attn_func = getattr(mod, "flex_flash_attn_func", None)
324
+ if flex_flash_attn_func is None:
325
+ try:
326
+ from magi_attention.functional.flex_flash_attn import flex_flash_attn_func
327
+ except Exception as exc:
328
+ raise RuntimeError(
329
+ "LA_FLASH_ATTN=magi requires "
330
+ "magi_attention.functional.flex_flash_attn.flex_flash_attn_func."
331
+ ) from exc
332
+
333
+ FULL, CAUSAL = 0, 1
334
+ causal_plan_cache = {}
335
+ try:
336
+ magi_params = set(inspect.signature(flex_flash_attn_func).parameters)
337
+ except (TypeError, ValueError):
338
+ magi_params = set()
339
+ supports_disable_fwd_atomic = "disable_fwd_atomic_reduction" in magi_params
340
+
341
+ def _disjoint_q_ranges(q_ranges):
342
+ seen = set()
343
+ for start, end in q_ranges:
344
+ key = (int(start), int(end))
345
+ if key in seen:
346
+ return False
347
+ seen.add(key)
348
+ return True
349
+
350
+ def _plan_disjoint_q_ranges(plan):
351
+ cached = plan.get("_la_flash_disjoint_q_ranges")
352
+ if cached is not None:
353
+ return bool(cached)
354
+ q_ranges = plan["q_ranges"].detach().to(device="cpu", dtype=torch.int32).tolist()
355
+ disjoint = _disjoint_q_ranges(q_ranges)
356
+ try:
357
+ plan["_la_flash_disjoint_q_ranges"] = disjoint
358
+ except Exception:
359
+ pass
360
+ return disjoint
361
+
362
+ def _tensor_plan(q_ranges, k_ranges, types, device):
363
+ return {
364
+ "q_ranges": torch.tensor(q_ranges, dtype=torch.int32, device=device).contiguous(),
365
+ "k_ranges": torch.tensor(k_ranges, dtype=torch.int32, device=device).contiguous(),
366
+ "attn_type_map": torch.tensor(types, dtype=torch.int32, device=device).contiguous(),
367
+ "_la_flash_disjoint_q_ranges": _disjoint_q_ranges(q_ranges),
368
+ }
369
+
370
+ def _offset_plan(plan, q_offset, k_offset):
371
+ return (
372
+ (plan["q_ranges"] + int(q_offset)).tolist(),
373
+ (plan["k_ranges"] + int(k_offset)).tolist(),
374
+ plan["attn_type_map"].tolist(),
375
+ )
376
+
377
+ def _causal_plan(bsz, q_len, kv_seq_len, device):
378
+ key = (int(bsz), int(q_len), int(kv_seq_len), device.type, device.index)
379
+ cached = causal_plan_cache.get(key)
380
+ if cached is not None:
381
+ return cached
382
+ q_ranges, k_ranges, types = [], [], []
383
+ for b in range(int(bsz)):
384
+ q_base = b * int(q_len)
385
+ k_base = b * int(kv_seq_len)
386
+ q_ranges.append([q_base, q_base + int(q_len)])
387
+ k_ranges.append([k_base, k_base + int(kv_seq_len)])
388
+ types.append(CAUSAL)
389
+ plan = _tensor_plan(q_ranges, k_ranges, types, device)
390
+ plan.update(
391
+ {
392
+ "flash_cu_seqlens_q": torch.arange(
393
+ 0,
394
+ (int(bsz) + 1) * int(q_len),
395
+ int(q_len),
396
+ dtype=torch.int32,
397
+ device=device,
398
+ ),
399
+ "flash_cu_seqlens_k": torch.arange(
400
+ 0,
401
+ (int(bsz) + 1) * int(kv_seq_len),
402
+ int(kv_seq_len),
403
+ dtype=torch.int32,
404
+ device=device,
405
+ ),
406
+ "flash_causal": True,
407
+ }
408
+ )
409
+ causal_plan_cache[key] = plan
410
+ return plan
411
+
412
+ def _row_segments(row):
413
+ idx = np.flatnonzero(row)
414
+ if idx.size == 0:
415
+ return ((0, 1),)
416
+ split = np.flatnonzero(np.diff(idx) > 1) + 1
417
+ starts = np.concatenate((idx[:1], idx[split]))
418
+ ends = np.concatenate((idx[split - 1], idx[-1:])) + 1
419
+ return tuple((int(s), int(e)) for s, e in zip(starts, ends))
420
+
421
+ def _visible_from_4d_mask(attention_mask, kv_seq_len):
422
+ mask = attention_mask[:, :, :, :kv_seq_len]
423
+ if mask.dtype == torch.bool:
424
+ return mask[:, 0].detach().to(device="cpu", dtype=torch.bool).contiguous()
425
+ mask_cpu = mask[:, 0].detach().to(device="cpu").contiguous()
426
+ if getattr(attention_mask, "_la_flash_visible_mask", False):
427
+ return (mask_cpu > 0).to(dtype=torch.bool)
428
+
429
+ max_value = float(mask_cpu.max().item()) if mask_cpu.numel() else 0.0
430
+ min_value = float(mask_cpu.min().item()) if mask_cpu.numel() else 0.0
431
+ if max_value > 0.0 and min_value >= 0.0:
432
+ return (mask_cpu > 0).to(dtype=torch.bool)
433
+ return (mask_cpu >= 0).to(dtype=torch.bool)
434
+
435
+ def _plan_from_visible_mask(attention_mask, bsz, q_len, kv_seq_len, device):
436
+ cache_key = (int(bsz), int(q_len), int(kv_seq_len), device.type, device.index)
437
+ cached = getattr(attention_mask, "_la_flash_magi_plan", None)
438
+ if cached is not None and cached[0] == cache_key:
439
+ return cached[1]
440
+
441
+ visible = _visible_from_4d_mask(attention_mask, int(kv_seq_len)).numpy()
442
+ q_ranges, k_ranges, types = [], [], []
443
+ for b in range(int(bsz)):
444
+ q_base = b * int(q_len)
445
+ k_base = b * int(kv_seq_len)
446
+ run_start = 0
447
+ run_segments = _row_segments(visible[b, 0])
448
+ for q in range(1, int(q_len)):
449
+ segments = _row_segments(visible[b, q])
450
+ if segments == run_segments:
451
+ continue
452
+ for start, end in run_segments:
453
+ q_ranges.append([q_base + run_start, q_base + q])
454
+ k_ranges.append([k_base + start, k_base + end])
455
+ types.append(FULL)
456
+ run_start = q
457
+ run_segments = segments
458
+ for start, end in run_segments:
459
+ q_ranges.append([q_base + run_start, q_base + int(q_len)])
460
+ k_ranges.append([k_base + start, k_base + end])
461
+ types.append(FULL)
462
+
463
+ plan = _tensor_plan(q_ranges, k_ranges, types, device)
464
+ try:
465
+ attention_mask._la_flash_magi_plan = (cache_key, plan)
466
+ except Exception:
467
+ pass
468
+ return plan
469
+
470
+ def _plan_from_magi_dict(attention_mask, bsz, q_len, kv_seq_len, device):
471
+ if int(bsz) == 1:
472
+ return attention_mask
473
+ q_ranges, k_ranges, types = [], [], []
474
+ for b in range(int(bsz)):
475
+ qs, ks, ts = _offset_plan(
476
+ attention_mask,
477
+ q_offset=b * int(q_len),
478
+ k_offset=b * int(kv_seq_len),
479
+ )
480
+ q_ranges.extend(qs)
481
+ k_ranges.extend(ks)
482
+ types.extend(ts)
483
+ return _tensor_plan(q_ranges, k_ranges, types, device)
484
+
485
+ def _magi_plan(attention_mask, bsz, q_len, kv_seq_len, device):
486
+ if isinstance(attention_mask, dict):
487
+ if attention_mask.get("_la_flash_batched", False):
488
+ return attention_mask
489
+ return _plan_from_magi_dict(attention_mask, bsz, q_len, kv_seq_len, device)
490
+ if attention_mask is None:
491
+ return _causal_plan(bsz, q_len, kv_seq_len, device)
492
+ if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
493
+ raise ValueError(
494
+ f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, "
495
+ f"but is {attention_mask.size()}"
496
+ )
497
+ return _plan_from_visible_mask(attention_mask, bsz, q_len, kv_seq_len, device)
498
+
499
+ class _BatchedMagiAttention(mod.Qwen2Attention):
500
+ """MagiAttention path with true batch inference via packed token ranges."""
501
+
502
+ def forward(
503
+ self,
504
+ hidden_states: torch.Tensor,
505
+ attention_mask=None,
506
+ position_ids=None,
507
+ past_key_value=None,
508
+ output_attentions=False,
509
+ use_cache=False,
510
+ **kwargs,
511
+ ):
512
+ if output_attentions:
513
+ raise NotImplementedError("MagiAttention does not support output_attentions=True")
514
+
515
+ bsz, q_len, _ = hidden_states.size()
516
+ query_states = self.q_proj(hidden_states)
517
+ key_states = self.k_proj(hidden_states)
518
+ value_states = self.v_proj(hidden_states)
519
+
520
+ query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
521
+ key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
522
+ value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
523
+
524
+ kv_seq_len = key_states.shape[-2]
525
+ if past_key_value is not None:
526
+ if self.layer_idx is None:
527
+ raise ValueError(
528
+ f"The cache structure has changed since version v4.36. If you are using "
529
+ f"{self.__class__.__name__} for auto-regressive decoding with k/v caching, "
530
+ "please initialize the attention class with a layer index."
531
+ )
532
+ kv_seq_len += past_key_value.get_seq_length(self.layer_idx)
533
+
534
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
535
+ query_states, key_states = mod.apply_rotary_pos_emb(
536
+ query_states, key_states, cos, sin, position_ids)
537
+
538
+ if past_key_value is not None:
539
+ cache_kwargs = {"sin": sin, "cos": cos}
540
+ key_states, value_states = past_key_value.update(
541
+ key_states, value_states, self.layer_idx, cache_kwargs)
542
+
543
+ kv_seq_len = key_states.shape[-2]
544
+ plan = _magi_plan(attention_mask, bsz, q_len, kv_seq_len, query_states.device)
545
+ magi_extra_kwargs = {}
546
+ if supports_disable_fwd_atomic:
547
+ magi_extra_kwargs["disable_fwd_atomic_reduction"] = (
548
+ (not self.training) and _plan_disjoint_q_ranges(plan)
549
+ )
550
+
551
+ query_states = query_states.transpose(1, 2).reshape(
552
+ bsz * q_len, self.num_heads, self.head_dim).contiguous()
553
+ key_states = key_states.transpose(1, 2).reshape(
554
+ bsz * kv_seq_len, self.num_key_value_heads, self.head_dim).contiguous()
555
+ value_states = value_states.transpose(1, 2).reshape(
556
+ bsz * kv_seq_len, self.num_key_value_heads, self.head_dim).contiguous()
557
+
558
+ attn_output, _ = flex_flash_attn_func(
559
+ query_states,
560
+ key_states,
561
+ value_states,
562
+ q_ranges=plan["q_ranges"],
563
+ k_ranges=plan["k_ranges"],
564
+ attn_type_map=plan["attn_type_map"],
565
+ softmax_scale=getattr(self, "softmax_scale", self.head_dim ** -0.5),
566
+ softcap=0.0,
567
+ deterministic=False,
568
+ **magi_extra_kwargs,
569
+ )
570
+ attn_output = attn_output.view(bsz, q_len, self.hidden_size)
571
+ attn_output = self.o_proj(attn_output)
572
+ return attn_output, None, past_key_value
573
+
574
+ return _BatchedMagiAttention
575
+
576
+
577
+ def build_la_flash_attention_class(mod):
578
+ """Build a Qwen2 attention subclass backed by LA Flash sparse ranges."""
579
+ try:
580
+ from kernel_utils import is_available, range_attention
581
+ except Exception as exc:
582
+ raise RuntimeError(
583
+ "LA_FLASH_ATTN=la_flash requires kernel_utils and FlashAttention."
584
+ ) from exc
585
+ if not is_available():
586
+ raise RuntimeError(
587
+ "LA_FLASH_ATTN=la_flash requires flash_attn.flash_attn_varlen_func."
588
+ )
589
+
590
+ FULL, CAUSAL = 0, 1
591
+ causal_plan_cache = {}
592
+
593
+ def _tensor_plan(q_ranges, k_ranges, types, device):
594
+ max_q_len = max((int(end) - int(start) for start, end in q_ranges), default=0)
595
+ max_k_len = max((int(end) - int(start) for start, end in k_ranges), default=0)
596
+ plan = {
597
+ "q_ranges": torch.tensor(q_ranges, dtype=torch.int32, device=device).contiguous(),
598
+ "k_ranges": torch.tensor(k_ranges, dtype=torch.int32, device=device).contiguous(),
599
+ "attn_type_map": torch.tensor(types, dtype=torch.int32, device=device).contiguous(),
600
+ "max_q_len": max_q_len,
601
+ "max_k_len": max_k_len,
602
+ }
603
+ plan.update(_la_flash_group_plan_tensors(q_ranges, types, device))
604
+ return plan
605
+
606
+ def _offset_plan(plan, q_offset, k_offset):
607
+ return (
608
+ (plan["q_ranges"] + int(q_offset)).tolist(),
609
+ (plan["k_ranges"] + int(k_offset)).tolist(),
610
+ plan["attn_type_map"].tolist(),
611
+ )
612
+
613
+ def _causal_plan(bsz, q_len, kv_seq_len, device):
614
+ key = (int(bsz), int(q_len), int(kv_seq_len), device.type, device.index)
615
+ cached = causal_plan_cache.get(key)
616
+ if cached is not None:
617
+ return cached
618
+ q_ranges, k_ranges, types = [], [], []
619
+ for b in range(int(bsz)):
620
+ q_base = b * int(q_len)
621
+ k_base = b * int(kv_seq_len)
622
+ q_ranges.append([q_base, q_base + int(q_len)])
623
+ k_ranges.append([k_base, k_base + int(kv_seq_len)])
624
+ types.append(CAUSAL)
625
+ plan = _tensor_plan(q_ranges, k_ranges, types, device)
626
+ plan.update(
627
+ {
628
+ "flash_cu_seqlens_q": torch.arange(
629
+ 0,
630
+ (int(bsz) + 1) * int(q_len),
631
+ int(q_len),
632
+ dtype=torch.int32,
633
+ device=device,
634
+ ),
635
+ "flash_cu_seqlens_k": torch.arange(
636
+ 0,
637
+ (int(bsz) + 1) * int(kv_seq_len),
638
+ int(kv_seq_len),
639
+ dtype=torch.int32,
640
+ device=device,
641
+ ),
642
+ "flash_causal": True,
643
+ }
644
+ )
645
+ causal_plan_cache[key] = plan
646
+ return plan
647
+
648
+ def _row_segments(row):
649
+ idx = np.flatnonzero(row)
650
+ if idx.size == 0:
651
+ return ((0, 1),)
652
+ split = np.flatnonzero(np.diff(idx) > 1) + 1
653
+ starts = np.concatenate((idx[:1], idx[split]))
654
+ ends = np.concatenate((idx[split - 1], idx[-1:])) + 1
655
+ return tuple((int(s), int(e)) for s, e in zip(starts, ends))
656
+
657
+ def _visible_from_4d_mask(attention_mask, kv_seq_len):
658
+ mask = attention_mask[:, :, :, :kv_seq_len]
659
+ if mask.dtype == torch.bool:
660
+ return mask[:, 0].detach().to(device="cpu", dtype=torch.bool).contiguous()
661
+ mask_cpu = mask[:, 0].detach().to(device="cpu").contiguous()
662
+ if getattr(attention_mask, "_la_flash_visible_mask", False):
663
+ return (mask_cpu > 0).to(dtype=torch.bool)
664
+
665
+ max_value = float(mask_cpu.max().item()) if mask_cpu.numel() else 0.0
666
+ min_value = float(mask_cpu.min().item()) if mask_cpu.numel() else 0.0
667
+ if max_value > 0.0 and min_value >= 0.0:
668
+ return (mask_cpu > 0).to(dtype=torch.bool)
669
+ return (mask_cpu >= 0).to(dtype=torch.bool)
670
+
671
+ def _prefix_len(row):
672
+ idx = np.flatnonzero(row)
673
+ if idx.size == 0:
674
+ return None
675
+ end = int(idx[-1]) + 1
676
+ if not bool(row[:end].all()) or bool(row[end:].any()):
677
+ return None
678
+ return end
679
+
680
+ def _causal_plan_from_visible(visible, bsz, q_len, kv_seq_len, device):
681
+ q_ranges, k_ranges, types = [], [], []
682
+ packed_flash = True
683
+ for b in range(int(bsz)):
684
+ first_len = _prefix_len(visible[b, 0])
685
+ if first_len is None:
686
+ return None
687
+ valid_len = int(first_len) + int(q_len) - 1
688
+ if valid_len < int(q_len) or valid_len > int(kv_seq_len):
689
+ return None
690
+ for q in range(int(q_len)):
691
+ row_len = _prefix_len(visible[b, q])
692
+ expected = valid_len - int(q_len) + q + 1
693
+ if row_len != expected:
694
+ return None
695
+ q_base = b * int(q_len)
696
+ k_base = b * int(kv_seq_len)
697
+ q_ranges.append([q_base, q_base + int(q_len)])
698
+ k_ranges.append([k_base, k_base + valid_len])
699
+ types.append(CAUSAL)
700
+ packed_flash = packed_flash and valid_len == int(kv_seq_len)
701
+
702
+ plan = _tensor_plan(q_ranges, k_ranges, types, device)
703
+ plan["_la_flash_disjoint_q_ranges"] = True
704
+ if packed_flash:
705
+ plan.update(
706
+ {
707
+ "flash_cu_seqlens_q": torch.arange(
708
+ 0,
709
+ (int(bsz) + 1) * int(q_len),
710
+ int(q_len),
711
+ dtype=torch.int32,
712
+ device=device,
713
+ ),
714
+ "flash_cu_seqlens_k": torch.arange(
715
+ 0,
716
+ (int(bsz) + 1) * int(kv_seq_len),
717
+ int(kv_seq_len),
718
+ dtype=torch.int32,
719
+ device=device,
720
+ ),
721
+ "flash_causal": True,
722
+ }
723
+ )
724
+ return plan
725
+
726
+ def _plan_from_visible_mask(attention_mask, bsz, q_len, kv_seq_len, device):
727
+ cache_key = (int(bsz), int(q_len), int(kv_seq_len), device.type, device.index, "la_flash")
728
+ cached = getattr(attention_mask, "_la_flash_range_plan", None)
729
+ if cached is not None and cached[0] == cache_key:
730
+ return cached[1]
731
+
732
+ visible = _visible_from_4d_mask(attention_mask, int(kv_seq_len)).numpy()
733
+ plan = _causal_plan_from_visible(visible, bsz, q_len, kv_seq_len, device)
734
+ if plan is not None:
735
+ try:
736
+ attention_mask._la_flash_range_plan = (cache_key, plan)
737
+ except Exception:
738
+ pass
739
+ return plan
740
+
741
+ q_ranges, k_ranges, types = [], [], []
742
+ for b in range(int(bsz)):
743
+ q_base = b * int(q_len)
744
+ k_base = b * int(kv_seq_len)
745
+ run_start = 0
746
+ run_segments = _row_segments(visible[b, 0])
747
+ for q in range(1, int(q_len)):
748
+ segments = _row_segments(visible[b, q])
749
+ if segments == run_segments:
750
+ continue
751
+ for start, end in run_segments:
752
+ q_ranges.append([q_base + run_start, q_base + q])
753
+ k_ranges.append([k_base + start, k_base + end])
754
+ types.append(FULL)
755
+ run_start = q
756
+ run_segments = segments
757
+ for start, end in run_segments:
758
+ q_ranges.append([q_base + run_start, q_base + int(q_len)])
759
+ k_ranges.append([k_base + start, k_base + end])
760
+ types.append(FULL)
761
+
762
+ plan = _tensor_plan(q_ranges, k_ranges, types, device)
763
+ try:
764
+ attention_mask._la_flash_range_plan = (cache_key, plan)
765
+ except Exception:
766
+ pass
767
+ return plan
768
+
769
+ def _plan_from_magi_dict(attention_mask, bsz, q_len, kv_seq_len, device):
770
+ if int(bsz) == 1:
771
+ return attention_mask
772
+ q_ranges, k_ranges, types = [], [], []
773
+ for b in range(int(bsz)):
774
+ qs, ks, ts = _offset_plan(
775
+ attention_mask,
776
+ q_offset=b * int(q_len),
777
+ k_offset=b * int(kv_seq_len),
778
+ )
779
+ q_ranges.extend(qs)
780
+ k_ranges.extend(ks)
781
+ types.extend(ts)
782
+ return _tensor_plan(q_ranges, k_ranges, types, device)
783
+
784
+ def _range_plan(attention_mask, bsz, q_len, kv_seq_len, device):
785
+ if isinstance(attention_mask, dict):
786
+ if attention_mask.get("_la_flash_batched", False):
787
+ return attention_mask
788
+ return _plan_from_magi_dict(attention_mask, bsz, q_len, kv_seq_len, device)
789
+ if attention_mask is None:
790
+ return _causal_plan(bsz, q_len, kv_seq_len, device)
791
+ if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
792
+ raise ValueError(
793
+ f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, "
794
+ f"but is {attention_mask.size()}"
795
+ )
796
+ return _plan_from_visible_mask(attention_mask, bsz, q_len, kv_seq_len, device)
797
+
798
+ class _LaFlashAttention(mod.Qwen2Attention):
799
+ """Range-plan attention path backed by FlashAttention sparse ranges."""
800
+
801
+ def forward(
802
+ self,
803
+ hidden_states: torch.Tensor,
804
+ attention_mask=None,
805
+ position_ids=None,
806
+ past_key_value=None,
807
+ output_attentions=False,
808
+ use_cache=False,
809
+ **kwargs,
810
+ ):
811
+ if output_attentions:
812
+ raise NotImplementedError("LA Flash attention does not support output_attentions=True")
813
+
814
+ bsz, q_len, _ = hidden_states.size()
815
+ query_states = self.q_proj(hidden_states)
816
+ key_states = self.k_proj(hidden_states)
817
+ value_states = self.v_proj(hidden_states)
818
+
819
+ query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
820
+ key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
821
+ value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
822
+
823
+ kv_seq_len = key_states.shape[-2]
824
+ if past_key_value is not None:
825
+ if self.layer_idx is None:
826
+ raise ValueError(
827
+ f"The cache structure has changed since version v4.36. If you are using "
828
+ f"{self.__class__.__name__} for auto-regressive decoding with k/v caching, "
829
+ "please initialize the attention class with a layer index."
830
+ )
831
+ kv_seq_len += past_key_value.get_seq_length(self.layer_idx)
832
+
833
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
834
+ query_states, key_states = mod.apply_rotary_pos_emb(
835
+ query_states, key_states, cos, sin, position_ids)
836
+
837
+ if past_key_value is not None:
838
+ cache_kwargs = {"sin": sin, "cos": cos}
839
+ key_states, value_states = past_key_value.update(
840
+ key_states, value_states, self.layer_idx, cache_kwargs)
841
+
842
+ kv_seq_len = key_states.shape[-2]
843
+ dense_backend = os.environ.get("LA_FLASH_DENSE_BACKEND", "sdpa").strip().lower()
844
+ if dense_backend == "sdpa" and not isinstance(attention_mask, dict):
845
+ dense_key_states = mod.repeat_kv(key_states, self.num_key_value_groups)
846
+ dense_value_states = mod.repeat_kv(value_states, self.num_key_value_groups)
847
+ if attention_mask is not None:
848
+ if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
849
+ raise ValueError(
850
+ f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, "
851
+ f"but is {attention_mask.size()}"
852
+ )
853
+ query_for_sdpa = query_states.contiguous()
854
+ key_for_sdpa = dense_key_states.contiguous()
855
+ value_for_sdpa = dense_value_states.contiguous()
856
+ is_causal = False
857
+ elif past_key_value is None:
858
+ query_for_sdpa = query_states
859
+ key_for_sdpa = dense_key_states
860
+ value_for_sdpa = dense_value_states
861
+ is_causal = bool(self.is_causal and q_len > 1)
862
+ else:
863
+ query_for_sdpa = key_for_sdpa = value_for_sdpa = None
864
+ is_causal = False
865
+ if query_for_sdpa is not None:
866
+ attn_output = torch.nn.functional.scaled_dot_product_attention(
867
+ query_for_sdpa,
868
+ key_for_sdpa,
869
+ value_for_sdpa,
870
+ attn_mask=attention_mask,
871
+ dropout_p=self.attention_dropout if self.training else 0.0,
872
+ is_causal=is_causal,
873
+ )
874
+ attn_output = attn_output.transpose(1, 2).contiguous()
875
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
876
+ attn_output = self.o_proj(attn_output)
877
+ return attn_output, None, past_key_value
878
+
879
+ plan = _range_plan(attention_mask, bsz, q_len, kv_seq_len, query_states.device)
880
+
881
+ query_states = query_states.transpose(1, 2).reshape(
882
+ bsz * q_len, self.num_heads, self.head_dim).contiguous()
883
+ key_states = key_states.transpose(1, 2).reshape(
884
+ bsz * kv_seq_len, self.num_key_value_heads, self.head_dim).contiguous()
885
+ value_states = value_states.transpose(1, 2).reshape(
886
+ bsz * kv_seq_len, self.num_key_value_heads, self.head_dim).contiguous()
887
+
888
+ attn_output = range_attention(
889
+ query_states,
890
+ key_states,
891
+ value_states,
892
+ plan["q_ranges"],
893
+ plan["k_ranges"],
894
+ plan["attn_type_map"],
895
+ getattr(self, "softmax_scale", self.head_dim ** -0.5),
896
+ segment_offsets=plan.get("segment_offsets"),
897
+ group_q_ranges=plan.get("group_q_ranges"),
898
+ group_attn_type_map=plan.get("group_attn_type_map"),
899
+ max_q_len=plan.get("max_q_len"),
900
+ max_k_len=plan.get("max_k_len"),
901
+ flash_cu_seqlens_q=plan.get("flash_cu_seqlens_q"),
902
+ flash_cu_seqlens_k=plan.get("flash_cu_seqlens_k"),
903
+ flash_causal=plan.get("flash_causal"),
904
+ disjoint_q_ranges=plan.get("_la_flash_disjoint_q_ranges"),
905
+ )
906
+ attn_output = attn_output.view(bsz, q_len, self.hidden_size)
907
+ attn_output = self.o_proj(attn_output)
908
+ return attn_output, None, past_key_value
909
+
910
+ return _LaFlashAttention
911
+
912
+
913
+ def _is_magi_plan(obj):
914
+ return isinstance(obj, dict) and {
915
+ "q_ranges",
916
+ "k_ranges",
917
+ "attn_type_map",
918
+ }.issubset(obj.keys())
919
+
920
+
921
+ def _la_flash_group_plan_tensors(q_ranges, types, device):
922
+ """Group consecutive Magi range entries that share the same query span.
923
+
924
+ Magi-style plans may represent one query span with multiple disjoint key
925
+ spans. LA Flash consumes those as one FlashAttention-backed softmax group.
926
+ """
927
+ if not q_ranges:
928
+ return {
929
+ "group_q_ranges": torch.empty((0, 2), dtype=torch.int32, device=device),
930
+ "segment_offsets": torch.zeros((1,), dtype=torch.int32, device=device),
931
+ "group_attn_type_map": torch.empty((0,), dtype=torch.int32, device=device),
932
+ }
933
+
934
+ grouped_q, grouped_types, offsets = [], [], [0]
935
+ last_q = None
936
+ last_type = None
937
+ for idx, (q_range, attn_type) in enumerate(zip(q_ranges, types)):
938
+ key = (int(q_range[0]), int(q_range[1]))
939
+ attn_type = int(attn_type)
940
+ if last_q is None:
941
+ grouped_q.append([key[0], key[1]])
942
+ grouped_types.append(attn_type)
943
+ last_q = key
944
+ last_type = attn_type
945
+ continue
946
+ if key == last_q and attn_type == last_type:
947
+ continue
948
+ offsets.append(idx)
949
+ grouped_q.append([key[0], key[1]])
950
+ grouped_types.append(attn_type)
951
+ last_q = key
952
+ last_type = attn_type
953
+ offsets.append(len(q_ranges))
954
+
955
+ return {
956
+ "group_q_ranges": torch.tensor(grouped_q, dtype=torch.int32, device=device).contiguous(),
957
+ "segment_offsets": torch.tensor(offsets, dtype=torch.int32, device=device).contiguous(),
958
+ "group_attn_type_map": torch.tensor(grouped_types, dtype=torch.int32, device=device).contiguous(),
959
+ "max_q_len": max((end - start for start, end in grouped_q), default=0),
960
+ }
961
+
962
+
963
+ def _record_sparse_plan_stats(model, q_ranges, k_ranges, types):
964
+ if os.environ.get("LA_FLASH_PLAN_STATS", "0") != "1":
965
+ return
966
+ stats = getattr(model, "_la_flash_sparse_plan_stats", None)
967
+ if stats is None:
968
+ stats = {
969
+ "calls": 0,
970
+ "ranges": 0,
971
+ "q_tokens": 0,
972
+ "k_tokens": 0,
973
+ "max_q_len": 0,
974
+ "max_k_len": 0,
975
+ "full_ranges": 0,
976
+ "causal_ranges": 0,
977
+ "other_ranges": 0,
978
+ }
979
+ model._la_flash_sparse_plan_stats = stats
980
+ stats["calls"] += 1
981
+ stats["ranges"] += len(q_ranges)
982
+ for (q_start, q_end), (k_start, k_end), attn_type in zip(q_ranges, k_ranges, types):
983
+ q_len = int(q_end) - int(q_start)
984
+ k_len = int(k_end) - int(k_start)
985
+ stats["q_tokens"] += q_len
986
+ stats["k_tokens"] += k_len
987
+ stats["max_q_len"] = max(stats["max_q_len"], q_len)
988
+ stats["max_k_len"] = max(stats["max_k_len"], k_len)
989
+ attn_type = int(attn_type)
990
+ if attn_type == 0:
991
+ stats["full_ranges"] += 1
992
+ elif attn_type == 1:
993
+ stats["causal_ranges"] += 1
994
+ else:
995
+ stats["other_ranges"] += 1
996
+
997
+
998
+ def build_magi_scheduler_ranges(model, attention_mask_2d, input_ids, past_len, mtp_window=False):
999
+ """Build batched Magi ranges directly from the hybrid scheduler mask.
1000
+
1001
+ The official Qwen2 SDPA dispatcher may optimize an all-valid 2D mask to
1002
+ ``None`` before decoder layers see it. That is correct for plain causal
1003
+ attention but loses LocateAnything's MTP generation-window rule. Building
1004
+ ranges here keeps Magi batch inference exact and avoids per-layer dense
1005
+ mask conversion.
1006
+ """
1007
+ requested_attn = getattr(model, "_la_flash_requested_attn", ATTN_MODE)
1008
+ if requested_attn not in {"magi", "la_flash"}:
1009
+ return None
1010
+ if attention_mask_2d is None or not hasattr(attention_mask_2d, "dim") or attention_mask_2d.dim() != 2:
1011
+ return None
1012
+
1013
+ bsz, q_len = int(input_ids.shape[0]), int(input_ids.shape[1])
1014
+ key_len = int(attention_mask_2d.shape[1])
1015
+ dev = input_ids.device
1016
+ llm = model.language_model.model
1017
+ block = int(getattr(llm, "block_size", N_FUTURE))
1018
+ causal_attn = bool(getattr(llm, "causal_attn", False))
1019
+ use_mtp_window = bool(mtp_window and q_len >= block and key_len >= block)
1020
+ q0 = max(0, q_len - block)
1021
+ k0 = max(0, key_len - block)
1022
+ blocked_k = k0 - 1
1023
+ past_len = int(past_len)
1024
+
1025
+ key_valid = attention_mask_2d.detach().to(device="cpu", dtype=torch.bool).contiguous().numpy()
1026
+ key_idx = np.arange(key_len)
1027
+ q_ranges, k_ranges, types = [], [], []
1028
+ if not use_mtp_window:
1029
+ causal_q_ranges, causal_k_ranges, causal_types = [], [], []
1030
+ causal_fast_path = True
1031
+ packed_flash = True
1032
+ for b in range(bsz):
1033
+ valid = np.flatnonzero(key_valid[b])
1034
+ if valid.size == 0:
1035
+ causal_fast_path = False
1036
+ break
1037
+ valid_len = int(valid[-1]) + 1
1038
+ if valid_len < q_len or not bool(key_valid[b, :valid_len].all()) or bool(key_valid[b, valid_len:].any()):
1039
+ causal_fast_path = False
1040
+ break
1041
+ packed_flash = packed_flash and valid_len == key_len
1042
+ q_base = b * q_len
1043
+ k_base = b * key_len
1044
+ causal_q_ranges.append([q_base, q_base + q_len])
1045
+ causal_k_ranges.append([k_base, k_base + valid_len])
1046
+ causal_types.append(1)
1047
+ if causal_fast_path:
1048
+ plan = {
1049
+ "q_ranges": torch.tensor(causal_q_ranges, dtype=torch.int32, device=dev).contiguous(),
1050
+ "k_ranges": torch.tensor(causal_k_ranges, dtype=torch.int32, device=dev).contiguous(),
1051
+ "attn_type_map": torch.tensor(causal_types, dtype=torch.int32, device=dev).contiguous(),
1052
+ "max_q_len": q_len,
1053
+ "max_k_len": max((end - start for start, end in causal_k_ranges), default=0),
1054
+ "_la_flash_batched": True,
1055
+ "_la_flash_disjoint_q_ranges": True,
1056
+ }
1057
+ if packed_flash:
1058
+ plan.update(
1059
+ {
1060
+ "flash_cu_seqlens_q": torch.arange(
1061
+ 0,
1062
+ (bsz + 1) * q_len,
1063
+ q_len,
1064
+ dtype=torch.int32,
1065
+ device=dev,
1066
+ ),
1067
+ "flash_cu_seqlens_k": torch.arange(
1068
+ 0,
1069
+ (bsz + 1) * key_len,
1070
+ key_len,
1071
+ dtype=torch.int32,
1072
+ device=dev,
1073
+ ),
1074
+ "flash_causal": True,
1075
+ }
1076
+ )
1077
+ plan.update(_la_flash_group_plan_tensors(causal_q_ranges, causal_types, dev))
1078
+ _record_sparse_plan_stats(model, causal_q_ranges, causal_k_ranges, causal_types)
1079
+ return plan
1080
+
1081
+ def row_segments(row):
1082
+ idx = np.flatnonzero(row)
1083
+ if idx.size == 0:
1084
+ return ((0, 1),)
1085
+ split = np.flatnonzero(np.diff(idx) > 1) + 1
1086
+ starts = np.concatenate((idx[:1], idx[split]))
1087
+ ends = np.concatenate((idx[split - 1], idx[-1:])) + 1
1088
+ return tuple((int(s), int(e)) for s, e in zip(starts, ends))
1089
+
1090
+ for b in range(bsz):
1091
+ q_base = b * q_len
1092
+ k_base = b * key_len
1093
+ run_start = 0
1094
+ run_segments = None
1095
+ if use_mtp_window and not causal_attn:
1096
+ prefix_q_len = q0
1097
+ prefix_k_end = past_len + prefix_q_len
1098
+ prefix_ok = (
1099
+ prefix_q_len > 0
1100
+ and prefix_k_end <= key_len
1101
+ and bool(key_valid[b, :prefix_k_end].all())
1102
+ )
1103
+ window_prefix_ok = blocked_k <= 0 or bool(key_valid[b, :blocked_k].all())
1104
+ window_ok = bool(key_valid[b, k0:key_len].all())
1105
+ if prefix_ok:
1106
+ q_ranges.append([q_base, q_base + prefix_q_len])
1107
+ k_ranges.append([k_base, k_base + prefix_k_end])
1108
+ types.append(1)
1109
+ run_start = prefix_q_len
1110
+ if run_start == prefix_q_len and prefix_q_len < q_len and window_prefix_ok and window_ok:
1111
+ if blocked_k > 0:
1112
+ q_ranges.append([q_base + prefix_q_len, q_base + q_len])
1113
+ k_ranges.append([k_base, k_base + blocked_k])
1114
+ types.append(0)
1115
+ q_ranges.append([q_base + prefix_q_len, q_base + q_len])
1116
+ k_ranges.append([k_base + k0, k_base + key_len])
1117
+ types.append(0)
1118
+ continue
1119
+
1120
+ for q in range(run_start, q_len):
1121
+ visible = key_valid[b] & (key_idx <= q + past_len)
1122
+ if use_mtp_window and q >= q0:
1123
+ if not causal_attn:
1124
+ visible = visible.copy()
1125
+ visible[k0:key_len] = key_valid[b, k0:key_len]
1126
+ if blocked_k >= 0:
1127
+ if visible.base is None:
1128
+ visible[blocked_k] = False
1129
+ else:
1130
+ visible = visible.copy()
1131
+ visible[blocked_k] = False
1132
+ segments = row_segments(visible)
1133
+ if run_segments is None:
1134
+ run_segments = segments
1135
+ continue
1136
+ if segments == run_segments:
1137
+ continue
1138
+ for start, end in run_segments:
1139
+ q_ranges.append([q_base + run_start, q_base + q])
1140
+ k_ranges.append([k_base + start, k_base + end])
1141
+ types.append(0)
1142
+ run_start = q
1143
+ run_segments = segments
1144
+ for start, end in run_segments:
1145
+ q_ranges.append([q_base + run_start, q_base + q_len])
1146
+ k_ranges.append([k_base + start, k_base + end])
1147
+ types.append(0)
1148
+
1149
+ seen_q_ranges = set()
1150
+ disjoint_q_ranges = True
1151
+ for start, end in q_ranges:
1152
+ key = (int(start), int(end))
1153
+ if key in seen_q_ranges:
1154
+ disjoint_q_ranges = False
1155
+ break
1156
+ seen_q_ranges.add(key)
1157
+
1158
+ plan = {
1159
+ "q_ranges": torch.tensor(q_ranges, dtype=torch.int32, device=dev).contiguous(),
1160
+ "k_ranges": torch.tensor(k_ranges, dtype=torch.int32, device=dev).contiguous(),
1161
+ "attn_type_map": torch.tensor(types, dtype=torch.int32, device=dev).contiguous(),
1162
+ "max_q_len": max((end - start for start, end in q_ranges), default=0),
1163
+ "max_k_len": max((end - start for start, end in k_ranges), default=0),
1164
+ "_la_flash_batched": True,
1165
+ "_la_flash_disjoint_q_ranges": disjoint_q_ranges,
1166
+ }
1167
+ plan.update(_la_flash_group_plan_tensors(q_ranges, types, dev))
1168
+ _record_sparse_plan_stats(model, q_ranges, k_ranges, types)
1169
+ return plan
1170
+
1171
+
1172
+ def _direct_base_forward(
1173
+ base,
1174
+ input_ids=None,
1175
+ visual_features=None,
1176
+ image_token_index=None,
1177
+ attention_mask=None,
1178
+ position_ids=None,
1179
+ past_key_values=None,
1180
+ inputs_embeds=None,
1181
+ use_cache=None,
1182
+ output_attentions=None,
1183
+ output_hidden_states=None,
1184
+ return_dict=None,
1185
+ ):
1186
+ mod = importlib.import_module(type(base).__module__)
1187
+ output_attentions = output_attentions if output_attentions is not None else base.config.output_attentions
1188
+ output_hidden_states = (
1189
+ output_hidden_states if output_hidden_states is not None else base.config.output_hidden_states
1190
+ )
1191
+ use_cache = use_cache if use_cache is not None else base.config.use_cache
1192
+
1193
+ if input_ids is not None and inputs_embeds is not None:
1194
+ raise ValueError("You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time")
1195
+ if input_ids is not None:
1196
+ batch_size, seq_length = input_ids.shape
1197
+ elif inputs_embeds is not None:
1198
+ batch_size, seq_length, _ = inputs_embeds.shape
1199
+ else:
1200
+ raise ValueError("You have to specify either decoder_input_ids or decoder_inputs_embeds")
1201
+
1202
+ past_key_values_length = 0
1203
+ use_legacy_cache = False
1204
+ if use_cache:
1205
+ Cache = getattr(mod, "Cache")
1206
+ DynamicCache = getattr(mod, "DynamicCache")
1207
+ use_legacy_cache = not isinstance(past_key_values, Cache)
1208
+ if use_legacy_cache:
1209
+ if past_key_values is None:
1210
+ past_key_values = DynamicCache()
1211
+ else:
1212
+ past_key_values = DynamicCache.from_legacy_cache(past_key_values)
1213
+ past_key_values_length = past_key_values.get_seq_length()
1214
+
1215
+ if position_ids is None:
1216
+ dev = input_ids.device if input_ids is not None else inputs_embeds.device
1217
+ position_ids = torch.arange(
1218
+ past_key_values_length,
1219
+ seq_length + past_key_values_length,
1220
+ dtype=torch.long,
1221
+ device=dev,
1222
+ ).unsqueeze(0).view(-1, seq_length)
1223
+ else:
1224
+ position_ids = position_ids.view(-1, seq_length).long()
1225
+
1226
+ if inputs_embeds is None:
1227
+ inputs_embeds = base.image_processing(input_ids, visual_features, image_token_index)
1228
+
1229
+ hidden_states = inputs_embeds
1230
+ all_hidden_states = () if output_hidden_states else None
1231
+ all_self_attns = () if output_attentions else None
1232
+ next_decoder_cache = None
1233
+
1234
+ for decoder_layer in base.layers:
1235
+ if output_hidden_states:
1236
+ all_hidden_states += (hidden_states,)
1237
+ layer_outputs = decoder_layer(
1238
+ hidden_states,
1239
+ attention_mask=attention_mask,
1240
+ position_ids=position_ids,
1241
+ past_key_value=past_key_values,
1242
+ output_attentions=output_attentions,
1243
+ use_cache=use_cache,
1244
+ )
1245
+ hidden_states = layer_outputs[0]
1246
+ if use_cache:
1247
+ next_decoder_cache = layer_outputs[2 if output_attentions else 1]
1248
+ if output_attentions:
1249
+ all_self_attns += (layer_outputs[1],)
1250
+
1251
+ hidden_states = base.norm(hidden_states)
1252
+ if output_hidden_states:
1253
+ all_hidden_states += (hidden_states,)
1254
+ next_cache = None
1255
+ if use_cache:
1256
+ next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache
1257
+ return SimpleNamespace(
1258
+ last_hidden_state=hidden_states,
1259
+ past_key_values=next_cache,
1260
+ hidden_states=all_hidden_states,
1261
+ attentions=all_self_attns,
1262
+ )
1263
+
1264
+
1265
+ def language_model_forward(model, **kwargs):
1266
+ """Forward through the text LM, bypassing official dense-mask prep for sparse plans."""
1267
+ lm = model.language_model
1268
+ return_logits = kwargs.pop("return_logits", True)
1269
+ logits_slice = kwargs.pop("logits_slice", None)
1270
+ attention_mask = kwargs.get("attention_mask")
1271
+ use_direct_sparse = (
1272
+ getattr(model, "_la_flash_requested_attn", ATTN_MODE) in {"magi", "la_flash"}
1273
+ and _is_magi_plan(attention_mask)
1274
+ )
1275
+ if not use_direct_sparse:
1276
+ return lm(**kwargs)
1277
+
1278
+ labels = kwargs.pop("labels", None)
1279
+ if labels is not None:
1280
+ raise NotImplementedError("labels are not supported in the direct sparse-plan decode forward")
1281
+ output_attentions = kwargs.get("output_attentions", None)
1282
+ output_hidden_states = kwargs.get("output_hidden_states", None)
1283
+ base_out = _direct_base_forward(lm.model, **kwargs)
1284
+ logits = None
1285
+ if return_logits:
1286
+ hidden_states = base_out.last_hidden_state
1287
+ if logits_slice is not None:
1288
+ hidden_states = hidden_states[:, logits_slice, :]
1289
+ logits = lm.lm_head(hidden_states).float()
1290
+ return SimpleNamespace(
1291
+ logits=logits,
1292
+ past_key_values=base_out.past_key_values,
1293
+ hidden_states=base_out.hidden_states if output_hidden_states else None,
1294
+ attentions=base_out.attentions if output_attentions else None,
1295
+ )
1296
+
1297
+
1298
+ _EagerCls = _SdpaCls = _LaFlashCls = _MagiCls = None
1299
+ def _attn_classes(mode=None):
1300
+ """Attention classes from the dynamic Qwen2 remote module.
1301
+
1302
+ The official Qwen2Model mask dispatcher only implements ``sdpa`` and
1303
+ single-row ``magi``. Eager, LA Flash, and batched Magi inference
1304
+ therefore swap the layer class while keeping the model's mask dispatcher
1305
+ pinned to ``sdpa``.
1306
+ """
1307
+ global _EagerCls, _SdpaCls, _LaFlashCls, _MagiCls
1308
+ mode = _normalize_attn_mode(mode) if mode is not None else None
1309
+ if _SdpaCls is None:
1310
+ mod = importlib.import_module(type(_model.language_model.model).__module__)
1311
+ _EagerCls = mod.Qwen2Attention
1312
+ _SdpaCls = mod.Qwen2SdpaAttention
1313
+ else:
1314
+ mod = importlib.import_module(type(_model.language_model.model).__module__)
1315
+ if (mode is None or mode == "la_flash") and _LaFlashCls is None:
1316
+ _LaFlashCls = build_la_flash_attention_class(mod)
1317
+ if (mode is None or mode == "magi") and _MagiCls is None:
1318
+ _MagiCls = build_batched_magi_attention_class(mod) if getattr(mod, "_MAGI_AVAILABLE", False) else None
1319
+ return _EagerCls, _SdpaCls, _LaFlashCls, _MagiCls
1320
+
1321
+ def _set_llm_mode(model, mode):
1322
+ """Swap every Qwen2 decoder layer's attention class.
1323
+
1324
+ Release backends keep ``Qwen2Model._attn_implementation='sdpa'`` so the
1325
+ official Qwen2 mask dispatcher stays available for dense-mask modes. The
1326
+ local ``la_flash`` and batched ``magi`` wrappers can also consume scheduler-built
1327
+ sparse plans directly, avoiding repeated per-layer dense mask conversion.
1328
+ """
1329
+ mode = _normalize_attn_mode(mode)
1330
+ eager, sdpa, la_flash, magi = _attn_classes(mode)
1331
+ impl = "sdpa"
1332
+ if mode == "sdpa":
1333
+ cls = sdpa
1334
+ elif mode == "eager":
1335
+ cls = eager
1336
+ elif mode == "la_flash":
1337
+ cls = la_flash
1338
+ elif mode == "magi":
1339
+ if magi is None:
1340
+ raise RuntimeError("MagiAttention is unavailable in the current Python environment.")
1341
+ cls = magi
1342
+ else:
1343
+ raise ValueError(f"unknown LLM attention mode: {mode}")
1344
+ llm = model.language_model.model
1345
+ for lyr in llm.layers:
1346
+ lyr.self_attn.__class__ = cls
1347
+ if mode == "magi":
1348
+ lyr.self_attn.softmax_scale = lyr.self_attn.head_dim ** -0.5
1349
+ llm._attn_implementation = impl
1350
+ llm.config._attn_implementation = llm._attn_implementation
1351
+ if hasattr(model.config, "text_config"):
1352
+ model.config.text_config._attn_implementation = llm._attn_implementation
1353
+ model.config._attn_implementation = llm._attn_implementation
1354
+ model._la_flash_requested_attn = mode
1355
+
1356
+ _st = _hp = None
1357
+ def _helpers():
1358
+ """The model's own sample_tokens / handle_pattern (the exact box decoders)."""
1359
+ global _st, _hp
1360
+ if _st is None:
1361
+ m = importlib.import_module(type(load()[2]).__module__)
1362
+ _st, _hp = m.sample_tokens, m.handle_pattern
1363
+ return _st, _hp
1364
+
1365
+
1366
+ _gu = None
1367
+ def _gen_utils():
1368
+ """The model's generate_utils module (apply_repetition_penalty / top_p_logits / top_k_logits /
1369
+ decode_bbox_avg / decode_ref / dists) -- the pieces sample_tokens_batched reuses verbatim."""
1370
+ global _gu
1371
+ if _gu is None:
1372
+ m = importlib.import_module(type(load()[2]).__module__)
1373
+ _gu = importlib.import_module(m.sample_tokens.__module__)
1374
+ return _gu
1375
+
1376
+
1377
+ def _env_float(name, default):
1378
+ val = os.environ.get(name)
1379
+ if val is None or val.strip() == "":
1380
+ return float(default)
1381
+ return float(val)
1382
+
1383
+
1384
+ def _coord_fallback_mode():
1385
+ mode = os.environ.get("LA_FLASH_COORD_FALLBACK_MODE", "legacy").strip().lower().replace("-", "_")
1386
+ aliases = {
1387
+ "": "legacy",
1388
+ "official": "legacy",
1389
+ "range": "legacy",
1390
+ "spread": "legacy",
1391
+ "none": "off",
1392
+ "disable": "off",
1393
+ "disabled": "off",
1394
+ "entropy_variance": "uncertainty",
1395
+ "entropy_var": "uncertainty",
1396
+ "ent_var": "uncertainty",
1397
+ "entropy_std": "uncertainty",
1398
+ }
1399
+ mode = aliases.get(mode, mode)
1400
+ if mode not in {"legacy", "uncertainty", "off"}:
1401
+ raise ValueError(
1402
+ "LA_FLASH_COORD_FALLBACK_MODE must be one of legacy, uncertainty, off"
1403
+ )
1404
+ return mode
1405
+
1406
+
1407
+ def _coord_uncertainty_threshold(coord_start_token_id, coord_end_token_id):
1408
+ """Return the coord uncertainty threshold in raw coord-token units.
1409
+
1410
+ Backward-compatible behavior:
1411
+ - LA_FLASH_COORD_UNCERTAINTY_THRESH > 1 is treated as raw coord-token RMSE.
1412
+ - LA_FLASH_COORD_UNCERTAINTY_THRESH <= 1 is treated as normalized by coord span.
1413
+ - LA_FLASH_COORD_UNCERTAINTY_NORM_THRESH is an explicit normalized override.
1414
+ """
1415
+ coord_span = max(float(coord_end_token_id - coord_start_token_id + 1), 1.0)
1416
+ norm_val = os.environ.get("LA_FLASH_COORD_UNCERTAINTY_NORM_THRESH")
1417
+ if norm_val is not None and norm_val.strip() != "":
1418
+ return float(norm_val) * coord_span
1419
+
1420
+ val = os.environ.get("LA_FLASH_COORD_UNCERTAINTY_THRESH")
1421
+ if val is None or val.strip() == "":
1422
+ return 20.0
1423
+ threshold = float(val)
1424
+ if 0.0 < threshold <= 1.0:
1425
+ return threshold * coord_span
1426
+ return threshold
1427
+
1428
+
1429
+ def _decode_bbox_with_uncertainty(logits, probs, token_ids, keep_k=4, generation_mode="hybrid"):
1430
+ """Decode an MTP box with configurable coord uncertainty fallback.
1431
+
1432
+ The default mode is the official LocateAnything rule. ``uncertainty`` keeps
1433
+ the same frame checks and top-k coord selection, but uses one scalar
1434
+ criterion per coordinate: the posterior RMSE of committing to the current
1435
+ MAP coordinate among valid coord candidates. This is the Bayes risk under
1436
+ squared coordinate error, so probabilities and token distances are folded
1437
+ into one threshold in coordinate-token units.
1438
+ """
1439
+ gu = _gen_utils()
1440
+ mode = _coord_fallback_mode()
1441
+ if mode == "legacy" or generation_mode != "hybrid":
1442
+ return gu.decode_bbox_avg(logits, probs, token_ids, keep_k=keep_k, generation_mode=generation_mode)
1443
+
1444
+ coord_start_token_id = token_ids["coord_start_token_id"]
1445
+ coord_end_token_id = token_ids["coord_end_token_id"]
1446
+ box_start_token_id = token_ids["box_start_token_id"]
1447
+ box_end_token_id = token_ids["box_end_token_id"]
1448
+ none_token_id = token_ids["none_token_id"]
1449
+ null_token_id = token_ids["null_token_id"]
1450
+ device = logits.device
1451
+
1452
+ box_type = gu.is_valid_box_frame(
1453
+ probs,
1454
+ token_ids,
1455
+ start_thresh=_env_float("LA_FLASH_COORD_BOX_START_THRESH", 0.7),
1456
+ end_thresh=_env_float("LA_FLASH_COORD_BOX_END_THRESH", 0.2),
1457
+ topk=keep_k,
1458
+ )
1459
+ if box_type == "empty_box":
1460
+ return torch.tensor([
1461
+ box_start_token_id,
1462
+ none_token_id,
1463
+ box_end_token_id,
1464
+ null_token_id,
1465
+ null_token_id,
1466
+ null_token_id,
1467
+ ], dtype=torch.long, device=device)
1468
+ if box_type == "illegal_box":
1469
+ return None
1470
+
1471
+ pos_probs, pos_ids = torch.topk(probs[1:5], k=keep_k, dim=-1)
1472
+ valid = (pos_ids >= coord_start_token_id) & (pos_ids <= coord_end_token_id)
1473
+ has_valid = valid.any(dim=-1)
1474
+ if not has_valid.all():
1475
+ return None
1476
+
1477
+ first_valid_idx = valid.long().argmax(dim=-1, keepdim=True)
1478
+ first_valid_ids = pos_ids.gather(-1, first_valid_idx).squeeze(-1)
1479
+ if mode == "off":
1480
+ final_coords = first_valid_ids
1481
+ else:
1482
+ valid_counts = valid.sum(dim=-1)
1483
+ valid_probs = torch.where(valid, pos_probs, torch.zeros_like(pos_probs))
1484
+ valid_mass = valid_probs.sum(dim=-1).clamp_min(1e-12)
1485
+ weights = valid_probs / valid_mass.unsqueeze(-1)
1486
+ coord_values = (pos_ids - coord_start_token_id).to(dtype=torch.float32)
1487
+ map_coord = (first_valid_ids - coord_start_token_id).to(dtype=torch.float32)
1488
+ uncertainty = (weights * (coord_values - map_coord.unsqueeze(-1)).pow(2)).sum(dim=-1).sqrt()
1489
+ is_abnormal = (
1490
+ (valid_counts > 1)
1491
+ & (uncertainty > _coord_uncertainty_threshold(coord_start_token_id, coord_end_token_id))
1492
+ )
1493
+ final_coords = torch.where(is_abnormal, torch.tensor(0, device=device), first_valid_ids)
1494
+
1495
+ start_t = torch.tensor([box_start_token_id], dtype=final_coords.dtype, device=device)
1496
+ end_t = torch.tensor([box_end_token_id], dtype=final_coords.dtype, device=device)
1497
+ return torch.cat([start_t, final_coords, end_t])
1498
+
1499
+
1500
+ def _apply_repetition_penalty_lowmem(logits, generated, repetition_penalty):
1501
+ """Apply the stock repetition penalty without allocating a [B, S, V] mask."""
1502
+ if repetition_penalty == 1.0:
1503
+ return logits
1504
+ _, _, vocab_size = logits.shape
1505
+ for row in range(logits.shape[0]):
1506
+ valid_tokens = generated[row].unique()
1507
+ valid_tokens = valid_tokens[(valid_tokens >= 0) & (valid_tokens < vocab_size)]
1508
+ if valid_tokens.numel() == 0:
1509
+ continue
1510
+ row_logits = logits[row, :, valid_tokens]
1511
+ logits[row, :, valid_tokens] = torch.where(
1512
+ row_logits > 0,
1513
+ row_logits / repetition_penalty,
1514
+ row_logits * repetition_penalty,
1515
+ )
1516
+ return logits
1517
+
1518
+
1519
+ def _finite_logit_bounds(dtype):
1520
+ finfo = torch.finfo(dtype)
1521
+ return finfo.min, finfo.max
1522
+
1523
+
1524
+ def _finite_logits(logits):
1525
+ if not logits.dtype.is_floating_point:
1526
+ logits = logits.float()
1527
+ min_val, max_val = _finite_logit_bounds(logits.dtype)
1528
+ return torch.nan_to_num(logits, nan=min_val, posinf=max_val, neginf=min_val)
1529
+
1530
+
1531
+ def _finite_logits_(logits):
1532
+ if not logits.dtype.is_floating_point:
1533
+ return logits.float()
1534
+ min_val, max_val = _finite_logit_bounds(logits.dtype)
1535
+ return logits.nan_to_num_(nan=min_val, posinf=max_val, neginf=min_val)
1536
+
1537
+
1538
+ def _top_p_logits_slice_(logits, top_p):
1539
+ sorted_logits, sorted_indices = torch.sort(logits, descending=True)
1540
+ cumulative_probs = torch.cumsum(torch.softmax(sorted_logits, dim=-1), dim=-1)
1541
+ sorted_indices_to_remove = cumulative_probs > top_p
1542
+ sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
1543
+ sorted_indices_to_remove[..., 0] = False
1544
+
1545
+ remove = torch.zeros_like(logits, dtype=torch.bool, device=logits.device)
1546
+ remove.scatter_(-1, sorted_indices, sorted_indices_to_remove)
1547
+ logits.masked_fill_(remove, torch.finfo(logits.dtype).min)
1548
+ return logits
1549
+
1550
+
1551
+ def _top_p_logits_(logits, top_p):
1552
+ """In-place nucleus filtering with bounded sort workspace.
1553
+
1554
+ The MTP sampler uses logits shaped ``[B, 6, V]``. Top-p is independent for
1555
+ each row and each future position, so filtering one position at a time keeps
1556
+ the expensive sorted-index workspace at ``[B, V]`` instead of ``[B, 6, V]``.
1557
+ """
1558
+ if logits.dim() == 3 and logits.shape[1] > 1:
1559
+ for pos in range(logits.shape[1]):
1560
+ _top_p_logits_slice_(logits[:, pos, :], top_p)
1561
+ return logits
1562
+ return _top_p_logits_slice_(logits, top_p)
1563
+
1564
+
1565
+ def _top_k_logits_(logits, top_k):
1566
+ """In-place top-k filtering mirroring generate_utils.top_k_logits."""
1567
+ top_k = min(int(top_k), logits.size(-1))
1568
+ threshold = torch.topk(logits, top_k)[0][..., -1, None]
1569
+ logits.masked_fill_(logits < threshold, torch.finfo(logits.dtype).min)
1570
+ return logits
1571
+
1572
+
1573
+ def _safe_probs(filtered_logits):
1574
+ """Softmax with CUDA-multinomial-safe cleanup and row-wise argmax fallback."""
1575
+ filtered_logits = _finite_logits(filtered_logits)
1576
+ probs = torch.softmax(filtered_logits, dim=-1, dtype=torch.float32)
1577
+ probs = torch.nan_to_num(probs, nan=0.0, posinf=0.0, neginf=0.0).clamp_min_(0.0)
1578
+ row_sum = probs.sum(dim=-1, keepdim=True)
1579
+ bad = (~torch.isfinite(row_sum)) | (row_sum <= 0)
1580
+ if bool(bad.any().item()):
1581
+ fallback = torch.zeros_like(probs)
1582
+ fallback.scatter_(-1, filtered_logits.argmax(dim=-1, keepdim=True), 1.0)
1583
+ probs = torch.where(bad, fallback, probs)
1584
+ row_sum = probs.sum(dim=-1, keepdim=True)
1585
+ return probs / row_sum.clamp_min(1.0e-20)
1586
+
1587
+
1588
+ def _sample_top_p_sorted_tokens(logits, top_p):
1589
+ """Sample from top-p filtered logits without scattering back to vocab order."""
1590
+ sorted_logits, sorted_indices = torch.sort(logits, descending=True, dim=-1)
1591
+ cumulative_probs = torch.cumsum(torch.softmax(sorted_logits, dim=-1), dim=-1)
1592
+ remove = cumulative_probs > top_p
1593
+ remove[..., 1:] = remove[..., :-1].clone()
1594
+ remove[..., 0] = False
1595
+ sorted_logits.masked_fill_(remove, torch.finfo(sorted_logits.dtype).min)
1596
+ sorted_probs = _safe_probs(sorted_logits)
1597
+ sample_idx = sorted_probs.argmax(dim=-1)
1598
+ try:
1599
+ sample_idx = torch.distributions.Categorical(probs=sorted_probs).sample()
1600
+ except Exception:
1601
+ pass
1602
+ return sorted_indices.gather(-1, sample_idx.unsqueeze(-1)).squeeze(-1)
1603
+
1604
+
1605
+ @torch.no_grad()
1606
+ def sample_tokens_batched(logits, generated, token_ids, per_row_temp,
1607
+ repetition_penalty=1.0, top_p=None, top_k=None,
1608
+ keep_k_avg=4, generation_mode='fast'):
1609
+ """Batched fork of generate_utils.sample_tokens for the MTP window [B,6,V]. The logits pipeline
1610
+ (rep-penalty / per-row temperature / top_p / top_k / softmax / sample) is ROW-INDEPENDENT, so run
1611
+ it ONCE over the whole batch instead of B times on [1,6,V] (the per-row san defeats batching by
1612
+ slicing wlogits[b:b+1]). Only the variable-length box ASSEMBLY (decode_bbox_avg -> ragged shapes,
1613
+ where sample_tokens' final torch.stack throws) stays per-row, returned as a LIST.
1614
+
1615
+ Equivalence to per-row san: every pipeline op reduces on dim=-1 only (never crosses the row dim),
1616
+ so row b's processed logits/probs are bit-identical to slicing first -> greedy (per_row_temp==0,
1617
+ argmax branch, no RNG) is BIT-EXACT. Under sampling, one batched Categorical changes the global
1618
+ RNG consumption order vs B per-row draws -> box-size jitter (blessed; greedy is the exact gate).
1619
+ apply_repetition_penalty already loops per-row internally, so passing the full [B,M] `generated`
1620
+ is row-correct. keep_k_avg/generation_mode mirror sample_tokens' decode_bbox_avg call EXACTLY
1621
+ (note: the per-row san passes keep_k=5 but decode_bbox_avg reads keep_k_avg, default 4 -- so 5 is
1622
+ a no-op there; we replicate keep_k_avg=4). Returns (x0[B,6], boxes: list of B 1-D LongTensors)."""
1623
+ gu = _gen_utils()
1624
+ B, S, V = logits.shape # S = N_FUTURE = 6
1625
+ if repetition_penalty != 1.0:
1626
+ logits = _apply_repetition_penalty_lowmem(logits, generated, repetition_penalty)
1627
+ t = per_row_temp.to(dtype=logits.dtype).view(B, 1, 1)
1628
+ sample_rows = per_row_temp > 0
1629
+ if bool(sample_rows.all().item()):
1630
+ logits.div_(t.clamp(min=1e-8))
1631
+ elif bool(sample_rows.any().item()):
1632
+ idx = sample_rows.nonzero(as_tuple=True)[0]
1633
+ logits[idx].div_(t[idx].clamp(min=1e-8))
1634
+ logits = _finite_logits_(logits)
1635
+ if top_p is not None and top_p < 1:
1636
+ logits = _top_p_logits_(logits, top_p)
1637
+ if top_k is not None and top_k > 0:
1638
+ logits = _top_k_logits_(logits, top_k)
1639
+ probs = _safe_probs(logits)
1640
+ x0 = probs.argmax(dim=-1) # [B,6]; greedy rows are final here
1641
+ samp = per_row_temp > 0
1642
+ if bool(samp.any()): # sampling rows: ONE batched Categorical draw
1643
+ idx = samp.nonzero(as_tuple=True)[0]
1644
+ try:
1645
+ x0[idx] = gu.dists.Categorical(probs=probs[idx]).sample()
1646
+ except Exception:
1647
+ pass # keep argmax (matches san's except: probs.max)
1648
+ boxes = []
1649
+ fallback = torch.zeros(1, dtype=x0.dtype, device=x0.device)
1650
+ for b in range(B): # variable-length box assembly (per-row, exact)
1651
+ db = _decode_bbox_with_uncertainty(
1652
+ logits[b], probs[b], token_ids,
1653
+ keep_k=keep_k_avg, generation_mode=generation_mode)
1654
+ if db is not None:
1655
+ boxes.append(db)
1656
+ else:
1657
+ ref = gu.decode_ref(logits[b], probs[b], token_ids)
1658
+ if ref is None:
1659
+ boxes.append(fallback)
1660
+ elif torch.is_tensor(ref):
1661
+ boxes.append(ref.to(dtype=x0.dtype, device=x0.device))
1662
+ else:
1663
+ boxes.append(torch.tensor(ref, dtype=x0.dtype, device=x0.device))
1664
+ return x0, boxes
1665
+
1666
+
1667
+ @torch.no_grad()
1668
+ def sample_next_tokens_batched(logits, generated, per_row_temp,
1669
+ repetition_penalty=1.0, top_p=None, top_k=None):
1670
+ """Batched one-token sampler for AR repair rows.
1671
+
1672
+ This mirrors the row-independent part of ``sample_tokens`` for logits shaped
1673
+ ``[B,1,V]``. It intentionally does not run bbox/ref assembly because AR mode
1674
+ only needs the next token before the state machine classifies it.
1675
+ """
1676
+ gu = _gen_utils()
1677
+ if logits.dim() != 3 or logits.shape[1] != 1:
1678
+ raise ValueError(f"AR batched sampler expects logits [B,1,V], got {tuple(logits.shape)}")
1679
+ B = int(logits.shape[0])
1680
+ if repetition_penalty != 1.0:
1681
+ logits = _apply_repetition_penalty_lowmem(logits, generated, repetition_penalty)
1682
+ t = per_row_temp.to(dtype=logits.dtype).view(B, 1, 1)
1683
+ sample_rows = per_row_temp > 0
1684
+ if bool(sample_rows.all().item()):
1685
+ logits.div_(t.clamp(min=1e-8))
1686
+ elif bool(sample_rows.any().item()):
1687
+ idx = sample_rows.nonzero(as_tuple=True)[0]
1688
+ logits[idx].div_(t[idx].clamp(min=1e-8))
1689
+ logits = _finite_logits_(logits)
1690
+ sorted_top_p = os.environ.get("AR_SORTED_TOPP", "0") == "1"
1691
+ default_top_p = sorted_top_p and top_p is not None and top_p < 1 and (top_k is None or top_k <= 0)
1692
+ if default_top_p and bool(sample_rows.all().item()):
1693
+ return _sample_top_p_sorted_tokens(logits, top_p)
1694
+ if top_p is not None and top_p < 1:
1695
+ logits = _top_p_logits_(logits, top_p)
1696
+ if top_k is not None and top_k > 0:
1697
+ logits = _top_k_logits_(logits, top_k)
1698
+ probs = _safe_probs(logits)
1699
+ x0 = probs.argmax(dim=-1)
1700
+ if bool(sample_rows.any().item()):
1701
+ # Keep row-ordered sampling as the release default. A single batched
1702
+ # Categorical is faster, but it consumes RNG differently from stock AR
1703
+ # repair and can alter default-temperature termination behavior.
1704
+ for row in sample_rows.nonzero(as_tuple=True)[0].tolist():
1705
+ try:
1706
+ x0[row : row + 1] = gu.dists.Categorical(probs=probs[row : row + 1]).sample()
1707
+ except Exception:
1708
+ pass
1709
+ return x0
1710
+
1711
+
1712
+ def load_pil(p):
1713
+ from PIL import Image
1714
+ im = Image.open(p).convert("RGB"); w, h = im.size
1715
+ if max(w, h) > MAX_DIM:
1716
+ s = MAX_DIM / max(w, h); im = im.resize((max(1, round(w*s)), max(1, round(h*s))), Image.LANCZOS)
1717
+ return im
1718
+
1719
+ def _preproc_one(im):
1720
+ """CPU-side processor for one image -> (pixel_values[bf16], grid[int32]). Split out of
1721
+ _encode_image so _encode_images can batch the GPU encode while preprocessing stays per-image."""
1722
+ tok, proc, model = load()
1723
+ msg = [{"role": "user", "content": [{"type": "image", "image": im}, {"type": "text", "text": "x"}]}]
1724
+ text = proc.py_apply_chat_template(msg, tokenize=False, add_generation_prompt=True)
1725
+ imgs, vids = proc.process_vision_info(msg)
1726
+ inp = proc(text=[text], images=imgs, videos=vids, return_tensors="pt").to(DEV)
1727
+ grid = inp.get("image_grid_hws")
1728
+ if isinstance(grid, np.ndarray): grid = torch.from_numpy(grid).to(DEV, dtype=torch.int32)
1729
+ return inp["pixel_values"].to(DT), grid
1730
+
1731
+
1732
+ def _vision_is_flash():
1733
+ """True iff MoonViT will actually run flash_attn_varlen (so cross-image packing is
1734
+ block-diagonal = exact AND a win). If the vision blocks are on sdpa/eager, OR the flash
1735
+ wheel is absent (multihead_attention falls back to the dense-mask sdpa path), packing is
1736
+ O(S^2) N^2 -> caller must stay per-image."""
1737
+ vm = load()[2].vision_model
1738
+ mod = importlib.import_module(type(vm).__module__)
1739
+ if getattr(mod, "flash_attn_varlen_func", None) is None:
1740
+ return False
1741
+ try:
1742
+ return vm.encoder.blocks[0].attn_implementation == "flash_attention_2"
1743
+ except Exception:
1744
+ return False
1745
+
1746
+
1747
+ @torch.no_grad()
1748
+ def _encode_images(ims):
1749
+ """N images -> list of [n_img_tokens, C] mlp1-projected visual_features, one per image
1750
+ (row-order). Drop-in for [_encode_image(im) for im in ims].
1751
+
1752
+ With flash present (_vision_is_flash) and N>1, packs images into
1753
+ extract_feature micro-batches: MoonViT's varlen cu_seqlens path is
1754
+ block-diagonal by image. Without flash, the dense SDPA fallback would scale
1755
+ with the packed total sequence length, so this function falls back to
1756
+ per-image encode. MTP_BATCH_VISION=0 also forces per-image encode."""
1757
+ tok, proc, model = load()
1758
+ pvs, grids = [], []
1759
+ for im in ims:
1760
+ pv, g = _preproc_one(im)
1761
+ pvs.append(pv); grids.append(g)
1762
+ if BATCH_VISION and len(ims) > 1 and _vision_is_flash():
1763
+ if VISION_ENCODE_BATCH_SIZE <= 0 or VISION_ENCODE_BATCH_SIZE >= len(ims):
1764
+ vit_list = model.extract_feature(torch.cat(pvs, dim=0), torch.cat(grids, dim=0))
1765
+ else:
1766
+ vit_list = []
1767
+ for start in range(0, len(ims), VISION_ENCODE_BATCH_SIZE):
1768
+ end = min(start + VISION_ENCODE_BATCH_SIZE, len(ims))
1769
+ vit_list.extend(
1770
+ model.extract_feature(
1771
+ torch.cat(pvs[start:end], dim=0),
1772
+ torch.cat(grids[start:end], dim=0),
1773
+ )
1774
+ )
1775
+ return [model.mlp1(v) for v in vit_list] # one [P_i, C] per image (patch_merger split)
1776
+ return [model.mlp1(torch.cat(model.extract_feature(pv, g), dim=0))
1777
+ for pv, g in zip(pvs, grids)] # per-image (flash absent / N==1 / forced off)
1778
+
1779
+
1780
+ @torch.no_grad()
1781
+ def _encode_image(im):
1782
+ """Single-image convenience wrapper (single-image callers); = _encode_images([im])[0]
1783
+ (takes the per-image path inside _encode_images, so bit-identical to the original)."""
1784
+ return _encode_images([im])[0]
1785
+
1786
+ @torch.no_grad()
1787
+ def _tokenize(im, query):
1788
+ """1-D prompt token ids for (image, query). Uses the model's own chat template."""
1789
+ tok, proc, model = load()
1790
+ msg = [{"role": "user", "content": [{"type": "image", "image": im},
1791
+ {"type": "text", "text": _PROMPT + query + "."}]}]
1792
+ text = proc.py_apply_chat_template(msg, tokenize=False, add_generation_prompt=True)
1793
+ imgs, vids = proc.process_vision_info(msg)
1794
+ return proc(text=[text], images=imgs, videos=vids, return_tensors="pt").to(DEV)["input_ids"][0]
1795
+
1796
+
1797
+ @torch.no_grad()
1798
+ def _tokenize_cached_image(query, image_token_count, im=None):
1799
+ """Tokenize a prompt when the image token count is already known.
1800
+
1801
+ This keeps the processor's chat template, but directly expands ``<image-1>``
1802
+ from the cached visual feature length. It avoids re-running the CPU image
1803
+ processor for every category prompt that shares the same image.
1804
+ """
1805
+ tok, proc, model = load()
1806
+ msg = [{"role": "user", "content": [{"type": "image", "image": im},
1807
+ {"type": "text", "text": _PROMPT + query + "."}]}]
1808
+ text = proc.py_apply_chat_template(msg, tokenize=False, add_generation_prompt=True)
1809
+ placeholder = f"<{getattr(proc, 'image_placeholder', 'image')}-1>"
1810
+ image_token = getattr(proc, "image_token", "<IMG_CONTEXT>")
1811
+ image_start = getattr(proc, "image_start_token", "<img>")
1812
+ image_end = getattr(proc, "image_end_token", "</img>")
1813
+ replacement = f"<image 1>{image_start}{image_token * int(image_token_count)}{image_end}"
1814
+ if placeholder not in text:
1815
+ raise ValueError(f"cached image placeholder {placeholder!r} was not found in chat template")
1816
+ text = text.replace(placeholder, replacement, 1)
1817
+ return tok([text], return_tensors="pt").to(DEV)["input_ids"][0]
1818
+
1819
+
1820
+ def _proc_full(im, query):
1821
+ """Full processor dict (input_ids, attention_mask, pixel_values, image_grid_hws) —
1822
+ used by the bench to drive the STOCK generate for the equivalence check."""
1823
+ tok, proc, model = load()
1824
+ msg = [{"role": "user", "content": [{"type": "image", "image": im},
1825
+ {"type": "text", "text": _PROMPT + query + "."}]}]
1826
+ text = proc.py_apply_chat_template(msg, tokenize=False, add_generation_prompt=True)
1827
+ imgs, vids = proc.process_vision_info(msg)
1828
+ inp = proc(text=[text], images=imgs, videos=vids, return_tensors="pt").to(DEV)
1829
+ grid = inp.get("image_grid_hws")
1830
+ if isinstance(grid, np.ndarray): grid = torch.from_numpy(grid).to(DEV, dtype=torch.int32)
1831
+ inp["image_grid_hws"] = grid
1832
+ return inp
1833
+
1834
+ def _pad_generated(prompt_ids, gen_ids, img_tok, dev):
1835
+ """Per-row [prompt + accepted] left-padded with the image token (already in every
1836
+ prompt -> .unique() unchanged -> repetition penalty identical to single-run)."""
1837
+ rows = [list(prompt_ids[b].tolist()) + gen_ids[b] for b in range(len(prompt_ids))]
1838
+ M = max(len(r) for r in rows)
1839
+ out = torch.full((len(rows), M), img_tok, dtype=torch.long, device=dev)
1840
+ for b, r in enumerate(rows):
1841
+ out[b, M - len(r):] = torch.tensor(r, dtype=torch.long, device=dev)
1842
+ return out
chat_template.json ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {
2
+ "chat_template": "{% set image_count = namespace(value=0) %}{% set video_count = namespace(value=0) %}{% for message in messages %}{% if loop.first and message['role'] != 'system' %}<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n{% endif %}<|im_start|>{{ message['role'] }}\n{% if message['content'] is string %}{{ message['content'] }}<|im_end|>\n{% else %}{% for content in message['content'] %}{% if content['type'] == 'image' or 'image' in content or 'image_url' in content %}{% set image_count.value = image_count.value + 1 %}{% if add_vision_id %}<image {{ image_count.value }}>{% endif %}<image-{{ image_count.value }}>{% elif content['type'] == 'video' or 'video' in content %}{% set video_count.value = video_count.value + 1 %}{% if add_vision_id %}<video {{ video_count.value }}>{% endif %}<video-{{ video_count.value }}>{% elif 'text' in content %}{{ content['text'] }}{% endif %}{% endfor %}<|im_end|>\n{% endif %}{% endfor %}{% if add_generation_prompt %}<|im_start|>assistant\n{% endif %}"
3
+ }
4
+
config.json ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_attn_implementation": "magi",
3
+ "_commit_hash": null,
4
+ "architectures": [
5
+ "LocateAnythingForConditionalGeneration"
6
+ ],
7
+ "auto_map": {
8
+ "AutoConfig": "configuration_locateanything.LocateAnythingConfig",
9
+ "AutoModel": "modeling_locateanything.LocateAnythingForConditionalGeneration"
10
+ },
11
+ "box_end_token_id": 151669,
12
+ "box_start_token_id": 151668,
13
+ "coord_end_token_id": 152677,
14
+ "coord_start_token_id": 151677,
15
+ "image_token_index": 151665,
16
+ "mlp_checkpoint": false,
17
+ "mlp_connector_layers": 2,
18
+ "model_type": "locateanything",
19
+ "none_token_id": 4064,
20
+ "ref_end_token_id": 151673,
21
+ "ref_start_token_id": 151672,
22
+ "template": null,
23
+ "text_config": {
24
+ "_attn_implementation_autoset": true,
25
+ "_name_or_path": "Qwen/Qwen2.5-3B-Instruct",
26
+ "architectures": [
27
+ "Qwen2ForCausalLM"
28
+ ],
29
+ "attention_dropout": 0.0,
30
+ "block_size": 6,
31
+ "bos_token_id": 151643,
32
+ "causal_attn": false,
33
+ "eos_token_id": 151645,
34
+ "hidden_act": "silu",
35
+ "hidden_size": 2048,
36
+ "initializer_range": 0.02,
37
+ "intermediate_size": 11008,
38
+ "max_position_embeddings": 32768,
39
+ "max_window_layers": 70,
40
+ "model_type": "qwen2",
41
+ "null_token_id": 152678,
42
+ "num_attention_heads": 16,
43
+ "num_hidden_layers": 36,
44
+ "num_key_value_heads": 2,
45
+ "rms_norm_eps": 1e-06,
46
+ "rope_scaling": null,
47
+ "rope_theta": 1000000.0,
48
+ "sliding_window": 32768,
49
+ "switch_token_id": 152679,
50
+ "text_mask_token_id": 151676,
51
+ "tie_word_embeddings": true,
52
+ "torch_dtype": "bfloat16",
53
+ "use_cache": false,
54
+ "use_sliding_window": false,
55
+ "vocab_size": 152681
56
+ },
57
+ "torch_dtype": "bfloat16",
58
+ "transformers_version": null,
59
+ "use_backbone_lora": 0,
60
+ "use_llm_lora": 0,
61
+ "vision_config": {
62
+ "_attn_implementation_autoset": true,
63
+ "_name_or_path": "moonshotai/MoonViT-SO-400M",
64
+ "auto_map": {
65
+ "AutoConfig": "moonshotai/MoonViT-SO-400M--configuration_moonvit.MoonViTConfig",
66
+ "AutoModel": "moonshotai/MoonViT-SO-400M--modeling_moonvit.MoonVitPretrainedModel"
67
+ },
68
+ "hidden_size": 1152,
69
+ "init_pos_emb_height": 64,
70
+ "init_pos_emb_width": 64,
71
+ "intermediate_size": 4304,
72
+ "merge_kernel_size": [
73
+ 2,
74
+ 2
75
+ ],
76
+ "model_type": "moonvit",
77
+ "num_attention_heads": 16,
78
+ "num_hidden_layers": 27,
79
+ "patch_size": 14,
80
+ "torch_dtype": "bfloat16"
81
+ }
82
+ }
configuration_locateanything.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # --------------------------------------------------------
2
+ # InternVL
3
+ # Copyright (c) 2023 OpenGVLab
4
+ # Licensed under The MIT License [see LICENSE for details]
5
+ # --------------------------------------------------------
6
+
7
+ import copy
8
+
9
+ from transformers.models.qwen2.configuration_qwen2 import Qwen2Config
10
+ from transformers.models.qwen3.configuration_qwen3 import Qwen3Config
11
+ from transformers.configuration_utils import PretrainedConfig
12
+ from transformers.utils import logging
13
+ logger = logging.get_logger(__name__)
14
+
15
+ class MoonViTConfig(PretrainedConfig):
16
+ model_type = "moonvit"
17
+
18
+ def __init__(
19
+ self,
20
+ patch_size: int = 14,
21
+ init_pos_emb_height: int = 64,
22
+ init_pos_emb_width: int = 64,
23
+ num_attention_heads: int = 16,
24
+ num_hidden_layers: int = 27,
25
+ hidden_size: int = 1152,
26
+ intermediate_size: int = 4304,
27
+ merge_kernel_size: tuple[int, int] = (2, 2),
28
+ **kwargs,
29
+ ):
30
+ super().__init__(**kwargs)
31
+ self.patch_size = patch_size
32
+ # Positional embedding config
33
+ self.init_pos_emb_height = init_pos_emb_height
34
+ self.init_pos_emb_width = init_pos_emb_width
35
+ # Transformer config
36
+ self.num_hidden_layers = num_hidden_layers
37
+ self.num_attention_heads = num_attention_heads
38
+ self.hidden_size = hidden_size
39
+ self.intermediate_size = intermediate_size
40
+ # Patch merger config
41
+ self.merge_kernel_size = merge_kernel_size
42
+
43
+
44
+ class LocateAnythingConfig(PretrainedConfig):
45
+ model_type = 'locateanything'
46
+ is_composition = True
47
+ sub_configs = {"vision_config": MoonViTConfig, "text_config": Qwen2Config}
48
+ def __init__(
49
+ self,
50
+ vision_config=None,
51
+ text_config=None,
52
+ use_backbone_lora=0,
53
+ use_llm_lora=0,
54
+ downsample_ratio=0.5,
55
+ template=None,
56
+ loss_version='v1',
57
+ mlp_checkpoint=False,
58
+ image_token_index=151667,
59
+ box_start_token_id=151668,
60
+ box_end_token_id=151669,
61
+ coord_start_token_id=151677,
62
+ coord_end_token_id=152677,
63
+ ref_start_token_id=151672,
64
+ ref_end_token_id=151673,
65
+ none_token_id=4064,
66
+ **kwargs):
67
+ super().__init__(**kwargs)
68
+
69
+ if vision_config is None:
70
+ vision_config = {'model_type': 'moonvit'}
71
+ logger.info('vision_config is None. Initializing the MoonViTConfig with default values.')
72
+
73
+ if text_config is None:
74
+ text_config = {'architectures': ['Qwen2ForCausalLM']}
75
+ logger.info('text_config is None. Initializing the Qwen2Config config with default values.')
76
+
77
+ if vision_config['model_type'] == 'moonvit':
78
+ self.vision_config = MoonViTConfig(**vision_config)
79
+ else:
80
+ raise ValueError('Unsupported model_type: {}. Only moonvit is supported.'.format(vision_config['model_type']))
81
+
82
+
83
+ if text_config['architectures'][0] == 'Qwen2ForCausalLM':
84
+ self.text_config = Qwen2Config(**text_config)
85
+ elif text_config['architectures'][0] == 'Qwen3ForCausalLM':
86
+ self.text_config = Qwen3Config(**text_config)
87
+ else:
88
+ raise ValueError('Unsupported architecture: {}. Only Qwen2ForCausalLM and Qwen3ForCausalLM are supported.'.format(text_config['architectures'][0]))
89
+ self.use_backbone_lora = use_backbone_lora
90
+ self.use_llm_lora = use_llm_lora
91
+ self.mlp_checkpoint = mlp_checkpoint
92
+ self.downsample_ratio = downsample_ratio
93
+ self.template = template
94
+ self.loss_version = loss_version
95
+ self.tie_word_embeddings = self.text_config.tie_word_embeddings
96
+ self.image_token_index = image_token_index
97
+ self.box_start_token_id = box_start_token_id
98
+ self.box_end_token_id = box_end_token_id
99
+ self.coord_start_token_id = coord_start_token_id
100
+ self.coord_end_token_id = coord_end_token_id
101
+ self.ref_start_token_id = ref_start_token_id
102
+ self.ref_end_token_id = ref_end_token_id
103
+ self.none_token_id = none_token_id
104
+
105
+ def to_dict(self):
106
+ """
107
+ Serializes this instance to a Python dictionary. Override the default [`~PretrainedConfig.to_dict`].
108
+
109
+ Returns:
110
+ `Dict[str, any]`: Dictionary of all the attributes that make up this configuration instance,
111
+ """
112
+ output = copy.deepcopy(self.__dict__)
113
+ output['vision_config'] = self.vision_config.to_dict()
114
+ output['text_config'] = self.text_config.to_dict()
115
+ output['model_type'] = self.__class__.model_type
116
+ output['use_backbone_lora'] = self.use_backbone_lora
117
+ output['use_llm_lora'] = self.use_llm_lora
118
+ output['downsample_ratio'] = self.downsample_ratio
119
+ output['template'] = self.template
120
+ output['image_token_index'] = self.image_token_index
121
+ output['box_start_token_id'] = self.box_start_token_id
122
+ output['box_end_token_id'] = self.box_end_token_id
123
+ output['coord_start_token_id'] = self.coord_start_token_id
124
+ output['coord_end_token_id'] = self.coord_end_token_id
125
+ output['ref_start_token_id'] = self.ref_start_token_id
126
+ output['ref_end_token_id'] = self.ref_end_token_id
127
+ output['none_token_id'] = self.none_token_id
128
+ output['_attn_implementation'] = self._attn_implementation
129
+ if hasattr(self, '_attn_implementation_autoset'):
130
+ output['_attn_implementation_autoset'] = self._attn_implementation_autoset
131
+ return output
configuration_qwen2.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2024 The Qwen team, Alibaba Group and the HuggingFace Inc. 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
+ """ Qwen2 model configuration"""
16
+
17
+ from transformers.configuration_utils import PretrainedConfig
18
+ from transformers.utils import logging
19
+
20
+
21
+ logger = logging.get_logger(__name__)
22
+
23
+ QWEN2_PRETRAINED_CONFIG_ARCHIVE_MAP = {
24
+ "Qwen/Qwen2-7B-beta": "https://huggingface.co/Qwen/Qwen2-7B-beta/resolve/main/config.json",
25
+ }
26
+
27
+
28
+ class Qwen2Config(PretrainedConfig):
29
+ r"""
30
+ This is the configuration class to store the configuration of a [`Qwen2Model`]. It is used to instantiate a
31
+ Qwen2 model according to the specified arguments, defining the model architecture. Instantiating a configuration
32
+ with the defaults will yield a similar configuration to that of
33
+ Qwen2-7B-beta [Qwen/Qwen2-7B-beta](https://huggingface.co/Qwen/Qwen2-7B-beta).
34
+
35
+ Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
36
+ documentation from [`PretrainedConfig`] for more information.
37
+
38
+
39
+ Args:
40
+ vocab_size (`int`, *optional*, defaults to 151936):
41
+ Vocabulary size of the Qwen2 model. Defines the number of different tokens that can be represented by the
42
+ `inputs_ids` passed when calling [`Qwen2Model`]
43
+ hidden_size (`int`, *optional*, defaults to 4096):
44
+ Dimension of the hidden representations.
45
+ intermediate_size (`int`, *optional*, defaults to 22016):
46
+ Dimension of the MLP representations.
47
+ num_hidden_layers (`int`, *optional*, defaults to 32):
48
+ Number of hidden layers in the Transformer encoder.
49
+ num_attention_heads (`int`, *optional*, defaults to 32):
50
+ Number of attention heads for each attention layer in the Transformer encoder.
51
+ num_key_value_heads (`int`, *optional*, defaults to 32):
52
+ This is the number of key_value heads that should be used to implement Grouped Query Attention. If
53
+ `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
54
+ `num_key_value_heads=1 the model will use Multi Query Attention (MQA) otherwise GQA is used. When
55
+ converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
56
+ by meanpooling all the original heads within that group. For more details checkout [this
57
+ paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to `32`.
58
+ hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
59
+ The non-linear activation function (function or string) in the decoder.
60
+ max_position_embeddings (`int`, *optional*, defaults to 32768):
61
+ The maximum sequence length that this model might ever be used with.
62
+ initializer_range (`float`, *optional*, defaults to 0.02):
63
+ The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
64
+ rms_norm_eps (`float`, *optional*, defaults to 1e-06):
65
+ The epsilon used by the rms normalization layers.
66
+ use_cache (`bool`, *optional*, defaults to `True`):
67
+ Whether or not the model should return the last key/values attentions (not used by all models). Only
68
+ relevant if `config.is_decoder=True`.
69
+ tie_word_embeddings (`bool`, *optional*, defaults to `False`):
70
+ Whether the model's input and output word embeddings should be tied.
71
+ rope_theta (`float`, *optional*, defaults to 10000.0):
72
+ The base period of the RoPE embeddings.
73
+ use_sliding_window (`bool`, *optional*, defaults to `False`):
74
+ Whether to use sliding window attention.
75
+ sliding_window (`int`, *optional*, defaults to 4096):
76
+ Sliding window attention (SWA) window size. If not specified, will default to `4096`.
77
+ max_window_layers (`int`, *optional*, defaults to 28):
78
+ The number of layers that use SWA (Sliding Window Attention). The bottom layers use SWA while the top use full attention.
79
+ attention_dropout (`float`, *optional*, defaults to 0.0):
80
+ The dropout ratio for the attention probabilities.
81
+
82
+ ```python
83
+ >>> from transformers import Qwen2Model, Qwen2Config
84
+
85
+ >>> # Initializing a Qwen2 style configuration
86
+ >>> configuration = Qwen2Config()
87
+
88
+ >>> # Initializing a model from the Qwen2-7B style configuration
89
+ >>> model = Qwen2Model(configuration)
90
+
91
+ >>> # Accessing the model configuration
92
+ >>> configuration = model.config
93
+ ```"""
94
+
95
+ model_type = "qwen2"
96
+ keys_to_ignore_at_inference = ["past_key_values"]
97
+
98
+ def __init__(
99
+ self,
100
+ vocab_size=151936,
101
+ hidden_size=4096,
102
+ intermediate_size=22016,
103
+ num_hidden_layers=32,
104
+ num_attention_heads=32,
105
+ num_key_value_heads=32,
106
+ hidden_act="silu",
107
+ max_position_embeddings=32768,
108
+ initializer_range=0.02,
109
+ rms_norm_eps=1e-6,
110
+ use_cache=True,
111
+ tie_word_embeddings=False,
112
+ rope_theta=10000.0,
113
+ use_sliding_window=False,
114
+ sliding_window=4096,
115
+ max_window_layers=28,
116
+ attention_dropout=0.0,
117
+ **kwargs,
118
+ ):
119
+ self.vocab_size = vocab_size
120
+ self.max_position_embeddings = max_position_embeddings
121
+ self.hidden_size = hidden_size
122
+ self.intermediate_size = intermediate_size
123
+ self.num_hidden_layers = num_hidden_layers
124
+ self.num_attention_heads = num_attention_heads
125
+ self.use_sliding_window = use_sliding_window
126
+ self.sliding_window = sliding_window
127
+ self.max_window_layers = max_window_layers
128
+
129
+ # for backward compatibility
130
+ if num_key_value_heads is None:
131
+ num_key_value_heads = num_attention_heads
132
+
133
+ self.num_key_value_heads = num_key_value_heads
134
+ self.hidden_act = hidden_act
135
+ self.initializer_range = initializer_range
136
+ self.rms_norm_eps = rms_norm_eps
137
+ self.use_cache = use_cache
138
+ self.rope_theta = rope_theta
139
+ self.attention_dropout = attention_dropout
140
+ if kwargs.get('attn_implementation', None) is None:
141
+ self.attn_implementation = kwargs['attn_implementation'] = 'flash_attention_2'
142
+ else:
143
+ self.attn_implementation = kwargs['attn_implementation']
144
+
145
+ super().__init__(
146
+ tie_word_embeddings=tie_word_embeddings,
147
+ **kwargs,
148
+ )
generate_utils.py ADDED
@@ -0,0 +1,504 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved.
2
+ #
3
+ # NVIDIA CORPORATION and its licensors retain all intellectual property
4
+ # and proprietary rights in and to this software, related documentation
5
+ # and any modifications thereto. Any use, reproduction, disclosure or
6
+ # distribution of this software and related documentation without an express
7
+ # license agreement from NVIDIA CORPORATION is strictly prohibited.
8
+
9
+ import torch
10
+ import torch.nn.functional as F
11
+ import torch.distributions as dists
12
+ from typing import Dict, Optional
13
+
14
+
15
+ def get_token_ids_from_config(config) -> Dict[str, int]:
16
+ """Extract all token IDs from the configuration object.
17
+
18
+ Args:
19
+ config: Configuration object (LocateAnythingConfig or similar)
20
+
21
+ Returns:
22
+ Dictionary containing all token IDs
23
+ """
24
+ token_ids = {}
25
+
26
+ # Get from main config
27
+ token_ids['box_start_token_id'] = getattr(config, 'box_start_token_id', 151668)
28
+ token_ids['box_end_token_id'] = getattr(config, 'box_end_token_id', 151669)
29
+ token_ids['coord_start_token_id'] = getattr(config, 'coord_start_token_id', 151677)
30
+ token_ids['coord_end_token_id'] = getattr(config, 'coord_end_token_id', 152677)
31
+ token_ids['ref_start_token_id'] = getattr(config, 'ref_start_token_id', 151672)
32
+ token_ids['ref_end_token_id'] = getattr(config, 'ref_end_token_id', 151673)
33
+ token_ids['none_token_id'] = getattr(config, 'none_token_id', 4064)
34
+
35
+ # Get from text_config
36
+ text_config = getattr(config, 'text_config', None)
37
+ if text_config is not None:
38
+ token_ids['null_token_id'] = getattr(text_config, 'null_token_id', 152678)
39
+ token_ids['im_end_token_id'] = getattr(text_config, 'eos_token_id', 151645)
40
+ token_ids['switch_token_id'] = getattr(text_config, 'switch_token_id', 152679)
41
+ token_ids['default_mask_token_id'] = getattr(text_config, 'text_mask_token_id', 151676)
42
+ else:
43
+ token_ids['null_token_id'] = 152678
44
+ token_ids['im_end_token_id'] = 151645
45
+ token_ids['switch_token_id'] = 152679
46
+ token_ids['default_mask_token_id'] = 151676
47
+
48
+ return token_ids
49
+
50
+
51
+ def top_p_logits(
52
+ logits: torch.Tensor,
53
+ top_p: float = None
54
+ ) -> torch.Tensor:
55
+ sorted_logits, sorted_indices = torch.sort(logits, descending=True)
56
+ cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
57
+ sorted_indices_to_remove = cumulative_probs > top_p
58
+ # Shift the indices to the right to keep the first token above the threshold
59
+ sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
60
+ sorted_indices_to_remove[..., 0] = 0
61
+
62
+ mask = torch.zeros_like(logits, dtype=torch.bool, device=logits.device)
63
+ mask = mask.scatter_(-1, sorted_indices, sorted_indices_to_remove)
64
+ logits = logits.masked_fill(mask, torch.finfo(logits.dtype).min)
65
+ return logits
66
+
67
+
68
+ def top_k_logits(
69
+ logits: torch.Tensor,
70
+ top_k: int = None
71
+ ) -> torch.Tensor:
72
+ top_k = min(top_k, logits.size(-1)) # Safety check
73
+ # Remove all tokens with a probability less than the last token of the top-k
74
+ indices_to_remove = logits < torch.topk(logits, top_k)[0][..., -1, None]
75
+ logits = logits.masked_fill(indices_to_remove, torch.finfo(logits.dtype).min)
76
+ return logits
77
+
78
+
79
+ def apply_repetition_penalty(
80
+ logits: torch.Tensor,
81
+ input_ids: torch.Tensor,
82
+ repetition_penalty: float = 1.0
83
+ ) -> torch.Tensor:
84
+ """
85
+ Apply repetition penalty to logits.
86
+
87
+ Args:
88
+ logits: Shape [batch_size, seq_len, vocab_size] or [batch_size, vocab_size]
89
+ input_ids: Previously generated token ids, shape [batch_size, seq_len]
90
+ repetition_penalty: Penalty factor. > 1.0 penalizes repetition, < 1.0 encourages it.
91
+
92
+ Returns:
93
+ Modified logits with repetition penalty applied.
94
+ """
95
+ if repetition_penalty == 1.0:
96
+ return logits
97
+
98
+ # Convert to 3D for vectorized computation
99
+ if logits.dim() == 2:
100
+ logits = logits.unsqueeze(1) # [B, 1, V]
101
+ squeeze_back = True
102
+ else:
103
+ squeeze_back = False
104
+
105
+ batch_size, seq_len, vocab_size = logits.shape
106
+
107
+ # Construct [B, V] bool mask marking tokens that have appeared in each batch
108
+ device = logits.device
109
+ token_mask = torch.zeros(batch_size, vocab_size, dtype=torch.bool, device=device)
110
+ for b in range(batch_size):
111
+ # Apply penalty only based on tokens already generated in this batch
112
+ unique_tokens = input_ids[b].unique()
113
+ # Prevent out-of-bounds: only keep IDs within vocab range
114
+ valid_tokens = unique_tokens[(unique_tokens >= 0) & (unique_tokens < vocab_size)]
115
+ if valid_tokens.numel() > 0:
116
+ token_mask[b, valid_tokens] = True
117
+
118
+ # Expand to [B, L, V] to align with logits
119
+ token_mask = token_mask.unsqueeze(1).expand(-1, seq_len, -1)
120
+
121
+ # Divide positive values by penalty, multiply negative values by penalty
122
+ positive = logits > 0
123
+ negative = ~positive
124
+
125
+ # Apply penalty only at mask positions
126
+ logits = torch.where(token_mask & positive, logits / repetition_penalty, logits)
127
+ logits = torch.where(token_mask & negative, logits * repetition_penalty, logits)
128
+
129
+ if squeeze_back:
130
+ logits = logits.squeeze(1)
131
+
132
+ return logits
133
+
134
+
135
+ def sample_tokens(
136
+ logits: torch.Tensor,
137
+ generated: torch.Tensor,
138
+ token_ids: Dict[str, int],
139
+ **generate_kwargs,
140
+ ):
141
+ batch_size, seq_len, vocab_size = logits.shape
142
+
143
+ repetition_penalty = generate_kwargs.get('repetition_penalty', 1.0)
144
+ temperature = generate_kwargs.get('temperature', 0)
145
+ top_p = generate_kwargs.get('top_p', None)
146
+ top_k = generate_kwargs.get('top_k', None)
147
+
148
+ # Apply repetition penalty based on all previously generated tokens
149
+ if repetition_penalty != 1.0:
150
+ logits = apply_repetition_penalty(logits, generated, repetition_penalty)
151
+
152
+ if temperature > 0:
153
+ logits = logits / temperature
154
+ if top_p is not None and top_p < 1:
155
+ logits = top_p_logits(logits, top_p)
156
+ if top_k is not None:
157
+ logits = top_k_logits(logits, top_k)
158
+
159
+ probs = torch.softmax(logits, dim=-1)
160
+
161
+ if temperature > 0:
162
+ try:
163
+ x0 = dists.Categorical(probs=probs).sample()
164
+ confidence = torch.gather(probs, -1, x0.unsqueeze(-1)).squeeze(-1)
165
+ except Exception:
166
+ confidence, x0 = probs.max(dim=-1)
167
+ else:
168
+ confidence, x0 = probs.max(dim=-1)
169
+
170
+ if seq_len == 1:
171
+ return probs, confidence, x0, None
172
+
173
+ box_avg = []
174
+ fallback_box = torch.zeros(1, dtype=x0.dtype, device=x0.device)
175
+
176
+ for b in range(batch_size):
177
+ decoded_box = decode_bbox_avg(
178
+ logits[b], probs[b], token_ids, keep_k=generate_kwargs.get('keep_k_avg', 4),
179
+ generation_mode=generate_kwargs.get('generation_mode', 'hybrid'),
180
+ )
181
+ if decoded_box is not None:
182
+ box_avg.append(decoded_box)
183
+ else:
184
+ out_ref = decode_ref(logits[b], probs[b], token_ids)
185
+ if out_ref is not None:
186
+ box_avg.append(torch.tensor(out_ref, dtype=x0.dtype, device=x0.device))
187
+ else:
188
+ box_avg.append(fallback_box)
189
+
190
+ box_avg = torch.stack(box_avg)
191
+
192
+ return probs, confidence, x0, box_avg
193
+
194
+
195
+ def sample_tokens_ar(
196
+ logits: torch.Tensor,
197
+ generated: torch.Tensor,
198
+ token_ids: Dict[str, int],
199
+ **generate_kwargs,
200
+ ):
201
+ """
202
+ Lightweight sampling function for AR single-step sampling only.
203
+
204
+ Args:
205
+ logits: [batch_size, vocab_size] or [batch_size, 1, vocab_size]
206
+ generated: [batch_size, seq_len]
207
+ """
208
+ # Convert to 3D for reusing repetition penalty and clipping logic
209
+ if logits.dim() == 2:
210
+ logits = logits.unsqueeze(1) # [B, 1, V]
211
+ batch_size, seq_len, vocab_size = logits.shape
212
+ assert seq_len == 1, "sample_tokens_ar only supports single-step AR sampling (seq_len == 1)"
213
+
214
+ repetition_penalty = generate_kwargs.get('repetition_penalty', 1.0)
215
+ temperature = generate_kwargs.get('temperature', 0)
216
+ top_p = generate_kwargs.get('top_p', None)
217
+ top_k = generate_kwargs.get('top_k', None)
218
+
219
+ # Apply repetition penalty only based on historically generated tokens
220
+ if repetition_penalty != 1.0:
221
+ logits = apply_repetition_penalty(logits, generated, repetition_penalty)
222
+
223
+ if temperature > 0:
224
+ logits = logits / temperature
225
+ if top_p is not None and top_p < 1:
226
+ logits = top_p_logits(logits, top_p)
227
+ if top_k is not None:
228
+ logits = top_k_logits(logits, top_k)
229
+
230
+ probs = torch.softmax(logits, dim=-1)
231
+
232
+ if temperature > 0:
233
+ try:
234
+ x0 = dists.Categorical(probs=probs).sample()
235
+ confidence = torch.gather(probs, -1, x0.unsqueeze(-1)).squeeze(-1)
236
+ except Exception:
237
+ confidence, x0 = probs.max(dim=-1)
238
+ else:
239
+ # For greedy: directly take the token with maximum probability
240
+ confidence, x0 = probs.max(dim=-1)
241
+
242
+ # Keep interface consistent with sample_tokens: return [B, 1, V] / [B, 1] shape
243
+ return probs, confidence, x0, None, None
244
+
245
+
246
+ def is_valid_box_frame(
247
+ probs,
248
+ token_ids: Dict[str, int],
249
+ start_thresh=0.6,
250
+ end_thresh=0.2,
251
+ topk=5,
252
+ ):
253
+ box_start_token_id = token_ids['box_start_token_id']
254
+ box_end_token_id = token_ids['box_end_token_id']
255
+ null_token_id = token_ids['null_token_id']
256
+ im_end_token_id = token_ids['im_end_token_id']
257
+ none_token_id = token_ids['none_token_id'] # none
258
+
259
+ p_start = probs[0, box_start_token_id]
260
+ if p_start >= start_thresh:
261
+ if (probs[1, none_token_id] > 0.2 and
262
+ probs[2, box_end_token_id] > 0.2 and
263
+ probs[3, null_token_id] > 0.1 and
264
+ probs[4, null_token_id] > 0.1):
265
+ return 'empty_box'
266
+
267
+ end_target_ids = torch.tensor([box_end_token_id, null_token_id, im_end_token_id], device=probs.device)
268
+ end_score = probs[5, end_target_ids].sum()
269
+
270
+ if end_score >= end_thresh:
271
+ return 'legal_box'
272
+
273
+ return 'illegal_box'
274
+
275
+
276
+ def decode_bbox_avg(
277
+ logits,
278
+ probs,
279
+ token_ids: Dict[str, int],
280
+ keep_k=5,
281
+ start_thresh=0.7,
282
+ end_thresh=0.2,
283
+ generation_mode: str = 'hybrid',
284
+ ):
285
+ """
286
+ Decode bounding box coordinates using top-k weighted average.
287
+
288
+ Args:
289
+ logits: Logits of shape (6, vocab_size)
290
+ probs: Probability distribution of shape (6, vocab_size)
291
+ token_ids: Dictionary containing all token IDs
292
+ keep_k: Number of top-k candidate tokens to keep at each position
293
+ start_thresh: Confidence threshold for box start token
294
+ end_thresh: Confidence threshold for box end token
295
+
296
+ Returns:
297
+ Decoded bounding box coordinate list in format [box_start, x1, x2, y1, y2, box_end],
298
+ or None if decoding fails
299
+ """
300
+ coord_start_token_id = token_ids['coord_start_token_id']
301
+ coord_end_token_id = token_ids['coord_end_token_id']
302
+ box_start_token_id = token_ids['box_start_token_id']
303
+ box_end_token_id = token_ids['box_end_token_id']
304
+ none_token_id = token_ids['none_token_id']
305
+
306
+ device = logits.device
307
+
308
+ box_type = is_valid_box_frame(
309
+ probs,
310
+ token_ids,
311
+ start_thresh=start_thresh,
312
+ end_thresh=end_thresh,
313
+ topk=keep_k
314
+ )
315
+ if box_type == 'empty_box':
316
+ # Handle the <box>none</box> case first
317
+ return torch.tensor([
318
+ box_start_token_id,
319
+ none_token_id,
320
+ box_end_token_id,
321
+ token_ids['null_token_id'],
322
+ token_ids['null_token_id'],
323
+ token_ids['null_token_id']
324
+ ], dtype=torch.long, device=probs.device)
325
+ elif box_type == 'illegal_box':
326
+ return None
327
+
328
+ # Extract probabilities at positions 1-4 and compute Top-K for all 4 positions at once
329
+ pos_probs, pos_ids = torch.topk(probs[1:5], k=keep_k, dim=-1)
330
+ mask = (pos_ids >= coord_start_token_id) & (pos_ids <= coord_end_token_id)
331
+ has_valid = mask.any(dim=-1) # shape: [4]
332
+ if not has_valid.all():
333
+ return None # not a box, exit...
334
+
335
+ first_valid_idx = mask.long().argmax(dim=-1, keepdim=True) # [4, 1]
336
+ # Extract highest-probability valid_probs[0] and corresponding valid_ids[0]
337
+ first_valid_probs = pos_probs.gather(-1, first_valid_idx).squeeze(-1) # [4]
338
+ first_valid_ids = pos_ids.gather(-1, first_valid_idx).squeeze(-1) # [4]
339
+ if generation_mode == 'hybrid':
340
+ valid_counts = mask.sum(dim=-1) # [4]
341
+ # Compute max/min of valid ids: fill invalid positions with extreme values to avoid interfering with max/min
342
+ LARGE_NUM, SMALL_NUM = 999999, -999999
343
+ valid_ids_for_max = torch.where(mask, pos_ids, torch.tensor(SMALL_NUM, device=device))
344
+ valid_ids_for_min = torch.where(mask, pos_ids, torch.tensor(LARGE_NUM, device=device))
345
+
346
+ valid_max = valid_ids_for_max.max(dim=-1)[0]
347
+ valid_min = valid_ids_for_min.min(dim=-1)[0]
348
+
349
+ is_abnormal = (first_valid_probs < 0.9) & (valid_counts > 1) & ((valid_max - valid_min) > 60)
350
+ # is_abnormal = (first_valid_probs < 0.7) & (valid_counts > 1) & ((valid_max - valid_min) > 80)
351
+
352
+ # Normal positions take top-1 (first_valid_ids); abnormal positions are replaced with 0
353
+ final_coords = torch.where(is_abnormal, torch.tensor(0, device=pos_ids.device), first_valid_ids)
354
+ elif generation_mode == 'fast':
355
+ final_coords = first_valid_ids
356
+
357
+
358
+ start_t = torch.tensor([box_start_token_id], dtype=final_coords.dtype, device=device)
359
+ end_t = torch.tensor([box_end_token_id], dtype=final_coords.dtype, device=device)
360
+
361
+ return torch.cat([start_t, final_coords, end_t])
362
+
363
+
364
+ def decode_ref(
365
+ logits,
366
+ probs,
367
+ token_ids: Dict[str, int],
368
+ keep_k=5,
369
+ start_thresh=0.6,
370
+ ):
371
+ ref_start_token_id = token_ids.get('ref_start_token_id')
372
+ coord_start_token_id = token_ids['coord_start_token_id']
373
+ coord_end_token_id = token_ids['coord_end_token_id']
374
+ device = probs.device
375
+ L = probs.size(0)
376
+
377
+ # 1. Check if the first position is <ref> and its probability meets start_thresh
378
+ # Note: we directly use the probability of the ref token at position 0 for the check
379
+ if probs[0, ref_start_token_id] < start_thresh:
380
+ return None
381
+
382
+ # 2. Extract Top-K probabilities and token IDs for all subsequent positions
383
+ pos_probs, pos_ids = torch.topk(probs[1:], k=keep_k, dim=-1) # shape: [L-1, keep_k]
384
+
385
+ # 3. Build mask: identify coordinate tokens (<0> ~ <1000>)
386
+ is_coord = (pos_ids >= coord_start_token_id) & (pos_ids <= coord_end_token_id)
387
+ # Invert: valid tokens are non-coordinate tokens
388
+ is_valid = ~is_coord # shape: [L-1, keep_k]
389
+
390
+ # Ensure each position has at least one non-coordinate valid token in its Top-K
391
+ has_valid = is_valid.any(dim=-1) # shape: [L-1]
392
+ if not has_valid.all():
393
+ return None
394
+
395
+ # 4. Get the highest-probability valid token
396
+ # Since topk results are sorted in descending order of probability,
397
+ # argmax returns the first index where is_valid is True, i.e., the index of the most probable valid token
398
+ first_valid_idx = is_valid.long().argmax(dim=-1, keepdim=True) # shape: [L-1, 1]
399
+
400
+ # Extract the final token IDs
401
+ final_text_ids = pos_ids.gather(-1, first_valid_idx).squeeze(-1) # shape: [L-1]
402
+
403
+ start_t = torch.tensor([ref_start_token_id], dtype=final_text_ids.dtype, device=device)
404
+
405
+ return torch.cat([start_t, final_text_ids])
406
+
407
+
408
+ def handle_pattern(x0, token_ids: Dict[str, int], generation_mode: str = 'hybrid'):
409
+ """
410
+ Args:
411
+ x0: Token ID list of length 6
412
+ token_ids: Dictionary containing all token IDs
413
+ """
414
+ null_token_id = token_ids['null_token_id']
415
+ im_end_token_id = token_ids['im_end_token_id']
416
+ box_start_token_id = token_ids['box_start_token_id']
417
+ box_end_token_id = token_ids['box_end_token_id']
418
+ none_token_id = token_ids['none_token_id']
419
+ coord_start_token_id = token_ids['coord_start_token_id']
420
+ coord_end_token_id = token_ids['coord_end_token_id']
421
+ ref_end_token_id = token_ids['ref_end_token_id']
422
+
423
+ x0 = x0.tolist()
424
+
425
+ if x0[0] == null_token_id:
426
+ return {
427
+ "type": "im_end",
428
+ "tokens": [im_end_token_id],
429
+ "need_switch_to_ar": False,
430
+ "is_terminal": True,
431
+ }
432
+ elif x0[0] == im_end_token_id:
433
+ return {
434
+ "type": "im_end",
435
+ "tokens": [im_end_token_id],
436
+ "need_switch_to_ar": False,
437
+ "is_terminal": True,
438
+ }
439
+ elif x0[:2] == [box_start_token_id, none_token_id]:
440
+ return {
441
+ "type": "empty_box",
442
+ "tokens": [box_start_token_id, none_token_id, box_end_token_id],
443
+ "need_switch_to_ar": False,
444
+ "is_terminal": False,
445
+ }
446
+ elif x0[0] == box_start_token_id:
447
+ coord_ix = 1
448
+ for coord in x0[1:5]:
449
+ if coord_start_token_id <= coord <= coord_end_token_id:
450
+ coord_ix += 1
451
+ else:
452
+ break
453
+
454
+ # Standard 4-coordinate bbox: <box><x1><x2><y1><y2></box>
455
+ if coord_ix == 5 and x0[5] == box_end_token_id:
456
+ return {
457
+ "type": "coord_box",
458
+ "tokens": x0,
459
+ "need_switch_to_ar": False,
460
+ "is_terminal": False,
461
+ }
462
+ # Two-coordinate pointing: <box><x><y></box>
463
+ # Convention: the first two coordinates are valid coord tokens, the third token is box_end.
464
+ # Remaining positions (if any) are not part of the pattern; truncate at box_end.
465
+ elif coord_ix == 3 and x0[3] == box_end_token_id:
466
+ return {
467
+ "type": "point_box",
468
+ "tokens": x0[:4],
469
+ "need_switch_to_ar": False,
470
+ "is_terminal": False,
471
+ }
472
+ else:
473
+ if generation_mode == 'fast':
474
+ # fast mode: treat as coord_box, stay in MTP
475
+ return {
476
+ "type": "coord_box",
477
+ "tokens": x0,
478
+ "need_switch_to_ar": False,
479
+ "is_terminal": False,
480
+ }
481
+ else:
482
+ # hybrid mode: error_box, switch to AR
483
+ return {
484
+ "type": "error_box",
485
+ "tokens": x0[:coord_ix],
486
+ "need_switch_to_ar": True,
487
+ "is_terminal": False,
488
+ }
489
+
490
+ else:
491
+ for i, token in enumerate(x0):
492
+ if token == null_token_id:
493
+ x0 = x0[:i]
494
+ break
495
+
496
+ if len(x0) >= 2 and x0[-1] == x0[-2] == ref_end_token_id:
497
+ x0 = x0[:-1]
498
+
499
+ return {
500
+ "type": "ref_object",
501
+ "tokens": x0,
502
+ "need_switch_to_ar": False,
503
+ "is_terminal": False,
504
+ }
generation_config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "bos_token_id": 151643,
4
+ "eos_token_id": 151645,
5
+ "transformers_version": "4.51.0"
6
+ }
image_processing_locateanything.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved.
2
+ #
3
+ # NVIDIA CORPORATION and its licensors retain all intellectual property
4
+ # and proprietary rights in and to this software, related documentation
5
+ # and any modifications thereto. Any use, reproduction, disclosure or
6
+ # distribution of this software and related documentation without an express
7
+ # license agreement from NVIDIA CORPORATION is strictly prohibited.
8
+
9
+ """Image processor class for KimiVL."""
10
+
11
+ import math
12
+ import numpy as np
13
+ from PIL import Image
14
+ from typing import Optional, Union
15
+
16
+ import torch
17
+ from torchvision.transforms import functional as TF
18
+ from transformers.image_utils import ImageInput, make_list_of_images, valid_images
19
+ from transformers.image_processing_utils import BaseImageProcessor, BatchFeature
20
+ from transformers.utils import TensorType
21
+ from transformers import AutoImageProcessor
22
+
23
+ MEAN = (0.5, 0.5, 0.5)
24
+ STD = (0.5, 0.5, 0.5)
25
+
26
+
27
+ class LocateAnythingImageProcessor(BaseImageProcessor):
28
+ model_type = "locateanything"
29
+
30
+ def __init__(
31
+ self,
32
+ patch_size: int = 14,
33
+ image_mean: tuple[float, float, float] = MEAN,
34
+ image_std: tuple[float, float, float] = STD,
35
+ in_token_limit: int = 4096,
36
+ merge_kernel_size: list[int, int] = [2, 2],
37
+ **kwargs,
38
+ ):
39
+ super().__init__(**kwargs)
40
+ self.in_token_limit = in_token_limit
41
+ self.patch_size = patch_size
42
+ self.image_mean = image_mean
43
+ self.image_std = image_std
44
+ self.merge_kernel_size = merge_kernel_size
45
+
46
+ def rescale(
47
+ self, image: Image.Image, merge_kernel_size: list[int, int] = [2, 2]
48
+ ) -> Image.Image:
49
+ w, h = image.size
50
+ patch_size = self.patch_size
51
+
52
+ if (w // patch_size) * (h // patch_size) > self.in_token_limit:
53
+ scale = math.sqrt(self.in_token_limit / ((w // patch_size) * (h // patch_size)))
54
+ new_w, new_h = int(w * scale), int(h * scale)
55
+ image = image.resize((new_w, new_h), Image.Resampling.BICUBIC)
56
+
57
+ new_w, new_h = image.size
58
+ pad_size_h = merge_kernel_size[0] * patch_size
59
+ pad_size_w = merge_kernel_size[1] * patch_size
60
+
61
+ target_w = math.ceil(new_w / pad_size_w) * pad_size_w
62
+ target_h = math.ceil(new_h / pad_size_h) * pad_size_h
63
+
64
+ if target_w != new_w or target_h != new_h:
65
+ image = image.resize((target_w, target_h), Image.Resampling.BICUBIC)
66
+
67
+ w, h = image.size
68
+ if w // patch_size >= 512 or h // patch_size >= 512:
69
+ raise ValueError("Exceed pos emb")
70
+
71
+ return image
72
+
73
+ def to_tensor(self, image: Image.Image) -> torch.Tensor:
74
+ return TF.to_tensor(image.convert("RGB"))
75
+
76
+ def normalize(self, image: torch.Tensor) -> torch.Tensor:
77
+ return TF.normalize(image, self.image_mean, self.image_std)
78
+
79
+ def patchify(self, image: torch.Tensor) -> tuple[torch.Tensor, list[int, int]]:
80
+ patch_size = self.patch_size
81
+ C, H, W = image.shape
82
+ patches = image.reshape(C, H // patch_size, patch_size, W // patch_size, patch_size)
83
+ patches = patches.permute(1, 3, 0, 2, 4)
84
+ patches = patches.contiguous().view(-1, C, patch_size, patch_size)
85
+ grid_hw = (H // patch_size, W // patch_size)
86
+ return patches, grid_hw
87
+
88
+ def _preprocess(self, image: ImageInput) -> tuple[torch.Tensor, list[int, int]]:
89
+ """
90
+ Preprocess image and patchify it.
91
+ Args:
92
+ image (`ImageInput`):
93
+ Image to preprocess. Expects pixel values ranging from 0 to 255. If pixel values range from 0 to 1, set `do_rescale=False`.
94
+ Returns:
95
+ patches: torch.Tensor
96
+ grid_hw: list[int, int]
97
+ """
98
+ image = self.rescale(image, self.merge_kernel_size)
99
+ image = self.to_tensor(image)
100
+ image = self.normalize(image)
101
+ patches, grid_hw = self.patchify(image)
102
+ return patches, grid_hw
103
+
104
+ def preprocess(
105
+ self,
106
+ images: ImageInput,
107
+ return_tensors: Optional[Union[str, TensorType]] = None,
108
+ ) -> BatchFeature:
109
+ images = make_list_of_images(images)
110
+
111
+ if not valid_images(images):
112
+ raise ValueError(
113
+ "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
114
+ "torch.Tensor, tf.Tensor or jax.ndarray."
115
+ )
116
+
117
+ pixel_values, image_grid_hws = [], []
118
+ for image in images:
119
+ patches, image_grid_hw = self._preprocess(image)
120
+ pixel_values.append(patches)
121
+ image_grid_hws.append(image_grid_hw)
122
+ pixel_values = torch.concat(pixel_values, dim=0)
123
+ image_grid_hws = np.array(image_grid_hws)
124
+ data = {"pixel_values": pixel_values, "image_grid_hws": image_grid_hws}
125
+
126
+ return BatchFeature(data=data, tensor_type=return_tensors)
127
+
128
+ AutoImageProcessor.register("LocateAnythingImageProcessor", LocateAnythingImageProcessor)
kernel_utils/README.md ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # LA Flash Utils
2
+
3
+ This folder contains the sparse attention utilities used by
4
+ `LA_FLASH_ATTN=la_flash`. The release path is implemented with
5
+ FlashAttention varlen over LocateAnything range plans. It does not include or
6
+ build a local C++/CUDA extension.
7
+
8
+ ## Features
9
+
10
+ - Supports batched LocateAnything hybrid MTP inference on A100, RTX 4090, and H100.
11
+ - Consumes Magi-style `q_ranges`, `k_ranges`, `segment_offsets`, and
12
+ `attn_type_map` plans generated by `batch_utils.hybrid_runtime`.
13
+ - Uses FlashAttention varlen for packed causal/full plans.
14
+ - Packs LocateAnything MTP full-window key segments before calling
15
+ FlashAttention, avoiding dense `[B,H,Q,K]` masks.
16
+ - Supports log-sum-exp merging for compatible non-packed multi-segment plans.
17
+
18
+ ## Attention Types
19
+
20
+ The release path intentionally supports only FlashAttention-compatible plan
21
+ types:
22
+
23
+ | Value | Meaning |
24
+ | --- | --- |
25
+ | `0` | Full attention over the listed key segment or packed key segments. |
26
+ | `1` | Bottom-right causal attention. |
27
+
28
+ ## How It Works
29
+
30
+ `batch_utils.hybrid_runtime` builds sparse range plans for the text decoder.
31
+ Each plan describes which query token intervals attend to which key/value token
32
+ intervals. `kernel_utils.range_attention` executes those plans with
33
+ FlashAttention instead of materializing dense SDPA masks.
34
+
35
+ The runtime follows three paths:
36
+
37
+ - **Packed simple plans:** when each query range maps to one contiguous
38
+ key/value range, LA Flash flattens the selected ranges, builds FlashAttention
39
+ `cu_seqlens_q` / `cu_seqlens_k`, and calls `flash_attn_varlen_func` directly.
40
+ - **Packed MTP full-window plans:** for hybrid MTP decode, multiple full
41
+ key/value windows for the same query block are concatenated into one packed
42
+ key/value sequence before the FlashAttention call. This keeps the sparse
43
+ memory profile without constructing a `[B,H,Q,K]` attention mask.
44
+ - **Compatible multi-segment plans:** when a query range attends to multiple
45
+ segments that cannot be packed as one sequence, each segment is evaluated with
46
+ FlashAttention and the partial outputs are merged with the standard
47
+ log-sum-exp softmax composition.
48
+
49
+ The output tensor shape and dtype match the decoder attention output expected
50
+ by the model. This path is inference-oriented and depends on FlashAttention's
51
+ forward kernels; it is not a custom autograd training backend.
52
+
53
+ ## Runtime Knobs
54
+
55
+ | Variable | Default | Meaning |
56
+ | --- | --- | --- |
57
+ | `LA_FLASH_ATTN` | `sdpa` | Set to `la_flash` to enable this backend through `batch_utils`. |
58
+ | `LA_FLASH_FASTPATH` | `auto` | Use FlashAttention varlen for packed simple plans. |
59
+ | `LA_FLASH_SEGMENT_FASTPATH` | `auto` | Use FlashAttention varlen for multi-segment sparse plans. Full segments are packed first; other compatible segments use LSE merging. |
60
+ | `LA_FLASH_PLAN_STATS` | `0` | Record sparse plan statistics in inference summaries. |
61
+
62
+ ## Notes
63
+
64
+ Dense prefill and stock worker-style generation should keep
65
+ `LA_FLASH_DENSE_BACKEND=sdpa`; LA Flash is used for sparse range plans
66
+ produced by `batch_utils`.
67
+
68
+ This package is for inference and evaluation. Training remains on the
69
+ MagiAttention backend; the batched sparse-plan decode runtime does not support
70
+ the `labels` training path.
71
+
72
+ ## Source Layout
73
+
74
+ - `range_attention.py`: FlashAttention varlen dispatch, sparse KV packing, LSE
75
+ merge fallback, and availability checks.
kernel_utils/__init__.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ """FlashAttention sparse range utilities for LocateAnything batch inference."""
2
+
3
+ from .range_attention import range_attention, is_available
4
+
5
+ __all__ = ["range_attention", "is_available"]
kernel_utils/range_attention.py ADDED
@@ -0,0 +1,394 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Sparse LocateAnything attention implemented with FlashAttention varlen.
2
+
3
+ The public API accepts flattened query/key/value tensors:
4
+
5
+ q: [total_q, num_q_heads, head_dim]
6
+ k: [total_k, num_kv_heads, head_dim]
7
+ v: [total_k, num_kv_heads, head_dim]
8
+
9
+ and a Magi-style range plan:
10
+
11
+ q_ranges: [num_ranges, 2]
12
+ k_ranges: [num_key_segments, 2]
13
+ segment_offsets: [num_query_groups + 1]
14
+ attn_type_map:
15
+ 0 = full attention over the listed key segment(s)
16
+ 1 = bottom-right causal attention
17
+
18
+ For LocateAnything hybrid MTP decode, batch_utils represents the window as a
19
+ causal prefix plus full-attention sparse window segments. This module packs
20
+ those visible KV segments and calls FlashAttention varlen, avoiding dense masks.
21
+ """
22
+ from __future__ import annotations
23
+
24
+ import os
25
+ from typing import Optional
26
+
27
+ import torch
28
+
29
+
30
+ _FLASH_ATTN_VARLEN = None
31
+ _FLASH_ATTN_ERROR: Optional[BaseException] = None
32
+
33
+
34
+ def _env_enabled(name: str, default: str = "auto") -> bool:
35
+ value = os.environ.get(name, default).strip().lower()
36
+ return value in {"", "auto", "1", "on", "true", "yes", "force"}
37
+
38
+
39
+ def is_available() -> bool:
40
+ try:
41
+ _load_flash_attn_varlen()
42
+ return True
43
+ except Exception:
44
+ return False
45
+
46
+
47
+ def _flash_fastpath_enabled() -> bool:
48
+ return _env_enabled("LA_FLASH_FASTPATH", "auto")
49
+
50
+
51
+ def _flash_segment_fastpath_enabled() -> bool:
52
+ return _env_enabled("LA_FLASH_SEGMENT_FASTPATH", "auto")
53
+
54
+
55
+ def _load_flash_attn_varlen():
56
+ global _FLASH_ATTN_VARLEN, _FLASH_ATTN_ERROR
57
+ if _FLASH_ATTN_VARLEN is not None:
58
+ return _FLASH_ATTN_VARLEN
59
+ if _FLASH_ATTN_ERROR is not None:
60
+ raise _FLASH_ATTN_ERROR
61
+ try:
62
+ from flash_attn import flash_attn_varlen_func
63
+
64
+ _FLASH_ATTN_VARLEN = flash_attn_varlen_func
65
+ return _FLASH_ATTN_VARLEN
66
+ except BaseException as exc:
67
+ _FLASH_ATTN_ERROR = exc
68
+ raise
69
+
70
+
71
+ def _coalesce_query_groups(q_ranges, k_ranges, attn_type_map):
72
+ """Group consecutive entries that share the same query span and mask type."""
73
+ if q_ranges.numel() == 0:
74
+ segment_offsets = torch.zeros((1,), dtype=torch.int32, device=q_ranges.device)
75
+ return q_ranges, k_ranges, segment_offsets, attn_type_map, 0, 0
76
+
77
+ q_cpu = q_ranges.detach().to(device="cpu", dtype=torch.int32).contiguous()
78
+ t_cpu = attn_type_map.detach().to(device="cpu", dtype=torch.int32).contiguous()
79
+ grouped_q = []
80
+ grouped_t = []
81
+ offsets = [0]
82
+ max_q_len = 0
83
+ last_q = None
84
+ last_t = None
85
+ for idx, (qr, attn_type) in enumerate(zip(q_cpu.tolist(), t_cpu.tolist())):
86
+ key = (int(qr[0]), int(qr[1]))
87
+ attn_type = int(attn_type)
88
+ if attn_type not in (0, 1):
89
+ raise RuntimeError(
90
+ "LA Flash path only supports FlashAttention-compatible attn_type 0/1. "
91
+ f"Got attn_type={attn_type}; regenerate a type 0/1 range plan."
92
+ )
93
+ if last_q is None:
94
+ grouped_q.append([key[0], key[1]])
95
+ grouped_t.append(attn_type)
96
+ max_q_len = max(max_q_len, key[1] - key[0])
97
+ last_q = key
98
+ last_t = attn_type
99
+ continue
100
+ if key == last_q and attn_type == last_t:
101
+ continue
102
+ offsets.append(idx)
103
+ grouped_q.append([key[0], key[1]])
104
+ grouped_t.append(attn_type)
105
+ max_q_len = max(max_q_len, key[1] - key[0])
106
+ last_q = key
107
+ last_t = attn_type
108
+ offsets.append(int(q_ranges.shape[0]))
109
+
110
+ k_cpu = k_ranges.detach().to(device="cpu", dtype=torch.int32).contiguous()
111
+ max_k_len = max((int(end) - int(start) for start, end in k_cpu.tolist()), default=0)
112
+
113
+ return (
114
+ torch.tensor(grouped_q, dtype=torch.int32, device=q_ranges.device).contiguous(),
115
+ k_ranges,
116
+ torch.tensor(offsets, dtype=torch.int32, device=q_ranges.device).contiguous(),
117
+ torch.tensor(grouped_t, dtype=torch.int32, device=q_ranges.device).contiguous(),
118
+ int(max_q_len),
119
+ int(max_k_len),
120
+ )
121
+
122
+
123
+ def _flash_lse_to_tq_h(lse, total_q, q_lengths=None):
124
+ if lse is None:
125
+ return None
126
+ if lse.dim() != 2:
127
+ if lse.dim() == 3 and q_lengths is not None and lse.shape[0] == len(q_lengths):
128
+ chunks = []
129
+ for idx, q_len in enumerate(q_lengths):
130
+ q_len = int(q_len)
131
+ if lse.shape[1] == 0 or q_len > lse.shape[2]:
132
+ return None
133
+ chunks.append(lse[idx, :, :q_len].transpose(0, 1).contiguous())
134
+ merged = torch.cat(chunks, dim=0).float()
135
+ return merged if merged.shape[0] == total_q else None
136
+ return None
137
+ if lse.shape[0] == total_q:
138
+ return lse.float()
139
+ if lse.shape[1] == total_q:
140
+ return lse.transpose(0, 1).contiguous().float()
141
+ return None
142
+
143
+
144
+ def _make_cu_seqlens(lengths, device):
145
+ return torch.tensor([0] + list(torch.tensor(lengths).cumsum(0).tolist()), device=device, dtype=torch.int32)
146
+
147
+
148
+ def _try_flash_segment_merge(
149
+ q,
150
+ k,
151
+ v,
152
+ k_ranges,
153
+ segment_offsets,
154
+ group_q_ranges,
155
+ group_attn_type_map,
156
+ softmax_scale,
157
+ ):
158
+ if not _flash_segment_fastpath_enabled():
159
+ return None
160
+ if q.dtype not in (torch.float16, torch.bfloat16) or k.dtype != q.dtype or v.dtype != q.dtype:
161
+ return None
162
+ if group_q_ranges is None or segment_offsets is None or group_attn_type_map is None:
163
+ return None
164
+
165
+ flash_attn_varlen = _load_flash_attn_varlen()
166
+ gq_cpu = group_q_ranges.detach().to(device="cpu", dtype=torch.int32).contiguous()
167
+ kr_cpu = k_ranges.detach().to(device="cpu", dtype=torch.int32).contiguous()
168
+ seg_cpu = segment_offsets.detach().to(device="cpu", dtype=torch.int32).contiguous()
169
+ type_cpu = group_attn_type_map.detach().to(device="cpu", dtype=torch.int32).contiguous()
170
+
171
+ groups = []
172
+ max_segments = 0
173
+ for group_idx, (q_start, q_end) in enumerate(gq_cpu.tolist()):
174
+ attn_type = int(type_cpu[group_idx].item())
175
+ if attn_type not in (0, 1):
176
+ return None
177
+ seg_start = int(seg_cpu[group_idx].item())
178
+ seg_end = int(seg_cpu[group_idx + 1].item())
179
+ if seg_end <= seg_start or q_end <= q_start:
180
+ return None
181
+ segments = kr_cpu[seg_start:seg_end].tolist()
182
+ max_segments = max(max_segments, len(segments))
183
+ groups.append((int(q_start), int(q_end), attn_type, [(int(a), int(b)) for a, b in segments]))
184
+
185
+ if not groups or max_segments == 0:
186
+ return None
187
+
188
+ can_pack_full_groups = all(attn_type == 0 or len(segments) == 1 for _, _, attn_type, segments in groups)
189
+ if can_pack_full_groups:
190
+ merged = torch.empty((q.shape[0], q.shape[1], q.shape[2]), device=q.device, dtype=q.dtype)
191
+ covered = torch.zeros((q.shape[0],), device=q.device, dtype=torch.bool)
192
+ for attn_type in (0, 1):
193
+ q_slices = []
194
+ k_slices = []
195
+ v_slices = []
196
+ q_lengths = []
197
+ k_lengths = []
198
+ targets = []
199
+ for q_start, q_end, group_type, segments in groups:
200
+ if group_type != attn_type:
201
+ continue
202
+ q_slices.append(q[q_start:q_end])
203
+ if attn_type == 0 and len(segments) > 1:
204
+ k_slices.append(torch.cat([k[start:end] for start, end in segments], dim=0))
205
+ v_slices.append(torch.cat([v[start:end] for start, end in segments], dim=0))
206
+ k_lengths.append(sum(end - start for start, end in segments))
207
+ else:
208
+ k_start, k_end = segments[0]
209
+ k_slices.append(k[k_start:k_end])
210
+ v_slices.append(v[k_start:k_end])
211
+ k_lengths.append(k_end - k_start)
212
+ q_lengths.append(q_end - q_start)
213
+ targets.append((q_start, q_end))
214
+ if not q_slices:
215
+ continue
216
+
217
+ out_pass = flash_attn_varlen(
218
+ torch.cat(q_slices, dim=0).contiguous(),
219
+ torch.cat(k_slices, dim=0).contiguous(),
220
+ torch.cat(v_slices, dim=0).contiguous(),
221
+ _make_cu_seqlens(q_lengths, q.device),
222
+ _make_cu_seqlens(k_lengths, q.device),
223
+ int(max(q_lengths)),
224
+ int(max(k_lengths)),
225
+ dropout_p=0.0,
226
+ softmax_scale=float(softmax_scale),
227
+ causal=bool(attn_type == 1),
228
+ )
229
+ if isinstance(out_pass, tuple):
230
+ out_pass = out_pass[0]
231
+
232
+ cursor = 0
233
+ for q_start, q_end in targets:
234
+ q_len = q_end - q_start
235
+ merged[q_start:q_end] = out_pass[cursor:cursor + q_len]
236
+ covered[q_start:q_end] = True
237
+ cursor += q_len
238
+
239
+ if bool(covered.all().item()):
240
+ return merged
241
+
242
+ merged = torch.zeros((q.shape[0], q.shape[1], q.shape[2]), device=q.device, dtype=torch.float32)
243
+ merged_lse = torch.full((q.shape[0], q.shape[1]), -float("inf"), device=q.device, dtype=torch.float32)
244
+ covered = torch.zeros((q.shape[0],), device=q.device, dtype=torch.bool)
245
+
246
+ for segment_idx in range(max_segments):
247
+ for attn_type in (0, 1):
248
+ q_slices = []
249
+ k_slices = []
250
+ v_slices = []
251
+ q_lengths = []
252
+ k_lengths = []
253
+ targets = []
254
+ for q_start, q_end, group_type, segments in groups:
255
+ if group_type != attn_type or segment_idx >= len(segments):
256
+ continue
257
+ k_start, k_end = segments[segment_idx]
258
+ if k_end <= k_start:
259
+ continue
260
+ q_slices.append(q[q_start:q_end])
261
+ k_slices.append(k[k_start:k_end])
262
+ v_slices.append(v[k_start:k_end])
263
+ q_lengths.append(q_end - q_start)
264
+ k_lengths.append(k_end - k_start)
265
+ targets.append((q_start, q_end))
266
+ if not q_slices:
267
+ continue
268
+
269
+ result = flash_attn_varlen(
270
+ torch.cat(q_slices, dim=0).contiguous(),
271
+ torch.cat(k_slices, dim=0).contiguous(),
272
+ torch.cat(v_slices, dim=0).contiguous(),
273
+ _make_cu_seqlens(q_lengths, q.device),
274
+ _make_cu_seqlens(k_lengths, q.device),
275
+ int(max(q_lengths)),
276
+ int(max(k_lengths)),
277
+ dropout_p=0.0,
278
+ softmax_scale=float(softmax_scale),
279
+ causal=bool(attn_type == 1),
280
+ return_attn_probs=True,
281
+ )
282
+ if not isinstance(result, tuple) or len(result) < 2:
283
+ return None
284
+ out_pass = result[0]
285
+ lse_pass = _flash_lse_to_tq_h(result[1], out_pass.shape[0], q_lengths)
286
+ if lse_pass is None:
287
+ return None
288
+
289
+ cursor = 0
290
+ for q_start, q_end in targets:
291
+ q_len = q_end - q_start
292
+ out_seg = out_pass[cursor:cursor + q_len].float()
293
+ lse_seg = lse_pass[cursor:cursor + q_len]
294
+ old_lse = merged_lse[q_start:q_end]
295
+ new_lse = torch.maximum(old_lse, lse_seg)
296
+ old_w = torch.exp(old_lse - new_lse)
297
+ seg_w = torch.exp(lse_seg - new_lse)
298
+ denom = (old_w + seg_w).clamp_min(1e-20)
299
+ merged[q_start:q_end] = (
300
+ merged[q_start:q_end] * old_w.unsqueeze(-1)
301
+ + out_seg * seg_w.unsqueeze(-1)
302
+ ) / denom.unsqueeze(-1)
303
+ merged_lse[q_start:q_end] = new_lse + torch.log(denom)
304
+ covered[q_start:q_end] = True
305
+ cursor += q_len
306
+
307
+ if not bool(covered.all().item()):
308
+ return None
309
+ return merged.to(dtype=q.dtype)
310
+
311
+
312
+ def range_attention(
313
+ q,
314
+ k,
315
+ v,
316
+ q_ranges,
317
+ k_ranges,
318
+ attn_type_map,
319
+ softmax_scale: float,
320
+ *,
321
+ segment_offsets=None,
322
+ group_q_ranges=None,
323
+ group_attn_type_map=None,
324
+ max_q_len=None,
325
+ max_k_len=None,
326
+ flash_cu_seqlens_q=None,
327
+ flash_cu_seqlens_k=None,
328
+ flash_causal=None,
329
+ disjoint_q_ranges=None,
330
+ ):
331
+ """Run sparse range attention through FlashAttention varlen."""
332
+ del disjoint_q_ranges
333
+ if not q.is_cuda:
334
+ raise RuntimeError("LA Flash range_attention requires CUDA tensors")
335
+ if segment_offsets is None or group_q_ranges is None or group_attn_type_map is None:
336
+ (
337
+ group_q_ranges,
338
+ k_ranges,
339
+ segment_offsets,
340
+ group_attn_type_map,
341
+ computed_max_q_len,
342
+ computed_max_k_len,
343
+ ) = _coalesce_query_groups(q_ranges, k_ranges, attn_type_map)
344
+ if max_q_len is None:
345
+ max_q_len = computed_max_q_len
346
+ if max_k_len is None:
347
+ max_k_len = computed_max_k_len
348
+ elif max_q_len is None:
349
+ lengths = (group_q_ranges[:, 1] - group_q_ranges[:, 0]).detach().to(device="cpu")
350
+ max_q_len = int(lengths.max().item()) if lengths.numel() else 0
351
+ if max_k_len is None:
352
+ k_lengths = (k_ranges[:, 1] - k_ranges[:, 0]).detach().to(device="cpu")
353
+ max_k_len = int(k_lengths.max().item()) if k_lengths.numel() else 0
354
+
355
+ if (
356
+ flash_cu_seqlens_q is not None
357
+ and flash_cu_seqlens_k is not None
358
+ and flash_causal is not None
359
+ and _flash_fastpath_enabled()
360
+ and q.dtype in (torch.float16, torch.bfloat16)
361
+ and k.dtype == q.dtype
362
+ and v.dtype == q.dtype
363
+ ):
364
+ flash_attn_varlen = _load_flash_attn_varlen()
365
+ return flash_attn_varlen(
366
+ q.contiguous(),
367
+ k.contiguous(),
368
+ v.contiguous(),
369
+ flash_cu_seqlens_q.contiguous().to(device=q.device, dtype=torch.int32),
370
+ flash_cu_seqlens_k.contiguous().to(device=q.device, dtype=torch.int32),
371
+ int(max_q_len),
372
+ int(max_k_len),
373
+ dropout_p=0.0,
374
+ softmax_scale=float(softmax_scale),
375
+ causal=bool(flash_causal),
376
+ )
377
+
378
+ segment_out = _try_flash_segment_merge(
379
+ q,
380
+ k,
381
+ v,
382
+ k_ranges,
383
+ segment_offsets,
384
+ group_q_ranges,
385
+ group_attn_type_map,
386
+ softmax_scale,
387
+ )
388
+ if segment_out is not None:
389
+ return segment_out
390
+
391
+ raise RuntimeError(
392
+ "LA Flash could not express this range plan with FlashAttention varlen. "
393
+ "Only attn_type 0/1 range plans are supported in the release path."
394
+ )
mask_magi_utils.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved.
2
+ #
3
+ # NVIDIA CORPORATION and its licensors retain all intellectual property
4
+ # and proprietary rights in and to this software, related documentation
5
+ # and any modifications thereto. Any use, reproduction, disclosure or
6
+ # distribution of this software and related documentation without an express
7
+ # license agreement from NVIDIA CORPORATION is strictly prohibited.
8
+
9
+ import torch
10
+
11
+ # MagiAttention attn_type_map convention
12
+ FULL, CAUSAL = 0, 1
13
+
14
+ def build_magi_ranges(kv_len: int, q_len: int, block_size: int, ar_decode: bool=False, device: str = "cpu"):
15
+ """
16
+ Fixed strategy:
17
+ - use_cache=True: Mask blocked_k = (kv_len - block_size - 1) column
18
+ - causal_attn=False: Window interior is FULL (bidirectional)
19
+ - If q_len==kv_len: Use coarse prefix version (fewer ranges)
20
+ - Otherwise: General decode version (recompute rows expanding visible region row by row)
21
+
22
+ Conventions:
23
+ - K/V global length kv_len: [0, kv_len)
24
+ - Current Q is "last q_len tokens"
25
+ - First r=q_len-block_size rows are recomputed; last block_size rows are window
26
+ """
27
+ assert 0 < q_len <= kv_len
28
+
29
+ if ar_decode:
30
+ return {
31
+ "q_ranges": torch.tensor([[0, q_len]], dtype=torch.int32, device=device).contiguous(),
32
+ "k_ranges": torch.tensor([[0, kv_len]], dtype=torch.int32, device=device).contiguous(),
33
+ "attn_type_map": torch.tensor([CAUSAL], dtype=torch.int32, device=device).contiguous(),
34
+ }
35
+
36
+
37
+ assert 0 < block_size <= q_len <= kv_len
38
+ B = block_size
39
+ r = q_len - B
40
+ q_global_start = kv_len - q_len
41
+
42
+ window_start_k = kv_len - B
43
+ blocked_k = window_start_k - 1 # The column that is blocked
44
+
45
+ q_ranges, k_ranges, types = [], [], []
46
+
47
+ # -------- prefix (q_len == kv_len) coarse-grained --------
48
+ if q_len == kv_len:
49
+ prefix_len = window_start_k # kv_len - B
50
+
51
+ # prefix->prefix: causal
52
+ if prefix_len > 0:
53
+ q_ranges += [[0, prefix_len]]
54
+ k_ranges += [[0, prefix_len]]
55
+ types += [CAUSAL]
56
+
57
+ # window->prefix: full, but exclude blocked_k => keys [0, blocked_k)
58
+ if prefix_len > 0 and blocked_k > 0:
59
+ q_ranges += [[prefix_len, kv_len]]
60
+ k_ranges += [[0, blocked_k]]
61
+ types += [FULL]
62
+
63
+ # window->window: full
64
+ q_ranges += [[prefix_len, kv_len]]
65
+ k_ranges += [[prefix_len, kv_len]]
66
+ types += [FULL]
67
+
68
+ return {
69
+ "q_ranges": torch.tensor(q_ranges, dtype=torch.int32, device=device).contiguous(),
70
+ "k_ranges": torch.tensor(k_ranges, dtype=torch.int32, device=device).contiguous(),
71
+ "attn_type_map": torch.tensor(types, dtype=torch.int32, device=device).contiguous(),
72
+ }
73
+
74
+ # -------- decode / general (q_len < kv_len) --------
75
+
76
+ # A) Recomputed rows: expand visible key cutoff row by row (use FULL + single-row q_range for precise shape)
77
+ for i in range(r):
78
+ g = q_global_start + i
79
+ q_ranges.append([i, i + 1])
80
+ k_ranges.append([0, g + 1]) # Allow keys [0, g]
81
+ types.append(FULL)
82
+
83
+ # B) Window rows: allow prefix but block blocked_k; window interior is full
84
+ q_win = [r, q_len]
85
+
86
+ # prefix keys [0, blocked_k)
87
+ if blocked_k > 0:
88
+ q_ranges.append(q_win)
89
+ k_ranges.append([0, blocked_k])
90
+ types.append(FULL)
91
+
92
+ # window keys [window_start_k, kv_len)
93
+ q_ranges.append(q_win)
94
+ k_ranges.append([window_start_k, kv_len])
95
+ types.append(FULL)
96
+
97
+ return {
98
+ "q_ranges": torch.tensor(q_ranges, dtype=torch.int32, device=device).contiguous(),
99
+ "k_ranges": torch.tensor(k_ranges, dtype=torch.int32, device=device).contiguous(),
100
+ "attn_type_map": torch.tensor(types, dtype=torch.int32, device=device).contiguous(),
101
+ }
mask_sdpa_utils.py ADDED
@@ -0,0 +1,232 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved.
2
+ #
3
+ # NVIDIA CORPORATION and its licensors retain all intellectual property
4
+ # and proprietary rights in and to this software, related documentation
5
+ # and any modifications thereto. Any use, reproduction, disclosure or
6
+ # distribution of this software and related documentation without an express
7
+ # license agreement from NVIDIA CORPORATION is strictly prohibited.
8
+
9
+ import torch
10
+
11
+
12
+ def find_prefix_seq_length_by_pe(
13
+ pe: torch.Tensor
14
+ ) -> torch.Tensor:
15
+ """
16
+ Find the sequence length where position encoding drops (indicating prefix boundary).
17
+ Args:
18
+ pe: Position encoding tensor of shape [Batch size, Sequence length ]
19
+ Contains position indices for each token in the sequence.
20
+ Returns:
21
+ torch.Tensor: A tensor of shape [B] containing:
22
+ - The index where position encoding drops for each sequence
23
+ - -1 if no drop occurs in the sequence
24
+ """
25
+ batch_size, seq_len = pe.shape
26
+ prev = pe[:, :-1]
27
+ curr = pe[:, 1:]
28
+ drop_mask = curr < prev # [batch_size, seq_len-1]
29
+
30
+ seq_len = torch.full((batch_size,), -1, dtype=torch.long)
31
+
32
+ for b in range(batch_size):
33
+ drop_pos = torch.nonzero(drop_mask[b], as_tuple=False)
34
+ if drop_pos.numel() > 0:
35
+ i = drop_pos[0].item() + 1 # Take first drop position (+1 because we compared shifted sequences)
36
+ seq_len[b] = i
37
+
38
+ return seq_len
39
+
40
+
41
+
42
+ def update_causal_mask_with_pad_non_visible_2d(
43
+ input_ids: torch.Tensor,
44
+ attn_mask_2d: torch.Tensor,
45
+ text_mask_token_id: int,
46
+ block_size: int = 4,
47
+ causal_attn: bool = False
48
+ ) -> torch.Tensor:
49
+ """
50
+ Updates a 2D attention mask for hole sequence through input_ids and text_mask_token_id
51
+
52
+ Args:
53
+ input_ids: Input token IDs (unused in current implementation)
54
+ attn_mask_2d: 2D attention mask matrix of shape [seq_len, seq_len] where:
55
+ - 0.0 indicates allowed attention
56
+ - -inf indicates masked attention
57
+ text_mask_token_id: ID representing masked tokens
58
+ block_size: Size of the diffusion window
59
+ causal_attn: If True, maintains strict causal masking throughout
60
+
61
+ Returns:
62
+ Modified attention mask with updated visibility patterns
63
+ """
64
+ seq_len = input_ids.shape[0]
65
+ device = input_ids.device
66
+
67
+ # Identify masked tokens and their preceding positions
68
+ input_mask = input_ids.eq(text_mask_token_id)
69
+ input_before_mask = torch.zeros_like(input_mask)
70
+ input_before_mask[:-1] = input_mask[1:]
71
+ mask_cols = (input_mask | input_before_mask)
72
+ non_mask = ~mask_cols
73
+
74
+ rows = torch.arange(seq_len, device=device)[:, None]
75
+ cols = torch.arange(seq_len, device=device)
76
+
77
+ indices = torch.arange(seq_len, device=device)
78
+ prev_non_mask = (indices * non_mask).cummax(dim=0).values
79
+
80
+ max_value = torch.iinfo(indices.dtype).max
81
+ mask_indices = torch.where(non_mask, indices, torch.full_like(indices, max_value))
82
+ reversed_mask_indices = torch.flip(mask_indices, dims=[0])
83
+ reversed_cummin = reversed_mask_indices.cummin(dim=0).values
84
+ next_non_mask = torch.flip(reversed_cummin, dims=[0])
85
+
86
+ infra_mask = (
87
+ (cols > prev_non_mask) &
88
+ (rows >= next_non_mask[None, :]) &
89
+ mask_cols[None, :]
90
+ )
91
+ attn_mask_2d.masked_fill_(infra_mask, -float('inf'))
92
+
93
+ if not causal_attn:
94
+ visible_mask = (
95
+ (rows > prev_non_mask[None, :]) &
96
+ (rows < cols) &
97
+ mask_cols[None, :]
98
+ )
99
+ attn_mask_2d.masked_fill_(visible_mask, 0.0)
100
+
101
+ return attn_mask_2d
102
+
103
+
104
+ def update_causal_mask_for_one_gen_window_2d(
105
+ input_ids: torch.Tensor,
106
+ attn_mask_2d: torch.Tensor,
107
+ block_size: int = 4,
108
+ use_cache: bool = True,
109
+ causal_attn: bool = False
110
+ ) -> torch.Tensor:
111
+ """
112
+ Updates a 2D attention mask for a diffusion window in transformer inference.
113
+
114
+ Args:
115
+ input_ids: Input token IDs (unused in current implementation)
116
+ attn_mask_2d: 2D attention mask matrix of shape [seq_len, seq_len] where:
117
+ - 0.0 indicates allowed attention
118
+ - -inf indicates masked attention
119
+ block_size: Size of the diffusion window
120
+ use_cache: Whether key-value cache is being used
121
+ causal_attn: If True, maintains strict causal masking throughout
122
+
123
+ Returns:
124
+ Modified attention mask with updated visibility patterns
125
+ """
126
+
127
+ if not causal_attn:
128
+ # Make the diffusion window (last block_size tokens) fully visible to itself
129
+ # This allows bidirectional attention within the diffusion window
130
+ attn_mask_2d[-block_size:, -block_size:] = 0.0
131
+ if use_cache:
132
+ # Mask the last token from previous round to prevent recomputation and maintain generation consistency.
133
+ attn_mask_2d[-block_size:, -block_size-1] = -float('inf')
134
+
135
+ return attn_mask_2d
136
+
137
+
138
+ def create_block_diff_mask_by_pe_4d(
139
+ block_size: int,
140
+ x0_len_list: torch.Tensor,
141
+ position_ids: torch.Tensor,
142
+ causal_attn: bool = False
143
+ ) -> tuple[torch.Tensor, torch.Tensor]:
144
+ """Generates a 4D attention mask for block-difference attention patterns.
145
+
146
+ The mask consists of three regions:
147
+ 1. Causal block (top-left): Standard causal attention for `x0` tokens.
148
+ 2. Mutual block (bottom-right): Non-causal attention within the same block for non-`x0` tokens.
149
+ 3. Prefix block (bottom-left): Non-`x0` tokens can attend to a prefix of `x0` tokens.
150
+
151
+ Args:
152
+ block_size (int): Size of processing blocks for non-`x0` tokens.
153
+ x0_len_list (torch.Tensor): Tensor of shape [B] containing lengths of `x0` segments per batch.
154
+ position_ids (torch.Tensor): Tensor of shape [B, seq_len] containing position IDs.
155
+ causal_attn (bool, optional): If True, enforces causal masking in mutual blocks. Defaults to False.
156
+
157
+ Returns:
158
+ tuple[torch.Tensor, torch.Tensor]:
159
+ - A float mask of shape [batch_size, 1, seq_len, seq_len] with `-inf` for masked positions (non visiable).
160
+ - A boolean mask of shape [batch_size, 1, seq_len, seq_len] indicating allowed attention positions.
161
+ """
162
+ batch_size, seq_len = position_ids.shape
163
+ device = position_ids.device
164
+
165
+ # Create position indices [batch_size, seq_len, seq_len]
166
+ q_idx = torch.arange(seq_len, device=device).view(1, seq_len, 1) # [1, seq_len, 1]
167
+ kv_idx = torch.arange(seq_len, device=device).view(1, 1, seq_len) # [1, 1, seq_len]
168
+
169
+ # Broadcast to [B, seq_len, seq_len]
170
+ x0_len = x0_len_list.view(batch_size, 1, 1) # [batch_size, 1, 1]
171
+ x0_flag_q = q_idx < x0_len # [batch_size, seq_len, seq_len]
172
+ x0_flag_kv = kv_idx < x0_len
173
+
174
+ # Block indices calculation [batch_size, seq_len, seq_len]
175
+ q_block_idx = (q_idx - x0_len) // block_size
176
+ kv_block_idx = (kv_idx - x0_len) // block_size
177
+
178
+ # causal block (top-left)
179
+ block_causal = x0_flag_q & x0_flag_kv & (q_idx >= kv_idx)
180
+
181
+ mutual_condition = (q_idx >= kv_idx) if causal_attn else torch.ones_like(q_idx, dtype=torch.bool)
182
+ block_mutual = (
183
+ ~x0_flag_q & ~x0_flag_kv &
184
+ (q_block_idx == kv_block_idx) &
185
+ mutual_condition
186
+ )
187
+
188
+ q_blk = torch.div(q_idx - x0_len, block_size, rounding_mode='floor')
189
+ q_blk_start = (x0_len_list.view(batch_size, 1) + q_blk[:, :, 0] * block_size).clamp(min=0, max=seq_len - 1)
190
+ prefix_len = position_ids.gather(1, q_blk_start)
191
+ prefix_len = prefix_len.unsqueeze(2)
192
+ block_prefix = (~x0_flag_q & x0_flag_kv) & (kv_idx < prefix_len)
193
+
194
+ final_mask = (block_causal | block_mutual | block_prefix)
195
+ customized_mask = torch.full_like(final_mask, float('-inf'), dtype=torch.bfloat16)
196
+ customized_mask.masked_fill_(final_mask, 0.0)
197
+
198
+ return customized_mask.unsqueeze(1).to(device=device), final_mask.unsqueeze(1).to(device=device)
199
+
200
+
201
+ def find_pred_pos_from_input_ids(
202
+ input_ids: torch.LongTensor = None,
203
+ text_mask_token_id: int = None,
204
+ ) -> torch.Tensor:
205
+ """Compute the relative prediction positions for masked tokens in a sequence.
206
+
207
+ For non-masked positions, the output is 0. For masked positions, the value increments
208
+ by 1 for each consecutive mask token, indicating how many steps ahead the prediction is.
209
+
210
+ Args:
211
+ input_ids (torch.LongTensor): Input token IDs of shape [batch_size, seq_len].
212
+ text_mask_token_id (int, optional): Token ID representing masked positions. Defaults to 151666.
213
+
214
+ Returns:
215
+ torch.Tensor: A tensor of shape [batch_size, seq_len] where:
216
+ - 0 indicates a non-masked token.
217
+ - n > 0 indicates the nth consecutive masked token (e.g., 1 = first mask, 2 = second mask, etc.).
218
+ """
219
+ batch_size, seq_len = input_ids.shape
220
+ device = input_ids.device
221
+
222
+ is_mask = (input_ids == text_mask_token_id)
223
+
224
+ base_mask = torch.zeros((batch_size, seq_len), dtype=torch.int8, device=device)
225
+
226
+ for b in range(batch_size):
227
+ for ix in range(1, seq_len):
228
+ if is_mask[b][ix] == True:
229
+ # Increment counter if current token is masked
230
+ base_mask[b][ix] = base_mask[b][ix-1] + 1
231
+
232
+ return base_mask
merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
model-00001-of-00002.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:923cfc10fed19808067da6df85a9a4220ddc1f9eb91ceee94c0fecd05d0f2d58
3
+ size 4959632160
model-00002-of-00002.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3459ba101f40594f3f62d3312014f1f8378b4ba3da3b1d562480045938fc7d47
3
+ size 2701795216
model.safetensors.index.json ADDED
@@ -0,0 +1,777 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "metadata": {
3
+ "total_size": 7661331936
4
+ },
5
+ "weight_map": {
6
+ "language_model.lm_head.weight": "model-00002-of-00002.safetensors",
7
+ "language_model.model.embed_tokens.weight": "model-00001-of-00002.safetensors",
8
+ "language_model.model.layers.0.input_layernorm.weight": "model-00001-of-00002.safetensors",
9
+ "language_model.model.layers.0.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
10
+ "language_model.model.layers.0.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
11
+ "language_model.model.layers.0.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
12
+ "language_model.model.layers.0.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
13
+ "language_model.model.layers.0.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
14
+ "language_model.model.layers.0.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
15
+ "language_model.model.layers.0.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
16
+ "language_model.model.layers.0.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
17
+ "language_model.model.layers.0.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
18
+ "language_model.model.layers.0.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
19
+ "language_model.model.layers.0.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
20
+ "language_model.model.layers.1.input_layernorm.weight": "model-00001-of-00002.safetensors",
21
+ "language_model.model.layers.1.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
22
+ "language_model.model.layers.1.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
23
+ "language_model.model.layers.1.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
24
+ "language_model.model.layers.1.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
25
+ "language_model.model.layers.1.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
26
+ "language_model.model.layers.1.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
27
+ "language_model.model.layers.1.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
28
+ "language_model.model.layers.1.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
29
+ "language_model.model.layers.1.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
30
+ "language_model.model.layers.1.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
31
+ "language_model.model.layers.1.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
32
+ "language_model.model.layers.10.input_layernorm.weight": "model-00001-of-00002.safetensors",
33
+ "language_model.model.layers.10.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
34
+ "language_model.model.layers.10.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
35
+ "language_model.model.layers.10.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
36
+ "language_model.model.layers.10.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
37
+ "language_model.model.layers.10.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
38
+ "language_model.model.layers.10.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
39
+ "language_model.model.layers.10.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
40
+ "language_model.model.layers.10.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
41
+ "language_model.model.layers.10.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
42
+ "language_model.model.layers.10.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
43
+ "language_model.model.layers.10.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
44
+ "language_model.model.layers.11.input_layernorm.weight": "model-00001-of-00002.safetensors",
45
+ "language_model.model.layers.11.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
46
+ "language_model.model.layers.11.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
47
+ "language_model.model.layers.11.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
48
+ "language_model.model.layers.11.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
49
+ "language_model.model.layers.11.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
50
+ "language_model.model.layers.11.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
51
+ "language_model.model.layers.11.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
52
+ "language_model.model.layers.11.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
53
+ "language_model.model.layers.11.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
54
+ "language_model.model.layers.11.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
55
+ "language_model.model.layers.11.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
56
+ "language_model.model.layers.12.input_layernorm.weight": "model-00001-of-00002.safetensors",
57
+ "language_model.model.layers.12.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
58
+ "language_model.model.layers.12.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
59
+ "language_model.model.layers.12.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
60
+ "language_model.model.layers.12.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
61
+ "language_model.model.layers.12.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
62
+ "language_model.model.layers.12.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
63
+ "language_model.model.layers.12.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
64
+ "language_model.model.layers.12.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
65
+ "language_model.model.layers.12.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
66
+ "language_model.model.layers.12.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
67
+ "language_model.model.layers.12.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
68
+ "language_model.model.layers.13.input_layernorm.weight": "model-00001-of-00002.safetensors",
69
+ "language_model.model.layers.13.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
70
+ "language_model.model.layers.13.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
71
+ "language_model.model.layers.13.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
72
+ "language_model.model.layers.13.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
73
+ "language_model.model.layers.13.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
74
+ "language_model.model.layers.13.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
75
+ "language_model.model.layers.13.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
76
+ "language_model.model.layers.13.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
77
+ "language_model.model.layers.13.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
78
+ "language_model.model.layers.13.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
79
+ "language_model.model.layers.13.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
80
+ "language_model.model.layers.14.input_layernorm.weight": "model-00001-of-00002.safetensors",
81
+ "language_model.model.layers.14.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
82
+ "language_model.model.layers.14.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
83
+ "language_model.model.layers.14.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
84
+ "language_model.model.layers.14.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
85
+ "language_model.model.layers.14.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
86
+ "language_model.model.layers.14.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
87
+ "language_model.model.layers.14.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
88
+ "language_model.model.layers.14.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
89
+ "language_model.model.layers.14.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
90
+ "language_model.model.layers.14.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
91
+ "language_model.model.layers.14.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
92
+ "language_model.model.layers.15.input_layernorm.weight": "model-00001-of-00002.safetensors",
93
+ "language_model.model.layers.15.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
94
+ "language_model.model.layers.15.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
95
+ "language_model.model.layers.15.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
96
+ "language_model.model.layers.15.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
97
+ "language_model.model.layers.15.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
98
+ "language_model.model.layers.15.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
99
+ "language_model.model.layers.15.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
100
+ "language_model.model.layers.15.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
101
+ "language_model.model.layers.15.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
102
+ "language_model.model.layers.15.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
103
+ "language_model.model.layers.15.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
104
+ "language_model.model.layers.16.input_layernorm.weight": "model-00001-of-00002.safetensors",
105
+ "language_model.model.layers.16.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
106
+ "language_model.model.layers.16.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
107
+ "language_model.model.layers.16.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
108
+ "language_model.model.layers.16.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
109
+ "language_model.model.layers.16.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
110
+ "language_model.model.layers.16.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
111
+ "language_model.model.layers.16.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
112
+ "language_model.model.layers.16.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
113
+ "language_model.model.layers.16.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
114
+ "language_model.model.layers.16.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
115
+ "language_model.model.layers.16.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
116
+ "language_model.model.layers.17.input_layernorm.weight": "model-00001-of-00002.safetensors",
117
+ "language_model.model.layers.17.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
118
+ "language_model.model.layers.17.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
119
+ "language_model.model.layers.17.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
120
+ "language_model.model.layers.17.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
121
+ "language_model.model.layers.17.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
122
+ "language_model.model.layers.17.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
123
+ "language_model.model.layers.17.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
124
+ "language_model.model.layers.17.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
125
+ "language_model.model.layers.17.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
126
+ "language_model.model.layers.17.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
127
+ "language_model.model.layers.17.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
128
+ "language_model.model.layers.18.input_layernorm.weight": "model-00001-of-00002.safetensors",
129
+ "language_model.model.layers.18.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
130
+ "language_model.model.layers.18.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
131
+ "language_model.model.layers.18.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
132
+ "language_model.model.layers.18.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
133
+ "language_model.model.layers.18.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
134
+ "language_model.model.layers.18.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
135
+ "language_model.model.layers.18.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
136
+ "language_model.model.layers.18.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
137
+ "language_model.model.layers.18.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
138
+ "language_model.model.layers.18.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
139
+ "language_model.model.layers.18.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
140
+ "language_model.model.layers.19.input_layernorm.weight": "model-00001-of-00002.safetensors",
141
+ "language_model.model.layers.19.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
142
+ "language_model.model.layers.19.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
143
+ "language_model.model.layers.19.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
144
+ "language_model.model.layers.19.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
145
+ "language_model.model.layers.19.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
146
+ "language_model.model.layers.19.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
147
+ "language_model.model.layers.19.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
148
+ "language_model.model.layers.19.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
149
+ "language_model.model.layers.19.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
150
+ "language_model.model.layers.19.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
151
+ "language_model.model.layers.19.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
152
+ "language_model.model.layers.2.input_layernorm.weight": "model-00001-of-00002.safetensors",
153
+ "language_model.model.layers.2.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
154
+ "language_model.model.layers.2.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
155
+ "language_model.model.layers.2.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
156
+ "language_model.model.layers.2.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
157
+ "language_model.model.layers.2.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
158
+ "language_model.model.layers.2.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
159
+ "language_model.model.layers.2.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
160
+ "language_model.model.layers.2.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
161
+ "language_model.model.layers.2.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
162
+ "language_model.model.layers.2.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
163
+ "language_model.model.layers.2.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
164
+ "language_model.model.layers.20.input_layernorm.weight": "model-00001-of-00002.safetensors",
165
+ "language_model.model.layers.20.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
166
+ "language_model.model.layers.20.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
167
+ "language_model.model.layers.20.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
168
+ "language_model.model.layers.20.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
169
+ "language_model.model.layers.20.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
170
+ "language_model.model.layers.20.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
171
+ "language_model.model.layers.20.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
172
+ "language_model.model.layers.20.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
173
+ "language_model.model.layers.20.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
174
+ "language_model.model.layers.20.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
175
+ "language_model.model.layers.20.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
176
+ "language_model.model.layers.21.input_layernorm.weight": "model-00001-of-00002.safetensors",
177
+ "language_model.model.layers.21.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
178
+ "language_model.model.layers.21.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
179
+ "language_model.model.layers.21.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
180
+ "language_model.model.layers.21.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
181
+ "language_model.model.layers.21.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
182
+ "language_model.model.layers.21.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
183
+ "language_model.model.layers.21.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
184
+ "language_model.model.layers.21.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
185
+ "language_model.model.layers.21.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
186
+ "language_model.model.layers.21.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
187
+ "language_model.model.layers.21.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
188
+ "language_model.model.layers.22.input_layernorm.weight": "model-00002-of-00002.safetensors",
189
+ "language_model.model.layers.22.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
190
+ "language_model.model.layers.22.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
191
+ "language_model.model.layers.22.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
192
+ "language_model.model.layers.22.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
193
+ "language_model.model.layers.22.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
194
+ "language_model.model.layers.22.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
195
+ "language_model.model.layers.22.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
196
+ "language_model.model.layers.22.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
197
+ "language_model.model.layers.22.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
198
+ "language_model.model.layers.22.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
199
+ "language_model.model.layers.22.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
200
+ "language_model.model.layers.23.input_layernorm.weight": "model-00002-of-00002.safetensors",
201
+ "language_model.model.layers.23.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
202
+ "language_model.model.layers.23.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
203
+ "language_model.model.layers.23.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
204
+ "language_model.model.layers.23.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
205
+ "language_model.model.layers.23.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
206
+ "language_model.model.layers.23.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
207
+ "language_model.model.layers.23.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
208
+ "language_model.model.layers.23.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
209
+ "language_model.model.layers.23.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
210
+ "language_model.model.layers.23.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
211
+ "language_model.model.layers.23.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
212
+ "language_model.model.layers.24.input_layernorm.weight": "model-00002-of-00002.safetensors",
213
+ "language_model.model.layers.24.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
214
+ "language_model.model.layers.24.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
215
+ "language_model.model.layers.24.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
216
+ "language_model.model.layers.24.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
217
+ "language_model.model.layers.24.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
218
+ "language_model.model.layers.24.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
219
+ "language_model.model.layers.24.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
220
+ "language_model.model.layers.24.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
221
+ "language_model.model.layers.24.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
222
+ "language_model.model.layers.24.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
223
+ "language_model.model.layers.24.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
224
+ "language_model.model.layers.25.input_layernorm.weight": "model-00002-of-00002.safetensors",
225
+ "language_model.model.layers.25.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
226
+ "language_model.model.layers.25.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
227
+ "language_model.model.layers.25.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
228
+ "language_model.model.layers.25.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
229
+ "language_model.model.layers.25.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
230
+ "language_model.model.layers.25.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
231
+ "language_model.model.layers.25.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
232
+ "language_model.model.layers.25.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
233
+ "language_model.model.layers.25.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
234
+ "language_model.model.layers.25.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
235
+ "language_model.model.layers.25.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
236
+ "language_model.model.layers.26.input_layernorm.weight": "model-00002-of-00002.safetensors",
237
+ "language_model.model.layers.26.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
238
+ "language_model.model.layers.26.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
239
+ "language_model.model.layers.26.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
240
+ "language_model.model.layers.26.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
241
+ "language_model.model.layers.26.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
242
+ "language_model.model.layers.26.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
243
+ "language_model.model.layers.26.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
244
+ "language_model.model.layers.26.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
245
+ "language_model.model.layers.26.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
246
+ "language_model.model.layers.26.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
247
+ "language_model.model.layers.26.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
248
+ "language_model.model.layers.27.input_layernorm.weight": "model-00002-of-00002.safetensors",
249
+ "language_model.model.layers.27.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
250
+ "language_model.model.layers.27.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
251
+ "language_model.model.layers.27.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
252
+ "language_model.model.layers.27.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
253
+ "language_model.model.layers.27.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
254
+ "language_model.model.layers.27.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
255
+ "language_model.model.layers.27.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
256
+ "language_model.model.layers.27.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
257
+ "language_model.model.layers.27.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
258
+ "language_model.model.layers.27.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
259
+ "language_model.model.layers.27.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
260
+ "language_model.model.layers.28.input_layernorm.weight": "model-00002-of-00002.safetensors",
261
+ "language_model.model.layers.28.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
262
+ "language_model.model.layers.28.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
263
+ "language_model.model.layers.28.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
264
+ "language_model.model.layers.28.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
265
+ "language_model.model.layers.28.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
266
+ "language_model.model.layers.28.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
267
+ "language_model.model.layers.28.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
268
+ "language_model.model.layers.28.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
269
+ "language_model.model.layers.28.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
270
+ "language_model.model.layers.28.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
271
+ "language_model.model.layers.28.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
272
+ "language_model.model.layers.29.input_layernorm.weight": "model-00002-of-00002.safetensors",
273
+ "language_model.model.layers.29.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
274
+ "language_model.model.layers.29.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
275
+ "language_model.model.layers.29.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
276
+ "language_model.model.layers.29.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
277
+ "language_model.model.layers.29.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
278
+ "language_model.model.layers.29.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
279
+ "language_model.model.layers.29.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
280
+ "language_model.model.layers.29.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
281
+ "language_model.model.layers.29.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
282
+ "language_model.model.layers.29.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
283
+ "language_model.model.layers.29.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
284
+ "language_model.model.layers.3.input_layernorm.weight": "model-00001-of-00002.safetensors",
285
+ "language_model.model.layers.3.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
286
+ "language_model.model.layers.3.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
287
+ "language_model.model.layers.3.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
288
+ "language_model.model.layers.3.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
289
+ "language_model.model.layers.3.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
290
+ "language_model.model.layers.3.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
291
+ "language_model.model.layers.3.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
292
+ "language_model.model.layers.3.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
293
+ "language_model.model.layers.3.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
294
+ "language_model.model.layers.3.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
295
+ "language_model.model.layers.3.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
296
+ "language_model.model.layers.30.input_layernorm.weight": "model-00002-of-00002.safetensors",
297
+ "language_model.model.layers.30.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
298
+ "language_model.model.layers.30.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
299
+ "language_model.model.layers.30.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
300
+ "language_model.model.layers.30.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
301
+ "language_model.model.layers.30.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
302
+ "language_model.model.layers.30.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
303
+ "language_model.model.layers.30.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
304
+ "language_model.model.layers.30.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
305
+ "language_model.model.layers.30.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
306
+ "language_model.model.layers.30.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
307
+ "language_model.model.layers.30.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
308
+ "language_model.model.layers.31.input_layernorm.weight": "model-00002-of-00002.safetensors",
309
+ "language_model.model.layers.31.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
310
+ "language_model.model.layers.31.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
311
+ "language_model.model.layers.31.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
312
+ "language_model.model.layers.31.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
313
+ "language_model.model.layers.31.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
314
+ "language_model.model.layers.31.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
315
+ "language_model.model.layers.31.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
316
+ "language_model.model.layers.31.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
317
+ "language_model.model.layers.31.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
318
+ "language_model.model.layers.31.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
319
+ "language_model.model.layers.31.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
320
+ "language_model.model.layers.32.input_layernorm.weight": "model-00002-of-00002.safetensors",
321
+ "language_model.model.layers.32.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
322
+ "language_model.model.layers.32.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
323
+ "language_model.model.layers.32.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
324
+ "language_model.model.layers.32.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
325
+ "language_model.model.layers.32.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
326
+ "language_model.model.layers.32.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
327
+ "language_model.model.layers.32.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
328
+ "language_model.model.layers.32.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
329
+ "language_model.model.layers.32.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
330
+ "language_model.model.layers.32.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
331
+ "language_model.model.layers.32.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
332
+ "language_model.model.layers.33.input_layernorm.weight": "model-00002-of-00002.safetensors",
333
+ "language_model.model.layers.33.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
334
+ "language_model.model.layers.33.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
335
+ "language_model.model.layers.33.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
336
+ "language_model.model.layers.33.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
337
+ "language_model.model.layers.33.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
338
+ "language_model.model.layers.33.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
339
+ "language_model.model.layers.33.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
340
+ "language_model.model.layers.33.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
341
+ "language_model.model.layers.33.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
342
+ "language_model.model.layers.33.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
343
+ "language_model.model.layers.33.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
344
+ "language_model.model.layers.34.input_layernorm.weight": "model-00002-of-00002.safetensors",
345
+ "language_model.model.layers.34.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
346
+ "language_model.model.layers.34.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
347
+ "language_model.model.layers.34.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
348
+ "language_model.model.layers.34.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
349
+ "language_model.model.layers.34.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
350
+ "language_model.model.layers.34.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
351
+ "language_model.model.layers.34.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
352
+ "language_model.model.layers.34.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
353
+ "language_model.model.layers.34.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
354
+ "language_model.model.layers.34.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
355
+ "language_model.model.layers.34.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
356
+ "language_model.model.layers.35.input_layernorm.weight": "model-00002-of-00002.safetensors",
357
+ "language_model.model.layers.35.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
358
+ "language_model.model.layers.35.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
359
+ "language_model.model.layers.35.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
360
+ "language_model.model.layers.35.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
361
+ "language_model.model.layers.35.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
362
+ "language_model.model.layers.35.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
363
+ "language_model.model.layers.35.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
364
+ "language_model.model.layers.35.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
365
+ "language_model.model.layers.35.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
366
+ "language_model.model.layers.35.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
367
+ "language_model.model.layers.35.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
368
+ "language_model.model.layers.4.input_layernorm.weight": "model-00001-of-00002.safetensors",
369
+ "language_model.model.layers.4.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
370
+ "language_model.model.layers.4.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
371
+ "language_model.model.layers.4.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
372
+ "language_model.model.layers.4.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
373
+ "language_model.model.layers.4.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
374
+ "language_model.model.layers.4.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
375
+ "language_model.model.layers.4.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
376
+ "language_model.model.layers.4.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
377
+ "language_model.model.layers.4.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
378
+ "language_model.model.layers.4.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
379
+ "language_model.model.layers.4.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
380
+ "language_model.model.layers.5.input_layernorm.weight": "model-00001-of-00002.safetensors",
381
+ "language_model.model.layers.5.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
382
+ "language_model.model.layers.5.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
383
+ "language_model.model.layers.5.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
384
+ "language_model.model.layers.5.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
385
+ "language_model.model.layers.5.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
386
+ "language_model.model.layers.5.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
387
+ "language_model.model.layers.5.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
388
+ "language_model.model.layers.5.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
389
+ "language_model.model.layers.5.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
390
+ "language_model.model.layers.5.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
391
+ "language_model.model.layers.5.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
392
+ "language_model.model.layers.6.input_layernorm.weight": "model-00001-of-00002.safetensors",
393
+ "language_model.model.layers.6.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
394
+ "language_model.model.layers.6.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
395
+ "language_model.model.layers.6.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
396
+ "language_model.model.layers.6.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
397
+ "language_model.model.layers.6.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
398
+ "language_model.model.layers.6.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
399
+ "language_model.model.layers.6.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
400
+ "language_model.model.layers.6.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
401
+ "language_model.model.layers.6.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
402
+ "language_model.model.layers.6.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
403
+ "language_model.model.layers.6.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
404
+ "language_model.model.layers.7.input_layernorm.weight": "model-00001-of-00002.safetensors",
405
+ "language_model.model.layers.7.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
406
+ "language_model.model.layers.7.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
407
+ "language_model.model.layers.7.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
408
+ "language_model.model.layers.7.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
409
+ "language_model.model.layers.7.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
410
+ "language_model.model.layers.7.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
411
+ "language_model.model.layers.7.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
412
+ "language_model.model.layers.7.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
413
+ "language_model.model.layers.7.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
414
+ "language_model.model.layers.7.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
415
+ "language_model.model.layers.7.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
416
+ "language_model.model.layers.8.input_layernorm.weight": "model-00001-of-00002.safetensors",
417
+ "language_model.model.layers.8.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
418
+ "language_model.model.layers.8.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
419
+ "language_model.model.layers.8.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
420
+ "language_model.model.layers.8.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
421
+ "language_model.model.layers.8.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
422
+ "language_model.model.layers.8.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
423
+ "language_model.model.layers.8.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
424
+ "language_model.model.layers.8.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
425
+ "language_model.model.layers.8.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
426
+ "language_model.model.layers.8.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
427
+ "language_model.model.layers.8.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
428
+ "language_model.model.layers.9.input_layernorm.weight": "model-00001-of-00002.safetensors",
429
+ "language_model.model.layers.9.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
430
+ "language_model.model.layers.9.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
431
+ "language_model.model.layers.9.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
432
+ "language_model.model.layers.9.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
433
+ "language_model.model.layers.9.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
434
+ "language_model.model.layers.9.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
435
+ "language_model.model.layers.9.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
436
+ "language_model.model.layers.9.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
437
+ "language_model.model.layers.9.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
438
+ "language_model.model.layers.9.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
439
+ "language_model.model.layers.9.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
440
+ "language_model.model.norm.weight": "model-00002-of-00002.safetensors",
441
+ "mlp1.0.bias": "model-00002-of-00002.safetensors",
442
+ "mlp1.0.weight": "model-00002-of-00002.safetensors",
443
+ "mlp1.1.bias": "model-00002-of-00002.safetensors",
444
+ "mlp1.1.weight": "model-00002-of-00002.safetensors",
445
+ "mlp1.3.bias": "model-00002-of-00002.safetensors",
446
+ "mlp1.3.weight": "model-00002-of-00002.safetensors",
447
+ "vision_model.encoder.blocks.0.mlp.fc0.bias": "model-00001-of-00002.safetensors",
448
+ "vision_model.encoder.blocks.0.mlp.fc0.weight": "model-00001-of-00002.safetensors",
449
+ "vision_model.encoder.blocks.0.mlp.fc1.bias": "model-00001-of-00002.safetensors",
450
+ "vision_model.encoder.blocks.0.mlp.fc1.weight": "model-00001-of-00002.safetensors",
451
+ "vision_model.encoder.blocks.0.norm0.bias": "model-00001-of-00002.safetensors",
452
+ "vision_model.encoder.blocks.0.norm0.weight": "model-00001-of-00002.safetensors",
453
+ "vision_model.encoder.blocks.0.norm1.bias": "model-00001-of-00002.safetensors",
454
+ "vision_model.encoder.blocks.0.norm1.weight": "model-00001-of-00002.safetensors",
455
+ "vision_model.encoder.blocks.0.wo.bias": "model-00001-of-00002.safetensors",
456
+ "vision_model.encoder.blocks.0.wo.weight": "model-00001-of-00002.safetensors",
457
+ "vision_model.encoder.blocks.0.wqkv.bias": "model-00001-of-00002.safetensors",
458
+ "vision_model.encoder.blocks.0.wqkv.weight": "model-00001-of-00002.safetensors",
459
+ "vision_model.encoder.blocks.1.mlp.fc0.bias": "model-00001-of-00002.safetensors",
460
+ "vision_model.encoder.blocks.1.mlp.fc0.weight": "model-00001-of-00002.safetensors",
461
+ "vision_model.encoder.blocks.1.mlp.fc1.bias": "model-00001-of-00002.safetensors",
462
+ "vision_model.encoder.blocks.1.mlp.fc1.weight": "model-00001-of-00002.safetensors",
463
+ "vision_model.encoder.blocks.1.norm0.bias": "model-00001-of-00002.safetensors",
464
+ "vision_model.encoder.blocks.1.norm0.weight": "model-00001-of-00002.safetensors",
465
+ "vision_model.encoder.blocks.1.norm1.bias": "model-00001-of-00002.safetensors",
466
+ "vision_model.encoder.blocks.1.norm1.weight": "model-00001-of-00002.safetensors",
467
+ "vision_model.encoder.blocks.1.wo.bias": "model-00001-of-00002.safetensors",
468
+ "vision_model.encoder.blocks.1.wo.weight": "model-00001-of-00002.safetensors",
469
+ "vision_model.encoder.blocks.1.wqkv.bias": "model-00001-of-00002.safetensors",
470
+ "vision_model.encoder.blocks.1.wqkv.weight": "model-00001-of-00002.safetensors",
471
+ "vision_model.encoder.blocks.10.mlp.fc0.bias": "model-00001-of-00002.safetensors",
472
+ "vision_model.encoder.blocks.10.mlp.fc0.weight": "model-00001-of-00002.safetensors",
473
+ "vision_model.encoder.blocks.10.mlp.fc1.bias": "model-00001-of-00002.safetensors",
474
+ "vision_model.encoder.blocks.10.mlp.fc1.weight": "model-00001-of-00002.safetensors",
475
+ "vision_model.encoder.blocks.10.norm0.bias": "model-00001-of-00002.safetensors",
476
+ "vision_model.encoder.blocks.10.norm0.weight": "model-00001-of-00002.safetensors",
477
+ "vision_model.encoder.blocks.10.norm1.bias": "model-00001-of-00002.safetensors",
478
+ "vision_model.encoder.blocks.10.norm1.weight": "model-00001-of-00002.safetensors",
479
+ "vision_model.encoder.blocks.10.wo.bias": "model-00001-of-00002.safetensors",
480
+ "vision_model.encoder.blocks.10.wo.weight": "model-00001-of-00002.safetensors",
481
+ "vision_model.encoder.blocks.10.wqkv.bias": "model-00001-of-00002.safetensors",
482
+ "vision_model.encoder.blocks.10.wqkv.weight": "model-00001-of-00002.safetensors",
483
+ "vision_model.encoder.blocks.11.mlp.fc0.bias": "model-00001-of-00002.safetensors",
484
+ "vision_model.encoder.blocks.11.mlp.fc0.weight": "model-00001-of-00002.safetensors",
485
+ "vision_model.encoder.blocks.11.mlp.fc1.bias": "model-00001-of-00002.safetensors",
486
+ "vision_model.encoder.blocks.11.mlp.fc1.weight": "model-00001-of-00002.safetensors",
487
+ "vision_model.encoder.blocks.11.norm0.bias": "model-00001-of-00002.safetensors",
488
+ "vision_model.encoder.blocks.11.norm0.weight": "model-00001-of-00002.safetensors",
489
+ "vision_model.encoder.blocks.11.norm1.bias": "model-00001-of-00002.safetensors",
490
+ "vision_model.encoder.blocks.11.norm1.weight": "model-00001-of-00002.safetensors",
491
+ "vision_model.encoder.blocks.11.wo.bias": "model-00001-of-00002.safetensors",
492
+ "vision_model.encoder.blocks.11.wo.weight": "model-00001-of-00002.safetensors",
493
+ "vision_model.encoder.blocks.11.wqkv.bias": "model-00001-of-00002.safetensors",
494
+ "vision_model.encoder.blocks.11.wqkv.weight": "model-00001-of-00002.safetensors",
495
+ "vision_model.encoder.blocks.12.mlp.fc0.bias": "model-00001-of-00002.safetensors",
496
+ "vision_model.encoder.blocks.12.mlp.fc0.weight": "model-00001-of-00002.safetensors",
497
+ "vision_model.encoder.blocks.12.mlp.fc1.bias": "model-00001-of-00002.safetensors",
498
+ "vision_model.encoder.blocks.12.mlp.fc1.weight": "model-00001-of-00002.safetensors",
499
+ "vision_model.encoder.blocks.12.norm0.bias": "model-00001-of-00002.safetensors",
500
+ "vision_model.encoder.blocks.12.norm0.weight": "model-00001-of-00002.safetensors",
501
+ "vision_model.encoder.blocks.12.norm1.bias": "model-00001-of-00002.safetensors",
502
+ "vision_model.encoder.blocks.12.norm1.weight": "model-00001-of-00002.safetensors",
503
+ "vision_model.encoder.blocks.12.wo.bias": "model-00001-of-00002.safetensors",
504
+ "vision_model.encoder.blocks.12.wo.weight": "model-00001-of-00002.safetensors",
505
+ "vision_model.encoder.blocks.12.wqkv.bias": "model-00001-of-00002.safetensors",
506
+ "vision_model.encoder.blocks.12.wqkv.weight": "model-00001-of-00002.safetensors",
507
+ "vision_model.encoder.blocks.13.mlp.fc0.bias": "model-00001-of-00002.safetensors",
508
+ "vision_model.encoder.blocks.13.mlp.fc0.weight": "model-00001-of-00002.safetensors",
509
+ "vision_model.encoder.blocks.13.mlp.fc1.bias": "model-00001-of-00002.safetensors",
510
+ "vision_model.encoder.blocks.13.mlp.fc1.weight": "model-00001-of-00002.safetensors",
511
+ "vision_model.encoder.blocks.13.norm0.bias": "model-00001-of-00002.safetensors",
512
+ "vision_model.encoder.blocks.13.norm0.weight": "model-00001-of-00002.safetensors",
513
+ "vision_model.encoder.blocks.13.norm1.bias": "model-00001-of-00002.safetensors",
514
+ "vision_model.encoder.blocks.13.norm1.weight": "model-00001-of-00002.safetensors",
515
+ "vision_model.encoder.blocks.13.wo.bias": "model-00001-of-00002.safetensors",
516
+ "vision_model.encoder.blocks.13.wo.weight": "model-00001-of-00002.safetensors",
517
+ "vision_model.encoder.blocks.13.wqkv.bias": "model-00001-of-00002.safetensors",
518
+ "vision_model.encoder.blocks.13.wqkv.weight": "model-00001-of-00002.safetensors",
519
+ "vision_model.encoder.blocks.14.mlp.fc0.bias": "model-00001-of-00002.safetensors",
520
+ "vision_model.encoder.blocks.14.mlp.fc0.weight": "model-00001-of-00002.safetensors",
521
+ "vision_model.encoder.blocks.14.mlp.fc1.bias": "model-00001-of-00002.safetensors",
522
+ "vision_model.encoder.blocks.14.mlp.fc1.weight": "model-00001-of-00002.safetensors",
523
+ "vision_model.encoder.blocks.14.norm0.bias": "model-00001-of-00002.safetensors",
524
+ "vision_model.encoder.blocks.14.norm0.weight": "model-00001-of-00002.safetensors",
525
+ "vision_model.encoder.blocks.14.norm1.bias": "model-00001-of-00002.safetensors",
526
+ "vision_model.encoder.blocks.14.norm1.weight": "model-00001-of-00002.safetensors",
527
+ "vision_model.encoder.blocks.14.wo.bias": "model-00001-of-00002.safetensors",
528
+ "vision_model.encoder.blocks.14.wo.weight": "model-00001-of-00002.safetensors",
529
+ "vision_model.encoder.blocks.14.wqkv.bias": "model-00001-of-00002.safetensors",
530
+ "vision_model.encoder.blocks.14.wqkv.weight": "model-00001-of-00002.safetensors",
531
+ "vision_model.encoder.blocks.15.mlp.fc0.bias": "model-00001-of-00002.safetensors",
532
+ "vision_model.encoder.blocks.15.mlp.fc0.weight": "model-00001-of-00002.safetensors",
533
+ "vision_model.encoder.blocks.15.mlp.fc1.bias": "model-00001-of-00002.safetensors",
534
+ "vision_model.encoder.blocks.15.mlp.fc1.weight": "model-00001-of-00002.safetensors",
535
+ "vision_model.encoder.blocks.15.norm0.bias": "model-00001-of-00002.safetensors",
536
+ "vision_model.encoder.blocks.15.norm0.weight": "model-00001-of-00002.safetensors",
537
+ "vision_model.encoder.blocks.15.norm1.bias": "model-00001-of-00002.safetensors",
538
+ "vision_model.encoder.blocks.15.norm1.weight": "model-00001-of-00002.safetensors",
539
+ "vision_model.encoder.blocks.15.wo.bias": "model-00001-of-00002.safetensors",
540
+ "vision_model.encoder.blocks.15.wo.weight": "model-00001-of-00002.safetensors",
541
+ "vision_model.encoder.blocks.15.wqkv.bias": "model-00001-of-00002.safetensors",
542
+ "vision_model.encoder.blocks.15.wqkv.weight": "model-00001-of-00002.safetensors",
543
+ "vision_model.encoder.blocks.16.mlp.fc0.bias": "model-00001-of-00002.safetensors",
544
+ "vision_model.encoder.blocks.16.mlp.fc0.weight": "model-00001-of-00002.safetensors",
545
+ "vision_model.encoder.blocks.16.mlp.fc1.bias": "model-00001-of-00002.safetensors",
546
+ "vision_model.encoder.blocks.16.mlp.fc1.weight": "model-00001-of-00002.safetensors",
547
+ "vision_model.encoder.blocks.16.norm0.bias": "model-00001-of-00002.safetensors",
548
+ "vision_model.encoder.blocks.16.norm0.weight": "model-00001-of-00002.safetensors",
549
+ "vision_model.encoder.blocks.16.norm1.bias": "model-00001-of-00002.safetensors",
550
+ "vision_model.encoder.blocks.16.norm1.weight": "model-00001-of-00002.safetensors",
551
+ "vision_model.encoder.blocks.16.wo.bias": "model-00001-of-00002.safetensors",
552
+ "vision_model.encoder.blocks.16.wo.weight": "model-00001-of-00002.safetensors",
553
+ "vision_model.encoder.blocks.16.wqkv.bias": "model-00001-of-00002.safetensors",
554
+ "vision_model.encoder.blocks.16.wqkv.weight": "model-00001-of-00002.safetensors",
555
+ "vision_model.encoder.blocks.17.mlp.fc0.bias": "model-00001-of-00002.safetensors",
556
+ "vision_model.encoder.blocks.17.mlp.fc0.weight": "model-00001-of-00002.safetensors",
557
+ "vision_model.encoder.blocks.17.mlp.fc1.bias": "model-00001-of-00002.safetensors",
558
+ "vision_model.encoder.blocks.17.mlp.fc1.weight": "model-00001-of-00002.safetensors",
559
+ "vision_model.encoder.blocks.17.norm0.bias": "model-00001-of-00002.safetensors",
560
+ "vision_model.encoder.blocks.17.norm0.weight": "model-00001-of-00002.safetensors",
561
+ "vision_model.encoder.blocks.17.norm1.bias": "model-00001-of-00002.safetensors",
562
+ "vision_model.encoder.blocks.17.norm1.weight": "model-00001-of-00002.safetensors",
563
+ "vision_model.encoder.blocks.17.wo.bias": "model-00001-of-00002.safetensors",
564
+ "vision_model.encoder.blocks.17.wo.weight": "model-00001-of-00002.safetensors",
565
+ "vision_model.encoder.blocks.17.wqkv.bias": "model-00001-of-00002.safetensors",
566
+ "vision_model.encoder.blocks.17.wqkv.weight": "model-00001-of-00002.safetensors",
567
+ "vision_model.encoder.blocks.18.mlp.fc0.bias": "model-00001-of-00002.safetensors",
568
+ "vision_model.encoder.blocks.18.mlp.fc0.weight": "model-00001-of-00002.safetensors",
569
+ "vision_model.encoder.blocks.18.mlp.fc1.bias": "model-00001-of-00002.safetensors",
570
+ "vision_model.encoder.blocks.18.mlp.fc1.weight": "model-00001-of-00002.safetensors",
571
+ "vision_model.encoder.blocks.18.norm0.bias": "model-00001-of-00002.safetensors",
572
+ "vision_model.encoder.blocks.18.norm0.weight": "model-00001-of-00002.safetensors",
573
+ "vision_model.encoder.blocks.18.norm1.bias": "model-00001-of-00002.safetensors",
574
+ "vision_model.encoder.blocks.18.norm1.weight": "model-00001-of-00002.safetensors",
575
+ "vision_model.encoder.blocks.18.wo.bias": "model-00001-of-00002.safetensors",
576
+ "vision_model.encoder.blocks.18.wo.weight": "model-00001-of-00002.safetensors",
577
+ "vision_model.encoder.blocks.18.wqkv.bias": "model-00001-of-00002.safetensors",
578
+ "vision_model.encoder.blocks.18.wqkv.weight": "model-00001-of-00002.safetensors",
579
+ "vision_model.encoder.blocks.19.mlp.fc0.bias": "model-00001-of-00002.safetensors",
580
+ "vision_model.encoder.blocks.19.mlp.fc0.weight": "model-00001-of-00002.safetensors",
581
+ "vision_model.encoder.blocks.19.mlp.fc1.bias": "model-00001-of-00002.safetensors",
582
+ "vision_model.encoder.blocks.19.mlp.fc1.weight": "model-00001-of-00002.safetensors",
583
+ "vision_model.encoder.blocks.19.norm0.bias": "model-00001-of-00002.safetensors",
584
+ "vision_model.encoder.blocks.19.norm0.weight": "model-00001-of-00002.safetensors",
585
+ "vision_model.encoder.blocks.19.norm1.bias": "model-00001-of-00002.safetensors",
586
+ "vision_model.encoder.blocks.19.norm1.weight": "model-00001-of-00002.safetensors",
587
+ "vision_model.encoder.blocks.19.wo.bias": "model-00001-of-00002.safetensors",
588
+ "vision_model.encoder.blocks.19.wo.weight": "model-00001-of-00002.safetensors",
589
+ "vision_model.encoder.blocks.19.wqkv.bias": "model-00001-of-00002.safetensors",
590
+ "vision_model.encoder.blocks.19.wqkv.weight": "model-00001-of-00002.safetensors",
591
+ "vision_model.encoder.blocks.2.mlp.fc0.bias": "model-00001-of-00002.safetensors",
592
+ "vision_model.encoder.blocks.2.mlp.fc0.weight": "model-00001-of-00002.safetensors",
593
+ "vision_model.encoder.blocks.2.mlp.fc1.bias": "model-00001-of-00002.safetensors",
594
+ "vision_model.encoder.blocks.2.mlp.fc1.weight": "model-00001-of-00002.safetensors",
595
+ "vision_model.encoder.blocks.2.norm0.bias": "model-00001-of-00002.safetensors",
596
+ "vision_model.encoder.blocks.2.norm0.weight": "model-00001-of-00002.safetensors",
597
+ "vision_model.encoder.blocks.2.norm1.bias": "model-00001-of-00002.safetensors",
598
+ "vision_model.encoder.blocks.2.norm1.weight": "model-00001-of-00002.safetensors",
599
+ "vision_model.encoder.blocks.2.wo.bias": "model-00001-of-00002.safetensors",
600
+ "vision_model.encoder.blocks.2.wo.weight": "model-00001-of-00002.safetensors",
601
+ "vision_model.encoder.blocks.2.wqkv.bias": "model-00001-of-00002.safetensors",
602
+ "vision_model.encoder.blocks.2.wqkv.weight": "model-00001-of-00002.safetensors",
603
+ "vision_model.encoder.blocks.20.mlp.fc0.bias": "model-00001-of-00002.safetensors",
604
+ "vision_model.encoder.blocks.20.mlp.fc0.weight": "model-00001-of-00002.safetensors",
605
+ "vision_model.encoder.blocks.20.mlp.fc1.bias": "model-00001-of-00002.safetensors",
606
+ "vision_model.encoder.blocks.20.mlp.fc1.weight": "model-00001-of-00002.safetensors",
607
+ "vision_model.encoder.blocks.20.norm0.bias": "model-00001-of-00002.safetensors",
608
+ "vision_model.encoder.blocks.20.norm0.weight": "model-00001-of-00002.safetensors",
609
+ "vision_model.encoder.blocks.20.norm1.bias": "model-00001-of-00002.safetensors",
610
+ "vision_model.encoder.blocks.20.norm1.weight": "model-00001-of-00002.safetensors",
611
+ "vision_model.encoder.blocks.20.wo.bias": "model-00001-of-00002.safetensors",
612
+ "vision_model.encoder.blocks.20.wo.weight": "model-00001-of-00002.safetensors",
613
+ "vision_model.encoder.blocks.20.wqkv.bias": "model-00001-of-00002.safetensors",
614
+ "vision_model.encoder.blocks.20.wqkv.weight": "model-00001-of-00002.safetensors",
615
+ "vision_model.encoder.blocks.21.mlp.fc0.bias": "model-00001-of-00002.safetensors",
616
+ "vision_model.encoder.blocks.21.mlp.fc0.weight": "model-00001-of-00002.safetensors",
617
+ "vision_model.encoder.blocks.21.mlp.fc1.bias": "model-00001-of-00002.safetensors",
618
+ "vision_model.encoder.blocks.21.mlp.fc1.weight": "model-00001-of-00002.safetensors",
619
+ "vision_model.encoder.blocks.21.norm0.bias": "model-00001-of-00002.safetensors",
620
+ "vision_model.encoder.blocks.21.norm0.weight": "model-00001-of-00002.safetensors",
621
+ "vision_model.encoder.blocks.21.norm1.bias": "model-00001-of-00002.safetensors",
622
+ "vision_model.encoder.blocks.21.norm1.weight": "model-00001-of-00002.safetensors",
623
+ "vision_model.encoder.blocks.21.wo.bias": "model-00001-of-00002.safetensors",
624
+ "vision_model.encoder.blocks.21.wo.weight": "model-00001-of-00002.safetensors",
625
+ "vision_model.encoder.blocks.21.wqkv.bias": "model-00001-of-00002.safetensors",
626
+ "vision_model.encoder.blocks.21.wqkv.weight": "model-00001-of-00002.safetensors",
627
+ "vision_model.encoder.blocks.22.mlp.fc0.bias": "model-00001-of-00002.safetensors",
628
+ "vision_model.encoder.blocks.22.mlp.fc0.weight": "model-00001-of-00002.safetensors",
629
+ "vision_model.encoder.blocks.22.mlp.fc1.bias": "model-00001-of-00002.safetensors",
630
+ "vision_model.encoder.blocks.22.mlp.fc1.weight": "model-00001-of-00002.safetensors",
631
+ "vision_model.encoder.blocks.22.norm0.bias": "model-00001-of-00002.safetensors",
632
+ "vision_model.encoder.blocks.22.norm0.weight": "model-00001-of-00002.safetensors",
633
+ "vision_model.encoder.blocks.22.norm1.bias": "model-00001-of-00002.safetensors",
634
+ "vision_model.encoder.blocks.22.norm1.weight": "model-00001-of-00002.safetensors",
635
+ "vision_model.encoder.blocks.22.wo.bias": "model-00001-of-00002.safetensors",
636
+ "vision_model.encoder.blocks.22.wo.weight": "model-00001-of-00002.safetensors",
637
+ "vision_model.encoder.blocks.22.wqkv.bias": "model-00001-of-00002.safetensors",
638
+ "vision_model.encoder.blocks.22.wqkv.weight": "model-00001-of-00002.safetensors",
639
+ "vision_model.encoder.blocks.23.mlp.fc0.bias": "model-00001-of-00002.safetensors",
640
+ "vision_model.encoder.blocks.23.mlp.fc0.weight": "model-00001-of-00002.safetensors",
641
+ "vision_model.encoder.blocks.23.mlp.fc1.bias": "model-00001-of-00002.safetensors",
642
+ "vision_model.encoder.blocks.23.mlp.fc1.weight": "model-00001-of-00002.safetensors",
643
+ "vision_model.encoder.blocks.23.norm0.bias": "model-00001-of-00002.safetensors",
644
+ "vision_model.encoder.blocks.23.norm0.weight": "model-00001-of-00002.safetensors",
645
+ "vision_model.encoder.blocks.23.norm1.bias": "model-00001-of-00002.safetensors",
646
+ "vision_model.encoder.blocks.23.norm1.weight": "model-00001-of-00002.safetensors",
647
+ "vision_model.encoder.blocks.23.wo.bias": "model-00001-of-00002.safetensors",
648
+ "vision_model.encoder.blocks.23.wo.weight": "model-00001-of-00002.safetensors",
649
+ "vision_model.encoder.blocks.23.wqkv.bias": "model-00001-of-00002.safetensors",
650
+ "vision_model.encoder.blocks.23.wqkv.weight": "model-00001-of-00002.safetensors",
651
+ "vision_model.encoder.blocks.24.mlp.fc0.bias": "model-00001-of-00002.safetensors",
652
+ "vision_model.encoder.blocks.24.mlp.fc0.weight": "model-00001-of-00002.safetensors",
653
+ "vision_model.encoder.blocks.24.mlp.fc1.bias": "model-00001-of-00002.safetensors",
654
+ "vision_model.encoder.blocks.24.mlp.fc1.weight": "model-00001-of-00002.safetensors",
655
+ "vision_model.encoder.blocks.24.norm0.bias": "model-00001-of-00002.safetensors",
656
+ "vision_model.encoder.blocks.24.norm0.weight": "model-00001-of-00002.safetensors",
657
+ "vision_model.encoder.blocks.24.norm1.bias": "model-00001-of-00002.safetensors",
658
+ "vision_model.encoder.blocks.24.norm1.weight": "model-00001-of-00002.safetensors",
659
+ "vision_model.encoder.blocks.24.wo.bias": "model-00001-of-00002.safetensors",
660
+ "vision_model.encoder.blocks.24.wo.weight": "model-00001-of-00002.safetensors",
661
+ "vision_model.encoder.blocks.24.wqkv.bias": "model-00001-of-00002.safetensors",
662
+ "vision_model.encoder.blocks.24.wqkv.weight": "model-00001-of-00002.safetensors",
663
+ "vision_model.encoder.blocks.25.mlp.fc0.bias": "model-00001-of-00002.safetensors",
664
+ "vision_model.encoder.blocks.25.mlp.fc0.weight": "model-00001-of-00002.safetensors",
665
+ "vision_model.encoder.blocks.25.mlp.fc1.bias": "model-00001-of-00002.safetensors",
666
+ "vision_model.encoder.blocks.25.mlp.fc1.weight": "model-00001-of-00002.safetensors",
667
+ "vision_model.encoder.blocks.25.norm0.bias": "model-00001-of-00002.safetensors",
668
+ "vision_model.encoder.blocks.25.norm0.weight": "model-00001-of-00002.safetensors",
669
+ "vision_model.encoder.blocks.25.norm1.bias": "model-00001-of-00002.safetensors",
670
+ "vision_model.encoder.blocks.25.norm1.weight": "model-00001-of-00002.safetensors",
671
+ "vision_model.encoder.blocks.25.wo.bias": "model-00001-of-00002.safetensors",
672
+ "vision_model.encoder.blocks.25.wo.weight": "model-00001-of-00002.safetensors",
673
+ "vision_model.encoder.blocks.25.wqkv.bias": "model-00001-of-00002.safetensors",
674
+ "vision_model.encoder.blocks.25.wqkv.weight": "model-00001-of-00002.safetensors",
675
+ "vision_model.encoder.blocks.26.mlp.fc0.bias": "model-00001-of-00002.safetensors",
676
+ "vision_model.encoder.blocks.26.mlp.fc0.weight": "model-00001-of-00002.safetensors",
677
+ "vision_model.encoder.blocks.26.mlp.fc1.bias": "model-00001-of-00002.safetensors",
678
+ "vision_model.encoder.blocks.26.mlp.fc1.weight": "model-00001-of-00002.safetensors",
679
+ "vision_model.encoder.blocks.26.norm0.bias": "model-00001-of-00002.safetensors",
680
+ "vision_model.encoder.blocks.26.norm0.weight": "model-00001-of-00002.safetensors",
681
+ "vision_model.encoder.blocks.26.norm1.bias": "model-00001-of-00002.safetensors",
682
+ "vision_model.encoder.blocks.26.norm1.weight": "model-00001-of-00002.safetensors",
683
+ "vision_model.encoder.blocks.26.wo.bias": "model-00001-of-00002.safetensors",
684
+ "vision_model.encoder.blocks.26.wo.weight": "model-00001-of-00002.safetensors",
685
+ "vision_model.encoder.blocks.26.wqkv.bias": "model-00001-of-00002.safetensors",
686
+ "vision_model.encoder.blocks.26.wqkv.weight": "model-00001-of-00002.safetensors",
687
+ "vision_model.encoder.blocks.3.mlp.fc0.bias": "model-00001-of-00002.safetensors",
688
+ "vision_model.encoder.blocks.3.mlp.fc0.weight": "model-00001-of-00002.safetensors",
689
+ "vision_model.encoder.blocks.3.mlp.fc1.bias": "model-00001-of-00002.safetensors",
690
+ "vision_model.encoder.blocks.3.mlp.fc1.weight": "model-00001-of-00002.safetensors",
691
+ "vision_model.encoder.blocks.3.norm0.bias": "model-00001-of-00002.safetensors",
692
+ "vision_model.encoder.blocks.3.norm0.weight": "model-00001-of-00002.safetensors",
693
+ "vision_model.encoder.blocks.3.norm1.bias": "model-00001-of-00002.safetensors",
694
+ "vision_model.encoder.blocks.3.norm1.weight": "model-00001-of-00002.safetensors",
695
+ "vision_model.encoder.blocks.3.wo.bias": "model-00001-of-00002.safetensors",
696
+ "vision_model.encoder.blocks.3.wo.weight": "model-00001-of-00002.safetensors",
697
+ "vision_model.encoder.blocks.3.wqkv.bias": "model-00001-of-00002.safetensors",
698
+ "vision_model.encoder.blocks.3.wqkv.weight": "model-00001-of-00002.safetensors",
699
+ "vision_model.encoder.blocks.4.mlp.fc0.bias": "model-00001-of-00002.safetensors",
700
+ "vision_model.encoder.blocks.4.mlp.fc0.weight": "model-00001-of-00002.safetensors",
701
+ "vision_model.encoder.blocks.4.mlp.fc1.bias": "model-00001-of-00002.safetensors",
702
+ "vision_model.encoder.blocks.4.mlp.fc1.weight": "model-00001-of-00002.safetensors",
703
+ "vision_model.encoder.blocks.4.norm0.bias": "model-00001-of-00002.safetensors",
704
+ "vision_model.encoder.blocks.4.norm0.weight": "model-00001-of-00002.safetensors",
705
+ "vision_model.encoder.blocks.4.norm1.bias": "model-00001-of-00002.safetensors",
706
+ "vision_model.encoder.blocks.4.norm1.weight": "model-00001-of-00002.safetensors",
707
+ "vision_model.encoder.blocks.4.wo.bias": "model-00001-of-00002.safetensors",
708
+ "vision_model.encoder.blocks.4.wo.weight": "model-00001-of-00002.safetensors",
709
+ "vision_model.encoder.blocks.4.wqkv.bias": "model-00001-of-00002.safetensors",
710
+ "vision_model.encoder.blocks.4.wqkv.weight": "model-00001-of-00002.safetensors",
711
+ "vision_model.encoder.blocks.5.mlp.fc0.bias": "model-00001-of-00002.safetensors",
712
+ "vision_model.encoder.blocks.5.mlp.fc0.weight": "model-00001-of-00002.safetensors",
713
+ "vision_model.encoder.blocks.5.mlp.fc1.bias": "model-00001-of-00002.safetensors",
714
+ "vision_model.encoder.blocks.5.mlp.fc1.weight": "model-00001-of-00002.safetensors",
715
+ "vision_model.encoder.blocks.5.norm0.bias": "model-00001-of-00002.safetensors",
716
+ "vision_model.encoder.blocks.5.norm0.weight": "model-00001-of-00002.safetensors",
717
+ "vision_model.encoder.blocks.5.norm1.bias": "model-00001-of-00002.safetensors",
718
+ "vision_model.encoder.blocks.5.norm1.weight": "model-00001-of-00002.safetensors",
719
+ "vision_model.encoder.blocks.5.wo.bias": "model-00001-of-00002.safetensors",
720
+ "vision_model.encoder.blocks.5.wo.weight": "model-00001-of-00002.safetensors",
721
+ "vision_model.encoder.blocks.5.wqkv.bias": "model-00001-of-00002.safetensors",
722
+ "vision_model.encoder.blocks.5.wqkv.weight": "model-00001-of-00002.safetensors",
723
+ "vision_model.encoder.blocks.6.mlp.fc0.bias": "model-00001-of-00002.safetensors",
724
+ "vision_model.encoder.blocks.6.mlp.fc0.weight": "model-00001-of-00002.safetensors",
725
+ "vision_model.encoder.blocks.6.mlp.fc1.bias": "model-00001-of-00002.safetensors",
726
+ "vision_model.encoder.blocks.6.mlp.fc1.weight": "model-00001-of-00002.safetensors",
727
+ "vision_model.encoder.blocks.6.norm0.bias": "model-00001-of-00002.safetensors",
728
+ "vision_model.encoder.blocks.6.norm0.weight": "model-00001-of-00002.safetensors",
729
+ "vision_model.encoder.blocks.6.norm1.bias": "model-00001-of-00002.safetensors",
730
+ "vision_model.encoder.blocks.6.norm1.weight": "model-00001-of-00002.safetensors",
731
+ "vision_model.encoder.blocks.6.wo.bias": "model-00001-of-00002.safetensors",
732
+ "vision_model.encoder.blocks.6.wo.weight": "model-00001-of-00002.safetensors",
733
+ "vision_model.encoder.blocks.6.wqkv.bias": "model-00001-of-00002.safetensors",
734
+ "vision_model.encoder.blocks.6.wqkv.weight": "model-00001-of-00002.safetensors",
735
+ "vision_model.encoder.blocks.7.mlp.fc0.bias": "model-00001-of-00002.safetensors",
736
+ "vision_model.encoder.blocks.7.mlp.fc0.weight": "model-00001-of-00002.safetensors",
737
+ "vision_model.encoder.blocks.7.mlp.fc1.bias": "model-00001-of-00002.safetensors",
738
+ "vision_model.encoder.blocks.7.mlp.fc1.weight": "model-00001-of-00002.safetensors",
739
+ "vision_model.encoder.blocks.7.norm0.bias": "model-00001-of-00002.safetensors",
740
+ "vision_model.encoder.blocks.7.norm0.weight": "model-00001-of-00002.safetensors",
741
+ "vision_model.encoder.blocks.7.norm1.bias": "model-00001-of-00002.safetensors",
742
+ "vision_model.encoder.blocks.7.norm1.weight": "model-00001-of-00002.safetensors",
743
+ "vision_model.encoder.blocks.7.wo.bias": "model-00001-of-00002.safetensors",
744
+ "vision_model.encoder.blocks.7.wo.weight": "model-00001-of-00002.safetensors",
745
+ "vision_model.encoder.blocks.7.wqkv.bias": "model-00001-of-00002.safetensors",
746
+ "vision_model.encoder.blocks.7.wqkv.weight": "model-00001-of-00002.safetensors",
747
+ "vision_model.encoder.blocks.8.mlp.fc0.bias": "model-00001-of-00002.safetensors",
748
+ "vision_model.encoder.blocks.8.mlp.fc0.weight": "model-00001-of-00002.safetensors",
749
+ "vision_model.encoder.blocks.8.mlp.fc1.bias": "model-00001-of-00002.safetensors",
750
+ "vision_model.encoder.blocks.8.mlp.fc1.weight": "model-00001-of-00002.safetensors",
751
+ "vision_model.encoder.blocks.8.norm0.bias": "model-00001-of-00002.safetensors",
752
+ "vision_model.encoder.blocks.8.norm0.weight": "model-00001-of-00002.safetensors",
753
+ "vision_model.encoder.blocks.8.norm1.bias": "model-00001-of-00002.safetensors",
754
+ "vision_model.encoder.blocks.8.norm1.weight": "model-00001-of-00002.safetensors",
755
+ "vision_model.encoder.blocks.8.wo.bias": "model-00001-of-00002.safetensors",
756
+ "vision_model.encoder.blocks.8.wo.weight": "model-00001-of-00002.safetensors",
757
+ "vision_model.encoder.blocks.8.wqkv.bias": "model-00001-of-00002.safetensors",
758
+ "vision_model.encoder.blocks.8.wqkv.weight": "model-00001-of-00002.safetensors",
759
+ "vision_model.encoder.blocks.9.mlp.fc0.bias": "model-00001-of-00002.safetensors",
760
+ "vision_model.encoder.blocks.9.mlp.fc0.weight": "model-00001-of-00002.safetensors",
761
+ "vision_model.encoder.blocks.9.mlp.fc1.bias": "model-00001-of-00002.safetensors",
762
+ "vision_model.encoder.blocks.9.mlp.fc1.weight": "model-00001-of-00002.safetensors",
763
+ "vision_model.encoder.blocks.9.norm0.bias": "model-00001-of-00002.safetensors",
764
+ "vision_model.encoder.blocks.9.norm0.weight": "model-00001-of-00002.safetensors",
765
+ "vision_model.encoder.blocks.9.norm1.bias": "model-00001-of-00002.safetensors",
766
+ "vision_model.encoder.blocks.9.norm1.weight": "model-00001-of-00002.safetensors",
767
+ "vision_model.encoder.blocks.9.wo.bias": "model-00001-of-00002.safetensors",
768
+ "vision_model.encoder.blocks.9.wo.weight": "model-00001-of-00002.safetensors",
769
+ "vision_model.encoder.blocks.9.wqkv.bias": "model-00001-of-00002.safetensors",
770
+ "vision_model.encoder.blocks.9.wqkv.weight": "model-00001-of-00002.safetensors",
771
+ "vision_model.encoder.final_layernorm.bias": "model-00001-of-00002.safetensors",
772
+ "vision_model.encoder.final_layernorm.weight": "model-00001-of-00002.safetensors",
773
+ "vision_model.patch_embed.pos_emb.weight": "model-00001-of-00002.safetensors",
774
+ "vision_model.patch_embed.proj.bias": "model-00001-of-00002.safetensors",
775
+ "vision_model.patch_embed.proj.weight": "model-00001-of-00002.safetensors"
776
+ }
777
+ }
modeling_locateanything.py ADDED
@@ -0,0 +1,537 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # --------------------------------------------------------
2
+ # NVIDIA
3
+ # Copyright (c) 2025 NVIDIA
4
+ # Licensed under The MIT License [see LICENSE for details]
5
+ # --------------------------------------------------------
6
+
7
+ import time
8
+ from typing import List, Optional, Tuple, Union
9
+
10
+ import numpy as np
11
+ import torch
12
+ from torch import nn
13
+ from torch.nn import CrossEntropyLoss
14
+ from transformers.generation import GenerationMixin
15
+ from transformers.modeling_outputs import CausalLMOutputWithPast
16
+ from transformers.modeling_utils import PreTrainedModel
17
+ from transformers.utils import add_start_docstrings, is_flash_attn_2_available, logging
18
+ from peft import LoraConfig, get_peft_model
19
+
20
+ from .configuration_locateanything import LocateAnythingConfig
21
+ from .modeling_qwen2 import Qwen2ForCausalLM
22
+ from .modeling_vit import MoonVitPretrainedModel
23
+ from transformers.models.qwen3.modeling_qwen3 import Qwen3ForCausalLM
24
+ from .mask_sdpa_utils import *
25
+ from .mask_magi_utils import *
26
+ from .configuration_qwen2 import Qwen2Config
27
+
28
+ from .generate_utils import (
29
+ sample_tokens,
30
+ handle_pattern,
31
+ get_token_ids_from_config,
32
+ )
33
+
34
+ logger = logging.get_logger(__name__)
35
+
36
+
37
+ LOCATEANYTHING_START_DOCSTRING = r"""
38
+ This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
39
+ library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
40
+ etc.)
41
+ This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
42
+ Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
43
+ and behavior.
44
+ Parameters:
45
+ config ([`LocateAnythingConfig`]):
46
+ Model configuration class with all the parameters of the model. Initializing with a config file does not
47
+ load the weights associated with the model, only the configuration. Check out the
48
+ [`~PreTrainedModel.from_pretrained`] method to load the model weights.
49
+ """
50
+
51
+ @add_start_docstrings(
52
+ "The bare LocateAnything Model outputting raw hidden-states without any specific head on top.",
53
+ LOCATEANYTHING_START_DOCSTRING,
54
+ )
55
+ class LocateAnythingPreTrainedModel(PreTrainedModel):
56
+ config_class = LocateAnythingConfig
57
+ base_model_prefix = "model"
58
+ main_input_name = 'input_ids'
59
+ supports_gradient_checkpointing = True
60
+ _no_split_modules = ["Qwen2DecoderLayer"]
61
+ _skip_keys_device_placement = "past_key_values"
62
+ _supports_flash_attn_2 = True
63
+ _supports_cache_class = True
64
+ _supports_static_cache = True
65
+ _supports_quantized_cache = True
66
+ _supports_sdpa = True
67
+
68
+ @classmethod
69
+ def _autoset_attn_implementation(cls, config, *args, **kwargs):
70
+ if getattr(config, '_attn_implementation', None) == 'magi':
71
+ return config
72
+ return super()._autoset_attn_implementation(config, *args, **kwargs)
73
+
74
+ def _check_and_adjust_attn_implementation(self, attn_implementation, is_init_check=False):
75
+ if attn_implementation == "magi":
76
+ return "magi"
77
+ return super()._check_and_adjust_attn_implementation(attn_implementation, is_init_check)
78
+
79
+ def _init_weights(self, module):
80
+ std = getattr(self.config, 'initializer_range', None) or self.config.text_config.initializer_range
81
+ if isinstance(module, (nn.Linear, nn.Conv2d)):
82
+ module.weight.data.normal_(mean=0.0, std=std)
83
+ if module.bias is not None:
84
+ module.bias.data.zero_()
85
+ elif isinstance(module, nn.Embedding):
86
+ module.weight.data.normal_(mean=0.0, std=std)
87
+ if module.padding_idx is not None:
88
+ module.weight.data[module.padding_idx].zero_()
89
+
90
+
91
+ class LocateAnythingForConditionalGeneration(LocateAnythingPreTrainedModel, GenerationMixin):
92
+ config_class = LocateAnythingConfig
93
+ def __init__(self, config: LocateAnythingConfig, vision_model=None, language_model=None):
94
+ super().__init__(config)
95
+
96
+ self.template = config.template
97
+ self.mlp_checkpoint = config.mlp_checkpoint
98
+
99
+ logger.info(f'mlp_checkpoint: {self.mlp_checkpoint}')
100
+ if vision_model is not None:
101
+ self.vision_model = vision_model
102
+ else:
103
+ if config.vision_config.model_type == 'moonvit':
104
+ vision_attn_impl = getattr(config.vision_config, '_attn_implementation', None) or 'flash_attention_2'
105
+ if vision_attn_impl == 'flash_attention_2' and not is_flash_attn_2_available():
106
+ logger.warning_once(
107
+ "flash_attn is not available for MoonViT inference; falling back to sdpa."
108
+ )
109
+ vision_attn_impl = 'sdpa'
110
+ config.vision_config._attn_implementation = vision_attn_impl
111
+ self.vision_model = MoonVitPretrainedModel(config.vision_config)
112
+ else:
113
+ raise ValueError(f'Unsupported vision model type: {config.vision_config.model_type}. Only moonvit is supported.')
114
+
115
+ text_attn_impl = (
116
+ getattr(config.text_config, '_attn_implementation', None)
117
+ or getattr(config, '_attn_implementation', None)
118
+ or 'magi'
119
+ )
120
+ config.text_config._attn_implementation = text_attn_impl
121
+
122
+ if language_model is not None:
123
+ self.language_model = language_model
124
+ else:
125
+ if config.text_config.architectures[0] == 'Qwen2ForCausalLM':
126
+ self.language_model = Qwen2ForCausalLM(config.text_config)
127
+ elif config.text_config.architectures[0] == 'Qwen3ForCausalLM':
128
+ self.language_model = Qwen3ForCausalLM(config.text_config)
129
+ else:
130
+ raise ValueError(f'Unsupported language model architecture: {config.text_config.architectures[0]}. Only Qwen2ForCausalLM and Qwen3ForCausalLM are supported.')
131
+
132
+ vit_hidden_size = config.vision_config.hidden_size
133
+ llm_hidden_size = config.text_config.hidden_size
134
+
135
+ # MLP for moonvit (without pixel_shuffle_back, direct mapping)
136
+ self.mlp1 = nn.Sequential(
137
+ nn.LayerNorm(vit_hidden_size*4),
138
+ nn.Linear(vit_hidden_size*4, llm_hidden_size),
139
+ nn.GELU(),
140
+ nn.Linear(llm_hidden_size, llm_hidden_size)
141
+ )
142
+ self.image_token_index = config.image_token_index
143
+ self.neftune_alpha = None
144
+
145
+ if config.use_backbone_lora:
146
+ self.wrap_backbone_lora(r=config.use_backbone_lora, lora_alpha=2 * config.use_backbone_lora)
147
+
148
+ self.use_llm_lora = config.use_llm_lora
149
+ if config.use_llm_lora:
150
+ self.wrap_llm_lora(r=config.use_llm_lora, lora_alpha=2 * config.use_llm_lora)
151
+
152
+ self.token_ids = get_token_ids_from_config(config)
153
+
154
+ # Set _no_split_modules dynamically based on the actual LLM architecture
155
+ arch = config.text_config.architectures[0] if hasattr(config.text_config, 'architectures') and config.text_config.architectures else 'Qwen2ForCausalLM'
156
+ if 'Qwen3' in arch:
157
+ self._no_split_modules = ["Qwen3DecoderLayer"]
158
+ else:
159
+ self._no_split_modules = ["Qwen2DecoderLayer"]
160
+
161
+
162
+ def wrap_backbone_lora(self, r=128, lora_alpha=256, lora_dropout=0.05):
163
+ lora_config = LoraConfig(
164
+ r=r,
165
+ target_modules=['self_attn.q_proj', 'self_attn.k_proj', 'self_attn.v_proj', 'self_attn.out_proj',
166
+ 'mlp.fc1', 'mlp.fc2'],
167
+ lora_alpha=lora_alpha,
168
+ lora_dropout=lora_dropout,
169
+ )
170
+ self.vision_model = get_peft_model(self.vision_model, lora_config)
171
+ self.vision_model.print_trainable_parameters()
172
+
173
+ def wrap_llm_lora(self, r=128, lora_alpha=256, lora_dropout=0.05):
174
+ lora_config = LoraConfig(
175
+ r=r,
176
+ target_modules=['self_attn.q_proj', 'self_attn.k_proj', 'self_attn.v_proj', 'self_attn.o_proj',
177
+ 'mlp.gate_proj', 'mlp.down_proj', 'mlp.up_proj'],
178
+ lora_alpha=lora_alpha,
179
+ lora_dropout=lora_dropout,
180
+ task_type='CAUSAL_LM'
181
+ )
182
+ self.language_model = get_peft_model(self.language_model, lora_config)
183
+ self.language_model.enable_input_require_grads()
184
+ self.language_model.print_trainable_parameters()
185
+ self.use_llm_lora = True
186
+
187
+
188
+ def forward(
189
+ self,
190
+ pixel_values: List[torch.FloatTensor],
191
+ input_ids: torch.LongTensor = None,
192
+ attention_mask: Optional[torch.Tensor] = None,
193
+ position_ids: Optional[torch.LongTensor] = None,
194
+ image_grid_hws: Optional[torch.Tensor] = None,
195
+ image_flags: Optional[torch.Tensor] = None,
196
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
197
+ labels: Optional[torch.LongTensor] = None,
198
+ use_cache: Optional[bool] = None,
199
+ output_attentions: Optional[bool] = None,
200
+ output_hidden_states: Optional[bool] = None,
201
+ return_dict: Optional[bool] = None,
202
+ ) -> Union[Tuple, CausalLMOutputWithPast]:
203
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
204
+
205
+ input_embeds = self.language_model.get_input_embeddings()(input_ids)
206
+
207
+ has_images = image_flags is not None and image_flags.sum() > 0
208
+
209
+ vit_embeds = self.extract_feature(pixel_values, image_grid_hws)
210
+
211
+ B, N, C = input_embeds.shape
212
+ input_embeds = input_embeds.reshape(B * N, C)
213
+
214
+ if has_images:
215
+ filtered_vit_embeds = []
216
+ idx = 0
217
+ for flag in image_flags:
218
+ flag_val = flag.item()
219
+ if flag_val != 0:
220
+ filtered_vit_embeds.extend(vit_embeds[idx:idx + flag_val])
221
+ idx += flag_val
222
+ else:
223
+ idx += 1
224
+
225
+ vit_embeds = filtered_vit_embeds
226
+ vit_embeds = torch.cat(vit_embeds, dim=0)
227
+
228
+ vit_embeds = self.mlp1(vit_embeds)
229
+ input_ids = input_ids.reshape(B * N)
230
+ selected = (input_ids == self.image_token_index)
231
+
232
+ input_embeds[selected] = input_embeds[selected] * 0.0 + vit_embeds[:selected.sum()]
233
+ else:
234
+ if vit_embeds:
235
+ vit_embeds = torch.cat(vit_embeds, dim=0)
236
+ vit_embeds = self.mlp1(vit_embeds)
237
+ input_ids = input_ids.reshape(B * N)
238
+ selected = (input_ids == self.image_token_index)
239
+ if selected.sum() > 0:
240
+ input_embeds[selected] = vit_embeds[:selected.sum()]
241
+
242
+ input_embeds = input_embeds.reshape(B, N, C)
243
+
244
+ outputs = self.language_model(
245
+ inputs_embeds=input_embeds,
246
+ attention_mask=attention_mask,
247
+ position_ids=position_ids,
248
+ past_key_values=past_key_values,
249
+ use_cache=use_cache,
250
+ output_attentions=output_attentions,
251
+ output_hidden_states=output_hidden_states,
252
+ )
253
+ logits = outputs.logits
254
+
255
+ loss = None
256
+ if labels is not None:
257
+ # Shift so that tokens < n predict n
258
+ shift_logits = logits[..., :-1, :].contiguous()
259
+ shift_labels = labels[..., 1:].contiguous()
260
+ # Flatten the tokens
261
+ loss_fct = CrossEntropyLoss()
262
+ shift_logits = shift_logits.view(-1, self.language_model.config.vocab_size)
263
+ shift_labels = shift_labels.view(-1)
264
+ # Enable model parallelism
265
+ shift_labels = shift_labels.to(shift_logits.device)
266
+ loss = loss_fct(shift_logits, shift_labels)
267
+
268
+ if not return_dict:
269
+ output = (logits,) + outputs[1:]
270
+ return (loss,) + output if loss is not None else output
271
+
272
+ return CausalLMOutputWithPast(
273
+ loss=loss,
274
+ logits=logits,
275
+ past_key_values=outputs.past_key_values,
276
+ hidden_states=outputs.hidden_states,
277
+ attentions=outputs.attentions,
278
+ )
279
+
280
+
281
+ def extract_feature(self, pixel_values, image_grid_hws):
282
+ vit_embeds = self.vision_model(pixel_values=pixel_values, grid_hws=image_grid_hws)
283
+
284
+ return vit_embeds
285
+
286
+ def get_input_embeddings(self):
287
+ return self.language_model.get_input_embeddings()
288
+
289
+ def set_input_embeddings(self, value):
290
+ self.language_model.set_input_embeddings(value)
291
+
292
+ def get_output_embeddings(self):
293
+ return self.language_model.get_output_embeddings()
294
+
295
+ def set_output_embeddings(self, new_embeddings):
296
+ self.language_model.set_output_embeddings(new_embeddings)
297
+
298
+ def set_decoder(self, decoder):
299
+ self.language_model.set_decoder(decoder)
300
+
301
+ def get_decoder(self):
302
+ return self.language_model.get_decoder()
303
+
304
+ @torch.no_grad()
305
+ def generate(
306
+ self,
307
+ pixel_values: Optional[torch.FloatTensor] = None,
308
+ input_ids: Optional[torch.FloatTensor] = None,
309
+ attention_mask: Optional[torch.LongTensor] = None,
310
+ visual_features: Optional[torch.FloatTensor] = None,
311
+ image_grid_hws: Optional[torch.Tensor] = None,
312
+ tokenizer = None,
313
+ n_future_tokens: int = 6,
314
+ **generate_kwargs,
315
+ ) -> torch.LongTensor:
316
+
317
+ verbose = generate_kwargs.pop('verbose', False)
318
+ start_time = time.time()
319
+ prefill_time = None
320
+
321
+ pixel_values = pixel_values.to(self.language_model.dtype)
322
+ # Convert numpy array to tensor if needed
323
+ if isinstance(image_grid_hws, np.ndarray):
324
+ image_grid_hws = torch.from_numpy(image_grid_hws).to(pixel_values.device, dtype=torch.int32)
325
+
326
+ batch_size, seq_len = input_ids.shape
327
+ assert batch_size == 1, 'only batch size = 1 is supported now'
328
+ assert generate_kwargs.get('use_cache', False), "Only use_cache=True is supported."
329
+
330
+ generated = input_ids.clone()
331
+ total_gen_length = min(tokenizer.model_max_length, seq_len + generate_kwargs.get('max_new_tokens', 2048))
332
+ iter_round = 0
333
+ past_key_values = None
334
+
335
+ # Extract visual features once before the loop
336
+ if visual_features is not None:
337
+ vit_embeds = visual_features
338
+ elif pixel_values is not None:
339
+ vit_embeds = self.extract_feature(pixel_values, image_grid_hws)
340
+ else:
341
+ vit_embeds = None
342
+
343
+ if image_grid_hws is not None:
344
+ vit_embeds = torch.cat(vit_embeds, dim=0)
345
+ vit_embeds = self.mlp1(vit_embeds)
346
+
347
+ # ==================== Generation Mode ====================
348
+ # 'fast' : MTP only, never fall back to AR
349
+ # 'slow' : AR only, pure auto-regressive decoding
350
+ # 'hybrid' : MTP first, fall back to AR on error, switch back on box_end
351
+ generation_mode = generate_kwargs.get('generation_mode', 'hybrid')
352
+ assert generation_mode in ('fast', 'slow', 'hybrid'), \
353
+ f"Unsupported generation_mode='{generation_mode}'. Use 'fast', 'slow', or 'hybrid'."
354
+
355
+ sampling_history = []
356
+
357
+
358
+ use_mtp = generation_mode in ('fast', 'hybrid')
359
+ switch_to_ar_count = 0
360
+
361
+ # Pre-allocate mask tokens and position ids
362
+ default_mask_token_id = self.token_ids['default_mask_token_id']
363
+ pre_mask_tokens = torch.full(
364
+ (batch_size, n_future_tokens - 1),
365
+ default_mask_token_id,
366
+ dtype=generated.dtype,
367
+ device=generated.device
368
+ )
369
+ max_possible_len = total_gen_length + n_future_tokens
370
+ full_position_ids = torch.arange(0, max_possible_len, device=generated.device).unsqueeze(0)
371
+
372
+
373
+ def _prepare_inputs_in_mtp(generated):
374
+ generated_with_mask = torch.cat(
375
+ (
376
+ generated,
377
+ generated[:, -1].unsqueeze(1),
378
+ pre_mask_tokens
379
+ ),
380
+ dim=1
381
+ ) # [batch_size, seq_len + 1 + n_future_tokens - 1]
382
+
383
+ # Update pe for kvcache
384
+ start_idx = past_key_values[0][0].size(2) if past_key_values is not None else 0
385
+ position_ids = full_position_ids[:, start_idx : generated_with_mask.size(1)].clone()
386
+ position_ids[0, -n_future_tokens:] -= 1
387
+
388
+ prepare_inputs = self.language_model.prepare_inputs_for_generation(
389
+ generated_with_mask,
390
+ past_key_values,
391
+ None,
392
+ inputs_embeds=None,
393
+ use_cache=True,
394
+ position_ids=position_ids
395
+ )
396
+ return prepare_inputs
397
+
398
+
399
+ def _prepare_input_in_ar(generated):
400
+ start_idx = past_key_values[0][0].size(2) if past_key_values is not None else 0
401
+ position_ids = full_position_ids[:, start_idx : generated.size(1)]
402
+ prepare_inputs = self.language_model.prepare_inputs_for_generation(
403
+ generated,
404
+ past_key_values,
405
+ None,
406
+ inputs_embeds=None,
407
+ use_cache=True,
408
+ position_ids=position_ids
409
+ )
410
+ return prepare_inputs
411
+
412
+
413
+ def _sample_token_in_mtp(generated, outputs):
414
+ """Sample tokens using MTP (Multi-Token Prediction) mode."""
415
+ next_token_logits = outputs.logits[:, -n_future_tokens:, :]
416
+ probs, confidence, x0, box_avg = sample_tokens(
417
+ next_token_logits, generated, self.token_ids, keep_k=5, **generate_kwargs
418
+ )
419
+
420
+ is_box_empty = (box_avg[0] == 0).all()
421
+ new_tokens = x0[0] if is_box_empty else box_avg[0]
422
+
423
+ out_pattern = handle_pattern(new_tokens, self.token_ids, generation_mode)
424
+ out_type = out_pattern['type']
425
+ out_token = torch.tensor(out_pattern['tokens'], dtype=x0.dtype, device=x0.device)
426
+
427
+ return out_type, out_token
428
+
429
+
430
+ def _sample_token_in_ar(generated, outputs):
431
+ """Sample a single token using AR (Auto-Regressive) mode."""
432
+ next_token_logits = outputs.logits[:, -1:, :]
433
+ probs, confidence, x0, _ = sample_tokens(
434
+ next_token_logits, generated, self.token_ids, **generate_kwargs
435
+ )
436
+
437
+ out_token = x0[0]
438
+ out_type = 'continue_ar'
439
+ token_val = out_token[0].item()
440
+
441
+ box_end_token_id = self.token_ids['box_end_token_id']
442
+ coord_start_token_id = self.token_ids['coord_start_token_id']
443
+ coord_end_token_id = self.token_ids['coord_end_token_id']
444
+ none_token_id = self.token_ids['none_token_id']
445
+ im_end_token_id = self.token_ids['im_end_token_id']
446
+
447
+ if generation_mode == 'hybrid':
448
+ # Hybrid AR phase: detect box boundaries to switch back to MTP
449
+ if token_val == box_end_token_id:
450
+ out_type = 'box_end_ar'
451
+ elif coord_start_token_id <= token_val <= coord_end_token_id or token_val == none_token_id:
452
+ out_type = 'coord_ar'
453
+ else:
454
+ out_type = 'im_end'
455
+ else:
456
+ # Slow mode: pure AR, only stop on im_end
457
+ if token_val == im_end_token_id:
458
+ out_type = 'im_end'
459
+
460
+ return out_type, out_token
461
+
462
+
463
+ # Generate loop
464
+ while generated.size(1) < total_gen_length:
465
+ iter_round += 1
466
+
467
+ # Step 1: Prepare inputs
468
+ if use_mtp:
469
+ prepare_inputs = _prepare_inputs_in_mtp(generated)
470
+ else:
471
+ prepare_inputs = _prepare_input_in_ar(generated)
472
+
473
+ if iter_round == 1:
474
+ prepare_inputs.update({
475
+ 'visual_features': vit_embeds,
476
+ 'image_token_index': self.config.image_token_index,
477
+ })
478
+
479
+ # Step 2: Model forward & update KV cache
480
+ with torch.no_grad():
481
+ outputs = self.language_model(**prepare_inputs)
482
+
483
+ past_key_values = tuple(
484
+ (kv[0][:, :, :generated.shape[1], :], kv[1][:, :, :generated.shape[1], :])
485
+ for kv in outputs.past_key_values
486
+ )
487
+
488
+ # Step 3: Sample tokens
489
+ if use_mtp:
490
+ out_type, out_token = _sample_token_in_mtp(generated, outputs)
491
+ else:
492
+ out_type, out_token = _sample_token_in_ar(generated, outputs)
493
+
494
+ if verbose:
495
+ sampling_history.append(('ar' if 'ar' in out_type else 'mtp', tokenizer.decode(out_token, skip_special_tokens=False)))
496
+
497
+ generated = torch.cat([generated, out_token.unsqueeze(0)], dim=1)
498
+
499
+ # Step 4: Mode switching & termination
500
+ if out_type == 'im_end':
501
+ break
502
+
503
+ if generation_mode == 'hybrid':
504
+ if out_type == 'error_box':
505
+ use_mtp = False
506
+ switch_to_ar_count += 1
507
+ elif out_type == 'box_end_ar':
508
+ use_mtp = True
509
+ # fast mode: use_mtp stays True always
510
+ # slow mode: use_mtp stays False always
511
+
512
+ if prefill_time is None:
513
+ prefill_time = time.time() - start_time
514
+
515
+ # Decode and return
516
+ generated_ids = generated[:, seq_len:]
517
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=False)
518
+
519
+ if verbose:
520
+ end_time = time.time()
521
+ num_tokens = generated_ids.size(1)
522
+ num_boxes = response[0].count("<box>")
523
+ total_time = end_time - start_time
524
+
525
+ out_info = f"\nStatistic Info, num_tokens={num_tokens}; " + \
526
+ f"generate_time(s)={total_time:.4f}; " + \
527
+ f"tps={(num_tokens / total_time):.4f}; " + \
528
+ f"forward_step={iter_round}; " + \
529
+ f"num_boxes={num_boxes}; " + \
530
+ f"bps={(num_boxes / total_time):.4f}; " + \
531
+ f"prefill_time={(prefill_time):.4f}; " + \
532
+ f"switch_to_ar={switch_to_ar_count}\n"
533
+ print(out_info)
534
+
535
+ return response[0], sampling_history, out_info
536
+
537
+ return response[0]
modeling_qwen2.py ADDED
@@ -0,0 +1,1738 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2024 The Qwen team, Alibaba Group and the HuggingFace Inc. team. All rights reserved.
3
+ #
4
+ # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
5
+ # and OPT implementations in this library. It has been modified from its
6
+ # original forms to accommodate minor architectural differences compared
7
+ # to GPT-NeoX and OPT used by the Meta AI team that trained the model.
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ """ PyTorch Qwen2 model."""
21
+ import inspect
22
+ import math
23
+ import copy
24
+ import warnings
25
+ from functools import partial
26
+ from typing import List, Optional, Tuple, Union
27
+
28
+ import torch
29
+ import torch.nn.functional as F
30
+ import torch.utils.checkpoint
31
+ from torch import nn
32
+ from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
33
+
34
+ from transformers.activations import ACT2FN
35
+ from transformers.cache_utils import Cache, DynamicCache
36
+ from transformers.modeling_attn_mask_utils import _prepare_4d_causal_attention_mask, _prepare_4d_causal_attention_mask_for_sdpa
37
+ from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast, SequenceClassifierOutputWithPast
38
+ from transformers.modeling_utils import PreTrainedModel
39
+ from transformers.utils import (
40
+ add_start_docstrings,
41
+ add_start_docstrings_to_model_forward,
42
+ is_flash_attn_2_available,
43
+ is_flash_attn_greater_or_equal_2_10,
44
+ logging,
45
+ replace_return_docstrings,
46
+ )
47
+ from .configuration_qwen2 import Qwen2Config
48
+
49
+ if is_flash_attn_2_available():
50
+ from flash_attn import flash_attn_func, flash_attn_varlen_func
51
+ from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input # noqa
52
+
53
+ _flash_supports_window_size = "window_size" in list(inspect.signature(flash_attn_func).parameters)
54
+
55
+
56
+ logger = logging.get_logger(__name__)
57
+
58
+ # Magi Attention Supported
59
+ _MAGI_AVAILABLE = False
60
+ try:
61
+ from magi_attention.functional.flex_flash_attn import flex_flash_attn_func
62
+ _MAGI_AVAILABLE = True
63
+ except ImportError:
64
+ flex_flash_attn_func = None
65
+
66
+
67
+ _CHECKPOINT_FOR_DOC = "Qwen/Qwen2-7B-beta"
68
+ _CONFIG_FOR_DOC = "Qwen2Config"
69
+
70
+ QWEN2_PRETRAINED_MODEL_ARCHIVE_LIST = [
71
+ "Qwen/Qwen2-7B-beta",
72
+ # See all Qwen2 models at https://huggingface.co/models?filter=qwen2
73
+ ]
74
+
75
+ from .mask_sdpa_utils import (
76
+ find_prefix_seq_length_by_pe,
77
+ update_causal_mask_with_pad_non_visible_2d,
78
+ update_causal_mask_for_one_gen_window_2d,
79
+ create_block_diff_mask_by_pe_4d,
80
+ find_pred_pos_from_input_ids
81
+ )
82
+
83
+ from .mask_magi_utils import build_magi_ranges
84
+
85
+ # Copied from transformers.models.llama.modeling_llama._get_unpad_data
86
+ def _get_unpad_data(attention_mask):
87
+ seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32)
88
+ indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten()
89
+ max_seqlen_in_batch = seqlens_in_batch.max().item()
90
+ cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.torch.int32), (1, 0))
91
+ return (
92
+ indices,
93
+ cu_seqlens,
94
+ max_seqlen_in_batch,
95
+ )
96
+
97
+
98
+ # Copied from transformers.models.llama.modeling_llama.LlamaRMSNorm with Llama->Qwen2
99
+ class Qwen2RMSNorm(nn.Module):
100
+ def __init__(self, hidden_size, eps=1e-6):
101
+ """
102
+ Qwen2RMSNorm is equivalent to T5LayerNorm
103
+ """
104
+ super().__init__()
105
+ self.weight = nn.Parameter(torch.ones(hidden_size))
106
+ self.variance_epsilon = eps
107
+
108
+ def forward(self, hidden_states):
109
+ input_dtype = hidden_states.dtype
110
+ hidden_states = hidden_states.to(torch.float32)
111
+ variance = hidden_states.pow(2).mean(-1, keepdim=True)
112
+ hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
113
+ return self.weight * hidden_states.to(input_dtype)
114
+
115
+
116
+ # Copied from transformers.models.llama.modeling_llama.LlamaRotaryEmbedding with Llama->Qwen2
117
+ class Qwen2RotaryEmbedding(nn.Module):
118
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None):
119
+ super().__init__()
120
+
121
+ self.dim = dim
122
+ self.max_position_embeddings = max_position_embeddings
123
+ self.base = base
124
+ inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2).float().to(device) / self.dim))
125
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
126
+
127
+ # Build here to make `torch.jit.trace` work.
128
+ self._set_cos_sin_cache(
129
+ seq_len=max_position_embeddings, device=self.inv_freq.device, dtype=torch.get_default_dtype()
130
+ )
131
+
132
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
133
+ self.max_seq_len_cached = seq_len
134
+ t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
135
+
136
+ freqs = torch.outer(t, self.inv_freq)
137
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
138
+ emb = torch.cat((freqs, freqs), dim=-1)
139
+ self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
140
+ self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
141
+
142
+ def forward(self, x, seq_len=None):
143
+ # x: [bs, num_attention_heads, seq_len, head_size]
144
+ if seq_len > self.max_seq_len_cached:
145
+ self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype)
146
+
147
+ return (
148
+ self.cos_cached[:seq_len].to(dtype=x.dtype),
149
+ self.sin_cached[:seq_len].to(dtype=x.dtype),
150
+ )
151
+
152
+
153
+ # Copied from transformers.models.llama.modeling_llama.rotate_half
154
+ def rotate_half(x):
155
+ """Rotates half the hidden dims of the input."""
156
+ x1 = x[..., : x.shape[-1] // 2]
157
+ x2 = x[..., x.shape[-1] // 2 :]
158
+ return torch.cat((-x2, x1), dim=-1)
159
+
160
+
161
+ # Copied from transformers.models.llama.modeling_llama.apply_rotary_pos_emb
162
+ def apply_rotary_pos_emb(q, k, cos, sin, position_ids, unsqueeze_dim=1):
163
+ """Applies Rotary Position Embedding to the query and key tensors.
164
+
165
+ Args:
166
+ q (`torch.Tensor`): The query tensor.
167
+ k (`torch.Tensor`): The key tensor.
168
+ cos (`torch.Tensor`): The cosine part of the rotary embedding.
169
+ sin (`torch.Tensor`): The sine part of the rotary embedding.
170
+ position_ids (`torch.Tensor`):
171
+ The position indices of the tokens corresponding to the query and key tensors. For example, this can be
172
+ used to pass offsetted position ids when working with a KV-cache.
173
+ unsqueeze_dim (`int`, *optional*, defaults to 1):
174
+ The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
175
+ sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
176
+ that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
177
+ k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
178
+ cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
179
+ the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
180
+ Returns:
181
+ `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
182
+ """
183
+ cos = cos[position_ids].unsqueeze(unsqueeze_dim)
184
+ sin = sin[position_ids].unsqueeze(unsqueeze_dim)
185
+ q_embed = (q * cos) + (rotate_half(q) * sin)
186
+ k_embed = (k * cos) + (rotate_half(k) * sin)
187
+ return q_embed, k_embed
188
+
189
+
190
+ # Copied from transformers.models.mistral.modeling_mistral.MistralMLP with Mistral->Qwen2
191
+ class Qwen2MLP(nn.Module):
192
+ def __init__(self, config):
193
+ super().__init__()
194
+ self.config = config
195
+ self.hidden_size = config.hidden_size
196
+ self.intermediate_size = config.intermediate_size
197
+ self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
198
+ self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
199
+ self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False)
200
+ self.act_fn = ACT2FN[config.hidden_act]
201
+
202
+ def forward(self, x):
203
+ return self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
204
+
205
+
206
+ # Copied from transformers.models.llama.modeling_llama.repeat_kv
207
+ def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
208
+ """
209
+ This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
210
+ num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
211
+ """
212
+ batch, num_key_value_heads, slen, head_dim = hidden_states.shape
213
+ if n_rep == 1:
214
+ return hidden_states
215
+ hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim)
216
+ return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
217
+
218
+
219
+ class Qwen2Attention(nn.Module):
220
+ """
221
+ Multi-headed attention from 'Attention Is All You Need' paper. Modified to use sliding window attention: Longformer
222
+ and "Generating Long Sequences with Sparse Transformers".
223
+ """
224
+
225
+ def __init__(self, config: Qwen2Config, layer_idx: Optional[int] = None):
226
+ super().__init__()
227
+ self.config = config
228
+ self.layer_idx = layer_idx
229
+ if layer_idx is None:
230
+ logger.warning_once(
231
+ f"Instantiating {self.__class__.__name__} without passing `layer_idx` is not recommended and will "
232
+ "to errors during the forward call, if caching is used. Please make sure to provide a `layer_idx` "
233
+ "when creating this class."
234
+ )
235
+
236
+ self.hidden_size = config.hidden_size
237
+ self.num_heads = config.num_attention_heads
238
+ self.head_dim = self.hidden_size // self.num_heads
239
+ self.num_key_value_heads = config.num_key_value_heads
240
+ self.num_key_value_groups = self.num_heads // self.num_key_value_heads
241
+ self.max_position_embeddings = config.max_position_embeddings
242
+ self.rope_theta = config.rope_theta
243
+ self.is_causal = True
244
+ self.attention_dropout = config.attention_dropout
245
+
246
+ if (self.head_dim * self.num_heads) != self.hidden_size:
247
+ raise ValueError(
248
+ f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}"
249
+ f" and `num_heads`: {self.num_heads})."
250
+ )
251
+ self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=True)
252
+ self.k_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=True)
253
+ self.v_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=True)
254
+ self.o_proj = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=False)
255
+
256
+ self.rotary_emb = Qwen2RotaryEmbedding(
257
+ self.head_dim,
258
+ max_position_embeddings=self.max_position_embeddings,
259
+ base=self.rope_theta,
260
+ )
261
+
262
+ def forward(
263
+ self,
264
+ hidden_states: torch.Tensor,
265
+ attention_mask: Optional[torch.Tensor] = None,
266
+ position_ids: Optional[torch.LongTensor] = None,
267
+ past_key_value: Optional[Cache] = None,
268
+ output_attentions: bool = False,
269
+ use_cache: bool = False,
270
+ **kwargs,
271
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
272
+ if "padding_mask" in kwargs:
273
+ warnings.warn(
274
+ "Passing `padding_mask` is deprecated and will be removed in v4.37. Please make sure use `attention_mask` instead.`"
275
+ )
276
+ bsz, q_len, _ = hidden_states.size()
277
+
278
+ query_states = self.q_proj(hidden_states)
279
+ key_states = self.k_proj(hidden_states)
280
+ value_states = self.v_proj(hidden_states)
281
+
282
+ query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
283
+ key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
284
+ value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
285
+
286
+ kv_seq_len = key_states.shape[-2]
287
+ if past_key_value is not None:
288
+ if self.layer_idx is None:
289
+ raise ValueError(
290
+ f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
291
+ "for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
292
+ "with a layer index."
293
+ )
294
+ kv_seq_len += past_key_value.get_seq_length(self.layer_idx)
295
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
296
+ query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
297
+
298
+ if past_key_value is not None:
299
+ cache_kwargs = {"sin": sin, "cos": cos} # Specific to RoPE models
300
+ key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
301
+
302
+ # repeat k/v heads if n_kv_heads < n_heads
303
+ key_states = repeat_kv(key_states, self.num_key_value_groups)
304
+ value_states = repeat_kv(value_states, self.num_key_value_groups)
305
+
306
+ attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(self.head_dim)
307
+
308
+ if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
309
+ raise ValueError(
310
+ f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is"
311
+ f" {attn_weights.size()}"
312
+ )
313
+
314
+ if attention_mask is not None:
315
+ if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
316
+ raise ValueError(
317
+ f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
318
+ )
319
+
320
+ attn_weights = attn_weights + attention_mask
321
+
322
+ # upcast attention to fp32
323
+ attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype)
324
+ attn_weights = nn.functional.dropout(attn_weights, p=self.attention_dropout, training=self.training)
325
+ attn_output = torch.matmul(attn_weights, value_states)
326
+
327
+ if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim):
328
+ raise ValueError(
329
+ f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is"
330
+ f" {attn_output.size()}"
331
+ )
332
+
333
+ attn_output = attn_output.transpose(1, 2).contiguous()
334
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
335
+
336
+ attn_output = self.o_proj(attn_output)
337
+
338
+ if not output_attentions:
339
+ attn_weights = None
340
+
341
+ return attn_output, attn_weights, past_key_value
342
+
343
+
344
+ class Qwen2FlashAttention2(Qwen2Attention):
345
+ """
346
+ Qwen2 flash attention module, following Qwen2 attention module. This module inherits from `Qwen2Attention`
347
+ as the weights of the module stays untouched. The only required change would be on the forward pass
348
+ where it needs to correctly call the public API of flash attention and deal with padding tokens
349
+ in case the input contains any of them. Additionally, for sliding window attention, we apply SWA only to the bottom
350
+ config.max_window_layers layers.
351
+ """
352
+
353
+ # Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2.__init__
354
+ def __init__(self, *args, **kwargs):
355
+ super().__init__(*args, **kwargs)
356
+
357
+ # TODO: Should be removed once Flash Attention for RoCm is bumped to 2.1.
358
+ # flash_attn<2.1 generates top-left aligned causal mask, while what is needed here is bottom-right alignement, that was made default for flash_attn>=2.1. This attribute is used to handle this difference. Reference: https://github.com/Dao-AILab/flash-attention/releases/tag/v2.1.0.
359
+ # Beware that with flash_attn<2.1, using q_seqlen != k_seqlen (except for the case q_seqlen == 1) produces a wrong mask (top-left).
360
+ self._flash_attn_uses_top_left_mask = not is_flash_attn_greater_or_equal_2_10()
361
+
362
+ def forward(
363
+ self,
364
+ hidden_states: torch.Tensor,
365
+ attention_mask: Optional[torch.Tensor] = None,
366
+ position_ids: Optional[torch.LongTensor] = None,
367
+ past_key_value: Optional[Cache] = None,
368
+ output_attentions: bool = False,
369
+ use_cache: bool = False,
370
+ **kwargs,
371
+ ):
372
+ if "padding_mask" in kwargs:
373
+ warnings.warn(
374
+ "Passing `padding_mask` is deprecated and will be removed in v4.37. Please make sure use `attention_mask` instead.`"
375
+ )
376
+
377
+ # overwrite attention_mask with padding_mask
378
+ attention_mask = kwargs.pop("padding_mask")
379
+ bsz, q_len, _ = hidden_states.size()
380
+
381
+ query_states = self.q_proj(hidden_states)
382
+ key_states = self.k_proj(hidden_states)
383
+ value_states = self.v_proj(hidden_states)
384
+
385
+ query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
386
+ key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
387
+ value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
388
+
389
+ kv_seq_len = key_states.shape[-2]
390
+ if past_key_value is not None:
391
+ if self.layer_idx is None:
392
+ raise ValueError(
393
+ f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
394
+ "for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
395
+ "with a layer index."
396
+ )
397
+ kv_seq_len += past_key_value.get_seq_length(self.layer_idx)
398
+
399
+ # Because the input can be padded, the absolute sequence length depends on the max position id.
400
+ rotary_seq_len = max(kv_seq_len, position_ids[:, -1].max().item()) + 1
401
+ cos, sin = self.rotary_emb(value_states, seq_len=rotary_seq_len)
402
+
403
+ query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
404
+
405
+ use_sliding_windows = (
406
+ _flash_supports_window_size
407
+ and getattr(self.config, "sliding_window", None) is not None
408
+ and kv_seq_len > self.config.sliding_window
409
+ and self.config.use_sliding_window
410
+ )
411
+
412
+ if not _flash_supports_window_size:
413
+ logger.warning_once(
414
+ "The current flash attention version does not support sliding window attention, for a more memory efficient implementation"
415
+ " make sure to upgrade flash-attn library."
416
+ )
417
+
418
+ if past_key_value is not None:
419
+ # Activate slicing cache only if the config has a value `sliding_windows` attribute
420
+ cache_has_contents = past_key_value.get_seq_length(self.layer_idx) > 0
421
+ if (
422
+ getattr(self.config, "sliding_window", None) is not None
423
+ and kv_seq_len > self.config.sliding_window
424
+ and cache_has_contents
425
+ ):
426
+ slicing_tokens = 1 - self.config.sliding_window
427
+
428
+ past_key = past_key_value[self.layer_idx][0]
429
+ past_value = past_key_value[self.layer_idx][1]
430
+
431
+ past_key = past_key[:, :, slicing_tokens:, :].contiguous()
432
+ past_value = past_value[:, :, slicing_tokens:, :].contiguous()
433
+
434
+ if past_key.shape[-2] != self.config.sliding_window - 1:
435
+ raise ValueError(
436
+ f"past key must have a shape of (`batch_size, num_heads, self.config.sliding_window-1, head_dim`), got"
437
+ f" {past_key.shape}"
438
+ )
439
+
440
+ if attention_mask is not None:
441
+ attention_mask = attention_mask[:, slicing_tokens:]
442
+ attention_mask = torch.cat([attention_mask, torch.ones_like(attention_mask[:, -1:])], dim=-1)
443
+
444
+ cache_kwargs = {"sin": sin, "cos": cos} # Specific to RoPE models
445
+ key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
446
+
447
+ # repeat k/v heads if n_kv_heads < n_heads
448
+ key_states = repeat_kv(key_states, self.num_key_value_groups)
449
+ value_states = repeat_kv(value_states, self.num_key_value_groups)
450
+ dropout_rate = 0.0 if not self.training else self.attention_dropout
451
+
452
+ # In PEFT, usually we cast the layer norms in float32 for training stability reasons
453
+ # therefore the input hidden states gets silently casted in float32. Hence, we need
454
+ # cast them back in float16 just to be sure everything works as expected.
455
+ input_dtype = query_states.dtype
456
+ if input_dtype == torch.float32:
457
+ if torch.is_autocast_enabled():
458
+ target_dtype = torch.get_autocast_gpu_dtype()
459
+ # Handle the case where the model is quantized
460
+ elif hasattr(self.config, "_pre_quantization_dtype"):
461
+ target_dtype = self.config._pre_quantization_dtype
462
+ else:
463
+ target_dtype = self.q_proj.weight.dtype
464
+
465
+ logger.warning_once(
466
+ f"The input hidden states seems to be silently casted in float32, this might be related to"
467
+ f" the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in"
468
+ f" {target_dtype}."
469
+ )
470
+
471
+ query_states = query_states.to(target_dtype)
472
+ key_states = key_states.to(target_dtype)
473
+ value_states = value_states.to(target_dtype)
474
+
475
+ # Reashape to the expected shape for Flash Attention
476
+ query_states = query_states.transpose(1, 2)
477
+ key_states = key_states.transpose(1, 2)
478
+ value_states = value_states.transpose(1, 2)
479
+
480
+ attn_output = self._flash_attention_forward(
481
+ query_states,
482
+ key_states,
483
+ value_states,
484
+ attention_mask,
485
+ q_len,
486
+ dropout=dropout_rate,
487
+ use_sliding_windows=use_sliding_windows,
488
+ )
489
+
490
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size).contiguous()
491
+ attn_output = self.o_proj(attn_output)
492
+
493
+ if not output_attentions:
494
+ attn_weights = None
495
+
496
+ return attn_output, attn_weights, past_key_value
497
+
498
+ def _flash_attention_forward(
499
+ self,
500
+ query_states,
501
+ key_states,
502
+ value_states,
503
+ attention_mask,
504
+ query_length,
505
+ dropout=0.0,
506
+ softmax_scale=None,
507
+ use_sliding_windows=False,
508
+ ):
509
+ """
510
+ Calls the forward method of Flash Attention - if the input hidden states contain at least one padding token
511
+ first unpad the input, then computes the attention scores and pad the final attention scores.
512
+
513
+ Args:
514
+ query_states (`torch.Tensor`):
515
+ Input query states to be passed to Flash Attention API
516
+ key_states (`torch.Tensor`):
517
+ Input key states to be passed to Flash Attention API
518
+ value_states (`torch.Tensor`):
519
+ Input value states to be passed to Flash Attention API
520
+ attention_mask (`torch.Tensor`):
521
+ The padding mask - corresponds to a tensor of size `(batch_size, seq_len)` where 0 stands for the
522
+ position of padding tokens and 1 for the position of non-padding tokens.
523
+ dropout (`int`, *optional*):
524
+ Attention dropout
525
+ softmax_scale (`float`, *optional*):
526
+ The scaling of QK^T before applying softmax. Default to 1 / sqrt(head_dim)
527
+ use_sliding_windows (`bool`, *optional*):
528
+ Whether to activate sliding window attention.
529
+ """
530
+ if not self._flash_attn_uses_top_left_mask:
531
+ causal = self.is_causal
532
+ else:
533
+ # TODO: Remove the `query_length != 1` check once Flash Attention for RoCm is bumped to 2.1. For details, please see the comment in LlamaFlashAttention2 __init__.
534
+ causal = self.is_causal and query_length != 1
535
+
536
+ # Decide whether to use SWA or not by layer index.
537
+ if use_sliding_windows and self.layer_idx >= self.config.max_window_layers:
538
+ use_sliding_windows = False
539
+
540
+ # Contains at least one padding token in the sequence
541
+ if attention_mask is not None:
542
+ batch_size = query_states.shape[0]
543
+ query_states, key_states, value_states, indices_q, cu_seq_lens, max_seq_lens = self._upad_input(
544
+ query_states, key_states, value_states, attention_mask, query_length
545
+ )
546
+
547
+ cu_seqlens_q, cu_seqlens_k = cu_seq_lens
548
+ max_seqlen_in_batch_q, max_seqlen_in_batch_k = max_seq_lens
549
+
550
+ if not use_sliding_windows:
551
+ attn_output_unpad = flash_attn_varlen_func(
552
+ query_states,
553
+ key_states,
554
+ value_states,
555
+ cu_seqlens_q=cu_seqlens_q,
556
+ cu_seqlens_k=cu_seqlens_k,
557
+ max_seqlen_q=max_seqlen_in_batch_q,
558
+ max_seqlen_k=max_seqlen_in_batch_k,
559
+ dropout_p=dropout,
560
+ softmax_scale=softmax_scale,
561
+ causal=causal,
562
+ )
563
+ else:
564
+ attn_output_unpad = flash_attn_varlen_func(
565
+ query_states,
566
+ key_states,
567
+ value_states,
568
+ cu_seqlens_q=cu_seqlens_q,
569
+ cu_seqlens_k=cu_seqlens_k,
570
+ max_seqlen_q=max_seqlen_in_batch_q,
571
+ max_seqlen_k=max_seqlen_in_batch_k,
572
+ dropout_p=dropout,
573
+ softmax_scale=softmax_scale,
574
+ causal=causal,
575
+ window_size=(self.config.sliding_window, self.config.sliding_window),
576
+ )
577
+
578
+ attn_output = pad_input(attn_output_unpad, indices_q, batch_size, query_length)
579
+ else:
580
+ if not use_sliding_windows:
581
+ attn_output = flash_attn_func(
582
+ query_states,
583
+ key_states,
584
+ value_states,
585
+ dropout,
586
+ softmax_scale=softmax_scale,
587
+ causal=causal,
588
+ )
589
+ else:
590
+ attn_output = flash_attn_func(
591
+ query_states,
592
+ key_states,
593
+ value_states,
594
+ dropout,
595
+ softmax_scale=softmax_scale,
596
+ causal=causal,
597
+ window_size=(self.config.sliding_window, self.config.sliding_window),
598
+ )
599
+
600
+ return attn_output
601
+
602
+ # Copied from transformers.models.mistral.modeling_mistral.MistralFlashAttention2._upad_input
603
+ def _upad_input(self, query_layer, key_layer, value_layer, attention_mask, query_length):
604
+ batch_size, kv_seq_len, num_heads, head_dim = key_layer.shape
605
+
606
+ # On the first iteration we need to properly re-create the padding mask
607
+ # by slicing it on the proper place
608
+ if kv_seq_len != attention_mask.shape[-1]:
609
+ attention_mask_num_tokens = attention_mask.shape[-1]
610
+ attention_mask = attention_mask[:, attention_mask_num_tokens - kv_seq_len :]
611
+
612
+ indices_k, cu_seqlens_k, max_seqlen_in_batch_k = _get_unpad_data(attention_mask)
613
+
614
+ key_layer = index_first_axis(key_layer.reshape(batch_size * kv_seq_len, num_heads, head_dim), indices_k)
615
+ value_layer = index_first_axis(value_layer.reshape(batch_size * kv_seq_len, num_heads, head_dim), indices_k)
616
+
617
+ if query_length == kv_seq_len:
618
+ query_layer = index_first_axis(
619
+ query_layer.reshape(batch_size * kv_seq_len, num_heads, head_dim), indices_k
620
+ )
621
+ cu_seqlens_q = cu_seqlens_k
622
+ max_seqlen_in_batch_q = max_seqlen_in_batch_k
623
+ indices_q = indices_k
624
+ elif query_length == 1:
625
+ max_seqlen_in_batch_q = 1
626
+ cu_seqlens_q = torch.arange(
627
+ batch_size + 1, dtype=torch.int32, device=query_layer.device
628
+ ) # There is a memcpy here, that is very bad.
629
+ indices_q = cu_seqlens_q[:-1]
630
+ query_layer = query_layer.squeeze(1)
631
+ else:
632
+ # The -q_len: slice assumes left padding.
633
+ attention_mask = attention_mask[:, -query_length:]
634
+ query_layer, indices_q, cu_seqlens_q, max_seqlen_in_batch_q = unpad_input(query_layer, attention_mask)
635
+
636
+ return (
637
+ query_layer,
638
+ key_layer,
639
+ value_layer,
640
+ indices_q,
641
+ (cu_seqlens_q, cu_seqlens_k),
642
+ (max_seqlen_in_batch_q, max_seqlen_in_batch_k),
643
+ )
644
+
645
+
646
+ # Copied from transformers.models.llama.modeling_llama.LlamaSdpaAttention with Llama->Qwen2
647
+ class Qwen2SdpaAttention(Qwen2Attention):
648
+ """
649
+ Qwen2 attention module using torch.nn.functional.scaled_dot_product_attention. This module inherits from
650
+ `Qwen2Attention` as the weights of the module stays untouched. The only changes are on the forward pass to adapt to
651
+ SDPA API.
652
+ """
653
+
654
+ # Adapted from Qwen2Attention.forward
655
+ def forward(
656
+ self,
657
+ hidden_states: torch.Tensor,
658
+ attention_mask: Optional[torch.Tensor] = None,
659
+ position_ids: Optional[torch.LongTensor] = None,
660
+ past_key_value: Optional[Cache] = None,
661
+ output_attentions: bool = False,
662
+ use_cache: bool = False,
663
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
664
+ if output_attentions:
665
+ # TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented.
666
+ logger.warning_once(
667
+ "Qwen2Model is using Qwen2SdpaAttention, but `torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to the manual attention implementation, "
668
+ 'but specifying the manual implementation will be required from Transformers version v5.0.0 onwards. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.'
669
+ )
670
+ return super().forward(
671
+ hidden_states=hidden_states,
672
+ attention_mask=attention_mask,
673
+ position_ids=position_ids,
674
+ past_key_value=past_key_value,
675
+ output_attentions=output_attentions,
676
+ use_cache=use_cache,
677
+ )
678
+
679
+ bsz, q_len, _ = hidden_states.size()
680
+
681
+ query_states = self.q_proj(hidden_states)
682
+ key_states = self.k_proj(hidden_states)
683
+ value_states = self.v_proj(hidden_states)
684
+
685
+ query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
686
+ key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
687
+ value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
688
+
689
+ kv_seq_len = key_states.shape[-2]
690
+ if past_key_value is not None:
691
+ kv_seq_len += past_key_value.get_seq_length(self.layer_idx)
692
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
693
+
694
+ query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
695
+
696
+ if past_key_value is not None:
697
+ cache_kwargs = {"sin": sin, "cos": cos} # Specific to RoPE models
698
+ key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
699
+
700
+ key_states = repeat_kv(key_states, self.num_key_value_groups)
701
+ value_states = repeat_kv(value_states, self.num_key_value_groups)
702
+
703
+ if attention_mask is not None:
704
+ if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
705
+ raise ValueError(
706
+ f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
707
+ )
708
+
709
+ # SDPA with memory-efficient backend is currently (torch==2.1.2) bugged with non-contiguous inputs with custom attn_mask,
710
+ # Reference: https://github.com/pytorch/pytorch/issues/112577.
711
+ if query_states.device.type == "cuda" and attention_mask is not None:
712
+ query_states = query_states.contiguous()
713
+ key_states = key_states.contiguous()
714
+ value_states = value_states.contiguous()
715
+
716
+ attn_output = torch.nn.functional.scaled_dot_product_attention(
717
+ query_states,
718
+ key_states,
719
+ value_states,
720
+ attn_mask=attention_mask,
721
+ dropout_p=self.attention_dropout if self.training else 0.0,
722
+ is_causal=False,
723
+ )
724
+
725
+ attn_output = attn_output.transpose(1, 2).contiguous()
726
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
727
+
728
+ attn_output = self.o_proj(attn_output)
729
+
730
+ return attn_output, None, past_key_value
731
+
732
+
733
+ class Qwen2SdpaAttentionGqa(Qwen2Attention):
734
+ """
735
+ Qwen2 attention module using torch.nn.functional.scaled_dot_product_attention. This module inherits from
736
+ `Qwen2Attention` as the weights of the module stays untouched. The only changes are on the forward pass to adapt to
737
+ SDPA API.
738
+ """
739
+
740
+ # Adapted from Qwen2Attention.forward
741
+ def forward(
742
+ self,
743
+ hidden_states: torch.Tensor,
744
+ attention_mask: Optional[torch.Tensor] = None,
745
+ position_ids: Optional[torch.LongTensor] = None,
746
+ past_key_value: Optional[Cache] = None,
747
+ output_attentions: bool = False,
748
+ use_cache: bool = False,
749
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
750
+ if output_attentions:
751
+ # TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented.
752
+ logger.warning_once(
753
+ "Qwen2Model is using Qwen2SdpaAttention, but `torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to the manual attention implementation, "
754
+ 'but specifying the manual implementation will be required from Transformers version v5.0.0 onwards. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.'
755
+ )
756
+ return super().forward(
757
+ hidden_states=hidden_states,
758
+ attention_mask=attention_mask,
759
+ position_ids=position_ids,
760
+ past_key_value=past_key_value,
761
+ output_attentions=output_attentions,
762
+ use_cache=use_cache,
763
+ )
764
+
765
+ bsz, q_len, _ = hidden_states.size()
766
+
767
+ query_states = self.q_proj(hidden_states)
768
+ key_states = self.k_proj(hidden_states)
769
+ value_states = self.v_proj(hidden_states)
770
+
771
+ query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
772
+ key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
773
+ value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
774
+
775
+ kv_seq_len = key_states.shape[-2]
776
+ if past_key_value is not None:
777
+ kv_seq_len += past_key_value.get_seq_length(self.layer_idx)
778
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
779
+
780
+ query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
781
+
782
+ if past_key_value is not None:
783
+ cache_kwargs = {"sin": sin, "cos": cos} # Specific to RoPE models
784
+ key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
785
+
786
+ # key_states = repeat_kv(key_states, self.num_key_value_groups)
787
+ # value_states = repeat_kv(value_states, self.num_key_value_groups)
788
+
789
+ if attention_mask is not None:
790
+ if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
791
+ raise ValueError(
792
+ f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
793
+ )
794
+
795
+ # SDPA with memory-efficient backend is currently (torch==2.1.2) bugged with non-contiguous inputs with custom attn_mask,
796
+ # Reference: https://github.com/pytorch/pytorch/issues/112577.
797
+ if query_states.device.type == "cuda" and attention_mask is not None:
798
+ query_states = query_states.contiguous()
799
+ key_states = key_states.contiguous()
800
+ value_states = value_states.contiguous()
801
+
802
+ with torch.backends.cuda.sdp_kernel(enable_flash=True,
803
+ enable_math=True,
804
+ enable_mem_efficient=False):
805
+
806
+ attn_output = torch.nn.functional.scaled_dot_product_attention(
807
+ query_states,
808
+ key_states,
809
+ value_states,
810
+ attn_mask=attention_mask,
811
+ enable_gqa=True,
812
+ dropout_p=self.attention_dropout if self.training else 0.0,
813
+ is_causal=False,
814
+ )
815
+
816
+ attn_output = attn_output.transpose(1, 2).contiguous()
817
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
818
+
819
+ attn_output = self.o_proj(attn_output)
820
+
821
+ return attn_output, None, past_key_value
822
+
823
+
824
+ class Qwen2MagiAttention(Qwen2Attention):
825
+ """
826
+ Qwen2 attention using MagiAttention for efficient training with MTP packing support.
827
+
828
+ MagiAttention uses range-based sparse attention patterns:
829
+ - q_ranges/k_ranges define which query/key ranges attend to each other
830
+ - attn_type_map specifies causal(1) or full(0) attention for each range pair
831
+ """
832
+
833
+ def __init__(self, *args, **kwargs):
834
+ super().__init__(*args, **kwargs)
835
+ if not _MAGI_AVAILABLE:
836
+ raise ImportError(
837
+ "magi_attention is not installed. Install with: pip install magi-attention"
838
+ )
839
+ self.softmax_scale = self.head_dim ** -0.5
840
+
841
+ def forward(
842
+ self,
843
+ hidden_states: torch.Tensor,
844
+ attention_mask: Optional[dict] = None, # magi_plan dict
845
+ position_ids: Optional[torch.LongTensor] = None,
846
+ past_key_value: Optional[Cache] = None,
847
+ output_attentions: bool = False,
848
+ use_cache: bool = False,
849
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
850
+ if output_attentions:
851
+ raise NotImplementedError('MagiAttention does not support output_attentions=True')
852
+
853
+ bsz, q_len, _ = hidden_states.size()
854
+ assert bsz == 1, "MagiAttention only supports batch_size=1 (use packing instead)"
855
+
856
+ query_states = self.q_proj(hidden_states)
857
+ key_states = self.k_proj(hidden_states)
858
+ value_states = self.v_proj(hidden_states)
859
+
860
+ # Magi expects [T, H, D] format (no batch dimension)
861
+ query_states = query_states.view(q_len, self.num_heads, self.head_dim)
862
+ key_states = key_states.view(q_len, self.num_key_value_heads, self.head_dim)
863
+ value_states = value_states.view(q_len, self.num_key_value_heads, self.head_dim)
864
+
865
+ kv_seq_len = q_len
866
+ if past_key_value is not None:
867
+ if self.layer_idx is None:
868
+ raise ValueError(
869
+ f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
870
+ "for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
871
+ "with a layer index."
872
+ )
873
+ kv_seq_len += past_key_value.get_seq_length(self.layer_idx)
874
+
875
+ cos, sin = self.rotary_emb(value_states.unsqueeze(0).transpose(1, 2), seq_len=kv_seq_len)
876
+
877
+ # Apply RoPE: need [B, H, L, D] format for apply_rotary_pos_emb
878
+ q_for_rope = query_states.unsqueeze(0).transpose(1, 2) # [1, H, L, D]
879
+ k_for_rope = key_states.unsqueeze(0).transpose(1, 2) # [1, Hkv, L, D]
880
+ q_for_rope, k_for_rope = apply_rotary_pos_emb(q_for_rope, k_for_rope, cos, sin, position_ids)
881
+
882
+ # Back to [T, H, D]
883
+ query_states = q_for_rope.squeeze(0).transpose(0, 1).contiguous() # [L, H, D]
884
+ key_states = k_for_rope.squeeze(0).transpose(0, 1).contiguous() # [L, Hkv, D]
885
+
886
+ if past_key_value is not None:
887
+ cache_kwargs = {"sin": sin, "cos": cos}
888
+ # Note: Magi doesn't support KV cache in training, this is for potential future use
889
+ key_states_4d = key_states.unsqueeze(0).transpose(1, 2)
890
+ value_states_4d = value_states.unsqueeze(0).transpose(1, 2)
891
+ key_states_4d, value_states_4d = past_key_value.update(
892
+ key_states_4d, value_states_4d, self.layer_idx, cache_kwargs
893
+ )
894
+ key_states = key_states_4d.squeeze(0).transpose(0, 1).contiguous()
895
+ value_states = value_states_4d.squeeze(0).transpose(0, 1).contiguous()
896
+
897
+ # Run Magi Attention
898
+ # attention_mask is a magi_plan dict with q_ranges, k_ranges, attn_type_map, etc.
899
+
900
+ attn_output, _ = flex_flash_attn_func(
901
+ query_states.contiguous(),
902
+ key_states.contiguous(),
903
+ value_states.contiguous(),
904
+ q_ranges=attention_mask["q_ranges"],
905
+ k_ranges=attention_mask["k_ranges"],
906
+ attn_type_map=attention_mask["attn_type_map"],
907
+ softmax_scale=self.softmax_scale,
908
+ softcap=0.0,
909
+ deterministic=False,
910
+ ) # [T, H, D]
911
+
912
+ # Reshape to [B, L, H*D]
913
+ attn_output = attn_output.view(1, q_len, self.hidden_size)
914
+ attn_output = self.o_proj(attn_output)
915
+
916
+ return attn_output, None, past_key_value
917
+
918
+
919
+ QWEN2_ATTENTION_CLASSES = {
920
+ "eager": Qwen2Attention,
921
+ "flash_attention_2": Qwen2FlashAttention2,
922
+ "sdpa": Qwen2SdpaAttention,
923
+ "magi": Qwen2MagiAttention,
924
+ }
925
+
926
+
927
+ class Qwen2DecoderLayer(nn.Module):
928
+ def __init__(self, config: Qwen2Config, layer_idx: int):
929
+ super().__init__()
930
+ self.hidden_size = config.hidden_size
931
+
932
+ if config._attn_implementation == 'magi' and not _MAGI_AVAILABLE:
933
+ if is_flash_attn_2_available():
934
+ logger.warning_once(
935
+ 'magi_attention not available, falling back to flash_attention_2'
936
+ )
937
+ config._attn_implementation = 'flash_attention_2'
938
+ else:
939
+ logger.warning_once(
940
+ 'magi_attention not available, falling back to sdpa'
941
+ )
942
+ config._attn_implementation = 'sdpa'
943
+ if config._attn_implementation == 'flash_attention_2' and not is_flash_attn_2_available():
944
+ logger.warning_once(
945
+ 'flash_attn is not available, falling back to sdpa'
946
+ )
947
+ config._attn_implementation = 'sdpa'
948
+
949
+ self.self_attn = QWEN2_ATTENTION_CLASSES[config._attn_implementation](config, layer_idx)
950
+
951
+ self.mlp = Qwen2MLP(config)
952
+ self.input_layernorm = Qwen2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
953
+ self.post_attention_layernorm = Qwen2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
954
+
955
+ def forward(
956
+ self,
957
+ hidden_states: torch.Tensor,
958
+ attention_mask: Optional[torch.Tensor] = None,
959
+ position_ids: Optional[torch.LongTensor] = None,
960
+ past_key_value: Optional[Tuple[torch.Tensor]] = None,
961
+ output_attentions: Optional[bool] = False,
962
+ use_cache: Optional[bool] = False,
963
+ **kwargs,
964
+ ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
965
+ if "padding_mask" in kwargs:
966
+ warnings.warn(
967
+ "Passing `padding_mask` is deprecated and will be removed in v4.37. "
968
+ "Please make sure use `attention_mask` instead.`"
969
+ )
970
+ """
971
+ Args:
972
+ hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
973
+ attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
974
+ `(batch, sequence_length)` where padding elements are indicated by 0.
975
+ output_attentions (`bool`, *optional*):
976
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under
977
+ returned tensors for more detail.
978
+ use_cache (`bool`, *optional*):
979
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
980
+ (see `past_key_values`).
981
+ past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
982
+ """
983
+
984
+ residual = hidden_states
985
+
986
+ hidden_states = self.input_layernorm(hidden_states)
987
+
988
+ # Self Attention
989
+ hidden_states, self_attn_weights, present_key_value = self.self_attn(
990
+ hidden_states=hidden_states,
991
+ attention_mask=attention_mask,
992
+ position_ids=position_ids,
993
+ past_key_value=past_key_value,
994
+ output_attentions=output_attentions,
995
+ use_cache=use_cache,
996
+ )
997
+ hidden_states = residual + hidden_states
998
+
999
+ # Fully Connected
1000
+ residual = hidden_states
1001
+ hidden_states = self.post_attention_layernorm(hidden_states)
1002
+ hidden_states = self.mlp(hidden_states)
1003
+ hidden_states = residual + hidden_states
1004
+
1005
+ outputs = (hidden_states,)
1006
+
1007
+ if output_attentions:
1008
+ outputs += (self_attn_weights,)
1009
+
1010
+ if use_cache:
1011
+ outputs += (present_key_value,)
1012
+
1013
+ return outputs
1014
+
1015
+
1016
+ QWEN2_START_DOCSTRING = r"""
1017
+ This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
1018
+ library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
1019
+ etc.)
1020
+
1021
+ This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
1022
+ Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
1023
+ and behavior.
1024
+
1025
+ Parameters:
1026
+ config ([`Qwen2Config`]):
1027
+ Model configuration class with all the parameters of the model. Initializing with a config file does not
1028
+ load the weights associated with the model, only the configuration. Check out the
1029
+ [`~PreTrainedModel.from_pretrained`] method to load the model weights.
1030
+ """
1031
+
1032
+
1033
+ @add_start_docstrings(
1034
+ "The bare Qwen2 Model outputting raw hidden-states without any specific head on top.",
1035
+ QWEN2_START_DOCSTRING,
1036
+ )
1037
+ class Qwen2PreTrainedModel(PreTrainedModel):
1038
+ config_class = Qwen2Config
1039
+ base_model_prefix = "model"
1040
+ supports_gradient_checkpointing = True
1041
+ _no_split_modules = ["Qwen2DecoderLayer"]
1042
+ _skip_keys_device_placement = "past_key_values"
1043
+ _supports_flash_attn_2 = True
1044
+ _supports_sdpa = True
1045
+ _supports_cache_class = True
1046
+
1047
+ @classmethod
1048
+ def _autoset_attn_implementation(cls, config, *args, **kwargs):
1049
+ if getattr(config, '_attn_implementation', None) == 'magi':
1050
+ return config
1051
+ return super()._autoset_attn_implementation(config, *args, **kwargs)
1052
+
1053
+ def _check_and_adjust_attn_implementation(self, attn_implementation, is_init_check=False):
1054
+ if attn_implementation == "magi":
1055
+ return "magi"
1056
+ return super()._check_and_adjust_attn_implementation(attn_implementation, is_init_check)
1057
+
1058
+ def _init_weights(self, module):
1059
+ std = self.config.initializer_range
1060
+ if isinstance(module, nn.Linear):
1061
+ module.weight.data.normal_(mean=0.0, std=std)
1062
+ if module.bias is not None:
1063
+ module.bias.data.zero_()
1064
+ elif isinstance(module, nn.Embedding):
1065
+ module.weight.data.normal_(mean=0.0, std=std)
1066
+ if module.padding_idx is not None:
1067
+ module.weight.data[module.padding_idx].zero_()
1068
+
1069
+
1070
+ QWEN2_INPUTS_DOCSTRING = r"""
1071
+ Args:
1072
+ input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
1073
+ Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
1074
+ it.
1075
+
1076
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
1077
+ [`PreTrainedTokenizer.__call__`] for details.
1078
+
1079
+ [What are input IDs?](../glossary#input-ids)
1080
+ attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
1081
+ Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
1082
+
1083
+ - 1 for tokens that are **not masked**,
1084
+ - 0 for tokens that are **masked**.
1085
+
1086
+ [What are attention masks?](../glossary#attention-mask)
1087
+
1088
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
1089
+ [`PreTrainedTokenizer.__call__`] for details.
1090
+
1091
+ If `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see
1092
+ `past_key_values`).
1093
+
1094
+ If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
1095
+ and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
1096
+ information on the default strategy.
1097
+
1098
+ - 1 indicates the head is **not masked**,
1099
+ - 0 indicates the head is **masked**.
1100
+ position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
1101
+ Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
1102
+ config.n_positions - 1]`.
1103
+
1104
+ [What are position IDs?](../glossary#position-ids)
1105
+ past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*):
1106
+ Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
1107
+ blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values`
1108
+ returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.
1109
+
1110
+ Two formats are allowed:
1111
+ - a [`~cache_utils.Cache`] instance;
1112
+ - Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of
1113
+ shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy
1114
+ cache format.
1115
+
1116
+ The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the
1117
+ legacy cache format will be returned.
1118
+
1119
+ If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't
1120
+ have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids`
1121
+ of shape `(batch_size, sequence_length)`.
1122
+ inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
1123
+ Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
1124
+ is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
1125
+ model's internal embedding lookup matrix.
1126
+ use_cache (`bool`, *optional*):
1127
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
1128
+ `past_key_values`).
1129
+ output_attentions (`bool`, *optional*):
1130
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
1131
+ tensors for more detail.
1132
+ output_hidden_states (`bool`, *optional*):
1133
+ Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
1134
+ more detail.
1135
+ return_dict (`bool`, *optional*):
1136
+ Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
1137
+ """
1138
+
1139
+
1140
+ @add_start_docstrings(
1141
+ "The bare Qwen2 Model outputting raw hidden-states without any specific head on top.",
1142
+ QWEN2_START_DOCSTRING,
1143
+ )
1144
+ class Qwen2Model(Qwen2PreTrainedModel):
1145
+ """
1146
+ Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`Qwen2DecoderLayer`]
1147
+
1148
+ Args:
1149
+ config: Qwen2Config
1150
+ """
1151
+
1152
+ def __init__(self, config: Qwen2Config):
1153
+ super().__init__(config)
1154
+ self.padding_idx = config.pad_token_id
1155
+ self.vocab_size = config.vocab_size
1156
+
1157
+ self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
1158
+ self.layers = nn.ModuleList(
1159
+ [Qwen2DecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
1160
+ )
1161
+ self._attn_implementation = config._attn_implementation
1162
+ self.norm = Qwen2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
1163
+
1164
+ self.gradient_checkpointing = False
1165
+ # Initialize weights and apply final processing
1166
+ self.post_init()
1167
+
1168
+ self.block_size = getattr(config, 'block_size', 6)
1169
+ self.causal_attn = getattr(config, 'causal_attn', False)
1170
+ self.text_mask_token_id = getattr(config, 'text_mask_token_id', 151676)
1171
+
1172
+
1173
+ def get_input_embeddings(self):
1174
+ return self.embed_tokens
1175
+
1176
+ def set_input_embeddings(self, value):
1177
+ self.embed_tokens = value
1178
+
1179
+ def image_processing(self, input_ids, visual_features, image_token_index):
1180
+ if visual_features is not None:
1181
+ input_embeds = self.get_input_embeddings()(input_ids)
1182
+ B, N, C = input_embeds.shape
1183
+ input_embeds = input_embeds.reshape(B * N, C)
1184
+
1185
+ input_ids = input_ids.reshape(B * N)
1186
+ selected = (input_ids == image_token_index)
1187
+ assert selected.sum() != 0
1188
+ input_embeds[selected] = visual_features.reshape(-1, C).to(input_embeds.device)
1189
+ input_embeds = input_embeds.reshape(B, N, C)
1190
+ else:
1191
+ input_embeds = self.get_input_embeddings()(input_ids)
1192
+ return input_embeds
1193
+
1194
+ @add_start_docstrings_to_model_forward(QWEN2_INPUTS_DOCSTRING)
1195
+ def forward(
1196
+ self,
1197
+ input_ids: torch.LongTensor = None,
1198
+ visual_features: Optional[torch.FloatTensor] = None,
1199
+ image_token_index: int = None,
1200
+ attention_mask: Optional[torch.Tensor] = None,
1201
+ position_ids: Optional[torch.LongTensor] = None,
1202
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1203
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1204
+ use_cache: Optional[bool] = None,
1205
+ output_attentions: Optional[bool] = None,
1206
+ output_hidden_states: Optional[bool] = None,
1207
+ return_dict: Optional[bool] = None,
1208
+ ) -> Union[Tuple, BaseModelOutputWithPast]:
1209
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
1210
+ output_hidden_states = (
1211
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
1212
+ )
1213
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
1214
+
1215
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1216
+
1217
+ # retrieve input_ids and inputs_embeds
1218
+ if input_ids is not None and inputs_embeds is not None:
1219
+ raise ValueError("You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time")
1220
+ elif input_ids is not None:
1221
+ batch_size, seq_length = input_ids.shape
1222
+ elif inputs_embeds is not None:
1223
+ batch_size, seq_length, _ = inputs_embeds.shape
1224
+ else:
1225
+ raise ValueError("You have to specify either decoder_input_ids or decoder_inputs_embeds")
1226
+
1227
+ if self.gradient_checkpointing and self.training:
1228
+ if use_cache:
1229
+ logger.warning_once(
1230
+ "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
1231
+ )
1232
+ use_cache = False
1233
+
1234
+ past_key_values_length = 0
1235
+
1236
+ if use_cache:
1237
+ use_legacy_cache = not isinstance(past_key_values, Cache)
1238
+ if use_legacy_cache:
1239
+ if past_key_values is None:
1240
+ past_key_values = DynamicCache()
1241
+ else:
1242
+ past_key_values = DynamicCache.from_legacy_cache(past_key_values)
1243
+ past_key_values_length = past_key_values.get_seq_length()
1244
+
1245
+ if position_ids is None:
1246
+ device = input_ids.device if input_ids is not None else inputs_embeds.device
1247
+ position_ids = torch.arange(
1248
+ past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device
1249
+ )
1250
+ position_ids = position_ids.unsqueeze(0).view(-1, seq_length)
1251
+ else:
1252
+ position_ids = position_ids.view(-1, seq_length).long()
1253
+
1254
+ if inputs_embeds is None:
1255
+ inputs_embeds = self.image_processing(input_ids, visual_features, image_token_index)
1256
+
1257
+ if attention_mask is not None and self._attn_implementation == "magi" and use_cache:
1258
+ is_padding_right = attention_mask[:, -1].sum().item() != batch_size
1259
+ if is_padding_right:
1260
+ raise ValueError(
1261
+ "You are attempting to perform batched generation with padding_side='right'"
1262
+ " this may lead to unexpected behaviour for Flash Attention version of Qwen2. Make sure to "
1263
+ " call `tokenizer.padding_side = 'left'` before tokenizing the input. "
1264
+ )
1265
+
1266
+ device = input_ids.device if input_ids is not None else inputs_embeds.device
1267
+
1268
+ x0_len = find_prefix_seq_length_by_pe(position_ids).to(device=device)
1269
+
1270
+ def _prepare_block_mask_for_inference(attention_mask):
1271
+ attention_mask = _prepare_4d_causal_attention_mask(
1272
+ attention_mask,
1273
+ (batch_size, seq_length),
1274
+ inputs_embeds,
1275
+ past_key_values_length,
1276
+ sliding_window=self.config.sliding_window,
1277
+ )
1278
+ # switch to ar mode
1279
+ if seq_length == 1 or (input_ids is not None and input_ids[0][-1].item() != self.text_mask_token_id):
1280
+ return attention_mask
1281
+
1282
+
1283
+ if attention_mask is None or len(attention_mask.shape) != 4:
1284
+ return attention_mask
1285
+
1286
+ # For SDLM, the generation window should set to bidirectional attention
1287
+ if use_cache:
1288
+ update_mask_func = partial(
1289
+ update_causal_mask_for_one_gen_window_2d,
1290
+ block_size=self.block_size,
1291
+ use_cache=use_cache,
1292
+ causal_attn=self.causal_attn,
1293
+ )
1294
+ else:
1295
+ update_mask_func = partial(
1296
+ update_causal_mask_with_pad_non_visible_2d,
1297
+ block_size=self.block_size,
1298
+ text_mask_token_id=self.text_mask_token_id,
1299
+ causal_attn=self.causal_attn,
1300
+ )
1301
+
1302
+ new_attention_mask = []
1303
+ for b in range(attention_mask.shape[0]):
1304
+ new_attention_mask.append(
1305
+ update_mask_func(
1306
+ input_ids[b],
1307
+ attention_mask[b][0],
1308
+ ).unsqueeze(0)
1309
+ )
1310
+ return torch.stack(new_attention_mask, dim=0)
1311
+
1312
+ def _prepare_block_mask_for_training():
1313
+ block_mask, _ = create_block_diff_mask_by_pe_4d(
1314
+ block_size=self.block_size,
1315
+ x0_len_list=x0_len,
1316
+ position_ids=position_ids,
1317
+ causal_attn=self.causal_attn,
1318
+ )
1319
+ return block_mask
1320
+
1321
+ if self._attn_implementation == "magi":
1322
+ ar_decode = seq_length == 1 or (input_ids is not None and input_ids[0][-1].item() != self.text_mask_token_id)
1323
+ attention_mask = build_magi_ranges(
1324
+ kv_len=seq_length + past_key_values_length,
1325
+ q_len=seq_length,
1326
+ block_size=self.block_size,
1327
+ ar_decode=ar_decode,
1328
+ device=device
1329
+ )
1330
+
1331
+ elif self._attn_implementation == "sdpa":
1332
+ attention_mask = _prepare_block_mask_for_training() if self.training else _prepare_block_mask_for_inference(attention_mask)
1333
+
1334
+ else:
1335
+ raise NotImplementedError(f'{self._attn_implementation=}')
1336
+
1337
+
1338
+ hidden_states = inputs_embeds
1339
+
1340
+ # decoder layers
1341
+ all_hidden_states = () if output_hidden_states else None
1342
+ all_self_attns = () if output_attentions else None
1343
+ next_decoder_cache = None
1344
+
1345
+ for decoder_layer in self.layers:
1346
+ if output_hidden_states:
1347
+ all_hidden_states += (hidden_states,)
1348
+
1349
+ if self.gradient_checkpointing and self.training:
1350
+ layer_outputs = self._gradient_checkpointing_func(
1351
+ decoder_layer.__call__,
1352
+ hidden_states,
1353
+ attention_mask,
1354
+ position_ids,
1355
+ past_key_values,
1356
+ output_attentions,
1357
+ use_cache,
1358
+ )
1359
+ else:
1360
+ layer_outputs = decoder_layer(
1361
+ hidden_states,
1362
+ attention_mask=attention_mask,
1363
+ position_ids=position_ids,
1364
+ past_key_value=past_key_values,
1365
+ output_attentions=output_attentions,
1366
+ use_cache=use_cache,
1367
+ )
1368
+
1369
+ hidden_states = layer_outputs[0]
1370
+
1371
+ if use_cache:
1372
+ next_decoder_cache = layer_outputs[2 if output_attentions else 1]
1373
+
1374
+ if output_attentions:
1375
+ all_self_attns += (layer_outputs[1],)
1376
+
1377
+ hidden_states = self.norm(hidden_states)
1378
+
1379
+ # add hidden states from the last decoder layer
1380
+ if output_hidden_states:
1381
+ all_hidden_states += (hidden_states,)
1382
+
1383
+ next_cache = None
1384
+ if use_cache:
1385
+ next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache
1386
+
1387
+ if not return_dict:
1388
+ return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None)
1389
+ return BaseModelOutputWithPast(
1390
+ last_hidden_state=hidden_states,
1391
+ past_key_values=next_cache,
1392
+ hidden_states=all_hidden_states,
1393
+ attentions=all_self_attns,
1394
+ )
1395
+
1396
+
1397
+ class Qwen2ForCausalLM(Qwen2PreTrainedModel):
1398
+ _tied_weights_keys = ["lm_head.weight"]
1399
+
1400
+ def __init__(self, config):
1401
+ super().__init__(config)
1402
+ self.model = Qwen2Model(config)
1403
+ self.vocab_size = config.vocab_size
1404
+ self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
1405
+
1406
+ self.text_mask_token_id = getattr(config, 'text_mask_token_id', 151676)
1407
+
1408
+ # Initialize weights and apply final processing
1409
+ self.post_init()
1410
+
1411
+
1412
+ def get_input_embeddings(self):
1413
+ return self.model.embed_tokens
1414
+
1415
+ def set_input_embeddings(self, value):
1416
+ self.model.embed_tokens = value
1417
+
1418
+ def get_output_embeddings(self):
1419
+ return self.lm_head
1420
+
1421
+ def set_output_embeddings(self, new_embeddings):
1422
+ self.lm_head = new_embeddings
1423
+
1424
+ def set_decoder(self, decoder):
1425
+ self.model = decoder
1426
+
1427
+ def get_decoder(self):
1428
+ return self.model
1429
+
1430
+ @add_start_docstrings_to_model_forward(QWEN2_INPUTS_DOCSTRING)
1431
+ @replace_return_docstrings(output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC)
1432
+ def forward(
1433
+ self,
1434
+ input_ids: torch.LongTensor = None,
1435
+ visual_features: Optional[torch.FloatTensor] = None,
1436
+ image_token_index: int = None,
1437
+ attention_mask: Optional[torch.Tensor] = None,
1438
+ position_ids: Optional[torch.LongTensor] = None,
1439
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1440
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1441
+ labels: Optional[torch.LongTensor] = None,
1442
+ use_cache: Optional[bool] = None,
1443
+ output_attentions: Optional[bool] = None,
1444
+ output_hidden_states: Optional[bool] = None,
1445
+ return_dict: Optional[bool] = None,
1446
+ ) -> Union[Tuple, CausalLMOutputWithPast]:
1447
+ r"""
1448
+ Args:
1449
+ labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
1450
+ Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
1451
+ config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
1452
+ (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
1453
+
1454
+ Returns:
1455
+
1456
+ Example:
1457
+
1458
+ ```python
1459
+ >>> from transformers import AutoTokenizer, Qwen2ForCausalLM
1460
+
1461
+ >>> model = Qwen2ForCausalLM.from_pretrained(PATH_TO_CONVERTED_WEIGHTS)
1462
+ >>> tokenizer = AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER)
1463
+
1464
+ >>> prompt = "Hey, are you conscious? Can you talk to me?"
1465
+ >>> inputs = tokenizer(prompt, return_tensors="pt")
1466
+
1467
+ >>> # Generate
1468
+ >>> generate_ids = model.generate(inputs.input_ids, max_length=30)
1469
+ >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
1470
+ "Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
1471
+ ```"""
1472
+
1473
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
1474
+ output_hidden_states = (
1475
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
1476
+ )
1477
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1478
+
1479
+ # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
1480
+ outputs = self.model(
1481
+ input_ids=input_ids,
1482
+ visual_features=visual_features,
1483
+ image_token_index=image_token_index,
1484
+ attention_mask=attention_mask,
1485
+ position_ids=position_ids,
1486
+ past_key_values=past_key_values,
1487
+ inputs_embeds=inputs_embeds,
1488
+ use_cache=use_cache,
1489
+ output_attentions=output_attentions,
1490
+ output_hidden_states=output_hidden_states,
1491
+ return_dict=return_dict,
1492
+ )
1493
+
1494
+ hidden_states = outputs[0]
1495
+ logits = self.lm_head(hidden_states)
1496
+ logits = logits.float()
1497
+
1498
+ loss = None
1499
+ if labels is not None:
1500
+
1501
+ # Shift so that tokens < n predict n
1502
+ shift_logits = logits[..., :-1, :].contiguous()
1503
+ shift_labels = labels[..., 1:].contiguous()
1504
+
1505
+ # Flatten the tokens
1506
+ loss_fct = CrossEntropyLoss()
1507
+ shift_logits = shift_logits.view(-1, self.config.vocab_size)
1508
+
1509
+ shift_labels = shift_labels.view(-1)
1510
+ shift_labels = shift_labels.to(shift_logits.device)
1511
+ loss = loss_fct(shift_logits, shift_labels)
1512
+
1513
+ pos_masks = find_pred_pos_from_input_ids(input_ids, text_mask_token_id=self.text_mask_token_id)
1514
+ shift_input_ids = input_ids[..., :-1].contiguous()
1515
+ shift_pos_masks = pos_masks[:, :-1]
1516
+ shift_input_ids = shift_input_ids.view(-1)
1517
+ max_n_future_tokens = min(4, self.model.block_size)
1518
+ pos_loss_list = torch.zeros(max_n_future_tokens, device=shift_logits.device)
1519
+ shift_pos_masks = shift_pos_masks.reshape(-1)
1520
+
1521
+ for ix in range(max_n_future_tokens):
1522
+ seg_loss = F.cross_entropy(
1523
+ shift_logits[shift_pos_masks == ix],
1524
+ shift_labels[shift_pos_masks == ix],
1525
+ reduction='mean'
1526
+ )
1527
+ pos_loss_list[ix] = seg_loss
1528
+
1529
+
1530
+ if not return_dict:
1531
+ output = (logits,) + outputs[1:]
1532
+ return (loss,) + output if loss is not None else output
1533
+
1534
+ if self.training:
1535
+ return CausalLMOutputWithPast(
1536
+ loss=loss,
1537
+ logits=logits,
1538
+ past_key_values=outputs.past_key_values,
1539
+ hidden_states=outputs.hidden_states,
1540
+ attentions=outputs.attentions,
1541
+ ), pos_loss_list
1542
+
1543
+ return CausalLMOutputWithPast(
1544
+ loss=loss,
1545
+ logits=logits,
1546
+ past_key_values=outputs.past_key_values,
1547
+ hidden_states=outputs.hidden_states,
1548
+ attentions=outputs.attentions,
1549
+ )
1550
+
1551
+ def prepare_inputs_for_generation(
1552
+ self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs
1553
+ ):
1554
+ # Omit tokens covered by past_key_values
1555
+ if past_key_values is not None:
1556
+ if isinstance(past_key_values, Cache):
1557
+ cache_length = past_key_values.get_seq_length()
1558
+ past_length = past_key_values.seen_tokens
1559
+ max_cache_length = past_key_values.get_max_length()
1560
+ else:
1561
+ cache_length = past_length = past_key_values[0][0].shape[2]
1562
+ max_cache_length = None
1563
+
1564
+ # Keep only the unprocessed tokens:
1565
+ # 1 - If the length of the attention_mask exceeds the length of input_ids, then we are in a setting where
1566
+ # some of the inputs are exclusively passed as part of the cache (e.g. when passing input_embeds as
1567
+ # input)
1568
+ if attention_mask is not None and attention_mask.shape[1] > input_ids.shape[1]:
1569
+ input_ids = input_ids[:, -(attention_mask.shape[1] - past_length) :]
1570
+ # 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens. We can discard
1571
+ # input_ids based on the past_length.
1572
+ elif past_length < input_ids.shape[1]:
1573
+ input_ids = input_ids[:, past_length:]
1574
+ # 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens.
1575
+
1576
+ # If we are about to go beyond the maximum cache length, we need to crop the input attention mask.
1577
+ if (
1578
+ max_cache_length is not None
1579
+ and attention_mask is not None
1580
+ and cache_length + input_ids.shape[1] > max_cache_length
1581
+ ):
1582
+ attention_mask = attention_mask[:, -max_cache_length:]
1583
+
1584
+ position_ids = kwargs.get("position_ids", None)
1585
+ if attention_mask is not None and position_ids is None:
1586
+ # create position_ids on the fly for batch generation
1587
+ position_ids = attention_mask.long().cumsum(-1) - 1
1588
+ position_ids.masked_fill_(attention_mask == 0, 1)
1589
+ if past_key_values:
1590
+ position_ids = position_ids[:, -input_ids.shape[1] :]
1591
+
1592
+ # if `inputs_embeds` are passed, we only want to use them in the 1st generation step
1593
+ if inputs_embeds is not None and past_key_values is None:
1594
+ model_inputs = {"inputs_embeds": inputs_embeds}
1595
+ else:
1596
+ model_inputs = {"input_ids": input_ids}
1597
+
1598
+ model_inputs.update(
1599
+ {
1600
+ "position_ids": position_ids,
1601
+ "past_key_values": past_key_values,
1602
+ "use_cache": kwargs.get("use_cache"),
1603
+ "attention_mask": attention_mask,
1604
+ }
1605
+ )
1606
+ return model_inputs
1607
+
1608
+ @staticmethod
1609
+ def _reorder_cache(past_key_values, beam_idx):
1610
+ reordered_past = ()
1611
+ for layer_past in past_key_values:
1612
+ reordered_past += (
1613
+ tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past),
1614
+ )
1615
+ return reordered_past
1616
+
1617
+
1618
+ @add_start_docstrings(
1619
+ """
1620
+ The Qwen2 Model transformer with a sequence classification head on top (linear layer).
1621
+
1622
+ [`Qwen2ForSequenceClassification`] uses the last token in order to do the classification, as other causal models
1623
+ (e.g. GPT-2) do.
1624
+
1625
+ Since it does classification on the last token, it requires to know the position of the last token. If a
1626
+ `pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
1627
+ no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
1628
+ padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
1629
+ each row of the batch).
1630
+ """,
1631
+ QWEN2_START_DOCSTRING,
1632
+ )
1633
+ class Qwen2ForSequenceClassification(Qwen2PreTrainedModel):
1634
+ def __init__(self, config):
1635
+ super().__init__(config)
1636
+ self.num_labels = config.num_labels
1637
+ self.model = Qwen2Model(config)
1638
+ self.score = nn.Linear(config.hidden_size, self.num_labels, bias=False)
1639
+
1640
+ # Initialize weights and apply final processing
1641
+ self.post_init()
1642
+
1643
+ def get_input_embeddings(self):
1644
+ return self.model.embed_tokens
1645
+
1646
+ def set_input_embeddings(self, value):
1647
+ self.model.embed_tokens = value
1648
+
1649
+ @add_start_docstrings_to_model_forward(QWEN2_INPUTS_DOCSTRING)
1650
+ def forward(
1651
+ self,
1652
+ input_ids: torch.LongTensor = None,
1653
+ attention_mask: Optional[torch.Tensor] = None,
1654
+ position_ids: Optional[torch.LongTensor] = None,
1655
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1656
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1657
+ labels: Optional[torch.LongTensor] = None,
1658
+ use_cache: Optional[bool] = None,
1659
+ output_attentions: Optional[bool] = None,
1660
+ output_hidden_states: Optional[bool] = None,
1661
+ return_dict: Optional[bool] = None,
1662
+ ) -> Union[Tuple, SequenceClassifierOutputWithPast]:
1663
+ r"""
1664
+ labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
1665
+ Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
1666
+ config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
1667
+ `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
1668
+ """
1669
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1670
+
1671
+ transformer_outputs = self.model(
1672
+ input_ids,
1673
+ attention_mask=attention_mask,
1674
+ position_ids=position_ids,
1675
+ past_key_values=past_key_values,
1676
+ inputs_embeds=inputs_embeds,
1677
+ use_cache=use_cache,
1678
+ output_attentions=output_attentions,
1679
+ output_hidden_states=output_hidden_states,
1680
+ return_dict=return_dict,
1681
+ )
1682
+ hidden_states = transformer_outputs[0]
1683
+ logits = self.score(hidden_states)
1684
+
1685
+ if input_ids is not None:
1686
+ batch_size = input_ids.shape[0]
1687
+ else:
1688
+ batch_size = inputs_embeds.shape[0]
1689
+
1690
+ if self.config.pad_token_id is None and batch_size != 1:
1691
+ raise ValueError("Cannot handle batch sizes > 1 if no padding token is defined.")
1692
+ if self.config.pad_token_id is None:
1693
+ sequence_lengths = -1
1694
+ else:
1695
+ if input_ids is not None:
1696
+ # if no pad token found, use modulo instead of reverse indexing for ONNX compatibility
1697
+ sequence_lengths = torch.eq(input_ids, self.config.pad_token_id).int().argmax(-1) - 1
1698
+ sequence_lengths = sequence_lengths % input_ids.shape[-1]
1699
+ sequence_lengths = sequence_lengths.to(logits.device)
1700
+ else:
1701
+ sequence_lengths = -1
1702
+
1703
+ pooled_logits = logits[torch.arange(batch_size, device=logits.device), sequence_lengths]
1704
+
1705
+ loss = None
1706
+ if labels is not None:
1707
+ labels = labels.to(logits.device)
1708
+ if self.config.problem_type is None:
1709
+ if self.num_labels == 1:
1710
+ self.config.problem_type = "regression"
1711
+ elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
1712
+ self.config.problem_type = "single_label_classification"
1713
+ else:
1714
+ self.config.problem_type = "multi_label_classification"
1715
+
1716
+ if self.config.problem_type == "regression":
1717
+ loss_fct = MSELoss()
1718
+ if self.num_labels == 1:
1719
+ loss = loss_fct(pooled_logits.squeeze(), labels.squeeze())
1720
+ else:
1721
+ loss = loss_fct(pooled_logits, labels)
1722
+ elif self.config.problem_type == "single_label_classification":
1723
+ loss_fct = CrossEntropyLoss()
1724
+ loss = loss_fct(pooled_logits.view(-1, self.num_labels), labels.view(-1))
1725
+ elif self.config.problem_type == "multi_label_classification":
1726
+ loss_fct = BCEWithLogitsLoss()
1727
+ loss = loss_fct(pooled_logits, labels)
1728
+ if not return_dict:
1729
+ output = (pooled_logits,) + transformer_outputs[1:]
1730
+ return ((loss,) + output) if loss is not None else output
1731
+
1732
+ return SequenceClassifierOutputWithPast(
1733
+ loss=loss,
1734
+ logits=pooled_logits,
1735
+ past_key_values=transformer_outputs.past_key_values,
1736
+ hidden_states=transformer_outputs.hidden_states,
1737
+ attentions=transformer_outputs.attentions,
1738
+ )
modeling_vit.py ADDED
@@ -0,0 +1,615 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved.
2
+ #
3
+ # NVIDIA CORPORATION and its licensors retain all intellectual property
4
+ # and proprietary rights in and to this software, related documentation
5
+ # and any modifications thereto. Any use, reproduction, disclosure or
6
+ # distribution of this software and related documentation without an express
7
+ # license agreement from NVIDIA CORPORATION is strictly prohibited.
8
+
9
+ import math
10
+ from copy import deepcopy
11
+ from typing import Union, Tuple, Sequence, Optional, List
12
+
13
+ import torch
14
+ import torch.nn as nn
15
+ import torch.nn.functional as F
16
+ try:
17
+ from transformers.activations import PytorchGELUTanh
18
+ except ImportError:
19
+ PytorchGELUTanh = lambda: nn.GELU(approximate='tanh')
20
+ from transformers.modeling_utils import PreTrainedModel
21
+ from transformers.utils import is_flash_attn_2_available, logging
22
+
23
+ if is_flash_attn_2_available():
24
+ from flash_attn import flash_attn_varlen_func
25
+ else:
26
+ flash_attn_varlen_func = None
27
+
28
+ from transformers.configuration_utils import PretrainedConfig
29
+
30
+
31
+ logger = logging.get_logger(__name__)
32
+
33
+
34
+ class MoonViTConfig(PretrainedConfig):
35
+ model_type = "moonvit"
36
+
37
+ def __init__(
38
+ self,
39
+ patch_size: int = 14,
40
+ init_pos_emb_height: int = 64,
41
+ init_pos_emb_width: int = 64,
42
+ num_attention_heads: int = 16,
43
+ num_hidden_layers: int = 27,
44
+ hidden_size: int = 1152,
45
+ intermediate_size: int = 4304,
46
+ merge_kernel_size: tuple[int, int] = (2, 2),
47
+ **kwargs,
48
+ ):
49
+ super().__init__(**kwargs)
50
+ self.patch_size = patch_size
51
+ # Positional embedding config
52
+ self.init_pos_emb_height = init_pos_emb_height
53
+ self.init_pos_emb_width = init_pos_emb_width
54
+ # Transformer config
55
+ self.num_hidden_layers = num_hidden_layers
56
+ self.num_attention_heads = num_attention_heads
57
+ self.hidden_size = hidden_size
58
+ self.intermediate_size = intermediate_size
59
+ # Patch merger config
60
+ self.merge_kernel_size = merge_kernel_size
61
+
62
+
63
+ def multihead_attention(
64
+ q: torch.Tensor,
65
+ k: torch.Tensor,
66
+ v: torch.Tensor,
67
+ q_cu_seqlens: Optional[torch.Tensor] = None,
68
+ k_cu_seqlens: Optional[torch.Tensor] = None,
69
+ ):
70
+ """Multi-head attention using flash attention 2.
71
+
72
+ Args:
73
+ q, k, v: tensor of shape (batch_size, seqlen, num_heads, head_dim),
74
+ or (tot_seqlens, num_heads, head_dim) if packing.
75
+ q_cu_seqlens (torch.Tensor): cumulative sequence lengths of q.
76
+ The first element should be 0 and the last element should be q.shape[0].
77
+ k_cu_seqlens (torch.Tensor): cumulative sequence lengths of k.
78
+ The first element should be 0 and the last element should be k.shape[0].
79
+
80
+ Returns:
81
+ output: shape (batch_size, seqlen, dim) or (tot_seqlens, dim) if packing,
82
+ where dim = num_heads * head_dim
83
+ """
84
+ if flash_attn_varlen_func is None:
85
+ logger.warning_once(
86
+ "flash_attn is not available for MoonViT; falling back to sdpa attention."
87
+ )
88
+ return sdpa_attention(
89
+ q,
90
+ k,
91
+ v,
92
+ q_cu_seqlens=q_cu_seqlens,
93
+ k_cu_seqlens=k_cu_seqlens,
94
+ )
95
+
96
+ # Unified format legal check
97
+ assert q.dim() == k.dim() == v.dim() == 3, "q, k, v must have 3 dims"
98
+ assert q_cu_seqlens[-1] == q.shape[0], "q_cu_seqlens must sum to q.shape[0]"
99
+ assert (
100
+ k_cu_seqlens[-1] == k.shape[0] == v.shape[0]
101
+ ), "k_cu_seqlens must sum to k.shape[0]"
102
+ assert q.dtype in [
103
+ torch.bfloat16,
104
+ torch.float16,
105
+ ], f"unsupported dtype {q.dtype} for multihead attn"
106
+
107
+ max_seqlen_q = (q_cu_seqlens[1:] - q_cu_seqlens[:-1]).max().item()
108
+ max_seqlen_k = (k_cu_seqlens[1:] - k_cu_seqlens[:-1]).max().item()
109
+ attn_out = flash_attn_varlen_func(
110
+ q,
111
+ k,
112
+ v,
113
+ q_cu_seqlens,
114
+ k_cu_seqlens,
115
+ max_seqlen_q,
116
+ max_seqlen_k,
117
+ causal=False,
118
+ )
119
+ attn_out = attn_out.flatten(start_dim=-2)
120
+
121
+ return attn_out
122
+
123
+
124
+ def sdpa_attention(
125
+ q: torch.Tensor,
126
+ k: torch.Tensor,
127
+ v: torch.Tensor,
128
+ q_cu_seqlens: Optional[torch.Tensor] = None,
129
+ k_cu_seqlens: Optional[torch.Tensor] = None,
130
+ ) -> torch.Tensor:
131
+ """SDPA attention.
132
+
133
+ Args:
134
+ q, k, v: tensor of shape (batch_size, seqlen, num_heads, head_dim),
135
+ or (tot_seqlens, num_heads, head_dim) if packing.
136
+ """
137
+ seq_length = q.shape[0]
138
+ attention_mask = torch.zeros(
139
+ [1, seq_length, seq_length], device=q.device, dtype=torch.bool
140
+ )
141
+ for i in range(1, len(q_cu_seqlens)):
142
+ attention_mask[
143
+ ...,
144
+ q_cu_seqlens[i - 1] : q_cu_seqlens[i],
145
+ q_cu_seqlens[i - 1] : q_cu_seqlens[i],
146
+ ] = True
147
+ q = q.transpose(0, 1)
148
+ k = k.transpose(0, 1)
149
+ v = v.transpose(0, 1)
150
+ attn_output = F.scaled_dot_product_attention(q, k, v, attention_mask, dropout_p=0.0)
151
+ attn_output = attn_output.transpose(0, 1)
152
+ attn_output = attn_output.reshape(seq_length, -1)
153
+ return attn_output
154
+
155
+
156
+ def eager_attention(
157
+ q: torch.Tensor,
158
+ k: torch.Tensor,
159
+ v: torch.Tensor,
160
+ q_cu_seqlens: Optional[torch.Tensor] = None,
161
+ k_cu_seqlens: Optional[torch.Tensor] = None,
162
+ ) -> torch.Tensor:
163
+ seq_length = q.shape[0]
164
+ attention_mask = torch.zeros(
165
+ [1, seq_length, seq_length], device=q.device, dtype=torch.bool
166
+ )
167
+ for i in range(1, len(q_cu_seqlens)):
168
+ attention_mask[
169
+ ...,
170
+ q_cu_seqlens[i - 1] : q_cu_seqlens[i],
171
+ q_cu_seqlens[i - 1] : q_cu_seqlens[i],
172
+ ] = True
173
+ q = q.transpose(0, 1)
174
+ k = k.transpose(0, 1)
175
+ v = v.transpose(0, 1)
176
+
177
+ attn_weight = q @ k.transpose(-2, -1) / math.sqrt(q.shape[-1])
178
+ attn_weight += attention_mask
179
+ attn_weight = torch.softmax(attn_weight, dim=-1, dtype=torch.float32).to(q.dtype)
180
+
181
+ attn_output = attn_weight @ v
182
+ attn_output = attn_output.transpose(0, 1)
183
+ attn_output = attn_output.reshape(seq_length, -1)
184
+ return attn_output
185
+
186
+
187
+ VL_VISION_ATTENTION_FUNCTIONS = {
188
+ "flash_attention_2": multihead_attention,
189
+ "sdpa": sdpa_attention,
190
+ "eager": eager_attention,
191
+ }
192
+
193
+
194
+ def _apply_rope_input_validation(x, freqs_cis):
195
+ assert x.ndim == freqs_cis.ndim + 1, (x.shape, freqs_cis.shape)
196
+ assert x.shape[:-2] == freqs_cis.shape[:-1], (x.shape, freqs_cis.shape)
197
+ assert x.shape[-1] == 2 * freqs_cis.shape[-1], (x.shape, freqs_cis.shape)
198
+ assert freqs_cis.dtype == torch.complex64, freqs_cis.dtype
199
+
200
+
201
+ def apply_rope(
202
+ xq: torch.Tensor, xk: torch.Tensor, freqs_cis: torch.Tensor
203
+ ) -> tuple[torch.Tensor, torch.Tensor]:
204
+ """
205
+ Args: (The leading dimensions of all inputs should be the same)
206
+ xq: query, tensor of shape (..., num_heads, head_dim)
207
+ xk: key, tensor of shape (..., num_heads, head_dim)
208
+ freqs_cis: tensor of shape (..., head_dim/2), dtype=torch.complex64. It contains the precomputed cis(freqs) for each position in the 2D grid.
209
+ Returns:
210
+ xq_out, xk_out: tensors of shape (..., num_heads, head_dim)
211
+ """
212
+ _apply_rope_input_validation(xq, freqs_cis)
213
+ _apply_rope_input_validation(xk, freqs_cis)
214
+
215
+ freqs_cis = freqs_cis.unsqueeze(-2) # ..., 1, head_dim/2
216
+ # ..., num_heads, head_dim/2
217
+ xq_ = torch.view_as_complex(xq.float().view(*xq.shape[:-1], -1, 2))
218
+ xk_ = torch.view_as_complex(xk.float().view(*xq.shape[:-1], -1, 2))
219
+ xq_out = torch.view_as_real(xq_ * freqs_cis).flatten(-2) # ..., num_heads, head_dim
220
+ xk_out = torch.view_as_real(xk_ * freqs_cis).flatten(-2) # ..., num_heads, head_dim
221
+ return xq_out.type_as(xq), xk_out.type_as(xk)
222
+
223
+
224
+ class Learnable2DInterpPosEmb(nn.Module):
225
+ def __init__(
226
+ self, height: int, width: int, dim: int, interpolation_mode: str = "bicubic"
227
+ ) -> None:
228
+ super().__init__()
229
+ self.height = height
230
+ self.width = width
231
+ self.interpolation_mode = interpolation_mode
232
+ self.weight = nn.Parameter(torch.empty(height, width, dim))
233
+ self.reset_parameters()
234
+
235
+ def reset_parameters(self):
236
+ nn.init.normal_(self.weight)
237
+
238
+ def forward(self, x: torch.Tensor, grid_hws: torch.Tensor) -> torch.Tensor:
239
+ pos_embs = []
240
+ for shape in grid_hws.tolist():
241
+ if shape == self.weight.shape[:-1]:
242
+ pos_embs.append(self.weight.flatten(end_dim=1))
243
+ else:
244
+ pos_embs.append(
245
+ F.interpolate(
246
+ self.weight.permute((2, 0, 1)).unsqueeze(0),
247
+ size=shape,
248
+ mode=self.interpolation_mode,
249
+ )
250
+ .squeeze(0)
251
+ .permute((1, 2, 0))
252
+ .flatten(end_dim=1)
253
+ )
254
+ out = x + torch.cat(pos_embs)
255
+ return out
256
+
257
+
258
+ class MoonVisionPatchEmbed(nn.Module):
259
+
260
+ def __init__(
261
+ self,
262
+ out_dim: int,
263
+ in_dim: int = 3,
264
+ patch_size: Union[int, Tuple[int, int]] = (14, 14),
265
+ pos_emb_height: int = 14,
266
+ pos_emb_width: int = 14,
267
+ ):
268
+ super().__init__()
269
+ assert isinstance(
270
+ patch_size, (int, Sequence)
271
+ ), f"Invalid patch_size type: {type(patch_size)}"
272
+ if isinstance(patch_size, int):
273
+ patch_size = (patch_size, patch_size)
274
+ assert (
275
+ len(patch_size) == 2
276
+ ), f"Expected patch_size to be a tuple of 2, got {patch_size}"
277
+ self.patch_size = patch_size
278
+
279
+ self.proj = nn.Conv2d(
280
+ in_dim, out_dim, kernel_size=patch_size, stride=patch_size
281
+ )
282
+
283
+ self.pos_emb = Learnable2DInterpPosEmb(
284
+ height=pos_emb_height, width=pos_emb_width, dim=out_dim
285
+ )
286
+
287
+ def forward(self, x: torch.Tensor, grid_hws: torch.Tensor) -> torch.Tensor:
288
+ """
289
+ Args:
290
+ x (L, Channels): input tensor
291
+ grid_hws (N, 2): grid height and width
292
+
293
+ Returns:
294
+ (L, Cout) tensor
295
+ """
296
+ x = self.proj(x).view(x.size(0), -1)
297
+ # apply positional embedding
298
+ x = self.pos_emb(x, grid_hws)
299
+ return x
300
+
301
+
302
+ class Rope2DPosEmb(nn.Module):
303
+ """2D rotary position embedding with multi-resolution support.
304
+
305
+ This class is intended to be used in the following way:
306
+ 1. Before training, create an instance of Rope2DPosEmb. This instance will hold the precomputed cis.
307
+ 2. Before each forward pass, call `get_freqs_cis_by_*` to get the `freqs_cis` tensor for this iteration.
308
+ 3. During the forward pass, pass the `freqs_cis` tensor to each attention layer, and call `apply` just before each attention operation.
309
+ The rope is shared across all attention layers and all heads.
310
+
311
+ Refs:
312
+ - RoFormer: https://arxiv.org/abs/2104.09864
313
+ - VisionLLaMA: https://arxiv.org/abs/2403.00522
314
+ - https://github.com/Meituan-AutoML/VisionLLaMA/blob/main/dit/models.py
315
+
316
+ Args:
317
+ dim (int): usually the multi-head attention dimension, should be divisible by 4 (TODO: relax this constraint if needed)
318
+ max_height (int): the maximum height of the 2D grid
319
+ max_width (int): the maximum width of the 2D grid
320
+ theta_base (float): the base of the theta
321
+ device (str): the device to store the precomputed cis
322
+ """
323
+
324
+ def __init__(self, dim: int, max_height: int, max_width: int, theta_base=10000):
325
+ super().__init__()
326
+ self.dim = dim
327
+ assert self.dim % 4 == 0, "dim must be divisible by 4"
328
+ self.max_height = max_height
329
+ self.max_width = max_width
330
+ self.theta_base = theta_base
331
+
332
+ self.freqs_cis = None
333
+
334
+ def extra_repr(self):
335
+ return f"dim={self.dim}, max_height={self.max_height}, max_width={self.max_width}, theta_base={self.theta_base}"
336
+
337
+ def _precompute_freqs_cis(self, device: torch.device) -> torch.Tensor:
338
+ """Calculate the cis(freqs) for each position in the 2D grid.
339
+
340
+ Return: complex tensor of shape (max_height, max_width, dim//2) and value:
341
+ height axis: ret[h, w, 2*i] = cis(h * theta_base**(-4*i/dim))
342
+ weight axis: ret[h, w, 2*i+1] = cis(w * theta_base**(-4*i/dim)) with (i in [0, dim//4))
343
+ note: `cis` is a mathematical notation defined by cis x = cos x + i sin x,
344
+ """
345
+ N = self.max_height * self.max_width
346
+ flat_pos = torch.arange(0, N).float().to(device)
347
+ x_pos = flat_pos % self.max_width
348
+ y_pos = flat_pos // self.max_width
349
+ dim_range = (
350
+ torch.arange(0, self.dim, 4)[: (self.dim // 4)].float().to(device)
351
+ ) # C/4
352
+ freqs = 1.0 / (self.theta_base ** (dim_range / self.dim))
353
+ x_freqs = torch.outer(x_pos, freqs).float() # N, C/4
354
+ y_freqs = torch.outer(y_pos, freqs).float() # N, C/4
355
+ x_cis = torch.polar(torch.ones_like(x_freqs), x_freqs) # N, C/4
356
+ y_cis = torch.polar(torch.ones_like(y_freqs), y_freqs) # N, C/4
357
+ # N, C/4, 2
358
+ freqs_cis = torch.cat(
359
+ [x_cis.unsqueeze(dim=-1), y_cis.unsqueeze(dim=-1)], dim=-1
360
+ )
361
+ # max_height, max_width, C/2
362
+ freqs_cis = freqs_cis.reshape(self.max_height, self.max_width, -1)
363
+ return freqs_cis
364
+
365
+ def get_freqs_cis(self, grid_hws: torch.Tensor) -> torch.Tensor:
366
+ """
367
+ Args:
368
+ grid_hws (torch.Tensor): grid height and width
369
+
370
+ Returns:
371
+ freqs_cis: tensor of shape (sum(t * height * width), dim//2)
372
+ """
373
+ if self.freqs_cis is None:
374
+ self.freqs_cis = self._precompute_freqs_cis(grid_hws.device)
375
+
376
+ shapes = grid_hws.tolist()
377
+ assert all(
378
+ 1 <= h <= self.max_height and 1 <= w <= self.max_width for h, w in shapes
379
+ ), (
380
+ shapes,
381
+ self.max_height,
382
+ self.max_width,
383
+ )
384
+ freqs_cis = torch.cat(
385
+ [self.freqs_cis[:h, :w].reshape(-1, self.dim // 2) for h, w in shapes],
386
+ dim=0,
387
+ )
388
+ return freqs_cis
389
+
390
+
391
+ class MLP2(nn.Module):
392
+ """
393
+ Args:
394
+ dims: [in_dim, hidden_dim, out_dim]
395
+ bias: whether to use bias in linear layer.
396
+ """
397
+
398
+ def __init__(self, dims: list[int], activation, bias=True):
399
+ super().__init__()
400
+ assert len(dims) == 3
401
+ self.fc0 = nn.Linear(dims[0], dims[1], bias=bias)
402
+ self.fc1 = nn.Linear(dims[1], dims[2], bias=bias)
403
+ self.activation = activation
404
+ for m in [self.fc0, self.fc1]:
405
+ nn.init.trunc_normal_(m.weight, std=math.sqrt(2 / m.in_features))
406
+ if m.bias is not None:
407
+ nn.init.zeros_(m.bias)
408
+
409
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
410
+ x = self.fc0(x)
411
+ x = self.activation(x)
412
+ return self.fc1(x)
413
+
414
+
415
+ class MoonVitEncoderLayer(nn.Module):
416
+
417
+ def __init__(
418
+ self,
419
+ num_heads: int,
420
+ hidden_dim: int,
421
+ mlp_dim: int,
422
+ *,
423
+ attn_implementation: str = "eager",
424
+ activation=F.gelu,
425
+ attn_bias: bool = False,
426
+ ):
427
+ super().__init__()
428
+ self.num_heads = num_heads
429
+ self.hidden_dim = hidden_dim
430
+ self.hidden_size_per_attention_head = self.hidden_dim // self.num_heads
431
+ self.attn_implementation = attn_implementation
432
+
433
+ self.norm0 = nn.LayerNorm(hidden_dim)
434
+ self.norm1 = nn.LayerNorm(hidden_dim)
435
+ self.mlp = MLP2([hidden_dim, mlp_dim, hidden_dim], activation)
436
+ self.wqkv = nn.Linear(hidden_dim, hidden_dim * 3, bias=attn_bias)
437
+ self.wo = nn.Linear(hidden_dim, hidden_dim, bias=attn_bias)
438
+
439
+ def attention_qkvpacked(
440
+ self,
441
+ x: torch.Tensor,
442
+ cu_seqlens: torch.Tensor,
443
+ rope_freqs_cis: Optional[torch.Tensor] = None,
444
+ ):
445
+ """
446
+ Args:
447
+ x (torch.Tensor): (batch_size, seqlen, hidden_dim)
448
+ cu_seqlens (torch.Tensor):
449
+ """
450
+ xqkv = self.wqkv(x)
451
+
452
+ qkv_shape = xqkv.size()[:-1] + (
453
+ 3,
454
+ self.num_heads,
455
+ self.hidden_size_per_attention_head,
456
+ )
457
+ # xqkv: (batch_size, seqlen, 3, nheads, headdim)
458
+ xqkv = xqkv.view(*qkv_shape)
459
+ xq, xk, xv = torch.unbind(xqkv, dim=-3)
460
+
461
+ xq, xk = apply_rope(xq, xk, rope_freqs_cis)
462
+
463
+ attn_func = VL_VISION_ATTENTION_FUNCTIONS[self.attn_implementation]
464
+ attn_out = attn_func(
465
+ xq, xk, xv, q_cu_seqlens=cu_seqlens, k_cu_seqlens=cu_seqlens
466
+ )
467
+
468
+ attn_out = self.wo(attn_out)
469
+ return attn_out
470
+
471
+ def forward(
472
+ self,
473
+ hidden_states: torch.Tensor,
474
+ cu_seqlens: torch.Tensor,
475
+ rope_freqs_cis: Union[torch.Tensor, None] = None,
476
+ ) -> torch.Tensor:
477
+ """
478
+ Args:
479
+ hidden_states: non-packed (B, N, D) or packed (L, D). if non-packed, seqlens should be None, if packed, seqlens should be set
480
+
481
+ Returns:
482
+ output: same shape of input, non-packed (B, N, D) for non-packed input, (L, D) for packed input
483
+ """
484
+ residual = hidden_states
485
+ hidden_states = self.norm0(hidden_states)
486
+ attn_out = self.attention_qkvpacked(
487
+ hidden_states, cu_seqlens, rope_freqs_cis=rope_freqs_cis
488
+ )
489
+ hidden_states = residual + attn_out
490
+
491
+ residual = hidden_states
492
+ hidden_states = self.mlp(self.norm1(hidden_states))
493
+ hidden_states = residual + hidden_states
494
+ return hidden_states
495
+
496
+
497
+ class MoonVitEncoder(nn.Module):
498
+
499
+ def __init__(
500
+ self,
501
+ hidden_dim: int,
502
+ num_layers: int,
503
+ block_cfg: dict,
504
+ ) -> None:
505
+ super().__init__()
506
+
507
+ self.rope_2d = Rope2DPosEmb(
508
+ block_cfg["hidden_dim"] // block_cfg["num_heads"], 512, 512
509
+ )
510
+ self.blocks = nn.ModuleList(
511
+ [MoonVitEncoderLayer(**block_cfg) for _ in range(num_layers)]
512
+ )
513
+ self.final_layernorm = nn.LayerNorm(hidden_dim)
514
+
515
+ def forward(
516
+ self, hidden_states: torch.Tensor, grid_hws: torch.Tensor
517
+ ) -> torch.Tensor:
518
+ rope_freqs_cis = self.rope_2d.get_freqs_cis(grid_hws=grid_hws)
519
+
520
+ lengths = torch.cat(
521
+ (
522
+ torch.zeros(1, device=hidden_states.device, dtype=grid_hws.dtype),
523
+ grid_hws[:, 0] * grid_hws[:, 1],
524
+ )
525
+ )
526
+ cu_seqlens = lengths.cumsum(dim=0, dtype=torch.int32)
527
+
528
+ for _, block in enumerate(self.blocks):
529
+ hidden_states = block(
530
+ hidden_states, cu_seqlens, rope_freqs_cis=rope_freqs_cis
531
+ )
532
+
533
+ hidden_states = self.final_layernorm(hidden_states)
534
+
535
+ return hidden_states
536
+
537
+
538
+ def patch_merger(
539
+ x: torch.Tensor,
540
+ grid_hws: torch.Tensor,
541
+ merge_kernel_size: list[int, int] = (2, 2),
542
+ ) -> List[torch.Tensor]:
543
+ d_model = x.size(-1)
544
+
545
+ outputs = []
546
+ pre_sum = 0
547
+ for x_shape in grid_hws.tolist():
548
+ height, width = x_shape[0], x_shape[1]
549
+ # Get the current sequence
550
+ seq = x[pre_sum : pre_sum + height * width]
551
+ # Reshape along self.merge_kernel_size and concat to the last dimension
552
+ kernel_height, kernel_width = merge_kernel_size
553
+ new_height, new_width = height // kernel_height, width // kernel_width
554
+ reshaped_seq = seq.view(
555
+ new_height, kernel_height, new_width, kernel_width, d_model
556
+ )
557
+ reshaped_seq = reshaped_seq.permute(0, 2, 1, 3, 4).contiguous()
558
+ padded_seq = reshaped_seq.view(
559
+ new_height * new_width, -1
560
+ )
561
+ outputs.append(padded_seq)
562
+ pre_sum += height * width
563
+
564
+ return outputs
565
+
566
+
567
+ class MoonVitPretrainedModel(PreTrainedModel):
568
+ config_class = MoonViTConfig
569
+ model_type = "moonvit"
570
+ _no_split_modules = ["PackingTransformer"]
571
+ _supports_flash_attn_2 = True
572
+ _supports_sdpa = True
573
+
574
+ def __init__(self, config: MoonViTConfig, *inputs, **kwargs):
575
+ super().__init__(config, *inputs, **kwargs)
576
+ config = deepcopy(config)
577
+ self.merge_kernel_size = config.merge_kernel_size
578
+ self.patch_size = config.patch_size
579
+ self.patch_embed = MoonVisionPatchEmbed(
580
+ out_dim=config.hidden_size,
581
+ patch_size=config.patch_size,
582
+ pos_emb_height=config.init_pos_emb_height,
583
+ pos_emb_width=config.init_pos_emb_width,
584
+ )
585
+
586
+ self.encoder = MoonVitEncoder(
587
+ hidden_dim=config.hidden_size,
588
+ num_layers=config.num_hidden_layers,
589
+ block_cfg={
590
+ "num_heads": config.num_attention_heads,
591
+ "hidden_dim": config.hidden_size,
592
+ "mlp_dim": config.intermediate_size,
593
+ "activation": PytorchGELUTanh(),
594
+ "attn_bias": True,
595
+ "attn_implementation": config._attn_implementation,
596
+ },
597
+ )
598
+
599
+ def forward(
600
+ self, pixel_values: torch.Tensor, grid_hws: torch.Tensor
601
+ ) -> torch.Tensor:
602
+ """
603
+ Args:
604
+ pixel_values (torch.Tensor): The input pixel values.
605
+ grid_hws (torch.Tensor): The grid height and width.
606
+
607
+ Returns:
608
+ torch.Tensor: The output tokens.
609
+ """
610
+ hidden_states = self.patch_embed(pixel_values, grid_hws)
611
+ hidden_states = self.encoder(hidden_states, grid_hws)
612
+ hidden_states = patch_merger(
613
+ hidden_states, grid_hws, merge_kernel_size=self.merge_kernel_size
614
+ )
615
+ return hidden_states
preprocessor_config.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "auto_map": {
3
+ "AutoImageProcessor": "image_processing_locateanything.LocateAnythingImageProcessor",
4
+ "AutoProcessor": "processing_locateanything.LocateAnythingProcessor"
5
+ },
6
+ "image_mean": [
7
+ 0.5,
8
+ 0.5,
9
+ 0.5
10
+ ],
11
+ "image_processor_type": "LocateAnythingImageProcessor",
12
+ "image_std": [
13
+ 0.5,
14
+ 0.5,
15
+ 0.5
16
+ ],
17
+ "in_token_limit": 25600,
18
+ "merge_kernel_size": [
19
+ 2,
20
+ 2
21
+ ],
22
+ "patch_size": 14,
23
+ "processor_class": "LocateAnythingProcessor"
24
+ }
processing_locateanything.py ADDED
@@ -0,0 +1,678 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2024 The HuggingFace Inc. team.
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
+ """
16
+ Processor class for LocateAnything.
17
+ """
18
+
19
+ import math
20
+ import os
21
+ from typing import Iterable, List, Union, Literal
22
+ import base64
23
+ import sys
24
+ import time
25
+ import warnings
26
+ from functools import lru_cache
27
+ from io import BytesIO
28
+ import re
29
+ import requests
30
+ import torch
31
+ import torchvision
32
+ from packaging import version
33
+ from PIL import Image
34
+ from torchvision import io
35
+ from torchvision import transforms
36
+ from torchvision.transforms import InterpolationMode
37
+ from typing import Optional, Any
38
+ import numpy as np
39
+
40
+ from transformers.feature_extraction_utils import BatchFeature
41
+ from transformers.image_utils import ImageInput
42
+ try:
43
+ from transformers.image_utils import VideoInput
44
+ except ImportError:
45
+ VideoInput = None
46
+ from transformers.processing_utils import ProcessingKwargs, ProcessorMixin, Unpack
47
+ from transformers.tokenization_utils_base import PreTokenizedInput, TextInput
48
+ from transformers.utils import logging
49
+ import lmdb
50
+ import cv2
51
+ import pickle
52
+ import decord
53
+
54
+ logger = logging.get_logger(__name__)
55
+
56
+ FPS = 2.0
57
+ MAX_FRAMES = 64
58
+ VIDEO_TOTAL_PIXELS = int(float(os.environ.get('VIDEO_MAX_PIXELS', 32000 * 28 * 28 * 0.9)))
59
+ logger.info(f"set VIDEO_TOTAL_PIXELS: {VIDEO_TOTAL_PIXELS}")
60
+
61
+
62
+ def to_rgb(pil_image: Image.Image) -> Image.Image:
63
+ if pil_image.mode == 'RGBA':
64
+ white_background = Image.new("RGB", pil_image.size, (255, 255, 255))
65
+ white_background.paste(pil_image, mask=pil_image.split()[3]) # Use alpha channel as mask
66
+ return white_background
67
+ else:
68
+ return pil_image.convert("RGB")
69
+
70
+ def read_img_from_lmdb_v2(image_data):
71
+ # special case for AgiBotWorld
72
+ lmdb_file, lmdb_key = image_data['lmdb_file'], image_data['lmdb_key']
73
+ key = lmdb_key.encode('ascii')
74
+ env = lmdb.open(lmdb_file, max_readers=10240, readonly=True, lock=False, readahead=False, meminit=False)
75
+ txn = env.begin()
76
+ value = txn.get(key)
77
+ if value is None:
78
+ print(f"Warning: Key {key} not found.")
79
+ return None
80
+ record = pickle.loads(value)
81
+ image_bgr = cv2.imdecode(np.frombuffer(record['image'], dtype=np.uint8), cv2.IMREAD_COLOR)
82
+ image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
83
+ image = Image.fromarray(image_rgb)
84
+
85
+ return image
86
+
87
+ def parse_lmdb_image_data(image_data):
88
+ lmdb_file = image_data['lmdb_file']
89
+ if not os.path.exists(lmdb_file):
90
+ if "/home/zhidingy/workspace/libs/eagle/Eagle2/" in lmdb_file:
91
+ image_data['lmdb_file'] = lmdb_file.replace("/home/zhidingy/workspace/libs/eagle/Eagle2/", "")
92
+ else:
93
+ raise ValueError(f"LMDB file {lmdb_file} does not exist")
94
+ # special case for AgiBotWorld
95
+ if 'AgiBotWorld' in image_data['lmdb_file']:
96
+ return read_img_from_lmdb_v2(image_data)
97
+
98
+ try:
99
+ env = lmdb.open(image_data['lmdb_file'], readonly=True, lock=False, max_readers=10240)
100
+ except Exception as e:
101
+ print(f"Failed to open lmdb file {image_data['lmdb_file']}. Error message: {e}", flush=True)
102
+ raise e
103
+
104
+ with env.begin(write=False) as txn:
105
+ try:
106
+ image_bin = txn.get(image_data['lmdb_key'].encode('ascii'))
107
+ buf = BytesIO(image_bin)
108
+ except Exception as e:
109
+ print(f"Failed to get image from lmdb file {image_data['lmdb_file']}. Error message: {e}", flush=True)
110
+ raise e
111
+ try:
112
+ image = Image.open(buf)
113
+ except Exception as e:
114
+ image_np = np.frombuffer(image_bin, dtype=np.uint8)
115
+ image_bgr = cv2.imdecode(image_np, cv2.IMREAD_COLOR)
116
+ image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
117
+ image = Image.fromarray(image_rgb)
118
+ return image
119
+
120
+ def fetch_image(ele: dict[str, str | Image.Image]) -> Image.Image:
121
+ if "image" in ele:
122
+ image = ele["image"]
123
+ else:
124
+ image = ele["image_url"]
125
+ image_obj = None
126
+ if isinstance(image, Image.Image):
127
+ image_obj = image
128
+ elif isinstance(image, dict) and 'lmdb_file' in image:
129
+ image_obj = parse_lmdb_image_data(image)
130
+ elif image.startswith("http://") or image.startswith("https://"):
131
+ response = requests.get(image, stream=True)
132
+ image_obj = Image.open(BytesIO(response.content))
133
+ elif image.startswith("file://"):
134
+ image_obj = Image.open(image[7:])
135
+ elif image.startswith("data:image"):
136
+ if "base64," in image:
137
+ _, base64_data = image.split("base64,", 1)
138
+ data = base64.b64decode(base64_data)
139
+ image_obj = Image.open(BytesIO(data))
140
+ else:
141
+ image_obj = Image.open(image)
142
+ if image_obj is None:
143
+ raise ValueError(f"Unrecognized image input, support local path, http url, base64 and PIL.Image, got {image}")
144
+ image = to_rgb(image_obj)
145
+
146
+ return image
147
+
148
+
149
+ def get_video_frame_indices(
150
+ ele: dict,
151
+ total_frames: int,
152
+ video_fps: int | float,
153
+ ) -> tuple[torch.Tensor, float]:
154
+ target_fps = ele.get("fps", FPS)
155
+ max_frames = ele.get("max_frames", MAX_FRAMES)
156
+
157
+ nframes = (total_frames / video_fps) * target_fps
158
+ nframes = int(round(nframes))
159
+ nframes = max(1, nframes)
160
+
161
+ if nframes > max_frames:
162
+ nframes = max_frames
163
+
164
+ nframes = min(nframes, total_frames)
165
+
166
+ if nframes == total_frames:
167
+ idx = torch.arange(total_frames).long()
168
+ else:
169
+ idx = torch.linspace(0, total_frames - 1, nframes).round().long()
170
+
171
+ sample_fps = nframes / max(total_frames, 1e-6) * video_fps
172
+
173
+ return idx, sample_fps
174
+
175
+ def _read_video_torchvision(
176
+ ele: dict,
177
+ ) -> (torch.Tensor, float, list):
178
+ """read video using torchvision.io.read_video and return also per-frame timestamps"""
179
+ video_path = ele["video"]
180
+ if version.parse(torchvision.__version__) < version.parse("0.19.0"):
181
+ if "http://" in video_path or "https://" in video_path:
182
+ warnings.warn("torchvision < 0.19.0 does not support http/https video path, please upgrade to 0.19.0.")
183
+ if "file://" in video_path:
184
+ video_path = video_path[7:]
185
+ st = time.time()
186
+
187
+ video, audio, info = io.read_video(
188
+ video_path,
189
+ start_pts=ele.get("video_start", 0.0),
190
+ end_pts=ele.get("video_end", None),
191
+ pts_unit="sec",
192
+ output_format="TCHW",
193
+ )
194
+ total_frames, video_fps = video.size(0), info["video_fps"]
195
+ logger.info(f"torchvision: {video_path=}, {total_frames=}, {video_fps=}, time={time.time() - st:.3f}s")
196
+
197
+ idx, sample_fps = get_video_frame_indices(ele, total_frames, video_fps)
198
+
199
+ start_time = ele.get("video_start", 0.0)
200
+ timestamps = (start_time + idx.to(torch.float32) / video_fps).tolist()
201
+
202
+ video = video[idx]
203
+ return video, sample_fps, timestamps
204
+
205
+
206
+ def is_decord_available() -> bool:
207
+ import importlib.util
208
+ return importlib.util.find_spec("decord") is not None
209
+
210
+ def _read_video_decord(
211
+ ele: dict,
212
+ ) -> (torch.Tensor, float, list):
213
+ """read video using decord.VideoReader and return also per-frame timestamps"""
214
+ video_path = ele["video"]
215
+ st = time.time()
216
+ vr = decord.VideoReader(video_path)
217
+
218
+ total_frames, video_fps = len(vr), vr.get_avg_fps()
219
+ logger.info(f"decord: {video_path=}, {total_frames=}, {video_fps=}, time={time.time() - st:.3f}s")
220
+
221
+ idx_tensor, sample_fps = get_video_frame_indices(ele, total_frames, video_fps)
222
+ idx = idx_tensor.tolist()
223
+
224
+ start_time = ele.get("video_start", 0.0)
225
+ timestamps = [start_time + i / video_fps for i in idx]
226
+
227
+ video = vr.get_batch(idx).asnumpy()
228
+ video = torch.tensor(video).permute(0, 3, 1, 2) # Convert to TCHW format
229
+
230
+ return video, sample_fps, timestamps
231
+
232
+
233
+ VIDEO_READER_BACKENDS = {
234
+ "decord": _read_video_decord,
235
+ "torchvision": _read_video_torchvision,
236
+ }
237
+
238
+
239
+ @lru_cache(maxsize=1)
240
+ def get_video_reader_backend() -> str:
241
+ if is_decord_available():
242
+ video_reader_backend = "decord"
243
+ else:
244
+ video_reader_backend = "torchvision"
245
+ return video_reader_backend
246
+
247
+
248
+ def fetch_video(ele: dict, return_video_sample_fps: bool = False, video_reader_backend: str = "torchvision") -> torch.Tensor | list[Image.Image]:
249
+ """
250
+ Fetches video, samples frames, resizes based on video_total_pixels, and returns as Tensor (TCHW).
251
+ """
252
+ if isinstance(ele["video"], str):
253
+ video_reader_backend = video_reader_backend if video_reader_backend is not None else get_video_reader_backend()
254
+ try:
255
+ video, sample_fps, timestamps = VIDEO_READER_BACKENDS[video_reader_backend](ele)
256
+ except Exception as e:
257
+ logger.warning(f"video_reader_backend {video_reader_backend} error, use torchvision as default, msg: {e}")
258
+ video, sample_fps, timestamps = VIDEO_READER_BACKENDS["torchvision"](ele)
259
+
260
+ nframes, _, height, width = video.shape
261
+
262
+ video_total_pixels = ele.get("video_total_pixels", VIDEO_TOTAL_PIXELS)
263
+ current_pixels = nframes * height * width
264
+
265
+ if current_pixels > video_total_pixels:
266
+ scale_factor = math.sqrt(video_total_pixels / current_pixels)
267
+ new_height = int(height * scale_factor)
268
+ new_width = int(width * scale_factor)
269
+
270
+ video = transforms.functional.resize(
271
+ video,
272
+ [new_height, new_width],
273
+ interpolation=InterpolationMode.BICUBIC,
274
+ antialias=True,
275
+ ).float()
276
+ else:
277
+ video = video.float()
278
+
279
+ if return_video_sample_fps:
280
+ return video, sample_fps, timestamps
281
+ return video
282
+
283
+ else:
284
+ assert isinstance(ele["video"], (list, tuple))
285
+ process_info = ele.copy()
286
+ process_info.pop("type", None)
287
+ process_info.pop("video", None)
288
+
289
+ images = [
290
+ fetch_image({"image": video_element, **process_info})
291
+ for video_element in ele["video"]
292
+ ]
293
+
294
+ nframes = len(images)
295
+ timestamps = [-1 for i in range(nframes)]
296
+
297
+ # For list of images, we return list of PIL images directly,
298
+ # the processor will handle conversion to tensor later.
299
+ if return_video_sample_fps:
300
+ return images, process_info.get("fps", 2.0), timestamps
301
+ return images
302
+
303
+ class LocateAnythingProcessorKwargs(ProcessingKwargs, total=False):
304
+ _defaults = {
305
+ "text_kwargs": {
306
+ "padding": False,
307
+ },
308
+ "images_kwargs": {},
309
+ "videos_kwargs": {},
310
+ }
311
+
312
+
313
+ class LocateAnythingProcessor(ProcessorMixin):
314
+ attributes = ["image_processor", "tokenizer"]
315
+ valid_kwargs = [
316
+ "chat_template",
317
+ "num_image_tokens",
318
+ "image_token",
319
+ "video_token",
320
+ "images_kwargs",
321
+ "videos_kwargs",
322
+ "text_kwargs",
323
+ ]
324
+ image_processor_class = "AutoImageProcessor"
325
+ tokenizer_class = "AutoTokenizer"
326
+
327
+ def __init__(
328
+ self,
329
+ image_processor=None,
330
+ tokenizer=None,
331
+ chat_template=None,
332
+ image_token='<IMG_CONTEXT>',
333
+ video_token='<IMG_CONTEXT>',
334
+ merge_kernel_size=[2, 2], # Note: This might need adjustment based on your patch_size (14*14)
335
+ image_placeholder='image',
336
+ video_placeholder='video',
337
+ image_start_token='<img>',
338
+ image_end_token='</img>',
339
+ **kwargs,
340
+ ):
341
+ self.image_token = tokenizer.image_token if hasattr(tokenizer, "image_token") else image_token
342
+ self.video_token = tokenizer.video_token if hasattr(tokenizer, "video_token") else video_token
343
+ self.image_token_id = (
344
+ tokenizer.image_token_id
345
+ if getattr(tokenizer, "image_token_id", None)
346
+ else tokenizer.convert_tokens_to_ids(self.image_token)
347
+ )
348
+ self.video_token_id = (
349
+ tokenizer.video_token_id
350
+ if getattr(tokenizer, "video_token_id", None)
351
+ else tokenizer.convert_tokens_to_ids(self.video_token)
352
+ )
353
+ self.image_placeholder = image_placeholder
354
+ self.video_placeholder = video_placeholder
355
+ self.merge_kernel_size = merge_kernel_size
356
+ self.image_start_token = image_start_token
357
+ self.image_end_token = image_end_token
358
+ if 'auto_map' in kwargs:
359
+ self.auto_map = kwargs['auto_map']
360
+ super().__init__(image_processor, tokenizer, chat_template=chat_template)
361
+
362
+
363
+ def replace_media_placeholder(self, text, image_list, video_list, timestamps_list, fps_list, **output_kwargs):
364
+
365
+ num_of_images_in_this_sample = 0
366
+ num_of_videos_in_this_sample = 0
367
+ pattern = re.compile(rf"<({self.image_placeholder}|{self.video_placeholder})-(\d+)>")
368
+ unified_frame_list = []
369
+
370
+ def replace_in_text(text):
371
+ def repl(match):
372
+ nonlocal unified_frame_list
373
+ nonlocal num_of_images_in_this_sample
374
+ nonlocal num_of_videos_in_this_sample
375
+ media_type = match.group(1)
376
+ idx_in_list = int(match.group(2)) - 1
377
+ idx_mapper = {0: "first", 1: "second", 2: "third", 3: "fourth", 4: "fifth", 5: "sixth", 6: "seventh", 7: "eighth", 8: "ninth", 9: "tenth"}
378
+
379
+ if media_type == 'image':
380
+ # Call LocateAnythingImageProcessor with a single image in a list
381
+ image_inputs = self.image_processor(images=[image_list[idx_in_list]], **output_kwargs["images_kwargs"])
382
+
383
+ num_of_tokens_list = [int(h * w) // (self.image_processor.merge_kernel_size[0] * self.image_processor.merge_kernel_size[1]) for h, w in image_inputs['image_grid_hws']]
384
+
385
+ special_placeholder = f"<image {idx_in_list+1}>{self.image_start_token}{self.image_token * num_of_tokens_list[0]}{self.image_end_token}"
386
+ unified_frame_list.append(image_inputs)
387
+ num_of_images_in_this_sample += 1
388
+
389
+ elif media_type == 'video':
390
+ video_obj = video_list[idx_in_list]
391
+
392
+ # Convert Tensor TCHW to list of PIL Images for the ImageProcessor
393
+ if isinstance(video_obj, torch.Tensor):
394
+ # video_obj is [T, C, H, W], float, likely 0-255 or standardized
395
+ # LocateAnythingImageProcessor expects PIL or 0-255 inputs usually.
396
+ # We need to convert back to PIL or List[Tensor] compatible with make_list_of_images
397
+ video_frames = []
398
+ for i in range(video_obj.shape[0]):
399
+ frame = video_obj[i] # [C, H, W]
400
+ # Assuming fetch_video returns float tensors.
401
+ # If they are 0-255, convert to uint8.
402
+ if frame.dtype.is_floating_point and frame.max() > 1.0:
403
+ frame = frame.byte()
404
+ elif frame.dtype.is_floating_point:
405
+ frame = (frame * 255).byte()
406
+
407
+ img = transforms.ToPILImage()(frame)
408
+ video_frames.append(img)
409
+ elif isinstance(video_obj, list):
410
+ # Already list of PIL images
411
+ video_frames = video_obj
412
+ else:
413
+ raise ValueError("Unsupported video format")
414
+
415
+ # Call ImageProcessor with list of frames
416
+ video_inputs = self.image_processor(images=video_frames, **output_kwargs["videos_kwargs"])
417
+
418
+ # Calculate tokens per frame
419
+ num_of_tokens_list = [int(h * w) // (self.image_processor.merge_kernel_size[0] * self.image_processor.merge_kernel_size[1]) for h, w in video_inputs['image_grid_hws']]
420
+
421
+ if timestamps_list is not None and -1 not in timestamps_list:
422
+ frame_timestamps = timestamps_list[idx_in_list]
423
+ else:
424
+ frame_timestamps = None
425
+ sampled_fps = fps_list[idx_in_list] if fps_list is not None else None
426
+
427
+ if frame_timestamps is not None:
428
+ # Ensure lengths match (sometimes rounding might cause off-by-one if not careful, but usually safe here)
429
+ if len(frame_timestamps) != len(num_of_tokens_list):
430
+ logger.warning(f"Timestamp mismatch: {len(frame_timestamps)} vs {len(num_of_tokens_list)}")
431
+ min_len = min(len(frame_timestamps), len(num_of_tokens_list))
432
+ frame_timestamps = frame_timestamps[:min_len]
433
+ num_of_tokens_list = num_of_tokens_list[:min_len]
434
+
435
+ special_placeholder = [f"Frame-{i+1}-{frame_timestamps[i]:.2f}s: {self.image_start_token}{self.image_token * num_of_tokens}{self.image_end_token}" for i, num_of_tokens in enumerate(num_of_tokens_list)]
436
+ else:
437
+ special_placeholder = [f"Frame-{i+1}: {self.image_start_token}{self.image_token * num_of_tokens}{self.image_end_token}" for i, num_of_tokens in enumerate(num_of_tokens_list)]
438
+
439
+ if sampled_fps is not None:
440
+ special_placeholder = f"The {idx_mapper[idx_in_list]} video sampled with {sampled_fps:.2f} fps: " + "".join(special_placeholder)
441
+ else:
442
+ special_placeholder = f"The {idx_mapper[idx_in_list]} video: " + "".join(special_placeholder)
443
+
444
+ unified_frame_list.append(video_inputs)
445
+ num_of_videos_in_this_sample += 1
446
+ else:
447
+ raise ValueError(f'Unknown media type: {media_type}')
448
+ return special_placeholder
449
+ return pattern.sub(repl, text)
450
+
451
+ text = replace_in_text(text)
452
+
453
+ if len(unified_frame_list) > 0:
454
+ # Concatenate all pixel values from all images/videos in this sample
455
+ pixel_values = torch.cat([frame['pixel_values'] for frame in unified_frame_list], dim=0)
456
+ # Concatenate grid hws
457
+ image_grid_hws = np.concatenate([frame['image_grid_hws'] for frame in unified_frame_list], axis=0)
458
+ else:
459
+ pixel_values = torch.empty(0)
460
+ image_grid_hws = np.empty(0)
461
+
462
+ return text, pixel_values, image_grid_hws, num_of_images_in_this_sample, num_of_videos_in_this_sample
463
+
464
+ def __call__(
465
+ self,
466
+ images: ImageInput = None,
467
+ text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None,
468
+ audio=None,
469
+ videos: VideoInput = None,
470
+ **kwargs: Unpack[LocateAnythingProcessorKwargs],
471
+ ) -> BatchFeature:
472
+ output_kwargs = self._merge_kwargs(
473
+ LocateAnythingProcessorKwargs,
474
+ tokenizer_init_kwargs=self.tokenizer.init_kwargs,
475
+ **kwargs,
476
+ )
477
+
478
+ if isinstance(text, str):
479
+ text_list = [text]
480
+ elif not isinstance(text, list) and not isinstance(text[0], str):
481
+ raise ValueError("Invalid input text. Please provide a string, or a list of strings")
482
+ elif isinstance(text, list) and isinstance(text[0], str):
483
+ text_list = text
484
+
485
+ if images is None: images = []
486
+ if videos is None: videos = []
487
+
488
+ pixel_values_list = []
489
+ image_grid_hws_list = []
490
+ new_sample_list = []
491
+ image_start_idx = 0
492
+ video_start_idx = 0
493
+ timestamps_batch = output_kwargs['videos_kwargs'].pop("timestamps", None)
494
+ fps_batch = output_kwargs['videos_kwargs'].pop("fps", None)
495
+
496
+ for sample in text_list:
497
+ timestamps_list = timestamps_batch[video_start_idx:] if timestamps_batch is not None else None
498
+ fps_list = fps_batch[video_start_idx:] if fps_batch is not None else None
499
+
500
+ sample, pixel_values, image_grid_hws, num_of_images_in_this_sample, num_of_videos_in_this_sample = self.replace_media_placeholder(
501
+ sample, images[image_start_idx:], videos[video_start_idx:], timestamps_list, fps_list, **output_kwargs
502
+ )
503
+ new_sample_list.append(sample)
504
+
505
+ if pixel_values.numel() > 0:
506
+ pixel_values_list.append(pixel_values)
507
+ image_grid_hws_list.append(image_grid_hws)
508
+
509
+ image_start_idx += num_of_images_in_this_sample
510
+ video_start_idx += num_of_videos_in_this_sample
511
+
512
+ image_inputs = {}
513
+ if len(pixel_values_list) > 0:
514
+ # Concatenate across the batch
515
+ image_inputs['pixel_values'] = torch.cat(pixel_values_list, dim=0)
516
+ image_inputs['image_grid_hws'] = np.concatenate(image_grid_hws_list, axis=0)
517
+
518
+ video_inputs = {} # Video data is merged into image_inputs now
519
+ text_inputs = self.tokenizer(new_sample_list, **output_kwargs["text_kwargs"])
520
+
521
+ return BatchFeature(data={**text_inputs, **image_inputs, **video_inputs})
522
+
523
+ def batch_decode(self, *args, **kwargs):
524
+ return self.tokenizer.batch_decode(*args, **kwargs)
525
+
526
+ def decode(self, *args, **kwargs):
527
+ return self.tokenizer.decode(*args, **kwargs)
528
+
529
+ @property
530
+ def model_input_names(self):
531
+ tokenizer_input_names = self.tokenizer.model_input_names
532
+ image_processor_input_names = self.image_processor.model_input_names
533
+ return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))
534
+
535
+ def save_pretrained(self, save_directory, **kwargs):
536
+ if os.path.isfile(save_directory):
537
+ raise ValueError(f"Provided path ({save_directory}) should be a directory, not a file")
538
+ os.makedirs(save_directory, exist_ok=True)
539
+ outputs = super().save_pretrained(save_directory, **kwargs)
540
+ return outputs
541
+
542
+ @classmethod
543
+ def from_pretrained(cls, pretrained_model_name_or_path, **kwargs):
544
+ processor = super().from_pretrained(pretrained_model_name_or_path, **kwargs)
545
+ if isinstance(processor, tuple):
546
+ processor = processor[0]
547
+ return processor
548
+
549
+ def process_vision_info(
550
+ self,
551
+ conversations: list[dict] | list[list[dict]],
552
+ return_video_kwargs: bool = False,
553
+ video_reader_backend: str = "torchvision",
554
+ ) -> tuple[list[Image.Image] | None, list[torch.Tensor | list[Image.Image]] | None, Optional[dict]]:
555
+
556
+ vision_infos = self.extract_vision_info(conversations)
557
+ image_inputs = []
558
+ video_inputs = []
559
+ video_sample_fps_list = []
560
+ video_timestamps_list = []
561
+
562
+ for vision_info in vision_infos:
563
+ if "image" in vision_info or "image_url" in vision_info:
564
+ image_inputs.append(fetch_image(vision_info))
565
+ elif "video" in vision_info:
566
+ video_input, video_sample_fps, video_timestamps = fetch_video(vision_info, return_video_sample_fps=True, video_reader_backend=video_reader_backend)
567
+ video_sample_fps_list.append(video_sample_fps)
568
+ video_inputs.append(video_input)
569
+ video_timestamps_list.append(video_timestamps)
570
+ else:
571
+ raise ValueError("image, image_url or video should in content.")
572
+
573
+ if len(image_inputs) == 0:
574
+ image_inputs = None
575
+ if len(video_inputs) == 0:
576
+ video_inputs = None
577
+
578
+ if return_video_kwargs:
579
+ return image_inputs, video_inputs, {'fps': video_sample_fps_list, 'timestamps': video_timestamps_list}
580
+ return image_inputs, video_inputs
581
+
582
+ def extract_vision_info(self, conversations: list[dict] | list[list[dict]]) -> list[dict]:
583
+ vision_infos = []
584
+ if isinstance(conversations[0], dict):
585
+ conversations = [conversations]
586
+ for conversation in conversations:
587
+ for message in conversation:
588
+ if isinstance(message["content"], list):
589
+ for ele in message["content"]:
590
+ if (
591
+ "image" in ele
592
+ or "image_url" in ele
593
+ or "video" in ele
594
+ or ele["type"] in ("image", "image_url", "video")
595
+ ):
596
+ vision_infos.append(ele)
597
+ return vision_infos
598
+
599
+ def py_apply_chat_template(self, messages, tokenize=False, add_generation_prompt=False):
600
+ assert tokenize == False, "tokenize is not supported yet"
601
+ result = ""
602
+ image_count = 0
603
+ video_count = 0
604
+
605
+ message_text = ""
606
+ for idx, message in enumerate(messages):
607
+ if message.get('role') != 'user': continue
608
+ content = message.get('content')
609
+ if isinstance(content, str):
610
+ message_text += content
611
+ elif isinstance(content, list):
612
+ for item in content:
613
+ if isinstance(item, dict) and "text" in item:
614
+ message_text += item["text"]
615
+ elif isinstance(item, str):
616
+ message_text += item
617
+
618
+ for idx, message in enumerate(messages):
619
+ if idx == 0 and message.get('role') != 'system':
620
+ result += "<|im_start|>system\n"
621
+ result += "You are a helpful assistant.\n"
622
+ result += "<|im_end|>\n"
623
+
624
+ result += f"<|im_start|>{message.get('role', '')}\n"
625
+ content = message.get('content')
626
+
627
+ if isinstance(content, str):
628
+ result += content
629
+ result += "<|im_end|>\n"
630
+ else:
631
+ for item in content:
632
+ if (isinstance(item, dict) and (item.get('type') == 'image' or 'image' in item or 'image_url' in item)):
633
+ image_count += 1
634
+ candidate_token = f"<image-{image_count}>"
635
+ if candidate_token not in message_text:
636
+ result += candidate_token
637
+ elif (isinstance(item, dict) and (item.get('type') == 'video' or 'video' in item)):
638
+ video_count += 1
639
+ candidate_token = f"<video-{video_count}>"
640
+ if candidate_token not in message_text:
641
+ result += candidate_token
642
+ elif isinstance(item, dict) and 'text' in item:
643
+ result += item['text']
644
+ elif isinstance(item, str):
645
+ result += item
646
+ result += "<|im_end|>\n"
647
+
648
+ if add_generation_prompt:
649
+ result += "<|im_start|>assistant\n"
650
+
651
+ return result
652
+
653
+
654
+ @classmethod
655
+ def from_args_and_dict(cls, args, processor_dict: dict[str, Any], **kwargs):
656
+ processor_dict = processor_dict.copy()
657
+ return_unused_kwargs = kwargs.pop("return_unused_kwargs", False)
658
+
659
+ if "processor_class" in processor_dict:
660
+ del processor_dict["processor_class"]
661
+
662
+ unused_kwargs = cls.validate_init_kwargs(processor_config=processor_dict, valid_kwargs=cls.valid_kwargs)
663
+ processor = cls(*args, **processor_dict)
664
+
665
+ for key in set(kwargs.keys()):
666
+ if hasattr(processor, key):
667
+ setattr(processor, key, kwargs.pop(key))
668
+
669
+ if isinstance(unused_kwargs, dict):
670
+ kwargs.update(unused_kwargs)
671
+ logger.info(f"Processor {processor}")
672
+ if return_unused_kwargs:
673
+ return processor, kwargs
674
+ else:
675
+ return processor
676
+
677
+
678
+ __all__ = ["LocateAnythingProcessor"]
processor_config.json ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "auto_map": {
3
+ "AutoImageProcessor": "image_processing_locateanything.LocateAnythingImageProcessor",
4
+ "AutoProcessor": "processing_locateanything.LocateAnythingProcessor"
5
+ },
6
+ "image_end_token": "</img>",
7
+ "image_placeholder": "image",
8
+ "image_start_token": "<img>",
9
+ "image_token": "<IMG_CONTEXT>",
10
+ "merge_kernel_size": [
11
+ 2,
12
+ 2
13
+ ],
14
+ "processor_class": "LocateAnythingProcessor",
15
+ "video_placeholder": "video",
16
+ "video_token": "<IMG_CONTEXT>",
17
+ "patch_size": 14
18
+ }
pyproject.toml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [build-system]
2
+ requires = ["setuptools>=68", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "locateanything-la-flash"
7
+ version = "0.1.0"
8
+ description = "LocateAnything batch utils with LA Flash inference backend."
9
+ requires-python = ">=3.10"
10
+ dependencies = [
11
+ "numpy",
12
+ "pillow",
13
+ "torch",
14
+ "transformers",
15
+ ]
16
+
17
+ [tool.setuptools]
18
+ packages = ["batch_utils", "kernel_utils"]
special_tokens_map.json ADDED
@@ -0,0 +1,1053 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "additional_special_tokens": [
3
+ "<|im_start|>",
4
+ "<|im_end|>",
5
+ "<|object_ref_start|>",
6
+ "<|object_ref_end|>",
7
+ "<|box_start|>",
8
+ "<|box_end|>",
9
+ "<|quad_start|>",
10
+ "<|quad_end|>",
11
+ "<|vision_start|>",
12
+ "<|vision_end|>",
13
+ "<|vision_pad|>",
14
+ "<|image_pad|>",
15
+ "<|video_pad|>",
16
+ "<IMG_CONTEXT>",
17
+ "<img>",
18
+ "</img>",
19
+ "<box>",
20
+ "</box>",
21
+ "<quad>",
22
+ "</quad>",
23
+ "<ref>",
24
+ "</ref>",
25
+ "<interval>",
26
+ "</interval>",
27
+ "<text_mask>",
28
+ "<0>",
29
+ "<1>",
30
+ "<2>",
31
+ "<3>",
32
+ "<4>",
33
+ "<5>",
34
+ "<6>",
35
+ "<7>",
36
+ "<8>",
37
+ "<9>",
38
+ "<10>",
39
+ "<11>",
40
+ "<12>",
41
+ "<13>",
42
+ "<14>",
43
+ "<15>",
44
+ "<16>",
45
+ "<17>",
46
+ "<18>",
47
+ "<19>",
48
+ "<20>",
49
+ "<21>",
50
+ "<22>",
51
+ "<23>",
52
+ "<24>",
53
+ "<25>",
54
+ "<26>",
55
+ "<27>",
56
+ "<28>",
57
+ "<29>",
58
+ "<30>",
59
+ "<31>",
60
+ "<32>",
61
+ "<33>",
62
+ "<34>",
63
+ "<35>",
64
+ "<36>",
65
+ "<37>",
66
+ "<38>",
67
+ "<39>",
68
+ "<40>",
69
+ "<41>",
70
+ "<42>",
71
+ "<43>",
72
+ "<44>",
73
+ "<45>",
74
+ "<46>",
75
+ "<47>",
76
+ "<48>",
77
+ "<49>",
78
+ "<50>",
79
+ "<51>",
80
+ "<52>",
81
+ "<53>",
82
+ "<54>",
83
+ "<55>",
84
+ "<56>",
85
+ "<57>",
86
+ "<58>",
87
+ "<59>",
88
+ "<60>",
89
+ "<61>",
90
+ "<62>",
91
+ "<63>",
92
+ "<64>",
93
+ "<65>",
94
+ "<66>",
95
+ "<67>",
96
+ "<68>",
97
+ "<69>",
98
+ "<70>",
99
+ "<71>",
100
+ "<72>",
101
+ "<73>",
102
+ "<74>",
103
+ "<75>",
104
+ "<76>",
105
+ "<77>",
106
+ "<78>",
107
+ "<79>",
108
+ "<80>",
109
+ "<81>",
110
+ "<82>",
111
+ "<83>",
112
+ "<84>",
113
+ "<85>",
114
+ "<86>",
115
+ "<87>",
116
+ "<88>",
117
+ "<89>",
118
+ "<90>",
119
+ "<91>",
120
+ "<92>",
121
+ "<93>",
122
+ "<94>",
123
+ "<95>",
124
+ "<96>",
125
+ "<97>",
126
+ "<98>",
127
+ "<99>",
128
+ "<100>",
129
+ "<101>",
130
+ "<102>",
131
+ "<103>",
132
+ "<104>",
133
+ "<105>",
134
+ "<106>",
135
+ "<107>",
136
+ "<108>",
137
+ "<109>",
138
+ "<110>",
139
+ "<111>",
140
+ "<112>",
141
+ "<113>",
142
+ "<114>",
143
+ "<115>",
144
+ "<116>",
145
+ "<117>",
146
+ "<118>",
147
+ "<119>",
148
+ "<120>",
149
+ "<121>",
150
+ "<122>",
151
+ "<123>",
152
+ "<124>",
153
+ "<125>",
154
+ "<126>",
155
+ "<127>",
156
+ "<128>",
157
+ "<129>",
158
+ "<130>",
159
+ "<131>",
160
+ "<132>",
161
+ "<133>",
162
+ "<134>",
163
+ "<135>",
164
+ "<136>",
165
+ "<137>",
166
+ "<138>",
167
+ "<139>",
168
+ "<140>",
169
+ "<141>",
170
+ "<142>",
171
+ "<143>",
172
+ "<144>",
173
+ "<145>",
174
+ "<146>",
175
+ "<147>",
176
+ "<148>",
177
+ "<149>",
178
+ "<150>",
179
+ "<151>",
180
+ "<152>",
181
+ "<153>",
182
+ "<154>",
183
+ "<155>",
184
+ "<156>",
185
+ "<157>",
186
+ "<158>",
187
+ "<159>",
188
+ "<160>",
189
+ "<161>",
190
+ "<162>",
191
+ "<163>",
192
+ "<164>",
193
+ "<165>",
194
+ "<166>",
195
+ "<167>",
196
+ "<168>",
197
+ "<169>",
198
+ "<170>",
199
+ "<171>",
200
+ "<172>",
201
+ "<173>",
202
+ "<174>",
203
+ "<175>",
204
+ "<176>",
205
+ "<177>",
206
+ "<178>",
207
+ "<179>",
208
+ "<180>",
209
+ "<181>",
210
+ "<182>",
211
+ "<183>",
212
+ "<184>",
213
+ "<185>",
214
+ "<186>",
215
+ "<187>",
216
+ "<188>",
217
+ "<189>",
218
+ "<190>",
219
+ "<191>",
220
+ "<192>",
221
+ "<193>",
222
+ "<194>",
223
+ "<195>",
224
+ "<196>",
225
+ "<197>",
226
+ "<198>",
227
+ "<199>",
228
+ "<200>",
229
+ "<201>",
230
+ "<202>",
231
+ "<203>",
232
+ "<204>",
233
+ "<205>",
234
+ "<206>",
235
+ "<207>",
236
+ "<208>",
237
+ "<209>",
238
+ "<210>",
239
+ "<211>",
240
+ "<212>",
241
+ "<213>",
242
+ "<214>",
243
+ "<215>",
244
+ "<216>",
245
+ "<217>",
246
+ "<218>",
247
+ "<219>",
248
+ "<220>",
249
+ "<221>",
250
+ "<222>",
251
+ "<223>",
252
+ "<224>",
253
+ "<225>",
254
+ "<226>",
255
+ "<227>",
256
+ "<228>",
257
+ "<229>",
258
+ "<230>",
259
+ "<231>",
260
+ "<232>",
261
+ "<233>",
262
+ "<234>",
263
+ "<235>",
264
+ "<236>",
265
+ "<237>",
266
+ "<238>",
267
+ "<239>",
268
+ "<240>",
269
+ "<241>",
270
+ "<242>",
271
+ "<243>",
272
+ "<244>",
273
+ "<245>",
274
+ "<246>",
275
+ "<247>",
276
+ "<248>",
277
+ "<249>",
278
+ "<250>",
279
+ "<251>",
280
+ "<252>",
281
+ "<253>",
282
+ "<254>",
283
+ "<255>",
284
+ "<256>",
285
+ "<257>",
286
+ "<258>",
287
+ "<259>",
288
+ "<260>",
289
+ "<261>",
290
+ "<262>",
291
+ "<263>",
292
+ "<264>",
293
+ "<265>",
294
+ "<266>",
295
+ "<267>",
296
+ "<268>",
297
+ "<269>",
298
+ "<270>",
299
+ "<271>",
300
+ "<272>",
301
+ "<273>",
302
+ "<274>",
303
+ "<275>",
304
+ "<276>",
305
+ "<277>",
306
+ "<278>",
307
+ "<279>",
308
+ "<280>",
309
+ "<281>",
310
+ "<282>",
311
+ "<283>",
312
+ "<284>",
313
+ "<285>",
314
+ "<286>",
315
+ "<287>",
316
+ "<288>",
317
+ "<289>",
318
+ "<290>",
319
+ "<291>",
320
+ "<292>",
321
+ "<293>",
322
+ "<294>",
323
+ "<295>",
324
+ "<296>",
325
+ "<297>",
326
+ "<298>",
327
+ "<299>",
328
+ "<300>",
329
+ "<301>",
330
+ "<302>",
331
+ "<303>",
332
+ "<304>",
333
+ "<305>",
334
+ "<306>",
335
+ "<307>",
336
+ "<308>",
337
+ "<309>",
338
+ "<310>",
339
+ "<311>",
340
+ "<312>",
341
+ "<313>",
342
+ "<314>",
343
+ "<315>",
344
+ "<316>",
345
+ "<317>",
346
+ "<318>",
347
+ "<319>",
348
+ "<320>",
349
+ "<321>",
350
+ "<322>",
351
+ "<323>",
352
+ "<324>",
353
+ "<325>",
354
+ "<326>",
355
+ "<327>",
356
+ "<328>",
357
+ "<329>",
358
+ "<330>",
359
+ "<331>",
360
+ "<332>",
361
+ "<333>",
362
+ "<334>",
363
+ "<335>",
364
+ "<336>",
365
+ "<337>",
366
+ "<338>",
367
+ "<339>",
368
+ "<340>",
369
+ "<341>",
370
+ "<342>",
371
+ "<343>",
372
+ "<344>",
373
+ "<345>",
374
+ "<346>",
375
+ "<347>",
376
+ "<348>",
377
+ "<349>",
378
+ "<350>",
379
+ "<351>",
380
+ "<352>",
381
+ "<353>",
382
+ "<354>",
383
+ "<355>",
384
+ "<356>",
385
+ "<357>",
386
+ "<358>",
387
+ "<359>",
388
+ "<360>",
389
+ "<361>",
390
+ "<362>",
391
+ "<363>",
392
+ "<364>",
393
+ "<365>",
394
+ "<366>",
395
+ "<367>",
396
+ "<368>",
397
+ "<369>",
398
+ "<370>",
399
+ "<371>",
400
+ "<372>",
401
+ "<373>",
402
+ "<374>",
403
+ "<375>",
404
+ "<376>",
405
+ "<377>",
406
+ "<378>",
407
+ "<379>",
408
+ "<380>",
409
+ "<381>",
410
+ "<382>",
411
+ "<383>",
412
+ "<384>",
413
+ "<385>",
414
+ "<386>",
415
+ "<387>",
416
+ "<388>",
417
+ "<389>",
418
+ "<390>",
419
+ "<391>",
420
+ "<392>",
421
+ "<393>",
422
+ "<394>",
423
+ "<395>",
424
+ "<396>",
425
+ "<397>",
426
+ "<398>",
427
+ "<399>",
428
+ "<400>",
429
+ "<401>",
430
+ "<402>",
431
+ "<403>",
432
+ "<404>",
433
+ "<405>",
434
+ "<406>",
435
+ "<407>",
436
+ "<408>",
437
+ "<409>",
438
+ "<410>",
439
+ "<411>",
440
+ "<412>",
441
+ "<413>",
442
+ "<414>",
443
+ "<415>",
444
+ "<416>",
445
+ "<417>",
446
+ "<418>",
447
+ "<419>",
448
+ "<420>",
449
+ "<421>",
450
+ "<422>",
451
+ "<423>",
452
+ "<424>",
453
+ "<425>",
454
+ "<426>",
455
+ "<427>",
456
+ "<428>",
457
+ "<429>",
458
+ "<430>",
459
+ "<431>",
460
+ "<432>",
461
+ "<433>",
462
+ "<434>",
463
+ "<435>",
464
+ "<436>",
465
+ "<437>",
466
+ "<438>",
467
+ "<439>",
468
+ "<440>",
469
+ "<441>",
470
+ "<442>",
471
+ "<443>",
472
+ "<444>",
473
+ "<445>",
474
+ "<446>",
475
+ "<447>",
476
+ "<448>",
477
+ "<449>",
478
+ "<450>",
479
+ "<451>",
480
+ "<452>",
481
+ "<453>",
482
+ "<454>",
483
+ "<455>",
484
+ "<456>",
485
+ "<457>",
486
+ "<458>",
487
+ "<459>",
488
+ "<460>",
489
+ "<461>",
490
+ "<462>",
491
+ "<463>",
492
+ "<464>",
493
+ "<465>",
494
+ "<466>",
495
+ "<467>",
496
+ "<468>",
497
+ "<469>",
498
+ "<470>",
499
+ "<471>",
500
+ "<472>",
501
+ "<473>",
502
+ "<474>",
503
+ "<475>",
504
+ "<476>",
505
+ "<477>",
506
+ "<478>",
507
+ "<479>",
508
+ "<480>",
509
+ "<481>",
510
+ "<482>",
511
+ "<483>",
512
+ "<484>",
513
+ "<485>",
514
+ "<486>",
515
+ "<487>",
516
+ "<488>",
517
+ "<489>",
518
+ "<490>",
519
+ "<491>",
520
+ "<492>",
521
+ "<493>",
522
+ "<494>",
523
+ "<495>",
524
+ "<496>",
525
+ "<497>",
526
+ "<498>",
527
+ "<499>",
528
+ "<500>",
529
+ "<501>",
530
+ "<502>",
531
+ "<503>",
532
+ "<504>",
533
+ "<505>",
534
+ "<506>",
535
+ "<507>",
536
+ "<508>",
537
+ "<509>",
538
+ "<510>",
539
+ "<511>",
540
+ "<512>",
541
+ "<513>",
542
+ "<514>",
543
+ "<515>",
544
+ "<516>",
545
+ "<517>",
546
+ "<518>",
547
+ "<519>",
548
+ "<520>",
549
+ "<521>",
550
+ "<522>",
551
+ "<523>",
552
+ "<524>",
553
+ "<525>",
554
+ "<526>",
555
+ "<527>",
556
+ "<528>",
557
+ "<529>",
558
+ "<530>",
559
+ "<531>",
560
+ "<532>",
561
+ "<533>",
562
+ "<534>",
563
+ "<535>",
564
+ "<536>",
565
+ "<537>",
566
+ "<538>",
567
+ "<539>",
568
+ "<540>",
569
+ "<541>",
570
+ "<542>",
571
+ "<543>",
572
+ "<544>",
573
+ "<545>",
574
+ "<546>",
575
+ "<547>",
576
+ "<548>",
577
+ "<549>",
578
+ "<550>",
579
+ "<551>",
580
+ "<552>",
581
+ "<553>",
582
+ "<554>",
583
+ "<555>",
584
+ "<556>",
585
+ "<557>",
586
+ "<558>",
587
+ "<559>",
588
+ "<560>",
589
+ "<561>",
590
+ "<562>",
591
+ "<563>",
592
+ "<564>",
593
+ "<565>",
594
+ "<566>",
595
+ "<567>",
596
+ "<568>",
597
+ "<569>",
598
+ "<570>",
599
+ "<571>",
600
+ "<572>",
601
+ "<573>",
602
+ "<574>",
603
+ "<575>",
604
+ "<576>",
605
+ "<577>",
606
+ "<578>",
607
+ "<579>",
608
+ "<580>",
609
+ "<581>",
610
+ "<582>",
611
+ "<583>",
612
+ "<584>",
613
+ "<585>",
614
+ "<586>",
615
+ "<587>",
616
+ "<588>",
617
+ "<589>",
618
+ "<590>",
619
+ "<591>",
620
+ "<592>",
621
+ "<593>",
622
+ "<594>",
623
+ "<595>",
624
+ "<596>",
625
+ "<597>",
626
+ "<598>",
627
+ "<599>",
628
+ "<600>",
629
+ "<601>",
630
+ "<602>",
631
+ "<603>",
632
+ "<604>",
633
+ "<605>",
634
+ "<606>",
635
+ "<607>",
636
+ "<608>",
637
+ "<609>",
638
+ "<610>",
639
+ "<611>",
640
+ "<612>",
641
+ "<613>",
642
+ "<614>",
643
+ "<615>",
644
+ "<616>",
645
+ "<617>",
646
+ "<618>",
647
+ "<619>",
648
+ "<620>",
649
+ "<621>",
650
+ "<622>",
651
+ "<623>",
652
+ "<624>",
653
+ "<625>",
654
+ "<626>",
655
+ "<627>",
656
+ "<628>",
657
+ "<629>",
658
+ "<630>",
659
+ "<631>",
660
+ "<632>",
661
+ "<633>",
662
+ "<634>",
663
+ "<635>",
664
+ "<636>",
665
+ "<637>",
666
+ "<638>",
667
+ "<639>",
668
+ "<640>",
669
+ "<641>",
670
+ "<642>",
671
+ "<643>",
672
+ "<644>",
673
+ "<645>",
674
+ "<646>",
675
+ "<647>",
676
+ "<648>",
677
+ "<649>",
678
+ "<650>",
679
+ "<651>",
680
+ "<652>",
681
+ "<653>",
682
+ "<654>",
683
+ "<655>",
684
+ "<656>",
685
+ "<657>",
686
+ "<658>",
687
+ "<659>",
688
+ "<660>",
689
+ "<661>",
690
+ "<662>",
691
+ "<663>",
692
+ "<664>",
693
+ "<665>",
694
+ "<666>",
695
+ "<667>",
696
+ "<668>",
697
+ "<669>",
698
+ "<670>",
699
+ "<671>",
700
+ "<672>",
701
+ "<673>",
702
+ "<674>",
703
+ "<675>",
704
+ "<676>",
705
+ "<677>",
706
+ "<678>",
707
+ "<679>",
708
+ "<680>",
709
+ "<681>",
710
+ "<682>",
711
+ "<683>",
712
+ "<684>",
713
+ "<685>",
714
+ "<686>",
715
+ "<687>",
716
+ "<688>",
717
+ "<689>",
718
+ "<690>",
719
+ "<691>",
720
+ "<692>",
721
+ "<693>",
722
+ "<694>",
723
+ "<695>",
724
+ "<696>",
725
+ "<697>",
726
+ "<698>",
727
+ "<699>",
728
+ "<700>",
729
+ "<701>",
730
+ "<702>",
731
+ "<703>",
732
+ "<704>",
733
+ "<705>",
734
+ "<706>",
735
+ "<707>",
736
+ "<708>",
737
+ "<709>",
738
+ "<710>",
739
+ "<711>",
740
+ "<712>",
741
+ "<713>",
742
+ "<714>",
743
+ "<715>",
744
+ "<716>",
745
+ "<717>",
746
+ "<718>",
747
+ "<719>",
748
+ "<720>",
749
+ "<721>",
750
+ "<722>",
751
+ "<723>",
752
+ "<724>",
753
+ "<725>",
754
+ "<726>",
755
+ "<727>",
756
+ "<728>",
757
+ "<729>",
758
+ "<730>",
759
+ "<731>",
760
+ "<732>",
761
+ "<733>",
762
+ "<734>",
763
+ "<735>",
764
+ "<736>",
765
+ "<737>",
766
+ "<738>",
767
+ "<739>",
768
+ "<740>",
769
+ "<741>",
770
+ "<742>",
771
+ "<743>",
772
+ "<744>",
773
+ "<745>",
774
+ "<746>",
775
+ "<747>",
776
+ "<748>",
777
+ "<749>",
778
+ "<750>",
779
+ "<751>",
780
+ "<752>",
781
+ "<753>",
782
+ "<754>",
783
+ "<755>",
784
+ "<756>",
785
+ "<757>",
786
+ "<758>",
787
+ "<759>",
788
+ "<760>",
789
+ "<761>",
790
+ "<762>",
791
+ "<763>",
792
+ "<764>",
793
+ "<765>",
794
+ "<766>",
795
+ "<767>",
796
+ "<768>",
797
+ "<769>",
798
+ "<770>",
799
+ "<771>",
800
+ "<772>",
801
+ "<773>",
802
+ "<774>",
803
+ "<775>",
804
+ "<776>",
805
+ "<777>",
806
+ "<778>",
807
+ "<779>",
808
+ "<780>",
809
+ "<781>",
810
+ "<782>",
811
+ "<783>",
812
+ "<784>",
813
+ "<785>",
814
+ "<786>",
815
+ "<787>",
816
+ "<788>",
817
+ "<789>",
818
+ "<790>",
819
+ "<791>",
820
+ "<792>",
821
+ "<793>",
822
+ "<794>",
823
+ "<795>",
824
+ "<796>",
825
+ "<797>",
826
+ "<798>",
827
+ "<799>",
828
+ "<800>",
829
+ "<801>",
830
+ "<802>",
831
+ "<803>",
832
+ "<804>",
833
+ "<805>",
834
+ "<806>",
835
+ "<807>",
836
+ "<808>",
837
+ "<809>",
838
+ "<810>",
839
+ "<811>",
840
+ "<812>",
841
+ "<813>",
842
+ "<814>",
843
+ "<815>",
844
+ "<816>",
845
+ "<817>",
846
+ "<818>",
847
+ "<819>",
848
+ "<820>",
849
+ "<821>",
850
+ "<822>",
851
+ "<823>",
852
+ "<824>",
853
+ "<825>",
854
+ "<826>",
855
+ "<827>",
856
+ "<828>",
857
+ "<829>",
858
+ "<830>",
859
+ "<831>",
860
+ "<832>",
861
+ "<833>",
862
+ "<834>",
863
+ "<835>",
864
+ "<836>",
865
+ "<837>",
866
+ "<838>",
867
+ "<839>",
868
+ "<840>",
869
+ "<841>",
870
+ "<842>",
871
+ "<843>",
872
+ "<844>",
873
+ "<845>",
874
+ "<846>",
875
+ "<847>",
876
+ "<848>",
877
+ "<849>",
878
+ "<850>",
879
+ "<851>",
880
+ "<852>",
881
+ "<853>",
882
+ "<854>",
883
+ "<855>",
884
+ "<856>",
885
+ "<857>",
886
+ "<858>",
887
+ "<859>",
888
+ "<860>",
889
+ "<861>",
890
+ "<862>",
891
+ "<863>",
892
+ "<864>",
893
+ "<865>",
894
+ "<866>",
895
+ "<867>",
896
+ "<868>",
897
+ "<869>",
898
+ "<870>",
899
+ "<871>",
900
+ "<872>",
901
+ "<873>",
902
+ "<874>",
903
+ "<875>",
904
+ "<876>",
905
+ "<877>",
906
+ "<878>",
907
+ "<879>",
908
+ "<880>",
909
+ "<881>",
910
+ "<882>",
911
+ "<883>",
912
+ "<884>",
913
+ "<885>",
914
+ "<886>",
915
+ "<887>",
916
+ "<888>",
917
+ "<889>",
918
+ "<890>",
919
+ "<891>",
920
+ "<892>",
921
+ "<893>",
922
+ "<894>",
923
+ "<895>",
924
+ "<896>",
925
+ "<897>",
926
+ "<898>",
927
+ "<899>",
928
+ "<900>",
929
+ "<901>",
930
+ "<902>",
931
+ "<903>",
932
+ "<904>",
933
+ "<905>",
934
+ "<906>",
935
+ "<907>",
936
+ "<908>",
937
+ "<909>",
938
+ "<910>",
939
+ "<911>",
940
+ "<912>",
941
+ "<913>",
942
+ "<914>",
943
+ "<915>",
944
+ "<916>",
945
+ "<917>",
946
+ "<918>",
947
+ "<919>",
948
+ "<920>",
949
+ "<921>",
950
+ "<922>",
951
+ "<923>",
952
+ "<924>",
953
+ "<925>",
954
+ "<926>",
955
+ "<927>",
956
+ "<928>",
957
+ "<929>",
958
+ "<930>",
959
+ "<931>",
960
+ "<932>",
961
+ "<933>",
962
+ "<934>",
963
+ "<935>",
964
+ "<936>",
965
+ "<937>",
966
+ "<938>",
967
+ "<939>",
968
+ "<940>",
969
+ "<941>",
970
+ "<942>",
971
+ "<943>",
972
+ "<944>",
973
+ "<945>",
974
+ "<946>",
975
+ "<947>",
976
+ "<948>",
977
+ "<949>",
978
+ "<950>",
979
+ "<951>",
980
+ "<952>",
981
+ "<953>",
982
+ "<954>",
983
+ "<955>",
984
+ "<956>",
985
+ "<957>",
986
+ "<958>",
987
+ "<959>",
988
+ "<960>",
989
+ "<961>",
990
+ "<962>",
991
+ "<963>",
992
+ "<964>",
993
+ "<965>",
994
+ "<966>",
995
+ "<967>",
996
+ "<968>",
997
+ "<969>",
998
+ "<970>",
999
+ "<971>",
1000
+ "<972>",
1001
+ "<973>",
1002
+ "<974>",
1003
+ "<975>",
1004
+ "<976>",
1005
+ "<977>",
1006
+ "<978>",
1007
+ "<979>",
1008
+ "<980>",
1009
+ "<981>",
1010
+ "<982>",
1011
+ "<983>",
1012
+ "<984>",
1013
+ "<985>",
1014
+ "<986>",
1015
+ "<987>",
1016
+ "<988>",
1017
+ "<989>",
1018
+ "<990>",
1019
+ "<991>",
1020
+ "<992>",
1021
+ "<993>",
1022
+ "<994>",
1023
+ "<995>",
1024
+ "<996>",
1025
+ "<997>",
1026
+ "<998>",
1027
+ "<999>",
1028
+ "<1000>",
1029
+ "<null>",
1030
+ "<switch>",
1031
+ {
1032
+ "content": "</c>",
1033
+ "lstrip": false,
1034
+ "normalized": false,
1035
+ "rstrip": false,
1036
+ "single_word": false
1037
+ }
1038
+ ],
1039
+ "eos_token": {
1040
+ "content": "<|im_end|>",
1041
+ "lstrip": false,
1042
+ "normalized": false,
1043
+ "rstrip": false,
1044
+ "single_word": false
1045
+ },
1046
+ "pad_token": {
1047
+ "content": "<|endoftext|>",
1048
+ "lstrip": false,
1049
+ "normalized": false,
1050
+ "rstrip": false,
1051
+ "single_word": false
1052
+ }
1053
+ }
tokenizer_config.json ADDED
The diff for this file is too large to render. See raw diff
 
trainer_state.json ADDED
The diff for this file is too large to render. See raw diff
 
training_args.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:995b5f0a2fe72453ddc8ce97e1a93747554ec3ec0ac92d86e82a57050db51b85
3
+ size 7288
vocab.json ADDED
The diff for this file is too large to render. See raw diff