Skip to content

Commit 8eda2ab

Browse files
[OpenVINO backend] support inference for Mistral & Gemma & GPT2 using OpenVINO backend
1 parent 8392ca9 commit 8eda2ab

33 files changed

+602
-10
lines changed

.github/workflows/actions.yml

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,25 @@ jobs:
1616
strategy:
1717
fail-fast: false
1818
matrix:
19-
backend: [tensorflow, jax, torch]
19+
backend: [tensorflow, jax, torch, openvino]
2020
version: [keras-stable]
2121
include:
2222
- backend: jax
2323
version: keras-3.5
2424
- backend: jax
2525
version: keras-nightly
26+
- backend: openvino
27+
version: keras-stable
28+
python-version: '3.10'
2629
runs-on: ubuntu-latest
2730
env:
2831
KERAS_BACKEND: ${{ matrix.backend }}
2932
steps:
3033
- uses: actions/checkout@v4
31-
- name: Set up Python 3.9
34+
- name: Set up Python
3235
uses: actions/setup-python@v5
3336
with:
34-
python-version: 3.9
37+
python-version: ${{ matrix.python-version || '3.9' }}
3538
- name: Get pip cache dir
3639
id: pip-cache
3740
run: |
@@ -48,6 +51,10 @@ jobs:
4851
run: |
4952
pip install -r requirements.txt --progress-bar off
5053
pip install --no-deps -e "." --progress-bar off
54+
if [[ "${{ matrix.backend }}" == "openvino" ]]; then
55+
pip uninstall -y keras
56+
pip install git+https://github.com/Mohamed-Ashraf273/keras.git@gsoc2025 --upgrade --force-reinstall --progress-bar off
57+
fi
5158
- name: Pin Keras 3.5
5259
if: ${{ matrix.version == 'keras-3.5'}}
5360
run: |
@@ -60,7 +67,13 @@ jobs:
6067
pip install keras-nightly --progress-bar off
6168
- name: Test with pytest
6269
run: |
63-
pytest keras_hub/
70+
if [ "${{ matrix.backend }}" == "openvino" ]; then
71+
IGNORE_FILE="openvino_excluded_tests.txt"
72+
IGNORE_ARGS=$(awk '{print "--ignore=" $0}' "$IGNORE_FILE")
73+
else
74+
IGNORE_ARGS=""
75+
fi
76+
pytest keras_hub/ $IGNORE_ARGS
6477
- name: Run integration tests
6578
run: |
6679
python pip_build.py --install

conftest.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import keras
44
import pytest
5+
from keras.src.backend import backend
56

67

78
def pytest_addoption(parser):
@@ -70,6 +71,10 @@ def pytest_configure(config):
7071
"markers",
7172
"kaggle_key_required: mark test needing a kaggle key",
7273
)
74+
config.addinivalue_line(
75+
"markers",
76+
"requires_trainable_backend: mark test for trainable backend only",
77+
)
7378

7479

7580
def pytest_collection_modifyitems(config, items):
@@ -110,6 +115,42 @@ def pytest_collection_modifyitems(config, items):
110115
if "kaggle_key_required" in item.keywords:
111116
item.add_marker(kaggle_key_required)
112117

118+
openvino_skipped_tests = []
119+
if backend() == "openvino":
120+
from pathlib import Path
121+
122+
workspace_root = Path(__file__).resolve().parents[0]
123+
file_path = workspace_root / "openvino_excluded_concrete_tests.txt"
124+
with open(file_path, "r") as file:
125+
openvino_skipped_tests = [
126+
line.strip() for line in file if line.strip()
127+
]
128+
129+
requires_trainable_backend = pytest.mark.skipif(
130+
backend() in ["openvino"],
131+
reason="Trainer not implemented for OpenVINO backend.",
132+
)
133+
134+
for item in items:
135+
if "requires_trainable_backend" in item.keywords:
136+
item.add_marker(requires_trainable_backend)
137+
# also, skip concrete tests for openvino, listed in the special file
138+
# this is more granular mechanism to exclude tests rather
139+
# than using --ignore option
140+
for skipped_test in openvino_skipped_tests:
141+
if skipped_test in item.nodeid:
142+
item.add_marker(
143+
skip_if_backend(
144+
"openvino",
145+
"Not supported operation by openvino backend",
146+
)
147+
)
148+
break
149+
150+
151+
def skip_if_backend(given_backend, reason):
152+
return pytest.mark.skipif(backend() == given_backend, reason=reason)
153+
113154

114155
# Disable traceback filtering for quicker debugging of tests failures.
115156
keras.config.disable_traceback_filtering()

integration_tests/basic_usage_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
import keras_hub
77

88

