API Perf: Replace pydantic per-element validation with C loop validation (#26355)

This commit is contained in:
Jialin Ouyang
2026-05-27 02:04:07 -07:00
committed by GitHub
parent d44584e8d8
commit 98bc6f3c22
4 changed files with 259 additions and 3 deletions
+10 -3
View File
@@ -25,9 +25,10 @@ from array import array
from collections import Counter
from dataclasses import dataclass, field
from enum import Enum
from typing import TYPE_CHECKING, Any, Dict, List, Literal, Optional, Union
from typing import TYPE_CHECKING, Annotated, Any, Dict, List, Literal, Optional, Union
import torch
from pydantic import PlainValidator
from sglang.srt.lora.lora_registry import LoRARef
from sglang.srt.managers.embed_types import PositionalEmbeds
@@ -40,6 +41,7 @@ from sglang.srt.observability.req_time_stats import (
)
from sglang.srt.sampling.sampling_params import SamplingParams
from sglang.srt.utils import ImageData, VideoData
from sglang.srt.utils.field_validators import validate_optional_list_i64_1d_2d
# Handle serialization of Image for pydantic
if TYPE_CHECKING:
@@ -136,8 +138,13 @@ MultimodalDataInputFormat = Union[
class GenerateReqInput(BaseReq):
# The input prompt. It can be a single prompt or a batch of prompts.
text: Optional[Union[List[str], str]] = None
# The token ids for text; one can specify either text or input_ids
input_ids: Optional[Union[List[List[int]], List[int]]] = None
# The token ids for text.
#
# Use C-loop validator to replace Pydantic per-element type check for efficiency.
input_ids: Annotated[
Optional[Union[List[List[int]], List[int]]],
PlainValidator(validate_optional_list_i64_1d_2d),
] = None
# The embeddings for input_ids; one can specify either text or input_ids or input_embeds.
input_embeds: Optional[Union[List[List[List[float]]], List[List[float]]]] = None
# The image input. It can be an image instance, file name, URL, or base64 encoded string.