[Model] Add HunyuanOCR support (#29327)
Signed-off-by: manayang <jackmanayang@gmail.com> Signed-off-by: Isotr0py <mozf@mail2.sysu.edu.cn> Signed-off-by: Roger Wang <hey@rogerw.io> Co-authored-by: sergeywang <sergeywang@tencent.com> Co-authored-by: manayang <jackmanayang@gmail.com> Co-authored-by: manayang <manayang@tencent.com> Co-authored-by: Roger Wang <hey@rogerw.io>
This commit is contained in:
co-authored by
sergeywang
manayang
manayang
Roger Wang
parent
87185c88d5
commit
92effb07a4
@@ -43,6 +43,8 @@ class CachedRequestState:
|
||||
mrope_positions: torch.Tensor | None = None
|
||||
mrope_position_delta: int | None = None
|
||||
|
||||
xdrope_positions: torch.Tensor | None = None
|
||||
|
||||
lora_request: LoRARequest | None = None
|
||||
prompt_embeds: torch.Tensor | None = None
|
||||
|
||||
|
||||
@@ -50,16 +50,21 @@ from vllm.distributed.parallel_state import (
|
||||
from vllm.forward_context import BatchDescriptor, set_forward_context
|
||||
from vllm.logger import init_logger
|
||||
from vllm.model_executor.layers.attention_layer_base import AttentionLayerBase
|
||||
from vllm.model_executor.layers.rotary_embedding import MRotaryEmbedding
|
||||
from vllm.model_executor.layers.rotary_embedding import (
|
||||
MRotaryEmbedding,
|
||||
XDRotaryEmbedding,
|
||||
)
|
||||
from vllm.model_executor.model_loader import TensorizerLoader, get_model_loader
|
||||
from vllm.model_executor.models.interfaces import (
|
||||
SupportsMRoPE,
|
||||
SupportsMultiModal,
|
||||
SupportsXDRoPE,
|
||||
is_mixture_of_experts,
|
||||
supports_eagle3,
|
||||
supports_mrope,
|
||||
supports_multimodal_pruning,
|
||||
supports_transcription,
|
||||
supports_xdrope,
|
||||
)
|
||||
from vllm.model_executor.models.interfaces_base import (
|
||||
VllmModelForPooling,
|
||||
@@ -324,6 +329,7 @@ class GPUModelRunner(
|
||||
# Multi-modal data support
|
||||
self.mm_registry = MULTIMODAL_REGISTRY
|
||||
self.uses_mrope = model_config.uses_mrope
|
||||
self.uses_xdrope_dim = model_config.uses_xdrope_dim
|
||||
self.supports_mm_inputs = self.mm_registry.supports_multimodal_inputs(
|
||||
model_config
|
||||
)
|
||||
@@ -512,6 +518,13 @@ class GPUModelRunner(
|
||||
(3, self.max_num_tokens + 1), dtype=torch.int64
|
||||
)
|
||||
|
||||
# Only relevant for models using XD-RoPE (e.g, HunYuan-VL)
|
||||
if self.uses_xdrope_dim > 0:
|
||||
# Similar to mrope but use assigned dimension number for RoPE, 4 as default.
|
||||
self.xdrope_positions = self._make_buffer(
|
||||
(self.uses_xdrope_dim, self.max_num_tokens + 1), dtype=torch.int64
|
||||
)
|
||||
|
||||
# None in the first PP rank. The rest are set after load_model.
|
||||
self.intermediate_tensors: IntermediateTensors | None = None
|
||||
|
||||
@@ -593,10 +606,14 @@ class GPUModelRunner(
|
||||
if isinstance(num_tokens, int):
|
||||
if self.uses_mrope:
|
||||
return self.mrope_positions.gpu[:, :num_tokens]
|
||||
if self.uses_xdrope_dim > 0:
|
||||
return self.xdrope_positions.gpu[:, :num_tokens]
|
||||
return self.positions.gpu[:num_tokens]
|
||||
else:
|
||||
if self.uses_mrope:
|
||||
return self.mrope_positions.gpu[:, num_tokens]
|
||||
if self.uses_xdrope_dim > 0:
|
||||
return self.xdrope_positions.gpu[:, num_tokens]
|
||||
return self.positions.gpu[num_tokens]
|
||||
|
||||
def _make_buffer(
|
||||
@@ -772,6 +789,10 @@ class GPUModelRunner(
|
||||
if self.uses_mrope:
|
||||
self._init_mrope_positions(req_state)
|
||||
|
||||
# Only relevant for models using XD-RoPE (e.g, HunYuan-VL)
|
||||
if self.uses_xdrope_dim > 0:
|
||||
self._init_xdrope_positions(req_state)
|
||||
|
||||
reqs_to_add.append(req_state)
|
||||
|
||||
# Update the states of the running/resumed requests.
|
||||
@@ -987,6 +1008,19 @@ class GPUModelRunner(
|
||||
)
|
||||
)
|
||||
|
||||
def _init_xdrope_positions(self, req_state: CachedRequestState):
|
||||
model = self.get_model()
|
||||
xdrope_model = cast(SupportsXDRoPE, model)
|
||||
assert req_state.prompt_token_ids is not None, (
|
||||
"XD-RoPE requires prompt_token_ids to be available."
|
||||
)
|
||||
assert supports_xdrope(model), "XD-RoPE support is not implemented."
|
||||
|
||||
req_state.xdrope_positions = xdrope_model.get_xdrope_input_positions(
|
||||
req_state.prompt_token_ids,
|
||||
req_state.mm_features,
|
||||
)
|
||||
|
||||
def _extract_mm_kwargs(
|
||||
self,
|
||||
scheduler_output: "SchedulerOutput",
|
||||
@@ -1231,6 +1265,11 @@ class GPUModelRunner(
|
||||
if self.uses_mrope:
|
||||
self._calc_mrope_positions(scheduler_output)
|
||||
|
||||
# Calculate XD-RoPE positions.
|
||||
# Only relevant for models using XD-RoPE (e.g, HunYuan-VL)
|
||||
if self.uses_xdrope_dim > 0:
|
||||
self._calc_xdrope_positions(scheduler_output)
|
||||
|
||||
# Get token indices.
|
||||
# E.g., [0, 1, 0, 1, 2, 3, 4, 0, 1, 2]
|
||||
# -> [0, 1, M, M + 1, M + 2, M + 3, M + 4, 2 * M, 2 * M + 1, 2 * M + 2]
|
||||
@@ -1364,6 +1403,12 @@ class GPUModelRunner(
|
||||
self.mrope_positions.cpu[:, :total_num_scheduled_tokens],
|
||||
non_blocking=True,
|
||||
)
|
||||
elif self.uses_xdrope_dim > 0:
|
||||
# Only relevant for models using XD-RoPE (e.g, HunYuan-VL)
|
||||
self.xdrope_positions.gpu[:, :total_num_scheduled_tokens].copy_(
|
||||
self.xdrope_positions.cpu[:, :total_num_scheduled_tokens],
|
||||
non_blocking=True,
|
||||
)
|
||||
else:
|
||||
# Common case (1D positions)
|
||||
self.positions.copy_to_gpu(total_num_scheduled_tokens)
|
||||
@@ -1793,6 +1838,53 @@ class GPUModelRunner(
|
||||
|
||||
mrope_pos_ptr += completion_part_len
|
||||
|
||||
def _calc_xdrope_positions(self, scheduler_output: "SchedulerOutput"):
|
||||
xdrope_pos_ptr = 0
|
||||
for index, req_id in enumerate(self.input_batch.req_ids):
|
||||
req = self.requests[req_id]
|
||||
assert req.xdrope_positions is not None
|
||||
|
||||
num_computed_tokens = self.input_batch.num_computed_tokens_cpu[index]
|
||||
num_scheduled_tokens = scheduler_output.num_scheduled_tokens[req_id]
|
||||
num_prompt_tokens = length_from_prompt_token_ids_or_embeds(
|
||||
req.prompt_token_ids, req.prompt_embeds
|
||||
)
|
||||
|
||||
if num_computed_tokens + num_scheduled_tokens > num_prompt_tokens:
|
||||
prompt_part_len = max(0, num_prompt_tokens - num_computed_tokens)
|
||||
completion_part_len = max(0, num_scheduled_tokens - prompt_part_len)
|
||||
else:
|
||||
prompt_part_len = num_scheduled_tokens
|
||||
completion_part_len = 0
|
||||
|
||||
assert num_scheduled_tokens == prompt_part_len + completion_part_len
|
||||
|
||||
if prompt_part_len > 0:
|
||||
# prompt's xdrope_positions are pre-computed
|
||||
dst_start = xdrope_pos_ptr
|
||||
dst_end = xdrope_pos_ptr + prompt_part_len
|
||||
src_start = num_computed_tokens
|
||||
src_end = num_computed_tokens + prompt_part_len
|
||||
|
||||
self.xdrope_positions.cpu[:, dst_start:dst_end] = req.xdrope_positions[
|
||||
:, src_start:src_end
|
||||
]
|
||||
xdrope_pos_ptr += prompt_part_len
|
||||
|
||||
if completion_part_len > 0:
|
||||
# compute completion's xdrope_positions on-the-fly
|
||||
dst_start = xdrope_pos_ptr
|
||||
dst_end = xdrope_pos_ptr + completion_part_len
|
||||
|
||||
XDRotaryEmbedding.get_next_input_positions_tensor(
|
||||
out=self.xdrope_positions.np,
|
||||
out_offset=dst_start,
|
||||
context_len=num_computed_tokens + prompt_part_len,
|
||||
num_new_tokens=completion_part_len,
|
||||
)
|
||||
|
||||
xdrope_pos_ptr += completion_part_len
|
||||
|
||||
def _calc_spec_decode_metadata(
|
||||
self,
|
||||
num_draft_tokens: np.ndarray,
|
||||
@@ -2037,6 +2129,7 @@ class GPUModelRunner(
|
||||
|
||||
req_start_idx = 0
|
||||
should_sync_mrope_positions = False
|
||||
should_sync_xdrope_positions = False
|
||||
|
||||
for req_id in self.input_batch.req_ids:
|
||||
mm_embeds_req: list[torch.Tensor] = []
|
||||
@@ -2110,6 +2203,10 @@ class GPUModelRunner(
|
||||
self._calc_mrope_positions(scheduler_output)
|
||||
self.mrope_positions.copy_to_gpu(total_num_scheduled_tokens)
|
||||
|
||||
if should_sync_xdrope_positions:
|
||||
self._calc_xdrope_positions(scheduler_output)
|
||||
self.xdrope_positions.copy_to_gpu(total_num_scheduled_tokens)
|
||||
|
||||
return mm_embeds, is_mm_embed
|
||||
|
||||
def get_model(self) -> nn.Module:
|
||||
@@ -2384,8 +2481,11 @@ class GPUModelRunner(
|
||||
input_ids = self.input_ids.gpu[:num_input_tokens]
|
||||
inputs_embeds = None
|
||||
model_kwargs = self._init_model_kwargs(num_input_tokens)
|
||||
|
||||
if self.uses_mrope:
|
||||
positions = self.mrope_positions.gpu[:, :num_input_tokens]
|
||||
elif self.uses_xdrope_dim > 0:
|
||||
positions = self.xdrope_positions.gpu[:, :num_input_tokens]
|
||||
else:
|
||||
positions = self.positions.gpu[:num_input_tokens]
|
||||
|
||||
@@ -3824,6 +3924,8 @@ class GPUModelRunner(
|
||||
|
||||
if self.uses_mrope:
|
||||
positions = self.mrope_positions.gpu[:, :num_tokens_after_padding]
|
||||
elif self.uses_xdrope_dim > 0:
|
||||
positions = self.xdrope_positions.gpu[:, :num_tokens_after_padding]
|
||||
else:
|
||||
positions = self.positions.gpu[:num_tokens_after_padding]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user