Repository navigation
fix(sentence_transformer): resolve device consistently with sibling branches instead of hardcoding cuda - #10843
Conversation
…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.
|
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. |
|
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. |
|
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. |
|
@codex security review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
What does this PR do?
Fixes an inconsistent device resolution in the fast-encoder path of
sentence_transformer.py.When
device_mapis a dict or"auto"/"sequential", three similar code blocks handle the device. Two of them (thefor_inferencebranch at line ~1538 and the normal loading branch at line ~1878) correctly setst_device = Noneto letSentenceTransformerdetect the device itself. But the fast-encoder path (line ~1609) hardcodesst_device = "cuda"under the same condition.This hardcode is:
SentenceTransformer(device="cuda")crashes withTorch not compiled with CUDA enabled.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 = Nonest_device = NoneletsSentenceTransformerresolve 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 whenNoneis passed for an auto/sequential device_map).Verification
On Ascend NPU (torch 2.14 + torch_npu), the previous
"cuda"hardcode causesSentenceTransformer(device="cuda")to fail withTorch not compiled with CUDA enabled, whilest_device = Nonelets the device resolve correctly. The fix is device-agnostic and does not alter CUDA semantics.