Skip to content

SWF(bug): stabilize sampling at tiny positive temperatures - #69

Merged
maplepaladin73 merged 1 commit into
mainfrom
devin/1795397213-sampler-tiny-temp
Nov 23, 2026
Merged

maplepaladin73 merged 1 commit into
mainfrom
devin/1795397213-sampler-tiny-temp

Conversation

@maplepaladin73

@maplepaladin73 maplepaladin73 commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Summary

Sampling with a tiny positive temperature no longer overflows and picks the lowest logit: logits [2, 1] at t = 1e-40 now pick token 0, not token 1.

Root cause: sample_core (sampler/sample_impl.rs) divided raw logits by temperature before subtracting the max. [2, 1] / 1e-40 becomes [inf, inf], so the softmax computes inf - inf = NaN and the cumulative draw falls through to the last candidate. The fix ranks and filters unscaled logits (dividing by a positive temperature does not change their order) and scales only the centered difference (logit - max) / t. The largest logit always gets weight 1, and the others underflow to 0 instead of becoming NaN.

  • sample_impl.rs: Top-k, top-p and min-p now rank the unscaled logits. The new softmax_weight helper applies the temperature after centering. Top-p skips zero-weight candidates.
  • absent.rs: Split-row bounds are compared in raw logit space, so they are not scaled to inf either. When temperature is subnormal or the scaled max is non-finite, top-p uses the existing full-row fallback, because the split summary's log mass cannot be trusted there.

Test plan

Regression tests in sampler/tests.rs: tiny_positive_temperature (token 0, finite weights [1, 0], top-k/min-p/top-p) and tiny_positive_temperature_with_absent_tokens (split-row path).

Before the fix (on main):

test sampler::tests::tiny_positive_temperature ... FAILED
assertion `left == right` failed
  left: 1
 right: 0
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 590 filtered out; finished in 0.00s

After the fix:

test sampler::tests::tiny_positive_temperature ... ok
test sampler::tests::tiny_positive_temperature_with_absent_tokens ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 590 filtered out; finished in 0.00s

Full spark-runtime CPU suite: 581 passed, 0 failed, 11 ignored (unit); 1 passed (integration).

Try it: ATLAS_SKIP_BUILD=1 CUDARC_CUDA_VERSION=13000 cargo test -p spark-runtime tiny_positive_temperature

  • cargo fmt --all -- --check
  • cargo clippy -p spark-runtime --tests (crate-scoped, ATLAS_SKIP_BUILD=1)
  • bash scripts/check-license-headers.sh: 4267 valid, 0 invalid
  • typos
  • Tested against a real model / hardware
  • Added regression tests

Notes for reviewers

Untested: No GPU or live-model run. GPU testing on robert2 is reserved for HY4, so only the CPU sampler was run. Normal temperatures follow the same math as before, now in a centered form, and the existing sampler tests still pass.

CLA


Software Factory opened this PR for Robert
Implemented in session Stabilize sampling at tiny positive temperatures
Review with Devin · Open in Devin Desktop


Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@maplepaladin73
maplepaladin73 merged commit 10aaa03 into main Nov 23, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant