Skip to content

SyncBatchNorm.convert_sync_batchnorm silently drops use_global_stats=True: frozen BatchNorm layers start updating their statistics #79943

Open

Description

@sibisrinivasb

bug描述 Describe the Bug

A BatchNorm layer created with use_global_stats=True is frozen: in train mode it normalises with its stored mean / variance and does not update them. After paddle.nn.SyncBatchNorm.convert_sync_batchnorm(model) the layer becomes a SyncBatchNorm, which has no such option: in train mode it normalises with the batch statistics and overwrites the stored ones on every forward pass. No warning or error is raised, so a partially frozen model changes behaviour silently.

Minimal reproduction (random input, no training loop):

import copy
import numpy as np
import paddle

paddle.set_device("gpu")
paddle.seed(0)
net = paddle.nn.Sequential(paddle.nn.Conv2D(3, 4, 3), paddle.nn.BatchNorm2D(4, use_global_stats=True))
net[1]._mean.set_value(paddle.to_tensor([0.5, -0.5, 1.0, -1.0]))
net[1]._variance.set_value(paddle.to_tensor([2.0, 0.5, 1.5, 3.0]))
x = paddle.to_tensor(np.random.RandomState(0).randn(8, 3, 16, 16).astype("float32"))

converted = paddle.nn.SyncBatchNorm.convert_sync_batchnorm(copy.deepcopy(net))
for name, m in (("BatchNorm2D(use_global_stats=True)", net), ("after convert_sync_batchnorm", converted)):
    m.train()
    before = m[1]._mean.numpy().copy()
    m(x)
    print(name, "| stored mean moved by", float(np.abs(m[1]._mean.numpy() - before).max()))

Output on 3.3.1:

BatchNorm2D(use_global_stats=True) | stored mean moved by 0.0
after convert_sync_batchnorm | stored mean moved by 0.10172402858734131

In eval mode the two models agree (max difference 9.5e-07), so the weights and statistics are copied correctly; only the train-mode behaviour changes. The train-mode outputs differ by up to 2.89 on this input.

Expected: a layer built with use_global_stats=True keeps using its stored statistics after the conversion, or the conversion warns / refuses. For comparison, torch.nn.SyncBatchNorm.convert_sync_batchnorm passes the layer's track_running_stats to the new layer and copies its training flag.

Where: python/paddle/nn/layer/norm.py. convert_sync_batchnorm builds SyncBatchNorm(num_features, momentum, epsilon, weight_attr, bias_attr, data_format, name) and never reads layer._use_global_stats; SyncBatchNorm.forward carries the comment use_global_stats only support False in sync_batch_norm.

Impact: fine-tuning with part of a network frozen. On several GPUs the conversion cannot simply be left out, because the layers that are still training need synchronized statistics; it is applied to the whole model, so the frozen layers lose their setting in the same call. Training scripts do this from a config switch: PaddleOCR tools/train.py converts the whole model when Global.use_sync_bn is set. We met it while fine-tuning a table-recognition model whose neck BatchNorm layers were frozen this way: their stored mean / variance drifted by 1.2 % / 3.7 % (up to 9 %) during the run while their weights were unchanged, and two runs with different settings produced the same degraded curve.

Proposed fix: in convert_sync_batchnorm, leave a layer with use_global_stats=True unconverted - it has no batch statistics to synchronize. I have this change ready with unit tests (a BatchNorm2D(use_global_stats=True) stays the same layer after the conversion while the other BatchNorm layers are converted; its stored statistics do not move in train mode) and will open a PR. An alternative is to support use_global_stats inside SyncBatchNorm itself and carry it through the conversion; I can do that instead if you prefer.

其他补充信息 Additional Supplementary Information

  • PaddlePaddle 3.3.1 (GPU wheel built for CUDA 12.6 / cuDNN 9.9), Python 3.12, Windows 11, one GPU. The conversion is the same code for any number of GPUs; I have not run a multi-GPU job.
  • paddle.sparse.nn.SyncBatchNorm.convert_sync_batchnorm (python/paddle/sparse/nn/layer/norm.py) follows the same pattern; I have not tested it.
  • Side note: in this Windows wheel the sync_batch_norm kernel is registered for GPU only, so the converted model cannot run on CPU at all (The kernel ... of kernel sync_batch_norm is not registered ... following Backends: GPU).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions