Skip to content

Validate SkipLayerNorm prepacked lengths - #31676

Open
Akshay Sonawane (apsonawane) wants to merge 1 commit into
mainfrom
fix/skip-layernorm-prepack-hidden-size-check
Open

Validate SkipLayerNorm prepacked lengths#31676
Akshay Sonawane (apsonawane) wants to merge 1 commit into
mainfrom
fix/skip-layernorm-prepack-hidden-size-check

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

This pull request improves the robustness and correctness of the SkipLayerNorm operator in ONNX Runtime by adding stricter validation of prepacked tensor sizes and enhancing test coverage for these checks. The changes ensure that mismatches in tensor dimensions are detected early, preventing potential runtime errors.

Validation and Error Checking Enhancements:

  • Added explicit size validation for prepacked tensors (skip, gamma, beta, bias) in SkipLayerNorm::Compute, ensuring their lengths match the expected hidden_size and providing clear error messages when mismatches occur.
  • Introduced new member variables to track the sizes of prepacked gamma, beta, and bias tensors, and updated their management in the constructor and PrePack method. [1] [2] [3]

Testing Improvements:

  • Added two new unit tests to verify that the operator correctly rejects prepacked tensors with incorrect sizes for gamma and skip, confirming that the new validation logic works as intended.

Co-authored-by: Copilot <223556219@users.noreply.github.com>

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.

🟡 Changes recommended

The new size-validation code has a signed/unsigned arithmetic issue that should be fixed, and the added checks for beta/bias lack corresponding unit test coverage.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.

Pull request overview

This PR tightens runtime validation around prepacked (FP16→FP32 converted) inputs for the contrib CPU SkipLayerNormalization / SkipSimplifiedLayerNormalization kernels, so malformed or mismatched prepacked tensors are rejected early with clearer error messages. It also adds unit tests to ensure the new validation is exercised.

Changes:

  • Added member state to track prepacked gamma/beta/bias tensor lengths and initialized them in the kernel constructor.
  • Added explicit size checks in SkipLayerNorm::Compute to validate prepacked skip/gamma/beta/bias lengths against hidden_size, returning descriptive failures.
  • Added two unit tests verifying failures for incorrect prepacked gamma and skip sizes.
File summaries
File Description
onnxruntime/test/contrib_ops/skiplayernorm_op_test.cc Adds unit tests for prepack rejection on invalid gamma and skip sizes.
onnxruntime/contrib_ops/cpu/skip_layer_norm.h Introduces new members to track prepacked tensor sizes.
onnxruntime/contrib_ops/cpu/skip_layer_norm.cc Implements hidden-size validation and prepacked size checks; records prepacked sizes in PrePack.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 2
  • Review effort level: Lite

We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.

Comment on lines 141 to +149
int hidden_size = static_cast<int>(input_dims[input_dims_size - 1]);
const size_t hidden_size_size = static_cast<size_t>(hidden_size);
ORT_RETURN_IF(hidden_size <= 0, "hidden_size must be positive.");

if (prepacked_skip_fp32_data_) {
ORT_RETURN_IF(prepacked_skip_fp32_size_ < hidden_size_size || (prepacked_skip_fp32_size_ % hidden_size_size) != 0,
"Prepacked skip length does not match hidden_size. hidden_size=", hidden_size,
", prepacked skip length=", prepacked_skip_fp32_size_, ".");
}
kNnapiExecutionProvider, kQnnExecutionProvider});
}

TEST(SkipLayerNormTest, SkipLayerNormPrePackRejectsShortGamma) {
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