Skip to content

fix(sentence_transformer): resolve device consistently with sibling branches instead of hardcoding cuda - #10843

Merged
danielhanchen merged 1 commit into
unslothai:mainfrom
li-lizhe:fix-sentence-transformer-fork-device
Sep 14, 2026
Merged

danielhanchen merged 1 commit into
unslothai:mainfrom
li-lizhe:fix-sentence-transformer-fork-device

Conversation

@li-lizhe

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes an inconsistent device resolution in the fast-encoder path of sentence_transformer.py.

When device_map is a dict or "auto"/"sequential", three similar code blocks handle the device. Two of them (the for_inference branch at line ~1538 and the normal loading branch at line ~1878) correctly set st_device = None to let SentenceTransformer detect the device itself. But the fast-encoder path (line ~1609) hardcodes st_device = "cuda" under the same condition.

This hardcode is:

  • Device-hostile: On non-CUDA devices (Ascend NPU, Apple MPS, Intel XPU, etc.) SentenceTransformer(device="cuda") crashes with Torch not compiled with CUDA enabled.
  • Inconsistent: The other two branches deliberately use None (device-agnostic) for exactly this case.

Change

             st_device = device_map
             if isinstance(st_device, dict) or (
                 isinstance(st_device, str) and st_device in ["auto", "sequential"]
             ):
-                st_device = "cuda"
+                st_device = None

st_device = None lets SentenceTransformer resolve the actual compute device at runtime, matching the behavior of the two sibling branches. No behavior change on CUDA (where the device is auto-detected as before when None is passed for an auto/sequential device_map).

Verification

On Ascend NPU (torch 2.14 + torch_npu), the previous "cuda" hardcode causes SentenceTransformer(device="cuda") to fail with Torch not compiled with CUDA enabled, while st_device = None lets the device resolve correctly. The fix is device-agnostic and does not alter CUDA semantics.

…nches

The fast encoder path in sentence_transformer.py hardcodes `st_device = "cuda"` when device_map is "auto" or "sequential", while the other two branches (for_inference at line 1538 and normal loading at line 1878) set `st_device = None` under the same condition.

This inconsistency:
1) Causes a crash on non-CUDA devices (NPU, MPS, etc.) where `SentenceTransformer(device="cuda")` fails
2) Is clearly an oversight — the other branches have the correct device-agnostic behavior

Fix: set `st_device = None` consistently, letting SentenceTransformer handle device detection.
@danielhanchen

Copy link
Copy Markdown
Member

Confirmed this hits unsloth/models/sentence_transformer.py where the fast encoder branch still forces "cuda" while the two sibling branches resolve to None, so non-CUDA hosts fail there. Will get this reviewed.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed the fast-encoder path in unsloth/models/sentence_transformer.py still sets st_device to "cuda" while the other two branches pass None for the same dict, auto and sequential case, so this lines them up. Will get this reviewed.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed the fast-encoder block in unsloth/models/sentence_transformer.py still hardcodes "cuda" while the for_inference and module-loading branches both pass None for dict/auto/sequential device maps. Will get this reviewed.

@danielhanchen

Copy link
Copy Markdown
Member

@codex security review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 14, 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-09-14T13:21:51.715371Z 1c69b92 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. 🚀

Reviewed commit: 1c69b92386

ℹ️ 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 c678258 into unslothai:main Sep 14, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants