r"""Applies synchronized batch normalization to the input. Refer to :class:`~.BatchNorm2d` and :class:`~.BatchNorm1d` for more information. Args: inp: input tensor. running_mean: tensor to store running mean. running_var: tensor to store running variance. we
(
inp: Tensor,
running_mean: Tensor,
running_var: Tensor,
weight: Optional[Tensor] = None,
bias: Optional[Tensor] = None,
training: bool = False,
momentum: Union[float, Tensor] = 0.9,
eps: float = 1e-5,
eps_mode="additive",
group=WORLD,
)
| 1473 | |
| 1474 | |
| 1475 | def sync_batch_norm( |
| 1476 | inp: Tensor, |
| 1477 | running_mean: Tensor, |
| 1478 | running_var: Tensor, |
| 1479 | weight: Optional[Tensor] = None, |
| 1480 | bias: Optional[Tensor] = None, |
| 1481 | training: bool = False, |
| 1482 | momentum: Union[float, Tensor] = 0.9, |
| 1483 | eps: float = 1e-5, |
| 1484 | eps_mode="additive", |
| 1485 | group=WORLD, |
| 1486 | ) -> Tensor: |
| 1487 | r"""Applies synchronized batch normalization to the input. |
| 1488 | |
| 1489 | Refer to :class:`~.BatchNorm2d` and :class:`~.BatchNorm1d` for more information. |
| 1490 | |
| 1491 | Args: |
| 1492 | inp: input tensor. |
| 1493 | running_mean: tensor to store running mean. |
| 1494 | running_var: tensor to store running variance. |
| 1495 | weight: scaling tensor in the learnable affine parameters. |
| 1496 | See :math:`\gamma` in :class:`~.BatchNorm2d`. |
| 1497 | bias: bias tensor in the learnable affine parameters. |
| 1498 | See :math:`\beta` in :class:`~.BatchNorm2d`. |
| 1499 | training: a boolean value to indicate whether batch norm is performed |
| 1500 | in traning mode. Default: False |
| 1501 | momentum: value used for the ``running_mean`` and ``running_var`` |
| 1502 | computation. Default: 0.9 |
| 1503 | eps: a value added to the denominator for numerical stability. |
| 1504 | Default: 1e-5 |
| 1505 | eps_mode: mode of calculation for eps, "max" or "additive". |
| 1506 | Default: "additive" |
| 1507 | group: communication group, caculate mean and variance between this group. |
| 1508 | Default: :obj:`~megengine.distributed.WORLD` |
| 1509 | """ |
| 1510 | _eps_mode = eps_mode.lower() |
| 1511 | assert _eps_mode in {"max", "additive"}, "unknown eps_mode: {}".format(eps_mode) |
| 1512 | if _eps_mode == "additive" and not (is_distributed() and training): |
| 1513 | return batch_norm( |
| 1514 | inp, |
| 1515 | running_mean, |
| 1516 | running_var, |
| 1517 | weight, |
| 1518 | bias, |
| 1519 | training=training, |
| 1520 | momentum=momentum, |
| 1521 | eps=eps, |
| 1522 | ) |
| 1523 | if amp._enabled: |
| 1524 | inp, weight, bias, running_mean, running_var = cast_tensors( |
| 1525 | inp, weight, bias, running_mean, running_var, promote=True |
| 1526 | ) |
| 1527 | |
| 1528 | _channels = make_shape_tuple(inp.shape)[1] |
| 1529 | _ndim = inp.ndim |
| 1530 | _device = inp.device |
| 1531 | _dtype = inp.dtype |
| 1532 |
no test coverage detected