support vllm (#6)
Browse files- support vllm (24b80d31a032dfc653c12c049c1370ff992caae3)
- Update config.json (d1daf81e9b327a9042c3f7c518f4a83f266adbd9)
- Rename configuration_ernie_45t_vl.py to configuration_ernie4_5_vl.py (3990673651b8a78fbe0d4a31f2f81d522cebed48)
- Rename modeling_ernie_45t_vl.py to modeling_ernie4_5_vl.py (88de2604ae51bccd3f129e8fa4296c537a81dd75)
- Rename processing_ernie_45t_vl.py to processing_ernie4_5_vl.py (202ec6ce58d4d10c88a1de2abb05f28711d2a001)
- Update tokenizer_config.json (28dc5d11cf1e4437d9bb0d1a78ee1ea904dfe2b9)
chat_template.json
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
{
|
| 2 |
-
"chat_template": "\n{%- set image_count = namespace(value=0) -%}\n{%- set video_count = namespace(value=0) -%}\n{{- '<|begin_of_sentence|>' }}\n{%- for message in messages -%}\n {%- if message.role in ['system', 'user'] -%}\n {%- if message.role == 'user' -%}\n {{- 'User: ' -}}\n {%- endif -%}\n {%- if message.content is string -%}\n {{- message.content -}}\n {%- else -%}\n {%- for content_item in message.content -%}\n {%- if content_item.type == 'text' -%}\n {{- content_item.text -}}\n {%- elif content_item.type
|
| 3 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"chat_template": "\n{%- set image_count = namespace(value=0) -%}\n{%- set video_count = namespace(value=0) -%}\n{{- '<|begin_of_sentence|>' }}\n{%- for message in messages -%}\n {%- if message.role in ['system', 'user'] -%}\n {%- if message.role == 'user' -%}\n {{- 'User: ' -}}\n {%- endif -%}\n {%- if message.content is string -%}\n {{- message.content -}}\n {%- else -%}\n {%- for content_item in message.content -%}\n {%- if content_item.type == 'text' -%}\n {{- content_item.text -}}\n {%- elif content_item.type in ['image_url', 'image'] -%}\n {%- set image_count.value = image_count.value + 1 -%}\n Picture {{ image_count.value }}:<|IMAGE_START|><|image@placeholder|><|IMAGE_END|>\n {%- elif content_item.type in ['video_url', 'video'] -%}\n {%- set video_count.value = video_count.value + 1 -%}\n Video {{ video_count.value }}:<|VIDEO_START|><|video@placeholder|><|VIDEO_END|>\n {%- endif -%}\n {%- endfor -%}\n {%- endif -%}\n {%- if message.role == 'system' -%}\n {{- '\n' -}}\n {%- endif -%}\n {%- elif message.role == 'assistant' -%}\n {%- macro extract_text_content(content_field) -%}\n {%- if content_field is string -%}\n {{- content_field -}}\n {%- elif content_field is iterable and content_field is not string -%}\n {%- set ns = namespace(text_parts=[]) -%}\n {%- set text_parts = [] -%}\n {%- for item in content_field -%}\n {%- if item.type == 'text' -%}\n {%- set ns.text_parts = ns.text_parts + [item.text] -%}\n {%- endif -%}\n {%- endfor -%}\n {{- ns.text_parts | join('') -}}\n {%- else -%}\n {{- '' -}}\n {%- endif -%}\n {%- endmacro -%}\n {%- set reasoning_content = extract_text_content(message.reasoning_content) -%}\n {%- set content = extract_text_content(message.content) -%}\n {%- if '</think>' in content %}\n {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}\n {%- set content = content.split('</think>')[-1].lstrip('\n') %}\n {%- endif %}\n {%- if reasoning_content %}\n {{- '\n' + 'Assistant: ' + '<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}\n {%- else %}\n {{- '\n' + 'Assistant: ' + content }}\n {%- endif %}\n {{- '<|end_of_sentence|>' }}\n {%- endif -%}\n{%- endfor -%}\n{%- if add_generation_prompt is not defined or add_generation_prompt is true %}\n {{- '\nAssistant: ' -}}\n {%- if enable_thinking is defined and enable_thinking is false %}\n {{- '<think>\n\n</think>\n\n' }}\n {%- endif %}\n {%- if enable_thinking is not defined or enable_thinking is true %}\n {{- '<think>' }}\n {%- endif %}\n{%- endif %}\n"
|
| 3 |
}
|
config.json
CHANGED
|
@@ -3,11 +3,11 @@
|
|
| 3 |
"Ernie4_5_VLMoeForConditionalGeneration"
|
| 4 |
],
|
| 5 |
"auto_map": {
|
| 6 |
-
"AutoConfig": "
|
| 7 |
-
"AutoModel": "
|
| 8 |
-
"AutoModelForCausalLM": "
|
| 9 |
-
"AutoProcessor": "
|
| 10 |
-
"AutoImageProcessor": "
|
| 11 |
},
|
| 12 |
"pad_token_id": 0,
|
| 13 |
"bos_token_id": 1,
|
|
@@ -16,6 +16,8 @@
|
|
| 16 |
"hidden_size": 8192,
|
| 17 |
"intermediate_size": 28672,
|
| 18 |
"im_patch_id": 100295,
|
|
|
|
|
|
|
| 19 |
"max_position_embeddings": 131072,
|
| 20 |
"num_attention_heads": 64,
|
| 21 |
"num_key_value_heads": 8,
|
|
@@ -43,6 +45,14 @@
|
|
| 43 |
"torch_dtype": "bfloat16",
|
| 44 |
"tie_word_embeddings": false,
|
| 45 |
"moe_multimodal_dispatch_use_allgather": "v2-alltoall-unpad-text",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
"vision_config": {
|
| 47 |
"attn_implementation": "eager",
|
| 48 |
"depth": 32,
|
|
|
|
| 3 |
"Ernie4_5_VLMoeForConditionalGeneration"
|
| 4 |
],
|
| 5 |
"auto_map": {
|
| 6 |
+
"AutoConfig": "configuration_ernie4_5_vl.Ernie4_5_VLMoEConfig",
|
| 7 |
+
"AutoModel": "modeling_ernie4_5_vl.Ernie4_5_VLMoeForConditionalGeneration",
|
| 8 |
+
"AutoModelForCausalLM": "modeling_ernie4_5_vl.Ernie4_5_VLMoeForConditionalGeneration",
|
| 9 |
+
"AutoProcessor": "processing_ernie4_5_vl.Ernie4_5_VLProcessor",
|
| 10 |
+
"AutoImageProcessor": "processing_ernie4_5_vl.Ernie4_5_VLImageProcessor"
|
| 11 |
},
|
| 12 |
"pad_token_id": 0,
|
| 13 |
"bos_token_id": 1,
|
|
|
|
| 16 |
"hidden_size": 8192,
|
| 17 |
"intermediate_size": 28672,
|
| 18 |
"im_patch_id": 100295,
|
| 19 |
+
"video_start_token_id": 101306,
|
| 20 |
+
"video_end_token_id": 101307,
|
| 21 |
"max_position_embeddings": 131072,
|
| 22 |
"num_attention_heads": 64,
|
| 23 |
"num_key_value_heads": 8,
|
|
|
|
| 45 |
"torch_dtype": "bfloat16",
|
| 46 |
"tie_word_embeddings": false,
|
| 47 |
"moe_multimodal_dispatch_use_allgather": "v2-alltoall-unpad-text",
|
| 48 |
+
"rope_scaling": {
|
| 49 |
+
"type": "default",
|
| 50 |
+
"mrope_section": [
|
| 51 |
+
22,
|
| 52 |
+
22,
|
| 53 |
+
20
|
| 54 |
+
]
|
| 55 |
+
},
|
| 56 |
"vision_config": {
|
| 57 |
"attn_implementation": "eager",
|
| 58 |
"depth": 32,
|
configuration_ernie_45t_vl.py → configuration_ernie4_5_vl.py
RENAMED
|
@@ -171,7 +171,7 @@ class Ernie4_5_Config(PretrainedConfig):
|
|
| 171 |
use_fast_ln=False,
|
| 172 |
weight_share_add_bias=True,
|
| 173 |
fuse_linear=False,
|
| 174 |
-
max_sequence_length=
|
| 175 |
ignored_index=-100,
|
| 176 |
add_tail_layers=False,
|
| 177 |
use_recompute_lm_head=False,
|
|
|
|
| 171 |
use_fast_ln=False,
|
| 172 |
weight_share_add_bias=True,
|
| 173 |
fuse_linear=False,
|
| 174 |
+
max_sequence_length=None,
|
| 175 |
ignored_index=-100,
|
| 176 |
add_tail_layers=False,
|
| 177 |
use_recompute_lm_head=False,
|
modeling_ernie_45t_vl.py → modeling_ernie4_5_vl.py
RENAMED
|
@@ -35,7 +35,7 @@ from transformers.generation import GenerationMixin
|
|
| 35 |
from transformers.modeling_outputs import ModelOutput
|
| 36 |
from transformers.modeling_utils import PreTrainedModel
|
| 37 |
from transformers.utils import logging
|
| 38 |
-
from .
|
| 39 |
DFNRopeVisionTransformerConfig,
|
| 40 |
Ernie4_5_MoEConfig,
|
| 41 |
Ernie4_5_VLMoEConfig,
|
|
|
|
| 35 |
from transformers.modeling_outputs import ModelOutput
|
| 36 |
from transformers.modeling_utils import PreTrainedModel
|
| 37 |
from transformers.utils import logging
|
| 38 |
+
from .configuration_ernie4_5_vl import (
|
| 39 |
DFNRopeVisionTransformerConfig,
|
| 40 |
Ernie4_5_MoEConfig,
|
| 41 |
Ernie4_5_VLMoEConfig,
|
processing_ernie_45t_vl.py → processing_ernie4_5_vl.py
RENAMED
|
@@ -26,6 +26,7 @@ import hashlib
|
|
| 26 |
import threading
|
| 27 |
import uuid
|
| 28 |
import decord
|
|
|
|
| 29 |
from typing import Any, Dict, List, Optional, Tuple, Union
|
| 30 |
|
| 31 |
import numpy as np
|
|
@@ -43,8 +44,12 @@ except:
|
|
| 43 |
# moviepy 2.0
|
| 44 |
import moviepy as mp
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
from transformers.utils import TensorType, logging
|
| 49 |
from transformers.video_utils import VideoInput
|
| 50 |
from transformers.processing_utils import ProcessorMixin
|
|
@@ -74,6 +79,293 @@ from transformers.image_utils import (
|
|
| 74 |
logger = logging.get_logger(__name__)
|
| 75 |
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
def round_by_factor(number: int, factor: int) -> int:
|
| 78 |
"""Returns the closest integer to 'number' that is divisible by 'factor'."""
|
| 79 |
return round(number / factor) * factor
|
|
@@ -199,7 +491,7 @@ def make_batched_videos(videos) -> List[VideoInput]:
|
|
| 199 |
raise ValueError(f"Could not make batched video from {videos}")
|
| 200 |
|
| 201 |
|
| 202 |
-
class
|
| 203 |
r"""
|
| 204 |
Constructs a adaptive image processor that dynamically resizes images based on the original images.
|
| 205 |
|
|
@@ -281,7 +573,7 @@ class Ernie_45T_VLImageProcessor(BaseImageProcessor):
|
|
| 281 |
isinstance(min_pixels, int) and min_pixels >= 0
|
| 282 |
), "min_pixels must be positive int"
|
| 283 |
logger.info(
|
| 284 |
-
f"{msg}
|
| 285 |
)
|
| 286 |
self.min_pixels = min_pixels
|
| 287 |
self.size["min_pixels"] = int(min_pixels)
|
|
@@ -290,7 +582,7 @@ class Ernie_45T_VLImageProcessor(BaseImageProcessor):
|
|
| 290 |
isinstance(max_pixels, int) and max_pixels > 0
|
| 291 |
), "max_pixels must be positive int"
|
| 292 |
logger.info(
|
| 293 |
-
f"{msg}
|
| 294 |
)
|
| 295 |
self.max_pixels = max_pixels
|
| 296 |
self.size["max_pixels"] = int(max_pixels)
|
|
@@ -1089,7 +1381,7 @@ def render_frame_timestamp(frame, timestamp, font_rate=0.1):
|
|
| 1089 |
IDS_TYPE_FLAG = {"text": 0, "image": 1, "video": 2, "audio": 3}
|
| 1090 |
|
| 1091 |
|
| 1092 |
-
class
|
| 1093 |
"""
|
| 1094 |
Processes multimodal chat messages into model-ready inputs,
|
| 1095 |
handling text, images, and videos with 3D positional embeddings.
|
|
@@ -1236,11 +1528,11 @@ class Ernie_45T_VLProcessor(ProcessorMixin):
|
|
| 1236 |
|
| 1237 |
def __call__(
|
| 1238 |
self,
|
| 1239 |
-
text: List[str],
|
| 1240 |
-
images: List[Image.Image],
|
| 1241 |
-
videos: List[List[Image.Image]],
|
| 1242 |
**kwargs,
|
| 1243 |
-
) ->
|
| 1244 |
"""
|
| 1245 |
Convert chat messages into model inputs.
|
| 1246 |
Returns a dict with input_ids, token_type_ids, position_ids, images, grid_thw, image_type_ids, labels.
|
|
@@ -1256,6 +1548,9 @@ class Ernie_45T_VLProcessor(ProcessorMixin):
|
|
| 1256 |
"pic_cnt": 0,
|
| 1257 |
"video_cnt": 0,
|
| 1258 |
}
|
|
|
|
|
|
|
|
|
|
| 1259 |
texts = text[0]
|
| 1260 |
|
| 1261 |
new_video_seg = True
|
|
@@ -1520,4 +1815,4 @@ class Ernie_45T_VLProcessor(ProcessorMixin):
|
|
| 1520 |
return list(tokenizer_input_names) + list(image_processor_input_names)
|
| 1521 |
|
| 1522 |
|
| 1523 |
-
__all__ = ["
|
|
|
|
| 26 |
import threading
|
| 27 |
import uuid
|
| 28 |
import decord
|
| 29 |
+
from shutil import copyfile
|
| 30 |
from typing import Any, Dict, List, Optional, Tuple, Union
|
| 31 |
|
| 32 |
import numpy as np
|
|
|
|
| 44 |
# moviepy 2.0
|
| 45 |
import moviepy as mp
|
| 46 |
|
| 47 |
+
import sentencepiece as spm
|
| 48 |
+
from transformers.tokenization_utils import PreTrainedTokenizer
|
| 49 |
+
from transformers.tokenization_utils_base import (
|
| 50 |
+
PaddingStrategy,
|
| 51 |
+
TextInput,
|
| 52 |
+
)
|
| 53 |
from transformers.utils import TensorType, logging
|
| 54 |
from transformers.video_utils import VideoInput
|
| 55 |
from transformers.processing_utils import ProcessorMixin
|
|
|
|
| 79 |
logger = logging.get_logger(__name__)
|
| 80 |
|
| 81 |
|
| 82 |
+
class Ernie4_5_VLTokenizer(PreTrainedTokenizer):
|
| 83 |
+
"""
|
| 84 |
+
Ernie4_5_VLTokenizer
|
| 85 |
+
"""
|
| 86 |
+
|
| 87 |
+
vocab_files_names = {
|
| 88 |
+
"vocab_file": "tokenizer.model",
|
| 89 |
+
}
|
| 90 |
+
# Model input names expected by the tokenizer
|
| 91 |
+
model_input_names = ["input_ids", "position_ids", "attention_mask", "labels"]
|
| 92 |
+
# Padding side (where to add padding tokens)
|
| 93 |
+
padding_side = "right"
|
| 94 |
+
|
| 95 |
+
def __init__(
|
| 96 |
+
self,
|
| 97 |
+
vocab_file,
|
| 98 |
+
bos_token="<s>",
|
| 99 |
+
cls_token="<cls>",
|
| 100 |
+
eos_token="</s>",
|
| 101 |
+
mask_token="<mask:0>",
|
| 102 |
+
pad_token="<pad>",
|
| 103 |
+
sep_token="<sep>",
|
| 104 |
+
unk_token="<unk>",
|
| 105 |
+
additional_special_tokens=None,
|
| 106 |
+
**kwargs,
|
| 107 |
+
):
|
| 108 |
+
"""
|
| 109 |
+
Initialize the Ernie4_5_VLTokenizer
|
| 110 |
+
|
| 111 |
+
Args:
|
| 112 |
+
vocab_file (str): Path to the tokenizer vocabulary model.
|
| 113 |
+
bos_token (str, optional): The beginning of sequence token. Defaults to `"<s>"`.
|
| 114 |
+
cls_token (str, optional): The classifier token. Defaults to `"<cls>"`.
|
| 115 |
+
eos_token (str, optional): The end of sequence token. Defaults to `"</s>"`.
|
| 116 |
+
mask_token (str, optional): The masking token. Defaults to `"<mask:0>"`.
|
| 117 |
+
pad_token (str, optional): The padding token. Defaults to `"<pad>"`.
|
| 118 |
+
sep_token (str, optional): The separation token. Defaults to `"<sep>"`.
|
| 119 |
+
unk_token (str, optional): The unknown tokens symbol. Defaults to `"<unk>"`.
|
| 120 |
+
additional_special_tokens (List[str], optional): Additional special tokens to use.
|
| 121 |
+
Defaults to `["<mask:1>", "<mask:7>"]`.
|
| 122 |
+
**kwargs (dict): Additional keyword arguments passed along to the superclass.
|
| 123 |
+
"""
|
| 124 |
+
|
| 125 |
+
# Store vocabulary file path
|
| 126 |
+
self.vocab_file = vocab_file
|
| 127 |
+
# Initialize SentencePiece processor
|
| 128 |
+
self.sp_model = spm.SentencePieceProcessor()
|
| 129 |
+
# Load the vocabulary model
|
| 130 |
+
self.sp_model.Load(vocab_file)
|
| 131 |
+
|
| 132 |
+
# Set default additional special tokens if none provided
|
| 133 |
+
if additional_special_tokens is None:
|
| 134 |
+
additional_special_tokens = ["<mask:1>", "<mask:7>"]
|
| 135 |
+
super().__init__(
|
| 136 |
+
bos_token=bos_token,
|
| 137 |
+
cls_token=cls_token,
|
| 138 |
+
eos_token=eos_token,
|
| 139 |
+
mask_token=mask_token,
|
| 140 |
+
pad_token=pad_token,
|
| 141 |
+
sep_token=sep_token,
|
| 142 |
+
unk_token=unk_token,
|
| 143 |
+
additional_special_tokens=additional_special_tokens,
|
| 144 |
+
**kwargs,
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
@property
|
| 148 |
+
def space_token(self):
|
| 149 |
+
"""Return the space token"""
|
| 150 |
+
return "<mask:1>"
|
| 151 |
+
|
| 152 |
+
@property
|
| 153 |
+
def space_token_id(self):
|
| 154 |
+
"""Return the ID of the space token"""
|
| 155 |
+
return self.sp_model.piece_to_id("<mask:1>")
|
| 156 |
+
|
| 157 |
+
@property
|
| 158 |
+
def gend_token(self):
|
| 159 |
+
"""Return the gender token"""
|
| 160 |
+
return "<mask:7>"
|
| 161 |
+
|
| 162 |
+
@property
|
| 163 |
+
def gend_token_id(self):
|
| 164 |
+
"""Return the ID of the gender token"""
|
| 165 |
+
return self.sp_model.piece_to_id("<mask:7>")
|
| 166 |
+
|
| 167 |
+
@property
|
| 168 |
+
def im_start_id(self):
|
| 169 |
+
"""Return the ID of the image start token"""
|
| 170 |
+
return self.sp_model.piece_to_id("<|im_start|>")
|
| 171 |
+
|
| 172 |
+
@property
|
| 173 |
+
def im_end_id(self):
|
| 174 |
+
"""Return the ID of the image end token"""
|
| 175 |
+
return self.sp_model.piece_to_id("<|im_end|>")
|
| 176 |
+
|
| 177 |
+
@property
|
| 178 |
+
def vocab_size(self):
|
| 179 |
+
"""Return the size of the vocabulary"""
|
| 180 |
+
return self.sp_model.vocab_size()
|
| 181 |
+
|
| 182 |
+
def get_vocab(self):
|
| 183 |
+
"""Return the vocabulary as a dictionary mapping tokens to IDs"""
|
| 184 |
+
vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)}
|
| 185 |
+
vocab.update(self.added_tokens_encoder)
|
| 186 |
+
return vocab
|
| 187 |
+
|
| 188 |
+
def _tokenize(self, text):
|
| 189 |
+
"""Tokenize the input text into pieces"""
|
| 190 |
+
return self.sp_model.encode_as_pieces(text)
|
| 191 |
+
|
| 192 |
+
def _convert_token_to_id(self, token):
|
| 193 |
+
"""Convert a token to its corresponding ID"""
|
| 194 |
+
return self.sp_model.piece_to_id(token)
|
| 195 |
+
|
| 196 |
+
def _convert_id_to_token(self, id):
|
| 197 |
+
"""Convert an ID to its corresponding token"""
|
| 198 |
+
return self.sp_model.id_to_piece(id)
|
| 199 |
+
|
| 200 |
+
def convert_tokens_to_string(self, tokens):
|
| 201 |
+
"""Convert a sequence of tokens back to a string"""
|
| 202 |
+
current_sub_tokens = []
|
| 203 |
+
out_string = ""
|
| 204 |
+
|
| 205 |
+
for token in tokens:
|
| 206 |
+
# Handle special tokens differently
|
| 207 |
+
if token in self.all_special_tokens:
|
| 208 |
+
out_string += self.sp_model.decode(current_sub_tokens) + token
|
| 209 |
+
current_sub_tokens = []
|
| 210 |
+
else:
|
| 211 |
+
current_sub_tokens.append(token)
|
| 212 |
+
|
| 213 |
+
# Add any remaining sub-tokens
|
| 214 |
+
out_string += self.sp_model.decode(current_sub_tokens)
|
| 215 |
+
return out_string
|
| 216 |
+
|
| 217 |
+
def prepare_for_model(self, *args, **kwargs):
|
| 218 |
+
"""Prepare the tokenized inputs for the model"""
|
| 219 |
+
# Remove add_special_tokens if present (not supported)
|
| 220 |
+
if "add_special_tokens" in kwargs:
|
| 221 |
+
kwargs.pop("add_special_tokens")
|
| 222 |
+
return super().prepare_for_model(*args, **kwargs)
|
| 223 |
+
|
| 224 |
+
def save_vocabulary(
|
| 225 |
+
self, save_directory, filename_prefix: Optional[str] = None
|
| 226 |
+
) -> Tuple[str]:
|
| 227 |
+
"""
|
| 228 |
+
Save the vocabulary and special tokens file to a directory.
|
| 229 |
+
|
| 230 |
+
Args:
|
| 231 |
+
save_directory (`str`): The directory to save the vocabulary to
|
| 232 |
+
filename_prefix (`str`, optional): Prefix to add to the filename
|
| 233 |
+
|
| 234 |
+
Returns:
|
| 235 |
+
`Tuple(str)`: Paths to the saved files
|
| 236 |
+
"""
|
| 237 |
+
if not os.path.isdir(save_directory):
|
| 238 |
+
logger.error(f"Vocabulary path ({save_directory}) should be a directory")
|
| 239 |
+
return
|
| 240 |
+
|
| 241 |
+
# Construct output vocabulary file path
|
| 242 |
+
out_vocab_file = os.path.join(
|
| 243 |
+
save_directory,
|
| 244 |
+
(filename_prefix + "-" if filename_prefix else "")
|
| 245 |
+
+ self.vocab_files_names["vocab_file"],
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
# Copy or create vocabulary file
|
| 249 |
+
if os.path.abspath(self.vocab_file) != os.path.abspath(
|
| 250 |
+
out_vocab_file
|
| 251 |
+
) and os.path.isfile(self.vocab_file):
|
| 252 |
+
copyfile(self.vocab_file, out_vocab_file)
|
| 253 |
+
elif not os.path.isfile(self.vocab_file):
|
| 254 |
+
with open(out_vocab_file, "wb") as fi:
|
| 255 |
+
content_spiece_model = self.sp_model.serialized_model_proto()
|
| 256 |
+
fi.write(content_spiece_model)
|
| 257 |
+
|
| 258 |
+
return (out_vocab_file,)
|
| 259 |
+
|
| 260 |
+
def _decode(self, *args, **kwargs):
|
| 261 |
+
"""Decode token_id back to text"""
|
| 262 |
+
# Remove some parameters that aren't used
|
| 263 |
+
kwargs.pop("clean_up_tokenization_spaces", None)
|
| 264 |
+
kwargs.pop("spaces_between_special_tokens", None)
|
| 265 |
+
|
| 266 |
+
# Call parent decode method with specific parameters
|
| 267 |
+
return super()._decode(
|
| 268 |
+
*args,
|
| 269 |
+
**kwargs,
|
| 270 |
+
clean_up_tokenization_spaces=False,
|
| 271 |
+
spaces_between_special_tokens=False,
|
| 272 |
+
)
|
| 273 |
+
|
| 274 |
+
def _pad(
|
| 275 |
+
self,
|
| 276 |
+
encoded_inputs: Dict,
|
| 277 |
+
max_length: Optional[int] = None,
|
| 278 |
+
padding_strategy=PaddingStrategy.DO_NOT_PAD,
|
| 279 |
+
pad_to_multiple_of: Optional[int] = None,
|
| 280 |
+
return_attention_mask: Optional[bool] = None,
|
| 281 |
+
**kwargs
|
| 282 |
+
) -> dict:
|
| 283 |
+
"""Pad the encoded inputs to the specified length"""
|
| 284 |
+
if return_attention_mask is None:
|
| 285 |
+
return_attention_mask = "attention_mask" in self.model_input_names
|
| 286 |
+
if return_attention_mask:
|
| 287 |
+
required_input = encoded_inputs[self.model_input_names[0]]
|
| 288 |
+
if padding_strategy == PaddingStrategy.LONGEST:
|
| 289 |
+
max_length = len(required_input)
|
| 290 |
+
|
| 291 |
+
# Adjust max_length if needed for multiple of padding
|
| 292 |
+
if (
|
| 293 |
+
max_length is not None
|
| 294 |
+
and pad_to_multiple_of is not None
|
| 295 |
+
and (max_length % pad_to_multiple_of != 0)
|
| 296 |
+
):
|
| 297 |
+
max_length = (
|
| 298 |
+
(max_length // pad_to_multiple_of) + 1
|
| 299 |
+
) * pad_to_multiple_of
|
| 300 |
+
|
| 301 |
+
# Check if padding is needed
|
| 302 |
+
needs_to_be_padded = (
|
| 303 |
+
padding_strategy != PaddingStrategy.DO_NOT_PAD
|
| 304 |
+
and len(required_input) != max_length
|
| 305 |
+
)
|
| 306 |
+
|
| 307 |
+
# Handle attention mask if present
|
| 308 |
+
if (
|
| 309 |
+
"attention_mask" in encoded_inputs
|
| 310 |
+
and encoded_inputs["attention_mask"] is not None
|
| 311 |
+
):
|
| 312 |
+
attention_mask = encoded_inputs.pop("attention_mask")
|
| 313 |
+
if isinstance(attention_mask, torch.Tensor):
|
| 314 |
+
attention_mask = attention_mask.numpy()
|
| 315 |
+
elif isinstance(attention_mask, list):
|
| 316 |
+
attention_mask = np.array(attention_mask)
|
| 317 |
+
elif not isinstance(attention_mask, np.ndarray):
|
| 318 |
+
raise ValueError(
|
| 319 |
+
f"Unexpected type {type(attention_mask)} of attention_mask, "
|
| 320 |
+
)
|
| 321 |
+
else:
|
| 322 |
+
# Create default attention mask if none provided
|
| 323 |
+
attention_mask = np.tril(
|
| 324 |
+
np.ones((len(required_input), len(required_input)), dtype=np.int64)
|
| 325 |
+
)
|
| 326 |
+
attention_mask = np.expand_dims(attention_mask, axis=0)
|
| 327 |
+
|
| 328 |
+
# Perform padding if needed
|
| 329 |
+
if needs_to_be_padded:
|
| 330 |
+
difference = max_length - len(required_input)
|
| 331 |
+
if self.padding_side == "right":
|
| 332 |
+
if attention_mask.ndim == 1:
|
| 333 |
+
pad_width = [(0, difference)]
|
| 334 |
+
else:
|
| 335 |
+
pad_width = [(0, 0), (0, difference), (0, difference)]
|
| 336 |
+
elif self.padding_side == "left":
|
| 337 |
+
if attention_mask.ndim == 1:
|
| 338 |
+
pad_width = [(difference, 0)]
|
| 339 |
+
else:
|
| 340 |
+
pad_width = [(0, 0), (difference, 0), (difference, 0)]
|
| 341 |
+
else:
|
| 342 |
+
raise ValueError(
|
| 343 |
+
"Invalid padding strategy:" + str(self.padding_side)
|
| 344 |
+
)
|
| 345 |
+
|
| 346 |
+
attention_mask = np.pad(
|
| 347 |
+
attention_mask,
|
| 348 |
+
pad_width=pad_width,
|
| 349 |
+
mode="constant",
|
| 350 |
+
constant_values=0,
|
| 351 |
+
)
|
| 352 |
+
|
| 353 |
+
# Call parent padding method
|
| 354 |
+
encoded_inputs = super()._pad(
|
| 355 |
+
encoded_inputs,
|
| 356 |
+
max_length,
|
| 357 |
+
padding_strategy=padding_strategy,
|
| 358 |
+
pad_to_multiple_of=pad_to_multiple_of,
|
| 359 |
+
return_attention_mask=False,
|
| 360 |
+
)
|
| 361 |
+
|
| 362 |
+
# Add attention mask back if needed
|
| 363 |
+
if return_attention_mask:
|
| 364 |
+
encoded_inputs["attention_mask"] = attention_mask.tolist()
|
| 365 |
+
|
| 366 |
+
return encoded_inputs
|
| 367 |
+
|
| 368 |
+
|
| 369 |
def round_by_factor(number: int, factor: int) -> int:
|
| 370 |
"""Returns the closest integer to 'number' that is divisible by 'factor'."""
|
| 371 |
return round(number / factor) * factor
|
|
|
|
| 491 |
raise ValueError(f"Could not make batched video from {videos}")
|
| 492 |
|
| 493 |
|
| 494 |
+
class Ernie4_5_VLImageProcessor(BaseImageProcessor):
|
| 495 |
r"""
|
| 496 |
Constructs a adaptive image processor that dynamically resizes images based on the original images.
|
| 497 |
|
|
|
|
| 573 |
isinstance(min_pixels, int) and min_pixels >= 0
|
| 574 |
), "min_pixels must be positive int"
|
| 575 |
logger.info(
|
| 576 |
+
f"{msg} Ernie4_5_VLImageProcessor set min_pixels = {min_pixels}"
|
| 577 |
)
|
| 578 |
self.min_pixels = min_pixels
|
| 579 |
self.size["min_pixels"] = int(min_pixels)
|
|
|
|
| 582 |
isinstance(max_pixels, int) and max_pixels > 0
|
| 583 |
), "max_pixels must be positive int"
|
| 584 |
logger.info(
|
| 585 |
+
f"{msg} Ernie4_5_VLImageProcessor set max_pixels = {max_pixels}"
|
| 586 |
)
|
| 587 |
self.max_pixels = max_pixels
|
| 588 |
self.size["max_pixels"] = int(max_pixels)
|
|
|
|
| 1381 |
IDS_TYPE_FLAG = {"text": 0, "image": 1, "video": 2, "audio": 3}
|
| 1382 |
|
| 1383 |
|
| 1384 |
+
class Ernie4_5_VLProcessor(ProcessorMixin):
|
| 1385 |
"""
|
| 1386 |
Processes multimodal chat messages into model-ready inputs,
|
| 1387 |
handling text, images, and videos with 3D positional embeddings.
|
|
|
|
| 1528 |
|
| 1529 |
def __call__(
|
| 1530 |
self,
|
| 1531 |
+
text: Union[str, List[str]],
|
| 1532 |
+
images: List[Image.Image] = [],
|
| 1533 |
+
videos: List[List[Image.Image]] = [],
|
| 1534 |
**kwargs,
|
| 1535 |
+
) -> BatchFeature:
|
| 1536 |
"""
|
| 1537 |
Convert chat messages into model inputs.
|
| 1538 |
Returns a dict with input_ids, token_type_ids, position_ids, images, grid_thw, image_type_ids, labels.
|
|
|
|
| 1548 |
"pic_cnt": 0,
|
| 1549 |
"video_cnt": 0,
|
| 1550 |
}
|
| 1551 |
+
if not isinstance(text, list):
|
| 1552 |
+
text = [text]
|
| 1553 |
+
|
| 1554 |
texts = text[0]
|
| 1555 |
|
| 1556 |
new_video_seg = True
|
|
|
|
| 1815 |
return list(tokenizer_input_names) + list(image_processor_input_names)
|
| 1816 |
|
| 1817 |
|
| 1818 |
+
__all__ = ["Ernie4_5_VLTokenizer", "Ernie4_5_VLImageProcessor", "Ernie4_5_VLProcessor"]
|
tokenizer_config.json
CHANGED
|
@@ -14,9 +14,9 @@
|
|
| 14 |
"tokenizer_class": "Ernie4_5_VLTokenizer",
|
| 15 |
"auto_map": {
|
| 16 |
"AutoTokenizer": [
|
| 17 |
-
"
|
| 18 |
null
|
| 19 |
]
|
| 20 |
},
|
| 21 |
-
"chat_template": "\n{%- set image_count = namespace(value=0) -%}\n{%- set video_count = namespace(value=0) -%}\n{{- '<|begin_of_sentence|>' }}\n{%- for message in messages -%}\n {%- if message.role in ['system', 'user'] -%}\n {%- if message.role == 'user' -%}\n {{- 'User: ' -}}\n {%- endif -%}\n {%- if message.content is string -%}\n {{- message.content -}}\n {%- else -%}\n {%- for content_item in message.content -%}\n {%- if content_item.type == 'text' -%}\n {{- content_item.text -}}\n {%- elif content_item.type
|
| 22 |
}
|
|
|
|
| 14 |
"tokenizer_class": "Ernie4_5_VLTokenizer",
|
| 15 |
"auto_map": {
|
| 16 |
"AutoTokenizer": [
|
| 17 |
+
"processing_ernie4_5_vl.Ernie4_5_VLTokenizer",
|
| 18 |
null
|
| 19 |
]
|
| 20 |
},
|
| 21 |
+
"chat_template": "\n{%- set image_count = namespace(value=0) -%}\n{%- set video_count = namespace(value=0) -%}\n{{- '<|begin_of_sentence|>' }}\n{%- for message in messages -%}\n {%- if message.role in ['system', 'user'] -%}\n {%- if message.role == 'user' -%}\n {{- 'User: ' -}}\n {%- endif -%}\n {%- if message.content is string -%}\n {{- message.content -}}\n {%- else -%}\n {%- for content_item in message.content -%}\n {%- if content_item.type == 'text' -%}\n {{- content_item.text -}}\n {%- elif content_item.type in ['image_url', 'image'] -%}\n {%- set image_count.value = image_count.value + 1 -%}\n Picture {{ image_count.value }}:<|IMAGE_START|><|image@placeholder|><|IMAGE_END|>\n {%- elif content_item.type in ['video_url', 'video'] -%}\n {%- set video_count.value = video_count.value + 1 -%}\n Video {{ video_count.value }}:<|VIDEO_START|><|video@placeholder|><|VIDEO_END|>\n {%- endif -%}\n {%- endfor -%}\n {%- endif -%}\n {%- if message.role == 'system' -%}\n {{- '\n' -}}\n {%- endif -%}\n {%- elif message.role == 'assistant' -%}\n {%- macro extract_text_content(content_field) -%}\n {%- if content_field is string -%}\n {{- content_field -}}\n {%- elif content_field is iterable and content_field is not string -%}\n {%- set ns = namespace(text_parts=[]) -%}\n {%- set text_parts = [] -%}\n {%- for item in content_field -%}\n {%- if item.type == 'text' -%}\n {%- set ns.text_parts = ns.text_parts + [item.text] -%}\n {%- endif -%}\n {%- endfor -%}\n {{- ns.text_parts | join('') -}}\n {%- else -%}\n {{- '' -}}\n {%- endif -%}\n {%- endmacro -%}\n {%- set reasoning_content = extract_text_content(message.reasoning_content) -%}\n {%- set content = extract_text_content(message.content) -%}\n {%- if '</think>' in content %}\n {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}\n {%- set content = content.split('</think>')[-1].lstrip('\n') %}\n {%- endif %}\n {%- if reasoning_content %}\n {{- '\n' + 'Assistant: ' + '<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}\n {%- else %}\n {{- '\n' + 'Assistant: ' + content }}\n {%- endif %}\n {{- '<|end_of_sentence|>' }}\n {%- endif -%}\n{%- endfor -%}\n{%- if add_generation_prompt is not defined or add_generation_prompt is true %}\n {{- '\nAssistant: ' -}}\n {%- if enable_thinking is defined and enable_thinking is false %}\n {{- '<think>\n\n</think>\n\n' }}\n {%- endif %}\n {%- if enable_thinking is not defined or enable_thinking is true %}\n {{- '<think>' }}\n {%- endif %}\n{%- endif %}\n"
|
| 22 |
}
|