Repository navigation
Description
Describe the Bug
apex.contrib.group_norm.GroupNorm returns inaccurate results for channels_last inputs whose per-group mean is large compared with their standard deviation. With float32 input of mean 100 and std 1 the largest absolute error against a float64 reference is about 5000x that of torch.nn.functional.group_norm in float32; with mean 10 and std 0.02 the error is of the order of the output values. Both the one-pass and the two-pass kernels are affected.
With mean 100 / std 1 the largest error is 4.6e-2 (output values are about +-5), against 8.3e-6 for F.group_norm in float32. Both forward kernels compute the variance as mean(x^2) - mean(x)^2 in float32, which cancels when mean^2 >> var:
apex/contrib/csrc/group_norm/group_norm_nhwc_fwd_one_pass_kernel.cuh:169:float var = smem_sums.y * params.inv_hwc_per_group - (mean * mean);apex/contrib/csrc/group_norm/group_norm_nhwc_fwd_two_pass.cu:264:float var = sum_sq * params.inv_hwc_per_group - (mean * mean);
When the difference comes out <= 0 the next line uses inv_stddev = 1 (var <= 0.f ? 1.f : rsqrtf(var + params.epsilon)), so the group is not normalized at all. The backward kernels use the same expression (group_norm_nhwc_bwd_one_pass_kernel.cuh:89, group_norm_nhwc_bwd_two_pass.cu:60).
Minimal Steps/Code to Reproduce the Bug
# apex.contrib.group_norm.GroupNorm (NHWC fast path) vs torch.nn.functional.group_norm, fp32, same input.
# Reference: F.group_norm evaluated in float64 on the same float32 input.
import torch
import torch.nn.functional as F
from apex.contrib.group_norm import GroupNorm
dev = "cuda"
G, C = 32, 320
U32 = 2.0 ** -24
print("shape (N,C,H,W) mean std | apex max|err| torch max|err| max|ref| | ratio apex/max(torch, 2u*max|ref|)")
for (N, H) in ((2, 32), (1, 64)): # H*W = 1024 -> one-pass kernel, H*W = 4096 -> two-pass kernel
for mean, std in ((0.0, 1.0), (10.0, 1.0), (100.0, 1.0), (10.0, 0.1), (10.0, 0.02)):
g = torch.Generator(device=dev).manual_seed(1234)
x = torch.randn(N, C, H, H, generator=g, device=dev) * std + mean
x = x.contiguous(memory_format=torch.channels_last)
gn = GroupNorm(G, C, eps=1e-5, affine=True, device=dev, dtype=torch.float32) # weight = 1, bias = 0
with torch.no_grad():
y_apex = gn(x)
y_torch = F.group_norm(x, G, gn.weight, gn.bias, 1e-5)
y_ref = F.group_norm(x.double(), G, gn.weight.double(), gn.bias.double(), 1e-5)
tmax = y_ref.abs().max().item()
e_apex = (y_apex.double() - y_ref).abs().max().item()
e_torch = (y_torch.double() - y_ref).abs().max().item()
q = e_apex / max(e_torch, 2 * U32 * tmax)
print("%-18s %6.1f %5.2f | %13.3e %14.3e %8.3f | %10.1f" % ((N, C, H, H), mean, std, e_apex, e_torch, tmax, q))Output (one-pass rows are identical in every run; two-pass rows vary slightly from run to run):
shape (N,C,H,W) mean std | apex max|err| torch max|err| max|ref| | ratio apex/max(torch, 2u*max|ref|)
(2, 320, 32, 32) 0.0 1.00 | 6.121e-07 6.699e-07 4.875 | 0.9
(2, 320, 32, 32) 10.0 1.00 | 7.897e-05 1.622e-06 4.875 | 48.7
(2, 320, 32, 32) 100.0 1.00 | 6.818e-03 9.790e-06 4.875 | 696.4
(2, 320, 32, 32) 10.0 0.10 | 5.524e-03 1.034e-05 4.873 | 534.3
(2, 320, 32, 32) 10.0 0.02 | 1.758e-01 6.684e-05 4.817 | 2630.1
(1, 320, 64, 64) 0.0 1.00 | 1.972e-06 6.311e-07 4.932 | 3.1
(1, 320, 64, 64) 10.0 1.00 | 4.159e-04 1.670e-06 4.932 | 249.1
(1, 320, 64, 64) 100.0 1.00 | 4.636e-02 8.302e-06 4.932 | 5584.4
(1, 320, 64, 64) 10.0 0.10 | 3.280e-02 1.315e-05 4.929 | 2494.2
(1, 320, 64, 64) 10.0 0.02 | 2.261e+00 5.180e-05 4.871 | 43643.7
float16 and bfloat16 inputs show the same loss of accuracy (e.g. float16, mean 10, std 0.02).
Expected Behavior
Accuracy comparable to torch.nn.functional.group_norm on the same float32 input (maximum absolute errors of about 1e-6 to 7e-5 in the cases above), e.g. by accumulating the centered second moment (second pass over x - mean, or a shifted/Welford-style update) instead of subtracting mean^2 from mean(x^2).
I have a patch for both forward and both backward NHWC kernels (accumulates around a per-group shift) that brings all the rows above to within ~3x of F.group_norm, and builds and passes test_group_norm.py on this GPU. Happy to open a PR.
Environment
- apex: master, commit 575968b, built from source with
APEX_CPP_EXT=1 APEX_CUDA_EXT=1 APEX_GROUP_NORM=1 pip install -v --no-build-isolation .(TORCH_CUDA_ARCH_LIST=12.0) - PyTorch 2.11.0+cu128, Python 3.12.3, CUDA toolkit 12.8 (nvcc 12.8.93), driver 595.99.02
- GPU: NVIDIA GeForce RTX 5080 Laptop GPU (sm_120), Ubuntu 24.04.5
python -m torch.utils.collect_env
apex: master, commit 575968bc1f9127ccd61003a681472a83af4ff1a1, built from source with APEX_CPP_EXT=1 APEX_CUDA_EXT=1 APEX_GROUP_NORM=1 pip install -v --no-build-isolation . (TORCH_CUDA_ARCH_LIST=12.0)
$ python -m torch.utils.collect_env
<frozen runpy>:128: RuntimeWarning: 'torch.utils.collect_env' found in sys.modules after import of package 'torch.utils', but prior to execution of 'torch.utils.collect_env'; this may result in unpredictable behaviour
Collecting environment information...
PyTorch version: 2.11.0+cu128
Is debug build: False
CUDA used to build PyTorch: 12.8
ROCM used to build PyTorch: N/A
OS: Ubuntu 24.04.5 LTS (x86_64)
GCC version: (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0
Clang version: Could not collect
CMake version: version 3.28.3
Libc version: glibc-2.39
Python version: 3.12.3 (main, Aug 31 2026, 10:18:26) [GCC 13.3.0] (64-bit runtime)
Python platform: Linux-7.0.0-38-generic-x86_64-with-glibc2.39
Is CUDA available: True
CUDA runtime version: 12.8.93
CUDA_MODULE_LOADING set to:
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 5080 Laptop GPU
Nvidia driver version: 595.99.02
cuDNN version: Could not collect
Is XPU available: False
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True
Caching allocator config: N/A
CPU:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 42 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 24
On-line CPU(s) list: 0-23
Vendor ID: GenuineIntel
Model name: Intel(R) Core(TM) Ultra 9 290HX Plus
CPU family: 6
Model: 198
Thread(s) per core: 1
Core(s) per socket: 24
Socket(s): 1
Stepping: 2
CPU(s) scaling MHz: 36%
CPU max MHz: 5500.0000
CPU min MHz: 800.0000
BogoMIPS: 6144.00
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdt_a rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect user_shstk avx_vnni lam wbnoinvd dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi vnmi umip pku ospke waitpkg gfni vaes vpclmulqdq rdpid bus_lock_detect movdiri movdir64b fsrm md_clear serialize arch_lbr ibt flush_l1d arch_capabilities
Virtualization: VT-x
L1d cache: 768 KiB (20 instances)
L1i cache: 1.3 MiB (20 instances)
L2 cache: 40 MiB (12 instances)
L3 cache: 36 MiB (1 instance)
NUMA node(s): 1
NUMA node0 CPU(s): 0-23
Vulnerability Gather data sampling: Not affected
Vulnerability Ghostwrite: Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit: Not affected
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Mmio stale data: Not affected
Vulnerability Old microcode: Not affected
Vulnerability Reg file data sampling: Not affected
Vulnerability Retbleed: Not affected
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; Enhanced / Automatic IBRS; IBPB conditional; PBRSB-eIBRS Not affected; BHI BHI_DIS_S
Vulnerability Srbds: Not affected
Vulnerability Tsa: Not affected
Vulnerability Tsx async abort: Not affected
Vulnerability Vmscape: Mitigation; IBPB before exit to userspace
Versions of relevant libraries:
[pip3] numpy==2.5.4
[pip3] nvidia-cublas-cu12==12.8.4.1
[pip3] nvidia-cuda-cupti-cu12==12.8.90
[pip3] nvidia-cuda-nvrtc-cu12==12.8.93
[pip3] nvidia-cuda-runtime-cu12==12.8.90
[pip3] nvidia-cudnn-cu12==9.19.0.56
[pip3] nvidia-cudnn-frontend==1.31.0
[pip3] nvidia-cufft-cu12==11.3.3.83
[pip3] nvidia-curand-cu12==10.3.9.90
[pip3] nvidia-cusolver-cu12==11.7.3.90
[pip3] nvidia-cusparse-cu12==12.5.8.93
[pip3] nvidia-cusparselt-cu12==0.7.1
[pip3] nvidia-nccl-cu12==2.28.9
[pip3] nvidia-nvjitlink-cu12==12.8.93
[pip3] nvidia-nvtx-cu12==12.8.90
[pip3] onnx==1.23.2
[pip3] onnx-ir==1.0.0
[pip3] onnxscript==0.7.2
[pip3] torch==2.11.0+cu128
[pip3] transformer_engine_torch==2.20.2
[pip3] triton==3.6.0
[conda] Could not collect
Activity