π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
# 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 re
import subprocess
import sys
import textwrap
from typing import Optional, Union
import pytest
import torch
from kornia.core._compat import torch_version_ge
from kornia.core.check import (
KORNIA_CHECK,
KORNIA_CHECK_DM_DESC,
KORNIA_CHECK_IS_COLOR,
KORNIA_CHECK_IS_COLOR_OR_GRAY,
KORNIA_CHECK_IS_GRAY,
KORNIA_CHECK_IS_IMAGE,
KORNIA_CHECK_IS_LIST_OF_TENSOR,
KORNIA_CHECK_IS_TENSOR,
KORNIA_CHECK_LAF,
KORNIA_CHECK_SAME_DEVICE,
KORNIA_CHECK_SAME_DEVICES,
KORNIA_CHECK_SAME_SHAPE,
KORNIA_CHECK_SAME_SIZE,
KORNIA_CHECK_SHAPE,
KORNIA_CHECK_SIZES,
KORNIA_CHECK_TYPE,
_check_async,
are_checks_enabled,
disable_checks,
enable_checks,
)
from kornia.core.exceptions import (
AsyncCheckError,
BaseError,
DeviceError,
ImageError,
ShapeError,
TypeCheckError,
ValueCheckError,
)
from testing.base import BaseTester, dynamo_is_available
class TestCheck:
def test_valid(self):
assert KORNIA_CHECK(True, "This is a test") is True
def test_invalid(self):
with pytest.raises(BaseError):
KORNIA_CHECK(False, "This is a test")
def test_invalid_raises_false(self):
assert KORNIA_CHECK(False, "This should not raise", raises=False) is False
def test_jit(self):
op_jit = torch.jit.script(KORNIA_CHECK)
assert op_jit is not None
assert op_jit(True, "Testing") is True
class TestCheckShape:
@pytest.mark.parametrize(
"data,shape",
[
(torch.rand(2, 3), ["*", "H", "W"]),
(torch.rand(3, 2, 3), ["3", "H", "W"]),
(torch.rand(1, 1, 2, 3), ["1", "1", "H", "W"]),
(torch.rand(2, 3, 2, 3), ["2", "3", "H", "W"]),
],
)
def test_valid(self, data, shape):
assert KORNIA_CHECK_SHAPE(data, shape) is True
@pytest.mark.parametrize(
"data,shape",
[
(torch.rand(2, 3), ["1", "H", "W"]),
(torch.rand(3, 2, 3), ["H", "W"]),
(torch.rand(1, 2, 3), ["3", "H", "W"]),
(torch.rand(1, 3, 2, 3), ["2", "C", "H", "W"]),
],
)
def test_invalid(self, data, shape):
with pytest.raises(ShapeError):
KORNIA_CHECK_SHAPE(data, shape)
def test_invalid_raises_false(self):
assert KORNIA_CHECK_SHAPE(torch.rand(2, 3), ["1", "H", "W"], raises=False) is False
def test_jit(self):
op_jit = torch.jit.script(KORNIA_CHECK_SHAPE)
assert op_jit is not None
assert op_jit(torch.rand(2, 3, 2, 3), ["2", "3", "H", "W"]) is True
@pytest.mark.parametrize("size", [(), (7,), (0, 3), (2, 3, 4, 5)])
def test_lone_wildcard_5187(self, device, dtype, size):
x = torch.zeros(size, device=device, dtype=dtype)
assert KORNIA_CHECK_SHAPE(x, ["*"]) is True
assert KORNIA_CHECK_SHAPE(x, ["*"], raises=False) is True
def test_empty_pattern_5187(self, device, dtype):
assert KORNIA_CHECK_SHAPE(torch.zeros((), device=device, dtype=dtype), []) is True
x = torch.zeros(2, 3, device=device, dtype=dtype)
assert KORNIA_CHECK_SHAPE(x, [], raises=False) is False
with pytest.raises(ShapeError, match="expected 0 dimensions, got 2") as exc:
KORNIA_CHECK_SHAPE(x, [], msg="scalar required")
assert exc.value.actual_shape == [2, 3]
assert exc.value.expected_shape == []
assert "scalar required" in str(exc.value)
@pytest.mark.parametrize(
"pattern,dimension",
[(["*", "4", "5"], 3), (["*", "5", "6"], 2), (["2", "4", "*"], 1), (["B", "C", "4", "5"], 3)],
)
def test_mismatch_tensor_dimension_5187(self, device, dtype, pattern, dimension):
x = torch.zeros(2, 3, 4, 6, device=device, dtype=dtype)
with pytest.raises(ShapeError, match=f"at dimension {dimension}:") as exc:
KORNIA_CHECK_SHAPE(x, pattern)
assert exc.value.actual_shape == [2, 3, 4, 6]
assert exc.value.expected_shape == pattern
assert KORNIA_CHECK_SHAPE(x, pattern, raises=False) is False
def test_jit_edge_patterns_5187(self, device, dtype):
op = torch.jit.script(KORNIA_CHECK_SHAPE)
scalar = torch.zeros((), device=device, dtype=dtype)
matrix = torch.zeros(2, 3, device=device, dtype=dtype)
assert op(scalar, []) is True
assert op(matrix, [], raises=False) is False
assert op(matrix, ["*"]) is True
assert op(matrix, ["*", "3"]) is True
assert op(matrix, ["*", "4"], raises=False) is False
class TestCheckSameShape:
def test_valid(self):
assert KORNIA_CHECK_SAME_SHAPE(torch.rand(2, 3), torch.rand(2, 3)) is True
assert KORNIA_CHECK_SAME_SHAPE(torch.rand(1, 2, 3), torch.rand(1, 2, 3)) is True
assert KORNIA_CHECK_SAME_SHAPE(torch.rand(2, 3, 3), torch.rand(2, 3, 3)) is True
def test_jit(self):
op_jit = torch.jit.script(KORNIA_CHECK_SAME_SHAPE)
assert op_jit is not None
assert op_jit(torch.rand(2, 3), torch.rand(2, 3)) is True
def test_invalid(self):
with pytest.raises(ShapeError):
KORNIA_CHECK_SAME_SHAPE(torch.rand(2, 3), torch.rand(2, 2, 3))
with pytest.raises(ShapeError):
KORNIA_CHECK_SAME_SHAPE(torch.rand(2, 3), torch.rand(1, 2, 3))
with pytest.raises(ShapeError):
KORNIA_CHECK_SAME_SHAPE(torch.rand(2, 3), torch.rand(2, 3, 3))
def test_invalid_raises_false(self):
assert KORNIA_CHECK_SAME_SHAPE(torch.rand(2, 3), torch.rand(2, 2, 3), raises=False) is False
class TestCheckType:
def test_valid(self):
assert KORNIA_CHECK_TYPE("hello", str) is True
assert KORNIA_CHECK_TYPE(23, int) is True
assert KORNIA_CHECK_TYPE(torch.rand(1), torch.Tensor) is True
assert KORNIA_CHECK_TYPE(torch.rand(1), (int, torch.Tensor)) is True
def test_invalid(self):
with pytest.raises(TypeCheckError):
KORNIA_CHECK_TYPE("world", int)
with pytest.raises(TypeCheckError):
KORNIA_CHECK_TYPE(23, float)
with pytest.raises(TypeCheckError):
KORNIA_CHECK_TYPE(23, (float, str))
def test_invalid_raises_false(self):
assert KORNIA_CHECK_TYPE("world", int, raises=False) is False
@pytest.mark.parametrize(
"typ, expected",
[
(int | str, "int | str"),
((int | str, bytes), "int | str | bytes"),
(Union[int, str], "int | str"),
(int | None, "int | None"),
(Optional[int], "int | None"),
],
ids=["pep604", "tuple", "typing-union", "pep604-none", "typing-optional"],
)
def test_invalid_union(self, typ, expected):
# A union is named by its members on every Python: typing's unions, and from 3.14 also ``int | str``, are
# themselves named "Union".
assert KORNIA_CHECK_TYPE(1.0, typ, raises=False) is False
with pytest.raises(TypeCheckError, match=rf"expected {re.escape(expected)}, got"):
KORNIA_CHECK_TYPE(1.0, typ)
class TestCheckIsTensor:
def test_valid(self):
assert KORNIA_CHECK_IS_TENSOR(torch.rand(1)) is True
def test_invalid(self):
with pytest.raises(TypeCheckError):
KORNIA_CHECK_IS_TENSOR([1, 2, 3])
def test_invalid_raises_false(self):
assert KORNIA_CHECK_IS_TENSOR([1, 2, 3], raises=False) is False
class TestCheckIsListOfTensor:
def test_valid(self):
assert KORNIA_CHECK_IS_LIST_OF_TENSOR([torch.rand(1), torch.rand(1), torch.rand(1)]) is True
def test_invalid(self):
with pytest.raises(TypeCheckError):
KORNIA_CHECK_IS_LIST_OF_TENSOR([torch.rand(1), [2, 3], torch.rand(1)])
with pytest.raises(TypeCheckError):
KORNIA_CHECK_IS_LIST_OF_TENSOR([1, 2, 3])
def test_invalid_raises_false(self):
assert KORNIA_CHECK_IS_LIST_OF_TENSOR([torch.rand(1), [2, 3], torch.rand(1)], raises=False) is False
@pytest.mark.parametrize(
"x, detail",
[
([torch.zeros(1), 1], "got list; element 1 is int"),
([torch.zeros(1), torch.zeros(1), [2, 3]], "got list; element 2 is list"),
((torch.zeros(1),), "got tuple"),
],
)
def test_invalid_message_names_the_offending_value(self, x, detail):
with pytest.raises(TypeCheckError, match=f"expected list\\[Tensor\\], {detail}\\."):
KORNIA_CHECK_IS_LIST_OF_TENSOR(x)
class TestCheckSameDevice:
def test_valid(self, device):
assert KORNIA_CHECK_SAME_DEVICE(torch.rand(1, device=device), torch.rand(1, device=device)) is True
@pytest.mark.skipif(not torch.cuda.is_available(), reason="Skip if no GPU.")
def test_invalid(self):
with pytest.raises(DeviceError):
KORNIA_CHECK_SAME_DEVICE(torch.rand(1, device="cpu"), torch.rand(1, device="cuda"))
@pytest.mark.skipif(not torch.cuda.is_available(), reason="Skip if no GPU.")
def test_invalid_raises_false(self):
assert (
KORNIA_CHECK_SAME_DEVICE(torch.rand(1, device="cpu"), torch.rand(1, device="cuda"), raises=False) is False
)
class TestCheckSameDevices:
@pytest.mark.parametrize("count", [1, 2, 3])
def test_valid(self, device, count):
assert KORNIA_CHECK_SAME_DEVICES([torch.rand(1, device=device) for _ in range(count)]) is True
@pytest.mark.parametrize(
"tensors, got, actual_type, expected_type",
[
([], "an empty list", None, None),
((), "tuple", tuple, list),
((torch.zeros(1),), "tuple", tuple, list),
(None, "NoneType", type(None), list),
("tensor", "str", str, list),
([1], "a list containing int", int, torch.Tensor),
([torch.zeros(1), 1], "a list containing int", int, torch.Tensor),
],
)
def test_invalid_input(self, tensors, got, actual_type, expected_type):
assert KORNIA_CHECK_SAME_DEVICES(tensors, raises=False) is False
with pytest.raises(TypeCheckError, match=f"Expected a non-empty list of tensors, got {got}\\.") as exc_info:
KORNIA_CHECK_SAME_DEVICES(tensors, raises=True)
assert exc_info.value.actual_type is actual_type
assert exc_info.value.expected_type is expected_type
@pytest.mark.parametrize("tensors", [[], (torch.zeros(1),), [torch.zeros(1), 1]])
def test_invalid_input_message(self, tensors):
with pytest.raises(TypeCheckError, match="custom message"):
KORNIA_CHECK_SAME_DEVICES(tensors, msg="custom message")
def test_invalid(self):
with pytest.raises(DeviceError) as exc_info:
KORNIA_CHECK_SAME_DEVICES([torch.rand(1), torch.empty(1, device="meta")], msg="custom message")
assert exc_info.value.expected_device == torch.device("cpu")
assert exc_info.value.actual_devices == [torch.device("cpu"), torch.device("meta")]
assert "custom message" in str(exc_info.value)
def test_invalid_raises_false(self):
assert KORNIA_CHECK_SAME_DEVICES([torch.rand(1), torch.empty(1, device="meta")], raises=False) is False
class TestCheckIsColor:
def test_valid(self):
assert KORNIA_CHECK_IS_COLOR(torch.rand(3, 4, 4)) is True
assert KORNIA_CHECK_IS_COLOR(torch.rand(1, 3, 4, 4)) is True
assert KORNIA_CHECK_IS_COLOR(torch.rand(2, 3, 4, 4)) is True
def test_invalid(self):
with pytest.raises(ImageError):
KORNIA_CHECK_IS_COLOR(torch.rand(1, 4, 4))
with pytest.raises(ImageError):
KORNIA_CHECK_IS_COLOR(torch.rand(2, 4, 4))
with pytest.raises(ImageError):
KORNIA_CHECK_IS_COLOR(torch.rand(3, 4, 4, 4))
with pytest.raises(ImageError):
KORNIA_CHECK_IS_COLOR(torch.rand(1, 3, 4, 4, 4))
def test_invalid_raises_false(self):
assert KORNIA_CHECK_IS_COLOR(torch.rand(1, 4, 4), raises=False) is False
def test_invalid_message_reports_the_shape(self):
with pytest.raises(ImageError, match=r"Not a color tensor\. Got shape \[2, 4, 5\]\."):
KORNIA_CHECK_IS_COLOR(torch.zeros(2, 4, 5))
def test_jit_raises_image_error(self):
op_jit = torch.jit.script(KORNIA_CHECK_IS_COLOR)
with pytest.raises(torch.jit.Error, match=r"ImageError: Not a color tensor\. Got shape \[2, 4, 5\]\."):
op_jit(torch.zeros(2, 4, 5))
class TestCheckIsGray:
def test_valid(self):
assert KORNIA_CHECK_IS_GRAY(torch.rand(1, 4, 4)) is True
assert KORNIA_CHECK_IS_GRAY(torch.rand(2, 1, 4, 4)) is True
assert KORNIA_CHECK_IS_GRAY(torch.rand(3, 1, 4, 4)) is True
def test_invalid(self):
with pytest.raises(ImageError):
KORNIA_CHECK_IS_GRAY(torch.rand(3, 4, 4))
with pytest.raises(ImageError):
KORNIA_CHECK_IS_GRAY(torch.rand(1, 4, 4, 4))
with pytest.raises(ImageError):
KORNIA_CHECK_IS_GRAY(torch.rand(1, 3, 4, 4))
with pytest.raises(ImageError):
KORNIA_CHECK_IS_GRAY(torch.rand(1, 3, 4, 4, 4))
def test_invalid_raises_false(self):
assert KORNIA_CHECK_IS_GRAY(torch.rand(1, 3, 4, 4, 4), raises=False) is False
def test_invalid_message_reports_the_shape(self):
with pytest.raises(ImageError, match=r"Not a gray tensor\. Got shape \[2, 4, 5\]\."):
KORNIA_CHECK_IS_GRAY(torch.zeros(2, 4, 5))
def test_jit_raises_image_error(self):
op_jit = torch.jit.script(KORNIA_CHECK_IS_GRAY)
with pytest.raises(torch.jit.Error, match=r"ImageError: Not a gray tensor\. Got shape \[2, 4, 5\]\."):
op_jit(torch.zeros(2, 4, 5))
class TestCheckIsColorOrGray:
def test_valid(self):
assert KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(3, 4, 4)) is True
assert KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(1, 3, 4, 4)) is True
assert KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(2, 3, 4, 4)) is True
assert KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(1, 4, 4)) is True
assert KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(2, 1, 4, 4)) is True
assert KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(3, 1, 4, 4)) is True
def test_invalid(self):
with pytest.raises(ImageError):
KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(1, 4, 4, 4))
with pytest.raises(ImageError):
KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(1, 3, 4, 4, 4))
def test_invalid_raises_false(self):
assert KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(1, 4, 4, 4), raises=False) is False
def test_invalid_message_reports_the_shape(self):
with pytest.raises(ImageError, match=r"Not a color or gray tensor\. Got shape \[2, 4, 5\]\."):
KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.zeros(2, 4, 5))
def test_jit_raises_image_error(self):
op_jit = torch.jit.script(KORNIA_CHECK_IS_COLOR_OR_GRAY)
with pytest.raises(torch.jit.Error, match=r"ImageError: Not a color or gray tensor\. Got shape \[2, 4, 5\]\."):
op_jit(torch.zeros(2, 4, 5))
class TestCheckDmDesc:
def test_valid(self):
assert KORNIA_CHECK_DM_DESC(torch.rand(4), torch.rand(8), torch.rand(4, 8)) is True
def test_invalid(self):
with pytest.raises(ShapeError):
KORNIA_CHECK_DM_DESC(torch.rand(4), torch.rand(8), torch.rand(4, 7))
with pytest.raises(ShapeError):
KORNIA_CHECK_DM_DESC(torch.rand(4), torch.rand(8), torch.rand(3, 8))
with pytest.raises(ShapeError):
KORNIA_CHECK_DM_DESC(torch.rand(4), torch.rand(8), torch.rand(3, 7))
with pytest.raises(ShapeError):
KORNIA_CHECK_DM_DESC(torch.rand(4), torch.rand(8), torch.rand(4, 3, 8))
def test_invalid_raises_false(self):
assert KORNIA_CHECK_DM_DESC(torch.rand(4), torch.rand(8), torch.rand(4, 7), raises=False) is False
@pytest.mark.parametrize(
"desc1, desc2, dm",
[
(torch.zeros(4, 128), torch.zeros(8, 128), torch.zeros(4)),
(torch.zeros(()), torch.zeros(8, 128), torch.zeros(4, 8)),
(torch.zeros(4, 128), torch.zeros(()), torch.zeros(4, 8)),
],
)
def test_invalid_rank(self, desc1, desc2, dm):
assert KORNIA_CHECK_DM_DESC(desc1, desc2, dm, raises=False) is False
with pytest.raises(ShapeError):
KORNIA_CHECK_DM_DESC(desc1, desc2, dm)
class TestCheckLaf:
def test_valid(self):
assert KORNIA_CHECK_LAF(torch.rand(4, 2, 2, 3)) is True
def test_invalid(self):
with pytest.raises(ShapeError):
KORNIA_CHECK_LAF(torch.rand(4, 2, 2))
with pytest.raises(ShapeError):
KORNIA_CHECK_LAF(torch.rand(4, 2, 3, 2))
with pytest.raises(ShapeError):
KORNIA_CHECK_LAF(torch.rand(4, 2, 2, 2))
with pytest.raises(ShapeError):
KORNIA_CHECK_LAF(torch.rand(4, 2, 3, 3, 3))
def test_invalid_raises_false(self):
assert KORNIA_CHECK_LAF(torch.rand(4, 2, 2), raises=False) is False
class TestCheckIsImage(BaseTester):
def test_valid_float(self):
assert KORNIA_CHECK_IS_IMAGE(torch.rand(3, 4, 4)) is True
assert KORNIA_CHECK_IS_IMAGE(torch.rand(2, 3, 4, 4)) is True
assert KORNIA_CHECK_IS_IMAGE(torch.rand(1, 1, 4, 4)) is True
def test_valid_int(self):
x = torch.randint(0, 256, (3, 4, 4), dtype=torch.uint8)
assert KORNIA_CHECK_IS_IMAGE(x) is True
y = torch.randint(0, 256, (2, 3, 4, 4), dtype=torch.uint8)
assert KORNIA_CHECK_IS_IMAGE(y) is True
def test_invalid_float_range(self):
with pytest.raises(ValueCheckError):
KORNIA_CHECK_IS_IMAGE(torch.tensor([[[-0.5, 1.2]]], dtype=torch.float32))
def test_invalid_int_range(self):
bad = torch.tensor([[[300]]], dtype=torch.int32)
with pytest.raises(ValueCheckError):
KORNIA_CHECK_IS_IMAGE(bad)
def test_invalid_shape(self):
x = torch.rand(1, 4, 4, 4)
with pytest.raises(ImageError):
KORNIA_CHECK_IS_IMAGE(x)
def test_invalid_range_no_raise(self):
bad = torch.tensor([[[-0.1, 2.0]]], dtype=torch.float32)
assert KORNIA_CHECK_IS_IMAGE(bad, raises=False) is False
def test_invalid_shape_no_raise(self):
bad = torch.rand(1, 4, 4, 4)
assert KORNIA_CHECK_IS_IMAGE(bad, raises=False) is False
@pytest.mark.parametrize("shape", [(5, 4, 5), (2, 4, 4, 4), (4, 5), (7,), ()])
def test_shape_verdict_does_not_depend_on_raises(self, device, dtype, shape):
# The values lie in [0, 1], so only the shape can fail the check.
x = torch.rand(shape, device=device, dtype=dtype)
assert KORNIA_CHECK_IS_IMAGE(x, raises=False) is False
with pytest.raises(ImageError):
KORNIA_CHECK_IS_IMAGE(x)
@pytest.mark.parametrize("nan_count", ["one", "all"])
def test_nan_fails_the_range_check(self, device, dtype, nan_count):
x = torch.rand(2, 3, 4, 5, device=device, dtype=dtype)
if nan_count == "all":
x.fill_(float("nan"))
else:
x[1, 2, 3, 4] = float("nan")
assert KORNIA_CHECK_IS_IMAGE(x, raises=False) is False
with pytest.raises(ValueCheckError, match=r"expected \[0, 1\]"):
KORNIA_CHECK_IS_IMAGE(x)
def test_float_range_error_reports_the_unit_range(self, device, dtype):
x = torch.tensor([[[-0.5, 0.25, 1.5]]], device=device, dtype=dtype)
with pytest.raises(ValueCheckError, match=r"expected \[0, 1\], got \[-0\.5, 1\.5\]\.") as err:
KORNIA_CHECK_IS_IMAGE(x)
assert err.value.actual_value == (-0.5, 1.5)
assert err.value.expected_range == (0.0, 1.0)
@pytest.mark.parametrize("shape", [(0, 3, 4, 5), (0, 1, 4, 5), (3, 0, 0)])
def test_empty_image_passes(self, device, dtype, shape):
# An empty image holds no value that could be out of range.
x = torch.rand(shape, device=device, dtype=dtype)
assert KORNIA_CHECK_IS_IMAGE(x, raises=False) is True
assert KORNIA_CHECK_IS_IMAGE(x) is True
u = torch.zeros(shape, device=device, dtype=torch.uint8)
assert KORNIA_CHECK_IS_IMAGE(u, raises=False) is True
assert KORNIA_CHECK_IS_IMAGE(u) is True
def test_empty_tensor_with_a_bad_shape_fails(self, device, dtype):
x = torch.rand(0, 5, 4, 5, device=device, dtype=dtype)
assert KORNIA_CHECK_IS_IMAGE(x, raises=False) is False
with pytest.raises(ImageError):
KORNIA_CHECK_IS_IMAGE(x)
@pytest.mark.parametrize("bits", [4, 8, 10, 16])
def test_integer_range_is_zero_to_two_to_the_bits_minus_one(self, device, bits):
top = 2**bits - 1
assert KORNIA_CHECK_IS_IMAGE(torch.full((3, 4, 5), top, device=device, dtype=torch.int32), bits=bits) is True
x = torch.full((3, 4, 5), top + 1, device=device, dtype=torch.int32)
assert KORNIA_CHECK_IS_IMAGE(x, bits=bits, raises=False) is False
with pytest.raises(ValueCheckError, match=rf"expected \[0, {top}\], got \[{top + 1}, {top + 1}\]\.") as err:
KORNIA_CHECK_IS_IMAGE(x, bits=bits)
assert err.value.actual_value == (top + 1, top + 1)
assert err.value.expected_range == (0, top)
@pytest.mark.parametrize(
("int_dtype", "bits"),
[(torch.int8, 8), (torch.int8, 9), (torch.int16, 16), (torch.int32, 32), (torch.int64, 64), (torch.int64, 63)],
)
def test_signed_dtype_accepts_every_nonnegative_value_that_fits_the_bits(self, device, int_dtype, bits):
# 2**bits - 1 exceeds these dtypes' maximum, so every non-negative value of the dtype is in range.
top = torch.iinfo(int_dtype).max
x = torch.tensor([[[0, 5, top]]], device=device, dtype=int_dtype)
assert KORNIA_CHECK_IS_IMAGE(x, bits=bits, raises=False) is True
assert KORNIA_CHECK_IS_IMAGE(x, bits=bits) is True
x[0, 0, 0] = -1
assert KORNIA_CHECK_IS_IMAGE(x, bits=bits, raises=False) is False
with pytest.raises(ValueCheckError) as err:
KORNIA_CHECK_IS_IMAGE(x, bits=bits)
assert err.value.actual_value == (-1, top)
assert err.value.expected_range == (0, 2**bits - 1)
@pytest.mark.parametrize("uint_dtype", [torch.uint16, torch.uint32, torch.uint64])
def test_wide_unsigned_dtype_is_range_checked(self, device, uint_dtype):
info = torch.iinfo(uint_dtype)
x = torch.tensor([[[0, 1000, info.max]]], dtype=uint_dtype).to(device)
assert KORNIA_CHECK_IS_IMAGE(x, bits=info.bits, raises=False) is True
assert KORNIA_CHECK_IS_IMAGE(x, bits=info.bits) is True
# With one bit fewer the largest value is out of range, and the error reports it exactly.
assert KORNIA_CHECK_IS_IMAGE(x, bits=info.bits - 1, raises=False) is False
with pytest.raises(ValueCheckError) as err:
KORNIA_CHECK_IS_IMAGE(x, bits=info.bits - 1)
assert err.value.actual_value == (0, info.max)
assert err.value.expected_range == (0, 2 ** (info.bits - 1) - 1)
class TestChecksEnableDisable:
"""Tests for the runtime enable/disable check control API."""
def setup_method(self):
# Always restore checks after each test to avoid side effects
enable_checks()
def teardown_method(self):
enable_checks()
def test_are_checks_enabled_default(self):
enable_checks()
assert are_checks_enabled() is True
def test_disable_checks(self):
disable_checks()
assert are_checks_enabled() is False
def test_enable_checks(self):
disable_checks()
enable_checks()
assert are_checks_enabled() is True
def test_disabled_checks_bypass_kornia_check(self):
disable_checks()
# With checks disabled, KORNIA_CHECK should return True even for False condition
result = KORNIA_CHECK(False, "should be bypassed")
assert result is True
def test_disabled_checks_bypass_shape_check(self):
disable_checks()
# With checks disabled, wrong shape should not raise
result = KORNIA_CHECK_SHAPE(torch.rand(2, 3), ["1", "H", "W"])
assert result is True
def test_enabled_checks_enforce_shape_check(self):
enable_checks()
with pytest.raises(ShapeError):
KORNIA_CHECK_SHAPE(torch.rand(2, 3), ["1", "H", "W"])
def test_env_var_disables_checks(self, monkeypatch):
monkeypatch.setenv("KORNIA_CHECKS", "0")
from kornia.core.check import _should_enable_checks
assert _should_enable_checks() is False
def test_env_var_enables_checks(self, monkeypatch):
from kornia.core.check import _should_enable_checks
monkeypatch.setenv("KORNIA_CHECKS", "1")
assert _should_enable_checks() is True
def test_env_var_true_string_variants(self, monkeypatch):
from kornia.core.check import _should_enable_checks
for val in ("true", "yes", "on", "1"):
monkeypatch.setenv("KORNIA_CHECKS", val)
assert _should_enable_checks() is True
def test_env_var_false_string(self, monkeypatch):
from kornia.core.check import _should_enable_checks
monkeypatch.setenv("KORNIA_CHECKS", "false")
assert _should_enable_checks() is False
class TestCheckSameSize:
@pytest.mark.parametrize(
"x_shape, y_shape, dim, allow_one",
[
((2, 3), (2, 5), 0, False),
((2, 3), (4, 3), 1, False),
((2, 3), (4, 3), -1, False),
((1, 3), (4, 3), 0, True),
],
)
def test_valid(self, x_shape, y_shape, dim, allow_one):
assert KORNIA_CHECK_SAME_SIZE(torch.rand(x_shape), torch.rand(y_shape), dim, allow_one) is True
def test_invalid_reports_both_sizes(self):
with pytest.raises(
ShapeError, match=r"dimension 0: expected 4, got 3\.\n.*\[3, 2\]\n.*\[4, 2\]\n a note"
) as e:
KORNIA_CHECK_SAME_SIZE(torch.rand(3, 2), torch.rand(4, 2), msg="a note")
assert e.value.actual_shape == [3, 2]
assert e.value.expected_shape == [4, 2]
def test_allow_one_is_for_the_first_tensor_only(self):
assert KORNIA_CHECK_SAME_SIZE(torch.rand(1, 2), torch.rand(4, 2), allow_one=True) is True
with pytest.raises(ShapeError, match="expected 1, got 4"):
KORNIA_CHECK_SAME_SIZE(torch.rand(4, 2), torch.rand(1, 2), allow_one=True)
with pytest.raises(ShapeError, match="expected 4 or 1, got 3"):
KORNIA_CHECK_SAME_SIZE(torch.rand(3, 2), torch.rand(4, 2), allow_one=True)
@pytest.mark.parametrize("dim", [2, -3])
def test_dimension_out_of_range(self, dim):
with pytest.raises(ShapeError, match=f"no dimension {dim}"):
KORNIA_CHECK_SAME_SIZE(torch.rand(3, 2), torch.rand(3, 2), dim)
def test_invalid_raises_false(self):
assert KORNIA_CHECK_SAME_SIZE(torch.rand(3, 2), torch.rand(4, 2), raises=False) is False
def test_jit(self):
op_jit = torch.jit.script(KORNIA_CHECK_SAME_SIZE)
assert op_jit(torch.rand(1, 2), torch.rand(4, 2), 0, True) is True
with pytest.raises(Exception, match="expected 4, got 3"):
op_jit(torch.rand(3, 2), torch.rand(4, 2))
def test_dynamo_relates_the_sizes_without_specializing(self, torch_optimizer, optimizer_backend):
from torch._dynamo.testing import CompileCounter
if optimizer_backend == "jit":
pytest.skip("counts the graphs of torch.compile, which torch.jit.script does not build")
def op(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
KORNIA_CHECK_SAME_SIZE(x, y, allow_one=True)
return x + y
counter = CompileCounter()
compiled = torch_optimizer(op, backend=counter, fullgraph=True, dynamic=True)
for batch in (2, 4, 6):
x, y = torch.rand(batch, batch + 3), torch.rand(batch, batch + 3)
assert torch.equal(compiled(x, y), x + y)
assert counter.frame_count == 1
@pytest.mark.skipif(not dynamo_is_available(), reason="torch.export is unavailable on this torch/python pair")
def test_size_oblivious_export_keeps_a_dynamic_size_against_a_size_of_one(self):
# The Dynamo ONNX exporter of torch 2.9.1 runs torch.export size-obliviously, where comparing a kernel batch of
# 1 with a dynamic batch first guarded the batch against 1, so a dynamic batch that admits 1 failed to export.
config = torch.fx.experimental._config
if not hasattr(config, "backed_size_oblivious"):
pytest.skip("torch.export has no size-oblivious mode in this torch release")
class Model(torch.nn.Module):
def forward(self, x: torch.Tensor) -> torch.Tensor:
KORNIA_CHECK_SAME_SIZE(torch.ones(1, 3, 3), x, allow_one=True)
KORNIA_CHECK_SAME_SIZE(x, x, allow_one=True)
return x * 2
with config.patch(backed_size_oblivious=True):
program = torch.export.export(
Model(), (torch.rand(2, 3, 8, 8),), dynamic_shapes=({0: torch.export.Dim("batch")},)
)
for batch in (1, 3):
x = torch.rand(batch, 3, 8, 8)
assert torch.equal(program.module()(x), x * 2)
class TestCheckSizes:
@pytest.mark.parametrize("shape, sizes", [((5,), [5]), ((2, 3), [None, 3]), ((2, 3, 4), [2, None, None]), ((), [])])
def test_valid(self, shape, sizes):
assert KORNIA_CHECK_SIZES(torch.rand(shape), sizes) is True
@pytest.mark.parametrize(
"shape, sizes, expected",
[
((4,), [5], r"\(5,\), got \[4\]"),
((5, 1), [5], r"\(5,\), got \[5, 1\]"),
((2, 3), [None, 4], r"\(\*, 4\), got \[2, 3\]"),
],
)
def test_invalid_reports_the_shapes(self, shape, sizes, expected):
with pytest.raises(ShapeError, match=f"Shape mismatch: expected {expected}") as e:
KORNIA_CHECK_SIZES(torch.rand(shape), sizes)
assert e.value.actual_shape == list(shape)
def test_invalid_raises_false(self):
assert KORNIA_CHECK_SIZES(torch.rand(4), [5], raises=False) is False
def test_jit(self):
op_jit = torch.jit.script(KORNIA_CHECK_SIZES)
assert op_jit(torch.rand(2, 3), [None, 3]) is True
with pytest.raises(Exception, match=r"expected \(\*, 4\)"):
op_jit(torch.rand(2, 3), [None, 4])
def test_dynamo_relates_the_sizes_without_specializing(self, torch_optimizer, optimizer_backend):
from torch._dynamo.testing import CompileCounter
if optimizer_backend == "jit":
pytest.skip("counts the graphs of torch.compile, which torch.jit.script does not build")
def op(pred: torch.Tensor, weight: torch.Tensor) -> torch.Tensor:
KORNIA_CHECK_SIZES(weight, [pred.shape[1]])
KORNIA_CHECK_SIZES(pred, [None, weight.shape[0], None])
return pred * weight[:, None]
counter = CompileCounter()
compiled = torch_optimizer(op, backend=counter, fullgraph=True, dynamic=True)
for classes in (3, 5, 7):
pred, weight = torch.rand(2, classes, classes + 4), torch.rand(classes)
assert torch.equal(compiled(pred, weight), pred * weight[:, None])
assert counter.frame_count == 1
class _Message:
"""A message function that counts its calls."""
def __init__(self) -> None:
self.calls = 0
def __call__(self) -> str:
self.calls += 1
return "deferred message text"
# helper -> (a call that passes, a call that fails), each taking (msg, raises); a helper with two failure paths has
# one case per path
_DEFERRED_CASES = {
"KORNIA_CHECK": (
lambda msg, raises: KORNIA_CHECK(True, msg, raises),
lambda msg, raises: KORNIA_CHECK(False, msg, raises),
),
"KORNIA_CHECK_SHAPE-ndim": (
lambda msg, raises: KORNIA_CHECK_SHAPE(torch.rand(2, 3), ["B", "N"], msg, raises),
lambda msg, raises: KORNIA_CHECK_SHAPE(torch.rand(2, 3), ["B", "N", "D"], msg, raises),
),
"KORNIA_CHECK_SHAPE-size": (
lambda msg, raises: KORNIA_CHECK_SHAPE(torch.rand(2, 3), ["B", "3"], msg, raises),
lambda msg, raises: KORNIA_CHECK_SHAPE(torch.rand(2, 3), ["B", "4"], msg, raises),
),
"KORNIA_CHECK_SAME_SIZE-size": (
lambda msg, raises: KORNIA_CHECK_SAME_SIZE(torch.rand(2, 3), torch.rand(2, 5), 0, False, msg, raises),
lambda msg, raises: KORNIA_CHECK_SAME_SIZE(torch.rand(3, 3), torch.rand(2, 5), 0, False, msg, raises),
),
"KORNIA_CHECK_SAME_SIZE-dim": (
lambda msg, raises: KORNIA_CHECK_SAME_SIZE(torch.rand(2, 3), torch.rand(2, 5), -2, False, msg, raises),
lambda msg, raises: KORNIA_CHECK_SAME_SIZE(torch.rand(2), torch.rand(2, 5), 1, False, msg, raises),
),
"KORNIA_CHECK_SIZES-ndim": (
lambda msg, raises: KORNIA_CHECK_SIZES(torch.rand(2, 3), [2, None], msg, raises),
lambda msg, raises: KORNIA_CHECK_SIZES(torch.rand(2, 3), [2], msg, raises),
),
"KORNIA_CHECK_SIZES-size": (
lambda msg, raises: KORNIA_CHECK_SIZES(torch.rand(2, 3), [None, 3], msg, raises),
lambda msg, raises: KORNIA_CHECK_SIZES(torch.rand(2, 3), [None, 4], msg, raises),
),
"KORNIA_CHECK_TYPE": (
lambda msg, raises: KORNIA_CHECK_TYPE(1, int, msg, raises),
lambda msg, raises: KORNIA_CHECK_TYPE("1", int, msg, raises),
),
"KORNIA_CHECK_IS_TENSOR": (
lambda msg, raises: KORNIA_CHECK_IS_TENSOR(torch.rand(2), msg, raises),
lambda msg, raises: KORNIA_CHECK_IS_TENSOR([1.0], msg, raises),
),
"KORNIA_CHECK_SAME_DEVICES": (
lambda msg, raises: KORNIA_CHECK_SAME_DEVICES([torch.rand(2)], msg, raises),
lambda msg, raises: KORNIA_CHECK_SAME_DEVICES([], msg, raises),
),
"KORNIA_CHECK_IS_COLOR": (
lambda msg, raises: KORNIA_CHECK_IS_COLOR(torch.rand(3, 4, 4), msg, raises),
lambda msg, raises: KORNIA_CHECK_IS_COLOR(torch.rand(1, 4, 4), msg, raises),
),
"KORNIA_CHECK_IS_GRAY": (
lambda msg, raises: KORNIA_CHECK_IS_GRAY(torch.rand(1, 4, 4), msg, raises),
lambda msg, raises: KORNIA_CHECK_IS_GRAY(torch.rand(3, 4, 4), msg, raises),
),
"KORNIA_CHECK_IS_COLOR_OR_GRAY": (
lambda msg, raises: KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(3, 4, 4), msg, raises),
lambda msg, raises: KORNIA_CHECK_IS_COLOR_OR_GRAY(torch.rand(2, 4, 4), msg, raises),
),
"KORNIA_CHECK_IS_IMAGE-shape": (
lambda msg, raises: KORNIA_CHECK_IS_IMAGE(torch.rand(3, 4, 4), msg, raises),
lambda msg, raises: KORNIA_CHECK_IS_IMAGE(torch.rand(2, 4, 4), msg, raises),
),
"KORNIA_CHECK_IS_IMAGE-range": (
lambda msg, raises: KORNIA_CHECK_IS_IMAGE(torch.rand(3, 4, 4), msg, raises),
lambda msg, raises: KORNIA_CHECK_IS_IMAGE(torch.full((3, 4, 4), 2.0), msg, raises),
),
}
class TestDeferredMessage:
"""Every helper that takes ``msg`` accepts a function for it and calls it only to raise."""
def teardown_method(self):
enable_checks()
@pytest.mark.parametrize("case", sorted(_DEFERRED_CASES))
def test_not_called_when_the_check_passes(self, case):
passes, _ = _DEFERRED_CASES[case]
msg = _Message()
assert passes(msg, True) is True
assert msg.calls == 0
@pytest.mark.parametrize("case", sorted(_DEFERRED_CASES))
def test_called_once_and_reported_when_the_check_fails(self, case):
_, fails = _DEFERRED_CASES[case]
msg = _Message()
with pytest.raises(BaseError, match="deferred message text"):
fails(msg, True)
assert msg.calls == 1
@pytest.mark.parametrize("case", sorted(_DEFERRED_CASES))
def test_not_called_when_the_check_does_not_raise(self, case):
_, fails = _DEFERRED_CASES[case]
msg = _Message()
assert fails(msg, False) is False
assert msg.calls == 0
@pytest.mark.parametrize("case", sorted(_DEFERRED_CASES))
def test_not_called_when_checks_are_disabled(self, case):
_, fails = _DEFERRED_CASES[case]
msg = _Message()
disable_checks()
assert fails(msg, True) is True
assert msg.calls == 0
def test_image_layout_takes_a_deferred_message(self):
from kornia.image.base import KORNIA_CHECK_IMAGE_LAYOUT, ChannelsOrder, ImageLayout, ImageSize
layout = ImageLayout(ImageSize(4, 5), 3, ChannelsOrder.CHANNELS_FIRST)
msg = _Message()
assert KORNIA_CHECK_IMAGE_LAYOUT(torch.rand(3, 4, 5), layout, msg) is True
assert msg.calls == 0
with pytest.raises(ShapeError, match="deferred message text"):
KORNIA_CHECK_IMAGE_LAYOUT(torch.rand(3, 4, 6), layout, msg)
assert msg.calls == 1
def test_jit_still_takes_a_string_message(self):
def op(x: torch.Tensor) -> torch.Tensor:
KORNIA_CHECK(x.shape[0] == 2, "a scripted message")
KORNIA_CHECK_SHAPE(x, ["2", "N"], "a scripted shape message")
return x * 2
scripted = torch.jit.script(op)
assert scripted(torch.ones(2, 3)).sum() == 12
with pytest.raises(Exception, match="a scripted message"):
scripted(torch.ones(3, 3))
@pytest.mark.parametrize("helper", ["KORNIA_CHECK", "KORNIA_CHECK_SHAPE"])
def test_dynamo_deferred_size_message_compiles_one_graph(self, helper, torch_optimizer, optimizer_backend):
"""Formatting a size inside the message function does not specialize it."""
from torch._dynamo.testing import CompileCounter
if optimizer_backend == "jit":
pytest.skip("counts the graphs of torch.compile, which torch.jit.script does not build")
def op(x: torch.Tensor) -> torch.Tensor:
if helper == "KORNIA_CHECK":
KORNIA_CHECK(x.shape[0] >= 1, lambda: f"Expected a batch, got {x.shape[0]} rows of {x.shape[1]}")
else:
KORNIA_CHECK_SHAPE(x, ["B", "N"], lambda: f"Expected (B, N), got {x.shape[0]} x {x.shape[1]}")
return x * 2
counter = CompileCounter()
compiled = torch_optimizer(op, backend=counter, fullgraph=True, dynamic=True)
for batch in (2, 4, 6):
x = torch.rand(batch, batch + 3)
assert torch.equal(compiled(x), x * 2)
assert counter.frame_count == 1
class TestCheckAsync(BaseTester):
"""``_check_async``: a tensor condition checked where it lives, without a host read on an accelerator."""
def setup_method(self):
enable_checks()
def teardown_method(self):
enable_checks()
@staticmethod
def _reads_on_the_host(device: torch.device) -> bool:
return device.type == "cpu" or (device.type == "mps" and not torch_version_ge(2, 13))
def test_smoke(self, device):
assert _check_async(torch.ones(3, device=device, dtype=torch.bool).all(), "never raised") is None
def test_invalid_on_the_host_raises_at_once(self, device):
if not self._reads_on_the_host(device):
pytest.skip("on CUDA, and on MPS from torch 2.13, the check is torch._assert_async")
with pytest.raises(AsyncCheckError, match="x must be positive"):
_check_async((torch.tensor([1.0, -1.0], device=device) > 0).all(), "x must be positive")
def test_invalid_on_mps_raises_at_the_next_sync(self, device):
if device.type != "mps" or not torch_version_ge(2, 13):
pytest.skip("pins the MPS assert kernel of torch 2.13")
_check_async((torch.tensor([1.0, -1.0], device=device) > 0).all(), "x must be positive")
with pytest.raises(RuntimeError, match="x must be positive"):
torch.mps.synchronize()
def test_invalid_on_cuda_is_a_device_assert(self, device):
# The assert leaves the CUDA context unusable, so it runs in a process of its own.
if device.type != "cuda":
pytest.skip("pins the CUDA device-side assert")
script = textwrap.dedent(
"""
import torch
from kornia.core.check import _check_async
_check_async((torch.tensor([1.0, -1.0], device="cuda") > 0).all(), "x must be positive")
print("returned")
try:
torch.cuda.synchronize()
except RuntimeError as error:
print(type(error).__name__)
"""
)
# Trusted, fixed command (the current interpreter running a literal script); no external input.
result = subprocess.run( # noqa: S603
[sys.executable, "-c", script], capture_output=True, text=True, check=False, timeout=300
)
# torch 2.5.1 has no AcceleratorError and raises a plain RuntimeError.
assert result.stdout.split() in (["returned", "AcceleratorError"], ["returned", "RuntimeError"]), (
result.stdout + result.stderr
)
assert "x must be positive" in result.stderr
def test_valid_on_cuda_does_not_synchronize(self, device):
if device.type != "cuda":
pytest.skip("CUDA host synchronizations need a CUDA device")
x = torch.rand(8, device=device) + 0.5
torch.cuda.synchronize()
previous = torch.cuda.get_sync_debug_mode()
torch.cuda.set_sync_debug_mode("error")
try:
_check_async((x > 0).all(), "x must be positive")
finally:
torch.cuda.set_sync_debug_mode(previous)
def test_meta_has_nothing_to_check(self):
_check_async(torch.zeros((), dtype=torch.bool, device="meta"), "never raised")
def test_skipped_when_checks_are_disabled(self):
disable_checks()
_check_async(torch.tensor(False), "never raised")
def test_skipped_under_vmap(self, device):
# vmap has no batching rule for torch._assert_async, so the check is skipped rather than failing the call.
def op(x: torch.Tensor) -> torch.Tensor:
_check_async((x > 0).all(), "x must be positive")
return x * 2
x = torch.rand(3, 4, device=device) + 0.5
self.assert_close(torch.vmap(op)(x), x * 2)
def test_guard_runs_when_checks_are_disabled(self):
# A guard keeps a later index in bounds, which a compiled CPU gather or scatter could not survive.
disable_checks()
with pytest.raises(AsyncCheckError, match="index out of range"):
_check_async(torch.tensor(False), "index out of range", guard=True)
def test_runs_under_the_other_functorch_transforms(self):
# Only vmap lacks a batching rule for the assert: grad, jvp and functionalize keep the check in eager code.
def op(x: torch.Tensor) -> torch.Tensor:
_check_async((x > 0).all(), "x must be positive")
return (x * 2).sum()
invalid = -torch.ones(3)
with pytest.raises(AsyncCheckError, match="x must be positive"):
torch.func.grad(op)(invalid)
with pytest.raises(AsyncCheckError, match="x must be positive"):
torch.func.jvp(op, (invalid,), (torch.ones(3),))
with pytest.raises(AsyncCheckError, match="x must be positive"):
torch.func.functionalize(op)(invalid)
def test_dynamo_under_functorch_transforms(self, device, torch_optimizer, optimizer_backend):
# Dynamo cannot trace the walk of the transform stack, so a compiled vmap or grad skips the check instead of
# breaking the graph.
if optimizer_backend == "jit":
pytest.skip("TorchScript does not script functorch transforms")
def op(x: torch.Tensor) -> torch.Tensor:
_check_async((x > 0).all(), "x must be positive")
return (x * 2).sum()
x = torch.rand(2, 3, device=device) + 0.5
batched = torch_optimizer(torch.vmap(op), fullgraph=True)
self.assert_close(batched(x), (x * 2).sum(1))
gradient = torch_optimizer(torch.func.grad(op), fullgraph=True)
self.assert_close(gradient(x[0]), torch.full_like(x[0], 2.0))
def test_jit_raises_on_the_host(self, device):
# TorchScript cannot read whether MPS has the assert kernel (from torch 2.13), so a scripted call reads a
# condition on any device but CUDA on the host, and raises during the call.
if device.type == "cuda":
pytest.skip("TorchScript drops torch._assert_async, so a scripted CUDA call is not checked")
def op(x: torch.Tensor) -> torch.Tensor:
_check_async((x > 0).all(), "x must be positive")
return x * 2
scripted = torch.jit.script(op)
assert torch.equal(scripted(torch.ones(2, device=device)), torch.full((2,), 2.0, device=device))
with pytest.raises(torch.jit.Error, match="AsyncCheckError: x must be positive"):
scripted(-torch.ones(2, device=device))
def test_dynamo(self, device, torch_optimizer, optimizer_backend):
if optimizer_backend == "jit":
pytest.skip("covered by test_jit_raises_on_the_host")
def op(x: torch.Tensor) -> torch.Tensor:
_check_async((x > 0).all(), "x must be positive")
return x * 2
compiled = torch_optimizer(op, fullgraph=True)
x = torch.rand(2, 3, device=device) + 0.5
self.assert_close(compiled(x), x * 2)
if device.type != "cpu":
pytest.skip("an invalid value would fail on the device; the CPU kernel raises in the process")
if not torch_version_ge(2, 9):
pytest.skip("Inductor compiles torch._assert_async to nothing before torch 2.9")
with pytest.raises(RuntimeError, match="x must be positive"):
compiled(-x)
def test_dynamo_guard_under_functorch_transforms(self, device, torch_optimizer, optimizer_backend):
# A guard keeps a later index in bounds, so a compiled grad keeps it, unlike the checks without one.
if optimizer_backend == "jit":
pytest.skip("TorchScript does not script functorch transforms")
def op(x: torch.Tensor) -> torch.Tensor:
_check_async((x > 0).all(), "x must be positive", guard=True)
return (x * 2).sum()
x = torch.full((3,), 0.5, device=device)
gradient = torch_optimizer(torch.func.grad(op), fullgraph=True)
self.assert_close(gradient(x), torch.full_like(x, 2.0))
if device.type != "cpu":
pytest.skip("an invalid value would fail on the device; the CPU kernel raises in the process")
if not torch_version_ge(2, 9):
pytest.skip("Inductor compiles torch._assert_async to nothing before torch 2.9")
with pytest.raises(RuntimeError, match="x must be positive"):
gradient(-x)