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

Add preliminary support of OpenVINO as Keras 3 backend #19727

Open
wants to merge 62 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
76d4c1a
[POC][OV] Support OpenVINO as Keras 3 backend
rkazants May 17, 2024
af254e9
Merge remote-tracking branch 'upstream/master' into rkazants/poc_open…
rkazants Sep 23, 2024
3e0772f
Mark all unsupported ops from numpy space
rkazants Sep 23, 2024
c2c1808
Mark unsupported ops in core, image, and linalg spaces
rkazants Sep 23, 2024
d26c22c
Mark unsupported ops in math, nn, random, and rnn spaces
rkazants Sep 23, 2024
9e29a73
Fix sorting imports
rkazants Sep 24, 2024
716a0bb
Format imports
rkazants Sep 24, 2024
c097126
Fix sorting imports
rkazants Sep 24, 2024
65fe256
Fix sorting imports
rkazants Sep 24, 2024
fe29eb9
Fix inference
rkazants Sep 24, 2024
f57847b
Remove openvino specific code in common part
rkazants Sep 24, 2024
8fb2dc5
Fix typo
rkazants Sep 24, 2024
3d8b41a
Clean-up code
rkazants Sep 25, 2024
08b74dc
Recover imports
rkazants Sep 25, 2024
52408b7
Sort imports properly
rkazants Sep 25, 2024
f41b454
Format source code
rkazants Sep 25, 2024
42364c2
Format the rest of source code
rkazants Sep 25, 2024
954ed1f
Continue format adjustment
rkazants Sep 25, 2024
facde41
Add OpenVINO dependency
rkazants Sep 25, 2024
22d5c17
Merge remote-tracking branch 'upstream/master' into rkazants/poc_open…
rkazants Oct 31, 2024
fa6b461
Fix inference using OV backend
rkazants Nov 1, 2024
0b1537d
Support bert_base_en_uncased and mobilenet_v3_small from Keras Hub
rkazants Nov 25, 2024
64681ab
Merge remote-tracking branch 'upstream/master' into rkazants/poc_open…
rkazants Nov 25, 2024
9d31b56
Remove extra openvino specific code from layer.py
rkazants Nov 25, 2024
9785746
Apply code-style formatting
rkazants Nov 25, 2024
43bd76d
Apply code-style formatting
rkazants Nov 25, 2024
4f68322
Fix remained code-style issue
rkazants Nov 25, 2024
34549b8
Run tests for OpenVINO backend in GHA
rkazants Nov 25, 2024
a418ede
Add config file for openvino backend validation
rkazants Nov 25, 2024
4dae81d
Add import test for openvino backend
rkazants Nov 25, 2024
1559ea0
Fix error in import_test.py
rkazants Nov 25, 2024
e72b32e
Add import_test for openvino backend
rkazants Nov 25, 2024
f086752
Add openvino specific integration tests in GHA
rkazants Nov 25, 2024
389c3f5
Exclude coverage for OpenVINO
rkazants Nov 25, 2024
770ba91
remove coverage for openvino backend
rkazants Nov 25, 2024
9162c15
Try layer tests for openvino backend
rkazants Nov 26, 2024
b5d7413
Run layer tests for openvino backend selectively
rkazants Nov 27, 2024
7ae168f
Mark enabled tests for openvino backend in a different way
rkazants Nov 27, 2024
45a5ed8
Update .github/workflows/actions.yml
rkazants Nov 27, 2024
702a719
Merge remote-tracking branch 'upstream/master' into rkazants/poc_open…
rkazants Nov 27, 2024
cf6542a
Fix import for BackendVariable
rkazants Nov 27, 2024
6026728
Fix errors in layer tests for openvino backend
rkazants Nov 27, 2024
e3e2aed
Add test for Elu via openvino backend
rkazants Nov 28, 2024
796f509
Fix sorted imports
rkazants Nov 28, 2024
758e774
Extend testing for attention
rkazants Nov 28, 2024
34dbfe0
Update keras/src/layers/attention/attention_test.py
rkazants Nov 28, 2024
c1f7604
Switch on activation tests for openvino backend
rkazants Nov 29, 2024
a2bfa71
Merge remote-tracking branch 'origin/rkazants/poc_openvino_backend' i…
rkazants Nov 29, 2024
8bf05d7
Switch on attention tests for openvino backend
rkazants Nov 29, 2024
b2024cf
Update keras/src/layers/attention/additive_attention_test.py
rkazants Nov 29, 2024
1ffc4af
Update keras/src/layers/attention/grouped_query_attention_test.py
rkazants Nov 29, 2024
95f4207
Run conv tests for openvino backend
rkazants Nov 29, 2024
cb74641
Merge remote-tracking branch 'origin/rkazants/poc_openvino_backend' i…
rkazants Nov 29, 2024
dce9f33
Fix convolution in openvino backend
rkazants Nov 29, 2024
39cce17
Work around constant creation for tuple
rkazants Nov 29, 2024
ffc4e22
Work around constant creation in reshape
rkazants Nov 29, 2024
437193a
Run depthwise conv tests for openvino backend
rkazants Nov 30, 2024
a316c7a
Fix get_ov_output for other x types
rkazants Dec 1, 2024
779235a
Fix elu translation
rkazants Dec 1, 2024
15fc0d7
Fix softmax and log_softmax for None axis
rkazants Dec 2, 2024
cba959a
Run nn tests for openvino backend
rkazants Dec 2, 2024
9ab6412
Merge remote-tracking branch 'upstream/master' into rkazants/poc_open…
rkazants Dec 2, 2024
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
11 changes: 9 additions & 2 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
fail-fast: false
matrix:
python-version: [3.9]
backend: [tensorflow, jax, torch, numpy]
backend: [tensorflow, jax, torch, numpy, openvino]
name: Run tests
runs-on: ubuntu-latest
env:
Expand Down Expand Up @@ -66,7 +66,7 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: false
- name: Test integrations
if: ${{ matrix.backend != 'numpy'}}
if: ${{ matrix.backend != 'numpy' && matrix.backend != 'openvino' }}
run: |
python integration_tests/import_test.py
python integration_tests/numerical_test.py
Expand All @@ -84,7 +84,14 @@ jobs:
run: |
pytest integration_tests/torch_workflow_test.py
python integration_tests/torch_custom_fit_test.py
- name: Test OpenVINO-specific integrations
if: ${{ matrix.backend == 'openvino' }}
run: |
python integration_tests/import_test.py
rkazants marked this conversation as resolved.
Show resolved Hide resolved
pytest keras --ignore keras/src/applications --cov=keras --cov-config=pyproject.toml -m openvino_backend
coverage xml --omit='keras/src/applications/*,keras/api' -o core-coverage.xml
- name: Test with pytest
if: ${{ matrix.backend != 'openvino'}}
run: |
pytest keras --ignore keras/src/applications --cov=keras --cov-config=pyproject.toml
coverage xml --omit='keras/src/applications/*,keras/api' -o core-coverage.xml
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/config/openvino/keras.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"floatx": "float32",
"epsilon": 1e-07,
"backend": "openvino",
"image_data_format": "channels_last"
}
8 changes: 6 additions & 2 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ def pytest_configure(config):
"markers",
"requires_trainable_backend: mark test for trainable backend only",
)
config.addinivalue_line(
"markers",
"openvino_backend: mark test case supported by OpenVINO backend",
)


