[GH-ISSUE #616] ds4: a CUDA error in the sampler reaches the client as token -1 (no error checks in sample() / sample_row() / sample_multi()), and logits() copies on the legacy stream #257

Open
opened 2026-10-29 21:23:24 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @SeriousRising on GitHub (Oct 28, 2026).
Original GitHub issue: https://github.com/mw00/project-maya/issues/616

Symptom

Found by reading; we have not hit a sampler CUDA error. If the head's device faults at the sample, the engine prints T -1, the server yields token id -1 to the client (serve/server.py:1008 yield int(line[2:])), and only the next forward fails, with "token -1 out of range". The error is reported a step late and in the wrong place.

Cause

src/ds4/ds4_model.cu (v1.1.35): the three samplers end with

int t = -1;
cudaMemcpyAsync(&t, d.tok, 4, cudaMemcpyDeviceToHost, d.st);
cudaStreamSynchronize(d.st);
return t;

with neither return code checked: sample_row :14066-14069 (verify's bonus token, :14138), sample :17580-17583, sample_multi (:17692, copy at :17709). The callers in src/ds4/ds4_main.cpp do not test for -1: round()'s plain step :338, the first token of a request :1317 (serve) and :1544 (--parallel), --parallel's steps :1669 and :1689, the CLI :2118. The serve loop prints T %d at :1337 and T %lld %d at :1599 unconditionally.

Separately, logits() (:17722) copies with a blocking cudaMemcpy on the legacy stream, but d.st is created cudaStreamNonBlocking (:2918), so that copy is not ordered after the head's kernels on d.st. It is safe today only because every path that reaches logits() has synchronised d.st first.

Proposed fix

  • tok_back(d): the copy + sync with their return codes checked; on an error it prints ds4 sample: <CUDA error> and returns -1. All three samplers use it.
  • One check where a token is emitted, so every sampler and caller is covered: serve (T %d) and --parallel (T <rid> %d) print ds4: the sample failed and finish the request with cancel; the CLI exits 1.
  • logits(): cudaMemcpyAsync on d.st + cudaStreamSynchronize, both checked.

Error paths and copy ordering only: no numeric change. A PR follows. Not platform-specific.

Originally created by @SeriousRising on GitHub (Oct 28, 2026). Original GitHub issue: https://github.com/mw00/project-maya/issues/616 ### Symptom Found by reading; we have not hit a sampler CUDA error. If the head's device faults at the sample, the engine prints `T -1`, the server yields token id -1 to the client (`serve/server.py:1008` `yield int(line[2:])`), and only the next `forward` fails, with "token -1 out of range". The error is reported a step late and in the wrong place. ### Cause `src/ds4/ds4_model.cu` (v1.1.35): the three samplers end with ```cpp int t = -1; cudaMemcpyAsync(&t, d.tok, 4, cudaMemcpyDeviceToHost, d.st); cudaStreamSynchronize(d.st); return t; ``` with neither return code checked: `sample_row` `:14066-14069` (verify's bonus token, `:14138`), `sample` `:17580-17583`, `sample_multi` (`:17692`, copy at `:17709`). The callers in `src/ds4/ds4_main.cpp` do not test for -1: `round()`'s plain step `:338`, the first token of a request `:1317` (serve) and `:1544` (`--parallel`), `--parallel`'s steps `:1669` and `:1689`, the CLI `:2118`. The serve loop prints `T %d` at `:1337` and `T %lld %d` at `:1599` unconditionally. Separately, `logits()` (`:17722`) copies with a blocking `cudaMemcpy` on the legacy stream, but `d.st` is created `cudaStreamNonBlocking` (`:2918`), so that copy is not ordered after the head's kernels on `d.st`. It is safe today only because every path that reaches `logits()` has synchronised `d.st` first. ### Proposed fix - `tok_back(d)`: the copy + sync with their return codes checked; on an error it prints `ds4 sample: <CUDA error>` and returns -1. All three samplers use it. - One check where a token is emitted, so every sampler and caller is covered: serve (`T %d`) and `--parallel` (`T <rid> %d`) print `ds4: the sample failed` and finish the request with `cancel`; the CLI exits 1. - `logits()`: `cudaMemcpyAsync` on `d.st` + `cudaStreamSynchronize`, both checked. Error paths and copy ordering only: no numeric change. A PR follows. Not platform-specific.
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
mw00/project-maya#257
No description provided.