Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 10 additions & 25 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,10 @@ def _install_device_type_stub(name: str) -> None:


# ---------------------------------------------------------------------------
# Apply unsloth-local upstream-drift fixes that need to run before pytest
# collects tests that import the affected third-party module directly.
#
# Specifically: ``from peft.utils import transformers_weight_conversion``
# blows up on (peft 0.19.x + transformers 4.x) because peft unconditionally
# imports two transformers-v5 submodules at module top. The production
# import path applies the stub-injection workaround via
# ``unsloth/_gpu_init.py``, but the GPU-free test harness above
# deliberately avoids triggering the full ``unsloth`` package init (which
# pulls in the CUDA / torch device chain). Load just the standalone
# import-fixes module by file path so drift detectors that probe peft
# see the same patched state a real unsloth install would.
# Apply the peft + transformers-4.x stub-injection fix before pytest collects
# tests that import peft.utils.transformers_weight_conversion. Production runs
# this via unsloth/_gpu_init.py, but the GPU-free harness above skips full
# package init, so we load just the standalone import-fixes module by path.
# ---------------------------------------------------------------------------


Expand All @@ -178,11 +170,9 @@ def _apply_unsloth_peft_import_fix_for_tests() -> None:
if mod_name in sys.modules:
mod = sys.modules[mod_name]
else:
# Submodule import requires SOME parent ``unsloth`` entry in
# sys.modules. Reuse one if a sibling conftest step already
# installed it (and don't pop in that case); otherwise install a
# bare skeleton and pop on the way out so subsequent
# ``import unsloth`` calls hit the real package init.
# Submodule import needs SOME parent ``unsloth`` entry; reuse or
# install a bare skeleton and pop on exit so later ``import unsloth``
# calls hit the real package init.
if "unsloth" not in sys.modules:
pkg = types.ModuleType("unsloth")
pkg.__path__ = list(pkg_spec.submodule_search_locations)
Expand Down Expand Up @@ -217,16 +207,11 @@ def _apply_unsloth_peft_import_fix_for_tests() -> None:
try:
fix()
except Exception:
# Individual fix is internally guarded; if the entry point itself
# blows up, don't take pytest collection down.
# Individual fix is internally guarded; don't take pytest down.
pass
finally:
# Drop our scratch skeleton so subsequent ``import unsloth``
# calls hit the real package init rather than our empty
# placeholder. The import-fixes module itself stays in
# sys.modules under ``unsloth.import_fixes`` -- python's import
# machinery is happy to find a submodule without an active
# parent entry.
# Drop scratch skeleton; import_fixes itself stays cached as
# ``unsloth.import_fixes`` without an active parent.
if _installed_skeleton:
sys.modules.pop("unsloth", None)

Expand Down
12 changes: 4 additions & 8 deletions unsloth/_gpu_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,10 @@
patch_torchcodec_audio_decoder()
disable_torchcodec_if_broken()
disable_broken_wandb()
# Must run BEFORE patch_peft_weight_converter_compatibility: on peft 0.19.x
# + transformers 4.x, ``from peft.utils import transformers_weight_conversion``
# raises ModuleNotFoundError because peft unconditionally imports
# ``transformers.conversion_mapping`` and ``transformers.core_model_loading``
# at module top, but neither exists on transformers <5. Stubbing those two
# submodules first lets the converter compat patch actually wrap
# ``build_peft_weight_mapping`` instead of silently no-opping in its bare
# ``except (ImportError, AttributeError): return``.
# Must run before patch_peft_weight_converter_compatibility -- stubs the
# transformers v5 submodules peft 0.19.x imports unconditionally, so the
# next patch can actually wrap build_peft_weight_mapping instead of being
# swallowed by its bare ImportError except.
fix_peft_transformers_weight_conversion_import()
patch_peft_weight_converter_compatibility()

Expand Down
Loading
Loading