open-mmlab/mmdetection · error · ValueError

avg_factor can not be used with reduction="sum"

Error message

avg_factor can not be used with reduction="sum"

What it means

weight_reduce_loss raises ValueError when both avg_factor is provided and reduction='sum'. Dividing by avg_factor while also asking for a raw sum is contradictory, so the combination is explicitly rejected.

Source

Thrown at mmdet/models/losses/utils.py:64

        Tensor: Processed loss values.
    """
    # if weight is specified, apply element-wise weight
    if weight is not None:
        loss = loss * weight

    # if avg_factor is not specified, just reduce the loss
    if avg_factor is None:
        loss = reduce_loss(loss, reduction)
    else:
        # if reduction is mean, then average the loss by avg_factor
        if reduction == 'mean':
            # Avoid causing ZeroDivisionError when avg_factor is 0.0,
            # i.e., all labels of an image belong to ignore index.
            eps = torch.finfo(torch.float32).eps
            loss = loss.sum() / (avg_factor + eps)
        # if reduction is 'none', then do nothing, otherwise raise an error
        elif reduction != 'none':
            raise ValueError('avg_factor can not be used with reduction="sum"')
    return loss


def weighted_loss(loss_func: Callable) -> Callable:
    """Create a weighted version of a given loss function.

    To use this decorator, the loss function must have the signature like
    `loss_func(pred, target, **kwargs)`. The function only needs to compute
    element-wise loss without any reduction. This decorator will add weight
    and reduction arguments to the function. The decorated function will have
    the signature like `loss_func(pred, target, weight=None, reduction='mean',
    avg_factor=None, **kwargs)`.

    :Example:

    >>> import torch
    >>> @weighted_loss
    >>> def l1_loss(pred, target):

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Use reduction='mean' together with avg_factor (the standard path)
  2. Or keep reduction='sum' and drop avg_factor, dividing manually afterwards

Example fix

# before
loss = self.loss_cls(pred, target, avg_factor=avg_factor, reduction='sum')
# after
loss = self.loss_cls(pred, target, avg_factor=avg_factor, reduction='mean')
Defensive patterns

Strategy: validation

Validate before calling

assert not (avg_factor is not None and reduction == 'sum'), 'avg_factor incompatible with reduction=sum'

Prevention

When it happens

Trigger: A loss call such as FocalLoss or CrossEntropyLoss forward(..., avg_factor=num_pos, reduction='sum') from a custom head that computes its own normalization.

Common situations: Custom dense heads passing both avg_factor and reduction='sum' in loss calls; overriding loss functions or targetassigners that forward both kwargs.

Related errors


AI-assisted analysis of open-mmlab/mmdetection@cfd5d3a985 (2026-08-27). Data as JSON: /api/errors/c3b8d23f18392808. Report an issue: GitHub.