-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This utility function is based on symmetric check, unlike numpy.allclose, and verifies that abs(x1-x2) < atol + rtol * max(abs(x1), abs(x2)) This way allclose(x1, x2) is symmetric, and allclose(x1,x2) implies allclose(x2, x1).
- Loading branch information
1 parent
4a2578f
commit 26862b4
Showing
3 changed files
with
232 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
# Data Parallel Control (dpctl) | ||
# | ||
# Copyright 2020-2023 Intel Corporation | ||
# | ||
# 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 numpy as np | ||
|
||
import dpctl.tensor as dpt | ||
import dpctl.utils as du | ||
|
||
from ._manipulation_functions import _broadcast_shape_impl | ||
from ._type_utils import _to_device_supported_dtype | ||
|
||
|
||
def _allclose_complex_fp(z1, z2, atol, rtol, equal_nan): | ||
z1r = dpt.real(z1) | ||
z1i = dpt.imag(z1) | ||
z2r = dpt.real(z2) | ||
z2i = dpt.imag(z2) | ||
if equal_nan: | ||
check1 = dpt.all(dpt.isnan(z1r) == dpt.isnan(z2r)) and dpt.all( | ||
dpt.isnan(z1i) == dpt.isnan(z2i) | ||
) | ||
else: | ||
check1 = ( | ||
dpt.logical_not(dpt.any(dpt.isnan(z1r))) | ||
and dpt.logical_not(dpt.any(dpt.isnan(z1i))) | ||
) and ( | ||
dpt.logical_not(dpt.any(dpt.isnan(z2r))) | ||
and dpt.logical_not(dpt.any(dpt.isnan(z2i))) | ||
) | ||
if not check1: | ||
return check1 | ||
mr = dpt.isinf(z1r) | ||
mi = dpt.isinf(z1i) | ||
check2 = dpt.all(mr == dpt.isinf(z2r)) and dpt.all(mi == dpt.isinf(z2i)) | ||
if not check2: | ||
return check2 | ||
check3 = dpt.all(z1r[mr] == z2r[mr]) and dpt.all(z1i[mi] == z2i[mi]) | ||
if not check3: | ||
return check3 | ||
mr = dpt.isfinite(z1r) | ||
mi = dpt.isfinite(z1i) | ||
mv1 = z1r[mr] | ||
mv2 = z2r[mr] | ||
check4 = dpt.all( | ||
dpt.abs(mv1 - mv2) | ||
< atol + rtol * dpt.maximum(dpt.abs(mv1), dpt.abs(mv2)) | ||
) | ||
if not check4: | ||
return check4 | ||
mv1 = z1i[mi] | ||
mv2 = z2i[mi] | ||
check5 = dpt.all( | ||
dpt.abs(mv1 - mv2) | ||
< atol + rtol * dpt.maximum(dpt.abs(mv1), dpt.abs(mv2)) | ||
) | ||
return check5 | ||
|
||
|
||
def _allclose_real_fp(r1, r2, atol, rtol, equal_nan): | ||
if equal_nan: | ||
check1 = dpt.all(dpt.isnan(r1) == dpt.isnan(r2)) | ||
else: | ||
check1 = dpt.logical_not(dpt.any(dpt.isnan(r1))) and dpt.logical_not( | ||
dpt.any(dpt.isnan(r2)) | ||
) | ||
if not check1: | ||
return check1 | ||
mr = dpt.isinf(r1) | ||
check2 = dpt.all(mr == dpt.isinf(r2)) | ||
if not check2: | ||
return check2 | ||
check3 = dpt.all(r1[mr] == r2[mr]) | ||
if not check3: | ||
return check3 | ||
m = dpt.isfinite(r1) | ||
mv1 = r1[m] | ||
mv2 = r2[m] | ||
check4 = dpt.all( | ||
dpt.abs(mv1 - mv2) | ||
< atol + rtol * dpt.maximum(dpt.abs(mv1), dpt.abs(mv2)) | ||
) | ||
return check4 | ||
|
||
|
||
def _allclose_others(r1, r2): | ||
return dpt.all(r1 == r2) | ||
|
||
|
||
def allclose(a1, a2, atol=1e-5, rtol=1e-8, equal_nan=False): | ||
"""allclose(a1, a2, atol=1e-5, rtol=1e-8) | ||
Returns True if two arrays are element-wise equal within tolerance. | ||
""" | ||
if not isinstance(a1, dpt.usm_ndarray): | ||
raise TypeError( | ||
f"Expected dpctl.tensor.usm_ndarray type, got {type(a1)}." | ||
) | ||
if not isinstance(a2, dpt.usm_ndarray): | ||
raise TypeError( | ||
f"Expected dpctl.tensor.usm_ndarray type, got {type(a2)}." | ||
) | ||
atol = float(atol) | ||
rtol = float(rtol) | ||
equal_nan = bool(equal_nan) | ||
exec_q = du.get_execution_queue(tuple(a.sycl_queue for a in (a1, a2))) | ||
if exec_q is None: | ||
raise du.ExecutionPlacementError( | ||
"Execution placement can not be unambiguously inferred " | ||
"from input arguments." | ||
) | ||
res_sh = _broadcast_shape_impl([a1.shape, a2.shape]) | ||
b1 = a1 | ||
b2 = a2 | ||
if b1.dtype == b2.dtype: | ||
res_dt = b1.dtype | ||
else: | ||
res_dt = np.promote_types(b1.dtype, b2.dtype) | ||
res_dt = _to_device_supported_dtype(res_dt, exec_q.sycl_device) | ||
b1 = dpt.astype(b1, res_dt) | ||
b2 = dpt.astype(b2, res_dt) | ||
|
||
b1 = dpt.broadcast_to(b1, res_sh) | ||
b2 = dpt.broadcast_to(b2, res_sh) | ||
|
||
k = b1.dtype.kind | ||
if k == "c": | ||
return _allclose_complex_fp(b1, b2, atol, rtol, equal_nan) | ||
elif k == "f": | ||
return _allclose_real_fp(b1, b2, atol, rtol, equal_nan) | ||
else: | ||
return _allclose_others(b1, b2) |
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,86 @@ | ||
import itertools | ||
|
||
import pytest | ||
|
||
import dpctl.tensor as dpt | ||
from dpctl.tests.helper import get_queue_or_skip, skip_if_dtype_not_supported | ||
|
||
_all_dtypes = [ | ||
"?", | ||
"i1", | ||
"u1", | ||
"i2", | ||
"u2", | ||
"i4", | ||
"u4", | ||
"i8", | ||
"u8", | ||
"f2", | ||
"f4", | ||
"f8", | ||
"c8", | ||
"c16", | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("dtype", _all_dtypes) | ||
def test_allclose(dtype): | ||
q = get_queue_or_skip() | ||
skip_if_dtype_not_supported(dtype, q) | ||
|
||
a1 = dpt.ones(10, dtype=dtype) | ||
a2 = dpt.ones(10, dtype=dtype) | ||
|
||
assert dpt.allclose(a1, a2) | ||
|
||
|
||
@pytest.mark.parametrize("dtype", ["f2", "f4", "f8"]) | ||
def test_allclose_real_fp(dtype): | ||
q = get_queue_or_skip() | ||
skip_if_dtype_not_supported(dtype, q) | ||
|
||
v = [dpt.nan, -dpt.nan, dpt.inf, -dpt.inf, -0.0, 0.0, 1.0, -1.0] | ||
a1 = dpt.asarray(v[2:], dtype=dtype) | ||
a2 = dpt.asarray(v[2:], dtype=dtype) | ||
|
||
tol = dpt.finfo(a1.dtype).resolution | ||
assert dpt.allclose(a1, a2, atol=tol, rtol=tol) | ||
|
||
a1 = dpt.asarray(v, dtype=dtype) | ||
a2 = dpt.asarray(v, dtype=dtype) | ||
|
||
assert not dpt.allclose(a1, a2, atol=tol, rtol=tol) | ||
assert dpt.allclose(a1, a2, atol=tol, rtol=tol, equal_nan=True) | ||
|
||
|
||
@pytest.mark.parametrize("dtype", ["c8", "c16"]) | ||
def test_allclose_complex_fp(dtype): | ||
q = get_queue_or_skip() | ||
skip_if_dtype_not_supported(dtype, q) | ||
|
||
v = [dpt.nan, -dpt.nan, dpt.inf, -dpt.inf, -0.0, 0.0, 1.0, -1.0] | ||
|
||
not_nans = [complex(*xy) for xy in itertools.product(v[2:], repeat=2)] | ||
z1 = dpt.asarray(not_nans, dtype=dtype) | ||
z2 = dpt.asarray(not_nans, dtype=dtype) | ||
|
||
tol = dpt.finfo(z1.dtype).resolution | ||
assert dpt.allclose(z1, z2, atol=tol, rtol=tol) | ||
|
||
both = [complex(*xy) for xy in itertools.product(v, repeat=2)] | ||
z1 = dpt.asarray(both, dtype=dtype) | ||
z2 = dpt.asarray(both, dtype=dtype) | ||
|
||
tol = dpt.finfo(z1.dtype).resolution | ||
assert not dpt.allclose(z1, z2, atol=tol, rtol=tol) | ||
assert dpt.allclose(z1, z2, atol=tol, rtol=tol, equal_nan=True) | ||
|
||
|
||
def test_allclose_validation(): | ||
with pytest.raises(TypeError): | ||
dpt.allclose(True, False) | ||
|
||
get_queue_or_skip() | ||
x = dpt.asarray(True) | ||
with pytest.raises(TypeError): | ||
dpt.allclose(x, False) |