detrium.optim.sync_batchnorm

Zero-CPU-sync and multi-branch coalesced SyncBatchNorm for PyTorch 2.4+. Two lossless (mathematically equivalent to ``torch.nn.SyncBatchNorm``) optimizations: 1. ``FastSyncBatchNorm`` (1A): Replaces ``torch.nn.SyncBatchNorm``'s boolean-mask indexing (``mask = count_all.squeeze(-1) >= 1; count_all[mask]...``), which triggers ``aten::nonzero`` CPU-GPU stream synchronizations on every SyncBN layer forward. Empty ranks (``numel == 0``) send neutral stats (``mean=0, invstd=eps**-0.5, count=0``) so non-empty ranks never hit division-by-zero without needing ``nonzero``. 2. ``MultiBranchSyncBNFunction`` / ``coalesced_sync_batchnorm`` (1B): Fuses parallel independent Conv+SyncBN branches into a single ``dist.all_gather_into_tensor`` in forward and a single ``dist.all_reduce(SUM)`` in backward over packed 1D channel stats, keeping 4D feature maps unconcatenated. Runtime toggles (enabled by default when ``sync_bn: True``): - ``DFINE_FAST_SYNCBN=0``: disable both 1A and 1B and fall back to stock ``nn.SyncBatchNorm``. - ``DFINE_COALESCE_SYNCBN=0``: disable 1B branch coalescing while keeping 1A active.

导出符号 · is_fast_syncbn_enabled、is_coalesce_syncbn_enabled、normalize_sync_bn_mode、FastSyncBNFunction、FastSyncBatchNorm、convert_fast_sync_batchnorm、can_coalesce_sync_batchnorm、MultiBranchSyncBNFunction、coalesced_sync_batchnorm、coalesced_input_proj、LocalBNRunningStatsSync、update_precise_bn_stats

class FastSyncBNFunction(torch.autograd.Function)

Single-layer asynchronous SyncBatchNorm without aten::nonzero CPU sync.

forward(ctx, input: torch.Tensor, weight: Optional[torch.Tensor], bias: Optional[torch.Tensor], running_mean: Optional[torch.Tensor], running_var: Optional[torch.Tensor], eps: float, momentum: float, process_group, world_size: int) -> torch.Tensor
名称类型默认值
ctx——
inputtorch.Tensor—
weightOptional[torch.Tensor]—
biasOptional[torch.Tensor]—
running_meanOptional[torch.Tensor]—
running_varOptional[torch.Tensor]—
epsfloat—
momentumfloat—
process_group——
world_sizeint—
class MultiBranchSyncBNFunction(torch.autograd.Function)

Coalesce K parallel SyncBN branches into 1 all_gather + 1 all_reduce.

forward(ctx, num_branches: int, eps_list: Tuple[float, ...], momentum_list: Tuple[float, ...], running_means: Tuple[Optional[torch.Tensor], ...], running_vars: Tuple[Optional[torch.Tensor], ...], process_group, world_size: int, *tensors: Optional[torch.Tensor]) -> Tuple[torch.Tensor, ...]
名称类型默认值
ctx——
num_branchesint—
eps_listTuple[float, ...]—
momentum_listTuple[float, ...]—
running_meansTuple[Optional[torch.Tensor], ...]—
running_varsTuple[Optional[torch.Tensor], ...]—
process_group——
world_sizeint—
tensorsOptional[torch.Tensor]—
class LocalBNRunningStatsSync

Packs local BatchNorm running_mean/running_var across ranks for ModelEMA. When ``sync_interval == 1``, applies the exact Law-of-Total-Variance correction using each rank's batch-mean deviation so multi-GPU ``running_var`` retains inter-rank batch variance at ~0.08 ms/step (1 small packed all_reduce).

名称类型默认值
modelnn.Module—
sync_intervalint1
bn_momentumOptional[float]None
can_coalesce_sync_batchnorm(norms: Sequence[nn.Module], xs: Union[torch.Tensor, Sequence[torch.Tensor]]) -> bool

Return True if a group of SyncBN modules and inputs can share one collective.

名称类型默认值
normsSequence[nn.Module]—
xsUnion[torch.Tensor, Sequence[torch.Tensor]]—
coalesced_sync_batchnorm(xs: Sequence[torch.Tensor], norms: Sequence[nn.Module]) -> Tuple[torch.Tensor, ...]

Run multiple independent SyncBatchNorm layers in a single collective pair.

名称类型默认值
xsSequence[torch.Tensor]—
normsSequence[nn.Module]—
coalesced_input_proj(input_proj: nn.ModuleList, feats: Sequence[torch.Tensor]) -> List[torch.Tensor]

Coalesce Sequential(conv, norm) input_proj layers across pyramid feature levels.

名称类型默认值
input_projnn.ModuleList—
featsSequence[torch.Tensor]—
update_precise_bn_stats(model: nn.Module, data_loader, device: torch.device, num_batches: int=64, use_amp: bool=True) -> float

Calibrate exact population BN running_mean/running_var across all ranks.

名称类型默认值
modelnn.Module—
data_loader——
devicetorch.device—
num_batchesint64
use_ampboolTrue