|
| 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 |
0 commit comments