Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
onnxruntime_validation.py155 linesDownload Raw Back to capi
1# -------------------------------------------------------------------------
2# Copyright (c) Microsoft Corporation. All rights reserved.
3# Licensed under the MIT License.
4# --------------------------------------------------------------------------
5"""
6Check OS requirements for ONNX Runtime Python Bindings.
7"""
8
9import linecache
10import platform
11import warnings
12
13
14def check_distro_info():
15    __my_distro__ = ""
16    __my_distro_ver__ = ""
17    __my_system__ = platform.system().lower()
18
19    __OS_RELEASE_FILE__ = "/etc/os-release"  # noqa: N806
20    __LSB_RELEASE_FILE__ = "/etc/lsb-release"  # noqa: N806
21
22    if __my_system__ == "windows":
23        __my_distro__ = __my_system__
24        __my_distro_ver__ = platform.release().lower()
25
26        if __my_distro_ver__ not in ["10", "11", "2016server", "2019server", "2022server", "2025server"]:
27            warnings.warn(
28                f"Unsupported Windows version ({__my_distro_ver__}). ONNX Runtime supports Windows 10 and above, or Windows Server 2016 and above."
29            )
30    elif __my_system__ == "linux":
31        """Although the 'platform' python module for getting Distro information works well on standard OS images
32        running on real hardware, it is not accurate when running on Azure VMs, Git Bash, Cygwin, etc.
33        The returned values for release and version are unpredictable for virtualized or emulated environments.
34        /etc/os-release and /etc/lsb_release files, on the other hand, are guaranteed to exist and have standard values
35        in all OSes supported by onnxruntime. The former is the current standard file to check OS info and the latter
36        is its predecessor.
37        """
38        # Newer systems have /etc/os-release with relevant distro info
39        __my_distro__ = linecache.getline(__OS_RELEASE_FILE__, 3)[3:-1]
40        __my_distro_ver__ = linecache.getline(__OS_RELEASE_FILE__, 6)[12:-2]
41
42        # Older systems may have /etc/os-release instead
43        if not __my_distro__:
44            __my_distro__ = linecache.getline(__LSB_RELEASE_FILE__, 1)[11:-1]
45            __my_distro_ver__ = linecache.getline(__LSB_RELEASE_FILE__, 2)[16:-1]
46
47        # Instead of trying to parse distro specific files,
48        # warn the user ONNX Runtime may not work out of the box
49        __my_distro__ = __my_distro__.lower()
50        __my_distro_ver__ = __my_distro_ver__.lower()
51    elif __my_system__ == "darwin":
52        __my_distro__ = __my_system__
53        __my_distro_ver__ = platform.release().lower()
54
55        if int(__my_distro_ver__.split(".")[0]) < 11:
56            warnings.warn(
57                f"Unsupported macOS version ({__my_distro_ver__}). ONNX Runtime supports macOS 11.0 or later."
58            )
59    elif __my_system__ == "aix":
60        import subprocess  # noqa: PLC0415
61
62        returned_output = subprocess.check_output("oslevel")
63        __my_distro_ver__str = returned_output.decode("utf-8")
64        __my_distro_ver = __my_distro_ver__str[:3]
65    else:
66        warnings.warn(
67            f"Unsupported platform ({__my_system__}). ONNX Runtime supports Linux, macOS, AIX and Windows platforms, only."
68        )
69
70
71def get_package_name_and_version_info():
72    package_name = ""
73    version = ""
74    cuda_version = ""
75
76    try:
77        from .build_and_package_info import __version__ as version  # noqa: PLC0415
78        from .build_and_package_info import package_name  # noqa: PLC0415
79
80        try:  # noqa: SIM105
81            from .build_and_package_info import cuda_version  # noqa: PLC0415
82        except ImportError:
83            # cuda_version is optional. For example, cpu only package does not have the attribute.
84            pass
85    except Exception as e:
86        warnings.warn("WARNING: failed to collect package name and version info")
87        print(e)
88
89    return package_name, version, cuda_version
90
91
92def check_training_module():
93    import_ortmodule_exception = None
94
95    has_ortmodule = False
96    try:
97        from onnxruntime.training.ortmodule import ORTModule  # noqa: F401, PLC0415
98
99        has_ortmodule = True
100    except ImportError:
101        # ORTModule not present
102        has_ortmodule = False
103    except Exception as e:
104        # this may happen if Cuda is not installed, we want to raise it after
105        # for any exception other than not having ortmodule, we want to continue
106        # device version validation and raise the exception after.
107        try:
108            from onnxruntime.training.ortmodule._fallback import ORTModuleInitException  # noqa: PLC0415
109
110            if isinstance(e, ORTModuleInitException):
111                # ORTModule is present but not ready to run yet
112                has_ortmodule = True
113        except Exception:
114            # ORTModule not present
115            has_ortmodule = False
116
117        if not has_ortmodule:
118            import_ortmodule_exception = e
119
120    # collect onnxruntime package name, version, and cuda version
121    package_name, version, cuda_version = get_package_name_and_version_info()
122
123    if has_ortmodule and cuda_version:
124        try:
125            # collect cuda library build info. the library info may not be available
126            # when the build environment has none or multiple libraries installed
127            try:
128                from .build_and_package_info import cudart_version  # noqa: PLC0415
129            except ImportError:
130                warnings.warn("WARNING: failed to get cudart_version from onnxruntime build info.")
131                cudart_version = None
132
133            def print_build_package_info():
134                warnings.warn(f"onnxruntime training package info: package_name: {package_name}")
135                warnings.warn(f"onnxruntime training package info: __version__: {version}")
136                warnings.warn(f"onnxruntime training package info: cuda_version: {cuda_version}")
137                warnings.warn(f"onnxruntime build info: cudart_version: {cudart_version}")
138
139            # collection cuda library info from current environment.
140            from onnxruntime.capi.onnxruntime_collect_build_info import find_cudart_versions  # noqa: PLC0415
141
142            local_cudart_versions = find_cudart_versions(build_env=False, build_cuda_version=cuda_version)
143            if cudart_version and local_cudart_versions and cudart_version not in local_cudart_versions:
144                print_build_package_info()
145                warnings.warn("WARNING: failed to find cudart version that matches onnxruntime build info")
146                warnings.warn(f"WARNING: found cudart versions: {local_cudart_versions}")
147        except Exception as e:
148            warnings.warn("WARNING: failed to collect onnxruntime version and build info")
149            print(e)
150
151    if import_ortmodule_exception:
152        raise import_ortmodule_exception
153
154    return has_ortmodule, package_name, version, cuda_version
155 
codekingpro/portable-devtools · Team Ai