Skip to content

Commit 394d826

Browse files
authored
Drop support for Python <=3.8 and TensorFlow <=2.4 (#787)
* Drop support for Python <=3.8 and TensorFlow <=2.2 * Use Python 3.9 typing syntax * Remove unused import
1 parent c58e77d commit 394d826

File tree

8 files changed

+39
-71
lines changed

8 files changed

+39
-71
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- name: Run PyFlakes
2525
run: pyflakes larq_compute_engine
2626
- name: Black code style
27-
run: black larq_compute_engine --check --target-version py36
27+
run: black larq_compute_engine --check --target-version py39
2828
- name: clang-format lint
2929
uses: DoozyX/[email protected]
3030
with:

.github/workflows/release.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ jobs:
9494
fetch-depth: 0
9595
- uses: actions/setup-python@v4
9696
with:
97-
python-version: 3.8
97+
python-version: 3.9
9898
- uses: actions/cache@v3
9999
id: cache
100100
with:
@@ -137,7 +137,7 @@ jobs:
137137
runs-on: macos-latest
138138
strategy:
139139
matrix:
140-
python-version: [3.7, 3.8, 3.9, "3.10", 3.11]
140+
python-version: [3.9, "3.10", 3.11]
141141
fail-fast: false
142142
steps:
143143
- uses: actions/checkout@v3
@@ -180,7 +180,7 @@ jobs:
180180
runs-on: macos-11
181181
strategy:
182182
matrix:
183-
python-version: [3.8, 3.9, "3.10", 3.11]
183+
python-version: [3.9, "3.10", 3.11]
184184
fail-fast: false
185185
steps:
186186
- uses: actions/checkout@v3
@@ -223,7 +223,7 @@ jobs:
223223
runs-on: ubuntu-latest
224224
strategy:
225225
matrix:
226-
python-version: [3.7, 3.8, 3.9, "3.10", 3.11]
226+
python-version: [3.9, "3.10", 3.11]
227227
fail-fast: false
228228
steps:
229229
- uses: actions/checkout@v3
@@ -266,7 +266,7 @@ jobs:
266266
runs-on: windows-2019
267267
strategy:
268268
matrix:
269-
python-version: [3.7, 3.8, 3.9, "3.10", 3.11]
269+
python-version: [3.9, "3.10", 3.11]
270270
fail-fast: false
271271
steps:
272272
- name: Configure Pagefile

.github/workflows/unittests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ jobs:
100100
runs-on: ubuntu-latest
101101
strategy:
102102
matrix:
103-
tf-version: [1.14.0, 1.15.5, 2.0.4, 2.1.4, 2.2.3, 2.3.3, 2.4.4, 2.5.3, 2.6.5, 2.7.4, 2.8.4, 2.9.3, 2.10.1, 2.11.0]
104-
python-version: [3.7]
103+
tf-version: [2.5.3, 2.6.5, 2.7.4, 2.8.4, 2.9.3, 2.10.1, 2.11.0]
104+
python-version: [3.9]
105105
flatbuffers-version: [2.0]
106106
protobuf-version: [3.19.6]
107107
include:

larq_compute_engine/mlir/python/converter.py

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
2-
from packaging import version
32
import warnings
4-
from typing import Optional, Tuple, Union
3+
from typing import Optional, Union
54
import tensorflow as tf
65
import tempfile
76

@@ -23,18 +22,17 @@
2322

2423
def concrete_function_from_keras_model(model):
2524
input_signature = None
26-
if version.parse(tf.__version__) >= version.parse("2.1"):
27-
# If the model's call is not a `tf.function`, then we need to first get its
28-
# input signature from `model_input_signature` method. We can't directly
29-
# call `trace_model_call` because otherwise the batch dimension is set
30-
# to None.
31-
# Once we have better support for dynamic shapes, we can remove this.
32-
if not isinstance(model.call, def_function.Function):
33-
# Pass `keep_original_batch_size=True` will ensure that we get an input
34-
# signature including the batch dimension specified by the user.
35-
input_signature = saving_utils.model_input_signature(
36-
model, keep_original_batch_size=True
37-
)
25+
# If the model's call is not a `tf.function`, then we need to first get its
26+
# input signature from `model_input_signature` method. We can't directly
27+
# call `trace_model_call` because otherwise the batch dimension is set
28+
# to None.
29+
# Once we have better support for dynamic shapes, we can remove this.
30+
if not isinstance(model.call, def_function.Function):
31+
# Pass `keep_original_batch_size=True` will ensure that we get an input
32+
# signature including the batch dimension specified by the user.
33+
input_signature = saving_utils.model_input_signature(
34+
model, keep_original_batch_size=True
35+
)
3836

3937
func = saving_utils.trace_model_call(model, input_signature)
4038
return func.get_concrete_function()
@@ -99,7 +97,7 @@ def convert_saved_model(
9997
inference_input_type: tf.DType = tf.float32,
10098
inference_output_type: tf.DType = tf.float32,
10199
target: str = "arm",
102-
experimental_default_int8_range: Optional[Tuple[float, float]] = None,
100+
experimental_default_int8_range: Optional[tuple[float, float]] = None,
103101
) -> bytes:
104102
"""Converts a SavedModel to TFLite flatbuffer.
105103
@@ -125,10 +123,6 @@ def convert_saved_model(
125123
# Returns
126124
The converted data in serialized format.
127125
"""
128-
if version.parse(tf.__version__) < version.parse("2.2"):
129-
raise RuntimeError(
130-
"TensorFlow 2.2 or newer is required for saved model conversion."
131-
)
132126
_validate_options(
133127
inference_input_type=inference_input_type,
134128
inference_output_type=inference_output_type,
@@ -175,7 +169,7 @@ def convert_keras_model(
175169
inference_input_type: tf.DType = tf.float32,
176170
inference_output_type: tf.DType = tf.float32,
177171
target: str = "arm",
178-
experimental_default_int8_range: Optional[Tuple[float, float]] = None,
172+
experimental_default_int8_range: Optional[tuple[float, float]] = None,
179173
) -> bytes:
180174
"""Converts a Keras model to TFLite flatbuffer.
181175
@@ -236,10 +230,7 @@ def convert_keras_model(
236230
)
237231

238232
func = concrete_function_from_keras_model(model)
239-
if version.parse(tf.__version__) >= version.parse("1.15"):
240-
frozen_func = convert_variables_to_constants_v2(func, lower_control_flow=False)
241-
else:
242-
frozen_func = convert_variables_to_constants_v2(func)
233+
frozen_func = convert_variables_to_constants_v2(func, lower_control_flow=False)
243234
input_tensors = [
244235
tensor for tensor in frozen_func.inputs if tensor.dtype != tf.dtypes.resource
245236
]

larq_compute_engine/mlir/python/converter_test.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import sys
22
import unittest
3-
from packaging import version
43
from unittest import mock
54

65
import tensorflow as tf
@@ -15,7 +14,6 @@
1514

1615
from larq_compute_engine.mlir.python.converter import convert_keras_model
1716
from larq_compute_engine.mlir._tf_tfl_flatbuffer import (
18-
convert_graphdef_to_tflite_flatbuffer as mocked_graphdef_converter,
1917
convert_saved_model_to_tflite_flatbuffer as mocked_saved_model_converter,
2018
)
2119

@@ -67,21 +65,9 @@ def test_model(self):
6765
with context.eager_mode():
6866
model = get_test_model()
6967
convert_keras_model(model)
70-
if version.parse(tf.__version__) < version.parse("2.2"):
71-
mocked_graphdef_converter.assert_called_once_with(
72-
mock.ANY,
73-
["quant_conv2d_input"],
74-
["DT_FLOAT"],
75-
[[1, 28, 28, 1]],
76-
["Identity"],
77-
False,
78-
"arm",
79-
None,
80-
)
81-
else:
82-
mocked_saved_model_converter.assert_called_once_with(
83-
mock.ANY, ["serve"], ["serving_default"], 1, "arm", None
84-
)
68+
mocked_saved_model_converter.assert_called_once_with(
69+
mock.ANY, ["serve"], ["serving_default"], 1, "arm", None
70+
)
8571

8672
def test_wrong_arg(self):
8773
with self.assertRaises(ValueError):

larq_compute_engine/tests/end2end_test.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import os
33
import sys
44
import tempfile
5-
from packaging import version
65

76
import larq as lq
87
import numpy as np
@@ -238,9 +237,6 @@ def test_simple_model(dataset, conversion_function, model_cls):
238237
def test_int8_input_output(
239238
conversion_function, model_cls, inference_input_type, inference_output_type
240239
):
241-
if version.parse(tf.__version__) < version.parse("2.2"):
242-
pytest.skip("TensorFlow 2.2 or newer is required for saved model conversion.")
243-
244240
model_lce = conversion_function(
245241
model_cls(),
246242
inference_input_type=inference_input_type,

larq_compute_engine/tflite/python/interpreter_base.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
from typing import Iterator, List, Tuple, Union, Optional
1+
from collections.abc import Iterator
2+
from typing import Union, Optional
23

34
import numpy as np
45
from tqdm import tqdm
56

6-
Data = Union[np.ndarray, List[np.ndarray]]
7+
Data = Union[np.ndarray, list[np.ndarray]]
78

89

9-
def data_generator(x: Union[Data, Iterator[Data]]) -> Iterator[List[np.ndarray]]:
10+
def data_generator(x: Union[Data, Iterator[Data]]) -> Iterator[list[np.ndarray]]:
1011
if isinstance(x, np.ndarray):
1112
for inputs in x:
1213
yield [np.expand_dims(inputs, axis=0)]
@@ -36,17 +37,17 @@ def input_types(self) -> list:
3637
return self.interpreter.input_types
3738

3839
@property
39-
def input_shapes(self) -> List[Tuple[int]]:
40+
def input_shapes(self) -> list[tuple[int]]:
4041
"""Returns a list of input shapes."""
4142
return self.interpreter.input_shapes
4243

4344
@property
44-
def input_scales(self) -> List[Optional[Union[float, List[float]]]]:
45+
def input_scales(self) -> list[Optional[Union[float, list[float]]]]:
4546
"""Returns a list of input scales."""
4647
return self.interpreter.input_scales
4748

4849
@property
49-
def input_zero_points(self) -> List[Optional[int]]:
50+
def input_zero_points(self) -> list[Optional[int]]:
5051
"""Returns a list of input zero points."""
5152
return self.interpreter.input_zero_points
5253

@@ -56,17 +57,17 @@ def output_types(self) -> list:
5657
return self.interpreter.output_types
5758

5859
@property
59-
def output_shapes(self) -> List[Tuple[int]]:
60+
def output_shapes(self) -> list[tuple[int]]:
6061
"""Returns a list of output shapes."""
6162
return self.interpreter.output_shapes
6263

6364
@property
64-
def output_scales(self) -> List[Optional[Union[float, List[float]]]]:
65+
def output_scales(self) -> list[Optional[Union[float, list[float]]]]:
6566
"""Returns a list of input scales."""
6667
return self.interpreter.output_scales
6768

6869
@property
69-
def output_zero_points(self) -> List[Optional[int]]:
70+
def output_zero_points(self) -> list[Optional[int]]:
7071
"""Returns a list of input zero points."""
7172
return self.interpreter.output_zero_points
7273

setup.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def get_version_number(default):
3030
setup(
3131
name="larq-compute-engine",
3232
version=get_version_number(default="0.11.1"),
33-
python_requires=">=3.7",
33+
python_requires=">=3.9",
3434
description="Highly optimized inference engine for binarized neural networks.",
3535
long_description=readme(),
3636
long_description_content_type="text/markdown",
@@ -39,12 +39,7 @@ def get_version_number(default):
3939
packages=find_packages(),
4040
ext_modules=ext_modules,
4141
url="https://larq.dev/",
42-
install_requires=[
43-
"flatbuffers>=2.0",
44-
"packaging>=19",
45-
"tqdm>=4",
46-
"importlib-metadata ~= 2.0 ; python_version<'3.8'",
47-
],
42+
install_requires=["flatbuffers>=2.0", "tqdm>=4"],
4843
extras_require={
4944
"tensorflow": ["tensorflow>=1.14"],
5045
"tensorflow_gpu": ["tensorflow-gpu>=1.14"],
@@ -60,10 +55,9 @@ def get_version_number(default):
6055
"License :: OSI Approved :: Apache Software License",
6156
"Programming Language :: Python :: 3",
6257
"Programming Language :: Python :: 3 :: Only",
63-
"Programming Language :: Python :: 3.7",
64-
"Programming Language :: Python :: 3.8",
6558
"Programming Language :: Python :: 3.9",
6659
"Programming Language :: Python :: 3.10",
60+
"Programming Language :: Python :: 3.11",
6761
"Topic :: Scientific/Engineering",
6862
"Topic :: Scientific/Engineering :: Mathematics",
6963
"Topic :: Scientific/Engineering :: Artificial Intelligence",

0 commit comments

Comments
 (0)