Skip to content

feat(sdpa): validate SDPA dropout in the PyTorch reference (ALMIOPEN-3178) - #128

Open
GarbyGilbertAMD wants to merge 55 commits into
mainfrom
users/ggilbert/ALMIOPEN-3178/sdpa-dropout-validate
Open

GarbyGilbertAMD wants to merge 55 commits into
mainfrom
users/ggilbert/ALMIOPEN-3178/sdpa-dropout-validate

Conversation

@GarbyGilbertAMD

@GarbyGilbertAMD GarbyGilbertAMD commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The PyTorch reference (--validate pytorch) now validates SDPA dropout: explicit-mask dropout (forward and backward, with dBias when the graph has a bias gradient), Philox dropout through the engine's own rng_dump output (forward), and probability 0 as no dropout (forward and backward). The tool also fills the dropout inputs. hipDNN engines now serve Philox dropout (rocm-libraries #15066) and the hipDNN references validate explicit-mask dropout, but the tool declined all of it, so engine rows failed or stayed unchecked for the wrong reason.

JIRA ID : ALMIOPEN-3178

Risk Assessment

Risk level 3. The change is a new validation path in the tool. It does not touch the timed loop (timing.py is unchanged) and does not change any graph without SDPA dropout. Behaviour does change for SDPA dropout graphs: rows that used to be declined are now graded, and some graphs now fail with --validate where they were unchecked before (see Technical Changes). The tuned-plan oracle path changes its internal interface (run_hipdnn_tuned takes a validate callable), and the one caller is migrated.

ASIC Coverage

