Lightning-AI/pytorch-lightning · warning

You called `self.log({self.meta.name!r}, ...)` in your `{sel

Error message

You called `self.log({self.meta.name!r}, ...)` in your `{self.meta.fx}` but the value needs to be floating to be reduced. Converting it to {dtype}. You can silence this warning by converting the value to floating point yourself. If you don't intend to reduce the value (for instance when logging the global step or epoch) then you can use `self.logger.log_metrics({{{self.meta.name!r}: ...}})` instead.

What it means

Result metric update warns when self.log receives a non-floating tensor (int/long/bool). Reduction ops need floats, so Lightning converts it to the default dtype. The message suggests converting yourself or using logger.log_metrics for non-reducible values like step/epoch numbers.

Source

Thrown at src/lightning/pytorch/trainer/connectors/logger_connector/result.py:212

            elif metadata.is_min_reduction:
                default = float("inf")
            else:
                default = 0.0
            # the logged value will be stored in float32 or higher to maintain accuracy
            self.add_state("value", torch.tensor(default, dtype=_get_default_dtype()), dist_reduce_fx=torch.sum)
            if self.meta.is_mean_reduction:
                self.cumulated_batch_size: Tensor
                self.add_state("cumulated_batch_size", torch.tensor(0), dist_reduce_fx=torch.sum)
        # this is defined here only because upstream is missing the type annotation
        self._forward_cache: Optional[Any] = None

    @override
    def update(self, value: _VALUE, batch_size: int) -> None:
        if self.is_tensor:
            value = cast(Tensor, value)
            dtype = _get_default_dtype()
            if not torch.is_floating_point(value):
                warning_cache.warn(
                    # do not include the value to avoid cache misses
                    f"You called `self.log({self.meta.name!r}, ...)` in your `{self.meta.fx}` but the value needs to"
                    f" be floating to be reduced. Converting it to {dtype}."
                    " You can silence this warning by converting the value to floating point yourself."
                    " If you don't intend to reduce the value (for instance when logging the global step or epoch) then"
                    f" you can use `self.logger.log_metrics({{{self.meta.name!r}: ...}})` instead."
                )
                value = value.to(dtype)
            if value.dtype not in (torch.float32, torch.float64):
                value = value.to(dtype)

            if self.meta.on_step:
                self._forward_cache = self.meta.sync(value.clone())  # `clone` because `sync` is in-place
                # performance: no need to accumulate on values only logged on_step
                if not self.meta.on_epoch:
                    self.value = self._forward_cache
                    return

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Convert to float: self.log('n', float(value)) or value.float()
  2. For non-reducible scalars (step/epoch), use self.logger.log_metrics({'epoch': value}, step=...) or enable enable_graph-free logging without reduction
  3. Pass reduce_fx that tolerates ints is not supported — casting is the fix

Example fix

# before
self.log('num_tokens', num_tokens)  # long tensor -> warning
# after
self.log('num_tokens', num_tokens.float())
Defensive patterns

Strategy: type-guard

Validate before calling

val = torch.as_tensor(val)
if val.is_floating_point():
    self.log(name, val, batch_size=bs)
else:
    self.log(name, val.float(), batch_size=bs)

Type guard

def is_float_tensor(v) -> bool:
    import torch
    return torch.is_tensor(v) and torch.is_floating_point(v)

Prevention

When it happens

Trigger: self.log('epoch', self.current_epoch) or self.log('count', int_tensor) inside training_step/validation_step; logging integer counters that get mean-reduced.

Common situations: Logging global step, batch index, or integer counts; debugging code that logs tensor shapes or indices.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/0e0f5ae954a77964. Report an issue: GitHub.