Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit ba797ac

Browse files
authored
Merge branch 'main' into interface-framework-onnx
2 parents 6b35d2c + 1ae34c5 commit ba797ac

File tree

18 files changed

+746
-27
lines changed

18 files changed

+746
-27
lines changed

.github/workflows/quality-check.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ jobs:
55
runs-on: ubuntu-latest
66
steps:
77
- uses: actions/checkout@v2
8+
- uses: actions/checkout@v2
9+
with:
10+
repository: "neuralmagic/sparsezoo"
11+
path: "sparsezoo"
12+
ref: ${{needs.test-setup.outputs.branch}}
13+
- name: "⚙️ Install sparsezoo dependencies"
14+
run: pip3 install sparsezoo/
15+
- name: "Clean sparsezoo directory"
16+
run: rm -r sparsezoo/
817
- name: "⚙️ Install dependencies"
918
run: pip3 install .[dev]
1019
- name: "🧹 Running quality checks"

src/sparseml/keras/__init__.py

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,11 @@
1313
# limitations under the License.
1414

1515
"""
16-
Code for working with the keras framework for creating /
17-
editing models for performance in the Neural Magic System
16+
Functionality for working with and sparsifying Models in the Keras framework
1817
"""
1918

2019
# flake8: noqa
2120

22-
try:
23-
import tensorflow
24-
25-
if tensorflow.__version__ < "2.1.0":
26-
raise RuntimeError("TensorFlow >= 2.1.0 is required, found {}".format(version))
27-
except:
28-
raise RuntimeError(
29-
"Unable to import tensorflow. TensorFlow>=2.1.0 is required"
30-
" to use sparseml.keras."
31-
)
32-
33-
34-
try:
35-
import keras
36-
37-
v = keras.__version__
38-
if v < "2.4.3":
39-
raise RuntimeError(
40-
"Native keras is found and will be used, but required >= 2.4.3, found {}".format(
41-
v
42-
)
43-
)
44-
except:
45-
pass
21+
from .base import *
22+
from .framework import detect_framework, framework_info, is_supported
23+
from .sparsification import sparsification_info

