Skip to content

tests: drift detectors cover transformers 5.x (mirror unsloth PR #5423)#642

Merged
danielhanchen merged 1 commit into
mainfrom
fix/drift-detector-transformers-5-coverage
May 14, 2026
Merged

tests: drift detectors cover transformers 5.x (mirror unsloth PR #5423)#642
danielhanchen merged 1 commit into
mainfrom
fix/drift-detector-transformers-5-coverage

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

The two transformers-targeting drift detectors in tests/test_upstream_import_fixes_drift.py have predicates written against transformers 4.x only. On the Core matrix's HF=latest + TRL=latest cell (transformers>=5,<6) they fire DRIFT DETECTED even when the upstream / unsloth fix combination is healthy.

Companion PR on unsloth: unslothai/unsloth#5423 (merged) shipped the matching disable_torchcodec_if_broken change and the same two predicate relaxations on its side. This PR mirrors just the test changes onto zoo (zoo no longer carries the import_fixes source, only the drift detectors, since PR #639).

What changes

1. test_pretrained_model_enable_input_require_grads_uses_old_pattern

Fired whenever the source contains for module in self.modules(). But the unsloth replacement that patch_enable_input_require_grads installs deliberately ALSO uses that pattern, just wrapped in try / except NotImplementedError. The predicate could not distinguish broken upstream from the working patch. Accept either:

  • pre-HF#41993 shape (no self.modules() loop in the source), or
  • post-patch shape (loop + NotImplementedError handler in the source).

2. test_transformers_torchcodec_available_flag_is_present

Asserted the pre-5.x module-level _torchcodec_available flag. transformers 5 replaced it with an lru_cache'd is_torchcodec_available() callable. Accept either symbol; the patch site in unsloth/import_fixes.py now targets both (PR #5423).

Local verification

  • transformers 4.57.6 + trl 0.25.1 + peft 0.19.1 + triton 3.5.1 + vllm 0.15.1+cu130 (Core HF=4.57.6 cell shape): 18 passed.
  • transformers 5.8.1 + peft 0.19.1 + torch 2.9 CPU (no trl / vllm / datasets / xformers): 12 passed, 6 skipped on missing optional libs, 0 failed.

Test plan

  • pytest tests/test_upstream_import_fixes_drift.py is 18/18 on the HF=4.57.6 cell shape.
  • Same suite is 12 passed / 6 skipped / 0 failed on transformers 5.8.1 + minimal deps.
  • CI passes.

The two transformers-targeting drift detectors in tests/test_upstream_
import_fixes_drift.py had predicates written against transformers 4.x
only; on the Core matrix's HF=latest cell (transformers>=5,<6) they
fire DRIFT DETECTED even when the upstream / fix combination is healthy.

  * test_pretrained_model_enable_input_require_grads_uses_old_pattern
    fired whenever the source contains "for module in self.modules()".
    But the unsloth replacement that patch_enable_input_require_grads
    installs deliberately ALSO uses that pattern, just wrapped in
    try / except NotImplementedError. The predicate could not
    distinguish broken upstream from the working patch. Accept either
    pre-HF#41993 shape (no self.modules() loop) or the post-patch shape
    (loop + NotImplementedError handler).

  * test_transformers_torchcodec_available_flag_is_present asserted the
    pre-5.x module-level _torchcodec_available flag. transformers 5
    replaced it with an lru_cache'd is_torchcodec_available() callable.
    Accept either symbol -- the patch in unsloth/import_fixes.py now
    targets both (PR #5423 on unslothai/unsloth).

Local verification:

  * transformers 4.57.6 + trl 0.25.1 + peft 0.19.1 + triton 3.5.1 +
    vllm 0.15.1+cu130 (the Core HF=4.57.6 cell shape): 18 passed.
  * transformers 5.8.1 + peft 0.19.1 + torch 2.9 CPU (no trl / vllm /
    datasets / xformers installed): 12 passed, 6 skipped on missing
    optional libs, 0 failed.

The corresponding import_fixes.py changes already shipped on
unslothai/unsloth#5423. Zoo only carries the detectors so this PR is
test-only.
@danielhanchen danielhanchen merged commit 44cf46c into main May 14, 2026
10 of 13 checks passed
@danielhanchen danielhanchen deleted the fix/drift-detector-transformers-5-coverage branch May 14, 2026 12:19

@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 updates drift detection tests in tests/test_upstream_import_fixes_drift.py to better handle changes in the transformers library. Specifically, it refines the logic for detecting patches in enable_input_require_grads and updates the torchcodec availability check to support both legacy flags and newer public functions. The reviewer suggested making the string matching for the NotImplementedError patch more specific to avoid potential false positives from comments or docstrings.

Comment on lines +204 to +207
if "for module in self.modules()" not in src:
return # healthy: pre-HF#41993 shape
if "NotImplementedError" in src:
return # healthy: unsloth's tolerant replacement 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 check for "NotImplementedError" in the source code is a bit broad and could potentially be triggered by comments or docstrings in the original function. Using a more specific string like "except NotImplementedError" would make the detection of the applied patch more robust while remaining consistent with the project's use of string-matching for drift detection.

Suggested change
if "for module in self.modules()" not in src:
return # healthy: pre-HF#41993 shape
if "NotImplementedError" in src:
return # healthy: unsloth's tolerant replacement is installed
if "for module in self.modules()" not in src:
return # healthy: pre-HF#41993 shape
if "except NotImplementedError" in src:
return # healthy: unsloth's tolerant replacement is installed
References
  1. It is acceptable to use fragile string-matching for code patching if it is consistent with the existing codebase's architecture. This suggestion refines the string match to be more specific without changing the underlying architecture.

@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: 0bb0665005

ℹ️ 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 on lines +225 to +226
has_func = callable(getattr(tf_iu, "is_torchcodec_available", None))
assert has_flag or has_func, (

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 Check the torchcodec backend mapping too

On transformers 5.x, accepting the presence of is_torchcodec_available alone leaves this drift detector green even when disable_torchcodec_if_broken cannot actually disable torchcodec for requires_backends(..., "torchcodec"): transformers.utils.import_utils.BACKENDS_MAPPING["torchcodec"] is built at import time with the original checker function, so rebinding only is_torchcodec_available leaves that path reporting a broken torchcodec install as available. Please make this detector cover the cached backend mapping entry as part of the 5.x patch contract, otherwise the regression this test is meant to guard can still slip through.

Useful? React with 👍 / 👎.

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