[Bugfix] Fix RuntimeError: Already borrowed by adding thread-safe Hugging Face fast-tokenizer wrappers (#41181)

Signed-off-by: Yifan Zong <yzong@redhat.com>
Co-authored-by: wang.yuqi <yuqi.wang@daocloud.io>
This commit is contained in:
yzong-rh
2026-05-05 14:04:01 +00:00
committed by GitHub
co-authored by wang.yuqi
parent 6fca518157
commit 20dcd984f9
5 changed files with 107 additions and 35 deletions
+2 -11
View File
@@ -1,7 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import asyncio
import copy
import time
from abc import ABC, abstractmethod
from collections.abc import Mapping, Sequence
@@ -108,17 +107,10 @@ class BaseRenderer(ABC, Generic[_T]):
if mm_registry.supports_multimodal_inputs(config.model_config):
mm_processor_cache = mm_registry.processor_cache_from_config(config)
# Deep-copy the tokenizer so the multimodal processor gets its
# own Rust tokenizer backend. Without this, concurrent access
# from AsyncMicrobatchTokenizer and call_hf_processor causes
# "RuntimeError: Already borrowed" from the Rust RefCell.
# See: https://github.com/huggingface/tokenizers/issues/537
mm_tokenizer = copy.deepcopy(tokenizer)
with set_default_torch_num_threads():
self.mm_processor = mm_registry.create_processor(
config.model_config,
tokenizer=mm_tokenizer,
tokenizer=self.tokenizer,
cache=mm_processor_cache,
)
@@ -130,11 +122,10 @@ class BaseRenderer(ABC, Generic[_T]):
# requests don't pollute the sender cache.
ro_cache = mm_registry.processor_only_cache_from_config(config)
if ro_cache is not None:
ro_tokenizer = copy.deepcopy(tokenizer)
with set_default_torch_num_threads():
self._readonly_mm_processor = mm_registry.create_processor(
config.model_config,
tokenizer=ro_tokenizer,
tokenizer=self.tokenizer,
cache=ro_cache,
)
+15 -1
View File
@@ -2,6 +2,7 @@
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from __future__ import annotations
import copy
import inspect
import itertools
import weakref
@@ -42,7 +43,7 @@ from vllm.multimodal.processing.processor import (
apply_token_matches,
find_mm_placeholders,
)
from vllm.tokenizers.hf import HfTokenizer
from vllm.tokenizers.hf import HfTokenizer, maybe_make_thread_pool
from vllm.transformers_utils.chat_templates import get_chat_template_fallback_path
from vllm.transformers_utils.processor import cached_get_processor
from vllm.utils.async_utils import make_async
@@ -785,6 +786,14 @@ class HfRenderer(BaseRenderer[HfTokenizer]):
config: VllmConfig,
tokenizer: HfTokenizer | None,
) -> None:
# Ensure the og tokenizer is never modified by maybe_make_thread_pool
tokenizer = copy.copy(tokenizer)
if (
# Skip for mock configs and tokenizers
getattr(config.model_config, "enable_prompt_embeds", False)
and isinstance(tokenizer, HfTokenizer)
):
_ensure_prompt_embeds_placeholder_token(tokenizer)
super().__init__(config, tokenizer)
self.use_unified_vision_chunk = getattr(
@@ -795,6 +804,11 @@ class HfRenderer(BaseRenderer[HfTokenizer]):
safe_apply_chat_template, executor=self._executor
)
if self.tokenizer is not None:
maybe_make_thread_pool(
self.tokenizer, config.model_config.renderer_num_workers + 1
)
def render_messages(
self,
messages: list[ChatCompletionMessageParam],