Repository navigation
[HIP] [Bugfix] sample: Map exponential's top uniform bucket to a positive sample - #7576
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
|
@syang-genai thanks for the fix. Here is the MI3xx run you asked for, on an MI355X (gfx950, ROCm 7.2.4). Setup. The image's aiter tree is an older public Results
Not covered. The older tree predates the stock On the ATOM side, the opt-in from ROCm/ATOM#2928 is ROCm/ATOM#3083. It uses the materialized-noise path for now, so it doesn't depend on this PR. |
|
@yelkhamra thank you, that's the MI3xx confirmation I was missing. The before/after arms with differing hashes and the check that the changed line appears exactly once make it easy to trust. On the two residual gaps:
Nothing else to change on my side. Could a maintainer take a look when there's a chance? The change is one character in |
|
@yelkhamra here is the higher-draw re-run of the hottest temperature I promised. On gfx1151, the fixed Setup. gfx1151, ROCm 7.2.4, torch 2.9.1. 1,024 identical rows of the same heavy-tailed bf16 logits as before (129,280-token vocab), 2,000 calls, so 2,048,000 draws per path, per temperature, per seed, with seeds 1 and 2. The target is the float64 softmax of the bf16 logits. Tokens are grouped by target probability, and z is the deviation from the target in binomial standard errors. "Stock" and "fixed" are the same AITER tree with and without the one-line change, each JIT-rebuilt. Share of draws on p < 1e-6 tokens (z in parentheses):
The other bands (1e-6 to 1e-4, 1e-4 to 1e-2, and ≥ 1e-2) are within |z| ≤ 1.9 for the fixed So:
If the T = 1.5 gap on MI355X held up at a similar number of draws, it would point to something arch-specific. I'm happy to share the script ( |
|
@syang-genai thanks for the higher-draw re-run and for following up on this. Agreed on the gap we flagged at the hottest temperature: with our smaller draw count, it is consistent with noise, so your higher-draw result is the more reliable reading. The common offset at the coolest temperature also matches what we saw in both paths, so we agree it is not sign-related. Yes, we would like to run |
|
@yelkhamra sure, here it is. It's a single file with no other imports from my setup; Run it once per build, with the label as the only argument, e.g.
|
|
@syang-genai thanks for sharing it. We ran The MI355X results line up with your gfx1151 ones on every point we re-ran (T = 1.0 and 1.5):
So on MI355X the fix does what it says, with nothing left over from the sign. Thanks again for the careful follow-up. |
|
@yelkhamra thanks for running it on MI355X, and for writing up each point. Good to have both architectures agree. @junhaha666 @yitingw1, could one of you take a look when you have a moment? You wrote and fixed the sample kernels (#7373). This is a one-character sign fix in |
|
Please fix CI first. |
…tive sample exponential_func_impl maps a uniform at or above 1 - eps/2 to a fixed tiny sample instead of -log(u). It used +eps/2 for the log there, so the sample came out as -eps/(2*lambd), a negative value. at::transformation::exponential uses -eps/2, which gives the intended tiny positive sample. mixed_sample and random_sample share this function. A negative sample makes that token's p / E negative, so it can never be drawn. About 8.7e-8 of hiprand's uniforms fall in the bucket, so on a 129,280-token vocab about 1.1% of rows lose a candidate. Those are the draws through which most of the low-probability tail gets picked: at T = 1, mixed_sample picked tokens with p < 1e-6 at 1.75% against a target of 2.19%. With the fix it picks 2.15%, the same as mixed_sample_outer_exponential fed torch's exponential_. op_tests/test_sample.py gains check_exponential_is_positive: over 2**27 draws the old sign gives about 11 negatives (18 in my run), and the fix gives none. Signed-off-by: syang-genai <[email protected]>
d601964 to
4beb983
Compare
|
@yitingw1 thanks, will do. The failure wasn't in the change itself: the Ruff job stopped while installing reviewdog ( I don't have permission to re-run jobs here, so I rebased onto current |
|
@yitingw1 thanks for approving the run. Ruff, Black and the other pre-checks pass on the rebased head now. The one failure is again outside the change: I can't re-run jobs on this repo. Could you use "Re-run failed jobs" on run 42618160340 when you have a moment? Thanks! |
|
@yitingw1 thanks for the re-run: CI is green now, including the 8 MI35X standard-test shards and the test gate. @junhaha666, it's ready for your review whenever you have a moment. It's the one-character sign fix in |
|
@junhaha666 @yitingw1 a gentle follow-up: this has been green since 11-10 and is waiting on a review. It's 2 files: the one-character sign fix in It's also the AITER half of ROCm/ATOM#2928: until it lands, If someone else should review changes to |
Summary
exponential_func_implmaps the uniform's top float32 bucket to a negative sample instead of a tiny positive one. As a result,mixed_sampleandrandom_samplecan never draw the affected token, and they under-sample the low-probability tail. This is a one-character fix, plus an op test.Motivation
For
rand >= 1 - eps/2, the function returnsc * bwithc = -1/lambd. Withb = +eps/2, that sample is negative.at::transformation::exponentialuses-eps/2for this bucket, so the sample is+eps/(2*lambd): a near-zero exponential, which in the raceargmax p / Ealmost always wins, as it should. A negative sample makesp / Enegative, so that token can never win.About 8.7e-8 of hiprand's uniforms land in the bucket, so with a 129,280-token vocab about 1.1% of rows have one such element. Those are exactly the draws through which most of the low-probability tail gets picked. This came up while checking whether ATOM could switch its temperature path from
mixed_sample_outer_exponential(fed by torch'sexponential_) tomixed_sample: ROCm/ATOM#2928.Changes
csrc/kernels/sample_kernels.cu:b = -epsilon/2, matching torch. This affectsmixed_sample,random_sampleandexponential; the*_outer_exponentialkernels are unchanged.op_tests/test_sample.py:check_exponential_is_positive, run after the existing edge checks.Performance (if applicable)
Not applicable. The fix changes a constant, and the code path is the same.
Testing
Documentation
It's a correctness fix with no API change.
Dependencies
Breaking Changes
None in the API. Sampled tokens for a given seed change in the rare rows that hit the bucket, since those tokens can now be drawn.
PR Self-Review
Reviewed revision
4beb983
Behavior and intent
An exponential sample is never negative. Before this change, about 8.7e-8 of samples were, which took one candidate out of about 1.1% of rows in
mixed_sampleandrandom_sampleat a 129k vocab.Validation and unverified scope
On gfx1151 (Strix Halo, ROCm 7.2.4, torch 2.9.1), I applied the same one-line change to the AITER build in my container, rebuilt
module_samplethrough the JIT, and compared it with the stock build. The setup was 64 rows with identical heavy-tailed bf16 logits, a 129,280-token vocab, and 128,000 draws per temperature.aiter.exponentialdrawsmixed_sampleshare of p < 1e-6 tokens at T = 0.7 (target 1.42%)For reference,
mixed_sample_outer_exponentialfedtorch.exponential_gives 1.24% / 2.15% on the same inputs. To pin the cause, I fedmixed_sample_outer_exponentialnoise built from the same uniforms with each sign for this bucket. The+eps/2version reproduces the stockmixed_sampledeficit (0.96% / 1.74%), and the-eps/2version matches torch. The small remaining gap at T = 0.7 also shows up with exact float64 noise, so it's unrelated.@yelkhamra also ran it on an MI355X (gfx950, ROCm 7.2.4): no non-positive samples after the change, and the same tail recovery at T = 1.0 and 1.5 with
hot_tail.py(2,048,000 draws per cell). See the comments on this PR.Not verified:
op_tests/test_sample.pyagainst this branch's tree. My machine's AITER build is older, so I ran the new check's logic standalone against both builds.PR-specific question
Which paths use it.
exponential_func_implis the transform for the three kernels that draw their own noise:random_sample;mixed_sample, for rows with T != 0 (T == 0 rows still go toargmax_impl);exponential.The
*_outer_exponentialkernels andgreedy_sampledon't call it.Architecture, dtype and shape.
module_samplefor every target arch, with no arch-specific branch.VLLM_DISPATCH_FLOATING_TYPES).exponentialcasts it to its output dtype afterwards; the new value, about 6e-8 /lambd, is representable and positive in fp32, bf16 and fp16.Invariant.
hiprand_uniform4returnsrandin (0, 1]. Below the bucket,-logf(rand) / lambd > 0; in the bucket, the sample is now+eps / (2 * lambd) > 0. So every sample is positive forlambd > 0.lambd <= 0was never validated, and this PR doesn't change that.Changed op tests
op_tests/test_sample.py:check_exponential_is_positivedraws 2^27 samples fromaiter.exponentialand asserts none is<= 0. With the old sign it fails with about 11 negatives on average (P(pass) ≈ 2e-5). It isn't redundant: the existing checks cover row bounds, vocab edges and T = 0 argmax, not the values the transform produces.