[Multimodal] Chunk and cache audio items whose offsets span several placeholder runs (#43301)

Co-authored-by: Hanming Lu <69857889+hanming-lu@users.noreply.github.com>
Co-authored-by: YavorGIvanov <YavorGIvanov@users.noreply.github.com>
This commit is contained in:
metamergebot
2026-10-09 10:57:01 -07:00
committed by GitHub
co-authored by Hanming Lu YavorGIvanov
parent 3bffe69b74
commit 87845cda2b
2 changed files with 293 additions and 94 deletions
+97 -81
View File
@@ -99,20 +99,19 @@ def _get_precomputed_embedding(
items_per_req = items[items_size[i] : items_size[i + 1]]
extend_len = extend_length[i] if i < len(extend_length) else 0
items_offset = items_offset_list[i]
if any(item.precomputed_embeddings is None for item in items_per_req):
chunk = None
else:
req_embeddings = torch.concat(
[item.precomputed_embeddings for item in items_per_req]
)
chunk, _, _ = get_embedding_chunk(
embedding=req_embeddings,
extend_prefix_len=prefix_length[i],
extend_seq_len=extend_len,
items_offset=items_offset,
)
chunk = [
get_embedding_chunk(
item.precomputed_embeddings,
prefix_length[i],
extend_len,
item.offsets,
)[0]
for item in items_per_req
]
if chunk is None and len(items_per_req) > 1:
return None
@@ -124,6 +123,9 @@ def _get_precomputed_embedding(
"MM inputs where only some items are precomputed."
)
precomputed_embeddings = [
tensor for chunks in precomputed_embeddings for tensor in chunks
]
# Normalize device across chunks before concat.
target_device = next(
(t.device for t in precomputed_embeddings if t.is_cuda),
@@ -133,7 +135,11 @@ def _get_precomputed_embedding(
t if t.device == target_device else t.to(target_device, non_blocking=True)
for t in precomputed_embeddings
]
result = torch.concat(precomputed_embeddings)
result = (
precomputed_embeddings[0]
if len(precomputed_embeddings) == 1
else torch.concat(precomputed_embeddings)
)
# some models embedding is 3-dim, reshape it to 2-dim (similar to get_embedding_chunk)
result = result.reshape(-1, result.shape[-1])
return result
@@ -257,6 +263,28 @@ def _acknowledge_deferred_cuda_ipc_cache_hits(
item.acknowledge_deferred_cuda_ipc_feature(consumer_count)
def _item_overlap(
item: MultimodalDataItem, chunk_start: int, chunk_end: int
) -> Optional[int]:
"""Token count of an item with several runs, or None if none intersects the chunk."""
if len(item.offsets) == 1:
start, end = item.offsets[0]
return end - start + 1 if end >= chunk_start and start < chunk_end else None
if any(end >= chunk_start and start < chunk_end for start, end in item.offsets):
return sum(end - start + 1 for start, end in item.offsets)
return None
def _slice_item_chunk(
emb: torch.Tensor, item: MultimodalDataItem, chunk_start: int, chunk_len: int
) -> torch.Tensor:
if len(item.offsets) == 1:
start, end = item.offsets[0]
local_start = max(start, chunk_start) - start
return emb[local_start : min(end + 1, chunk_start + chunk_len) - start]
return get_embedding_chunk(emb, chunk_start, chunk_len, item.offsets)[0]
def _get_chunked_embedding_full(
data_embedding_func: DataEmbeddingFunc,
embedding_items_per_req: List[MultimodalDataItem],
@@ -335,9 +363,7 @@ class PerImageRequestInfo:
items_offset: List[Tuple[int, int]]
extend_prefix_len: int
extend_seq_len: int
overlapping: List[Tuple[int, MultimodalDataItem, int, int]] = field(
default_factory=list
)
overlapping: List[Tuple[int, MultimodalDataItem, int]] = field(default_factory=list)
def _batch_encode_per_image_misses(
@@ -364,15 +390,13 @@ def _batch_encode_per_image_misses(
chunk_end = chunk_start + req_info.extend_seq_len # exclusive
overlapping = []
if req_info.extend_seq_len > 0:
for idx, (item, (start, end)) in enumerate(
zip(req_info.items, req_info.items_offset)
):
if end >= chunk_start and start < chunk_end:
overlapping.append((idx, item, start, end))
for idx, item in enumerate(req_info.items):
token_count = _item_overlap(item, chunk_start, chunk_end)
if token_count is not None:
overlapping.append((idx, item, token_count))
req_info.overlapping = overlapping
for _idx, item, start, end in overlapping:
expected_token_count = end - start + 1
for _idx, item, expected_token_count in overlapping:
cache_key = (item.hash, expected_token_count)
if cache_key in hash_to_embedding:
continue
@@ -389,8 +413,8 @@ def _batch_encode_per_image_misses(
unique_misses[cache_key] = (item, expected_token_count)
elif cache_key not in unique_misses:
if (
start >= chunk_start
and end < chunk_end
item.offsets[0][0] >= chunk_start
and item.offsets[-1][1] < chunk_end
and item.can_defer_cuda_ipc_feature_reconstruction()
):
item.model_specific_data[BORROW_CUDA_IPC_FEATURE_KEY] = True
@@ -423,7 +447,10 @@ def _batch_encode_per_image_misses(
)
split_embeddings = torch.split(all_miss_embedding, token_counts, dim=0)
for cache_key, emb in zip(ordered_cache_keys, split_embeddings):
embedding_cache.set(cache_key[0], EmbeddingResult(embedding=emb))
if embedding_cache.set(cache_key[0], EmbeddingResult(embedding=emb)):
cached = embedding_cache.get_single(cache_key[0])
if _embedding_token_count(cached.embedding) == cache_key[1]:
emb = cached.embedding
# Keep a local ref (no extra GPU memory) so assembly never fails due to LRU eviction.
hash_to_embedding[cache_key] = emb
@@ -433,35 +460,32 @@ def _batch_encode_per_image_misses(
def _get_chunked_embedding_by_item(
data_embedding_func: DataEmbeddingFunc,
embedding_items_per_req: List[MultimodalDataItem],
items_offset: List[Tuple[int, int]],
extend_prefix_len: int,
extend_seq_len: int,
device: torch.device,
) -> Optional[torch.Tensor]:
) -> List[torch.Tensor]:
"""
Per-image chunk-aware encoding for one request.
Items must already be split per-image (each item has exactly one offset).
Return ordered item slices for one request's chunk.
Items must already be split per image; an audio item may span several runs.
"""
chunk_start = extend_prefix_len
chunk_end = extend_prefix_len + extend_seq_len # exclusive
if extend_seq_len <= 0:
return None
return []
overlapping = []
for idx, (item, (start, end)) in enumerate(
zip(embedding_items_per_req, items_offset)
):
if end >= chunk_start and start < chunk_end:
overlapping.append((idx, item, start, end))
for idx, item in enumerate(embedding_items_per_req):
token_count = _item_overlap(item, chunk_start, chunk_end)
if token_count is not None:
overlapping.append((idx, item, token_count))
if not overlapping:
return None
return []
cached_embeddings = {}
miss_items = []
for idx, item, start, end in overlapping:
expected_token_count = end - start + 1
for idx, item, expected_token_count in overlapping:
cached = embedding_cache.get_single(item.hash)
if cached is not None:
cached_embedding = cached.embedding
@@ -473,12 +497,12 @@ def _get_chunked_embedding_by_item(
_discard_mismatched_cached_embedding(
item.hash, expected_token_count, cached_token_count
)
miss_items.append((idx, item, start, end))
miss_items.append((idx, item, expected_token_count))
else:
miss_items.append((idx, item, start, end))
miss_items.append((idx, item, expected_token_count))
if miss_items:
miss_item_list = [item for _, item, _, _ in miss_items]
miss_item_list = [item for _, item, _ in miss_items]
_move_items_to_device(miss_item_list, device, data_embedding_func)
all_miss_embedding = data_embedding_func(miss_item_list)
@@ -498,52 +522,41 @@ def _get_chunked_embedding_by_item(
-1, all_miss_embedding.shape[-1]
)
# Split output by per-item token count
token_counts = [end - start + 1 for _, _, start, end in miss_items]
token_counts = [n for _, _, n in miss_items]
split_embeddings = torch.split(all_miss_embedding, token_counts, dim=0)
for (idx, item, _, _), emb in zip(miss_items, split_embeddings):
for (idx, item, expected_token_count), emb in zip(miss_items, split_embeddings):
if embedding_cache.set(item.hash, EmbeddingResult(embedding=emb)):
cached = embedding_cache.get_single(item.hash)
if _embedding_token_count(cached.embedding) == expected_token_count:
emb = cached.embedding
cached_embeddings[idx] = emb
embedding_cache.set(item.hash, EmbeddingResult(embedding=emb))
chunk_slices = []
for idx, _, start, end in overlapping:
emb = cached_embeddings[idx]
overlap_start = max(start, chunk_start)
overlap_end = min(end, chunk_end - 1) # inclusive
local_start = overlap_start - start
local_end = overlap_end - start + 1 # exclusive for slicing
chunk_slices.append(emb[local_start:local_end])
return torch.cat(chunk_slices, dim=0)
return [
_slice_item_chunk(cached_embeddings[idx], item, chunk_start, extend_seq_len)
for idx, item, _ in overlapping
]
def _assemble_per_image_chunk(
overlapping: List[Tuple[int, MultimodalDataItem, int, int]],
overlapping: List[Tuple[int, MultimodalDataItem, int]],
hash_to_embedding: Dict[Tuple[Optional[int], int], torch.Tensor],
extend_prefix_len: int,
extend_seq_len: int,
) -> Optional[torch.Tensor]:
) -> List[torch.Tensor]:
"""
Assemble the chunk embedding for one request from pre-computed embeddings.
Return ordered item slices for one request from pre-computed embeddings.
All overlapping items must already have their embeddings in hash_to_embedding.
"""
if not overlapping:
return None
chunk_start = extend_prefix_len
chunk_end = extend_prefix_len + extend_seq_len # exclusive
chunk_slices = []
for _idx, item, start, end in overlapping:
cache_key = (item.hash, end - start + 1)
emb = hash_to_embedding[cache_key] # shape: (end - start + 1, hidden)
overlap_start = max(start, chunk_start)
overlap_end = min(end, chunk_end - 1) # inclusive
local_start = overlap_start - start
local_end = overlap_end - start + 1 # exclusive for slicing
chunk_slices.append(emb[local_start:local_end])
return torch.cat(chunk_slices, dim=0)
return [
_slice_item_chunk(
hash_to_embedding[(item.hash, token_count)],
item,
extend_prefix_len,
extend_seq_len,
)
for _idx, item, token_count in overlapping
]
def _get_chunked_prefill_embedding(
@@ -593,21 +606,23 @@ def _get_chunked_prefill_embedding(
extend_seq_len=extend_seq_len,
)
is_per_image = all(len(item.offsets) == 1 for item in embedding_items_per_req)
# Audio items may span several runs (interleaved with transcript text).
is_per_image = all(
len(item.offsets) == 1 or item.is_audio()
for item in embedding_items_per_req
)
if is_per_image:
if _is_hip or _is_npu or _is_xpu:
# ROCm CI regressed with one large cross-request ViT batch; keep
# the previous per-request path on HIP/NPU/XPU while CUDA uses batching.
chunk = _get_chunked_embedding_by_item(
chunk_slices = _get_chunked_embedding_by_item(
data_embedding_func,
embedding_items_per_req,
items_offset,
extend_prefix_len,
extend_seq_len,
device,
)
if chunk is not None:
all_chunks.append((i, chunk))
all_chunks.extend((i, chunk) for chunk in chunk_slices)
else:
per_image_requests.append(req_info)
else:
@@ -622,14 +637,13 @@ def _get_chunked_prefill_embedding(
# Phase 2: assemble per-request chunks in original request order
for req_info in per_image_requests:
chunk = _assemble_per_image_chunk(
chunk_slices = _assemble_per_image_chunk(
req_info.overlapping,
hash_to_embedding,
req_info.extend_prefix_len,
req_info.extend_seq_len,
)
if chunk is not None:
all_chunks.append((req_info.req_idx, chunk))
all_chunks.extend((req_info.req_idx, chunk) for chunk in chunk_slices)
for req_info in full_path_requests:
chunk_embedding, input_ids = _get_chunked_embedding_full(
@@ -650,6 +664,8 @@ def _get_chunked_prefill_embedding(
if len(embedding_list) == 0:
return None, input_ids
if len(embedding_list) == 1:
return embedding_list[0], input_ids
return torch.concat(embedding_list, dim=0), input_ids