已合并
扩展ruff-pre-commit hooks (第一批目录) #4256
Water8L创建于 7月10日
扩展ruff-pre-commit hooks (第一批目录) #4256
已合并
Water8L创建于 7月10日
共 118 个文件变更+9174-4718
@@ -36,6 +36,42 @@ repos:
36 - "--verbose"36 - "--verbose"
37 - "-i"37 - "-i"
38 38 
39+ # Python 代码检查
40+ - repo: https://gitcode.com/gh_mirrors/ru/ruff-pre-commit
41+ rev: v0.14.14
42+ hooks:
43+ - id: ruff-check
44+ # 临时忽略存量问题,按目录治理后逐项移除。
45+ args:
46+ - "--output-format"
47+ - "github"
48+ - "--fix"
49+ - "--extend-ignore"
50+ - "E402,E711,E712,E721,F401,F403,F405,F811,F821,F841"
51+ types: [python]
52+ files: ^(cmake|docs|impl|scripts|tests)/
53+ exclude: &ruff_exclude |
54+ (?x)^(
55+ \.agent/|
56+ cmake/asc/legacy_modules/util/extract_host_stub\.py|
57+ tests/python/aclrt_launch_kernel/test_aclrt_launch_kernel\.py|
58+ tests/python/adapter/test_ascendc_super_kernel\.py|
59+ tests/python/adapter/test_compile_op\.py|
60+ tests/python/adapter/test_get_op_tiling\.py|
61+ tests/python/asc_op_compiler/test_ascendc_super_kernel\.py|
62+ tests/python/asc_op_compiler/test_compile_op\.py|
63+ tests/python/asc_op_compiler/test_get_op_tiling\.py|
64+ tests/python/asc_opc/stub/files/conv_compute\.py|
65+ tests/python/asc_opc/stub/files/gather_v2\.py|
66+ tests/python/asc_opc/stub/files/ge_ir_pb2\.py|
67+ tools/build/asc_op_compile_base/adapter/compile_op\.py|
68+ tools/build/asc_op_compile_base/asc_op_compiler/compile_op\.py
69+ )$
70+ - id: ruff-format
71+ types: [python]
72+ files: ^(cmake|docs|impl|scripts|tests)/
73+ exclude: *ruff_exclude
74+ 
39 # OAT 合规性检查 (Open Source Audit Tool)75 # OAT 合规性检查 (Open Source Audit Tool)
40 - repo: local76 - repo: local
41 hooks:77 hooks:
@@ -16,28 +16,43 @@ import os
16import glob16import glob
17import json17import json
18import sys18import sys
19-from asc_op_compile_base.asc_op_compiler.op_tiling import _ASCEND_OPP_PATH_ENV, \19+from asc_op_compile_base.asc_op_compiler.op_tiling import (
20- _ASCEND_OPP_PATH_DEFAULT, op_impl_path20+ _ASCEND_OPP_PATH_ENV,
21+ _ASCEND_OPP_PATH_DEFAULT,
22+ op_impl_path,
23+)
21from asc_op_compile_base.common.utils.log_utils import LogUtil, AscendCLogLevel24from asc_op_compile_base.common.utils.log_utils import LogUtil, AscendCLogLevel
22 25 
23 26 
24def check_optype_duplicate(args, ini_optypes):27def check_optype_duplicate(args, ini_optypes):
25 opp_path = os.environ.get(_ASCEND_OPP_PATH_ENV, _ASCEND_OPP_PATH_DEFAULT)28 opp_path = os.environ.get(_ASCEND_OPP_PATH_ENV, _ASCEND_OPP_PATH_DEFAULT)
26- json_path_base = os.path.join(opp_path, op_impl_path, os.path.join("ai_core", "tbe", "config", args.soc_version))29+ json_path_base = os.path.join(
27- LogUtil.print_compile_log("check_optype_duplicate", f"json path base is {json_path_base}", \30+ opp_path,
28- AscendCLogLevel.LOG_DEBUG, LogUtil.Option.NON_SOC)31+ op_impl_path,
29- json_pattern = json_path_base + f"/*.json"32+ os.path.join("ai_core", "tbe", "config", args.soc_version),
33+ )
34+ LogUtil.print_compile_log(
35+ "check_optype_duplicate",
36+ f"json path base is {json_path_base}",
37+ AscendCLogLevel.LOG_DEBUG,
38+ LogUtil.Option.NON_SOC,
39+ )
40+ json_pattern = json_path_base + "/*.json"
30 json_files = glob.glob(json_pattern, recursive=True)41 json_files = glob.glob(json_pattern, recursive=True)
31 42 
32 if len(json_files) == 0:43 if len(json_files) == 0:
33- LogUtil.print_compile_log("check_optype_duplicate", f"no json files found", \44+ LogUtil.print_compile_log(
34- AscendCLogLevel.LOG_DEBUG, LogUtil.Option.NON_SOC)45+ "check_optype_duplicate",
46+ "no json files found",
47+ AscendCLogLevel.LOG_DEBUG,
48+ LogUtil.Option.NON_SOC,
49+ )
35 return 050 return 0
36 51 
37 optypes = []52 optypes = []
38 for json_file in json_files:53 for json_file in json_files:
39 try:54 try:
40- with open(json_file, 'r') as fd:55+ with open(json_file, "r") as fd:
41 ops_info = json.load(fd)56 ops_info = json.load(fd)
42 optypes += ops_info.keys()57 optypes += ops_info.keys()
43 except Exception as err:58 except Exception as err:
@@ -45,10 +60,18 @@ def check_optype_duplicate(args, ini_optypes):
45 return 160 return 1
46 61 
47 intersection_ops = list(set(ini_optypes) & set(optypes))62 intersection_ops = list(set(ini_optypes) & set(optypes))
48- LogUtil.print_compile_log("check_optype_duplicate", f"custom optypes {ini_optypes}", \63+ LogUtil.print_compile_log(
49- AscendCLogLevel.LOG_DEBUG, LogUtil.Option.NON_SOC)64+ "check_optype_duplicate",
50- LogUtil.print_compile_log("check_optype_duplicate", \65+ f"custom optypes {ini_optypes}",
51- f"duplicate optypes {intersection_ops}", AscendCLogLevel.LOG_DEBUG, LogUtil.Option.NON_SOC)66+ AscendCLogLevel.LOG_DEBUG,
67+ LogUtil.Option.NON_SOC,
68+ )
69+ LogUtil.print_compile_log(
70+ "check_optype_duplicate",
71+ f"duplicate optypes {intersection_ops}",
72+ AscendCLogLevel.LOG_DEBUG,
73+ LogUtil.Option.NON_SOC,
74+ )
52 if len(intersection_ops) != 0:75 if len(intersection_ops) != 0:
53 sys.stderr.write(" ".join(intersection_ops))76 sys.stderr.write(" ".join(intersection_ops))
54 return 277 return 2
@@ -58,8 +81,12 @@ def check_optype_duplicate(args, ini_optypes):
58 81 
59def get_optypes(args):82def get_optypes(args):
60 op_config = configparser.ConfigParser()83 op_config = configparser.ConfigParser()
61- LogUtil.print_compile_log("check_optype_duplicate", f"ini file: {args.ini_file}", \84+ LogUtil.print_compile_log(
62- AscendCLogLevel.LOG_DEBUG, LogUtil.Option.NON_SOC)85+ "check_optype_duplicate",
86+ f"ini file: {args.ini_file}",
87+ AscendCLogLevel.LOG_DEBUG,
88+ LogUtil.Option.NON_SOC,
89+ )
63 90 
64 op_config.read(args.ini_file)91 op_config.read(args.ini_file)
65 return op_config.sections()92 return op_config.sections()
@@ -67,12 +94,8 @@ def get_optypes(args):
67 94 
68def args_parse():95def args_parse():
69 parser = argparse.ArgumentParser()96 parser = argparse.ArgumentParser()
70- parser.add_argument(97+ parser.add_argument("-i", "--ini-file", help="op info ini.")
71- "-i", "--ini-file", help="op info ini."98+ parser.add_argument("-v", "--soc-version", help="soc version.")
72- )
73- parser.add_argument(
74- "-v", "--soc-version", help="soc version."
75- )
76 return parser.parse_args()99 return parser.parse_args()
77 100 
78 101 
@@ -10,10 +10,7 @@
10# See LICENSE in the root of the software repository for the full text of the License.10# See LICENSE in the root of the software repository for the full text of the License.
11# ----------------------------------------------------------------------------------------------------------11# ----------------------------------------------------------------------------------------------------------
12 12 
13-import sys
14import os13import os
15-import subprocess
16-import time
17import glob14import glob
18import configparser15import configparser
19import shutil16import shutil
@@ -40,7 +37,9 @@ class CompileKernel:
40 self.compile_sh = []37 self.compile_sh = []
41 self.target_name = args.target_name38 self.target_name = args.target_name
42 self.auto_gen_path = args.auto_gen_path39 self.auto_gen_path = args.auto_gen_path
43- self.source_ini_file = os.path.join(self.auto_gen_path, self.target_name + "_custom_source_files.ini")40+ self.source_ini_file = os.path.join(
41+ self.auto_gen_path, self.target_name + "_custom_source_files.ini"
42+ )
44 43 
45 if os.path.exists(self.source_ini_file):44 if os.path.exists(self.source_ini_file):
46 self.get_op_cpp_file()45 self.get_op_cpp_file()
@@ -72,19 +71,31 @@ class CompileKernel:
72 find_type = True71 find_type = True
73 72 
74 if src_ini_config.has_option(sub_sections, "compute_unit"):73 if src_ini_config.has_option(sub_sections, "compute_unit"):
75- src_compute_units = src_ini_config.get(sub_sections, "compute_unit").split(",")74+ src_compute_units = src_ini_config.get(
75+ sub_sections, "compute_unit"
76+ ).split(",")
76 for sub_unit in src_compute_units:77 for sub_unit in src_compute_units:
77 src_compute_units_low.append(_trans_soc_ver_to_short(sub_unit))78 src_compute_units_low.append(_trans_soc_ver_to_short(sub_unit))
78 79 
79- if len(src_compute_units_low) == 0 or self.op_soc_ver in src_compute_units_low:80+ if (
81+ len(src_compute_units_low) == 0
82+ or self.op_soc_ver in src_compute_units_low
83+ ):
80 if src_ini_config.has_option(sub_sections, "kernel_dir"):84 if src_ini_config.has_option(sub_sections, "kernel_dir"):
81 self.op_cpp_file = os.path.realpath(85 self.op_cpp_file = os.path.realpath(
82- os.path.join(self.impl_path,86+ os.path.join(
87+ self.impl_path,
83 src_ini_config.get(sub_sections, "kernel_dir"),88 src_ini_config.get(sub_sections, "kernel_dir"),
84- src_ini_config.get(sub_sections, "kernel_file")))89+ src_ini_config.get(sub_sections, "kernel_file"),
90+ )
91+ )
85 else:92 else:
86 self.op_cpp_file = os.path.realpath(93 self.op_cpp_file = os.path.realpath(
87- os.path.join(self.impl_path, src_ini_config.get(sub_sections, "kernel_file")))94+ os.path.join(
95+ self.impl_path,
96+ src_ini_config.get(sub_sections, "kernel_file"),
97+ )
98+ )
88 find_cpp_file = True99 find_cpp_file = True
89 break100 break
90 if find_cpp_file != find_type:101 if find_cpp_file != find_type:
@@ -95,7 +106,7 @@ class CompileKernel:
95 )106 )
96 107 
97 def clean(self: any):108 def clean(self: any):
98- if 'dump_cce' not in self.op_debug_config:109+ if "dump_cce" not in self.op_debug_config:
99 shutil.rmtree(self.working_dir)110 shutil.rmtree(self.working_dir)
100 return111 return
101 112 
@@ -109,7 +120,12 @@ class CompileKernel:
109 os.makedirs(os.path.join(self.working_dir, "dynamic"), exist_ok=True)120 os.makedirs(os.path.join(self.working_dir, "dynamic"), exist_ok=True)
110 cfg_dir[const_var.AUTO_GEN_DIR] = os.path.dirname(self.op_cfg_ini)121 cfg_dir[const_var.AUTO_GEN_DIR] = os.path.dirname(self.op_cfg_ini)
111 ascendc_impl_build.write_scripts(122 ascendc_impl_build.write_scripts(
112- self.op_cfg_ini, rep_cfg, cfg_dir, [self.op_type], self.compile_options, self.source_ini_file123+ self.op_cfg_ini,
124+ rep_cfg,
125+ cfg_dir,
126+ [self.op_type],
127+ self.compile_options,
128+ self.source_ini_file,
113 )129 )
114 py_files = glob.glob(os.path.join(self.working_dir, "dynamic", "*.py"))130 py_files = glob.glob(os.path.join(self.working_dir, "dynamic", "*.py"))
115 if py_files is None or len(py_files) != 1:131 if py_files is None or len(py_files) != 1:
@@ -133,13 +149,20 @@ class CompileKernel:
133 base_dir = os.path.dirname(self.op_cfg_ini)149 base_dir = os.path.dirname(self.op_cfg_ini)
134 opc_config_file = os.path.join(base_dir, "custom_opc_options.ini")150 opc_config_file = os.path.join(base_dir, "custom_opc_options.ini")
135 ascendc_bin_param_build.gen_bin_param_file(151 ascendc_bin_param_build.gen_bin_param_file(
136- self.op_cfg_ini, bin_param_path, self.op_soc_ver, opc_config_file, [self.op_type]152+ self.op_cfg_ini,
153+ bin_param_path,
154+ self.op_soc_ver,
155+ opc_config_file,
156+ [self.op_type],
137 )157 )
138- tiling_key_info, op_debug_config, kernel_json_file, _, kernel_template_input_info = (158+ (
139- ascendc_bin_param_build.parse_op_debug_config(159+ tiling_key_info,
140- opc_config_file,160+ op_debug_config,
141- self.op_soc_ver161+ kernel_json_file,
142- )162+ _,
163+ kernel_template_input_info,
164+ ) = ascendc_bin_param_build.parse_op_debug_config(
165+ opc_config_file, self.op_soc_ver
143 )166 )
144 if self.op_type in op_debug_config:167 if self.op_type in op_debug_config:
145 self.op_debug_config = op_debug_config[self.op_type]168 self.op_debug_config = op_debug_config[self.op_type]
@@ -167,18 +190,26 @@ class CompileKernel:
167 190 
168 def ascendc_put_json(self: any):191 def ascendc_put_json(self: any):
169 if self.json_file is not None:192 if self.json_file is not None:
170- json_file_dir = os.path.join(self.build_opp_path,193+ json_file_dir = os.path.join(
171- "op_impl",194+ self.build_opp_path,
172- "ai_core",195+ "op_impl",
173- "tbe",196+ "ai_core",
174- "config",197+ "tbe",
175- self.op_soc_ver)198+ "config",
199+ self.op_soc_ver,
200+ )
176 os.makedirs(json_file_dir)201 os.makedirs(json_file_dir)
177 shutil.copy(self.json_file, json_file_dir)202 shutil.copy(self.json_file, json_file_dir)
178- build_json_file = os.path.join(json_file_dir, "aic-{}-ops-info.json".format(self.op_soc_ver))203+ build_json_file = os.path.join(
204+ json_file_dir, "aic-{}-ops-info.json".format(self.op_soc_ver)
205+ )
179 if not os.path.exists(build_json_file):206 if not os.path.exists(build_json_file):
180 self.clean()207 self.clean()
181- raise RuntimeError("prepare json file aic-{}-ops-info.json failed!".format(self.op_soc_ver))208+ raise RuntimeError(
209+ "prepare json file aic-{}-ops-info.json failed!".format(
210+ self.op_soc_ver
211+ )
212+ )
182 213 
183 def ascendc_build(self: any):214 def ascendc_build(self: any):
184 op_info = ascendc_op_info.OpInfo(self.op_type, self.op_cfg_ini)215 op_info = ascendc_op_info.OpInfo(self.op_type, self.op_cfg_ini)
@@ -206,13 +237,29 @@ class CompileKernel:
206 fd.write("\n".join(sub_cmd))237 fd.write("\n".join(sub_cmd))
207 238 
208 if os.getenv("TILINGKEY_PAR_COMPILE") is None:239 if os.getenv("TILINGKEY_PAR_COMPILE") is None:
209- cmd_str = ('export HI_PYTHON=python3 && export ASCEND_CUSTOM_OPP_PATH={} && export TILINGKEY_PAR_COMPILE=1'240+ cmd_str = (
210- '&& make -f {} PY={} OUT={} CPP={}')241+ "export HI_PYTHON=python3 && export ASCEND_CUSTOM_OPP_PATH={} && export TILINGKEY_PAR_COMPILE=1"
242+ "&& make -f {} PY={} OUT={} CPP={}"
243+ )
211 else:244 else:
212- cmd_str = ('export HI_PYTHON=python3 && export ASCEND_CUSTOM_OPP_PATH={} && make -f {} PY={} OUT={} CPP={}')245+ cmd_str = "export HI_PYTHON=python3 && export ASCEND_CUSTOM_OPP_PATH={} && make -f {} PY={} OUT={} CPP={}"
213- if os.system(cmd_str.format(self.build_opp_path, mkfile, self.op_impl_py, op_bin_dir, self.op_cpp_file)) != 0:246+ if (
214- raise RuntimeError('Kernel Compilation Error: OpType {} Kernel File {}!'.format(247+ os.system(
215- self.op_type, self.op_cpp_file))248+ cmd_str.format(
249+ self.build_opp_path,
250+ mkfile,
251+ self.op_impl_py,
252+ op_bin_dir,
253+ self.op_cpp_file,
254+ )
255+ )
256+ != 0
257+ ):
258+ raise RuntimeError(
259+ "Kernel Compilation Error: OpType {} Kernel File {}!".format(
260+ self.op_type, self.op_cpp_file
261+ )
262+ )
216 263 
217 264 
218def args_parse():265def args_parse():
@@ -241,13 +288,25 @@ def args_parse():
241 "-o", "--output-path", nargs="?", help="Output path of compile result."288 "-o", "--output-path", nargs="?", help="Output path of compile result."
242 )289 )
243 parser.add_argument(290 parser.add_argument(
244- "-dy", "--dynamic-dir", nargs="?", default=None, help="dynamic path of source compile."291+ "-dy",
292+ "--dynamic-dir",
293+ nargs="?",
294+ default=None,
295+ help="dynamic path of source compile.",
245 )296 )
246 parser.add_argument(297 parser.add_argument(
247- "-eb", "--enable-binary", nargs="?", default=None, help="whether binary compile is enabled."298+ "-eb",
299+ "--enable-binary",
300+ nargs="?",
301+ default=None,
302+ help="whether binary compile is enabled.",
248 )303 )
249 parser.add_argument(304 parser.add_argument(
250- "-j", "--json-file", nargs="?", default=None, help="aic-<compute-unit>-ops-info.json file path."305+ "-j",
306+ "--json-file",
307+ nargs="?",
308+ default=None,
309+ help="aic-<compute-unit>-ops-info.json file path.",
251 )310 )
252 # $(MAKE) is necessary for parallel compiling311 # $(MAKE) is necessary for parallel compiling
253 parser.add_argument(312 parser.add_argument(
@@ -13,7 +13,6 @@
13import configparser13import configparser
14import argparse14import argparse
15import os15import os
16-import stat
17import shutil16import shutil
18from pathlib import Path17from pathlib import Path
19import warnings18import warnings
@@ -23,8 +22,12 @@ def args_parse():
23 parser = argparse.ArgumentParser()22 parser = argparse.ArgumentParser()
24 parser.add_argument("-p", "--auto-gen-path", required=True, help="auto gen path")23 parser.add_argument("-p", "--auto-gen-path", required=True, help="auto gen path")
25 parser.add_argument("-n", "--target-name", required=True, help="kernel target name")24 parser.add_argument("-n", "--target-name", required=True, help="kernel target name")
26- parser.add_argument("-s", "--kernel-base-dir", required=True, help="kernel base dir")25+ parser.add_argument(
27- parser.add_argument("-d", "--copy-dst-dir", required=True, help="copy kernel file dst dir")26+ "-s", "--kernel-base-dir", required=True, help="kernel base dir"
27+ )
28+ parser.add_argument(
29+ "-d", "--copy-dst-dir", required=True, help="copy kernel file dst dir"
30+ )
28 return parser.parse_args()31 return parser.parse_args()
29 32 
30 33 
@@ -64,12 +67,18 @@ def copy_dir(src_dir, dst_dir, exclude_dir=None):
64 continue67 continue
65 for item in items + dirs:68 for item in items + dirs:
66 src_item = os.path.abspath(os.path.join(dir_path, item))69 src_item = os.path.abspath(os.path.join(dir_path, item))
67- dst_item = os.path.abspath(os.path.join(dst_dir, dir_path.replace(src_dir, "."), item))70+ dst_item = os.path.abspath(
71+ os.path.join(dst_dir, dir_path.replace(src_dir, "."), item)
72+ )
68 if os.path.islink(src_item):73 if os.path.islink(src_item):
69- raise ValueError(f"unsupport soft link file, please check it. {os.path.abspath(src_item)}")74+ raise ValueError(
75+ f"unsupport soft link file, please check it. {os.path.abspath(src_item)}"
76+ )
70 elif os.path.isdir(src_item):77 elif os.path.isdir(src_item):
71 continue78 continue
72- if not check_file_extension(src_item, ["", ".txt", ".h", ".hpp", ".cpp", ".c"]):79+ if not check_file_extension(
80+ src_item, ["", ".txt", ".h", ".hpp", ".cpp", ".c"]
81+ ):
73 warnings.warn(82 warnings.warn(
74 f"\n[Warning]: Unexpected file type found in {os.path.abspath(src_item)}"83 f"\n[Warning]: Unexpected file type found in {os.path.abspath(src_item)}"
75 f"\n[Warning]: Please check dir is required and correct : {os.path.abspath(dir_path)}",84 f"\n[Warning]: Please check dir is required and correct : {os.path.abspath(dir_path)}",
@@ -83,11 +92,15 @@ def copy_dir(src_dir, dst_dir, exclude_dir=None):
83def copy_file(src_file, dst_file):92def copy_file(src_file, dst_file):
84 try:93 try:
85 if not os.path.exists(src_file):94 if not os.path.exists(src_file):
86- raise FileNotFoundError(f"kernel src file {os.path.abspath(src_file)} not found, please check it.")95+ raise FileNotFoundError(
96+ f"kernel src file {os.path.abspath(src_file)} not found, please check it."
97+ )
87 os.makedirs(os.path.dirname(dst_file), exist_ok=True)98 os.makedirs(os.path.dirname(dst_file), exist_ok=True)
88 if (not src_file.endswith(".txt")) and (not os.path.exists(dst_file)):99 if (not src_file.endswith(".txt")) and (not os.path.exists(dst_file)):
89 if os.path.islink(src_file):100 if os.path.islink(src_file):
90- raise ValueError(f"unsupport soft link file, please check it. {os.path.abspath(src_file)}")101+ raise ValueError(
102+ f"unsupport soft link file, please check it. {os.path.abspath(src_file)}"
103+ )
91 elif os.path.isfile(src_file):104 elif os.path.isfile(src_file):
92 shutil.copy2(src_file, dst_file)105 shutil.copy2(src_file, dst_file)
93 else:106 else:
@@ -126,13 +139,21 @@ def is_safe_relative_path(path: str) -> bool:
126 139 
127 140 
128def copy_kernel_src_file(args):141def copy_kernel_src_file(args):
129- kernel_source_ini_file_path = os.path.join(args.auto_gen_path, args.target_name + "_custom_source_files.ini")142+ kernel_source_ini_file_path = os.path.join(
143+ args.auto_gen_path, args.target_name + "_custom_source_files.ini"
144+ )
130 if os.path.exists(args.copy_dst_dir):145 if os.path.exists(args.copy_dst_dir):
131 shutil.rmtree(args.copy_dst_dir)146 shutil.rmtree(args.copy_dst_dir)
132 suffixes_to_ignore = [".txt"]147 suffixes_to_ignore = [".txt"]
133- cmake_current_binary_path = os.path.abspath(os.path.join(args.copy_dst_dir, "..", "..", ".."))148+ cmake_current_binary_path = os.path.abspath(
149+ os.path.join(args.copy_dst_dir, "..", "..", "..")
150+ )
134 if not os.path.exists(kernel_source_ini_file_path):151 if not os.path.exists(kernel_source_ini_file_path):
135- copy_dir(args.kernel_base_dir, args.copy_dst_dir, exclude_dir=cmake_current_binary_path)152+ copy_dir(
153+ args.kernel_base_dir,
154+ args.copy_dst_dir,
155+ exclude_dir=cmake_current_binary_path,
156+ )
136 kernel_src_config = configparser.ConfigParser()157 kernel_src_config = configparser.ConfigParser()
137 kernel_src_config.read(kernel_source_ini_file_path)158 kernel_src_config.read(kernel_source_ini_file_path)
138 sections = kernel_src_config.sections()159 sections = kernel_src_config.sections()
@@ -13,18 +13,18 @@
13import sys13import sys
14import stat14import stat
15import os15import os
16-import re
17-import json
18-import const_var
19 16 
20 17 
21-def write_options_to_file(file_name: str, options_str: str, \18+def write_options_to_file(
22- op_type: str, compute_unit: str, split_char: str):19+ file_name: str, options_str: str, op_type: str, compute_unit: str, split_char: str
20+):
23 flags = os.O_WRONLY | os.O_CREAT21 flags = os.O_WRONLY | os.O_CREAT
24 modes = stat.S_IWUSR | stat.S_IRUSR22 modes = stat.S_IWUSR | stat.S_IRUSR
25 try:23 try:
26- with os.fdopen(os.open(file_name, flags, modes), 'a') as fd:24+ with os.fdopen(os.open(file_name, flags, modes), "a") as fd:
27- fd.write(op_type + split_char + compute_unit + split_char + options_str + '\n')25+ fd.write(
26+ op_type + split_char + compute_unit + split_char + options_str + "\n"
27+ )
28 except Exception as err:28 except Exception as err:
29 print("write compile options config file failed")29 print("write compile options config file failed")
30 raise (err)30 raise (err)
@@ -39,20 +39,20 @@ def _handle_oom_option(opts, opc_debug_config):
39 39 
40def _handle_input_param_file(opts):40def _handle_input_param_file(opts):
41 """Handle input param file option."""41 """Handle input param file option."""
42- return "--input-param-file=" + opts.strip().split('=')[1]42+ return "--input-param-file=" + opts.strip().split("=")[1]
43 43 
44 44 
45def _handle_tiling_key(opts):45def _handle_tiling_key(opts):
46- keys = opts.strip().split('=')[1].split(',')46+ keys = opts.strip().split("=")[1].split(",")
47 return ";".join([key for key in keys])47 return ";".join([key for key in keys])
48 48 
49 49 
50def _handle_kernel_template_input(opts):50def _handle_kernel_template_input(opts):
51 if "=" not in opts:51 if "=" not in opts:
52- raise RuntimeError('Invalid --kernel-template-input option format!')52+ raise RuntimeError("Invalid --kernel-template-input option format!")
53 input_value = opts.split("=", 1)53 input_value = opts.split("=", 1)
54 if not input_value[1]:54 if not input_value[1]:
55- raise RuntimeError('No value given for --kernel-template-input option!')55+ raise RuntimeError("No value given for --kernel-template-input option!")
56 return input_value[1]56 return input_value[1]
57 57 
58 58 
@@ -91,7 +91,9 @@ def _parse_compile_options(compile_options):
91 opc_template_kernel_str = ""91 opc_template_kernel_str = ""
92 92 
93 for opts in compile_options:93 for opts in compile_options:
94- category, value = _parse_single_option(opts, opc_debug_config, opc_kernel_config)94+ category, value = _parse_single_option(
95+ opts, opc_debug_config, opc_kernel_config
96+ )
95 if category == "compile_opt":97 if category == "compile_opt":
96 compile_opt.append(opts)98 compile_opt.append(opts)
97 elif category == "input_param":99 elif category == "input_param":
@@ -101,13 +103,24 @@ def _parse_compile_options(compile_options):
101 elif category == "template_input":103 elif category == "template_input":
102 opc_template_kernel_str = value104 opc_template_kernel_str = value
103 105 
104- return compile_opt, opc_debug_config, opc_kernel_config, opc_tiling_keys, input_param_file, opc_template_kernel_str106+ return (
107+ compile_opt,
108+ opc_debug_config,
109+ opc_kernel_config,
110+ opc_tiling_keys,
111+ input_param_file,
112+ opc_template_kernel_str,
113+ )
105 114 
106 115 
107-def _build_opc_config_str(opc_debug_config, opc_tiling_keys, opc_template_kernel_str, opc_kernel_config):116+def _build_opc_config_str(
117+ opc_debug_config, opc_tiling_keys, opc_template_kernel_str, opc_kernel_config
118+):
108 opc_config_str = ""119 opc_config_str = ""
109 if opc_debug_config:120 if opc_debug_config:
110- opc_config_str = "--op_debug_config=" + ';'.join([opt for opt in opc_debug_config])121+ opc_config_str = "--op_debug_config=" + ";".join(
122+ [opt for opt in opc_debug_config]
123+ )
111 if len(opc_tiling_keys) > 0:124 if len(opc_tiling_keys) > 0:
112 if opc_config_str != "":125 if opc_config_str != "":
113 opc_config_str += "@"126 opc_config_str += "@"
@@ -119,47 +132,63 @@ def _build_opc_config_str(opc_debug_config, opc_tiling_keys, opc_template_kernel
119 if opc_kernel_config:132 if opc_kernel_config:
120 if opc_config_str != "":133 if opc_config_str != "":
121 opc_config_str += "@"134 opc_config_str += "@"
122- opc_config_str += ' '.join(opc_kernel_config)135+ opc_config_str += " ".join(opc_kernel_config)
123 return opc_config_str136 return opc_config_str
124 137 
125 138 
126def _write_compile_options(compile_options_file, compile_opt, op_type, compute_unit):139def _write_compile_options(compile_options_file, compile_opt, op_type, compute_unit):
127 if len(compile_opt) > 0:140 if len(compile_opt) > 0:
128- options_str = ';'.join([opt for opt in compile_opt])141+ options_str = ";".join([opt for opt in compile_opt])
129- write_options_to_file(compile_options_file, options_str, op_type, compute_unit, ",")142+ write_options_to_file(
143+ compile_options_file, options_str, op_type, compute_unit, ","
144+ )
130 145 
131 146 
132def _write_opc_config(opc_config_file, opc_config_str, op_type, compute_unit):147def _write_opc_config(opc_config_file, opc_config_str, op_type, compute_unit):
133 if opc_config_str != "":148 if opc_config_str != "":
134- write_options_to_file(opc_config_file, opc_config_str, op_type, compute_unit, "@")149+ write_options_to_file(
150+ opc_config_file, opc_config_str, op_type, compute_unit, "@"
151+ )
135 152 
136 153 
137def _write_input_param_file(opc_config_file, input_param_file, op_type, compute_unit):154def _write_input_param_file(opc_config_file, input_param_file, op_type, compute_unit):
138 if input_param_file != "":155 if input_param_file != "":
139 if op_type == "ALL" or compute_unit == "":156 if op_type == "ALL" or compute_unit == "":
140- raise RuntimeError('--input-param-file must be used with a COMPUTE_UNIT, and OP_TYPE cannot be ALL.')157+ raise RuntimeError(
141- write_options_to_file(opc_config_file, input_param_file, op_type, compute_unit, "@")158+ "--input-param-file must be used with a COMPUTE_UNIT, and OP_TYPE cannot be ALL."
159+ )
160+ write_options_to_file(
161+ opc_config_file, input_param_file, op_type, compute_unit, "@"
162+ )
142 163 
143 164 
144-def gen_compile_options(compile_options_file: str, op_type: str, \165+def gen_compile_options(
145- compute_unit: str, compile_options: list):166+ compile_options_file: str, op_type: str, compute_unit: str, compile_options: list
167+):
146 base_dir = os.path.dirname(compile_options_file)168 base_dir = os.path.dirname(compile_options_file)
147 opc_config_file = os.path.join(base_dir, "custom_opc_options.ini")169 opc_config_file = os.path.join(base_dir, "custom_opc_options.ini")
148 170 
149- compile_opt, opc_debug_config, opc_kernel_config, opc_tiling_keys, input_param_file, opc_template_kernel_str = \171+ (
150- _parse_compile_options(compile_options)172+ compile_opt,
173+ opc_debug_config,
174+ opc_kernel_config,
175+ opc_tiling_keys,
176+ input_param_file,
177+ opc_template_kernel_str,
178+ ) = _parse_compile_options(compile_options)
151 179 
152 _write_compile_options(compile_options_file, compile_opt, op_type, compute_unit)180 _write_compile_options(compile_options_file, compile_opt, op_type, compute_unit)
153 181 
154- opc_config_str = _build_opc_config_str(opc_debug_config, opc_tiling_keys, opc_template_kernel_str, \182+ opc_config_str = _build_opc_config_str(
155- opc_kernel_config)183+ opc_debug_config, opc_tiling_keys, opc_template_kernel_str, opc_kernel_config
184+ )
156 _write_opc_config(opc_config_file, opc_config_str, op_type, compute_unit)185 _write_opc_config(opc_config_file, opc_config_str, op_type, compute_unit)
157 _write_input_param_file(opc_config_file, input_param_file, op_type, compute_unit)186 _write_input_param_file(opc_config_file, input_param_file, op_type, compute_unit)
158 187 
159 188 
160def parse_options(args):189def parse_options(args):
161 result = []190 result = []
162- current = ''191+ current = ""
163 in_quotes = False192 in_quotes = False
164 quote_char = None193 quote_char = None
165 194 
@@ -172,23 +201,24 @@ def parse_options(args):
172 else:201 else:
173 in_quotes = True202 in_quotes = True
174 quote_char = ch203 quote_char = ch
175- if ch == ';' and not in_quotes:204+ if ch == ";" and not in_quotes:
176 result.append(current)205 result.append(current)
177- current = ''206+ current = ""
178 else:207 else:
179 current += ch208 current += ch
180 if not in_quotes:209 if not in_quotes:
181 result.append(current)210 result.append(current)
182- current = ''211+ current = ""
183 else:212 else:
184 pass213 pass
185 if current:214 if current:
186 result.append(current)215 result.append(current)
187 return result216 return result
188 217 
189-if __name__ == '__main__':218+ 
219+if __name__ == "__main__":
190 if len(sys.argv) < 4:220 if len(sys.argv) < 4:
191- raise RuntimeError('arguments must greater than 4')221+ raise RuntimeError("arguments must greater than 4")
192 compute_soc = ""222 compute_soc = ""
193 comp_options = []223 comp_options = []
194 for i in range(len(sys.argv) - 3):224 for i in range(len(sys.argv) - 3):
@@ -11,7 +11,6 @@
11# ----------------------------------------------------------------------------------------------------------11# ----------------------------------------------------------------------------------------------------------
12 12 
13import os13import os
14-import sys
15import glob14import glob
16import shutil15import shutil
17import argparse16import argparse
@@ -23,13 +22,20 @@ def run_command(args, **others):
23 try:22 try:
24 subprocess.run(args, check=True, **others)23 subprocess.run(args, check=True, **others)
25 except subprocess.CalledProcessError as e:24 except subprocess.CalledProcessError as e:
26- LogUtil.print_compile_log("", f"Command failed: {e}!", AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC)25+ LogUtil.print_compile_log(
26+ "",
27+ f"Command failed: {e}!",
28+ AscendCLogLevel.LOG_ERROR,
29+ LogUtil.Option.NON_SOC,
30+ )
27 31 
28 32 
29def args_parse():33def args_parse():
30 parser = argparse.ArgumentParser()34 parser = argparse.ArgumentParser()
31 parser.add_argument(35 parser.add_argument(
32- "-f", "--object-files-dir", help="Output files from host object targets, generated by cmake."36+ "-f",
37+ "--object-files-dir",
38+ help="Output files from host object targets, generated by cmake.",
33 )39 )
34 40 
35 parser.add_argument(41 parser.add_argument(
@@ -37,27 +43,32 @@ def args_parse():
37 )43 )
38 44 
39 parser.add_argument(45 parser.add_argument(
40- "-t", "--tmp-obj-dir", help="Temporary dir for combining object files to static library."46+ "-t",
47+ "--tmp-obj-dir",
48+ help="Temporary dir for combining object files to static library.",
41 )49 )
42 50 
43 parser.add_argument(51 parser.add_argument(
44- "-o", "--output-file", help="Output static library of current customize operator project."52+ "-o",
53+ "--output-file",
54+ help="Output static library of current customize operator project.",
45 )55 )
46 56 
47 parser.add_argument(57 parser.add_argument(
48- "-r", "--remove-tmp-files", default="1", help="Whether to remove temporary files."58+ "-r",
59+ "--remove-tmp-files",
60+ default="1",
61+ help="Whether to remove temporary files.",
49 )62 )
50 63 
51- parser.add_argument(64+ parser.add_argument("-p", "--package-name", default="", help="package name.")
52- "-p", "--package-name", default="", help="package name."
53- )
54 return parser.parse_args()65 return parser.parse_args()
55 66 
56 67 
57def get_object_list(obj_file, ori_object_list):68def get_object_list(obj_file, ori_object_list):
58 with open(obj_file) as f:69 with open(obj_file) as f:
59 cur_obj_target = os.path.basename(obj_file)70 cur_obj_target = os.path.basename(obj_file)
60- cur_obj_target = cur_obj_target[:cur_obj_target.find(".")]71+ cur_obj_target = cur_obj_target[: cur_obj_target.find(".")]
61 72 
62 cur_obj_list = []73 cur_obj_list = []
63 for line in f:74 for line in f:
@@ -74,7 +85,9 @@ def collect_object_from_files(object_files_dir, tmp_obj_dir):
74 raise RuntimeError("-o/--object-files is empty, please check and reset.")85 raise RuntimeError("-o/--object-files is empty, please check and reset.")
75 86 
76 if os.path.isfile(tmp_obj_dir):87 if os.path.isfile(tmp_obj_dir):
77- raise RuntimeError("-t/--tmp-obj-dir is an existing file, is must be a directory, please check and reset.")88+ raise RuntimeError(
89+ "-t/--tmp-obj-dir is an existing file, is must be a directory, please check and reset."
90+ )
78 91 
79 if os.path.exists(tmp_obj_dir):92 if os.path.exists(tmp_obj_dir):
80 shutil.rmtree(tmp_obj_dir)93 shutil.rmtree(tmp_obj_dir)
@@ -100,24 +113,29 @@ def collect_object_from_files(object_files_dir, tmp_obj_dir):
100 113 
101 114 
102def unpack_kernel_library(kernel_libs_dir, objects, tmp_obj_dir):115def unpack_kernel_library(kernel_libs_dir, objects, tmp_obj_dir):
103- 
104 kernel_libs_parts = []116 kernel_libs_parts = []
105- opregistry_pattern = kernel_libs_dir + f"/**/libopregistry.a"117+ opregistry_pattern = kernel_libs_dir + "/**/libopregistry.a"
106 opregistry_file_path = glob.glob(opregistry_pattern, recursive=True)118 opregistry_file_path = glob.glob(opregistry_pattern, recursive=True)
107 if len(opregistry_file_path) != 1:119 if len(opregistry_file_path) != 1:
108- raise RuntimeError(f"libopregistry.a not exist in {kernel_libs_dir}, please check and reset.")120+ raise RuntimeError(
121+ f"libopregistry.a not exist in {kernel_libs_dir}, please check and reset."
122+ )
109 kernel_libs_parts.append(opregistry_file_path[0])123 kernel_libs_parts.append(opregistry_file_path[0])
110 124 
111- kernels_pattern = kernel_libs_dir + f"/**/libkernels.a"125+ kernels_pattern = kernel_libs_dir + "/**/libkernels.a"
112 kernels_file_path = glob.glob(kernels_pattern, recursive=True)126 kernels_file_path = glob.glob(kernels_pattern, recursive=True)
113 if len(kernels_file_path) != 1:127 if len(kernels_file_path) != 1:
114- raise RuntimeError(f"libkernels.a not exist in {kernel_libs_dir}, please check and reset.")128+ raise RuntimeError(
129+ f"libkernels.a not exist in {kernel_libs_dir}, please check and reset."
130+ )
115 131 
116 kernel_libs_parts.append(kernels_file_path[0])132 kernel_libs_parts.append(kernels_file_path[0])
117 133 
118 for lib in kernel_libs_parts:134 for lib in kernel_libs_parts:
119 output = subprocess.check_output(["ar", "-t", lib])135 output = subprocess.check_output(["ar", "-t", lib])
120- objects += [x.strip() for x in output.decode("utf-8").split("\n") if x.strip() != ""]136+ objects += [
137+ x.strip() for x in output.decode("utf-8").split("\n") if x.strip() != ""
138+ ]
121 run_command(["ar", "x", lib], cwd=tmp_obj_dir)139 run_command(["ar", "x", lib], cwd=tmp_obj_dir)
122 140 
123 141 
@@ -132,9 +150,14 @@ def pack_static_library(output_file, objects, tmp_obj_dir, package_name):
132 step_size = 30150 step_size = 30
133 tmp_files = []151 tmp_files = []
134 for index in range(0, len(objects), step_size):152 for index in range(0, len(objects), step_size):
135- tmp_file = os.path.join(output_dir_path, package_name + f"_ascendc_tmp{index}.o")153+ tmp_file = os.path.join(
154+ output_dir_path, package_name + f"_ascendc_tmp{index}.o"
155+ )
136 tmp_files.append(tmp_file)156 tmp_files.append(tmp_file)
137- run_command(["ld", "-r", "-o", tmp_file] + objects[index: index + step_size], cwd=tmp_obj_dir)157+ run_command(
158+ ["ld", "-r", "-o", tmp_file] + objects[index : index + step_size],
159+ cwd=tmp_obj_dir,
160+ )
138 161 
139 run_command(["ld", "-r", "-o", output_obj_file] + tmp_files[:], cwd=tmp_obj_dir)162 run_command(["ld", "-r", "-o", output_obj_file] + tmp_files[:], cwd=tmp_obj_dir)
140 163 
@@ -150,7 +173,9 @@ def main():
150 173 
151 objects = collect_object_from_files(args.object_files_dir, args.tmp_obj_dir)174 objects = collect_object_from_files(args.object_files_dir, args.tmp_obj_dir)
152 unpack_kernel_library(args.kernel_libs_dir, objects, args.tmp_obj_dir)175 unpack_kernel_library(args.kernel_libs_dir, objects, args.tmp_obj_dir)
153- pack_static_library(args.output_file, objects, args.tmp_obj_dir, args.package_name)176+ pack_static_library(
177+ args.output_file, objects, args.tmp_obj_dir, args.package_name
178+ )
154 179 
155 if args.remove_tmp_files == "1":180 if args.remove_tmp_files == "1":
156 remove_temporary_files(args.tmp_obj_dir)181 remove_temporary_files(args.tmp_obj_dir)
@@ -17,11 +17,10 @@ import os
17 17 
18def args_parse():18def args_parse():
19 parser = argparse.ArgumentParser()19 parser = argparse.ArgumentParser()
20- parser.add_argument(20+ parser.add_argument("-i", "--ini-file", help="op info ini.")
21- "-i", "--ini-file", help="op info ini."
22- )
23 return parser.parse_args()21 return parser.parse_args()
24 22 
23+ 
25if __name__ == "__main__":24if __name__ == "__main__":
26 args = args_parse()25 args = args_parse()
27 op_config = configparser.ConfigParser()26 op_config = configparser.ConfigParser()
@@ -28,13 +28,13 @@ class OpConfig(NamedTuple):
28 28 
29 29 
30def load_json(json_file: str):30def load_json(json_file: str):
31- with open(json_file, encoding='utf-8') as file:31+ with open(json_file, encoding="utf-8") as file:
32 json_content = json.load(file)32 json_content = json.load(file)
33 return json_content33 return json_content
34 34 
35 35 
36def get_specified_suffix_file(root_dir, suffix):36def get_specified_suffix_file(root_dir, suffix):
37- specified_suffix = os.path.join(root_dir, '**/*{}'.format(suffix))37+ specified_suffix = os.path.join(root_dir, "**/*{}".format(suffix))
38 all_suffix_files = glob.glob(specified_suffix, recursive=True)38 all_suffix_files = glob.glob(specified_suffix, recursive=True)
39 return sorted(all_suffix_files)39 return sorted(all_suffix_files)
40 40 
@@ -46,33 +46,33 @@ def add_dict_key(dict_to_add, key, value):
46 46 
47 47 
48def correct_format_mode(format_mode):48def correct_format_mode(format_mode):
49- if format_mode == 'FormatDefault':49+ if format_mode == "FormatDefault":
50- return 'nd_agnostic'50+ return "nd_agnostic"
51- if format_mode == 'FormatAgnostic':51+ if format_mode == "FormatAgnostic":
52- return 'static_nd_agnostic'52+ return "static_nd_agnostic"
53- if format_mode == 'FormatFixed':53+ if format_mode == "FormatFixed":
54- return 'normal'54+ return "normal"
55 return format_mode55 return format_mode
56 56 
57 57 
58def get_input_or_output_config(in_or_out):58def get_input_or_output_config(in_or_out):
59 param_dict = {}59 param_dict = {}
60- name = in_or_out.get('name')60+ name = in_or_out.get("name")
61- index = in_or_out.get('index')61+ index = in_or_out.get("index")
62- param_type = in_or_out.get('paramType')62+ param_type = in_or_out.get("paramType")
63 63 
64- format_match_mode = in_or_out.get('format_match_mode')64+ format_match_mode = in_or_out.get("format_match_mode")
65 format_mode = correct_format_mode(format_match_mode)65 format_mode = correct_format_mode(format_match_mode)
66 66 
67- dtype_mode = in_or_out.get('dtype_match_mode')67+ dtype_mode = in_or_out.get("dtype_match_mode")
68- if dtype_mode == 'DtypeByte':68+ if dtype_mode == "DtypeByte":
69- dtype_mode = 'bit'69+ dtype_mode = "bit"
70 70 
71- add_dict_key(param_dict, 'name', name)71+ add_dict_key(param_dict, "name", name)
72- add_dict_key(param_dict, 'index', index)72+ add_dict_key(param_dict, "index", index)
73- add_dict_key(param_dict, 'paramType', param_type)73+ add_dict_key(param_dict, "paramType", param_type)
74- add_dict_key(param_dict, 'dtypeMode', dtype_mode)74+ add_dict_key(param_dict, "dtypeMode", dtype_mode)
75- add_dict_key(param_dict, 'formatMode', format_mode)75+ add_dict_key(param_dict, "formatMode", format_mode)
76 return param_dict76 return param_dict
77 77 
78 78 
@@ -97,10 +97,10 @@ def gen_attrs_config(attrs):
97 attrs_list = []97 attrs_list = []
98 for attr in attrs:98 for attr in attrs:
99 attrs_dict = {}99 attrs_dict = {}
100- name = attr.get('name')100+ name = attr.get("name")
101- mode = attr.get('mode')101+ mode = attr.get("mode")
102- add_dict_key(attrs_dict, 'name', name)102+ add_dict_key(attrs_dict, "name", name)
103- add_dict_key(attrs_dict, 'mode', mode)103+ add_dict_key(attrs_dict, "mode", mode)
104 attrs_list.append(attrs_dict)104 attrs_list.append(attrs_dict)
105 return attrs_list105 return attrs_list
106 106 
@@ -108,116 +108,138 @@ def gen_attrs_config(attrs):
108def get_params_config(support_info):108def get_params_config(support_info):
109 params_dict = {}109 params_dict = {}
110 110 
111- inputs = support_info.get('inputs')111+ inputs = support_info.get("inputs")
112 inputs_list = get_inputs_or_outputs_config(inputs)112 inputs_list = get_inputs_or_outputs_config(inputs)
113- params_dict['inputs'] = inputs_list113+ params_dict["inputs"] = inputs_list
114 114 
115- outputs = support_info.get('outputs')115+ outputs = support_info.get("outputs")
116 outputs_list = get_inputs_or_outputs_config(outputs)116 outputs_list = get_inputs_or_outputs_config(outputs)
117- params_dict['outputs'] = outputs_list117+ params_dict["outputs"] = outputs_list
118 118 
119- attrs = support_info.get('attrs')119+ attrs = support_info.get("attrs")
120 if attrs is not None:120 if attrs is not None:
121 attrs_list = gen_attrs_config(attrs)121 attrs_list = gen_attrs_config(attrs)
122- params_dict['attrs'] = attrs_list122+ params_dict["attrs"] = attrs_list
123 123 
124 return params_dict124 return params_dict
125 125 
126 126 
127def add_simplified_config(op_info, binary_info_config, config):127def add_simplified_config(op_info, binary_info_config, config):
128- simplified_key = op_info.support_info.get('simplifiedKey')128+ simplified_key = op_info.support_info.get("simplifiedKey")
129 129 
130- json_path = op_info.obj_file.split('.')[0] + '.json'130+ json_path = op_info.obj_file.split(".")[0] + ".json"
131 131 
132 simple_cfg = config.get(binary_info_config)132 simple_cfg = config.get(binary_info_config)
133 op_cfg = simple_cfg.get(op_info.op_type)133 op_cfg = simple_cfg.get(op_info.op_type)
134 if not op_cfg:134 if not op_cfg:
135- op_cfg = {'dynamicRankSupport': True}135+ op_cfg = {"dynamicRankSupport": True}
136 136 
137- simplified_key_mode = op_info.support_info.get('simplifiedKeyMode')137+ simplified_key_mode = op_info.support_info.get("simplifiedKeyMode")
138- add_dict_key(op_cfg, 'simplifiedKeyMode', simplified_key_mode)138+ add_dict_key(op_cfg, "simplifiedKeyMode", simplified_key_mode)
139 139 
140- optional_input_mode = op_info.support_info.get('optionalInputMode')140+ optional_input_mode = op_info.support_info.get("optionalInputMode")
141- optional_output_mode = op_info.support_info.get('optionalOutputMode')141+ optional_output_mode = op_info.support_info.get("optionalOutputMode")
142- add_dict_key(op_cfg, 'optionalInputMode', optional_input_mode)142+ add_dict_key(op_cfg, "optionalInputMode", optional_input_mode)
143 if optional_output_mode is not None:143 if optional_output_mode is not None:
144- add_dict_key(op_cfg, 'optionalOutputMode', optional_output_mode)144+ add_dict_key(op_cfg, "optionalOutputMode", optional_output_mode)
145 145 
146 params_info = get_params_config(op_info.support_info)146 params_info = get_params_config(op_info.support_info)
147- op_cfg['params'] = params_info147+ op_cfg["params"] = params_info
148- op_cfg['binaryList'] = []148+ op_cfg["binaryList"] = []
149 simple_cfg[op_info.op_type] = op_cfg149 simple_cfg[op_info.op_type] = op_cfg
150 150 
151- bin_list = op_cfg.get('binaryList')151+ bin_list = op_cfg.get("binaryList")
152 if op_info.core_type == 0 and op_info.task_ration == "tilingKey":152 if op_info.core_type == 0 and op_info.task_ration == "tilingKey":
153- bin_list.append({'coreType': op_info.core_type, 'simplifiedKey': simplified_key,153+ bin_list.append(
154- 'multiKernelType': 1, 'binPath': op_info.obj_file, 'jsonPath': json_path})154+ {
155+ "coreType": op_info.core_type,
156+ "simplifiedKey": simplified_key,
157+ "multiKernelType": 1,
158+ "binPath": op_info.obj_file,
159+ "jsonPath": json_path,
160+ }
161+ )
155 else:162 else:
156- bin_list.append({'coreType': op_info.core_type, 'simplifiedKey': simplified_key,163+ bin_list.append(
157- 'binPath': op_info.obj_file, 'jsonPath': json_path})164+ {
165+ "coreType": op_info.core_type,
166+ "simplifiedKey": simplified_key,
167+ "binPath": op_info.obj_file,
168+ "jsonPath": json_path,
169+ }
170+ )
158 171 
159 172 
160def add_op_config(op_file, bin_info, config):173def add_op_config(op_file, bin_info, config):
161 op_cfg = config.get(op_file)174 op_cfg = config.get(op_file)
162 if not op_cfg:175 if not op_cfg:
163- op_cfg = {'binList': []}176+ op_cfg = {"binList": []}
164 config[op_file] = op_cfg177 config[op_file] = op_cfg
165- op_cfg.get('binList').append(bin_info)178+ op_cfg.get("binList").append(bin_info)
166 179 
167 180 
168def gen_ops_config(json_file, soc, binary_info_config, config):181def gen_ops_config(json_file, soc, binary_info_config, config):
169- core_type_map = {'MIX': 0, 'AiCore': 1, 'VectorCore': 2, 'MIX_AICORE': 3, 'MIX_VECTOR_CORE': 4, 'MIX_AIV': 4}182+ core_type_map = {
183+ "MIX": 0,
184+ "AiCore": 1,
185+ "VectorCore": 2,
186+ "MIX_AICORE": 3,
187+ "MIX_VECTOR_CORE": 4,
188+ "MIX_AIV": 4,
189+ }
170 contents = load_json(json_file)190 contents = load_json(json_file)
171- if ('binFileName' not in contents) or ('supportInfo' not in contents):191+ if ("binFileName" not in contents) or ("supportInfo" not in contents):
172 return192 return
173 json_base_name = os.path.basename(json_file)193 json_base_name = os.path.basename(json_file)
174 op_dir = os.path.basename(os.path.dirname(json_file))194 op_dir = os.path.basename(os.path.dirname(json_file))
175 195 
176- support_info = contents.get('supportInfo')196+ support_info = contents.get("supportInfo")
177- bin_name = contents.get('binFileName')197+ bin_name = contents.get("binFileName")
178- bin_suffix = contents.get('binFileSuffix')198+ bin_suffix = contents.get("binFileSuffix")
179 core_type = contents.get("coreType")199 core_type = contents.get("coreType")
180 task_ration = contents.get("taskRation")200 task_ration = contents.get("taskRation")
181 core_type = core_type_map.get(core_type, -1)201 core_type = core_type_map.get(core_type, -1)
182- if core_type == -1 and soc != 'ascend310b':202+ if core_type == -1 and soc != "ascend310b":
183 raise Exception("[ERROR]: must set coreType in json when soc version is {soc}.")203 raise Exception("[ERROR]: must set coreType in json when soc version is {soc}.")
184 204 
185 bin_file_name = bin_name + bin_suffix205 bin_file_name = bin_name + bin_suffix
186- op_type = bin_name.split('_')[0]206+ op_type = bin_name.split("_")[0]
187- op_file = op_dir + '.json'207+ op_file = op_dir + ".json"
188 bin_info = {}208 bin_info = {}
189 209 
190- add_dict_key(bin_info, 'implMode', support_info.get('implMode'))210+ add_dict_key(bin_info, "implMode", support_info.get("implMode"))
191- add_dict_key(bin_info, 'int64Mode', support_info.get('int64Mode'))211+ add_dict_key(bin_info, "int64Mode", support_info.get("int64Mode"))
192- add_dict_key(bin_info, 'simplifiedKeyMode', support_info.get('simplifiedKeyMode'))212+ add_dict_key(bin_info, "simplifiedKeyMode", support_info.get("simplifiedKeyMode"))
193 213 
194- simplified_key = support_info.get('simplifiedKey')214+ simplified_key = support_info.get("simplifiedKey")
195 if simplified_key is not None:215 if simplified_key is not None:
196- bin_info['simplifiedKey'] = simplified_key216+ bin_info["simplifiedKey"] = simplified_key
197 obj_file = os.path.join(soc, op_dir, bin_file_name)217 obj_file = os.path.join(soc, op_dir, bin_file_name)
198 op_info = OpConfig(218 op_info = OpConfig(
199- op_type=op_type,219+ op_type=op_type,
200- support_info=support_info,220+ support_info=support_info,
201- core_type=core_type,221+ core_type=core_type,
202- task_ration=task_ration,222+ task_ration=task_ration,
203- obj_file=obj_file,223+ obj_file=obj_file,
204- )224+ )
205 add_simplified_config(op_info, binary_info_config, config)225 add_simplified_config(op_info, binary_info_config, config)
206 226 
207- add_dict_key(bin_info, 'dynamicParamMode', support_info.get('dynamicParamMode'))227+ add_dict_key(bin_info, "dynamicParamMode", support_info.get("dynamicParamMode"))
208- bin_info['staticKey'] = support_info.get('staticKey')228+ bin_info["staticKey"] = support_info.get("staticKey")
209- bin_info['inputs'] = support_info.get('inputs')229+ bin_info["inputs"] = support_info.get("inputs")
210- bin_info['outputs'] = support_info.get('outputs')230+ bin_info["outputs"] = support_info.get("outputs")
211- if support_info.get('attrs'):231+ if support_info.get("attrs"):
212- bin_info['attrs'] = support_info.get('attrs')232+ bin_info["attrs"] = support_info.get("attrs")
213 233 
214- add_dict_key(bin_info, 'opMode', support_info.get('opMode'))234+ add_dict_key(bin_info, "opMode", support_info.get("opMode"))
215- add_dict_key(bin_info, 'optionalInputMode', support_info.get('optionalInputMode'))235+ add_dict_key(bin_info, "optionalInputMode", support_info.get("optionalInputMode"))
216- add_dict_key(bin_info, 'deterministic', support_info.get('deterministic'))236+ add_dict_key(bin_info, "deterministic", support_info.get("deterministic"))
217- if support_info.get('optionalOutputMode') is not None:237+ if support_info.get("optionalOutputMode") is not None:
218- add_dict_key(bin_info, 'optionalOutputMode', support_info.get('optionalOutputMode'))238+ add_dict_key(
239+ bin_info, "optionalOutputMode", support_info.get("optionalOutputMode")
240+ )
219 241 
220- bin_info['binInfo'] = {'jsonFilePath': os.path.join(soc, op_dir, json_base_name)}242+ bin_info["binInfo"] = {"jsonFilePath": os.path.join(soc, op_dir, json_base_name)}
221 add_op_config(op_file, bin_info, config)243 add_op_config(op_file, bin_info, config)
222 244 
223 245 
@@ -237,16 +259,15 @@ def check_single_op_is_void(root_dir, skip_error=False):
237def write_jsons(out_dir, file_list, config):259def write_jsons(out_dir, file_list, config):
238 for json_name in file_list:260 for json_name in file_list:
239 json_file = os.path.join(out_dir, json_name)261 json_file = os.path.join(out_dir, json_name)
240- with os.fdopen(os.open(json_file, const_var.WFLAGS, const_var.WMODES), 'w') as fd:262+ with os.fdopen(
241- json.dump(config.get(json_name), fd, indent=' ')263+ os.open(json_file, const_var.WFLAGS, const_var.WMODES), "w"
264+ ) as fd:
265+ json.dump(config.get(json_name), fd, indent=" ")
242 266 
243 267 
244-def generate_operator_cfg_file(json_files,268+def generate_operator_cfg_file(
245- binary_info_config,269+ json_files, binary_info_config, soc, out_dir, gen_json_status
246- soc,270+):
247- out_dir,
248- gen_json_status):
249- 
250 if not json_files:271 if not json_files:
251 return272 return
252 273 
@@ -259,7 +280,9 @@ def generate_operator_cfg_file(json_files,
259 gen_ops_config(_json, soc, binary_info_config, config)280 gen_ops_config(_json, soc, binary_info_config, config)
260 281 
261 if gen_json_status == "single_json":282 if gen_json_status == "single_json":
262- file_list = [json_file for json_file in config.keys() if json_file != binary_info_config]283+ file_list = [
284+ json_file for json_file in config.keys() if json_file != binary_info_config
285+ ]
263 elif gen_json_status == "summary_json":286 elif gen_json_status == "summary_json":
264 file_list = [binary_info_config]287 file_list = [binary_info_config]
265 else:288 else:
@@ -268,14 +291,15 @@ def generate_operator_cfg_file(json_files,
268 write_jsons(out_dir, file_list, config)291 write_jsons(out_dir, file_list, config)
269 292 
270 293 
271-def gen_all_config(root_dir, soc, out_dir,294+def gen_all_config(
272- skip_binary_info_config, op_range="all", skip_error=False):295+ root_dir, soc, out_dir, skip_binary_info_config, op_range="all", skip_error=False
296+):
273 if op_range != "relocatable":297 if op_range != "relocatable":
274 flag = check_single_op_is_void(root_dir, skip_error)298 flag = check_single_op_is_void(root_dir, skip_error)
275 if flag is False:299 if flag is False:
276 return flag300 return flag
277- all_json_files = get_specified_suffix_file(root_dir, '.json')301+ all_json_files = get_specified_suffix_file(root_dir, ".json")
278- relocatable_json_files = get_specified_suffix_file(root_dir, '_relocatable.json')302+ relocatable_json_files = get_specified_suffix_file(root_dir, "_relocatable.json")
279 normal_json_files = list(set(all_json_files) - set(relocatable_json_files))303 normal_json_files = list(set(all_json_files) - set(relocatable_json_files))
280 os.makedirs(out_dir, exist_ok=True)304 os.makedirs(out_dir, exist_ok=True)
281 if op_range != "relocatable":305 if op_range != "relocatable":
@@ -298,14 +322,20 @@ def gen_all_config(root_dir, soc, out_dir,
298 # normal kernel322 # normal kernel
299 if op_range == "all" or op_range == "normal":323 if op_range == "all" or op_range == "normal":
300 binary_info_config = "binary_info_config.json"324 binary_info_config = "binary_info_config.json"
301- generate_operator_cfg_file(normal_json_files, binary_info_config,325+ generate_operator_cfg_file(
302- soc, out_dir, gen_normale_json)326+ normal_json_files, binary_info_config, soc, out_dir, gen_normale_json
327+ )
303 328 
304 # relocatable kernel329 # relocatable kernel
305 if op_range == "all" or op_range == "relocatable":330 if op_range == "all" or op_range == "relocatable":
306 binary_info_config = "relocatable_kernel_info_config.json"331 binary_info_config = "relocatable_kernel_info_config.json"
307- generate_operator_cfg_file(relocatable_json_files, binary_info_config,332+ generate_operator_cfg_file(
308- soc, out_dir, gen_relocatable_json)333+ relocatable_json_files,
334+ binary_info_config,
335+ soc,
336+ out_dir,
337+ gen_relocatable_json,
338+ )
309 return True339 return True
310 340 
311 341 
@@ -329,37 +359,37 @@ def gen_all_soc_config(all_path):
329 359 
330def args_parse():360def args_parse():
331 parser = argparse.ArgumentParser()361 parser = argparse.ArgumentParser()
332- parser.add_argument('-p',362+ parser.add_argument(
333- '--path',363+ "-p",
334- nargs='?',364+ "--path",
335- required=True,365+ nargs="?",
336- help='Parse the path of the json file.')366+ required=True,
367+ help="Parse the path of the json file.",
368+ )
337 369 
338- parser.add_argument('-s',370+ parser.add_argument(
339- '--soc',371+ "-s", "--soc", nargs="?", required=True, help="Parse the soc_version of ops."
340- nargs='?',372+ )
341- required=True,
342- help='Parse the soc_version of ops.')
343 373 
344- parser.add_argument('-o',374+ parser.add_argument("-o", "--out", nargs="?", help="Output directory.")
345- '--out',
346- nargs='?',
347- help='Output directory.')
348 375 
349- parser.add_argument('--skip-binary-info-config',376+ parser.add_argument(
350- action='store_true',377+ "--skip-binary-info-config",
351- help='binary_info_config.json file is not parsed.')378+ action="store_true",
379+ help="binary_info_config.json file is not parsed.",
380+ )
352 381 
353- parser.add_argument('--op-range',382+ parser.add_argument(
354- type=str,383+ "--op-range",
355- choices=["all", "normal", "relocatable"],384+ type=str,
356- default='all',385+ choices=["all", "normal", "relocatable"],
357- help='all operators/normal operators/relocatable operators.')386+ default="all",
387+ help="all operators/normal operators/relocatable operators.",
388+ )
358 389 
359- parser.add_argument('--skip-error',390+ parser.add_argument(
360- nargs='?',391+ "--skip-error", nargs="?", default=False, help="skip error msg."
361- default=False,392+ )
362- help='skip error msg.')
363 393 
364 return parser.parse_args()394 return parser.parse_args()
365 395 
@@ -371,11 +401,17 @@ def main():
371 else:401 else:
372 out_dir = args.out402 out_dir = args.out
373 403 
374- flag = gen_all_config(args.path, args.soc, out_dir,404+ flag = gen_all_config(
375- args.skip_binary_info_config, args.op_range, args.skip_error)405+ args.path,
406+ args.soc,
407+ out_dir,
408+ args.skip_binary_info_config,
409+ args.op_range,
410+ args.skip_error,
411+ )
376 if flag is False:412 if flag is False:
377 sys.exit(1)413 sys.exit(1)
378 414 
379 415 
380-if __name__ == '__main__':416+if __name__ == "__main__":
381 main()417 main()
@@ -11,7 +11,6 @@
11# ----------------------------------------------------------------------------------------------------------11# ----------------------------------------------------------------------------------------------------------
12 12 
13import os13import os
14-import sys
15import subprocess14import subprocess
16import json15import json
17import glob16import glob
@@ -33,8 +32,12 @@ class PackKernel:
33 try:32 try:
34 os.makedirs(self.out_path, exist_ok=True)33 os.makedirs(self.out_path, exist_ok=True)
35 except Exception as e:34 except Exception as e:
36- LogUtil.print_compile_log("", f"make {self.out_path} error: {e}!",35+ LogUtil.print_compile_log(
37- AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC)36+ "",
37+ f"make {self.out_path} error: {e}!",
38+ AscendCLogLevel.LOG_ERROR,
39+ LogUtil.Option.NON_SOC,
40+ )
38 41 
39 def load_json(self: any, json_file: str):42 def load_json(self: any, json_file: str):
40 with open(json_file, encoding="utf-8") as file:43 with open(json_file, encoding="utf-8") as file:
@@ -48,14 +51,29 @@ class PackKernel:
48 def ascendc_gen_object(self: any, in_file: str, soc: str):51 def ascendc_gen_object(self: any, in_file: str, soc: str):
49 sym = self.get_symbol("_binary_" + in_file)52 sym = self.get_symbol("_binary_" + in_file)
50 out_file = os.path.join(self.out_path, sym + ".o")53 out_file = os.path.join(self.out_path, sym + ".o")
51- #ascend610lite only support aarch6454+ # ascend610lite only support aarch64
52- if soc == 'ascend610lite':55+ if soc == "ascend610lite":
53 try:56 try:
54- subprocess.run(['llvm-objcopy', '--input-target', 'binary', '--output-target', 'elf64-littleaarch64',57+ subprocess.run(
55- '--binary-architecture', 'aarch64', in_file, out_file])58+ [
56- except Exception as e:59+ "llvm-objcopy",
57- LogUtil.print_compile_log("", " ascend610lite execute objcopy fail!",60+ "--input-target",
58- AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC)61+ "binary",
62+ "--output-target",
63+ "elf64-littleaarch64",
64+ "--binary-architecture",
65+ "aarch64",
66+ in_file,
67+ out_file,
68+ ]
69+ )
70+ except Exception:
71+ LogUtil.print_compile_log(
72+ "",
73+ " ascend610lite execute objcopy fail!",
74+ AscendCLogLevel.LOG_ERROR,
75+ LogUtil.Option.NON_SOC,
76+ )
59 return None77 return None
60 return [sym + "_start", sym + "_end"]78 return [sym + "_start", sym + "_end"]
61 uname = os.popen("uname -m").read().strip()79 uname = os.popen("uname -m").read().strip()
@@ -65,16 +83,42 @@ class PackKernel:
65 target_platform = uname83 target_platform = uname
66 try:84 try:
67 if target_platform == "x86_64":85 if target_platform == "x86_64":
68- subprocess.run(['llvm-objcopy', '--input-target', 'binary', '--output-target', 'elf64-x86-64',86+ subprocess.run(
69- '--binary-architecture', 'i386', in_file, out_file])87+ [
88+ "llvm-objcopy",
89+ "--input-target",
90+ "binary",
91+ "--output-target",
92+ "elf64-x86-64",
93+ "--binary-architecture",
94+ "i386",
95+ in_file,
96+ out_file,
97+ ]
98+ )
70 elif target_platform == "aarch64":99 elif target_platform == "aarch64":
71- subprocess.run(['llvm-objcopy', '--input-target', 'binary', '--output-target', 'elf64-littleaarch64',100+ subprocess.run(
72- '--binary-architecture', 'aarch64', in_file, out_file])101+ [
102+ "llvm-objcopy",
103+ "--input-target",
104+ "binary",
105+ "--output-target",
106+ "elf64-littleaarch64",
107+ "--binary-architecture",
108+ "aarch64",
109+ in_file,
110+ out_file,
111+ ]
112+ )
73 else:113 else:
74- subprocess.run(['echo', 'unsupported environment!'])114+ subprocess.run(["echo", "unsupported environment!"])
75 except Exception as e:115 except Exception as e:
76- LogUtil.print_compile_log("", f"{target_platform} execute objcopy error: {e}!",116+ LogUtil.print_compile_log(
77- AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC)117+ "",
118+ f"{target_platform} execute objcopy error: {e}!",
119+ AscendCLogLevel.LOG_ERROR,
120+ LogUtil.Option.NON_SOC,
121+ )
78 return None122 return None
79 return [sym + "_start", sym + "_end"]123 return [sym + "_start", sym + "_end"]
80 124 
@@ -125,10 +169,12 @@ class PackKernel:
125 def ascendc_gen_header(self: any):169 def ascendc_gen_header(self: any):
126 for op_type in self.op_info.keys():170 for op_type in self.op_info.keys():
127 op_obj = self.op_info.get(op_type)171 op_obj = self.op_info.get(op_type)
128- macro_op = "#define {}_OP_RESOURCES std::make_tuple<std::vector<void *>, \\\n" \172+ macro_op = (
129- " std::map<ge::AscendString, std::vector<std::tuple<const uint8_t *, const uint8_t *>>>, \\\n" \173+ "#define {}_OP_RESOURCES std::make_tuple<std::vector<void *>, \\\n"
130- " std::vector<std::tuple<const uint8_t *, const uint8_t *>>>({{{}}}, \\\n".format(174+ " std::map<ge::AscendString, std::vector<std::tuple<const uint8_t *, const uint8_t *>>>, \\\n"
131- op_type, ", ".join(op_obj.get("op_fun"))175+ " std::vector<std::tuple<const uint8_t *, const uint8_t *>>>({{{}}}, \\\n".format(
176+ op_type, ", ".join(op_obj.get("op_fun"))
177+ )
132 )178 )
133 op_bin = op_obj.get("op_bin")179 op_bin = op_obj.get("op_bin")
134 socs_res = []180 socs_res = []
@@ -151,9 +197,15 @@ class PackKernel:
151 macro_str = '#define {}_RESOURCES {{{{"{}", {}}}}}'.format(197 macro_str = '#define {}_RESOURCES {{{{"{}", {}}}}}'.format(
152 op_type, op_type, "{}_OP_RESOURCES".format(op_type)198 op_type, op_type, "{}_OP_RESOURCES".format(op_type)
153 )199 )
154- var_str = ("extern gert::OpImplRegisterV2 op_impl_register_optiling_{};\n".format(op_type))200+ var_str = (
201+ "extern gert::OpImplRegisterV2 op_impl_register_optiling_{};\n".format(
202+ op_type
203+ )
204+ )
155 if len(op_syms) > 0:205 if len(op_syms) > 0:
156- var_str += ('extern uint8_t ' + ";\nextern uint8_t ".join(op_syms) + ";\n")206+ var_str += (
207+ "extern uint8_t " + ";\nextern uint8_t ".join(op_syms) + ";\n"
208+ )
157 head_file = os.path.join(self.out_path, "{}_op_resource.h".format(op_type))209 head_file = os.path.join(self.out_path, "{}_op_resource.h".format(op_type))
158 try:210 try:
159 with os.fdopen(211 with os.fdopen(
@@ -166,12 +218,16 @@ class PackKernel:
166 fd.write('#include "graph/ascend_string.h"\n')218 fd.write('#include "graph/ascend_string.h"\n')
167 fd.write('#include "register/op_impl_registry.h"\n\n')219 fd.write('#include "register/op_impl_registry.h"\n\n')
168 fd.write(var_str)220 fd.write(var_str)
169- fd.write('\n')221+ fd.write("\n")
170 fd.write(macro_op)222 fd.write(macro_op)
171 fd.write(macro_str)223 fd.write(macro_str)
172 except Exception as e:224 except Exception as e:
173- LogUtil.print_compile_log("", f"{op_type}_op_resource.h create error: {e}!",225+ LogUtil.print_compile_log(
174- AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC)226+ "",
227+ f"{op_type}_op_resource.h create error: {e}!",
228+ AscendCLogLevel.LOG_ERROR,
229+ LogUtil.Option.NON_SOC,
230+ )
175 231 
176 def ascendc_gen_lib(self: any):232 def ascendc_gen_lib(self: any):
177 out_lib = os.path.join(self.out_path, "libkernels.a")233 out_lib = os.path.join(self.out_path, "libkernels.a")
@@ -184,11 +240,15 @@ class PackKernel:
184 sub_objs = objs[start : start + batch_size]240 sub_objs = objs[start : start + batch_size]
185 start += batch_size241 start += batch_size
186 try:242 try:
187- subprocess.run(['ar', 'qc', out_lib] + sub_objs)243+ subprocess.run(["ar", "qc", out_lib] + sub_objs)
188- subprocess.run(['ranlib', out_lib])244+ subprocess.run(["ranlib", out_lib])
189 except Exception as e:245 except Exception as e:
190- LogUtil.print_compile_log("", f"execute ar/ranlib command error: {e}!",246+ LogUtil.print_compile_log(
191- AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC)247+ "",
248+ f"execute ar/ranlib command error: {e}!",
249+ AscendCLogLevel.LOG_ERROR,
250+ LogUtil.Option.NON_SOC,
251+ )
192 252 
193 def ascendc_gen_opsinfo(self: any):253 def ascendc_gen_opsinfo(self: any):
194 ascendc_ops_config.gen_all_soc_config(self.in_path)254 ascendc_ops_config.gen_all_soc_config(self.in_path)
@@ -203,10 +263,18 @@ def args_parse():
203 "-o", "--output-path", nargs="?", help="Output path of compile result."263 "-o", "--output-path", nargs="?", help="Output path of compile result."
204 )264 )
205 parser.add_argument(265 parser.add_argument(
206- "-l", "--enable-library", nargs="?", default=None, help="Whether library is enabled."266+ "-l",
267+ "--enable-library",
268+ nargs="?",
269+ default=None,
270+ help="Whether library is enabled.",
207 )271 )
208 parser.add_argument(272 parser.add_argument(
209- "-p", "--platform", nargs="?", default=None, help="target platform is x86_64 or aarch64."273+ "-p",
274+ "--platform",
275+ nargs="?",
276+ default=None,
277+ help="target platform is x86_64 or aarch64.",
210 )278 )
211 return parser.parse_args()279 return parser.parse_args()
212 280 
@@ -11,7 +11,6 @@
11# ----------------------------------------------------------------------------------------------------------11# ----------------------------------------------------------------------------------------------------------
12 12 
13import os13import os
14-import sys
15import subprocess14import subprocess
16import glob15import glob
17import argparse16import argparse
@@ -35,28 +34,44 @@ class PackKernel:
35 self.platform = args.platform34 self.platform = args.platform
36 self.op_info = {}35 self.op_info = {}
37 self.file_info = {}36 self.file_info = {}
38- if (os.path.exists(self.copy_path)):37+ if os.path.exists(self.copy_path):
39 try:38 try:
40 shutil.rmtree(self.copy_path)39 shutil.rmtree(self.copy_path)
41- except OSError as e:40+ except OSError:
42- LogUtil.print_compile_log("", f"remove {self.copy_path} error!",41+ LogUtil.print_compile_log(
43- AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC)42+ "",
44- if (os.path.exists(self.out_path)):43+ f"remove {self.copy_path} error!",
44+ AscendCLogLevel.LOG_ERROR,
45+ LogUtil.Option.NON_SOC,
46+ )
47+ if os.path.exists(self.out_path):
45 try:48 try:
46 shutil.rmtree(self.out_path)49 shutil.rmtree(self.out_path)
47- except OSError as e:50+ except OSError:
48- LogUtil.print_compile_log("", f"remove {self.out_path} error!",51+ LogUtil.print_compile_log(
49- AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC)52+ "",
53+ f"remove {self.out_path} error!",
54+ AscendCLogLevel.LOG_ERROR,
55+ LogUtil.Option.NON_SOC,
56+ )
50 try:57 try:
51 os.makedirs(self.copy_path, exist_ok=True)58 os.makedirs(self.copy_path, exist_ok=True)
52 except Exception as e:59 except Exception as e:
53- LogUtil.print_compile_log("", f"make {self.copy_path} error: {e}!",60+ LogUtil.print_compile_log(
54- AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC)61+ "",
62+ f"make {self.copy_path} error: {e}!",
63+ AscendCLogLevel.LOG_ERROR,
64+ LogUtil.Option.NON_SOC,
65+ )
55 try:66 try:
56 os.makedirs(self.out_path, exist_ok=True)67 os.makedirs(self.out_path, exist_ok=True)
57 except Exception as e:68 except Exception as e:
58- LogUtil.print_compile_log("", f"make {self.out_path} error: {e}!",69+ LogUtil.print_compile_log(
59- AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC)70+ "",
71+ f"make {self.out_path} error: {e}!",
72+ AscendCLogLevel.LOG_ERROR,
73+ LogUtil.Option.NON_SOC,
74+ )
60 75 
61 def get_symbol(self: any, name: str):76 def get_symbol(self: any, name: str):
62 name = name.replace("/", "_")77 name = name.replace("/", "_")
@@ -68,14 +83,29 @@ class PackKernel:
68 path = vname + "/" + path83 path = vname + "/" + path
69 sym = self.get_symbol("_binary_" + in_file)84 sym = self.get_symbol("_binary_" + in_file)
70 out_file = os.path.join(self.out_path, sym + ".o")85 out_file = os.path.join(self.out_path, sym + ".o")
71- #ascend610lite only support aarch6486+ # ascend610lite only support aarch64
72 if path.find("ascend610lite") != -1:87 if path.find("ascend610lite") != -1:
73 try:88 try:
74- subprocess.run(['llvm-objcopy', '--input-target', 'binary', '--output-target', 'elf64-littleaarch64',89+ subprocess.run(
75- '--binary-architecture', 'aarch64', in_file, out_file])90+ [
76- except Exception as e:91+ "llvm-objcopy",
77- LogUtil.print_compile_log("", " ascend610lite execute objcopy fail!",92+ "--input-target",
78- AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC)93+ "binary",
94+ "--output-target",
95+ "elf64-littleaarch64",
96+ "--binary-architecture",
97+ "aarch64",
98+ in_file,
99+ out_file,
100+ ]
101+ )
102+ except Exception:
103+ LogUtil.print_compile_log(
104+ "",
105+ " ascend610lite execute objcopy fail!",
106+ AscendCLogLevel.LOG_ERROR,
107+ LogUtil.Option.NON_SOC,
108+ )
79 return None109 return None
80 return [sym + "_start", sym + "_end"]110 return [sym + "_start", sym + "_end"]
81 111 
@@ -86,16 +116,42 @@ class PackKernel:
86 target_platform = uname116 target_platform = uname
87 try:117 try:
88 if target_platform == "x86_64":118 if target_platform == "x86_64":
89- subprocess.run(['llvm-objcopy', '--input-target', 'binary', '--output-target', 'elf64-x86-64',119+ subprocess.run(
90- '--binary-architecture', 'i386', in_file, out_file])120+ [
121+ "llvm-objcopy",
122+ "--input-target",
123+ "binary",
124+ "--output-target",
125+ "elf64-x86-64",
126+ "--binary-architecture",
127+ "i386",
128+ in_file,
129+ out_file,
130+ ]
131+ )
91 elif target_platform == "aarch64":132 elif target_platform == "aarch64":
92- subprocess.run(['llvm-objcopy', '--input-target', 'binary', '--output-target', 'elf64-littleaarch64',133+ subprocess.run(
93- '--binary-architecture', 'aarch64', in_file, out_file])134+ [
135+ "llvm-objcopy",
136+ "--input-target",
137+ "binary",
138+ "--output-target",
139+ "elf64-littleaarch64",
140+ "--binary-architecture",
141+ "aarch64",
142+ in_file,
143+ out_file,
144+ ]
145+ )
94 else:146 else:
95- subprocess.run(['echo', 'unsupported environment!'])147+ subprocess.run(["echo", "unsupported environment!"])
96 except Exception as e:148 except Exception as e:
97- LogUtil.print_compile_log("", f"{target_platform} execute objcopy error: {e}!",149+ LogUtil.print_compile_log(
98- AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC)150+ "",
151+ f"{target_platform} execute objcopy error: {e}!",
152+ AscendCLogLevel.LOG_ERROR,
153+ LogUtil.Option.NON_SOC,
154+ )
99 return None155 return None
100 return [sym + "_start", sym + "_end"]156 return [sym + "_start", sym + "_end"]
101 157 
@@ -123,7 +179,9 @@ class PackKernel:
123 op_info[file_name] = []179 op_info[file_name] = []
124 path, filename = os.path.split(op_cfgs[file_name])180 path, filename = os.path.split(op_cfgs[file_name])
125 op_info[file_name].append(os.path.join(self.vendor_name, path))181 op_info[file_name].append(os.path.join(self.vendor_name, path))
126- op_info[file_name].append(self.ascendc_gen_object(op_cfgs[file_name], path, self.vendor_name))182+ op_info[file_name].append(
183+ self.ascendc_gen_object(op_cfgs[file_name], path, self.vendor_name)
184+ )
127 self.op_info = op_info185 self.op_info = op_info
128 186 
129 def ascendc_gen_register(self, macro_op, var_str):187 def ascendc_gen_register(self, macro_op, var_str):
@@ -139,44 +197,68 @@ class PackKernel:
139 fd.write('#include "graph/ascend_string.h"\n')197 fd.write('#include "graph/ascend_string.h"\n')
140 fd.write('#include "register/op_bin_info.h"\n')198 fd.write('#include "register/op_bin_info.h"\n')
141 fd.write('#include "register/op_lib_register.h"\n')199 fd.write('#include "register/op_lib_register.h"\n')
142- fd.write('#include <dlfcn.h>\n')200+ fd.write("#include <dlfcn.h>\n")
143 fd.write('#include "../pkg_inc/base/dlog_pub.h"\n\n')201 fd.write('#include "../pkg_inc/base/dlog_pub.h"\n\n')
144 fd.write(var_str)202 fd.write(var_str)
145- fd.write('\n')203+ fd.write("\n")
146- fd.write("#define ASCENDC_MODULE_NAME static_cast<int32_t>(ASCENDCKERNEL)\n")204+ fd.write(
147- fd.write("#define LOG_ERROR(format, ...) \\\n")205+ "#define ASCENDC_MODULE_NAME static_cast<int32_t>(ASCENDCKERNEL)\n"
148- fd.write(" do { \\\n")206+ )
149- fd.write(" dlog_error(ASCENDC_MODULE_NAME, \"[%s] \" format \"\\n\", __FUNCTION__, ")207+ fd.write(
208+ "#define LOG_ERROR(format, ...) \\\n"
209+ )
210+ fd.write(
211+ " do { \\\n"
212+ )
213+ fd.write(
214+ ' dlog_error(ASCENDC_MODULE_NAME, "[%s] " format "\\n", __FUNCTION__, '
215+ )
150 fd.write("##__VA_ARGS__); \\\n")216 fd.write("##__VA_ARGS__); \\\n")
151 fd.write(" } while (0)\n")217 fd.write(" } while (0)\n")
152 fd.write("namespace {\n")218 fd.write("namespace {\n")
153 fd.write("uint32_t OpLibInitFunc(ge::AscendString& op_lib_path) {\n")219 fd.write("uint32_t OpLibInitFunc(ge::AscendString& op_lib_path) {\n")
154 fd.write(" static " + macro_op)220 fd.write(" static " + macro_op)
155- fd.write(" static ops::OpBinInfo g_binInfo(\"" + self.vendor_name + "\", __ascendc_op_info_")221+ fd.write(
222+ ' static ops::OpBinInfo g_binInfo("'
223+ + self.vendor_name
224+ + '", __ascendc_op_info_'
225+ )
156 fd.write(self.vendor_name + ");\n")226 fd.write(self.vendor_name + ");\n")
157 fd.write("Dl_info dlInfo;\n")227 fd.write("Dl_info dlInfo;\n")
158 fd.write("if (!dladdr((void*)&OpLibInitFunc, &dlInfo)) {\n")228 fd.write("if (!dladdr((void*)&OpLibInitFunc, &dlInfo)) {\n")
159- fd.write(" LOG_ERROR(\"dladdr failed: %s\", dlerror());\n")229+ fd.write(' LOG_ERROR("dladdr failed: %s", dlerror());\n')
160 fd.write(" return 1;\n")230 fd.write(" return 1;\n")
161 fd.write("}\n")231 fd.write("}\n")
162 fd.write("std::string targetPath = dlInfo.dli_fname;\n")232 fd.write("std::string targetPath = dlInfo.dli_fname;\n")
163 fd.write("if (!ops::OpBinInfo::Check(targetPath)) {\n")233 fd.write("if (!ops::OpBinInfo::Check(targetPath)) {\n")
164- fd.write(" LOG_ERROR(\"Path %s only support shared library, but it is not.\",targetPath.c_str());\n")234+ fd.write(
235+ ' LOG_ERROR("Path %s only support shared library, but it is not.",targetPath.c_str());\n'
236+ )
165 fd.write(" return 1;\n")237 fd.write(" return 1;\n")
166 fd.write("}\n")238 fd.write("}\n")
167 fd.write(" return g_binInfo.Generate(&op_lib_path, targetPath);\n")239 fd.write(" return g_binInfo.Generate(&op_lib_path, targetPath);\n")
168 fd.write("}\n")240 fd.write("}\n")
169- fd.write("REGISTER_OP_LIB(" + self.vendor_name + ").RegOpLibInit(OpLibInitFunc);\n")241+ fd.write(
242+ "REGISTER_OP_LIB("
243+ + self.vendor_name
244+ + ").RegOpLibInit(OpLibInitFunc);\n"
245+ )
170 fd.write("}\n")246 fd.write("}\n")
171 except Exception as e:247 except Exception as e:
172- LogUtil.print_compile_log("", f"custom_op_registry_V2.cpp create error: {e}!",248+ LogUtil.print_compile_log(
173- AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC)249+ "",
250+ f"custom_op_registry_V2.cpp create error: {e}!",
251+ AscendCLogLevel.LOG_ERROR,
252+ LogUtil.Option.NON_SOC,
253+ )
174 254 
175 def ascendc_gen_header(self: any):255 def ascendc_gen_header(self: any):
176 socs_res = []256 socs_res = []
177 var_str = ""257 var_str = ""
178- macro_op = ("std::vector<std::tuple<ge::AscendString, ge::AscendString, "258+ macro_op = (
179- "const uint8_t *, const uint8_t *>> __ascendc_op_info = \n")259+ "std::vector<std::tuple<ge::AscendString, ge::AscendString, "
260+ "const uint8_t *, const uint8_t *>> __ascendc_op_info = \n"
261+ )
180 for file_name in self.op_info.keys():262 for file_name in self.op_info.keys():
181 file_addr = self.op_info.get(file_name)263 file_addr = self.op_info.get(file_name)
182 soc_pairs = []264 soc_pairs = []
@@ -187,11 +269,13 @@ class PackKernel:
187 op_syms.append(pair_addr)269 op_syms.append(pair_addr)
188 pair_addr1 = "&" + pair_addr270 pair_addr1 = "&" + pair_addr
189 soc_pairs.append(pair_addr1)271 soc_pairs.append(pair_addr1)
190- soc_res += '{}, {}'.format(soc_pairs[0], soc_pairs[1])272+ soc_res += "{}, {}".format(soc_pairs[0], soc_pairs[1])
191 soc_res += "}, \n"273 soc_res += "}, \n"
192 socs_res.append(soc_res)274 socs_res.append(soc_res)
193 if len(op_syms) > 0:275 if len(op_syms) > 0:
194- var_str += "".join(["extern uint8_t {};\n".format(sym) for sym in op_syms])276+ var_str += "".join(
277+ ["extern uint8_t {};\n".format(sym) for sym in op_syms]
278+ )
195 macro_op += "{{\n{}}}; \n".format("".join(socs_res))279 macro_op += "{{\n{}}}; \n".format("".join(socs_res))
196 head_file = os.path.join(self.out_path, "ge_table_op_resource.h")280 head_file = os.path.join(self.out_path, "ge_table_op_resource.h")
197 try:281 try:
@@ -205,15 +289,23 @@ class PackKernel:
205 fd.write('#include "graph/ascend_string.h"\n')289 fd.write('#include "graph/ascend_string.h"\n')
206 fd.write('#include "register/op_impl_registry.h"\n\n')290 fd.write('#include "register/op_impl_registry.h"\n\n')
207 fd.write(var_str)291 fd.write(var_str)
208- fd.write('\n')292+ fd.write("\n")
209 fd.write("namespace AscendC {\n")293 fd.write("namespace AscendC {\n")
210 fd.write(macro_op)294 fd.write(macro_op)
211 fd.write("}\n")295 fd.write("}\n")
212 except Exception as e:296 except Exception as e:
213- LogUtil.print_compile_log("", f"ge_table_op_resource.h create error: {e}!",297+ LogUtil.print_compile_log(
214- AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC)298+ "",
215- macro_op_register = ("std::vector<std::tuple<ge::AscendString, ge::AscendString, "299+ f"ge_table_op_resource.h create error: {e}!",
216- "const uint8_t *, const uint8_t *>> __ascendc_op_info_" + self.vendor_name + " = \n")300+ AscendCLogLevel.LOG_ERROR,
301+ LogUtil.Option.NON_SOC,
302+ )
303+ macro_op_register = (
304+ "std::vector<std::tuple<ge::AscendString, ge::AscendString, "
305+ "const uint8_t *, const uint8_t *>> __ascendc_op_info_"
306+ + self.vendor_name
307+ + " = \n"
308+ )
217 macro_op_register += "{{\n{}}}; \n".format("".join(socs_res))309 macro_op_register += "{{\n{}}}; \n".format("".join(socs_res))
218 self.ascendc_gen_register(macro_op_register, var_str)310 self.ascendc_gen_register(macro_op_register, var_str)
219 311 
@@ -228,11 +320,15 @@ class PackKernel:
228 sub_objs = objs[start : start + batch_size]320 sub_objs = objs[start : start + batch_size]
229 start += batch_size321 start += batch_size
230 try:322 try:
231- subprocess.run(['ar', 'qc', out_lib] + sub_objs)323+ subprocess.run(["ar", "qc", out_lib] + sub_objs)
232- subprocess.run(['ranlib', out_lib])324+ subprocess.run(["ranlib", out_lib])
233 except Exception as e:325 except Exception as e:
234- LogUtil.print_compile_log("", f"execute ar/ranlib command error: {e}!",326+ LogUtil.print_compile_log(
235- AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC)327+ "",
328+ f"execute ar/ranlib command error: {e}!",
329+ AscendCLogLevel.LOG_ERROR,
330+ LogUtil.Option.NON_SOC,
331+ )
236 332 
237 def ascendc_copy_dir_recursive(self: any, src_dir, dst_dir):333 def ascendc_copy_dir_recursive(self: any, src_dir, dst_dir):
238 try:334 try:
@@ -244,9 +340,13 @@ class PackKernel:
244 for dir_path, _, items in os.walk(src_dir):340 for dir_path, _, items in os.walk(src_dir):
245 for item in items:341 for item in items:
246 src_item = os.path.join(dir_path, item)342 src_item = os.path.join(dir_path, item)
247- dst_item = os.path.join(dst_dir, dir_path.replace(src_dir, '.'), item)343+ dst_item = os.path.join(
344+ dst_dir, dir_path.replace(src_dir, "."), item
345+ )
248 os.makedirs(os.path.dirname(dst_item), exist_ok=True)346 os.makedirs(os.path.dirname(dst_item), exist_ok=True)
249- if (not src_item.endswith(".txt")) and (not os.path.exists(dst_item)):347+ if (not src_item.endswith(".txt")) and (
348+ not os.path.exists(dst_item)
349+ ):
250 shutil.copy2(src_item, dst_item)350 shutil.copy2(src_item, dst_item)
251 351 
252 except Exception as e:352 except Exception as e:
@@ -261,8 +361,12 @@ class PackKernel:
261 try:361 try:
262 shutil.copytree(source_file, target_file)362 shutil.copytree(source_file, target_file)
263 except Exception as e:363 except Exception as e:
264- LogUtil.print_compile_log("", f"copy {source_file} error: {e}!",364+ LogUtil.print_compile_log(
265- AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC)365+ "",
366+ f"copy {source_file} error: {e}!",
367+ AscendCLogLevel.LOG_ERROR,
368+ LogUtil.Option.NON_SOC,
369+ )
266 370 
267 def ascendc_copy_file(self: any, src_dir: str, target_dir: str):371 def ascendc_copy_file(self: any, src_dir: str, target_dir: str):
268 file_list = os.listdir(src_dir)372 file_list = os.listdir(src_dir)
@@ -272,13 +376,21 @@ class PackKernel:
272 try:376 try:
273 os.makedirs(target_dir, exist_ok=True)377 os.makedirs(target_dir, exist_ok=True)
274 except Exception as e:378 except Exception as e:
275- LogUtil.print_compile_log("", f"make {target_dir} error: {e}!",379+ LogUtil.print_compile_log(
276- AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC)380+ "",
381+ f"make {target_dir} error: {e}!",
382+ AscendCLogLevel.LOG_ERROR,
383+ LogUtil.Option.NON_SOC,
384+ )
277 try:385 try:
278 shutil.copy(source_file, target_dir)386 shutil.copy(source_file, target_dir)
279 except Exception as e:387 except Exception as e:
280- LogUtil.print_compile_log("", f"copy {source_file} error: {e}!",388+ LogUtil.print_compile_log(
281- AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC)389+ "",
390+ f"copy {source_file} error: {e}!",
391+ AscendCLogLevel.LOG_ERROR,
392+ LogUtil.Option.NON_SOC,
393+ )
282 394 
283 def ascendc_copy_func(self: any):395 def ascendc_copy_func(self: any):
284 os.chdir(self.in_path)396 os.chdir(self.in_path)
@@ -286,7 +398,11 @@ class PackKernel:
286 if os.path.isdir("./framework"):398 if os.path.isdir("./framework"):
287 framework_catalog = os.listdir("framework")399 framework_catalog = os.listdir("framework")
288 for catalog_file in framework_catalog:400 for catalog_file in framework_catalog:
289- if catalog_file == "tf_plugin" or catalog_file == "caffe_plugin" or catalog_file == "onnx_plugin":401+ if (
402+ catalog_file == "tf_plugin"
403+ or catalog_file == "caffe_plugin"
404+ or catalog_file == "onnx_plugin"
405+ ):
290 source_dir = self.kernel_binary_path + "/tbe/op_info_cfg/ai_core"406 source_dir = self.kernel_binary_path + "/tbe/op_info_cfg/ai_core"
291 dst_dir = os.path.join(self.copy_path, "framework", self.framework_type)407 dst_dir = os.path.join(self.copy_path, "framework", self.framework_type)
292 self.ascendc_copy_file(source_dir, dst_dir)408 self.ascendc_copy_file(source_dir, dst_dir)
@@ -297,18 +413,24 @@ class PackKernel:
297 dst_dir = os.path.join(self.copy_path, "op_impl/ai_core/tbe/config")413 dst_dir = os.path.join(self.copy_path, "op_impl/ai_core/tbe/config")
298 self.ascendc_copy_dir(source_dir, dst_dir)414 self.ascendc_copy_dir(source_dir, dst_dir)
299 source_dir = self.kernel_binary_path + "/binary/dynamic"415 source_dir = self.kernel_binary_path + "/binary/dynamic"
300- dst_dir = os.path.join(self.copy_path, "op_impl/ai_core/tbe", self.vendor_name + "_impl", "dynamic")416+ dst_dir = os.path.join(
417+ self.copy_path, "op_impl/ai_core/tbe", self.vendor_name + "_impl", "dynamic"
418+ )
301 self.ascendc_copy_dir_recursive(source_dir, dst_dir)419 self.ascendc_copy_dir_recursive(source_dir, dst_dir)
302 for compute_unit in self.op_soc_ver:420 for compute_unit in self.op_soc_ver:
303 source_dir = os.path.join(self.kernel_binary_path + "/binary", compute_unit)421 source_dir = os.path.join(self.kernel_binary_path + "/binary", compute_unit)
304- dst_dir = os.path.join(self.copy_path, "op_impl/ai_core/tbe/kernel", compute_unit)422+ dst_dir = os.path.join(
423+ self.copy_path, "op_impl/ai_core/tbe/kernel", compute_unit
424+ )
305 self.ascendc_copy_dir(source_dir, dst_dir)425 self.ascendc_copy_dir(source_dir, dst_dir)
306 source_dir = self.kernel_binary_path + "/binary/config"426 source_dir = self.kernel_binary_path + "/binary/config"
307 dst_dir = os.path.join(self.copy_path, "op_impl/ai_core/tbe/kernel/config")427 dst_dir = os.path.join(self.copy_path, "op_impl/ai_core/tbe/kernel/config")
308 self.ascendc_copy_dir(source_dir, dst_dir)428 self.ascendc_copy_dir(source_dir, dst_dir)
309 so_file = "op_impl/ai_core/tbe/op_master_device/lib/libcust_opmaster.so"429 so_file = "op_impl/ai_core/tbe/op_master_device/lib/libcust_opmaster.so"
310 if os.path.exists(so_file):430 if os.path.exists(so_file):
311- dst_dir = os.path.join(self.copy_path, "op_impl/ai_core/tbe/op_master_device/lib")431+ dst_dir = os.path.join(
432+ self.copy_path, "op_impl/ai_core/tbe/op_master_device/lib"
433+ )
312 os.makedirs(dst_dir, exist_ok=True)434 os.makedirs(dst_dir, exist_ok=True)
313 shutil.copy(so_file, dst_dir)435 shutil.copy(so_file, dst_dir)
314 436 
@@ -327,23 +449,28 @@ def args_parse():
327 parser.add_argument(449 parser.add_argument(
328 "-o", "--output-path", nargs="?", help="Output path of compile result."450 "-o", "--output-path", nargs="?", help="Output path of compile result."
329 )451 )
330- parser.add_argument(452+ parser.add_argument("-n", "--vendor-name", nargs="?", help="Vendor name.")
331- "-n", "--vendor-name", nargs="?", help="Vendor name."453+ parser.add_argument("-u", "--compute-unit", nargs="?", help="Compute unit.")
332- )
333- parser.add_argument(
334- "-u", "--compute-unit", nargs="?", help="Compute unit."
335- )
336 parser.add_argument(454 parser.add_argument(
337 "-t", "--framework-type", nargs="?", help="Framework type, eg:tensorflow."455 "-t", "--framework-type", nargs="?", help="Framework type, eg:tensorflow."
338 )456 )
339 parser.add_argument(457 parser.add_argument(
340- "-p", "--platform", nargs="?", default=None, help="target platform is x86_64 or aarch64."458+ "-p",
459+ "--platform",
460+ nargs="?",
461+ default=None,
462+ help="target platform is x86_64 or aarch64.",
341 )463 )
342 parser.add_argument(464 parser.add_argument(
343- "-k", "--kernel-binary-path", nargs="?", default=None, help="target kernel binary path."465+ "-k",
466+ "--kernel-binary-path",
467+ nargs="?",
468+ default=None,
469+ help="target kernel binary path.",
344 )470 )
345 return parser.parse_args()471 return parser.parse_args()
346 472 
473+ 
347if __name__ == "__main__":474if __name__ == "__main__":
348 args = args_parse()475 args = args_parse()
349 kernel_packer = PackKernel(args)476 kernel_packer = PackKernel(args)
@@ -14,29 +14,17 @@ import configparser
14import argparse14import argparse
15import os15import os
16import stat16import stat
17-import configparser
18import hashlib17import hashlib
19 18 
19+ 
20def args_parse():20def args_parse():
21 parser = argparse.ArgumentParser()21 parser = argparse.ArgumentParser()
22- parser.add_argument(22+ parser.add_argument("-p", "--auto-gen-path", required=True, help="auto gen path")
23- "-p", "--auto-gen-path", required=True, help="auto gen path"23+ parser.add_argument("-n", "--target-name", required=True, help="kernel target name")
24- )24+ parser.add_argument("-t", "--op-type", required=True, help="op type")
25- parser.add_argument(25+ parser.add_argument("-f", "--op-kernel-file", help="op kernel file")
26- "-n", "--target-name", required=True, help="kernel target name"26+ parser.add_argument("-c", "--compute-unit", nargs="*", help="compute unit")
27- )27+ parser.add_argument("-d", "--op-kernel-dir", help="op kernel dir")
28- parser.add_argument(
29- "-t", "--op-type", required=True, help="op type"
30- )
31- parser.add_argument(
32- "-f", "--op-kernel-file", help="op kernel file"
33- )
34- parser.add_argument(
35- "-c", "--compute-unit", nargs="*", help="compute unit"
36- )
37- parser.add_argument(
38- "-d", "--op-kernel-dir", help="op kernel dir"
39- )
40 return parser.parse_args()28 return parser.parse_args()
41 29 
42 30 
@@ -52,7 +40,9 @@ def is_relative_path(path):
52 40 
53 41 
54def gen_custom_source_files(args):42def gen_custom_source_files(args):
55- gen_file_path = os.path.join(args.auto_gen_path, args.target_name + "_custom_source_files.ini")43+ gen_file_path = os.path.join(
44+ args.auto_gen_path, args.target_name + "_custom_source_files.ini"
45+ )
56 src_ini_config = None46 src_ini_config = None
57 if os.path.exists(gen_file_path):47 if os.path.exists(gen_file_path):
58 src_ini_config = configparser.ConfigParser()48 src_ini_config = configparser.ConfigParser()
@@ -60,25 +50,37 @@ def gen_custom_source_files(args):
60 flags = os.O_WRONLY | os.O_CREAT50 flags = os.O_WRONLY | os.O_CREAT
61 modes = stat.S_IWUSR | stat.S_IRUSR51 modes = stat.S_IWUSR | stat.S_IRUSR
62 try:52 try:
63- with os.fdopen(os.open(gen_file_path, flags, modes), 'a') as fd:53+ with os.fdopen(os.open(gen_file_path, flags, modes), "a") as fd:
64 hash_value = hashlib.sha256(54 hash_value = hashlib.sha256(
65- (args.op_type + str(args.op_kernel_file) + \55+ (
66- str(args.op_kernel_dir) + str(args.compute_unit)).encode('utf-8')).hexdigest()56+ args.op_type
67- if src_ini_config is not None and hash_value + "_" + args.op_type in src_ini_config.sections():57+ + str(args.op_kernel_file)
58+ + str(args.op_kernel_dir)
59+ + str(args.compute_unit)
60+ ).encode("utf-8")
61+ ).hexdigest()
62+ if (
63+ src_ini_config is not None
64+ and hash_value + "_" + args.op_type in src_ini_config.sections()
65+ ):
68 return True66 return True
69- fd.write("[" + hash_value + "_" + args.op_type + "]" + '\n')67+ fd.write("[" + hash_value + "_" + args.op_type + "]" + "\n")
70 if args.op_kernel_dir is not None:68 if args.op_kernel_dir is not None:
71 flag = is_relative_path(args.op_kernel_dir)69 flag = is_relative_path(args.op_kernel_dir)
72 if not flag:70 if not flag:
73- print("[ERROR] op kernel dir should be relative path to kernel base dir")71+ print(
72+ "[ERROR] op kernel dir should be relative path to kernel base dir"
73+ )
74 return flag74 return flag
75- fd.write("kernel_dir=" + args.op_kernel_dir + '\n')75+ fd.write("kernel_dir=" + args.op_kernel_dir + "\n")
76 if args.op_kernel_file is not None:76 if args.op_kernel_file is not None:
77- fd.write("kernel_file=" + args.op_kernel_file + '\n')77+ fd.write("kernel_file=" + args.op_kernel_file + "\n")
78 if args.compute_unit is not None:78 if args.compute_unit is not None:
79- tmp_compute_unit = ",".join(x.strip() for x in args.compute_unit[0].strip().split())79+ tmp_compute_unit = ",".join(
80- fd.write("compute_unit=" + tmp_compute_unit + '\n')80+ x.strip() for x in args.compute_unit[0].strip().split()
81- except Exception as err:81+ )
82+ fd.write("compute_unit=" + tmp_compute_unit + "\n")
83+ except Exception:
82 print("write custom source files ini failed")84 print("write custom source files ini failed")
83 return False85 return False
84 return True86 return True
@@ -9,13 +9,7 @@
9# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.9# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10# See LICENSE in the root of the software repository for the full text of the License.10# See LICENSE in the root of the software repository for the full text of the License.
11# ----------------------------------------------------------------------------------------------------------11# ----------------------------------------------------------------------------------------------------------
12-import os
13-import stat
14-import ctypes
15import collections12import collections
16-import shutil
17-import subprocess
18-import copy
19 13 
20"""CODE_* is used to cube/vector api is called in operator code14"""CODE_* is used to cube/vector api is called in operator code
21CODE_MIX means both cube and vector api is called15CODE_MIX means both cube and vector api is called
@@ -38,8 +32,17 @@ def _is_v220(op_product: str):
38 return False32 return False
39 33 
40 34 
41-InfoCodeChannelParams = collections.namedtuple('InfoCodeChannelParams', \35+InfoCodeChannelParams = collections.namedtuple(
42-['src_file', 'tiling_header', 'kernel_name', 'outdir', 'op_product', 'compile_options'])36+ "InfoCodeChannelParams",
37+ [
38+ "src_file",
39+ "tiling_header",
40+ "kernel_name",
41+ "outdir",
42+ "op_product",
43+ "compile_options",
44+ ],
45+)
43 46 
44 47 
45def infer_code_channel(params: InfoCodeChannelParams):48def infer_code_channel(params: InfoCodeChannelParams):
@@ -10,11 +10,8 @@
10# See LICENSE in the root of the software repository for the full text of the License.10# See LICENSE in the root of the software repository for the full text of the License.
11# ----------------------------------------------------------------------------------------------------------11# ----------------------------------------------------------------------------------------------------------
12 12 
13-import configparser
14import argparse13import argparse
15import os14import os
16-import stat
17-import hashlib
18import multiprocessing15import multiprocessing
19import const_var16import const_var
20 17 
@@ -65,15 +62,9 @@ def args_parse():
65 parser.add_argument(62 parser.add_argument(
66 "-s", "--src-file", nargs="*", required=True, help="input src files"63 "-s", "--src-file", nargs="*", required=True, help="input src files"
67 )64 )
68- parser.add_argument(65+ parser.add_argument("-d", "--output-dir", required=True, help="output dir")
69- "-d", "--output-dir", required=True, help="output dir"66+ parser.add_argument("-o", "--output", required=True, help="output")
70- )67+ parser.add_argument("-p", "--cann-path", required=True, help="cann path")
71- parser.add_argument(
72- "-o", "--output", required=True, help="output"
73- )
74- parser.add_argument(
75- "-p", "--cann-path", required=True, help="cann path"
76- )
77 parser.add_argument(68 parser.add_argument(
78 "-j", "--parallel-jobs", required=False, help="compile parallel jobs"69 "-j", "--parallel-jobs", required=False, help="compile parallel jobs"
79 )70 )
@@ -111,14 +102,15 @@ def gen_compile_make_file(param_args):
111 param_args.cxx_compiler,102 param_args.cxx_compiler,
112 f"-D_GLIBCXX_USE_CXX11_ABI=0 -std=c++11 {compile_options} -I{param_args.cann_path}/include",103 f"-D_GLIBCXX_USE_CXX11_ABI=0 -std=c++11 {compile_options} -I{param_args.cann_path}/include",
113 debug_content,104 debug_content,
114- f"{link_options} -lexe_graph -lregister -ltiling_api -L{param_args.cann_path}/lib64")105+ f"{link_options} -lexe_graph -lregister -ltiling_api -L{param_args.cann_path}/lib64",
106+ )
115 fd.write(compile_cmd)107 fd.write(compile_cmd)
116 108 
117 109 
118def run_compile_cmd(param_args):110def run_compile_cmd(param_args):
119 mkfile = os.path.join(param_args.output_dir, "ascendc_all_ops.make")111 mkfile = os.path.join(param_args.output_dir, "ascendc_all_ops.make")
120 if not os.path.exists(mkfile):112 if not os.path.exists(mkfile):
121- raise RuntimeError('ascendc_all_ops.make not exist')113+ raise RuntimeError("ascendc_all_ops.make not exist")
122 system_cpu_count = 2 * multiprocessing.cpu_count() - 2114 system_cpu_count = 2 * multiprocessing.cpu_count() - 2
123 if param_args.parallel_jobs is not None:115 if param_args.parallel_jobs is not None:
124 system_cpu_count = min(system_cpu_count, int(param_args.parallel_jobs))116 system_cpu_count = min(system_cpu_count, int(param_args.parallel_jobs))
@@ -126,7 +118,7 @@ def run_compile_cmd(param_args):
126 cmd = f"make -f {mkfile} -j{parallel_compile_job}"118 cmd = f"make -f {mkfile} -j{parallel_compile_job}"
127 ret = os.system(cmd)119 ret = os.system(cmd)
128 if ret != 0:120 if ret != 0:
129- raise RuntimeError('compile ascend all ops failed')121+ raise RuntimeError("compile ascend all ops failed")
130 122 
131 123 
132if __name__ == "__main__":124if __name__ == "__main__":
@@ -14,23 +14,32 @@ import os
14import stat14import stat
15 15 
16 16 
17-REPLAY_BATCH = 'batch'17+REPLAY_BATCH = "batch"
18-REPLAY_ITERATE = 'iterate'18+REPLAY_ITERATE = "iterate"
19-CFG_IMPL_DIR = 'impl_dir'19+CFG_IMPL_DIR = "impl_dir"
20-CFG_OUT_DIR = 'out_dir'20+CFG_OUT_DIR = "out_dir"
21-AUTO_GEN_DIR = 'auto_gen_dir'21+AUTO_GEN_DIR = "auto_gen_dir"
22WFLAGS = os.O_WRONLY | os.O_CREAT | os.O_TRUNC22WFLAGS = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
23WMODES = stat.S_IWUSR | stat.S_IRUSR23WMODES = stat.S_IWUSR | stat.S_IRUSR
24-SOC_MAP_EXT = {'ascend310p': 'Ascend310P3', 'ascend310b': 'Ascend310B1',24+SOC_MAP_EXT = {
25- 'ascend910': 'Ascend910A', 'ascend910b': 'Ascend910B1',25+ "ascend310p": "Ascend310P3",
26- 'ascend910_93': 'Ascend910_9391', 'ascend610lite': 'Ascend610Lite',26+ "ascend310b": "Ascend310B1",
27- 'ascend950': 'Ascend950PR_9599', 'kirinx90': 'KirinX90', 'kirin9030': 'Kirin9030',27+ "ascend910": "Ascend910A",
28- 'mc62': 'MC62CM12AA', 'mc32dm11a': 'MC32DM11AA', 'ascend350': 'Ascend350_354f'}28+ "ascend910b": "Ascend910B1",
29-BIN_CMD = 'opc $1 --main_func={fun} --input_param={param} --soc_version={soc} \29+ "ascend910_93": "Ascend910_9391",
30---output=$2 --impl_mode={impl} --simplified_key_mode=0 --op_mode=dynamic\n'30+ "ascend610lite": "Ascend610Lite",
31+ "ascend950": "Ascend950PR_9599",
32+ "kirinx90": "KirinX90",
33+ "kirin9030": "Kirin9030",
34+ "mc62": "MC62CM12AA",
35+ "mc32dm11a": "MC32DM11AA",
36+ "ascend350": "Ascend350_354f",
37+}
38+BIN_CMD = "opc $1 --main_func={fun} --input_param={param} --soc_version={soc} \
39+--output=$2 --impl_mode={impl} --simplified_key_mode=0 --op_mode=dynamic\n"
31SET_PLOG_LEVEL_ERROR = "export ASCEND_GLOBAL_LOG_LEVEL=3\n"40SET_PLOG_LEVEL_ERROR = "export ASCEND_GLOBAL_LOG_LEVEL=3\n"
32SET_PLOG_STDOUT = "export ASCEND_SLOG_PRINT_TO_STDOUT=1\n"41SET_PLOG_STDOUT = "export ASCEND_SLOG_PRINT_TO_STDOUT=1\n"
33-SRC_ENV = '''42+SRC_ENV = """
34while true; do43while true; do
35 case "$1" in44 case "$1" in
36 --kernel-src=*)45 --kernel-src=*)
@@ -45,15 +54,23 @@ while true; do
45 ;;54 ;;
46 esac55 esac
47done56done
48-'''57+"""
49-CHK_CMD = '''58+CHK_CMD = """
50if ! test -f $2/{res_file} ; then59if ! test -f $2/{res_file} ; then
51 echo "$2/{res_file} not generated!"60 echo "$2/{res_file} not generated!"
52 exit 161 exit 1
53fi62fi
54-'''63+"""
55-ATTR_DEF_VAL = {'str' : '', 'int': 0, 'float': 0.0, 'bool': False, 'list_bool': [],64+ATTR_DEF_VAL = {
56- 'list_int': [], 'list_float': [], 'list_list_int': [[]]}65+ "str": "",
66+ "int": 0,
67+ "float": 0.0,
68+ "bool": False,
69+ "list_bool": [],
70+ "list_int": [],
71+ "list_float": [],
72+ "list_list_int": [[]],
73+}
57 74 
58 75 
59def conv_soc_ver(ver: str):76def conv_soc_ver(ver: str):
@@ -18,31 +18,32 @@
18import json18import json
19import os19import os
20import sys20import sys
21-import stat
22import const_var21import const_var
23 22 
24 23 
25-if __name__ == '__main__':24+if __name__ == "__main__":
26 if len(sys.argv) != 3:25 if len(sys.argv) != 3:
27 print(sys.argv)26 print(sys.argv)
28- print('argv error, inert_op_info.py your_op_file lib_op_file')27+ print("argv error, inert_op_info.py your_op_file lib_op_file")
29 sys.exit(2)28 sys.exit(2)
30 29 
31- with open(sys.argv[1], 'r') as load_f:30+ with open(sys.argv[1], "r") as load_f:
32 insert_operator = json.load(load_f)31 insert_operator = json.load(load_f)
33 32 
34 all_operators = {}33 all_operators = {}
35 if os.path.exists(sys.argv[2]):34 if os.path.exists(sys.argv[2]):
36 if os.path.getsize(sys.argv[2]) != 0:35 if os.path.getsize(sys.argv[2]) != 0:
37- with open(sys.argv[2], 'r') as load_f:36+ with open(sys.argv[2], "r") as load_f:
38 all_operators = json.load(load_f)37 all_operators = json.load(load_f)
39 38 
40 for k in insert_operator.keys():39 for k in insert_operator.keys():
41 if k in all_operators.keys():40 if k in all_operators.keys():
42- print('replace op:[', k, '] success')41+ print("replace op:[", k, "] success")
43 else:42 else:
44- print('insert op:[', k, '] success')43+ print("insert op:[", k, "] success")
45 all_operators[k] = insert_operator[k]44 all_operators[k] = insert_operator[k]
46 45 
47- with os.fdopen(os.open(sys.argv[2], const_var.WFLAGS, const_var.WMODES), 'w') as json_file:46+ with os.fdopen(
47+ os.open(sys.argv[2], const_var.WFLAGS, const_var.WMODES), "w"
48+ ) as json_file:
48 json_file.write(json.dumps(all_operators, indent=4))49 json_file.write(json.dumps(all_operators, indent=4))
@@ -10,140 +10,138 @@
10# See LICENSE in the root of the software repository for the full text of the License.10# See LICENSE in the root of the software repository for the full text of the License.
11# ----------------------------------------------------------------------------------------------------------11# ----------------------------------------------------------------------------------------------------------
12 12 
13-import sys
14import os13import os
15import re14import re
16import glob15import glob
17import json16import json
18import argparse17import argparse
19-import const_var
20 18 
21 19 
22DATA_TYPE_DICT = {20DATA_TYPE_DICT = {
23- 'float32': 0,21+ "float32": 0,
24- 'float16': 1,22+ "float16": 1,
25- 'int8': 2,23+ "int8": 2,
26- 'int32': 3,24+ "int32": 3,
27- 'uint8': 4,25+ "uint8": 4,
28- 'int16': 6,26+ "int16": 6,
29- 'uint16': 7,27+ "uint16": 7,
30- 'uint32': 8,28+ "uint32": 8,
31- 'int64': 9,29+ "int64": 9,
32- 'uint64': 10,30+ "uint64": 10,
33- 'double': 11,31+ "double": 11,
34- 'bool': 12,32+ "bool": 12,
35- 'string': 13,33+ "string": 13,
36- 'dual_sub_int8': 14,34+ "dual_sub_int8": 14,
37- 'dual_sub_uint8': 15,35+ "dual_sub_uint8": 15,
38- 'complex64': 16,36+ "complex64": 16,
39- 'complex128': 17,37+ "complex128": 17,
40- 'qint8': 18,38+ "qint8": 18,
41- 'qint16': 19,39+ "qint16": 19,
42- 'qint32': 20,40+ "qint32": 20,
43- 'quint8': 21,41+ "quint8": 21,
44- 'quint16': 22,42+ "quint16": 22,
45- 'resource': 23,43+ "resource": 23,
46- 'string_ref': 24,44+ "string_ref": 24,
47- 'dual': 25,45+ "dual": 25,
48- 'variant': 26,46+ "variant": 26,
49- 'bf16': 27,47+ "bf16": 27,
50- 'bfloat16': 27,48+ "bfloat16": 27,
51- 'undefined': 28,49+ "undefined": 28,
52- 'int4': 29,50+ "int4": 29,
53- 'uint1': 30,51+ "uint1": 30,
54- 'int2': 31,52+ "int2": 31,
55- 'complex32': 3353+ "complex32": 33,
56}54}
57 55 
58FORMAT_DICT = {56FORMAT_DICT = {
59- 'NCHW': 0,57+ "NCHW": 0,
60- 'NHWC': 1,58+ "NHWC": 1,
61- 'ND': 2,59+ "ND": 2,
62- 'NC1HWC0': 3,60+ "NC1HWC0": 3,
63- 'FRACTAL_Z': 4,61+ "FRACTAL_Z": 4,
64- 'NC1C0HWPAD': 5,62+ "NC1C0HWPAD": 5,
65- 'NHWC1C0': 6,63+ "NHWC1C0": 6,
66- 'FSR_NCHW': 7,64+ "FSR_NCHW": 7,
67- 'FRACTAL_DECONV': 8,65+ "FRACTAL_DECONV": 8,
68- 'C1HWNC0': 9,66+ "C1HWNC0": 9,
69- 'FRACTAL_DECONV_TRANSPOSE': 10,67+ "FRACTAL_DECONV_TRANSPOSE": 10,
70- 'FRACTAL_DECONV_SP_STRIDE_TRANS': 11,68+ "FRACTAL_DECONV_SP_STRIDE_TRANS": 11,
71- 'NC1HWC0_C04': 12,69+ "NC1HWC0_C04": 12,
72- 'FRACTAL_Z_C04': 13,70+ "FRACTAL_Z_C04": 13,
73- 'CHWN': 14,71+ "CHWN": 14,
74- 'FRACTAL_DECONV_SP_STRIDE8_TRANS': 15,72+ "FRACTAL_DECONV_SP_STRIDE8_TRANS": 15,
75- 'HWCN': 16,73+ "HWCN": 16,
76- 'NC1KHKWHWC0': 17,74+ "NC1KHKWHWC0": 17,
77- 'BN_WEIGHT': 18,75+ "BN_WEIGHT": 18,
78- 'FILTER_HWCK': 19,76+ "FILTER_HWCK": 19,
79- 'HASHTABLE_LOOKUP_LOOKUPS': 20,77+ "HASHTABLE_LOOKUP_LOOKUPS": 20,
80- 'HASHTABLE_LOOKUP_KEYS': 21,78+ "HASHTABLE_LOOKUP_KEYS": 21,
81- 'HASHTABLE_LOOKUP_VALUE': 22,79+ "HASHTABLE_LOOKUP_VALUE": 22,
82- 'HASHTABLE_LOOKUP_OUTPUT': 23,80+ "HASHTABLE_LOOKUP_OUTPUT": 23,
83- 'HASHTABLE_LOOKUP_HITS': 24,81+ "HASHTABLE_LOOKUP_HITS": 24,
84- 'C1HWNCoC0': 25,82+ "C1HWNCoC0": 25,
85- 'MD': 26,83+ "MD": 26,
86- 'NDHWC': 27,84+ "NDHWC": 27,
87- 'FRACTAL_ZZ': 28,85+ "FRACTAL_ZZ": 28,
88- 'FRACTAL_NZ': 29,86+ "FRACTAL_NZ": 29,
89- 'NCDHW': 30,87+ "NCDHW": 30,
90- 'DHWCN': 31,88+ "DHWCN": 31,
91- 'NDC1HWC0': 32,89+ "NDC1HWC0": 32,
92- 'FRACTAL_Z_3D': 33,90+ "FRACTAL_Z_3D": 33,
93- 'CN': 34,91+ "CN": 34,
94- 'NC': 35,92+ "NC": 35,
95- 'DHWNC': 36,93+ "DHWNC": 36,
96- 'FRACTAL_Z_3D_TRANSPOSE': 37,94+ "FRACTAL_Z_3D_TRANSPOSE": 37,
97- 'FRACTAL_ZN_LSTM': 38,95+ "FRACTAL_ZN_LSTM": 38,
98- 'FRACTAL_Z_G': 39,96+ "FRACTAL_Z_G": 39,
99- 'RESERVED': 40,97+ "RESERVED": 40,
100- 'ALL': 41,98+ "ALL": 41,
101- 'NULL': 42,99+ "NULL": 42,
102- 'ND_RNN_BIAS': 43,100+ "ND_RNN_BIAS": 43,
103- 'FRACTAL_ZN_RNN': 44,101+ "FRACTAL_ZN_RNN": 44,
104- 'NYUV': 45,102+ "NYUV": 45,
105- 'NYUV_A': 46,103+ "NYUV_A": 46,
106- 'NCL': 47,104+ "NCL": 47,
107- 'FRACTAL_Z_WINO': 48,105+ "FRACTAL_Z_WINO": 48,
108- 'C1HWC0': 49,106+ "C1HWC0": 49,
109- 'FRACTAL_NZ_C0_16': 50,107+ "FRACTAL_NZ_C0_16": 50,
110- 'FRACTAL_NZ_C0_32': 51,108+ "FRACTAL_NZ_C0_32": 51,
111- 'FRACTAL_NZ_C0_2': 52,109+ "FRACTAL_NZ_C0_2": 52,
112- 'FRACTAL_NZ_C0_4': 53,110+ "FRACTAL_NZ_C0_4": 53,
113- 'FRACTAL_NZ_C0_8': 54,111+ "FRACTAL_NZ_C0_8": 54,
114- 'MAX': 55,112+ "MAX": 55,
115}113}
116 114 
117 115 
118def load_json(json_file: str):116def load_json(json_file: str):
119- with open(json_file, encoding='utf-8') as file:117+ with open(json_file, encoding="utf-8") as file:
120 json_content = json.load(file)118 json_content = json.load(file)
121 return json_content119 return json_content
122 120 
123 121 
124def get_specified_suffix_file(root_dir, suffix):122def get_specified_suffix_file(root_dir, suffix):
125- specified_suffix = os.path.join(root_dir, '**/*.{}'.format(suffix))123+ specified_suffix = os.path.join(root_dir, "**/*.{}".format(suffix))
126 all_suffix_files = glob.glob(specified_suffix, recursive=True)124 all_suffix_files = glob.glob(specified_suffix, recursive=True)
127 return all_suffix_files125 return all_suffix_files
128 126 
129 127 
130def get_deterministic_value(support_info):128def get_deterministic_value(support_info):
131- deterministic_key = 'deterministic'129+ deterministic_key = "deterministic"
132 if deterministic_key not in support_info:130 if deterministic_key not in support_info:
133 return 0131 return 0
134 deterministic_value = support_info.get(deterministic_key)132 deterministic_value = support_info.get(deterministic_key)
135- if deterministic_value == 'true':133+ if deterministic_value == "true":
136 return 1134 return 1
137 else:135 else:
138 return 0136 return 0
139 137 
140 138 
141def get_precision_value(support_info):139def get_precision_value(support_info):
142- precision_key = 'implMode'140+ precision_key = "implMode"
143 precision_value = support_info.get(precision_key)141 precision_value = support_info.get(precision_key)
144- if precision_value == 'high_performance':142+ if precision_value == "high_performance":
145 _value = 1143 _value = 1
146- elif precision_value == 'high_precision':144+ elif precision_value == "high_precision":
147 _value = 2145 _value = 2
148 else:146 else:
149 _value = 0147 _value = 0
@@ -156,13 +154,13 @@ def get_overflow_value(support_info):
156 154 
157def get_parameters(info):155def get_parameters(info):
158 if info:156 if info:
159- if 'dtype' in info:157+ if "dtype" in info:
160- data_type = info['dtype']158+ data_type = info["dtype"]
161 data_type_value = DATA_TYPE_DICT.get(data_type)159 data_type_value = DATA_TYPE_DICT.get(data_type)
162 else:160 else:
163 data_type_value = 0161 data_type_value = 0
164- if 'format' in info:162+ if "format" in info:
165- _format = info['format']163+ _format = info["format"]
166 _format_value = FORMAT_DICT.get(_format)164 _format_value = FORMAT_DICT.get(_format)
167 else:165 else:
168 _format_value = 0166 _format_value = 0
@@ -192,52 +190,49 @@ def get_all_parameters(support_info, _type):
192 190 
193 191 
194def get_all_input_parameters(support_info):192def get_all_input_parameters(support_info):
195- result = get_all_parameters(support_info, 'inputs')193+ result = get_all_parameters(support_info, "inputs")
196- return '/'.join(result)194+ return "/".join(result)
197 195 
198 196 
199def insert_content_into_file(input_file, content):197def insert_content_into_file(input_file, content):
200- with open(input_file, 'r+') as file:198+ with open(input_file, "r+") as file:
201 lines = file.readlines()199 lines = file.readlines()
202 for index, line in enumerate(lines):200 for index, line in enumerate(lines):
203 match_result = re.search(r'"staticKey":', line)201 match_result = re.search(r'"staticKey":', line)
204 if match_result:202 if match_result:
205 count = len(line) - len(line.lstrip())203 count = len(line) - len(line.lstrip())
206- new_content = "{}{}".format(' ' * count, content)204+ new_content = "{}{}".format(" " * count, content)
207 # 插入到前一行,防止插入最后时还需要考虑是否添加逗号205 # 插入到前一行,防止插入最后时还需要考虑是否添加逗号
208 lines.insert(index, new_content)206 lines.insert(index, new_content)
209 break207 break
210 file.seek(0)208 file.seek(0)
211- file.write(''.join(lines))209+ file.write("".join(lines))
212 210 
213 211 
214def insert_simplified_keys(json_file):212def insert_simplified_keys(json_file):
215 contents = load_json(json_file)213 contents = load_json(json_file)
216 # 不存在'binFileName'或者'supportInfo'字段时,非需要替换的解析json文件214 # 不存在'binFileName'或者'supportInfo'字段时,非需要替换的解析json文件
217- if ('binFileName' not in contents) or ('supportInfo' not in contents):215+ if ("binFileName" not in contents) or ("supportInfo" not in contents):
218 return216 return
219- support_info = contents.get('supportInfo')217+ support_info = contents.get("supportInfo")
220- bin_file_name = contents.get('binFileName')218+ bin_file_name = contents.get("binFileName")
221 # 'simplifiedKey'字段已经存在时,直接返回,不重复生成219 # 'simplifiedKey'字段已经存在时,直接返回,不重复生成
222- if 'simplifiedKey' in support_info:220+ if "simplifiedKey" in support_info:
223 return221 return
224- op_type = bin_file_name.split('_')[0]222+ op_type = bin_file_name.split("_")[0]
225 deterministic = str(get_deterministic_value(support_info))223 deterministic = str(get_deterministic_value(support_info))
226 precision = str(get_precision_value(support_info))224 precision = str(get_precision_value(support_info))
227 overflow = str(get_overflow_value(support_info))225 overflow = str(get_overflow_value(support_info))
228 input_parameters = get_all_input_parameters(support_info)226 input_parameters = get_all_input_parameters(support_info)
229- key = '{}/d={},p={},o={}/{}/'.format(227+ key = "{}/d={},p={},o={}/{}/".format(
230- op_type,228+ op_type, deterministic, precision, overflow, input_parameters
231- deterministic,229+ )
232- precision,
233- overflow,
234- input_parameters)
235 result = '"simplifiedKey": "' + key + '",\n'230 result = '"simplifiedKey": "' + key + '",\n'
236 insert_content_into_file(json_file, result)231 insert_content_into_file(json_file, result)
237 232 
238 233 
239def insert_all_simplified_keys(root_dir):234def insert_all_simplified_keys(root_dir):
240- suffix = 'json'235+ suffix = "json"
241 all_json_files = get_specified_suffix_file(root_dir, suffix)236 all_json_files = get_specified_suffix_file(root_dir, suffix)
242 for _json in all_json_files:237 for _json in all_json_files:
243 insert_simplified_keys(_json)238 insert_simplified_keys(_json)
@@ -245,11 +240,13 @@ def insert_all_simplified_keys(root_dir):
245 240 
246def args_parse():241def args_parse():
247 parser = argparse.ArgumentParser()242 parser = argparse.ArgumentParser()
248- parser.add_argument('-p',243+ parser.add_argument(
249- '--path',244+ "-p",
250- nargs='?',245+ "--path",
251- required=True,246+ nargs="?",
252- help='Parse the path of the json file.')247+ required=True,
248+ help="Parse the path of the json file.",
249+ )
253 return parser.parse_args()250 return parser.parse_args()
254 251 
255 252 
@@ -260,5 +257,5 @@ def main():
260 insert_all_simplified_keys(args.path)257 insert_all_simplified_keys(args.path)
261 258 
262 259 
263-if __name__ == '__main__':260+if __name__ == "__main__":
264 main()261 main()
@@ -10,12 +10,11 @@
10# See LICENSE in the root of the software repository for the full text of the License.10# See LICENSE in the root of the software repository for the full text of the License.
11# ----------------------------------------------------------------------------------------------------------11# ----------------------------------------------------------------------------------------------------------
12 12 
13-import sys
14import os13import os
15 14 
16 15 
17-OP_ALL = '__ALLOP__'16+OP_ALL = "__ALLOP__"
18-SOC_ALL = '__ALLSOC__'17+SOC_ALL = "__ALLSOC__"
19SOC_TO_SHORT_SOC_MAP = {18SOC_TO_SHORT_SOC_MAP = {
20 "ascend910a": "ascend910",19 "ascend910a": "ascend910",
21 "ascend910proa": "ascend910",20 "ascend910proa": "ascend910",
@@ -83,17 +82,73 @@ SOC_TO_SHORT_SOC_MAP = {
83 "ascend350_355e": "ascend350",82 "ascend350_355e": "ascend350",
84}83}
85CONFLICT_KEYWORDS = {84CONFLICT_KEYWORDS = {
86- "and", "as", "assert", "break", "class", "continue", "def", "del", "elif", "else",85+ "and",
87- "except", "finally", "for", "from", "global", "if", "import", "in", "is", "lambda",86+ "as",
88- "not", "or", "pass", "raise", "return", "try", "while", "with", "yield", "False",87+ "assert",
89- "None", "True", "nonlocal", "arg", "__inputs__", "__outputs__", "options", "bisheng",88+ "break",
90- "bisheng_path", "tikcpp_path", "impl_mode", "custom_compile_options",89+ "class",
91- "custom_all_compile_options", "soc_version", "soc_short", "custom_compile_options_soc",90+ "continue",
92- "custom_all_compile_options_soc", "origin_func_name", "ascendc_src_dir_ex",91+ "def",
93- "ascendc_src_dir", "ascendc_src_file", "src", "op_type", "code_channel", "op_info",92+ "del",
94- "compile_op", "get_code_channel", "result", "__attrs__", "isinstance", "attr",93+ "elif",
95- "get_current_build_config", "_build_args", "get_dtype_fmt_options", "shutil", "os",94+ "else",
96- "get_kernel_source"95+ "except",
96+ "finally",
97+ "for",
98+ "from",
99+ "global",
100+ "if",
101+ "import",
102+ "in",
103+ "is",
104+ "lambda",
105+ "not",
106+ "or",
107+ "pass",
108+ "raise",
109+ "return",
110+ "try",
111+ "while",
112+ "with",
113+ "yield",
114+ "False",
115+ "None",
116+ "True",
117+ "nonlocal",
118+ "arg",
119+ "__inputs__",
120+ "__outputs__",
121+ "options",
122+ "bisheng",
123+ "bisheng_path",
124+ "tikcpp_path",
125+ "impl_mode",
126+ "custom_compile_options",
127+ "custom_all_compile_options",
128+ "soc_version",
129+ "soc_short",
130+ "custom_compile_options_soc",
131+ "custom_all_compile_options_soc",
132+ "origin_func_name",
133+ "ascendc_src_dir_ex",
134+ "ascendc_src_dir",
135+ "ascendc_src_file",
136+ "src",
137+ "op_type",
138+ "code_channel",
139+ "op_info",
140+ "compile_op",
141+ "get_code_channel",
142+ "result",
143+ "__attrs__",
144+ "isinstance",
145+ "attr",
146+ "get_current_build_config",
147+ "_build_args",
148+ "get_dtype_fmt_options",
149+ "shutil",
150+ "os",
151+ "get_kernel_source",
97}152}
98 153 
99 154 
@@ -126,9 +181,9 @@ class OpDesc:
126 self.output_shape_depend_on_compute = []181 self.output_shape_depend_on_compute = []
127 self.op_fmt_sel = False182 self.op_fmt_sel = False
128 self.op_chk_support = False183 self.op_chk_support = False
129- self.op_intf = ''184+ self.op_intf = ""
130- self.kern_name = ''185+ self.kern_name = ""
131- self.op_file = ''186+ self.op_file = ""
132 self.op_replay_flag = False187 self.op_replay_flag = False
133 self.op_replay_batch = False188 self.op_replay_batch = False
134 self.input_idx = -1189 self.input_idx = -1
@@ -136,11 +191,11 @@ class OpDesc:
136 self.max_block_num = 32191 self.max_block_num = 32
137 self.max_shape_size = 268435456192 self.max_shape_size = 268435456
138 self.dynamic_shape = False193 self.dynamic_shape = False
139- self.op_range_limit = ''194+ self.op_range_limit = ""
140 self.custom_compile_options = {}195 self.custom_compile_options = {}
141 self.custom_all_compile_options = {}196 self.custom_all_compile_options = {}
142- self.impl_mode_str = ''197+ self.impl_mode_str = ""
143- self.json_file = ''198+ self.json_file = ""
144 self.param_type_dynamic = False199 self.param_type_dynamic = False
145 self.mc2_ctx = []200 self.mc2_ctx = []
146 self.bin_cprs_list = []201 self.bin_cprs_list = []
@@ -149,72 +204,76 @@ class OpDesc:
149 204 
150 @staticmethod205 @staticmethod
151 def _parse_digit(conf: str) -> int:206 def _parse_digit(conf: str) -> int:
152- return int(conf.split('=')[1])207+ return int(conf.split("=")[1])
153 208 
154 @staticmethod209 @staticmethod
155 def _parse_flag(conf: str) -> bool:210 def _parse_flag(conf: str) -> bool:
156- if 'true' == conf.split('=')[1]:211+ if "true" == conf.split("=")[1]:
157 return True212 return True
158 return False213 return False
159 214 
160 @staticmethod215 @staticmethod
161 def _parse_str(conf: str) -> str:216 def _parse_str(conf: str) -> str:
162- return conf.split('=')[1]217+ return conf.split("=")[1]
163 218 
164 @staticmethod219 @staticmethod
165 def _parse_list(conf: str) -> list:220 def _parse_list(conf: str) -> list:
166- return conf.split('=')[1].split(',')221+ return conf.split("=")[1].split(",")
167 222 
168 def parse_input(self: any, conf: str):223 def parse_input(self: any, conf: str):
169- if conf.startswith('input{}.name'.format(int(self.input_idx) + 1)):224+ if conf.startswith("input{}.name".format(int(self.input_idx) + 1)):
170 self.input_idx += 1225 self.input_idx += 1
171 self.input_ori_name.append(self._parse_str(conf))226 self.input_ori_name.append(self._parse_str(conf))
172- self.input_name.append(self.input_ori_name[-1] + '_in__')227+ self.input_name.append(self.input_ori_name[-1] + "_in__")
173- elif conf.startswith('input{}.paramType'.format(int(self.input_idx))):228+ elif conf.startswith("input{}.paramType".format(int(self.input_idx))):
174 param_type = self._parse_str(conf)229 param_type = self._parse_str(conf)
175 self.input_type.append(param_type)230 self.input_type.append(param_type)
176 if param_type == "dynamic":231 if param_type == "dynamic":
177 self.param_type_dynamic = True232 self.param_type_dynamic = True
178- elif conf.startswith('input{}.dtype'.format(int(self.input_idx))):233+ elif conf.startswith("input{}.dtype".format(int(self.input_idx))):
179 self.input_dtype.append(self._parse_str(conf))234 self.input_dtype.append(self._parse_str(conf))
180- elif conf.startswith('input{}.for_bin_dtype'.format(int(self.input_idx))):235+ elif conf.startswith("input{}.for_bin_dtype".format(int(self.input_idx))):
181- self.input_dtype_for_bin.update({self.input_idx : self._parse_str(conf)})236+ self.input_dtype_for_bin.update({self.input_idx: self._parse_str(conf)})
182- elif conf.startswith('input{}.format'.format(int(self.input_idx))):237+ elif conf.startswith("input{}.format".format(int(self.input_idx))):
183 self.input_fmt.append(self._parse_str(conf))238 self.input_fmt.append(self._parse_str(conf))
184- elif conf.startswith('input{}.for_bin_format'.format(int(self.input_idx))):239+ elif conf.startswith("input{}.for_bin_format".format(int(self.input_idx))):
185- self.input_fmt_for_bin.update({self.input_idx : self._parse_str(conf)})240+ self.input_fmt_for_bin.update({self.input_idx: self._parse_str(conf)})
186- elif conf.startswith('input{}.virtual'.format(int(self.input_idx))):241+ elif conf.startswith("input{}.virtual".format(int(self.input_idx))):
187 self.input_virt[self.input_idx] = self._parse_str(conf)242 self.input_virt[self.input_idx] = self._parse_str(conf)
188- elif conf.startswith('input{}.valueDepend'.format(int(self.input_idx))):243+ elif conf.startswith("input{}.valueDepend".format(int(self.input_idx))):
189 self.input_value_depend[self.input_idx] = self._parse_str(conf)244 self.input_value_depend[self.input_idx] = self._parse_str(conf)
190- elif conf.startswith('input{}.initValue'.format(int(self.input_idx))):245+ elif conf.startswith("input{}.initValue".format(int(self.input_idx))):
191- raise Exception(f'[ERROR]: Op: {{\'{self.op_type}\'}} input {self.input_ori_name[int(self.input_idx)]}\246+ raise Exception(
192- has InitValue, which is not supported!')247+ f"[ERROR]: Op: {{'{self.op_type}'}} input {self.input_ori_name[int(self.input_idx)]}\
248+ has InitValue, which is not supported!"
249+ )
193 else:250 else:
194 return251 return
195 252 
196 def parse_output(self: any, conf: str):253 def parse_output(self: any, conf: str):
197- if conf.startswith('output{}.name'.format(int(self.output_idx) + 1)):254+ if conf.startswith("output{}.name".format(int(self.output_idx) + 1)):
198 self.output_idx += 1255 self.output_idx += 1
199 self.output_ori_name.append(self._parse_str(conf))256 self.output_ori_name.append(self._parse_str(conf))
200- self.output_name.append(self.output_ori_name[-1] + '_out_')257+ self.output_name.append(self.output_ori_name[-1] + "_out_")
201 self.output_init_value.append(None)258 self.output_init_value.append(None)
202- elif conf.startswith('output{}.paramType'.format(int(self.output_idx))):259+ elif conf.startswith("output{}.paramType".format(int(self.output_idx))):
203 param_type = self._parse_str(conf)260 param_type = self._parse_str(conf)
204 self.output_type.append(param_type)261 self.output_type.append(param_type)
205 if param_type == "dynamic":262 if param_type == "dynamic":
206 self.param_type_dynamic = True263 self.param_type_dynamic = True
207- elif conf.startswith('output{}.dtype'.format(int(self.output_idx))):264+ elif conf.startswith("output{}.dtype".format(int(self.output_idx))):
208 self.output_dtype.append(self._parse_str(conf))265 self.output_dtype.append(self._parse_str(conf))
209- elif conf.startswith('output{}.for_bin_dtype'.format(int(self.output_idx))):266+ elif conf.startswith("output{}.for_bin_dtype".format(int(self.output_idx))):
210- self.output_dtype_for_bin.update({self.output_idx : self._parse_str(conf)})267+ self.output_dtype_for_bin.update({self.output_idx: self._parse_str(conf)})
211- elif conf.startswith('output{}.format'.format(int(self.output_idx))):268+ elif conf.startswith("output{}.format".format(int(self.output_idx))):
212 self.output_fmt.append(self._parse_str(conf))269 self.output_fmt.append(self._parse_str(conf))
213- elif conf.startswith('output{}.for_bin_format'.format(int(self.output_idx))):270+ elif conf.startswith("output{}.for_bin_format".format(int(self.output_idx))):
214- self.output_fmt_for_bin.update({self.output_idx : self._parse_str(conf)})271+ self.output_fmt_for_bin.update({self.output_idx: self._parse_str(conf)})
215- elif conf.startswith('output{}.initValue'.format(int(self.output_idx))):272+ elif conf.startswith("output{}.initValue".format(int(self.output_idx))):
216 self.output_init_value[int(self.output_idx)] = self._parse_str(conf)273 self.output_init_value[int(self.output_idx)] = self._parse_str(conf)
217- elif conf.startswith('output{}.outputShapeDependOnCompute=true'.format(int(self.output_idx))):274+ elif conf.startswith(
275+ "output{}.outputShapeDependOnCompute=true".format(int(self.output_idx))
276+ ):
218 self.output_shape_depend_on_compute.append(int(self.output_idx))277 self.output_shape_depend_on_compute.append(int(self.output_idx))
219 else:278 else:
220 return279 return
@@ -244,20 +303,22 @@ class OpDesc:
244 self.attr_list = self._parse_list(conf)303 self.attr_list = self._parse_list(conf)
245 intersection_element = set(self.attr_list) & CONFLICT_KEYWORDS304 intersection_element = set(self.attr_list) & CONFLICT_KEYWORDS
246 if intersection_element:305 if intersection_element:
247- raise Exception(f'[ERROR]: The attribute name: {intersection_element} in op: {{\'{self.op_type}\'}} \306+ raise Exception(
248-conflicts with the built-in variable name. Use a complex name or prefix the operator name.')307+ f"[ERROR]: The attribute name: {intersection_element} in op: {{'{self.op_type}'}} \
308+conflicts with the built-in variable name. Use a complex name or prefix the operator name."
309+ )
249 310 
250 def parse_mc2_ctx(self: any, conf: str):311 def parse_mc2_ctx(self: any, conf: str):
251 self.mc2_ctx = self._parse_list(conf)312 self.mc2_ctx = self._parse_list(conf)
252 313 
253 @staticmethod314 @staticmethod
254 def _camel_to_snake(camel_case_str: str):315 def _camel_to_snake(camel_case_str: str):
255- snake_case_str = ''316+ snake_case_str = ""
256 for i, c in enumerate(camel_case_str):317 for i, c in enumerate(camel_case_str):
257 if i == 0:318 if i == 0:
258 snake_case_str += c.lower()319 snake_case_str += c.lower()
259 elif c.isupper():320 elif c.isupper():
260- snake_case_str += '_' + c.lower()321+ snake_case_str += "_" + c.lower()
261 else:322 else:
262 snake_case_str += c323 snake_case_str += c
263 return snake_case_str324 return snake_case_str
@@ -266,12 +327,14 @@ conflicts with the built-in variable name. Use a complex name or prefix the oper
266 for attr in self.attr_list:327 for attr in self.attr_list:
267 if self.attr_val.get(attr) is None:328 if self.attr_val.get(attr) is None:
268 self.attr_val[attr] = {}329 self.attr_val[attr] = {}
269- if conf.startswith('attr_{}.type'.format(attr)):330+ if conf.startswith("attr_{}.type".format(attr)):
270- self.attr_val.get(attr)['type'] = self._camel_to_snake(self._parse_str(conf))331+ self.attr_val.get(attr)["type"] = self._camel_to_snake(
271- elif conf.startswith('attr_{}.paramType'.format(attr)):332+ self._parse_str(conf)
272- self.attr_val.get(attr)['paramType'] = self._parse_str(conf)333+ )
273- elif conf.startswith('attr_{}.defaultValue'.format(attr)):334+ elif conf.startswith("attr_{}.paramType".format(attr)):
274- self.attr_val.get(attr)['defaultValue'] = self._parse_str(conf)335+ self.attr_val.get(attr)["paramType"] = self._parse_str(conf)
336+ elif conf.startswith("attr_{}.defaultValue".format(attr)):
337+ self.attr_val.get(attr)["defaultValue"] = self._parse_str(conf)
275 338 
276 def parse_replay_val(self: any, batch_list: list, iterator_list: list):339 def parse_replay_val(self: any, batch_list: list, iterator_list: list):
277 if self.op_type in batch_list:340 if self.op_type in batch_list:
@@ -308,7 +371,8 @@ def _trans_soc_ver_to_short(soc_ver: str):
308 if low_soc_ver not in SOC_TO_SHORT_SOC_MAP:371 if low_soc_ver not in SOC_TO_SHORT_SOC_MAP:
309 raise ValueError(372 raise ValueError(
310 f"Unsupported SOC version: '{soc_ver}'. "373 f"Unsupported SOC version: '{soc_ver}'. "
311- f"Please verify with `npu-smi info` and ensure correct version string.")374+ f"Please verify with `npu-smi info` and ensure correct version string."
375+ )
312 return SOC_TO_SHORT_SOC_MAP[low_soc_ver]376 return SOC_TO_SHORT_SOC_MAP[low_soc_ver]
313 377 
314 378 
@@ -326,46 +390,58 @@ def _get_op_custom_options(op_descs: list, auto_gen_dir: str):
326 return {}390 return {}
327 file = os.path.join(auto_gen_dir, "custom_compile_options.ini")391 file = os.path.join(auto_gen_dir, "custom_compile_options.ini")
328 if not os.path.exists(file):392 if not os.path.exists(file):
329- print(f'WARNING: cannot find {auto_gen_dir}/custom_compile_options.ini')393+ print(f"WARNING: cannot find {auto_gen_dir}/custom_compile_options.ini")
330 return {}394 return {}
331- with open (file, 'r') as fd:395+ with open(file, "r") as fd:
332 lines = fd.readlines()396 lines = fd.readlines()
333 for line in lines:397 for line in lines:
334- param_list = str.split(line.rstrip('\n'), ',')398+ param_list = str.split(line.rstrip("\n"), ",")
335 if len(param_list) != 3:399 if len(param_list) != 3:
336- raise Exception(f'ERROR: custom compile option {param_list} len is not 3')400+ raise Exception(
401+ f"ERROR: custom compile option {param_list} len is not 3"
402+ )
337 op_type = param_list[0]403 op_type = param_list[0]
338- if op_type.upper() == 'ALL':404+ if op_type.upper() == "ALL":
339 op_type = OP_ALL405 op_type = OP_ALL
340 if op_type != OP_ALL and _is_op_type_in_opdesc(op_descs, op_type) == False:406 if op_type != OP_ALL and _is_op_type_in_opdesc(op_descs, op_type) == False:
341 continue407 continue
342 soc_ver_compile_options = {}408 soc_ver_compile_options = {}
343 soc_ver = param_list[1]409 soc_ver = param_list[1]
344 options_str = param_list[2]410 options_str = param_list[2]
345- options = str.split(options_str, ';')411+ options = str.split(options_str, ";")
346 tmp_impl_mode = get_impl_mode(options)412 tmp_impl_mode = get_impl_mode(options)
347- if soc_ver == '':413+ if soc_ver == "":
348 soc_ver_compile_options[SOC_ALL] = options414 soc_ver_compile_options[SOC_ALL] = options
349 else:415 else:
350- soc_ver_list = str.split(soc_ver, ';')416+ soc_ver_list = str.split(soc_ver, ";")
351 for ver in soc_ver_list:417 for ver in soc_ver_list:
352 short_ver = _trans_soc_ver_to_short(ver)418 short_ver = _trans_soc_ver_to_short(ver)
353 soc_ver_compile_options[short_ver] = options419 soc_ver_compile_options[short_ver] = options
354 if op_type == OP_ALL:420 if op_type == OP_ALL:
355- _set_all_options_to_opdescs(op_descs, soc_ver_compile_options, tmp_impl_mode)421+ _set_all_options_to_opdescs(
422+ op_descs, soc_ver_compile_options, tmp_impl_mode
423+ )
356 else:424 else:
357- _set_options_to_opdesc(op_descs, op_type, soc_ver_compile_options, tmp_impl_mode)425+ _set_options_to_opdesc(
426+ op_descs, op_type, soc_ver_compile_options, tmp_impl_mode
427+ )
358 428 
359 429 
360-def get_op_desc(file: str, batch_list: list, iterator_list: list, builder: any,430+def get_op_desc(
361- op_type: list, auto_gen_dir: str = None) -> list:431+ file: str,
432+ batch_list: list,
433+ iterator_list: list,
434+ builder: any,
435+ op_type: list,
436+ auto_gen_dir: str = None,
437+) -> list:
362 op_descs = []438 op_descs = []
363 op_match = False439 op_match = False
364- with open(file, 'r') as fd:440+ with open(file, "r") as fd:
365 lines = fd.readlines()441 lines = fd.readlines()
366 for line in lines:442 for line in lines:
367 line = line.strip()443 line = line.strip()
368- if line.startswith('['):444+ if line.startswith("["):
369 name = line[1:-1]445 name = line[1:-1]
370 if op_type is None or name in op_type:446 if op_type is None or name in op_type:
371 op_match = True447 op_match = True
@@ -23,25 +23,118 @@ import stat
23import sys23import sys
24 24 
25 25 
26-ATTR_TYPE_LIST = ["int", "float", "bool", "str", "listInt", "listFloat", "listBool", "listStr", "listListInt",26+ATTR_TYPE_LIST = [
27- "type", "listType", "tensor", "listTensor"]27+ "int",
28+ "float",
29+ "bool",
30+ "str",
31+ "listInt",
32+ "listFloat",
33+ "listBool",
34+ "listStr",
35+ "listListInt",
36+ "type",
37+ "listType",
38+ "tensor",
39+ "listTensor",
40+]
28ATTR_PARAMTYPE_LIST = ["optional", "required"]41ATTR_PARAMTYPE_LIST = ["optional", "required"]
29-BOOL_FLAG_KEY = ["dynamicFormat", "dynamicShapeSupport", "dynamicRankSupport", "precision_reduce", "heavyOp",42+BOOL_FLAG_KEY = [
30- "needCheckSupport", "enableVectorCore"]43+ "dynamicFormat",
44+ "dynamicShapeSupport",
45+ "dynamicRankSupport",
46+ "precision_reduce",
47+ "heavyOp",
48+ "needCheckSupport",
49+ "enableVectorCore",
50+]
31BOOL_LIST = ["true", "false"]51BOOL_LIST = ["true", "false"]
32-DTYPE_LIST = ["float16", "float", "float32", "int8", "int16", "int32", "uint8", "uint16", "uint32", "bool",52+DTYPE_LIST = [
33- "int64", "uint64", "qint8", "qint16", "qint32", "quint8", "quint16", "double", "complex32", "complex64",53+ "float16",
34- "complex128", "string", "resource", "dual", "dual_sub_int8", "dual_sub_uint8", "string_ref",54+ "float",
35- "int4", "bfloat16", "uint1", "hifloat8", "float8_e4m3fn", "float8_e5m2", "float8_e8m0", "float4_e2m1",55+ "float32",
36- "float4_e1m2", "int2"]56+ "int8",
37-FORMAT_LIST = ["NCHW", "NHWC", "ND", "NC1HWC0", "FRACTAL_Z", "NC1C0HWPAD", "NHWC1C0", "FSR_NCHW", "FRACTAL_DECONV",57+ "int16",
38- "C1HWNC0", "FRACTAL_DECONV_TRANSPOSE", "FRACTAL_DECONV_SP_STRIDE_TRANS", "NC1HWC0_C04",58+ "int32",
39- "FRACTAL_Z_C04", "CHWN", "FRACTAL_DECONV_SP_STRIDE8_TRANS", "HWCN", "NC1KHKWHWC0", "BN_WEIGHT",59+ "uint8",
40- "FILTER_HWCK", "HASHTABLE_LOOKUP_LOOKUPS", "HASHTABLE_LOOKUP_KEYS", "HASHTABLE_LOOKUP_VALUE",60+ "uint16",
41- "HASHTABLE_LOOKUP_OUTPUT", "HASHTABLE_LOOKUP_HITS", "C1HWNCoC0", "MD", "NDHWC", "FRACTAL_ZZ",61+ "uint32",
42- "FRACTAL_NZ", "NCDHW", "DHWCN", "NDC1HWC0", "FRACTAL_Z_3D", "CN", "NC", "DHWNC",62+ "bool",
43- "FRACTAL_Z_3D_TRANSPOSE", "FRACTAL_ZN_LSTM", "FRACTAL_ZN_RNN", "FRACTAL_Z_G",63+ "int64",
44- "FRACTAL_NZ_C0_2", "FRACTAL_NZ_C0_4", "FRACTAL_NZ_C0_8", "NULL"]64+ "uint64",
65+ "qint8",
66+ "qint16",
67+ "qint32",
68+ "quint8",
69+ "quint16",
70+ "double",
71+ "complex32",
72+ "complex64",
73+ "complex128",
74+ "string",
75+ "resource",
76+ "dual",
77+ "dual_sub_int8",
78+ "dual_sub_uint8",
79+ "string_ref",
80+ "int4",
81+ "bfloat16",
82+ "uint1",
83+ "hifloat8",
84+ "float8_e4m3fn",
85+ "float8_e5m2",
86+ "float8_e8m0",
87+ "float4_e2m1",
88+ "float4_e1m2",
89+ "int2",
90+]
91+FORMAT_LIST = [
92+ "NCHW",
93+ "NHWC",
94+ "ND",
95+ "NC1HWC0",
96+ "FRACTAL_Z",
97+ "NC1C0HWPAD",
98+ "NHWC1C0",
99+ "FSR_NCHW",
100+ "FRACTAL_DECONV",
101+ "C1HWNC0",
102+ "FRACTAL_DECONV_TRANSPOSE",
103+ "FRACTAL_DECONV_SP_STRIDE_TRANS",
104+ "NC1HWC0_C04",
105+ "FRACTAL_Z_C04",
106+ "CHWN",
107+ "FRACTAL_DECONV_SP_STRIDE8_TRANS",
108+ "HWCN",
109+ "NC1KHKWHWC0",
110+ "BN_WEIGHT",
111+ "FILTER_HWCK",
112+ "HASHTABLE_LOOKUP_LOOKUPS",
113+ "HASHTABLE_LOOKUP_KEYS",
114+ "HASHTABLE_LOOKUP_VALUE",
115+ "HASHTABLE_LOOKUP_OUTPUT",
116+ "HASHTABLE_LOOKUP_HITS",
117+ "C1HWNCoC0",
118+ "MD",
119+ "NDHWC",
120+ "FRACTAL_ZZ",
121+ "FRACTAL_NZ",
122+ "NCDHW",
123+ "DHWCN",
124+ "NDC1HWC0",
125+ "FRACTAL_Z_3D",
126+ "CN",
127+ "NC",
128+ "DHWNC",
129+ "FRACTAL_Z_3D_TRANSPOSE",
130+ "FRACTAL_ZN_LSTM",
131+ "FRACTAL_ZN_RNN",
132+ "FRACTAL_Z_G",
133+ "FRACTAL_NZ_C0_2",
134+ "FRACTAL_NZ_C0_4",
135+ "FRACTAL_NZ_C0_8",
136+ "NULL",
137+]
45 138 
46 139 
47def parse_ini_files(ini_files):140def parse_ini_files(ini_files):
@@ -66,8 +159,11 @@ def check_file_size(input_file):
66 except OSError as os_error:159 except OSError as os_error:
67 print('[ERROR] Failed to open "%s". %s' % (input_file, str(os_error)))160 print('[ERROR] Failed to open "%s". %s' % (input_file, str(os_error)))
68 raise OSError from os_error161 raise OSError from os_error
69- if file_size > 10*1024*1024:162+ if file_size > 10 * 1024 * 1024:
70- print('[WARN] The size of %s exceeds 10MB, it may take more time to run, please wait.' % input_file)163+ print(
164+ "[WARN] The size of %s exceeds 10MB, it may take more time to run, please wait."
165+ % input_file
166+ )
71 167 
72 168 
73def parse_ini_to_obj(ini_file, tbe_ops_info):169def parse_ini_to_obj(ini_file, tbe_ops_info):
@@ -95,14 +191,15 @@ def parse_ini_to_obj(ini_file, tbe_ops_info):
95 tbe_ops_info[op_type] = op_dict191 tbe_ops_info[op_type] = op_dict
96 find_op_type = True192 find_op_type = True
97 elif "=" in line:193 elif "=" in line:
98- key1 = line[:line.index("=")]194+ key1 = line[: line.index("=")]
99- key2 = line[line.index("=")+1:]195+ key2 = line[line.index("=") + 1 :]
100 key1_0, key1_1 = key1.split(".")196 key1_0, key1_1 = key1.split(".")
101 if key1_0 not in op_dict:197 if key1_0 not in op_dict:
102 op_dict[key1_0] = {}198 op_dict[key1_0] = {}
103 if key1_1 in op_dict.get(key1_0):199 if key1_1 in op_dict.get(key1_0):
104- raise RuntimeError("Op:" + op_type + " " + key1_0 + " " +200+ raise RuntimeError(
105- key1_1 + " is repeated!")201+ "Op:" + op_type + " " + key1_0 + " " + key1_1 + " is repeated!"
202+ )
106 dic_key = op_dict.get(key1_0)203 dic_key = op_dict.get(key1_0)
107 dic_key[key1_1] = key2204 dic_key[key1_1] = key2
108 else:205 else:
@@ -174,7 +271,10 @@ def check_attr(op_dict, is_valid):
174 is_valid = check_attr_dict(attr_dict, is_valid, attr)271 is_valid = check_attr_dict(attr_dict, is_valid, attr)
175 else:272 else:
176 is_valid = False273 is_valid = False
177- print("%s is required in .ini file, when attr.list is %s!" % (attr, attr_list_str))274+ print(
275+ "%s is required in .ini file, when attr.list is %s!"
276+ % (attr, attr_list_str)
277+ )
178 return is_valid278 return is_valid
179 279 
180 280 
@@ -223,7 +323,11 @@ def check_type_format(op_info, is_valid, op_info_key):
223 if op_info_dtype_num > 0 and op_info_format_num > 0:323 if op_info_dtype_num > 0 and op_info_format_num > 0:
224 if op_info_dtype_num != op_info_format_num:324 if op_info_dtype_num != op_info_format_num:
225 is_valid = False325 is_valid = False
226- print("The number of {0}.dtype not match the number of {0}.format.".format(op_info_key))326+ print(
327+ "The number of {0}.dtype not match the number of {0}.format.".format(
328+ op_info_key
329+ )
330+ )
227 return is_valid331 return is_valid
228 332 
229 333 
@@ -249,14 +353,25 @@ def check_op_info(tbe_ops):
249 if required_op_input_info_key not in op_input_info:353 if required_op_input_info_key not in op_input_info:
250 missing_keys.append(required_op_input_info_key)354 missing_keys.append(required_op_input_info_key)
251 if len(missing_keys) > 0:355 if len(missing_keys) > 0:
252- print("op: " + op_key + " " + op_info_key + " missing: " +356+ print(
253- ",".join(missing_keys))357+ "op: "
358+ + op_key
359+ + " "
360+ + op_info_key
361+ + " missing: "
362+ + ",".join(missing_keys)
363+ )
254 is_valid = False364 is_valid = False
255 else:365 else:
256- if not op_input_info["paramType"] in param_type_valid_value:366+ if op_input_info["paramType"] not in param_type_valid_value:
257- print("op: " + op_key + " " + op_info_key + \367+ print(
258- " paramType not valid, valid key:[dynamic, "368+ "op: "
259- "optional, required]")369+ + op_key
370+ + " "
371+ + op_info_key
372+ + " paramType not valid, valid key:[dynamic, "
373+ "optional, required]"
374+ )
260 is_valid = False375 is_valid = False
261 is_valid = check_type_format(op_input_info, is_valid, op_info_key)376 is_valid = check_type_format(op_input_info, is_valid, op_info_key)
262 if op_info_key.startswith("output"):377 if op_info_key.startswith("output"):
@@ -266,14 +381,25 @@ def check_op_info(tbe_ops):
266 if required_op_input_info_key not in op_input_info:381 if required_op_input_info_key not in op_input_info:
267 missing_keys.append(required_op_input_info_key)382 missing_keys.append(required_op_input_info_key)
268 if len(missing_keys) > 0:383 if len(missing_keys) > 0:
269- print("op: " + op_key + " " + op_info_key + " missing: " +384+ print(
270- ",".join(missing_keys))385+ "op: "
386+ + op_key
387+ + " "
388+ + op_info_key
389+ + " missing: "
390+ + ",".join(missing_keys)
391+ )
271 is_valid = False392 is_valid = False
272 else:393 else:
273- if not op_input_info["paramType"] in param_type_valid_value:394+ if op_input_info["paramType"] not in param_type_valid_value:
274- print("op: " + op_key + " " + op_info_key +395+ print(
275- " paramType not valid, valid key:[dynamic, "396+ "op: "
276- "optional, required]")397+ + op_key
398+ + " "
399+ + op_info_key
400+ + " paramType not valid, valid key:[dynamic, "
401+ "optional, required]"
402+ )
277 is_valid = False403 is_valid = False
278 is_valid = check_type_format(op_input_info, is_valid, op_info_key)404 is_valid = check_type_format(op_input_info, is_valid, op_info_key)
279 is_valid = check_attr(op_dict, is_valid)405 is_valid = check_attr(op_dict, is_valid)
@@ -294,12 +420,12 @@ def write_json_file(tbe_ops_info, json_file_path):
294 json_file_real_path = os.path.realpath(json_file_path)420 json_file_real_path = os.path.realpath(json_file_path)
295 wr_flag = os.O_WRONLY | os.O_CREAT421 wr_flag = os.O_WRONLY | os.O_CREAT
296 wr_mode = stat.S_IWUSR | stat.S_IRUSR422 wr_mode = stat.S_IWUSR | stat.S_IRUSR
297- with os.fdopen(os.open(json_file_real_path, wr_flag, wr_mode), 'w') as file_path:423+ with os.fdopen(os.open(json_file_real_path, wr_flag, wr_mode), "w") as file_path:
298 # The owner have all rights£¬group only have read rights424 # The owner have all rights£¬group only have read rights
299- os.chmod(json_file_real_path, stat.S_IWUSR + stat.S_IRGRP425+ os.chmod(json_file_real_path, stat.S_IWUSR + stat.S_IRGRP + stat.S_IRUSR)
300- + stat.S_IRUSR)426+ json.dump(
301- json.dump(tbe_ops_info, file_path, sort_keys=True, indent=4,427+ tbe_ops_info, file_path, sort_keys=True, indent=4, separators=(",", ":")
302- separators=(',', ':'))428+ )
303 print("Compile op info cfg successfully.")429 print("Compile op info cfg successfully.")
304 430 
305 431 
@@ -320,7 +446,7 @@ def parse_ini_to_json(ini_file_paths, outfile_path):
320 return True446 return True
321 447 
322 448 
323-if __name__ == '__main__':449+if __name__ == "__main__":
324 args = sys.argv450 args = sys.argv
325 451 
326 OUTPUT_FILE_PATH = "tbe_ops_info.json"452 OUTPUT_FILE_PATH = "tbe_ops_info.json"
@@ -16,7 +16,7 @@ import os
16 16 
17 17 
18def read_json(file):18def read_json(file):
19- with open(file, 'r') as fd:19+ with open(file, "r") as fd:
20 config = json.load(fd)20 config = json.load(fd)
21 return config21 return config
22 22 
@@ -25,17 +25,17 @@ def get_config_opts(file):
25 config = read_json(file)25 config = read_json(file)
26 26 
27 src_dir = os.path.abspath(os.path.dirname(file))27 src_dir = os.path.abspath(os.path.dirname(file))
28- opts = ''28+ opts = ""
29 29 
30 for conf in config:30 for conf in config:
31- if conf == 'configurePresets':31+ if conf == "configurePresets":
32 for node in config[conf]:32 for node in config[conf]:
33- macros = node.get('cacheVariables')33+ macros = node.get("cacheVariables")
34 if macros is not None:34 if macros is not None:
35 for key in macros:35 for key in macros:
36- opts += '-D{}={} '.format(key, macros[key]['value'])36+ opts += "-D{}={} ".format(key, macros[key]["value"])
37 37 
38- opts = opts.replace('${sourceDir}', src_dir)38+ opts = opts.replace("${sourceDir}", src_dir)
39 print(opts)39 print(opts)
40 40 
41 41 
@@ -13,6 +13,7 @@
13"""13"""
14split_opc_json_with_op_list.py14split_opc_json_with_op_list.py
15"""15"""
16+ 
16import sys17import sys
17import os18import os
18import json19import json
@@ -62,5 +63,6 @@ def split_json_files(ori_json, output_dir):
62 63 
63 return generated_files64 return generated_files
64 65 
65-if __name__ == '__main__':66+ 
67+if __name__ == "__main__":
66 generated_files = split_json_files(sys.argv[1], sys.argv[2])68 generated_files = split_json_files(sys.argv[1], sys.argv[2])
@@ -21,32 +21,32 @@ CODE_MAX = 3
21 21 
22 22 
23def v220_mode(inst) -> int:23def v220_mode(inst) -> int:
24- if inst[6] == 'f':24+ if inst[6] == "f":
25 # MATRIX25 # MATRIX
26 return CODE_AIC26 return CODE_AIC
27- elif inst[6] == 'c':27+ elif inst[6] == "c":
28 # FIXP28 # FIXP
29 return CODE_AIC29 return CODE_AIC
30- elif inst[6] == '8':30+ elif inst[6] == "8":
31- if inst[4] == '4' and inst[5] == '0' and inst[7] == '0':31+ if inst[4] == "4" and inst[5] == "0" and inst[7] == "0":
32 # MOVEMASK32 # MOVEMASK
33 return 033 return 0
34 # SIMD INST34 # SIMD INST
35 return CODE_AIV35 return CODE_AIV
36- elif inst[6] == '9':36+ elif inst[6] == "9":
37 # SIMD INST37 # SIMD INST
38 return CODE_AIV38 return CODE_AIV
39 # DMA MOVE39 # DMA MOVE
40- elif inst[6] == '6':40+ elif inst[6] == "6":
41- if inst[7] == 'b' and (int(inst[4], 16) & 0x8) == 0x8:41+ if inst[7] == "b" and (int(inst[4], 16) & 0x8) == 0x8:
42 # MOV_{SRC}_TO_{DST}_ALIGN42 # MOV_{SRC}_TO_{DST}_ALIGN
43 return CODE_AIV43 return CODE_AIV
44 else:44 else:
45 # MOV_CUB145 # MOV_CUB1
46 return CODE_AIC46 return CODE_AIC
47 # DMA MOVE47 # DMA MOVE
48- elif inst[6] == '7':48+ elif inst[6] == "7":
49- if inst[7] == '0' and (int(inst[4], 16) & 0x8) == 0x8:49+ if inst[7] == "0" and (int(inst[4], 16) & 0x8) == 0x8:
50 # MOV_UB_TO_XX50 # MOV_UB_TO_XX
51 return CODE_AIV51 return CODE_AIV
52 elif (int(inst[0], 16) & 0x7) == 0 and (int(inst[1], 16) & 0x8) == 0x8:52 elif (int(inst[0], 16) & 0x7) == 0 and (int(inst[1], 16) & 0x8) == 0x8:
@@ -61,22 +61,22 @@ def v220_mode(inst) -> int:
61 61 
62def v310_mode_vec_ofile(little_endian, binary_32) -> int:62def v310_mode_vec_ofile(little_endian, binary_32) -> int:
63 vf_high_low_map = {63 vf_high_low_map = {
64- '01000010011000100': '00', # wait_intra_block64+ "01000010011000100": "00", # wait_intra_block
65- '01000010010000100': '00', # set_intra_block65+ "01000010010000100": "00", # set_intra_block
66- '01000000101000100': '00', # set_flag66+ "01000000101000100": "00", # set_flag
67- '01000000110000100': '00', # wait_flag67+ "01000000110000100": "00", # wait_flag
68 }68 }
69 # DMA69 # DMA
70 vec_high_low_map = {70 vec_high_low_map = {
71- '0110101010': '1', # ND_DMA_DCI71+ "0110101010": "1", # ND_DMA_DCI
72 }72 }
73 vec_high_map = {73 vec_high_map = {
74- '0110111001', # ND_DMA_OUT_TO_UB74+ "0110111001", # ND_DMA_OUT_TO_UB
75- '0111010010', # MOV_OUT_TO_UB_ALIGN_V275+ "0111010010", # MOV_OUT_TO_UB_ALIGN_V2
76- '0111010011', # MOV_UB_TO_OUT_ALIGN_V276+ "0111010011", # MOV_UB_TO_OUT_ALIGN_V2
77 }77 }
78 vec_high_mid_map = {78 vec_high_mid_map = {
79- '011100001': '0100', # MOV_UB_TO_L179+ "011100001": "0100", # MOV_UB_TO_L1
80 }80 }
81 high_9 = binary_32[:9]81 high_9 = binary_32[:9]
82 high_10 = binary_32[:10]82 high_10 = binary_32[:10]
@@ -85,15 +85,20 @@ def v310_mode_vec_ofile(little_endian, binary_32) -> int:
85 85 
86 conditions = [86 conditions = [
87 # PIPE_V: exclude movemask87 # PIPE_V: exclude movemask
88- (little_endian[0] == '1' and little_endian[1] == '5' and88+ (
89- not (binary_32[:11] == "00010101110" and89+ little_endian[0] == "1"
90- binary_32[16:26] == "0000000000" and90+ and little_endian[1] == "5"
91- binary_32[27:] == "10011")),91+ and not (
92- 92+ binary_32[:11] == "00010101110"
93+ and binary_32[16:26] == "0000000000"
94+ and binary_32[27:] == "10011"
95+ )
96+ ),
93 # set, wait97 # set, wait
94- (binary_32[:17] in vf_high_low_map and98+ (
95- binary_32[30:] == vf_high_low_map[binary_32[:17]]),99+ binary_32[:17] in vf_high_low_map
96- 100+ and binary_32[30:] == vf_high_low_map[binary_32[:17]]
101+ ),
97 # DMA102 # DMA
98 (high_10 in vec_high_low_map and low_1 == vec_high_low_map[high_10]),103 (high_10 in vec_high_low_map and low_1 == vec_high_low_map[high_10]),
99 (high_10 in vec_high_map),104 (high_10 in vec_high_map),
@@ -108,28 +113,28 @@ def v310_mode_vec_ofile(little_endian, binary_32) -> int:
108 113 
109def v310_mode_cube_ofile(little_endian, binary_32) -> int:114def v310_mode_cube_ofile(little_endian, binary_32) -> int:
110 cube_high_low_map = {115 cube_high_low_map = {
111- '0110000001': '10', # SET_L1_2D116+ "0110000001": "10", # SET_L1_2D
112- '0110011000': '10', # LOAD_OUT_TO_L1_2Dv2117+ "0110011000": "10", # LOAD_OUT_TO_L1_2Dv2
113 }118 }
114 cube_high_low2_map = {119 cube_high_low2_map = {
115- '0110110100': '1', # LOAD_L1_TO_L0B_2D_TRANSPOSE120+ "0110110100": "1", # LOAD_L1_TO_L0B_2D_TRANSPOSE
116- '0110110101': '1',121+ "0110110101": "1",
117- '0110110110': '1',122+ "0110110110": "1",
118- '0110110111': '1',123+ "0110110111": "1",
119 }124 }
120 cube_high_map = {125 cube_high_map = {
121- '0110110000', # LOAD_L1_TO_L0A_2Dv2和LOAD_L1_TO_L0B_2Dv2126+ "0110110000", # LOAD_L1_TO_L0A_2Dv2和LOAD_L1_TO_L0B_2Dv2
122- '0110110001',127+ "0110110001",
123- '0110110010',128+ "0110110010",
124- '0110110011',129+ "0110110011",
125- '0110101000', # MOV_OUT _TO_L1 _MULTI_DN2NZ130+ "0110101000", # MOV_OUT _TO_L1 _MULTI_DN2NZ
126- '0110101100', # MOV_OUT _TO_L1 _MULTI_ND2NZ131+ "0110101100", # MOV_OUT _TO_L1 _MULTI_ND2NZ
127- '0110111010', # MOV_OUT_TO_L1_V2132+ "0110111010", # MOV_OUT_TO_L1_V2
128- '0111011000', # LOAD_L1_TO_L0A_MX_2Dv2和LOAD_L1_TO_L0B_MX_2Dv2133+ "0111011000", # LOAD_L1_TO_L0A_MX_2Dv2和LOAD_L1_TO_L0B_MX_2Dv2
129- '0110011100', # LOAD_L1_TO_L0A_3Dv2和LOAD_L1_TO_L0B_3Dv2134+ "0110011100", # LOAD_L1_TO_L0A_3Dv2和LOAD_L1_TO_L0B_3Dv2
130- '0110011101',135+ "0110011101",
131- '0110010100',136+ "0110010100",
132- '0110010101',137+ "0110010101",
133 }138 }
134 high_9 = binary_32[:9]139 high_9 = binary_32[:9]
135 high_10 = binary_32[:10]140 high_10 = binary_32[:10]
@@ -137,18 +142,33 @@ def v310_mode_cube_ofile(little_endian, binary_32) -> int:
137 142 
138 conditions = [143 conditions = [
139 # Fixpipe144 # Fixpipe
140- (little_endian[0] == 'c' and little_endian[1] in '0123'),145+ (little_endian[0] == "c" and little_endian[1] in "0123"),
141- 
142 # matrix instr146 # matrix instr
143- (little_endian[0] == 'e' and little_endian[1] in '012345' and binary_32[30:] == '00'),147+ (
144- (little_endian[0] == 'f' and little_endian[1] in '01'),148+ little_endian[0] == "e"
145- (little_endian[0] == 'f' and little_endian[1] == '6' and binary_32[30:] == '00'),149+ and little_endian[1] in "012345"
146- (little_endian[0] == 'f' and little_endian[1] in '2345abcd' and binary_32[30] == '0'),150+ and binary_32[30:] == "00"
147- 151+ ),
152+ (little_endian[0] == "f" and little_endian[1] in "01"),
153+ (
154+ little_endian[0] == "f"
155+ and little_endian[1] == "6"
156+ and binary_32[30:] == "00"
157+ ),
158+ (
159+ little_endian[0] == "f"
160+ and little_endian[1] in "2345abcd"
161+ and binary_32[30] == "0"
162+ ),
148 # DMA163 # DMA
149- (high_9 == '011100100' and mid_36 in ('0001', '0101')), # DMA move inst, include MOV L1 TO UB164+ (
165+ high_9 == "011100100" and mid_36 in ("0001", "0101")
166+ ), # DMA move inst, include MOV L1 TO UB
150 (high_10 in cube_high_low_map and binary_32[30:] == cube_high_low_map[high_10]),167 (high_10 in cube_high_low_map and binary_32[30:] == cube_high_low_map[high_10]),
151- (high_10 in cube_high_low2_map and binary_32[31] == cube_high_low2_map[high_10]),168+ (
169+ high_10 in cube_high_low2_map
170+ and binary_32[31] == cube_high_low2_map[high_10]
171+ ),
152 (high_10 in cube_high_map),172 (high_10 in cube_high_map),
153 ]173 ]
154 174 
@@ -172,20 +192,22 @@ def v310_mode(inst, cubemode) -> int:
172 )192 )
173 193 
174 194 
175-def get_code_channel(dst_file: str, c310mode_cubemode_tuple: Tuple) -> Tuple[bool, int, str]:195+def get_code_channel(
196+ dst_file: str, c310mode_cubemode_tuple: Tuple
197+) -> Tuple[bool, int, str]:
176 c310mode, cubemode = c310mode_cubemode_tuple198 c310mode, cubemode = c310mode_cubemode_tuple
177 if not os.path.isfile(dst_file):199 if not os.path.isfile(dst_file):
178 return False, CODE_DEFAULT, f"file {dst_file} doesn't exist."200 return False, CODE_DEFAULT, f"file {dst_file} doesn't exist."
179 201 
180- objdump_cmd = ['llvm-objdump', '-s', '-j', '.text', dst_file]202+ objdump_cmd = ["llvm-objdump", "-s", "-j", ".text", dst_file]
181 proc = subprocess.run(203 proc = subprocess.run(
182 objdump_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False204 objdump_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False
183 )205 )
184 out = proc.stdout.decode()206 out = proc.stdout.decode()
185 if proc.returncode != 0:207 if proc.returncode != 0:
186- return False, CODE_DEFAULT, f'llvm-objdump error, message is {out}'208+ return False, CODE_DEFAULT, f"llvm-objdump error, message is {out}"
187 mode = 0209 mode = 0
188- lines = out.split('\n')210+ lines = out.split("\n")
189 for line in lines:211 for line in lines:
190 insts = line.strip().split()212 insts = line.strip().split()
191 if len(insts) < 5:213 if len(insts) < 5:
@@ -199,5 +221,5 @@ def get_code_channel(dst_file: str, c310mode_cubemode_tuple: Tuple) -> Tuple[boo
199 mode |= v220_mode(inst)221 mode |= v220_mode(inst)
200 222 
201 if mode >= CODE_MAX:223 if mode >= CODE_MAX:
202- return False, mode, f'unknown code mode {mode}.'224+ return False, mode, f"unknown code mode {mode}."
203- return True, mode, ''225+ return True, mode, ""
@@ -14,57 +14,57 @@ import re
14from host_stub_util import CodeMode14from host_stub_util import CodeMode
15 15 
16FUNC_SIGNATURE = re.compile(16FUNC_SIGNATURE = re.compile(
17- r'(?:template\s*<\s*([^>]+?)\s*>\s*\n?\s*)?'17+ r"(?:template\s*<\s*([^>]+?)\s*>\s*\n?\s*)?"
18- r'__attribute__\(\(cce_kernel\)\)\s*'18+ r"__attribute__\(\(cce_kernel\)\)\s*"
19- r'(?:\[aicore\]|__attribute__\(\(cce_aicore\)\))\s*'19+ r"(?:\[aicore\]|__attribute__\(\(cce_aicore\)\))\s*"
20- r'(.+?)\s*\{',20+ r"(.+?)\s*\{",
21 re.DOTALL,21 re.DOTALL,
22)22)
23-FUNC_PARAMS = re.compile(r'\((.+)\)', re.DOTALL)23+FUNC_PARAMS = re.compile(r"\((.+)\)", re.DOTALL)
24 24 
25-STRUCT = re.compile(r'^\s*struct\s+(\w+)\s*\{', re.MULTILINE)25+STRUCT = re.compile(r"^\s*struct\s+(\w+)\s*\{", re.MULTILINE)
26 26 
27-CCE_GLOBAL = '__attribute__((cce_global))'27+CCE_GLOBAL = "__attribute__((cce_global))"
28-FUNC_PARAM_SKIPS = ('__restrict__', '__restrict', 'struct')28+FUNC_PARAM_SKIPS = ("__restrict__", "__restrict", "struct")
29 29 
30-ATTRIBUTE = re.compile(r'__attribute__\(\(\w+\)\)')30+ATTRIBUTE = re.compile(r"__attribute__\(\(\w+\)\)")
31 31 
32-COMMENT = re.compile(r'[ \t]*//.*')32+COMMENT = re.compile(r"[ \t]*//.*")
33 33 
34-TYPE_BASENAME = re.compile(r'\w+')34+TYPE_BASENAME = re.compile(r"\w+")
35 35 
36-MIX_CORE_MACRO = '__MIX_CORE_MACRO__'36+MIX_CORE_MACRO = "__MIX_CORE_MACRO__"
37 37 
38FUN_TEMPLATE_HASH_TILING_KEY_BASE = 100000038FUN_TEMPLATE_HASH_TILING_KEY_BASE = 1000000
39 39 
40STR_TO_KERNEL_TYPE_V200 = {40STR_TO_KERNEL_TYPE_V200 = {
41- "KERNEL_TYPE_AICORE" : CodeMode.AIC,41+ "KERNEL_TYPE_AICORE": CodeMode.AIC,
42- "KERNEL_TYPE_VECTORCORE" : CodeMode.AIC,42+ "KERNEL_TYPE_VECTORCORE": CodeMode.AIC,
43- "KERNEL_TYPE_MIX_VECTOR_CORE" : CodeMode.MIX_VECTOR_CORE,43+ "KERNEL_TYPE_MIX_VECTOR_CORE": CodeMode.MIX_VECTOR_CORE,
44}44}
45 45 
46STR_TO_KERNEL_TYPE_V220 = {46STR_TO_KERNEL_TYPE_V220 = {
47- "KERNEL_TYPE_AIV_ONLY" : CodeMode.KERNEL_TYPE_AIV_ONLY,47+ "KERNEL_TYPE_AIV_ONLY": CodeMode.KERNEL_TYPE_AIV_ONLY,
48- "KERNEL_TYPE_AIC_ONLY" : CodeMode.KERNEL_TYPE_AIC_ONLY,48+ "KERNEL_TYPE_AIC_ONLY": CodeMode.KERNEL_TYPE_AIC_ONLY,
49- "KERNEL_TYPE_MIX_AIV_1_0" : CodeMode.KERNEL_TYPE_MIX_AIV_1_0,49+ "KERNEL_TYPE_MIX_AIV_1_0": CodeMode.KERNEL_TYPE_MIX_AIV_1_0,
50- "KERNEL_TYPE_MIX_AIC_1_0" : CodeMode.KERNEL_TYPE_MIX_AIC_1_0,50+ "KERNEL_TYPE_MIX_AIC_1_0": CodeMode.KERNEL_TYPE_MIX_AIC_1_0,
51- "KERNEL_TYPE_MIX_AIC_1_1" : CodeMode.KERNEL_TYPE_MIX_AIC_1_1,51+ "KERNEL_TYPE_MIX_AIC_1_1": CodeMode.KERNEL_TYPE_MIX_AIC_1_1,
52- "KERNEL_TYPE_MIX_AIC_1_2" : CodeMode.KERNEL_TYPE_MIX_AIC_1_2,52+ "KERNEL_TYPE_MIX_AIC_1_2": CodeMode.KERNEL_TYPE_MIX_AIC_1_2,
53}53}
54 54 
55STR_TO_KERNEL_TYPE_C310 = {55STR_TO_KERNEL_TYPE_C310 = {
56- "KERNEL_TYPE_AIV_ONLY" : CodeMode.KERNEL_TYPE_AIV_ONLY,56+ "KERNEL_TYPE_AIV_ONLY": CodeMode.KERNEL_TYPE_AIV_ONLY,
57- "KERNEL_TYPE_AIC_ONLY" : CodeMode.KERNEL_TYPE_AIC_ONLY,57+ "KERNEL_TYPE_AIC_ONLY": CodeMode.KERNEL_TYPE_AIC_ONLY,
58- "KERNEL_TYPE_MIX_AIV_1_0" : CodeMode.KERNEL_TYPE_MIX_AIV_1_0,58+ "KERNEL_TYPE_MIX_AIV_1_0": CodeMode.KERNEL_TYPE_MIX_AIV_1_0,
59- "KERNEL_TYPE_MIX_AIC_1_0" : CodeMode.KERNEL_TYPE_MIX_AIC_1_0,59+ "KERNEL_TYPE_MIX_AIC_1_0": CodeMode.KERNEL_TYPE_MIX_AIC_1_0,
60- "KERNEL_TYPE_MIX_AIC_1_1" : CodeMode.KERNEL_TYPE_MIX_AIC_1_1,60+ "KERNEL_TYPE_MIX_AIC_1_1": CodeMode.KERNEL_TYPE_MIX_AIC_1_1,
61- "KERNEL_TYPE_MIX_AIC_1_2" : CodeMode.KERNEL_TYPE_MIX_AIC_1_2,61+ "KERNEL_TYPE_MIX_AIC_1_2": CodeMode.KERNEL_TYPE_MIX_AIC_1_2,
62}62}
63 63 
64STR_TO_KERNEL_TYPE_L300 = {64STR_TO_KERNEL_TYPE_L300 = {
65- "KERNEL_TYPE_AICORE" : CodeMode.AIC,65+ "KERNEL_TYPE_AICORE": CodeMode.AIC,
66}66}
67 67 
68STR_TO_KERNEL_TYPE_L311 = {68STR_TO_KERNEL_TYPE_L311 = {
69- "KERNEL_TYPE_AICORE" : CodeMode.AIC,69+ "KERNEL_TYPE_AICORE": CodeMode.AIC,
70}70}
@@ -30,7 +30,7 @@ class ParseSrcFilePathError(ExtractError):
30 30 
31def get_template_config_filepath(dst_dir: str) -> str:31def get_template_config_filepath(dst_dir: str) -> str:
32 """Get template_config.cmake destination file path."""32 """Get template_config.cmake destination file path."""
33- return os.path.join(dst_dir, 'template_config.cmake')33+ return os.path.join(dst_dir, "template_config.cmake")
34 34 
35 35 
36def generate_template_config_code(filepaths: List[str]) -> str:36def generate_template_config_code(filepaths: List[str]) -> str:
@@ -38,11 +38,13 @@ def generate_template_config_code(filepaths: List[str]) -> str:
38 config_options = ""38 config_options = ""
39 disable_kernel_check_option = "--cce-disable-kernel-global-attr-check"39 disable_kernel_check_option = "--cce-disable-kernel-global-attr-check"
40 src_file_pattern = r'#\s*\d+\s*"([^"]+)"'40 src_file_pattern = r'#\s*\d+\s*"([^"]+)"'
41- template_kernel_func_pattern = (r'template<([^<>]*(?:<[^<>]*>)*[^<>]*)>\s*__attribute__\(\(cce_kernel\)\)'41+ template_kernel_func_pattern = (
42- r'\s*(?:\[aicore\]|__attribute__\(\(cce_aicore\)\))\s*(.+?)\s*\{')42+ r"template<([^<>]*(?:<[^<>]*>)*[^<>]*)>\s*__attribute__\(\(cce_kernel\)\)"
43+ r"\s*(?:\[aicore\]|__attribute__\(\(cce_aicore\)\))\s*(.+?)\s*\{"
44+ )
43 for path in filepaths:45 for path in filepaths:
44 try:46 try:
45- with open(path, encoding='utf-8') as file:47+ with open(path, encoding="utf-8") as file:
46 data = file.read()48 data = file.read()
47 first_line = data.splitlines()[0]49 first_line = data.splitlines()[0]
48 src_file_match = re.search(src_file_pattern, first_line)50 src_file_match = re.search(src_file_pattern, first_line)
@@ -52,8 +54,8 @@ def generate_template_config_code(filepaths: List[str]) -> str:
52 raise ParseSrcFilePathError()54 raise ParseSrcFilePathError()
53 template_match = re.compile(template_kernel_func_pattern, re.DOTALL)55 template_match = re.compile(template_kernel_func_pattern, re.DOTALL)
54 if not template_match.search(data):56 if not template_match.search(data):
55- config_options += f'set_source_files_properties({src_file_path} \57+ config_options += f"set_source_files_properties({src_file_path} \
56-PROPERTIES COMPILE_OPTIONS {disable_kernel_check_option})\n'58+PROPERTIES COMPILE_OPTIONS {disable_kernel_check_option})\n"
57 59 
58 except Exception as err:60 except Exception as err:
59 print("[ERROR]: read file failed, filename is: {}".format(path))61 print("[ERROR]: read file failed, filename is: {}".format(path))
@@ -61,20 +63,20 @@ PROPERTIES COMPILE_OPTIONS {disable_kernel_check_option})\n'
61 return config_options63 return config_options
62 64 
63 65 
64- 66+def generate_save_template_config_commands(
65-def generate_save_template_config_commands(filepaths: List[str],67+ filepaths: List[str], dst_dir: str
66- dst_dir: str) -> Iterator[Tuple[str, str]]:68+) -> Iterator[Tuple[str, str]]:
67 """Generate save template_config commands."""69 """Generate save template_config commands."""
68 yield (70 yield (
69 get_template_config_filepath(dst_dir),71 get_template_config_filepath(dst_dir),
70- generate_template_config_code(filepaths)72+ generate_template_config_code(filepaths),
71 )73 )
72 74 
73 75 
74def main(argv: List[str]):76def main(argv: List[str]):
75 parser = argparse.ArgumentParser()77 parser = argparse.ArgumentParser()
76- parser.add_argument('filepaths', nargs='+', help='Preprocessed file paths.')78+ parser.add_argument("filepaths", nargs="+", help="Preprocessed file paths.")
77- parser.add_argument('-d', '--dst-dir', default='.', help='Destination directory.')79+ parser.add_argument("-d", "--dst-dir", default=".", help="Destination directory.")
78 80 
79 args = parser.parse_args(argv)81 args = parser.parse_args(argv)
80 82 
@@ -82,9 +84,7 @@ def main(argv: List[str]):
82 if not os.path.exists(dst_dir):84 if not os.path.exists(dst_dir):
83 os.makedirs(dst_dir)85 os.makedirs(dst_dir)
84 86 
85- do_save_commands(87+ do_save_commands(generate_save_template_config_commands(args.filepaths, dst_dir))
86- generate_save_template_config_commands(args.filepaths, dst_dir)
87- )
88 88 
89 return True89 return True
90 90 
@@ -94,7 +94,7 @@ def main_with_except(argv: List[str]):
94 try:94 try:
95 return main(argv)95 return main(argv)
96 except ArgumentError as ex:96 except ArgumentError as ex:
97- print(f'error: check arguments error, {ex}')97+ print(f"error: check arguments error, {ex}")
98 return False98 return False
99 99 
100 100 
@@ -24,38 +24,39 @@ from typing import List
24def save_file(filepath: str, content: str):24def save_file(filepath: str, content: str):
25 """Save file."""25 """Save file."""
26 try:26 try:
27- with open(filepath, 'w', encoding='utf-8') as file:27+ with open(filepath, "w", encoding="utf-8") as file:
28 os.chmod(filepath, stat.S_IRUSR + stat.S_IWUSR)28 os.chmod(filepath, stat.S_IRUSR + stat.S_IWUSR)
29 file.write(content)29 file.write(content)
30 except Exception as err:30 except Exception as err:
31 print("[ERROR]: write file failed, filename is: {}".format(filepath))31 print("[ERROR]: write file failed, filename is: {}".format(filepath))
32 raise err32 raise err
33 33 
34+ 
34def extract_archive(archive_path: str, dst_dir: str):35def extract_archive(archive_path: str, dst_dir: str):
35 """Extract archive into destination directory."""36 """Extract archive into destination directory."""
36- subprocess.run(['ar', '-x', archive_path], cwd=dst_dir, check=True)37+ subprocess.run(["ar", "-x", archive_path], cwd=dst_dir, check=True)
37 38 
38 39 
39def get_objects_in_archive(archive_path: str) -> List[str]:40def get_objects_in_archive(archive_path: str) -> List[str]:
40 """Get objects in archive."""41 """Get objects in archive."""
41 result = subprocess.run(42 result = subprocess.run(
42- ['ar', '-t', archive_path], check=True,43+ ["ar", "-t", archive_path],
43- stdout=subprocess.PIPE, stderr=subprocess.STDOUT44+ check=True,
45+ stdout=subprocess.PIPE,
46+ stderr=subprocess.STDOUT,
44 )47 )
45 display = result.stdout.decode()48 display = result.stdout.decode()
46 return display.split()49 return display.split()
47 50 
48 51 
49-def generate_cmake_config_content(target: str,52+def generate_cmake_config_content(target: str, dst_dir: str, objects: List[str]) -> str:
50- dst_dir: str, objects: List[str]) -> str:
51- 
52 all_object_list = [os.path.join(dst_dir, _object) for _object in objects]53 all_object_list = [os.path.join(dst_dir, _object) for _object in objects]
53- all_objects = ';'.join(all_object_list)54+ all_objects = ";".join(all_object_list)
54 buff = io.StringIO()55 buff = io.StringIO()
55- buff.write(f'add_library({target} OBJECT IMPORTED)\n')56+ buff.write(f"add_library({target} OBJECT IMPORTED)\n")
56- buff.write(f'set_target_properties({target} PROPERTIES\n')57+ buff.write(f"set_target_properties({target} PROPERTIES\n")
57 buff.write(f' IMPORTED_OBJECTS "{all_objects}"\n')58 buff.write(f' IMPORTED_OBJECTS "{all_objects}"\n')
58- buff.write(')\n')59+ buff.write(")\n")
59 return buff.getvalue()60 return buff.getvalue()
60 61 
61 62 
@@ -63,10 +64,10 @@ def main(argv):
63 """Main process."""64 """Main process."""
64 # -a xxx.a -t xxx -d xxx -o xxx.cmake65 # -a xxx.a -t xxx -d xxx -o xxx.cmake
65 parser = argparse.ArgumentParser()66 parser = argparse.ArgumentParser()
66- parser.add_argument('-a', '--archive', required=True, help='Archive name')67+ parser.add_argument("-a", "--archive", required=True, help="Archive name")
67- parser.add_argument('-t', '--target', required=True, help='Output target name.')68+ parser.add_argument("-t", "--target", required=True, help="Output target name.")
68- parser.add_argument('-d', '--dst-dir', default='.', help='Destination directory.')69+ parser.add_argument("-d", "--dst-dir", default=".", help="Destination directory.")
69- parser.add_argument('-o', '--cmake', required=True, help='Output cmake config.')70+ parser.add_argument("-o", "--cmake", required=True, help="Output cmake config.")
70 args = parser.parse_args(argv)71 args = parser.parse_args(argv)
71 72 
72 if not os.path.exists(args.dst_dir):73 if not os.path.exists(args.dst_dir):
@@ -76,10 +77,12 @@ def main(argv):
76 77 
77 objects = get_objects_in_archive(args.archive)78 objects = get_objects_in_archive(args.archive)
78 79 
79- cmake_config_content = generate_cmake_config_content(args.target, args.dst_dir, objects)80+ cmake_config_content = generate_cmake_config_content(
81+ args.target, args.dst_dir, objects
82+ )
80 83 
81 save_file(os.path.join(args.dst_dir, args.cmake), cmake_config_content)84 save_file(os.path.join(args.dst_dir, args.cmake), cmake_config_content)
82 85 
83 86 
84-if __name__ == '__main__':87+if __name__ == "__main__":
85 main(sys.argv[1:])88 main(sys.argv[1:])
@@ -14,7 +14,7 @@ import io
14 14 
15 15 
16def generate_host_stub_set_exception_dump_source() -> str:16def generate_host_stub_set_exception_dump_source() -> str:
17- exception_dump_source = r'''17+ exception_dump_source = r"""
18#if !(defined(ASCENDC_DUMP) && ASCENDC_DUMP == 0)18#if !(defined(ASCENDC_DUMP) && ASCENDC_DUMP == 0)
19static void ascendc_set_exception_dump_info(uint32_t dumpSize)19static void ascendc_set_exception_dump_info(uint32_t dumpSize)
20{20{
@@ -45,7 +45,7 @@ static void ascendc_set_exception_dump_info(uint32_t dumpSize)
45 }45 }
46}46}
47#endif47#endif
48-'''48+"""
49 return exception_dump_source49 return exception_dump_source
50 50 
51 51 
@@ -56,7 +56,7 @@ def generate_host_stub_head_code(has_mix: bool, has_aic: bool, has_aiv: bool) ->
56 ascend_kernel_struct = []56 ascend_kernel_struct = []
57 57 
58 buff = io.StringIO()58 buff = io.StringIO()
59- buff.write(r'''#include <stdio.h>59+ buff.write(r"""#include <stdio.h>
60#include <sys/types.h>60#include <sys/types.h>
61#include <sys/stat.h>61#include <sys/stat.h>
62#include <fcntl.h>62#include <fcntl.h>
@@ -66,34 +66,34 @@ def generate_host_stub_head_code(has_mix: bool, has_aic: bool, has_aiv: bool) ->
66#include <stdint.h>66#include <stdint.h>
67#include <dlfcn.h>67#include <dlfcn.h>
68#include <securec.h>68#include <securec.h>
69-''')69+""")
70 70 
71- buff.write('\n')71+ buff.write("\n")
72- buff.write('''static char ascendcErrMsg[1024] = {0};72+ buff.write("""static char ascendcErrMsg[1024] = {0};
73-''')73+""")
74- buff.write('\n')74+ buff.write("\n")
75 if has_mix:75 if has_mix:
76 type_nums += 176 type_nums += 1
77- buff.write('''static void *g_kernel_handle = nullptr;77+ buff.write("""static void *g_kernel_handle = nullptr;
78-''')78+""")
79- buff.write('\n')79+ buff.write("\n")
80 80 
81 if has_aiv:81 if has_aiv:
82 type_nums += 182 type_nums += 1
83- buff.write('''static void *g_kernel_handle_aiv = nullptr;83+ buff.write("""static void *g_kernel_handle_aiv = nullptr;
84-''')84+""")
85- buff.write('\n')85+ buff.write("\n")
86 86 
87 if has_aic:87 if has_aic:
88 type_nums += 188 type_nums += 1
89- buff.write('''static void *g_kernel_handle_aic = nullptr;89+ buff.write("""static void *g_kernel_handle_aic = nullptr;
90-''')90+""")
91- buff.write('\n')91+ buff.write("\n")
92 92 
93- buff.write(r'''struct ascend_kernels {93+ buff.write(r"""struct ascend_kernels {
94 uint32_t version;94 uint32_t version;
95 uint32_t type_cnt;95 uint32_t type_cnt;
96-''')96+""")
97 97 
98 # version value98 # version value
99 ascend_kernel_struct.append(str(1))99 ascend_kernel_struct.append(str(1))
@@ -102,46 +102,48 @@ def generate_host_stub_head_code(has_mix: bool, has_aic: bool, has_aiv: bool) ->
102 ascend_kernel_struct.append(str(type_nums))102 ascend_kernel_struct.append(str(type_nums))
103 103 
104 if has_mix:104 if has_mix:
105- buff.write(''' uint32_t mix_type;105+ buff.write(""" uint32_t mix_type;
106 uint32_t mix_len;106 uint32_t mix_len;
107 uint32_t mix_file_len;107 uint32_t mix_file_len;
108 uint8_t mix_buf[__replaced_mix_len];108 uint8_t mix_buf[__replaced_mix_len];
109-''')109+""")
110 ascend_kernel_struct.append(str(0))110 ascend_kernel_struct.append(str(0))
111- ascend_kernel_struct.append('__replaced_mix_len')111+ ascend_kernel_struct.append("__replaced_mix_len")
112- ascend_kernel_struct.append('__replaced_mix_file_len')112+ ascend_kernel_struct.append("__replaced_mix_file_len")
113- ascend_kernel_struct.append(r'{0}')113+ ascend_kernel_struct.append(r"{0}")
114 114 
115 if has_aiv:115 if has_aiv:
116- buff.write(''' uint32_t aiv_type;116+ buff.write(""" uint32_t aiv_type;
117 uint32_t aiv_len;117 uint32_t aiv_len;
118 uint32_t aiv_file_len;118 uint32_t aiv_file_len;
119 uint8_t aiv_buf[__replaced_aiv_len];119 uint8_t aiv_buf[__replaced_aiv_len];
120-''')120+""")
121 ascend_kernel_struct.append(str(1))121 ascend_kernel_struct.append(str(1))
122- ascend_kernel_struct.append('__replaced_aiv_len')122+ ascend_kernel_struct.append("__replaced_aiv_len")
123- ascend_kernel_struct.append('__replaced_aiv_file_len')123+ ascend_kernel_struct.append("__replaced_aiv_file_len")
124- ascend_kernel_struct.append(r'{0}')124+ ascend_kernel_struct.append(r"{0}")
125 125 
126 if has_aic:126 if has_aic:
127- buff.write(''' uint32_t aic_type;127+ buff.write(""" uint32_t aic_type;
128 uint32_t aic_len;128 uint32_t aic_len;
129 uint32_t aic_file_len;129 uint32_t aic_file_len;
130 uint8_t aic_buf[__replaced_aic_len];130 uint8_t aic_buf[__replaced_aic_len];
131-''')131+""")
132 ascend_kernel_struct.append(str(2))132 ascend_kernel_struct.append(str(2))
133- ascend_kernel_struct.append('__replaced_aic_len')133+ ascend_kernel_struct.append("__replaced_aic_len")
134- ascend_kernel_struct.append('__replaced_aic_file_len')134+ ascend_kernel_struct.append("__replaced_aic_file_len")
135- ascend_kernel_struct.append(r'{0}')135+ ascend_kernel_struct.append(r"{0}")
136 136 
137- buff.write('} __replaced_ascend_kernel __attribute__ ((section ("__replaced_ascend_section"))) = {')137+ buff.write(
138+ '} __replaced_ascend_kernel __attribute__ ((section ("__replaced_ascend_section"))) = {'
139+ )
138 140 
139- ascend_kernel_str = ','.join(ascend_kernel_struct)141+ ascend_kernel_str = ",".join(ascend_kernel_struct)
140- buff.write(f'{ascend_kernel_str}')142+ buff.write(f"{ascend_kernel_str}")
141 143 
142- buff.write('};\n\n')144+ buff.write("};\n\n")
143 145 
144- buff.write('''extern "C" {146+ buff.write("""extern "C" {
145uint32_t RegisterAscendBinary(const char *fileBuf, size_t fileSize, uint32_t type, void **handle);147uint32_t RegisterAscendBinary(const char *fileBuf, size_t fileSize, uint32_t type, void **handle);
146uint32_t LaunchAscendKernel(void *handle, const uint64_t key, const uint32_t numBlocks, void **args,148uint32_t LaunchAscendKernel(void *handle, const uint64_t key, const uint32_t numBlocks, void **args,
147 uint32_t size, const void *stream);149 uint32_t size, const void *stream);
@@ -157,17 +159,17 @@ void AscendProfRegister();
157uint32_t GetCoreNumForMixVectorCore(uint32_t *aiCoreNum, uint32_t *vectorCoreNum);159uint32_t GetCoreNumForMixVectorCore(uint32_t *aiCoreNum, uint32_t *vectorCoreNum);
158uint32_t LaunchAscendKernelForVectorCore(const char *opType, void *handle, const uint64_t key, void **args, uint32_t size,160uint32_t LaunchAscendKernelForVectorCore(const char *opType, void *handle, const uint64_t key, void **args, uint32_t size,
159 const void *stream, bool enableProf, uint32_t aicNumBlocks, uint32_t aivNumBlocks, uint32_t aivNumBlocksOffset);161 const void *stream, bool enableProf, uint32_t aicNumBlocks, uint32_t aivNumBlocks, uint32_t aivNumBlocksOffset);
160-''')162+""")
161 163 
162- buff.write('''}\n\nnamespace Adx {''')164+ buff.write("""}\n\nnamespace Adx {""")
163 165 
164- buff.write('''166+ buff.write("""
165 void AdumpPrintWorkSpace(const void *workSpaceAddr, const size_t dumpWorkSpaceSize,167 void AdumpPrintWorkSpace(const void *workSpaceAddr, const size_t dumpWorkSpaceSize,
166 void *stream, const char *opType);168 void *stream, const char *opType);
167}169}
168-''')170+""")
169 171 
170- buff.write('''172+ buff.write("""
171 class KernelHandleGradUnregister {173 class KernelHandleGradUnregister {
172 private:174 private:
173 KernelHandleGradUnregister() {}175 KernelHandleGradUnregister() {}
@@ -181,49 +183,49 @@ uint32_t LaunchAscendKernelForVectorCore(const char *opType, void *handle, const
181 return instance;183 return instance;
182 }184 }
183 ~KernelHandleGradUnregister(){185 ~KernelHandleGradUnregister(){
184-''')186+""")
185 187 
186 if has_mix:188 if has_mix:
187- buff.write(''' if (g_kernel_handle) {189+ buff.write(""" if (g_kernel_handle) {
188 UnregisterAscendBinary(g_kernel_handle);190 UnregisterAscendBinary(g_kernel_handle);
189 g_kernel_handle = nullptr;191 g_kernel_handle = nullptr;
190 }192 }
191-''')193+""")
192 194 
193 if has_aiv:195 if has_aiv:
194- buff.write(''' if (g_kernel_handle_aiv) {196+ buff.write(""" if (g_kernel_handle_aiv) {
195 UnregisterAscendBinary(g_kernel_handle_aiv);197 UnregisterAscendBinary(g_kernel_handle_aiv);
196 g_kernel_handle_aiv = nullptr;198 g_kernel_handle_aiv = nullptr;
197 }199 }
198-''')200+""")
199 201 
200 if has_aic:202 if has_aic:
201- buff.write(''' if (g_kernel_handle_aic) {203+ buff.write(""" if (g_kernel_handle_aic) {
202 UnregisterAscendBinary(g_kernel_handle_aic);204 UnregisterAscendBinary(g_kernel_handle_aic);
203 g_kernel_handle_aic = nullptr;205 g_kernel_handle_aic = nullptr;
204 }206 }
205-''')207+""")
206- buff.write(''' }208+ buff.write(""" }
207 };209 };
208-''')210+""")
209- buff.write('\n')211+ buff.write("\n")
210 212 
211- buff.write(r'''static void __register_kernels(void) __attribute__((constructor));213+ buff.write(r"""static void __register_kernels(void) __attribute__((constructor));
212void __register_kernels(void)214void __register_kernels(void)
213{215{
214 const char* compileSocVersion = "__replaced_ascend_compile_soc_version";216 const char* compileSocVersion = "__replaced_ascend_compile_soc_version";
215 uint32_t ret;217 uint32_t ret;
216-''')218+""")
217- buff.write('\n')219+ buff.write("\n")
218 220 
219- buff.write(r''' bool checkSocVersion = AscendCheckSoCVersion(compileSocVersion, ascendcErrMsg);221+ buff.write(r""" bool checkSocVersion = AscendCheckSoCVersion(compileSocVersion, ascendcErrMsg);
220 if (!checkSocVersion) {222 if (!checkSocVersion) {
221 return;223 return;
222 }224 }
223-''')225+""")
224 226 
225 if has_mix:227 if has_mix:
226- buff.write(r''' ret = RegisterAscendBinary(228+ buff.write(r""" ret = RegisterAscendBinary(
227 (const char *)__replaced_ascend_kernel.mix_buf,229 (const char *)__replaced_ascend_kernel.mix_buf,
228 __replaced_ascend_kernel.mix_file_len,230 __replaced_ascend_kernel.mix_file_len,
229 0,231 0,
@@ -231,11 +233,11 @@ void __register_kernels(void)
231 if (ret != 0) {233 if (ret != 0) {
232 printf("RegisterAscendBinary mix ret %u \n", ret);234 printf("RegisterAscendBinary mix ret %u \n", ret);
233 }235 }
234-''')236+""")
235- buff.write('\n')237+ buff.write("\n")
236 238 
237 if has_aiv:239 if has_aiv:
238- buff.write(r''' ret = RegisterAscendBinary(240+ buff.write(r""" ret = RegisterAscendBinary(
239 (const char *)__replaced_ascend_kernel.aiv_buf,241 (const char *)__replaced_ascend_kernel.aiv_buf,
240 __replaced_ascend_kernel.aiv_file_len,242 __replaced_ascend_kernel.aiv_file_len,
241 1,243 1,
@@ -243,11 +245,11 @@ void __register_kernels(void)
243 if (ret != 0) {245 if (ret != 0) {
244 printf("RegisterAscendBinary aiv ret %u \n", ret);246 printf("RegisterAscendBinary aiv ret %u \n", ret);
245 }247 }
246-''')248+""")
247- buff.write('\n')249+ buff.write("\n")
248 250 
249 if has_aic:251 if has_aic:
250- buff.write(r''' ret = RegisterAscendBinary(252+ buff.write(r""" ret = RegisterAscendBinary(
251 (const char *)__replaced_ascend_kernel.aic_buf,253 (const char *)__replaced_ascend_kernel.aic_buf,
252 __replaced_ascend_kernel.aic_file_len,254 __replaced_ascend_kernel.aic_file_len,
253 2,255 2,
@@ -255,11 +257,11 @@ void __register_kernels(void)
255 if (ret != 0) {257 if (ret != 0) {
256 printf("RegisterAscendBinary aic ret %u \n", ret);258 printf("RegisterAscendBinary aic ret %u \n", ret);
257 }259 }
258-''')260+""")
259- buff.write('\n')261+ buff.write("\n")
260 262 
261- buff.write(''' AscendProfRegister();263+ buff.write(""" AscendProfRegister();
262}264}
263-''')265+""")
264 266 
265 return buff.getvalue()267 return buff.getvalue()
@@ -14,6 +14,7 @@ from enum import Enum
14 14 
15class CodeMode(Enum):15class CodeMode(Enum):
16 """Code mode."""16 """Code mode."""
17+ 
17 MIX = 018 MIX = 0
18 AIV = 119 AIV = 1
19 AIC = 220 AIC = 2
@@ -29,6 +30,7 @@ class CodeMode(Enum):
29 30 
30class FuncMetaType(Enum):31class FuncMetaType(Enum):
31 """function meta type."""32 """function meta type."""
33+ 
32 F_TYPE_KTYPE = 134 F_TYPE_KTYPE = 1
33 F_TYPE_CROSS_CORE_SYNC = 235 F_TYPE_CROSS_CORE_SYNC = 2
34 F_TYPE_MAX = 336 F_TYPE_MAX = 3
@@ -64,7 +66,8 @@ class TooMuchFuncParamParts(ParseFuncSignatureError):
64 66 
65class GetFuncParamPartError(ExtractError):67class GetFuncParamPartError(ExtractError):
66 """Get function parameter part error."""68 """Get function parameter part error."""
67- def __init__(self, func_param: 'FuncParam'):69+ 
70+ def __init__(self, func_param: "FuncParam"):
68 super().__init__(func_param)71 super().__init__(func_param)
69 self.func_param = func_param72 self.func_param = func_param
70 73 
@@ -12,11 +12,7 @@
12 12 
13import argparse13import argparse
14import os14import os
15-import shutil
16import subprocess15import subprocess
17-import sys
18-import stat
19-from typing import List
20 16 
21 17 
22class CompareError(Exception):18class CompareError(Exception):
@@ -30,7 +26,7 @@ class MergeCMDError(Exception):
30def run_merge_cmd(script, linker, build_type, input_file, output_file):26def run_merge_cmd(script, linker, build_type, input_file, output_file):
31 """run cmds"""27 """run cmds"""
32 28 
33- input_str = ' '.join(input_file)29+ input_str = " ".join(input_file)
34 30 
35 output_dir = os.path.dirname(output_file)31 output_dir = os.path.dirname(output_file)
36 output_name = os.path.basename(output_file)32 output_name = os.path.basename(output_file)
@@ -53,7 +49,7 @@ def single_obj_merge(script, linker, build_type, all_merge_list):
53def read_file(source_file):49def read_file(source_file):
54 """read file content."""50 """read file content."""
55 try:51 try:
56- with open(source_file, encoding='utf-8') as file:52+ with open(source_file, encoding="utf-8") as file:
57 content = file.readlines()53 content = file.readlines()
58 return content54 return content
59 except Exception as err:55 except Exception as err:
@@ -64,11 +60,11 @@ def read_file(source_file):
64def get_obj_name(obj_file):60def get_obj_name(obj_file):
65 """get obj name."""61 """get obj name."""
66 obj_base_name = os.path.basename(obj_file)62 obj_base_name = os.path.basename(obj_file)
67- obj_split_name = obj_base_name.split('.')63+ obj_split_name = obj_base_name.split(".")
68 prefix = obj_split_name[0]64 prefix = obj_split_name[0]
69 file_name = prefix.replace("auto_gen_", "")65 file_name = prefix.replace("auto_gen_", "")
70 suffix = obj_split_name[-1]66 suffix = obj_split_name[-1]
71- return rf'{file_name}.{suffix}'67+ return rf"{file_name}.{suffix}"
72 68 
73 69 
74def get_normal_obj_info(input_dir, file_name, output_dir):70def get_normal_obj_info(input_dir, file_name, output_dir):
@@ -125,44 +121,23 @@ def get_mix_obj_info(aic_dir, aiv_dir, file_name, output_dir):
125def parse_args():121def parse_args():
126 """parse parameters"""122 """parse parameters"""
127 parser = argparse.ArgumentParser()123 parser = argparse.ArgumentParser()
128- parser.add_argument('-l',124+ parser.add_argument("-l", "--linker", required=True, help="ccec linker")
129- '--linker',
130- required=True,
131- help='ccec linker')
132 125 
133- parser.add_argument('-o',126+ parser.add_argument("-o", "--output", required=True, help="output directory")
134- '--output',
135- required=True,
136- help='output directory')
137 127 
138- parser.add_argument('--build-type',128+ parser.add_argument("--build-type", required=True, help="build type")
139- required=True,
140- help='build type')
141 129 
142- parser.add_argument('--script',130+ parser.add_argument("--script", required=True, help="merge script")
143- required=True,
144- help='merge script')
145 131 
146- parser.add_argument('--aiv-dir',132+ parser.add_argument("--aiv-dir", nargs="?", help="aiv directory")
147- nargs='?',
148- help='aiv directory')
149 133 
150- parser.add_argument('--aic-dir',134+ parser.add_argument("--aic-dir", nargs="?", help="aic directory")
151- nargs='?',
152- help='aic directory')
153 135 
154- parser.add_argument('--normal-dir',136+ parser.add_argument("--normal-dir", nargs="?", help="normal directory")
155- nargs='?',
156- help='normal directory')
157 137 
158- parser.add_argument('-n',138+ parser.add_argument("-n", "--name", nargs="?", help="output name")
159- '--name',
160- nargs='?',
161- help='output name')
162 139 
163- parser.add_argument('--fatbin',140+ parser.add_argument("--fatbin", action="store_true", help="fatbin mode")
164- action='store_true',
165- help='fatbin mode')
166 141 
167 args = parser.parse_args()142 args = parser.parse_args()
168 143 
@@ -177,28 +152,38 @@ def main():
177 all_merge_list = []152 all_merge_list = []
178 153 
179 if args.normal_dir:154 if args.normal_dir:
180- normal_objs, merge_normal = get_normal_obj_info(args.normal_dir, 'mix_build.flag', args.output)155+ normal_objs, merge_normal = get_normal_obj_info(
156+ args.normal_dir, "mix_build.flag", args.output
157+ )
181 all_source_list.extend(normal_objs)158 all_source_list.extend(normal_objs)
182 all_merge_list.extend(merge_normal)159 all_merge_list.extend(merge_normal)
183 else:160 else:
184- aic_objs, merge_aic = get_normal_obj_info(args.aic_dir, 'aic_build.flag', args.output)161+ aic_objs, merge_aic = get_normal_obj_info(
162+ args.aic_dir, "aic_build.flag", args.output
163+ )
185 all_source_list.extend(aic_objs)164 all_source_list.extend(aic_objs)
186 all_merge_list.extend(merge_aic)165 all_merge_list.extend(merge_aic)
187 166 
188- aiv_objs, merge_aiv = get_normal_obj_info(args.aiv_dir, 'aiv_build.flag', args.output)167+ aiv_objs, merge_aiv = get_normal_obj_info(
168+ args.aiv_dir, "aiv_build.flag", args.output
169+ )
189 all_source_list.extend(aiv_objs)170 all_source_list.extend(aiv_objs)
190 all_merge_list.extend(merge_aiv)171 all_merge_list.extend(merge_aiv)
191 172 
192- mix_objs, merge_mix = get_mix_obj_info(args.aic_dir, args.aiv_dir, 'mix_build.flag', args.output)173+ mix_objs, merge_mix = get_mix_obj_info(
174+ args.aic_dir, args.aiv_dir, "mix_build.flag", args.output
175+ )
193 all_source_list.extend(mix_objs)176 all_source_list.extend(mix_objs)
194 all_merge_list.extend(merge_mix)177 all_merge_list.extend(merge_mix)
195 178 
196 if args.fatbin:179 if args.fatbin:
197 output_file = os.path.join(args.output, args.name)180 output_file = os.path.join(args.output, args.name)
198- run_merge_cmd(args.script, args.linker, args.build_type, all_source_list, output_file)181+ run_merge_cmd(
182+ args.script, args.linker, args.build_type, all_source_list, output_file
183+ )
199 else:184 else:
200 single_obj_merge(args.script, args.linker, args.build_type, all_merge_list)185 single_obj_merge(args.script, args.linker, args.build_type, all_merge_list)
201 186 
202 187 
203-if __name__ == '__main__':188+if __name__ == "__main__":
204 main()189 main()
@@ -13,11 +13,7 @@
13import argparse13import argparse
14import glob14import glob
15import os15import os
16-import shutil
17import subprocess16import subprocess
18-import sys
19-import stat
20-from typing import List
21 17 
22 18 
23class TooFewLinkCmd(Exception):19class TooFewLinkCmd(Exception):
@@ -37,7 +33,7 @@ class CompileCMDError(Exception):
37 33 
38 34 
39def get_link_file(root_dir, target_name):35def get_link_file(root_dir, target_name):
40- link_file = f'{root_dir}/CMakeFiles/{target_name}.dir/link.txt'36+ link_file = f"{root_dir}/CMakeFiles/{target_name}.dir/link.txt"
41 37 
42 if os.path.exists(link_file):38 if os.path.exists(link_file):
43 return link_file39 return link_file
@@ -48,7 +44,7 @@ def get_link_file(root_dir, target_name):
48def read_file(source_file):44def read_file(source_file):
49 """read file content."""45 """read file content."""
50 try:46 try:
51- with open(source_file, encoding='utf-8') as file:47+ with open(source_file, encoding="utf-8") as file:
52 content = file.readlines()48 content = file.readlines()
53 return content49 return content
54 except Exception as err:50 except Exception as err:
@@ -59,20 +55,22 @@ def read_file(source_file):
59def get_link_cmd(link_file):55def get_link_cmd(link_file):
60 contents = read_file(link_file)56 contents = read_file(link_file)
61 if not contents:57 if not contents:
62- raise TooFewLinkCmd(f"There are too few compilation commands in the file: {link_file}")58+ raise TooFewLinkCmd(
59+ f"There are too few compilation commands in the file: {link_file}"
60+ )
63 return contents61 return contents
64 62 
65 63 
66def get_add_obj(add_dir):64def get_add_obj(add_dir):
67- objs = glob.glob(f'{add_dir}/**/*.o', recursive=True)65+ objs = glob.glob(f"{add_dir}/**/*.o", recursive=True)
68 if not objs:66 if not objs:
69 raise TooFewObj(f"There is no obj file in this directory: {add_dir}")67 raise TooFewObj(f"There is no obj file in this directory: {add_dir}")
70 else:68 else:
71- return ' '.join(sorted(objs))69+ return " ".join(sorted(objs))
72 70 
73 71 
74def get_recompile_cmd(origin_cmd, insert_cmd):72def get_recompile_cmd(origin_cmd, insert_cmd):
75- key_words = 'host_stub.cpp.o'73+ key_words = "host_stub.cpp.o"
76 index = origin_cmd.find(key_words)74 index = origin_cmd.find(key_words)
77 if index != -1:75 if index != -1:
78 index += len(key_words)76 index += len(key_words)
@@ -84,24 +82,22 @@ def get_recompile_cmd(origin_cmd, insert_cmd):
84def run_recompile_cmd(root_dir, compile_cmd):82def run_recompile_cmd(root_dir, compile_cmd):
85 """run cmds"""83 """run cmds"""
86 cmds = compile_cmd.split()84 cmds = compile_cmd.split()
87- print(f'recompile: {compile_cmd}', flush=True)85+ print(f"recompile: {compile_cmd}", flush=True)
88 result = subprocess.run(cmds, check=True, cwd=root_dir)86 result = subprocess.run(cmds, check=True, cwd=root_dir)
89 if result.returncode != 0:87 if result.returncode != 0:
90- raise CompileCMDError(f"recompile command failed, return code: {result.returncode}")88+ raise CompileCMDError(
89+ f"recompile command failed, return code: {result.returncode}"
90+ )
91 91 
92 92 
93def parse_args():93def parse_args():
94 """parse parameters"""94 """parse parameters"""
95 parser = argparse.ArgumentParser()95 parser = argparse.ArgumentParser()
96- parser.add_argument('--root-dir',96+ parser.add_argument("--root-dir", required=True, help="target root directory")
97- required=True,97+ parser.add_argument("--target-name", required=True, help="target name")
98- help='target root directory')98+ parser.add_argument(
99- parser.add_argument('--target-name',99+ "--add-dir", required=True, help="the directory where the added obj is located"
100- required=True,100+ )
101- help='target name')
102- parser.add_argument('--add-dir',
103- required=True,
104- help='the directory where the added obj is located')
105 args = parser.parse_args()101 args = parser.parse_args()
106 return args102 return args
107 103 
@@ -117,5 +113,5 @@ def main():
117 run_recompile_cmd(args.root_dir, recompile_cmd)113 run_recompile_cmd(args.root_dir, recompile_cmd)
118 114 
119 115 
120-if __name__ == '__main__':116+if __name__ == "__main__":
121 main()117 main()
@@ -16,7 +16,8 @@ import re
16from typing import Tuple16from typing import Tuple
17 17 
18 18 
19-STRUCT_CONTENT = re.compile(r'\{(.+)\}', re.DOTALL)19+STRUCT_CONTENT = re.compile(r"\{(.+)\}", re.DOTALL)
20+ 
20 21 
21class ParseError(Exception):22class ParseError(Exception):
22 """Parse error."""23 """Parse error."""
@@ -24,11 +25,11 @@ class ParseError(Exception):
24 25 
25def extract_type_name(line: str) -> str:26def extract_type_name(line: str) -> str:
26 """Extract struct member type name."""27 """Extract struct member type name."""
27- line = line.split(',')[0].strip()28+ line = line.split(",")[0].strip()
28- line = line.split('=')[0].strip()29+ line = line.split("=")[0].strip()
29 parts = line.split()30 parts = line.split()
30 # drop the last part to get type name.31 # drop the last part to get type name.
31- return ' '.join(parts[:-1])32+ return " ".join(parts[:-1])
32 33 
33 34 
34def parse_struct_by_str(struct_str: str) -> Tuple[str, ...]:35def parse_struct_by_str(struct_str: str) -> Tuple[str, ...]:
@@ -38,7 +39,7 @@ def parse_struct_by_str(struct_str: str) -> Tuple[str, ...]:
38 raise ParseError()39 raise ParseError()
39 40 
40 struct_content = search_obj.group(1)41 struct_content = search_obj.group(1)
41- struct_mems = struct_content.split(';')42+ struct_mems = struct_content.split(";")
42 struct_mems = [mem.strip() for mem in struct_mems if mem.strip()]43 struct_mems = [mem.strip() for mem in struct_mems if mem.strip()]
43 struct_types = tuple(extract_type_name(mem) for mem in struct_mems)44 struct_types = tuple(extract_type_name(mem) for mem in struct_mems)
44 return struct_types45 return struct_types
@@ -45,27 +45,27 @@ MY_STRUCT_EX = """struct MyStruct{
45 45 
46def test_parse_struct_by_str_01():46def test_parse_struct_by_str_01():
47 result = parse_struct_by_str(TAIL_STRUCT)47 result = parse_struct_by_str(TAIL_STRUCT)
48- assert result == ('uint32_t',)48+ assert result == ("uint32_t",)
49 49 
50 50 
51def test_parse_struct_by_str_02():51def test_parse_struct_by_str_02():
52 result = parse_struct_by_str(TAIL_STRUCT_2)52 result = parse_struct_by_str(TAIL_STRUCT_2)
53- assert result == ('unsigned int',)53+ assert result == ("unsigned int",)
54 54 
55 55 
56def test_parse_struct_by_str_03():56def test_parse_struct_by_str_03():
57 result = parse_struct_by_str(TILING_STRUCT)57 result = parse_struct_by_str(TILING_STRUCT)
58- assert result == ('uint32_t', 'uint64_t', 'TailStruct')58+ assert result == ("uint32_t", "uint64_t", "TailStruct")
59 59 
60 60 
61def test_parse_struct_by_str_04():61def test_parse_struct_by_str_04():
62 result = parse_struct_by_str(MY_STRUCT)62 result = parse_struct_by_str(MY_STRUCT)
63- assert result == ('uint8_t',)63+ assert result == ("uint8_t",)
64 64 
65 65 
66def test_parse_struct_by_str_05():66def test_parse_struct_by_str_05():
67 result = parse_struct_by_str(MY_STRUCT_2)67 result = parse_struct_by_str(MY_STRUCT_2)
68- assert result == ('unsigned long long',)68+ assert result == ("unsigned long long",)
69 69 
70 70 
71def test_parse_struct_by_str_ex_01():71def test_parse_struct_by_str_ex_01():
@@ -14,8 +14,6 @@ import math
14import re14import re
15import stat15import stat
16import argparse16import argparse
17-from io import FileIO
18-from typing import Iterator, List, Optional
19 17 
20 18 
21def ceil_4(file_len: int) -> int:19def ceil_4(file_len: int) -> int:
@@ -26,51 +24,50 @@ def ceil_4(file_len: int) -> int:
26 24 
27def get_ascendc_kernel(soc_version: str, target_name: str) -> str:25def get_ascendc_kernel(soc_version: str, target_name: str) -> str:
28 soc_version = soc_version.replace("-", "_")26 soc_version = soc_version.replace("-", "_")
29- return f'__ascend_kernel_{soc_version}_{target_name}'27+ return f"__ascend_kernel_{soc_version}_{target_name}"
30 28 
31 29 
32def get_ascendc_section(soc_version: str, target_name: str) -> str:30def get_ascendc_section(soc_version: str, target_name: str) -> str:
33 soc_version = soc_version.replace("-", "_")31 soc_version = soc_version.replace("-", "_")
34- return rf'.ascend.kernel.{soc_version}.{target_name}'32+ return rf".ascend.kernel.{soc_version}.{target_name}"
35 33 
36 34 
37-def update_source_section(origin_content: str,35+def update_source_section(
38- soc_version: str,36+ origin_content: str, soc_version: str, target_name: str
39- target_name: str) -> str:37+) -> str:
40- 
41 update_content = origin_content38 update_content = origin_content
42 39 
43 ascendc_kernel = get_ascendc_kernel(soc_version, target_name)40 ascendc_kernel = get_ascendc_kernel(soc_version, target_name)
44- replaced_ascend_kernel = '__replaced_ascend_kernel'41+ replaced_ascend_kernel = "__replaced_ascend_kernel"
45 update_content = re.sub(replaced_ascend_kernel, ascendc_kernel, update_content)42 update_content = re.sub(replaced_ascend_kernel, ascendc_kernel, update_content)
46 43 
47 ascendc_section = get_ascendc_section(soc_version, target_name)44 ascendc_section = get_ascendc_section(soc_version, target_name)
48- replaced_ascend_section = '__replaced_ascend_section'45+ replaced_ascend_section = "__replaced_ascend_section"
49 update_content = re.sub(replaced_ascend_section, ascendc_section, update_content)46 update_content = re.sub(replaced_ascend_section, ascendc_section, update_content)
50 47 
51 replaced_ascend_kernel_soc_version = "__replaced_ascend_compile_soc_version"48 replaced_ascend_kernel_soc_version = "__replaced_ascend_compile_soc_version"
52- update_content = re.sub(replaced_ascend_kernel_soc_version, soc_version, update_content)49+ update_content = re.sub(
50+ replaced_ascend_kernel_soc_version, soc_version, update_content
51+ )
53 52 
54 return update_content53 return update_content
55 54 
56 55 
57def update_source_content(obj_path: str, origin_content: str) -> str:56def update_source_content(obj_path: str, origin_content: str) -> str:
58 """Update source content."""57 """Update source content."""
59- update_flags = {'aic': 'device_aic.o',58+ update_flags = {"aic": "device_aic.o", "aiv": "device_aiv.o", "mix": "device.o"}
60- 'aiv': 'device_aiv.o',
61- 'mix': 'device.o'}
62 59 
63 update_content = origin_content60 update_content = origin_content
64 61 
65 for op_type, obj in update_flags.items():62 for op_type, obj in update_flags.items():
66- flag_file = os.path.join(obj_path, f'{op_type}_build.flag')63+ flag_file = os.path.join(obj_path, f"{op_type}_build.flag")
67 obj_file = os.path.join(obj_path, obj)64 obj_file = os.path.join(obj_path, obj)
68 if os.path.exists(flag_file):65 if os.path.exists(flag_file):
69 obj_size = os.stat(obj_file).st_size66 obj_size = os.stat(obj_file).st_size
70 file_size = ceil_4(obj_size)67 file_size = ceil_4(obj_size)
71 68 
72- _file_len_str = f'__replaced_{op_type}_file_len'69+ _file_len_str = f"__replaced_{op_type}_file_len"
73- _file_str = f'__replaced_{op_type}_len'70+ _file_str = f"__replaced_{op_type}_len"
74 update_content = re.sub(_file_len_str, str(obj_size), update_content)71 update_content = re.sub(_file_len_str, str(obj_size), update_content)
75 update_content = re.sub(_file_str, str(file_size), update_content)72 update_content = re.sub(_file_str, str(file_size), update_content)
76 73 
@@ -80,15 +77,15 @@ def update_source_content(obj_path: str, origin_content: str) -> str:
80def main():77def main():
81 """Main process."""78 """Main process."""
82 parser = argparse.ArgumentParser()79 parser = argparse.ArgumentParser()
83- parser.add_argument('code_path')80+ parser.add_argument("code_path")
84- parser.add_argument('obj_path')81+ parser.add_argument("obj_path")
85- parser.add_argument('soc_version')82+ parser.add_argument("soc_version")
86- parser.add_argument('target_name')83+ parser.add_argument("target_name")
87 args = parser.parse_args()84 args = parser.parse_args()
88 85 
89- source_file = os.path.join(args.code_path, 'host_stub.cpp')86+ source_file = os.path.join(args.code_path, "host_stub.cpp")
90 try:87 try:
91- with open(source_file, encoding='utf-8') as file:88+ with open(source_file, encoding="utf-8") as file:
92 content = file.read()89 content = file.read()
93 except Exception as err:90 except Exception as err:
94 print("[ERROR]: read file failed, filename is {}".format(source_file))91 print("[ERROR]: read file failed, filename is {}".format(source_file))
@@ -98,12 +95,13 @@ def main():
98 95 
99 update_content = update_source_content(args.obj_path, update_section)96 update_content = update_source_content(args.obj_path, update_section)
100 try:97 try:
101- with open(source_file, 'w', encoding='utf-8') as file:98+ with open(source_file, "w", encoding="utf-8") as file:
102 os.chmod(source_file, stat.S_IRUSR + stat.S_IWUSR)99 os.chmod(source_file, stat.S_IRUSR + stat.S_IWUSR)
103 file.write(update_content)100 file.write(update_content)
104 except Exception as err:101 except Exception as err:
105 print("[ERROR]: write file failed, filename is {}".format(source_file))102 print("[ERROR]: write file failed, filename is {}".format(source_file))
106 raise err103 raise err
107 104 
108-if __name__ == '__main__':105+ 
106+if __name__ == "__main__":
109 main()107 main()
@@ -12,6 +12,12 @@
12 12 
13 13 
14__version__ = "0.1.0"14__version__ = "0.1.0"
15-__all__ = ["markdown_to_html", "gfm_to_html", "parse_file", "parse_string", "render_html"]15+__all__ = [
16+ "markdown_to_html",
17+ "gfm_to_html",
18+ "parse_file",
19+ "parse_string",
20+ "render_html",
21+]
16 22 
17from .parser import markdown_to_html, gfm_to_html, parse_file, parse_string, render_html23from .parser import markdown_to_html, gfm_to_html, parse_file, parse_string, render_html
@@ -20,7 +20,9 @@ from pathlib import Path
20 20 
21from .parser import parse_file, parse_string, render_html21from .parser import parse_file, parse_string, render_html
22 22 
23-logging.basicConfig(format="%(asctime)s [%(levelname)s] %(message)s", level=logging.INFO)23+logging.basicConfig(
24+ format="%(asctime)s [%(levelname)s] %(message)s", level=logging.INFO
25+)
24logger = logging.getLogger("mdparser")26logger = logging.getLogger("mdparser")
25 27 
26_STATIC_EXTS = {28_STATIC_EXTS = {
@@ -43,12 +45,18 @@ def _build_parser():
43 prog="mdparser",45 prog="mdparser",
44 description="High-performance Markdown-to-HTML converter based on cmark-gfm (C).",46 description="High-performance Markdown-to-HTML converter based on cmark-gfm (C).",
45 )47 )
46- parser.add_argument("input", nargs="*", help="Markdown file(s) or directory to parse")48+ parser.add_argument(
49+ "input", nargs="*", help="Markdown file(s) or directory to parse"
50+ )
47 parser.add_argument("-o", "--output", help="Output file or directory")51 parser.add_argument("-o", "--output", help="Output file or directory")
48 parser.add_argument("--no-gfm", action="store_true", help="Disable GFM extensions")52 parser.add_argument("--no-gfm", action="store_true", help="Disable GFM extensions")
49 parser.add_argument("--stdin", action="store_true", help="Read from stdin")53 parser.add_argument("--stdin", action="store_true", help="Read from stdin")
50- parser.add_argument("-s", "--style", action="store_true", help="Embed GitHub-style CSS")54+ parser.add_argument(
51- parser.add_argument("-r", "--recursive", action="store_true", help="Scan recursively")55+ "-s", "--style", action="store_true", help="Embed GitHub-style CSS"
56+ )
57+ parser.add_argument(
58+ "-r", "--recursive", action="store_true", help="Scan recursively"
59+ )
52 return parser60 return parser
53 61 
54 62 
@@ -60,67 +60,67 @@ _CANN_OPEN_RE = re.compile(
60)60)
61 61 
62_CANN_CLOSE_RE = re.compile(62_CANN_CLOSE_RE = re.compile(
63- r'</cann-filter\b[^>]*>',63+ r"</cann-filter\b[^>]*>",
64 re.IGNORECASE,64 re.IGNORECASE,
65)65)
66 66 
67-_OTHER_TAG_NAMES = r'term|ph|__gm__|__ubuf__'67+_OTHER_TAG_NAMES = r"term|ph|__gm__|__ubuf__"
68 68 
69_OTHER_TAG_LINE_RE = re.compile(69_OTHER_TAG_LINE_RE = re.compile(
70- r'^[ \t]*</?(' + _OTHER_TAG_NAMES + r')\b[^>]*>[ \t]*$',70+ r"^[ \t]*</?(" + _OTHER_TAG_NAMES + r")\b[^>]*>[ \t]*$",
71 re.IGNORECASE,71 re.IGNORECASE,
72)72)
73 73 
74_OTHER_TAG_WRAP_RE = re.compile(74_OTHER_TAG_WRAP_RE = re.compile(
75- r'<(' + _OTHER_TAG_NAMES + r')\b[^>]*>([\s\S]*?)</\1>',75+ r"<(" + _OTHER_TAG_NAMES + r")\b[^>]*>([\s\S]*?)</\1>",
76 re.IGNORECASE,76 re.IGNORECASE,
77)77)
78 78 
79_OTHER_TAG_ANY_RE = re.compile(79_OTHER_TAG_ANY_RE = re.compile(
80- r'</?(' + _OTHER_TAG_NAMES + r')\b[^>]*>',80+ r"</?(" + _OTHER_TAG_NAMES + r")\b[^>]*>",
81 re.IGNORECASE,81 re.IGNORECASE,
82)82)
83 83 
84 84 
85-_FENCE_OPEN_RE = re.compile(r'^[ \t]{0,3}(`{3,}|~{3,})', re.MULTILINE)85+_FENCE_OPEN_RE = re.compile(r"^[ \t]{0,3}(`{3,}|~{3,})", re.MULTILINE)
86 86 
87-_INLINE_CODE_SPAN_RE = re.compile(r'`+[^\n]*?`+')87+_INLINE_CODE_SPAN_RE = re.compile(r"`+[^\n]*?`+")
88 88 
89-_LONE_TILDE_RE = re.compile(r'(?<!\\)(?<!~)~(?!~)')89+_LONE_TILDE_RE = re.compile(r"(?<!\\)(?<!~)~(?!~)")
90 90 
91 91 
92def _escape_lone_tildes_in_line(line: str) -> str:92def _escape_lone_tildes_in_line(line: str) -> str:
93 result = []93 result = []
94 last_end = 094 last_end = 0
95 for m in _INLINE_CODE_SPAN_RE.finditer(line):95 for m in _INLINE_CODE_SPAN_RE.finditer(line):
96- before = line[last_end:m.start()]96+ before = line[last_end : m.start()]
97- result.append(_LONE_TILDE_RE.sub(r'\\~', before))97+ result.append(_LONE_TILDE_RE.sub(r"\\~", before))
98 result.append(m.group())98 result.append(m.group())
99 last_end = m.end()99 last_end = m.end()
100 remaining = line[last_end:]100 remaining = line[last_end:]
101- result.append(_LONE_TILDE_RE.sub(r'\\~', remaining))101+ result.append(_LONE_TILDE_RE.sub(r"\\~", remaining))
102- return ''.join(result)102+ return "".join(result)
103 103 
104 104 
105def _escape_lone_tildes(md_text: str) -> str:105def _escape_lone_tildes(md_text: str) -> str:
106- lines = md_text.split('\n')106+ lines = md_text.split("\n")
107 result = []107 result = []
108 in_fence = False108 in_fence = False
109 fence_char = None109 fence_char = None
110 110 
111 for line in lines:111 for line in lines:
112- stripped = line.lstrip(' \t')112+ stripped = line.lstrip(" \t")
113 113 
114 if in_fence:114 if in_fence:
115 result.append(line)115 result.append(line)
116 if fence_char and re.match(116 if fence_char and re.match(
117- rf'^{re.escape(fence_char)}{{3,}}[ \t]*$', stripped117+ rf"^{re.escape(fence_char)}{{3,}}[ \t]*$", stripped
118 ):118 ):
119 in_fence = False119 in_fence = False
120 fence_char = None120 fence_char = None
121 continue121 continue
122 122 
123- fence_match = re.match(r'^(`{3,}|~{3,})', stripped)123+ fence_match = re.match(r"^(`{3,}|~{3,})", stripped)
124 if fence_match:124 if fence_match:
125 in_fence = True125 in_fence = True
126 fence_char = fence_match.group(1)[0]126 fence_char = fence_match.group(1)[0]
@@ -129,41 +129,41 @@ def _escape_lone_tildes(md_text: str) -> str:
129 129 
130 result.append(_escape_lone_tildes_in_line(line))130 result.append(_escape_lone_tildes_in_line(line))
131 131 
132- return '\n'.join(result)132+ return "\n".join(result)
133 133 
134 134 
135-_MATH_BLOCK_RE = re.compile(r'(?<!\\)\$\$[\s\S]*?(?<!\\)\$\$')135+_MATH_BLOCK_RE = re.compile(r"(?<!\\)\$\$[\s\S]*?(?<!\\)\$\$")
136 136 
137-_MATH_INLINE_RE = re.compile(r'(?<!\\)(?<!\$)\$(?!\$)[^\n$]+?(?<!\\)\$(?!\$)')137+_MATH_INLINE_RE = re.compile(r"(?<!\\)(?<!\$)\$(?!\$)[^\n$]+?(?<!\\)\$(?!\$)")
138 138 
139-_MATH_PLACEHOLDER_FMT = '@@MATH{n}@@'139+_MATH_PLACEHOLDER_FMT = "@@MATH{n}@@"
140 140 
141 141 
142def _split_by_fences(md_text: str):142def _split_by_fences(md_text: str):
143- lines = md_text.split('\n')143+ lines = md_text.split("\n")
144 segments = []144 segments = []
145 current = []145 current = []
146 is_code = False146 is_code = False
147 fence_char = None147 fence_char = None
148 148 
149 for line in lines:149 for line in lines:
150- stripped = line.lstrip(' \t')150+ stripped = line.lstrip(" \t")
151 151 
152 if is_code:152 if is_code:
153 current.append(line)153 current.append(line)
154 if fence_char and re.match(154 if fence_char and re.match(
155- rf'^{re.escape(fence_char)}{{3,}}[ \t]*$', stripped155+ rf"^{re.escape(fence_char)}{{3,}}[ \t]*$", stripped
156 ):156 ):
157 is_code = False157 is_code = False
158 fence_char = None158 fence_char = None
159- segments.append((True, '\n'.join(current)))159+ segments.append((True, "\n".join(current)))
160 current = []160 current = []
161 continue161 continue
162 162 
163- fence_match = re.match(r'^(`{3,}|~{3,})', stripped)163+ fence_match = re.match(r"^(`{3,}|~{3,})", stripped)
164 if fence_match:164 if fence_match:
165 if current:165 if current:
166- segments.append((False, '\n'.join(current)))166+ segments.append((False, "\n".join(current)))
167 current = []167 current = []
168 is_code = True168 is_code = True
169 fence_char = fence_match.group(1)[0]169 fence_char = fence_match.group(1)[0]
@@ -173,7 +173,7 @@ def _split_by_fences(md_text: str):
173 current.append(line)173 current.append(line)
174 174 
175 if current:175 if current:
176- segments.append((is_code, '\n'.join(current)))176+ segments.append((is_code, "\n".join(current)))
177 177 
178 return segments178 return segments
179 179 
@@ -183,7 +183,7 @@ def _make_code_stasher():
183 183 
184 def _stash(m):184 def _stash(m):
185 spans.append(m.group(0))185 spans.append(m.group(0))
186- return f'\x00CODE{len(spans) - 1}\x00'186+ return f"\x00CODE{len(spans) - 1}\x00"
187 187 
188 return _stash, spans188 return _stash, spans
189 189 
@@ -213,17 +213,17 @@ def _extract_math_blocks(md_text: str) -> tuple:
213 text = _MATH_INLINE_RE.sub(stash_math, text)213 text = _MATH_INLINE_RE.sub(stash_math, text)
214 214 
215 for i, code in enumerate(code_spans):215 for i, code in enumerate(code_spans):
216- text = text.replace(f'\x00CODE{i}\x00', code)216+ text = text.replace(f"\x00CODE{i}\x00", code)
217 217 
218 result.append(text)218 result.append(text)
219 219 
220- return '\n'.join(result), blocks220+ return "\n".join(result), blocks
221 221 
222 222 
223def _restore_math_blocks(html: str, blocks: list) -> str:223def _restore_math_blocks(html: str, blocks: list) -> str:
224 for i, block in enumerate(blocks):224 for i, block in enumerate(blocks):
225 placeholder = _MATH_PLACEHOLDER_FMT.format(n=i)225 placeholder = _MATH_PLACEHOLDER_FMT.format(n=i)
226- encoded = block.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')226+ encoded = block.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
227 html = html.replace(placeholder, encoded)227 html = html.replace(placeholder, encoded)
228 return html228 return html
229 229 
@@ -235,8 +235,8 @@ def _convert_cann_filter(md_text: str) -> str:
235 若跨表格单元格/列表项边界则产生非法 HTML 结构。转为注释后 cmarkgfm235 若跨表格单元格/列表项边界则产生非法 HTML 结构。转为注释后 cmarkgfm
236 透传为无害注释,不破坏结构。后续由 config.mjs 将注释还原为 div[data-filter]。236 透传为无害注释,不破坏结构。后续由 config.mjs 将注释还原为 div[data-filter]。
237 """237 """
238- md_text = _CANN_OPEN_RE.sub(r'<!--cann-filter:\1-->', md_text)238+ md_text = _CANN_OPEN_RE.sub(r"<!--cann-filter:\1-->", md_text)
239- md_text = _CANN_CLOSE_RE.sub(r'<!--/cann-filter-->', md_text)239+ md_text = _CANN_CLOSE_RE.sub(r"<!--/cann-filter-->", md_text)
240 return md_text240 return md_text
241 241 
242 242 
@@ -245,17 +245,18 @@ def _strip_other_tags(md_text: str) -> str:
245 245 
246 这些标签同样会被 cmarkgfm 视为 raw HTML block,破坏 markdown 结构。246 这些标签同样会被 cmarkgfm 视为 raw HTML block,破坏 markdown 结构。
247 """247 """
248- lines = md_text.split('\n')248+ lines = md_text.split("\n")
249 result = []249 result = []
250 for line in lines:250 for line in lines:
251 if _OTHER_TAG_LINE_RE.match(line):251 if _OTHER_TAG_LINE_RE.match(line):
252 continue252 continue
253 result.append(line)253 result.append(line)
254- md_text = '\n'.join(result)254+ md_text = "\n".join(result)
255- md_text = _OTHER_TAG_WRAP_RE.sub(r'\2', md_text)255+ md_text = _OTHER_TAG_WRAP_RE.sub(r"\2", md_text)
256- md_text = _OTHER_TAG_ANY_RE.sub('', md_text)256+ md_text = _OTHER_TAG_ANY_RE.sub("", md_text)
257 return md_text257 return md_text
258 258 
259+ 
259_CALLOUT_RE = re.compile(260_CALLOUT_RE = re.compile(
260 r"<blockquote>\s*\n<p>\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\](.*?)</p>\s*\n(.*?)</blockquote>",261 r"<blockquote>\s*\n<p>\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\](.*?)</p>\s*\n(.*?)</blockquote>",
261 re.DOTALL,262 re.DOTALL,
@@ -552,7 +553,7 @@ def _highlight_code(html: str) -> str:
552 lang = m.group(1)553 lang = m.group(1)
553 code = m.group(2)554 code = m.group(2)
554 text = html_mod.unescape(code)555 text = html_mod.unescape(code)
555- text = re.sub(r'<br\s*/?>', '\n', text)556+ text = re.sub(r"<br\s*/?>", "\n", text)
556 if not lang:557 if not lang:
557 lang = "text"558 lang = "text"
558 label = _PYGMENTS_LANG_LABELS.get(lang.lower(), lang.capitalize())559 label = _PYGMENTS_LANG_LABELS.get(lang.lower(), lang.capitalize())
@@ -42,7 +42,7 @@ _LEGACY_TILING_STRUCTS = [
42 "UnPadTiling",42 "UnPadTiling",
43 "PadTiling",43 "PadTiling",
44 "TopkTiling",44 "TopkTiling",
45- "ConfusionTransposeTiling"45+ "ConfusionTransposeTiling",
46]46]
47 47 
48 48 
@@ -53,7 +53,7 @@ def gen_tiling(tiling_header_file):
53 print("warning: no userdef tiling header file: ", tiling_header_file)53 print("warning: no userdef tiling header file: ", tiling_header_file)
54 return single_tiling_source54 return single_tiling_source
55 print("generate tiling def header file: ", tiling_header_file)55 print("generate tiling def header file: ", tiling_header_file)
56- pattern = re.compile(r'[(](.*)[)]', re.S)56+ pattern = re.compile(r"[(](.*)[)]", re.S)
57 57 
58 def parse_legacy_tiling(struct_def):58 def parse_legacy_tiling(struct_def):
59 # export legacy tiling structs with 'using namespace' to ensure compatibility59 # export legacy tiling structs with 'using namespace' to ensure compatibility
@@ -61,38 +61,43 @@ def gen_tiling(tiling_header_file):
61 if struct_def in _LEGACY_TILING_STRUCTS:61 if struct_def in _LEGACY_TILING_STRUCTS:
62 single_legacy_tiling_export += f"using {_NAMESPACE}::{struct_def};\n"62 single_legacy_tiling_export += f"using {_NAMESPACE}::{struct_def};\n"
63 63 
64- with open(tiling_header_file, 'r') as fd:64+ with open(tiling_header_file, "r") as fd:
65 lines = fd.readlines()65 lines = fd.readlines()
66 for line in lines:66 for line in lines:
67 line = line.strip()67 line = line.strip()
68- if (line.startswith('BEGIN_TILING_DATA_DEF')):68+ if line.startswith("BEGIN_TILING_DATA_DEF"):
69- single_tiling_source += '#pragma pack(push, 8)\n'69+ single_tiling_source += "#pragma pack(push, 8)\n"
70- single_tiling_source += 'struct '70+ single_tiling_source += "struct "
71 struct_def = re.findall(pattern, line)[0]71 struct_def = re.findall(pattern, line)[0]
72- single_tiling_source += struct_def + ' {\n'72+ single_tiling_source += struct_def + " {\n"
73 parse_legacy_tiling(struct_def)73 parse_legacy_tiling(struct_def)
74- elif (line.startswith('TILING_DATA_FIELD_DEF_ARR')):74+ elif line.startswith("TILING_DATA_FIELD_DEF_ARR"):
75 field_params = re.findall(pattern, line)[0]75 field_params = re.findall(pattern, line)[0]
76- fds = field_params.split(',')76+ fds = field_params.split(",")
77- single_tiling_source += ' {} {}[{}] = {{}};\n'.format(fds[0].strip(), fds[2].strip(), fds[1].strip())77+ single_tiling_source += " {} {}[{}] = {{}};\n".format(
78- elif (line.startswith('TILING_DATA_FIELD_DEF_STRUCT')):78+ fds[0].strip(), fds[2].strip(), fds[1].strip()
79+ )
80+ elif line.startswith("TILING_DATA_FIELD_DEF_STRUCT"):
79 field_params = re.findall(pattern, line)[0]81 field_params = re.findall(pattern, line)[0]
80- fds = field_params.split(',')82+ fds = field_params.split(",")
81- single_tiling_source += ' {} {};\n'.format(fds[0].strip(), fds[1].strip())83+ single_tiling_source += " {} {};\n".format(
82- elif (line.startswith('TILING_DATA_FIELD_DEF')):84+ fds[0].strip(), fds[1].strip()
85+ )
86+ elif line.startswith("TILING_DATA_FIELD_DEF"):
83 field_params = re.findall(pattern, line)[0]87 field_params = re.findall(pattern, line)[0]
84- fds = field_params.split(',')88+ fds = field_params.split(",")
85- single_tiling_source += ' {} {} = 0;\n'.format(fds[0].strip(), fds[1].strip())89+ single_tiling_source += " {} {} = 0;\n".format(
86- elif (line.startswith('END_TILING_DATA_DEF')):90+ fds[0].strip(), fds[1].strip()
87- single_tiling_source += '};\n'91+ )
88- single_tiling_source += '#pragma pack(pop)\n'92+ elif line.startswith("END_TILING_DATA_DEF"):
93+ single_tiling_source += "};\n"
94+ single_tiling_source += "#pragma pack(pop)\n"
89 return single_tiling_source, single_legacy_tiling_export95 return single_tiling_source, single_legacy_tiling_export
90 96 
91 97 
92- 98+if __name__ == "__main__":
93-if __name__ == '__main__':
94 if len(sys.argv) <= 2:99 if len(sys.argv) <= 2:
95- raise RuntimeError('arguments must greater than 2')100+ raise RuntimeError("arguments must greater than 2")
96 res = """#ifndef __TIKCFW_KERNEL_TILING_H_101 res = """#ifndef __TIKCFW_KERNEL_TILING_H_
97#define __TIKCFW_KERNEL_TILING_H_102#define __TIKCFW_KERNEL_TILING_H_
98 103 
@@ -122,7 +127,7 @@ if __name__ == '__main__':
122 res += tiling_source + "} // namespace tiling\n} // namespace AscendC\n\n"127 res += tiling_source + "} // namespace tiling\n} // namespace AscendC\n\n"
123 res += legacy_tiling_export128 res += legacy_tiling_export
124 129 
125- res += '#endif\n'130+ res += "#endif\n"
126 131 
127 generate_file = sys.argv[2]132 generate_file = sys.argv[2]
128 absolute_file = os.path.abspath(generate_file)133 absolute_file = os.path.abspath(generate_file)
@@ -130,5 +135,7 @@ if __name__ == '__main__':
130 if not os.path.exists(generate_dir):135 if not os.path.exists(generate_dir):
131 os.makedirs(generate_dir, exist_ok=True)136 os.makedirs(generate_dir, exist_ok=True)
132 137 
133- with os.fdopen(os.open(absolute_file, os.O_RDWR | os.O_CREAT | os.O_TRUNC), 'w') as ofd:138+ with os.fdopen(
139+ os.open(absolute_file, os.O_RDWR | os.O_CREAT | os.O_TRUNC), "w"
140+ ) as ofd:
134 ofd.write(res)141 ofd.write(res)
@@ -30,9 +30,10 @@ class IniParser(object):
30 """30 """
31 initial parser class31 initial parser class
32 """32 """
33+ 
33 required_op_info_keys = ["opKernelLib"]34 required_op_info_keys = ["opKernelLib"]
34 required_custom_op_info_keys = ["kernelSo", "functionName"]35 required_custom_op_info_keys = ["kernelSo", "functionName"]
35- input_output_info_keys = {'format', 'type', 'name'} # set for difference36+ input_output_info_keys = {"format", "type", "name"} # set for difference
36 37 
37 def __init__(self):38 def __init__(self):
38 self.aicpu_ops_info = None39 self.aicpu_ops_info = None
@@ -63,20 +64,24 @@ class IniParser(object):
63 # option in op is configuration for op, eg. opInfo.engine=DNN_VM_AICPU64 # option in op is configuration for op, eg. opInfo.engine=DNN_VM_AICPU
64 for opt in cfg.options(op):65 for opt in cfg.options(op):
65 if len(opt.split(".")) != 2:66 if len(opt.split(".")) != 2:
66- print("## Parse op [%s] setting: \"%s\", not recognized!" % (op, opt))67+ print('## Parse op [%s] setting: "%s", not recognized!' % (op, opt))
67 continue68 continue
68 # one opt_sec will include serval info, eg. opInfo: {"engine": xxx, "flagAsync": xxx, ...}69 # one opt_sec will include serval info, eg. opInfo: {"engine": xxx, "flagAsync": xxx, ...}
69 opt_sec, opt_subsec = opt.split(".")70 opt_sec, opt_subsec = opt.split(".")
70 if opt_sec not in self.aicpu_ops_info.get(op):71 if opt_sec not in self.aicpu_ops_info.get(op):
71 self.aicpu_ops_info.get(op)[opt_sec] = {opt_subsec: cfg[op][opt]}72 self.aicpu_ops_info.get(op)[opt_sec] = {opt_subsec: cfg[op][opt]}
72 else:73 else:
73- self.aicpu_ops_info.get(op)[opt_sec].update({opt_subsec: cfg[op][opt]})74+ self.aicpu_ops_info.get(op)[opt_sec].update(
75+ {opt_subsec: cfg[op][opt]}
76+ )
74 77 
75 def check_custom_op_info(self, op_name, op_info):78 def check_custom_op_info(self, op_name, op_info):
76 """79 """
77 Check aicpu_cust_kernel.ini op definition80 Check aicpu_cust_kernel.ini op definition
78 """81 """
79- missing_keys = [k for k in self.required_custom_op_info_keys if k not in op_info]82+ missing_keys = [
83+ k for k in self.required_custom_op_info_keys if k not in op_info
84+ ]
80 if len(missing_keys) > 0:85 if len(missing_keys) > 0:
81 print("op: " + op_name + " opInfo missing: " + ",".join(missing_keys))86 print("op: " + op_name + " opInfo missing: " + ",".join(missing_keys))
82 raise KeyError("bad key value")87 raise KeyError("bad key value")
@@ -123,28 +128,40 @@ class IniParser(object):
123 self.check_op_info(op_name, sec_info)128 self.check_op_info(op_name, sec_info)
124 op_info_flag = True129 op_info_flag = True
125 130 
126- elif (op_sec[:5] == "input" and op_sec[5:].isdigit()) or \131+ elif (
127- (op_sec[:6] == "output" and op_sec[6:].isdigit()) or \132+ (op_sec[:5] == "input" and op_sec[5:].isdigit())
128- (op_sec[:13] == "dynamic_input" and op_sec[13:].isdigit()) or \133+ or (op_sec[:6] == "output" and op_sec[6:].isdigit())
129- (op_sec[:14] == "dynamic_output" and op_sec[14:].isdigit()):134+ or (op_sec[:13] == "dynamic_input" and op_sec[13:].isdigit())
135+ or (op_sec[:14] == "dynamic_output" and op_sec[14:].isdigit())
136+ ):
130 ret = self.check_op_input_output(sec_info)137 ret = self.check_op_input_output(sec_info)
131 if not ret:138 if not ret:
132- print("## %s: %s should has format type or name as the key, but getting %s" %139+ print(
133- (op_name, op_sec, sec_info))140+ "## %s: %s should has format type or name as the key, but getting %s"
141+ % (op_name, op_sec, sec_info)
142+ )
134 raise KeyError("bad op_sets key")143 raise KeyError("bad op_sets key")
135 op_io_flag = False144 op_io_flag = False
136 145 
137 else:146 else:
138- print("Only opInfo, input[0-9], output[0-9] can be used as a key, "147+ print(
139- "but op %s has the key %s" % (op_name, op_sec))148+ "Only opInfo, input[0-9], output[0-9] can be used as a key, "
149+ "but op %s has the key %s" % (op_name, op_sec)
150+ )
140 raise KeyError("bad key value")151 raise KeyError("bad key value")
141 if not op_info_flag:152 if not op_info_flag:
142 if self.warn_print:153 if self.warn_print:
143- print("%s\t## OP %s: defined missing opInfo section %s" % (COLOR_RED, op_name, COLOR_END))154+ print(
155+ "%s\t## OP %s: defined missing opInfo section %s"
156+ % (COLOR_RED, op_name, COLOR_END)
157+ )
144 self.warning_ops["opInfo"].append(op_name)158 self.warning_ops["opInfo"].append(op_name)
145 if not op_io_flag:159 if not op_io_flag:
146 if self.warn_print:160 if self.warn_print:
147- print("%s\t## OP %s: defined missing input/output section %s" % (COLOR_CYAN, op_name, COLOR_END))161+ print(
162+ "%s\t## OP %s: defined missing input/output section %s"
163+ % (COLOR_CYAN, op_name, COLOR_END)
164+ )
148 self.warning_ops["io"].append(op_name)165 self.warning_ops["io"].append(op_name)
149 # if custom flag is set, we will push all custom op in the aicpu_op_info166 # if custom flag is set, we will push all custom op in the aicpu_op_info
150 # else we will remove them, and push into individual custom json167 # else we will remove them, and push into individual custom json
@@ -157,23 +174,37 @@ class IniParser(object):
157 """174 """
158 Write all the data into op json175 Write all the data into op json
159 """176 """
177+ 
160 def _write(info, file):178 def _write(info, file):
161 with open(file, "w") as f:179 with open(file, "w") as f:
162 # Only the owner and group have rights180 # Only the owner and group have rights
163- os.chmod(file, stat.S_IWGRP + stat.S_IWUSR + stat.S_IRGRP + stat.S_IRUSR)181+ os.chmod(
164- json.dump(info, f, sort_keys=True, indent=4, separators=(',', ':'))182+ file, stat.S_IWGRP + stat.S_IWUSR + stat.S_IRGRP + stat.S_IRUSR
183+ )
184+ json.dump(info, f, sort_keys=True, indent=4, separators=(",", ":"))
165 185 
166 json_file_real_path = os.path.realpath(json_file_path)186 json_file_real_path = os.path.realpath(json_file_path)
167 _write(self.aicpu_ops_info, json_file_real_path)187 _write(self.aicpu_ops_info, json_file_real_path)
168- print(">>>> Found %s AICPU ops, write into: %s" % (len(self.aicpu_ops_info), json_file_real_path))188+ print(
189+ ">>>> Found %s AICPU ops, write into: %s"
190+ % (len(self.aicpu_ops_info), json_file_real_path)
191+ )
169 192 
170 if not self.custom_flag:193 if not self.custom_flag:
171 file_path, file_name = os.path.split(json_file_real_path)194 file_path, file_name = os.path.split(json_file_real_path)
172- custom_file_path = os.path.join(file_path, "%s_custom%s" % os.path.splitext(file_name))195+ custom_file_path = os.path.join(
196+ file_path, "%s_custom%s" % os.path.splitext(file_name)
197+ )
173 _write(self.custom_ops_info, custom_file_path)198 _write(self.custom_ops_info, custom_file_path)
174- print(">>>> Found %s custom AICPU ops, write into: %s" % (len(self.custom_ops_info), custom_file_path))199+ print(
200+ ">>>> Found %s custom AICPU ops, write into: %s"
201+ % (len(self.custom_ops_info), custom_file_path)
202+ )
175 else:203 else:
176- print("### Custom flag is set, all custom ops have been integrated into: %s" % json_file_real_path)204+ print(
205+ "### Custom flag is set, all custom ops have been integrated into: %s"
206+ % json_file_real_path
207+ )
177 208 
178 def parse(self, ini_paths: list, out_file_path, custom=False):209 def parse(self, ini_paths: list, out_file_path, custom=False):
179 """210 """
@@ -190,32 +221,31 @@ class IniParser(object):
190 try:221 try:
191 self.check_op_info_setting()222 self.check_op_info_setting()
192 except KeyError as e:223 except KeyError as e:
193- print("bad format key value, failed to generate json file, detail info: \n%s" % e)224+ print(
225+ "bad format key value, failed to generate json file, detail info: \n%s"
226+ % e
227+ )
194 finally:228 finally:
195 self.write(out_file_path)229 self.write(out_file_path)
196 if self.warning_ops and self.warn_print:230 if self.warning_ops and self.warn_print:
197 print(COLOR_BOLD + "=" * 80 + COLOR_END)231 print(COLOR_BOLD + "=" * 80 + COLOR_END)
198 for warn_type, warn_ops in self.warning_ops.items():232 for warn_type, warn_ops in self.warning_ops.items():
199- print("\tNo \"%s\" ops set: %s" % (warn_type, warn_ops))233+ print('\tNo "%s" ops set: %s' % (warn_type, warn_ops))
200 print("parse try except normal")234 print("parse try except normal")
201 235 
202 236 
203def main():237def main():
204- """ A Parser function for ini file. """238+ """A Parser function for ini file."""
205 parser = argparse.ArgumentParser(239 parser = argparse.ArgumentParser(
206 prog="Parser_ini.py",240 prog="Parser_ini.py",
207 usage="python3 PATH-TO/parser_ini.py [INI FILES] [OUTPUT_FILE.json] OPERATION",241 usage="python3 PATH-TO/parser_ini.py [INI FILES] [OUTPUT_FILE.json] OPERATION",
208 description="Parser ini info and check tool",242 description="Parser ini info and check tool",
209- add_help=True243+ add_help=True,
210 )244 )
211 parser.add_argument(245 parser.add_argument(
212- "-c", "--custom", action="store_true",246+ "-c", "--custom", action="store_true", help="Custom op compiled in"
213- help="Custom op compiled in"
214- )
215- parser.add_argument(
216- "FILES", nargs='*',
217- help=argparse.SUPPRESS
218 )247 )
248+ parser.add_argument("FILES", nargs="*", help=argparse.SUPPRESS)
219 args = parser.parse_args()249 args = parser.parse_args()
220 outfile_path = "tf_kernel.json"250 outfile_path = "tf_kernel.json"
221 ini_file_paths = []251 ini_file_paths = []
@@ -233,5 +263,5 @@ def main():
233 ini_parser.parse(ini_file_paths, outfile_path, custom=args.custom)263 ini_parser.parse(ini_file_paths, outfile_path, custom=args.custom)
234 264 
235 265 
236-if __name__ == '__main__':266+if __name__ == "__main__":
237 main()267 main()