def pytest_collection_modifyitems(config, items):
requires_trainable_backend = pytest.mark.skipif(
backend() == "numpy",
reason="Trainer not implemented for NumPy backend.",
backend() == "numpy" or backend() == "openvino",
reason="Trainer not implemented for NumPy and OpenVINO backend.",
)
for item in items:
if "requires_trainable_backend" in item.keywords:
Expand Down
19 changes: 14 additions & 5 deletions integration_tests/import_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"--extra-index-url https://download.pytorch.org/whl/cpu ",
),
"jax": ("jax[cpu]", ""),
"openvino": ("openvino", ""),
}


Expand Down Expand Up @@ -57,18 +58,26 @@ def manage_venv_installs(whl_path):
"pip uninstall -y "
+ BACKEND_REQ[other_backends[0]][0]
+ " "
+ BACKEND_REQ[other_backends[1]][0],
+ BACKEND_REQ[other_backends[1]][0]
+ " "
+ BACKEND_REQ[other_backends[2]][0],
# Install `.whl` package
"pip install " + whl_path,
]
run_commands_venv(install_setup)


def run_keras_flow():
test_script = [
# Runs the example script
"python -m pytest integration_tests/basic_full_flow.py",
]
if backend.backend() == "openvino":
test_script = [
# Runs the example script
"python -m pytest integration_tests/openvino/basic_full_flow.py",
]
else:
test_script = [
# Runs the example script
"python -m pytest integration_tests/basic_full_flow.py",
]
run_commands_venv(test_script)


Expand Down
24 changes: 24 additions & 0 deletions integration_tests/openvino/basic_full_flow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import numpy as np

import keras
from keras.src import testing


class BasicFlowTest(testing.TestCase):
def test_basic_fit(self):
def get_expected_result(x1, x2):
z = x1 + x2
return 1.0 / (1.0 + np.exp(-z))

x1 = keras.Input(shape=(2,), dtype="float32")
x2 = keras.Input(shape=(2,), dtype="float32")
add = keras.layers.Add()([x1, x2])
sigmoid = keras.activations.sigmoid(add)
model = keras.Model(inputs=[x1, x2], outputs=sigmoid)

x = np.random.random((128, 4))
y = np.random.random((128, 4))

result = model.predict([x, y])
expected = get_expected_result(x, y)
self.assertAllClose(result, expected, rtol=1e-05)
5 changes: 5 additions & 0 deletions keras/src/backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
from keras.src.backend.numpy import * # noqa: F403
from keras.src.backend.numpy.core import Variable as BackendVariable

distribution_lib = None
elif backend() == "openvino":
from keras.src.backend.openvino import * # noqa: F403
from keras.src.backend.openvino.core import Variable as BackendVariable

distribution_lib = None
else:
raise ValueError(f"Unable to import backend : {backend()}")
Expand Down
24 changes: 24 additions & 0 deletions keras/src/backend/openvino/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from keras.src.backend.common.name_scope import name_scope
from keras.src.backend.openvino import core
from keras.src.backend.openvino import image
from keras.src.backend.openvino import linalg
from keras.src.backend.openvino import math
from keras.src.backend.openvino import nn
from keras.src.backend.openvino import numpy
from keras.src.backend.openvino import random
from keras.src.backend.openvino.core import IS_THREAD_SAFE
from keras.src.backend.openvino.core import SUPPORTS_SPARSE_TENSORS
from keras.src.backend.openvino.core import Variable
from keras.src.backend.openvino.core import cast
from keras.src.backend.openvino.core import compute_output_spec
from keras.src.backend.openvino.core import cond
from keras.src.backend.openvino.core import convert_to_numpy
from keras.src.backend.openvino.core import convert_to_tensor
from keras.src.backend.openvino.core import is_tensor
from keras.src.backend.openvino.core import random_seed_dtype
from keras.src.backend.openvino.core import shape
from keras.src.backend.openvino.core import vectorized_map
from keras.src.backend.openvino.rnn import cudnn_ok
from keras.src.backend.openvino.rnn import gru
from keras.src.backend.openvino.rnn import lstm
from keras.src.backend.openvino.rnn import rnn
Loading