Skip to content

Check dim order in the optimized layer_norm as the portable one does - #21866

Open
SuryanshSS1011 wants to merge 1 commit into
pytorch:mainfrom
SuryanshSS1011:fix/optimized-layer-norm-dim-order
Open

Check dim order in the optimized layer_norm as the portable one does#21866
SuryanshSS1011 wants to merge 1 commit into
pytorch:mainfrom
SuryanshSS1011:fix/optimized-layer-norm-dim-order

Conversation

@SuryanshSS1011

Copy link
Copy Markdown
Contributor

Summary

Related to #21828 and #21865 (same underlying assumption in the optimized kernel library).

aten.native_layer_norm returns wrong data on a channels-last input, off by 3.192 against eager PyTorch where it matches exactly on contiguous input. Nothing errors.

The portable kernel is fine. It carries this, with the reason written down:

// Only support default dim order for now.
// TODO: Support other dim orders.
ET_KERNEL_CHECK(
    ctx, tensor_is_default_dim_order(input), InvalidArgument, ret_val);

The optimized kernel has neither that check nor the matching tensors_have_same_dim_order, and native_layer_norm.out is mapped to torch::executor::opt_native_layer_norm_out in optimized.yaml. So any build with EXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON gets the ungated one.

It needs the guard rather than stride-aware indexing, because it splits the buffer into M rows of N contiguous elements:

const size_t M = getLeadingDims(input, dim);
const size_t N = getTrailingDims(input, dim) * dim_size;

That layout only exists in the default dim order. This copies the two checks across from the portable kernel so the two agree.

Test plan

OpNativeLayerNormTest.NonDefaultDimOrderDies passes a channels-last input with out, mean and rstd all channels-last, so the same dim order check passes and only the default dim order check can reject. mean and rstd share the input's rank with the normalized dims set to 1, which the kernel requires before it reaches any dim order check.

The test file is shared by both kernel libraries, so it runs against the portable kernel too, where it already passes.

Copilot AI lite review requested due to automatic review settings August 15, 2026 03:22
@pytorch-bot

pytorch-bot Bot commented Aug 15, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/21866

Note: Links to docs will display an error until the docs builds have been completed.

⚠️ 13 Awaiting Approval

As of commit 128d5cb with merge base 7dd3335 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 15, 2026
@SuryanshSS1011

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "release notes: ops & kernels"

@pytorch-bot pytorch-bot Bot added the release notes: ops & kernels Changes to the opset and any new / changed kernel implementations label Aug 15, 2026
Copilot AI review requested due to automatic review settings August 15, 2026 03:48
@SuryanshSS1011
SuryanshSS1011 force-pushed the fix/optimized-layer-norm-dim-order branch from 05bbe79 to 128d5cb Compare August 15, 2026 03:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. release notes: ops & kernels Changes to the opset and any new / changed kernel implementations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants