Upload 15 files
Browse files- .gitattributes +1 -0
- added_tokens.json +47 -0
- chat_template.json +3 -0
- config.json +69 -0
- configuration_dots.py +76 -0
- generation_config.json +8 -0
- merges.txt +0 -0
- model.safetensors +3 -0
- modeling_dots_ocr.py +131 -0
- modeling_dots_ocr_vllm.py +429 -0
- modeling_dots_vision.py +404 -0
- preprocessor_config.json +29 -0
- special_tokens_map.json +31 -0
- tokenizer.json +3 -0
- tokenizer_config.json +393 -0
- vocab.json +0 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
added_tokens.json
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"</tool_call>": 151658,
|
3 |
+
"<tool_call>": 151657,
|
4 |
+
"<|assistant|>": 151672,
|
5 |
+
"<|box_end|>": 151649,
|
6 |
+
"<|box_start|>": 151648,
|
7 |
+
"<|endofassistant|>": 151673,
|
8 |
+
"<|endofimg|>": 151667,
|
9 |
+
"<|endofslice|>": 151682,
|
10 |
+
"<|endofsystemprompt|>": 151669,
|
11 |
+
"<|endoftext|>": 151643,
|
12 |
+
"<|endofuser|>": 151671,
|
13 |
+
"<|file_sep|>": 151664,
|
14 |
+
"<|fim_middle|>": 151660,
|
15 |
+
"<|fim_pad|>": 151662,
|
16 |
+
"<|fim_prefix|>": 151659,
|
17 |
+
"<|fim_suffix|>": 151661,
|
18 |
+
"<|im_end|>": 151645,
|
19 |
+
"<|im_start|>": 151644,
|
20 |
+
"<|image_gen_end|>": 151687,
|
21 |
+
"<|image_gen_start|>": 151686,
|
22 |
+
"<|image_pad|>": 151655,
|
23 |
+
"<|imgpad|>": 151665,
|
24 |
+
"<|imgrowend|>": 151683,
|
25 |
+
"<|img|>": 151666,
|
26 |
+
"<|object_ref_end|>": 151647,
|
27 |
+
"<|object_ref_start|>": 151646,
|
28 |
+
"<|pictotext|>": 151679,
|
29 |
+
"<|pic|>": 151677,
|
30 |
+
"<|polygon_end|>": 151685,
|
31 |
+
"<|polygon_start|>": 151684,
|
32 |
+
"<|quad_end|>": 151651,
|
33 |
+
"<|quad_start|>": 151650,
|
34 |
+
"<|ref_end|>": 151675,
|
35 |
+
"<|ref_start|>": 151674,
|
36 |
+
"<|repo_name|>": 151663,
|
37 |
+
"<|slice|>": 151681,
|
38 |
+
"<|systemprompt|>": 151668,
|
39 |
+
"<|text|>": 151678,
|
40 |
+
"<|user|>": 151670,
|
41 |
+
"<|video_pad|>": 151656,
|
42 |
+
"<|vision_end|>": 151653,
|
43 |
+
"<|vision_pad|>": 151654,
|
44 |
+
"<|vision_start|>": 151652,
|
45 |
+
"[PAD]": 151680,
|
46 |
+
"[SEP]": 151676
|
47 |
+
}
|
chat_template.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"chat_template": "{% set image_count = namespace(value=0) %}{% set video_count = namespace(value=0) %}{%- for m in messages %}{%- if m.role == 'system' %}{{- '<|system|>' + m.content + '<|endofsystem|>\n' }}{%- elif m.role == 'user' %}{% if m.content is string %}{{- '<|user|>' + m.content + '<|endofuser|>' }}{% else %} {% for content in m.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 %}Picture {{ image_count.value }}: {% endif %}<|img|><|imgpad|><|endofimg|>{% 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 %}<|img|><|video_pad|><|endofimg|>{% elif 'text' in content %}{{ content['text'] }}{% endif %}{% endfor %}{%- endif %}{%- elif m.role == 'assistant' %}{{- '<|assistant|>' + m.content }}{%- if not loop.last %}{{- '<|endofassistant|>' }}{%- endif %}{%- endif %}{%- endfor %}{%- if messages[-1].role != 'assistant' %}{{- '<|assistant|>' }}{%- endif %}"
|
3 |
+
}
|
config.json
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": [
|
3 |
+
"DotsOCRForCausalLM"
|
4 |
+
],
|
5 |
+
"attention_bias": true,
|
6 |
+
"attention_dropout": 0.0,
|
7 |
+
"auto_map": {
|
8 |
+
"AutoConfig": "configuration_dots.DotsOCRConfig",
|
9 |
+
"AutoModelForCausalLM": "modeling_dots_ocr.DotsOCRForCausalLM"
|
10 |
+
},
|
11 |
+
"hidden_act": "silu",
|
12 |
+
"hidden_size": 1536,
|
13 |
+
"image_token_id": 151665,
|
14 |
+
"initializer_range": 0.02,
|
15 |
+
"intermediate_size": 8960,
|
16 |
+
"max_position_embeddings": 131072,
|
17 |
+
"max_window_layers": 28,
|
18 |
+
"model_type": "dots_ocr",
|
19 |
+
"num_attention_heads": 12,
|
20 |
+
"num_hidden_layers": 28,
|
21 |
+
"num_key_value_heads": 2,
|
22 |
+
"quantization_config": {
|
23 |
+
"_load_in_4bit": true,
|
24 |
+
"_load_in_8bit": false,
|
25 |
+
"bnb_4bit_compute_dtype": "bfloat16",
|
26 |
+
"bnb_4bit_quant_storage": "uint8",
|
27 |
+
"bnb_4bit_quant_type": "nf4",
|
28 |
+
"bnb_4bit_use_double_quant": true,
|
29 |
+
"llm_int8_enable_fp32_cpu_offload": false,
|
30 |
+
"llm_int8_has_fp16_weight": false,
|
31 |
+
"llm_int8_skip_modules": null,
|
32 |
+
"llm_int8_threshold": 6.0,
|
33 |
+
"load_in_4bit": true,
|
34 |
+
"load_in_8bit": false,
|
35 |
+
"quant_method": "bitsandbytes"
|
36 |
+
},
|
37 |
+
"rms_norm_eps": 1e-06,
|
38 |
+
"rope_scaling": null,
|
39 |
+
"rope_theta": 1000000,
|
40 |
+
"sliding_window": 131072,
|
41 |
+
"tie_word_embeddings": false,
|
42 |
+
"torch_dtype": "float16",
|
43 |
+
"transformers_version": "4.51.3",
|
44 |
+
"use_cache": true,
|
45 |
+
"use_sliding_window": false,
|
46 |
+
"video_token_id": 151656,
|
47 |
+
"vision_config": {
|
48 |
+
"_attn_implementation_autoset": true,
|
49 |
+
"attn_implementation": "flash_attention_2",
|
50 |
+
"embed_dim": 1536,
|
51 |
+
"gradient_checkpointing": false,
|
52 |
+
"hidden_size": 1536,
|
53 |
+
"init_merger_std": 0.02,
|
54 |
+
"initializer_range": 0.02,
|
55 |
+
"intermediate_size": 4224,
|
56 |
+
"is_causal": false,
|
57 |
+
"model_type": "dots_vit",
|
58 |
+
"num_attention_heads": 12,
|
59 |
+
"num_channels": 3,
|
60 |
+
"num_hidden_layers": 42,
|
61 |
+
"patch_size": 14,
|
62 |
+
"post_norm": true,
|
63 |
+
"rms_norm_eps": 1e-05,
|
64 |
+
"spatial_merge_size": 2,
|
65 |
+
"temporal_patch_size": 1,
|
66 |
+
"use_bias": false
|
67 |
+
},
|
68 |
+
"vocab_size": 151936
|
69 |
+
}
|
configuration_dots.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any, Optional
|
2 |
+
from transformers.configuration_utils import PretrainedConfig
|
3 |
+
from transformers.models.qwen2 import Qwen2Config
|
4 |
+
from transformers import Qwen2_5_VLProcessor, AutoProcessor
|
5 |
+
from transformers.models.auto.configuration_auto import CONFIG_MAPPING
|
6 |
+
|
7 |
+
|
8 |
+
class DotsVisionConfig(PretrainedConfig):
|
9 |
+
model_type: str = "dots_vit"
|
10 |
+
|
11 |
+
def __init__(
|
12 |
+
self,
|
13 |
+
embed_dim: int = 1536, # vision encoder embed size
|
14 |
+
hidden_size: int = 1536, # after merger hidden size
|
15 |
+
intermediate_size: int = 4224,
|
16 |
+
num_hidden_layers: int = 42,
|
17 |
+
num_attention_heads: int = 12,
|
18 |
+
num_channels: int = 3,
|
19 |
+
patch_size: int = 14,
|
20 |
+
spatial_merge_size: int = 2,
|
21 |
+
temporal_patch_size: int = 1,
|
22 |
+
rms_norm_eps: float = 1e-5,
|
23 |
+
use_bias: bool = False,
|
24 |
+
attn_implementation="flash_attention_2", # "eager","sdpa","flash_attention_2"
|
25 |
+
initializer_range=0.02,
|
26 |
+
init_merger_std=0.02,
|
27 |
+
is_causal=False, # ve causal forward
|
28 |
+
post_norm=True,
|
29 |
+
gradient_checkpointing=False,
|
30 |
+
**kwargs: Any,
|
31 |
+
):
|
32 |
+
super().__init__(**kwargs)
|
33 |
+
self.embed_dim = embed_dim
|
34 |
+
self.hidden_size = hidden_size
|
35 |
+
self.intermediate_size = intermediate_size
|
36 |
+
self.num_hidden_layers = num_hidden_layers
|
37 |
+
self.num_attention_heads = num_attention_heads
|
38 |
+
self.num_channels = num_channels
|
39 |
+
self.patch_size = patch_size
|
40 |
+
self.spatial_merge_size = spatial_merge_size
|
41 |
+
self.temporal_patch_size = temporal_patch_size
|
42 |
+
self.rms_norm_eps = rms_norm_eps
|
43 |
+
self.use_bias = use_bias
|
44 |
+
self.attn_implementation = attn_implementation
|
45 |
+
self.initializer_range = initializer_range
|
46 |
+
self.init_merger_std = init_merger_std
|
47 |
+
self.is_causal = is_causal
|
48 |
+
self.post_norm = post_norm
|
49 |
+
self.gradient_checkpointing = gradient_checkpointing
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
class DotsOCRConfig(Qwen2Config):
|
54 |
+
model_type = "dots_ocr"
|
55 |
+
def __init__(self,
|
56 |
+
image_token_id = 151665,
|
57 |
+
video_token_id = 151656,
|
58 |
+
vision_config: Optional[dict] = None, *args, **kwargs):
|
59 |
+
super().__init__(*args, **kwargs)
|
60 |
+
self.image_token_id = image_token_id
|
61 |
+
self.video_token_id = video_token_id
|
62 |
+
self.vision_config = DotsVisionConfig(**(vision_config or {}))
|
63 |
+
|
64 |
+
def save_pretrained(self, save_directory, **kwargs):
|
65 |
+
self._auto_class = None
|
66 |
+
super().save_pretrained(save_directory, **kwargs)
|
67 |
+
|
68 |
+
|
69 |
+
class DotsVLProcessor(Qwen2_5_VLProcessor):
|
70 |
+
def __init__(self, image_processor=None, tokenizer=None, chat_template=None, **kwargs):
|
71 |
+
super().__init__(image_processor, tokenizer, chat_template=chat_template)
|
72 |
+
self.image_token = "<|imgpad|>" if not hasattr(tokenizer, "image_token") else tokenizer.image_token
|
73 |
+
|
74 |
+
|
75 |
+
AutoProcessor.register("dots_ocr", DotsVLProcessor)
|
76 |
+
CONFIG_MAPPING.register("dots_ocr", DotsOCRConfig)
|
generation_config.json
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"eos_token_id": [
|
3 |
+
151643,
|
4 |
+
151673
|
5 |
+
],
|
6 |
+
"max_length": 32768,
|
7 |
+
"transformers_version": "4.51.3"
|
8 |
+
}
|
merges.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
model.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:71df47a6a3dfc89048d6f4998a7b1585b5bf1d65423f7883590ce42935481ca7
|
3 |
+
size 2263132669
|
modeling_dots_ocr.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List, Optional, Tuple, Union
|
2 |
+
|
3 |
+
import torch
|
4 |
+
from transformers.modeling_outputs import CausalLMOutputWithPast
|
5 |
+
from transformers.models.qwen2 import Qwen2ForCausalLM
|
6 |
+
|
7 |
+
from .configuration_dots import DotsVisionConfig, DotsOCRConfig
|
8 |
+
from .modeling_dots_vision import DotsVisionTransformer
|
9 |
+
|
10 |
+
|
11 |
+
DOTS_VLM_MAX_IMAGES = 200
|
12 |
+
|
13 |
+
|
14 |
+
class DotsOCRForCausalLM(Qwen2ForCausalLM):
|
15 |
+
config_class = DotsOCRConfig
|
16 |
+
|
17 |
+
def __init__(self, config: DotsOCRConfig):
|
18 |
+
super().__init__(config)
|
19 |
+
|
20 |
+
if isinstance(self.config.vision_config, dict):
|
21 |
+
vision_config = DotsVisionConfig(**self.config.vision_config)
|
22 |
+
self.config.vision_config = vision_config
|
23 |
+
else:
|
24 |
+
vision_config = self.config.vision_config
|
25 |
+
|
26 |
+
self.vision_tower = DotsVisionTransformer(vision_config)
|
27 |
+
|
28 |
+
def prepare_inputs_embeds(
|
29 |
+
self,
|
30 |
+
input_ids: torch.LongTensor,
|
31 |
+
pixel_values: Optional[torch.FloatTensor] = None,
|
32 |
+
grid_thw: Optional[torch.FloatTensor] = None,
|
33 |
+
img_mask: Optional[torch.BoolTensor] = None,
|
34 |
+
) -> torch.Tensor:
|
35 |
+
inputs_embeds = self.get_input_embeddings()(input_ids)
|
36 |
+
|
37 |
+
if pixel_values is not None:
|
38 |
+
assert img_mask is not None
|
39 |
+
if grid_thw.shape[0] > DOTS_VLM_MAX_IMAGES:
|
40 |
+
print(
|
41 |
+
f"Num image exceeded: {grid_thw.shape[0]} > {DOTS_VLM_MAX_IMAGES}, which may cause FSDP hang"
|
42 |
+
)
|
43 |
+
|
44 |
+
vision_embeddings = self.vision_tower(pixel_values, grid_thw)
|
45 |
+
|
46 |
+
true_indices = torch.nonzero(img_mask).squeeze()
|
47 |
+
if len(true_indices) > vision_embeddings.size(0):
|
48 |
+
print(
|
49 |
+
f"img_mask sum > VE and will be truncated, mask.sum()={len(true_indices)} {vision_embeddings.size(0)=}"
|
50 |
+
)
|
51 |
+
true_indices = true_indices[: vision_embeddings.size(0)]
|
52 |
+
new_img_mask = torch.zeros_like(img_mask, device=img_mask.device)
|
53 |
+
new_img_mask[true_indices[:, 0], true_indices[:, 1]] = True
|
54 |
+
else:
|
55 |
+
new_img_mask = img_mask
|
56 |
+
|
57 |
+
assert (
|
58 |
+
vision_embeddings.size(0) == new_img_mask.sum()
|
59 |
+
), f"{vision_embeddings.size(0)=}, {new_img_mask.sum()=}"
|
60 |
+
|
61 |
+
inputs_embeds = inputs_embeds.masked_scatter(
|
62 |
+
new_img_mask.to(inputs_embeds.device).unsqueeze(-1).expand_as(inputs_embeds),
|
63 |
+
vision_embeddings.to(inputs_embeds.device).type(inputs_embeds.dtype),
|
64 |
+
)
|
65 |
+
|
66 |
+
return inputs_embeds
|
67 |
+
|
68 |
+
def forward(
|
69 |
+
self,
|
70 |
+
input_ids: torch.LongTensor,
|
71 |
+
pixel_values: Optional[torch.FloatTensor] = None,
|
72 |
+
image_grid_thw: Optional[torch.FloatTensor] = None,
|
73 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
74 |
+
attention_mask: Optional[torch.Tensor] = None,
|
75 |
+
position_ids: Optional[torch.LongTensor] = None,
|
76 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
77 |
+
labels: Optional[torch.LongTensor] = None,
|
78 |
+
output_attentions: Optional[bool] = None,
|
79 |
+
output_hidden_states: Optional[bool] = None,
|
80 |
+
return_dict: Optional[bool] = None,
|
81 |
+
use_cache: Optional[bool] = None,
|
82 |
+
logits_to_keep: int = 0,
|
83 |
+
**loss_kwargs,
|
84 |
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
85 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
86 |
+
assert len(input_ids) >= 1, f"empty input_ids {input_ids.shape=} will cause gradnorm nan"
|
87 |
+
if inputs_embeds is None:
|
88 |
+
img_mask = input_ids == self.config.image_token_id
|
89 |
+
inputs_embeds = self.prepare_inputs_embeds(input_ids, pixel_values, image_grid_thw, img_mask)
|
90 |
+
|
91 |
+
outputs = super().forward(
|
92 |
+
inputs_embeds=inputs_embeds,
|
93 |
+
attention_mask=attention_mask,
|
94 |
+
position_ids=position_ids,
|
95 |
+
past_key_values=past_key_values,
|
96 |
+
labels=labels,
|
97 |
+
use_cache=use_cache if use_cache is not None else self.config.use_cache,
|
98 |
+
output_attentions=output_attentions,
|
99 |
+
output_hidden_states=output_hidden_states,
|
100 |
+
# return_dict=return_dict,
|
101 |
+
logits_to_keep=logits_to_keep,
|
102 |
+
**loss_kwargs,
|
103 |
+
)
|
104 |
+
|
105 |
+
return outputs
|
106 |
+
|
107 |
+
def prepare_inputs_for_generation(
|
108 |
+
self,
|
109 |
+
input_ids,
|
110 |
+
past_key_values=None,
|
111 |
+
inputs_embeds=None,
|
112 |
+
pixel_values=None,
|
113 |
+
attention_mask=None,
|
114 |
+
cache_position=None,
|
115 |
+
num_logits_to_keep=None,
|
116 |
+
**kwargs,
|
117 |
+
):
|
118 |
+
model_inputs = super().prepare_inputs_for_generation(
|
119 |
+
input_ids,
|
120 |
+
past_key_values=past_key_values,
|
121 |
+
inputs_embeds=inputs_embeds,
|
122 |
+
attention_mask=attention_mask,
|
123 |
+
cache_position=cache_position,
|
124 |
+
num_logits_to_keep=num_logits_to_keep,
|
125 |
+
**kwargs,
|
126 |
+
)
|
127 |
+
|
128 |
+
if cache_position[0] == 0:
|
129 |
+
model_inputs["pixel_values"] = pixel_values
|
130 |
+
|
131 |
+
return model_inputs
|
modeling_dots_ocr_vllm.py
ADDED
@@ -0,0 +1,429 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from functools import cached_property
|
2 |
+
from typing import Iterable, Literal, Mapping, Optional, Set, Tuple, TypedDict, Union
|
3 |
+
|
4 |
+
import torch
|
5 |
+
import torch.nn as nn
|
6 |
+
from transformers.models.qwen2_vl import Qwen2VLImageProcessor, Qwen2VLProcessor
|
7 |
+
from transformers.models.qwen2_vl.image_processing_qwen2_vl import smart_resize
|
8 |
+
from vllm import ModelRegistry
|
9 |
+
from vllm.config import VllmConfig
|
10 |
+
from vllm.model_executor.layers.sampler import SamplerOutput, get_sampler
|
11 |
+
from vllm.model_executor.models.interfaces import MultiModalEmbeddings, SupportsMultiModal
|
12 |
+
from vllm.model_executor.models.qwen2 import Qwen2ForCausalLM
|
13 |
+
from vllm.model_executor.models.qwen2_5_vl import (
|
14 |
+
Qwen2_5_VLMultiModalProcessor,
|
15 |
+
Qwen2_5_VLProcessingInfo,
|
16 |
+
)
|
17 |
+
from vllm.model_executor.models.qwen2_vl import Qwen2VLDummyInputsBuilder
|
18 |
+
from vllm.model_executor.models.utils import (
|
19 |
+
AutoWeightsLoader,
|
20 |
+
WeightsMapper,
|
21 |
+
init_vllm_registered_model,
|
22 |
+
maybe_prefix,
|
23 |
+
merge_multimodal_embeddings,
|
24 |
+
)
|
25 |
+
from vllm.model_executor.sampling_metadata import SamplingMetadata
|
26 |
+
from vllm.multimodal import MULTIMODAL_REGISTRY
|
27 |
+
from vllm.multimodal.inputs import MultiModalDataDict
|
28 |
+
from vllm.multimodal.parse import ImageSize
|
29 |
+
from vllm.sequence import IntermediateTensors
|
30 |
+
|
31 |
+
from .configuration_dots import DotsVisionConfig
|
32 |
+
from .configuration_dots import DotsOCRConfig
|
33 |
+
from .modeling_dots_vision import DotsVisionTransformer
|
34 |
+
|
35 |
+
|
36 |
+
class DotsOCRImagePixelInputs(TypedDict):
|
37 |
+
type: Literal["pixel_values", "image_grid_thw"]
|
38 |
+
|
39 |
+
pixel_values: torch.Tensor
|
40 |
+
image_grid_thw: torch.Tensor
|
41 |
+
|
42 |
+
|
43 |
+
class DotsOCRImageEmbeddingInputs(TypedDict):
|
44 |
+
type: Literal["image_embeds", "image_grid_thw"]
|
45 |
+
image_embeds: torch.Tensor
|
46 |
+
"""Supported types:
|
47 |
+
- List[`torch.Tensor`]: A list of tensors holding all images' features.
|
48 |
+
Each tensor holds an image's features.
|
49 |
+
- `torch.Tensor`: A tensor holding all images' features
|
50 |
+
(concatenation of all images' feature tensors).
|
51 |
+
|
52 |
+
Tensor shape: `(num_image_features, hidden_size)`
|
53 |
+
- `num_image_features` varies based on
|
54 |
+
the number and resolution of the images.
|
55 |
+
- `hidden_size` must match the hidden size of language model backbone.
|
56 |
+
"""
|
57 |
+
|
58 |
+
image_grid_thw: torch.Tensor
|
59 |
+
|
60 |
+
|
61 |
+
DotsOCRImageInputs = Union[DotsOCRImagePixelInputs, DotsOCRImageEmbeddingInputs]
|
62 |
+
|
63 |
+
|
64 |
+
class DotsOCRMultiModalProcessor(Qwen2_5_VLMultiModalProcessor):
|
65 |
+
pass
|
66 |
+
|
67 |
+
|
68 |
+
class DotsOCRDummyInputsBuilder(Qwen2VLDummyInputsBuilder):
|
69 |
+
def get_dummy_mm_data(
|
70 |
+
self,
|
71 |
+
seq_len: int,
|
72 |
+
mm_counts: Mapping[str, int],
|
73 |
+
) -> MultiModalDataDict:
|
74 |
+
num_images = mm_counts.get("image", 0)
|
75 |
+
|
76 |
+
target_width, target_height = self.info.get_image_size_with_most_features()
|
77 |
+
|
78 |
+
return {
|
79 |
+
"image": self._get_dummy_images(width=target_width, height=target_height, num_images=num_images),
|
80 |
+
}
|
81 |
+
|
82 |
+
|
83 |
+
class DotsOCRProcessingInfo(Qwen2_5_VLProcessingInfo):
|
84 |
+
def get_hf_config(self) -> DotsOCRConfig:
|
85 |
+
config = self.ctx.get_hf_config()
|
86 |
+
if not config.__class__.__name__ == 'DotsOCRConfig':
|
87 |
+
raise TypeError(f"Expected DotsOCRConfig, got {type(config)}")
|
88 |
+
|
89 |
+
if hasattr(config, "vision_config") and isinstance(config.vision_config, dict):
|
90 |
+
config.vision_config = DotsVisionConfig(**config.vision_config)
|
91 |
+
|
92 |
+
return config
|
93 |
+
|
94 |
+
def get_hf_processor(
|
95 |
+
self,
|
96 |
+
*,
|
97 |
+
min_pixels: Optional[int] = None,
|
98 |
+
max_pixels: Optional[int] = None,
|
99 |
+
size: Optional[dict[str, int]] = None,
|
100 |
+
**kwargs: object,
|
101 |
+
) -> Qwen2VLProcessor:
|
102 |
+
processor = self.ctx.get_hf_processor(
|
103 |
+
Qwen2VLProcessor,
|
104 |
+
image_processor=self.get_image_processor(min_pixels=min_pixels, max_pixels=max_pixels, size=size),
|
105 |
+
**kwargs,
|
106 |
+
)
|
107 |
+
processor.image_token = "<|imgpad|>"
|
108 |
+
processor.video_token = "<|video_pad|>"
|
109 |
+
return processor
|
110 |
+
|
111 |
+
def _get_vision_info(
|
112 |
+
self,
|
113 |
+
*,
|
114 |
+
image_width: int,
|
115 |
+
image_height: int,
|
116 |
+
num_frames: int = 1,
|
117 |
+
do_resize: bool = True,
|
118 |
+
image_processor: Optional[Qwen2VLImageProcessor],
|
119 |
+
) -> tuple[ImageSize, int]:
|
120 |
+
if image_processor is None:
|
121 |
+
image_processor = self.get_image_processor()
|
122 |
+
|
123 |
+
hf_config: DotsOCRConfig = self.get_hf_config()
|
124 |
+
vision_config = hf_config.vision_config
|
125 |
+
patch_size = vision_config.patch_size
|
126 |
+
merge_size = vision_config.spatial_merge_size
|
127 |
+
temporal_patch_size = vision_config.temporal_patch_size
|
128 |
+
|
129 |
+
if do_resize:
|
130 |
+
resized_height, resized_width = smart_resize(
|
131 |
+
height=image_height,
|
132 |
+
width=image_width,
|
133 |
+
factor=patch_size * merge_size,
|
134 |
+
min_pixels=image_processor.min_pixels,
|
135 |
+
max_pixels=image_processor.max_pixels,
|
136 |
+
)
|
137 |
+
preprocessed_size = ImageSize(width=resized_width, height=resized_height)
|
138 |
+
else:
|
139 |
+
preprocessed_size = ImageSize(width=image_width, height=image_height)
|
140 |
+
|
141 |
+
# NOTE: Frames are padded to be divisible by `temporal_patch_size`
|
142 |
+
# https://github.com/huggingface/transformers/blob/v4.48.3/src/transformers/models/qwen2_vl/image_processing_qwen2_vl.py#L294
|
143 |
+
padded_num_frames = num_frames + num_frames % temporal_patch_size
|
144 |
+
|
145 |
+
grid_t = max(padded_num_frames // temporal_patch_size, 1)
|
146 |
+
grid_h = preprocessed_size.height // patch_size
|
147 |
+
grid_w = preprocessed_size.width // patch_size
|
148 |
+
|
149 |
+
num_patches = grid_t * grid_h * grid_w
|
150 |
+
num_vision_tokens = num_patches // (merge_size**2)
|
151 |
+
|
152 |
+
return preprocessed_size, num_vision_tokens
|
153 |
+
|
154 |
+
|
155 |
+
@MULTIMODAL_REGISTRY.register_processor(
|
156 |
+
Qwen2_5_VLMultiModalProcessor,
|
157 |
+
info=DotsOCRProcessingInfo,
|
158 |
+
dummy_inputs=DotsOCRDummyInputsBuilder,
|
159 |
+
)
|
160 |
+
class DotsOCRForCausalLM(nn.Module, SupportsMultiModal):
|
161 |
+
hf_to_vllm_mapper = WeightsMapper(
|
162 |
+
orig_to_new_prefix={
|
163 |
+
"lm_head.": "language_model.lm_head.",
|
164 |
+
"model.": "language_model.model.",
|
165 |
+
}
|
166 |
+
)
|
167 |
+
_tp_plan = {}
|
168 |
+
|
169 |
+
def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
|
170 |
+
super().__init__()
|
171 |
+
|
172 |
+
self.config: DotsOCRConfig = vllm_config.model_config.hf_config
|
173 |
+
self.quant_config = vllm_config.quant_config
|
174 |
+
self.multimodal_config = vllm_config.model_config.multimodal_config
|
175 |
+
|
176 |
+
if isinstance(self.config.vision_config, dict):
|
177 |
+
vision_config = DotsVisionConfig(**self.config.vision_config)
|
178 |
+
self.config.vision_config = vision_config
|
179 |
+
else:
|
180 |
+
vision_config = self.config.vision_config
|
181 |
+
|
182 |
+
self.vision_tower = DotsVisionTransformer(vision_config)
|
183 |
+
self.language_model: Qwen2ForCausalLM = init_vllm_registered_model(
|
184 |
+
vllm_config=vllm_config,
|
185 |
+
hf_config=self.config,
|
186 |
+
prefix=maybe_prefix(prefix, "language_model"),
|
187 |
+
architectures=["Qwen2ForCausalLM"],
|
188 |
+
)
|
189 |
+
|
190 |
+
@cached_property
|
191 |
+
def sampler(self):
|
192 |
+
if hasattr(self.language_model, "sampler"):
|
193 |
+
return self.language_model.sampler
|
194 |
+
|
195 |
+
return get_sampler()
|
196 |
+
|
197 |
+
def _validate_and_reshape_mm_tensor(self, mm_input: object, name: str) -> torch.Tensor:
|
198 |
+
if not isinstance(mm_input, (torch.Tensor, list)):
|
199 |
+
raise ValueError(f"Incorrect type of {name}. " f"Got type: {type(mm_input)}")
|
200 |
+
if isinstance(mm_input, torch.Tensor):
|
201 |
+
if mm_input.ndim == 2:
|
202 |
+
return mm_input
|
203 |
+
if mm_input.ndim != 3:
|
204 |
+
raise ValueError(
|
205 |
+
f"{name} should be 2D or batched 3D tensor. "
|
206 |
+
f"Got ndim: {mm_input.ndim} "
|
207 |
+
f"(shape={mm_input.shape})"
|
208 |
+
)
|
209 |
+
return torch.concat(list(mm_input))
|
210 |
+
else:
|
211 |
+
return torch.concat(mm_input)
|
212 |
+
|
213 |
+
def _parse_and_validate_image_input(self, **kwargs: object) -> Optional[DotsOCRImageInputs]:
|
214 |
+
pixel_values = kwargs.pop("pixel_values", None)
|
215 |
+
image_embeds = kwargs.pop("image_embeds", None)
|
216 |
+
image_grid_thw = kwargs.pop("image_grid_thw", None)
|
217 |
+
|
218 |
+
if pixel_values is None and image_embeds is None:
|
219 |
+
return None
|
220 |
+
|
221 |
+
if pixel_values is not None:
|
222 |
+
pixel_values = self._validate_and_reshape_mm_tensor(pixel_values, "image pixel values")
|
223 |
+
image_grid_thw = self._validate_and_reshape_mm_tensor(image_grid_thw, "image grid_thw")
|
224 |
+
|
225 |
+
if not isinstance(pixel_values, (torch.Tensor, list)):
|
226 |
+
raise ValueError("Incorrect type of image pixel values. " f"Got type: {type(pixel_values)}")
|
227 |
+
|
228 |
+
return DotsOCRImagePixelInputs(
|
229 |
+
type="pixel_values", pixel_values=pixel_values, image_grid_thw=image_grid_thw
|
230 |
+
)
|
231 |
+
|
232 |
+
if image_embeds is not None:
|
233 |
+
image_embeds = self._validate_and_reshape_mm_tensor(image_embeds, "image embeds")
|
234 |
+
image_grid_thw = self._validate_and_reshape_mm_tensor(image_grid_thw, "image grid_thw")
|
235 |
+
|
236 |
+
if not isinstance(image_embeds, torch.Tensor):
|
237 |
+
raise ValueError("Incorrect type of image embeddings. " f"Got type: {type(image_embeds)}")
|
238 |
+
return DotsOCRImageEmbeddingInputs(
|
239 |
+
type="image_embeds", image_embeds=image_embeds, image_grid_thw=image_grid_thw
|
240 |
+
)
|
241 |
+
|
242 |
+
def vision_forward(self, pixel_values: torch.Tensor, image_grid_thw: torch.Tensor):
|
243 |
+
from vllm.distributed import (
|
244 |
+
get_tensor_model_parallel_group,
|
245 |
+
get_tensor_model_parallel_rank,
|
246 |
+
get_tensor_model_parallel_world_size,
|
247 |
+
)
|
248 |
+
|
249 |
+
assert self.vision_tower is not None
|
250 |
+
|
251 |
+
tp_rank = get_tensor_model_parallel_rank()
|
252 |
+
tp = get_tensor_model_parallel_world_size()
|
253 |
+
|
254 |
+
image_grid_thw_chunk = image_grid_thw.chunk(tp)
|
255 |
+
image_sizes_consum = torch.tensor([i.prod(-1).sum() for i in image_grid_thw_chunk]).cumsum(dim=0)
|
256 |
+
merge_size_square = self.vision_tower.config.spatial_merge_size**2
|
257 |
+
image_embedding = torch.zeros(
|
258 |
+
(
|
259 |
+
pixel_values.shape[0] // merge_size_square,
|
260 |
+
self.vision_tower.config.hidden_size,
|
261 |
+
),
|
262 |
+
device=pixel_values.device,
|
263 |
+
dtype=pixel_values.dtype,
|
264 |
+
)
|
265 |
+
|
266 |
+
if tp_rank < len(image_sizes_consum):
|
267 |
+
idx_start = 0 if tp_rank == 0 else image_sizes_consum[tp_rank - 1].item()
|
268 |
+
idx_end = image_sizes_consum[tp_rank].item()
|
269 |
+
pixel_values_part = pixel_values[idx_start:idx_end]
|
270 |
+
image_grid_thw_part = image_grid_thw_chunk[tp_rank]
|
271 |
+
image_embedding_part = self.vision_tower(pixel_values_part, image_grid_thw_part)
|
272 |
+
image_embedding[idx_start // merge_size_square : idx_end // merge_size_square] = image_embedding_part
|
273 |
+
|
274 |
+
group = get_tensor_model_parallel_group().device_group
|
275 |
+
torch.distributed.all_reduce(image_embedding, group=group)
|
276 |
+
return image_embedding
|
277 |
+
|
278 |
+
def _process_image_input(self, image_input: DotsOCRImageInputs) -> tuple[torch.Tensor, ...]:
|
279 |
+
grid_thw = image_input["image_grid_thw"]
|
280 |
+
assert grid_thw.ndim == 2
|
281 |
+
|
282 |
+
if image_input["type"] == "image_embeds":
|
283 |
+
image_embeds = image_input["image_embeds"].type(self.vision_tower.dtype)
|
284 |
+
else:
|
285 |
+
pixel_values = image_input["pixel_values"].type(self.vision_tower.dtype)
|
286 |
+
image_embeds = self.vision_forward(pixel_values, grid_thw)[
|
287 |
+
:, : self.config.hidden_size
|
288 |
+
]
|
289 |
+
|
290 |
+
# Split concatenated embeddings for each image item.
|
291 |
+
merge_size = self.vision_tower.config.spatial_merge_size
|
292 |
+
sizes = grid_thw.prod(-1) // merge_size // merge_size
|
293 |
+
|
294 |
+
return image_embeds.split(sizes.tolist())
|
295 |
+
|
296 |
+
def _parse_and_validate_multimodal_inputs(self, **kwargs: object) -> dict:
|
297 |
+
modalities = {}
|
298 |
+
|
299 |
+
# Preserve the order of modalities if there are multiple of them
|
300 |
+
# from the order of kwargs.
|
301 |
+
for input_key in kwargs:
|
302 |
+
if input_key in ("pixel_values", "image_embeds") and "images" not in modalities:
|
303 |
+
modalities["images"] = self._parse_and_validate_image_input(**kwargs)
|
304 |
+
return modalities
|
305 |
+
|
306 |
+
def get_language_model(self) -> torch.nn.Module:
|
307 |
+
return self.language_model
|
308 |
+
|
309 |
+
def get_multimodal_embeddings(self, **kwargs: object) -> Optional[MultiModalEmbeddings]:
|
310 |
+
modalities = self._parse_and_validate_multimodal_inputs(**kwargs)
|
311 |
+
if not modalities:
|
312 |
+
return None
|
313 |
+
|
314 |
+
# The result multimodal_embeddings is tuple of tensors, with each
|
315 |
+
# tensor correspoending to a multimodal data item (image or video).
|
316 |
+
multimodal_embeddings: tuple[torch.Tensor, ...] = ()
|
317 |
+
|
318 |
+
# NOTE: It is important to iterate over the keys in this dictionary
|
319 |
+
# to preserve the order of the modalities.
|
320 |
+
for modality in modalities:
|
321 |
+
if modality == "images":
|
322 |
+
image_input = modalities["images"]
|
323 |
+
vision_embeddings = self._process_image_input(image_input)
|
324 |
+
multimodal_embeddings += vision_embeddings
|
325 |
+
|
326 |
+
return multimodal_embeddings
|
327 |
+
|
328 |
+
def get_input_embeddings(
|
329 |
+
self,
|
330 |
+
input_ids: torch.Tensor,
|
331 |
+
multimodal_embeddings: Optional[MultiModalEmbeddings] = None,
|
332 |
+
) -> torch.Tensor:
|
333 |
+
inputs_embeds = self.language_model.get_input_embeddings(input_ids)
|
334 |
+
if multimodal_embeddings is not None:
|
335 |
+
inputs_embeds = merge_multimodal_embeddings(
|
336 |
+
input_ids,
|
337 |
+
inputs_embeds,
|
338 |
+
multimodal_embeddings,
|
339 |
+
[self.config.image_token_id, self.config.video_token_id],
|
340 |
+
)
|
341 |
+
|
342 |
+
return inputs_embeds
|
343 |
+
|
344 |
+
def get_input_embeddings_v0(
|
345 |
+
self,
|
346 |
+
input_ids: torch.Tensor,
|
347 |
+
image_input: Optional[DotsOCRImagePixelInputs] = None,
|
348 |
+
) -> torch.Tensor:
|
349 |
+
inputs_embeds = self.get_input_embeddings(input_ids)
|
350 |
+
if image_input is not None:
|
351 |
+
image_embeds = self._process_image_input(image_input)
|
352 |
+
inputs_embeds = merge_multimodal_embeddings(
|
353 |
+
input_ids,
|
354 |
+
inputs_embeds,
|
355 |
+
image_embeds,
|
356 |
+
placeholder_token_id=self.config.image_token_id,
|
357 |
+
)
|
358 |
+
return inputs_embeds
|
359 |
+
|
360 |
+
def forward(
|
361 |
+
self,
|
362 |
+
input_ids: Optional[torch.Tensor],
|
363 |
+
positions: torch.Tensor,
|
364 |
+
intermediate_tensors: Optional[IntermediateTensors] = None,
|
365 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
366 |
+
**kwargs,
|
367 |
+
) -> Union[torch.Tensor, IntermediateTensors]:
|
368 |
+
if intermediate_tensors is not None:
|
369 |
+
inputs_embeds = None
|
370 |
+
elif inputs_embeds is None and kwargs.get("pixel_values") is not None:
|
371 |
+
image_input = self._parse_and_validate_image_input(**kwargs)
|
372 |
+
if image_input is None:
|
373 |
+
inputs_embeds = None
|
374 |
+
else:
|
375 |
+
assert input_ids is not None
|
376 |
+
inputs_embeds = self.get_input_embeddings_v0(
|
377 |
+
input_ids,
|
378 |
+
image_input=image_input,
|
379 |
+
)
|
380 |
+
input_ids = None
|
381 |
+
|
382 |
+
hidden_states = self.language_model(
|
383 |
+
input_ids=input_ids,
|
384 |
+
positions=positions,
|
385 |
+
intermediate_tensors=intermediate_tensors,
|
386 |
+
inputs_embeds=inputs_embeds,
|
387 |
+
)
|
388 |
+
|
389 |
+
return hidden_states
|
390 |
+
|
391 |
+
def compute_logits(
|
392 |
+
self,
|
393 |
+
hidden_states: torch.Tensor,
|
394 |
+
sampling_metadata: SamplingMetadata,
|
395 |
+
) -> Optional[torch.Tensor]:
|
396 |
+
return self.language_model.compute_logits(hidden_states, sampling_metadata)
|
397 |
+
|
398 |
+
def sample(
|
399 |
+
self,
|
400 |
+
logits: Optional[torch.Tensor],
|
401 |
+
sampling_metadata: SamplingMetadata,
|
402 |
+
) -> Optional[SamplerOutput]:
|
403 |
+
next_tokens = self.sampler(logits, sampling_metadata)
|
404 |
+
return next_tokens
|
405 |
+
|
406 |
+
def load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]]) -> Set[str]:
|
407 |
+
loader = AutoWeightsLoader(self)
|
408 |
+
return loader.load_weights(weights, mapper=self.hf_to_vllm_mapper)
|
409 |
+
|
410 |
+
|
411 |
+
def patch_vllm_chat_placeholder():
|
412 |
+
from vllm.entrypoints.chat_utils import BaseMultiModalItemTracker
|
413 |
+
|
414 |
+
ori = BaseMultiModalItemTracker._placeholder_str
|
415 |
+
|
416 |
+
def _placeholder_str(self, modality, current_count: int) -> Optional[str]:
|
417 |
+
hf_config = self._model_config.hf_config
|
418 |
+
model_type = hf_config.model_type
|
419 |
+
if modality in ("image",) and model_type in ["dots_ocr"]:
|
420 |
+
return "<|img|><|imgpad|><|endofimg|>"
|
421 |
+
return ori(self, modality, current_count)
|
422 |
+
|
423 |
+
BaseMultiModalItemTracker._placeholder_str = _placeholder_str
|
424 |
+
|
425 |
+
ModelRegistry.register_model(
|
426 |
+
"DotsOCRForCausalLM", DotsOCRForCausalLM,
|
427 |
+
)
|
428 |
+
|
429 |
+
patch_vllm_chat_placeholder()
|
modeling_dots_vision.py
ADDED
@@ -0,0 +1,404 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
|
3 |
+
import torch
|
4 |
+
import torch.nn as nn
|
5 |
+
import torch.nn.functional as F
|
6 |
+
import torch.utils.checkpoint
|
7 |
+
from flash_attn import flash_attn_varlen_func
|
8 |
+
from torch.nn import LayerNorm
|
9 |
+
from transformers.modeling_utils import PreTrainedModel
|
10 |
+
from .configuration_dots import DotsVisionConfig
|
11 |
+
|
12 |
+
|
13 |
+
def rotate_half(x):
|
14 |
+
"""Rotates half the hidden dims of the input."""
|
15 |
+
x1 = x[..., : x.shape[-1] // 2]
|
16 |
+
x2 = x[..., x.shape[-1] // 2 :]
|
17 |
+
return torch.cat((-x2, x1), dim=-1)
|
18 |
+
|
19 |
+
|
20 |
+
def apply_rotary_pos_emb_vision(tensor: torch.Tensor, freqs: torch.Tensor) -> torch.Tensor:
|
21 |
+
orig_dtype = tensor.dtype
|
22 |
+
tensor = tensor.float()
|
23 |
+
|
24 |
+
cos = freqs.cos()
|
25 |
+
sin = freqs.sin()
|
26 |
+
|
27 |
+
cos = cos.unsqueeze(1).repeat(1, 1, 2).unsqueeze(0).float()
|
28 |
+
sin = sin.unsqueeze(1).repeat(1, 1, 2).unsqueeze(0).float()
|
29 |
+
|
30 |
+
output = (tensor * cos) + (rotate_half(tensor) * sin)
|
31 |
+
|
32 |
+
output = output.to(orig_dtype)
|
33 |
+
|
34 |
+
return output
|
35 |
+
|
36 |
+
|
37 |
+
class VisionRotaryEmbedding(nn.Module):
|
38 |
+
def __init__(self, dim: int, theta: float = 10000.0) -> None:
|
39 |
+
super().__init__()
|
40 |
+
inv_freq = 1.0 / (theta ** (torch.arange(0, dim, 2, dtype=torch.float) / dim))
|
41 |
+
self.register_buffer("inv_freq", inv_freq, persistent=False)
|
42 |
+
|
43 |
+
def forward(self, seqlen: int) -> torch.Tensor:
|
44 |
+
seq = torch.arange(seqlen, device=self.inv_freq.device, dtype=self.inv_freq.dtype)
|
45 |
+
freqs = torch.outer(seq, self.inv_freq)
|
46 |
+
return freqs
|
47 |
+
|
48 |
+
|
49 |
+
class PatchMerger(nn.Module):
|
50 |
+
def __init__(
|
51 |
+
self,
|
52 |
+
dim: int,
|
53 |
+
context_dim: int,
|
54 |
+
spatial_merge_size: int = 2,
|
55 |
+
pre_norm="layernorm",
|
56 |
+
init_merger_std=None,
|
57 |
+
) -> None:
|
58 |
+
super().__init__()
|
59 |
+
self.hidden_size = context_dim * (spatial_merge_size**2)
|
60 |
+
self.pre_norm = pre_norm
|
61 |
+
if self.pre_norm == "layernorm":
|
62 |
+
self.ln_q = LayerNorm(context_dim, eps=1e-6)
|
63 |
+
elif self.pre_norm == "rmsnorm":
|
64 |
+
self.ln_q = RMSNorm(context_dim, eps=1e-6)
|
65 |
+
else:
|
66 |
+
print("no norm in patch merger")
|
67 |
+
|
68 |
+
self.mlp = nn.Sequential(
|
69 |
+
nn.Linear(self.hidden_size, self.hidden_size),
|
70 |
+
nn.GELU(),
|
71 |
+
nn.Linear(self.hidden_size, dim),
|
72 |
+
)
|
73 |
+
|
74 |
+
if init_merger_std is not None:
|
75 |
+
nn.init.normal_(self.mlp[0].weight, mean=0.0, std=init_merger_std)
|
76 |
+
nn.init.zeros_(self.mlp[0].bias)
|
77 |
+
nn.init.normal_(self.mlp[2].weight, mean=0.0, std=init_merger_std)
|
78 |
+
nn.init.zeros_(self.mlp[2].bias)
|
79 |
+
|
80 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
81 |
+
if self.pre_norm:
|
82 |
+
x = self.mlp(self.ln_q(x).view(-1, self.hidden_size))
|
83 |
+
else:
|
84 |
+
x = self.mlp(x.view(-1, self.hidden_size))
|
85 |
+
return x
|
86 |
+
|
87 |
+
|
88 |
+
class VisionAttention(nn.Module):
|
89 |
+
def __init__(self, config, dim: int, num_heads: int = 16, bias=True) -> None:
|
90 |
+
super().__init__()
|
91 |
+
self.num_heads = num_heads
|
92 |
+
self.head_dim = dim // num_heads
|
93 |
+
self.qkv = nn.Linear(dim, dim * 3, bias=bias)
|
94 |
+
self.proj = nn.Linear(dim, dim, bias=bias)
|
95 |
+
|
96 |
+
def forward(
|
97 |
+
self,
|
98 |
+
hidden_states: torch.Tensor,
|
99 |
+
cu_seqlens: torch.Tensor,
|
100 |
+
rotary_pos_emb: torch.Tensor = None,
|
101 |
+
) -> torch.Tensor:
|
102 |
+
seq_length = hidden_states.shape[0]
|
103 |
+
|
104 |
+
q, k, v = self.qkv(hidden_states).reshape(seq_length, 3, self.num_heads, -1).permute(1, 0, 2, 3).unbind(0)
|
105 |
+
q = apply_rotary_pos_emb_vision(q.unsqueeze(0), rotary_pos_emb).squeeze(0)
|
106 |
+
k = apply_rotary_pos_emb_vision(k.unsqueeze(0), rotary_pos_emb).squeeze(0)
|
107 |
+
|
108 |
+
attention_mask = torch.full(
|
109 |
+
[1, seq_length, seq_length], torch.finfo(q.dtype).min, device=q.device, dtype=q.dtype
|
110 |
+
)
|
111 |
+
for i in range(1, len(cu_seqlens)):
|
112 |
+
attention_mask[..., cu_seqlens[i - 1] : cu_seqlens[i], cu_seqlens[i - 1] : cu_seqlens[i]] = 0
|
113 |
+
|
114 |
+
q = q.transpose(0, 1)
|
115 |
+
k = k.transpose(0, 1)
|
116 |
+
v = v.transpose(0, 1)
|
117 |
+
attn_weights = torch.matmul(q, k.transpose(1, 2)) / math.sqrt(self.head_dim)
|
118 |
+
attn_weights = attn_weights + attention_mask
|
119 |
+
attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(q.dtype)
|
120 |
+
attn_output = torch.matmul(attn_weights, v)
|
121 |
+
attn_output = attn_output.transpose(0, 1)
|
122 |
+
attn_output = attn_output.reshape(seq_length, -1)
|
123 |
+
attn_output = self.proj(attn_output)
|
124 |
+
return attn_output
|
125 |
+
|
126 |
+
|
127 |
+
class VisionFlashAttention2(nn.Module):
|
128 |
+
def __init__(self, config, dim: int, num_heads: int = 16, bias=True) -> None:
|
129 |
+
super().__init__()
|
130 |
+
self.num_heads = num_heads
|
131 |
+
self.qkv = nn.Linear(dim, dim * 3, bias=bias)
|
132 |
+
self.proj = nn.Linear(dim, dim, bias=bias)
|
133 |
+
self.config = config
|
134 |
+
self.is_causal = config.is_causal
|
135 |
+
|
136 |
+
def forward(
|
137 |
+
self,
|
138 |
+
hidden_states: torch.Tensor,
|
139 |
+
cu_seqlens: torch.Tensor,
|
140 |
+
rotary_pos_emb: torch.Tensor = None,
|
141 |
+
) -> torch.Tensor:
|
142 |
+
seq_length = hidden_states.shape[0]
|
143 |
+
q, k, v = (
|
144 |
+
self.qkv(hidden_states).reshape(seq_length, 3, self.num_heads, -1).permute(1, 0, 2, 3).unbind(0)
|
145 |
+
) # 'shd'
|
146 |
+
q = apply_rotary_pos_emb_vision(q.unsqueeze(0), rotary_pos_emb).squeeze(0)
|
147 |
+
k = apply_rotary_pos_emb_vision(k.unsqueeze(0), rotary_pos_emb).squeeze(0)
|
148 |
+
max_seqlen = (cu_seqlens[1:] - cu_seqlens[:-1]).max().item()
|
149 |
+
attn_output = flash_attn_varlen_func(
|
150 |
+
q, k, v, cu_seqlens, cu_seqlens, max_seqlen, max_seqlen, causal=self.is_causal
|
151 |
+
).reshape(seq_length, -1)
|
152 |
+
attn_output = self.proj(attn_output)
|
153 |
+
|
154 |
+
return attn_output
|
155 |
+
|
156 |
+
|
157 |
+
class VisionSdpaAttention(nn.Module):
|
158 |
+
def __init__(self, config, dim: int, num_heads: int = 16, bias=True) -> None:
|
159 |
+
super().__init__()
|
160 |
+
self.num_heads = num_heads
|
161 |
+
self.qkv = nn.Linear(dim, dim * 3, bias=bias)
|
162 |
+
self.proj = nn.Linear(dim, dim, bias=bias)
|
163 |
+
self.config = config
|
164 |
+
|
165 |
+
def forward(
|
166 |
+
self,
|
167 |
+
hidden_states: torch.Tensor,
|
168 |
+
cu_seqlens: torch.Tensor,
|
169 |
+
rotary_pos_emb: torch.Tensor = None,
|
170 |
+
) -> torch.Tensor:
|
171 |
+
seq_length = hidden_states.shape[0]
|
172 |
+
q, k, v = self.qkv(hidden_states).reshape(seq_length, 3, self.num_heads, -1).permute(1, 0, 2, 3).unbind(0)
|
173 |
+
|
174 |
+
q = apply_rotary_pos_emb_vision(q.unsqueeze(0), rotary_pos_emb).squeeze(0)
|
175 |
+
k = apply_rotary_pos_emb_vision(k.unsqueeze(0), rotary_pos_emb).squeeze(0)
|
176 |
+
|
177 |
+
attention_mask = torch.zeros([1, seq_length, seq_length], device=q.device, dtype=torch.bool)
|
178 |
+
for i in range(1, len(cu_seqlens)):
|
179 |
+
attention_mask[..., cu_seqlens[i - 1] : cu_seqlens[i], cu_seqlens[i - 1] : cu_seqlens[i]] = True
|
180 |
+
|
181 |
+
q = q.transpose(0, 1)
|
182 |
+
k = k.transpose(0, 1)
|
183 |
+
v = v.transpose(0, 1)
|
184 |
+
|
185 |
+
attn_output = F.scaled_dot_product_attention(q, k, v, attention_mask, dropout_p=0.0)
|
186 |
+
attn_output = attn_output.transpose(0, 1)
|
187 |
+
attn_output = attn_output.reshape(seq_length, -1)
|
188 |
+
|
189 |
+
attn_output = self.proj(attn_output)
|
190 |
+
return attn_output
|
191 |
+
|
192 |
+
|
193 |
+
DOTS_VISION_ATTENTION_CLASSES = {
|
194 |
+
"eager": VisionAttention,
|
195 |
+
"flash_attention_2": VisionFlashAttention2,
|
196 |
+
"sdpa": VisionSdpaAttention,
|
197 |
+
}
|
198 |
+
|
199 |
+
|
200 |
+
class RMSNorm(nn.Module):
|
201 |
+
def __init__(self, dim: int, eps: float = 1e-6):
|
202 |
+
super().__init__()
|
203 |
+
self.weight = nn.Parameter(torch.ones(dim))
|
204 |
+
self.eps = eps
|
205 |
+
|
206 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
207 |
+
output = self._norm(x.float()).type_as(x)
|
208 |
+
return output * self.weight
|
209 |
+
|
210 |
+
def extra_repr(self) -> str:
|
211 |
+
return f"{tuple(self.weight.shape)}, eps={self.eps}"
|
212 |
+
|
213 |
+
def _norm(self, x: torch.Tensor) -> torch.Tensor:
|
214 |
+
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
|
215 |
+
|
216 |
+
|
217 |
+
class DotsSwiGLUFFN(nn.Module):
|
218 |
+
def __init__(self, config):
|
219 |
+
super().__init__()
|
220 |
+
hidden_features = config.intermediate_size
|
221 |
+
in_features = config.embed_dim
|
222 |
+
bias = config.use_bias
|
223 |
+
|
224 |
+
self.fc1 = nn.Linear(in_features, hidden_features, bias=bias)
|
225 |
+
self.fc2 = nn.Linear(hidden_features, in_features, bias=bias)
|
226 |
+
self.fc3 = nn.Linear(in_features, hidden_features, bias=bias)
|
227 |
+
|
228 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
229 |
+
x = F.silu(self.fc1(x)) * self.fc3(x)
|
230 |
+
x = self.fc2(x)
|
231 |
+
return x
|
232 |
+
|
233 |
+
|
234 |
+
|
235 |
+
class DotsPatchEmbed(nn.Module):
|
236 |
+
def __init__(self, config):
|
237 |
+
super().__init__()
|
238 |
+
self.num_channels = config.num_channels
|
239 |
+
self.patch_size = config.patch_size
|
240 |
+
self.temporal_patch_size = config.temporal_patch_size
|
241 |
+
self.embed_dim = config.embed_dim
|
242 |
+
self.config = config
|
243 |
+
self.proj = nn.Conv2d(
|
244 |
+
config.num_channels,
|
245 |
+
config.embed_dim,
|
246 |
+
kernel_size=(config.patch_size, config.patch_size),
|
247 |
+
stride=(config.patch_size, config.patch_size),
|
248 |
+
)
|
249 |
+
self.norm = RMSNorm(config.embed_dim, eps=config.rms_norm_eps)
|
250 |
+
|
251 |
+
def forward(self, x: torch.Tensor, grid_thw=None) -> torch.Tensor:
|
252 |
+
x = x.view(-1, self.num_channels, self.temporal_patch_size, self.patch_size, self.patch_size)[:, :, 0]
|
253 |
+
x = self.proj(x).view(-1, self.embed_dim)
|
254 |
+
x = self.norm(x)
|
255 |
+
return x
|
256 |
+
|
257 |
+
|
258 |
+
class DotsViTPreprocessor(nn.Module):
|
259 |
+
def __init__(self, config):
|
260 |
+
super().__init__()
|
261 |
+
self.patch_h = config.patch_size
|
262 |
+
self.patch_w = config.patch_size
|
263 |
+
self.embed_dim = config.embed_dim
|
264 |
+
self.config = config
|
265 |
+
self.patchifier = DotsPatchEmbed(config)
|
266 |
+
|
267 |
+
def forward(self, x: torch.Tensor, grid_thw=None) -> torch.Tensor:
|
268 |
+
tokens = self.patchifier(x, grid_thw)
|
269 |
+
return tokens
|
270 |
+
|
271 |
+
|
272 |
+
class DotsVisionBlock(nn.Module):
|
273 |
+
def __init__(self, config, attn_implementation: str = "flash_attention_2"):
|
274 |
+
super().__init__()
|
275 |
+
self.attn = DOTS_VISION_ATTENTION_CLASSES[attn_implementation](
|
276 |
+
config, config.embed_dim, num_heads=config.num_attention_heads, bias=config.use_bias
|
277 |
+
)
|
278 |
+
self.norm1 = RMSNorm(config.embed_dim, eps=config.rms_norm_eps)
|
279 |
+
self.mlp = DotsSwiGLUFFN(config)
|
280 |
+
self.norm2 = RMSNorm(config.embed_dim, eps=config.rms_norm_eps)
|
281 |
+
|
282 |
+
def forward(self, hidden_states, cu_seqlens, rotary_pos_emb) -> torch.Tensor:
|
283 |
+
hidden_states = hidden_states + self.attn(
|
284 |
+
self.norm1(hidden_states), cu_seqlens=cu_seqlens, rotary_pos_emb=rotary_pos_emb
|
285 |
+
)
|
286 |
+
hidden_states = hidden_states + self.mlp(self.norm2(hidden_states))
|
287 |
+
return hidden_states
|
288 |
+
|
289 |
+
|
290 |
+
class DotsVisionTransformer(PreTrainedModel):
|
291 |
+
def __init__(self, config: DotsVisionConfig) -> None:
|
292 |
+
super().__init__(config)
|
293 |
+
self.config = config
|
294 |
+
self.spatial_merge_size = config.spatial_merge_size
|
295 |
+
|
296 |
+
self.patch_embed = DotsViTPreprocessor(config)
|
297 |
+
self._init_weights(self.patch_embed.patchifier.proj)
|
298 |
+
|
299 |
+
head_dim = config.embed_dim // config.num_attention_heads
|
300 |
+
|
301 |
+
self.rotary_pos_emb = VisionRotaryEmbedding(head_dim // 2)
|
302 |
+
|
303 |
+
_num_hidden_layers = config.num_hidden_layers
|
304 |
+
self.blocks = nn.ModuleList(
|
305 |
+
[DotsVisionBlock(config, config.attn_implementation) for _ in range(_num_hidden_layers)]
|
306 |
+
)
|
307 |
+
|
308 |
+
if self.config.post_norm:
|
309 |
+
self.post_trunk_norm = RMSNorm(config.embed_dim, eps=config.rms_norm_eps)
|
310 |
+
|
311 |
+
self.merger = PatchMerger(
|
312 |
+
dim=config.hidden_size,
|
313 |
+
context_dim=config.embed_dim,
|
314 |
+
spatial_merge_size=config.spatial_merge_size,
|
315 |
+
init_merger_std=self.config.init_merger_std,
|
316 |
+
)
|
317 |
+
|
318 |
+
self.gradient_checkpointing = False
|
319 |
+
self._gradient_checkpointing_func = torch.utils.checkpoint.checkpoint
|
320 |
+
|
321 |
+
def _init_weights(self, module):
|
322 |
+
std = self.config.initializer_range
|
323 |
+
if isinstance(module, (nn.Linear, nn.Conv3d)):
|
324 |
+
module.weight.data.normal_(mean=0.0, std=std)
|
325 |
+
if module.bias is not None:
|
326 |
+
module.bias.data.zero_()
|
327 |
+
elif isinstance(module, nn.Embedding):
|
328 |
+
module.weight.data.normal_(mean=0.0, std=std)
|
329 |
+
if module.padding_idx is not None:
|
330 |
+
module.weight.data[module.padding_idx].zero_()
|
331 |
+
|
332 |
+
@property
|
333 |
+
def dtype(self) -> torch.dtype:
|
334 |
+
return self.blocks[0].mlp.fc2.weight.dtype
|
335 |
+
|
336 |
+
@property
|
337 |
+
def device(self) -> torch.device:
|
338 |
+
return self.blocks[0].mlp.fc2.weight.device
|
339 |
+
|
340 |
+
def get_pos_ids_by_grid(self, grid_thw):
|
341 |
+
pos_ids = []
|
342 |
+
for t, h, w in grid_thw:
|
343 |
+
hpos_ids = torch.arange(h).unsqueeze(1).expand(-1, w)
|
344 |
+
hpos_ids = hpos_ids.reshape(
|
345 |
+
h // self.spatial_merge_size,
|
346 |
+
self.spatial_merge_size,
|
347 |
+
w // self.spatial_merge_size,
|
348 |
+
self.spatial_merge_size,
|
349 |
+
)
|
350 |
+
hpos_ids = hpos_ids.permute(0, 2, 1, 3)
|
351 |
+
hpos_ids = hpos_ids.flatten()
|
352 |
+
|
353 |
+
wpos_ids = torch.arange(w).unsqueeze(0).expand(h, -1)
|
354 |
+
wpos_ids = wpos_ids.reshape(
|
355 |
+
h // self.spatial_merge_size,
|
356 |
+
self.spatial_merge_size,
|
357 |
+
w // self.spatial_merge_size,
|
358 |
+
self.spatial_merge_size,
|
359 |
+
)
|
360 |
+
wpos_ids = wpos_ids.permute(0, 2, 1, 3)
|
361 |
+
wpos_ids = wpos_ids.flatten()
|
362 |
+
pos_ids.append(
|
363 |
+
torch.stack([hpos_ids, wpos_ids], dim=-1).repeat(t, 1)
|
364 |
+
)
|
365 |
+
|
366 |
+
return pos_ids
|
367 |
+
|
368 |
+
def rot_pos_emb(self, grid_thw):
|
369 |
+
pos_ids = self.get_pos_ids_by_grid(grid_thw)
|
370 |
+
pos_ids = torch.cat(pos_ids, dim=0)
|
371 |
+
max_grid_size = grid_thw[:, 1:].max()
|
372 |
+
rotary_pos_emb_full = self.rotary_pos_emb(max_grid_size)
|
373 |
+
rotary_pos_emb = rotary_pos_emb_full[pos_ids].flatten(1)
|
374 |
+
return rotary_pos_emb
|
375 |
+
|
376 |
+
def forward(self, hidden_states: torch.Tensor, grid_thw: torch.Tensor, bf16=True) -> torch.Tensor:
|
377 |
+
if bf16:
|
378 |
+
hidden_states = hidden_states.bfloat16()
|
379 |
+
hidden_states = self.patch_embed(hidden_states, grid_thw)
|
380 |
+
|
381 |
+
rotary_pos_emb = self.rot_pos_emb(grid_thw)
|
382 |
+
|
383 |
+
cu_seqlens = torch.repeat_interleave(grid_thw[:, 1] * grid_thw[:, 2], grid_thw[:, 0]).cumsum(
|
384 |
+
dim=0,
|
385 |
+
dtype=grid_thw.dtype if torch.jit.is_tracing() else torch.int32,
|
386 |
+
)
|
387 |
+
cu_seqlens = F.pad(cu_seqlens, (1, 0), value=0)
|
388 |
+
|
389 |
+
for blk in self.blocks:
|
390 |
+
if self.gradient_checkpointing and self.training:
|
391 |
+
hidden_states = self._gradient_checkpointing_func(
|
392 |
+
blk.__call__,
|
393 |
+
hidden_states,
|
394 |
+
cu_seqlens,
|
395 |
+
rotary_pos_emb,
|
396 |
+
)
|
397 |
+
else:
|
398 |
+
hidden_states = blk(hidden_states, cu_seqlens=cu_seqlens, rotary_pos_emb=rotary_pos_emb)
|
399 |
+
|
400 |
+
if self.config.post_norm:
|
401 |
+
hidden_states = self.post_trunk_norm(hidden_states)
|
402 |
+
|
403 |
+
hidden_states = self.merger(hidden_states)
|
404 |
+
return hidden_states
|
preprocessor_config.json
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"do_convert_rgb": true,
|
3 |
+
"do_normalize": true,
|
4 |
+
"do_rescale": true,
|
5 |
+
"do_resize": true,
|
6 |
+
"image_mean": [
|
7 |
+
0.48145466,
|
8 |
+
0.4578275,
|
9 |
+
0.40821073
|
10 |
+
],
|
11 |
+
"image_processor_type": "Qwen2VLImageProcessor",
|
12 |
+
"image_std": [
|
13 |
+
0.26862954,
|
14 |
+
0.26130258,
|
15 |
+
0.27577711
|
16 |
+
],
|
17 |
+
"max_pixels": 11289600,
|
18 |
+
"merge_size": 2,
|
19 |
+
"min_pixels": 3136,
|
20 |
+
"patch_size": 14,
|
21 |
+
"processor_class": "DotsVLProcessor",
|
22 |
+
"resample": 3,
|
23 |
+
"rescale_factor": 0.00392156862745098,
|
24 |
+
"size": {
|
25 |
+
"longest_edge": 11289600,
|
26 |
+
"shortest_edge": 3136
|
27 |
+
},
|
28 |
+
"temporal_patch_size": 1
|
29 |
+
}
|
special_tokens_map.json
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"additional_special_tokens": [
|
3 |
+
"<|im_start|>",
|
4 |
+
"<|im_end|>",
|
5 |
+
"<|object_ref_start|>",
|
6 |
+
"<|object_ref_end|>",
|
7 |
+
"<|box_start|>",
|
8 |
+
"<|box_end|>",
|
9 |
+
"<|quad_start|>",
|
10 |
+
"<|quad_end|>",
|
11 |
+
"<|vision_start|>",
|
12 |
+
"<|vision_end|>",
|
13 |
+
"<|vision_pad|>",
|
14 |
+
"<|image_pad|>",
|
15 |
+
"<|video_pad|>"
|
16 |
+
],
|
17 |
+
"eos_token": {
|
18 |
+
"content": "<|endoftext|>",
|
19 |
+
"lstrip": false,
|
20 |
+
"normalized": false,
|
21 |
+
"rstrip": false,
|
22 |
+
"single_word": false
|
23 |
+
},
|
24 |
+
"pad_token": {
|
25 |
+
"content": "[PAD]",
|
26 |
+
"lstrip": false,
|
27 |
+
"normalized": false,
|
28 |
+
"rstrip": false,
|
29 |
+
"single_word": false
|
30 |
+
}
|
31 |
+
}
|
tokenizer.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:904d81ff0cfa066dbc0b6a21e10ded6ebb7c2d8df14100d851f90bb7878bd5de
|
3 |
+
size 11426251
|
tokenizer_config.json
ADDED
@@ -0,0 +1,393 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"add_bos_token": false,
|
3 |
+
"add_prefix_space": false,
|
4 |
+
"added_tokens_decoder": {
|
5 |
+
"151643": {
|
6 |
+
"content": "<|endoftext|>",
|
7 |
+
"lstrip": false,
|
8 |
+
"normalized": false,
|
9 |
+
"rstrip": false,
|
10 |
+
"single_word": false,
|
11 |
+
"special": true
|
12 |
+
},
|
13 |
+
"151644": {
|
14 |
+
"content": "<|im_start|>",
|
15 |
+
"lstrip": false,
|
16 |
+
"normalized": false,
|
17 |
+
"rstrip": false,
|
18 |
+
"single_word": false,
|
19 |
+
"special": true
|
20 |
+
},
|
21 |
+
"151645": {
|
22 |
+
"content": "<|im_end|>",
|
23 |
+
"lstrip": false,
|
24 |
+
"normalized": false,
|
25 |
+
"rstrip": false,
|
26 |
+
"single_word": false,
|
27 |
+
"special": true
|
28 |
+
},
|
29 |
+
"151646": {
|
30 |
+
"content": "<|object_ref_start|>",
|
31 |
+
"lstrip": false,
|
32 |
+
"normalized": false,
|
33 |
+
"rstrip": false,
|
34 |
+
"single_word": false,
|
35 |
+
"special": true
|
36 |
+
},
|
37 |
+
"151647": {
|
38 |
+
"content": "<|object_ref_end|>",
|
39 |
+
"lstrip": false,
|
40 |
+
"normalized": false,
|
41 |
+
"rstrip": false,
|
42 |
+
"single_word": false,
|
43 |
+
"special": true
|
44 |
+
},
|
45 |
+
"151648": {
|
46 |
+
"content": "<|box_start|>",
|
47 |
+
"lstrip": false,
|
48 |
+
"normalized": false,
|
49 |
+
"rstrip": false,
|
50 |
+
"single_word": false,
|
51 |
+
"special": true
|
52 |
+
},
|
53 |
+
"151649": {
|
54 |
+
"content": "<|box_end|>",
|
55 |
+
"lstrip": false,
|
56 |
+
"normalized": false,
|
57 |
+
"rstrip": false,
|
58 |
+
"single_word": false,
|
59 |
+
"special": true
|
60 |
+
},
|
61 |
+
"151650": {
|
62 |
+
"content": "<|quad_start|>",
|
63 |
+
"lstrip": false,
|
64 |
+
"normalized": false,
|
65 |
+
"rstrip": false,
|
66 |
+
"single_word": false,
|
67 |
+
"special": true
|
68 |
+
},
|
69 |
+
"151651": {
|
70 |
+
"content": "<|quad_end|>",
|
71 |
+
"lstrip": false,
|
72 |
+
"normalized": false,
|
73 |
+
"rstrip": false,
|
74 |
+
"single_word": false,
|
75 |
+
"special": true
|
76 |
+
},
|
77 |
+
"151652": {
|
78 |
+
"content": "<|vision_start|>",
|
79 |
+
"lstrip": false,
|
80 |
+
"normalized": false,
|
81 |
+
"rstrip": false,
|
82 |
+
"single_word": false,
|
83 |
+
"special": true
|
84 |
+
},
|
85 |
+
"151653": {
|
86 |
+
"content": "<|vision_end|>",
|
87 |
+
"lstrip": false,
|
88 |
+
"normalized": false,
|
89 |
+
"rstrip": false,
|
90 |
+
"single_word": false,
|
91 |
+
"special": true
|
92 |
+
},
|
93 |
+
"151654": {
|
94 |
+
"content": "<|vision_pad|>",
|
95 |
+
"lstrip": false,
|
96 |
+
"normalized": false,
|
97 |
+
"rstrip": false,
|
98 |
+
"single_word": false,
|
99 |
+
"special": true
|
100 |
+
},
|
101 |
+
"151655": {
|
102 |
+
"content": "<|image_pad|>",
|
103 |
+
"lstrip": false,
|
104 |
+
"normalized": false,
|
105 |
+
"rstrip": false,
|
106 |
+
"single_word": false,
|
107 |
+
"special": true
|
108 |
+
},
|
109 |
+
"151656": {
|
110 |
+
"content": "<|video_pad|>",
|
111 |
+
"lstrip": false,
|
112 |
+
"normalized": false,
|
113 |
+
"rstrip": false,
|
114 |
+
"single_word": false,
|
115 |
+
"special": true
|
116 |
+
},
|
117 |
+
"151657": {
|
118 |
+
"content": "<tool_call>",
|
119 |
+
"lstrip": false,
|
120 |
+
"normalized": false,
|
121 |
+
"rstrip": false,
|
122 |
+
"single_word": false,
|
123 |
+
"special": false
|
124 |
+
},
|
125 |
+
"151658": {
|
126 |
+
"content": "</tool_call>",
|
127 |
+
"lstrip": false,
|
128 |
+
"normalized": false,
|
129 |
+
"rstrip": false,
|
130 |
+
"single_word": false,
|
131 |
+
"special": false
|
132 |
+
},
|
133 |
+
"151659": {
|
134 |
+
"content": "<|fim_prefix|>",
|
135 |
+
"lstrip": false,
|
136 |
+
"normalized": false,
|
137 |
+
"rstrip": false,
|
138 |
+
"single_word": false,
|
139 |
+
"special": false
|
140 |
+
},
|
141 |
+
"151660": {
|
142 |
+
"content": "<|fim_middle|>",
|
143 |
+
"lstrip": false,
|
144 |
+
"normalized": false,
|
145 |
+
"rstrip": false,
|
146 |
+
"single_word": false,
|
147 |
+
"special": false
|
148 |
+
},
|
149 |
+
"151661": {
|
150 |
+
"content": "<|fim_suffix|>",
|
151 |
+
"lstrip": false,
|
152 |
+
"normalized": false,
|
153 |
+
"rstrip": false,
|
154 |
+
"single_word": false,
|
155 |
+
"special": false
|
156 |
+
},
|
157 |
+
"151662": {
|
158 |
+
"content": "<|fim_pad|>",
|
159 |
+
"lstrip": false,
|
160 |
+
"normalized": false,
|
161 |
+
"rstrip": false,
|
162 |
+
"single_word": false,
|
163 |
+
"special": false
|
164 |
+
},
|
165 |
+
"151663": {
|
166 |
+
"content": "<|repo_name|>",
|
167 |
+
"lstrip": false,
|
168 |
+
"normalized": false,
|
169 |
+
"rstrip": false,
|
170 |
+
"single_word": false,
|
171 |
+
"special": false
|
172 |
+
},
|
173 |
+
"151664": {
|
174 |
+
"content": "<|file_sep|>",
|
175 |
+
"lstrip": false,
|
176 |
+
"normalized": false,
|
177 |
+
"rstrip": false,
|
178 |
+
"single_word": false,
|
179 |
+
"special": false
|
180 |
+
},
|
181 |
+
"151665": {
|
182 |
+
"content": "<|imgpad|>",
|
183 |
+
"lstrip": false,
|
184 |
+
"normalized": false,
|
185 |
+
"rstrip": false,
|
186 |
+
"single_word": false,
|
187 |
+
"special": true
|
188 |
+
},
|
189 |
+
"151666": {
|
190 |
+
"content": "<|img|>",
|
191 |
+
"lstrip": false,
|
192 |
+
"normalized": false,
|
193 |
+
"rstrip": false,
|
194 |
+
"single_word": false,
|
195 |
+
"special": true
|
196 |
+
},
|
197 |
+
"151667": {
|
198 |
+
"content": "<|endofimg|>",
|
199 |
+
"lstrip": false,
|
200 |
+
"normalized": false,
|
201 |
+
"rstrip": false,
|
202 |
+
"single_word": false,
|
203 |
+
"special": true
|
204 |
+
},
|
205 |
+
"151668": {
|
206 |
+
"content": "<|systemprompt|>",
|
207 |
+
"lstrip": false,
|
208 |
+
"normalized": false,
|
209 |
+
"rstrip": false,
|
210 |
+
"single_word": false,
|
211 |
+
"special": true
|
212 |
+
},
|
213 |
+
"151669": {
|
214 |
+
"content": "<|endofsystemprompt|>",
|
215 |
+
"lstrip": false,
|
216 |
+
"normalized": false,
|
217 |
+
"rstrip": false,
|
218 |
+
"single_word": false,
|
219 |
+
"special": true
|
220 |
+
},
|
221 |
+
"151670": {
|
222 |
+
"content": "<|user|>",
|
223 |
+
"lstrip": false,
|
224 |
+
"normalized": false,
|
225 |
+
"rstrip": false,
|
226 |
+
"single_word": false,
|
227 |
+
"special": true
|
228 |
+
},
|
229 |
+
"151671": {
|
230 |
+
"content": "<|endofuser|>",
|
231 |
+
"lstrip": false,
|
232 |
+
"normalized": false,
|
233 |
+
"rstrip": false,
|
234 |
+
"single_word": false,
|
235 |
+
"special": true
|
236 |
+
},
|
237 |
+
"151672": {
|
238 |
+
"content": "<|assistant|>",
|
239 |
+
"lstrip": false,
|
240 |
+
"normalized": false,
|
241 |
+
"rstrip": false,
|
242 |
+
"single_word": false,
|
243 |
+
"special": true
|
244 |
+
},
|
245 |
+
"151673": {
|
246 |
+
"content": "<|endofassistant|>",
|
247 |
+
"lstrip": false,
|
248 |
+
"normalized": false,
|
249 |
+
"rstrip": false,
|
250 |
+
"single_word": false,
|
251 |
+
"special": true
|
252 |
+
},
|
253 |
+
"151674": {
|
254 |
+
"content": "<|ref_start|>",
|
255 |
+
"lstrip": false,
|
256 |
+
"normalized": false,
|
257 |
+
"rstrip": false,
|
258 |
+
"single_word": false,
|
259 |
+
"special": true
|
260 |
+
},
|
261 |
+
"151675": {
|
262 |
+
"content": "<|ref_end|>",
|
263 |
+
"lstrip": false,
|
264 |
+
"normalized": false,
|
265 |
+
"rstrip": false,
|
266 |
+
"single_word": false,
|
267 |
+
"special": true
|
268 |
+
},
|
269 |
+
"151676": {
|
270 |
+
"content": "[SEP]",
|
271 |
+
"lstrip": false,
|
272 |
+
"normalized": false,
|
273 |
+
"rstrip": false,
|
274 |
+
"single_word": false,
|
275 |
+
"special": true
|
276 |
+
},
|
277 |
+
"151677": {
|
278 |
+
"content": "<|pic|>",
|
279 |
+
"lstrip": false,
|
280 |
+
"normalized": false,
|
281 |
+
"rstrip": false,
|
282 |
+
"single_word": false,
|
283 |
+
"special": true
|
284 |
+
},
|
285 |
+
"151678": {
|
286 |
+
"content": "<|text|>",
|
287 |
+
"lstrip": false,
|
288 |
+
"normalized": false,
|
289 |
+
"rstrip": false,
|
290 |
+
"single_word": false,
|
291 |
+
"special": true
|
292 |
+
},
|
293 |
+
"151679": {
|
294 |
+
"content": "<|pictotext|>",
|
295 |
+
"lstrip": false,
|
296 |
+
"normalized": false,
|
297 |
+
"rstrip": false,
|
298 |
+
"single_word": false,
|
299 |
+
"special": true
|
300 |
+
},
|
301 |
+
"151680": {
|
302 |
+
"content": "[PAD]",
|
303 |
+
"lstrip": false,
|
304 |
+
"normalized": false,
|
305 |
+
"rstrip": false,
|
306 |
+
"single_word": false,
|
307 |
+
"special": true
|
308 |
+
},
|
309 |
+
"151681": {
|
310 |
+
"content": "<|slice|>",
|
311 |
+
"lstrip": false,
|
312 |
+
"normalized": false,
|
313 |
+
"rstrip": false,
|
314 |
+
"single_word": false,
|
315 |
+
"special": true
|
316 |
+
},
|
317 |
+
"151682": {
|
318 |
+
"content": "<|endofslice|>",
|
319 |
+
"lstrip": false,
|
320 |
+
"normalized": false,
|
321 |
+
"rstrip": false,
|
322 |
+
"single_word": false,
|
323 |
+
"special": true
|
324 |
+
},
|
325 |
+
"151683": {
|
326 |
+
"content": "<|imgrowend|>",
|
327 |
+
"lstrip": false,
|
328 |
+
"normalized": false,
|
329 |
+
"rstrip": false,
|
330 |
+
"single_word": false,
|
331 |
+
"special": true
|
332 |
+
},
|
333 |
+
"151684": {
|
334 |
+
"content": "<|polygon_start|>",
|
335 |
+
"lstrip": false,
|
336 |
+
"normalized": false,
|
337 |
+
"rstrip": false,
|
338 |
+
"single_word": false,
|
339 |
+
"special": true
|
340 |
+
},
|
341 |
+
"151685": {
|
342 |
+
"content": "<|polygon_end|>",
|
343 |
+
"lstrip": false,
|
344 |
+
"normalized": false,
|
345 |
+
"rstrip": false,
|
346 |
+
"single_word": false,
|
347 |
+
"special": true
|
348 |
+
},
|
349 |
+
"151686": {
|
350 |
+
"content": "<|image_gen_start|>",
|
351 |
+
"lstrip": false,
|
352 |
+
"normalized": false,
|
353 |
+
"rstrip": false,
|
354 |
+
"single_word": false,
|
355 |
+
"special": true
|
356 |
+
},
|
357 |
+
"151687": {
|
358 |
+
"content": "<|image_gen_end|>",
|
359 |
+
"lstrip": false,
|
360 |
+
"normalized": false,
|
361 |
+
"rstrip": false,
|
362 |
+
"single_word": false,
|
363 |
+
"special": true
|
364 |
+
}
|
365 |
+
},
|
366 |
+
"additional_special_tokens": [
|
367 |
+
"<|im_start|>",
|
368 |
+
"<|im_end|>",
|
369 |
+
"<|object_ref_start|>",
|
370 |
+
"<|object_ref_end|>",
|
371 |
+
"<|box_start|>",
|
372 |
+
"<|box_end|>",
|
373 |
+
"<|quad_start|>",
|
374 |
+
"<|quad_end|>",
|
375 |
+
"<|vision_start|>",
|
376 |
+
"<|vision_end|>",
|
377 |
+
"<|vision_pad|>",
|
378 |
+
"<|image_pad|>",
|
379 |
+
"<|video_pad|>"
|
380 |
+
],
|
381 |
+
"bos_token": null,
|
382 |
+
"chat_template": "{%- for m in messages %}\n {%- if m.role == 'system' %}\n {{- '<|system|>' + m.content + '<|endofsystem|>\\n' }}\n {%- elif m.role == 'user' %}\n {{- '<|user|>' + m.content + '<|endofuser|>' }}\n {%- elif m.role == 'assistant' %}\n {{- '<|assistant|>' + m.content }}\n {%- if not loop.last %}\n {{- '<|endofassistant|>' }}\n {%- endif %}\n {%- endif %}\n{%- endfor %}\n{%- if messages[-1].role != 'assistant' %}\n {{- '<|assistant|>' }}\n{%- endif %}",
|
383 |
+
"clean_up_tokenization_spaces": false,
|
384 |
+
"eos_token": "<|endoftext|>",
|
385 |
+
"errors": "replace",
|
386 |
+
"extra_special_tokens": {},
|
387 |
+
"model_max_length": 131072,
|
388 |
+
"pad_token": "[PAD]",
|
389 |
+
"processor_class": "DotsVLProcessor",
|
390 |
+
"split_special_tokens": false,
|
391 |
+
"tokenizer_class": "Qwen2Tokenizer",
|
392 |
+
"unk_token": null
|
393 |
+
}
|
vocab.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|