Skip to content

Commit

Permalink
Ditch Keras 2 support (#1658)
Browse files Browse the repository at this point in the history
* Ditch Keras 2

* Try workaround for tf take_along_axis error

* Remove dependencies
  • Loading branch information
mattdangerw authored Jun 18, 2024
1 parent 001453d commit e0efbc8
Show file tree
Hide file tree
Showing 243 changed files with 548 additions and 831 deletions.
36 changes: 2 additions & 34 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,8 @@ permissions:
contents: read

jobs:
keras_2:
name: Test the code with Keras 2
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.9
uses: actions/setup-python@v5
with:
python-version: 3.9
- name: Get pip cache dir
id: pip-cache
run: |
python -m pip install --upgrade pip setuptools
echo "::set-output name=dir::$(pip cache dir)"
- name: pip cache
uses: actions/cache@v4
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: ${{ runner.os }}-pip-${{ hashFiles('setup.py') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
pip install -r requirements-common.txt --progress-bar off
pip install tensorflow-text==2.14 tensorflow==2.14 keras-core
pip install --no-deps -e "." --progress-bar off
- name: Test with pytest
run: |
pytest --cov=keras_nlp --cov-report xml:coverage.xml keras_nlp/
- name: Run integration tests
run: |
python pip_build.py --install && cd integration_tests && pytest .
keras_3:
name: Test the code with Keras 3
run_tests:
name: Test the code
strategy:
fail-fast: false
matrix:
Expand Down
21 changes: 4 additions & 17 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"The TensorFlow package is required for data preprocessing with any backend."
)

from keras_nlp.src.backend import config as backend_config
from keras_nlp.src.backend import keras
import keras


def pytest_addoption(parser):
Expand Down Expand Up @@ -59,7 +58,7 @@ def pytest_configure(config):
# Verify that device has GPU and detected by backend
if config.getoption("--check_gpu"):
found_gpu = False
backend = backend_config.backend()
backend = keras.config.backend()
if backend == "jax":
import jax

Expand Down Expand Up @@ -88,10 +87,6 @@ def pytest_configure(config):
"markers",
"tf_only: mark test as a tf only test",
)
config.addinivalue_line(
"markers",
"keras_3_only: mark test as a keras 3 only test",
)
config.addinivalue_line(
"markers",
"kaggle_key_required: mark test needing a kaggle key",
Expand All @@ -113,13 +108,9 @@ def pytest_collection_modifyitems(config, items):
reason="need --run_extra_large option to run",
)
tf_only = pytest.mark.skipif(
not backend_config.backend() == "tensorflow",
not keras.config.backend() == "tensorflow",
reason="tests only run on tf backend",
)
keras_3_only = pytest.mark.skipif(
not backend_config.keras_3(),
reason="tests only run on with multi-backend keras",
)
found_kaggle_key = all(
[
os.environ.get("KAGGLE_USERNAME", None),
Expand All @@ -137,13 +128,9 @@ def pytest_collection_modifyitems(config, items):
item.add_marker(skip_extra_large)
if "tf_only" in item.keywords:
item.add_marker(tf_only)
if "keras_3_only" in item.keywords:
item.add_marker(keras_3_only)
if "kaggle_key_required" in item.keywords:
item.add_marker(kaggle_key_required)


# Disable traceback filtering for quicker debugging of tests failures.
tf.debugging.disable_traceback_filtering()
if backend_config.keras_3():
keras.config.disable_traceback_filtering()
keras.config.disable_traceback_filtering()
2 changes: 1 addition & 1 deletion keras_nlp/src/api_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import types

from keras_nlp.src.backend import keras
import keras

try:
import namex
Expand Down
37 changes: 0 additions & 37 deletions keras_nlp/src/backend/__init__.py

This file was deleted.

63 changes: 0 additions & 63 deletions keras_nlp/src/backend/config.py

This file was deleted.

42 changes: 0 additions & 42 deletions keras_nlp/src/backend/keras2.py

This file was deleted.

43 changes: 0 additions & 43 deletions keras_nlp/src/backend/ops.py

This file was deleted.

20 changes: 0 additions & 20 deletions keras_nlp/src/backend/random.py

This file was deleted.

5 changes: 3 additions & 2 deletions keras_nlp/src/layers/modeling/alibi_bias.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
# limitations under the License.
import math

import keras
from keras import ops

from keras_nlp.src.api_export import keras_nlp_export
from keras_nlp.src.backend import keras
from keras_nlp.src.backend import ops


@keras_nlp_export("keras_nlp.layers.AlibiBias")
Expand Down
7 changes: 4 additions & 3 deletions keras_nlp/src/layers/modeling/alibi_bias_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from keras_nlp.src.backend import keras
from keras_nlp.src.backend import ops
from keras_nlp.src.backend import random
import keras
from keras import ops
from keras import random

from keras_nlp.src.layers.modeling.alibi_bias import AlibiBias
from keras_nlp.src.tests.test_case import TestCase

Expand Down
5 changes: 3 additions & 2 deletions keras_nlp/src/layers/modeling/cached_multi_head_attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import keras
from keras import ops

from keras_nlp.src.api_export import keras_nlp_export
from keras_nlp.src.backend import keras
from keras_nlp.src.backend import ops


@keras_nlp_export("keras_nlp.layers.CachedMultiHeadAttention")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from keras_nlp.src.backend import config
from keras_nlp.src.backend import ops
from keras_nlp.src.backend import random
from keras import ops
from keras import random

from keras_nlp.src.layers.modeling.cached_multi_head_attention import (
CachedMultiHeadAttention,
)
Expand All @@ -37,9 +37,6 @@ def test_layer_behaviors(self):
expected_output_shape=(2, 4, 6),
expected_num_trainable_weights=8,
expected_num_non_trainable_variables=1,
# Keras 2 does not handle mixed precision correctly when not set
# globally.
run_precision_checks=config.keras_3(),
)

def test_cache_call_is_correct(self):
Expand Down
5 changes: 3 additions & 2 deletions keras_nlp/src/layers/modeling/f_net_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import keras
from keras import ops

from keras_nlp.src.api_export import keras_nlp_export
from keras_nlp.src.backend import keras
from keras_nlp.src.backend import ops
from keras_nlp.src.utils.keras_utils import clone_initializer


Expand Down
Loading

0 comments on commit e0efbc8

Please sign in to comment.