Skip to content

Expand gemma_rmsnorm input dimension - #367

Open
MeowMiaoJ wants to merge 5 commits into
sgl-project:mainfrom
MeowMiaoJ:expand_gemma_rmsnorm
Open

Expand gemma_rmsnorm input dimension#367
MeowMiaoJ wants to merge 5 commits into
sgl-project:mainfrom
MeowMiaoJ:expand_gemma_rmsnorm

Conversation

@MeowMiaoJ

@MeowMiaoJ MeowMiaoJ commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

According to gemma3 model in sglang, gemma_rmsnorm may receive a 4d input. This PR expand the input dimension to flattenable/unflattenable 4d shape and add corresponding test.

@MeowMiaoJ
MeowMiaoJ force-pushed the expand_gemma_rmsnorm branch from 0be1a4f to 0cd7d1f Compare August 14, 2026 01:46
@MeowMiaoJ
MeowMiaoJ marked this pull request as ready for review August 19, 2026 01:49
Copilot AI lite review requested due to automatic review settings August 19, 2026 01:49

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 extends the SYCL RMSNorm stride-handling to accept Gemma-style 4D inputs (with a constrained leading dimension) and adds pytest coverage for 4D contiguous and non-flattenable view layouts, aligning sgl_kernel.gemma_rmsnorm with Gemma3 usage patterns in sglang.

Changes:

  • Allow _check_layer_norm_inputs to accept 4D inputs.
  • Extend get_row_strides() to support 4D tensors (requiring dim0 size 1) and derive strides from the last 3 dimensions.
  • Add new 4D gemma_rmsnorm test cases, including a non-flattenable QKV-slice-like layout and an invalid-leading-dim error test.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
tests/test_norm.py Adds 4D test coverage for gemma_rmsnorm, including non-flattenable stride patterns and error handling.
src/sycl/RMSNorm.cpp Extends row-stride derivation to 4D and uses it in gemma_rmsnorm/rmsnorm kernels.
src/sycl/Norm.h Updates input-dimension validation to allow 4D inputs in the shared norm input checker.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/sycl/RMSNorm.cpp
Comment on lines +64 to 72
// For 3D/4D tensors, the outer is the second-to-last dimension and
// the inner is the last dimension.
int64_t outer_stride = t.stride(-3);
int64_t inner_size = t.size(-2);
int64_t inner_stride = t.stride(-2);
if (((t.dim() == 3) && (t.size(0) == 1)) || t.size(-2) == 1 || outer_stride == inner_size * inner_stride) {
// Flattenable: a single stride describes all rows.
return {inner_stride, 1, 0};
}
Comment thread src/sycl/RMSNorm.cpp
// since our two-level (outer, inner) stride formula cannot represent
// a third level of striding.
TORCH_CHECK(
t.size(0) == 1, "get_row_strides: leading dimension 0 must have size 1 for a 4D tensor, got size ", t.size(0));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we support cases of t.size(0) > 1 ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We don't support t.size(0) > 1 because the only 4d size of gemma3's q/k input is [1, h, s, head_dim]. We can support t.size(0) > 1 for extensibility.

Comment thread src/sycl/RMSNorm.cpp
int64_t outer_stride = t.stride(-3);
int64_t inner_size = t.size(-2);
int64_t inner_stride = t.stride(-2);
if (t.size(-3) == 1 || outer_stride == inner_size * inner_stride) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please be aware of these copilot comments and ensure the tests cover these corner cases.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Cover tests by new commit.

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.

3 participants