From 147c563a88519033c6e5b84d928c3531a9658bf7 Mon Sep 17 00:00:00 2001 From: Matt Watson Date: Mon, 23 Dec 2024 11:11:49 -0800 Subject: [PATCH 1/2] Try requiring a miniumum version of keras --- .github/workflows/actions.yml | 8 ++++---- .../layers/modeling/reversible_embedding.py | 19 +++---------------- .../modeling/reversible_embedding_test.py | 7 ------- keras_hub/src/models/backbone.py | 5 ----- keras_hub/src/tests/test_case.py | 3 +-- keras_hub/src/utils/keras_utils.py | 13 ------------- setup.py | 7 ++----- 7 files changed, 10 insertions(+), 52 deletions(-) diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml index 004b197769..0c8a89c5af 100644 --- a/.github/workflows/actions.yml +++ b/.github/workflows/actions.yml @@ -20,7 +20,7 @@ jobs: version: [keras-stable] include: - backend: jax - version: keras-3.1 + version: keras-3.4 - backend: jax version: keras-nightly runs-on: ubuntu-latest @@ -48,11 +48,11 @@ jobs: run: | pip install -r requirements.txt --progress-bar off pip install --no-deps -e "." --progress-bar off - - name: Pin Keras 3.1 - if: ${{ matrix.version == 'keras-3.1'}} + - name: Pin Keras 3.4 + if: ${{ matrix.version == 'keras-3.4'}} run: | pip uninstall -y keras - pip install keras==3.1.0 --progress-bar off + pip install keras==3.4.1 --progress-bar off - name: Pin Keras Nightly if: ${{ matrix.version == 'keras-nightly'}} run: | diff --git a/keras_hub/src/layers/modeling/reversible_embedding.py b/keras_hub/src/layers/modeling/reversible_embedding.py index 549416d2a0..39692cc840 100644 --- a/keras_hub/src/layers/modeling/reversible_embedding.py +++ b/keras_hub/src/layers/modeling/reversible_embedding.py @@ -1,9 +1,7 @@ import keras from keras import ops -from packaging.version import parse from keras_hub.src.api_export import keras_hub_export -from keras_hub.src.utils.keras_utils import assert_quantization_support @keras_hub_export("keras_hub.layers.ReversibleEmbedding") @@ -145,10 +143,6 @@ def save_own_variables(self, store): if not self.built: return super().save_own_variables(store) - # Before Keras 3.2, the reverse weight is saved in the super() call. - # After Keras 3.2, the reverse weight must be saved manually. - if parse(keras.version()) < parse("3.2.0"): - return target_variables = [] if not self.tie_weights: # Store the reverse embedding weights as the last weights. @@ -239,9 +233,7 @@ def _int8_call(self, inputs, reverse=False): def quantize(self, mode, type_check=True): import gc - import inspect - assert_quantization_support() if type_check and type(self) is not ReversibleEmbedding: raise NotImplementedError( f"Layer {self.__class__.__name__} does not have a `quantize()` " @@ -250,14 +242,9 @@ def quantize(self, mode, type_check=True): self._check_quantize_args(mode, self.compute_dtype) def abs_max_quantize(inputs, axis): - sig = inspect.signature(keras.quantizers.abs_max_quantize) - if "to_numpy" in sig.parameters: - return keras.quantizers.abs_max_quantize( - inputs, axis=axis, to_numpy=True - ) - else: - # `keras<=3.4.1` doesn't support `to_numpy` - return keras.quantizers.abs_max_quantize(inputs, axis=axis) + return keras.quantizers.abs_max_quantize( + inputs, axis=axis, to_numpy=True + ) self._tracker.unlock() if mode == "int8": diff --git a/keras_hub/src/layers/modeling/reversible_embedding_test.py b/keras_hub/src/layers/modeling/reversible_embedding_test.py index 00d87ed875..4482854449 100644 --- a/keras_hub/src/layers/modeling/reversible_embedding_test.py +++ b/keras_hub/src/layers/modeling/reversible_embedding_test.py @@ -10,7 +10,6 @@ ReversibleEmbedding, ) from keras_hub.src.tests.test_case import TestCase -from keras_hub.src.utils.keras_utils import has_quantization_support class ReversibleEmbeddingTest(TestCase): @@ -97,9 +96,6 @@ def test_reverse_dtype(self): ("tie_weights", True), ("untie_weights", False) ) def test_quantize_int8(self, tie_weights): - if not has_quantization_support(): - self.skipTest("This version of Keras doesn't support quantization.") - layer_config = dict( input_dim=100, output_dim=32, tie_weights=tie_weights ) @@ -148,9 +144,6 @@ def test_quantize_int8(self, tie_weights): ("untie_weights", False), ) def test_quantize_dtype_argument(self, tie_weights): - if not has_quantization_support(): - self.skipTest("This version of Keras doesn't support quantization.") - self.run_layer_test( cls=ReversibleEmbedding, init_kwargs={ diff --git a/keras_hub/src/models/backbone.py b/keras_hub/src/models/backbone.py index dfe4b31b31..1ff75c1534 100644 --- a/keras_hub/src/models/backbone.py +++ b/keras_hub/src/models/backbone.py @@ -1,7 +1,6 @@ import keras from keras_hub.src.api_export import keras_hub_export -from keras_hub.src.utils.keras_utils import assert_quantization_support from keras_hub.src.utils.preset_utils import builtin_presets from keras_hub.src.utils.preset_utils import get_preset_loader from keras_hub.src.utils.preset_utils import get_preset_saver @@ -83,10 +82,6 @@ def token_embedding(self): def token_embedding(self, value): self._token_embedding = value - def quantize(self, mode, **kwargs): - assert_quantization_support() - return super().quantize(mode, **kwargs) - def get_config(self): # Don't chain to super here. `get_config()` for functional models is # a nested layer config and cannot be passed to Backbone constructors. diff --git a/keras_hub/src/tests/test_case.py b/keras_hub/src/tests/test_case.py index 61bc7108ed..8053fff63b 100644 --- a/keras_hub/src/tests/test_case.py +++ b/keras_hub/src/tests/test_case.py @@ -15,7 +15,6 @@ ) from keras_hub.src.models.retinanet.feature_pyramid import FeaturePyramid from keras_hub.src.tokenizers.tokenizer import Tokenizer -from keras_hub.src.utils.keras_utils import has_quantization_support from keras_hub.src.utils.tensor_utils import is_float_dtype @@ -487,7 +486,7 @@ def run_backbone_test( self.run_precision_test(cls, init_kwargs, input_data) # Check quantization. - if run_quantization_check and has_quantization_support(): + if run_quantization_check: self.run_quantization_test(backbone, cls, init_kwargs, input_data) def run_vision_backbone_test( diff --git a/keras_hub/src/utils/keras_utils.py b/keras_hub/src/utils/keras_utils.py index 6cc4dddc4b..457372fb0c 100644 --- a/keras_hub/src/utils/keras_utils.py +++ b/keras_hub/src/utils/keras_utils.py @@ -2,7 +2,6 @@ import keras from absl import logging -from packaging.version import parse try: import tensorflow as tf @@ -41,18 +40,6 @@ def gelu_approximate(x): return keras.activations.gelu(x, approximate=True) -def has_quantization_support(): - return False if parse(keras.version()) < parse("3.4.0") else True - - -def assert_quantization_support(): - if not has_quantization_support(): - raise ValueError( - "Quantization API requires Keras >= 3.4.0 to function " - f"correctly. Received: '{keras.version()}'" - ) - - def standardize_data_format(data_format): if data_format is None: return keras.config.image_data_format() diff --git a/setup.py b/setup.py index aac2e1fde6..0b3ab00279 100644 --- a/setup.py +++ b/setup.py @@ -38,17 +38,14 @@ def get_version(rel_path): author_email="keras-hub@google.com", license="Apache License 2.0", install_requires=[ + "keras>=3.4.1", "absl-py", "numpy", "packaging", "regex", "rich", "kagglehub", - # Don't require tensorflow-text on MacOS, there are no binaries for ARM. - # Also, we rely on tensorflow *transitively* through tensorflow-text. - # This avoid a slowdown during `pip install keras-hub` where pip would - # download many version of both libraries to find compatible versions. - "tensorflow-text; platform_system != 'Darwin'", + "tensorflow-text", ], extras_require={ "extras": [ From c74ec0bcabb8d78467b7b1624486297ce19af778 Mon Sep 17 00:00:00 2001 From: Matt Watson Date: Mon, 23 Dec 2024 12:52:11 -0800 Subject: [PATCH 2/2] Bump to 3.5 as the min version --- .github/workflows/actions.yml | 8 ++++---- setup.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml index 0c8a89c5af..a4cd9a4c38 100644 --- a/.github/workflows/actions.yml +++ b/.github/workflows/actions.yml @@ -20,7 +20,7 @@ jobs: version: [keras-stable] include: - backend: jax - version: keras-3.4 + version: keras-3.5 - backend: jax version: keras-nightly runs-on: ubuntu-latest @@ -48,11 +48,11 @@ jobs: run: | pip install -r requirements.txt --progress-bar off pip install --no-deps -e "." --progress-bar off - - name: Pin Keras 3.4 - if: ${{ matrix.version == 'keras-3.4'}} + - name: Pin Keras 3.5 + if: ${{ matrix.version == 'keras-3.5'}} run: | pip uninstall -y keras - pip install keras==3.4.1 --progress-bar off + pip install keras==3.5.0 --progress-bar off - name: Pin Keras Nightly if: ${{ matrix.version == 'keras-nightly'}} run: | diff --git a/setup.py b/setup.py index 0b3ab00279..6495f56bf5 100644 --- a/setup.py +++ b/setup.py @@ -38,7 +38,7 @@ def get_version(rel_path): author_email="keras-hub@google.com", license="Apache License 2.0", install_requires=[ - "keras>=3.4.1", + "keras>=3.5", "absl-py", "numpy", "packaging",