vllm.renderers.hf
¶
Classes:
-
BaseHfRenderer–Shared tokenization and echo reconstruction for HF-family renderers.
-
HfRenderer– -
HfTokensPrompt–Tokenized HF prompt with metadata for rebuilding echoed text.
Functions:
-
build_video_prompts_from_mm_data–Build video prompts from vision_chunk data.
-
detokenize_prompt–Rebuild prompt text without tokenizer-added special tokens.
-
detokenize_prompt_async–Rebuild prompt text using the renderer's asynchronous decoder.
-
rebuild_mm_uuids_from_mm_data–Rebuild mm_uuids after vision_chunk processing.
-
tokenize_prompt–Tokenize text, preserving an optional mask for truncated echoes.
_CONTENT_FORMATS = dict[tuple[str | None, bool, str, str | None, str | None, bool], 'ChatTemplateContentFormat']()
module-attribute
¶
Used in _resolve_chat_template_content_format to avoid resolving and parsing
the chat template on every request.
lru_cache cannot be used because tools is a list and ModelConfig defines
__eq__ without __hash__, so the key holds the fields resolve_chat_template
selects the template by. Only the presence of tools is part of it: it decides
whether the processor template is tried, and its contents never reach the AST.
_PROCESSOR_CHAT_TEMPLATES = dict[tuple[str, str | None, str | None, bool], str | None]()
module-attribute
¶
Used in _try_get_processor_chat_template to avoid calling
cached_get_processor again if the processor fails to be loaded.
This is needed because lru_cache does not cache when an exception happens.
BaseHfRenderer
¶
HfRenderer
¶
Bases: BaseHfRenderer[HfTokenizer]
_apply_prompt_embeds_to_engine_input(engine_input, prompt_embeds_tensors, mm_updates)
¶
Augment engine_input in-place with a prompt_embeds modality.
Mixed mode: called after _process_multimodal has already run on the
pre-expanded token IDs (expansion was done in _process_tokens before
calling super()). Locates the already-expanded prompt_embeds spans
and adds prompt_embeds entries to mm_kwargs, mm_hashes, and
mm_placeholders.
_apply_prompt_embeds_to_prompt(prompt, prompt_embeds_tensors, placeholder_token_id)
staticmethod
¶
Mutate prompt from TokensPrompt to EmbedsPrompt shape.
Pure prompt_embeds path only (no other MM modalities). Expands
each <prompt_embeds> sentinel token into an N-token span and builds
the full-length prompt_embeds tensor + prompt_is_token_ids mask
that the engine's enable_prompt_embeds worker branch consumes.
_process_tokens(prompt, *, skip_mm_cache=False)
¶
Pre-expand prompt_embeds sentinels before delegating to the MM
processor, then attach prompt_embeds modality data to the result.
Mixed mode only: the _prompt_embeds stash is set by
render_messages when prompt_embeds co-exist with other MM data
(images, audio, …). We expand each 1-token sentinel to an N-token
span before calling super()._process_tokens() so the MM
processor records all placeholder offsets in the final (post-expansion)
coordinate space, no offset shifting needed afterwards.
_process_tokens_async(prompt, *, skip_mm_cache=False)
async
¶
Async equivalent of _process_tokens.
_render_with_timeout(coro)
async
¶
Run a template-rendering coroutine with a timeout.
On timeout, replaces the executor so subsequent renders are not permanently blocked by the stuck thread.
_replace_executor()
¶
Replace the inner render pool after a stuck worker.
Called after a render timeout so that the stuck thread (which CPython
cannot interrupt) does not permanently block all subsequent renders.
The executor object is kept so make_async wrappers bound at
init (tokenize, decode, pooling, derender) keep submitting work.
The old inner pool is abandoned with wait=False — its thread will
eventually finish or be reaped on process exit.
HfTokensPrompt
¶
Bases: TokensPrompt
Tokenized HF prompt with metadata for rebuilding echoed text.
Attributes:
-
prompt_special_tokens_mask(list[int]) –Encoding mask, sliced alongside prompt tokens before any padding.
prompt_special_tokens_mask
instance-attribute
¶
Encoding mask, sliced alongside prompt tokens before any padding.
_attr_dotted_name(node)
¶
Render an attribute-assignment target like ns.rest as a dotted name.
_build_mixed_prompt_embeds(token_ids, prompt_embeds_tensors, positions)
¶
Build the full-length prompt_embeds tensor and the is_token_ids
mask aligned to token_ids.
_build_prompt_embeds_positions(token_ids, num_tensors, mm_prompt_updates)
¶
Locate each prompt_embeds placeholder span in token_ids.
Expects token_ids to already contain expanded N-token spans.
Returns [(start_idx, length), ...] aligned with the tensors.
_build_prompt_embeds_updates(prompt_embeds_tensors, placeholder_token_id)
¶
Build MultiModalPromptUpdates for prompt_embeds expansion.
Each tensor produces a PromptReplacement that maps
[placeholder_token_id] -> [placeholder_token_id] x N
(where N = tensor.shape[0]).
_consolidate_system_messages(conversation)
¶
Merge all system messages into one at position 0.
Some chat templates (e.g. Qwen 3.6) require the system message to be the very first message. After developer-to-system conversion, system messages may appear at non-first positions; this merges them into a single message.
_ensure_prompt_embeds_placeholder_token(tokenizer)
¶
Register PROMPT_EMBEDS_PLACEHOLDER_TOKEN as a special token and return
its token ID.
_expand_prompt_embeds_placeholders(token_ids, mm_prompt_updates)
¶
Expand each 1-token prompt_embeds sentinel into an N-token span.
Uses apply_token_matches. Each single placeholder token in
token_ids is replaced with a consecutive span of
tensor.shape[0] copies, following tensors in order.
_filter_prompt_embeds_by_template(conversation, prompt_embeds_tensors, rendered)
¶
Drop embeddings whose placeholders are omitted by the chat template.
_iter_nodes_assign_content_item(root)
¶
Yield loops that iterate over message content or macro-bound content.
build_video_prompts_from_mm_data(mm_data)
¶
Build video prompts from vision_chunk data.
Collects prompts from video chunks and groups them by video_idx.
Parameters:
-
(mm_data¶MultiModalDataDict) –Processed multimodal data with vision_chunk items
Returns:
detokenize_prompt(tokenizer, prompt)
¶
Rebuild prompt text without tokenizer-added special tokens.
detokenize_prompt_async(decode, prompt)
async
¶
Rebuild prompt text using the renderer's asynchronous decoder.
rebuild_mm_uuids_from_mm_data(mm_uuids, mm_data)
¶
Rebuild mm_uuids after vision_chunk processing.
When videos are split into chunks, the original UUIDs need to be updated to reflect the new UUIDs generated for each chunk.
Parameters:
-
(mm_uuids¶MultiModalUUIDDict) –Original UUIDs dictionary
-
(mm_data¶MultiModalDataDict) –Processed multimodal data with vision_chunk items
Returns:
-
MultiModalUUIDDict–Updated UUIDs dictionary with chunk UUIDs
tokenize_prompt(tokenizer, prompt, params, *, want_offsets=False)
¶
Tokenize text, preserving an optional mask for truncated echoes.