|
| 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 torch |
| 24 | + |
| 25 | + torch_err = None |
| 26 | +except Exception as err: |
| 27 | + torch = object() # TODO: populate with fake object for necessary imports |
| 28 | + torch_err = err |
| 29 | + |
| 30 | +try: |
| 31 | + import torchvision |
| 32 | + |
| 33 | + torchvision_err = None |
| 34 | +except Exception as err: |
| 35 | + torchvision = object() # TODO: populate with fake object for necessary imports |
| 36 | + torchvision_err = err |
| 37 | + |
| 38 | + |
| 39 | +__all__ = [ |
| 40 | + "torch", |
| 41 | + "torch_err", |
| 42 | + "torchvision", |
| 43 | + "torchvision_err", |
| 44 | + "check_torch_install", |
| 45 | + "check_torchvision_install", |
| 46 | + "require_torch", |
| 47 | + "require_torchvision", |
| 48 | +] |
| 49 | + |
| 50 | + |
| 51 | +_TORCH_MIN_VERSION = "1.0.0" |
| 52 | +_TORCH_MAX_VERSION = "1.8.100" # set bug to 100 to support all future 1.8.X versions |
| 53 | + |
| 54 | + |
| 55 | +def check_torch_install( |
| 56 | + min_version: Optional[str] = _TORCH_MIN_VERSION, |
| 57 | + max_version: Optional[str] = _TORCH_MAX_VERSION, |
| 58 | + raise_on_error: bool = True, |
| 59 | +) -> bool: |
| 60 | + """ |
| 61 | + Check that the torch package is installed. |
| 62 | + If raise_on_error, will raise an ImportError if it is not installed or |
| 63 | + the required version range, if set, is not installed. |
| 64 | + If not raise_on_error, will return True if installed with required version |
| 65 | + and False otherwise. |
| 66 | +
|
| 67 | + :param min_version: The minimum version for torch that it must be greater than |
| 68 | + or equal to, if unset will require no minimum version |
| 69 | + :type min_version: str |
| 70 | + :param max_version: The maximum version for torch that it must be less than |
| 71 | + or equal to, if unset will require no maximum version. |
| 72 | + :type max_version: str |
| 73 | + :param raise_on_error: True to raise any issues such as not installed, |
| 74 | + minimum version, or maximum version as ImportError. False to return the result. |
| 75 | + :type raise_on_error: bool |
| 76 | + :return: If raise_on_error, will return False if torch is not installed |
| 77 | + or the version is outside the accepted bounds and True if everything is correct. |
| 78 | + :rtype: bool |
| 79 | + """ |
| 80 | + if torch_err is not None: |
| 81 | + if raise_on_error: |
| 82 | + raise torch_err |
| 83 | + return False |
| 84 | + |
| 85 | + return check_version("torch", min_version, max_version, raise_on_error) |
| 86 | + |
| 87 | + |
| 88 | +def check_torchvision_install( |
| 89 | + min_version: Optional[str] = None, |
| 90 | + max_version: Optional[str] = None, |
| 91 | + raise_on_error: bool = True, |
| 92 | +) -> bool: |
| 93 | + """ |
| 94 | + Check that the torchvision package is installed. |
| 95 | + If raise_on_error, will raise an ImportError if it is not installed or |
| 96 | + the required version range, if set, is not installed. |
| 97 | + If not raise_on_error, will return True if installed with required version |
| 98 | + and False otherwise. |
| 99 | +
|
| 100 | + :param min_version: The minimum version for torchvision that it must be greater than |
| 101 | + or equal to, if unset will require no minimum version |
| 102 | + :type min_version: str |
| 103 | + :param max_version: The maximum version for torchvision that it must be less than |
| 104 | + or equal to, if unset will require no maximum version. |
| 105 | + :type max_version: str |
| 106 | + :param raise_on_error: True to raise any issues such as not installed, |
| 107 | + minimum version, or maximum version as ImportError. False to return the result. |
| 108 | + :type raise_on_error: bool |
| 109 | + :return: If raise_on_error, will return False if torchvision is not installed |
| 110 | + or the version is outside the accepted bounds and True if everything is correct. |
| 111 | + :rtype: bool |
| 112 | + """ |
| 113 | + if torchvision_err is not None: |
| 114 | + if raise_on_error: |
| 115 | + raise torchvision_err |
| 116 | + return False |
| 117 | + |
| 118 | + return check_version("torchvision", min_version, max_version, raise_on_error) |
| 119 | + |
| 120 | + |
| 121 | +def require_torch( |
| 122 | + min_version: Optional[str] = _TORCH_MIN_VERSION, |
| 123 | + max_version: Optional[str] = _TORCH_MAX_VERSION, |
| 124 | +): |
| 125 | + """ |
| 126 | + Decorator function to require use of torch. |
| 127 | + Will check that torch package is installed and within the bounding |
| 128 | + ranges of min_version and max_version if they are set before calling |
| 129 | + the wrapped function. |
| 130 | + See :func:`check_torch_install` for more info. |
| 131 | +
|
| 132 | + :param min_version: The minimum version for torch that it must be greater than |
| 133 | + or equal to, if unset will require no minimum version |
| 134 | + :type min_version: str |
| 135 | + :param max_version: The maximum version for torch that it must be less than |
| 136 | + or equal to, if unset will require no maximum version. |
| 137 | + :type max_version: str |
| 138 | + """ |
| 139 | + |
| 140 | + def _decorator(func): |
| 141 | + @functools.wraps(func) |
| 142 | + def _wrapper(*args, **kwargs): |
| 143 | + check_torch_install(min_version, max_version) |
| 144 | + |
| 145 | + return func(*args, **kwargs) |
| 146 | + |
| 147 | + return _wrapper |
| 148 | + |
| 149 | + return _decorator |
| 150 | + |
| 151 | + |
| 152 | +def require_torchvision( |
| 153 | + min_version: Optional[str] = None, max_version: Optional[str] = None |
| 154 | +): |
| 155 | + """ |
| 156 | + Decorator function to require use of torchvision. |
| 157 | + Will check that torchvision package is installed and within the bounding |
| 158 | + ranges of min_version and max_version if they are set before calling |
| 159 | + the wrapped function. |
| 160 | + See :func:`check_torchvision_install` for more info. |
| 161 | +
|
| 162 | + :param min_version: The minimum version for torchvision that it must be greater than |
| 163 | + or equal to, if unset will require no minimum version |
| 164 | + :type min_version: str |
| 165 | + :param max_version: The maximum version for torchvision that it must be less than |
| 166 | + or equal to, if unset will require no maximum version. |
| 167 | + :type max_version: str |
| 168 | + """ |
| 169 | + |
| 170 | + def _decorator(func): |
| 171 | + @functools.wraps(func) |
| 172 | + def _wrapper(*args, **kwargs): |
| 173 | + check_torchvision_install(min_version, max_version) |
| 174 | + |
| 175 | + return func(*args, **kwargs) |
| 176 | + |
| 177 | + return _wrapper |
| 178 | + |
| 179 | + return _decorator |
0 commit comments