Skip to content

fix(chat_templates): remove CUDA hardcoded device from stopping criteria - #10684

Merged
danielhanchen merged 5 commits into
unslothai:mainfrom
li-lizhe:fix/device-agnostic-stopping-criteria
Sep 14, 2026
Merged

danielhanchen merged 5 commits into
unslothai:mainfrom
li-lizhe:fix/device-agnostic-stopping-criteria

Conversation

@li-lizhe

Copy link
Copy Markdown
Contributor

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

StoppingCriteriaSub builds its stop token on CUDA unconditionally:

def __init__(self, stops = "eos_token", device = "cuda", encounters = 1):
    ...
    self.stop_token = torch.tensor(tokenizer.eos_token_id, device = "cuda")
    ...
    self.stop_token = self.stop_token.input_ids.ravel()[1:].to("cuda")

On non-CUDA accelerators (e.g. Ascend NPU with torch_npu, where torch.cuda is not compiled in), torch.tensor(..., device="cuda") and .to("cuda") raise RuntimeError: Torch not compiled with CUDA enabled. This makes create_stopping_criteria unusable off-CUDA.

Fix

  • Drop the device="cuda" parameter and the explicit .to("cuda") calls.
  • Build the stop token on CPU.
  • In __call__, lazily move the stop token to input_ids.device only when they differ (a one-time move, cached thereafter).

Verification

Verified on Ascend NPU (torch 2.14, torch_npu, 910B):

  • stop_token is created on CPU, then correctly moved to npu:0 on the first __call__ with an NPU input tensor.
  • A CPU input tensor leaves the stop token on CPU (no unnecessary device move).
  • The single_match and length matching logic is unchanged.

Device-agnostic: on CUDA, input_ids arrives on CUDA and the stop token is moved there on the first call, preserving existing behavior.

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.
@danielhanchen

Copy link
Copy Markdown
Member

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.

@danielhanchen

Copy link
Copy Markdown
Member

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.
@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:32:38.388418Z 72e8e20 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.

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 14, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 14, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 3a1642874a

ℹ️ 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".

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 72e8e201e9

ℹ️ 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 added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 14, 2026
@danielhanchen
danielhanchen merged commit 0f46188 into unslothai:main Sep 14, 2026
1 check passed
This was referenced Oct 8, 2026
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