94057c3d3e
PR Test (NPU) / check-changes (push) Has been cancelled
PR Test (NPU) / pr-gate (push) Has been cancelled
PR Test (NPU) / set-image-config (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-4-npu-a3 (push) Has been cancelled
PR Test (NPU) / stage-b-test-16-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-1-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-2-npu-a3 (push) Has been cancelled
PR Test (Arm64) / pr-gate (push) Has been cancelled
PR Test (Arm64) / check-changes (push) Has been cancelled
PR Test (Arm64) / build-test (push) Has been cancelled
PR Test (sgl-router) / gate (push) Has been cancelled
PR Test (sgl-router) / tier-1 — lint (push) Has been cancelled
PR Test (sgl-router) / tier-2 — build + test (push) Has been cancelled
PR Test (sgl-router) / tier-3 — docker (placeholder) (push) Has been cancelled
PR Test (sgl-router) / tier-3 — k8s integration (push) Has been cancelled
PR Test (sgl-router) / tier-3 — e2e (push) Has been cancelled
PR Test (sgl-router) / finish (push) Has been cancelled
PR Test (NPU) / single-node-poc (map[name:qwen3_6_27b_w8a8_1p_in64k_out1k_50ms runner:linux-aarch64-a3-2 test_case:test/registered/ascend/performance/qwen3_6_27b/test_npu_qwen3_6_27b_w8a8_1p_in64k_out1k_50ms.py test_type:perf]) (push) Has been cancelled
PR Test (NPU) / pr-test-npu-finish (push) Has been cancelled
PR Test (Xeon) / pr-gate (push) Has been cancelled
PR Test (Xeon) / check-changes (push) Has been cancelled
PR Test (Xeon) / build-test (, xeon-gnr, base-b-test-cpu) (push) Has been cancelled
PR Test (XPU) / check-changes (push) Has been cancelled
PR Test (XPU) / pr-gate (push) Has been cancelled
PR Test (XPU) / stage-a-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / wait-for-stage-a (push) Has been cancelled
PR Test (XPU) / stage-b-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / finish (push) Has been cancelled
CI Model Inventory / build-inventory (push) Has been cancelled
Lint / lint (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Compilation Check (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Manual Policy (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Request Processing (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Summary (push) Has been cancelled
PR Test (SMG) / build-wheel (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on windows (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (x86_64 - auto) (push) Has been cancelled
PR Test (SMG) / python-unit-tests (push) Has been cancelled
PR Test (SMG) / unit-tests (push) Has been cancelled
PR Test (SMG) / benchmarks (push) Has been cancelled
PR Test (SMG) / chat-completions (push) Has been cancelled
PR Test (SMG) / chat-completions-4gpu (push) Has been cancelled
PR Test (SMG) / e2e (push) Has been cancelled
PR Test (SMG) / docker-build-test (push) Has been cancelled
PR Test (SMG) / k8s-integration (push) Has been cancelled
PR Test (SMG) / finish (push) Has been cancelled
PR Test (SMG) / summarize-benchmarks (push) Has been cancelled
Release SGLang Model Gateway Docker Image / publish (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Build SDist (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Upload to PyPI (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (aarch64, 12.9, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (x86_64, 12.9, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu129 (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (aarch64, 13.0, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (x86_64, 13.0, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu130 (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 700) (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 720) (push) Has been cancelled
Release SGLang Kernels / release-rocm700 (push) Has been cancelled
Release SGLang Kernels / release-rocm720 (push) Has been cancelled
Release SGLang Kernels / build-musa43 (43, 3.10) (push) Has been cancelled
Release SGLang Kernels / release-musa43 (push) Has been cancelled
120 lines
4.3 KiB
Python
120 lines
4.3 KiB
Python
"""Standalone UNLIMITED-OCR processor."""
|
|
|
|
import hashlib
|
|
import logging
|
|
from typing import List, Union
|
|
|
|
import torch
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
from sglang.srt.managers.schedule_batch import MultimodalProcessorOutput
|
|
from sglang.srt.models.unlimited_ocr import UnlimitedOCRForCausalLM
|
|
from sglang.srt.multimodal.processors.base_processor import (
|
|
BaseMultimodalProcessor,
|
|
MultimodalSpecialTokens,
|
|
)
|
|
|
|
_IMAGE_MODE_PRESETS = {
|
|
"tiny": (512, 512, False),
|
|
"small": (640, 640, False),
|
|
"base": (1024, 1024, False),
|
|
"large": (1280, 1280, False),
|
|
"gundam": (1024, 640, True),
|
|
}
|
|
_DEFAULT_MODE = "gundam"
|
|
|
|
|
|
def _resolve_mode(images_config, num_images: int = 1) -> dict:
|
|
"""Return processor kwargs from images_config (or default)."""
|
|
mode = _DEFAULT_MODE
|
|
if images_config:
|
|
mode = images_config.get("image_mode", _DEFAULT_MODE)
|
|
key = mode.strip().lower()
|
|
preset = _IMAGE_MODE_PRESETS.get(key)
|
|
if preset is None:
|
|
logger.error(
|
|
f"Unknown image_mode '{mode}'. Supported: {', '.join(_IMAGE_MODE_PRESETS)}"
|
|
)
|
|
raise ValueError(
|
|
f"Unknown image_mode '{mode}'. "
|
|
f"Supported: {', '.join(_IMAGE_MODE_PRESETS)}"
|
|
)
|
|
_MULTI_IMAGE_ALLOWED = ("tiny", "small", "base")
|
|
base_size, image_size, crop_mode = preset
|
|
if num_images > 1 and key not in _MULTI_IMAGE_ALLOWED:
|
|
raise ValueError(
|
|
f"image_mode='{mode}' is not supported with multiple images "
|
|
f"(got {num_images} images). "
|
|
f"Please use one of: {list(_MULTI_IMAGE_ALLOWED)}"
|
|
)
|
|
return dict(zip(("base_size", "image_size", "crop_mode"), preset))
|
|
|
|
|
|
class UnlimitedOCRProcessor(BaseMultimodalProcessor):
|
|
"""Multimodal processor for UNLIMITED-OCR model."""
|
|
|
|
models = [UnlimitedOCRForCausalLM]
|
|
gpu_image_decode = False
|
|
|
|
def __init__(self, hf_config, server_args, _processor, *args, **kwargs):
|
|
"""Initialize UnlimitedOCRProcessor."""
|
|
super().__init__(hf_config, server_args, _processor, *args, **kwargs)
|
|
self.mm_tokens = MultimodalSpecialTokens(
|
|
image_token="<image>", image_token_id=self._processor.image_token_id
|
|
).build(_processor)
|
|
|
|
@staticmethod
|
|
def _mix_config_into_hash(mm_items, processor_kwargs):
|
|
"""Mix images_config into mm_item hashes so that different configs
|
|
produce different pad_values, avoiding radix/embedding cache collisions."""
|
|
from sglang.srt.managers.mm_utils import hash_feature
|
|
|
|
config_bytes = str(sorted(processor_kwargs.items())).encode()
|
|
for item in mm_items:
|
|
if item.feature is not None:
|
|
base_hash = hash_feature(item.feature)
|
|
elif item.precomputed_embeddings is not None:
|
|
base_hash = hash_feature(item.precomputed_embeddings)
|
|
else:
|
|
continue
|
|
combined = hashlib.sha256(
|
|
base_hash.to_bytes(8, byteorder="big") + config_bytes
|
|
).digest()[:8]
|
|
item.hash = int.from_bytes(combined, byteorder="big", signed=False)
|
|
|
|
async def process_mm_data_async(
|
|
self, image_data: List[Union[str, bytes]], input_text, *args, **kwargs
|
|
):
|
|
"""Process multimodal data asynchronously."""
|
|
request_obj = kwargs.get("request_obj")
|
|
images_config = (
|
|
getattr(request_obj, "images_config", None) if request_obj else None
|
|
)
|
|
processor_kwargs = _resolve_mode(images_config, num_images=len(image_data))
|
|
|
|
prefix = images_config.get("prefix", "") if images_config else ""
|
|
|
|
base_output = await self.load_mm_data(
|
|
prompt=input_text,
|
|
multimodal_tokens=self.mm_tokens,
|
|
image_data=image_data,
|
|
)
|
|
mm_items, input_ids, _ = self.process_and_combine_mm_data(
|
|
base_output, self.mm_tokens, **processor_kwargs
|
|
)
|
|
|
|
if prefix:
|
|
prefix_ids = self._tokenizer.encode(prefix, add_special_tokens=False)
|
|
input_ids = torch.cat(
|
|
[input_ids, torch.tensor(prefix_ids, dtype=input_ids.dtype)]
|
|
)
|
|
|
|
self._mix_config_into_hash(mm_items, processor_kwargs)
|
|
|
|
return MultimodalProcessorOutput(
|
|
mm_items=mm_items,
|
|
input_ids=input_ids.tolist(),
|
|
im_token_id=self.mm_tokens.image_token_id,
|
|
)
|