Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

perf(losses): dice_loss skips the one-hot target for large CPU logits (#6082)

Dmytro Mishkindfc63f114e93
diff --git a/changelog.d/6082.fixed.md b/changelog.d/6082.fixed.md
new file mode 100644
index 000000000..e7d9fee4f
--- /dev/null
+++ b/changelog.d/6082.fixed.md
@@ -0,0 +1,13 @@
+`dice_loss` and `DiceLoss` run faster on the CPU for float32 and float64 logits with at least 2**15 values (and class
+weights, if any, of the logits' dtype): the loss no longer builds a one-hot target and full-size products of it. The
+intersection takes each pixel's target-class probability, the micro average sums the (weighted) probabilities over the
+classes of each pixel, and the macro average gets each class's probability mass from a batched matrix-vector product and
+adds the per-class sums into (batch, class) bins. On 21-class 256 x 256 logits, forward plus backward, measured on an
+i7-14700K under WSL2 (where page faults cost more than on native Linux), the default micro loss is 4.5x faster at batch
+8 and 4.2x at batch 32 in eager mode, the macro loss 3.3x and 3.1x and the class-weighted micro loss 3.1x and 2.9x; with
+the faults removed from both paths by `GLIBC_TUNABLES` they are 2.9x to 4.3x faster, and 1.1x to 1.2x under
+`torch.compile` on torch 2.14. CUDA is unchanged. Values and gradients agree with the one-hot sums to rounding, with the
+same NaN positions, and labels outside the classes raise `one_hot`'s errors, also under `torch.compile`; exported,
+traced, autocast and functorch calls, and smaller inputs, keep the one-hot target. So do compiled calls on torch
+releases that report a `torch.compile` capture as an export (2.5.1 and 2.9.1 among them), and there a compiled call does
+not check the labels, as on main.
diff --git a/kornia/losses/dice.py b/kornia/losses/dice.py
index 4611736e5..07611d22d 100644
--- a/kornia/losses/dice.py
+++ b/kornia/losses/dice.py
@@ -26,14 +26,147 @@ import torch.nn.functional as F
from torch import nn
from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_IS_TENSOR
+from kornia.core.utils import is_autocast_enabled, is_compiling, is_exporting
from kornia.losses._utils import mask_ignore_pixels
-from kornia.losses.one_hot import one_hot
+from kornia.losses.one_hot import _check_class_labels, one_hot
# based on:
# https://github.com/kevinzakka/pytorch-goodies/blob/master/losses.py
# https://github.com/Lightning-AI/metrics/blob/v0.11.3/src/torchmetrics/functional/classification/dice.py#L66-L207
+# Measured forward + backward, 4 threads, against the one-hot path: the macro Dice loses on small inputs (0.82x to 0.95x
+# up to about 2**13 logits) and wins from about 2**15 (1.2x at 2 x 21 x 32 x 32); the micro Dice wins at every size.
+# Smaller inputs also keep the one-hot path's summation order exactly.
+_CPU_MIN_NUMEL = 1 << 15
+
+
+def _dice_loss_cpu_eligible(pred: torch.Tensor, weight: Optional[torch.Tensor]) -> bool:
+ """Select native-precision CPU logits, whose one-hot target and full-size products are fresh memory every call."""
+ # Keep the one-hot path where it is what the caller captures or asked for: the exporters and the legacy tracer
+ # record it, functorch transforms (vmap, grad) have no batching rule for the label check below, autocast would
+ # lower the batched matrix-vector product below to bfloat16, and it computes a float32 pred with a float64 weight
+ # in float64.
+ return (
+ pred.device.type == "cpu"
+ and pred.dtype in (torch.float32, torch.float64)
+ and (weight is None or (isinstance(weight, torch.Tensor) and weight.dtype == pred.dtype))
+ # Before the size check: torch.export would turn it into a guard of the exported program, which then fails at
+ # run time for logits on the other side of the threshold although its dynamic shapes allow them. The legacy
+ # tracer would record it as a constant with a TracerWarning.
+ and not is_exporting()
+ and not torch.jit.is_tracing()
+ and pred.numel() >= _CPU_MIN_NUMEL
+ and not torch._C._are_functorch_transforms_active()
+ and not is_autocast_enabled()
+ )
+
+
+def _check_weight(pred: torch.Tensor, weight: torch.Tensor) -> None:
+ """Validate the class weights as :func:`dice_loss` does."""
+ num_of_classes = pred.shape[1]
+ KORNIA_CHECK_IS_TENSOR(weight, "weight must be torch.Tensor or None.")
+ KORNIA_CHECK(
+ (weight.shape[0] == num_of_classes and weight.numel() == num_of_classes),
+ f"weight must hold one value per class of pred: got weight of shape {weight.shape} for pred of shape "
+ f"{pred.shape}",
+ )
+ KORNIA_CHECK(
+ weight.device == pred.device,
+ f"weight and pred must be in the same device. Got: {weight.device} and {pred.device}",
+ )
+
+
+def _target_probability(pred_soft: torch.Tensor, target: torch.Tensor, fused: bool) -> torch.Tensor:
+ """The probability ``(B, H, W)`` of each pixel's target class.
+
+ The one-hot selection sums one probability and exact zeros, so a gather gives the same values and gradients
+ bitwise. Eager code gathers: no full-size temporary. ``fused`` selects instead, for ``torch.compile`` to fuse the
+ selection into the softmax and its backward, where the gather's backward would be a separate full-size scatter.
+ """
+ if fused:
+ classes = torch.arange(pred_soft.shape[1], device=pred_soft.device).view(1, -1, 1, 1)
+ return torch.where(classes == target.unsqueeze(1), pred_soft, 0.0).sum(1)
+ return pred_soft.gather(1, target.unsqueeze(1)).squeeze(1)
+
+
+def _dice_loss_cpu(
+ pred: torch.Tensor,
+ target: torch.Tensor,
+ average: str,
+ eps: float,
+ weight: Optional[torch.Tensor],
+ ignore_index: Optional[int],
+) -> torch.Tensor:
+ r"""The Dice loss of :func:`dice_loss` without a one-hot target.
+
+ A one-hot target makes every sum a full-size product: on a large CPU batch, fresh memory that is faulted in on
+ every call. Here the intersection is the softmax of each pixel's target class. The micro Dice sums
+ :math:`\sum_c w_c p_c` per pixel; the macro Dice needs the probability mass of class :math:`c` over the valid
+ pixels of sample :math:`b`, :math:`S_{bc} = \sum_{hw} m\, p_c`, a batched matrix-vector product, and adds its
+ per-class intersections and target pixel counts into ``(B, C)`` bins. The ratio and the averaging are those of
+ :func:`dice_loss`. Values agree with the one-hot sums to rounding.
+ """
+ batch, num_classes = pred.shape[:2]
+ target, target_mask = mask_ignore_pixels(target, ignore_index)
+ KORNIA_CHECK(target.dtype == torch.int64, f"labels must be of dtype torch.int64. Got: {target.dtype}")
+ # one_hot's checks and messages, before any gather, scatter or selection: compiled, a label outside the classes
+ # would be dropped silently by the selection, and an Inductor gather's bounds check aborts the process.
+ target = _check_class_labels(target, num_classes)
+ if weight is not None:
+ _check_weight(pred, weight)
+
+ pred_soft = pred.softmax(1)
+ valid = torch.ones_like(target, dtype=pred.dtype) if target_mask is None else target_mask.to(pred.dtype)
+ # The masses before the target probability: the backward then reaches the gather's fresh gradient first and adds
+ # the masses' gradient to it in place, instead of allocating another full-size buffer for the sum.
+ if average == "micro":
+ # 2 sum(w_t p_t) / (sum_c w_c sum(p_c) + sum(w_t)) over the valid pixels, w = 1 without weights.
+ if weight is None:
+ mass = (pred_soft.sum(1) * valid).sum((1, 2))
+ else:
+ mass = ((pred_soft * weight.view(1, -1, 1, 1)).sum(1) * valid).sum((1, 2))
+ # Measured compiled on the CPU, the micro sums fuse with the selection (1.15x over the one-hot path at batch 8
+ # and 32) and not with the gather (0.98x).
+ prob_target = _target_probability(pred_soft, target, fused=is_compiling())
+ if weight is None:
+ target_mass = valid
+ else:
+ # A gather, not weight[target]: the indexing backward accumulates in a nondeterministic order on the CPU.
+ # In float64: the gather's backward is a CPU scatter_add, one running sum per class over the pixels.
+ weight_target = weight.reshape(-1).to(torch.float64).expand(batch, num_classes).gather(1, target.flatten(1))
+ weight_target = weight_target.view(target.shape).to(pred.dtype)
+ target_mass = weight_target * valid
+ intersection = (prob_target * target_mass).sum((1, 2))
+ cardinality = mass + target_mass.sum((1, 2))
+ present = None
+ else:
+ class_mass = pred_soft.sum((2, 3)) if target_mask is None else torch.einsum("bchw,bhw->bc", pred_soft, valid)
+ # The bins below are a scatter that breaks fusion anyway; compiled, the gather is then the faster form (1.10x
+ # over the one-hot path, against 1.04x for the selection).
+ prob_target = _target_probability(pred_soft, target, fused=False) * valid
+ index = target.flatten(1)
+ # Float64 bins: a CPU scatter_add adds each class's pixels into one running sum, and a float32 one rounds
+ # a target probability near 1 up to 1 once the sum passes a few thousand, so a class's Dice could exceed 1.
+ bins = pred_soft.new_zeros(batch, num_classes, dtype=torch.float64)
+ intersection = bins.scatter_add(1, index, prob_target.flatten(1).to(torch.float64)).to(pred.dtype)
+ target_count = bins.scatter_add(1, index, valid.flatten(1).to(torch.float64)).to(pred.dtype)
+ cardinality = class_mass + target_count
+ present = target_count > 0
+
+ weight = pred.new_ones(num_classes) if weight is None else weight
+ empty = cardinality == 0
+ dice_score = (2.0 * intersection / (cardinality + eps).masked_fill(empty, 1.0)).masked_fill(empty, 0.0)
+ dice_loss = -dice_score + 1.0
+ if present is not None:
+ weight = weight * present
+ normalizer = weight.sum(-1)
+ empty = normalizer == 0
+ dice_loss = (dice_loss * weight).sum(-1) / normalizer.masked_fill(empty, 1)
+ dice_loss = dice_loss.masked_fill(empty, 1)
+ return torch.mean(dice_loss).to(torch.promote_types(pred.dtype, weight.dtype))
+
+
def dice_loss(
pred: torch.Tensor,
target: torch.Tensor,
@@ -78,6 +211,16 @@ def dice_loss(
- Pixels labelled ``ignore_index`` (default ``-100``) leave both sums of their own sample. A sample whose
pixels are all ignored, or whose present classes all have weight 0, enters the batch mean as loss 1 with a
zero gradient, at ``eps=0`` as well.
+ - On the CPU, float32 or float64 logits with at least :math:`2^{15}` values and a ``weight`` that is
+ ``None`` or of their dtype skip the one-hot target, unless ``torch.export`` or ``torch.jit.trace`` captures
+ the call, a functorch transform runs or autocast is on: the intersection takes each pixel's target-class
+ probability, ``'micro'`` sums the (weighted) probabilities of each pixel over the classes, and ``'macro'``
+ takes the probability mass of each class from a batched matrix-vector product and adds the per-class sums
+ into ``(B, C)`` bins, so no full-size product with a one-hot target is formed. The result agrees with the
+ one-hot sums to rounding, with the same NaN positions; a label outside ``[0, C)`` that is not
+ ``ignore_index`` raises the same ``RuntimeError`` as ``one_hot``, also under ``torch.compile``. Some torch
+ releases (2.5.1 and 2.9.1 among them) report a ``torch.compile`` capture as an export: a compiled call keeps
+ the one-hot target there, and its compiled ``one_hot`` does not check the labels.
Args:
pred: logits torch.Tensor with shape :math:`(N, C, H, W)` where C = number of classes.
@@ -126,6 +269,9 @@ def dice_loss(
possible_average = {"micro", "macro"}
KORNIA_CHECK(average in possible_average, f"The `average` has to be one of {possible_average}. Got: {average}")
+ if _dice_loss_cpu_eligible(pred, weight):
+ return _dice_loss_cpu(pred, target, average, eps, weight, ignore_index)
+
# compute F.softmax over the classes axis
pred_soft: torch.Tensor = F.softmax(pred, dim=1)
diff --git a/tests/losses/test_dice_cpu.py b/tests/losses/test_dice_cpu.py
new file mode 100644
index 000000000..91f94d939
--- /dev/null
+++ b/tests/losses/test_dice_cpu.py
@@ -0,0 +1,464 @@
+# LICENSE HEADER MANAGED BY add-license-header
+#
+# Copyright 2018 Kornia Team
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import importlib
+import math
+import subprocess
+import sys
+import warnings
+from pathlib import Path
+from typing import Optional
+
+import pytest
+import torch
+
+from kornia.core._compat import torch_version_ge
+from kornia.core.utils import is_exporting
+from kornia.losses import DiceLoss, dice_loss
+
+from testing.base import DYNAMO_UNAVAILABLE_REASON, BaseTester, dynamo_is_available
+
+dice_module = importlib.import_module("kornia.losses.dice")
+
+# 2 x 21 x 128 x 128 logits are past the CPU path's size threshold; the 21 classes are those of the flagship benchmark.
+LARGE = (2, 21, 128, 128)
+
+
+def _require_native_cpu(device: torch.device, dtype: torch.dtype) -> None:
+ if device.type != "cpu" or dtype not in (torch.float32, torch.float64):
+ pytest.skip("the CPU one-hot-free Dice path supports native CPU float32 and float64 only")
+
+
+def _inputs(shape, device, dtype, ignore_index: Optional[int] = -100, seed: int = 0):
+ generator = torch.Generator().manual_seed(seed)
+ logits = (3 * torch.randn(shape, generator=generator)).to(device=device, dtype=dtype)
+ # Classes 0 .. C - 2 only: the last class is absent from the target, as macro averaging must notice.
+ labels = torch.randint(max(1, shape[1] - 1), (shape[0], *shape[2:]), generator=generator)
+ if ignore_index is not None:
+ labels[torch.rand(labels.shape, generator=generator) < 0.2] = ignore_index
+ return logits, labels.to(device)
+
+
+def _value_and_grads(logits, labels, *args, weight=None, **kwargs):
+ logits = logits.detach().clone().requires_grad_()
+ inputs = [logits]
+ if weight is not None:
+ weight = weight.detach().clone().requires_grad_()
+ inputs.append(weight)
+ loss = dice_loss(logits, labels, *args, weight=weight, **kwargs)
+ return (loss, *torch.autograd.grad(loss, inputs))
+
+
+def _reference(monkeypatch, *args, **kwargs):
+ """Value and gradients of the one-hot path: a one-hot target and full-size products."""
+ with monkeypatch.context() as patch:
+ patch.setattr(dice_module, "_dice_loss_cpu_eligible", lambda pred, weight: False)
+ return _value_and_grads(*args, **kwargs)
+
+
+def _count_calls(monkeypatch) -> list:
+ calls = []
+ cpu_loss = dice_module._dice_loss_cpu
+
+ def counting(pred, *args):
+ calls.append(pred.shape)
+ return cpu_loss(pred, *args)
+
+ monkeypatch.setattr(dice_module, "_dice_loss_cpu", counting)
+ return calls
+
+
+class TestDiceLossCpu(BaseTester):
+ def assert_close_scaled(self, actual: torch.Tensor, expected: torch.Tensor) -> None:
+ """``assert_close`` relative to the largest finite entry of ``expected``.
+
+ A mean over a large batch makes gradients of about 1 / (B H W), far below the absolute tolerance, so both
+ sides are divided by that entry first; the dtype tolerances then bound the error relative to it.
+ """
+ finite = expected[expected.isfinite()]
+ scale = finite.abs().max() if finite.numel() else expected.new_ones(())
+ scale = torch.where(scale > 0, scale, torch.ones_like(scale))
+ self.assert_close(actual / scale, expected / scale)
+
+ def assert_close_with_nan(self, actual: torch.Tensor, expected: torch.Tensor) -> None:
+ assert torch.equal(actual.isnan(), expected.isnan())
+ self.assert_close_scaled(actual.masked_fill(actual.isnan(), 0), expected.masked_fill(expected.isnan(), 0))
+
+ @pytest.mark.parametrize("average", ["micro", "macro"])
+ @pytest.mark.parametrize("weighted", [False, True])
+ def test_large_input_takes_the_cpu_path(self, monkeypatch, average, weighted, device, dtype):
+ _require_native_cpu(device, dtype)
+ calls = _count_calls(monkeypatch)
+ logits, labels = _inputs(LARGE, device, dtype)
+ weight = torch.linspace(0.5, 1.5, LARGE[1], device=device, dtype=dtype) if weighted else None
+ _value_and_grads(logits, labels, average, weight=weight)
+ with torch.no_grad():
+ dice_loss(logits.requires_grad_(), labels, average, weight=weight)
+ assert calls == [LARGE, LARGE]
+
+ @pytest.mark.parametrize(
+ "case",
+ ["small", "float32-pred-float64-weight", "half", "trace", "functorch", "autocast"],
+ ids=lambda case: case,
+ )
+ def test_other_inputs_keep_the_one_hot_target(self, monkeypatch, case, device, dtype):
+ calls = _count_calls(monkeypatch)
+ if case == "half":
+ dtype = torch.bfloat16
+ logits, labels = _inputs((1, 21, 32, 32) if case == "small" else LARGE, device, dtype)
+ weight = None
+ if case == "float32-pred-float64-weight":
+ if device.type == "mps":
+ pytest.skip("MPS has no float64")
+ logits, weight = logits.float(), torch.ones(LARGE[1], device=device, dtype=torch.float64)
+ if case == "trace":
+ torch.jit.trace(lambda x: dice_loss(x, labels), (logits,), check_trace=False)
+ elif case == "functorch":
+ torch.vmap(lambda x, y: dice_loss(x[None], y[None]))(logits, labels)
+ torch.func.grad(lambda x: dice_loss(x, labels))(logits)
+ elif case == "autocast":
+ with torch.autocast(device.type, dtype=torch.bfloat16):
+ dice_loss(logits, labels)
+ else:
+ dice_loss(logits, labels, weight=weight)
+ assert calls == []
+
+ @pytest.mark.parametrize("average", ["micro", "macro"])
+ @pytest.mark.parametrize("weighted", [False, True])
+ @pytest.mark.parametrize("ignore_index", [None, -100, 255])
+ @pytest.mark.parametrize("eps", [1e-8, 0.0])
+ def test_matches_one_hot_target(self, monkeypatch, average, weighted, ignore_index, eps, device, dtype):
+ _require_native_cpu(device, dtype)
+ logits, labels = _inputs(LARGE, device, dtype, ignore_index)
+ weight = torch.linspace(0.5, 1.5, LARGE[1], device=device, dtype=dtype) if weighted else None
+ kwargs = {"weight": weight, "ignore_index": ignore_index}
+ expected = _reference(monkeypatch, logits, labels, average, eps, **kwargs)
+ actual = _value_and_grads(logits, labels, average, eps, **kwargs)
+ for actual_value, expected_value in zip(actual, expected):
+ self.assert_close_scaled(actual_value, expected_value)
+
+ @pytest.mark.parametrize("average", ["micro", "macro"])
+ @pytest.mark.parametrize("weighted", [False, True])
+ def test_large_homogeneous_target_matches_one_hot_target(self, monkeypatch, average, weighted, device, dtype):
+ # A CPU scatter_add adds every pixel of a class into one running sum: the macro bins, and the backward of the
+ # gather of the class weights. In float32, adding p_t = 0.9 to it 2**18 times gave an intersection of 236,496
+ # instead of 235,930, a 4 % loss error.
+ _require_native_cpu(device, dtype)
+ calls = _count_calls(monkeypatch)
+ logits = torch.zeros(1, 2, 512, 512, device=device, dtype=dtype)
+ logits[:, 0] = math.log(9.0)
+ labels = torch.zeros(1, 512, 512, device=device, dtype=torch.long)
+ weight = torch.tensor([0.5, 1.5], device=device, dtype=dtype) if weighted else None
+ expected = _reference(monkeypatch, logits, labels, average, weight=weight, ignore_index=None)
+ actual = _value_and_grads(logits, labels, average, weight=weight, ignore_index=None)
+ for actual_value, expected_value in zip(actual, expected):
+ self.assert_close_scaled(actual_value, expected_value)
+ assert calls
+
+ @pytest.mark.parametrize("shape", [(21, 1), (21, 1, 1)])
+ def test_column_weight_matches_one_hot_target(self, monkeypatch, shape, device, dtype):
+ # The weight check accepts one value per class along dim 0 in any shape, and the one-hot micro Dice uses it.
+ _require_native_cpu(device, dtype)
+ calls = _count_calls(monkeypatch)
+ logits, labels = _inputs(LARGE, device, dtype)
+ weight = torch.linspace(0.5, 1.5, LARGE[1], device=device, dtype=dtype).reshape(shape)
+ expected = _reference(monkeypatch, logits, labels, "micro", weight=weight)
+ actual = _value_and_grads(logits, labels, "micro", weight=weight)
+ for actual_value, expected_value in zip(actual, expected):
+ self.assert_close_scaled(actual_value, expected_value)
+ assert calls
+
+ @pytest.mark.parametrize("average", ["micro", "macro"])
+ @pytest.mark.parametrize("weighted", [False, True])
+ def test_edge_samples_match_one_hot_target(self, monkeypatch, average, weighted, device, dtype):
+ # Sample 0 is fully ignored, sample 1 holds class 0 only, whose weight is 0 when weighted; sample 2 is normal.
+ _require_native_cpu(device, dtype)
+ monkeypatch.setattr(dice_module, "_CPU_MIN_NUMEL", 0)
+ calls = _count_calls(monkeypatch)
+ logits, labels = _inputs((3, 4, 5, 6), device, dtype)
+ labels[0] = -100
+ labels[1] = 0
+ weight = torch.tensor([0.0, 1.0, 2.0, 3.0], device=device, dtype=dtype) if weighted else None
+ for eps in (1e-8, 0.0):
+ expected = _reference(monkeypatch, logits, labels, average, eps, weight=weight)
+ actual = _value_and_grads(logits, labels, average, eps, weight=weight)
+ for actual_value, expected_value in zip(actual, expected):
+ self.assert_close_scaled(actual_value, expected_value)
+ assert calls
+
+ @pytest.mark.parametrize("shape", [(3, 1, 4, 5), (0, 4, 5, 6), (3, 4, 0, 6), (1, 2, 1, 1)])
+ @pytest.mark.parametrize("average", ["micro", "macro"])
+ def test_shapes_match_one_hot_target(self, monkeypatch, shape, average, device, dtype):
+ _require_native_cpu(device, dtype)
+ monkeypatch.setattr(dice_module, "_CPU_MIN_NUMEL", 0)
+ calls = _count_calls(monkeypatch)
+ logits, labels = _inputs(shape, device, dtype, ignore_index=None)
+ expected = _reference(monkeypatch, logits, labels, average)
+ actual = _value_and_grads(logits, labels, average)
+ self.assert_close_with_nan(actual[0], expected[0])
+ self.assert_close_scaled(actual[1], expected[1])
+ assert calls
+
+ @pytest.mark.parametrize("layout", ["transposed", "channels_last", "sliced"])
+ @pytest.mark.parametrize("average", ["micro", "macro"])
+ @pytest.mark.parametrize("weighted", [False, True])
+ def test_strided_inputs_match_one_hot_target(self, monkeypatch, layout, average, weighted, device, dtype):
+ _require_native_cpu(device, dtype)
+ logits, labels = _inputs(LARGE, device, dtype)
+ if layout == "transposed":
+ logits, labels = logits.transpose(-1, -2), labels.transpose(-1, -2)
+ elif layout == "channels_last":
+ logits = logits.contiguous(memory_format=torch.channels_last)
+ else:
+ logits = torch.cat([logits, logits], dim=1)[:, ::2]
+ weight = torch.linspace(0.5, 1.5, LARGE[1], device=device, dtype=dtype) if weighted else None
+ expected = _reference(monkeypatch, logits, labels, average, weight=weight)
+ actual = _value_and_grads(logits, labels, average, weight=weight)
+ for actual_value, expected_value in zip(actual, expected):
+ self.assert_close_scaled(actual_value, expected_value)
+ # The gradient keeps the one-hot path's layout.
+ assert actual[1].stride() == expected[1].stride()
+
+ @pytest.mark.parametrize("average", ["micro", "macro"])
+ @pytest.mark.parametrize("ignore_index", [None, -100])
+ def test_non_finite_logits_match_one_hot_target(self, monkeypatch, average, ignore_index, device, dtype):
+ _require_native_cpu(device, dtype)
+ logits, labels = _inputs(LARGE, device, dtype, ignore_index=None)
+ # +inf, -inf and NaN logits on a labelled pixel of sample 0; with an ignore index, the same on an ignored one.
+ logits[0, 3, 0, 0] = float("inf")
+ logits[0, 5, 0, 1] = float("-inf")
+ logits[0, 7, 0, 2] = float("nan")
+ logits[0, :, 0, 3] = float("-inf")
+ if ignore_index is not None:
+ logits[0, 3, 1, 0] = float("inf")
+ logits[0, 7, 1, 2] = float("nan")
+ labels[0, 1] = ignore_index
+ expected = _reference(monkeypatch, logits, labels, average, ignore_index=ignore_index)
+ actual = _value_and_grads(logits, labels, average, ignore_index=ignore_index)
+ self.assert_close_with_nan(actual[0], expected[0])
+ self.assert_close_with_nan(actual[1], expected[1])
+ # Sample 1 has finite logits: its gradient is unaffected.
+ assert actual[1][1].isfinite().all()
+
+ def test_fused_selection_is_bitwise_the_gather(self, device, dtype):
+ # The one-hot comparison that torch.compile fuses selects the same values and gradients as the gather.
+ _require_native_cpu(device, dtype)
+ logits, labels = _inputs(LARGE, device, dtype, ignore_index=None)
+ logits[0, :, 0, :4] = float("nan")
+ upstream = torch.randn(labels.shape, generator=torch.Generator().manual_seed(1)).to(device=device, dtype=dtype)
+ results = []
+ for fused in (False, True):
+ x = logits.clone().requires_grad_()
+ selected = dice_module._target_probability(x.softmax(1), labels, fused)
+ results.append((selected, *torch.autograd.grad((selected * upstream).nansum(), x)))
+ for actual_value, expected_value in zip(results[1], results[0]):
+ assert torch.equal(actual_value.isnan(), expected_value.isnan())
+ assert torch.equal(actual_value.nan_to_num(), expected_value.nan_to_num())
+
+ @pytest.mark.parametrize("weighted", [False, True])
+ def test_fused_micro_loss_is_bitwise_the_gathered_one(self, monkeypatch, weighted, device, dtype):
+ _require_native_cpu(device, dtype)
+ logits, labels = _inputs(LARGE, device, dtype)
+ weight = torch.linspace(0.5, 1.5, LARGE[1], device=device, dtype=dtype) if weighted else None
+ expected = _value_and_grads(logits, labels, "micro", weight=weight)
+ monkeypatch.setattr(dice_module, "is_compiling", lambda: True)
+ actual = _value_and_grads(logits, labels, "micro", weight=weight)
+ for actual_value, expected_value in zip(actual, expected):
+ assert torch.equal(actual_value, expected_value)
+
+ @pytest.mark.parametrize("average", ["micro", "macro"])
+ def test_gradients_are_deterministic(self, average, device, dtype):
+ # As on the one-hot path, repeated calls give the same bits, also for the weight's gradient.
+ _require_native_cpu(device, dtype)
+ logits, labels = _inputs(LARGE, device, dtype)
+ weight = torch.linspace(0.5, 1.5, LARGE[1], device=device, dtype=dtype)
+ first = _value_and_grads(logits, labels, average, weight=weight)
+ for _ in range(3):
+ for actual_value, expected_value in zip(_value_and_grads(logits, labels, average, weight=weight), first):
+ assert torch.equal(actual_value, expected_value)
+
+ @pytest.mark.parametrize("one_hot_path", [False, True])
+ def test_invalid_labels_raise(self, monkeypatch, one_hot_path, device, dtype):
+ # The same errors as one_hot's on the one-hot path: negative labels first, then labels past the classes.
+ _require_native_cpu(device, dtype)
+ if one_hot_path:
+ monkeypatch.setattr(dice_module, "_dice_loss_cpu_eligible", lambda pred, weight: False)
+ logits, labels = _inputs(LARGE, device, dtype, ignore_index=None)
+ with pytest.raises(Exception, match=r"labels must be of dtype torch\.int64"):
+ dice_loss(logits, labels.float())
+ weight = torch.ones(LARGE[1], device=device, dtype=dtype)
+ for value, message in ((LARGE[1], "Class values must be smaller than num_classes"), (-3, "non-negative")):
+ bad = labels.clone()
+ bad[0, 0, 0] = value
+ for average in ("micro", "macro"):
+ for w in (None, weight):
+ with pytest.raises(RuntimeError, match=message):
+ dice_loss(logits, bad, average, weight=w)
+
+ @pytest.mark.parametrize("average", ["micro", "macro"])
+ def test_gradcheck(self, monkeypatch, average, device):
+ _require_native_cpu(device, torch.float64)
+ monkeypatch.setattr(dice_module, "_CPU_MIN_NUMEL", 0)
+ calls = _count_calls(monkeypatch)
+ logits, labels = _inputs((2, 4, 3, 5), device, torch.float64)
+ weight = torch.tensor([0.5, 1.0, 2.0, 1.5], device=device, dtype=torch.float64)
+ self.gradcheck(
+ lambda x, w: dice_loss(x, labels, average, weight=w), (logits, weight), dtypes=[torch.float64] * 2
+ )
+ logits.requires_grad_()
+ assert torch.autograd.gradgradcheck(lambda x: dice_loss(x, labels, average), (logits,))
+ assert calls
+
+ @pytest.mark.parametrize("average", ["micro", "macro"])
+ def test_forward_mode_ad_matches_one_hot_target(self, monkeypatch, average, device, dtype):
+ _require_native_cpu(device, dtype)
+ logits, labels = _inputs(LARGE, device, dtype)
+ tangent = torch.randn_like(logits)
+
+ def jvp():
+ with torch.autograd.forward_ad.dual_level():
+ dual = torch.autograd.forward_ad.make_dual(logits, tangent)
+ return torch.autograd.forward_ad.unpack_dual(dice_loss(dual, labels, average))
+
+ calls = _count_calls(monkeypatch)
+ actual = jvp()
+ assert calls
+ with monkeypatch.context() as patch:
+ patch.setattr(dice_module, "_dice_loss_cpu_eligible", lambda pred, weight: False)
+ expected = jvp()
+ self.assert_close(actual.primal, expected.primal)
+ self.assert_close_scaled(actual.tangent, expected.tangent)
+
+ @pytest.mark.parametrize("average", ["micro", "macro"])
+ def test_vmap_has_no_user_warning(self, monkeypatch, average, device, dtype):
+ # vmap keeps the one-hot target, as on main: the label check has no batching rule.
+ _require_native_cpu(device, dtype)
+ logits, labels = _inputs(LARGE, device, dtype)
+ with warnings.catch_warnings(record=True) as recorded:
+ warnings.simplefilter("always")
+ actual = torch.vmap(lambda x, y: dice_loss(x[None], y[None], average))(logits, labels)
+ assert not [w for w in recorded if issubclass(w.category, UserWarning)]
+ expected = torch.stack([dice_loss(x[None], y[None], average) for x, y in zip(logits, labels)])
+ self.assert_close(actual, expected)
+
+ def test_module_and_backward_to_leaf(self, monkeypatch, device, dtype):
+ _require_native_cpu(device, dtype)
+ logits, labels = _inputs(LARGE, device, dtype)
+ weight = torch.linspace(0.5, 1.5, LARGE[1], device=device, dtype=dtype)
+ expected = _reference(monkeypatch, logits, labels, "macro", weight=weight)
+ leaf = logits.clone().requires_grad_()
+ loss = DiceLoss("macro", weight=weight)(leaf, labels)
+ loss.backward()
+ self.assert_close(loss.detach(), expected[0])
+ self.assert_close_scaled(leaf.grad, expected[1])
+
+ @pytest.mark.parametrize("average", ["micro", "macro"])
+ def test_dynamo_on_an_eligible_input(self, average, device, dtype, torch_optimizer, optimizer_backend):
+ _require_native_cpu(device, dtype)
+ if optimizer_backend == "jit":
+ pytest.skip("dice_loss is not scriptable: its average check builds a set")
+ if dtype == torch.float64 and not torch_version_ge(2, 6):
+ # On torch 2.5.1 a compiled call keeps the one-hot target (is_exporting() is is_compiling() there), and
+ # Inductor cannot compile its float64 backward at this size: "fmadd" has no double-vector overload.
+ pytest.skip("torch 2.5.1's Inductor fails on float64 dice_loss backward, with or without this path")
+ logits, labels = _inputs(LARGE, device, dtype)
+ weight = torch.linspace(0.5, 1.5, LARGE[1], device=device, dtype=dtype)
+ expected = _value_and_grads(logits, labels, average, weight=weight)
+ compiled = torch_optimizer(dice_loss)
+ x = logits.detach().clone().requires_grad_()
+ actual = compiled(x, labels, average, 1e-8, weight)
+ (grad,) = torch.autograd.grad(actual, x)
+ self.assert_close(actual, expected[0])
+ self.assert_close_scaled(grad, expected[1])
+
+
+# Run in a fresh interpreter: an index check that fails inside an Inductor CPU kernel can abort the whole process.
+_COMPILED_INVALID_LABEL = """
+import sys, warnings
+warnings.filterwarnings("ignore")
+import torch, kornia
+average, weighted, value = sys.argv[1], sys.argv[2] == "1", int(sys.argv[3])
+pred = torch.randn(2, 21, 64, 64)
+target = torch.randint(0, 21, (2, 64, 64))
+target[0, 0, 0] = value
+weight = torch.linspace(0.5, 1.5, 21) if weighted else None
+loss = torch.compile(kornia.losses.dice_loss, fullgraph=True)
+try:
+ loss(pred, target, average, 1e-8, weight)
+except RuntimeError as error:
+ print("RuntimeError:", error)
+else:
+ print("no error")
+"""
+
+
+def _compiled_calls_keep_the_one_hot_target() -> bool:
+ # Some releases (2.5.1 and 2.9.1 checked) also report a torch.compile capture as an export, and an exported call
+ # keeps the one-hot target.
+ probe = torch.compile(lambda x: x + float(is_exporting()), backend="eager", fullgraph=True)
+ return bool(probe(torch.zeros(())) > 0)
+
+
[email protected](not dynamo_is_available(), reason=DYNAMO_UNAVAILABLE_REASON)
+class TestDiceLossCpuCompiledLabels(BaseTester):
+ @pytest.mark.parametrize("average, weighted", [("micro", False), ("micro", True), ("macro", False)])
+ @pytest.mark.parametrize("value, message", [(21, "smaller than num_classes"), (-3, "non-negative")])
+ def test_dynamo_invalid_label_raises(self, average, weighted, value, message, device, dtype, optimizer_backend):
+ _require_native_cpu(device, dtype)
+ if dtype != torch.float32 or optimizer_backend != "inductor":
+ pytest.skip("one fresh-process inductor compile per case, in float32")
+ if _compiled_calls_keep_the_one_hot_target():
+ pytest.skip("this torch release compiles the one-hot path, which does not check labels")
+ # The checkout under test, which "-c" imports first from the working directory.
+ root = Path(dice_module.__file__).resolve().parents[2]
+ result = subprocess.run( # noqa: S603 - this interpreter and a literal program; no shell or external input.
+ [sys.executable, "-c", _COMPILED_INVALID_LABEL, average, str(int(weighted)), str(value)],
+ cwd=root,
+ capture_output=True,
+ text=True,
+ timeout=900,
+ check=False,
+ )
+ assert result.returncode == 0, result.stderr[-2000:]
+ assert result.stdout.startswith("RuntimeError:"), result.stdout
+ assert message in result.stdout
+
+
+class _Exportable(torch.nn.Module):
+ def forward(self, logits: torch.Tensor, labels: torch.Tensor) -> torch.Tensor:
+ return dice_loss(logits, labels, "macro")
+
+
[email protected](not dynamo_is_available(), reason=DYNAMO_UNAVAILABLE_REASON)
+class TestDiceLossCpuExport(BaseTester):
+ # torch.export keeps the one-hot target: the CPU path's size check would become a guard of the exported
+ # program, which then fails at run time for logits on the other side of the threshold although its dynamic shapes
+ # allow them.
+ @pytest.mark.parametrize(
+ "example, other", [((2, 21, 128, 128), (3, 21, 8, 12)), ((2, 21, 8, 8), (3, 21, 160, 170))]
+ )
+ def test_dynamic_sizes_run_on_both_sides_of_the_threshold(self, example, other, device, dtype):
+ _require_native_cpu(device, dtype)
+ dim = torch.export.Dim
+ batch, height, width = dim("B", min=1, max=64), dim("H", min=2, max=4096), dim("W", min=2, max=4096)
+ dynamic = {"logits": {0: batch, 2: height, 3: width}, "labels": {0: batch, 1: height, 2: width}}
+ program = torch.export.export(_Exportable(), _inputs(example, device, dtype), dynamic_shapes=dynamic)
+ assert any(node.target == torch.ops.aten.one_hot.default for node in program.graph.nodes)
+ exported = program.module()
+ for shape in (example, other):
+ logits, labels = _inputs(shape, device, dtype)
+ self.assert_close(exported(logits, labels), dice_loss(logits, labels, "macro"))