diff --git a/.gitignore b/.gitignore index 52c3e2fb73c..9bbd42ed631 100644 --- a/.gitignore +++ b/.gitignore @@ -94,6 +94,8 @@ test_temp_summary_event_file/ .clangd mindspore/version.py mindspore/default_config.py +mindspore/device_target.py +mindspore/package_name.py mindspore/.commit_id # lite test file diff --git a/cmake/package.cmake b/cmake/package.cmake index 9f7a2410113..1e0bc752b5c 100644 --- a/cmake/package.cmake +++ b/cmake/package.cmake @@ -11,18 +11,23 @@ set(CPACK_TEMPORARY_PACKAGE_FILE_NAME ${CMAKE_SOURCE_DIR}/build/package/mindspor set(CPACK_TEMPORARY_INSTALL_DIRECTORY ${CMAKE_SOURCE_DIR}/build/package/mindspore) if (ENABLE_GE) set(CPACK_MS_BACKEND "ge") + set(CPACK_MS_TARGET "ascend-cpu") set(CPACK_MS_PACKAGE_NAME "mindspore") elseif (ENABLE_GPU) set(CPACK_MS_BACKEND "ms") + set(CPACK_MS_TARGET "gpu-cpu") set(CPACK_MS_PACKAGE_NAME "mindspore-gpu") elseif (ENABLE_D) set(CPACK_MS_BACKEND "ms") + set(CPACK_MS_TARGET "ascend-cpu") set(CPACK_MS_PACKAGE_NAME "mindspore-ascend") elseif (ENABLE_CPU) set(CPACK_MS_BACKEND "ms") + set(CPACK_MS_TARGET "cpu") set(CPACK_MS_PACKAGE_NAME "mindspore") else () set(CPACK_MS_BACKEND "debug") + set(CPACK_MS_TARGET "ascend-gpu-cpu") set(CPACK_MS_PACKAGE_NAME "mindspore") endif () include(CPack) diff --git a/cmake/package_script.cmake b/cmake/package_script.cmake index 0ade0af696f..89b586ed4a5 100644 --- a/cmake/package_script.cmake +++ b/cmake/package_script.cmake @@ -61,6 +61,7 @@ execute_process( string(REPLACE " " "" GIT_COMMIT_ID ${GIT_COMMIT_ID}) set(ENV{BACKEND_POLICY} ${CPACK_MS_BACKEND}) +set(ENV{BACKEND_TARGET} ${CPACK_MS_TARGET}) set(ENV{MS_PACKAGE_NAME} ${CPACK_MS_PACKAGE_NAME}) set(ENV{COMMIT_ID} ${GIT_COMMIT_ID}) diff --git a/mindspore/__init__.py b/mindspore/__init__.py index 593597cc3f0..b5085c01a73 100755 --- a/mindspore/__init__.py +++ b/mindspore/__init__.py @@ -14,6 +14,7 @@ # ============================================================================ """MindSpore package.""" +from ._version_check import check_version_and_env_config from . import common, train from .common import * from .ops import _op_impl diff --git a/mindspore/_version_check.py b/mindspore/_version_check.py new file mode 100644 index 00000000000..6fa691bdb1e --- /dev/null +++ b/mindspore/_version_check.py @@ -0,0 +1,256 @@ +# Copyright 2020 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +"""version and config check""" +import os +import sys +from pathlib import Path +from abc import abstractmethod, ABCMeta +from packaging import version +from . import log as logger +from .version import __version__ +from .package_name import __package_name__ + + +class EnvChecker(metaclass=ABCMeta): + """basic class for environment check""" + + @abstractmethod + def check_env(self, e): + pass + + @abstractmethod + def set_env(self): + pass + + @abstractmethod + def check_version(self): + pass + + +class GPUEnvChecker(EnvChecker): + """gpu environment check""" + + def __init__(self): + self.version = ["10.1"] + self.cuda_path = "/usr/local/cuda" + if os.path.exists(self.cuda_path): + # cuda default path + self.cuda_bin = self.cuda_path + "/bin" + self.cuda_lib = self.cuda_path + "/lib64" + self.cuda_version = self.cuda_path + "/version.txt" + else: + # custom or unknown environment + self.cuda_path = "" + self.cuda_bin = "" + self.cuda_lib = "" + self.cuda_version = "" + + # env + self.path = os.getenv("PATH") + self.ld_lib_path = os.getenv("LD_LIBRARY_PATH") + + # check + self.path_check = "/cuda" + self.ld_lib_path_check = "/cuda" + self.v = "0" + + def check_env(self, e): + self._check_env() + raise e + + def set_env(self): + if not self.cuda_bin: + self._check_env() + return + + if Path(self.cuda_bin).is_dir(): + os.environ['PATH'] = self.cuda_bin + ":" + os.environ['PATH'] + else: + raise EnvironmentError( + f"No such directory: {self.cuda_bin}, please check if cuda is installed correctly.") + + def check_version(self): + v = self._read_version(self.cuda_version) + if not Path(self.cuda_version).is_file(): + logger.warning("Using custom cuda path, cuda version checking is skiped, please make sure " + "Ascend 910 AI software package version is supported, you can reference to the installation " + "guidelines https://www.mindspore.cn/install") + return + + v = version.parse(v) + v_str = str(v.major) + "." + str(v.minor) + if v_str not in self.version: + raise EnvironmentError(f"MindSpore version {__version__} and cuda version {v_str} does not match, " + "reference to the match info on: https://www.mindspore.cn/install") + + def _check_env(self): + """gpu cuda path check""" + if self.path is None or self.path_check not in self.path: + logger.warning("Can not find nvcc compiler(need by mindspore-gpu), please check if you have set env " + "PATH, you can reference to the installation guidelines https://www.mindspore.cn/install") + + if self.ld_lib_path is None or self.ld_lib_path_check not in self.ld_lib_path: + logger.warning("Can not find cuda so(need by mindspore-gpu), please check if you have set env " + "LD_LIBRARY_PATH, you can reference to the installation guidelines " + "https://www.mindspore.cn/install") + + def _read_version(self, file_path): + """get gpu version info""" + with open(file_path, 'r') as f: + all_info = f.readlines() + for line in all_info: + if line.startswith("CUDA Version"): + self.v = line.strip().split("CUDA Version")[1] + return self.v + return self.v + + +class AscendEnvChecker(EnvChecker): + """ascend environment check""" + + def __init__(self): + self.version = ["1.75.T15.0.B150"] + atlas_fwk_version = "/usr/local/Ascend/nnae/latest/fwkacllib/version.info" + hisi_fwk_version = "/usr/local/Ascend/fwkacllib/version.info" + if os.path.exists(atlas_fwk_version): + # atlas default path + self.fwk_path = "/usr/local/Ascend/nnae/latest/fwkacllib" + self.op_impl_path = "/usr/local/Ascend/nnae/latest/opp/op_impl/built-in/ai_core/tbe" + self.tbe_path = self.fwk_path + "/lib64" + self.cce_path = self.fwk_path + "/ccec_compiler/bin" + self.fwk_version = atlas_fwk_version + elif os.path.exists(hisi_fwk_version): + # hisi default path + self.fwk_path = "/usr/local/Ascend/fwkacllib" + self.op_impl_path = "/usr/local/Ascend/opp/op_impl/built-in/ai_core/tbe" + self.tbe_path = self.fwk_path + "/lib64" + self.cce_path = self.fwk_path + "/ccec_compiler/bin" + self.fwk_version = hisi_fwk_version + else: + # custom or unknown environment + self.fwk_path = "" + self.op_impl_path = "" + self.tbe_path = "" + self.cce_path = "" + self.fwk_version = "" + + # env + self.path = os.getenv("PATH") + self.python_path = os.getenv("PYTHONPATH") + self.ld_lib_path = os.getenv("LD_LIBRARY_PATH") + + # check content + self.path_check = "/fwkacllib/ccec_compiler/bin/" + self.python_path_check = "opp/op_impl/built_in/ai_core/tbe/" + self.ld_lib_path_check_fwk = "/fwkacllib/lib64/" + self.ld_lib_path_check_addons = "/add-ons/" + self.v = "" + + def check_env(self, e): + self._check_env() + raise e + + def check_version(self): + if not Path(self.fwk_version).is_file(): + logger.warning("Using custom Ascend 910 AI software package path, package version checking is skiped, " + "please make sure Ascend 910 AI software package version is supported, you can reference to " + "the installation guidelines https://www.mindspore.cn/install") + return + + v = self._read_version(self.fwk_version) + if v not in self.version: + raise EnvironmentError( + f"MindSpore version {__version__} and Ascend 910 AI software package version {v} does not " + "match, reference to the match info on: https://www.mindspore.cn/install") + + def set_env(self): + if not self.tbe_path: + self._check_env() + return + + try: + # pylint: disable=unused-import + import te + except RuntimeError: + if Path(self.tbe_path).is_dir(): + os.environ['LD_LIBRARY_PATH'] = self.tbe_path + else: + raise EnvironmentError( + f"No such directory: {self.tbe_path}, Please check if Ascend 910 AI software package is " + "installed correctly.") + + if Path(self.op_impl_path).is_dir(): + sys.path.append(self.op_impl_path) + else: + raise EnvironmentError( + f"No such directory: {self.op_impl_path}, Please check if Ascend 910 AI software package is " + "installed correctly.") + + if Path(self.cce_path).is_dir(): + os.environ['PATH'] = self.cce_path + ":" + os.environ['PATH'] + else: + raise EnvironmentError( + f"No such directory: {self.cce_path}, Please check if Ascend 910 AI software package is " + "installed correctly.") + + def _check_env(self): + """ascend dependence path check""" + if self.path is None or self.path_check not in self.path: + logger.warning("Can not find ccec_compiler(need by mindspore-ascend), please check if you have set env " + "PATH, you can reference to the installation guidelines https://www.mindspore.cn/install") + + if self.python_path is None or self.python_path_check not in self.python_path: + logger.warning( + "Can not find tbe op implement(need by mindspore-ascend), please check if you have set env " + "PYTHONPATH, you can reference to the installation guidelines " + "https://www.mindspore.cn/install") + + if self.ld_lib_path is None or not (self.ld_lib_path_check_fwk in self.ld_lib_path and + self.ld_lib_path_check_addons in self.ld_lib_path): + logger.warning("Can not find driver so(need by mindspore-ascend), please check if you have set env " + "LD_LIBRARY_PATH, you can reference to the installation guidelines " + "https://www.mindspore.cn/install") + + def _read_version(self, file_path): + """get ascend version info""" + with open(file_path, 'r') as f: + all_info = f.readlines() + for line in all_info: + if line.startswith("Version="): + self.v = line.strip().split("=")[1] + return self.v + return self.v + +def check_version_and_env_config(): + """check version and env config""" + if __package_name__.lower() == "mindspore-ascend": + env_checker = AscendEnvChecker() + elif __package_name__.lower() == "mindspore-gpu": + env_checker = GPUEnvChecker() + else: + logger.info(f"Package version {__package_name__} does not need to check any environment variable, skipping.") + return + + try: + # pylint: disable=unused-import + from . import _c_expression + # check version of ascend site or cuda + env_checker.check_version() + + env_checker.set_env() + except ImportError as e: + env_checker.check_env(e) + +check_version_and_env_config() diff --git a/mindspore/ccsrc/CMakeLists.txt b/mindspore/ccsrc/CMakeLists.txt index 4ab486a3379..e917c4f5cbe 100644 --- a/mindspore/ccsrc/CMakeLists.txt +++ b/mindspore/ccsrc/CMakeLists.txt @@ -255,9 +255,16 @@ elseif (CMAKE_SYSTEM_NAME MATCHES "Windows") else () MESSAGE(FATAL_ERROR "other platform: ${CMAKE_SYSTEM_NAME}") endif () +set(MINDSPORE_RPATH ${ORIGIN_PATH}/lib) +if (ENABLE_D) + set(MINDSPORE_RPATH ${MINDSPORE_RPATH}:/usr/local/Ascend/nnae/latest/fwkacllib/lib64) + set(MINDSPORE_RPATH ${MINDSPORE_RPATH}:/usr/local/Ascend/fwkacllib/lib64) + set(MINDSPORE_RPATH ${MINDSPORE_RPATH}:/usr/local/Ascend/add-ons) +elseif (ENABLE_GPU) + set(MINDSPORE_RPATH ${MINDSPORE_RPATH}:/usr/local/cuda/lib64) +endif () -set(ORIGIN_PATH ${ORIGIN_PATH}/lib) -set_target_properties(_c_expression PROPERTIES INSTALL_RPATH ${ORIGIN_PATH}) +set_target_properties(_c_expression PROPERTIES INSTALL_RPATH ${MINDSPORE_RPATH}) if (CMAKE_SYSTEM_NAME MATCHES "Windows") target_link_libraries(mindspore mindspore::pybind11_module) @@ -293,7 +300,7 @@ if (ENABLE_GPU) ${CUDA_PATH}/lib64/stubs/libcuda.so ${CUDA_PATH}/lib64/libcusolver.so) if (ENABLE_MPI) - set_target_properties(_ms_mpi PROPERTIES INSTALL_RPATH ${ORIGIN_PATH}) + set_target_properties(_ms_mpi PROPERTIES INSTALL_RPATH ${MINDSPORE_RPATH}) endif() endif () diff --git a/mindspore/context.py b/mindspore/context.py index ebdd79f5657..0d97bcb0edd 100644 --- a/mindspore/context.py +++ b/mindspore/context.py @@ -27,6 +27,8 @@ from mindspore._checkparam import args_type_check from mindspore.parallel._auto_parallel_context import _set_auto_parallel_context, _get_auto_parallel_context, \ _reset_auto_parallel_context from mindspore.parallel._ps_context import _set_ps_context, _get_ps_context, _reset_ps_context +from .device_target import __device_target__ +from .package_name import __package_name__ __all__ = ['GRAPH_MODE', 'PYNATIVE_MODE', 'set_context', 'get_context', 'set_auto_parallel_context', 'get_auto_parallel_context', 'reset_auto_parallel_context', 'ParallelMode', 'set_ps_context', @@ -560,6 +562,10 @@ def set_context(**kwargs): # set device target first if 'device_target' in kwargs: ctx.set_device_target(kwargs['device_target']) + device = kwargs['device_target'] + if not device.lower() in __device_target__: + raise ValueError(f"Error, package type {__package_name__} support device type {__device_target__}, " + f"but got device target {device}") device = ctx.get_param(ms_ctx_param.device_target) for key, value in kwargs.items(): if not _check_target_specific_cfgs(device, key): diff --git a/requirements.txt b/requirements.txt index 18d5c6069df..a9ed29486b3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,3 +15,4 @@ sklearn >= 0.0 # for st test pandas >= 1.0.2 # for ut test bs4 astunparse +packaging diff --git a/setup.py b/setup.py index 624cf673e18..64a139f2579 100644 --- a/setup.py +++ b/setup.py @@ -26,6 +26,7 @@ from setuptools.command.build_py import build_py version = '0.7.0' backend_policy = os.getenv('BACKEND_POLICY') +device_target = os.getenv('BACKEND_TARGET') commit_id = os.getenv('COMMIT_ID').replace("\n", "") package_name = os.getenv('MS_PACKAGE_NAME').replace("\n", "") @@ -54,8 +55,32 @@ def _write_commit_file(file): file.write("__commit_id__ = '{}'\n".format(commit_id)) +def _write_package_name(file): + file.write("__package_name__ = '{}'\n".format(package_name)) + + +def _write_device_target(file): + file.write("__device_target__ = '{}'\n".format(device_target)) + + def build_dependencies(): """generate python file""" + target = os.path.join(pkg_dir, 'mindspore', 'device_target.py') + with open(target, 'w') as f: + _write_device_target(f) + + target = os.path.join(pwd, 'mindspore', 'device_target.py') + with open(target, 'w') as f: + _write_device_target(f) + + package_info = os.path.join(pkg_dir, 'mindspore', 'package_name.py') + with open(package_info, 'w') as f: + _write_package_name(f) + + package_info = os.path.join(pwd, 'mindspore', 'package_name.py') + with open(package_info, 'w') as f: + _write_package_name(f) + version_file = os.path.join(pkg_dir, 'mindspore', 'version.py') with open(version_file, 'w') as f: _write_version(f) @@ -93,7 +118,8 @@ required_package = [ 'sympy >= 1.4', 'cffi >= 1.13.2', 'decorator >= 4.4.0', - 'astunparse >= 1.6.3' + 'astunparse >= 1.6.3', + 'packaging' ] package_data = {