The code is host-side Python, independent of the architecture. The engine rows that it grades depend on the architecture, so the hardware runs below use gfx950 (FlyDSL, ASM SDPA) and gfx942 (ASM SDPA, hip_flash2). Passing unit CI is enough for the code. The hardware runs are evidence of the grading, not a merge gate for other architectures. The hipDNN scratch builds that served them used a rocm-libraries build with the FlyDSL and kernel-ingestor options on. The tool pin (rocm-libraries 28f91cf, after the merge of main) includes the hipDNN dropout reference rules and engines (rocm-libraries #14358, #15066 and #15643).

Testing Summary

  • Unit tests (no GPU) added by area, each aimed at a plausible bug:
    • Input fill (test_input_dropout.py, test_sdpa_dropout.py): mask is exactly 0 or 1 at the keep rate and repeats for a seed; scale and scale_inv follow the keep rate; a keep rate outside (0, 1] is refused; seed and offset are nonzero; the other inputs do not change; a null dropout_probability counts as absent.
    • Reference handler (test_pytorch_provider.py, test_pytorch_ops.py): closed-form explicit-mask forward and the backward gradient check with dropout_scale_inv (a mutant that drops the factor fails it); dBias of an explicit-mask backward graph as the dropout-scaled dS before the attention scale; Philox reference from a given 0/1 dump at p other than 0.5 (the 1/p bug); composition with causal, sliding window, bias, ALiBi and sinks; one decline test per hipDNN decline rule; paged and ragged dropout declined by name; host-memory estimate counts the dropout temporaries. The tests that pinned the old rejections are replaced.
    • Dump check and NaN fill (test_correctness.py, test_buffer_manager_torch.py): values outside {0, 1} fail; the count-based keep-rate bound (a dump with one zero at p = 0 passes, as the bound allows one element; an all-zero dump at p = 0 fails); the NaN fill covers strided padding and rejects a non-FLOAT output; the rng_dump output is not compared.
    • Suite runner and oracle (test_suite_runner.py, test_oracle.py): each engine row is graded against its own dump; an engine that never writes the dump fails; a dump that is bad fails before any reference runs; Philox without rng_dump fails and names it, also when a CPU reference would not fit; a non-FLOAT dump fails and keeps its timings; a dump that does not fit host memory gives unchecked before any fill; device out-of-memory retry on the host; the tuned child fills its dump with NaN and the parent validates the tuned plan from the child's saved dump; one row runs end to end against the real reference, also under a strict backend.
  • Whole unit suite, at the final tip merged with main: 2257 passed. Every failure needs files outside the test archive (setup_env.py, sample_configs/) or a permission check, as on the base; none is in the changed code.
  • Hardware, gfx950, before the merge with main, each outcome as expected:
    • Philox forward with rng_dump on the FlyDSL engine: the rows pass (three graphs), and the tuned plan validates with --oracle.
    • Philox without rng_dump: the engine rows fail (exit 3) and the message names rng_dump.
    • Explicit mask on the ASM engine: the engine ignores the mask and the row fails on O (exit 3).
    • ASM at p = 0 with rng_dump: the engine leaves the dump unwritten and the row fails the dump check (message: "rng_dump value check failed: element 0 is nan", exit 3).
    • FlyDSL at p = 0 with a dump: the engine declines the graph, so its row is skipped ("not applicable to this graph").
  • Mutation sweep: seeded mutants of the new code (wrong Philox scale, missing dropout_scale_inv factor, reused dump, zero instead of NaN fill, null read as 0, skipped value or rate check, each decline gate removed in turn, shortfall check moved first, strict backend scope, missing tuned validation, missing memory budget, missing out-of-memory retry, and others). At the final tip the unit tests kill all 66 mutants, including mutants for dBias with dropout, the fused-path bypass of a dropout backward, the backward variable-length decline and a backward seed or offset decline at p = 0.
  • Hardware, gfx942, before the merge with main, each outcome as expected:
    • Explicit mask on the ASM engine and on the hip_flash2 engine (fp16, head dimensions 64 and 128): the engines ignore the mask and the rows fail on O (exit 3).
    • ASM at p = 0 with rng_dump: the dump is left unwritten and the row fails the dump check (exit 3).
  • Recheck of the two tests that timed out on PR CI, at an earlier tip: both pass. test_oracle_times_a_knob_built_plan runs the hipDNN --oracle path, which does not import torch. test_pytorch_runtime_imports_torch_before_optional_hip_timer ran with the ROCm nightly torch that CI uses. The later commits do not reach either test. On that CI run the whole suite was slow, not only these two tests.
  • Device path: not run. It needs a ROCm PyTorch built for the ROCm of the hipDNN build; only a nightly build exists for it. The unit tests cover the device branch's error handling (out-of-memory retry on the host) with a fake provider; the hardware runs above used the host reference.

Testing Checklist

  • Unit tests, whole suite - pytest tests/unit -m "not gpu" - Status: Passed (2257 passed; failures only where files are outside the test archive, as on the base)
  • Hardware, gfx950 - Philox with and without rng_dump, explicit mask on ASM, p = 0 on ASM and FlyDSL, --oracle on FlyDSL - Status: Passed (all outcomes as expected)
  • Mutation sweep - unit tests against seeded mutants - Status: Passed (66 of 66 mutants killed)
  • GPU recheck - the two tests that timed out in the GitHub checks - Status: Passed (2 of 2, at an earlier tip)
  • Hardware, gfx942 - explicit mask on ASM and hip_flash2, p = 0 dump on ASM - Status: Passed (all outcomes as expected)
  • PR CI - GitHub PR checks - Status: Pending

Technical Changes

  • execution/sdpa_dropout.py (new, no torch import): reads the dropout attributes (a null dropout_probability counts as absent) and finds rng_dump.
  • execution/buffer_manager.py: fills dropout_mask with 0 and 1 at keep rate 0.9, dropout_scale as 1 / keep rate, dropout_scale_inv as the keep rate, and seed and offset with nonzero values, from a separate generator so other inputs do not change; fills rng_dump with NaN before each validation run.
  • execution/correctness.py: checks an engine's rng_dump (values in {0, 1}; keep rate within 6 sigma plus one element, counted on the number of ones; first bad index named); check_correctness can ignore listed outputs.
  • execution/pytorch_ops/handlers/sdpa.py, _sdpa_backend.py, _warnings.py, __init__.py: explicit-mask dropout forward and backward as separate ops (an explicit-mask backward graph with a bias gradient also gives dBias, the dropout-scaled dS before the attention scale, as the hipDNN CPU reference and SEM-014 define); p = 0 as no dropout, also in the backward, where a graph with dropout_probability 0 and no dropout mask, scale or scale_inv is no dropout, with or without seed and offset, as in hipDNN's backward references; dropout with causal, sliding window, bias, ALiBi and sinks; a Philox reference built from a given dump; declines for each hipDNN decline rule and for paged and ragged dropout; a reference warning for --runtime pytorch.
  • execution/suite_runner.py, validation/providers/pytorch_provider.py, validation/reference_provider.py: a scoped Philox dump channel; each Philox engine row is graded against its own dump and reference after the timed loop; Philox without rng_dump is decided before the host-memory check; the host-memory estimate (_sdpa_scratch_bytes) counts the dropout temporaries.
  • execution/oracle.py, cli/hipdnn_tuned_child.py: run_hipdnn_tuned takes a validate(outputs) callable; the tuned child fills its dump with NaN and the parent validates the tuned plan from the saved outputs.
  • Merges of main (first e31db1d, now through 61dc475): SDPA backward (feat(sdpa): support SDPA backward graphs and bump rocm-libraries (ALMIOPEN-2975, ALMIOPEN-2977) #120), ragged (feat(sdpa): support ragged and variable-length SDPA tensors #118), aiter (feat(runtime): add --runtime aiter for direct AITER SDPA benchmarking against hipDNN (ALMIOPEN-3222) #119), fix: overhaul review follow-ups: telemetry device, setup checks, CLI and compare errors, test gaps (ALMIOPEN-2866) #110 and fix(oracle): one timing mode for the PyTorch child, flush release, env warning (ALMIOPEN-2866) #111, then fix(setup, rocke): AITER build without /opt/rocm; gfx950 dense variants without head counts #136, feat(results): schema 3 with a geometry-hash graph_id #138 (result schema 3) and feat: move Workloads/ to a tag-addressed graph store (migration, --tag, s3:// source, Workloads removal) #139 (Workloads store), then feat(pytorch): record the SDPA backend and GPU kernels of PyTorch rows #137 (PyTorch SDPA backend record), chore(deps): bump rocm-libraries from 4577381 to 28f91cf #142 and fix(deps): bump rocm-libraries to 4577381 for the 10.3.0a20261123 MIOpen #144 (rocm-libraries pin to 28f91cf, which fixes the MIOpen plugin load with the current ROCm nightly) and chore(deps): bump ROCm/rocm-security-gh/.github/workflows/security-baseline.yml from 1.0.8 to 1.0.9 in the github-actions group across 1 directory #140. The dropout handlers sit beside the new backward and ragged code.
  • Docs: docs/usage.md, docs/methodology.md, docs/results-schema.md, AGENTS.md describe the formulas, the fill, the declined graphs and the new module.
  • Behaviour users see:
    • Exit 3: a Philox graph without rng_dump, with a non-FLOAT rng_dump, with paged or ragged dropout, or a Philox backward graph now fails its engine rows with --validate (timings stay). A bad or unwritten dump fails the row. A Philox dropout_probability that is NaN or outside [0, 1) also fails the row with a dump, and the message names dropout_probability.
    • Exit 1: a dropout_scale in the graph JSON whose 1 / dropout_scale is outside (0, 1] is a graph error for the hipdnn and pytorch runtimes.
    • unchecked (exit 0): a Philox row whose dump fill and read would not fit in 80 % of the available host memory, or whose reference does not fit; its tuned plan is not validated. A bad dump still fails.
    • A Philox graph has no reference row. Its rows are graded per row. A Philox row timed with rng_dump includes the dump write in each launch, so it is not a training-step time.
    • --runtime pytorch runs explicit-mask graphs and declines Philox graphs. --runtime rocke still declines dropout. A strict --pytorch-sdpa-backend fails the rows of an explicit-mask graph (no native SDPA call), but not Philox rows with rng_dump.
    • The timed loop is unchanged.
    • Limit of the Philox check: it shows that the dump is a 0/1 mask at the expected keep rate and that O matches a reference built from that dump within the tolerance of the output data type. With bf16 outputs and thousands of keys, that tolerance cannot tell the dumped mask from another mask, or detect a softmax normalised again after the mask (docs/methodology.md). An engine that writes a correct dump but omits the 1 / (1 - p) scale passes when p is below about the relative tolerance (0.03 for bf16).
  • Differences from the ticket text (the ticket is not edited):
    • Criterion 1 loses the ALMIOPEN-2861 clause: the fill does not follow the integration-test recipe (follow-up (a)).
    • The background claim that nothing can reproduce a kernel's random stream is stale: the semantics now define the mask bits. It reads "the hipDNN references do not regenerate the stream".
    • Criterion 8 names rocm-libraries #15643 as the backward half of ALMIOPEN-3149. It is merged. Backward p = 0 with seed or offset is now no dropout, as in hipDNN's backward references; backward Philox (p above 0 with seed and offset) stays declined, as in hipDNN's references.
    • The dependency on a Philox engine is replaced: FlyDSL defaults OFF in hipDNN and the tool setup does not enable it, so the hardware runs used a scratch build with FlyDSL and the kernel ingestor on. The pin now carries the hipDNN dropout reference rules, through the merge of main (the pin is rocm-libraries 28f91cf and includes #14358, #15066 and #15643).
    • Out of scope gains: paged, ragged and variable-length dropout is declined by name. The PyTorch reference does not implement it; hipDNN's own reference densifies these layouts, so this is a tool limit (follow-up (e)). Philox backward rows fail: hipDNN's backward graph has no rng_dump (#14358), so the reference has no mask. The FlyDSL backward engines (#15643) serve it, but no reference validates it.
  • Follow-ups:
    • (a) Switch the dropout input fill to the integration-test recipe once ALMIOPEN-2861 lands.
    • (b) Validate a passing explicit-mask engine row and rocKE Philox rows once AICK-2954 lands; then lift the --runtime rocke dropout decline. Until then the explicit-mask evidence is failing rows (ASM and hip_flash2 ignore the mask, ALMIOPEN-3123).
    • (d) Enable HIPKERNELPROVIDER_ENABLE_FLYDSL in setup_env.py and pull the FlyDSL descriptors, so gfx950 CI runs Philox rows.
    • (e) Validate dropout on paged and ragged SDPA graphs.
    • (f) Regenerate the SEM-015 Philox mask in the reference (develop has a pure-torch Philox, #15232), for a bit-exact dump check and for Philox without rng_dump.
    • (g) Jira hygiene: close ALMIOPEN-3150 as the rejected alternative; point the backward half of ALMIOPEN-3149 at #15643.
    • (h) Reference warnings: skip the kernel-mask reasons (window, bottom-right) when a graph takes the manual fp32 path, for dropout and for sinks alike.
    • (i) A graph with more than one Philox forward node: name the one-dump-per-graph limit in the decline message.

…PEN-3178)

run_hipdnn_tuned now takes a validate callable instead of the tensor infos
and reference outputs. The tuned child saves its outputs when a callable is
given and fills the rng_dump buffer with NaN first. For a Philox graph the
callable checks the tuned child's own dump and builds a reference from it.

ALMIOPEN-3178
…nce (ALMIOPEN-3178)

The backward reference now accepts dropout_mask and dropout_scale with an
optional dropout_scale_inv. It applies the mask to dP, scales D by
dropout_scale_inv and builds dV from the dropped and scaled P. The cases the
hipDNN references decline stay declined.

ALMIOPEN-3178
…MIOPEN-3178)

Remove the rng_dump output row and the dropout_mask and dropout_scale input
rows from the tests that pinned the old rejections. The new dropout decline
tests cover the same rules.

ALMIOPEN-3178
…EN-3178)

A Philox graph whose rng_dump is not FLOAT made the NaN fill raise, so the
engine row became an error row and lost its timings. The reference now
declines such a graph before the fill, as the hipDNN references do, so with
--validate the engine rows fail and keep their timings.

ALMIOPEN-3178
…IOPEN-3178)

The CPU reference size estimate now counts an SDPA dropout forward (explicit
mask, or rng_dump with a non-null probability) like a sink forward: three
score-block temporaries, plus the float32 band when bounds apply.

ALMIOPEN-3178
…MIOPEN-3178)

