Skip to content

Support InstanceNorm3d in the PyTorch converter - #2914

Open
sandeepvijayarao09 wants to merge 1 commit into
apple:mainfrom
sandeepvijayarao09:instance-norm-3d
Open

sandeepvijayarao09 wants to merge 1 commit into
apple:mainfrom
sandeepvijayarao09:instance-norm-3d

Conversation

@sandeepvijayarao09

@sandeepvijayarao09 sandeepvijayarao09 commented Nov 5, 2026 •

Copy link
Copy Markdown

Fixes #1989.

Problem

The instance_norm MIL op only accepts inputs of rank 3 or 4, but the torch frontend passed rank-5 inputs straight through. As a result, nn.InstanceNorm3d:

  • converted, but the ML program failed to compile: parameter x[0] has invalid rank 5, expected [3, 4]
  • on the neuralnetwork backend, 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_norm converter, rank-5 inputs now take one of two paths:

  • Static N, C and W (the common case): reshape (N, C, D, H, W) → (N, C, D*H, W), run the existing rank-4 instance_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 with shape(x).
  • Dynamic N, C or W: compute the normalization directly with reduce_mean and the existing _var helper, then apply the per-channel affine. This is the same approach _group_norm_impl uses. 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

TestInstanceNorm in test_torch_ops.py gets 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: RangeDim on D and H (reshape path), plus D, H and W (decomposition path), with and without affine

I ran TestInstanceNorm, TestGroupNorm and TestNorms locally (macOS, Apple silicon, Python 3.12, torch 2.8.0): 444 passed, 0 failed, including all 24 new 3D cases.

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

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

InstanceNorm3D missing

1 participant