[Bug][Relax][ONNX] LayerNormalization mean reduction loses accuracy for large-offset float32 inputs
Les mainteneurs répondent en général sous 3 jours
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 45/100
- Type d'issue
- Bug
- Clarté
- Plutôt claire
- Activité
- Active
- Stack technique
- python
- Domaine
- backend, machine-learning
Piste de recherche
Start with the ONNX LayerNormalization reproduction bundle: run repro.py in the configured TVM build environment and compare its CPU/LLVM results with the float64 two-pass reference. Then trace the lowered TIR mean reduction, since the issue localizes the error to that step before variance is computed. Done means the large-offset input produces accurate statistics and normalized outputs consistent with the reference across the reported paths.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Expected behavior
For the attached ONNX model, TVM should produce LayerNormalization outputs consistent with a numerically stable reference for the supplied finite float32 input. Inputs A and B contain the same values up to a representable translation: float32(B + 80000) == A element-wise. Their normalized values should therefore agree within float32 rounding error.
A two-pass reference computes the mean and variance in float64, then casts the final outputs to float32.
This is a follow-up to #19592, which was closed by #19643. On commit 3d89c499, the attached single-node model produces finite outputs, but TVM returns a Mean of 80000.0078125. The correctly rounded float32 mean is 80000.1328125 (16 ULP higher). A sequential float32 sum reproduces TVM’s Mean exactly. With epsilon=1e-5, TVM returns an InvStdDev of 6.84518, compared with 13.83470 from a float64 two-pass reference.
Actual behavior
With input A, TVM returns a Mean of 80000.0078125. The float64 mean is 80000.134765625, which rounds to float32 as 80000.1328125. The TVM result differs by 0.125, or 16 float32 ULP at this magnitude. A sequential left-to-right float32 sum over the same input also returns 80000.0078125.
The resulting InvStdDev is 6.8451757 in TVM and 13.834698 in the float64 two-pass reference. The maximum absolute error in Y is 1.4692705.
For translated input B, TVM returns Mean 0.134765625 and InvStdDev 13.8346987. Its maximum absolute Y error against the two-pass reference is 1.49e-7.
The behavior reproduces with none, default, and default_build CPU/LLVM paths. The lowered TIR computes variance in two passes, using the computed mean before summing squared deviations. The observed mean error occurs before that variance calculation.
Environment
- TVM 0.28.dev0, commit 3d89c4991c5a37492f470800231a5ea8f57a95c6
- Ubuntu 26.04.1 LTS under WSL2, Linux 6.18.58.1, x86_64
- Intel Core i7-14700KF; LLVM 21.1.8
- Python 3.10.22; NumPy 1.26.4; ONNX 1.21.0
- Target LLVM; Relax VM on CPU
- ONNX Runtime 1.23.2 is installed but is not used as the correctness oracle
Steps to reproduce
layernorm_issue_attachments.zip
Attach model.onnx, inputs_A.npz, inputs_B.npz, and repro.py from the reproduction bundle. Configure the TVM build environment, then run:
python repro.py
The model contains one opset-17 LayerNormalization node with input shape (1, 1, 128), axis=-1, epsilon=1e-5, and outputs Y, Mean, and InvStdDev. The input archives contain A and B respectively. The script runs the ONNX reference and TVM's none, default, and default_build paths, then compares all three outputs with a NumPy float64 two-pass reference.
The run should report float32(B+80000)==A True. For A, TVM Mean should be 80000.0078125. For B, it should be 0.134765625. All three TVM paths reproduce the same result.
Numerical results
| Input | Output | TVM | Float64 two-pass reference | Maximum absolute error |
|---|---|---|---|---|
| A | Mean | 80000.0078125 | 80000.1328125 | 0.125 |
| A | InvStdDev | 6.8451757 | 13.8346977 | 6.9895220 |
| A | Y | see attached output | see attached output | 1.4692705 |
| B | Mean | 0.134765625 | 0.134765625 | 0 |
| B | InvStdDev | 13.8346987 | 13.8346977 | 0.00000095 |
| B | Y | see attached output | see attached output | 0.00000015 |
For A, the exact float64 mean is 80000.134765625; its correctly rounded float32 value is 80000.1328125. The sequential float32 sum is 10240001.0, giving a mean of 80000.0078125, identical to TVM. The maximum absolute difference between the complete 128-element Y arrays for A and B is 1.4692706 in TVM and 0 in the NumPy two-pass reference.
Related fixes
TVM PR #19643 changed LayerNorm variance computation to a two-pass formula for issue #19592. The lowered TIR in this reproduction already uses that formula. The present result points to the preceding mean reduction under large-offset float32 inputs.
TVM PR #20510 addresses ONNX LayerNormalization statistics outputs and fused/decomposed frontend behavior. This reproduction checks Y, Mean, and InvStdDev and observes the same numerical result across the three CPU paths.
Triage
- needs-triage
- Langage dominant
- Python
- Étoiles
- 13.9k
- Forks
- 4.1k
- Merge moyen
- 4 j 22 h
- PR mergées (30 j)
- 22
Préparer son environnement
Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de apache/tvm
-
[Bug] [Relax] `relax.op.unique(return_inverse=True)` returns an input-shaped inverse for N-D input when `axis` is NonePeut-être pris Une pull request liée à cette issue est ouverte ou déjà fusionnée. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
Les mainteneurs répondent en général sous 3 jours
-
[Bug][TOPI] `topi.scanop` (`cumsum` / `cumprod`) performs out-of-bounds write/read on empty tensorsOuverteneeds-triage type: bug
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
Les mainteneurs répondent en général sous 3 jours
-
[Bug] [Relax] take_backward does not normalize negative axis causing heap OOB write and wrong gradientsPeut-être pris @pierceluu10 l’a pris il y a 5 jours. Ouverteneeds-triage type: bug
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
Les mainteneurs répondent en général sous 3 jours
-
[Bug][Relax][ONNX] NonZero returns the wrong output shape for scalar inputsPeut-être pris @tandede l’a pris il y a 9 jours. Ouverteneeds-triage type: bug
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
Les mainteneurs répondent en général sous 3 jours
-
needs-triage type: bug
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
apache/tvm#21043 · 1 commentaire ·
Les mainteneurs répondent en général sous 3 jours
Toutes les issues de apache/tvm
Issues similaires
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 92/100
Techiebutler/freeframe#687 ·
Les mainteneurs répondent en général sous 1 jour
-
bug verbose GC
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100
IBM/javacore-analyser#677 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 79/100
Les mainteneurs répondent en général sous 2 jours
-
area/config comp/plugins P1 platform/qqbot tool/file type/bug
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
NousResearch/hermes-agent#163932 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
plugins/dsh: todayStr() computes UTC but is documented as local — journal entries written 00:00–08:00 local land in the previous day's filePeut-être pris @Beehtrix l’a pris aujourd’hui. Ouvertebug
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
zilliztech/memsearch#907 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour