Repository navigation
Skip constant folding for non-deterministic Bernoulli - #3072
Open
Mohammed Alkindi (MohammedAlkindi) wants to merge 2 commits into
Open
Mohammed Alkindi (MohammedAlkindi) wants to merge 2 commits into
Mohammed Alkindi (MohammedAlkindi) wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The focused fix follows existing handling and includes an appropriate regression test, with no unresolved findings.
0 open findings
What changed in this PR
Prevents constant folding from freezing Bernoulli samples, preserving randomness across model runs.
Changes:
- Adds
Bernoullito the existing non-deterministic operator exclusion set. - Adds a regression test verifying it remains in the graph with constant input.
| File | Description |
|---|---|
| onnxscript/optimizer/_constant_folding.py | Excludes Bernoulli from constant folding. |
| onnxscript/optimizer/_constant_folding_test.py | Tests preservation of Bernoulli with constant probabilities. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Justin Chu (justinchuby)
approved these changes
Oct 20, 2026
Justin Chu (justinchuby)
enabled auto-merge (squash)
October 20, 2026 23:05
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3072 +/- ##
==========================================
+ Coverage 73.01% 73.02% +0.01%
==========================================
Files 267 267
Lines 32641 32647 +6
Branches 3101 3101
==========================================
+ Hits 23832 23841 +9
+ Misses 7767 7765 -2
+ Partials 1042 1041 -1 ☔ View full report in Codecov by Harness. |
Justin Chu (justinchuby)
pushed a commit
that referenced
this pull request
Oct 28, 2026
Constant folding treats a training-mode `Dropout` on a constant input like any other op: it runs the reference implementation once during optimization and stores the sampled result as an initializer. The optimized model then drops the same elements on every run instead of drawing a new mask. This is reachable from `torch.onnx.export`, which runs `optimize()` by default. For example, `F.dropout(self.weight, 0.5, training=True)` on a small parameter exports as `Dropout` followed by `Add`, and after optimization only the `Add` is left, reading a fixed tensor. Two exports of the same module bake in two different masks. The fix adds `Dropout` to `_NON_DETERMINISTIC_OPS`. The `Dropout` partial evaluator still runs before that check, so inference-mode and `ratio=0` dropouts are still replaced by `Identity` as before; only the random case stays in the graph. `test_dropout_in_training_mode_on_constant_input_is_not_folded` fails before (`['Add'] != ['Dropout', 'Add']`) and passes after. `onnxscript/optimizer`: 81 passed; `onnxscript/rewriter`: 581 passed, 3 skipped. This is the same kind of freeze as `Bernoulli` in #3072, kept separate because `Dropout` is only random in training mode. Happy to fold it into #3072 or rebase after it lands if you would rather handle it there.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bernoulliwas missing from_NON_DETERMINISTIC_OPS, so constant folding evaluated it once during optimization and replaced it with a fixed tensor. ABernoullion a constant probability input, such as a dropout-style mask, then returns the same sample on every run. It now stays in the graph, like theRandomUniform,RandomNormalandMultinomialops already in the set.test_bernoulli_is_not_foldedfails before ([] != ['Bernoulli']) and passes after.onnxscript/optimizer: 75 passed.ruff checkandruff format --checkare clean.