Name the host memory exception for declined graphs, limit the p = 0 rule for
backward nodes, scope the dropout_scale graph error to the hipdnn and
pytorch runtimes, and list every decline rule.

ALMIOPEN-3178
…nce (ALMIOPEN-3178)

Grade a Philox engine row with the real PyTorch reference provider and fake
hipDNN buffers: the row passes with its own dump and output, and fails with
a flipped output or the dump complement. A strict SDPA backend selection
does not affect the per-row reference.

ALMIOPEN-3178
…ALMIOPEN-3178)

The mask-without-scale and scale-without-mask decline cases now check that
the message names both keys, and a rank-1 mask case covers the rank check.
Without them, removing either gate still passed because a later lookup
declined the graph.

ALMIOPEN-3178
The dump check shows that the dump is a 0/1 mask at the expected keep rate
and that the outputs match a reference built from it within the output data
type tolerance. It cannot tell the dumped mask from another mask at bf16 with
many keys. Also split the sentences that were longer than 25 words.

ALMIOPEN-3178
…t reference (ALMIOPEN-3178)

A query row whose keys are all masked, for example with a bottom-right
causal mask and more queries than keys, made the dropout reference return
NaN. It now returns zero output for such a row, as the reference without
dropout and the hipDNN reference do. The stats stay -inf.

