Repository navigation
fix(chat_templates): remove CUDA hardcoded device from stopping criteria - #10684
danielhanchen merged 5 commits into
Conversation
Remove device='cuda' default parameter and hardcoded .to('cuda') calls
in StoppingCriteriaSub. Create tensors on CPU, then lazily move to
input_ids.device in __call__ if needed. This makes the stopping
criteria work on any accelerator (CUDA, NPU, XPU, etc.) without
assuming a CUDA device.
Verification: stop_token correctly moves from CPU to NPU device on
first call. CPU input also works without unnecessary moves.
|
Confirmed the stop token in unsloth/chat_templates.py is still built with a hardcoded cuda device, and the lazy move to input_ids.device keeps the CUDA path the same. Will get this reviewed. |
|
Confirmed unsloth/chat_templates.py still builds the stop token with device="cuda", which breaks generation on non-CUDA builds, and the lazy move to input_ids.device keeps CUDA behaviour unchanged. Will get this reviewed. |
These fail on the unfixed code with a cuda/cpu device mismatch, and on a non-CUDA build they failed at construction with "Torch not compiled with CUDA enabled". They use a stub tokenizer and need no accelerator, so the two CPU cases also run on CPU-only CI; the placement test skips itself without one. Covers construction on CPU, eos and multi-token stop words, the stop token following the input device in both directions, and the cached tensor not being recopied on every generated token.
|
@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". |
|
Codex Review: Didn't find any major issues. Swish! 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?
Removes the CUDA-hardcoded device from the stopping-criteria used during generation, so it works on any accelerator (Ascend NPU, Intel XPU, etc.) without assuming a CUDA device.
Problem
StoppingCriteriaSubbuilds its stop token on CUDA unconditionally:On non-CUDA accelerators (e.g. Ascend NPU with torch_npu, where
torch.cudais not compiled in),torch.tensor(..., device="cuda")and.to("cuda")raiseRuntimeError: Torch not compiled with CUDA enabled. This makescreate_stopping_criteriaunusable off-CUDA.Fix
device="cuda"parameter and the explicit.to("cuda")calls.__call__, lazily move the stop token toinput_ids.deviceonly when they differ (a one-time move, cached thereafter).Verification
Verified on Ascend NPU (torch 2.14, torch_npu, 910B):
stop_tokenis created on CPU, then correctly moved tonpu:0on the first__call__with an NPU input tensor.single_matchandlengthmatching logic is unchanged.Device-agnostic: on CUDA,
input_idsarrives on CUDA and the stop token is moved there on the first call, preserving existing behavior.