-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
224 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import candle | ||
from candle import Tensor | ||
|
||
|
||
def _assert_tensor_metadata( | ||
actual: Tensor, | ||
expected: Tensor, | ||
check_device: bool = True, | ||
check_dtype: bool = True, | ||
check_layout: bool = True, | ||
check_stride: bool = False, | ||
): | ||
if check_device: | ||
assert actual.device == expected.device, f"Device mismatch: {actual.device} != {expected.device}" | ||
|
||
if check_dtype: | ||
assert str(actual.dtype) == str(expected.dtype), f"Dtype mismatch: {actual.dtype} != {expected.dtype}" | ||
|
||
if check_layout: | ||
assert actual.shape == expected.shape, f"Shape mismatch: {actual.shape} != {expected.shape}" | ||
|
||
if check_stride: | ||
assert actual.stride == expected.stride, f"Stride mismatch: {actual.stride} != {expected.stride}" | ||
|
||
|
||
def assert_equal( | ||
actual: Tensor, | ||
expected: Tensor, | ||
check_device: bool = True, | ||
check_dtype: bool = True, | ||
check_layout: bool = True, | ||
check_stride: bool = False, | ||
): | ||
""" | ||
Asserts that two tensors are exact equals. | ||
""" | ||
_assert_tensor_metadata(actual, expected, check_device, check_dtype, check_layout, check_stride) | ||
assert (actual - expected).abs().sum_all().values() == 0, f"Tensors mismatch: {actual} != {expected}" | ||
|
||
|
||
def assert_almost_equal( | ||
actual: Tensor, | ||
expected: Tensor, | ||
rtol=1e-05, | ||
atol=1e-08, | ||
check_device: bool = True, | ||
check_dtype: bool = True, | ||
check_layout: bool = True, | ||
check_stride: bool = False, | ||
): | ||
""" | ||
Asserts, that two tensors are almost equal by performing an element wise comparison of the tensors with a tolerance. | ||
Computes: |actual - expected| ≤ atol + rtol x |expected| | ||
""" | ||
_assert_tensor_metadata(actual, expected, check_device, check_dtype, check_layout, check_stride) | ||
|
||
# Secure against overflow of u32 and u8 tensors | ||
diff = ( | ||
(actual - expected).abs() | ||
if actual.sum_all().values() > expected.sum_all().values() | ||
else (expected - actual).abs() | ||
) | ||
threshold = (expected.abs().to_dtype(candle.f32) * rtol + atol).to(expected) | ||
|
||
assert (diff <= threshold).sum_all().values() == actual.nelements, f"Difference between tensors was to great" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import candle | ||
from candle import Tensor | ||
from candle.testing import assert_equal, assert_almost_equal | ||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize("dtype", [candle.f32, candle.f64, candle.f16, candle.u32, candle.u8]) | ||
def test_assert_equal_asserts_correctly(dtype: candle.DType): | ||
a = Tensor([1, 2, 3]).to(dtype) | ||
b = Tensor([1, 2, 3]).to(dtype) | ||
assert_equal(a, b) | ||
|
||
with pytest.raises(AssertionError): | ||
assert_equal(a, b + 1) | ||
|
||
|
||
@pytest.mark.parametrize("dtype", [candle.f32, candle.f64, candle.f16, candle.u32, candle.u8]) | ||
def test_assert_almost_equal_asserts_correctly(dtype: candle.DType): | ||
a = Tensor([1, 2, 3]).to(dtype) | ||
b = Tensor([1, 2, 3]).to(dtype) | ||
assert_almost_equal(a, b) | ||
|
||
with pytest.raises(AssertionError): | ||
assert_almost_equal(a, b + 1) | ||
|
||
assert_almost_equal(a, b + 1, atol=20) | ||
assert_almost_equal(a, b + 1, rtol=20) | ||
|
||
with pytest.raises(AssertionError): | ||
assert_almost_equal(a, b + 1, atol=0.9) | ||
|
||
with pytest.raises(AssertionError): | ||
assert_almost_equal(a, b + 1, rtol=0.1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters