Developer Guide and Reference

  • 2022.1
  • 04/11/2022
  • Public Content
Contents

Batch Normalization

General

The batch normalization primitive performs a forward or backward batch normalization operation on tensors with number of dimensions equal to 2 or more.
Forward
The batch normalization operation is defined by the following formulas. We show formulas only for 2D spatial data which are straightforward to generalize to cases of higher and lower dimensions. Variable names follow the standard Naming Conventions.
LaTex Math image.
where
Mean and variance are computed at runtime or provided by a user. When mean and variance are computed at runtime, the following formulas are used:
  • LaTex Math image.,
  • LaTex Math image..
The LaTex Math image. and LaTex Math image. tensors are considered learnable.
In training mode, the primitive also optionally supports fusion with ReLU activation with zero negative slope applied to the result (see dnnl_fuse_norm_relu flag).
  • The batch normalization primitive computes population mean and variance and not the sample or unbiased versions that are typically used to compute running mean and variance.
  • Using the mean and variance computed by the batch normalization primitive, running mean and variance LaTex Math image. and LaTex Math image. can be computed as
    LaTex Math image.
Difference Between Forward Training and Forward Inference
  • If mean and variance are computed at runtime (i.e., dnnl_use_global_stats is not set), they become outputs for the propagation kind dnnl_forward_training (because they would be required during the backward propagation) and are not exposed for the propagation kind dnnl_forward_inference.
  • If batch normalization is created with ReLU fusion (i.e., dnnl_fuse_norm_relu is set), for the propagation kind dnnl_forward_training the primitive would produce a
    workspace
    memory as one extra output. This memory is required to compute the backward propagation. When the primitive is executed with propagation kind dnnl_forward_inference, the workspace is not produced. Behavior would be the same as creating a batch normalization primitive with ReLU as a post-op (see section below).
Backward
The backward propagation computes LaTex Math image., LaTex Math image., and LaTex Math image. based on LaTex Math image., LaTex Math image., LaTex Math image., LaTex Math image., LaTex Math image., and LaTex Math image..
The tensors marked with an asterisk are used only when the primitive is configured to use LaTex Math image. and LaTex Math image. (i.e., dnnl_use_scaleshift, dnnl_use_scale or dnnl_use_shift are set).

Execution Arguments

Depending on the flags and propagation kind, the batch normalization primitive requires different inputs and outputs. For clarity, a summary is shown below.
Inputs
: LaTex Math image.
Outputs
: LaTex Math image.
Inputs
: LaTex Math image.
Outputs
: LaTex Math image. , LaTex Math image. , LaTex Math image.
Inputs
: LaTex Math image. , LaTex Math image. , LaTex Math image. , LaTex Math image.
Outputs
: LaTex Math image.
Same as for dnnl_backward
Inputs
: LaTex Math image. , LaTex Math image. , LaTex Math image.
Outputs
: LaTex Math image.
Inputs
: LaTex Math image. , LaTex Math image. , LaTex Math image.
Outputs
: LaTex Math image.
Inputs
: LaTex Math image. , LaTex Math image. , LaTex Math image. , LaTex Math image.
Outputs
: LaTex Math image.
Same as for dnnl_backward
Inputs
: LaTex Math image. , LaTex Math image. , LaTex Math image.
Outputs
: LaTex Math image.
Inputs
: LaTex Math image. , LaTex Math image. , LaTex Math image.
Outputs
: LaTex Math image. , LaTex Math image. , LaTex Math image.
Inputs
: LaTex Math image. , LaTex Math image. , LaTex Math image. , LaTex Math image. , LaTex Math image. , LaTex Math image.
Outputs
: LaTex Math image. , LaTex Math image. , LaTex Math image.
Not supported
Inputs
: LaTex Math image. , LaTex Math image.
Outputs
: LaTex Math image.
Inputs
: LaTex Math image. , LaTex Math image.
Outputs
: LaTex Math image. , LaTex Math image. , LaTex Math image.
Inputs
: LaTex Math image. , LaTex Math image. , LaTex Math image. , LaTex Math image. , LaTex Math image.
Outputs
: LaTex Math image. , LaTex Math image.
Not supported
Inputs
: LaTex Math image. , LaTex Math image.
Outputs
: LaTex Math image.
Inputs
: LaTex Math image. , LaTex Math image.
Outputs
: LaTex Math image. , LaTex Math image. , LaTex Math image.
Inputs
: LaTex Math image. , LaTex Math image. , LaTex Math image. , LaTex Math image. , LaTex Math image.
Outputs
: LaTex Math image. , LaTex Math image.
Not supported
Inputs
: LaTex Math image. , LaTex Math image. , LaTex Math image. , LaTex Math image. , LaTex Math image.
Outputs
: LaTex Math image.
Inputs
: LaTex Math image. , LaTex Math image. , LaTex Math image. , LaTex Math image. , LaTex Math image.
Outputs
: LaTex Math image.
Inputs
: LaTex Math image. , LaTex Math image. , LaTex Math image. , LaTex Math image. , LaTex Math image. , LaTex Math image.
Outputs
: LaTex Math image. , LaTex Math image. , LaTex Math image.
Not supported
Inputs
: same as with
flags
Outputs
: same as with
flags
Inputs
: same as with
flags
Outputs
: same as with
flags
, Workspace
Inputs
: same as with
flags
, Workspace
Outputs
: same as with
flags
Same as for dnnl_backward if
flags
do not contain dnnl_use_scaleshift ; not supported otherwise
When executed, the inputs and outputs should be mapped to an execution argument index as specified by the following table.
Primitive Input/Output
Execution Argument Index
LaTex Math image.
DNNL_ARG_SRC
LaTex Math image.
DNNL_ARG_SCALE_SHIFT
LaTex Math image.
DNNL_ARG_SCALE
LaTex Math image.
DNNL_ARG_SHIFT
mean ( LaTex Math image. )
DNNL_ARG_MEAN
variance ( LaTex Math image. )
DNNL_ARG_VARIANCE
LaTex Math image.
DNNL_ARG_DST
workspace
DNNL_ARG_WORKSPACE
LaTex Math image.
DNNL_ARG_DIFF_DST
LaTex Math image.
DNNL_ARG_DIFF_SRC
LaTex Math image.
DNNL_ARG_DIFF_SCALE_SHIFT
LaTex Math image.
DNNL_ARG_DIFF_SCALE
LaTex Math image.
DNNL_ARG_DIFF_SHIFT

