Fix vllm ipykernel patch (#3907)

* Implement vLLM patch for notebook detection

Add patch for vLLM compatibility in notebook environments.

* Fix sys.stdout.fileno for vLLM compatibility

Patch sys.stdout.fileno for vLLM compatibility in notebooks.

* Add patch_vllm_for_notebooks to initialization

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Harden vLLM notebook stdout patch

* Use logger for vLLM notebook patch

* Clarify vLLM notebook patch log message

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: danielhanchen <danielhanchen@users.noreply.github.com>
This commit is contained in:
pluesclues 2026-01-20 00:04:27 -05:00 committed by GitHub
commit 9172be8cfc
2 changed files with 62 additions and 0 deletions

View file

@ -134,6 +134,7 @@ from .import_fixes import (
patch_enable_input_require_grads,
fix_openenv_no_vllm,
fix_executorch,
patch_vllm_for_notebooks,
)
fix_xformers_performance_issue()
@ -147,6 +148,7 @@ patch_datasets()
patch_enable_input_require_grads()
fix_openenv_no_vllm()
fix_executorch()
patch_vllm_for_notebooks()
del fix_xformers_performance_issue
del fix_vllm_aimv2_issue
@ -159,6 +161,7 @@ del patch_datasets
del patch_enable_input_require_grads
del fix_openenv_no_vllm
del fix_executorch
del patch_vllm_for_notebooks
# Torch 2.4 has including_emulation
if DEVICE_TYPE == "cuda":

View file

@ -204,6 +204,65 @@ def fix_xformers_performance_issue():
logger.info(f"Unsloth: Failed patching Xformers with error = {str(e)}")
def patch_vllm_for_notebooks():
import sys
ipython = None
try:
from IPython import get_ipython as _get_ipython
except Exception:
_get_ipython = None
if _get_ipython is not None:
try:
ipython = _get_ipython()
except Exception:
ipython = None
if ipython is None:
try:
import builtins
_get_ipython = getattr(builtins, "get_ipython", None)
if callable(_get_ipython):
ipython = _get_ipython()
except Exception:
ipython = None
if ipython is None:
return
try:
shell = ipython.__class__.__name__
is_notebook = shell == "ZMQInteractiveShell" or "google.colab" in str(
type(ipython)
)
except Exception:
return
if not is_notebook:
return
if not hasattr(sys.stdout, "fileno"):
return
needs_patch = False
try:
fd = sys.stdout.fileno()
if not isinstance(fd, int) or fd < 0:
needs_patch = True
except Exception:
needs_patch = True
if not needs_patch:
return
logger.info(
"Unsloth: Notebook detected - Patching sys.stdout.fileno for newer `vllm>=0.12.0` versions"
)
sys.stdout.fileno = lambda: 1
# ValueError: 'aimv2' is already used by a Transformers config, pick another name.
def fix_vllm_aimv2_issue():
spec = importlib.util.find_spec("vllm")