Skip to content

Tests: clear the cached tensor parallel layout before every test - #17472

Merged
danielhanchen merged 1 commit into
mainfrom
tests-fresh-tp-layout
Nov 11, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
tests-fresh-tp-layout

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Repo tests (CPU, rest) can fail on any PR, depending on which tests share an xdist worker (seen on #17466, which only touches the Studio frontend):

tests/test_parallel_env.py:1210: in test_pre_init_query_reads_a_pending_peft_config
E   AssertionError: Regex pattern did not match.
E     Expected regex: 'cp x tp'
E     Actual message: "Unsloth: context parallelism (context_parallel_size = 2) does not compose with tensor parallel (UNSLOTH_TP_SIZE=2) yet: ..."

unsloth.models.tp_env.env_layout() reads the UNSLOTH_TP_SIZE / UNSLOTH_PARALLEL layout once per process, by design. tests/test_parallel_env.py's clean_env fixture resets that cache on teardown only. When an earlier test in the same worker, from another file, read the layout with tensor parallel unset, None stays cached. The test above then sets UNSLOTH_TP_SIZE=2 and UNSLOTH_CP_SIZE=2, _composed_layout() sees the stale None, and the trainer raises the generic "does not compose" refusal instead of the layout's own cp x tp one.

The refusal logic and the test's expectation are both right; only the cache crosses tests. Reproduced on main by running any test that calls env_layout() before this one (1 failed).

Change

  • tests/conftest.py: a new autouse fixture clears the tp_env layout cache before every test. It only does so when unsloth.models.tp_env is already imported, so it adds no unsloth import to tests that do not use it.
  • New tests/test_tp_layout_isolation.py: the first test leaves the layout read, the second asserts it starts unread. Run in file order without the fixture, the second test fails.

No library code changes.

Testing

  • tests/test_tp_layout_isolation.py with tests/test_parallel_env.py, file order: 136 passed. With main's tests/conftest.py: test_the_next_test_starts_with_the_layout_unread fails.
  • A test that calls env_layout() followed by test_pre_init_query_reads_a_pending_peft_config: fails on main, passes here.
  • With -n 4 and random order: test_tp_layout_isolation.py, test_parallel_env.py, test_tp_env_gloo.py, test_pp_env_gloo.py, test_parallel_capabilities_precision.py: 198 passed, 1 skipped.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-11T05:36:03.337729Z b16ca9d 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

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: b16ca9d5d8

ℹ️ 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 4b10969 into main Nov 11, 2026
@danielhanchen
danielhanchen deleted the tests-fresh-tp-layout branch November 11, 2026 07:00
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