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

_check_version.py 12 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  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, software
  10. # 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. """version and config check"""
  16. import os
  17. import sys
  18. from pathlib import Path
  19. from abc import abstractmethod, ABCMeta
  20. from packaging import version
  21. from . import log as logger
  22. from .version import __version__
  23. from .default_config import __package_name__
  24. class EnvChecker(metaclass=ABCMeta):
  25. """basic class for environment check"""
  26. @abstractmethod
  27. def check_env(self, e):
  28. pass
  29. @abstractmethod
  30. def set_env(self):
  31. pass
  32. @abstractmethod
  33. def check_version(self):
  34. pass
  35. class GPUEnvChecker(EnvChecker):
  36. """gpu environment check"""
  37. def __init__(self):
  38. self.version = ["10.1"]
  39. self.cuda_path = "/usr/local/cuda"
  40. if os.path.exists(self.cuda_path):
  41. # cuda default path
  42. self.cuda_bin = self.cuda_path + "/bin"
  43. self.cuda_lib = self.cuda_path + "/lib64"
  44. self.cuda_version = self.cuda_path + "/version.txt"
  45. else:
  46. # custom or unknown environment
  47. self.cuda_path = ""
  48. self.cuda_bin = ""
  49. self.cuda_lib = ""
  50. self.cuda_version = ""
  51. # env
  52. self.path = os.getenv("PATH")
  53. self.ld_lib_path = os.getenv("LD_LIBRARY_PATH")
  54. # check
  55. self.path_check = "/cuda"
  56. self.ld_lib_path_check = "/cuda"
  57. self.v = "0"
  58. def check_env(self, e):
  59. self._check_env()
  60. raise e
  61. def set_env(self):
  62. if not self.cuda_bin:
  63. self._check_env()
  64. return
  65. if Path(self.cuda_bin).is_dir():
  66. os.environ['PATH'] = self.cuda_bin + ":" + os.environ['PATH']
  67. else:
  68. raise EnvironmentError(
  69. f"No such directory: {self.cuda_bin}, please check if cuda is installed correctly.")
  70. def check_version(self):
  71. if not Path(self.cuda_version).is_file():
  72. logger.warning("Using custom cuda path, cuda version checking is skiped, please make sure "
  73. "cuda version is supported, you can reference to the installation guidelines "
  74. "https://www.mindspore.cn/install")
  75. return
  76. v = self._read_version(self.cuda_version)
  77. v = version.parse(v)
  78. v_str = str(v.major) + "." + str(v.minor)
  79. if v_str not in self.version:
  80. logger.warning(f"MindSpore version {__version__} and cuda version {v_str} does not match, "
  81. "reference to the match info on: https://www.mindspore.cn/install")
  82. def _check_env(self):
  83. """gpu cuda path check"""
  84. if self.path is None or self.path_check not in self.path:
  85. logger.warning("Can not find nvcc compiler(need by mindspore-gpu), please check if you have set env "
  86. "PATH, you can reference to the installation guidelines https://www.mindspore.cn/install")
  87. if self.ld_lib_path is None or self.ld_lib_path_check not in self.ld_lib_path:
  88. logger.warning("Can not find cuda so(need by mindspore-gpu), please check if you have set env "
  89. "LD_LIBRARY_PATH, you can reference to the installation guidelines "
  90. "https://www.mindspore.cn/install")
  91. def _read_version(self, file_path):
  92. """get gpu version info"""
  93. with open(file_path, 'r') as f:
  94. all_info = f.readlines()
  95. for line in all_info:
  96. if line.startswith("CUDA Version"):
  97. self.v = line.strip().split("CUDA Version")[1]
  98. return self.v
  99. return self.v
  100. class AscendEnvChecker(EnvChecker):
  101. """ascend environment check"""
  102. def __init__(self):
  103. self.version = ["1.75.22.0.220"]
  104. atlas_nnae_version = "/usr/local/Ascend/nnae/latest/fwkacllib/version.info"
  105. atlas_toolkit_version = "/usr/local/Ascend/ascend-toolkit/latest/fwkacllib/version.info"
  106. hisi_fwk_version = "/usr/local/Ascend/fwkacllib/version.info"
  107. if os.path.exists(atlas_nnae_version):
  108. # atlas default path
  109. self.fwk_path = "/usr/local/Ascend/nnae/latest/fwkacllib"
  110. self.op_impl_path = "/usr/local/Ascend/nnae/latest/opp/op_impl/built-in/ai_core/tbe"
  111. self.tbe_path = self.fwk_path + "/lib64"
  112. self.cce_path = self.fwk_path + "/ccec_compiler/bin"
  113. self.fwk_version = atlas_nnae_version
  114. self.op_path = "/usr/local/Ascend/nnae/latest/opp"
  115. elif os.path.exists(atlas_toolkit_version):
  116. # atlas default path
  117. self.fwk_path = "/usr/local/Ascend/ascend-toolkit/latest/fwkacllib"
  118. self.op_impl_path = "/usr/local/Ascend/ascend-toolkit/latest/opp/op_impl/built-in/ai_core/tbe"
  119. self.tbe_path = self.fwk_path + "/lib64"
  120. self.cce_path = self.fwk_path + "/ccec_compiler/bin"
  121. self.fwk_version = atlas_toolkit_version
  122. self.op_path = "/usr/local/Ascend/ascend-toolkit/latest/opp"
  123. elif os.path.exists(hisi_fwk_version):
  124. # hisi default path
  125. self.fwk_path = "/usr/local/Ascend/fwkacllib"
  126. self.op_impl_path = "/usr/local/Ascend/opp/op_impl/built-in/ai_core/tbe"
  127. self.tbe_path = self.fwk_path + "/lib64"
  128. self.cce_path = self.fwk_path + "/ccec_compiler/bin"
  129. self.fwk_version = hisi_fwk_version
  130. self.op_path = ""
  131. else:
  132. # custom or unknown environment
  133. self.fwk_path = ""
  134. self.op_impl_path = ""
  135. self.tbe_path = ""
  136. self.cce_path = ""
  137. self.fwk_version = ""
  138. self.op_path = ""
  139. # env
  140. self.path = os.getenv("PATH")
  141. self.python_path = os.getenv("PYTHONPATH")
  142. self.ld_lib_path = os.getenv("LD_LIBRARY_PATH")
  143. self.ascend_opp_path = os.getenv("ASCEND_OPP_PATH")
  144. # check content
  145. self.path_check = "/fwkacllib/ccec_compiler/bin/"
  146. self.python_path_check = "opp/op_impl/built_in/ai_core/tbe/"
  147. self.ld_lib_path_check_fwk = "/fwkacllib/lib64/"
  148. self.ld_lib_path_check_addons = "/add-ons/"
  149. self.ascend_opp_path_check = "/op"
  150. self.v = ""
  151. def check_env(self, e):
  152. self._check_env()
  153. raise e
  154. def check_version(self):
  155. if not Path(self.fwk_version).is_file():
  156. logger.warning("Using custom Ascend 910 AI software package path, package version checking is skiped, "
  157. "please make sure Ascend 910 AI software package version is supported, you can reference to "
  158. "the installation guidelines https://www.mindspore.cn/install")
  159. return
  160. v = self._read_version(self.fwk_version)
  161. if v not in self.version:
  162. logger.warning(f"MindSpore version {__version__} and Ascend 910 AI software package version {v} does not "
  163. "match, reference to the match info on: https://www.mindspore.cn/install")
  164. def set_env(self):
  165. if not self.tbe_path:
  166. self._check_env()
  167. return
  168. try:
  169. # pylint: disable=unused-import
  170. import te
  171. except RuntimeError:
  172. if Path(self.tbe_path).is_dir():
  173. os.environ['LD_LIBRARY_PATH'] = self.tbe_path
  174. else:
  175. raise EnvironmentError(
  176. f"No such directory: {self.tbe_path}, Please check if Ascend 910 AI software package is "
  177. "installed correctly.")
  178. if Path(self.op_impl_path).is_dir():
  179. sys.path.append(self.op_impl_path)
  180. else:
  181. raise EnvironmentError(
  182. f"No such directory: {self.op_impl_path}, Please check if Ascend 910 AI software package is "
  183. "installed correctly.")
  184. if Path(self.cce_path).is_dir():
  185. os.environ['PATH'] = self.cce_path + ":" + os.environ['PATH']
  186. else:
  187. raise EnvironmentError(
  188. f"No such directory: {self.cce_path}, Please check if Ascend 910 AI software package is "
  189. "installed correctly.")
  190. if self.op_path is None:
  191. pass
  192. elif Path(self.op_path).is_dir():
  193. os.environ['ASCEND_OPP_PATH'] = self.op_path
  194. else:
  195. raise EnvironmentError(
  196. f"No such directory: {self.op_path}, Please check if Ascend 910 AI software package is "
  197. "installed correctly.")
  198. def _check_env(self):
  199. """ascend dependence path check"""
  200. if self.path is None or self.path_check not in self.path:
  201. logger.warning("Can not find ccec_compiler(need by mindspore-ascend), please check if you have set env "
  202. "PATH, you can reference to the installation guidelines https://www.mindspore.cn/install")
  203. if self.python_path is None or self.python_path_check not in self.python_path:
  204. logger.warning(
  205. "Can not find tbe op implement(need by mindspore-ascend), please check if you have set env "
  206. "PYTHONPATH, you can reference to the installation guidelines "
  207. "https://www.mindspore.cn/install")
  208. if self.ld_lib_path is None or not (self.ld_lib_path_check_fwk in self.ld_lib_path and
  209. self.ld_lib_path_check_addons in self.ld_lib_path):
  210. logger.warning("Can not find driver so(need by mindspore-ascend), please check if you have set env "
  211. "LD_LIBRARY_PATH, you can reference to the installation guidelines "
  212. "https://www.mindspore.cn/install")
  213. if self.ascend_opp_path is None or self.ascend_opp_path_check not in self.ascend_opp_path:
  214. logger.warning(
  215. "Can not find opp path (need by mindspore-ascend), please check if you have set env ASCEND_OPP_PATH, "
  216. "you can reference to the installation guidelines https://www.mindspore.cn/install")
  217. def _read_version(self, file_path):
  218. """get ascend version info"""
  219. with open(file_path, 'r') as f:
  220. all_info = f.readlines()
  221. for line in all_info:
  222. if line.startswith("Version="):
  223. self.v = line.strip().split("=")[1]
  224. return self.v
  225. return self.v
  226. def check_version_and_env_config():
  227. """check version and env config"""
  228. if __package_name__.lower() == "mindspore-ascend":
  229. env_checker = AscendEnvChecker()
  230. elif __package_name__.lower() == "mindspore-gpu":
  231. env_checker = GPUEnvChecker()
  232. else:
  233. logger.info(f"Package version {__package_name__} does not need to check any environment variable, skipping.")
  234. return
  235. try:
  236. # pylint: disable=unused-import
  237. from . import _c_expression
  238. # check version of ascend site or cuda
  239. env_checker.check_version()
  240. env_checker.set_env()
  241. except ImportError as e:
  242. env_checker.check_env(e)
  243. def _set_pb_env():
  244. """Set env variable `PROTOCOL_BUFFERS` to prevent memory overflow."""
  245. if os.getenv("PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION") == "cpp":
  246. logger.warning("Current env variable `PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp`,\
  247. When the parameter is too large, it may cause memory limit error.\
  248. This can be solved by set env `PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python`.")
  249. elif os.getenv("PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION") == "":
  250. logger.warning("Set the env `PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python` to prevent memory overflow.")
  251. os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
  252. check_version_and_env_config()
  253. _set_pb_env()