From b0b27c938331dcdf377947dca914870ac955f438 Mon Sep 17 00:00:00 2001 From: Saicharan Ramineni <84414237+GodlyDonuts@users.noreply.github.com> Date: Wed, 17 Jun 2026 20:25:41 -0400 Subject: [PATCH] Shim removed vllm.transformers_utils.tokenizer so fast_inference works on vLLM >= 0.22 (#6390) * Shim removed vllm.transformers_utils.tokenizer for older unsloth-zoo vLLM >= 0.22 (PR vllm-project/vllm#35024) deleted `vllm.transformers_utils.tokenizer`. Older unsloth-zoo patch_vllm_lora_tokenizer() does an unguarded `import vllm.transformers_utils.tokenizer`, crashing fast_inference with `No module named 'vllm.transformers_utils.tokenizer'`. Add fix_vllm_lora_tokenizer_module(): a meta path finder appended after the real finders that provides a no-op stub module only when vLLM no longer ships it. Registered in _gpu_init.py before vLLM is imported, so users who upgrade unsloth but keep an older unsloth-zoo are protected. Refs unslothai/unsloth#6385 * Shorten comments in fix_vllm_lora_tokenizer_module --------- Co-authored-by: Daniel Han --- unsloth/_gpu_init.py | 3 ++ unsloth/import_fixes.py | 69 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/unsloth/_gpu_init.py b/unsloth/_gpu_init.py index 7998d62191..917717e08e 100644 --- a/unsloth/_gpu_init.py +++ b/unsloth/_gpu_init.py @@ -169,6 +169,7 @@ from unsloth_zoo.device_type import ( from .import_fixes import ( fix_xformers_performance_issue, fix_vllm_aimv2_issue, + fix_vllm_lora_tokenizer_module, check_vllm_torch_sm100_compatibility, fix_vllm_guided_decoding_params, fix_vllm_pdl_blackwell, @@ -195,6 +196,7 @@ from .import_fixes import ( fix_xformers_performance_issue() fix_vllm_aimv2_issue() +fix_vllm_lora_tokenizer_module() # Check vLLM + torch < 2.9.0 + SM100 compatibility BEFORE importing vLLM check_vllm_torch_sm100_compatibility() fix_vllm_guided_decoding_params() @@ -224,6 +226,7 @@ patch_accelerate_recursively_apply() del fix_xformers_performance_issue del fix_vllm_aimv2_issue +del fix_vllm_lora_tokenizer_module del check_vllm_torch_sm100_compatibility del fix_vllm_guided_decoding_params del fix_trl_vllm_ascend diff --git a/unsloth/import_fixes.py b/unsloth/import_fixes.py index 87003b5985..ffbc24362f 100644 --- a/unsloth/import_fixes.py +++ b/unsloth/import_fixes.py @@ -419,6 +419,75 @@ def fix_vllm_aimv2_issue(): logger.info(f"Unsloth: Failed patching vLLM with error = {str(e)}") +# vLLM >= 0.22 (PR #35024) deleted `vllm.transformers_utils.tokenizer`, but an +# older unsloth_zoo still imports it unguarded and crashes (issue #6385). Supply +# a stub via a meta path finder appended AFTER the real finders, so it only +# activates when vLLM no longer ships the module. +_VLLM_LORA_TOKENIZER_MODULE = "vllm.transformers_utils.tokenizer" +_VLLM_TOKENIZER_STUB_SENTINEL = "__unsloth_vllm_tokenizer_stub__" + + +def _unsloth_return_no_lora_tokenizer(*args, **kwargs): + # None -> vLLM uses the base tokenizer for LoRA (matches unsloth_zoo). + return None + + +class _VllmLoraTokenizerStubLoader(importlib.abc.Loader): + __slots__ = ("module_name",) + + def __init__(self, module_name): + self.module_name = module_name + + def create_module(self, spec): + import types + + module = types.ModuleType(self.module_name) + module.__file__ = f"" + module.__package__ = self.module_name.rpartition(".")[0] + setattr(module, _VLLM_TOKENIZER_STUB_SENTINEL, True) + module.get_lora_tokenizer = _unsloth_return_no_lora_tokenizer + module.get_lora_tokenizer_async = _unsloth_return_no_lora_tokenizer + return module + + def exec_module(self, module): + return None + + +class _VllmLoraTokenizerStubFinder(importlib.abc.MetaPathFinder): + __slots__ = (_VLLM_TOKENIZER_STUB_SENTINEL,) + + def __init__(self): + setattr(self, _VLLM_TOKENIZER_STUB_SENTINEL, True) + + def find_spec( + self, + fullname, + path = None, + target = None, + ): + if fullname != _VLLM_LORA_TOKENIZER_MODULE: + return None + return importlib.machinery.ModuleSpec( + name = fullname, + loader = _VllmLoraTokenizerStubLoader(fullname), + is_package = False, + ) + + +def fix_vllm_lora_tokenizer_module(): + if importlib.util.find_spec("vllm") is None: + return + for finder in sys.meta_path: + if getattr(finder, _VLLM_TOKENIZER_STUB_SENTINEL, False): + return + # Appended, not inserted at 0, so a real module on older vLLM always wins. + sys.meta_path.append(_VllmLoraTokenizerStubFinder()) + logger.info( + "Unsloth: Installed `vllm.transformers_utils.tokenizer` compatibility " + "stub for newer vLLM versions" + ) + + def fix_vllm_guided_decoding_params(): def _maybe_raise_vllm_transformers_mismatch(error): error_text = str(error)