π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(core): document that is_exporting is export-specific, not capture-specific (#5626)
Adarsh Mishra9f3618092903
diff --git a/kornia/core/utils.py b/kornia/core/utils.pyindex 89f7dfada..3954656d6 100644--- a/kornia/core/utils.py+++ b/kornia/core/utils.py@@ -636,9 +636,10 @@ def is_compiling() -> bool:def _is_exporting_eager() -> bool:if _torch_is_exporting is not None:return bool(_torch_is_exporting())- # torch < 2.6 has no export flag. Inside a Dynamo trace the newer releases constant-fold- # ``torch.compiler.is_exporting`` to ``True`` for ``torch.compile`` as well as for- # ``torch.export``, so ``is_compiling`` is the fallback with the same semantics.+ # torch < 2.6 has no export flag, so ``is_compiling`` is the fallback: it is true under+ # ``torch.compile`` and ``torch.export`` alike. Newer releases differ - ``torch.compile`` can+ # report ``False`` for the export flag - which is why call sites must pick the predicate they+ # actually mean.return is_compiling()@@ -647,11 +648,19 @@ def is_exporting() -> bool:Used to switch to export-safe arithmetic (closed-form inverses, ``sort``-based medians, ...) andto skip in-``forward`` side effects (e.g. stashing per-call state on ``self``) that- ``torch.export`` rejects, without changing the captured output. Inside a Dynamo trace torch- folds its own flag to ``True`` for ``torch.compile`` too, so the export-safe paths are also- what a compiled graph contains; on torch < 2.6, which has no export flag, ``is_compiling`` is- used for the same reason. Always ``False`` inside TorchScript, so the guard is safe to call- from scripted functions.+ ``torch.export`` rejects, without changing the captured output. Always ``False`` inside+ TorchScript, so the guard is safe to call from scripted functions.++ The flag is meant to be export-specific, not capture-specific, but whether it is also true inside+ a ``torch.compile`` trace depends on the Torch release, so a compiled call can take different+ paths across versions (#5037). It is false under ``torch.jit.trace`` and the legacy ONNX+ exporter. Where the intent is "inside any Dynamo capture" - skipping a data-dependent check that+ breaks ``fullgraph`` - use :func:`is_compiling`, which covers ``torch.compile`` and+ ``torch.export`` alike on every supported release. Keep this predicate for what only an export+ needs: :func:`crop_by_indices` routing to its dedicated export implementation, where letting a+ compiled call take that route changes the sampling the eager and compiled paths agree on, and+ skipping per-call state on ``self`` that ``torch.export`` rejects but a compiled call records and+ a caller reads back afterwards, such as the ``transform_matrix`` of an augmentation."""if torch.jit.is_scripting():return Falsediff --git a/tests/core/test_helpers.py b/tests/core/test_helpers.pyindex 95b5a1b72..ab61f1cee 100644--- a/tests/core/test_helpers.py+++ b/tests/core/test_helpers.py@@ -33,6 +33,7 @@ from kornia.core.utils import (_torch_svd_cast,batched_forward,is_autocast_enabled,+ is_compiling,is_exporting,is_mps_tensor_safe,register_module_state,@@ -40,7 +41,7 @@ from kornia.core.utils import (safe_solve_with_mask,)-from testing.base import BaseTester, assert_close+from testing.base import DYNAMO_UNAVAILABLE_REASON, BaseTester, assert_close, dynamo_is_availabledef _issue_5476_batch(device, dtype):@@ -338,9 +339,9 @@ class TestExportHelpers:assert torch.jit.script(is_exporting)() is Falsedef test_is_exporting_falls_back_to_is_compiling(self, monkeypatch):- # torch < 2.6 has no ``torch.compiler.is_exporting``; inside a Dynamo trace the guard must- # still be true, as it is on newer torch where Dynamo folds the flag to True for- # ``torch.compile`` as well.+ # torch < 2.6 has no ``torch.compiler.is_exporting``, so inside a Dynamo trace the guard falls+ # back to ``is_compiling`` and is true under ``torch.compile`` too. Newer torch answers with its+ # own flag, whose value under ``torch.compile`` depends on the release (#5037).from kornia.core import utilsmonkeypatch.setattr(utils, "_torch_is_exporting", None)@@ -357,6 +358,33 @@ class TestExportHelpers:pytest.skip(f"no Dynamo here: {e}")assert seen == [True]+ @pytest.mark.skipif(not dynamo_is_available(), reason=DYNAMO_UNAVAILABLE_REASON)+ def test_is_compiling_covers_capture_and_export(self):+ # Whether ``is_exporting`` is also true inside a compiled trace depends on the Torch release+ # (#5037), so the promise that holds across versions is on ``is_compiling``: both capture+ # modes are reported, which is what a guard that only means "do not branch on data" needs.+ x = torch.zeros(2)+ seen = []++ def fn(v):+ seen.append(is_compiling())+ return v + 1++ torch.compile(fn, backend="eager")(x)+ assert seen == [True]++ exported = []++ class Mod(torch.nn.Module):+ def forward(self, v):+ exported.append(is_compiling())+ return v + 1++ torch.export.export(Mod(), (x,))+ assert exported+ assert all(exported)+ assert is_compiling() is False+def test_register_module_state_wraps_leaf(self, device, dtype):m = torch.nn.Module()x = torch.rand(3, device=device, dtype=dtype)