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
- Use reduction='mean' together with avg_factor (the standard path)
- 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
- Pass avg_factor only with reduction='mean'/'none'
- Review custom head loss calls for both kwargs
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
- Default ``avg_non_ignore`` is False, if you would like to ig
- The annotation file of Open Images Challenge should be a txt
- Invalid text mode "{self.text_mode}".
- No sample in split "{self.split}".
- sampler should be an instance of ``Sampler``, but got {sampl
AI-assisted analysis of open-mmlab/mmdetection@cfd5d3a985 (2026-08-27).
Data as JSON: /api/errors/c3b8d23f18392808.
Report an issue: GitHub.