示例#1
0
def test_check_version():
    assert check_version("sparseml",
                         alternate_package_names=["sparseml-nightly"])

    assert not check_version(
        "sparseml",
        min_version="10.0.0",
        raise_on_error=False,
        alternate_package_names=["sparseml-nightly"],
    )
    with pytest.raises(ImportError):
        check_version(
            "sparseml",
            min_version="10.0.0",
            alternate_package_names=["sparseml-nightly"],
        )

    assert not check_version(
        "sparseml",
        max_version="0.0.1",
        raise_on_error=False,
        alternate_package_names=["sparseml-nightly"],
    )
    with pytest.raises(ImportError):
        check_version(
            "sparseml",
            max_version="0.0.1",
            alternate_package_names=["sparseml-nightly"],
        )

    assert not check_version("unknown", raise_on_error=False)
    with pytest.raises(ImportError):
        check_version("unknown")
示例#2
0
def check_keras_install(
    min_tf_version: Optional[str] = _DEF_TF_MIN_VERSION,
    max_tf_version: Optional[str] = None,
    min_native_version: Optional[str] = _DEF_KERAS_MIN_VERSION,
    require_tensorflow_backend: bool = True,
    raise_on_error: bool = True,
) -> bool:
    """
    Check that the keras package is installed.
    If raise_on_error, will raise an ImportError if it is not installed or
    the required version range, if set, is not installed.
    If not raise_on_error, will return True if installed with required version
    and False otherwise.

    :param min_tf_version: The minimum version for keras that it must be greater than
        or equal to, if unset will require no minimum version
    :type min_tf_version: str
    :param max_tf_version: The maximum version for keras that it must be less than
        or equal to, if unset will require no maximum version.
    :type max_tf_version: str
    :param min_native_version: The minimum version for native keras that it must be
        greater than or equal to if installed
    :type min_native_version: str
    :param require_tensorflow_backend: True to require keras to use the tensorflow
        backend, False otherwise.
    :type require_tensorflow_backend: bool
    :param raise_on_error: True to raise any issues such as not installed,
        minimum version, or maximum version as ImportError. False to return the result.
    :type raise_on_error: bool
    :return: If raise_on_error, will return False if keras is not installed
        or the version is outside the accepted bounds and True if everything is correct.
    :rtype: bool
    """
    if keras_err is not None:
        if raise_on_error:
            raise keras_err
        return False

    if tensorflow_err is not None and require_tensorflow_backend:
        if raise_on_error:
            raise tensorflow_err
        return False

    if require_tensorflow_backend and not check_version(
            "tensorflow", min_tf_version, max_tf_version, raise_on_error):
        return False

    if is_native_keras and not check_version("keras", min_native_version, None,
                                             raise_on_error):
        return False

    return True
示例#3
0
文件: base.py 项目: kevinaer/sparseml
def check_torchvision_install(
    min_version: Optional[str] = None,
    max_version: Optional[str] = None,
    raise_on_error: bool = True,
) -> bool:
    """
    Check that the torchvision package is installed.
    If raise_on_error, will raise an ImportError if it is not installed or
    the required version range, if set, is not installed.
    If not raise_on_error, will return True if installed with required version
    and False otherwise.

    :param min_version: The minimum version for torchvision that it must be greater than
        or equal to, if unset will require no minimum version
    :type min_version: str
    :param max_version: The maximum version for torchvision that it must be less than
        or equal to, if unset will require no maximum version.
    :type max_version: str
    :param raise_on_error: True to raise any issues such as not installed,
        minimum version, or maximum version as ImportError. False to return the result.
    :type raise_on_error: bool
    :return: If raise_on_error, will return False if torchvision is not installed
        or the version is outside the accepted bounds and True if everything is correct.
    :rtype: bool
    """
    if torchvision_err is not None:
        if raise_on_error:
            raise torchvision_err
        return False

    return check_version("torchvision", min_version, max_version,
                         raise_on_error)
示例#4
0
def test_check_version():
    assert check_version("sparseml")

    assert not check_version(
        "sparseml", min_version="10.0.0", raise_on_error=False)
    with pytest.raises(ImportError):
        check_version("sparseml", min_version="10.0.0")

    assert not check_version(
        "sparseml", max_version="0.0.1", raise_on_error=False)
    with pytest.raises(ImportError):
        check_version("sparseml", max_version="0.0.1")

    assert not check_version("unknown", raise_on_error=False)
    with pytest.raises(ImportError):
        check_version("unknown")
示例#5
0
def check_tensorflow_install(
    min_version: Optional[str] = _TENSORFLOW_MIN_VERSION,
    max_version: Optional[str] = _TENSORFLOW_MAX_VERSION,
    raise_on_error: bool = True,
    allow_env_ignore_flag: bool = True,
) -> bool:
    """
    Check that the tensorflow package is installed.
    If raise_on_error, will raise an ImportError if it is not installed or
    the required version range, if set, is not installed.
    If not raise_on_error, will return True if installed with required version
    and False otherwise.

    :param min_version: The minimum version for tensorflow that it must be greater than
        or equal to, if unset will require no minimum version
    :type min_version: str
    :param max_version: The maximum version for tensorflow that it must be less than
        or equal to, if unset will require no maximum version.
    :type max_version: str
    :param raise_on_error: True to raise any issues such as not installed,
        minimum version, or maximum version as ImportError. False to return the result.
    :type raise_on_error: bool
    :param allow_env_ignore_flag: True to allow the env variable SPARSEML_IGNORE_TFV1
        to ignore the tensorflow install and version checks.
        False to ignore the ignore flag.
    :type allow_env_ignore_flag: bool
    :return: If raise_on_error, will return False if tensorflow is not installed
        or the version is outside the accepted bounds and True if everything is correct.
    :rtype: bool
    """
    if allow_env_ignore_flag and os.getenv("SPARSEML_IGNORE_TFV1", False):
        return True

    if tensorflow_err is not None:
        if raise_on_error:
            raise tensorflow_err
        return False

    return check_version(
        "tensorflow",
        min_version,
        max_version,
        raise_on_error,
        alternate_package_names=["tensorflow-gpu"],
    )