Skip to content

Conversation

@Jonahcb
Copy link
Contributor

@Jonahcb Jonahcb commented Sep 13, 2025

Description

  • Relax LayerNorm input validation to accept norm_size == 1; only reject zero-size (and less than zero).
    • Change norm_size <= 1norm_size < 1.
    • Update error text to “larger than 0”.
  • File: onnxruntime/core/providers/cpu/nn/layer_norm_helper.h
  • Add tests for the degenerate case (normalized extent is 1):
    • LayerNorm_NormSizeEqualsOne_Succeeds: with bias → output equals bias.
    • LayerNorm_NormSizeEqualsOne_NoBias: without bias → output all zeros.
  • File: onnxruntime/test/contrib_ops/layer_norm_op_test.cc
  • No compute-kernel changes; existing CPU/CUDA implementations already produce normalized zeros for norm_size == 1, yielding bias (if provided) as final output.

Motivation and Context

  • Fixes a regression where LayerNormalization errored for norm_size == 1:

    Size of X.shape[axis:] must be larger than 1, got 1

  • ONNX spec does not forbid norm_size == 1, and PyTorch returns zeros in this case; ONNX Runtime should align for backward compatibility with older models.
  • Semantics for norm_size == 1: mean == x, var == 0normalized == 0 → output equals bias if provided, else 0.
  • No behavior change for norm_size > 1.

Resolves: #25896


Files Changed

  • onnxruntime/core/providers/cpu/nn/layer_norm_helper.h
    • Allow norm_size == 1; only reject norm_size < 1.
    • Update error message to “larger than 0”.
  • onnxruntime/test/contrib_ops/layer_norm_op_test.cc
    • Replace failure case for size 1 with success tests (with/without bias).

@Jonahcb
Copy link
Contributor Author

Jonahcb commented Sep 15, 2025

@kunal-vaishnavi This failure doesn't seem to be related to my edits. Are you able to assign reviewers to get this merged? Thank you!

@Jonahcb
Copy link
Contributor Author

Jonahcb commented Sep 19, 2025

@snnn @fs-eire @skottmckay @yuslepukhin @kunal-vaishnavi It's not clear to me who is responsible for which part of the repository, but could you please assign reviewers to this so that this bug can be patched? Thank you!

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