Implementation Details

General Notes
  1. The different flavors of the primitive are partially controlled by the
    flags
    parameter that is passed to the operation descriptor initialization function (e.g., dnnl::batch_normalization_forward::desc::desc()). Multiple flags can be set using the bitwise OR operator (
    |
    ). Flag dnnl_use_scaleshift can not be mixed with dnnl_use_scale or dnnl_use_shift.
  2. For forward propagation, the mean and variance might be either computed at runtime (in which case they are outputs of the primitive) or provided by a user (in which case they are inputs). In the latter case, a user must set the dnnl_use_global_stats flag. For the backward propagation, the mean and variance are always input parameters.
  3. The memory format and data type for
    src
    and
    dst
    are assumed to be the same, and in the API they are typically referred to as
    data
    (e.g., see
    data_desc
    in dnnl::batch_normalization_forward::desc::desc()). The same is true for
    diff_src
    and
    diff_dst
    . The corresponding memory descriptors are referred to as
    diff_data_desc
    .
  4. Both forward and backward propagation support in-place operations, meaning that LaTex Math image. can be used as input and output for forward propagation, and LaTex Math image. can be used as input and output for backward propagation. In case of an in-place operation, the original data will be overwritten. Note, however, that backward propagation requires original LaTex Math image., hence the corresponding forward propagation should not be performed in-place.
  5. As mentioned above, the batch normalization primitive can be fused with ReLU activation even in the training mode. In this case, on the forward propagation the primitive has one additional output,
    workspace
    , that should be passed during the backward propagation.
Data Type Support
The operation supports the following combinations of data types:
Propagation
Source / Destination
Mean / Variance / ScaleShift
forward / backward
f32, bf16
f32
forward
f16
f32
forward
s8
f32
There might be hardware- or implementation-specific restrictions. Check the Implementation Limitations section below.
Data Representation
Mean and Variance
The mean (LaTex Math image.) and variance (LaTex Math image.) are separate 1D tensors of size LaTex Math image..
The format of the corresponding memory object must be dnnl_x (dnnl_a).
Scale and Shift
If dnnl_use_scaleshift is used, the scale (LaTex Math image.) and shift (LaTex Math image.) are combined in a single 2D tensor of shape LaTex Math image..
If dnnl_use_scale or dnnl_use_shift are used, the scale (LaTex Math image.) and shift (LaTex Math image.) are separate 1D tensors of shape LaTex Math image..
The format of the corresponding memory object must be dnnl_nc (dnnl_ab).
Source, Destination, and Their Gradients
Like other CNN primitives, the batch normalization primitive expects data to be LaTex Math image. tensor.
The batch normalization primitive is optimized for the following memory formats:
Spatial
Logical tensor
Implementations optimized for memory formats
0D
NC
1D
NCW
dnnl_ncw ( dnnl_abc ), dnnl_nwc ( dnnl_acb ),
optimized^
2D
NCHW
dnnl_nchw ( dnnl_abcd ), dnnl_nhwc ( dnnl_acdb ),
optimized^
3D
NCDHW
Here optimized^ means the format that comes out of any preceding compute-intensive primitive.
Post-Ops and Attributes
Post-ops and attributes enable you to modify the behavior of the batch normalization primitive by chaining certain operations after the batch normalization operation. The following post-ops are supported by batch normalization primitives:
Propagation
Type
Operation
Description
forward
post-op
eltwise
Applies an Eltwise operation to the result (currently only dnnl_eltwise_relu algorithm is supported)
As mentioned in Primitive Attributes, the post-ops should be used for inference only. For instance, using ReLU as a post-op would not produce the additional output
workspace
that is required to compute backward propagation correctly. Hence, in case of training one should use the dnnl_fuse_norm_relu directly.

Implementation Limitations

  1. Refer to Data Types for limitations related to data types support.
  2. For the data types that have forward propagation support only, mean and variance must be provided by a user (i.e., dnnl_use_global_stats is set).

Performance Tips

  1. For backward propagation, use the same memory format for
    src
    ,
    diff_dst
    , and
    diff_src
    (the format of the
    diff_dst
    and
    diff_src
    are always the same because of the API). Different formats are functionally supported but lead to highly suboptimal performance.
  2. Use in-place operations whenever possible (see caveats in General Notes).

Examples

This C++ API example demonstrates how to create and execute a Batch Normalization primitive in forward training propagation mode.
Key optimizations included in this example:
  • In-place primitive execution;
  • Source memory format for an optimized primitive implementation;
  • Fused post-ops via operation descriptor flags;

Product and Performance Information

1

Performance varies by use, configuration and other factors. Learn more at www.Intel.com/PerformanceIndex.