Skip to content

Implement output_shapes for RMSNormVJP and LayerNormVJP - #5010

Open
archidoge0 wants to merge 2 commits into
ml-explore:mainfrom
archidoge0:fix/norm-vjp-shapeless
Open

archidoge0 wants to merge 2 commits into
ml-explore:mainfrom
archidoge0:fix/norm-vjp-shapeless

Conversation

@archidoge0

Copy link
Copy Markdown
Contributor
  • ☑️ I understand it is strictly prohibited to use AI to write PR description
  • AI usage disclosure: Used AI to help write the shapeless gradient unit tests in test_fast.py; PR description written manually.

Fixes #4985.

RMSNormVJP and LayerNormVJP are multi-output primitives, so compile_replace(..., shapeless=True) calls Primitive::output_shapes on them when replaying mx.compile(mx.grad(...), shapeless=True). Neither primitive implemented output_shapes, raising ValueError at replay time.

This adds output_shapes to both VJP primitives in mlx/fast_primitives.h:

  • RMSNormVJP: {inputs[0].shape(), inputs[1].shape()} (gx, gw, including the scalar placeholder when weight=None)
  • LayerNormVJP: {inputs[0].shape(), inputs[1].shape(), inputs[2].shape()} (gx, gw, gb)

Added test_rms_norm_grad_shapeless and test_layer_norm_grad_shapeless in python/tests/test_fast.py covering optional weight/bias, varying batch and feature sizes (including looped-kernel dims 4099 / 8196 and empty batch (0, 64)), verified locally on Metal (M3 Max).

@nastya236

nastya236 commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Thank you for the contribution! We haven't noticed it before because for training we usually compile for a specific shape.
Some tests are failing, do you mind to check?

@nastya236
nastya236 self-requested a review November 29, 2026 00:42
@nastya236 nastya236 added the await response This pull request is waiting for response from the author. label Nov 29, 2026
@archidoge0

archidoge0 commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor Author

@nastya236

Thank you for the contribution! We haven't noticed it before because for training we usually compile for a specific shape. Some tests are failing, do you mind to check?

Thanks for checking! Those failures were not caused by RMSNormVJP / LayerNormVJP, the two edge-case shapes in my test hit two separate bugs on main:

  1. (2, 129) on CUDA failed because cu::layer_norm_vjp (layer_norm.cu) indexes the 4-element wn / gn vectors with i instead of i - index * N_READS when axis_size % N_READS != 0 (reported in [BUG] [CUDA] mx.fast.layer_norm: wrong input gradient when last dimension is not a multiple of vector width #5013, fix in [CUDA] Fix tail vector indexing in layer_norm_vjp #5014).
  2. (0, 64) on CPU failed because Compiled::output_shapes (compile.cpp) uses std::max(0, 1) -> 1 instead of broadcast_shapes, returning shape (1, 64) instead of (0, 64) (reported in [BUG] [Compile] mx.compile(shapeless=True): SIGSEGV on empty broadcast and out-of-bounds read on mismatched shapes #5015, fix in Use broadcast_shapes in Compiled::output_shapes #5016).

I updated those two shapes in test_fast.py (8d5cda3) so this PR tests #4985 cleanly on its own. Could you re-run CI when you have a chance?

@zcbenz zcbenz removed the await response This pull request is waiting for response from the author. label Nov 29, 2026
@zcbenz

zcbenz commented Nov 29, 2026

Copy link
Copy Markdown
Member

Can you take care of the crash in CPU backend? Probably another bug discovered 😅

@zcbenz zcbenz added the await response This pull request is waiting for response from the author. label Nov 29, 2026
@archidoge0

Copy link
Copy Markdown
Contributor Author

Can you take care of the crash in CPU backend? Probably another bug discovered 😅

@zcbenz already posted below/above (I don’t know how GitHub order the thread…

@zcbenz

zcbenz commented Nov 29, 2026

Copy link
Copy Markdown
Member

You said you updated the tests to test the bug cleanly so I thought it would avoid the crash, I can rerun CI after merging #5016.

@zcbenz zcbenz added await dependency This pull request is waiting for other work to be merged first. and removed await response This pull request is waiting for response from the author. labels Nov 29, 2026
@zcbenz
zcbenz force-pushed the fix/norm-vjp-shapeless branch from 8d5cda3 to 99e218f Compare November 29, 2026 08:13
@zcbenz zcbenz added await response This pull request is waiting for response from the author. and removed await dependency This pull request is waiting for other work to be merged first. labels Nov 29, 2026
@zcbenz

zcbenz commented Nov 29, 2026

Copy link
Copy Markdown
Member

The CPU backend is still crashing at the tests.

@archidoge0

Copy link
Copy Markdown
Contributor Author

The CPU backend is still crashing at the tests.

Fix via: #5020

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

await response This pull request is waiting for response from the author.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] [Compile] mx.compile(shapeless=True): ValueError on mx.grad of mx.fast.rms_norm and mx.fast.layer_norm

3 participants