Skip to content

Const fold scatter_along_axis and scaled_dot_product_attention correctly - #2802

Open
LeSingh1 wants to merge 1 commit into
apple:mainfrom
LeSingh1:value-inference-ignored-inputs
Open

Const fold scatter_along_axis and scaled_dot_product_attention correctly#2802
LeSingh1 wants to merge 1 commit into
apple:mainfrom
LeSingh1:value-inference-ignored-inputs

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Two value_inference implementations ignore an input that changes the result, so const folding produces a different answer than execution.

scatter_along_axis always computes mode="update", but the op's default is add. scaled_dot_product_attention ignores attn_mask, so a const-folded call silently produces unmasked attention.

Both now honour the input, or decline to fold when they can't.

Tested with real predictions on this machine: each new test fails on main and passes here.

Both ops had a value_inference that ignored an input which changes the
result, so a program whose operands happen to be const folds to the wrong
value and the op disappears from the model.

scatter_along_axis: value_inference always called np.put_along_axis, i.e.
it always computed mode="update". Every other mode was folded to the
overwrite result, including the default mode="add". Compute the mode's
reduction with the matching numpy ufunc applied through ufunc.at so that
repeated indices accumulate, which is what the runtime does.

scaled_dot_product_attention: value_inference only applied attn_mask when
the mask had a value, and otherwise silently produced unmasked attention.
This op has no @precondition, so with const query/key/value and a mask
computed at runtime the whole attention collapsed to a const of the
unmasked result. Return None instead, so the op stays in the graph.
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