Skip to content

Guard CUDA LayerNorm/RMSNorm int32 offset range - #31650

Open
apsonawane wants to merge 1 commit into
mainfrom
msrc/cuda-layernorm-offset-overflow-fix
Open

Guard CUDA LayerNorm/RMSNorm int32 offset range#31650
apsonawane wants to merge 1 commit into
mainfrom
msrc/cuda-layernorm-offset-overflow-fix

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

This pull request adds input validation checks to prevent integer overflow issues during CUDA kernel indexing in the LayerNorm and RMSNorm CUDA operators. The main goal is to ensure that the product of num_rows and norm_size does not exceed INT_MAX, which could lead to incorrect behavior or crashes.

Input validation for CUDA kernel indexing:

  • Added a check in LayerNorm::ComputeInternal (in layer_norm.cc) to return an error if num_rows * norm_size exceeds INT_MAX, preventing integer overflow during CUDA kernel indexing.
  • Added a similar check in RMSNorm::ComputeInternal (in rms_norm.cc) to ensure the input size does not exceed CUDA kernel indexing limits.

Code maintenance:

  • Included the <limits> header in both layer_norm.cc and rms_norm.cc to support the new input validation logic. [1] [2]

Reject inputs where num_rows * norm_size exceeds INT_MAX before launching kernels that use int32 row offsets.

This prevents overflow in per-row pointer offset arithmetic in LayerNormalization and RMSNormalization CUDA paths.

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.

Pull request overview

This PR adds input-size validation to the CUDA implementations of LayerNormalization and RMSNormalization to prevent int32 overflow in CUDA kernel indexing (notably around num_rows * norm_size).

Changes:

  • Added a pre-launch guard in LayerNorm::ComputeInternal to reject inputs where num_rows * norm_size exceeds INT_MAX.
  • Added a similar guard in RMSNorm::ComputeInternal.
  • Included <limits> to support the new bounds checks.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
onnxruntime/core/providers/cuda/nn/layer_norm.cc Adds an INT_MAX-based size guard before launching the CUDA LayerNorm kernel.
onnxruntime/core/providers/cuda/nn/rms_norm.cc Adds the same INT_MAX-based size guard for CUDA RMSNorm (LayerNorm-based) execution.

Comment on lines +80 to +82
ORT_RETURN_IF(params.num_rows > 0 &&
params.norm_size > std::numeric_limits<int>::max() / params.num_rows,
"RMSNormalization input is too large for CUDA kernel indexing: num_rows * norm_size exceeds INT_MAX");
Comment on lines +92 to +95
ORT_RETURN_IF(params.num_rows > 0 &&
params.norm_size > std::numeric_limits<int>::max() / params.num_rows,
"LayerNormalization input is too large for CUDA kernel indexing: num_rows * norm_size exceeds INT_MAX");

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