Skip to content

Add regression tests for the stray-forward compile-cache reset - #6569

Merged
danielhanchen merged 3 commits into
mainfrom
test/stray-forward-compile-reset-regression
Jun 22, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
test/stray-forward-compile-reset-regression

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Follow-up to #6511. That PR fixed the stray-forward torch.compile cache poisoning, but the squash merge captured the branch before the test commit, so the regression tests never reached main. This adds them back, test-only, no source changes.

tests/test_pretrain_compile_reset.py runs under the GPU-free tests/conftest.py harness and guards the two issues #6511 addressed:

  • _unsloth_reset_stray_compile_cache is an exported module-level symbol in unsloth.models._utils. It previously existed only inside the RLTrainer_replacement template string in rl.py, so from unsloth.models.rl import _unsloth_reset_stray_compile_cache raised ImportError and every non-RL consumer silently no-op'd. This pins it as importable and in __all__.
  • _unsloth_install_pretrain_detector keeps a recorded "seen" forward on an idempotent reinstall with a live hook, and only resets it after teardown.
  • Only a grad-enabled pre-train forward marks the cache as poisoned (a no-grad probe is clean).
  • The reset warns and clears seen when a stray forward was seen, tears the detector hook down even on the clean path, and walks the .model/.base_model/.module wrapper chain to reach a nested marker.

All 8 tests pass against current main.

@codex review

danielhanchen and others added 2 commits June 22, 2026 12:58
Follow-up to #6511, which fixed the bug but whose squash merge did not
include the tests. These cover the two issues that fix addressed, under
the GPU-free tests/conftest.py harness:

- _unsloth_reset_stray_compile_cache is an exported module-level symbol in
  unsloth.models._utils (it previously lived only inside the RL trainer
  template string, so every non-RL import silently no-op'd)
- _unsloth_install_pretrain_detector keeps a recorded "seen" forward on an
  idempotent reinstall with a live hook, and only resets it after teardown
- only a grad-enabled pre-train forward marks the cache poisoned
- the reset warns and clears seen when a stray forward was seen, tears the
  hook down even on the clean path, and walks the .model/.base_model/.module
  wrapper chain to reach a nested marker

@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 adds a new test file tests/test_pretrain_compile_reset.py to verify the stray-pre-train-forward detector and its torch.compile cache reset behavior. The review feedback points out that test_reset_clears_seen_and_warns_when_a_stray_forward_was_seen could fail if UNSLOTH_COMPILE_DISABLE is set to "1" in the test environment, and suggests using monkeypatch to explicitly set it to "0" during the test.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +97 to +106
m = torch.nn.Linear(2, 2)
_unsloth_install_pretrain_detector(m)
m._unsloth_pretrain_marker["seen"] = True # a stray pre-train forward
trainer = _Trainer()
trainer.model = m

with warnings.catch_warnings(record = True) as caught:
warnings.simplefilter("always")
_unsloth_reset_stray_compile_cache(trainer)

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

If UNSLOTH_COMPILE_DISABLE is set to "1" in the test environment (which is common for GPU-free test suites to avoid compilation overhead or errors), _unsloth_reset_stray_compile_cache will skip raising the warning. This will cause test_reset_clears_seen_and_warns_when_a_stray_forward_was_seen to fail because caught will be empty.

To make this test robust against environment settings, use pytest's monkeypatch fixture to temporarily set UNSLOTH_COMPILE_DISABLE to "0" during the test.

Suggested change
m = torch.nn.Linear(2, 2)
_unsloth_install_pretrain_detector(m)
m._unsloth_pretrain_marker["seen"] = True # a stray pre-train forward
trainer = _Trainer()
trainer.model = m
with warnings.catch_warnings(record = True) as caught:
warnings.simplefilter("always")
_unsloth_reset_stray_compile_cache(trainer)
def test_reset_clears_seen_and_warns_when_a_stray_forward_was_seen(monkeypatch):
monkeypatch.setenv("UNSLOTH_COMPILE_DISABLE", "0")
m = torch.nn.Linear(2, 2)
_unsloth_install_pretrain_detector(m)
m._unsloth_pretrain_marker["seen"] = True # a stray pre-train forward
trainer = _Trainer()
trainer.model = m
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
_unsloth_reset_stray_compile_cache(trainer)

The reset only warns and resets Dynamo when UNSLOTH_COMPILE_DISABLE != "1".
A GPU-free CI env that sets it to "1" would make the warn assertion in
test_reset_clears_seen_and_warns_when_a_stray_forward_was_seen flaky.
monkeypatch it to "0" in both warn-path tests so the warn / no-warn
assertions are deterministic and test the seen flag, not the env.
@danielhanchen

Copy link
Copy Markdown
Member Author

Good catch, addressed. _unsloth_reset_stray_compile_cache only warns and resets when UNSLOTH_COMPILE_DISABLE != "1", so a CI env that sets it to "1" would have made the warn assertion flaky.

Both warn-path tests now monkeypatch.setenv("UNSLOTH_COMPILE_DISABLE", "0"):

  • test_reset_clears_seen_and_warns_when_a_stray_forward_was_seen deterministically gets the warning.
  • test_reset_tears_down_hook_even_when_not_seen pins compile on too, so the absent warning proves seen == False is the reason, not a disabled-compile short circuit.

Verified by running the suite with UNSLOTH_COMPILE_DISABLE=1 set: all 8 pass.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: e43a4ab8ac

ℹ️ 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 86d65f3 into main Jun 22, 2026
10 of 15 checks passed
@danielhanchen
danielhanchen deleted the test/stray-forward-compile-reset-regression branch June 22, 2026 14:22
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