Image-Text-to-Text
Transformers
Safetensors
English
Chinese
ernie4_5_moe_vl
ERNIE4.5
conversational
custom_code
Instructions to use baidu/ERNIE-4.5-VL-28B-A3B-PT with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use baidu/ERNIE-4.5-VL-28B-A3B-PT with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="baidu/ERNIE-4.5-VL-28B-A3B-PT", trust_remote_code=True) messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForMultimodalLM model = AutoModelForMultimodalLM.from_pretrained("baidu/ERNIE-4.5-VL-28B-A3B-PT", trust_remote_code=True, device_map="auto") - Inference
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use baidu/ERNIE-4.5-VL-28B-A3B-PT with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "baidu/ERNIE-4.5-VL-28B-A3B-PT" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "baidu/ERNIE-4.5-VL-28B-A3B-PT", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/baidu/ERNIE-4.5-VL-28B-A3B-PT
- SGLang
How to use baidu/ERNIE-4.5-VL-28B-A3B-PT with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "baidu/ERNIE-4.5-VL-28B-A3B-PT" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "baidu/ERNIE-4.5-VL-28B-A3B-PT", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "baidu/ERNIE-4.5-VL-28B-A3B-PT" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "baidu/ERNIE-4.5-VL-28B-A3B-PT", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use baidu/ERNIE-4.5-VL-28B-A3B-PT with Docker Model Runner:
docker model run hf.co/baidu/ERNIE-4.5-VL-28B-A3B-PT
support vllm (#5)
Browse files- support vllm (316acb15bbd575436bd49b75342322a096416111)
- support vllm (72b8bbbd1d69137403b3fd99791baa095f45b4aa)
- Rename configuration_ernie_45t_vl.py to configuration_ernie4_5_vl.py (809266fed5474a641f7ce4456c359a26fb91204f)
- Rename modeling_ernie_45t_vl.py to modeling_ernie4_5_vl.py (29a6f7fcad8b6aac0efe04cf576fe42a1646c66d)
- Rename processing_ernie_45t_vl.py to processing_ernie4_5_vl.py (b75712804bb0a43ddc3da8013dee62fecb6f9a16)
- Update tokenizer_config.json (ef51e65acfaeec016ab12797a5511a708a402073)
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,
|
|
@@ -17,6 +17,8 @@
|
|
| 17 |
"hidden_size": 2560,
|
| 18 |
"intermediate_size": 12288,
|
| 19 |
"im_patch_id": 100295,
|
|
|
|
|
|
|
| 20 |
"max_position_embeddings": 131072,
|
| 21 |
"model_type": "ernie4_5_moe_vl",
|
| 22 |
"moe_capacity": [128, 128, 128],
|
|
@@ -44,6 +46,14 @@
|
|
| 44 |
"use_cache": true,
|
| 45 |
"use_rmsnorm": true,
|
| 46 |
"use_bias": false,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
"vision_config": {
|
| 48 |
"attn_implementation": "eager",
|
| 49 |
"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],
|
|
|
|
| 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,
|
|
|
|
| 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
|
File without changes
|
processing_ernie_45t_vl.py → processing_ernie4_5_vl.py
RENAMED
|
@@ -491,7 +491,7 @@ def make_batched_videos(videos) -> List[VideoInput]:
|
|
| 491 |
raise ValueError(f"Could not make batched video from {videos}")
|
| 492 |
|
| 493 |
|
| 494 |
-
class
|
| 495 |
r"""
|
| 496 |
Constructs a adaptive image processor that dynamically resizes images based on the original images.
|
| 497 |
|
|
@@ -573,7 +573,7 @@ class Ernie_45T_VLImageProcessor(BaseImageProcessor):
|
|
| 573 |
isinstance(min_pixels, int) and min_pixels >= 0
|
| 574 |
), "min_pixels must be positive int"
|
| 575 |
logger.info(
|
| 576 |
-
f"{msg}
|
| 577 |
)
|
| 578 |
self.min_pixels = min_pixels
|
| 579 |
self.size["min_pixels"] = int(min_pixels)
|
|
@@ -582,7 +582,7 @@ class Ernie_45T_VLImageProcessor(BaseImageProcessor):
|
|
| 582 |
isinstance(max_pixels, int) and max_pixels > 0
|
| 583 |
), "max_pixels must be positive int"
|
| 584 |
logger.info(
|
| 585 |
-
f"{msg}
|
| 586 |
)
|
| 587 |
self.max_pixels = max_pixels
|
| 588 |
self.size["max_pixels"] = int(max_pixels)
|
|
@@ -1381,7 +1381,7 @@ def render_frame_timestamp(frame, timestamp, font_rate=0.1):
|
|
| 1381 |
IDS_TYPE_FLAG = {"text": 0, "image": 1, "video": 2, "audio": 3}
|
| 1382 |
|
| 1383 |
|
| 1384 |
-
class
|
| 1385 |
"""
|
| 1386 |
Processes multimodal chat messages into model-ready inputs,
|
| 1387 |
handling text, images, and videos with 3D positional embeddings.
|
|
@@ -1528,11 +1528,11 @@ class Ernie_45T_VLProcessor(ProcessorMixin):
|
|
| 1528 |
|
| 1529 |
def __call__(
|
| 1530 |
self,
|
| 1531 |
-
text: List[str],
|
| 1532 |
-
images: List[Image.Image],
|
| 1533 |
-
videos: List[List[Image.Image]],
|
| 1534 |
**kwargs,
|
| 1535 |
-
) ->
|
| 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,6 +1548,9 @@ class Ernie_45T_VLProcessor(ProcessorMixin):
|
|
| 1548 |
"pic_cnt": 0,
|
| 1549 |
"video_cnt": 0,
|
| 1550 |
}
|
|
|
|
|
|
|
|
|
|
| 1551 |
texts = text[0]
|
| 1552 |
|
| 1553 |
new_video_seg = True
|
|
@@ -1812,4 +1815,4 @@ class Ernie_45T_VLProcessor(ProcessorMixin):
|
|
| 1812 |
return list(tokenizer_input_names) + list(image_processor_input_names)
|
| 1813 |
|
| 1814 |
|
| 1815 |
-
__all__ = ["Ernie4_5_VLTokenizer", "
|
|
|
|
| 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 |
}
|