Skip to content

Commit

Permalink
[Lang] [test] Add ti.get_rel_eps() for maximal relative error toleran…
Browse files Browse the repository at this point in the history
…ce on current backend (#1798)

* [test] Add default_epsilon_of_arch to make tests pass on Metal

* rename
  • Loading branch information
k-ye authored Aug 28, 2020
1 parent e0994ee commit 6045d82
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
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

0 comments on commit 6045d82

Please sign in to comment.