Skip to content

Commit 02ac57a

Browse files
add tolerance to specific tests
1 parent 2b6ecab commit 02ac57a

File tree

1 file changed

+48
-19
lines changed

1 file changed

+48
-19
lines changed

keras/src/ops/nn_test.py

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,14 +1324,17 @@ def test_polar(self):
13241324

13251325

13261326
class NNOpsCorrectnessTest(testing.TestCase):
1327-
def assertAllClose(self, x1, x2, atol=1e-6, rtol=1e-6, msg=None):
1327+
def setUp(self):
1328+
super().setUp()
13281329
if backend.backend() == "openvino":
1329-
# OpenVINO seems to use lower precision for some operations,
1330-
# or employs some different algorithms that wind up with
1331-
# slightly different results. To address this, we relax
1332-
# the tolerances for OpenVINO backend.
1333-
atol = 1e-3
1334-
super().assertAllClose(x1, x2, atol=atol, rtol=rtol, msg=msg)
1330+
import openvino as ov
1331+
import openvino.properties.hint as hints
1332+
1333+
core = ov.Core()
1334+
core.set_property(
1335+
"CPU",
1336+
{hints.execution_mode: hints.ExecutionMode.PERFORMANCE},
1337+
)
13351338

13361339
def test_relu(self):
13371340
x = np.array([-1, 0, 1, 2, 3], dtype=np.float32)
@@ -1360,7 +1363,13 @@ def test_softplus(self):
13601363

13611364
def test_softsign(self):
13621365
x = np.array([-1, 0, 1, 2, 3], dtype=np.float32)
1363-
self.assertAllClose(knn.softsign(x), [-0.5, 0, 0.5, 0.6666667, 0.75])
1366+
if backend.backend() == "openvino":
1367+
kwargs = {"atol": 1e-3}
1368+
else:
1369+
kwargs = {}
1370+
self.assertAllClose(
1371+
knn.softsign(x), [-0.5, 0, 0.5, 0.6666667, 0.75], **kwargs
1372+
)
13641373

13651374
def test_silu(self):
13661375
x = np.array([-1, 0, 1, 2, 3], dtype=np.float32)
@@ -1378,41 +1387,57 @@ def test_log_sigmoid(self):
13781387

13791388
def test_leaky_relu(self):
13801389
x = np.array([-1, 0, 1, 2, 3], dtype=np.float32)
1381-
self.assertAllClose(
1382-
knn.leaky_relu(x),
1383-
[-0.2, 0, 1, 2, 3],
1384-
)
1390+
if backend.backend() == "openvino":
1391+
kwargs = {"atol": 1e-3}
1392+
else:
1393+
kwargs = {}
1394+
self.assertAllClose(knn.leaky_relu(x), [-0.2, 0, 1, 2, 3], **kwargs)
13851395

13861396
def test_hard_sigmoid(self):
13871397
x = np.array([-1, 0, 1, 2, 3], dtype=np.float32)
1398+
if backend.backend() == "openvino":
1399+
kwargs = {"atol": 1e-3}
1400+
else:
1401+
kwargs = {}
13881402
self.assertAllClose(
13891403
knn.hard_sigmoid(x),
13901404
[0.33333334, 0.5, 0.6666667, 0.8333334, 1.0],
1405+
**kwargs,
13911406
)
13921407

13931408
def test_hard_silu(self):
13941409
x = np.array([-3, -2, -1, 0, 1, 2, 3], dtype=np.float32)
1410+
if backend.backend() == "openvino":
1411+
kwargs = {"atol": 1e-3}
1412+
else:
1413+
kwargs = {}
13951414
self.assertAllClose(
13961415
knn.hard_silu(x),
13971416
[-0.0, -0.333333, -0.333333, 0.0, 0.6666667, 1.6666667, 3.0],
1417+
**kwargs,
13981418
)
13991419

14001420
def test_elu(self):
14011421
x = np.array([-1, 0, 1, 2, 3], dtype=np.float32)
1422+
if backend.backend() == "openvino":
1423+
kwargs = {"atol": 1e-3}
1424+
else:
1425+
kwargs = {}
1426+
self.assertAllClose(knn.elu(x), [-0.63212055, 0, 1, 2, 3], **kwargs)
14021427
self.assertAllClose(
1403-
knn.elu(x),
1404-
[-0.63212055, 0, 1, 2, 3],
1405-
)
1406-
self.assertAllClose(
1407-
knn.elu(x, alpha=0.5),
1408-
[-0.31606027, 0, 1, 2, 3],
1428+
knn.elu(x, alpha=0.5), [-0.31606027, 0, 1, 2, 3], **kwargs
14091429
)
14101430

14111431
def test_selu(self):
14121432
x = np.array([-1, 0, 1, 2, 3], dtype=np.float32)
1433+
if backend.backend() == "openvino":
1434+
kwargs = {"atol": 1e-3}
1435+
else:
1436+
kwargs = {}
14131437
self.assertAllClose(
14141438
knn.selu(x),
14151439
[-1.1113307, 0.0, 1.050701, 2.101402, 3.152103],
1440+
**kwargs,
14161441
)
14171442

14181443
def test_gelu(self):
@@ -2288,7 +2313,11 @@ def test_ctc_loss(self):
22882313
output_length = np.array([3, 2])
22892314

22902315
result = knn.ctc_loss(labels, outputs, label_length, output_length)
2291-
self.assertAllClose(result, np.array([3.4411672, 1.91680186]))
2316+
if backend.backend() == "openvino":
2317+
kwargs = {"atol": 1e-3}
2318+
else:
2319+
kwargs = {}
2320+
self.assertAllClose(result, np.array([3.4411672, 1.91680186]), **kwargs)
22922321

22932322
def test_ctc_decode(self):
22942323
inputs = np.array(

0 commit comments

Comments
 (0)