To Fold or Not to Fold: a Necessary and Sufficient Condition on Batch-Normalization Layers Folding

Edouard Yvinec, Arnaud Dapogny, Kevin Bailly · DOI

Introduction

Batch-Normalization (BN) is a cornerstone of modern deep learning, essential for stabilizing training. However, during inference, BN layers become computational liabilities. They introduce sequential bottlenecks that slow down execution, particularly on edge devices. While "folding" BN layers into adjacent convolutional layers is a common optimization, current industry practices are surprisingly suboptimal. Standard "naive folding" only addresses simple sequences where a BN layer immediately follows a convolutional layer.

This paper introduces BaN-OFF, a mathematically rigorous approach based on a "necessary and sufficient condition" that identifies every foldable BN layer in complex architectures. By treating neural networks as Directed Acyclic Graphs (DAGs), BaN-OFF achieves significant inference speedups—up to 63%—without any loss in model accuracy.

The BaN-OFF Approach

The core innovation of BaN-OFF lies in its treatment of the neural network as a DAG. Layers are categorized into three types: Expressive layers (affine operations like Convolution or Fully-Connected), Non-affine layers (non-linearities like ReLU), and Other layers (structural operations like pooling or additions).

Comparison of folding scenarios: (a) standard sequential folding, (b) folding across multiple BNs, and (c) BaN-OFF's ability to fold BNs in complex branching structures.

The authors propose Theorem 1, which establishes that a BN layer can be folded if it is contained within a connected sub-graph of affine operations. The BaN-OFF algorithm identifies these foldable clusters and updates the weights of "input" expressive layers to incorporate the BN's scaling. Crucially, it also adjusts "output" expressive layers to negate the transformation for paths that bypassed the BN, ensuring the final output remains mathematically identical to the original model. This perspective allows folding even when BN layers are separated from convolutional layers by additions or pooling, provided the math is adjusted downstream.

Experimental Evaluation

The researchers validated BaN-OFF across a wide variety of tasks, including image classification (ImageNet), object detection (MS COCO), and semantic segmentation (CityScapes). The method systematically outperformed state-of-the-art (SOTA) folding algorithms across all tested configurations.

Speed-up comparison between SOTA folding and BaN-OFF across various models and tasks. BaN-OFF consistently provides higher gains, especially in complex segmentation models like U-Net.

Key Results

  • Massive Speedups: On semantic segmentation tasks using U-Net, BaN-OFF achieved a 63% reduction in latency on an NVIDIA RTX 3090.
  • Handling Complexity: In architectures designed to break naive folding, where standard methods achieved 0% speedup, BaN-OFF delivered a 47.8% improvement.
  • Zero Accuracy Loss: Because the transformation is mathematically equivalent, predictions remain identical within a tiny numerical margin ($10^{-6}$) due to floating-point arithmetic.
  • Hardware Sensitivity: The study highlights that GPUs benefit more from this optimization than CPUs. Since GPUs are massive parallel processing engines, they are more sensitive to the "stalls" caused by sequential BN operations.

Conclusion and Future Work

BaN-OFF demonstrates that sequentiality is the primary enemy of inference speed. By removing these bottlenecks through advanced graph-based folding, significant performance gains are possible. Furthermore, BaN-OFF acts as a force multiplier for other compression techniques like pruning and quantization.

However, limitations remain. On extremely low-precision hardware (e.g., 4-bit quantization), the re-calculation of weights might introduce noticeable numerical shifts. Additionally, the method's effectiveness is reduced in architectures densely packed with non-linearities. Future work will explore extending this logic to dynamic neural networks where the graph structure changes per input.

Summary updated

BibTeX
@inproceedings{yvinec2022fold,
  author = {Edouard Yvinec and Arnaud Dapogny and Kevin Bailly},
  title = {{To Fold or Not to Fold: a Necessary and Sufficient Condition on Batch-Normalization Layers Folding}},
  booktitle = {International Joint Conference on Artificial Intelligence},
  year = {2022},
  url = {https://openreview.net/forum?id=6ZPFuQsTx_},
}
Figure