Skip to content

Commit 4bd5c02

Browse files
test: Avoid tensorflow macOS floating point deviation with pytest.approx (#1761)
* Use pytest.approx in test_simple_tensor_ops to avoid floating point difference with tensorflow on macOS (compared to Linux). * Add a pytest.mark.xfail test for the failing operation to know if future tensorflow releases cause this to floating point difference to stop happening.
1 parent 270d59d commit 4bd5c02

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

tests/test_tensor.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from sys import platform
12
import pytest
23
import logging
34
import numpy as np
@@ -51,7 +52,10 @@ def test_simple_tensor_ops(backend):
5152
4,
5253
]
5354
assert tb.tolist(tb.sqrt(tb.astensor([4, 9, 16]))) == [2, 3, 4]
54-
assert tb.tolist(tb.log(tb.exp(tb.astensor([2, 3, 4])))) == [2, 3, 4]
55+
# c.f. Issue #1759
56+
assert tb.tolist(tb.log(tb.exp(tb.astensor([2, 3, 4])))) == pytest.approx(
57+
[2, 3, 4], 1e-9
58+
)
5559
assert tb.tolist(tb.abs(tb.astensor([-1, -2]))) == [1, 2]
5660
assert tb.tolist(tb.erf(tb.astensor([-2.0, -1.0, 0.0, 1.0, 2.0]))) == pytest.approx(
5761
[
@@ -86,6 +90,17 @@ def test_simple_tensor_ops(backend):
8690
]
8791

8892

93+
@pytest.mark.xfail(platform == "darwin", reason="c.f. Issue #1759")
94+
@pytest.mark.only_tensorflow
95+
def test_simple_tensor_ops_floating_point(backend):
96+
"""
97+
xfail test to know if test_simple_tensor_ops stops failing for tensorflow
98+
on macos
99+
"""
100+
tb = pyhf.tensorlib
101+
assert tb.tolist(tb.log(tb.exp(tb.astensor([2, 3, 4])))) == [2, 3, 4]
102+
103+
89104
def test_tensor_where_scalar(backend):
90105
tb = pyhf.tensorlib
91106
assert tb.tolist(tb.where(tb.astensor([1, 0, 1], dtype="bool"), 1, 2)) == [1, 2, 1]

0 commit comments

Comments
 (0)