ALMIOPEN-3178
…PEN-3178)

A backward node with a nonzero dropout_probability was declined with the
forward message, which asks for an rng_dump output that a backward node
cannot have. The backward rules now run first and say that Philox backward
dropout is not supported (rocm-libraries #15643, draft).

ALMIOPEN-3178
Give the real reason Philox backward rows fail, note that backward
dropout_probability 0 is accepted as no dropout, add the missing-scale limit
of the Philox check and the rng_dump without seed or offset decline, carve
out the Philox exception from the reference row text, and use active voice.

ALMIOPEN-3178
…f range (ALMIOPEN-3178)

check_rng_dump raised a math domain error for a probability below 0 or above
1, which turned the engine row into an error row, and it passed NaN. It now
returns a mismatch that names dropout_probability for NaN and for any value
outside [0, 1), so the row fails with exit code 3.

ALMIOPEN-3178
…roadcast shapes (ALMIOPEN-3178)

The backward explicit-mask dropout tests fitted in one score block and used
only the full mask shape. A new test shrinks the score block so the query
rows span several blocks, with full, head-broadcast and batch-broadcast
masks.

ALMIOPEN-3178
The decline message no longer claims a pull request state. It says that the
hipDNN backward graph has no dropout mask output, so the reference has no
mask to apply. The comments that cite hipDNN reference rules now name the
rocm-libraries pull requests that added them, which postdate the submodule
pin.

ALMIOPEN-3178
A graph-supplied dropout_scale of 1.0 is a keep rate of exactly 1. It is
accepted and gives an all-ones mask. No test covered that boundary.

ALMIOPEN-3178
@GarbyGilbertAMD
GarbyGilbertAMD marked this pull request as ready for review November 13, 2026 21:29
@GarbyGilbertAMD
GarbyGilbertAMD requested a review from a team as a code owner November 13, 2026 21:29
Bring in SDPA backward support with dBias and the rocm-libraries bump
(#120), ragged and variable-length SDPA (#118), the aiter runtime (#119)
and the oracle follow-ups (#110, #111). The dropout reference now composes
with the backward dBias: dBias is the dropout-scaled dS before the attention
scale, as hipDNN's CPU reference defines it. Dropout with paged or ragged
K/V stays declined. The dropout tests use the new left_bound rule.

ALMIOPEN-3178
…(ALMIOPEN-3178)

The keep-rate check compared rates against 6 * sqrt(keep * (1 - keep) / n)
+ 1 / n. At p = 0 a dump with exactly one zero sits on that bound, and the
division by n rounded it one ULP past the bound for many sizes, so a valid
dump failed. The check now compares the count of ones with keep * n against
6 * sqrt(keep * (1 - keep) * n) + 1, which is exact at p = 0. hipDNN's
harness keeps the rate form, so it can still reject such a dump.

ALMIOPEN-3178
The dBias bullet applied the dropout mask and dropout_scale_inv twice. It
now says dBias is the dS above, taken before the attention scale and summed
over the bias's broadcast dims, as hipDNN's CPU reference does. A wrong
dropout_scale_inv also shows in dBias.

ALMIOPEN-3178
… (ALMIOPEN-3178)

After the merge, test_oracle.py builds its own validate callable, so no
test checked that suite_runner hands the tuned run a validator built from
the graph's resolved tensor infos. The new test calls the captured
validator on matching -inf SDPA stats with matching_infinities set, and on
a mismatch as the control.

ALMIOPEN-3178
…PEN-3178)

The merge of main dropped the explicit-mask checks and the Philox dump
lookup from compile_sdpa, so every dense forward SDPA graph raised a
NameError. Restore them, and run the paged and ragged dropout decline
before main's paged checks again so its message names the layout. The
decline tests now use graphs that are valid without dropout, a backward
variable-length case covers the backward decline, and a test shows a
dropout backward never takes the fused path.

ALMIOPEN-3178
…ernel (ALMIOPEN-3178)

On CPU torch the backward probe never reports a fused kernel, so the test
passed whether or not a dropout backward bypasses the fused path. The test
now simulates a host where a fused kernel is available, which returns
gradients without dropout, and checks that the dropout closed form still
holds.

ALMIOPEN-3178
…id (ALMIOPEN-3178)

read_dropout did not look under "parameters" for the dropout tensor uids,
so a backward node with seed_tensor_uid stored there was not declined,
although every other optional-uid lookup in the handler reads that
section. The Philox ragged decline case now gives a length source on both
sides, so main's padding rule passes and the dropout decline is what fires.

ALMIOPEN-3178
Bring in the result schema version 3 (#138), the Workloads restructure
(#139) and #136. No conflicts.

ALMIOPEN-3178
…ne as tool limits (ALMIOPEN-3178)

The docs now give the keep-rate bound in the count form the check uses and
note that hipDNN's harness, which keeps the rate form, can reject a dump
this check accepts at the one-element edge. The dropout decline for paged,
ragged or variable-length K/V is described as a limit of the PyTorch
reference: hipDNN's own reference densifies these layouts.

ALMIOPEN-3178
…rect decline wording (ALMIOPEN-3178)

sdpa_dropout.py kept its own copy of the optional-uid reader, and that copy
drifted from the shared one once already. It now calls the shared torch-free
node_value reader. The paged, ragged and variable-length dropout declines
say they are a limit of the PyTorch reference, not a hipDNN rule. The
backward Philox decline cites rocm-libraries #14358 (no backward rng_dump),
and the comments no longer say the submodule pin predates the dropout rules.

ALMIOPEN-3178
Bring in the rocm-libraries bump to 28f91cf (#142, #144), which fixes the
MIOpen plugin load with the current ROCm nightly, and the PyTorch SDPA
backend record (#137). The import conflicts in oracle.py and
pytorch_ops/__init__.py keep the imports of both sides.

ALMIOPEN-3178
…MIOPEN-3178)

A backward graph with dropout_probability 0 and no dropout mask, scale or
scale_inv is no dropout, with or without seed and offset, as in hipDNN's
backward references (rocm-libraries #15643). Philox backward with p above 0
stays declined: the backward graph has no rng_dump (rocm-libraries #14358).
The dBias note names the current rocm-libraries pin.

ALMIOPEN-3178
…eed and offset (ALMIOPEN-3178)

hipDNN's backward references now read a backward graph with
dropout_probability 0 and no dropout mask, scale or scale_inv as no
dropout, with or without seed and offset (rocm-libraries #15643, now in
the pin). The PyTorch reference follows: such a graph gives the gradients
of the graph without dropout fields. Philox backward with a probability
above 0 stays declined, because the backward graph has no rng_dump.

ALMIOPEN-3178
…IOPEN-3178)

Since a backward probability of 0 is no dropout, _decline_backward_philox
only has to decline a probability above 0. Its seed and offset decline
only replaced the messages of the forward dropout rules and the scale_inv
check, which decline the same graphs and name the key. Its probability-0
early return then guarded nothing.

ALMIOPEN-3178

@SamuelReeder SamuelReeder left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM overall!

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.

2 participants