Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lang] [test] Add ti.get_rel_eps() for maximal relative error tolerance on current backend #1798

Merged
merged 2 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions python/taichi/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@


# Helper functions
def get_rel_eps():
arch = ti.cfg.arch
if arch == ti.opengl:
return 1e-3
elif arch == ti.metal:
# Debatable, different hardware could yield different precisions
# On AMD Radeon Pro 5500M, 1e-6 works fine...
# https://github.com/taichi-dev/taichi/pull/1779
return 1e-4
return 1e-6


def approx(expected, **kwargs):
'''Tweaked pytest.approx for OpenGL low percisions'''
import pytest
Expand All @@ -19,11 +31,7 @@ def __ne__(self, other):
if isinstance(expected, bool):
return boolean_integer(expected)

if ti.cfg.arch == ti.opengl:
kwargs['rel'] = max(kwargs.get('rel', 1e-6), 1e-3)

if ti.cfg.arch == ti.metal:
kwargs['rel'] = max(kwargs.get('rel', 1e-6), 1e-4)
kwargs['rel'] = max(kwargs.get('rel', 1e-6), get_rel_eps())

return pytest.approx(expected, **kwargs)

Expand Down Expand Up @@ -109,6 +117,7 @@ def wrapped(*args, **kwargs):


__all__ = [
'get_rel_eps',
'approx',
'allclose',
'make_temp_file',
Expand Down
6 changes: 3 additions & 3 deletions tests/python/test_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_require_extensions_2():
@pytest.mark.parametrize('allclose',
[ti.allclose, lambda x, y: x == ti.approx(y)])
def test_allclose_rel(x, allclose):
rel = 1e-3 if ti.cfg.arch == ti.opengl else 1e-6
rel = ti.get_rel_eps()
assert not allclose(x + x * rel * 3.0, x)
assert not allclose(x + x * rel * 1.2, x)
assert allclose(x + x * rel * 0.9, x)
Expand All @@ -92,7 +92,7 @@ def test_allclose_rel(x, allclose):
@pytest.mark.parametrize('allclose',
[ti.allclose, lambda x, y: x == ti.approx(y)])
def test_allclose_rel_reordered1(x, allclose):
rel = 1e-3 if ti.cfg.arch == ti.opengl else 1e-6
rel = ti.get_rel_eps()
assert not allclose(x + x * rel * 3.0, x)
assert not allclose(x + x * rel * 1.2, x)
assert allclose(x + x * rel * 0.9, x)
Expand All @@ -109,7 +109,7 @@ def test_allclose_rel_reordered1(x, allclose):
[ti.allclose, lambda x, y: x == ti.approx(y)])
@ti.test()
def test_allclose_rel_reordered2(x, allclose):
rel = 1e-3 if ti.cfg.arch == ti.opengl else 1e-6
rel = ti.get_rel_eps()
assert not allclose(x + x * rel * 3.0, x)
assert not allclose(x + x * rel * 1.2, x)
assert allclose(x + x * rel * 0.9, x)
Expand Down