Skip to content

fix(cpu): return "cpu" from get_device_type() instead of raising when no GPU found#604

Draft
LeoBorcherding wants to merge 10 commits into
unslothai:mainfrom
LeoBorcherding:fix/cpu-no-torch-import
Draft

fix(cpu): return "cpu" from get_device_type() instead of raising when no GPU found#604
LeoBorcherding wants to merge 10 commits into
unslothai:mainfrom
LeoBorcherding:fix/cpu-no-torch-import

Conversation

@LeoBorcherding

@LeoBorcherding LeoBorcherding commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

Companion branch: https://github.com/LeoBorcherding/unsloth/tree/fix/cpu-no-torch-import
Companion issue: unslothai/unsloth#5008

Changes

  • device_type.py: return "cpu" instead of raising NotImplementedError when no GPU accelerator found
  • __init__.py: gate all torch-dependent imports and allocator config behind _HAS_TORCH, add CPU stubs for device type functions
  • temporary_patches/gpt_oss.py: guard CUDA memory query behind device type check to avoid crash on CPU

Validation

Validated in LeoBorcherding/unsloth@fix/cpu-no-torch-import. Full validation log posted in unsloth/pull/5119.

@LeoBorcherding

Copy link
Copy Markdown
Contributor Author

Marking PR's as draft, need to test again after latest improvements. Will retest both paths tomorrow against the latest commits before merging.

@LeoBorcherding LeoBorcherding marked this pull request as draft April 21, 2026 05:43

@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 enhances the library's robustness in environments where PyTorch is not installed by introducing a _HAS_TORCH flag and providing fallback values or mocks for torch-dependent variables and functions. It also updates device detection to return "cpu" instead of raising an error when no GPU is found. The review feedback suggests refactoring repetitive _HAS_TORCH checks into a single block for better readability and removing redundant checks where dependent boolean flags already account for the absence of torch.

Comment thread unsloth_zoo/__init__.py
Comment thread unsloth_zoo/__init__.py Outdated
Comment thread unsloth_zoo/__init__.py Outdated
@LeoBorcherding

LeoBorcherding commented Apr 21, 2026

Copy link
Copy Markdown
Contributor Author

This PR is ready and working with unsloth#5119.

Tested end-to-end on Windows CPU-only:

  • grouped-gemm probe fallback working ✅
  • unsloth-zoo imports successfully without torch ✅
  • All features gate correctly for no-torch mode ✅
  • Still required Linux Test and validation ❌

Ready for review with #5119.

@LeoBorcherding LeoBorcherding force-pushed the fix/cpu-no-torch-import branch from 6a63dd7 to 7b3c412 Compare April 22, 2026 07:58
@LeoBorcherding

LeoBorcherding commented Apr 22, 2026

Copy link
Copy Markdown
Contributor Author

Linux validation done - 3/3 pass on Ubuntu 24.04 (WSL2, Python 3.12, torch==2.11.0+cpu, no GPU). Same results as Windows.

Both PRs are fully validated across platforms. Ready to merge whenever #5119 is reviewed.

@rolandtannous

rolandtannous commented Apr 22, 2026

Copy link
Copy Markdown

check comment on Companion PR: unslothai/unsloth#5119

Comment thread unsloth_zoo/temporary_patches/moe_utils.py
Comment thread unsloth_zoo/__init__.py
LeoBorcherding and others added 2 commits April 23, 2026 01:18
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>

@LeoBorcherding LeoBorcherding left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

addressed Datta's concerns, and applied some of Gemini's suggestions

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.

3 participants