Skip to content

Keep synthesized layer_norm gamma/beta at normalized_shape - #55

Merged
gokulkrishna98 merged 2 commits into
apple:mainfrom
gokulkrishna98:dev/gokul/layer_norm_test_fix
Aug 12, 2026
Merged

Keep synthesized layer_norm gamma/beta at normalized_shape#55
gokulkrishna98 merged 2 commits into
apple:mainfrom
gokulkrishna98:dev/gokul/layer_norm_test_fix

Conversation

@gokulkrishna98

Copy link
Copy Markdown
Contributor

When elementwise_affine=False, replace_layer_norm synthesized the identity gamma/beta as a flat numel-element vector, so a layer norm over normalized_shape = (4, 8) emitted a composite taking tensor<32xf32> gamma/beta while its declaration said axes = [1, 2]. Numerics were correct (the body reshaped them back), but the composite boundary contradicted the declared axes and differed from the elementwise_affine=True case, where ATen's params arrive already shaped (4, 8).

Build the constants at normalized_shape so both modes present the same interface, and drop the now-dead reshape.

Test plan:

  • pytest tests/ops/test_ops_ir.py -k LayerNorm (new IR regression test)
  • pytest tests/ops/test_ops.py -k "LayerNorm or layer_norm"
  • pytest tests/ops -n auto

When elementwise_affine=False, replace_layer_norm built the identity
gamma/beta as a flat numel-element vector, so a layer_norm over
normalized_shape (4, 8) produced a composite taking tensor<32xf32>
gamma/beta while its declaration said axes = [1, 2]. The body reshaped
them back before broadcasting, so numerics were correct, but the
composite boundary was inconsistent with the declared axes and differed
from the elementwise_affine=True case, where ATen's real params arrive
already shaped (4, 8). A consumer that implements the composite from its
declaration instead of inlining the body sees a rank-1 gamma against two
normalized axes.

Build the constants with np.ones/np.zeros at normalized_shape so both
paths present the same interface, and drop the now-dead reshape.
@gokulkrishna98
gokulkrishna98 force-pushed the dev/gokul/layer_norm_test_fix branch from 6daf3e0 to 5ef1bd1 Compare August 11, 2026 18:08
@gokulkrishna98
gokulkrishna98 marked this pull request as ready for review August 11, 2026 20:17
@gokulkrishna98
gokulkrishna98 merged commit 5fbd5bd into apple:main Aug 12, 2026
2 checks passed
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.

2 participants