onnxruntime_collect_build_info.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # -------------------------------------------------------------------------
  2. # Copyright (c) Microsoft Corporation. All rights reserved.
  3. # Licensed under the MIT License.
  4. # --------------------------------------------------------------------------
  5. import ctypes
  6. import sys
  7. import warnings
  8. def find_cudart_versions(build_env=False, build_cuda_version=None):
  9. # ctypes.CDLL and ctypes.util.find_library load the latest installed library.
  10. # it may not the the library that would be loaded by onnxruntime.
  11. # for example, in an environment with Cuda 11.1 and subsequently
  12. # conda cudatoolkit 10.2.89 installed. ctypes will find cudart 10.2. however,
  13. # onnxruntime built with Cuda 11.1 will find and load cudart for Cuda 11.1.
  14. # for the above reason, we need find all versions in the environment and
  15. # only give warnings if the expected cuda version is not found.
  16. # in onnxruntime build environment, we expected only one Cuda version.
  17. if not sys.platform.startswith("linux"):
  18. warnings.warn("find_cudart_versions only works on Linux")
  19. return None
  20. cudart_possible_versions = {None, build_cuda_version}
  21. def get_cudart_version(find_cudart_version=None):
  22. cudart_lib_filename = "libcudart.so"
  23. if find_cudart_version:
  24. cudart_lib_filename = cudart_lib_filename + "." + find_cudart_version
  25. try:
  26. cudart = ctypes.CDLL(cudart_lib_filename)
  27. cudart.cudaRuntimeGetVersion.restype = int
  28. cudart.cudaRuntimeGetVersion.argtypes = [ctypes.POINTER(ctypes.c_int)]
  29. version = ctypes.c_int()
  30. status = cudart.cudaRuntimeGetVersion(ctypes.byref(version))
  31. if status != 0:
  32. return None
  33. except Exception:
  34. return None
  35. return version.value
  36. # use set to avoid duplications
  37. cudart_found_versions = {get_cudart_version(cudart_version) for cudart_version in cudart_possible_versions}
  38. # convert to list and remove None
  39. return [ver for ver in cudart_found_versions if ver]