69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
from typing import List, Union
|
|
|
|
from transformers.utils import logging
|
|
|
|
from sglang.srt.managers.multimodal_processor import (
|
|
BaseMultimodalProcessor as SGLangBaseProcessor,
|
|
)
|
|
from sglang.srt.managers.multimodal_processors.base_processor import (
|
|
MultimodalSpecialTokens,
|
|
)
|
|
from sglang.srt.managers.schedule_batch import Modality, MultimodalDataItem
|
|
from sglang.srt.models.gemma3_mm import Gemma3ForConditionalGeneration
|
|
|
|
# Copied from: https://github.com/huggingface/transformers/blob/main/src/transformers/models/gemma3/image_processing_gemma3_fast.py
|
|
# will be removed in the future
|
|
logger = logging.get_logger(__name__)
|
|
|
|
|
|
class Gemma3SGLangImageProcessor(SGLangBaseProcessor):
|
|
models = [Gemma3ForConditionalGeneration]
|
|
|
|
def __init__(self, hf_config, server_args, _processor):
|
|
super().__init__(hf_config, server_args, _processor)
|
|
self.IMAGE_TOKEN = "<start_of_image>"
|
|
self.IM_START_TOKEN_ID = hf_config.boi_token_index
|
|
self.IM_END_TOKEN_ID = hf_config.eoi_token_index
|
|
|
|
async def process_mm_data_async(
|
|
self,
|
|
image_data: List[Union[str, bytes]],
|
|
input_ids,
|
|
request_obj,
|
|
max_req_input_len,
|
|
*args,
|
|
**kwargs,
|
|
):
|
|
if not image_data:
|
|
return None
|
|
if isinstance(image_data, str):
|
|
image_data = [image_data]
|
|
|
|
image_token = self.IMAGE_TOKEN
|
|
base_output = self.load_mm_data(
|
|
prompt=input_ids,
|
|
image_data=image_data,
|
|
multimodal_tokens=MultimodalSpecialTokens(image_token=image_token),
|
|
max_req_input_len=max_req_input_len,
|
|
discard_alpha_channel=True,
|
|
)
|
|
|
|
ret = self.process_mm_data(
|
|
input_text=base_output.input_text, images=base_output.images
|
|
)
|
|
|
|
items = []
|
|
for i, image in enumerate(base_output.images):
|
|
item = MultimodalDataItem(
|
|
pixel_values=ret["pixel_values"][i],
|
|
modality=Modality.IMAGE,
|
|
)
|
|
items += [item]
|
|
|
|
return {
|
|
"mm_items": items,
|
|
"input_ids": ret["input_ids"].flatten().tolist(),
|
|
"im_start_id": self.IM_START_TOKEN_ID,
|
|
"im_end_id": self.IM_END_TOKEN_ID,
|
|
}
|