diff --git a/mindspore/python/mindspore/_extends/parallel_compile/tbe_compiler/tbe_adapter.py b/mindspore/python/mindspore/_extends/parallel_compile/tbe_compiler/tbe_adapter.py index 0553c4f0cef..556326bd51a 100644 --- a/mindspore/python/mindspore/_extends/parallel_compile/tbe_compiler/tbe_adapter.py +++ b/mindspore/python/mindspore/_extends/parallel_compile/tbe_compiler/tbe_adapter.py @@ -22,7 +22,6 @@ from datetime import datetime from tbe.common.rl_bank.bank_manager import set_current_op_name from tbe.common.repository_manager.interface import cann_kb_finalize, cann_kb_init -from tbe.common.rl_bank.bank_cfg import LocalLock from te.platform.cce_conf import te_set_version from te.platform.cce_policy import set_L1_info from te_fusion.compile_task_manager import dispatch_prebuild_task, dispatch_single_op_compile_task, import_py_module, \ @@ -35,7 +34,7 @@ from te_fusion.parallel_compilation import init_multi_process_env, start_ga_mult get_finished_compilation_task from .tbe_helper import get_soc_info, assemble_op_args, get_compute_op_list, get_options_info, get_fuzz_build_info, \ - adjust_custom_op_info, pack_op_args, get_module_name, get_real_op_debug_level + adjust_custom_op_info, pack_op_args, get_module_name, get_real_op_debug_level, LocalLock from .tbe_job import TbeJob, JobStatus PLATFORM_FLAG = ["Ascend310", "Ascend910", "Hi3796CV300ES", "Ascend710", "Ascend610", "Hi3796CV300CS", "SD3403"] diff --git a/mindspore/python/mindspore/_extends/parallel_compile/tbe_compiler/tbe_helper.py b/mindspore/python/mindspore/_extends/parallel_compile/tbe_compiler/tbe_helper.py index 91cbf241b82..7e7378895c2 100644 --- a/mindspore/python/mindspore/_extends/parallel_compile/tbe_compiler/tbe_helper.py +++ b/mindspore/python/mindspore/_extends/parallel_compile/tbe_compiler/tbe_helper.py @@ -14,11 +14,81 @@ # ============================================================================ """tbe helper to parse json content""" import os +import stat +import fcntl from enum import Enum from .tbe_job import JobType +def create_dir(dir_path): + """ + create dir + :param dir_path: + :return: T/F + """ + dir_path = os.path.realpath(dir_path) + is_exists = os.path.exists(dir_path) + if not is_exists: + try: + os.makedirs(dir_path, 0o750, exist_ok=True) + except (OSError, TypeError) as excep: + raise excep + finally: + pass + return True + + +def write_to_file(file_path, content=""): + """ + write to file + :param file_path: + :param content: + :return: T/F + """ + dir_name = os.path.dirname(file_path) + ret = create_dir(dir_name) + if not ret: + return False + + with os.fdopen(os.open(file_path, os.O_WRONLY | os.O_CREAT, \ + stat.S_IWUSR | stat.S_IRUSR | stat.S_IRGRP), 'w') as file_handler: + file_handler.write(content) + return True + + +class LocalLock: + """ + LocalLock + """ + + def __init__(self, lock_file): + if not os.path.exists(lock_file): + write_to_file(lock_file) + self.lock_fd = os.open(lock_file, os.O_WRONLY | os.O_CREAT, stat.S_IWUSR | stat.S_IRUSR | stat.S_IRGRP) + + def __del__(self): + try: + os.close(self.lock_fd) + except OSError: + pass + finally: + pass + + def lock(self): + """ + lock + """ + fcntl.flock(self.lock_fd, fcntl.LOCK_EX) + + def unlock(self): + """ + unlock + """ + fcntl.flock(self.lock_fd, fcntl.LOCK_UN) + os.close(self.lock_fd) + + class BuildType(Enum): """ Build Type """ INITIALLY = "initially_build"