m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.9 KiB

6 months ago
  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: # noqa
  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]
  40. def find_cudnn_supported_cuda_versions(build_env=False):
  41. # comments in get_cudart_version apply here
  42. if not sys.platform.startswith("linux"):
  43. warnings.warn("find_cudnn_versions only works on Linux")
  44. cudnn_possible_versions = {None}
  45. if not build_env:
  46. # if not in a build environment, there may be more than one installed cudnn.
  47. # https://developer.nvidia.com/rdp/cudnn-archive to include all that may support Cuda 10+.
  48. cudnn_possible_versions.update(
  49. {
  50. "8.2",
  51. "8.1.1",
  52. "8.1.0",
  53. "8.0.5",
  54. "8.0.4",
  55. "8.0.3",
  56. "8.0.2",
  57. "8.0.1",
  58. "7.6.5",
  59. "7.6.4",
  60. "7.6.3",
  61. "7.6.2",
  62. "7.6.1",
  63. "7.6.0",
  64. "7.5.1",
  65. "7.5.0",
  66. "7.4.2",
  67. "7.4.1",
  68. "7.3.1",
  69. "7.3.0",
  70. }
  71. )
  72. def get_cudnn_supported_cuda_version(find_cudnn_version=None):
  73. cudnn_lib_filename = "libcudnn.so"
  74. if find_cudnn_version:
  75. cudnn_lib_filename = cudnn_lib_filename + "." + find_cudnn_version
  76. # in cudnn.h cudnn version are calculated as:
  77. # #define CUDNN_VERSION (CUDNN_MAJOR * 1000 + CUDNN_MINOR * 100 + CUDNN_PATCHLEVEL)
  78. try:
  79. cudnn = ctypes.CDLL(cudnn_lib_filename)
  80. # cudnn_ver = cudnn.cudnnGetVersion()
  81. cuda_ver = cudnn.cudnnGetCudartVersion()
  82. return cuda_ver
  83. except: # noqa
  84. return None
  85. # use set to avoid duplications
  86. cuda_found_versions = {get_cudnn_supported_cuda_version(cudnn_version) for cudnn_version in cudnn_possible_versions}
  87. # convert to list and remove None
  88. return [ver for ver in cuda_found_versions if ver]