Repository navigation
Support InstanceNorm3d in the PyTorch converter - #2914
Open
sandeepvijayarao09 wants to merge 1 commit into
Open
sandeepvijayarao09 wants to merge 1 commit into
sandeepvijayarao09 wants to merge 1 commit into
Conversation
The instance_norm MIL op only supports rank 3 and 4 inputs, so
nn.InstanceNorm3d produced a model that failed to compile as an ML
program ("invalid rank 5, expected [3, 4]") and gave wrong results on
the neuralnetwork backend.
For rank-5 inputs with static N, C and W, fold (D, H, W) into
(D * H, W), run the rank-4 instance_norm and reshape back. Instance norm
reduces over all spatial dims of each (N, C) slice, so the result is
unchanged. When N, C or W is dynamic, compute the normalization with
reduce_mean and variance instead.
Fixes apple#1989
sandeepvijayarao09
force-pushed
the
instance-norm-3d
branch
from
November 6, 2026 00:36
04900a3 to
813ca7d
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1989.
Problem
The
instance_normMIL op only accepts inputs of rank 3 or 4, but the torch frontend passed rank-5 inputs straight through. As a result,nn.InstanceNorm3d:parameter x[0] has invalid rank 5, expected [3, 4]neuralnetworkbackend, converted and ran but silently gave wrong output (max abs error ≈ 3.2 against PyTorch on a random(2, 3, 4, 5, 6)input)Change
In the torch
instance_normconverter, rank-5 inputs now take one of two paths:(N, C, D, H, W)→(N, C, D*H, W), run the existing rank-4instance_norm, then reshape back. Instance norm reduces over all spatial dimensions of each(N, C)slice, so folding D into H doesn't change the result, and the fused op is still used. If D or H is dynamic, the target shape is[N, C, -1, W]and the output is reshaped back withshape(x).reduce_meanand the existing_varhelper, then apply the per-channel affine. This is the same approach_group_norm_impluses. A reshape driven only by a runtime shape loses the static channel count, which fails both on the neuralnetwork backend and in the Core ML runtime, so that route doesn't work here.Rank 3 and 4 behavior is unchanged.
Tests
TestInstanceNormintest_torch_ops.pygets two new tests:test_instancenorm_3d: all compute units, backends and frontends,num_features∈ {5, 1},affine∈ {True, False} (with randomized affine params)test_instancenorm_3d_dynamic_shape:RangeDimon D and H (reshape path), plus D, H and W (decomposition path), with and without affineI ran
TestInstanceNorm,TestGroupNormandTestNormslocally (macOS, Apple silicon, Python 3.12, torch 2.8.0): 444 passed, 0 failed, including all 24 new 3D cases.