From 4fbfd36ec58758440d466d3ec121f93f37ef03f9 Mon Sep 17 00:00:00 2001 From: Hridaya Sharma Date: Sat, 5 Apr 2025 13:01:55 +0530 Subject: [PATCH 1/4] Added decomposition for numpy.isclose --- .../openvino/excluded_concrete_tests.txt | 3 +-- keras/src/backend/openvino/numpy.py | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/keras/src/backend/openvino/excluded_concrete_tests.txt b/keras/src/backend/openvino/excluded_concrete_tests.txt index 3000452394c5..7f88956caa09 100644 --- a/keras/src/backend/openvino/excluded_concrete_tests.txt +++ b/keras/src/backend/openvino/excluded_concrete_tests.txt @@ -28,7 +28,6 @@ NumpyDtypeTest::test_floor NumpyDtypeTest::test_hstack NumpyDtypeTest::test_identity NumpyDtypeTest::test_inner -NumpyDtypeTest::test_isclose NumpyDtypeTest::test_isfinite NumpyDtypeTest::test_isinf NumpyDtypeTest::test_isnan @@ -165,4 +164,4 @@ NumpyTwoInputOpsCorrectnessTest::test_quantile NumpyTwoInputOpsCorrectnessTest::test_take_along_axis NumpyTwoInputOpsCorrectnessTest::test_tensordot NumpyTwoInputOpsCorrectnessTest::test_vdot -NumpyTwoInputOpsCorrectnessTest::test_where \ No newline at end of file +NumpyTwoInputOpsCorrectnessTest::test_where diff --git a/keras/src/backend/openvino/numpy.py b/keras/src/backend/openvino/numpy.py index bc0276d4373f..16a2e6c8a505 100644 --- a/keras/src/backend/openvino/numpy.py +++ b/keras/src/backend/openvino/numpy.py @@ -778,9 +778,21 @@ def imag(x): def isclose(x1, x2, rtol=1e-5, atol=1e-8, equal_nan=False): - raise NotImplementedError( - "`isclose` is not supported with openvino backend" - ) + + abs_diff = ov_opset.abs(x1 - x2) + abs_x2 = ov_opset.abs(x2) + rtol_tensor = ov_opset.constant(rtol, dtype=x1.dtype) + atol_tensor = ov_opset.constant(atol, dtype=x2.dtype) + total_tolerance = atol_tensor + rtol_tensor * abs_x2 + is_close = ov_opset.less_equal(abs_diff, total_tolerance) + + if equal_nan: + nan_a = ov_opset.isnan(x1) + nan_b = ov_opset.isnan(x2) + both_nan = ov_opset.logical_and(nan_a, nan_b) + is_close = ov_opset.logical_or(is_close, both_nan) + + return is_close def isfinite(x): From 1b89acbc5960beab2623e1dd18b1ffa496e3c06c Mon Sep 17 00:00:00 2001 From: Hridaya Sharma Date: Sat, 5 Apr 2025 13:13:04 +0530 Subject: [PATCH 2/4] Removed test from excluded list --- keras/src/backend/openvino/excluded_concrete_tests.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/keras/src/backend/openvino/excluded_concrete_tests.txt b/keras/src/backend/openvino/excluded_concrete_tests.txt index 7f88956caa09..b3276f209bb9 100644 --- a/keras/src/backend/openvino/excluded_concrete_tests.txt +++ b/keras/src/backend/openvino/excluded_concrete_tests.txt @@ -156,7 +156,6 @@ NumpyTwoInputOpsCorrectnessTest::test_digitize NumpyTwoInputOpsCorrectnessTest::test_divide_no_nan NumpyTwoInputOpsCorrectnessTest::test_einsum NumpyTwoInputOpsCorrectnessTest::test_inner -NumpyTwoInputOpsCorrectnessTest::test_isclose NumpyTwoInputOpsCorrectnessTest::test_linspace NumpyTwoInputOpsCorrectnessTest::test_logspace NumpyTwoInputOpsCorrectnessTest::test_outer From 5e4ccf899aef6f754b8e846aa0afd0ee8dca3010 Mon Sep 17 00:00:00 2001 From: Hridaya Sharma Date: Sat, 5 Apr 2025 13:54:20 +0530 Subject: [PATCH 3/4] Fixed failed test cases --- keras/src/backend/openvino/numpy.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/keras/src/backend/openvino/numpy.py b/keras/src/backend/openvino/numpy.py index 16a2e6c8a505..373d0d95a68e 100644 --- a/keras/src/backend/openvino/numpy.py +++ b/keras/src/backend/openvino/numpy.py @@ -778,21 +778,18 @@ def imag(x): def isclose(x1, x2, rtol=1e-5, atol=1e-8, equal_nan=False): - abs_diff = ov_opset.abs(x1 - x2) abs_x2 = ov_opset.abs(x2) - rtol_tensor = ov_opset.constant(rtol, dtype=x1.dtype) - atol_tensor = ov_opset.constant(atol, dtype=x2.dtype) + rtol_tensor = ov_opset.constant(np.array(rtol, dtype=np.float32)) + atol_tensor = ov_opset.constant(np.array(atol, dtype=np.float32)) total_tolerance = atol_tensor + rtol_tensor * abs_x2 is_close = ov_opset.less_equal(abs_diff, total_tolerance) - if equal_nan: nan_a = ov_opset.isnan(x1) nan_b = ov_opset.isnan(x2) both_nan = ov_opset.logical_and(nan_a, nan_b) is_close = ov_opset.logical_or(is_close, both_nan) - - return is_close + return OpenVINOKerasTensor(is_close.output(0)) def isfinite(x): From e3f6579c1ef04f79a7881706495a8a6f1701e32e Mon Sep 17 00:00:00 2001 From: Hridaya Sharma Date: Sat, 5 Apr 2025 15:20:56 +0530 Subject: [PATCH 4/4] Fixed dtype error --- keras/src/backend/openvino/numpy.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/keras/src/backend/openvino/numpy.py b/keras/src/backend/openvino/numpy.py index 373d0d95a68e..709a88cfc495 100644 --- a/keras/src/backend/openvino/numpy.py +++ b/keras/src/backend/openvino/numpy.py @@ -778,17 +778,21 @@ def imag(x): def isclose(x1, x2, rtol=1e-5, atol=1e-8, equal_nan=False): + dtype = OPENVINO_DTYPES[config.floatx()] + + x1 = ov_opset.convert(get_ov_output(x1), dtype) + x2 = ov_opset.convert(get_ov_output(x2), dtype) + rtol = ov_opset.convert(get_ov_output(rtol), dtype) + atol = ov_opset.convert(get_ov_output(atol), dtype) + abs_diff = ov_opset.abs(x1 - x2) abs_x2 = ov_opset.abs(x2) - rtol_tensor = ov_opset.constant(np.array(rtol, dtype=np.float32)) - atol_tensor = ov_opset.constant(np.array(atol, dtype=np.float32)) - total_tolerance = atol_tensor + rtol_tensor * abs_x2 + total_tolerance = atol + rtol * abs_x2 is_close = ov_opset.less_equal(abs_diff, total_tolerance) if equal_nan: - nan_a = ov_opset.isnan(x1) - nan_b = ov_opset.isnan(x2) - both_nan = ov_opset.logical_and(nan_a, nan_b) + both_nan = ov_opset.logical_and(ov_opset.isnan(x1), ov_opset.isnan(x2)) is_close = ov_opset.logical_or(is_close, both_nan) + return OpenVINOKerasTensor(is_close.output(0))