support vllm (#2)
Browse files- support vllm (c7716cef25ba1eef5bec2a539453cbef9b88ee3d)
- Update config.json (ba8f23ad1c359ca7ac149bae3123bf38d2989e43)
- Rename configuration_ernie_45t_vl.py to configuration_ernie4_5_vl.py (dc38b3c0c3f64c1c32ce7e8b1dedf83d47efd115)
- Update configuration_ernie4_5_vl.py (af466974158d43c4070c4243ae7ad82f2e8380b5)
- Rename modeling_ernie_45t_vl.py to modeling_ernie4_5_vl.py (753232921ad32debb593699b568e9c70331857a5)
- Rename processing_ernie_45t_vl.py to processing_ernie4_5_vl.py (cdeb408adaf7208fd9cc65b5efc220142d1c77c5)
- Update tokenizer_config.json (facc0daa460b975c0172a2bcbf53850c87b71b29)
chat_template.json
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
{
|
| 2 |
-
|
| 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,
|
|
@@ -17,6 +17,9 @@
|
|
| 17 |
"hidden_size": 2560,
|
| 18 |
"intermediate_size": 12288,
|
| 19 |
"im_patch_id": 100295,
|
|
|
|
|
|
|
|
|
|
| 20 |
"model_type": "ernie4_5_moe_vl",
|
| 21 |
"moe_capacity": [128, 128, 128],
|
| 22 |
"moe_gate": "topk",
|
|
@@ -43,6 +46,14 @@
|
|
| 43 |
"use_cache": true,
|
| 44 |
"use_rmsnorm": true,
|
| 45 |
"use_bias": false,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,
|
|
|
|
| 17 |
"hidden_size": 2560,
|
| 18 |
"intermediate_size": 12288,
|
| 19 |
"im_patch_id": 100295,
|
| 20 |
+
"video_start_token_id": 101306,
|
| 21 |
+
"video_end_token_id": 101307,
|
| 22 |
+
"max_position_embeddings": 131072,
|
| 23 |
"model_type": "ernie4_5_moe_vl",
|
| 24 |
"moe_capacity": [128, 128, 128],
|
| 25 |
"moe_gate": "topk",
|
|
|
|
| 46 |
"use_cache": true,
|
| 47 |
"use_rmsnorm": true,
|
| 48 |
"use_bias": false,
|
| 49 |
+
"rope_scaling": {
|
| 50 |
+
"type": "default",
|
| 51 |
+
"mrope_section": [
|
| 52 |
+
22,
|
| 53 |
+
22,
|
| 54 |
+
20
|
| 55 |
+
]
|
| 56 |
+
},
|
| 57 |
"vision_config": {
|
| 58 |
"attn_implementation": "eager",
|
| 59 |
"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,
|
|
@@ -539,17 +539,19 @@ class Ernie4_5_VLMoEConfig(Ernie4_5_MoEConfig):
|
|
| 539 |
"activation_function": "hidden_act",
|
| 540 |
}
|
| 541 |
base_model_tp_plan = {
|
| 542 |
-
"
|
| 543 |
-
"
|
| 544 |
-
"
|
| 545 |
-
"
|
| 546 |
-
"
|
| 547 |
-
"
|
| 548 |
-
"
|
| 549 |
-
"
|
| 550 |
-
"
|
| 551 |
-
"
|
| 552 |
-
"
|
|
|
|
|
|
|
| 553 |
}
|
| 554 |
|
| 555 |
def __init__(
|
|
|
|
| 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,
|
|
|
|
| 539 |
"activation_function": "hidden_act",
|
| 540 |
}
|
| 541 |
base_model_tp_plan = {
|
| 542 |
+
"model.layers.*.self_attn.q_proj": "colwise_rep",
|
| 543 |
+
"model.layers.*.self_attn.k_proj": "colwise_rep",
|
| 544 |
+
"model.layers.*.self_attn.v_proj": "colwise_rep",
|
| 545 |
+
"model.layers.*.self_attn.o_proj": "rowwise_rep",
|
| 546 |
+
"model.layers.*.mlp.experts.*.gate_proj": "colwise",
|
| 547 |
+
"model.layers.*.mlp.experts.*.up_proj": "colwise",
|
| 548 |
+
"model.layers.*.mlp.experts.*.down_proj": "rowwise",
|
| 549 |
+
"model.layers.*.mlp_text.experts.*.gate_proj": "colwise",
|
| 550 |
+
"model.layers.*.mlp_text.experts.*.up_proj": "colwise",
|
| 551 |
+
"model.layers.*.mlp_text.experts.*.down_proj": "rowwise",
|
| 552 |
+
"model.layers.*.mlp.gate_proj": "colwise",
|
| 553 |
+
"model.layers.*.mlp.up_proj": "colwise",
|
| 554 |
+
"model.layers.*.mlp.down_proj": "rowwise"
|
| 555 |
}
|
| 556 |
|
| 557 |
def __init__(
|
modeling_ernie_45t_vl.py → modeling_ernie4_5_vl.py
RENAMED
|
@@ -27,13 +27,14 @@ import numpy as np
|
|
| 27 |
import torch
|
| 28 |
import torch.nn as nn
|
| 29 |
import torch.nn.functional as F
|
|
|
|
| 30 |
|
| 31 |
from transformers.activations import ACT2FN
|
| 32 |
from transformers.generation import GenerationMixin
|
| 33 |
from transformers.modeling_outputs import ModelOutput
|
| 34 |
from transformers.modeling_utils import PreTrainedModel
|
| 35 |
from transformers.utils import logging
|
| 36 |
-
from .
|
| 37 |
DFNRopeVisionTransformerConfig,
|
| 38 |
Ernie4_5_MoEConfig,
|
| 39 |
Ernie4_5_VLMoEConfig,
|
|
@@ -321,6 +322,7 @@ class Ernie4_5_Attention(nn.Module):
|
|
| 321 |
self.hidden_size = config.hidden_size
|
| 322 |
self.num_heads = config.num_attention_heads
|
| 323 |
self.num_key_value_heads = config.num_key_value_heads
|
|
|
|
| 324 |
self.head_dim = self.hidden_size // self.num_heads
|
| 325 |
self.is_gqa = (
|
| 326 |
self.num_key_value_heads is not None
|
|
@@ -373,7 +375,10 @@ class Ernie4_5_Attention(nn.Module):
|
|
| 373 |
freq_allocation=self.freq_allocation,
|
| 374 |
)
|
| 375 |
self.config = config
|
| 376 |
-
|
|
|
|
|
|
|
|
|
|
| 377 |
|
| 378 |
def forward(
|
| 379 |
self,
|
|
@@ -446,6 +451,47 @@ class Ernie4_5_Attention(nn.Module):
|
|
| 446 |
)
|
| 447 |
return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
|
| 448 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 449 |
def core_attn(
|
| 450 |
self,
|
| 451 |
q,
|
|
@@ -493,19 +539,13 @@ class Ernie4_5_Attention(nn.Module):
|
|
| 493 |
if getattr(self.config, "scale_qk_coeff", 1.0) != 1.0:
|
| 494 |
product = product * getattr(self.config, "scale_qk_coeff", 1.0)
|
| 495 |
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
mask = torch.triu(
|
| 504 |
-
torch.ones((seq_len, seq_len), dtype=torch.bool, device=product.device),
|
| 505 |
-
diagonal=1,
|
| 506 |
-
)
|
| 507 |
-
product = product.masked_fill(mask, float("-inf"))
|
| 508 |
-
weights = F.softmax(product, dim=-1)
|
| 509 |
|
| 510 |
weights = weights.to(origin_dtype)
|
| 511 |
|
|
@@ -1508,16 +1548,8 @@ class MOELayer(nn.Module):
|
|
| 1508 |
)
|
| 1509 |
assert self.gate.config.moe_use_aux_free
|
| 1510 |
|
| 1511 |
-
|
| 1512 |
-
|
| 1513 |
-
self.rank = torch.distributed.get_rank()
|
| 1514 |
-
except:
|
| 1515 |
-
self.world_size = 1
|
| 1516 |
-
self.rank = 0
|
| 1517 |
-
if self.world_size < 1:
|
| 1518 |
-
self.world_size = 1
|
| 1519 |
-
if self.rank < 0:
|
| 1520 |
-
self.rank = 0
|
| 1521 |
|
| 1522 |
self.multimodal_experts = (
|
| 1523 |
isinstance(moe_num_experts, (tuple, list)) and len(moe_num_experts) > 1
|
|
@@ -1602,7 +1634,11 @@ class MOELayer(nn.Module):
|
|
| 1602 |
S, H = x.shape
|
| 1603 |
E = gate_logits.shape[1]
|
| 1604 |
device = x.device
|
| 1605 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1606 |
combine_weights = topk_prob # [S, k]
|
| 1607 |
expert_id = topk_idx # [S, k]
|
| 1608 |
y = x.new_zeros((E, capacity, H)) # [E, C, H]
|
|
@@ -1803,7 +1839,7 @@ class MOEAllGatherLayerV2(MOELayer):
|
|
| 1803 |
enable_reverse_token_drop=False,
|
| 1804 |
all_to_all_dropout=0,
|
| 1805 |
group_experts=False,
|
| 1806 |
-
use_expert_out_alltoall=True,
|
| 1807 |
use_expert_alltoall_overlap=False,
|
| 1808 |
use_padding=True,
|
| 1809 |
dense_token_type=3, # considerd as dense tokens (no moe)
|
|
@@ -2729,7 +2765,6 @@ class Ernie4_5_PretrainedModel(PreTrainedModel):
|
|
| 2729 |
config_class = Ernie4_5_MoEConfig
|
| 2730 |
base_model_prefix = "ernie"
|
| 2731 |
_no_split_modules = ["Ernie4_5_DecoderLayer"]
|
| 2732 |
-
# _keep_in_fp32_modules = ["mlp.gate", "e_score_correction_bias"]
|
| 2733 |
|
| 2734 |
|
| 2735 |
class Ernie4_5_Model(Ernie4_5_PretrainedModel):
|
|
@@ -2876,7 +2911,6 @@ class Ernie4_5_Model(Ernie4_5_PretrainedModel):
|
|
| 2876 |
past_key_value = (
|
| 2877 |
past_key_values[idx] if past_key_values is not None else None
|
| 2878 |
)
|
| 2879 |
-
|
| 2880 |
layer_outputs = decoder_layer(
|
| 2881 |
hidden_states,
|
| 2882 |
attention_mask,
|
|
@@ -3224,15 +3258,61 @@ class Ernie4_5_MoeForCausalLM(Ernie4_5_PretrainedModel, GenerationMixin):
|
|
| 3224 |
"""
|
| 3225 |
return self.model
|
| 3226 |
|
| 3227 |
-
|
| 3228 |
-
|
| 3229 |
-
|
| 3230 |
-
|
| 3231 |
-
|
| 3232 |
-
|
| 3233 |
-
|
| 3234 |
-
|
| 3235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3236 |
|
| 3237 |
|
| 3238 |
class VisionMlp(nn.Module):
|
|
@@ -3381,33 +3461,27 @@ class VisionAttention(nn.Module):
|
|
| 3381 |
k = apply_rotary_pos_emb_vision(k.unsqueeze(dim=0), rotary_pos_emb).squeeze(
|
| 3382 |
dim=0
|
| 3383 |
)
|
| 3384 |
-
|
| 3385 |
-
max_seqlen = (cu_seqlens[1:] - cu_seqlens[:-1]).max().item()
|
| 3386 |
-
|
| 3387 |
-
attention_mask = torch.full(
|
| 3388 |
-
[1, seq_length, seq_length],
|
| 3389 |
-
torch.finfo(q.dtype).min,
|
| 3390 |
-
device=q.device,
|
| 3391 |
-
dtype=q.dtype,
|
| 3392 |
-
)
|
| 3393 |
-
for i in range(1, len(cu_seqlens)):
|
| 3394 |
-
attention_mask[
|
| 3395 |
-
...,
|
| 3396 |
-
cu_seqlens[i - 1] : cu_seqlens[i],
|
| 3397 |
-
cu_seqlens[i - 1] : cu_seqlens[i],
|
| 3398 |
-
] = 0
|
| 3399 |
-
|
| 3400 |
q = q.transpose(0, 1)
|
| 3401 |
k = k.transpose(0, 1)
|
| 3402 |
v = v.transpose(0, 1)
|
| 3403 |
-
|
| 3404 |
-
|
| 3405 |
-
|
| 3406 |
-
|
| 3407 |
-
|
| 3408 |
-
|
| 3409 |
-
attn_output =
|
| 3410 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3411 |
attn_output = self.proj(attn_output)
|
| 3412 |
return attn_output
|
| 3413 |
|
|
@@ -3943,7 +4017,10 @@ class Ernie4_5_VLMoeForConditionalGeneration(Ernie4_5_MoeForCausalLM):
|
|
| 3943 |
image_type_ids[:, -1:] if image_type_ids is not None else None
|
| 3944 |
)
|
| 3945 |
|
| 3946 |
-
|
|
|
|
|
|
|
|
|
|
| 3947 |
|
| 3948 |
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
|
| 3949 |
if inputs_embeds is not None and past_key_values is None:
|
|
@@ -4077,7 +4154,7 @@ class Ernie4_5_VLMoeForConditionalGeneration(Ernie4_5_MoeForCausalLM):
|
|
| 4077 |
|
| 4078 |
if images is not None and image_features is not None:
|
| 4079 |
inputs_embeds = self.vision_mapping_forward(
|
| 4080 |
-
token_type_ids,
|
| 4081 |
token_type_ids_w_video,
|
| 4082 |
input_ids,
|
| 4083 |
mm_input_ids,
|
|
@@ -4091,7 +4168,7 @@ class Ernie4_5_VLMoeForConditionalGeneration(Ernie4_5_MoeForCausalLM):
|
|
| 4091 |
|
| 4092 |
outputs = self.model(
|
| 4093 |
position_ids=position_ids,
|
| 4094 |
-
attention_mask=
|
| 4095 |
token_type_ids=token_type_ids,
|
| 4096 |
inputs_embeds=inputs_embeds,
|
| 4097 |
use_cache=use_cache,
|
|
|
|
| 27 |
import torch
|
| 28 |
import torch.nn as nn
|
| 29 |
import torch.nn.functional as F
|
| 30 |
+
from torch.nn.attention import SDPBackend, sdpa_kernel
|
| 31 |
|
| 32 |
from transformers.activations import ACT2FN
|
| 33 |
from transformers.generation import GenerationMixin
|
| 34 |
from transformers.modeling_outputs import ModelOutput
|
| 35 |
from transformers.modeling_utils import PreTrainedModel
|
| 36 |
from transformers.utils import logging
|
| 37 |
+
from .configuration_ernie4_5_vl import (
|
| 38 |
DFNRopeVisionTransformerConfig,
|
| 39 |
Ernie4_5_MoEConfig,
|
| 40 |
Ernie4_5_VLMoEConfig,
|
|
|
|
| 322 |
self.hidden_size = config.hidden_size
|
| 323 |
self.num_heads = config.num_attention_heads
|
| 324 |
self.num_key_value_heads = config.num_key_value_heads
|
| 325 |
+
self.num_key_value_groups = config.num_attention_heads // config.num_key_value_heads
|
| 326 |
self.head_dim = self.hidden_size // self.num_heads
|
| 327 |
self.is_gqa = (
|
| 328 |
self.num_key_value_heads is not None
|
|
|
|
| 375 |
freq_allocation=self.freq_allocation,
|
| 376 |
)
|
| 377 |
self.config = config
|
| 378 |
+
if self.config.use_flash_attention:
|
| 379 |
+
self.attn_func = self._flash_attention_wrapper
|
| 380 |
+
else:
|
| 381 |
+
self.attn_func = self.core_attn
|
| 382 |
|
| 383 |
def forward(
|
| 384 |
self,
|
|
|
|
| 451 |
)
|
| 452 |
return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
|
| 453 |
|
| 454 |
+
def _flash_attention_wrapper(
|
| 455 |
+
self,
|
| 456 |
+
q,
|
| 457 |
+
k,
|
| 458 |
+
v,
|
| 459 |
+
attention_mask=None,
|
| 460 |
+
attn_mask_start_row_indices=None,
|
| 461 |
+
seq_length=None,
|
| 462 |
+
):
|
| 463 |
+
"""Wrapper for flash attention implementation.
|
| 464 |
+
Args:
|
| 465 |
+
q (torch.Tensor): Query tensor
|
| 466 |
+
k (torch.Tensor): Key tensor
|
| 467 |
+
v (torch.Tensor): Value tensor
|
| 468 |
+
attention_mask (Optional[torch.Tensor]): Attention mask
|
| 469 |
+
attn_mask_start_row_indices (Optional[torch.Tensor]): Variable length indices
|
| 470 |
+
seq_length (Optional[int]): Sequence length
|
| 471 |
+
Returns:
|
| 472 |
+
Tuple[torch.Tensor, torch.Tensor]: Attention output and weights
|
| 473 |
+
"""
|
| 474 |
+
q = q.transpose(1, 2)
|
| 475 |
+
k = k.transpose(1, 2)
|
| 476 |
+
v = v.transpose(1, 2)
|
| 477 |
+
|
| 478 |
+
with sdpa_kernel(SDPBackend.FLASH_ATTENTION):
|
| 479 |
+
out = F.scaled_dot_product_attention(
|
| 480 |
+
q,
|
| 481 |
+
k,
|
| 482 |
+
v,
|
| 483 |
+
attn_mask=None,
|
| 484 |
+
dropout_p=self.config.attention_probs_dropout_prob,
|
| 485 |
+
is_causal=q.shape[-2] == k.shape[-2],
|
| 486 |
+
scale=1
|
| 487 |
+
/ (getattr(self.config, "scale_qk_coeff", 1.0) * self.head_dim**0.5),
|
| 488 |
+
enable_gqa=self.is_gqa,
|
| 489 |
+
)
|
| 490 |
+
out = out.transpose(1, 2)
|
| 491 |
+
out = out.contiguous().view(out.size(0), out.size(1), -1)
|
| 492 |
+
|
| 493 |
+
return out, None
|
| 494 |
+
|
| 495 |
def core_attn(
|
| 496 |
self,
|
| 497 |
q,
|
|
|
|
| 539 |
if getattr(self.config, "scale_qk_coeff", 1.0) != 1.0:
|
| 540 |
product = product * getattr(self.config, "scale_qk_coeff", 1.0)
|
| 541 |
|
| 542 |
+
seq_len = product.size(-1)
|
| 543 |
+
mask = torch.triu(
|
| 544 |
+
torch.ones((seq_len, seq_len), dtype=torch.bool, device=product.device),
|
| 545 |
+
diagonal=1,
|
| 546 |
+
)
|
| 547 |
+
product = product.masked_fill(mask, float("-inf"))
|
| 548 |
+
weights = F.softmax(product, dim=-1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 549 |
|
| 550 |
weights = weights.to(origin_dtype)
|
| 551 |
|
|
|
|
| 1548 |
)
|
| 1549 |
assert self.gate.config.moe_use_aux_free
|
| 1550 |
|
| 1551 |
+
self.world_size = 1
|
| 1552 |
+
self.rank = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1553 |
|
| 1554 |
self.multimodal_experts = (
|
| 1555 |
isinstance(moe_num_experts, (tuple, list)) and len(moe_num_experts) > 1
|
|
|
|
| 1634 |
S, H = x.shape
|
| 1635 |
E = gate_logits.shape[1]
|
| 1636 |
device = x.device
|
| 1637 |
+
if self.use_correction_bias:
|
| 1638 |
+
_, topk_idx = torch.topk(gate_logits + self.moe_statics.e_score_correction_bias[0].detach().to(gate_logits.device), k, dim=-1)
|
| 1639 |
+
topk_prob = torch.gather(gate_logits, dim=1, index=topk_idx) # [Seq, k]
|
| 1640 |
+
else:
|
| 1641 |
+
topk_prob, topk_idx = torch.topk(gate_logits, k, dim=-1) # [S, k]
|
| 1642 |
combine_weights = topk_prob # [S, k]
|
| 1643 |
expert_id = topk_idx # [S, k]
|
| 1644 |
y = x.new_zeros((E, capacity, H)) # [E, C, H]
|
|
|
|
| 1839 |
enable_reverse_token_drop=False,
|
| 1840 |
all_to_all_dropout=0,
|
| 1841 |
group_experts=False,
|
| 1842 |
+
use_expert_out_alltoall=True,
|
| 1843 |
use_expert_alltoall_overlap=False,
|
| 1844 |
use_padding=True,
|
| 1845 |
dense_token_type=3, # considerd as dense tokens (no moe)
|
|
|
|
| 2765 |
config_class = Ernie4_5_MoEConfig
|
| 2766 |
base_model_prefix = "ernie"
|
| 2767 |
_no_split_modules = ["Ernie4_5_DecoderLayer"]
|
|
|
|
| 2768 |
|
| 2769 |
|
| 2770 |
class Ernie4_5_Model(Ernie4_5_PretrainedModel):
|
|
|
|
| 2911 |
past_key_value = (
|
| 2912 |
past_key_values[idx] if past_key_values is not None else None
|
| 2913 |
)
|
|
|
|
| 2914 |
layer_outputs = decoder_layer(
|
| 2915 |
hidden_states,
|
| 2916 |
attention_mask,
|
|
|
|
| 3258 |
"""
|
| 3259 |
return self.model
|
| 3260 |
|
| 3261 |
+
# @staticmethod
|
| 3262 |
+
def _update_model_kwargs_for_generation(self, outputs, model_kwargs, is_encoder_decoder=False):
|
| 3263 |
+
"""
|
| 3264 |
+
Updates model kwargs for generation.
|
| 3265 |
+
|
| 3266 |
+
Args:
|
| 3267 |
+
outputs (Any): Model outputs.
|
| 3268 |
+
model_kwargs (dict): Current model kwargs.
|
| 3269 |
+
is_encoder_decoder (bool): Whether using encoder-decoder architecture.
|
| 3270 |
+
|
| 3271 |
+
Returns:
|
| 3272 |
+
dict: Updated model kwargs.
|
| 3273 |
+
"""
|
| 3274 |
+
# update cache
|
| 3275 |
+
if isinstance(outputs, tuple) and len(outputs) > 1 and not isinstance(outputs[1], torch.Tensor):
|
| 3276 |
+
model_kwargs["past_key_values"] = outputs[1]
|
| 3277 |
+
|
| 3278 |
+
if isinstance(outputs, CausalLMOutputWithCrossAttentions) and "past_key_values" in outputs:
|
| 3279 |
+
model_kwargs["past_key_values"] = outputs.past_key_values
|
| 3280 |
+
|
| 3281 |
+
# update token_type_ids with last value
|
| 3282 |
+
if "token_type_ids" in model_kwargs and model_kwargs["token_type_ids"] is not None:
|
| 3283 |
+
token_type_ids = model_kwargs["token_type_ids"]
|
| 3284 |
+
model_kwargs["token_type_ids"] = torch.cat([token_type_ids, token_type_ids[:, -1:]], dim=-1)
|
| 3285 |
+
|
| 3286 |
+
if not is_encoder_decoder and model_kwargs.get("attention_mask", None) is not None:
|
| 3287 |
+
# update attention mask
|
| 3288 |
+
attention_mask = model_kwargs["attention_mask"]
|
| 3289 |
+
model_kwargs["attention_mask"] = torch.cat(
|
| 3290 |
+
[
|
| 3291 |
+
attention_mask,
|
| 3292 |
+
torch.ones((attention_mask.shape[0], 1), dtype=torch.int64, device=attention_mask.device),
|
| 3293 |
+
],
|
| 3294 |
+
dim=-1,
|
| 3295 |
+
)
|
| 3296 |
+
|
| 3297 |
+
# update role_ids
|
| 3298 |
+
if "role_ids" in model_kwargs and model_kwargs["role_ids"] is not None:
|
| 3299 |
+
role_ids = model_kwargs["role_ids"]
|
| 3300 |
+
model_kwargs["role_ids"] = torch.cat([role_ids, role_ids[:, -1:]], dim=-1)
|
| 3301 |
+
|
| 3302 |
+
if self.config.get('rope_3d', False):
|
| 3303 |
+
assert "position_ids" in model_kwargs, "position_ids must be provided if rope_3d is on"
|
| 3304 |
+
position_ids = model_kwargs["position_ids"]
|
| 3305 |
+
bsz = position_ids.shape[0]
|
| 3306 |
+
|
| 3307 |
+
max_position = position_ids.max(dim=1, keepdim=True)[0] # [batch_size, 1, hidden_dim]
|
| 3308 |
+
new_positions = max_position + 1
|
| 3309 |
+
|
| 3310 |
+
model_kwargs["position_ids"] = torch.cat(
|
| 3311 |
+
[position_ids, new_positions],
|
| 3312 |
+
dim=1
|
| 3313 |
+
)
|
| 3314 |
+
|
| 3315 |
+
return model_kwargs
|
| 3316 |
|
| 3317 |
|
| 3318 |
class VisionMlp(nn.Module):
|
|
|
|
| 3461 |
k = apply_rotary_pos_emb_vision(k.unsqueeze(dim=0), rotary_pos_emb).squeeze(
|
| 3462 |
dim=0
|
| 3463 |
)
|
| 3464 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3465 |
q = q.transpose(0, 1)
|
| 3466 |
k = k.transpose(0, 1)
|
| 3467 |
v = v.transpose(0, 1)
|
| 3468 |
+
|
| 3469 |
+
lengths = cu_seqlens[1:] - cu_seqlens[:-1]
|
| 3470 |
+
splits = [
|
| 3471 |
+
torch.split(tensor, lengths.tolist(), dim=1) for tensor in (q, k, v)
|
| 3472 |
+
]
|
| 3473 |
+
|
| 3474 |
+
attn_output = []
|
| 3475 |
+
for q, k, v in zip(*splits):
|
| 3476 |
+
attn_weights = torch.matmul(q, k.transpose(1, 2)) / math.sqrt(self.head_dim)
|
| 3477 |
+
attn_weights = nn.functional.softmax(
|
| 3478 |
+
attn_weights, dim=-1, dtype=torch.float32
|
| 3479 |
+
).to(q.dtype)
|
| 3480 |
+
attn_output_splited = torch.matmul(attn_weights, v)
|
| 3481 |
+
attn_output_splited = attn_output_splited.transpose(0, 1)
|
| 3482 |
+
attn_output.append(attn_output_splited)
|
| 3483 |
+
attn_output = torch.cat(attn_output, dim=0)
|
| 3484 |
+
attn_output = attn_output.reshape(seq_length, -1).contiguous()
|
| 3485 |
attn_output = self.proj(attn_output)
|
| 3486 |
return attn_output
|
| 3487 |
|
|
|
|
| 4017 |
image_type_ids[:, -1:] if image_type_ids is not None else None
|
| 4018 |
)
|
| 4019 |
|
| 4020 |
+
if self.config.use_flash_attention:
|
| 4021 |
+
attention_mask = None
|
| 4022 |
+
else:
|
| 4023 |
+
attention_mask = kwargs.get("attention_mask", None)
|
| 4024 |
|
| 4025 |
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
|
| 4026 |
if inputs_embeds is not None and past_key_values is None:
|
|
|
|
| 4154 |
|
| 4155 |
if images is not None and image_features is not None:
|
| 4156 |
inputs_embeds = self.vision_mapping_forward(
|
| 4157 |
+
token_type_ids[..., :-1],
|
| 4158 |
token_type_ids_w_video,
|
| 4159 |
input_ids,
|
| 4160 |
mm_input_ids,
|
|
|
|
| 4168 |
|
| 4169 |
outputs = self.model(
|
| 4170 |
position_ids=position_ids,
|
| 4171 |
+
attention_mask=attention_mask,
|
| 4172 |
token_type_ids=token_type_ids,
|
| 4173 |
inputs_embeds=inputs_embeds,
|
| 4174 |
use_cache=use_cache,
|
processing_ernie_45t_vl.py → processing_ernie4_5_vl.py
RENAMED
|
@@ -17,7 +17,6 @@
|
|
| 17 |
import copy
|
| 18 |
import io
|
| 19 |
import os
|
| 20 |
-
import re
|
| 21 |
import math
|
| 22 |
import random
|
| 23 |
import requests
|
|
@@ -28,14 +27,13 @@ import threading
|
|
| 28 |
import uuid
|
| 29 |
import decord
|
| 30 |
from shutil import copyfile
|
| 31 |
-
from typing import Dict, List, Optional, Tuple, Union
|
| 32 |
|
| 33 |
import numpy as np
|
| 34 |
import torch
|
| 35 |
from PIL import Image, ImageDraw, ImageFont
|
| 36 |
from PIL.ExifTags import TAGS
|
| 37 |
from collections import defaultdict
|
| 38 |
-
from typing import Any, Dict, List, Union
|
| 39 |
from pathlib import Path
|
| 40 |
from tempfile import NamedTemporaryFile as ntf
|
| 41 |
|
|
@@ -52,7 +50,6 @@ from transformers.tokenization_utils_base import (
|
|
| 52 |
PaddingStrategy,
|
| 53 |
TextInput,
|
| 54 |
)
|
| 55 |
-
from transformers.utils import logging
|
| 56 |
from transformers.utils import TensorType, logging
|
| 57 |
from transformers.video_utils import VideoInput
|
| 58 |
from transformers.processing_utils import ProcessorMixin
|
|
@@ -82,132 +79,419 @@ from transformers.image_utils import (
|
|
| 82 |
logger = logging.get_logger(__name__)
|
| 83 |
|
| 84 |
|
| 85 |
-
|
| 86 |
-
"""
|
| 87 |
-
|
|
|
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
def
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
min_pixels: int = 4 * 28 * 28,
|
| 105 |
-
max_pixels: int = 16384 * 28 * 28,
|
| 106 |
-
):
|
| 107 |
-
"""
|
| 108 |
-
Rescales the image so that the following conditions are met:
|
| 109 |
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
if height > width:
|
| 119 |
-
new_width = max(factor, round_by_factor(width, factor))
|
| 120 |
-
new_height = floor_by_factor(new_width * MAX_RATIO, factor)
|
| 121 |
-
else:
|
| 122 |
-
new_height = max(factor, round_by_factor(height, factor))
|
| 123 |
-
new_width = floor_by_factor(new_height * MAX_RATIO, factor)
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
)
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
h_bar = floor_by_factor(height / beta, factor)
|
| 138 |
-
w_bar = floor_by_factor(width / beta, factor)
|
| 139 |
-
elif h_bar * w_bar < min_pixels:
|
| 140 |
-
beta = math.sqrt(min_pixels / (height * width))
|
| 141 |
-
h_bar = ceil_by_factor(height * beta, factor)
|
| 142 |
-
w_bar = ceil_by_factor(width * beta, factor)
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
-
|
|
|
|
|
|
|
| 148 |
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
-
def
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
"""
|
| 154 |
-
if image.dtype == np.uint8:
|
| 155 |
-
return False
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
and isinstance(images[0], (list, tuple))
|
| 175 |
-
and is_valid_image(images[0][0])
|
| 176 |
-
):
|
| 177 |
-
return [img for img_list in images for img in img_list]
|
| 178 |
|
| 179 |
-
|
| 180 |
-
|
|
|
|
| 181 |
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
-
|
| 189 |
-
def make_batched_videos(videos) -> List[VideoInput]:
|
| 190 |
-
"""dummy"""
|
| 191 |
-
if (
|
| 192 |
-
isinstance(videos, (list, tuple))
|
| 193 |
-
and isinstance(videos[0], (list, tuple))
|
| 194 |
-
and is_valid_image(videos[0][0])
|
| 195 |
-
):
|
| 196 |
-
return videos
|
| 197 |
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
|
| 204 |
-
|
| 205 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
raise ValueError(f"Could not make batched video from {videos}")
|
| 208 |
|
| 209 |
|
| 210 |
-
class
|
| 211 |
r"""
|
| 212 |
Constructs a adaptive image processor that dynamically resizes images based on the original images.
|
| 213 |
|
|
@@ -289,7 +573,7 @@ class Ernie_45T_VLImageProcessor(BaseImageProcessor):
|
|
| 289 |
isinstance(min_pixels, int) and min_pixels >= 0
|
| 290 |
), "min_pixels must be positive int"
|
| 291 |
logger.info(
|
| 292 |
-
f"{msg}
|
| 293 |
)
|
| 294 |
self.min_pixels = min_pixels
|
| 295 |
self.size["min_pixels"] = int(min_pixels)
|
|
@@ -298,7 +582,7 @@ class Ernie_45T_VLImageProcessor(BaseImageProcessor):
|
|
| 298 |
isinstance(max_pixels, int) and max_pixels > 0
|
| 299 |
), "max_pixels must be positive int"
|
| 300 |
logger.info(
|
| 301 |
-
f"{msg}
|
| 302 |
)
|
| 303 |
self.max_pixels = max_pixels
|
| 304 |
self.size["max_pixels"] = int(max_pixels)
|
|
@@ -618,298 +902,15 @@ class Ernie_45T_VLImageProcessor(BaseImageProcessor):
|
|
| 618 |
return BatchFeature(data=data, tensor_type=return_tensors)
|
| 619 |
|
| 620 |
|
| 621 |
-
class Ernie4_5_VLTokenizer(PreTrainedTokenizer):
|
| 622 |
-
"""
|
| 623 |
-
Ernie4_5_VLTokenizer
|
| 624 |
-
"""
|
| 625 |
-
|
| 626 |
-
vocab_files_names = {
|
| 627 |
-
"vocab_file": "tokenizer.model",
|
| 628 |
-
}
|
| 629 |
-
# Model input names expected by the tokenizer
|
| 630 |
-
model_input_names = ["input_ids", "position_ids", "attention_mask", "labels"]
|
| 631 |
-
# Padding side (where to add padding tokens)
|
| 632 |
-
padding_side = "right"
|
| 633 |
-
|
| 634 |
-
def __init__(
|
| 635 |
-
self,
|
| 636 |
-
vocab_file,
|
| 637 |
-
bos_token="<s>",
|
| 638 |
-
cls_token="<cls>",
|
| 639 |
-
eos_token="</s>",
|
| 640 |
-
mask_token="<mask:0>",
|
| 641 |
-
pad_token="<pad>",
|
| 642 |
-
sep_token="<sep>",
|
| 643 |
-
unk_token="<unk>",
|
| 644 |
-
additional_special_tokens=None,
|
| 645 |
-
**kwargs,
|
| 646 |
-
):
|
| 647 |
-
"""
|
| 648 |
-
Initialize the Ernie4_5_VLTokenizer
|
| 649 |
-
|
| 650 |
-
Args:
|
| 651 |
-
vocab_file (str): Path to the tokenizer vocabulary model.
|
| 652 |
-
bos_token (str, optional): The beginning of sequence token. Defaults to `"<s>"`.
|
| 653 |
-
cls_token (str, optional): The classifier token. Defaults to `"<cls>"`.
|
| 654 |
-
eos_token (str, optional): The end of sequence token. Defaults to `"</s>"`.
|
| 655 |
-
mask_token (str, optional): The masking token. Defaults to `"<mask:0>"`.
|
| 656 |
-
pad_token (str, optional): The padding token. Defaults to `"<pad>"`.
|
| 657 |
-
sep_token (str, optional): The separation token. Defaults to `"<sep>"`.
|
| 658 |
-
unk_token (str, optional): The unknown tokens symbol. Defaults to `"<unk>"`.
|
| 659 |
-
additional_special_tokens (List[str], optional): Additional special tokens to use.
|
| 660 |
-
Defaults to `["<mask:1>", "<mask:7>"]`.
|
| 661 |
-
**kwargs (dict): Additional keyword arguments passed along to the superclass.
|
| 662 |
-
"""
|
| 663 |
-
|
| 664 |
-
# Store vocabulary file path
|
| 665 |
-
self.vocab_file = vocab_file
|
| 666 |
-
# Initialize SentencePiece processor
|
| 667 |
-
self.sp_model = spm.SentencePieceProcessor()
|
| 668 |
-
# Load the vocabulary model
|
| 669 |
-
self.sp_model.Load(vocab_file)
|
| 670 |
-
|
| 671 |
-
# Set default additional special tokens if none provided
|
| 672 |
-
if additional_special_tokens is None:
|
| 673 |
-
additional_special_tokens = ["<mask:1>", "<mask:7>"]
|
| 674 |
-
super().__init__(
|
| 675 |
-
bos_token=bos_token,
|
| 676 |
-
cls_token=cls_token,
|
| 677 |
-
eos_token=eos_token,
|
| 678 |
-
mask_token=mask_token,
|
| 679 |
-
pad_token=pad_token,
|
| 680 |
-
sep_token=sep_token,
|
| 681 |
-
unk_token=unk_token,
|
| 682 |
-
additional_special_tokens=additional_special_tokens,
|
| 683 |
-
**kwargs,
|
| 684 |
-
)
|
| 685 |
-
|
| 686 |
-
@property
|
| 687 |
-
def space_token(self):
|
| 688 |
-
"""Return the space token"""
|
| 689 |
-
return "<mask:1>"
|
| 690 |
-
|
| 691 |
-
@property
|
| 692 |
-
def space_token_id(self):
|
| 693 |
-
"""Return the ID of the space token"""
|
| 694 |
-
return self.sp_model.piece_to_id("<mask:1>")
|
| 695 |
-
|
| 696 |
-
@property
|
| 697 |
-
def gend_token(self):
|
| 698 |
-
"""Return the gender token"""
|
| 699 |
-
return "<mask:7>"
|
| 700 |
-
|
| 701 |
-
@property
|
| 702 |
-
def gend_token_id(self):
|
| 703 |
-
"""Return the ID of the gender token"""
|
| 704 |
-
return self.sp_model.piece_to_id("<mask:7>")
|
| 705 |
-
|
| 706 |
-
@property
|
| 707 |
-
def im_start_id(self):
|
| 708 |
-
"""Return the ID of the image start token"""
|
| 709 |
-
return self.sp_model.piece_to_id("<|im_start|>")
|
| 710 |
-
|
| 711 |
-
@property
|
| 712 |
-
def im_end_id(self):
|
| 713 |
-
"""Return the ID of the image end token"""
|
| 714 |
-
return self.sp_model.piece_to_id("<|im_end|>")
|
| 715 |
-
|
| 716 |
-
@property
|
| 717 |
-
def vocab_size(self):
|
| 718 |
-
"""Return the size of the vocabulary"""
|
| 719 |
-
return self.sp_model.vocab_size()
|
| 720 |
-
|
| 721 |
-
def get_vocab(self):
|
| 722 |
-
"""Return the vocabulary as a dictionary mapping tokens to IDs"""
|
| 723 |
-
vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)}
|
| 724 |
-
vocab.update(self.added_tokens_encoder)
|
| 725 |
-
return vocab
|
| 726 |
-
|
| 727 |
-
def _tokenize(self, text):
|
| 728 |
-
"""Tokenize the input text into pieces"""
|
| 729 |
-
return self.sp_model.encode_as_pieces(text)
|
| 730 |
-
|
| 731 |
-
def _convert_token_to_id(self, token):
|
| 732 |
-
"""Convert a token to its corresponding ID"""
|
| 733 |
-
return self.sp_model.piece_to_id(token)
|
| 734 |
-
|
| 735 |
-
def _convert_id_to_token(self, id):
|
| 736 |
-
"""Convert an ID to its corresponding token"""
|
| 737 |
-
return self.sp_model.id_to_piece(id)
|
| 738 |
-
|
| 739 |
-
def convert_tokens_to_string(self, tokens):
|
| 740 |
-
"""Convert a sequence of tokens back to a string"""
|
| 741 |
-
current_sub_tokens = []
|
| 742 |
-
out_string = ""
|
| 743 |
-
|
| 744 |
-
for token in tokens:
|
| 745 |
-
# Handle special tokens differently
|
| 746 |
-
if token in self.all_special_tokens:
|
| 747 |
-
out_string += self.sp_model.decode(current_sub_tokens) + token
|
| 748 |
-
current_sub_tokens = []
|
| 749 |
-
else:
|
| 750 |
-
current_sub_tokens.append(token)
|
| 751 |
-
|
| 752 |
-
# Add any remaining sub-tokens
|
| 753 |
-
out_string += self.sp_model.decode(current_sub_tokens)
|
| 754 |
-
return out_string
|
| 755 |
-
|
| 756 |
-
def prepare_for_model(self, *args, **kwargs):
|
| 757 |
-
"""Prepare the tokenized inputs for the model"""
|
| 758 |
-
# Remove add_special_tokens if present (not supported)
|
| 759 |
-
if "add_special_tokens" in kwargs:
|
| 760 |
-
kwargs.pop("add_special_tokens")
|
| 761 |
-
return super().prepare_for_model(*args, **kwargs)
|
| 762 |
-
|
| 763 |
-
def save_vocabulary(
|
| 764 |
-
self, save_directory, filename_prefix: Optional[str] = None
|
| 765 |
-
) -> Tuple[str]:
|
| 766 |
-
"""
|
| 767 |
-
Save the vocabulary and special tokens file to a directory.
|
| 768 |
-
|
| 769 |
-
Args:
|
| 770 |
-
save_directory (`str`): The directory to save the vocabulary to
|
| 771 |
-
filename_prefix (`str`, optional): Prefix to add to the filename
|
| 772 |
-
|
| 773 |
-
Returns:
|
| 774 |
-
`Tuple(str)`: Paths to the saved files
|
| 775 |
-
"""
|
| 776 |
-
if not os.path.isdir(save_directory):
|
| 777 |
-
logger.error(f"Vocabulary path ({save_directory}) should be a directory")
|
| 778 |
-
return
|
| 779 |
-
|
| 780 |
-
# Construct output vocabulary file path
|
| 781 |
-
out_vocab_file = os.path.join(
|
| 782 |
-
save_directory,
|
| 783 |
-
(filename_prefix + "-" if filename_prefix else "")
|
| 784 |
-
+ self.vocab_files_names["vocab_file"],
|
| 785 |
-
)
|
| 786 |
-
|
| 787 |
-
# Copy or create vocabulary file
|
| 788 |
-
if os.path.abspath(self.vocab_file) != os.path.abspath(
|
| 789 |
-
out_vocab_file
|
| 790 |
-
) and os.path.isfile(self.vocab_file):
|
| 791 |
-
copyfile(self.vocab_file, out_vocab_file)
|
| 792 |
-
elif not os.path.isfile(self.vocab_file):
|
| 793 |
-
with open(out_vocab_file, "wb") as fi:
|
| 794 |
-
content_spiece_model = self.sp_model.serialized_model_proto()
|
| 795 |
-
fi.write(content_spiece_model)
|
| 796 |
-
|
| 797 |
-
return (out_vocab_file,)
|
| 798 |
-
|
| 799 |
-
def _decode(self, *args, **kwargs):
|
| 800 |
-
"""Decode token_id back to text"""
|
| 801 |
-
# Remove some parameters that aren't used
|
| 802 |
-
kwargs.pop("clean_up_tokenization_spaces", None)
|
| 803 |
-
kwargs.pop("spaces_between_special_tokens", None)
|
| 804 |
-
|
| 805 |
-
# Call parent decode method with specific parameters
|
| 806 |
-
return super()._decode(
|
| 807 |
-
*args,
|
| 808 |
-
**kwargs,
|
| 809 |
-
clean_up_tokenization_spaces=False,
|
| 810 |
-
spaces_between_special_tokens=False,
|
| 811 |
-
)
|
| 812 |
-
|
| 813 |
-
def _pad(
|
| 814 |
-
self,
|
| 815 |
-
encoded_inputs: Dict,
|
| 816 |
-
max_length: Optional[int] = None,
|
| 817 |
-
padding_strategy=PaddingStrategy.DO_NOT_PAD,
|
| 818 |
-
pad_to_multiple_of: Optional[int] = None,
|
| 819 |
-
return_attention_mask: Optional[bool] = None,
|
| 820 |
-
) -> dict:
|
| 821 |
-
"""Pad the encoded inputs to the specified length"""
|
| 822 |
-
if return_attention_mask is None:
|
| 823 |
-
return_attention_mask = "attention_mask" in self.model_input_names
|
| 824 |
-
if return_attention_mask:
|
| 825 |
-
required_input = encoded_inputs[self.model_input_names[0]]
|
| 826 |
-
if padding_strategy == PaddingStrategy.LONGEST:
|
| 827 |
-
max_length = len(required_input)
|
| 828 |
-
|
| 829 |
-
# Adjust max_length if needed for multiple of padding
|
| 830 |
-
if (
|
| 831 |
-
max_length is not None
|
| 832 |
-
and pad_to_multiple_of is not None
|
| 833 |
-
and (max_length % pad_to_multiple_of != 0)
|
| 834 |
-
):
|
| 835 |
-
max_length = (
|
| 836 |
-
(max_length // pad_to_multiple_of) + 1
|
| 837 |
-
) * pad_to_multiple_of
|
| 838 |
-
|
| 839 |
-
# Check if padding is needed
|
| 840 |
-
needs_to_be_padded = (
|
| 841 |
-
padding_strategy != PaddingStrategy.DO_NOT_PAD
|
| 842 |
-
and len(required_input) != max_length
|
| 843 |
-
)
|
| 844 |
-
|
| 845 |
-
# Handle attention mask if present
|
| 846 |
-
if (
|
| 847 |
-
"attention_mask" in encoded_inputs
|
| 848 |
-
and encoded_inputs["attention_mask"] is not None
|
| 849 |
-
):
|
| 850 |
-
attention_mask = encoded_inputs.pop("attention_mask")
|
| 851 |
-
if isinstance(attention_mask, torch.Tensor):
|
| 852 |
-
attention_mask = attention_mask.numpy()
|
| 853 |
-
elif isinstance(attention_mask, list):
|
| 854 |
-
attention_mask = np.array(attention_mask)
|
| 855 |
-
elif not isinstance(attention_mask, np.ndarray):
|
| 856 |
-
raise ValueError(
|
| 857 |
-
f"Unexpected type {type(attention_mask)} of attention_mask, "
|
| 858 |
-
)
|
| 859 |
-
else:
|
| 860 |
-
# Create default attention mask if none provided
|
| 861 |
-
attention_mask = np.tril(
|
| 862 |
-
np.ones((len(required_input), len(required_input)), dtype=np.int64)
|
| 863 |
-
)
|
| 864 |
-
attention_mask = np.expand_dims(attention_mask, axis=0)
|
| 865 |
-
|
| 866 |
-
# Perform padding if needed
|
| 867 |
-
if needs_to_be_padded:
|
| 868 |
-
difference = max_length - len(required_input)
|
| 869 |
-
if self.padding_side == "right":
|
| 870 |
-
if attention_mask.ndim == 1:
|
| 871 |
-
pad_width = [(0, difference)]
|
| 872 |
-
else:
|
| 873 |
-
pad_width = [(0, 0), (0, difference), (0, difference)]
|
| 874 |
-
elif self.padding_side == "left":
|
| 875 |
-
if attention_mask.ndim == 1:
|
| 876 |
-
pad_width = [(difference, 0)]
|
| 877 |
-
else:
|
| 878 |
-
pad_width = [(0, 0), (difference, 0), (difference, 0)]
|
| 879 |
-
else:
|
| 880 |
-
raise ValueError(
|
| 881 |
-
"Invalid padding strategy:" + str(self.padding_side)
|
| 882 |
-
)
|
| 883 |
-
|
| 884 |
-
attention_mask = np.pad(
|
| 885 |
-
attention_mask,
|
| 886 |
-
pad_width=pad_width,
|
| 887 |
-
mode="constant",
|
| 888 |
-
constant_values=0,
|
| 889 |
-
)
|
| 890 |
-
|
| 891 |
-
# Call parent padding method
|
| 892 |
-
encoded_inputs = super()._pad(
|
| 893 |
-
encoded_inputs,
|
| 894 |
-
max_length,
|
| 895 |
-
padding_strategy=padding_strategy,
|
| 896 |
-
pad_to_multiple_of=pad_to_multiple_of,
|
| 897 |
-
return_attention_mask=False,
|
| 898 |
-
)
|
| 899 |
-
|
| 900 |
-
# Add attention mask back if needed
|
| 901 |
-
if return_attention_mask:
|
| 902 |
-
encoded_inputs["attention_mask"] = attention_mask.tolist()
|
| 903 |
-
|
| 904 |
-
return encoded_inputs
|
| 905 |
-
|
| 906 |
-
|
| 907 |
RAW_VIDEO_DIR = "./download_tmp/raw_video/"
|
| 908 |
RAW_IMAGE_DIR = "./download_tmp/raw_images/"
|
| 909 |
EXTRACTED_FRAME_DIR = "./download_tmp/extracted_frames/"
|
| 910 |
TMP_DIR = "./download_tmp/upload_tmp/"
|
| 911 |
|
| 912 |
FONT_PATH = os.path.join(Path(__file__).parent.absolute(), "Roboto-Regular.ttf")
|
|
|
|
|
|
|
|
|
|
| 913 |
|
| 914 |
|
| 915 |
def is_gif(data: bytes) -> bool:
|
|
@@ -1380,7 +1381,7 @@ def render_frame_timestamp(frame, timestamp, font_rate=0.1):
|
|
| 1380 |
IDS_TYPE_FLAG = {"text": 0, "image": 1, "video": 2, "audio": 3}
|
| 1381 |
|
| 1382 |
|
| 1383 |
-
class
|
| 1384 |
"""
|
| 1385 |
Processes multimodal chat messages into model-ready inputs,
|
| 1386 |
handling text, images, and videos with 3D positional embeddings.
|
|
@@ -1527,11 +1528,11 @@ class Ernie_45T_VLProcessor(ProcessorMixin):
|
|
| 1527 |
|
| 1528 |
def __call__(
|
| 1529 |
self,
|
| 1530 |
-
text: List[str],
|
| 1531 |
-
images: List[Image.Image],
|
| 1532 |
-
videos: List[List[Image.Image]],
|
| 1533 |
**kwargs,
|
| 1534 |
-
) ->
|
| 1535 |
"""
|
| 1536 |
Convert chat messages into model inputs.
|
| 1537 |
Returns a dict with input_ids, token_type_ids, position_ids, images, grid_thw, image_type_ids, labels.
|
|
@@ -1547,6 +1548,9 @@ class Ernie_45T_VLProcessor(ProcessorMixin):
|
|
| 1547 |
"pic_cnt": 0,
|
| 1548 |
"video_cnt": 0,
|
| 1549 |
}
|
|
|
|
|
|
|
|
|
|
| 1550 |
texts = text[0]
|
| 1551 |
|
| 1552 |
new_video_seg = True
|
|
@@ -1811,4 +1815,4 @@ class Ernie_45T_VLProcessor(ProcessorMixin):
|
|
| 1811 |
return list(tokenizer_input_names) + list(image_processor_input_names)
|
| 1812 |
|
| 1813 |
|
| 1814 |
-
__all__ = ["
|
|
|
|
| 17 |
import copy
|
| 18 |
import io
|
| 19 |
import os
|
|
|
|
| 20 |
import math
|
| 21 |
import random
|
| 22 |
import requests
|
|
|
|
| 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
|
| 33 |
import torch
|
| 34 |
from PIL import Image, ImageDraw, ImageFont
|
| 35 |
from PIL.ExifTags import TAGS
|
| 36 |
from collections import defaultdict
|
|
|
|
| 37 |
from pathlib import Path
|
| 38 |
from tempfile import NamedTemporaryFile as ntf
|
| 39 |
|
|
|
|
| 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
|
| 372 |
+
|
| 373 |
+
|
| 374 |
+
def ceil_by_factor(number: int, factor: int) -> int:
|
| 375 |
+
"""Returns the smallest integer greater than or equal to 'number' that is divisible by 'factor'."""
|
| 376 |
+
return math.ceil(number / factor) * factor
|
| 377 |
+
|
| 378 |
+
|
| 379 |
+
def floor_by_factor(number: int, factor: int) -> int:
|
| 380 |
+
"""Returns the largest integer less than or equal to 'number' that is divisible by 'factor'."""
|
| 381 |
+
return math.floor(number / factor) * factor
|
| 382 |
+
|
| 383 |
+
|
| 384 |
+
def smart_resize(
|
| 385 |
+
height: int,
|
| 386 |
+
width: int,
|
| 387 |
+
factor: int = 28,
|
| 388 |
+
min_pixels: int = 4 * 28 * 28,
|
| 389 |
+
max_pixels: int = 16384 * 28 * 28,
|
| 390 |
+
):
|
| 391 |
+
"""
|
| 392 |
+
Rescales the image so that the following conditions are met:
|
| 393 |
+
|
| 394 |
+
1. Both dimensions (height and width) are divisible by 'factor'.
|
| 395 |
+
|
| 396 |
+
2. The total number of pixels is within the range ['min_pixels', 'max_pixels'].
|
| 397 |
+
|
| 398 |
+
3. The aspect ratio of the image is maintained as closely as possible.
|
| 399 |
+
"""
|
| 400 |
+
MAX_RATIO = 200
|
| 401 |
+
if max(height, width) / min(height, width) > MAX_RATIO:
|
| 402 |
+
if height > width:
|
| 403 |
+
new_width = max(factor, round_by_factor(width, factor))
|
| 404 |
+
new_height = floor_by_factor(new_width * MAX_RATIO, factor)
|
| 405 |
+
else:
|
| 406 |
+
new_height = max(factor, round_by_factor(height, factor))
|
| 407 |
+
new_width = floor_by_factor(new_height * MAX_RATIO, factor)
|
| 408 |
+
|
| 409 |
+
logger.info(
|
| 410 |
+
f"absolute aspect ratio must be smaller than {MAX_RATIO}, got {max(height, width) / min(height, width)},\
|
| 411 |
+
resize to {max(new_height, new_width) / min(new_height, new_width)}"
|
| 412 |
+
)
|
| 413 |
+
|
| 414 |
+
height = new_height
|
| 415 |
+
width = new_width
|
| 416 |
+
|
| 417 |
+
h_bar = max(factor, round_by_factor(height, factor))
|
| 418 |
+
w_bar = max(factor, round_by_factor(width, factor))
|
| 419 |
+
if h_bar * w_bar > max_pixels:
|
| 420 |
+
beta = math.sqrt((height * width) / max_pixels)
|
| 421 |
+
h_bar = floor_by_factor(height / beta, factor)
|
| 422 |
+
w_bar = floor_by_factor(width / beta, factor)
|
| 423 |
+
elif h_bar * w_bar < min_pixels:
|
| 424 |
+
beta = math.sqrt(min_pixels / (height * width))
|
| 425 |
+
h_bar = ceil_by_factor(height * beta, factor)
|
| 426 |
+
w_bar = ceil_by_factor(width * beta, factor)
|
| 427 |
+
|
| 428 |
+
if min_pixels > h_bar * w_bar or h_bar * w_bar > max_pixels:
|
| 429 |
+
raise ValueError(f"encounter invalid h_bar: {h_bar}, w_bar: {w_bar}")
|
| 430 |
+
|
| 431 |
+
return h_bar, w_bar
|
| 432 |
+
|
| 433 |
+
|
| 434 |
+
def is_scaled_image(image: np.ndarray) -> bool:
|
| 435 |
+
"""
|
| 436 |
+
Checks to see whether the pixel values have already been rescaled to [0, 1].
|
| 437 |
+
"""
|
| 438 |
+
if image.dtype == np.uint8:
|
| 439 |
+
return False
|
| 440 |
+
|
| 441 |
+
# It's possible the image has pixel values in [0, 255] but is of floating type
|
| 442 |
+
return np.min(image) >= 0 and np.max(image) <= 1
|
| 443 |
+
|
| 444 |
+
|
| 445 |
+
def make_batched_images(images) -> List[List[ImageInput]]:
|
| 446 |
+
"""
|
| 447 |
+
Accepts images in list or nested list format, and makes a list of images for preprocessing.
|
| 448 |
+
|
| 449 |
+
Args:
|
| 450 |
+
images (`Union[List[List[ImageInput]], List[ImageInput], ImageInput]`):
|
| 451 |
+
The input image.
|
| 452 |
+
|
| 453 |
+
Returns:
|
| 454 |
+
list: A list of images.
|
| 455 |
+
"""
|
| 456 |
+
if (
|
| 457 |
+
isinstance(images, (list, tuple))
|
| 458 |
+
and isinstance(images[0], (list, tuple))
|
| 459 |
+
and is_valid_image(images[0][0])
|
| 460 |
+
):
|
| 461 |
+
return [img for img_list in images for img in img_list]
|
| 462 |
+
|
| 463 |
+
elif isinstance(images, (list, tuple)) and is_valid_image(images[0]):
|
| 464 |
+
return images
|
| 465 |
+
|
| 466 |
+
elif is_valid_image(images):
|
| 467 |
+
return [images]
|
| 468 |
+
|
| 469 |
+
raise ValueError(f"Could not make batched images from {images}")
|
| 470 |
+
|
| 471 |
+
|
| 472 |
+
# Copied from transformers.models.llava_next_video.image_processing_llava_next_video.make_batched_videos
|
| 473 |
+
def make_batched_videos(videos) -> List[VideoInput]:
|
| 474 |
+
"""dummy"""
|
| 475 |
+
if (
|
| 476 |
+
isinstance(videos, (list, tuple))
|
| 477 |
+
and isinstance(videos[0], (list, tuple))
|
| 478 |
+
and is_valid_image(videos[0][0])
|
| 479 |
+
):
|
| 480 |
+
return videos
|
| 481 |
+
|
| 482 |
+
elif isinstance(videos, (list, tuple)) and is_valid_image(videos[0]):
|
| 483 |
+
if isinstance(videos[0], Image.Image):
|
| 484 |
+
return [videos]
|
| 485 |
+
elif len(videos[0].shape) == 4:
|
| 486 |
+
return [list(video) for video in videos]
|
| 487 |
+
|
| 488 |
+
elif is_valid_image(videos) and len(videos.shape) == 4:
|
| 489 |
+
return [list(videos)]
|
| 490 |
|
| 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)
|
|
|
|
| 902 |
return BatchFeature(data=data, tensor_type=return_tensors)
|
| 903 |
|
| 904 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 905 |
RAW_VIDEO_DIR = "./download_tmp/raw_video/"
|
| 906 |
RAW_IMAGE_DIR = "./download_tmp/raw_images/"
|
| 907 |
EXTRACTED_FRAME_DIR = "./download_tmp/extracted_frames/"
|
| 908 |
TMP_DIR = "./download_tmp/upload_tmp/"
|
| 909 |
|
| 910 |
FONT_PATH = os.path.join(Path(__file__).parent.absolute(), "Roboto-Regular.ttf")
|
| 911 |
+
if not os.path.exists(FONT_PATH):
|
| 912 |
+
ttf = requests.get("https://paddlenlp.bj.bcebos.com/vision-language-models/materials/Roboto-Regular.ttf")
|
| 913 |
+
open(FONT_PATH, "wb").write(ttf.content)
|
| 914 |
|
| 915 |
|
| 916 |
def is_gif(data: bytes) -> bool:
|
|
|
|
| 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 |
+
}
|