Skip to content

XNNPACK batch norm fusion crashes on any top-level nn.Sequential #21541

Description

@slipstr34m

While verifying #21489 I exported a textbook CNN written as one top-level nn.Sequential (conv, batchnorm, relu, maxpool). With the pooling crash fixed, the same model now dies one pass later: FuseBatchNormPass crashes before any support decision is reached. The identical layers behind a named attribute lower fine.

Repro

import torch
import torch.nn as nn
from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner
from executorch.exir import to_edge_transform_and_lower

model = nn.Sequential(
    nn.Conv2d(3, 16, 3, padding=1),
    nn.BatchNorm2d(16),
    nn.ReLU(),
).eval()

ep = torch.export.export(model, (torch.randn(1, 3, 32, 32),))
to_edge_transform_and_lower(ep, partitioner=[XnnpackPartitioner()])
Exception: An error occurred when running the 'FuseBatchNormPass' pass
...
ValueError: '0_weight_fused_bn' is not in list
  backends/xnnpack/_passes/fuse_batch_norm.py:241, in create_constant_placeholder
  backends/transforms/utils.py:151, in node_names.index(name)

Cause

A top-level nn.Sequential names its parameters 0.weight, 1.weight, so the fused weight placeholder is requested as 0_weight_fused_bn. torch.fx refuses a leading digit and assigns _0_weight_fused_bn instead, but create_constant_placeholder then looks the node up by the requested name:

node = graph.create_node(op="placeholder", name=name, target=name)
...
node_index = node_names.index(name)  # ValueError: fx assigned node.name, not name

Any rename triggers it, not just leading digits. #14055 hits the same lines through Vulkan because torchvision regnet module names contain a hyphen. Models with named submodules produce FQNs that sanitize cleanly, which is why existing coverage never reaches this path.

runtime/test/test_runtime_xnnpack.py carries a test_conv_bn skipped on exactly this crash.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions