Skip to content

native_group_norm: Handle non-contiguous tensors, rather than throwing - #186414

Closed
benjaminglass1 wants to merge 33 commits into
gh/benjaminglass1/140/basefrom
gh/benjaminglass1/140/head
Closed

benjaminglass1 wants to merge 33 commits into
gh/benjaminglass1/140/basefrom
gh/benjaminglass1/140/head

Conversation

@benjaminglass1

@benjaminglass1 benjaminglass1 commented Jun 5, 2026 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

native_group_norm and native_group_norm_backward have always assumed contiguity for some tensors, and asserted it for others. The assertions cause unexpected behavior for vmap, which may batch in non-contiguous ways. The assumptions (obviously) can cause memory access errors. This PR changes the implementations to instead call contiguous() with an appropriate memory format, which fixes vmap and the OOB accesses without using any extra memory in situations where callers have already ensured contiguous inputs.

This does mean that a situation which previously threw an error (non-contiguous X) now succeeds silently with an extra allocation. It's unlikely that anyone is relying on this behavior, however, since most people would have been calling native_group_norm through nn.functional.group_norm, which already did calls to contiguous().

Additional important changes:

  1. Short-circuit evaluation has been made more consistent, triggering whenever N == 0 and returning empty (or zero, for backward) tensors without ever calling the native kernels.
  2. The code now throws errors if either the number of channels or flattened inner size is 0, since the results are non-sensical (and incompatible with gradcheck) in either of those two cases.
  3. This PR removes a bypass for the contiguous() calls on the XPU backward pass (in derivatives.yaml). This is because XPU is already calling contiguous() internally, so the bypass was misleading to begin with. See https://github.com/intel/torch-xpu-ops/blob/main/src/ATen/native/xpu/sycl/GroupNormKernels.cpp#L1371.
  4. Two VMAP tests have been skipped for native_group_norm. They were previously XFAILs that failed on the first OpInfo test (i.e. relatively quickly), but now that they succeed they take 3-5 minutes each to run, which is just too long for our pipeline.
  5. The CompositeExplicitAutograd operator math_group_norm now does some basic validation on inputs to match the other group_norm operators.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jun 5, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⏳ No Failures, 2 Pending

As of commit bb2b9e9 with merge base 526b9ce (image):
💚 Looks good so far! There are no failures yet. 💚

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

[ghstack-poisoned]
@benjaminglass1 benjaminglass1 added ciflow/trunk Trigger trunk jobs on your pull request ciflow/inductor labels Jun 9, 2026
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@benjaminglass1
benjaminglass1 marked this pull request as ready for review June 10, 2026 17:24
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Starting merge as part of PR stack under #190794

pytorchmergebot pushed a commit that referenced this pull request Jul 25, 2026
Take a bunch of cases where we check for (ptr == nullptr) and just make them implicit bool(ptr), as well as some assorted reformatting.

Pull Request resolved: #190246
Approved by: https://github.com/slayton58
ghstack dependencies: #186414
Harsha0Sree pushed a commit to Harsha0Sree/pytorch that referenced this pull request Jul 25, 2026
pytorch#186414)

`native_group_norm` and `native_group_norm_backward` have always assumed contiguity for some tensors, and asserted it for others. The assertions cause unexpected behavior for `vmap`, which may batch in non-contiguous ways. The assumptions (obviously) can cause memory access errors. This PR changes the implementations to instead call `contiguous()` with an appropriate memory format, which fixes vmap and the OOB accesses without using any extra memory in situations where callers have already ensured contiguous inputs.

This does mean that a situation which previously threw an error (non-contiguous X) now succeeds silently with an extra allocation. It's unlikely that anyone is relying on this behavior, however, since most people would have been calling `native_group_norm` through `nn.functional.group_norm`, which already did calls to `contiguous()`.

Additional important changes:

1. Short-circuit evaluation has been made more consistent, triggering whenever N == 0 and returning empty (or zero, for backward) tensors without ever calling the native kernels.
2. The code now throws errors if either the number of channels or flattened inner size is 0, since the results are non-sensical (and incompatible with gradcheck) in either of those two cases.
3. This PR removes a bypass for the `contiguous()` calls on the XPU backward pass (in `derivatives.yaml`). This is because XPU is already calling `contiguous()` internally, so the bypass was misleading to begin with. See https://github.com/intel/torch-xpu-ops/blob/main/src/ATen/native/xpu/sycl/GroupNormKernels.cpp#L1371.
4. Two VMAP tests have been skipped for `native_group_norm`. They were previously XFAILs that failed on the first OpInfo test (i.e. relatively quickly), but now that they succeed they take 3-5 minutes each to run, which is just too long for our pipeline.
5. The `CompositeExplicitAutograd` operator `math_group_norm` now does some basic validation on inputs to match the other `group_norm` operators.
Pull Request resolved: pytorch#186414
Approved by: https://github.com/slayton58
Harsha0Sree pushed a commit to Harsha0Sree/pytorch that referenced this pull request Jul 25, 2026
Take a bunch of cases where we check for (ptr == nullptr) and just make them implicit bool(ptr), as well as some assorted reformatting.

Pull Request resolved: pytorch#190246
Approved by: https://github.com/slayton58
ghstack dependencies: pytorch#186414
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
pytorch#186414)

`native_group_norm` and `native_group_norm_backward` have always assumed contiguity for some tensors, and asserted it for others. The assertions cause unexpected behavior for `vmap`, which may batch in non-contiguous ways. The assumptions (obviously) can cause memory access errors. This PR changes the implementations to instead call `contiguous()` with an appropriate memory format, which fixes vmap and the OOB accesses without using any extra memory in situations where callers have already ensured contiguous inputs.

This does mean that a situation which previously threw an error (non-contiguous X) now succeeds silently with an extra allocation. It's unlikely that anyone is relying on this behavior, however, since most people would have been calling `native_group_norm` through `nn.functional.group_norm`, which already did calls to `contiguous()`.

Additional important changes:

1. Short-circuit evaluation has been made more consistent, triggering whenever N == 0 and returning empty (or zero, for backward) tensors without ever calling the native kernels.
2. The code now throws errors if either the number of channels or flattened inner size is 0, since the results are non-sensical (and incompatible with gradcheck) in either of those two cases.
3. This PR removes a bypass for the `contiguous()` calls on the XPU backward pass (in `derivatives.yaml`). This is because XPU is already calling `contiguous()` internally, so the bypass was misleading to begin with. See https://github.com/intel/torch-xpu-ops/blob/main/src/ATen/native/xpu/sycl/GroupNormKernels.cpp#L1371.
4. Two VMAP tests have been skipped for `native_group_norm`. They were previously XFAILs that failed on the first OpInfo test (i.e. relatively quickly), but now that they succeed they take 3-5 minutes each to run, which is just too long for our pipeline.
5. The `CompositeExplicitAutograd` operator `math_group_norm` now does some basic validation on inputs to match the other `group_norm` operators.
Pull Request resolved: pytorch#186414
Approved by: https://github.com/slayton58
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Take a bunch of cases where we check for (ptr == nullptr) and just make them implicit bool(ptr), as well as some assorted reformatting.

Pull Request resolved: pytorch#190246
Approved by: https://github.com/slayton58
ghstack dependencies: pytorch#186414
MelindaTan pushed a commit to MelindaTan/pytorch that referenced this pull request Jul 31, 2026
pytorch#186414)

`native_group_norm` and `native_group_norm_backward` have always assumed contiguity for some tensors, and asserted it for others. The assertions cause unexpected behavior for `vmap`, which may batch in non-contiguous ways. The assumptions (obviously) can cause memory access errors. This PR changes the implementations to instead call `contiguous()` with an appropriate memory format, which fixes vmap and the OOB accesses without using any extra memory in situations where callers have already ensured contiguous inputs.

This does mean that a situation which previously threw an error (non-contiguous X) now succeeds silently with an extra allocation. It's unlikely that anyone is relying on this behavior, however, since most people would have been calling `native_group_norm` through `nn.functional.group_norm`, which already did calls to `contiguous()`.

Additional important changes:

1. Short-circuit evaluation has been made more consistent, triggering whenever N == 0 and returning empty (or zero, for backward) tensors without ever calling the native kernels.
2. The code now throws errors if either the number of channels or flattened inner size is 0, since the results are non-sensical (and incompatible with gradcheck) in either of those two cases.
3. This PR removes a bypass for the `contiguous()` calls on the XPU backward pass (in `derivatives.yaml`). This is because XPU is already calling `contiguous()` internally, so the bypass was misleading to begin with. See https://github.com/intel/torch-xpu-ops/blob/main/src/ATen/native/xpu/sycl/GroupNormKernels.cpp#L1371.
4. Two VMAP tests have been skipped for `native_group_norm`. They were previously XFAILs that failed on the first OpInfo test (i.e. relatively quickly), but now that they succeed they take 3-5 minutes each to run, which is just too long for our pipeline.
5. The `CompositeExplicitAutograd` operator `math_group_norm` now does some basic validation on inputs to match the other `group_norm` operators.
Pull Request resolved: pytorch#186414
Approved by: https://github.com/slayton58
MelindaTan pushed a commit to MelindaTan/pytorch that referenced this pull request Jul 31, 2026
Take a bunch of cases where we check for (ptr == nullptr) and just make them implicit bool(ptr), as well as some assorted reformatting.

Pull Request resolved: pytorch#190246
Approved by: https://github.com/slayton58
ghstack dependencies: pytorch#186414
jansel added a commit that referenced this pull request Aug 9, 2026
Root cause: export recorded guards from metadata-only layout, broadcast, and view decisions that used example-shape truth for symbolic size-one checks. In the issue repro, temporal slicing feeds GroupNormSpatial through flatten, reshape, and group_norm; those checks excluded valid values from the declared Dim range.

Use static proofs for optional metadata fast paths that can safely fall back, while preserving guard-based behavior for normal torch.compile broadcasting. Preserve the original requested fake-view shape across retries, avoid specializing unknown source dimensions when reshaping to a size-one target, and directly flatten the statically size-one leading-dimension case needed by the repro. Symbolic group_norm inputs clone into a statically known-safe memory format instead of invoking guard-producing contiguity checks.

After rebasing over #186414, native_group_norm owns concrete input and affine contiguity. Keep only the high-level symbolic input clone here and pass weight and bias through unchanged, avoiding duplicate validation and conversions while retaining the guard-free symbolic path.

Accuracy runs also reapply the existing determinism setup after benchmark model construction. Some model constructors reset global determinism flags, which otherwise makes the eager-vs-eager check fail before the compiler path runs.

Fixes #143463

Generated by my agent

Test Plan:

```bash
.venv/bin/python -m pip install -e . -v --no-build-isolation
.venv/bin/python test/export/test_export.py TestDynamismExpression.test_reshape_view_static_size_one_before_dynamic_dim TestDynamismExpression.test_group_norm_spatial_dynamic_temporal_dim TestDynamismExpression.test_reshape_view_backed_size_oblivious TestDynamismExpression.test_view_unify_cross_symbols -q
.venv/bin/python test/export/test_export.py TestDynamismExpression.test_group_norm_spatial_dynamic_temporal_dim_cuda -q
.venv/bin/python test/export/test_export.py TestExport.test_dim_dynamic -q
.venv/bin/python test/test_dynamic_shapes.py TestUbackedOps.test_unbacked_reshape1 TestUbackedOps.test_backed_size_oblivious_broadcast -q
.venv/bin/python -m py_compile torch/_refs/__init__.py test/export/test_export.py
CPATH=/usr/lib/gcc/x86_64-redhat-linux/11/include lintrunner -a
git diff --check
git diff --cached --check
```

ghstack-source-id: e954362

Pull-Request: #186032
@github-actions
github-actions Bot deleted the gh/benjaminglass1/140/head branch August 24, 2026 02:10
minmingzhu pushed a commit to minmingzhu/torch-xpu-ops that referenced this pull request Sep 15, 2026
XFailed in PyTorch:
pytorch/pytorch@6d13b65
PR correctly addressing the issue in PyTorch:
pytorch/pytorch#186414

Fixes: intel#4044

Signed-off-by: Artur Kłoniecki <[email protected]>
Co-authored-by: Benedykt Bela <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/mps Run MPS tests (subset of trunk) ciflow/trunk Trigger trunk jobs on your pull request Merged open source topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants