const_deduplication: compare constexpr inputs by value, not by str(ndarray) - #2805
Open
LeSingh1 wants to merge 1 commit into
Open
const_deduplication: compare constexpr inputs by value, not by str(ndarray)#2805LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
…array) find_constexprs builds a hash key from each input, and for inputs below const_threshold it uses str(v.val). Unlike find_constants, which re-checks the actual values when a hash bucket is hit, find_constexprs takes the string as the answer, and then merges with force_replace=True, which also turns off the check inside replace_uses_of_var_after_op. str(np.ndarray) is not a value comparison. It rounds to `precision` fractional digits and summarizes arrays longer than `threshold`, and both are read from the process wide np.printoptions. Two per channel quantization scales one ulp apart both render as "[0.0001 ...]", so the two constexpr_affine_dequantize ops that carry them are merged and the second layer runs with the first layer's scales. With printoptions changed anywhere in the process, arbitrarily different values collide. Key on the exact bytes instead. This only ever merges fewer ops than before: inputs that are genuinely equal still produce an equal key.
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.
const_deduplicationusedstr(np.ndarray)as an equality key and then merged withforce_replace=True, without re-checking the values. numpy truncates long arrays, so two different per-channel quantization scales both render as[0.0001 ...]and get merged. The rendering also depends on process-globalnp.printoptions.Constexpr inputs are now compared by value.
The new test fails on
main. I left the neighbouringnp.allclose(rtol=0, atol=DTYPE2ATOL[dtype])comparison infind_constantsalone — it also merges non-equal constants, but the tolerance looks deliberate and that is a separate call.