Skip to content

remove unsloth_zoo/import_fixes.py: redundant with unsloth's#639

Merged
danielhanchen merged 3 commits into
mainfrom
chore/remove-zoo-import-fixes
May 14, 2026
Merged

remove unsloth_zoo/import_fixes.py: redundant with unsloth's#639
danielhanchen merged 3 commits into
mainfrom
chore/remove-zoo-import-fixes

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

PR #637 introduced unsloth_zoo/import_fixes.py containing seven fix_* / patch_* workarounds for upstream pathologies on triton / vllm / trl / peft / transformers / datasets / torchcodec. Six of those seven functions are byte-for-byte (or near-byte-for-byte) mirrors of identically-named functions already living on unsloth/import_fixes.py and run there at import unsloth time. The seventh, fix_peft_transformers_weight_conversion_import, is the only novel one; it is being ported to unsloth/import_fixes.py via the open PR unslothai/unsloth#5416.

Hosting the same patches on both packages adds drift surface without any operational benefit: unsloth_zoo/__init__.py already raises ImportError unless find_spec("unsloth") succeeds (the _SKIP_GPU_INIT branch), and at runtime import unsloth always runs before anything touches zoo. By the time we reach zoo init the fixes are already applied; running them a second time is a no-op when it works and a footgun (mirror divergence) when it doesn't.

Changes

  • Delete unsloth_zoo/import_fixes.py (973 lines).
  • unsloth_zoo/__init__.py: drop the from .import_fixes import apply_import_fixes block. Replace with a comment explaining why zoo no longer hosts these patches (and where they now live).
  • tests/conftest.py section 3: replace the file-path loader for unsloth_zoo.import_fixes with a guarded import unsloth so the CPU-free test harness still triggers unsloth/import_fixes.py and the drift detectors see the patched state. ImportError is swallowed so security-only test runs (no unsloth installed) keep passing.
  • tests/test_upstream_import_fixes_drift.py::test_triton_compiled_kernel_has_num_ctas_and_cluster_dims: relax the detector to accept either a class-level num_ctas (the zoo-side shape, no longer present) or an instance-level __init__ wrapper (unsloth's actual shape). The class-level attr was a stricter zoo-only stamping; the instance-level wrapper is what torch Inductor's make_launcher actually needs, and is what unsloth/import_fixes.py::fix_triton_compiled_kernel_missing_attrs installs.

Compatibility

Drift Detection

All drift detectors in tests/test_upstream_import_fixes_drift.py continue to fire on real upstream regressions. The tests already cite unsloth/import_fixes.py line numbers in their docstrings, so the rewire is consistent with their stated source of truth.

Test Plan

  • pytest tests/test_upstream_import_fixes_drift.py tests/test_upstream_pinned_symbols_*.py tests/test_zoo_history_regressions_deep.py tests/test_zoo_source_upstream_refs.py tests/security -q -> 226 passed, 5 skipped (matches baseline before the rewire).
  • python -c "import unsloth_zoo" still imports cleanly with the patches in place (verified transformers.conversion_mapping and transformers.core_model_loading stubs are installed in sys.modules after the import chain runs).
  • CI on the consolidated-tests-ci workflow.

Related

…l patch

unsloth/import_fixes.py::fix_triton_compiled_kernel_missing_attrs wraps
CompiledKernel.__init__ to inject num_ctas / cluster_dims per-instance.
The previous drift detector only accepted a class-level num_ctas (the
shape zoo's now-removed import_fixes used to install). The class-level
attr is unnecessary for the upstream pathology -- torch Inductor's
make_launcher only ever sees CompiledKernel instances, and unsloth's
__init__ wrapper covers them. Accept either shape so the detector
still surfaces real upstream drift without false-firing on the
instance-level fix shape.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request removes the redundant import_fixes.py module from unsloth_zoo, delegating the application of upstream drift fixes to the main unsloth package. The test suite and initialization logic are updated to reflect this change. A review comment correctly identified a potential AttributeError in the drift detection test when inspecting the __code__ attribute of a class's __init__ method without a null check, providing a fix to ensure graceful failure.

Comment on lines +435 to +442
if init is not None:
code = getattr(init, "__code__", None)
freevars = set(getattr(code, "co_freevars", ()) or ())
co_names = set(getattr(code, "co_names", ()) or ())
if "_orig_init" in freevars or {"num_ctas", "cluster_dims"}.issubset(
co_names
):
return # healthy: unsloth's __init__ wrapper is installed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The code currently accesses attributes on code without verifying that it is not None. If ck_cls.__init__ is a built-in function or a wrapper that does not expose __code__ (such as a slot wrapper like object.__init__), getattr(init, "__code__", None) will return None. This would cause an AttributeError on the subsequent lines when calling getattr(code, ...). Adding a guard for code ensures the test fails gracefully with the intended drift message instead of crashing with an unhandled exception during pytest collection or execution.

Suggested change
if init is not None:
code = getattr(init, "__code__", None)
freevars = set(getattr(code, "co_freevars", ()) or ())
co_names = set(getattr(code, "co_names", ()) or ())
if "_orig_init" in freevars or {"num_ctas", "cluster_dims"}.issubset(
co_names
):
return # healthy: unsloth's __init__ wrapper is installed
if init is not None:
code = getattr(init, "__code__", None)
if code is not None:
freevars = set(getattr(code, "co_freevars", ()) or ())
co_names = set(getattr(code, "co_names", ()) or ())
if "_orig_init" in freevars or {"num_ctas", "cluster_dims"}.issubset(
co_names
):
return # healthy: unsloth's __init__ wrapper is installed

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 90ba8b1167

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread unsloth_zoo/__init__.py
Comment on lines +97 to +101
# in ``unsloth/import_fixes.py`` and are applied at ``import unsloth`` time.
# ``unsloth_zoo`` cannot be imported standalone -- the GPU init path below
# requires ``find_spec("unsloth")`` to succeed -- so ``import unsloth`` has
# always already run by the time we get here, and the patches are in
# place. We therefore do NOT re-host or re-apply them at zoo import time.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Import unsloth before relying on its fixes

This assumes unsloth/import_fixes.py has already executed, but find_spec("unsloth") later in this file only proves the package is installed; it does not import it. A downstream user can still import unsloth_zoo directly before importing unsloth (and pyproject.toml also does not pin a minimum unsloth version containing the newly ported PEFT fix), so the deleted local apply_import_fixes() no longer installs the transformers conversion stubs or the triton/vLLM patches. On supported stacks such as peft 0.19.x with transformers 4.x, the next from peft.utils import transformers_weight_conversion can again raise ModuleNotFoundError instead of being repaired at zoo import time.

Useful? React with 👍 / 👎.

Pull unslothai/unsloth@main rather than PyPI so the matrix cells
exercise the latest patch chain (specifically PR #5416's stub-module
injection for peft.utils.transformers_weight_conversion, which the
drift detector needs). Reverts back to PyPI once the next unsloth
release ships.
@danielhanchen danielhanchen merged commit f4bb7bc into main May 14, 2026
11 of 14 checks passed
@danielhanchen danielhanchen deleted the chore/remove-zoo-import-fixes branch May 14, 2026 11:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant