Skip to content

Do not delegate CoreML instance_norm with a rank 5 input - #23688

Open
hemanth1999k wants to merge 2 commits into
pytorch:mainfrom
hemanth1999k:coreml-reject-instance-norm-rank5
Open

hemanth1999k wants to merge 2 commits into
pytorch:mainfrom
hemanth1999k:coreml-reject-instance-norm-rank5

Conversation

@hemanth1999k

@hemanth1999k hemanth1999k commented Oct 11, 2026 •

Copy link
Copy Markdown

Summary

Core ML's instance_norm only accepts rank 3 and rank 4 inputs. InstanceNorm3d currently lowers to Core ML without complaint, then fails when the program loads:

parameter x[0] has invalid rank 5

CoreMLPartitioner now overrides support for instance_norm when its input is rank 5. ops_to_not_decompose therefore stops keeping it whole, it decomposes into ops Core ML runs, and the model stays delegated and matches eager. InstanceNorm1d and InstanceNorm2d are unchanged.

Fixes #11702

Test plan

Added test_instance_norm_3d_is_decomposed to backends/apple/coreml/test/test_coreml_partitioner.py. It covers InstanceNorm3d on (1, 3, 4, 4, 4) and an affine InstanceNorm3d with batch size 2 on (2, 3, 4, 5, 6). For each it checks that instance_norm is not in ops_to_not_decompose, lowers with to_edge_transform_and_lower, runs the program through executorch.runtime.Runtime, and compares with eager (atol/rtol 1e-2 because Core ML computes in fp16). It also checks that InstanceNorm2d is still kept whole.

python -m pytest backends/apple/coreml/test/test_coreml_partitioner.py

13/13 pass with the change; on main only the new test fails. Run on macOS 26.2, executorch 1.5.1 / main, torch 2.14.0, coremltools 9.0. ufmt and flake8 are clean.

cc @kimishpatel @YifanShenSZ @cymbalrush @metascroy @nil-is-all

Core ML's instance_norm only accepts rank 3 and 4 inputs. InstanceNorm3d
lowered fine, then failed to load:

  parameter x[0] has invalid rank 5

The partitioner now overrides support for instance_norm over a rank 5
input, so ops_to_not_decompose no longer keeps it whole. It decomposes
into ops Core ML runs, and the model stays delegated and matches eager,
including affine with batch size > 1. InstanceNorm1d/2d are unchanged.

Fixes pytorch#11702
@pytorch-bot

pytorch-bot Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⚠️ 16 Awaiting Approval

As of commit 5eb63a7 with merge base f2575be (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

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

@linux-foundation-easycla

linux-foundation-easycla Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: hemanth1999k / name: hemanth1999k (25f5acd)

@meta-cla

meta-cla Bot commented Oct 11, 2026

Copy link
Copy Markdown

Hi @hemanth1999k!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@hemanth1999k

Copy link
Copy Markdown
Author

@pytorchbot label "release notes: apple"

@hemanth1999k

Copy link
Copy Markdown
Author

/easycla

@pytorch-bot pytorch-bot Bot added the release notes: apple Changes to the Apple backend delegate label Oct 11, 2026
@meta-cla

meta-cla Bot commented Oct 11, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 11, 2026
@executorch-triage executorch-triage Bot added the community: contribution PRs coming from community (excluding hardware partners) label Oct 11, 2026
@nil-is-all nil-is-all added module: coreml Issues related to Apple's Core ML delegation and code under backends/apple/coreml/ backend tester This bug was found by the backend test suite. labels Oct 12, 2026
self.assertIn("executorch_call_delegate", op_names)
self.assertNotIn("aten.argmax.default", op_names)

def test_instance_norm_3d_is_decomposed(self):

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.

Can we test that rank 3/4 lower properly by testing the op is not in the lowered edge program, but executorch_call_delegate is, and then test rank 5 behaves differently.

See

self.assertIn("executorch_call_delegate", op_names)
self.assertNotIn("aten.argmax.default", op_names)

In the argmax example.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done. Rank 3 and rank 4 (with and without affine) now assert executorch_call_delegate is present, aten.instance_norm.default is not in the lowered edge program, and instance_norm is inside the delegate.

Rank 5 behaves differently: instance_norm is in neither the graph nor the delegate. It decomposes to _native_batch_norm_legit (+ view/repeat), which is still delegated, and the test asserts that. Without the partitioner change the rank 5 case fails these assertions, and on macOS it fails to load with "parameter x[0] has invalid rank 5".

Lower rank 3, 4 and 5 instance_norm and assert on the graph rather than
only on ops_to_not_decompose: rank 3 / 4 instance_norm is delegated
whole (a delegate call, no instance_norm left outside it), while rank 5
is decomposed, with no instance_norm in the graph or in the delegate,
and the batch-norm based decomposition delegated.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend tester This bug was found by the backend test suite. CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. community: contribution PRs coming from community (excluding hardware partners) module: coreml Issues related to Apple's Core ML delegation and code under backends/apple/coreml/ release notes: apple Changes to the Apple backend delegate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CoreML InstanceNorm3d fails to load

3 participants