src/sparseml/keras/base.py

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing,
10+
# software distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import functools
17+
from typing import Optional
18+
19+
from sparseml.base import check_version
20+
21+
22+
try:
23+
import keras
24+
25+
keras_err = None
26+
is_native_keras = True
27+
except Exception as err:
28+
keras = object()
29+
keras_err = err
30+
is_native_keras = False
31+
32+
try:
33+
import tensorflow
34+
35+
tensorflow_err = None
36+
37+
if keras_err:
38+
from tensorflow import keras
39+
40+
keras_err = None
41+
except Exception as err:
42+
tensorflow = object() # TODO: populate with fake object for necessary improvements
43+
tensorflow_err = err
44+
45+
46+
try:
47+
import keras2onnx
48+
49+
keras2onnx_err = None
50+
except Exception as err:
51+
keras2onnx = object() # TODO: populate with fake object for necessary imports
52+
keras2onnx_err = err
53+
54+
55+
__all__ = [
56+
"keras",
57+
"keras_err",
58+
"tensorflow",
59+
"tensorflow_err",
60+
"keras2onnx",
61+
"keras2onnx_err",
62+
"is_native_keras",
63+
"check_keras_install",
64+
"check_keras2onnx_install",
65+
"require_keras",
66+
"require_keras2onnx",
67+
]
68+
69+
_DEF_TF_MIN_VERSION = "2.1.0"
70+
_DEF_KERAS_MIN_VERSION = "2.4.3"
71+
_KERAS2ONNX_MIN_VERSION = "1.0.0"
72+
73+
74+
def check_keras_install(
75+
min_tf_version: Optional[str] = _DEF_TF_MIN_VERSION,
76+
max_tf_version: Optional[str] = None,
77+
min_native_version: Optional[str] = _DEF_KERAS_MIN_VERSION,
78+
require_tensorflow_backend: bool = True,
79+
raise_on_error: bool = True,
80+
) -> bool:
81+
"""
82+
Check that the keras package is installed.
83+
If raise_on_error, will raise an ImportError if it is not installed or
84+
the required version range, if set, is not installed.
85+
If not raise_on_error, will return True if installed with required version
86+
and False otherwise.
87+
88+
:param min_tf_version: The minimum version for keras that it must be greater than
89+
or equal to, if unset will require no minimum version
90+
:type min_tf_version: str
91+
:param max_tf_version: The maximum version for keras that it must be less than
92+
or equal to, if unset will require no maximum version.
93+
:type max_tf_version: str
94+
:param min_native_version: The minimum version for native keras that it must be
95+
greater than or equal to if installed
96+
:type min_native_version: str
97+
:param require_tensorflow_backend: True to require keras to use the tensorflow
98+
backend, False otherwise.
99+
:type require_tensorflow_backend: bool
100+
:param raise_on_error: True to raise any issues such as not installed,
101+
minimum version, or maximum version as ImportError. False to return the result.
102+
:type raise_on_error: bool
103+
:return: If raise_on_error, will return False if keras is not installed
104+
or the version is outside the accepted bounds and True if everything is correct.
105+
:rtype: bool
106+
"""
107+
if keras_err is not None:
108+
if raise_on_error:
109+
raise keras_err
110+
return False
111+
112+
if tensorflow_err is not None and require_tensorflow_backend:
113+
if raise_on_error:
114+
raise tensorflow_err
115+
return False
116+
117+
if require_tensorflow_backend and not check_version(
118+
"tensorflow", min_tf_version, max_tf_version, raise_on_error
119+
):
120+
return False
121+
122+
if is_native_keras and not check_version(
123+
"keras", min_native_version, None, raise_on_error
124+
):
125+
return False
126+
127+
return True
128+
129+
130+
def check_keras2onnx_install(
131+
min_version: Optional[str] = _KERAS2ONNX_MIN_VERSION,
132+
max_version: Optional[str] = None,
133+
raise_on_error: bool = True,
134+
) -> bool:
135+
"""
136+
Check that the keras2onnx package is installed.
137+
If raise_on_error, will raise an ImportError if it is not installed or
138+
the required version range, if set, is not installed.
139+
If not raise_on_error, will return True if installed with required version
140+
and False otherwise.
141+
142+
:param min_version: The minimum version for keras2onnx that it must be greater than
143+
or equal to, if unset will require no minimum version
144+
:type min_version: str
145+
:param max_version: The maximum version for keras2onnx that it must be less than
146+
or equal to, if unset will require no maximum version.
147+
:type max_version: str
148+
:param raise_on_error: True to raise any issues such as not installed,
149+
minimum version, or maximum version as ImportError. False to return the result.
150+
:type raise_on_error: bool
151+
:return: If raise_on_error, will return False if keras2onnx is not installed
152+
or the version is outside the accepted bounds and True if everything is correct.
153+
:rtype: bool
154+
"""
155+
if keras2onnx_err is not None:
156+
if raise_on_error:
157+
raise keras2onnx_err
158+
return False
159+
160+
return check_version("keras2onnx", min_version, max_version, raise_on_error)
161+
162+
163+
def require_keras(
164+
min_tf_version: Optional[str] = _DEF_TF_MIN_VERSION,
165+
max_tf_version: Optional[str] = None,
166+
min_native_version: Optional[str] = _DEF_KERAS_MIN_VERSION,
167+
require_tensorflow_backend: bool = True,
168+
):
169+
"""
170+
Decorator function to require use of keras.
171+
Will check that keras package is installed and within the bounding
172+
ranges of min_version and max_version if they are set before calling
173+
the wrapped function.
174+
See :func:`check_keras_install` for more info.
175+
176+
:param min_tf_version: The minimum version for keras that it must be greater than
177+
or equal to, if unset will require no minimum version
178+
:type min_tf_version: str
179+
:param max_tf_version: The maximum version for keras that it must be less than
180+
or equal to, if unset will require no maximum version.
181+
:type max_tf_version: str
182+
:param min_native_version: The minimum version for native keras that it must be
183+
greater than or equal to if installed
184+
:type min_native_version: str
185+
:param require_tensorflow_backend: True to require keras to use the tensorflow
186+
backend, False otherwise.
187+
:type require_tensorflow_backend: bool
188+
:param require_tensorflow_backend: True to require keras to use the tensorflow
189+
backend, False otherwise.
190+
:type require_tensorflow_backend: bool
191+
"""
192+
193+
def _decorator(func):
194+
@functools.wraps(func)
195+
def _wrapper(*args, **kwargs):
196+
check_keras_install(
197+
min_tf_version,
198+
max_tf_version,
199+
min_native_version,
200+
require_tensorflow_backend,
201+
)
202+
203+
return func(*args, **kwargs)
204+
205+
return _wrapper
206+
207+
return _decorator
208+
209+
210+
def require_keras2onnx(
211+
min_version: Optional[str] = _KERAS2ONNX_MIN_VERSION,
212+
max_version: Optional[str] = None,
213+
):
214+
"""
215+
Decorator function to require use of keras2onnx.
216+
Will check that keras2onnx package is installed and within the bounding
217+
ranges of min_version and max_version if they are set before calling
218+
the wrapped function.
219+
See :func:`check_keras2onnx_install` for more info.
220+
221+
param min_version: The minimum version for keras2onnx that it must be greater than
222+
or equal to, if unset will require no minimum version
223+
:type min_version: str
224+
:param max_version: The maximum version for keras2onnx that it must be less than
225+
or equal to, if unset will require no maximum version.
226+
:type max_version: str
227+
"""
228+
229+
def _decorator(func):
230+
@functools.wraps(func)
231+
def _wrapper(*args, **kwargs):
232+
check_keras2onnx_install(min_version, max_version)
233+
234+
return func(*args, **kwargs)
235+
236+
return _wrapper
237+
238+
return _decorator

src/sparseml/keras/datasets/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818

1919
# flake8: noqa
2020

21+
from ..base import check_keras_install as _check_keras_install
2122
from .classification import *
2223
from .dataset import *
2324
from .helpers import *
2425
from .registry import *
26+
27+
28+
_check_keras_install() # TODO: remove once files within package load without installs
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing,
10+
# software distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# flake8: noqa
16+
17+
"""
18+
Functionality related to detecting and getting information for
19+
support and sparsification in the Keras framework.
20+
"""
21+
22+
# flake8: noqa
23+
24+
from .info import *

0 commit comments

Comments
 (0)