Skip to content

Chunked mask test: unwrap every layer, including unsloth_zoo's mask wrapper - #13248

Merged
danielhanchen merged 2 commits into
mainfrom
ci-chunked-mask-fixture
Oct 10, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
ci-chunked-mask-fixture

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

Repo tests (CPU, rest) fails on every PR since unslothai/unsloth-zoo#1656 landed (for example #13219, #13222, #13230, #13231):

tests/test_chunked_mask_block_sequence_ids.py:77: in test_static_cache_generate_matches_dynamic
    with pytest.raises(TypeError, match = "block_sequence_ids"):
E   Failed: DID NOT RAISE TypeError

Bisected with transformers 5.17.0 (the CI pin): unsloth-zoo at 94e2429~1 gives 2 passed and 2 skipped, and at 94e2429 (#1656, "Keep the original signature on the patched mask builders") gives 1 failed. The unsloth commit makes no difference: a8ff841, 554eb6b and main all fail against zoo main.

Both products are correct. The test fixture is what broke:

  • conftest's import unsloth stacks two wrappers. Unsloth's block_sequence_ids fix wraps the stock create_chunked_causal_mask, and unsloth_zoo's patch_transformers_masks then wraps the fix. The zoo wrapper keeps the fix on _unsloth_original_create_chunked_causal_mask and sets no __wrapped__. LAYER_PATTERN_TO_MASK_FUNCTION_MAPPING["chunked_attention"] still points at the fix.
  • Before cannot open shared object file: No such file or directory #1656 the zoo wrapper's signature read as (*args, **kwargs), so _chunked_mask_rejects_block_sequence_ids returned False and these tests skipped.
  • cannot open shared object file: No such file or directory #1656 copies the real signature onto the wrapper, so the tests now run. But the unpatched fixture only unwrapped when the outermost function carried Unsloth's flag. The outermost function is the zoo wrapper, so the fixture changed nothing, generate still went through the fix, and the expected TypeError never came.

Llama-4 static-cache generate itself works under the stacked wrappers, so the product needs no change.

Fix

The fixture now follows the whole chain down to the stock transformers function: the outermost function, the zoo stash if present, then each Unsloth __wrapped__. Every place _swap_function_references touches (the module attribute, the mask mapping including nested dicts and partials, and transformers.* / compiled-cache module globals) is pointed at that function through monkeypatch, so teardown restores each one exactly.

Verification

Environment Before After
transformers 5.17.0 + peft + zoo main (CI shape) 1 failed 3 passed, 1 skipped
transformers 5.17.0, zoo before #1656 2 passed, 2 skipped (static test never ran) 3 passed, 1 skipped
transformers 5.17.0, no peft 1 failed 3 passed, 1 skipped
transformers 5.19.0 + peft 1 failed 3 passed, 1 skipped
transformers 4.57.6 2 passed, 2 skipped 2 passed, 2 skipped
  • Sabotage: changing the fix to kwargs.get instead of kwargs.pop for block_sequence_ids makes 2 tests fail.
  • State check: a test run before and after this file sees the same zoo wrapper on create_chunked_causal_mask and the same Unsloth fix in the mapping.

…rapper

unsloth-zoo#1656 copied the original signature onto patch_transformers_masks' wrappers. Before it, the zoo wrapper on create_chunked_causal_mask read as (*args, **kwargs), so these tests skipped. Now they run, and the 'unpatched' fixture only unwrapped the outermost function: that is the zoo wrapper, which wraps Unsloth's fix (kept on _unsloth_original_create_chunked_causal_mask) and sets no __wrapped__, while LAYER_PATTERN_TO_MASK_FUNCTION_MAPPING still points at the fix. So the unpatched static-cache generate went through the fix and test_static_cache_generate_matches_dynamic failed with DID NOT RAISE TypeError.

The fixture now walks the whole chain to the stock transformers function and points every reference at it through monkeypatch, so teardown restores each one.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-10T17:12:35.637801Z 106dcd4 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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: 47bd2e80a1

ℹ️ 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".

elif is_wrapper(value):
monkeypatch.setitem(mapping, key, original)
monkeypatch.setattr(masking_utils, "create_chunked_causal_mask", original)
for name, module in list(sys.modules.items()):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Restore wrappers in modules imported during the test

This scan only registers modules that exist before the fixture yields. In test_static_cache_generate_matches_dynamic, _tiny_llama4() subsequently imports transformers.models.llama4.modeling_llama4; the call to fix_transformers_chunked_mask_block_sequence_ids() then rewrites that module's reference directly to the new per-test wrapper, but monkeypatch never recorded its previous value. Teardown therefore restores the module attribute and mapping while leaving modeling_llama4.create_chunked_causal_mask pointing at the test wrapper, so later tests can bypass the restored unsloth-zoo wrapper chain and become order-dependent. The fixture needs teardown logic that also restores references created or rewritten after setup.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Correct. When modeling_llama4 is first imported inside the test, monkeypatch never recorded it, so teardown left it on the per-test wrapper. The fixture now records which modules existed at setup and, on teardown, rebinds any module imported afterwards whose reference resolves to the stock function or a test-made wrapper to the restored outer function. Checked by dropping Llama-4 from sys.modules before the file and asserting afterwards that modeling_llama4 points at masking_utils.create_chunked_causal_mask: fails on the previous head, passes now.

…g the test

monkeypatch only records modules that existed at setup. When modeling_llama4 is first imported inside the test it binds the stock function, and the fix rewrites it to a wrapper this test made, which teardown left in place. Rebind such modules to the function a normal import would have seen.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 10, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 106dcd4f2e

ℹ️ 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".

@danielhanchen
danielhanchen merged commit c538b0f into main Oct 10, 2026
@danielhanchen
danielhanchen deleted the ci-chunked-mask-fixture branch October 10, 2026 17:16
danielhanchen added a commit that referenced this pull request Oct 10, 2026
…rt (#13257)

#13248 moved the fixture to monkeypatch and stopped calling _swap_function_references, but kept the import. The PR-diff import-hoist guard (scripts/verify_import_hoist.py) flags an import that was used before and no longer is, so the next PR touching this file would fail Source lint.
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