Commit a7953c5
[Qualcomm] Support native_layer_norm and affine-free LayerNorm in QNN backend (pytorch#18990)
# [Qualcomm] Support native_layer_norm and affine-free LayerNorm in QNN
backend
## Summary
Adds QNN backend support for `aten.native_layer_norm.default` (which is
the decomposed form of `torch.nn.LayerNorm`) and handles models where
weight/bias are not provided (`elementwise_affine=False`).
## Problem
When exporting models with `torch.native_layer_norm` or
`torch.nn.LayerNorm(affine=False)` to the QNN backend, the following
issues occur:
1. **Missing `native_layer_norm` visitor**: The original
`LayerNormVisitor` only targets `aten.layer_norm.default`, but PyTorch
decomposes `torch.nn.LayerNorm` to `aten.native_layer_norm.default`
during export.
2. **None weight/bias**: When `elementwise_affine=False`, the weight and
bias arguments are `None`. QNN x86_64 runtime cannot handle `None`
tensor inputs, causing `AttributeError` when calling `get_parameter()`.
## Solution
### 1. Update visitor target (`op_layer_norm.py`)
Change the visitor target from `aten.layer_norm.default` to
`aten.native_layer_norm.default`:
```python
# Before
target = ["aten.layer_norm.default"]
# After
target = ["aten.native_layer_norm.default"]
```
This is correct because during ExecuTorch export,
`aten.layer_norm.default` is decomposed to
`aten.native_layer_norm.default` **before** the QNN lowering stage.
### 2. Handle None weight/bias (`op_layer_norm.py`)
When weight/bias are `None`, create synthetic tensors:
- Missing weight → `torch.ones(normalized_shapes)` (identity transform)
- Missing bias → `torch.zeros(normalized_shapes)` (no offset)
Create synthetic `fx.Node` objects to register these as QNN static
tensors:
```python
weight_tensor = torch.ones(normalized_shapes, dtype=torch.float32)
weight_node = torch.fx.Node(
node.graph,
node.name + "_runtime_weight",
"call_function",
exir_ops.edge.aten.tensor.default,
(),
{},
)
# Preserve quant_attrs with zero_point=0 for QNN compatibility
```
### 3. Use same annotator for both ops (`htp_rules.py`)
The quantizer annotator registers both `aten.layer_norm.default` and
`aten.native_layer_norm.default` to the same `LayerNorm` class, since
both ops have identical argument schemas:
```python
@register_annotator(
[torch.ops.aten.layer_norm.default, torch.ops.aten.native_layer_norm.default],
QnnConstants.OpLayerNorm.op_name,
)
```
### 4. Add None check to `get_parameter()` (`utils.py`)
Guard against `None` nodes to prevent `AttributeError`:
```python
if node is None:
return None
```
## Files Changed
| File | Changes |
|------|---------|
| `builders/op_layer_norm.py` | Add `native_layer_norm` support + handle
None weight/bias |
| `builders/utils.py` | Add None guard in `get_parameter()` |
| `quantizer/annotators/htp_rules.py` | Register annotator for both ops
|
| `tests/models.py` | Add `NativeLayerNorm` test model |
| `tests/test_qnn_delegate.py` | Add floating-point and quantized tests
|
## Test Plan
Run QNN delegate tests for layer_norm:
```bash
python backends/qualcomm/tests/test_qnn_delegate.py \
-k "test_qnn_backend_layer_norm or test_qnn_backend_native_layer_norm" \
--soc_model SM8650 \
--build_folder build-x86/ \
--executorch_root . \
--enable_x86_64
```
Expected: 4 tests pass (2 floating-point, 2 quantized).
## Release Notes
- `Release notes: qualcomm`
## Related Issues
This resolves the issue where FLUX2 transformer export fails with:
- `[QNN Delegate Op Builder]: LayerNorm weight is None, skipping`
- `AttributeError: 'NoneType' object has no attribute 'name'`
Fixes pytorch#18989
- Labels: bug, module:qnn
@abhinaykukkadapu
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>1 parent 07b9feb commit a7953c5
7 files changed
Lines changed: 84 additions & 43 deletions
File tree
- backends/qualcomm
- _passes
- builders
- quantizer/annotators
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| |||
130 | 130 | | |
131 | 131 | | |
132 | 132 | | |
133 | | - | |
| 133 | + | |
134 | 134 | | |
135 | 135 | | |
136 | 136 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
15 | 20 | | |
16 | 21 | | |
17 | 22 | | |
| |||
31 | 36 | | |
32 | 37 | | |
33 | 38 | | |
| 39 | + | |
34 | 40 | | |
35 | 41 | | |
36 | 42 | | |
| |||
53 | 59 | | |
54 | 60 | | |
55 | 61 | | |
56 | | - | |
57 | 62 | | |
58 | | - | |
59 | | - | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
60 | 67 | | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
67 | 78 | | |
68 | | - | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
69 | 92 | | |
70 | | - | |
71 | | - | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
72 | 97 | | |
73 | 98 | | |
74 | 99 | | |
| |||
79 | 104 | | |
80 | 105 | | |
81 | 106 | | |
82 | | - | |
| 107 | + | |
83 | 108 | | |
84 | 109 | | |
85 | 110 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
44 | 46 | | |
45 | 47 | | |
46 | 48 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
851 | 851 | | |
852 | 852 | | |
853 | 853 | | |
854 | | - | |
| 854 | + | |
| 855 | + | |
855 | 856 | | |
856 | 857 | | |
857 | 858 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
478 | 478 | | |
479 | 479 | | |
480 | 480 | | |
481 | | - | |
482 | | - | |
483 | | - | |
484 | | - | |
| 481 | + | |
| 482 | + | |
485 | 483 | | |
486 | 484 | | |
487 | 485 | | |
| |||
492 | 490 | | |
493 | 491 | | |
494 | 492 | | |
495 | | - | |
496 | | - | |
497 | | - | |
498 | | - | |
499 | | - | |
500 | | - | |
501 | | - | |
502 | | - | |
503 | | - | |
504 | | - | |
505 | | - | |
506 | | - | |
507 | | - | |
508 | | - | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
509 | 509 | | |
510 | 510 | | |
511 | 511 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1495 | 1495 | | |
1496 | 1496 | | |
1497 | 1497 | | |
1498 | | - | |
| 1498 | + | |
1499 | 1499 | | |
1500 | | - | |
| 1500 | + | |
| 1501 | + | |
| 1502 | + | |
| 1503 | + | |
| 1504 | + | |
| 1505 | + | |
1501 | 1506 | | |
1502 | 1507 | | |
1503 | 1508 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1564 | 1564 | | |
1565 | 1565 | | |
1566 | 1566 | | |
1567 | | - | |
| 1567 | + | |
| 1568 | + | |
| 1569 | + | |
| 1570 | + | |
| 1571 | + | |
1568 | 1572 | | |
1569 | 1573 | | |
1570 | 1574 | | |
| |||
4339 | 4343 | | |
4340 | 4344 | | |
4341 | 4345 | | |
4342 | | - | |
| 4346 | + | |
| 4347 | + | |
| 4348 | + | |
| 4349 | + | |
| 4350 | + | |
4343 | 4351 | | |
4344 | 4352 | | |
4345 | 4353 | | |
| |||
0 commit comments