Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 8 additions & 3 deletions keras_nlp/backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
"""

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

if config.keras_3():
import keras # noqa: E402
else:
import keras_nlp.backend.keras2 as keras # noqa: E402

from keras_nlp.backend import ops # noqa: E402
from keras_nlp.backend import random # noqa: E402
44 changes: 0 additions & 44 deletions keras_nlp/backend/keras.py

This file was deleted.

42 changes: 42 additions & 0 deletions keras_nlp/backend/keras2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2023 The KerasNLP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import types

try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use `keras_nlp`, please install Tensorflow: "
"`pip install tensorflow`. The TensorFlow package is required for "
"data preprocessing with any backend."
)

from tensorflow.keras import * # noqa: F403, F401
from tensorflow.keras import utils

from keras_nlp.backend import config # noqa: F401

# Shims to handle symbol renames for older `tf.keras` versions.
if not hasattr(tf.keras, "saving"):
saving = types.SimpleNamespace()
else:
from tensorflow.keras import saving

if not hasattr(saving, "deserialize_keras_object"):
saving.deserialize_keras_object = utils.deserialize_keras_object
if not hasattr(saving, "serialize_keras_object"):
saving.serialize_keras_object = utils.serialize_keras_object
if not hasattr(saving, "register_keras_serializable"):
saving.register_keras_serializable = utils.register_keras_serializable