Repository navigation
fix: decide constant folding structurally instead of by sampling (spec 07) - #25
Merged
Merged
Conversation
…c 07) GraphOptimizer's is_constant/is_constant_zero/is_constant_one previously sampled a node 3-4 times and compared the results to decide constancy. A low-entropy distribution (e.g. bernoulli(0.99)) had a high chance of sampling the same value repeatedly by chance and being silently folded into a constant, changing the distribution the optimized graph represents. ComputationNode::Leaf now carries a constant_value: Option<T>, set only by the new ComputationNode::constant constructor (and Uncertain::point, internally) or by the optimizer's own folded results. Every is_constant* check and constant-folding match arm reads this field directly instead of ever calling sample(), so constancy is now a structural fact about how a node was built rather than an inference from observed behavior.
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.
Summary
GraphOptimizer::is_constant/is_constant_zero/is_constant_onepreviously decided constancy by sampling a node 3-4 times and comparing results — a low-entropy distribution (e.g.bernoulli(0.99)) had a high chance of sampling the same value repeatedly by chance and being silently folded into a constant, changing the distribution the optimized graph actually represents.ComputationNode::Leafnow carriesconstant_value: Option<T>, set only by the newComputationNode::constant(value)constructor (andUncertain::point, internally) or by the optimizer's own folded results. Every constancy check and constant-folding match arm reads this field directly — zerosample()calls anywhere in the decision path, matching the spec's own grep acceptance test literally.GraphOptimizerisn't wired intoUncertain<T>'s arithmetic operators at all — it's an opt-in utility invoked manually. Unchanged by this spec, flagged for Spec 08 (effective CSE has the same characteristic).bernoulli(0.99)scenario, now a hard guarantee rather than statistical),point(0.0)+x/point(2.0)*point(3.0)folding through the real public API,normal(0, 1e-12)never folded, and a same-seed optimized-vs-unoptimized equivalence test (sample_with+ChaCha8Rng).examples/optimizations/graph_optimization.rs's literal0/1demo operands to useComputationNode::constant— with the old sampling-based check removed, the demo would otherwise silently stop demonstrating identity elimination/constant folding while still printing the same correct final number.ComputationNode::constant;Uncertain::point's signature/behavior is unchanged.Test plan
just devgreen (fmt, clippy-D warnings, tests default +parallel, doc tests, audit, deny, crap regression gate)--all-featuresand--features parallelawk/grepthat nosample()call appears inis_constant_zero/is_constant_one's bodiesexamples/optimizations/graph_optimization.rsmanually — still prints the correct 32.0 result both optimized and unoptimizedcrap_baseline.jsonregenerated (10 functions improved from simplified call sites, 2 now-unused helpers removed)CHANGELOG.md/specs/README.mdupdated