feat: optional caller-supplied mm_hashes on GenerateReqInput (#25300)

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: ishandhanani <82981111+ishandhanani@users.noreply.github.com>
This commit is contained in:
Kris Hung
2026-06-01 20:04:37 +02:00
committed by GitHub
co-authored by Claude Opus 4.7 ishandhanani
parent f6a5a1b59c
commit 86afa21ca7
4 changed files with 133 additions and 0 deletions
+11
View File
@@ -324,6 +324,8 @@ class Engine(EngineScoreMixin, EngineBase):
image_data: Optional[MultimodalDataInputFormat] = None,
audio_data: Optional[MultimodalDataInputFormat] = None,
video_data: Optional[MultimodalDataInputFormat] = None,
# See GenerateReqInput.mm_hashes / async_generate for the contract.
mm_hashes: Optional[Union[List[str], List[List[str]]]] = None,
return_logprob: Optional[Union[List[bool], bool]] = False,
logprob_start_len: Optional[Union[List[int], int]] = None,
top_logprobs_num: Optional[Union[List[int], int]] = None,
@@ -361,6 +363,7 @@ class Engine(EngineScoreMixin, EngineBase):
image_data=image_data,
audio_data=audio_data,
video_data=video_data,
mm_hashes=mm_hashes,
return_logprob=return_logprob,
logprob_start_len=logprob_start_len,
top_logprobs_num=top_logprobs_num,
@@ -416,6 +419,13 @@ class Engine(EngineScoreMixin, EngineBase):
image_data: Optional[MultimodalDataInputFormat] = None,
audio_data: Optional[MultimodalDataInputFormat] = None,
video_data: Optional[MultimodalDataInputFormat] = None,
# Optional per-image hashes the caller has already computed (hex strings,
# one per image in `image_data`). When supplied, each MultimodalDataItem's
# `hash` is initialised from this list and `set_pad_value` skips the
# internal `hash_feature()` recompute. Intended for external KV routers
# that compute their own per-image hash for routing decisions and need
# sglang's prefix-cache key to align. See GenerateReqInput.mm_hashes.
mm_hashes: Optional[Union[List[str], List[List[str]]]] = None,
return_logprob: Optional[Union[List[bool], bool]] = False,
logprob_start_len: Optional[Union[List[int], int]] = None,
top_logprobs_num: Optional[Union[List[int], int]] = None,
@@ -453,6 +463,7 @@ class Engine(EngineScoreMixin, EngineBase):
image_data=image_data,
audio_data=audio_data,
video_data=video_data,
mm_hashes=mm_hashes,
return_logprob=return_logprob,
logprob_start_len=logprob_start_len,
top_logprobs_num=top_logprobs_num,
+9
View File
@@ -158,6 +158,15 @@ class GenerateReqInput(BaseReq):
video_data: Optional[MultimodalDataInputFormat] = None
# The audio input. Like image data, it can be a file name, a url, or base64 encoded string.
audio_data: Optional[MultimodalDataInputFormat] = None
# Optional per-image hashes the caller has already computed (hex strings,
# one per image in `image_data`). When supplied, each MultimodalDataItem's
# `hash` is initialised from this list and `set_pad_value` skips the
# internal `hash_feature()` recompute, so the resulting `pad_value` is
# deterministic from the caller's hash. Intended for external KV routers
# that compute their own per-image hash for routing decisions and need
# sglang's prefix-cache key to align. When unset, behavior is unchanged
# (sglang hashes the processor feature tensor).
mm_hashes: Optional[Union[List[str], List[List[str]]]] = None
# Whether to extract and process audio from video inputs.
use_audio_in_video: bool = False
# The sampling_params. See descriptions below.
@@ -840,6 +840,37 @@ class TokenizerManager(TokenizerControlMixin, TokenizerManagerScoreMixin):
token_type_ids = mm_inputs.token_type_ids
if not isinstance(token_type_ids, list):
token_type_ids = token_type_ids.flatten().tolist()
# Caller-supplied per-image hashes (external KV routers, e.g.
# routing-aware orchestrators that compute a content-addressed
# hash before dispatch). Setting MultimodalDataItem.hash here
# short-circuits the internal hash_feature() recompute inside
# set_pad_value(), making the derived pad_value deterministic
# from the caller's hash. That alignment lets the router's
# routing decision agree with sglang's prefix-cache key for
# the same image. On any per-item parse error or list-length
# mismatch we fall back to the internal recompute so a
# malformed mm_hashes never blocks a request.
caller_mm_hashes = getattr(obj, "mm_hashes", None)
if caller_mm_hashes and mm_inputs and mm_inputs.mm_items:
if len(caller_mm_hashes) != len(mm_inputs.mm_items):
logger.warning(
"mm_hashes length (%d) != mm_items length (%d); "
"ignoring caller hashes for this request.",
len(caller_mm_hashes),
len(mm_inputs.mm_items),
)
else:
for item, hex_hash in zip(mm_inputs.mm_items, caller_mm_hashes):
if not isinstance(item, MultimodalDataItem):
continue
try:
item.hash = int(hex_hash, 16)
except (TypeError, ValueError):
logger.warning(
"Ignoring malformed mm_hashes entry %r; "
"this item will fall back to hash_feature().",
hex_hash,
)
if (
envs.SGLANG_MM_PRECOMPUTE_HASH.get()
and mm_inputs