Skip to content

revert: stop touching DEVICE_TYPE == cuda branches for CPU CI - #5473

Merged
danielhanchen merged 1 commit into
mainfrom
ci-cb15a5-prod-path-revert
May 16, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
ci-cb15a5-prod-path-revert

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

#5429 tightened three production-path branches to DEVICE_TYPE == cuda and torch.cuda.is_available() and added a new else: SUPPORTS_BFLOAT16 = False arm to let import unsloth.trainer survive on a CPU-only CI host. Unsloth is already shipped + tested on Intel XPU / AMD HIP / NVIDIA CUDA and we do not want any new branching in those hot paths.

Move the entire CPU-CI handling to one place: the top of unsloth/device_type.get_device_type(). The UNSLOTH_ALLOW_CPU=1 sentinel short-circuits detection and returns cuda before any torch probe runs. Every downstream DEVICE_TYPE == cuda branch then behaves identically to a real CUDA host. The two duplicate UNSLOTH_ALLOW_CPU returns later in the function are dropped (the top-of-function check supersedes them).

Reverted call-site changes:

  • unsloth/_gpu_init.py:212 back to if DEVICE_TYPE == cuda:
  • unsloth/_gpu_init.py:247 back to if DEVICE_TYPE == cuda:
  • unsloth/models/_utils.py:1207 back to if DEVICE_TYPE == cuda:
  • unsloth/_gpu_init.py: drop the new else: SUPPORTS_BFLOAT16 = False branch (dead under the top-of-function short-circuit).

Kept (test-only env-var gates, prod hosts never set the var):

  • unsloth/_gpu_init.py if env != '1': _patch_trl_trainer()
  • unsloth/models/rl.py:PatchFastRL if env == '1': return

Verified locally:

  • CUDA_VISIBLE_DEVICES=5 python -c 'import unsloth.trainer' -> trl.SFTTrainer.init.qualname == UnslothSFTTrainer.init (TRL still patched on real CUDA hosts).
  • UNSLOTH_ALLOW_CPU=1 + aggressive cuda spoof import succeeds and trl.SFTTrainer.init.qualname stays SFTTrainer.init.
  • pytest tests/_zoo_compiler_cache_shim.py -> 5 passed, 1 skipped.

#5429 (cb15a7a) tightened three production-path branches to
DEVICE_TYPE == "cuda" and torch.cuda.is_available() and added a
new else: SUPPORTS_BFLOAT16 = False arm to let `import unsloth.trainer`
survive on a CPU-only CI host. We already ship the package on Intel
XPU / AMD HIP / NVIDIA CUDA and don't want any extra branching in
those hot paths.

Move the entire CPU-CI handling to one place -- the top of
unsloth/device_type.get_device_type() -- so the UNSLOTH_ALLOW_CPU=1
sentinel short-circuits detection and returns "cuda" before any
torch probe runs. Every downstream DEVICE_TYPE == "cuda" branch
then behaves identically to a real CUDA host, with no additional
checks. The two existing duplicate UNSLOTH_ALLOW_CPU returns
later in the function are dropped (the new top-of-function check
covers both).

Revert the three call-site changes:
- unsloth/_gpu_init.py:212  -> back to `if DEVICE_TYPE == "cuda":`
- unsloth/_gpu_init.py:247  -> back to `if DEVICE_TYPE == "cuda":`
- unsloth/models/_utils.py:1207 -> back to `if DEVICE_TYPE == "cuda":`
- unsloth/_gpu_init.py: drop the new `else: SUPPORTS_BFLOAT16 = False`
  branch (dead under the top-of-function short-circuit).

Keep the two env-var gates that are needed for zoo's drift detectors
to inspect pristine TRL source (no behavioural change on production
hosts that never set UNSLOTH_ALLOW_CPU):
- unsloth/_gpu_init.py: `if env != "1": _patch_trl_trainer()`
- unsloth/models/rl.py:PatchFastRL: `if env == "1": return`

