Skip to content

const_deduplication: compare constexpr inputs by value, not by str(ndarray) - #2805

Open
LeSingh1 wants to merge 1 commit into
apple:mainfrom
LeSingh1:const-dedup-equality
Open

const_deduplication: compare constexpr inputs by value, not by str(ndarray)#2805
LeSingh1 wants to merge 1 commit into
apple:mainfrom
LeSingh1:const-dedup-equality

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

const_deduplication used str(np.ndarray) as an equality key and then merged with force_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-global np.printoptions.

Constexpr inputs are now compared by value.

The new test fails on main. I left the neighbouring np.allclose(rtol=0, atol=DTYPE2ATOL[dtype]) comparison in find_constants alone — it also merges non-equal constants, but the tolerance looks deliberate and that is a separate call.

…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.
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.

1 participant