|
| 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 | +Functionality related to detecting and getting information for |
| 17 | +support and sparsification in the DeepSparse framework. |
| 18 | +""" |
| 19 | + |
| 20 | +import logging |
| 21 | +from typing import Any |
| 22 | + |
| 23 | +from sparseml.base import Framework, get_version |
| 24 | +from sparseml.deepsparse.base import check_deepsparse_install |
| 25 | +from sparseml.deepsparse.sparsification import sparsification_info |
| 26 | +from sparseml.framework import FrameworkInferenceProviderInfo, FrameworkInfo |
| 27 | +from sparseml.sparsification import SparsificationInfo |
| 28 | +from sparsezoo import File, Model |
| 29 | + |
| 30 | + |
| 31 | +__all__ = ["is_supported", "detect_framework", "framework_info"] |
| 32 | + |
| 33 | + |
| 34 | +_LOGGER = logging.getLogger(__name__) |
| 35 | + |
| 36 | + |
| 37 | +def is_supported(item: Any) -> bool: |
| 38 | + """ |
| 39 | + :param item: The item to detect the support for |
| 40 | + :type item: Any |
| 41 | + :return: True if the item is supported by deepsparse, False otherwise |
| 42 | + :rtype: bool |
| 43 | + """ |
| 44 | + framework = detect_framework(item) |
| 45 | + |
| 46 | + return framework == Framework.deepsparse |
| 47 | + |
| 48 | + |
| 49 | +def detect_framework(item: Any) -> Framework: |
| 50 | + """ |
| 51 | + Detect the supported ML framework for a given item specifically for the |
| 52 | + deepsparse package. |
| 53 | + Supported input types are the following: |
| 54 | + - A Framework enum |
| 55 | + - A string of any case representing the name of the framework |
| 56 | + (deepsparse, onnx, keras, pytorch, tensorflow_v1) |
| 57 | + - A supported file type within the framework such as model files: |
| 58 | + (onnx, pth, h5, pb) |
| 59 | + - An object from a supported ML framework such as a model instance |
| 60 | + If the framework cannot be determined, will return Framework.unknown |
| 61 | +
|
| 62 | + :param item: The item to detect the ML framework for |
| 63 | + :type item: Any |
| 64 | + :return: The detected framework from the given item |
| 65 | + :rtype: Framework |
| 66 | + """ |
| 67 | + framework = Framework.unknown |
| 68 | + |
| 69 | + if isinstance(item, Framework): |
| 70 | + _LOGGER.debug("framework detected from Framework instance") |
| 71 | + framework = item |
| 72 | + elif isinstance(item, str) and item.lower().strip() in Framework.__members__: |
| 73 | + _LOGGER.debug("framework detected from Framework string instance") |
| 74 | + framework = Framework[item.lower().strip()] |
| 75 | + elif isinstance(item, str) and ( |
| 76 | + "deepsparse" in item.lower().strip() or "deep sparse" in item.lower().strip() |
| 77 | + ): |
| 78 | + _LOGGER.debug("framework detected from deepsparse text") |
| 79 | + # string, check if it's a string saying deepsparse first |
| 80 | + framework = Framework.deepsparse |
| 81 | + elif isinstance(item, str) and ".onnx" in item.lower().strip(): |
| 82 | + _LOGGER.debug("framework detected from .onnx") |
| 83 | + # string, check if it's a file url or path that ends with onnx extension |
| 84 | + framework = Framework.deepsparse |
| 85 | + elif isinstance(item, Model) or isinstance(item, File): |
| 86 | + _LOGGER.debug("framework detected from SparseZoo instance") |
| 87 | + # sparsezoo model/file, deepsparse supports these natively |
| 88 | + framework = Framework.deepsparse |
| 89 | + |
| 90 | + return framework |
| 91 | + |
| 92 | + |
| 93 | +def framework_info() -> FrameworkInfo: |
| 94 | + """ |
| 95 | + Detect the information for the deepsparse framework such as package versions, |
| 96 | + availability for core actions such as training and inference, |
| 97 | + sparsification support, and inference provider support. |
| 98 | +
|
| 99 | + :return: The framework info for deepsparse |
| 100 | + :rtype: FrameworkInfo |
| 101 | + """ |
| 102 | + arch = {} |
| 103 | + |
| 104 | + if check_deepsparse_install(raise_on_error=False): |
| 105 | + from deepsparse.cpu import cpu_architecture |
| 106 | + |
| 107 | + arch = cpu_architecture() |
| 108 | + |
| 109 | + cpu_warnings = [] |
| 110 | + if arch and arch.isa != "avx512": |
| 111 | + cpu_warnings.append( |
| 112 | + "AVX512 instruction set not detected, inference performance will be limited" |
| 113 | + ) |
| 114 | + if arch and arch.isa != "avx512" and arch.isa != "avx2": |
| 115 | + cpu_warnings.append( |
| 116 | + "AVX2 and AVX512 instruction sets not detected, " |
| 117 | + "inference performance will be severely limited" |
| 118 | + ) |
| 119 | + if arch and not arch.vni: |
| 120 | + cpu_warnings.append( |
| 121 | + "VNNI instruction set not detected, " |
| 122 | + "quantized inference performance will be limited" |
| 123 | + ) |
| 124 | + |
| 125 | + cpu_provider = FrameworkInferenceProviderInfo( |
| 126 | + name="cpu", |
| 127 | + description=( |
| 128 | + "Performant CPU provider within DeepSparse specializing in speedup of " |
| 129 | + "sparsified models using AVX and VNNI instruction sets" |
| 130 | + ), |
| 131 | + device="cpu", |
| 132 | + supported_sparsification=SparsificationInfo(), # TODO: fill in when available |
| 133 | + available=check_deepsparse_install(raise_on_error=False), |
| 134 | + properties={ |
| 135 | + "cpu_architecture": arch, |
| 136 | + }, |
| 137 | + warnings=cpu_warnings, |
| 138 | + ) |
| 139 | + |
| 140 | + return FrameworkInfo( |
| 141 | + framework=Framework.deepsparse, |
| 142 | + package_versions={ |
| 143 | + "deepsparse": get_version(package_name="deepsparse", raise_on_error=False), |
| 144 | + "sparsezoo": get_version(package_name="sparsezoo", raise_on_error=False), |
| 145 | + "sparseml": get_version(package_name="sparseml", raise_on_error=False), |
| 146 | + }, |
| 147 | + sparsification=sparsification_info(), |
| 148 | + inference_providers=[cpu_provider], |
| 149 | + training_available=False, |
| 150 | + sparsification_available=False, |
| 151 | + exporting_onnx_available=False, |
| 152 | + inference_available=True, |
| 153 | + ) |
0 commit comments