Verified:
- CUDA_VISIBLE_DEVICES=5 python -c "import unsloth.trainer" produces
  UnslothSFTTrainer.__init__ (TRL still patched on real hosts).
- UNSLOTH_ALLOW_CPU=1 + aggressive cuda spoof import succeeds and
  trl.SFTTrainer.__init__.__qualname__ stays SFTTrainer.__init__.
- pytest tests/_zoo_compiler_cache_shim.py -> 5 passed, 1 skipped.
@danielhanchen
danielhanchen requested a review from mmathew23 as a code owner May 16, 2026 02:31

@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: 7df20e6551

ℹ️ 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/_gpu_init.py
Comment on lines +212 to 213
if DEVICE_TYPE == "cuda":
major_version, minor_version = torch.cuda.get_device_capability()

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 Keep CPU fallback from entering CUDA capability probes

When UNSLOTH_ALLOW_CPU=1 is set on a CPU-only CI host, get_device_type() now returns "cuda" before checking torch.cuda.is_available(), so this restored branch immediately calls torch.cuda.get_device_capability() during import. On a CPU-only or non-CUDA torch build that raises before import unsloth.trainer can complete, which regresses the test-only fallback this commit is trying to preserve; the previous and torch.cuda.is_available() guard skipped this probe in that exact environment.

Useful? React with 👍 / 👎.

@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 centralizes the CPU-only CI logic by moving the UNSLOTH_ALLOW_CPU environment variable check to the beginning of the get_device_type function, which now returns 'cuda' when the flag is enabled. Correspondingly, several torch.cuda.is_available() checks were removed in _gpu_init.py and _utils.py. Feedback indicates that removing these checks poses a risk of runtime crashes in CPU environments where torch.cuda methods are not mocked. Additionally, there are concerns regarding the initialization of SUPPORTS_BFLOAT16 and the source of the DEVICE_TYPE import in _gpu_init.py.

Comment thread unsloth/_gpu_init.py

# Torch 2.4 has including_emulation
if DEVICE_TYPE == "cuda" and torch.cuda.is_available():
if DEVICE_TYPE == "cuda":

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.

high

Removing the torch.cuda.is_available() check while spoofing DEVICE_TYPE to "cuda" in get_device_type() introduces a risk of runtime crashes. If UNSLOTH_ALLOW_CPU=1 is set on a machine without a GPU and torch.cuda is not explicitly mocked (e.g., outside of the test harness), the call to torch.cuda.get_device_capability() on the next line will fail.

Additionally, SUPPORTS_BFLOAT16 is not initialized before this block. If DEVICE_TYPE is neither "cuda", "hip", nor "xpu" (for instance, if it's "mlx"), the variable will remain undefined in this module's scope.

Furthermore, DEVICE_TYPE in this file is currently imported from unsloth_zoo.device_type (line 125). Since the centralized CPU-CI logic was added to the local unsloth/device_type.py, this file will not see the spoofed value unless the import is updated to from .device_type import ....

Suggested change
if DEVICE_TYPE == "cuda":
SUPPORTS_BFLOAT16 = False
if DEVICE_TYPE == "cuda":

Comment thread unsloth/models/_utils.py
HAS_FLASH_ATTENTION_SOFTCAPPING = False

if DEVICE_TYPE == "cuda" and torch.cuda.is_available():
if DEVICE_TYPE == "cuda":

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

Removing torch.cuda.is_available() here makes the code susceptible to crashes when DEVICE_TYPE is spoofed to "cuda" on CPU-only environments without appropriate mocking of torch.cuda.get_device_capability(). While this is intended for CI, it reduces the robustness of the library when the UNSLOTH_ALLOW_CPU flag is used in other contexts.

@danielhanchen
danielhanchen merged commit 77e0929 into main May 16, 2026
44 of 45 checks passed
@danielhanchen
danielhanchen deleted the ci-cb15a5-prod-path-revert branch May 16, 2026 02:41
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