9+
@unittest.skipIf(
10+
keras.backend.backend() == "openvino",
11+
"Skip for non-trainable backends like OpenVINO",
12+
)
913
class BasicUsageTest(unittest.TestCase):
1014
def test_transformer(self):
1115
# Tokenize some inputs with a binary label.

keras_hub/src/layers/modeling/alibi_bias_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import keras
2+
import pytest
23
from keras import ops
34
from keras import random
45

@@ -7,6 +8,7 @@
78

89

910
class AlibiBiasTest(TestCase):
11+
@pytest.mark.requires_trainable_backend
1012
def test_layer_behaviors(self):
1113
alibi_bias_max = 8
1214
batch_size = 4

keras_hub/src/layers/modeling/anchor_generator_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
reason="Bbox utils are not supported before keras < 3.8.0",
1515
)
1616
class AnchorGeneratorTest(TestCase):
17+
@pytest.mark.requires_trainable_backend
1718
def test_layer_behaviors(self):
1819
images_shape = (8, 128, 128, 3)
1920
self.run_layer_test(

keras_hub/src/layers/modeling/cached_multi_head_attention_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
from keras import ops
23
from keras import random
34

@@ -8,6 +9,7 @@
89

910

1011
class CachedMultiHeadAttentionTest(TestCase):
12+
@pytest.mark.requires_trainable_backend
1113
def test_layer_behaviors(self):
1214
self.run_layer_test(
1315
cls=CachedMultiHeadAttention,
@@ -75,6 +77,7 @@ def call(outputs, cache):
7577
self.assertAllClose(output, no_loop_outputs)
7678
self.assertAllClose(output_cache, no_loop_cache)
7779

80+
@pytest.mark.requires_trainable_backend
7881
def test_training_propagation(self):
7982
batch_size = 2
8083
seq_len = 5

keras_hub/src/layers/modeling/f_net_encoder_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
from keras import ops
23
from keras import random
34

@@ -6,6 +7,7 @@
67

78

89
class FNetEncoderTest(TestCase):
10+
@pytest.mark.requires_trainable_backend
911
def test_layer_behaviors(self):
1012
self.run_layer_test(
1113
cls=FNetEncoder,
@@ -31,6 +33,7 @@ def test_value_error_when_invalid_kernel_initializer(self):
3133
kernel_initializer="Invalid",
3234
)
3335

36+
@pytest.mark.requires_trainable_backend
3437
def test_training_propagation(self):
3538
x = random.uniform(shape=(2, 4, 6))
3639
layer = FNetEncoder(

keras_hub/src/layers/modeling/masked_lm_head_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
from keras import random
23

34
from keras_hub.src.layers.modeling.masked_lm_head import MaskedLMHead
@@ -8,6 +9,7 @@
89

910

1011
class MaskedLMHeadTest(TestCase):
12+
@pytest.mark.requires_trainable_backend
1113
def test_layer_behaviors(self):
1214
self.run_layer_test(
1315
cls=MaskedLMHead,
@@ -27,6 +29,7 @@ def test_layer_behaviors(self):
2729
expected_num_trainable_weights=6,
2830
)
2931

32+
@pytest.mark.requires_trainable_backend
3033
def test_layer_behaviors_with_embedding(self):
3134
embedding = ReversibleEmbedding(100, 16)
3235
embedding.build((4, 10))

keras_hub/src/layers/modeling/position_embedding_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import keras
22
import numpy as np
3+
import pytest
34
from keras import ops
45
from keras import random
56

@@ -15,6 +16,7 @@ def custom_init(shape, dtype=None):
1516

1617

1718
class PositionEmbeddingTest(TestCase):
19+
@pytest.mark.requires_trainable_backend
1820
def test_layer_behaviors(self):
1921
self.run_layer_test(
2022
cls=PositionEmbedding,
@@ -26,6 +28,7 @@ def test_layer_behaviors(self):
2628
expected_num_trainable_weights=1,
2729
)
2830

31+
@pytest.mark.requires_trainable_backend
2932
def test_layer_behaviors_4d(self):
3033
self.run_layer_test(
3134
cls=PositionEmbedding,

keras_hub/src/layers/modeling/reversible_embedding_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import keras
44
import numpy as np
5+
import pytest
56
from absl.testing import parameterized
67
from keras import ops
78
from keras import random
@@ -17,6 +18,7 @@ class ReversibleEmbeddingTest(TestCase):
1718
("tie_weights", True),
1819
("untie_weights", False),
1920
)
21+
@pytest.mark.requires_trainable_backend
2022
def test_layer_behaviors_tied(self, tie_weights):
2123
self.run_layer_test(
2224
cls=ReversibleEmbedding,

0 commit comments

Comments
 (0)