Merged
style: ruff 配置行宽 120 并关闭 magic trailing comma #6286
style: ruff 配置行宽 120 并关闭 magic trailing comma #6286
Merged
zhangyujia77Add on Sep 21
Total: 560 files changed+10456-37567
@@ -48,9 +48,7 @@ SKILL_DIR = SCRIPT_DIR.parent
48SKILLS_DIR = SKILL_DIR.parent48SKILLS_DIR = SKILL_DIR.parent
49REPO_ROOT = SKILL_DIR.parents[2]49REPO_ROOT = SKILL_DIR.parents[2]
50NPU_ARCH_FACTS_PATH = SKILLS_DIR / "asc-npu-arch" / "references" / "npu-arch-facts.json"50NPU_ARCH_FACTS_PATH = SKILLS_DIR / "asc-npu-arch" / "references" / "npu-arch-facts.json"
51-GENERATION_CONSTRAINTS_PATH = (51+GENERATION_CONSTRAINTS_PATH = SKILL_DIR / "references" / "foundations" / "generation-constraints.json"
52- SKILL_DIR / "references" / "foundations" / "generation-constraints.json"
53-)
54REFERENCE_CONSTRAINT_FILES = {52REFERENCE_CONSTRAINT_FILES = {
55 "npu_arch_facts": str(NPU_ARCH_FACTS_PATH),53 "npu_arch_facts": str(NPU_ARCH_FACTS_PATH),
56 "generation_constraints": str(GENERATION_CONSTRAINTS_PATH),54 "generation_constraints": str(GENERATION_CONSTRAINTS_PATH),
@@ -88,9 +86,7 @@ def get_npu_arch_facts() -> Dict[str, Any]:
88 86 
89def get_generation_constraints() -> Dict[str, Any]:87def get_generation_constraints() -> Dict[str, Any]:
90 """Return UT generation constraints from the structured reference."""88 """Return UT generation constraints from the structured reference."""
91- return _load_reference_json_safe(89+ return _load_reference_json_safe("generation_constraints", GENERATION_CONSTRAINTS_PATH)
92- "generation_constraints", GENERATION_CONSTRAINTS_PATH
93- )
94 90 
95 91 
96def ensure_reference_constraints_loaded() -> None:92def ensure_reference_constraints_loaded() -> None:
@@ -98,9 +94,7 @@ def ensure_reference_constraints_loaded() -> None:
98 if not _REFERENCE_LOAD_ERRORS:94 if not _REFERENCE_LOAD_ERRORS:
99 return95 return
100 96 
101- details = "; ".join(97+ details = "; ".join(f"{name}: {exc}" for name, exc in sorted(_REFERENCE_LOAD_ERRORS.items()))
102- f"{name}: {exc}" for name, exc in sorted(_REFERENCE_LOAD_ERRORS.items())
103- )
104 raise RuntimeError(f"failed to load structured references: {details}")98 raise RuntimeError(f"failed to load structured references: {details}")
105 99 
106 100 
@@ -109,9 +103,7 @@ def _enum_member_name(value: str) -> str:
109 return re.sub(r"[^A-Z0-9]+", "_", value.upper()).strip("_")103 return re.sub(r"[^A-Z0-9]+", "_", value.upper()).strip("_")
110 104 
111 105 
112-def _enum_members_from_reference(106+def _enum_members_from_reference(facts: Dict[str, Any], fallback: Dict[str, str]) -> Dict[str, str]:
113- facts: Dict[str, Any], fallback: Dict[str, str]
114-) -> Dict[str, str]:
115 """Build Enum members from a reference object, keeping import usable on failure."""107 """Build Enum members from a reference object, keeping import usable on failure."""
116 members = {108 members = {
117 fact.get("enum_name", _enum_member_name(item_name)): item_name109 fact.get("enum_name", _enum_member_name(item_name)): item_name
@@ -134,26 +126,18 @@ C_API_GENERATION_FACTS = GENERATION_CONSTRAINTS.get("c_api_generation", {})
134UTILS_GENERATION_FACTS = GENERATION_CONSTRAINTS.get("utils_generation", {})126UTILS_GENERATION_FACTS = GENERATION_CONSTRAINTS.get("utils_generation", {})
135 127 
136 128 
137-ApiType = Enum(129+ApiType = Enum("ApiType", _enum_members_from_reference(API_TYPE_FACTS, {"UNAVAILABLE": "unavailable"}))
138- "ApiType",
139- _enum_members_from_reference(API_TYPE_FACTS, {"UNAVAILABLE": "unavailable"}),
140-)
141 130 
142 131 
143API_TYPE_MAP = {api_type.value: api_type for api_type in ApiType}132API_TYPE_MAP = {api_type.value: api_type for api_type in ApiType}
144API_TYPE_CHOICES = list(API_TYPE_MAP.keys())133API_TYPE_CHOICES = list(API_TYPE_MAP.keys())
145API_RESTRICTIONS = GENERATION_CONSTRAINTS.get("api_restrictions", {})134API_RESTRICTIONS = GENERATION_CONSTRAINTS.get("api_restrictions", {})
146-AIV_GENERIC_BINARY_APIS = frozenset(135+AIV_GENERIC_BINARY_APIS = frozenset(name.lower() for name in AIV_GENERATION_FACTS.get("generic_binary_apis", []))
147- name.lower() for name in AIV_GENERATION_FACTS.get("generic_binary_apis", [])
148-)
149AIV_GENERIC_SCALAR_TENSOR_DISPATCH_APIS = frozenset(136AIV_GENERIC_SCALAR_TENSOR_DISPATCH_APIS = frozenset(
150- name.lower()137+ name.lower() for name in AIV_GENERATION_FACTS.get("generic_scalar_tensor_dispatch_apis", [])
151- for name in AIV_GENERATION_FACTS.get("generic_scalar_tensor_dispatch_apis", [])
152)138)
153AIV_EXPLICIT_TEMPLATES = frozenset(AIV_GENERATION_FACTS.get("explicit_templates", []))139AIV_EXPLICIT_TEMPLATES = frozenset(AIV_GENERATION_FACTS.get("explicit_templates", []))
154-AIC_GENERIC_MMAD_LIKE_APIS = frozenset(140+AIC_GENERIC_MMAD_LIKE_APIS = frozenset(name.lower() for name in AIC_GENERATION_FACTS.get("generic_mmad_like_apis", []))
155- name.lower() for name in AIC_GENERATION_FACTS.get("generic_mmad_like_apis", [])
156-)
157AIC_EXPLICIT_TEMPLATES = frozenset(AIC_GENERATION_FACTS.get("explicit_templates", []))141AIC_EXPLICIT_TEMPLATES = frozenset(AIC_GENERATION_FACTS.get("explicit_templates", []))
158REG_GENERIC_TEMPLATES = frozenset(REG_GENERATION_FACTS.get("generic_templates", []))142REG_GENERIC_TEMPLATES = frozenset(REG_GENERATION_FACTS.get("generic_templates", []))
159SIMT_GENERIC_TEMPLATES = frozenset(SIMT_GENERATION_FACTS.get("generic_templates", []))143SIMT_GENERIC_TEMPLATES = frozenset(SIMT_GENERATION_FACTS.get("generic_templates", []))
@@ -161,32 +145,11 @@ C_API_GENERIC_TEMPLATES = frozenset(C_API_GENERATION_FACTS.get("generic_template
161UTILS_GENERIC_TEMPLATES = frozenset(UTILS_GENERATION_FACTS.get("generic_templates", []))145UTILS_GENERIC_TEMPLATES = frozenset(UTILS_GENERATION_FACTS.get("generic_templates", []))
162 146 
163 147 
164-ChipArch = Enum(148+ChipArch = Enum("ChipArch", _enum_members_from_reference(CHIP_FACTS, {"UNAVAILABLE": "unavailable"}))
165- "ChipArch",
166- _enum_members_from_reference(CHIP_FACTS, {"UNAVAILABLE": "unavailable"}),
167-)
168CHIP_ARCH_BY_NAME = {chip.value: chip for chip in ChipArch}149CHIP_ARCH_BY_NAME = {chip.value: chip for chip in ChipArch}
169-ARCH_DIR_MAP = dict(150+ARCH_DIR_MAP = dict((CHIP_ARCH_BY_NAME[chip_name], fact["arch_dir"]) for chip_name, fact in CHIP_FACTS.items())
170- (151+NPU_ARCH_MAP = dict((CHIP_ARCH_BY_NAME[chip_name], str(fact["npu_arch"])) for chip_name, fact in CHIP_FACTS.items())
171- CHIP_ARCH_BY_NAME[chip_name],152+SOC_VERSION_MAP = dict((CHIP_ARCH_BY_NAME[chip_name], fact["soc_version"]) for chip_name, fact in CHIP_FACTS.items())
172- fact["arch_dir"],
173- )
174- for chip_name, fact in CHIP_FACTS.items()
175-)
176-NPU_ARCH_MAP = dict(
177- (
178- CHIP_ARCH_BY_NAME[chip_name],
179- str(fact["npu_arch"]),
180- )
181- for chip_name, fact in CHIP_FACTS.items()
182-)
183-SOC_VERSION_MAP = dict(
184- (
185- CHIP_ARCH_BY_NAME[chip_name],
186- fact["soc_version"],
187- )
188- for chip_name, fact in CHIP_FACTS.items()
189-)
190DTYPE_MAP = dict(153DTYPE_MAP = dict(
191 (154 (
192 dtype_name,155 dtype_name,
@@ -200,12 +163,7 @@ DTYPE_MAP = dict(
200 for dtype_name, fact in DTYPE_FACTS.items()163 for dtype_name, fact in DTYPE_FACTS.items()
201)164)
202GENERATOR_DTYPE_MAP = dict(165GENERATOR_DTYPE_MAP = dict(
203- (166+ (dtype_name, fact) for dtype_name, fact in DTYPE_MAP.items() if fact.get("generic_ut_generation", False)
204- dtype_name,
205- fact,
206- )
207- for dtype_name, fact in DTYPE_MAP.items()
208- if fact.get("generic_ut_generation", False)
209)167)
210 168 
211 169 
@@ -262,9 +220,7 @@ def _load_adv_profile_entry(api_name: str, raw_profile: Any) -> AdvApiProfile:
262 return AdvApiProfile(source=source, output=output)220 return AdvApiProfile(source=source, output=output)
263 221 
264 222 
265-def get_adv_api_profile(223+def get_adv_api_profile(api_name: str, kernel_params: Dict[str, Any]) -> Optional[AdvApiProfile]:
266- api_name: str, kernel_params: Dict[str, Any]
267-) -> Optional[AdvApiProfile]:
268 """Return an executable high-level API profile explicitly supplied by this run."""224 """Return an executable high-level API profile explicitly supplied by this run."""
269 kernel_params = kernel_params or {}225 kernel_params = kernel_params or {}
270 profile = kernel_params.get("adv_profile", {})226 profile = kernel_params.get("adv_profile", {})
@@ -273,9 +229,7 @@ def get_adv_api_profile(
273 return _load_adv_profile_entry(normalize_adv_api_name(api_name), profile)229 return _load_adv_profile_entry(normalize_adv_api_name(api_name), profile)
274 230 
275 231 
276-def get_adv_profile_output_path(232+def get_adv_profile_output_path(api_name: str, kernel_params: Dict[str, Any]) -> Optional[str]:
277- api_name: str, kernel_params: Dict[str, Any]
278-) -> Optional[str]:
279 """Return the output path for an explicitly supplied high-level API profile."""233 """Return the output path for an explicitly supplied high-level API profile."""
280 profile = get_adv_api_profile(api_name, kernel_params)234 profile = get_adv_api_profile(api_name, kernel_params)
281 return profile.output if profile is not None else None235 return profile.output if profile is not None else None
@@ -293,15 +247,12 @@ def normalize_generated_cpp(code: str) -> str:
293 return normalized247 return normalized
294 248 
295 249 
296-def render_gtest_values_instantiation(250+def render_gtest_values_instantiation(api_name: str, api_name_upper: str, test_params: List[str]) -> str:
297- api_name: str, api_name_upper: str, test_params: List[str]
298-) -> str:
299 """Render gtest value instantiation in the repository clang-format style."""251 """Render gtest value instantiation in the repository clang-format style."""
300 testsuite = f"{api_name}Testsuite"252 testsuite = f"{api_name}Testsuite"
301 if len(test_params) == 1:253 if len(test_params) == 1:
302 return (254 return (
303- "INSTANTIATE_TEST_CASE_P(\n"255+ f"INSTANTIATE_TEST_CASE_P(\n TEST_{api_name_upper}, {testsuite}, ::testing::Values({test_params[0]}));"
304- f" TEST_{api_name_upper}, {testsuite}, ::testing::Values({test_params[0]}));"
305 )256 )
306 257 
307 values = ",\n ".join(test_params)258 values = ",\n ".join(test_params)
@@ -1128,19 +1079,14 @@ class AIVUTGenerator(UTGenerator):
1128 )1079 )
1129 1080 
1130 def _validate_binary_cases(self) -> None:1081 def _validate_binary_cases(self) -> None:
1131- non_binary_cases = [1082+ non_binary_cases = [tc.name for tc in self.config.test_cases if tc.input_count != 2]
1132- tc.name for tc in self.config.test_cases if tc.input_count != 2
1133- ]
1134 if non_binary_cases:1083 if non_binary_cases:
1135 raise ValueError(1084 raise ValueError(
1136- "binary AIV template requires input_count=2 for every test case; "1085+ f"binary AIV template requires input_count=2 for every test case; non-binary cases: {non_binary_cases}"
1137- f"non-binary cases: {non_binary_cases}"
1138 )1086 )
1139 1087 
1140 def _validate_scalar_tensor_dispatch_cases(self) -> None:1088 def _validate_scalar_tensor_dispatch_cases(self) -> None:
1141- invalid_cases = [1089+ invalid_cases = [tc.name for tc in self.config.test_cases if tc.input_count != 1]
1142- tc.name for tc in self.config.test_cases if tc.input_count != 1
1143- ]
1144 if invalid_cases:1090 if invalid_cases:
1145 raise ValueError(1091 raise ValueError(
1146 "scalar_tensor_dispatch AIV template requires input_count=1 for every test case; "1092 "scalar_tensor_dispatch AIV template requires input_count=1 for every test case; "
@@ -1168,9 +1114,7 @@ class AIVUTGenerator(UTGenerator):
1168 f"{dtype_info['size']}, {func_name}, {init_func_name}}}"1114 f"{dtype_info['size']}, {func_name}, {init_func_name}}}"
1169 )1115 )
1170 variables["INSTANTIATION"] = render_gtest_values_instantiation(1116 variables["INSTANTIATION"] = render_gtest_values_instantiation(
1171- self.config.api_name,1117+ self.config.api_name, self.config.api_name.upper(), test_params
1172- self.config.api_name.upper(),
1173- test_params,
1174 )1118 )
1175 1119 
1176 # 渲染各部分1120 # 渲染各部分
@@ -1208,14 +1152,9 @@ class AIVUTGenerator(UTGenerator):
1208 if tc.additional_params.get("tensor_trait", False):1152 if tc.additional_params.get("tensor_trait", False):
1209 dtype_name = f"TensorTrait<{tc.dtype}>"1153 dtype_name = f"TensorTrait<{tc.dtype}>"
1210 func_name = f"main_{self.config.api_name}<{dtype_name}>"1154 func_name = f"main_{self.config.api_name}<{dtype_name}>"
1211- test_params.append(1155+ test_params.append(f"{self.config.api_name}TestParams{{{tc.data_size}, {dtype_info['size']}, {func_name}}}")
1212- f"{self.config.api_name}TestParams{{{tc.data_size}, "
1213- f"{dtype_info['size']}, {func_name}}}"
1214- )
1215 variables["INSTANTIATION"] = render_gtest_values_instantiation(1156 variables["INSTANTIATION"] = render_gtest_values_instantiation(
1216- self.config.api_name,1157+ self.config.api_name, self.config.api_name.upper(), test_params
1217- self.config.api_name.upper(),
1218- test_params,
1219 )1158 )
1220 1159 
1221 return (1160 return (
@@ -1223,24 +1162,12 @@ class AIVUTGenerator(UTGenerator):
1223 "aiv_scalar_tensor_dispatch_basic",1162 "aiv_scalar_tensor_dispatch_basic",
1224 {1163 {
1225 **variables,1164 **variables,
1226- "KERNEL_CLASS": self.render(1165+ "KERNEL_CLASS": self.render("aiv_scalar_tensor_dispatch_kernel_class", variables),
1227- "aiv_scalar_tensor_dispatch_kernel_class", variables1166+ "MAIN_FUNCTION": self.render("aiv_scalar_tensor_dispatch_main_function", variables),
1228- ),1167+ "PARAM_STRUCT": self.render("aiv_scalar_tensor_dispatch_param_struct", variables),
1229- "MAIN_FUNCTION": self.render(1168+ "TEST_CLASS": self.render("aiv_scalar_tensor_dispatch_test_class", variables),
1230- "aiv_scalar_tensor_dispatch_main_function", variables1169+ "INSTANTIATION": self.render("aiv_scalar_tensor_dispatch_instantiation", variables),
1231- ),1170+ "TEST_CASE": self.render("aiv_scalar_tensor_dispatch_test_case", variables),
1232- "PARAM_STRUCT": self.render(
1233- "aiv_scalar_tensor_dispatch_param_struct", variables
1234- ),
1235- "TEST_CLASS": self.render(
1236- "aiv_scalar_tensor_dispatch_test_class", variables
1237- ),
1238- "INSTANTIATION": self.render(
1239- "aiv_scalar_tensor_dispatch_instantiation", variables
1240- ),
1241- "TEST_CASE": self.render(
1242- "aiv_scalar_tensor_dispatch_test_case", variables
1243- ),
1244 },1171 },
1245 ).rstrip()1172 ).rstrip()
1246 + "\n"1173 + "\n"
@@ -1347,8 +1274,7 @@ class ADVUTGenerator(UTGenerator):
1347 source_path = REPO_ROOT / profile.source1274 source_path = REPO_ROOT / profile.source
1348 if not source_path.is_file():1275 if not source_path.is_file():
1349 raise FileNotFoundError(1276 raise FileNotFoundError(
1350- f"ADV executable profile for {self.config.api_name!r} points to "1277+ f"ADV executable profile for {self.config.api_name!r} points to a missing reference UT: {source_path}"
1351- f"a missing reference UT: {source_path}"
1352 )1278 )
1353 1279 
1354 return normalize_generated_cpp(source_path.read_text(encoding="utf-8"))1280 return normalize_generated_cpp(source_path.read_text(encoding="utf-8"))
@@ -105,9 +105,7 @@ def parse_coverage_report(report_path: Optional[str]) -> Dict[str, Any]:
105 if match:105 if match:
106 return float(match.group(1))106 return float(match.group(1))
107 107 
108- for row in re.findall(108+ for row in re.findall(r"<tr\b[^>]*>.*?</tr>", content, re.IGNORECASE | re.DOTALL):
109- r"<tr\b[^>]*>.*?</tr>", content, re.IGNORECASE | re.DOTALL
110- ):
111 row_text = normalize_html_text(row)109 row_text = normalize_html_text(row)
112 if not re.search(rf"(?:^|\s){label_re}\s*:", row_text, re.IGNORECASE):110 if not re.search(rf"(?:^|\s){label_re}\s*:", row_text, re.IGNORECASE):
113 continue111 continue
@@ -128,13 +126,7 @@ def parse_coverage_report(report_path: Optional[str]) -> Dict[str, Any]:
128 "reason": "未在 coverage report 中解析到 Lines / Functions 百分比",126 "reason": "未在 coverage report 中解析到 Lines / Functions 百分比",
129 }127 }
130 128 
131- return {129+ return {"status": "available", "lines": lines, "functions": functions, "source": report_path, "reason": None}
132- "status": "available",
133- "lines": lines,
134- "functions": functions,
135- "source": report_path,
136- "reason": None,
137- }
138 130 
139 131 
140def get_token_usage(args: argparse.Namespace) -> Dict[str, Any]:132def get_token_usage(args: argparse.Namespace) -> Dict[str, Any]:
@@ -142,16 +134,10 @@ def get_token_usage(args: argparse.Namespace) -> Dict[str, Any]:
142 prompt = args.prompt_tokens134 prompt = args.prompt_tokens
143 completion = args.completion_tokens135 completion = args.completion_tokens
144 total = args.total_tokens136 total = args.total_tokens
145- source = (137+ source = "cli_args" if any(value is not None for value in [prompt, completion, total]) else None
146- "cli_args"
147- if any(value is not None for value in [prompt, completion, total])
148- else None
149- )
150 reason = None138 reason = None
151 139 
152- def parse_env_int(140+ def parse_env_int(value: Optional[str], name: str, invalid: List[str]) -> Optional[int]:
153- value: Optional[str], name: str, invalid: List[str]
154- ) -> Optional[int]:
155 if value is None:141 if value is None:
156 return None142 return None
157 143 
@@ -173,9 +159,7 @@ def get_token_usage(args: argparse.Namespace) -> Dict[str, Any]:
173 source = "env"159 source = "env"
174 invalid_env = []160 invalid_env = []
175 prompt = parse_env_int(env_prompt, "ASC_API_UT_PROMPT_TOKENS", invalid_env)161 prompt = parse_env_int(env_prompt, "ASC_API_UT_PROMPT_TOKENS", invalid_env)
176- completion = parse_env_int(162+ completion = parse_env_int(env_completion, "ASC_API_UT_COMPLETION_TOKENS", invalid_env)
177- env_completion, "ASC_API_UT_COMPLETION_TOKENS", invalid_env
178- )
179 total = parse_env_int(env_total, "ASC_API_UT_TOTAL_TOKENS", invalid_env)163 total = parse_env_int(env_total, "ASC_API_UT_TOTAL_TOKENS", invalid_env)
180 if invalid_env:164 if invalid_env:
181 reason = "忽略非法 token 环境变量: " + ", ".join(invalid_env)165 reason = "忽略非法 token 环境变量: " + ", ".join(invalid_env)
@@ -224,10 +208,7 @@ def print_generation_report(report: Dict[str, Any], stream) -> None:
224 write_line(f" 生成耗时: {report['elapsed_seconds']}s", stream)208 write_line(f" 生成耗时: {report['elapsed_seconds']}s", stream)
225 209 
226 if coverage["status"] == "available":210 if coverage["status"] == "available":
227- write_line(211+ write_line(f" 当前覆盖率: Lines={coverage['lines']}%, Functions={coverage['functions']}%", stream)
228- f" 当前覆盖率: Lines={coverage['lines']}%, Functions={coverage['functions']}%",
229- stream,
230- )
231 write_line(f" 覆盖率来源: {coverage['source']}", stream)212 write_line(f" 覆盖率来源: {coverage['source']}", stream)
232 else:213 else:
233 write_line(f" 当前覆盖率: 未获取 ({coverage['reason']})", stream)214 write_line(f" 当前覆盖率: 未获取 ({coverage['reason']})", stream)
@@ -279,10 +260,7 @@ def default_kernel_params(api_type: str) -> Dict[str, int]:
279 260 
280def default_input_count(api_type: str, api_name: str) -> int:261def default_input_count(api_type: str, api_name: str) -> int:
281 """Return the safest CLI default input count for generic templates."""262 """Return the safest CLI default input count for generic templates."""
282- if (263+ if api_type == ApiType.AIV.value and api_name.lower() in AIV_GENERIC_SCALAR_TENSOR_DISPATCH_APIS:
283- api_type == ApiType.AIV.value
284- and api_name.lower() in AIV_GENERIC_SCALAR_TENSOR_DISPATCH_APIS
285- ):
286 return 1264 return 1
287 return 2265 return 2
288 266 
@@ -294,9 +272,7 @@ def validate_config(config: Dict[str, Any]) -> List[str]:
294 # 验证 API 类型272 # 验证 API 类型
295 valid_types = API_TYPE_CHOICES273 valid_types = API_TYPE_CHOICES
296 if config.get("api_type") not in valid_types:274 if config.get("api_type") not in valid_types:
297- errors.append(275+ errors.append(f"无效的 api_type: {config.get('api_type')}, 有效值: {valid_types}")
298- f"无效的 api_type: {config.get('api_type')}, 有效值: {valid_types}"
299- )
300 276 
301 # 验证芯片架构277 # 验证芯片架构
302 valid_chips = list(CHIP_ARCH_BY_NAME.keys())278 valid_chips = list(CHIP_ARCH_BY_NAME.keys())
@@ -371,9 +347,7 @@ def get_output_path(config: UTConfig) -> str:
371 npu_arch = NPU_ARCH_MAP[config.chip]347 npu_arch = NPU_ARCH_MAP[config.chip]
372 return f"tests/api/c_api/npu_arch_{npu_arch}/vector_compute/test_{config.api_name.lower()}.cpp"348 return f"tests/api/c_api/npu_arch_{npu_arch}/vector_compute/test_{config.api_name.lower()}.cpp"
373 elif config.api_type == ApiType.ADV:349 elif config.api_type == ApiType.ADV:
374- profile_output = get_adv_profile_output_path(350+ profile_output = get_adv_profile_output_path(config.api_name, config.kernel_params)
375- config.api_name, config.kernel_params
376- )
377 if profile_output:351 if profile_output:
378 return profile_output352 return profile_output
379 return f"tests/api/adv_api/{config.api_name.lower()}/test_operator_{config.api_name.lower()}.cpp"353 return f"tests/api/adv_api/{config.api_name.lower()}/test_operator_{config.api_name.lower()}.cpp"
@@ -413,23 +387,15 @@ def main():
413 387 
414 parser.add_argument("--type", "-t", choices=API_TYPE_CHOICES, help="API 类型")388 parser.add_argument("--type", "-t", choices=API_TYPE_CHOICES, help="API 类型")
415 389 
416- parser.add_argument(390+ parser.add_argument("--api", "-a", type=str, help="API 名称 (如: Add, Mmad, asc_add)")
417- "--api", "-a", type=str, help="API 名称 (如: Add, Mmad, asc_add)"
418- )
419 391 
420- parser.add_argument(392+ parser.add_argument("--chip", choices=list(CHIP_ARCH_BY_NAME.keys()), help="目标芯片架构")
421- "--chip", choices=list(CHIP_ARCH_BY_NAME.keys()), help="目标芯片架构"
422- )
423 393 
424 parser.add_argument("--output", "-o", type=str, default=None, help="输出文件路径")394 parser.add_argument("--output", "-o", type=str, default=None, help="输出文件路径")
425 395 
426- parser.add_argument(396+ parser.add_argument("--output-dir", type=str, default=None, help="输出目录(自动生成文件名)")
427- "--output-dir", type=str, default=None, help="输出目录(自动生成文件名)"
428- )
429 397 
430- parser.add_argument(398+ parser.add_argument("--data-size", "-d", type=int, default=256, help="测试数据大小 (默认: 256)")
431- "--data-size", "-d", type=int, default=256, help="测试数据大小 (默认: 256)"
432- )
433 399 
434 parser.add_argument(400 parser.add_argument(
435 "--dtype",401 "--dtype",
@@ -439,53 +405,25 @@ def main():
439 help="通用模板可直接初始化的数据类型 (默认: half)",405 help="通用模板可直接初始化的数据类型 (默认: half)",
440 )406 )
441 407 
442- parser.add_argument(408+ parser.add_argument("--test-count", type=int, default=3, help="生成测试用例数量 (默认: 3)")
443- "--test-count", type=int, default=3, help="生成测试用例数量 (默认: 3)"409+ 
444- )410+ parser.add_argument("--template-config", action="store_true", help="输出配置文件模板并退出")
411+ 
412+ parser.add_argument("--list-supported", action="store_true", help="列出支持的配置选项")
413+ 
414+ parser.add_argument("--validate", action="store_true", help="仅验证配置,不生成代码")
445 415 
446 parser.add_argument(416 parser.add_argument(
447- "--template-config", action="store_true", help="输出配置文件模板并退出"417+ "--coverage-report", type=str, default=None, help="当前覆盖率报告 HTML 路径,用于报告 Lines / Functions 覆盖率"
448 )418 )
449 419 
450- parser.add_argument(420+ parser.add_argument("--prompt-tokens", type=int, default=None, help="外部调用环境记录的 prompt token 数")
451- "--list-supported", action="store_true", help="列出支持的配置选项"
452- )
453 421 
454- parser.add_argument(422+ parser.add_argument("--completion-tokens", type=int, default=None, help="外部调用环境记录的 completion token 数")
455- "--validate", action="store_true", help="仅验证配置,不生成代码"
456- )
457 423 
458- parser.add_argument(424+ parser.add_argument("--total-tokens", type=int, default=None, help="外部调用环境记录的 total token 数")
459- "--coverage-report",
460- type=str,
461- default=None,
462- help="当前覆盖率报告 HTML 路径,用于报告 Lines / Functions 覆盖率",
463- )
464 425 
465- parser.add_argument(426+ parser.add_argument("--report-json", type=str, default=None, help="将生成报告写入指定 JSON 文件")
466- "--prompt-tokens",
467- type=int,
468- default=None,
469- help="外部调用环境记录的 prompt token 数",
470- )
471- 
472- parser.add_argument(
473- "--completion-tokens",
474- type=int,
475- default=None,
476- help="外部调用环境记录的 completion token 数",
477- )
478- 
479- parser.add_argument(
480- "--total-tokens",
481- type=int,
482- default=None,
483- help="外部调用环境记录的 total token 数",
484- )
485- 
486- parser.add_argument(
487- "--report-json", type=str, default=None, help="将生成报告写入指定 JSON 文件"
488- )
489 427 
490 args = parser.parse_args()428 args = parser.parse_args()
491 429 
@@ -518,14 +456,8 @@ def main():
518 456 
519 write_line("\n=== 文档内置数据类型 ===")457 write_line("\n=== 文档内置数据类型 ===")
520 for dtype, info in DTYPE_MAP.items():458 for dtype, info in DTYPE_MAP.items():
521- init = (459+ init = "generic-init" if info.get("generic_ut_generation", False) else "api-specific-init"
522- "generic-init"460+ write_line(f" - {dtype}: size={info['size']}, bits={info.get('bit_width', 'unknown')}, {init}")
523- if info.get("generic_ut_generation", False)
524- else "api-specific-init"
525- )
526- write_line(
527- f" - {dtype}: size={info['size']}, bits={info.get('bit_width', 'unknown')}, {init}"
528- )
529 461 
530 write_line("\n=== 通用 UT 生成可直接初始化的数据类型 ===")462 write_line("\n=== 通用 UT 生成可直接初始化的数据类型 ===")
531 for dtype, info in GENERATOR_DTYPE_MAP.items():463 for dtype, info in GENERATOR_DTYPE_MAP.items():
@@ -29,28 +29,14 @@ SUPPORTED_PATTERNS = (
29 ("printf", re.compile(r"\b(?:AscendC::)?(?:printf|PRINTF)\s*\(")),29 ("printf", re.compile(r"\b(?:AscendC::)?(?:printf|PRINTF)\s*\(")),
30 ("assert", re.compile(r"\b(?:assert|ascendc_assert)\s*\(")),30 ("assert", re.compile(r"\b(?:assert|ascendc_assert)\s*\(")),
31 ("__trap", re.compile(r"\b__trap\s*\(")),31 ("__trap", re.compile(r"\b__trap\s*\(")),
32- (32+ ("asc_dump", re.compile(r"\basc_dump(?:_(?:gm|ubuf|l1buf|cbuf|reg))?\s*(?:<[^;{}()]*>)?\s*\(")),
33- "asc_dump",
34- re.compile(
35- r"\basc_dump(?:_(?:gm|ubuf|l1buf|cbuf|reg))?\s*(?:<[^;{}()]*>)?\s*\("
36- ),
37- ),
38 ("clock", re.compile(r"\bclock\s*\(")),33 ("clock", re.compile(r"\bclock\s*\(")),
39 ("asc_time_stamp", re.compile(r"\basc_time_stamp\s*\(")),34 ("asc_time_stamp", re.compile(r"\basc_time_stamp\s*\(")),
40)35)
41 36 
42-EXCLUDED_APIS = {37+EXCLUDED_APIS = {"asc_prof_start", "asc_prof_stop", "asc_mark_stamp", "TRACE_START", "TRACE_STOP"}
43- "asc_prof_start",
44- "asc_prof_stop",
45- "asc_mark_stamp",
46- "TRACE_START",
47- "TRACE_STOP",
48-}
49 38 
50-EXCLUDED_PATTERNS = {39+EXCLUDED_PATTERNS = {api: re.compile(r"\b" + re.escape(api) + r"(?:\s*<[^;{}()]*>)?\s*\(") for api in EXCLUDED_APIS}
51- api: re.compile(r"\b" + re.escape(api) + r"(?:\s*<[^;{}()]*>)?\s*\(")
52- for api in EXCLUDED_APIS
53-}
54 40 
55REQUIRED_HEADERS = {41REQUIRED_HEADERS = {
56 "printf": "utils/debug/asc_printf.h",42 "printf": "utils/debug/asc_printf.h",
@@ -63,11 +49,7 @@ REQUIRED_HEADERS = {
63 49 
64 50 
65def _line_numbers(text: str, pattern: re.Pattern[str]) -> List[int]:51def _line_numbers(text: str, pattern: re.Pattern[str]) -> List[int]:
66- return [52+ return [index for index, line in enumerate(text.splitlines(), start=1) if pattern.search(line)]
67- index
68- for index, line in enumerate(text.splitlines(), start=1)
69- if pattern.search(line)
70- ]
71 53 
72 54 
73def scan_text(text: str, filename: str = "<memory>") -> Dict[str, object]:55def scan_text(text: str, filename: str = "<memory>") -> Dict[str, object]:
@@ -95,11 +77,7 @@ def scan_text(text: str, filename: str = "<memory>") -> Dict[str, object]:
95 missing_headers = (77 missing_headers = (
96 {}78 {}
97 if "kernel_operator.h" in headers79 if "kernel_operator.h" in headers
98- else {80+ else {api: REQUIRED_HEADERS[api] for api in supported if REQUIRED_HEADERS[api] not in headers}
99- api: REQUIRED_HEADERS[api]
100- for api in supported
101- if REQUIRED_HEADERS[api] not in headers
102- }
103 )81 )
104 82 
105 return {83 return {
@@ -131,25 +109,15 @@ def _merge_reports(reports: Iterable[Dict[str, object]]) -> Dict[str, object]:
131 for api in report["excluded"]: # type: ignore[index]109 for api in report["excluded"]: # type: ignore[index]
132 if api not in excluded:110 if api not in excluded:
133 excluded.append(api)111 excluded.append(api)
134- return {112+ return {"files": report_list, "supported": supported, "excluded": sorted(excluded)}
135- "files": report_list,
136- "supported": supported,
137- "excluded": sorted(excluded),
138- }
139 113 
140 114 
141def main(argv: List[str] | None = None) -> int:115def main(argv: List[str] | None = None) -> int:
142 parser = argparse.ArgumentParser(description=__doc__)116 parser = argparse.ArgumentParser(description=__doc__)
117+ parser.add_argument("paths", nargs="*", type=Path, help="AscendC source files to scan")
118+ parser.add_argument("--json", action="store_true", help="write a machine-readable JSON report")
143 parser.add_argument(119 parser.add_argument(
144- "paths", nargs="*", type=Path, help="AscendC source files to scan"120+ "--fail-on-excluded", action="store_true", help="return 1 when an excluded profiling API is found"
145- )
146- parser.add_argument(
147- "--json", action="store_true", help="write a machine-readable JSON report"
148- )
149- parser.add_argument(
150- "--fail-on-excluded",
151- action="store_true",
152- help="return 1 when an excluded profiling API is found",
153 )121 )
154 args = parser.parse_args(argv)122 args = parser.parse_args(argv)
155 123 
@@ -41,10 +41,7 @@ class ScanDebugApisTest(unittest.TestCase):
41 41 
42 result = scan_text(source, "kernel.asc")42 result = scan_text(source, "kernel.asc")
43 43 
44- self.assertEqual(44+ self.assertEqual(result["supported"], ["printf", "assert", "__trap", "asc_dump", "clock", "asc_time_stamp"])
45- result["supported"],
46- ["printf", "assert", "__trap", "asc_dump", "clock", "asc_time_stamp"],
47- )
48 self.assertEqual(result["excluded"], [])45 self.assertEqual(result["excluded"], [])
49 self.assertEqual(result["files"], ["kernel.asc"])46 self.assertEqual(result["files"], ["kernel.asc"])
50 47 
@@ -55,8 +52,7 @@ class ScanDebugApisTest(unittest.TestCase):
55 52 
56 self.assertEqual(result["supported"], [])53 self.assertEqual(result["supported"], [])
57 self.assertEqual(54 self.assertEqual(
58- result["excluded"],55+ result["excluded"], sorted(EXCLUDED_APIS & {"asc_prof_start", "TRACE_START", "asc_mark_stamp"})
59- sorted(EXCLUDED_APIS & {"asc_prof_start", "TRACE_START", "asc_mark_stamp"}),
60 )56 )
61 57 
62 def test_accepts_kernel_operator_as_umbrella_header(self):58 def test_accepts_kernel_operator_as_umbrella_header(self):
@@ -81,9 +77,7 @@ class ScanDebugApisTest(unittest.TestCase):
81 "--fail-on-excluded",77 "--fail-on-excluded",
82 str(source_path),78 str(source_path),
83 ]79 ]
84- completed = subprocess.run(80+ completed = subprocess.run(command, capture_output=True, text=True, check=False)
85- command, capture_output=True, text=True, check=False
86- )
87 81 
88 self.assertEqual(completed.returncode, 1)82 self.assertEqual(completed.returncode, 1)
89 report = json.loads(completed.stdout)83 report = json.loads(completed.stdout)
@@ -89,9 +89,7 @@ class BinParamBuilder(opdesc_parser.OpDesc):
89 for fmt_out in self.output_fmt:89 for fmt_out in self.output_fmt:
90 format_list.append(fmt_out.split(","))90 format_list.append(fmt_out.split(","))
91 91 
92- dtype_for_bin_list = [92+ dtype_for_bin_list = [[] for _ in range(len(self.input_dtype) + len(self.output_dtype))]
93- [] for _ in range(len(self.input_dtype) + len(self.output_dtype))
94- ]
95 format_for_bin_list = copy.deepcopy(dtype_for_bin_list)93 format_for_bin_list = copy.deepcopy(dtype_for_bin_list)
96 94 
97 for key, value in self.input_dtype_for_bin.items():95 for key, value in self.input_dtype_for_bin.items():
@@ -103,9 +101,7 @@ class BinParamBuilder(opdesc_parser.OpDesc):
103 for key, value in self.output_fmt_for_bin.items():101 for key, value in self.output_fmt_for_bin.items():
104 format_for_bin_list[key + len(self.input_dtype)] = value.split(",")102 format_for_bin_list[key + len(self.input_dtype)] = value.split(",")
105 103 
106- return ParamInfo(104+ return ParamInfo(dtype_list, format_list, dtype_for_bin_list, format_for_bin_list)
107- dtype_list, format_list, dtype_for_bin_list, format_for_bin_list
108- )
109 105 
110 def gen_bin_cprs_list(self: any, param_info: ParamInfo):106 def gen_bin_cprs_list(self: any, param_info: ParamInfo):
111 combine_dict = {}107 combine_dict = {}
@@ -162,46 +158,26 @@ class BinParamBuilder(opdesc_parser.OpDesc):
162 head_idx = self.bin_cprs_head[index]158 head_idx = self.bin_cprs_head[index]
163 for cmb_idx in sub_list:159 for cmb_idx in sub_list:
164 for i in range(0, input_size):160 for i in range(0, input_size):
165- self.input_dtype_for_bin_list[i][head_idx].append(161+ self.input_dtype_for_bin_list[i][head_idx].append(param_info.dtype_list[i][cmb_idx])
166- param_info.dtype_list[i][cmb_idx]162+ self.input_fmt_for_bin_list[i][head_idx].append(param_info.format_list[i][cmb_idx])
167- )
168- self.input_fmt_for_bin_list[i][head_idx].append(
169- param_info.format_list[i][cmb_idx]
170- )
171 for i in range(0, output_size):163 for i in range(0, output_size):
172- self.output_dtype_for_bin_list[i][head_idx].append(164+ self.output_dtype_for_bin_list[i][head_idx].append(param_info.dtype_list[i + input_size][cmb_idx])
173- param_info.dtype_list[i + input_size][cmb_idx]165+ self.output_fmt_for_bin_list[i][head_idx].append(param_info.format_list[i + input_size][cmb_idx])
174- )
175- self.output_fmt_for_bin_list[i][head_idx].append(
176- param_info.format_list[i + input_size][cmb_idx]
177- )
178 166 
179 def rm_cprs_cmb(self: any, dtype_list, format_list, input_size, output_size):167 def rm_cprs_cmb(self: any, dtype_list, format_list, input_size, output_size):
180 for i in range(0, input_size):168 for i in range(0, input_size):
181 self.input_dtype_for_bin_list[i] = [169 self.input_dtype_for_bin_list[i] = [
182- element170+ element for index, element in enumerate(self.input_dtype_for_bin_list[i]) if index in self.bin_save_list
183- for index, element in enumerate(self.input_dtype_for_bin_list[i])
184- if index in self.bin_save_list
185 ]171 ]
186 self.input_fmt_for_bin_list[i] = [172 self.input_fmt_for_bin_list[i] = [
187- element173+ element for index, element in enumerate(self.input_fmt_for_bin_list[i]) if index in self.bin_save_list
188- for index, element in enumerate(self.input_fmt_for_bin_list[i])
189- if index in self.bin_save_list
190- ]
191- new_dtype_list = [
192- element
193- for index, element in enumerate(dtype_list[i])
194- if index in self.bin_save_list
195 ]174 ]
175+ new_dtype_list = [element for index, element in enumerate(dtype_list[i]) if index in self.bin_save_list]
196 new_dtype_str = ""176 new_dtype_str = ""
197 for dtype in new_dtype_list:177 for dtype in new_dtype_list:
198 new_dtype_str += f"{dtype},"178 new_dtype_str += f"{dtype},"
199 self.input_dtype[i] = new_dtype_str[:-1]179 self.input_dtype[i] = new_dtype_str[:-1]
200- new_format_list = [180+ new_format_list = [element for index, element in enumerate(format_list[i]) if index in self.bin_save_list]
201- element
202- for index, element in enumerate(format_list[i])
203- if index in self.bin_save_list
204- ]
205 new_format_str = ""181 new_format_str = ""
206 for fmt in new_format_list:182 for fmt in new_format_list:
207 new_format_str += f"{fmt},"183 new_format_str += f"{fmt},"
@@ -213,23 +189,17 @@ class BinParamBuilder(opdesc_parser.OpDesc):
213 if index in self.bin_save_list189 if index in self.bin_save_list
214 ]190 ]
215 self.output_fmt_for_bin_list[i] = [191 self.output_fmt_for_bin_list[i] = [
216- element192+ element for index, element in enumerate(self.output_fmt_for_bin_list[i]) if index in self.bin_save_list
217- for index, element in enumerate(self.output_fmt_for_bin_list[i])
218- if index in self.bin_save_list
219 ]193 ]
220 new_dtype_list = [194 new_dtype_list = [
221- element195+ element for index, element in enumerate(dtype_list[i + input_size]) if index in self.bin_save_list
222- for index, element in enumerate(dtype_list[i + input_size])
223- if index in self.bin_save_list
224 ]196 ]
225 new_dtype_str = ""197 new_dtype_str = ""
226 for dtype in new_dtype_list:198 for dtype in new_dtype_list:
227 new_dtype_str += f"{dtype},"199 new_dtype_str += f"{dtype},"
228 self.output_dtype[i] = new_dtype_str[:-1]200 self.output_dtype[i] = new_dtype_str[:-1]
229 new_format_list = [201 new_format_list = [
230- element202+ element for index, element in enumerate(format_list[i + input_size]) if index in self.bin_save_list
231- for index, element in enumerate(format_list[i + input_size])
232- if index in self.bin_save_list
233 ]203 ]
234 new_format_str = ""204 new_format_str = ""
235 for fmt in new_format_list:205 for fmt in new_format_list:
@@ -238,12 +208,7 @@ class BinParamBuilder(opdesc_parser.OpDesc):
238 208 
239 def is_set_for_bin_query(self: any):209 def is_set_for_bin_query(self: any):
240 return any(210 return any(
241- [211+ [self.input_dtype_for_bin, self.output_dtype_for_bin, self.input_fmt_for_bin, self.output_fmt_for_bin]
242- self.input_dtype_for_bin,
243- self.output_dtype_for_bin,
244- self.input_fmt_for_bin,
245- self.output_fmt_for_bin,
246- ]
247 )212 )
248 213 
249 def for_bin_list_match(self: any):214 def for_bin_list_match(self: any):
@@ -255,13 +220,9 @@ class BinParamBuilder(opdesc_parser.OpDesc):
255 self.gen_bin_cprs_list(param_info)220 self.gen_bin_cprs_list(param_info)
256 self.gen_for_bin_list(param_info)221 self.gen_for_bin_list(param_info)
257 if len(self.bin_save_list) == len(self.input_dtype[0].split(",")):222 if len(self.bin_save_list) == len(self.input_dtype[0].split(",")):
258- print(223+ print("WARNING: ForBinQuery can not compress number of bin file with this set, please check!!.")
259- "WARNING: ForBinQuery can not compress number of bin file with this set, please check!!."
260- )
261 return224 return
262- self.rm_cprs_cmb(225+ self.rm_cprs_cmb(param_info.dtype_list, param_info.format_list, input_size, output_size)
263- param_info.dtype_list, param_info.format_list, input_size, output_size
264- )
265 226 
266 def gen_input_json_with_super_config(self: any, param_file: str):227 def gen_input_json_with_super_config(self: any, param_file: str):
267 with open(param_file, "r") as f:228 with open(param_file, "r") as f:
@@ -272,13 +233,9 @@ class BinParamBuilder(opdesc_parser.OpDesc):
272 if "bin_filename" in op_node:233 if "bin_filename" in op_node:
273 op_node["bin_filename"] += "_relocatable"234 op_node["bin_filename"] += "_relocatable"
274 else:235 else:
275- print(236+ print(f"[WARNING]The op_node in {param_file} does not have bin_filename field, please check!!!")
276- f"[WARNING]The op_node in {param_file} does not have bin_filename field, please check!!!"
277- )
278 else:237 else:
279- print(238+ print(f"[WARNING]The op_list in {param_file} is empty or not list type, please check!!!")
280- f"[WARNING]The op_list in {param_file} is empty or not list type, please check!!!"
281- )
282 239 
283 base_name = os.path.basename(param_file)240 base_name = os.path.basename(param_file)
284 name, ext = os.path.splitext(base_name)241 name, ext = os.path.splitext(base_name)
@@ -302,22 +259,12 @@ class BinParamBuilder(opdesc_parser.OpDesc):
302 index_value += 1259 index_value += 1
303 new_param_file = self.gen_input_json_with_super_config(param_file)260 new_param_file = self.gen_input_json_with_super_config(param_file)
304 bin_file += "_relocatable"261 bin_file += "_relocatable"
305- self._write_build_cmd(262+ self._write_build_cmd(new_param_file, bin_file, index_value, output_dir, True)
306- new_param_file, bin_file, index_value, output_dir, True
307- )
308 else:263 else:
309- print(264+ print("[ERROR]the generation of input_json based on specified json is failed")
310- "[ERROR]the generation of input_json based on specified json is failed"
311- )
312 265 
313- def gen_input_json_based_on_specified_json(266+ def gen_input_json_based_on_specified_json(self: any, ori_json: str, output_dir: str):
314- self: any, ori_json: str, output_dir: str267+ generated_files = split_json_files(ori_json, output_dir, expected_op_type=self.op_type)
315- ):
316- generated_files = split_json_files(
317- ori_json,
318- output_dir,
319- expected_op_type=self.op_type,
320- )
321 index_value = -1268 index_value = -1
322 if generated_files:269 if generated_files:
323 for param_file in generated_files:270 for param_file in generated_files:
@@ -329,13 +276,9 @@ class BinParamBuilder(opdesc_parser.OpDesc):
329 index_value += 1276 index_value += 1
330 new_param_file = self.gen_input_json_with_super_config(param_file)277 new_param_file = self.gen_input_json_with_super_config(param_file)
331 bin_file += "_relocatable"278 bin_file += "_relocatable"
332- self._write_build_cmd(279+ self._write_build_cmd(new_param_file, bin_file, index_value, output_dir, True)
333- new_param_file, bin_file, index_value, output_dir, True
334- )
335 else:280 else:
336- print(281+ print("[ERROR]the generation of input_json based on specified json is failed")
337- "[ERROR]the generation of input_json based on specified json is failed"
338- )
339 282 
340 def _gen_inputs_for_input_json(self: any, index, required_parameter, inputs):283 def _gen_inputs_for_input_json(self: any, index, required_parameter, inputs):
341 input_name_len = len(self.input_name)284 input_name_len = len(self.input_name)
@@ -347,10 +290,7 @@ class BinParamBuilder(opdesc_parser.OpDesc):
347 para["name"] = self.input_name[idx][:-5]290 para["name"] = self.input_name[idx][:-5]
348 para["index"] = idx291 para["index"] = idx
349 para["dtype"] = idtypes[index]292 para["dtype"] = idtypes[index]
350- if (293+ if self.is_set_for_bin_query() and self.input_dtype_for_bin_list[idx][index]:
351- self.is_set_for_bin_query()
352- and self.input_dtype_for_bin_list[idx][index]
353- ):
354 para["dtypeForBinQuery"] = self.input_dtype_for_bin_list[idx][index]294 para["dtypeForBinQuery"] = self.input_dtype_for_bin_list[idx][index]
355 para["format"] = ifmts[index]295 para["format"] = ifmts[index]
356 if self.is_set_for_bin_query() and self.input_fmt_for_bin_list[idx][index]:296 if self.is_set_for_bin_query() and self.input_fmt_for_bin_list[idx][index]:
@@ -379,10 +319,7 @@ class BinParamBuilder(opdesc_parser.OpDesc):
379 para["name"] = self.output_name[idx][:-5]319 para["name"] = self.output_name[idx][:-5]
380 para["index"] = idx320 para["index"] = idx
381 para["dtype"] = odtypes[index]321 para["dtype"] = odtypes[index]
382- if (322+ if self.is_set_for_bin_query() and self.output_dtype_for_bin_list[idx][index]:
383- self.is_set_for_bin_query()
384- and self.output_dtype_for_bin_list[idx][index]
385- ):
386 para["dtypeForBinQuery"] = self.output_dtype_for_bin_list[idx][index]323 para["dtypeForBinQuery"] = self.output_dtype_for_bin_list[idx][index]
387 para["format"] = ofmts[index]324 para["format"] = ofmts[index]
388 if self.is_set_for_bin_query() and self.output_fmt_for_bin_list[idx][index]:325 if self.is_set_for_bin_query() and self.output_fmt_for_bin_list[idx][index]:
@@ -420,9 +357,7 @@ class BinParamBuilder(opdesc_parser.OpDesc):
420 else:357 else:
421 count = len(self.input_dtype[0].split(","))358 count = len(self.input_dtype[0].split(","))
422 if count == 0:359 if count == 0:
423- raise RuntimeError(360+ raise RuntimeError(f"Op {self.op_type} must have at least one input or output")
424- f"Op {self.op_type} must have at least one input or output"
425- )
426 required_parameters = set()361 required_parameters = set()
427 index_value = -1362 index_value = -1
428 363 
@@ -473,14 +408,10 @@ class BinParamBuilder(opdesc_parser.OpDesc):
473 param_file = os.path.realpath(param_file)408 param_file = os.path.realpath(param_file)
474 self._write_build_json(param_file, param)409 self._write_build_json(param_file, param)
475 index_value += 1410 index_value += 1
476- self._write_build_cmd(411+ self._write_build_cmd(param_file, bin_file, index_value, auto_gen_path, True)
477- param_file, bin_file, index_value, auto_gen_path, True
478- )
479 412 
480 def _write_build_json(self: any, param_file: str, param):413 def _write_build_json(self: any, param_file: str, param):
481- with os.fdopen(414+ with os.fdopen(os.open(param_file, const_var.WFLAGS, const_var.WMODES), "w") as fd:
482- os.open(param_file, const_var.WFLAGS, const_var.WMODES), "w"
483- ) as fd:
484 json.dump(param, fd, indent=" ")415 json.dump(param, fd, indent=" ")
485 416 
486 def _generate_check_result(self: any, enable_tiling_keys: bool, bin_file: str):417 def _generate_check_result(self: any, enable_tiling_keys: bool, bin_file: str):
@@ -505,14 +436,7 @@ grep -q "None of the given tiling keys are in the supported list"; then\n'
505 check_result += "fi\n"436 check_result += "fi\n"
506 return check_result437 return check_result
507 438 
508- def _write_build_cmd(439+ def _write_build_cmd(self: any, param_file: str, bin_file: str, index: int, auto_gen_path: str, super_mode=False):
509- self: any,
510- param_file: str,
511- bin_file: str,
512- index: int,
513- auto_gen_path: str,
514- super_mode=False,
515- ):
516 hard_soc = const_var.conv_soc_ver(self.soc)440 hard_soc = const_var.conv_soc_ver(self.soc)
517 if not hard_soc:441 if not hard_soc:
518 hard_soc = self.soc.capitalize()442 hard_soc = self.soc.capitalize()
@@ -523,10 +447,8 @@ grep -q "None of the given tiling keys are in the supported list"; then\n'
523 compile_file = os.path.join(self.out_path, "-".join(name_com) + ".sh")447 compile_file = os.path.join(self.out_path, "-".join(name_com) + ".sh")
524 compile_file = os.path.realpath(compile_file)448 compile_file = os.path.realpath(compile_file)
525 449 
526- bin_cmd_str = (450+ bin_cmd_str = "res=$(asc_opc $1 --main_func={fun} --input_param={param} --soc_version={soc} \
527- "res=$(asc_opc $1 --main_func={fun} --input_param={param} --soc_version={soc} \
528 --output=$2 --impl_mode={impl} --op_mode=dynamic "451 --output=$2 --impl_mode={impl} --op_mode=dynamic "
529- )
530 452 
531 build_cmd_var = "#!/bin/bash\n"453 build_cmd_var = "#!/bin/bash\n"
532 build_cmd_var += f'echo "[{self.soc}] Generating {bin_file} ..."\n'454 build_cmd_var += f'echo "[{self.soc}] Generating {bin_file} ..."\n'
@@ -540,10 +462,7 @@ grep -q "None of the given tiling keys are in the supported list"; then\n'
540 if hard_soc == "Ascend610Lite":462 if hard_soc == "Ascend610Lite":
541 build_cmd_var += f"export ASCEND_CUSTOM_OPP_PATH={auto_gen_path}:$ASCEND_CUSTOM_OPP_PATH \n"463 build_cmd_var += f"export ASCEND_CUSTOM_OPP_PATH={auto_gen_path}:$ASCEND_CUSTOM_OPP_PATH \n"
542 build_cmd_var += bin_cmd_str.format(464 build_cmd_var += bin_cmd_str.format(
543- fun=self.op_intf,465+ fun=self.op_intf, soc=hard_soc, param=param_file, impl="high_performance,optional"
544- soc=hard_soc,
545- param=param_file,
546- impl="high_performance,optional",
547 )466 )
548 build_cmd_var += f" --simplified_key_mode={self.simplified_key_mode}"467 build_cmd_var += f" --simplified_key_mode={self.simplified_key_mode}"
549 468 
@@ -559,9 +478,7 @@ grep -q "None of the given tiling keys are in the supported list"; then\n'
559 build_cmd_var += f" --op_debug_config={op_debug_str}"478 build_cmd_var += f" --op_debug_config={op_debug_str}"
560 479 
561 if super_mode and self.op_super_config:480 if super_mode and self.op_super_config:
562- op_super_config_str = " ".join(481+ op_super_config_str = " ".join([str(_key) for _key in list(self.op_super_config)])
563- [str(_key) for _key in list(self.op_super_config)]
564- )
565 build_cmd_var += f" {op_super_config_str}"482 build_cmd_var += f" {op_super_config_str}"
566 483 
567 if self.kernel_template_input:484 if self.kernel_template_input:
@@ -575,9 +492,7 @@ grep -q "None of the given tiling keys are in the supported list"; then\n'
575 build_cmd_var += check_result492 build_cmd_var += check_result
576 build_cmd_var += f'echo "[{self.soc}] Generating {bin_file} Done"\n'493 build_cmd_var += f'echo "[{self.soc}] Generating {bin_file} Done"\n'
577 494 
578- with os.fdopen(495+ with os.fdopen(os.open(compile_file, const_var.WFLAGS, const_var.WMODES), "w") as fd:
579- os.open(compile_file, const_var.WFLAGS, const_var.WMODES), "w"
580- ) as fd:
581 fd.write(build_cmd_var)496 fd.write(build_cmd_var)
582 497 
583 498 
@@ -689,9 +604,7 @@ def _process_opc_options(
689 _process_op_debug_config_option(op_type, options, op_debug_config)604 _process_op_debug_config_option(op_type, options, op_debug_config)
690 _process_kernel_json_file_option(op_type, options, kernel_json_file)605 _process_kernel_json_file_option(op_type, options, kernel_json_file)
691 _process_input_param_file_option(op_type, options, input_param_file)606 _process_input_param_file_option(op_type, options, input_param_file)
692- _process_kernel_template_input_option(607+ _process_kernel_template_input_option(op_type, options, kernel_template_input_info)
693- op_type, options, kernel_template_input_info
694- )
695 _process_simplified_key_mode_option(op_type, options, simplified_key_mode_info)608 _process_simplified_key_mode_option(op_type, options, simplified_key_mode_info)
696 609 
697 610 
@@ -751,25 +664,19 @@ def parse_op_debug_config(opc_config_file: str, soc: str) -> OpDebugConfig:
751def gen_option_config(debug_config, super_config, op_debug_config):664def gen_option_config(debug_config, super_config, op_debug_config):
752 for _op_type, _op_option in op_debug_config.items():665 for _op_type, _op_option in op_debug_config.items():
753 for _option in _op_option:666 for _option in _op_option:
754- if _option.startswith("--op_relocatable_kernel_binary") and (667+ if _option.startswith("--op_relocatable_kernel_binary") and ("false" in _option or "False" in _option):
755- "false" in _option or "False" in _option
756- ):
757 continue668 continue
758- elif _option.startswith(669+ elif _option.startswith("--op_relocatable_kernel_binary") or _option.startswith(
759- "--op_relocatable_kernel_binary"670+ "--op_super_kernel_options"
760- ) or _option.startswith("--op_super_kernel_options"):671+ ):
761 super_config[_op_type].add(_option)672 super_config[_op_type].add(_option)
762 else:673 else:
763 debug_config[_op_type].add(_option)674 debug_config[_op_type].add(_option)
764 675 
765 676 
766-def gen_bin_param_file(677+def gen_bin_param_file(cfgfile: str, out_dir: str, soc: str, opc_config_file: str = "", ops: list = None):
767- cfgfile: str, out_dir: str, soc: str, opc_config_file: str = "", ops: list = None
768-):
769 if not os.path.exists(cfgfile):678 if not os.path.exists(cfgfile):
770- print(679+ print(f"INFO: {cfgfile} does not exists in this project, skip generating compile commands.")
771- f"INFO: {cfgfile} does not exists in this project, skip generating compile commands."
772- )
773 return680 return
774 681 
775 debug_config = defaultdict(set)682 debug_config = defaultdict(set)
@@ -816,9 +723,7 @@ def gen_bin_param_file(
816 if all_soc_key in kernel_template_input_info:723 if all_soc_key in kernel_template_input_info:
817 op_desc.set_kernel_template_input(kernel_template_input_info[all_soc_key])724 op_desc.set_kernel_template_input(kernel_template_input_info[all_soc_key])
818 if op_desc.op_type in kernel_template_input_info:725 if op_desc.op_type in kernel_template_input_info:
819- op_desc.set_kernel_template_input(726+ op_desc.set_kernel_template_input(kernel_template_input_info[op_desc.op_type])
820- kernel_template_input_info[op_desc.op_type]
821- )
822 727 
823 key_params = ""728 key_params = ""
824 if op_desc.op_type in input_param_file:729 if op_desc.op_type in input_param_file:
@@ -844,6 +749,4 @@ if __name__ == "__main__":
844 args = parse_args(sys.argv)749 args = parse_args(sys.argv)
845 if len(args.argv) <= 3:750 if len(args.argv) <= 3:
846 raise RuntimeError("arguments must greater than 3")751 raise RuntimeError("arguments must greater than 3")
847- gen_bin_param_file(752+ gen_bin_param_file(args.argv[1], args.argv[2], args.argv[3], opc_config_file=args.opc_config_file)
848- args.argv[1], args.argv[2], args.argv[3], opc_config_file=args.opc_config_file
849- )
@@ -16,21 +16,13 @@ import os
16import glob16import glob
17import json17import json
18import sys18import sys
19-from asc_op_compile_base.asc_op_compiler.op_tiling import (19+from asc_op_compile_base.asc_op_compiler.op_tiling import _ASCEND_OPP_PATH_ENV, _ASCEND_OPP_PATH_DEFAULT, op_impl_path
20- _ASCEND_OPP_PATH_ENV,
21- _ASCEND_OPP_PATH_DEFAULT,
22- op_impl_path,
23-)
24from asc_op_compile_base.common.utils.log_utils import LogUtil, AscendCLogLevel20from asc_op_compile_base.common.utils.log_utils import LogUtil, AscendCLogLevel
25 21 
26 22 
27def check_optype_duplicate(args, ini_optypes):23def check_optype_duplicate(args, ini_optypes):
28 opp_path = os.environ.get(_ASCEND_OPP_PATH_ENV, _ASCEND_OPP_PATH_DEFAULT)24 opp_path = os.environ.get(_ASCEND_OPP_PATH_ENV, _ASCEND_OPP_PATH_DEFAULT)
29- json_path_base = os.path.join(25+ json_path_base = os.path.join(opp_path, op_impl_path, os.path.join("ai_core", "tbe", "config", args.soc_version))
30- opp_path,
31- op_impl_path,
32- os.path.join("ai_core", "tbe", "config", args.soc_version),
33- )
34 LogUtil.print_compile_log(26 LogUtil.print_compile_log(
35 "check_optype_duplicate",27 "check_optype_duplicate",
36 f"json path base is {json_path_base}",28 f"json path base is {json_path_base}",
@@ -42,10 +34,7 @@ def check_optype_duplicate(args, ini_optypes):
42 34 
43 if len(json_files) == 0:35 if len(json_files) == 0:
44 LogUtil.print_compile_log(36 LogUtil.print_compile_log(
45- "check_optype_duplicate",37+ "check_optype_duplicate", "no json files found", AscendCLogLevel.LOG_DEBUG, LogUtil.Option.NON_SOC
46- "no json files found",
47- AscendCLogLevel.LOG_DEBUG,
48- LogUtil.Option.NON_SOC,
49 )38 )
50 return 039 return 0
51 40 
@@ -61,10 +50,7 @@ def check_optype_duplicate(args, ini_optypes):
61 50 
62 intersection_ops = list(set(ini_optypes) & set(optypes))51 intersection_ops = list(set(ini_optypes) & set(optypes))
63 LogUtil.print_compile_log(52 LogUtil.print_compile_log(
64- "check_optype_duplicate",53+ "check_optype_duplicate", f"custom optypes {ini_optypes}", AscendCLogLevel.LOG_DEBUG, LogUtil.Option.NON_SOC
65- f"custom optypes {ini_optypes}",
66- AscendCLogLevel.LOG_DEBUG,
67- LogUtil.Option.NON_SOC,
68 )54 )
69 LogUtil.print_compile_log(55 LogUtil.print_compile_log(
70 "check_optype_duplicate",56 "check_optype_duplicate",
@@ -82,10 +68,7 @@ def check_optype_duplicate(args, ini_optypes):
82def get_optypes(args):68def get_optypes(args):
83 op_config = configparser.ConfigParser()69 op_config = configparser.ConfigParser()
84 LogUtil.print_compile_log(70 LogUtil.print_compile_log(
85- "check_optype_duplicate",71+ "check_optype_duplicate", f"ini file: {args.ini_file}", AscendCLogLevel.LOG_DEBUG, LogUtil.Option.NON_SOC
86- f"ini file: {args.ini_file}",
87- AscendCLogLevel.LOG_DEBUG,
88- LogUtil.Option.NON_SOC,
89 )72 )
90 73 
91 op_config.read(args.ini_file)74 op_config.read(args.ini_file)
@@ -37,17 +37,12 @@ class CompileKernel:
37 self.compile_sh = []37 self.compile_sh = []
38 self.target_name = args.target_name38 self.target_name = args.target_name
39 self.auto_gen_path = args.auto_gen_path39 self.auto_gen_path = args.auto_gen_path
40- self.source_ini_file = os.path.join(40+ self.source_ini_file = os.path.join(self.auto_gen_path, self.target_name + "_custom_source_files.ini")
41- self.auto_gen_path, self.target_name + "_custom_source_files.ini"
42- )
43 41 
44 if os.path.exists(self.source_ini_file):42 if os.path.exists(self.source_ini_file):
45 self.get_op_cpp_file()43 self.get_op_cpp_file()
46 44 
47- self.working_dir = os.path.join(45+ self.working_dir = os.path.join(os.getcwd(), self.op_type + "_" + self.op_soc_ver)
48- os.getcwd(),
49- self.op_type + "_" + self.op_soc_ver,
50- )
51 self.build_opp_path = os.path.join(self.working_dir, "customize")46 self.build_opp_path = os.path.join(self.working_dir, "customize")
52 os.makedirs(self.working_dir, exist_ok=True)47 os.makedirs(self.working_dir, exist_ok=True)
53 os.makedirs(self.op_output, exist_ok=True)48 os.makedirs(self.op_output, exist_ok=True)
@@ -71,16 +66,11 @@ class CompileKernel:
71 find_type = True66 find_type = True
72 67 
73 if src_ini_config.has_option(sub_sections, "compute_unit"):68 if src_ini_config.has_option(sub_sections, "compute_unit"):
74- src_compute_units = src_ini_config.get(69+ src_compute_units = src_ini_config.get(sub_sections, "compute_unit").split(",")
75- sub_sections, "compute_unit"
76- ).split(",")
77 for sub_unit in src_compute_units:70 for sub_unit in src_compute_units:
78 src_compute_units_low.append(_trans_soc_ver_to_short(sub_unit))71 src_compute_units_low.append(_trans_soc_ver_to_short(sub_unit))
79 72 
80- if (73+ if len(src_compute_units_low) == 0 or self.op_soc_ver in src_compute_units_low:
81- len(src_compute_units_low) == 0
82- or self.op_soc_ver in src_compute_units_low
83- ):
84 if src_ini_config.has_option(sub_sections, "kernel_dir"):74 if src_ini_config.has_option(sub_sections, "kernel_dir"):
85 self.op_cpp_file = os.path.realpath(75 self.op_cpp_file = os.path.realpath(
86 os.path.join(76 os.path.join(
@@ -91,10 +81,7 @@ class CompileKernel:
91 )81 )
92 else:82 else:
93 self.op_cpp_file = os.path.realpath(83 self.op_cpp_file = os.path.realpath(
94- os.path.join(84+ os.path.join(self.impl_path, src_ini_config.get(sub_sections, "kernel_file"))
95- self.impl_path,
96- src_ini_config.get(sub_sections, "kernel_file"),
97- )
98 )85 )
99 find_cpp_file = True86 find_cpp_file = True
100 break87 break
@@ -120,28 +107,19 @@ class CompileKernel:
120 os.makedirs(os.path.join(self.working_dir, "dynamic"), exist_ok=True)107 os.makedirs(os.path.join(self.working_dir, "dynamic"), exist_ok=True)
121 cfg_dir[const_var.AUTO_GEN_DIR] = os.path.dirname(self.op_cfg_ini)108 cfg_dir[const_var.AUTO_GEN_DIR] = os.path.dirname(self.op_cfg_ini)
122 ascendc_impl_build.write_scripts(109 ascendc_impl_build.write_scripts(
123- self.op_cfg_ini,110+ self.op_cfg_ini, rep_cfg, cfg_dir, [self.op_type], self.compile_options, self.source_ini_file
124- rep_cfg,
125- cfg_dir,
126- [self.op_type],
127- self.compile_options,
128- self.source_ini_file,
129 )111 )
130 py_files = glob.glob(os.path.join(self.working_dir, "dynamic", "*.py"))112 py_files = glob.glob(os.path.join(self.working_dir, "dynamic", "*.py"))
131 if py_files is None or len(py_files) != 1:113 if py_files is None or len(py_files) != 1:
132 self.clean()114 self.clean()
133 raise RuntimeError("compile py file {} generated error!".format(py_files))115 raise RuntimeError("compile py file {} generated error!".format(py_files))
134- self.op_impl_py = os.path.join(116+ self.op_impl_py = os.path.join(self.working_dir, "dynamic", self.op_type + ".py")
135- self.working_dir, "dynamic", self.op_type + ".py"
136- )
137 if self.dynamic_dir is not None:117 if self.dynamic_dir is not None:
138 shutil.copy(py_files[0], self.dynamic_dir)118 shutil.copy(py_files[0], self.dynamic_dir)
139 os.rename(py_files[0], self.op_impl_py)119 os.rename(py_files[0], self.op_impl_py)
140 if not os.path.exists(self.op_impl_py):120 if not os.path.exists(self.op_impl_py):
141 self.clean()121 self.clean()
142- raise RuntimeError(122+ raise RuntimeError("compile py file {} not generated!".format(self.op_impl_py))
143- "compile py file {} not generated!".format(self.op_impl_py)
144- )
145 123 
146 def ascendc_gen_param(self: any):124 def ascendc_gen_param(self: any):
147 bin_param_path = os.path.join(self.working_dir, "bin_param")125 bin_param_path = os.path.join(self.working_dir, "bin_param")
@@ -149,21 +127,10 @@ class CompileKernel:
149 base_dir = os.path.dirname(self.op_cfg_ini)127 base_dir = os.path.dirname(self.op_cfg_ini)
150 opc_config_file = os.path.join(base_dir, "custom_opc_options.ini")128 opc_config_file = os.path.join(base_dir, "custom_opc_options.ini")
151 ascendc_bin_param_build.gen_bin_param_file(129 ascendc_bin_param_build.gen_bin_param_file(
152- self.op_cfg_ini,130+ self.op_cfg_ini, bin_param_path, self.op_soc_ver, opc_config_file, [self.op_type]
153- bin_param_path,
154- self.op_soc_ver,
155- opc_config_file,
156- [self.op_type],
157 )131 )
158- (132+ (tiling_key_info, op_debug_config, kernel_json_file, _, kernel_template_input_info, _) = (
159- tiling_key_info,133+ ascendc_bin_param_build.parse_op_debug_config(opc_config_file, self.op_soc_ver)
160- op_debug_config,
161- kernel_json_file,
162- _,
163- kernel_template_input_info,
164- _,
165- ) = ascendc_bin_param_build.parse_op_debug_config(
166- opc_config_file, self.op_soc_ver
167 )134 )
168 if self.op_type in op_debug_config:135 if self.op_type in op_debug_config:
169 self.op_debug_config = op_debug_config[self.op_type]136 self.op_debug_config = op_debug_config[self.op_type]
@@ -179,9 +146,7 @@ class CompileKernel:
179 raise RuntimeError("compile binary shell file not generated!")146 raise RuntimeError("compile binary shell file not generated!")
180 147 
181 def ascendc_put_tiling(self: any):148 def ascendc_put_tiling(self: any):
182- tiling_path = os.path.join(149+ tiling_path = os.path.join(self.build_opp_path, "op_impl", "ai_core", "tbe", "op_tiling")
183- self.build_opp_path, "op_impl", "ai_core", "tbe", "op_tiling"
184- )
185 os.makedirs(tiling_path)150 os.makedirs(tiling_path)
186 tiling_so = os.path.join(tiling_path, "liboptiling.so")151 tiling_so = os.path.join(tiling_path, "liboptiling.so")
187 os.symlink(self.op_tiling, tiling_so)152 os.symlink(self.op_tiling, tiling_so)
@@ -191,26 +156,13 @@ class CompileKernel:
191 156 
192 def ascendc_put_json(self: any):157 def ascendc_put_json(self: any):
193 if self.json_file is not None:158 if self.json_file is not None:
194- json_file_dir = os.path.join(159+ json_file_dir = os.path.join(self.build_opp_path, "op_impl", "ai_core", "tbe", "config", self.op_soc_ver)
195- self.build_opp_path,
196- "op_impl",
197- "ai_core",
198- "tbe",
199- "config",
200- self.op_soc_ver,
201- )
202 os.makedirs(json_file_dir)160 os.makedirs(json_file_dir)
203 shutil.copy(self.json_file, json_file_dir)161 shutil.copy(self.json_file, json_file_dir)
204- build_json_file = os.path.join(162+ build_json_file = os.path.join(json_file_dir, "aic-{}-ops-info.json".format(self.op_soc_ver))
205- json_file_dir, "aic-{}-ops-info.json".format(self.op_soc_ver)
206- )
207 if not os.path.exists(build_json_file):163 if not os.path.exists(build_json_file):
208 self.clean()164 self.clean()
209- raise RuntimeError(165+ raise RuntimeError("prepare json file aic-{}-ops-info.json failed!".format(self.op_soc_ver))
210- "prepare json file aic-{}-ops-info.json failed!".format(
211- self.op_soc_ver
212- )
213- )
214 166 
215 def ascendc_build(self: any):167 def ascendc_build(self: any):
216 op_info = ascendc_op_info.OpInfo(self.op_type, self.op_cfg_ini)168 op_info = ascendc_op_info.OpInfo(self.op_type, self.op_cfg_ini)
@@ -226,11 +178,7 @@ class CompileKernel:
226 os.makedirs(build_path)178 os.makedirs(build_path)
227 all_tar.append(tar)179 all_tar.append(tar)
228 sub_cmd.append(tar + ":")180 sub_cmd.append(tar + ":")
229- sub_cmd.append(181+ sub_cmd.append("\tcd {} && bash {} --kernel-src=$(CPP) $(PY) $(OUT) $(MAKE)".format(build_path, sh))
230- "\tcd {} && bash {} --kernel-src=$(CPP) $(PY) $(OUT) $(MAKE)".format(
231- build_path, sh
232- )
233- )
234 index += 1182 index += 1
235 mkfile = os.path.join(self.working_dir, op_file + ".make")183 mkfile = os.path.join(self.working_dir, op_file + ".make")
236 with os.fdopen(os.open(mkfile, const_var.WFLAGS, const_var.WMODES), "w") as fd:184 with os.fdopen(os.open(mkfile, const_var.WFLAGS, const_var.WMODES), "w") as fd:
@@ -244,81 +192,33 @@ class CompileKernel:
244 )192 )
245 else:193 else:
246 cmd_str = "export HI_PYTHON=python3 && export ASCEND_CUSTOM_OPP_PATH={} && make -f {} PY={} OUT={} CPP={}"194 cmd_str = "export HI_PYTHON=python3 && export ASCEND_CUSTOM_OPP_PATH={} && make -f {} PY={} OUT={} CPP={}"
247- if (195+ if os.system(cmd_str.format(self.build_opp_path, mkfile, self.op_impl_py, op_bin_dir, self.op_cpp_file)) != 0:
248- os.system(
249- cmd_str.format(
250- self.build_opp_path,
251- mkfile,
252- self.op_impl_py,
253- op_bin_dir,
254- self.op_cpp_file,
255- )
256- )
257- != 0
258- ):
259 raise RuntimeError(196 raise RuntimeError(
260- "Kernel Compilation Error: OpType {} Kernel File {}!".format(197+ "Kernel Compilation Error: OpType {} Kernel File {}!".format(self.op_type, self.op_cpp_file)
261- self.op_type, self.op_cpp_file
262- )
263 )198 )
264 199 
265 200 
266def args_parse():201def args_parse():
267 parser = argparse.ArgumentParser()202 parser = argparse.ArgumentParser()
268- parser.add_argument(203+ parser.add_argument("-n", "--op-type", nargs="?", help="Op type(Camel string) to compile.")
269- "-n", "--op-type", nargs="?", help="Op type(Camel string) to compile."
270- )
271 parser.add_argument("-s", "--src-file", nargs="?", help="Op kernel source file.")204 parser.add_argument("-s", "--src-file", nargs="?", help="Op kernel source file.")
272 205 
273 parser.add_argument("-u", "--compute-unit", nargs="?", help="Compute unit.")206 parser.add_argument("-u", "--compute-unit", nargs="?", help="Compute unit.")
274- parser.add_argument(207+ parser.add_argument("-c", "--compile-options", nargs="?", help="Compile options of compiler.")
275- "-c", "--compile-options", nargs="?", help="Compile options of compiler."208+ parser.add_argument("-d", "--debug-config", nargs="?", help="Debug config of op, ref opc op-debug-config.")
276- )
277- parser.add_argument(
278- "-d",
279- "--debug-config",
280- nargs="?",
281- help="Debug config of op, ref opc op-debug-config.",
282- )
283 parser.add_argument("-i", "--config-ini", nargs="?", help="Op config ini file.")209 parser.add_argument("-i", "--config-ini", nargs="?", help="Op config ini file.")
284- parser.add_argument(210+ parser.add_argument("-t", "--tiling-lib", nargs="?", help="Tiling shared library file.")
285- "-t", "--tiling-lib", nargs="?", help="Tiling shared library file."
286- )
287 211 
212+ parser.add_argument("-o", "--output-path", nargs="?", help="Output path of compile result.")
213+ parser.add_argument("-dy", "--dynamic-dir", nargs="?", default=None, help="dynamic path of source compile.")
214+ parser.add_argument("-eb", "--enable-binary", nargs="?", default=None, help="whether binary compile is enabled.")
288 parser.add_argument(215 parser.add_argument(
289- "-o", "--output-path", nargs="?", help="Output path of compile result."216+ "-j", "--json-file", nargs="?", default=None, help="aic-<compute-unit>-ops-info.json file path."
290- )
291- parser.add_argument(
292- "-dy",
293- "--dynamic-dir",
294- nargs="?",
295- default=None,
296- help="dynamic path of source compile.",
297- )
298- parser.add_argument(
299- "-eb",
300- "--enable-binary",
301- nargs="?",
302- default=None,
303- help="whether binary compile is enabled.",
304- )
305- parser.add_argument(
306- "-j",
307- "--json-file",
308- nargs="?",
309- default=None,
310- help="aic-<compute-unit>-ops-info.json file path.",
311 )217 )
312 # $(MAKE) is necessary for parallel compiling218 # $(MAKE) is necessary for parallel compiling
313- parser.add_argument(219+ parser.add_argument("-b", "--build-tool", nargs="?", default=None, help="build tool must be make.")
314- "-b", "--build-tool", nargs="?", default=None, help="build tool must be make."220+ parser.add_argument("-tn", "--target-name", nargs="?", default=None, help="kernel target name.")
315- )221+ parser.add_argument("-a", "--auto-gen-path", nargs="?", default=None, help="auto gen file path.")
316- parser.add_argument(
317- "-tn", "--target-name", nargs="?", default=None, help="kernel target name."
318- )
319- parser.add_argument(
320- "-a", "--auto-gen-path", nargs="?", default=None, help="auto gen file path."
321- )
322 return parser.parse_args()222 return parser.parse_args()
323 223 
324 224 
@@ -22,12 +22,8 @@ def args_parse():
22 parser = argparse.ArgumentParser()22 parser = argparse.ArgumentParser()
23 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")
24 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")
25- parser.add_argument(25+ parser.add_argument("-s", "--kernel-base-dir", required=True, help="kernel base dir")
26- "-s", "--kernel-base-dir", required=True, help="kernel base dir"26+ parser.add_argument("-d", "--copy-dst-dir", required=True, help="copy kernel file dst dir")
27- )
28- parser.add_argument(
29- "-d", "--copy-dst-dir", required=True, help="copy kernel file dst dir"
30- )
31 return parser.parse_args()27 return parser.parse_args()
32 28 
33 29 
@@ -67,18 +63,12 @@ def copy_dir(src_dir, dst_dir, exclude_dir=None):
67 continue63 continue
68 for item in items + dirs:64 for item in items + dirs:
69 src_item = os.path.abspath(os.path.join(dir_path, item))65 src_item = os.path.abspath(os.path.join(dir_path, item))
70- dst_item = os.path.abspath(66+ dst_item = os.path.abspath(os.path.join(dst_dir, dir_path.replace(src_dir, "."), item))
71- os.path.join(dst_dir, dir_path.replace(src_dir, "."), item)
72- )
73 if os.path.islink(src_item):67 if os.path.islink(src_item):
74- raise ValueError(68+ raise ValueError(f"unsupport soft link file, please check it. {os.path.abspath(src_item)}")
75- f"unsupport soft link file, please check it. {os.path.abspath(src_item)}"
76- )
77 elif os.path.isdir(src_item):69 elif os.path.isdir(src_item):
78 continue70 continue
79- if not check_file_extension(71+ if not check_file_extension(src_item, ["", ".txt", ".h", ".hpp", ".cpp", ".c"]):
80- src_item, ["", ".txt", ".h", ".hpp", ".cpp", ".c"]
81- ):
82 warnings.warn(72 warnings.warn(
83 f"\n[Warning]: Unexpected file type found in {os.path.abspath(src_item)}"73 f"\n[Warning]: Unexpected file type found in {os.path.abspath(src_item)}"
84 f"\n[Warning]: Please check dir is required and correct : {os.path.abspath(dir_path)}",74 f"\n[Warning]: Please check dir is required and correct : {os.path.abspath(dir_path)}",
@@ -92,15 +82,11 @@ def copy_dir(src_dir, dst_dir, exclude_dir=None):
92def copy_file(src_file, dst_file):82def copy_file(src_file, dst_file):
93 try:83 try:
94 if not os.path.exists(src_file):84 if not os.path.exists(src_file):
95- raise FileNotFoundError(85+ raise FileNotFoundError(f"kernel src file {os.path.abspath(src_file)} not found, please check it.")
96- f"kernel src file {os.path.abspath(src_file)} not found, please check it."
97- )
98 os.makedirs(os.path.dirname(dst_file), exist_ok=True)86 os.makedirs(os.path.dirname(dst_file), exist_ok=True)
99 if (not src_file.endswith(".txt")) and (not os.path.exists(dst_file)):87 if (not src_file.endswith(".txt")) and (not os.path.exists(dst_file)):
100 if os.path.islink(src_file):88 if os.path.islink(src_file):
101- raise ValueError(89+ raise ValueError(f"unsupport soft link file, please check it. {os.path.abspath(src_file)}")
102- f"unsupport soft link file, please check it. {os.path.abspath(src_file)}"
103- )
104 elif os.path.isfile(src_file):90 elif os.path.isfile(src_file):
105 shutil.copy2(src_file, dst_file)91 shutil.copy2(src_file, dst_file)
106 else:92 else:
@@ -139,21 +125,13 @@ def is_safe_relative_path(path: str) -> bool:
139 125 
140 126 
141def copy_kernel_src_file(args):127def copy_kernel_src_file(args):
142- kernel_source_ini_file_path = os.path.join(128+ kernel_source_ini_file_path = os.path.join(args.auto_gen_path, args.target_name + "_custom_source_files.ini")
143- args.auto_gen_path, args.target_name + "_custom_source_files.ini"
144- )
145 if os.path.exists(args.copy_dst_dir):129 if os.path.exists(args.copy_dst_dir):
146 shutil.rmtree(args.copy_dst_dir)130 shutil.rmtree(args.copy_dst_dir)
147 suffixes_to_ignore = [".txt"]131 suffixes_to_ignore = [".txt"]
148- cmake_current_binary_path = os.path.abspath(132+ cmake_current_binary_path = os.path.abspath(os.path.join(args.copy_dst_dir, "..", "..", ".."))
149- os.path.join(args.copy_dst_dir, "..", "..", "..")
150- )
151 if not os.path.exists(kernel_source_ini_file_path):133 if not os.path.exists(kernel_source_ini_file_path):
152- copy_dir(134+ copy_dir(args.kernel_base_dir, args.copy_dst_dir, exclude_dir=cmake_current_binary_path)
153- args.kernel_base_dir,
154- args.copy_dst_dir,
155- exclude_dir=cmake_current_binary_path,
156- )
157 kernel_src_config = configparser.ConfigParser()135 kernel_src_config = configparser.ConfigParser()
158 kernel_src_config.read(kernel_source_ini_file_path)136 kernel_src_config.read(kernel_source_ini_file_path)
159 sections = kernel_src_config.sections()137 sections = kernel_src_config.sections()
@@ -17,16 +17,12 @@ import os
17from opdesc_parser import _trans_short_soc_to_soc_version17from opdesc_parser import _trans_short_soc_to_soc_version
18 18 
19 19 
20-def write_options_to_file(20+def write_options_to_file(file_name: str, options_str: str, op_type: str, compute_unit: str, split_char: str):
21- file_name: str, options_str: str, op_type: str, compute_unit: str, split_char: str
22-):
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(25+ fd.write(op_type + split_char + compute_unit + split_char + options_str + "\n")
28- op_type + split_char + compute_unit + split_char + options_str + "\n"
29- )
30 except Exception as err:26 except Exception as err:
31 print("write compile options config file failed")27 print("write compile options config file failed")
32 raise (err)28 raise (err)
@@ -96,9 +92,7 @@ def _parse_compile_options(compile_options):
96 opc_template_kernel_str = ""92 opc_template_kernel_str = ""
97 93 
98 for opts in compile_options:94 for opts in compile_options:
99- category, value = _parse_single_option(95+ category, value = _parse_single_option(opts, opc_debug_config, opc_kernel_config)
100- opts, opc_debug_config, opc_kernel_config
101- )
102 if category == "compile_opt":96 if category == "compile_opt":
103 compile_opt.append(opts)97 compile_opt.append(opts)
104 elif category == "input_param":98 elif category == "input_param":
@@ -118,14 +112,10 @@ def _parse_compile_options(compile_options):
118 )112 )
119 113 
120 114 
121-def _build_opc_config_str(115+def _build_opc_config_str(opc_debug_config, opc_tiling_keys, opc_template_kernel_str, opc_kernel_config):
122- opc_debug_config, opc_tiling_keys, opc_template_kernel_str, opc_kernel_config
123-):
124 opc_config_str = ""116 opc_config_str = ""
125 if opc_debug_config:117 if opc_debug_config:
126- opc_config_str = "--op_debug_config=" + ";".join(118+ opc_config_str = "--op_debug_config=" + ";".join([opt for opt in opc_debug_config])
127- [opt for opt in opc_debug_config]
128- )
129 if len(opc_tiling_keys) > 0:119 if len(opc_tiling_keys) > 0:
130 if opc_config_str != "":120 if opc_config_str != "":
131 opc_config_str += "@"121 opc_config_str += "@"
@@ -144,43 +134,28 @@ def _build_opc_config_str(
144def _write_compile_options(compile_options_file, compile_opt, op_type, compute_unit):134def _write_compile_options(compile_options_file, compile_opt, op_type, compute_unit):
145 if len(compile_opt) > 0:135 if len(compile_opt) > 0:
146 options_str = ";".join([opt for opt in compile_opt])136 options_str = ";".join([opt for opt in compile_opt])
147- write_options_to_file(137+ write_options_to_file(compile_options_file, options_str, op_type, compute_unit, ",")
148- compile_options_file, options_str, op_type, compute_unit, ","
149- )
150 138 
151 139 
152def _write_opc_config(opc_config_file, opc_config_str, op_type, compute_unit):140def _write_opc_config(opc_config_file, opc_config_str, op_type, compute_unit):
153 if opc_config_str != "":141 if opc_config_str != "":
154- write_options_to_file(142+ write_options_to_file(opc_config_file, opc_config_str, op_type, compute_unit, "@")
155- opc_config_file, opc_config_str, op_type, compute_unit, "@"
156- )
157 143 
158 144 
159def _write_input_param_file(opc_config_file, input_param_file, op_type, compute_unit):145def _write_input_param_file(opc_config_file, input_param_file, op_type, compute_unit):
160 if input_param_file != "":146 if input_param_file != "":
161 if op_type == "ALL" or compute_unit == "":147 if op_type == "ALL" or compute_unit == "":
162- raise RuntimeError(148+ raise RuntimeError("--input-param-file must be used with a COMPUTE_UNIT, and OP_TYPE cannot be ALL.")
163- "--input-param-file must be used with a COMPUTE_UNIT, and OP_TYPE cannot be ALL."149+ write_options_to_file(opc_config_file, input_param_file, op_type, compute_unit, "@")
164- )
165- write_options_to_file(
166- opc_config_file, input_param_file, op_type, compute_unit, "@"
167- )
168 150 
169 151 
170-def gen_compile_options(152+def gen_compile_options(compile_options_file: str, op_type: str, compute_unit: str, compile_options: list):
171- compile_options_file: str, op_type: str, compute_unit: str, compile_options: list
172-):
173 base_dir = os.path.dirname(compile_options_file)153 base_dir = os.path.dirname(compile_options_file)
174 opc_config_file = os.path.join(base_dir, "custom_opc_options.ini")154 opc_config_file = os.path.join(base_dir, "custom_opc_options.ini")
175 155 
176- (156+ (compile_opt, opc_debug_config, opc_kernel_config, opc_tiling_keys, input_param_file, opc_template_kernel_str) = (
177- compile_opt,157+ _parse_compile_options(compile_options)
178- opc_debug_config,158+ )
179- opc_kernel_config,
180- opc_tiling_keys,
181- input_param_file,
182- opc_template_kernel_str,
183- ) = _parse_compile_options(compile_options)
184 159 
185 _write_compile_options(compile_options_file, compile_opt, op_type, compute_unit)160 _write_compile_options(compile_options_file, compile_opt, op_type, compute_unit)
186 161 
@@ -242,14 +217,9 @@ if __name__ == "__main__":
242 else:217 else:
243 comp_options.append(arg)218 comp_options.append(arg)
244 if soc_series and compute_soc:219 if soc_series and compute_soc:
245- raise RuntimeError(220+ raise RuntimeError("SOC_SERIES and COMPUTE_UNIT cannot be used at the same time")
246- "SOC_SERIES and COMPUTE_UNIT cannot be used at the same time"
247- )
248 if soc_series:221 if soc_series:
249- compute_soc = ";".join(222+ compute_soc = ";".join(_trans_short_soc_to_soc_version(soc_ver) for soc_ver in soc_series.split(";"))
250- _trans_short_soc_to_soc_version(soc_ver)
251- for soc_ver in soc_series.split(";")
252- )
253 elif compute_soc != "":223 elif compute_soc != "":
254 compute_soc = compute_soc[0:-1]224 compute_soc = compute_soc[0:-1]
255 final_options = []225 final_options = []
@@ -22,44 +22,20 @@ def run_command(args, **others):
22 try:22 try:
23 subprocess.run(args, check=True, **others)23 subprocess.run(args, check=True, **others)
24 except subprocess.CalledProcessError as e:24 except subprocess.CalledProcessError as e:
25- LogUtil.print_compile_log(25+ LogUtil.print_compile_log("", f"Command failed: {e}!", AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC)
26- "",
27- f"Command failed: {e}!",
28- AscendCLogLevel.LOG_ERROR,
29- LogUtil.Option.NON_SOC,
30- )
31 26 
32 27 
33def args_parse():28def args_parse():
34 parser = argparse.ArgumentParser()29 parser = argparse.ArgumentParser()
35- parser.add_argument(30+ parser.add_argument("-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.",
39- )
40 31 
41- parser.add_argument(32+ parser.add_argument("-k", "--kernel-libs-dir", default=[], help="Output files from kernel libs.")
42- "-k", "--kernel-libs-dir", default=[], help="Output files from kernel libs."
43- )
44 33 
45- parser.add_argument(34+ parser.add_argument("-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.",
49- )
50 35 
51- parser.add_argument(36+ parser.add_argument("-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.",
55- )
56 37 
57- parser.add_argument(38+ parser.add_argument("-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.",
62- )
63 39 
64 parser.add_argument("-p", "--package-name", default="", help="package name.")40 parser.add_argument("-p", "--package-name", default="", help="package name.")
65 return parser.parse_args()41 return parser.parse_args()
@@ -85,9 +61,7 @@ def collect_object_from_files(object_files_dir, tmp_obj_dir):
85 raise RuntimeError("-o/--object-files is empty, please check and reset.")61 raise RuntimeError("-o/--object-files is empty, please check and reset.")
86 62 
87 if os.path.isfile(tmp_obj_dir):63 if os.path.isfile(tmp_obj_dir):
88- raise RuntimeError(64+ raise RuntimeError("-t/--tmp-obj-dir is an existing file, is must be a directory, please check and reset.")
89- "-t/--tmp-obj-dir is an existing file, is must be a directory, please check and reset."
90- )
91 65 
92 if os.path.exists(tmp_obj_dir):66 if os.path.exists(tmp_obj_dir):
93 shutil.rmtree(tmp_obj_dir)67 shutil.rmtree(tmp_obj_dir)
@@ -117,25 +91,19 @@ def unpack_kernel_library(kernel_libs_dir, objects, tmp_obj_dir):
117 opregistry_pattern = kernel_libs_dir + "/**/libopregistry.a"91 opregistry_pattern = kernel_libs_dir + "/**/libopregistry.a"
118 opregistry_file_path = glob.glob(opregistry_pattern, recursive=True)92 opregistry_file_path = glob.glob(opregistry_pattern, recursive=True)
119 if len(opregistry_file_path) != 1:93 if len(opregistry_file_path) != 1:
120- raise RuntimeError(94+ raise RuntimeError(f"libopregistry.a not exist in {kernel_libs_dir}, please check and reset.")
121- f"libopregistry.a not exist in {kernel_libs_dir}, please check and reset."
122- )
123 kernel_libs_parts.append(opregistry_file_path[0])95 kernel_libs_parts.append(opregistry_file_path[0])
124 96 
125 kernels_pattern = kernel_libs_dir + "/**/libkernels.a"97 kernels_pattern = kernel_libs_dir + "/**/libkernels.a"
126 kernels_file_path = glob.glob(kernels_pattern, recursive=True)98 kernels_file_path = glob.glob(kernels_pattern, recursive=True)
127 if len(kernels_file_path) != 1:99 if len(kernels_file_path) != 1:
128- raise RuntimeError(100+ raise RuntimeError(f"libkernels.a not exist in {kernel_libs_dir}, please check and reset.")
129- f"libkernels.a not exist in {kernel_libs_dir}, please check and reset."
130- )
131 101 
132 kernel_libs_parts.append(kernels_file_path[0])102 kernel_libs_parts.append(kernels_file_path[0])
133 103 
134 for lib in kernel_libs_parts:104 for lib in kernel_libs_parts:
135 output = subprocess.check_output(["ar", "-t", lib])105 output = subprocess.check_output(["ar", "-t", lib])
136- objects += [106+ objects += [x.strip() for x in output.decode("utf-8").split("\n") if x.strip() != ""]
137- x.strip() for x in output.decode("utf-8").split("\n") if x.strip() != ""
138- ]
139 run_command(["ar", "x", lib], cwd=tmp_obj_dir)107 run_command(["ar", "x", lib], cwd=tmp_obj_dir)
140 108 
141 109 
@@ -150,14 +118,9 @@ def pack_static_library(output_file, objects, tmp_obj_dir, package_name):
150 step_size = 30118 step_size = 30
151 tmp_files = []119 tmp_files = []
152 for index in range(0, len(objects), step_size):120 for index in range(0, len(objects), step_size):
153- tmp_file = os.path.join(121+ tmp_file = os.path.join(output_dir_path, package_name + f"_ascendc_tmp{index}.o")
154- output_dir_path, package_name + f"_ascendc_tmp{index}.o"
155- )
156 tmp_files.append(tmp_file)122 tmp_files.append(tmp_file)
157- run_command(123+ run_command(["ld", "-r", "-o", tmp_file] + objects[index : index + step_size], cwd=tmp_obj_dir)
158- ["ld", "-r", "-o", tmp_file] + objects[index : index + step_size],
159- cwd=tmp_obj_dir,
160- )
161 124 
162 run_command(["ld", "-r", "-o", output_obj_file] + tmp_files[:], cwd=tmp_obj_dir)125 run_command(["ld", "-r", "-o", output_obj_file] + tmp_files[:], cwd=tmp_obj_dir)
163 126 
@@ -173,9 +136,7 @@ def main():
173 136 
174 objects = collect_object_from_files(args.object_files_dir, args.tmp_obj_dir)137 objects = collect_object_from_files(args.object_files_dir, args.tmp_obj_dir)
175 unpack_kernel_library(args.kernel_libs_dir, objects, args.tmp_obj_dir)138 unpack_kernel_library(args.kernel_libs_dir, objects, args.tmp_obj_dir)
176- pack_static_library(139+ pack_static_library(args.output_file, objects, args.tmp_obj_dir, args.package_name)
177- args.output_file, objects, args.tmp_obj_dir, args.package_name
178- )
179 140 
180 if args.remove_tmp_files == "1":141 if args.remove_tmp_files == "1":
181 remove_temporary_files(args.tmp_obj_dir)142 remove_temporary_files(args.tmp_obj_dir)
@@ -353,13 +353,7 @@ class AdpBuilder(opdesc_parser.OpDesc):
353 self.op_compile_option: str = "{}"353 self.op_compile_option: str = "{}"
354 super().__init__(op_type)354 super().__init__(op_type)
355 355 
356- def write_adapt(356+ def write_adapt(self: any, impl_path, path: str, op_compile_option_all: list = None, src_ini_config=None):
357- self: any,
358- impl_path,
359- path: str,
360- op_compile_option_all: list = None,
361- src_ini_config=None,
362- ):
363 self._build_paradefault()357 self._build_paradefault()
364 src_file_path = None358 src_file_path = None
365 if src_ini_config is not None:359 if src_ini_config is not None:
@@ -372,9 +366,7 @@ class AdpBuilder(opdesc_parser.OpDesc):
372 src_ini_config.get(sub_sections, "kernel_file"),366 src_ini_config.get(sub_sections, "kernel_file"),
373 )367 )
374 else:368 else:
375- src_file_path = os.path.join(369+ src_file_path = os.path.join(impl_path, src_ini_config.get(sub_sections, "kernel_file"))
376- impl_path, src_ini_config.get(sub_sections, "kernel_file")
377- )
378 break370 break
379 if os.environ.get("BUILD_BUILTIN_OPP") != "1" and impl_path != "":371 if os.environ.get("BUILD_BUILTIN_OPP") != "1" and impl_path != "":
380 if src_file_path is not None:372 if src_file_path is not None:
@@ -382,9 +374,7 @@ class AdpBuilder(opdesc_parser.OpDesc):
382 else:374 else:
383 src_file = os.path.join(impl_path, self.op_file + ".cpp")375 src_file = os.path.join(impl_path, self.op_file + ".cpp")
384 if not os.path.exists(src_file):376 if not os.path.exists(src_file):
385- src_file = os.path.join(377+ src_file = os.path.join(impl_path, optype_snake(self.op_type), self.op_file + ".cpp")
386- impl_path, optype_snake(self.op_type), self.op_file + ".cpp"
387- )
388 if not os.path.exists(src_file):378 if not os.path.exists(src_file):
389 raise FileNotFoundError(379 raise FileNotFoundError(
390 f"operator: {src_file} source file does not found, "380 f"operator: {src_file} source file does not found, "
@@ -460,8 +450,7 @@ class AdpBuilder(opdesc_parser.OpDesc):
460 "="450 "="
461 + "["451 + "["
462 + ", ".join(452 + ", ".join(
463- word.strip().capitalize()453+ word.strip().capitalize() for word in self.argsdefv[i + argidx].strip("[]").split(",")
464- for word in self.argsdefv[i + argidx].strip("[]").split(",")
465 )454 )
466 + "]"455 + "]"
467 )456 )
@@ -517,9 +506,7 @@ class AdpBuilder(opdesc_parser.OpDesc):
517 val.append('"paramType":"optional"')506 val.append('"paramType":"optional"')
518 val.append('"shape":[1]')507 val.append('"shape":[1]')
519 val.append('"ori_shape":[1]')508 val.append('"ori_shape":[1]')
520- virt_exp.append(509+ virt_exp.append(" " + self.input_name[index] + " = {" + ",".join(val) + "}")
521- " " + self.input_name[index] + " = {" + ",".join(val) + "}"
522- )
523 if len(virt_exp) > 0:510 if len(virt_exp) > 0:
524 return "\n".join(virt_exp)511 return "\n".join(virt_exp)
525 else:512 else:
@@ -554,9 +541,7 @@ class AdpBuilder(opdesc_parser.OpDesc):
554 self.argsdefv.append(attrval)541 self.argsdefv.append(attrval)
555 continue542 continue
556 if optional:543 if optional:
557- self.argsdefv.append(544+ self.argsdefv.append(ATTR_DEFAULT.get(self.attr_val.get(attr).get("type")))
558- ATTR_DEFAULT.get(self.attr_val.get(attr).get("type"))
559- )
560 else:545 else:
561 self.argsdefv.append(None)546 self.argsdefv.append(None)
562 547 
@@ -573,11 +558,7 @@ class AdpBuilder(opdesc_parser.OpDesc):
573 if os.path.exists(impl_head_file):558 if os.path.exists(impl_head_file):
574 with open(impl_head_file, "r", newline="") as fh:559 with open(impl_head_file, "r", newline="") as fh:
575 impl_head = fh.read()560 impl_head = fh.read()
576- fd.write(561+ fd.write(impl_head.format(former_year, curr_year, self.input_ori_name, self.output_ori_name))
577- impl_head.format(
578- former_year, curr_year, self.input_ori_name, self.output_ori_name
579- )
580- )
581 562 
582 def _write_argparse(self: any, fd: object):563 def _write_argparse(self: any, fd: object):
583 args = self._build_paralist(False)564 args = self._build_paralist(False)
@@ -609,11 +590,7 @@ class AdpBuilder(opdesc_parser.OpDesc):
609 fd.write(" if {} != None:\n".format(attr))590 fd.write(" if {} != None:\n".format(attr))
610 fd.write(" attr = {}\n")591 fd.write(" attr = {}\n")
611 fd.write(' attr["name"] = "{}"\n'.format(attr))592 fd.write(' attr["name"] = "{}"\n'.format(attr))
612- fd.write(593+ fd.write(' attr["dtype"] = "{}"\n'.format(self.attr_val.get(attr).get("type")))
613- ' attr["dtype"] = "{}"\n'.format(
614- self.attr_val.get(attr).get("type")
615- )
616- )
617 fd.write(' attr["value"] = {}\n'.format(attr))594 fd.write(' attr["value"] = {}\n'.format(attr))
618 fd.write(" __attrs__.append(attr)\n")595 fd.write(" __attrs__.append(attr)\n")
619 fd.write(" return __inputs__, __outputs__, __attrs__\n")596 fd.write(" return __inputs__, __outputs__, __attrs__\n")
@@ -651,12 +628,8 @@ class AdpBuilder(opdesc_parser.OpDesc):
651 del op_compile_options["impl_mode"]628 del op_compile_options["impl_mode"]
652 self.op_compile_option = json.dumps(op_compile_options)629 self.op_compile_option = json.dumps(op_compile_options)
653 else:630 else:
654- self.impl_mode = (631+ self.impl_mode = ", impl_mode ='" + op_compile_options["impl_mode"] + "'"
655- ", impl_mode ='" + op_compile_options["impl_mode"] + "'"632+ self.impl_mode_op_info = ", impl_mode ='" + op_compile_options["impl_mode"] + "'"
656- )
657- self.impl_mode_op_info = (
658- ", impl_mode ='" + op_compile_options["impl_mode"] + "'"
659- )
660 elif len(self.impl_mode_str) == 0:633 elif len(self.impl_mode_str) == 0:
661 self.impl_mode = ', impl_mode = ""'634 self.impl_mode = ', impl_mode = ""'
662 self.impl_mode_op_info = ", impl_mode = impl_mode"635 self.impl_mode_op_info = ", impl_mode = impl_mode"
@@ -671,13 +644,9 @@ class AdpBuilder(opdesc_parser.OpDesc):
671 if sub_sections[65:] == self.op_type:644 if sub_sections[65:] == self.op_type:
672 src_compute_units_low = []645 src_compute_units_low = []
673 if src_ini_config.has_option(sub_sections, "compute_unit"):646 if src_ini_config.has_option(sub_sections, "compute_unit"):
674- src_compute_units = src_ini_config.get(647+ src_compute_units = src_ini_config.get(sub_sections, "compute_unit").split(",")
675- sub_sections, "compute_unit"
676- ).split(",")
677 for sub_unit in src_compute_units:648 for sub_unit in src_compute_units:
678- src_compute_units_low.append(649+ src_compute_units_low.append(_trans_soc_ver_to_short(sub_unit))
679- _trans_soc_ver_to_short(sub_unit)
680- )
681 if len(src_compute_units_low) == 0:650 if len(src_compute_units_low) == 0:
682 if src_ini_config.has_option(sub_sections, "kernel_dir"):651 if src_ini_config.has_option(sub_sections, "kernel_dir"):
683 src_file_dict["__ALLSOC__"] = (652 src_file_dict["__ALLSOC__"] = (
@@ -686,9 +655,7 @@ class AdpBuilder(opdesc_parser.OpDesc):
686 + src_ini_config.get(sub_sections, "kernel_file")655 + src_ini_config.get(sub_sections, "kernel_file")
687 )656 )
688 else:657 else:
689- src_file_dict["__ALLSOC__"] = src_ini_config.get(658+ src_file_dict["__ALLSOC__"] = src_ini_config.get(sub_sections, "kernel_file")
690- sub_sections, "kernel_file"
691- )
692 else:659 else:
693 for op_compute_unit in src_compute_units_low:660 for op_compute_unit in src_compute_units_low:
694 if src_ini_config.has_option(sub_sections, "kernel_dir"):661 if src_ini_config.has_option(sub_sections, "kernel_dir"):
@@ -698,9 +665,7 @@ class AdpBuilder(opdesc_parser.OpDesc):
698 + src_ini_config.get(sub_sections, "kernel_file")665 + src_ini_config.get(sub_sections, "kernel_file")
699 )666 )
700 else:667 else:
701- src_file_dict[str(op_compute_unit)] = (668+ src_file_dict[str(op_compute_unit)] = src_ini_config.get(sub_sections, "kernel_file")
702- src_ini_config.get(sub_sections, "kernel_file")
703- )
704 return src_file_dict669 return src_file_dict
705 670 
706 def _write_impl(self: any, fd: object, impl_path: str = "", src_ini_config=None):671 def _write_impl(self: any, fd: object, impl_path: str = "", src_ini_config=None):
@@ -745,23 +710,14 @@ class AdpBuilder(opdesc_parser.OpDesc):
745 )710 )
746 )711 )
747 712 
748- value_depend_obj = {713+ value_depend_obj = {key: value for key, value in self.input_value_depend.items()}
749- key: value for key, value in self.input_value_depend.items()
750- }
751 extend_opt = {"valueDepend": value_depend_obj}714 extend_opt = {"valueDepend": value_depend_obj}
752 if os.environ.get("BUILD_BUILTIN_OPP") == "1":715 if os.environ.get("BUILD_BUILTIN_OPP") == "1":
753 relative_kernel_src_path = os.path.realpath(716 relative_kernel_src_path = os.path.realpath(
754- self._get_kernel_source(717+ self._get_kernel_source(impl_path, src, optype_snake(self.op_type), optype_snake_ex(self.op_type))
755- impl_path,
756- src,
757- optype_snake(self.op_type),
758- optype_snake_ex(self.op_type),
759- )
760 )718 )
761 # to match src path in .dat file system, turn relative path into absolute path719 # to match src path in .dat file system, turn relative path into absolute path
762- abs_rel_kernel_src_path = os.path.join(720+ abs_rel_kernel_src_path = os.path.join("/", os.path.relpath(relative_kernel_src_path, impl_path))
763- "/", os.path.relpath(relative_kernel_src_path, impl_path)
764- )
765 721 
766 # compiling hidden src file requires src path before packaging .dat file,722 # compiling hidden src file requires src path before packaging .dat file,
767 # hard code such src path to <op_type>.py723 # hard code such src path to <op_type>.py
@@ -804,24 +760,14 @@ class AdpBuilder(opdesc_parser.OpDesc):
804 argsdef = self._build_paralist()760 argsdef = self._build_paralist()
805 argsval = self._build_paralist(False)761 argsval = self._build_paralist(False)
806 if cap_name == "check_supported":762 if cap_name == "check_supported":
807- fd.write(763+ fd.write(SUP_API.format(cap_name, argsdef, self.impl_mode, argsval, cap_name, self.op_type))
808- SUP_API.format(
809- cap_name, argsdef, self.impl_mode, argsval, cap_name, self.op_type
810- )
811- )
812 else:764 else:
813- fd.write(765+ fd.write(CAP_API.format(cap_name, argsdef, self.impl_mode, argsval, cap_name, self.op_type))
814- CAP_API.format(
815- cap_name, argsdef, self.impl_mode, argsval, cap_name, self.op_type
816- )
817- )
818 766 
819 def _write_glz(self: any, fd: object):767 def _write_glz(self: any, fd: object):
820 argsdef = self._build_paralist()768 argsdef = self._build_paralist()
821 argsval = self._build_paralist(False)769 argsval = self._build_paralist(False)
822- fd.write(770+ fd.write(GLZ_API.format(self.op_type, self.op_intf, argsdef, argsval, self.op_type))
823- GLZ_API.format(self.op_type, self.op_intf, argsdef, argsval, self.op_type)
824- )
825 771 
826 772 
827def write_scripts(773def write_scripts(
@@ -834,28 +780,18 @@ def write_scripts(
834):780):
835 batch_lists = cfgs.get(const_var.REPLAY_BATCH).split(";")781 batch_lists = cfgs.get(const_var.REPLAY_BATCH).split(";")
836 iterator_lists = cfgs.get(const_var.REPLAY_ITERATE).split(";")782 iterator_lists = cfgs.get(const_var.REPLAY_ITERATE).split(";")
837- if isinstance(kernel_source_ini_file, str) and os.path.exists(783+ if isinstance(kernel_source_ini_file, str) and os.path.exists(kernel_source_ini_file):
838- kernel_source_ini_file
839- ):
840 src_ini_config = configparser.ConfigParser(interpolation=None)784 src_ini_config = configparser.ConfigParser(interpolation=None)
841 src_ini_config.read(kernel_source_ini_file)785 src_ini_config.read(kernel_source_ini_file)
842 else:786 else:
843 src_ini_config = None787 src_ini_config = None
844 file_map = {}788 file_map = {}
845 op_descs = opdesc_parser.get_op_desc(789 op_descs = opdesc_parser.get_op_desc(
846- cfgfile,790+ cfgfile, batch_lists, iterator_lists, AdpBuilder, ops, dirs.get(const_var.AUTO_GEN_DIR)
847- batch_lists,
848- iterator_lists,
849- AdpBuilder,
850- ops,
851- dirs.get(const_var.AUTO_GEN_DIR),
852 )791 )
853 for op_desc in op_descs:792 for op_desc in op_descs:
854 op_desc.write_adapt(793 op_desc.write_adapt(
855- dirs.get(const_var.CFG_IMPL_DIR),794+ dirs.get(const_var.CFG_IMPL_DIR), dirs.get(const_var.CFG_OUT_DIR), op_compile_option, src_ini_config
856- dirs.get(const_var.CFG_OUT_DIR),
857- op_compile_option,
858- src_ini_config,
859 )795 )
860 file_map[op_desc.op_type] = op_desc.op_file796 file_map[op_desc.op_type] = op_desc.op_file
861 return file_map797 return file_map
@@ -865,9 +801,7 @@ class OpFileNotExistsError(Exception):
865 """File does not exist error."""801 """File does not exist error."""
866 802 
867 def __str__(self) -> str:803 def __str__(self) -> str:
868- return (804+ return f"File aic-*-ops-info.ini does not exist in directory {super().__str__()}"
869- f"File aic-*-ops-info.ini does not exist in directory {super().__str__()}"
870- )
871 805 
872 806 
873def get_ops_info_files(opsinfo_dir: List[str]) -> List[str]:807def get_ops_info_files(opsinfo_dir: List[str]) -> List[str]:
@@ -19,9 +19,7 @@ PYF_PATH = os.path.dirname(os.path.realpath(__file__))
19 19 
20class OpInfo:20class OpInfo:
21 def __init__(self: any, op_type: str, cfg_file: str):21 def __init__(self: any, op_type: str, cfg_file: str):
22- op_descs = opdesc_parser.get_op_desc(22+ op_descs = opdesc_parser.get_op_desc(cfg_file, [], [], opdesc_parser.OpDesc, [op_type])
23- cfg_file, [], [], opdesc_parser.OpDesc, [op_type]
24- )
25 if op_descs is None or len(op_descs) != 1:23 if op_descs is None or len(op_descs) != 1:
26 raise RuntimeError("cannot get op info of {}".format(op_type))24 raise RuntimeError("cannot get op info of {}".format(op_type))
27 self.op_desc = op_descs[0]25 self.op_desc = op_descs[0]
@@ -179,14 +179,7 @@ def add_op_config(op_file, bin_info, config):
179 179 
180 180 
181def gen_ops_config(json_file, soc, binary_info_config, config):181def gen_ops_config(json_file, soc, binary_info_config, config):
182- core_type_map = {182+ core_type_map = {"MIX": 0, "AiCore": 1, "VectorCore": 2, "MIX_AICORE": 3, "MIX_VECTOR_CORE": 4, "MIX_AIV": 4}
183- "MIX": 0,
184- "AiCore": 1,
185- "VectorCore": 2,
186- "MIX_AICORE": 3,
187- "MIX_VECTOR_CORE": 4,
188- "MIX_AIV": 4,
189- }
190 contents = load_json(json_file)183 contents = load_json(json_file)
191 if ("binFileName" not in contents) or ("supportInfo" not in contents):184 if ("binFileName" not in contents) or ("supportInfo" not in contents):
192 return185 return
@@ -216,11 +209,7 @@ def gen_ops_config(json_file, soc, binary_info_config, config):
216 bin_info["simplifiedKey"] = simplified_key209 bin_info["simplifiedKey"] = simplified_key
217 obj_file = os.path.join(soc, op_dir, bin_file_name)210 obj_file = os.path.join(soc, op_dir, bin_file_name)
218 op_info = OpConfig(211 op_info = OpConfig(
219- op_type=op_type,212+ op_type=op_type, support_info=support_info, core_type=core_type, task_ration=task_ration, obj_file=obj_file
220- support_info=support_info,
221- core_type=core_type,
222- task_ration=task_ration,
223- obj_file=obj_file,
224 )213 )
225 add_simplified_config(op_info, binary_info_config, config)214 add_simplified_config(op_info, binary_info_config, config)
226 215 
@@ -235,9 +224,7 @@ def gen_ops_config(json_file, soc, binary_info_config, config):
235 add_dict_key(bin_info, "optionalInputMode", support_info.get("optionalInputMode"))224 add_dict_key(bin_info, "optionalInputMode", support_info.get("optionalInputMode"))
236 add_dict_key(bin_info, "deterministic", support_info.get("deterministic"))225 add_dict_key(bin_info, "deterministic", support_info.get("deterministic"))
237 if support_info.get("optionalOutputMode") is not None:226 if support_info.get("optionalOutputMode") is not None:
238- add_dict_key(227+ add_dict_key(bin_info, "optionalOutputMode", support_info.get("optionalOutputMode"))
239- bin_info, "optionalOutputMode", support_info.get("optionalOutputMode")
240- )
241 228 
242 bin_info["binInfo"] = {"jsonFilePath": os.path.join(soc, op_dir, json_base_name)}229 bin_info["binInfo"] = {"jsonFilePath": os.path.join(soc, op_dir, json_base_name)}
243 add_op_config(op_file, bin_info, config)230 add_op_config(op_file, bin_info, config)
@@ -259,15 +246,11 @@ def check_single_op_is_void(root_dir, skip_error=False):
259def write_jsons(out_dir, file_list, config):246def write_jsons(out_dir, file_list, config):
260 for json_name in file_list:247 for json_name in file_list:
261 json_file = os.path.join(out_dir, json_name)248 json_file = os.path.join(out_dir, json_name)
262- with os.fdopen(249+ with os.fdopen(os.open(json_file, const_var.WFLAGS, const_var.WMODES), "w") as fd:
263- os.open(json_file, const_var.WFLAGS, const_var.WMODES), "w"
264- ) as fd:
265 json.dump(config.get(json_name), fd, indent=" ")250 json.dump(config.get(json_name), fd, indent=" ")
266 251 
267 252 
268-def generate_operator_cfg_file(253+def generate_operator_cfg_file(json_files, binary_info_config, soc, out_dir, gen_json_status):
269- json_files, binary_info_config, soc, out_dir, gen_json_status
270-):
271 if not json_files:254 if not json_files:
272 return255 return
273 256 
@@ -280,9 +263,7 @@ def generate_operator_cfg_file(
280 gen_ops_config(_json, soc, binary_info_config, config)263 gen_ops_config(_json, soc, binary_info_config, config)
281 264 
282 if gen_json_status == "single_json":265 if gen_json_status == "single_json":
283- file_list = [266+ file_list = [json_file for json_file in config.keys() if json_file != binary_info_config]
284- json_file for json_file in config.keys() if json_file != binary_info_config
285- ]
286 elif gen_json_status == "summary_json":267 elif gen_json_status == "summary_json":
287 file_list = [binary_info_config]268 file_list = [binary_info_config]
288 else:269 else:
@@ -291,9 +272,7 @@ def generate_operator_cfg_file(
291 write_jsons(out_dir, file_list, config)272 write_jsons(out_dir, file_list, config)
292 273 
293 274 
294-def gen_all_config(275+def gen_all_config(root_dir, soc, out_dir, 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-):
297 if op_range != "relocatable":276 if op_range != "relocatable":
298 flag = check_single_op_is_void(root_dir, skip_error)277 flag = check_single_op_is_void(root_dir, skip_error)
299 if flag is False:278 if flag is False:
@@ -322,20 +301,12 @@ def gen_all_config(
322 # normal kernel301 # normal kernel
323 if op_range == "all" or op_range == "normal":302 if op_range == "all" or op_range == "normal":
324 binary_info_config = "binary_info_config.json"303 binary_info_config = "binary_info_config.json"
325- generate_operator_cfg_file(304+ generate_operator_cfg_file(normal_json_files, binary_info_config, soc, out_dir, gen_normale_json)
326- normal_json_files, binary_info_config, soc, out_dir, gen_normale_json
327- )
328 305 
329 # relocatable kernel306 # relocatable kernel
330 if op_range == "all" or op_range == "relocatable":307 if op_range == "all" or op_range == "relocatable":
331 binary_info_config = "relocatable_kernel_info_config.json"308 binary_info_config = "relocatable_kernel_info_config.json"
332- generate_operator_cfg_file(309+ generate_operator_cfg_file(relocatable_json_files, binary_info_config, soc, out_dir, gen_relocatable_json)
333- relocatable_json_files,
334- binary_info_config,
335- soc,
336- out_dir,
337- gen_relocatable_json,
338- )
339 return True310 return True
340 311 
341 312 
@@ -359,24 +330,14 @@ def gen_all_soc_config(all_path):
359 330 
360def args_parse():331def args_parse():
361 parser = argparse.ArgumentParser()332 parser = argparse.ArgumentParser()
362- parser.add_argument(333+ parser.add_argument("-p", "--path", nargs="?", required=True, help="Parse the path of the json file.")
363- "-p",
364- "--path",
365- nargs="?",
366- required=True,
367- help="Parse the path of the json file.",
368- )
369 334 
370- parser.add_argument(335+ parser.add_argument("-s", "--soc", nargs="?", required=True, help="Parse the soc_version of ops.")
371- "-s", "--soc", nargs="?", required=True, help="Parse the soc_version of ops."
372- )
373 336 
374 parser.add_argument("-o", "--out", nargs="?", help="Output directory.")337 parser.add_argument("-o", "--out", nargs="?", help="Output directory.")
375 338 
376 parser.add_argument(339 parser.add_argument(
377- "--skip-binary-info-config",340+ "--skip-binary-info-config", action="store_true", help="binary_info_config.json file is not parsed."
378- action="store_true",
379- help="binary_info_config.json file is not parsed.",
380 )341 )
381 342 
382 parser.add_argument(343 parser.add_argument(
@@ -387,9 +348,7 @@ def args_parse():
387 help="all operators/normal operators/relocatable operators.",348 help="all operators/normal operators/relocatable operators.",
388 )349 )
389 350 
390- parser.add_argument(351+ parser.add_argument("--skip-error", nargs="?", default=False, help="skip error msg.")
391- "--skip-error", nargs="?", default=False, help="skip error msg."
392- )
393 352 
394 return parser.parse_args()353 return parser.parse_args()
395 354 
@@ -401,14 +360,7 @@ def main():
401 else:360 else:
402 out_dir = args.out361 out_dir = args.out
403 362 
404- flag = gen_all_config(363+ flag = gen_all_config(args.path, args.soc, out_dir, 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- )
412 if flag is False:364 if flag is False:
413 sys.exit(1)365 sys.exit(1)
414 366 
@@ -33,10 +33,7 @@ class PackKernel:
33 os.makedirs(self.out_path, exist_ok=True)33 os.makedirs(self.out_path, exist_ok=True)
34 except Exception as e:34 except Exception as e:
35 LogUtil.print_compile_log(35 LogUtil.print_compile_log(
36- "",36+ "", f"make {self.out_path} error: {e}!", AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC
37- f"make {self.out_path} error: {e}!",
38- AscendCLogLevel.LOG_ERROR,
39- LogUtil.Option.NON_SOC,
40 )37 )
41 38 
42 def load_json(self: any, json_file: str):39 def load_json(self: any, json_file: str):
@@ -69,10 +66,7 @@ class PackKernel:
69 )66 )
70 except Exception:67 except Exception:
71 LogUtil.print_compile_log(68 LogUtil.print_compile_log(
72- "",69+ "", " ascend610lite execute objcopy fail!", AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC
73- " ascend610lite execute objcopy fail!",
74- AscendCLogLevel.LOG_ERROR,
75- LogUtil.Option.NON_SOC,
76 )70 )
77 return None71 return None
78 return [sym + "_start", sym + "_end"]72 return [sym + "_start", sym + "_end"]
@@ -114,10 +108,7 @@ class PackKernel:
114 subprocess.run(["echo", "unsupported environment!"])108 subprocess.run(["echo", "unsupported environment!"])
115 except Exception as e:109 except Exception as e:
116 LogUtil.print_compile_log(110 LogUtil.print_compile_log(
117- "",111+ "", f"{target_platform} execute objcopy error: {e}!", AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC
118- f"{target_platform} execute objcopy error: {e}!",
119- AscendCLogLevel.LOG_ERROR,
120- LogUtil.Option.NON_SOC,
121 )112 )
122 return None113 return None
123 return [sym + "_start", sym + "_end"]114 return [sym + "_start", sym + "_end"]
@@ -186,9 +177,7 @@ class PackKernel:
186 for pair_addr in soc_syms:177 for pair_addr in soc_syms:
187 pair_addr1 = ["&" + s for s in pair_addr]178 pair_addr1 = ["&" + s for s in pair_addr]
188 op_syms += pair_addr179 op_syms += pair_addr
189- soc_pairs.append(180+ soc_pairs.append(" {{ {} }} ".format(", \\\n ".join(pair_addr1)))
190- " {{ {} }} ".format(", \\\n ".join(pair_addr1))
191- )
192 soc_res += ", \\\n ".join(soc_pairs)181 soc_res += ", \\\n ".join(soc_pairs)
193 soc_res += " } }"182 soc_res += " } }"
194 socs_res.append(soc_res)183 socs_res.append(soc_res)
@@ -197,20 +186,12 @@ class PackKernel:
197 macro_str = '#define {}_RESOURCES {{{{"{}", {}}}}}'.format(186 macro_str = '#define {}_RESOURCES {{{{"{}", {}}}}}'.format(
198 op_type, op_type, "{}_OP_RESOURCES".format(op_type)187 op_type, op_type, "{}_OP_RESOURCES".format(op_type)
199 )188 )
200- var_str = (189+ var_str = "extern gert::OpImplRegisterV2 op_impl_register_optiling_{};\n".format(op_type)
201- "extern gert::OpImplRegisterV2 op_impl_register_optiling_{};\n".format(
202- op_type
203- )
204- )
205 if len(op_syms) > 0:190 if len(op_syms) > 0:
206- var_str += (191+ var_str += "extern uint8_t " + ";\nextern uint8_t ".join(op_syms) + ";\n"
207- "extern uint8_t " + ";\nextern uint8_t ".join(op_syms) + ";\n"
208- )
209 head_file = os.path.join(self.out_path, "{}_op_resource.h".format(op_type))192 head_file = os.path.join(self.out_path, "{}_op_resource.h".format(op_type))
210 try:193 try:
211- with os.fdopen(194+ with os.fdopen(os.open(head_file, const_var.WFLAGS, const_var.WMODES), "w") as fd:
212- os.open(head_file, const_var.WFLAGS, const_var.WMODES), "w"
213- ) as fd:
214 fd.write("#include <stdint.h>\n")195 fd.write("#include <stdint.h>\n")
215 fd.write("#include <map>\n")196 fd.write("#include <map>\n")
216 fd.write("#include <tuple>\n")197 fd.write("#include <tuple>\n")
@@ -223,10 +204,7 @@ class PackKernel:
223 fd.write(macro_str)204 fd.write(macro_str)
224 except Exception as e:205 except Exception as e:
225 LogUtil.print_compile_log(206 LogUtil.print_compile_log(
226- "",207+ "", f"{op_type}_op_resource.h create error: {e}!", AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC
227- f"{op_type}_op_resource.h create error: {e}!",
228- AscendCLogLevel.LOG_ERROR,
229- LogUtil.Option.NON_SOC,
230 )208 )
231 209 
232 def ascendc_gen_lib(self: any):210 def ascendc_gen_lib(self: any):
@@ -244,10 +222,7 @@ class PackKernel:
244 subprocess.run(["ranlib", out_lib])222 subprocess.run(["ranlib", out_lib])
245 except Exception as e:223 except Exception as e:
246 LogUtil.print_compile_log(224 LogUtil.print_compile_log(
247- "",225+ "", f"execute ar/ranlib command error: {e}!", AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC
248- f"execute ar/ranlib command error: {e}!",
249- AscendCLogLevel.LOG_ERROR,
250- LogUtil.Option.NON_SOC,
251 )226 )
252 227 
253 def ascendc_gen_opsinfo(self: any):228 def ascendc_gen_opsinfo(self: any):
@@ -256,26 +231,10 @@ class PackKernel:
256 231 
257def args_parse():232def args_parse():
258 parser = argparse.ArgumentParser()233 parser = argparse.ArgumentParser()
259- parser.add_argument(234+ parser.add_argument("-i", "--input-path", nargs="?", help="Input path of compile result.")
260- "-i", "--input-path", nargs="?", help="Input path of compile result."235+ parser.add_argument("-o", "--output-path", nargs="?", help="Output path of compile result.")
261- )236+ parser.add_argument("-l", "--enable-library", nargs="?", default=None, help="Whether library is enabled.")
262- parser.add_argument(237+ parser.add_argument("-p", "--platform", nargs="?", default=None, help="target platform is x86_64 or aarch64.")
263- "-o", "--output-path", nargs="?", help="Output path of compile result."
264- )
265- parser.add_argument(
266- "-l",
267- "--enable-library",
268- nargs="?",
269- default=None,
270- help="Whether library is enabled.",
271- )
272- parser.add_argument(
273- "-p",
274- "--platform",
275- nargs="?",
276- default=None,
277- help="target platform is x86_64 or aarch64.",
278- )
279 return parser.parse_args()238 return parser.parse_args()
280 239 
281 240 
@@ -39,38 +39,26 @@ class PackKernel:
39 shutil.rmtree(self.copy_path)39 shutil.rmtree(self.copy_path)
40 except OSError:40 except OSError:
41 LogUtil.print_compile_log(41 LogUtil.print_compile_log(
42- "",42+ "", f"remove {self.copy_path} error!", AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC
43- f"remove {self.copy_path} error!",
44- AscendCLogLevel.LOG_ERROR,
45- LogUtil.Option.NON_SOC,
46 )43 )
47 if os.path.exists(self.out_path):44 if os.path.exists(self.out_path):
48 try:45 try:
49 shutil.rmtree(self.out_path)46 shutil.rmtree(self.out_path)
50 except OSError:47 except OSError:
51 LogUtil.print_compile_log(48 LogUtil.print_compile_log(
52- "",49+ "", f"remove {self.out_path} error!", AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC
53- f"remove {self.out_path} error!",
54- AscendCLogLevel.LOG_ERROR,
55- LogUtil.Option.NON_SOC,
56 )50 )
57 try:51 try:
58 os.makedirs(self.copy_path, exist_ok=True)52 os.makedirs(self.copy_path, exist_ok=True)
59 except Exception as e:53 except Exception as e:
60 LogUtil.print_compile_log(54 LogUtil.print_compile_log(
61- "",55+ "", f"make {self.copy_path} error: {e}!", AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC
62- f"make {self.copy_path} error: {e}!",
63- AscendCLogLevel.LOG_ERROR,
64- LogUtil.Option.NON_SOC,
65 )56 )
66 try:57 try:
67 os.makedirs(self.out_path, exist_ok=True)58 os.makedirs(self.out_path, exist_ok=True)
68 except Exception as e:59 except Exception as e:
69 LogUtil.print_compile_log(60 LogUtil.print_compile_log(
70- "",61+ "", f"make {self.out_path} error: {e}!", AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC
71- f"make {self.out_path} error: {e}!",
72- AscendCLogLevel.LOG_ERROR,
73- LogUtil.Option.NON_SOC,
74 )62 )
75 63 
76 def get_symbol(self: any, name: str):64 def get_symbol(self: any, name: str):
@@ -101,10 +89,7 @@ class PackKernel:
101 )89 )
102 except Exception:90 except Exception:
103 LogUtil.print_compile_log(91 LogUtil.print_compile_log(
104- "",92+ "", " ascend610lite execute objcopy fail!", AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC
105- " ascend610lite execute objcopy fail!",
106- AscendCLogLevel.LOG_ERROR,
107- LogUtil.Option.NON_SOC,
108 )93 )
109 return None94 return None
110 return [sym + "_start", sym + "_end"]95 return [sym + "_start", sym + "_end"]
@@ -147,10 +132,7 @@ class PackKernel:
147 subprocess.run(["echo", "unsupported environment!"])132 subprocess.run(["echo", "unsupported environment!"])
148 except Exception as e:133 except Exception as e:
149 LogUtil.print_compile_log(134 LogUtil.print_compile_log(
150- "",135+ "", f"{target_platform} execute objcopy error: {e}!", AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC
151- f"{target_platform} execute objcopy error: {e}!",
152- AscendCLogLevel.LOG_ERROR,
153- LogUtil.Option.NON_SOC,
154 )136 )
155 return None137 return None
156 return [sym + "_start", sym + "_end"]138 return [sym + "_start", sym + "_end"]
@@ -179,17 +161,13 @@ class PackKernel:
179 op_info[file_name] = []161 op_info[file_name] = []
180 path, filename = os.path.split(op_cfgs[file_name])162 path, filename = os.path.split(op_cfgs[file_name])
181 op_info[file_name].append(os.path.join(self.vendor_name, path))163 op_info[file_name].append(os.path.join(self.vendor_name, path))
182- op_info[file_name].append(164+ op_info[file_name].append(self.ascendc_gen_object(op_cfgs[file_name], path, self.vendor_name))
183- self.ascendc_gen_object(op_cfgs[file_name], path, self.vendor_name)
184- )
185 self.op_info = op_info165 self.op_info = op_info
186 166 
187 def ascendc_gen_register(self, macro_op, var_str):167 def ascendc_gen_register(self, macro_op, var_str):
188 registry_file = os.path.join(self.registry_path, "custom_op_registry_V2.cpp")168 registry_file = os.path.join(self.registry_path, "custom_op_registry_V2.cpp")
189 try:169 try:
190- with os.fdopen(170+ with os.fdopen(os.open(registry_file, const_var.WFLAGS, const_var.WMODES), "w") as fd:
191- os.open(registry_file, const_var.WFLAGS, const_var.WMODES), "w"
192- ) as fd:
193 fd.write("#include <stdint.h>\n")171 fd.write("#include <stdint.h>\n")
194 fd.write("#include <map>\n")172 fd.write("#include <map>\n")
195 fd.write("#include <tuple>\n")173 fd.write("#include <tuple>\n")
@@ -201,28 +179,16 @@ class PackKernel:
201 fd.write('#include "../pkg_inc/base/dlog_pub.h"\n\n')179 fd.write('#include "../pkg_inc/base/dlog_pub.h"\n\n')
202 fd.write(var_str)180 fd.write(var_str)
203 fd.write("\n")181 fd.write("\n")
204- fd.write(182+ fd.write("#define ASCENDC_MODULE_NAME static_cast<int32_t>(ASCENDCKERNEL)\n")
205- "#define ASCENDC_MODULE_NAME static_cast<int32_t>(ASCENDCKERNEL)\n"183+ fd.write("#define LOG_ERROR(format, ...) \\\n")
206- )184+ fd.write(" do { \\\n")
207- fd.write(185+ fd.write(' dlog_error(ASCENDC_MODULE_NAME, "[%s] " format "\\n", __FUNCTION__, ')
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- )
216 fd.write("##__VA_ARGS__); \\\n")186 fd.write("##__VA_ARGS__); \\\n")
217 fd.write(" } while (0)\n")187 fd.write(" } while (0)\n")
218 fd.write("namespace {\n")188 fd.write("namespace {\n")
219 fd.write("uint32_t OpLibInitFunc(ge::AscendString& op_lib_path) {\n")189 fd.write("uint32_t OpLibInitFunc(ge::AscendString& op_lib_path) {\n")
220 fd.write(" static " + macro_op)190 fd.write(" static " + macro_op)
221- fd.write(191+ fd.write(' static ops::OpBinInfo g_binInfo("' + self.vendor_name + '", __ascendc_op_info_')
222- ' static ops::OpBinInfo g_binInfo("'
223- + self.vendor_name
224- + '", __ascendc_op_info_'
225- )
226 fd.write(self.vendor_name + ");\n")192 fd.write(self.vendor_name + ");\n")
227 fd.write("Dl_info dlInfo;\n")193 fd.write("Dl_info dlInfo;\n")
228 fd.write("if (!dladdr((void*)&OpLibInitFunc, &dlInfo)) {\n")194 fd.write("if (!dladdr((void*)&OpLibInitFunc, &dlInfo)) {\n")
@@ -231,25 +197,16 @@ class PackKernel:
231 fd.write("}\n")197 fd.write("}\n")
232 fd.write("std::string targetPath = dlInfo.dli_fname;\n")198 fd.write("std::string targetPath = dlInfo.dli_fname;\n")
233 fd.write("if (!ops::OpBinInfo::Check(targetPath)) {\n")199 fd.write("if (!ops::OpBinInfo::Check(targetPath)) {\n")
234- fd.write(200+ fd.write(' LOG_ERROR("Path %s only support shared library, but it is not.",targetPath.c_str());\n')
235- ' LOG_ERROR("Path %s only support shared library, but it is not.",targetPath.c_str());\n'
236- )
237 fd.write(" return 1;\n")201 fd.write(" return 1;\n")
238 fd.write("}\n")202 fd.write("}\n")
239 fd.write(" return g_binInfo.Generate(&op_lib_path, targetPath);\n")203 fd.write(" return g_binInfo.Generate(&op_lib_path, targetPath);\n")
240 fd.write("}\n")204 fd.write("}\n")
241- fd.write(205+ fd.write("REGISTER_OP_LIB(" + self.vendor_name + ").RegOpLibInit(OpLibInitFunc);\n")
242- "REGISTER_OP_LIB("
243- + self.vendor_name
244- + ").RegOpLibInit(OpLibInitFunc);\n"
245- )
246 fd.write("}\n")206 fd.write("}\n")
247 except Exception as e:207 except Exception as e:
248 LogUtil.print_compile_log(208 LogUtil.print_compile_log(
249- "",209+ "", f"custom_op_registry_V2.cpp create error: {e}!", AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC
250- f"custom_op_registry_V2.cpp create error: {e}!",
251- AscendCLogLevel.LOG_ERROR,
252- LogUtil.Option.NON_SOC,
253 )210 )
254 211 
255 def ascendc_gen_header(self: any):212 def ascendc_gen_header(self: any):
@@ -273,15 +230,11 @@ class PackKernel:
273 soc_res += "}, \n"230 soc_res += "}, \n"
274 socs_res.append(soc_res)231 socs_res.append(soc_res)
275 if len(op_syms) > 0:232 if len(op_syms) > 0:
276- var_str += "".join(233+ var_str += "".join(["extern uint8_t {};\n".format(sym) for sym in op_syms])
277- ["extern uint8_t {};\n".format(sym) for sym in op_syms]
278- )
279 macro_op += "{{\n{}}}; \n".format("".join(socs_res))234 macro_op += "{{\n{}}}; \n".format("".join(socs_res))
280 head_file = os.path.join(self.out_path, "ge_table_op_resource.h")235 head_file = os.path.join(self.out_path, "ge_table_op_resource.h")
281 try:236 try:
282- with os.fdopen(237+ with os.fdopen(os.open(head_file, const_var.WFLAGS, const_var.WMODES), "w") as fd:
283- os.open(head_file, const_var.WFLAGS, const_var.WMODES), "w"
284- ) as fd:
285 fd.write("#include <stdint.h>\n")238 fd.write("#include <stdint.h>\n")
286 fd.write("#include <map>\n")239 fd.write("#include <map>\n")
287 fd.write("#include <tuple>\n")240 fd.write("#include <tuple>\n")
@@ -295,16 +248,11 @@ class PackKernel:
295 fd.write("}\n")248 fd.write("}\n")
296 except Exception as e:249 except Exception as e:
297 LogUtil.print_compile_log(250 LogUtil.print_compile_log(
298- "",251+ "", f"ge_table_op_resource.h create error: {e}!", AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC
299- f"ge_table_op_resource.h create error: {e}!",
300- AscendCLogLevel.LOG_ERROR,
301- LogUtil.Option.NON_SOC,
302 )252 )
303 macro_op_register = (253 macro_op_register = (
304 "std::vector<std::tuple<ge::AscendString, ge::AscendString, "254 "std::vector<std::tuple<ge::AscendString, ge::AscendString, "
305- "const uint8_t *, const uint8_t *>> __ascendc_op_info_"255+ "const uint8_t *, const uint8_t *>> __ascendc_op_info_" + self.vendor_name + " = \n"
306- + self.vendor_name
307- + " = \n"
308 )256 )
309 macro_op_register += "{{\n{}}}; \n".format("".join(socs_res))257 macro_op_register += "{{\n{}}}; \n".format("".join(socs_res))
310 self.ascendc_gen_register(macro_op_register, var_str)258 self.ascendc_gen_register(macro_op_register, var_str)
@@ -324,10 +272,7 @@ class PackKernel:
324 subprocess.run(["ranlib", out_lib])272 subprocess.run(["ranlib", out_lib])
325 except Exception as e:273 except Exception as e:
326 LogUtil.print_compile_log(274 LogUtil.print_compile_log(
327- "",275+ "", f"execute ar/ranlib command error: {e}!", AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC
328- f"execute ar/ranlib command error: {e}!",
329- AscendCLogLevel.LOG_ERROR,
330- LogUtil.Option.NON_SOC,
331 )276 )
332 277 
333 def ascendc_copy_dir_recursive(self: any, src_dir, dst_dir):278 def ascendc_copy_dir_recursive(self: any, src_dir, dst_dir):
@@ -340,13 +285,9 @@ class PackKernel:
340 for dir_path, _, items in os.walk(src_dir):285 for dir_path, _, items in os.walk(src_dir):
341 for item in items:286 for item in items:
342 src_item = os.path.join(dir_path, item)287 src_item = os.path.join(dir_path, item)
343- dst_item = os.path.join(288+ dst_item = os.path.join(dst_dir, dir_path.replace(src_dir, "."), item)
344- dst_dir, dir_path.replace(src_dir, "."), item
345- )
346 os.makedirs(os.path.dirname(dst_item), exist_ok=True)289 os.makedirs(os.path.dirname(dst_item), exist_ok=True)
347- if (not src_item.endswith(".txt")) and (290+ if (not src_item.endswith(".txt")) and (not os.path.exists(dst_item)):
348- not os.path.exists(dst_item)
349- ):
350 shutil.copy2(src_item, dst_item)291 shutil.copy2(src_item, dst_item)
351 292 
352 except Exception as e:293 except Exception as e:
@@ -362,10 +303,7 @@ class PackKernel:
362 shutil.copytree(source_file, target_file)303 shutil.copytree(source_file, target_file)
363 except Exception as e:304 except Exception as e:
364 LogUtil.print_compile_log(305 LogUtil.print_compile_log(
365- "",306+ "", f"copy {source_file} error: {e}!", AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC
366- f"copy {source_file} error: {e}!",
367- AscendCLogLevel.LOG_ERROR,
368- LogUtil.Option.NON_SOC,
369 )307 )
370 308 
371 def ascendc_copy_file(self: any, src_dir: str, target_dir: str):309 def ascendc_copy_file(self: any, src_dir: str, target_dir: str):
@@ -377,19 +315,13 @@ class PackKernel:
377 os.makedirs(target_dir, exist_ok=True)315 os.makedirs(target_dir, exist_ok=True)
378 except Exception as e:316 except Exception as e:
379 LogUtil.print_compile_log(317 LogUtil.print_compile_log(
380- "",318+ "", f"make {target_dir} error: {e}!", AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC
381- f"make {target_dir} error: {e}!",
382- AscendCLogLevel.LOG_ERROR,
383- LogUtil.Option.NON_SOC,
384 )319 )
385 try:320 try:
386 shutil.copy(source_file, target_dir)321 shutil.copy(source_file, target_dir)
387 except Exception as e:322 except Exception as e:
388 LogUtil.print_compile_log(323 LogUtil.print_compile_log(
389- "",324+ "", f"copy {source_file} error: {e}!", AscendCLogLevel.LOG_ERROR, LogUtil.Option.NON_SOC
390- f"copy {source_file} error: {e}!",
391- AscendCLogLevel.LOG_ERROR,
392- LogUtil.Option.NON_SOC,
393 )325 )
394 326 
395 def ascendc_copy_func(self: any):327 def ascendc_copy_func(self: any):
@@ -398,11 +330,7 @@ class PackKernel:
398 if os.path.isdir("./framework"):330 if os.path.isdir("./framework"):
399 framework_catalog = os.listdir("framework")331 framework_catalog = os.listdir("framework")
400 for catalog_file in framework_catalog:332 for catalog_file in framework_catalog:
401- if (333+ if catalog_file == "tf_plugin" or catalog_file == "caffe_plugin" or catalog_file == "onnx_plugin":
402- catalog_file == "tf_plugin"
403- or catalog_file == "caffe_plugin"
404- or catalog_file == "onnx_plugin"
405- ):
406 source_dir = self.kernel_binary_path + "/tbe/op_info_cfg/ai_core"334 source_dir = self.kernel_binary_path + "/tbe/op_info_cfg/ai_core"
407 dst_dir = os.path.join(self.copy_path, "framework", self.framework_type)335 dst_dir = os.path.join(self.copy_path, "framework", self.framework_type)
408 self.ascendc_copy_file(source_dir, dst_dir)336 self.ascendc_copy_file(source_dir, dst_dir)
@@ -413,61 +341,33 @@ class PackKernel:
413 dst_dir = os.path.join(self.copy_path, "op_impl/ai_core/tbe/config")341 dst_dir = os.path.join(self.copy_path, "op_impl/ai_core/tbe/config")
414 self.ascendc_copy_dir(source_dir, dst_dir)342 self.ascendc_copy_dir(source_dir, dst_dir)
415 source_dir = self.kernel_binary_path + "/binary/dynamic"343 source_dir = self.kernel_binary_path + "/binary/dynamic"
416- dst_dir = os.path.join(344+ dst_dir = os.path.join(self.copy_path, "op_impl/ai_core/tbe", self.vendor_name + "_impl", "dynamic")
417- self.copy_path, "op_impl/ai_core/tbe", self.vendor_name + "_impl", "dynamic"
418- )
419 self.ascendc_copy_dir_recursive(source_dir, dst_dir)345 self.ascendc_copy_dir_recursive(source_dir, dst_dir)
420 for compute_unit in self.op_soc_ver:346 for compute_unit in self.op_soc_ver:
421 source_dir = os.path.join(self.kernel_binary_path + "/binary", compute_unit)347 source_dir = os.path.join(self.kernel_binary_path + "/binary", compute_unit)
422- dst_dir = os.path.join(348+ dst_dir = os.path.join(self.copy_path, "op_impl/ai_core/tbe/kernel", compute_unit)
423- self.copy_path, "op_impl/ai_core/tbe/kernel", compute_unit
424- )
425 self.ascendc_copy_dir(source_dir, dst_dir)349 self.ascendc_copy_dir(source_dir, dst_dir)
426 source_dir = self.kernel_binary_path + "/binary/config"350 source_dir = self.kernel_binary_path + "/binary/config"
427 dst_dir = os.path.join(self.copy_path, "op_impl/ai_core/tbe/kernel/config")351 dst_dir = os.path.join(self.copy_path, "op_impl/ai_core/tbe/kernel/config")
428 self.ascendc_copy_dir(source_dir, dst_dir)352 self.ascendc_copy_dir(source_dir, dst_dir)
429 so_file = "op_impl/ai_core/tbe/op_master_device/lib/libcust_opmaster.so"353 so_file = "op_impl/ai_core/tbe/op_master_device/lib/libcust_opmaster.so"
430 if os.path.exists(so_file):354 if os.path.exists(so_file):
431- dst_dir = os.path.join(355+ dst_dir = os.path.join(self.copy_path, "op_impl/ai_core/tbe/op_master_device/lib")
432- self.copy_path, "op_impl/ai_core/tbe/op_master_device/lib"
433- )
434 os.makedirs(dst_dir, exist_ok=True)356 os.makedirs(dst_dir, exist_ok=True)
435 shutil.copy(so_file, dst_dir)357 shutil.copy(so_file, dst_dir)
436 358 
437 359 
438def args_parse():360def args_parse():
439 parser = argparse.ArgumentParser()361 parser = argparse.ArgumentParser()
440- parser.add_argument(362+ parser.add_argument("-r", "--registry-file-path", help="Output registry cpp file path.")
441- "-r", "--registry-file-path", help="Output registry cpp file path."363+ parser.add_argument("-i", "--input-path", nargs="?", help="Input path of compile result.")
442- )364+ parser.add_argument("-c", "--base-path", nargs="?", help="Base path of compile result.")
443- parser.add_argument(365+ parser.add_argument("-o", "--output-path", nargs="?", help="Output path of compile result.")
444- "-i", "--input-path", nargs="?", help="Input path of compile result."
445- )
446- parser.add_argument(
447- "-c", "--base-path", nargs="?", help="Base path of compile result."
448- )
449- parser.add_argument(
450- "-o", "--output-path", nargs="?", help="Output path of compile result."
451- )
452 parser.add_argument("-n", "--vendor-name", nargs="?", help="Vendor name.")366 parser.add_argument("-n", "--vendor-name", nargs="?", help="Vendor name.")
453 parser.add_argument("-u", "--compute-unit", nargs="?", help="Compute unit.")367 parser.add_argument("-u", "--compute-unit", nargs="?", help="Compute unit.")
454- parser.add_argument(368+ parser.add_argument("-t", "--framework-type", nargs="?", help="Framework type, eg:tensorflow.")
455- "-t", "--framework-type", nargs="?", help="Framework type, eg:tensorflow."369+ parser.add_argument("-p", "--platform", nargs="?", default=None, help="target platform is x86_64 or aarch64.")
456- )370+ parser.add_argument("-k", "--kernel-binary-path", nargs="?", default=None, help="target kernel binary path.")
457- parser.add_argument(
458- "-p",
459- "--platform",
460- nargs="?",
461- default=None,
462- help="target platform is x86_64 or aarch64.",
463- )
464- parser.add_argument(
465- "-k",
466- "--kernel-binary-path",
467- nargs="?",
468- default=None,
469- help="target kernel binary path.",
470- )
471 return parser.parse_args()371 return parser.parse_args()
472 372 
473 373 
@@ -32,10 +32,7 @@ def args_parse():
32 args = parser.parse_args()32 args = parser.parse_args()
33 if args.soc_series:33 if args.soc_series:
34 args.compute_unit = [34 args.compute_unit = [
35- " ".join(35+ " ".join(_trans_short_soc_to_soc_version(soc_ver) for soc_ver in args.soc_series[0].strip().split())
36- _trans_short_soc_to_soc_version(soc_ver)
37- for soc_ver in args.soc_series[0].strip().split()
38- )
39 ]36 ]
40 return args37 return args
41 38 
@@ -52,9 +49,7 @@ def is_relative_path(path):
52 49 
53 50 
54def gen_custom_source_files(args):51def gen_custom_source_files(args):
55- gen_file_path = os.path.join(52+ gen_file_path = os.path.join(args.auto_gen_path, args.target_name + "_custom_source_files.ini")
56- args.auto_gen_path, args.target_name + "_custom_source_files.ini"
57- )
58 src_ini_config = None53 src_ini_config = None
59 if os.path.exists(gen_file_path):54 if os.path.exists(gen_file_path):
60 src_ini_config = configparser.ConfigParser()55 src_ini_config = configparser.ConfigParser()
@@ -64,33 +59,23 @@ def gen_custom_source_files(args):
64 try:59 try:
65 with os.fdopen(os.open(gen_file_path, flags, modes), "a") as fd:60 with os.fdopen(os.open(gen_file_path, flags, modes), "a") as fd:
66 hash_value = hashlib.sha256(61 hash_value = hashlib.sha256(
67- (62+ (args.op_type + str(args.op_kernel_file) + str(args.op_kernel_dir) + str(args.compute_unit)).encode(
68- args.op_type63+ "utf-8"
69- + str(args.op_kernel_file)64+ )
70- + str(args.op_kernel_dir)
71- + str(args.compute_unit)
72- ).encode("utf-8")
73 ).hexdigest()65 ).hexdigest()
74- if (66+ if src_ini_config is not None and hash_value + "_" + args.op_type in src_ini_config.sections():
75- src_ini_config is not None
76- and hash_value + "_" + args.op_type in src_ini_config.sections()
77- ):
78 return True67 return True
79 fd.write("[" + hash_value + "_" + args.op_type + "]" + "\n")68 fd.write("[" + hash_value + "_" + args.op_type + "]" + "\n")
80 if args.op_kernel_dir is not None:69 if args.op_kernel_dir is not None:
81 flag = is_relative_path(args.op_kernel_dir)70 flag = is_relative_path(args.op_kernel_dir)
82 if not flag:71 if not flag:
83- print(72+ print("[ERROR] op kernel dir should be relative path to kernel base dir")
84- "[ERROR] op kernel dir should be relative path to kernel base dir"
85- )
86 return flag73 return flag
87 fd.write("kernel_dir=" + args.op_kernel_dir + "\n")74 fd.write("kernel_dir=" + args.op_kernel_dir + "\n")
88 if args.op_kernel_file is not None:75 if args.op_kernel_file is not None:
89 fd.write("kernel_file=" + args.op_kernel_file + "\n")76 fd.write("kernel_file=" + args.op_kernel_file + "\n")
90 if args.compute_unit is not None:77 if args.compute_unit is not None:
91- tmp_compute_unit = ",".join(78+ tmp_compute_unit = ",".join(x.strip() for x in args.compute_unit[0].strip().split())
92- x.strip() for x in args.compute_unit[0].strip().split()
93- )
94 fd.write("compute_unit=" + tmp_compute_unit + "\n")79 fd.write("compute_unit=" + tmp_compute_unit + "\n")
95 except Exception:80 except Exception:
96 print("write custom source files ini failed")81 print("write custom source files ini failed")
@@ -33,15 +33,7 @@ def _is_v220(op_product: str):
33 33 
34 34 
35InfoCodeChannelParams = collections.namedtuple(35InfoCodeChannelParams = collections.namedtuple(
36- "InfoCodeChannelParams",36+ "InfoCodeChannelParams", ["src_file", "tiling_header", "kernel_name", "outdir", "op_product", "compile_options"]
37- [
38- "src_file",
39- "tiling_header",
40- "kernel_name",
41- "outdir",
42- "op_product",
43- "compile_options",
44- ],
45)37)
46 38 
47 39 
@@ -59,25 +59,15 @@ clean:
59 59 
60def args_parse():60def args_parse():
61 parser = argparse.ArgumentParser()61 parser = argparse.ArgumentParser()
62- parser.add_argument(62+ parser.add_argument("-s", "--src-file", nargs="*", required=True, help="input src files")
63- "-s", "--src-file", nargs="*", required=True, help="input src files"
64- )
65 parser.add_argument("-d", "--output-dir", required=True, help="output dir")63 parser.add_argument("-d", "--output-dir", required=True, help="output dir")
66 parser.add_argument("-o", "--output", required=True, help="output")64 parser.add_argument("-o", "--output", required=True, help="output")
67 parser.add_argument("-p", "--cann-path", required=True, help="cann path")65 parser.add_argument("-p", "--cann-path", required=True, help="cann path")
68- parser.add_argument(66+ parser.add_argument("-j", "--parallel-jobs", required=False, help="compile parallel jobs")
69- "-j", "--parallel-jobs", required=False, help="compile parallel jobs"67+ parser.add_argument("-c", "--compile-options", nargs="*", required=False, help="compile options")
70- )68+ parser.add_argument("-l", "--link-options", nargs="*", required=False, help="compile options")
71- parser.add_argument(
72- "-c", "--compile-options", nargs="*", required=False, help="compile options"
73- )
74- parser.add_argument(
75- "-l", "--link-options", nargs="*", required=False, help="compile options"
76- )
77 69 
78- parser.add_argument(70+ parser.add_argument("-c++", "--cxx-compiler", required=False, help="compile options")
79- "-c++", "--cxx-compiler", required=False, help="compile options"
80- )
81 return parser.parse_args()71 return parser.parse_args()
82 72 
83 73 
@@ -43,7 +43,5 @@ if __name__ == "__main__":
43 print("insert op:[", k, "] success")43 print("insert op:[", k, "] success")
44 all_operators[k] = insert_operator[k]44 all_operators[k] = insert_operator[k]
45 45 
46- with os.fdopen(46+ with os.fdopen(os.open(sys.argv[2], const_var.WFLAGS, const_var.WMODES), "w") as json_file:
47- os.open(sys.argv[2], const_var.WFLAGS, const_var.WMODES), "w"
48- ) as json_file:
49 json_file.write(json.dumps(all_operators, indent=4))47 json_file.write(json.dumps(all_operators, indent=4))
@@ -224,9 +224,7 @@ def insert_simplified_keys(json_file):
224 precision = str(get_precision_value(support_info))224 precision = str(get_precision_value(support_info))
225 overflow = str(get_overflow_value(support_info))225 overflow = str(get_overflow_value(support_info))
226 input_parameters = get_all_input_parameters(support_info)226 input_parameters = get_all_input_parameters(support_info)
227- key = "{}/d={},p={},o={}/{}/".format(227+ key = "{}/d={},p={},o={}/{}/".format(op_type, deterministic, precision, overflow, input_parameters)
228- op_type, deterministic, precision, overflow, input_parameters
229- )
230 result = '"simplifiedKey": "' + key + '",\n'228 result = '"simplifiedKey": "' + key + '",\n'
231 insert_content_into_file(json_file, result)229 insert_content_into_file(json_file, result)
232 230 
@@ -240,13 +238,7 @@ def insert_all_simplified_keys(root_dir):
240 238 
241def args_parse():239def args_parse():
242 parser = argparse.ArgumentParser()240 parser = argparse.ArgumentParser()
243- parser.add_argument(241+ parser.add_argument("-p", "--path", nargs="?", required=True, help="Parse the path of the json file.")
244- "-p",
245- "--path",
246- nargs="?",
247- required=True,
248- help="Parse the path of the json file.",
249- )
250 return parser.parse_args()242 return parser.parse_args()
251 243 
252 244 
@@ -272,9 +272,7 @@ class OpDesc:
272 self.output_fmt_for_bin.update({self.output_idx: self._parse_str(conf)})272 self.output_fmt_for_bin.update({self.output_idx: self._parse_str(conf)})
273 elif conf.startswith("output{}.initValue".format(int(self.output_idx))):273 elif conf.startswith("output{}.initValue".format(int(self.output_idx))):
274 self.output_init_value[int(self.output_idx)] = self._parse_str(conf)274 self.output_init_value[int(self.output_idx)] = self._parse_str(conf)
275- elif conf.startswith(275+ elif conf.startswith("output{}.outputShapeDependOnCompute=true".format(int(self.output_idx))):
276- "output{}.outputShapeDependOnCompute=true".format(int(self.output_idx))
277- ):
278 self.output_shape_depend_on_compute.append(int(self.output_idx))276 self.output_shape_depend_on_compute.append(int(self.output_idx))
279 else:277 else:
280 return278 return
@@ -329,9 +327,7 @@ conflicts with the built-in variable name. Use a complex name or prefix the oper
329 if self.attr_val.get(attr) is None:327 if self.attr_val.get(attr) is None:
330 self.attr_val[attr] = {}328 self.attr_val[attr] = {}
331 if conf.startswith("attr_{}.type".format(attr)):329 if conf.startswith("attr_{}.type".format(attr)):
332- self.attr_val.get(attr)["type"] = self._camel_to_snake(330+ self.attr_val.get(attr)["type"] = self._camel_to_snake(self._parse_str(conf))
333- self._parse_str(conf)
334- )
335 elif conf.startswith("attr_{}.paramType".format(attr)):331 elif conf.startswith("attr_{}.paramType".format(attr)):
336 self.attr_val.get(attr)["paramType"] = self._parse_str(conf)332 self.attr_val.get(attr)["paramType"] = self._parse_str(conf)
337 elif conf.startswith("attr_{}.defaultValue".format(attr)):333 elif conf.startswith("attr_{}.defaultValue".format(attr)):
@@ -406,9 +402,7 @@ def _get_op_custom_options(op_descs: list, auto_gen_dir: str):
406 for line in lines:402 for line in lines:
407 param_list = str.split(line.rstrip("\n"), ",")403 param_list = str.split(line.rstrip("\n"), ",")
408 if len(param_list) != 3:404 if len(param_list) != 3:
409- raise Exception(405+ raise Exception(f"ERROR: custom compile option {param_list} len is not 3")
410- f"ERROR: custom compile option {param_list} len is not 3"
411- )
412 op_type = param_list[0]406 op_type = param_list[0]
413 if op_type.upper() == "ALL":407 if op_type.upper() == "ALL":
414 op_type = OP_ALL408 op_type = OP_ALL
@@ -427,22 +421,13 @@ def _get_op_custom_options(op_descs: list, auto_gen_dir: str):
427 short_ver = _trans_soc_ver_to_short(ver)421 short_ver = _trans_soc_ver_to_short(ver)
428 soc_ver_compile_options[short_ver] = options422 soc_ver_compile_options[short_ver] = options
429 if op_type == OP_ALL:423 if op_type == OP_ALL:
430- _set_all_options_to_opdescs(424+ _set_all_options_to_opdescs(op_descs, soc_ver_compile_options, tmp_impl_mode)
431- op_descs, soc_ver_compile_options, tmp_impl_mode
432- )
433 else:425 else:
434- _set_options_to_opdesc(426+ _set_options_to_opdesc(op_descs, op_type, soc_ver_compile_options, tmp_impl_mode)
435- op_descs, op_type, soc_ver_compile_options, tmp_impl_mode
436- )
437 427 
438 428 
439def get_op_desc(429def get_op_desc(
440- file: str,430+ file: str, batch_list: list, iterator_list: list, builder: any, op_type: list, auto_gen_dir: str = None
441- batch_list: list,
442- iterator_list: list,
443- builder: any,
444- op_type: list,
445- auto_gen_dir: str = None,
446) -> list:431) -> list:
447 op_descs = []432 op_descs = []
448 op_match = False433 op_match = False
@@ -160,10 +160,7 @@ def check_file_size(input_file):
160 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)))
161 raise OSError from os_error161 raise OSError from os_error
162 if file_size > 10 * 1024 * 1024:162 if file_size > 10 * 1024 * 1024:
163- print(163+ print("[WARN] The size of %s exceeds 10MB, it may take more time to run, please wait." % input_file)
164- "[WARN] The size of %s exceeds 10MB, it may take more time to run, please wait."
165- % input_file
166- )
167 164 
168 165 
169def parse_ini_to_obj(ini_file, tbe_ops_info):166def parse_ini_to_obj(ini_file, tbe_ops_info):
@@ -197,9 +194,7 @@ def parse_ini_to_obj(ini_file, tbe_ops_info):
197 if key1_0 not in op_dict:194 if key1_0 not in op_dict:
198 op_dict[key1_0] = {}195 op_dict[key1_0] = {}
199 if key1_1 in op_dict.get(key1_0):196 if key1_1 in op_dict.get(key1_0):
200- raise RuntimeError(197+ raise RuntimeError("Op:" + op_type + " " + key1_0 + " " + key1_1 + " is repeated!")
201- "Op:" + op_type + " " + key1_0 + " " + key1_1 + " is repeated!"
202- )
203 dic_key = op_dict.get(key1_0)198 dic_key = op_dict.get(key1_0)
204 dic_key[key1_1] = key2199 dic_key[key1_1] = key2
205 else:200 else:
@@ -271,10 +266,7 @@ def check_attr(op_dict, is_valid):
271 is_valid = check_attr_dict(attr_dict, is_valid, attr)266 is_valid = check_attr_dict(attr_dict, is_valid, attr)
272 else:267 else:
273 is_valid = False268 is_valid = False
274- print(269+ print("%s is required in .ini file, when attr.list is %s!" % (attr, attr_list_str))
275- "%s is required in .ini file, when attr.list is %s!"
276- % (attr, attr_list_str)
277- )
278 return is_valid270 return is_valid
279 271 
280 272 
@@ -323,11 +315,7 @@ def check_type_format(op_info, is_valid, op_info_key):
323 if op_info_dtype_num > 0 and op_info_format_num > 0:315 if op_info_dtype_num > 0 and op_info_format_num > 0:
324 if op_info_dtype_num != op_info_format_num:316 if op_info_dtype_num != op_info_format_num:
325 is_valid = False317 is_valid = False
326- print(318+ print("The number of {0}.dtype not match the number of {0}.format.".format(op_info_key))
327- "The number of {0}.dtype not match the number of {0}.format.".format(
328- op_info_key
329- )
330- )
331 return is_valid319 return is_valid
332 320 
333 321 
@@ -353,23 +341,12 @@ def check_op_info(tbe_ops):
353 if required_op_input_info_key not in op_input_info:341 if required_op_input_info_key not in op_input_info:
354 missing_keys.append(required_op_input_info_key)342 missing_keys.append(required_op_input_info_key)
355 if len(missing_keys) > 0:343 if len(missing_keys) > 0:
356- print(344+ print("op: " + op_key + " " + op_info_key + " missing: " + ",".join(missing_keys))
357- "op: "
358- + op_key
359- + " "
360- + op_info_key
361- + " missing: "
362- + ",".join(missing_keys)
363- )
364 is_valid = False345 is_valid = False
365 else:346 else:
366 if op_input_info["paramType"] not in param_type_valid_value:347 if op_input_info["paramType"] not in param_type_valid_value:
367 print(348 print(
368- "op: "349+ "op: " + op_key + " " + op_info_key + " paramType not valid, valid key:[dynamic, "
369- + op_key
370- + " "
371- + op_info_key
372- + " paramType not valid, valid key:[dynamic, "
373 "optional, required]"350 "optional, required]"
374 )351 )
375 is_valid = False352 is_valid = False
@@ -381,23 +358,12 @@ def check_op_info(tbe_ops):
381 if required_op_input_info_key not in op_input_info:358 if required_op_input_info_key not in op_input_info:
382 missing_keys.append(required_op_input_info_key)359 missing_keys.append(required_op_input_info_key)
383 if len(missing_keys) > 0:360 if len(missing_keys) > 0:
384- print(361+ print("op: " + op_key + " " + op_info_key + " missing: " + ",".join(missing_keys))
385- "op: "
386- + op_key
387- + " "
388- + op_info_key
389- + " missing: "
390- + ",".join(missing_keys)
391- )
392 is_valid = False362 is_valid = False
393 else:363 else:
394 if op_input_info["paramType"] not in param_type_valid_value:364 if op_input_info["paramType"] not in param_type_valid_value:
395 print(365 print(
396- "op: "366+ "op: " + op_key + " " + op_info_key + " paramType not valid, valid key:[dynamic, "
397- + op_key
398- + " "
399- + op_info_key
400- + " paramType not valid, valid key:[dynamic, "
401 "optional, required]"367 "optional, required]"
402 )368 )
403 is_valid = False369 is_valid = False
@@ -423,9 +389,7 @@ def write_json_file(tbe_ops_info, json_file_path):
423 with os.fdopen(os.open(json_file_real_path, wr_flag, wr_mode), "w") as file_path:389 with os.fdopen(os.open(json_file_real_path, wr_flag, wr_mode), "w") as file_path:
424 # The owner have all rights£¬group only have read rights390 # The owner have all rights£¬group only have read rights
425 os.chmod(json_file_real_path, stat.S_IWUSR + stat.S_IRGRP + stat.S_IRUSR)391 os.chmod(json_file_real_path, stat.S_IWUSR + stat.S_IRGRP + stat.S_IRUSR)
426- json.dump(392+ json.dump(tbe_ops_info, file_path, sort_keys=True, indent=4, separators=(",", ":"))
427- tbe_ops_info, file_path, sort_keys=True, indent=4, separators=(",", ":")
428- )
429 print("Compile op info cfg successfully.")393 print("Compile op info cfg successfully.")
430 394 
431 395 
@@ -45,41 +45,29 @@ def _get_binary_json_op_list(binary_json):
45def _check_binary_json(binary_json, expected_op_type):45def _check_binary_json(binary_json, expected_op_type):
46 op_list = _get_binary_json_op_list(binary_json)46 op_list = _get_binary_json_op_list(binary_json)
47 if binary_json.get("op_type") != expected_op_type:47 if binary_json.get("op_type") != expected_op_type:
48- raise ValueError(48+ raise ValueError(f"op_type: expected '{expected_op_type}', got '{binary_json.get('op_type')}'")
49- f"op_type: expected '{expected_op_type}', got '{binary_json.get('op_type')}'"
50- )
51 49 
52 bin_filenames = set()50 bin_filenames = set()
53 simplified_keys = set()51 simplified_keys = set()
54 for index, op in enumerate(op_list):52 for index, op in enumerate(op_list):
55 op_path = f"op_list[{index}]"53 op_path = f"op_list[{index}]"
56 bin_filename = op.get("bin_filename")54 bin_filename = op.get("bin_filename")
57- if (55+ if not isinstance(bin_filename, str) or not bin_filename or os.path.basename(bin_filename) != bin_filename:
58- not isinstance(bin_filename, str)
59- or not bin_filename
60- or os.path.basename(bin_filename) != bin_filename
61- ):
62 raise ValueError(f"{op_path}.bin_filename: invalid value '{bin_filename}'")56 raise ValueError(f"{op_path}.bin_filename: invalid value '{bin_filename}'")
63 output_name = os.path.splitext(bin_filename)[0]57 output_name = os.path.splitext(bin_filename)[0]
64- if output_name != expected_op_type and not output_name.startswith(58+ if output_name != expected_op_type and not output_name.startswith(f"{expected_op_type}_"):
65- f"{expected_op_type}_"
66- ):
67 raise ValueError(59 raise ValueError(
68 f"{op_path}.bin_filename: expected '{expected_op_type}' or "60 f"{op_path}.bin_filename: expected '{expected_op_type}' or "
69 f"prefix '{expected_op_type}_', got '{bin_filename}'"61 f"prefix '{expected_op_type}_', got '{bin_filename}'"
70 )62 )
71 if output_name in bin_filenames:63 if output_name in bin_filenames:
72- raise ValueError(64+ raise ValueError(f"{op_path}.bin_filename: duplicate output '{output_name}'")
73- f"{op_path}.bin_filename: duplicate output '{output_name}'"
74- )
75 bin_filenames.add(output_name)65 bin_filenames.add(output_name)
76 simplified_key = op.get("simplified_key")66 simplified_key = op.get("simplified_key")
77 if not isinstance(simplified_key, str) or not simplified_key:67 if not isinstance(simplified_key, str) or not simplified_key:
78 raise ValueError(f"{op_path}.simplified_key: expected non-empty string")68 raise ValueError(f"{op_path}.simplified_key: expected non-empty string")
79 if simplified_key in simplified_keys:69 if simplified_key in simplified_keys:
80- raise ValueError(70+ raise ValueError(f"{op_path}.simplified_key: duplicate value '{simplified_key}'")
81- f"{op_path}.simplified_key: duplicate value '{simplified_key}'"
82- )
83 simplified_keys.add(simplified_key)71 simplified_keys.add(simplified_key)
84 72 
85 73 
@@ -0,0 +1,11 @@
1+#!/usr/bin/env python
2+# -*- coding: utf-8 -*-
3+# ----------------------------------------------------------------------------------------------------------
4+# Copyright (c) 2025 Huawei Technologies Co., Ltd.
5+# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
6+# CANN Open Software License Agreement Version 2.0 (the "License").
7+# Please refer to the License for details. You may not use this file except in compliance with the License.
8+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
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.
11+# ----------------------------------------------------------------------------------------------------------
@@ -68,7 +68,7 @@ def v310_mode_vec_ofile(little_endian, binary_32) -> int:
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
@@ -76,7 +76,7 @@ def v310_mode_vec_ofile(little_endian, binary_32) -> int:
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]
@@ -88,17 +88,10 @@ def v310_mode_vec_ofile(little_endian, binary_32) -> int:
88 (88 (
89 little_endian[0] == "1"89 little_endian[0] == "1"
90 and little_endian[1] == "5"90 and little_endian[1] == "5"
91- and not (91+ and not (binary_32[:11] == "00010101110" and binary_32[16:26] == "0000000000" and binary_32[27:] == "10011")
92- binary_32[:11] == "00010101110"
93- and binary_32[16:26] == "0000000000"
94- and binary_32[27:] == "10011"
95- )
96 ),92 ),
97 # set, wait93 # set, wait
98- (94+ (binary_32[:17] in vf_high_low_map and binary_32[30:] == vf_high_low_map[binary_32[:17]]),
99- binary_32[:17] in vf_high_low_map
100- and binary_32[30:] == vf_high_low_map[binary_32[:17]]
101- ),
102 # DMA95 # DMA
103 (high_10 in vec_high_low_map and low_1 == vec_high_low_map[high_10]),96 (high_10 in vec_high_low_map and low_1 == vec_high_low_map[high_10]),
104 (high_10 in vec_high_map),97 (high_10 in vec_high_map),
@@ -144,31 +137,14 @@ def v310_mode_cube_ofile(little_endian, binary_32) -> int:
144 # Fixpipe137 # Fixpipe
145 (little_endian[0] == "c" and little_endian[1] in "0123"),138 (little_endian[0] == "c" and little_endian[1] in "0123"),
146 # matrix instr139 # matrix instr
147- (140+ (little_endian[0] == "e" and little_endian[1] in "012345" and binary_32[30:] == "00"),
148- little_endian[0] == "e"
149- and little_endian[1] in "012345"
150- and binary_32[30:] == "00"
151- ),
152 (little_endian[0] == "f" and little_endian[1] in "01"),141 (little_endian[0] == "f" and little_endian[1] in "01"),
153- (142+ (little_endian[0] == "f" and little_endian[1] == "6" and binary_32[30:] == "00"),
154- little_endian[0] == "f"143+ (little_endian[0] == "f" and little_endian[1] in "2345abcd" and binary_32[30] == "0"),
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- ),
163 # DMA144 # DMA
164- (145+ (high_9 == "011100100" and mid_36 in ("0001", "0101")), # DMA move inst, include MOV L1 TO UB
165- high_9 == "011100100" and mid_36 in ("0001", "0101")
166- ), # DMA move inst, include MOV L1 TO UB
167 (high_10 in cube_high_low_map and binary_32[30:] == cube_high_low_map[high_10]),146 (high_10 in cube_high_low_map and binary_32[30:] == cube_high_low_map[high_10]),
168- (147+ (high_10 in cube_high_low2_map and binary_32[31] == cube_high_low2_map[high_10]),
169- high_10 in cube_high_low2_map
170- and binary_32[31] == cube_high_low2_map[high_10]
171- ),
172 (high_10 in cube_high_map),148 (high_10 in cube_high_map),
173 ]149 ]
174 150 
@@ -185,24 +161,16 @@ def v310_mode(inst, cubemode) -> int:
185 little_endian = f"{int.from_bytes(int(inst, 16).to_bytes(4, 'little'), 'big'):08x}"161 little_endian = f"{int.from_bytes(int(inst, 16).to_bytes(4, 'little'), 'big'):08x}"
186 binary_32 = bin(int(little_endian, 16))[2:].zfill(32)162 binary_32 = bin(int(little_endian, 16))[2:].zfill(32)
187 163 
188- return (164+ return v310_mode_cube_ofile(little_endian, binary_32) if cubemode else v310_mode_vec_ofile(little_endian, binary_32)
189- v310_mode_cube_ofile(little_endian, binary_32)
190- if cubemode
191- else v310_mode_vec_ofile(little_endian, binary_32)
192- )
193 165 
194 166 
195-def get_code_channel(167+def get_code_channel(dst_file: str, c310mode_cubemode_tuple: Tuple) -> Tuple[bool, int, str]:
196- dst_file: str, c310mode_cubemode_tuple: Tuple
197-) -> Tuple[bool, int, str]:
198 c310mode, cubemode = c310mode_cubemode_tuple168 c310mode, cubemode = c310mode_cubemode_tuple
199 if not os.path.isfile(dst_file):169 if not os.path.isfile(dst_file):
200 return False, CODE_DEFAULT, f"file {dst_file} doesn't exist."170 return False, CODE_DEFAULT, f"file {dst_file} doesn't exist."
201 171 
202 objdump_cmd = ["llvm-objdump", "-s", "-j", ".text", dst_file]172 objdump_cmd = ["llvm-objdump", "-s", "-j", ".text", dst_file]
203- proc = subprocess.run(173+ proc = subprocess.run(objdump_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False)
204- objdump_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False
205- )
206 out = proc.stdout.decode()174 out = proc.stdout.decode()
207 if proc.returncode != 0:175 if proc.returncode != 0:
208 return False, CODE_DEFAULT, f"llvm-objdump error, message is {out}"176 return False, CODE_DEFAULT, f"llvm-objdump error, message is {out}"
@@ -61,10 +61,6 @@ STR_TO_KERNEL_TYPE_C310 = {
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 
64-STR_TO_KERNEL_TYPE_L300 = {64+STR_TO_KERNEL_TYPE_L300 = {"KERNEL_TYPE_AICORE": CodeMode.AIC}
65- "KERNEL_TYPE_AICORE": CodeMode.AIC,
66-}
67 65 
68-STR_TO_KERNEL_TYPE_L311 = {66+STR_TO_KERNEL_TYPE_L311 = {"KERNEL_TYPE_AICORE": CodeMode.AIC}
69- "KERNEL_TYPE_AICORE": CodeMode.AIC,
70-}
@@ -63,14 +63,9 @@ PROPERTIES COMPILE_OPTIONS {disable_kernel_check_option})\n"
63 return config_options63 return config_options
64 64 
65 65 
66-def generate_save_template_config_commands(66+def generate_save_template_config_commands(filepaths: List[str], dst_dir: str) -> Iterator[Tuple[str, str]]:
67- filepaths: List[str], dst_dir: str
68-) -> Iterator[Tuple[str, str]]:
69 """Generate save template_config commands."""67 """Generate save template_config commands."""
70- yield (68+ yield (get_template_config_filepath(dst_dir), generate_template_config_code(filepaths))
71- get_template_config_filepath(dst_dir),
72- generate_template_config_code(filepaths),
73- )
74 69 
75 70 
76def main(argv: List[str]):71def main(argv: List[str]):
@@ -39,12 +39,7 @@ def extract_archive(archive_path: str, dst_dir: str):
39 39 
40def get_objects_in_archive(archive_path: str) -> List[str]:40def get_objects_in_archive(archive_path: str) -> List[str]:
41 """Get objects in archive."""41 """Get objects in archive."""
42- result = subprocess.run(42+ result = subprocess.run(["ar", "-t", archive_path], check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
43- ["ar", "-t", archive_path],
44- check=True,
45- stdout=subprocess.PIPE,
46- stderr=subprocess.STDOUT,
47- )
48 display = result.stdout.decode()43 display = result.stdout.decode()
49 return display.split()44 return display.split()
50 45 
@@ -77,9 +72,7 @@ def main(argv):
77 72 
78 objects = get_objects_in_archive(args.archive)73 objects = get_objects_in_archive(args.archive)
79 74 
80- cmake_config_content = generate_cmake_config_content(75+ cmake_config_content = generate_cmake_config_content(args.target, args.dst_dir, objects)
81- args.target, args.dst_dir, objects
82- )
83 76 
84 save_file(os.path.join(args.dst_dir, args.cmake), cmake_config_content)77 save_file(os.path.join(args.dst_dir, args.cmake), cmake_config_content)
85 78 
@@ -134,9 +134,7 @@ def generate_host_stub_head_code(has_mix: bool, has_aic: bool, has_aiv: bool) ->
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(137+ buff.write('} __replaced_ascend_kernel __attribute__ ((section ("__replaced_ascend_section"))) = {')
138- '} __replaced_ascend_kernel __attribute__ ((section ("__replaced_ascend_section"))) = {'
139- )
140 138 
141 ascend_kernel_str = ",".join(ascend_kernel_struct)139 ascend_kernel_str = ",".join(ascend_kernel_struct)
142 buff.write(f"{ascend_kernel_str}")140 buff.write(f"{ascend_kernel_str}")
@@ -152,35 +152,25 @@ def main():
152 all_merge_list = []152 all_merge_list = []
153 153 
154 if args.normal_dir:154 if args.normal_dir:
155- normal_objs, merge_normal = get_normal_obj_info(155+ normal_objs, merge_normal = get_normal_obj_info(args.normal_dir, "mix_build.flag", args.output)
156- args.normal_dir, "mix_build.flag", args.output
157- )
158 all_source_list.extend(normal_objs)156 all_source_list.extend(normal_objs)
159 all_merge_list.extend(merge_normal)157 all_merge_list.extend(merge_normal)
160 else:158 else:
161- aic_objs, merge_aic = get_normal_obj_info(159+ aic_objs, merge_aic = get_normal_obj_info(args.aic_dir, "aic_build.flag", args.output)
162- args.aic_dir, "aic_build.flag", args.output
163- )
164 all_source_list.extend(aic_objs)160 all_source_list.extend(aic_objs)
165 all_merge_list.extend(merge_aic)161 all_merge_list.extend(merge_aic)
166 162 
167- aiv_objs, merge_aiv = get_normal_obj_info(163+ aiv_objs, merge_aiv = get_normal_obj_info(args.aiv_dir, "aiv_build.flag", args.output)
168- args.aiv_dir, "aiv_build.flag", args.output
169- )
170 all_source_list.extend(aiv_objs)164 all_source_list.extend(aiv_objs)
171 all_merge_list.extend(merge_aiv)165 all_merge_list.extend(merge_aiv)
172 166 
173- mix_objs, merge_mix = get_mix_obj_info(167+ mix_objs, merge_mix = get_mix_obj_info(args.aic_dir, args.aiv_dir, "mix_build.flag", args.output)
174- args.aic_dir, args.aiv_dir, "mix_build.flag", args.output
175- )
176 all_source_list.extend(mix_objs)168 all_source_list.extend(mix_objs)
177 all_merge_list.extend(merge_mix)169 all_merge_list.extend(merge_mix)
178 170 
179 if args.fatbin:171 if args.fatbin:
180 output_file = os.path.join(args.output, args.name)172 output_file = os.path.join(args.output, args.name)
181- run_merge_cmd(173+ run_merge_cmd(args.script, args.linker, args.build_type, all_source_list, output_file)
182- args.script, args.linker, args.build_type, all_source_list, output_file
183- )
184 else:174 else:
185 single_obj_merge(args.script, args.linker, args.build_type, all_merge_list)175 single_obj_merge(args.script, args.linker, args.build_type, all_merge_list)
186 176 
@@ -55,9 +55,7 @@ def read_file(source_file):
55def get_link_cmd(link_file):55def get_link_cmd(link_file):
56 contents = read_file(link_file)56 contents = read_file(link_file)
57 if not contents:57 if not contents:
58- raise TooFewLinkCmd(58+ raise TooFewLinkCmd(f"There are too few compilation commands in the file: {link_file}")
59- f"There are too few compilation commands in the file: {link_file}"
60- )
61 return contents59 return contents
62 60 
63 61 
@@ -85,9 +83,7 @@ def run_recompile_cmd(root_dir, compile_cmd):
85 print(f"recompile: {compile_cmd}", flush=True)83 print(f"recompile: {compile_cmd}", flush=True)
86 result = subprocess.run(cmds, check=True, cwd=root_dir)84 result = subprocess.run(cmds, check=True, cwd=root_dir)
87 if result.returncode != 0:85 if result.returncode != 0:
88- raise CompileCMDError(86+ raise CompileCMDError(f"recompile command failed, return code: {result.returncode}")
89- f"recompile command failed, return code: {result.returncode}"
90- )
91 87 
92 88 
93def parse_args():89def parse_args():
@@ -95,9 +91,7 @@ def parse_args():
95 parser = argparse.ArgumentParser()91 parser = argparse.ArgumentParser()
96 parser.add_argument("--root-dir", required=True, help="target root directory")92 parser.add_argument("--root-dir", required=True, help="target root directory")
97 parser.add_argument("--target-name", required=True, help="target name")93 parser.add_argument("--target-name", required=True, help="target name")
98- parser.add_argument(94+ parser.add_argument("--add-dir", required=True, help="the directory where the added obj is located")
99- "--add-dir", required=True, help="the directory where the added obj is located"
100- )
101 args = parser.parse_args()95 args = parser.parse_args()
102 return args96 return args
103 97 
@@ -32,9 +32,7 @@ def get_ascendc_section(soc_version: str, target_name: str) -> str:
32 return rf".ascend.kernel.{soc_version}.{target_name}"32 return rf".ascend.kernel.{soc_version}.{target_name}"
33 33 
34 34 
35-def update_source_section(35+def update_source_section(origin_content: str, soc_version: str, target_name: str) -> str:
36- origin_content: str, soc_version: str, target_name: str
37-) -> str:
38 update_content = origin_content36 update_content = origin_content
39 37 
40 ascendc_kernel = get_ascendc_kernel(soc_version, target_name)38 ascendc_kernel = get_ascendc_kernel(soc_version, target_name)
@@ -46,9 +44,7 @@ def update_source_section(
46 update_content = re.sub(replaced_ascend_section, ascendc_section, update_content)44 update_content = re.sub(replaced_ascend_section, ascendc_section, update_content)
47 45 
48 replaced_ascend_kernel_soc_version = "__replaced_ascend_compile_soc_version"46 replaced_ascend_kernel_soc_version = "__replaced_ascend_compile_soc_version"
49- update_content = re.sub(47+ update_content = re.sub(replaced_ascend_kernel_soc_version, soc_version, update_content)
50- replaced_ascend_kernel_soc_version, soc_version, update_content
51- )
52 48 
53 return update_content49 return update_content
54 50 
@@ -12,12 +12,6 @@
12 12 
13 13 
14__version__ = "0.1.0"14__version__ = "0.1.0"
15-__all__ = [15+__all__ = ["markdown_to_html", "gfm_to_html", "parse_file", "parse_string", "render_html"]
16- "markdown_to_html",
17- "gfm_to_html",
18- "parse_file",
19- "parse_string",
20- "render_html",
21-]
22 16 
23from .parser import markdown_to_html, gfm_to_html, parse_file, parse_string, render_html17from .parser import markdown_to_html, gfm_to_html, parse_file, parse_string, render_html
@@ -20,43 +20,22 @@ 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(23+logging.basicConfig(format="%(asctime)s [%(levelname)s] %(message)s", level=logging.INFO)
24- format="%(asctime)s [%(levelname)s] %(message)s", level=logging.INFO
25-)
26logger = logging.getLogger("mdparser")24logger = logging.getLogger("mdparser")
27 25 
28-_STATIC_EXTS = {26+_STATIC_EXTS = {".png", ".jpg", ".jpeg", ".gif", ".svg", ".ico", ".webp", ".bmp", ".pdf", ".zip", ".tar.gz"}
29- ".png",
30- ".jpg",
31- ".jpeg",
32- ".gif",
33- ".svg",
34- ".ico",
35- ".webp",
36- ".bmp",
37- ".pdf",
38- ".zip",
39- ".tar.gz",
40-}
41 27 
42 28 
43def _build_parser():29def _build_parser():
44 parser = argparse.ArgumentParser(30 parser = argparse.ArgumentParser(
45- prog="mdparser",31+ prog="mdparser", 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).",
47- )
48- parser.add_argument(
49- "input", nargs="*", help="Markdown file(s) or directory to parse"
50 )32 )
33+ parser.add_argument("input", nargs="*", help="Markdown file(s) or directory to parse")
51 parser.add_argument("-o", "--output", help="Output file or directory")34 parser.add_argument("-o", "--output", help="Output file or directory")
52 parser.add_argument("--no-gfm", action="store_true", help="Disable GFM extensions")35 parser.add_argument("--no-gfm", action="store_true", help="Disable GFM extensions")
53 parser.add_argument("--stdin", action="store_true", help="Read from stdin")36 parser.add_argument("--stdin", action="store_true", help="Read from stdin")
54- parser.add_argument(37+ parser.add_argument("-s", "--style", action="store_true", help="Embed GitHub-style CSS")
55- "-s", "--style", action="store_true", help="Embed GitHub-style CSS"38+ parser.add_argument("-r", "--recursive", action="store_true", help="Scan recursively")
56- )
57- parser.add_argument(
58- "-r", "--recursive", action="store_true", help="Scan recursively"
59- )
60 return parser39 return parser
61 40 
62 41 
@@ -31,17 +31,9 @@ def _load_css():
31 if _CSS is not None:31 if _CSS is not None:
32 return _CSS32 return _CSS
33 candidates = [33 candidates = [
34- pathlib.Path(__file__).parent.parent.parent34+ pathlib.Path(__file__).parent.parent.parent / "node_modules" / "github-markdown-css" / "github-markdown.css",
35- / "node_modules"35+ pathlib.Path(__file__).parent.parent / "node_modules" / "github-markdown-css" / "github-markdown.css",
36- / "github-markdown-css"36+ pathlib.Path("/home/zjj/md-parser/node_modules/github-markdown-css/github-markdown.css"),
37- / "github-markdown.css",
38- pathlib.Path(__file__).parent.parent
39- / "node_modules"
40- / "github-markdown-css"
41- / "github-markdown.css",
42- pathlib.Path(
43- "/home/zjj/md-parser/node_modules/github-markdown-css/github-markdown.css"
44- ),
45 ]37 ]
46 for p in candidates:38 for p in candidates:
47 if p.exists():39 if p.exists():
@@ -55,32 +47,17 @@ _OPTIONS = cmarkgfm.Options.CMARK_OPT_UNSAFE
55 47 
56_MD_LINK_RE = re.compile(r'(href|src)="((?!https?:|//)[^"]+)\.md(#[^"]*)?"')48_MD_LINK_RE = re.compile(r'(href|src)="((?!https?:|//)[^"]+)\.md(#[^"]*)?"')
57 49 
58-_CANN_OPEN_RE = re.compile(50+_CANN_OPEN_RE = re.compile(r'<cann-filter\b[^>]*npu[_-]type\s*=\s*"([^"]+)"[^>]*>', re.IGNORECASE)
59- r'<cann-filter\b[^>]*npu[_-]type\s*=\s*"([^"]+)"[^>]*>',
60- re.IGNORECASE,
61-)
62 51 
63-_CANN_CLOSE_RE = re.compile(52+_CANN_CLOSE_RE = re.compile(r"</cann-filter\b[^>]*>", re.IGNORECASE)
64- r"</cann-filter\b[^>]*>",
65- re.IGNORECASE,
66-)
67 53 
68_OTHER_TAG_NAMES = r"term|ph|__gm__|__ubuf__"54_OTHER_TAG_NAMES = r"term|ph|__gm__|__ubuf__"
69 55 
70-_OTHER_TAG_LINE_RE = re.compile(56+_OTHER_TAG_LINE_RE = re.compile(r"^[ \t]*</?(" + _OTHER_TAG_NAMES + r")\b[^>]*>[ \t]*$", re.IGNORECASE)
71- r"^[ \t]*</?(" + _OTHER_TAG_NAMES + r")\b[^>]*>[ \t]*$",
72- re.IGNORECASE,
73-)
74 57 
75-_OTHER_TAG_WRAP_RE = re.compile(58+_OTHER_TAG_WRAP_RE = re.compile(r"<(" + _OTHER_TAG_NAMES + r")\b[^>]*>([\s\S]*?)</\1>", re.IGNORECASE)
76- r"<(" + _OTHER_TAG_NAMES + r")\b[^>]*>([\s\S]*?)</\1>",
77- re.IGNORECASE,
78-)
79 59 
80-_OTHER_TAG_ANY_RE = re.compile(60+_OTHER_TAG_ANY_RE = re.compile(r"</?(" + _OTHER_TAG_NAMES + r")\b[^>]*>", re.IGNORECASE)
81- r"</?(" + _OTHER_TAG_NAMES + r")\b[^>]*>",
82- re.IGNORECASE,
83-)
84 61 
85 62 
86_FENCE_OPEN_RE = re.compile(r"^[ \t]{0,3}(`{3,}|~{3,})", re.MULTILINE)63_FENCE_OPEN_RE = re.compile(r"^[ \t]{0,3}(`{3,}|~{3,})", re.MULTILINE)
@@ -98,19 +75,12 @@ _HTML_BLOCK_TAG_NAMES = (
98 r"search|section|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul"75 r"search|section|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul"
99)76)
100 77 
101-_HTML_BLOCK_TAG_RE = re.compile(78+_HTML_BLOCK_TAG_RE = re.compile(rf"^</?(?:{_HTML_BLOCK_TAG_NAMES})(?:[ \t\n\f\r]|/?>)", re.IGNORECASE)
102- rf"^</?(?:{_HTML_BLOCK_TAG_NAMES})(?:[ \t\n\f\r]|/?>)",
103- re.IGNORECASE,
104-)
105 79 
106-_HTML_BLOCK_TYPE1_RE = re.compile(80+_HTML_BLOCK_TYPE1_RE = re.compile(r"^<(script|pre|style|textarea)(?:[ \t\n\f\r]|>|$)", re.IGNORECASE)
107- r"^<(script|pre|style|textarea)(?:[ \t\n\f\r]|>|$)",
108- re.IGNORECASE,
109-)
110 81 
111_HTML_BLOCK_TYPE1_END_RE = {82_HTML_BLOCK_TYPE1_END_RE = {
112- tag: re.compile(rf"</{tag}[ \t]*>", re.IGNORECASE)83+ tag: re.compile(rf"</{tag}[ \t]*>", re.IGNORECASE) for tag in ("script", "pre", "style", "textarea")
113- for tag in ("script", "pre", "style", "textarea")
114}84}
115 85 
116_HTML_BLOCK_SPECIAL_RE = (86_HTML_BLOCK_SPECIAL_RE = (
@@ -183,9 +153,7 @@ def _escape_lone_tildes(md_text: str) -> str:
183 153 
184 if in_fence:154 if in_fence:
185 result.append(line)155 result.append(line)
186- if fence_char and re.match(156+ if fence_char and re.match(rf"^{re.escape(fence_char)}{{3,}}[ \t]*$", stripped):
187- rf"^{re.escape(fence_char)}{{3,}}[ \t]*$", stripped
188- ):
189 in_fence = False157 in_fence = False
190 fence_char = None158 fence_char = None
191 previous_line_blank = not stripped159 previous_line_blank = not stripped
@@ -248,9 +216,7 @@ def _split_by_fences(md_text: str):
248 216 
249 if is_code:217 if is_code:
250 current.append(line)218 current.append(line)
251- if fence_char and re.match(219+ if fence_char and re.match(rf"^{re.escape(fence_char)}{{3,}}[ \t]*$", stripped):
252- rf"^{re.escape(fence_char)}{{3,}}[ \t]*$", stripped
253- ):
254 is_code = False220 is_code = False
255 fence_char = None221 fence_char = None
256 segments.append((True, "\n".join(current)))222 segments.append((True, "\n".join(current)))
@@ -355,8 +321,7 @@ def _strip_other_tags(md_text: str) -> str:
355 321 
356 322 
357_CALLOUT_RE = re.compile(323_CALLOUT_RE = re.compile(
358- r"<blockquote>\s*\n<p>\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\](.*?)</p>\s*\n(.*?)</blockquote>",324+ r"<blockquote>\s*\n<p>\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\](.*?)</p>\s*\n(.*?)</blockquote>", re.DOTALL
359- re.DOTALL,
360)325)
361 326 
362_CALLOUT_ICONS = {327_CALLOUT_ICONS = {
@@ -519,9 +484,7 @@ def _fix_links(html: str, source_path=None) -> str:
519 def _replace_markdown_link(match):484 def _replace_markdown_link(match):
520 link_path = match.group(2)485 link_path = match.group(2)
521 if match.group(1).lower() == "href" and repo_root is not None:486 if match.group(1).lower() == "href" and repo_root is not None:
522- target = (487+ target = (pathlib.Path(source_path).resolve().parent / unquote(link_path)).resolve()
523- pathlib.Path(source_path).resolve().parent / unquote(link_path)
524- ).resolve()
525 if _is_within(target, repo_root) and not _is_within(target, docs_root):488 if _is_within(target, repo_root) and not _is_within(target, docs_root):
526 return match.group(0)489 return match.group(0)
527 return f'{match.group(1)}="{link_path}.html{match.group(3) or ""}"'490 return f'{match.group(1)}="{link_path}.html{match.group(3) or ""}"'
@@ -536,13 +499,9 @@ def _fix_callouts(html: str) -> str:
536 title = m.group(2).strip()499 title = m.group(2).strip()
537 body_content = m.group(3).strip()500 body_content = m.group(3).strip()
538 icon = _CALLOUT_ICONS.get(callout_type, "")501 icon = _CALLOUT_ICONS.get(callout_type, "")
539- type_label = dict(502+ type_label = dict(NOTE="Note", TIP="Tip", IMPORTANT="Important", WARNING="Warning", CAUTION="Caution").get(
540- NOTE="Note",503+ callout_type, callout_type
541- TIP="Tip",504+ )
542- IMPORTANT="Important",
543- WARNING="Warning",
544- CAUTION="Caution",
545- ).get(callout_type, callout_type)
546 if not body_content and "\n" in title:505 if not body_content and "\n" in title:
547 title, body_content = title.split("\n", 1)506 title, body_content = title.split("\n", 1)
548 title = title.strip()507 title = title.strip()
@@ -607,9 +566,7 @@ def _get_pygments_css() -> str:
607 global _PYGMENTS_CSS566 global _PYGMENTS_CSS
608 if _PYGMENTS_CSS is not None:567 if _PYGMENTS_CSS is not None:
609 return _PYGMENTS_CSS568 return _PYGMENTS_CSS
610- _PYGMENTS_CSS = HtmlFormatter(style="default").get_style_defs(569+ _PYGMENTS_CSS = HtmlFormatter(style="default").get_style_defs(".markdown-body .highlight")
611- ".markdown-body .highlight"
612- )
613 return _PYGMENTS_CSS570 return _PYGMENTS_CSS
614 571 
615 572 
@@ -670,10 +627,7 @@ _HIGHLIGHT_CSS = """
670}627}
671"""628"""
672 629 
673-_CODEFENCE_RE = re.compile(630+_CODEFENCE_RE = re.compile(r'<pre(?:\s+lang="([^"]*)")?>\s*<code>(.*?)</code>\s*</pre>', re.DOTALL)
674- r'<pre(?:\s+lang="([^"]*)")?>\s*<code>(.*?)</code>\s*</pre>',
675- re.DOTALL,
676-)
677 631 
678 632 
679def _highlight_code(html: str) -> str:633def _highlight_code(html: str) -> str:
@@ -45,10 +45,7 @@ class MarkdownLinkTests(unittest.TestCase):
45 repo = pathlib.Path(temp_dir)45 repo = pathlib.Path(temp_dir)
46 source = repo / "docs" / "en" / "page.md"46 source = repo / "docs" / "en" / "page.md"
47 source.parent.mkdir(parents=True)47 source.parent.mkdir(parents=True)
48- source.write_text(48+ source.write_text("[internal](./other.md)\n[root](../../CONTRIBUTING_en.md)\n", encoding="utf-8")
49- "[internal](./other.md)\n[root](../../CONTRIBUTING_en.md)\n",
50- encoding="utf-8",
51- )
52 49 
53 html = parse_file(str(source))50 html = parse_file(str(source))
54 51 
@@ -24,9 +24,7 @@ ERROR_TOL = 1e-4
24def verify_result(output, golden):24def verify_result(output, golden):
25 output = np.fromfile(output, dtype=np.float32).reshape(-1)25 output = np.fromfile(output, dtype=np.float32).reshape(-1)
26 golden = np.fromfile(golden, dtype=np.float32).reshape(-1)26 golden = np.fromfile(golden, dtype=np.float32).reshape(-1)
27- different_element_results = np.isclose(27+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
28- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
29- )
30 different_element_indexes = np.where(different_element_results == False)[0]28 different_element_indexes = np.where(different_element_results == False)[0]
31 for index in range(len(different_element_indexes)):29 for index in range(len(different_element_indexes)):
32 real_index = different_element_indexes[index]30 real_index = different_element_indexes[index]
@@ -34,12 +32,7 @@ def verify_result(output, golden):
34 output_data = output[real_index]32 output_data = output[real_index]
35 print(33 print(
36 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"34 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"
37- % (35+ % (real_index, golden_data, output_data, abs(output_data - golden_data) / golden_data)
38- real_index,
39- golden_data,
40- output_data,
41- abs(output_data - golden_data) / golden_data,
42- )
43 )36 )
44 if index == 100:37 if index == 100:
45 break38 break
@@ -22,9 +22,7 @@ def gen_golden_data():
22 k = 12822 k = 128
23 x1_gm = np.random.randint(1, 10, [m, k]).astype(np.float16)23 x1_gm = np.random.randint(1, 10, [m, k]).astype(np.float16)
24 x2_gm = np.random.randint(1, 10, [k, n]).astype(np.float16)24 x2_gm = np.random.randint(1, 10, [k, n]).astype(np.float16)
25- golden = np.matmul(x1_gm.astype(np.float32), x2_gm.astype(np.float32)).astype(25+ golden = np.matmul(x1_gm.astype(np.float32), x2_gm.astype(np.float32)).astype(np.float32)
26- np.float32
27- )
28 os.makedirs("input", exist_ok=True)26 os.makedirs("input", exist_ok=True)
29 os.makedirs("output", exist_ok=True)27 os.makedirs("output", exist_ok=True)
30 x1_gm.tofile("./input/x1_gm.bin")28 x1_gm.tofile("./input/x1_gm.bin")
@@ -24,9 +24,7 @@ ERROR_TOL = 1e-4
24def verify_result(output, golden):24def verify_result(output, golden):
25 output = np.fromfile(output, dtype=np.float32).reshape(-1)25 output = np.fromfile(output, dtype=np.float32).reshape(-1)
26 golden = np.fromfile(golden, dtype=np.float32).reshape(-1)26 golden = np.fromfile(golden, dtype=np.float32).reshape(-1)
27- different_element_results = np.isclose(27+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
28- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
29- )
30 different_element_indexes = np.where(different_element_results == False)[0]28 different_element_indexes = np.where(different_element_results == False)[0]
31 for index in range(len(different_element_indexes)):29 for index in range(len(different_element_indexes)):
32 real_index = different_element_indexes[index]30 real_index = different_element_indexes[index]
@@ -34,12 +32,7 @@ def verify_result(output, golden):
34 output_data = output[real_index]32 output_data = output[real_index]
35 print(33 print(
36 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"34 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"
37- % (35+ % (real_index, golden_data, output_data, abs(output_data - golden_data) / golden_data)
38- real_index,
39- golden_data,
40- output_data,
41- abs(output_data - golden_data) / golden_data,
42- )
43 )36 )
44 if index == 100:37 if index == 100:
45 break38 break
@@ -22,9 +22,7 @@ def gen_golden_data():
22 k = 6422 k = 64
23 x1_gm = np.random.randint(-10, 10, [m, k]).astype(np.float16)23 x1_gm = np.random.randint(-10, 10, [m, k]).astype(np.float16)
24 x2_gm = np.random.randint(-10, 10, [k, n]).astype(np.float16)24 x2_gm = np.random.randint(-10, 10, [k, n]).astype(np.float16)
25- golden = np.matmul(x1_gm.astype(np.float32), x2_gm.astype(np.float32)).astype(25+ golden = np.matmul(x1_gm.astype(np.float32), x2_gm.astype(np.float32)).astype(np.float16)
26- np.float16
27- )
28 os.makedirs("input", exist_ok=True)26 os.makedirs("input", exist_ok=True)
29 os.makedirs("output", exist_ok=True)27 os.makedirs("output", exist_ok=True)
30 x1_gm.tofile("./input/x1_gm.bin")28 x1_gm.tofile("./input/x1_gm.bin")
@@ -24,9 +24,7 @@ ERROR_TOL = 1e-4
24def verify_result(output, golden):24def verify_result(output, golden):
25 output = np.fromfile(output, dtype=np.float16).reshape(-1)25 output = np.fromfile(output, dtype=np.float16).reshape(-1)
26 golden = np.fromfile(golden, dtype=np.float16).reshape(-1)26 golden = np.fromfile(golden, dtype=np.float16).reshape(-1)
27- different_element_results = np.isclose(27+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
28- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
29- )
30 different_element_indexes = np.where(different_element_results == False)[0]28 different_element_indexes = np.where(different_element_results == False)[0]
31 for index in range(len(different_element_indexes)):29 for index in range(len(different_element_indexes)):
32 real_index = different_element_indexes[index]30 real_index = different_element_indexes[index]
@@ -34,12 +32,7 @@ def verify_result(output, golden):
34 output_data = output[real_index]32 output_data = output[real_index]
35 print(33 print(
36 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"34 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"
37- % (35+ % (real_index, golden_data, output_data, abs(output_data - golden_data) / golden_data)
38- real_index,
39- golden_data,
40- output_data,
41- abs(output_data - golden_data) / golden_data,
42- )
43 )36 )
44 if index == 100:37 if index == 100:
45 break38 break
@@ -24,9 +24,7 @@ def gen_golden_data():
24 input_b = np.random.randint(1, 10, [k, n]).astype(np.float16)24 input_b = np.random.randint(1, 10, [k, n]).astype(np.float16)
25 input_bias = np.random.randint(1, 10, [n]).astype(np.float32)25 input_bias = np.random.randint(1, 10, [n]).astype(np.float32)
26 alpha = 0.00126 alpha = 0.001
27- golden = (27+ golden = (np.matmul(input_a.astype(np.float32), input_b.astype(np.float32)) + input_bias).astype(np.float32)
28- np.matmul(input_a.astype(np.float32), input_b.astype(np.float32)) + input_bias
29- ).astype(np.float32)
30 golden = np.where(golden >= 0, golden, golden * alpha)28 golden = np.where(golden >= 0, golden, golden * alpha)
31 os.makedirs("input", exist_ok=True)29 os.makedirs("input", exist_ok=True)
32 os.makedirs("output", exist_ok=True)30 os.makedirs("output", exist_ok=True)
@@ -24,9 +24,7 @@ ERROR_TOL = 1e-4
24def verify_result(output, golden):24def verify_result(output, golden):
25 output = np.fromfile(output, dtype=np.float32).reshape(-1)25 output = np.fromfile(output, dtype=np.float32).reshape(-1)
26 golden = np.fromfile(golden, dtype=np.float32).reshape(-1)26 golden = np.fromfile(golden, dtype=np.float32).reshape(-1)
27- different_element_results = np.isclose(27+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
28- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
29- )
30 different_element_indexes = np.where(different_element_results == False)[0]28 different_element_indexes = np.where(different_element_results == False)[0]
31 for index in range(len(different_element_indexes)):29 for index in range(len(different_element_indexes)):
32 real_index = different_element_indexes[index]30 real_index = different_element_indexes[index]
@@ -34,12 +32,7 @@ def verify_result(output, golden):
34 output_data = output[real_index]32 output_data = output[real_index]
35 print(33 print(
36 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"34 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"
37- % (35+ % (real_index, golden_data, output_data, abs(output_data - golden_data) / golden_data)
38- real_index,
39- golden_data,
40- output_data,
41- abs(output_data - golden_data) / golden_data,
42- )
43 )36 )
44 if index == 100:37 if index == 100:
45 break38 break
@@ -22,9 +22,7 @@ def gen_golden_data():
22 k = 12822 k = 128
23 x1_gm = np.random.randint(-10, 10, [m, k]).astype(np.float16)23 x1_gm = np.random.randint(-10, 10, [m, k]).astype(np.float16)
24 x2_gm = np.random.randint(-10, 10, [k, n]).astype(np.float16)24 x2_gm = np.random.randint(-10, 10, [k, n]).astype(np.float16)
25- golden = np.matmul(x1_gm.astype(np.float32), x2_gm.astype(np.float32)).astype(25+ golden = np.matmul(x1_gm.astype(np.float32), x2_gm.astype(np.float32)).astype(np.float16)
26- np.float16
27- )
28 golden = np.where(golden >= 0, golden, golden * 0.001)26 golden = np.where(golden >= 0, golden, golden * 0.001)
29 os.makedirs("input", exist_ok=True)27 os.makedirs("input", exist_ok=True)
30 os.makedirs("output", exist_ok=True)28 os.makedirs("output", exist_ok=True)
@@ -24,9 +24,7 @@ ERROR_TOL = 1e-4
24def verify_result(output, golden):24def verify_result(output, golden):
25 output = np.fromfile(output, dtype=np.float16).reshape(-1)25 output = np.fromfile(output, dtype=np.float16).reshape(-1)
26 golden = np.fromfile(golden, dtype=np.float16).reshape(-1)26 golden = np.fromfile(golden, dtype=np.float16).reshape(-1)
27- different_element_results = np.isclose(27+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
28- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
29- )
30 different_element_indexes = np.where(different_element_results == False)[0]28 different_element_indexes = np.where(different_element_results == False)[0]
31 for index in range(len(different_element_indexes)):29 for index in range(len(different_element_indexes)):
32 real_index = different_element_indexes[index]30 real_index = different_element_indexes[index]
@@ -34,12 +32,7 @@ def verify_result(output, golden):
34 output_data = output[real_index]32 output_data = output[real_index]
35 print(33 print(
36 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"34 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"
37- % (35+ % (real_index, golden_data, output_data, abs(output_data - golden_data) / golden_data)
38- real_index,
39- golden_data,
40- output_data,
41- abs(output_data - golden_data) / golden_data,
42- )
43 )36 )
44 if index == 100:37 if index == 100:
45 break38 break
@@ -26,9 +26,7 @@ def verify_result(output, golden):
26 golden = np.fromfile(golden, dtype=np.float32).reshape(-1)26 golden = np.fromfile(golden, dtype=np.float32).reshape(-1)
27 print("output:", output[0:10], output[-10:])27 print("output:", output[0:10], output[-10:])
28 print("golden:", golden[0:10], golden[-10:])28 print("golden:", golden[0:10], golden[-10:])
29- different_element_results = np.isclose(29+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
30- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
31- )
32 different_element_indexes = np.where(different_element_results == False)[0]30 different_element_indexes = np.where(different_element_results == False)[0]
33 for index in range(len(different_element_indexes)):31 for index in range(len(different_element_indexes)):
34 real_index = different_element_indexes[index]32 real_index = different_element_indexes[index]
@@ -36,12 +34,7 @@ def verify_result(output, golden):
36 output_data = output[real_index]34 output_data = output[real_index]
37 print(35 print(
38 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"36 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"
39- % (37+ % (real_index, golden_data, output_data, abs(output_data - golden_data) / golden_data)
40- real_index,
41- golden_data,
42- output_data,
43- abs(output_data - golden_data) / golden_data,
44- )
45 )38 )
46 if index == 100:39 if index == 100:
47 break40 break
@@ -26,9 +26,7 @@ def verify_result(output, golden):
26 golden = np.fromfile(golden, dtype=np.float32).reshape(-1)26 golden = np.fromfile(golden, dtype=np.float32).reshape(-1)
27 print("output:", output[0:10], output[-10:])27 print("output:", output[0:10], output[-10:])
28 print("golden:", golden[0:10], golden[-10:])28 print("golden:", golden[0:10], golden[-10:])
29- different_element_results = np.isclose(29+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
30- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
31- )
32 different_element_indexes = np.where(different_element_results == False)[0]30 different_element_indexes = np.where(different_element_results == False)[0]
33 for index in range(len(different_element_indexes)):31 for index in range(len(different_element_indexes)):
34 real_index = different_element_indexes[index]32 real_index = different_element_indexes[index]
@@ -36,12 +34,7 @@ def verify_result(output, golden):
36 output_data = output[real_index]34 output_data = output[real_index]
37 print(35 print(
38 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"36 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"
39- % (37+ % (real_index, golden_data, output_data, abs(output_data - golden_data) / golden_data)
40- real_index,
41- golden_data,
42- output_data,
43- abs(output_data - golden_data) / golden_data,
44- )
45 )38 )
46 if index == 100:39 if index == 100:
47 break40 break
@@ -22,9 +22,7 @@ def gen_golden_data():
22 n = 25622 n = 256
23 x1_gm = np.random.randint(-10, 10, [m, k]).astype(np.float16)23 x1_gm = np.random.randint(-10, 10, [m, k]).astype(np.float16)
24 x2_gm = np.random.randint(-10, 10, [k, n]).astype(np.float16)24 x2_gm = np.random.randint(-10, 10, [k, n]).astype(np.float16)
25- golden = np.matmul(x1_gm.astype(np.float32), x2_gm.astype(np.float32)).astype(25+ golden = np.matmul(x1_gm.astype(np.float32), x2_gm.astype(np.float32)).astype(np.float16)
26- np.float16
27- )
28 os.makedirs("input", exist_ok=True)26 os.makedirs("input", exist_ok=True)
29 os.makedirs("output", exist_ok=True)27 os.makedirs("output", exist_ok=True)
30 x1_gm.tofile("./input/x1_gm.bin")28 x1_gm.tofile("./input/x1_gm.bin")
@@ -24,9 +24,7 @@ ERROR_TOL = 1e-4
24def verify_result(output, golden):24def verify_result(output, golden):
25 output = np.fromfile(output, dtype=np.float16).reshape(-1)25 output = np.fromfile(output, dtype=np.float16).reshape(-1)
26 golden = np.fromfile(golden, dtype=np.float16).reshape(-1)26 golden = np.fromfile(golden, dtype=np.float16).reshape(-1)
27- different_element_results = np.isclose(27+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
28- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
29- )
30 different_element_indexes = np.where(different_element_results == False)[0]28 different_element_indexes = np.where(different_element_results == False)[0]
31 for index in range(len(different_element_indexes)):29 for index in range(len(different_element_indexes)):
32 real_index = different_element_indexes[index]30 real_index = different_element_indexes[index]
@@ -34,12 +32,7 @@ def verify_result(output, golden):
34 output_data = output[real_index]32 output_data = output[real_index]
35 print(33 print(
36 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"34 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"
37- % (35+ % (real_index, golden_data, output_data, abs(output_data - golden_data) / golden_data)
38- real_index,
39- golden_data,
40- output_data,
41- abs(output_data - golden_data) / golden_data,
42- )
43 )36 )
44 if index == 100:37 if index == 100:
45 break38 break
@@ -22,9 +22,7 @@ def gen_golden_data():
22 n = 25622 n = 256
23 x1_gm = np.random.randint(1, 10, [m, k]).astype(np.float16)23 x1_gm = np.random.randint(1, 10, [m, k]).astype(np.float16)
24 x2_gm = np.random.randint(1, 10, [k, n]).astype(np.float16)24 x2_gm = np.random.randint(1, 10, [k, n]).astype(np.float16)
25- golden = np.matmul(x1_gm.astype(np.float32), x2_gm.astype(np.float32)).astype(25+ golden = np.matmul(x1_gm.astype(np.float32), x2_gm.astype(np.float32)).astype(np.float16)
26- np.float16
27- )
28 os.makedirs("input", exist_ok=True)26 os.makedirs("input", exist_ok=True)
29 os.makedirs("output", exist_ok=True)27 os.makedirs("output", exist_ok=True)
30 x1_gm.tofile("./input/x1_gm.bin")28 x1_gm.tofile("./input/x1_gm.bin")
@@ -24,9 +24,7 @@ ERROR_TOL = 1e-4
24def verify_result(output, golden):24def verify_result(output, golden):
25 output = np.fromfile(output, dtype=np.float16).reshape(-1)25 output = np.fromfile(output, dtype=np.float16).reshape(-1)
26 golden = np.fromfile(golden, dtype=np.float16).reshape(-1)26 golden = np.fromfile(golden, dtype=np.float16).reshape(-1)
27- different_element_results = np.isclose(27+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
28- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
29- )
30 different_element_indexes = np.where(different_element_results == False)[0]28 different_element_indexes = np.where(different_element_results == False)[0]
31 for index in range(len(different_element_indexes)):29 for index in range(len(different_element_indexes)):
32 real_index = different_element_indexes[index]30 real_index = different_element_indexes[index]
@@ -34,12 +32,7 @@ def verify_result(output, golden):
34 output_data = output[real_index]32 output_data = output[real_index]
35 print(33 print(
36 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"34 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"
37- % (35+ % (real_index, golden_data, output_data, abs(output_data - golden_data) / golden_data)
38- real_index,
39- golden_data,
40- output_data,
41- abs(output_data - golden_data) / golden_data,
42- )
43 )36 )
44 if index == 100:37 if index == 100:
45 break38 break
@@ -41,9 +41,7 @@ def gen_data():
41 bias = np.arange(MATMUL_N, dtype=np.float32)41 bias = np.arange(MATMUL_N, dtype=np.float32)
42 scale = np.where(np.arange(MATMUL_N) % 2 == 0, 1.0, 0.5).astype(np.float32)42 scale = np.where(np.arange(MATMUL_N) % 2 == 0, 1.0, 0.5).astype(np.float32)
43 scale_bits = scale.view(np.uint32).astype(np.uint64)43 scale_bits = scale.view(np.uint32).astype(np.uint64)
44- quant = np.uint64(FIXPIPE_QUANT_META_BIT) | (44+ quant = np.uint64(FIXPIPE_QUANT_META_BIT) | (scale_bits & np.uint64(FIXPIPE_SCALE_MASK))
45- scale_bits & np.uint64(FIXPIPE_SCALE_MASK)
46- )
47 45 
48 # The raw HiFloat8 value 0x08 represents 1.0 for both input matrices.46 # The raw HiFloat8 value 0x08 represents 1.0 for both input matrices.
49 a_value = np.ones((MATMUL_M, MATMUL_K), dtype=np.float32)47 a_value = np.ones((MATMUL_M, MATMUL_K), dtype=np.float32)
@@ -36,10 +36,7 @@ def verify_result(output_path, golden_path):
36 output = np.fromfile(output_path, dtype=np.uint16)36 output = np.fromfile(output_path, dtype=np.uint16)
37 golden = np.fromfile(golden_path, dtype=np.uint16)37 golden = np.fromfile(golden_path, dtype=np.uint16)
38 if output.size != OUTPUT_ELEMENT_COUNT or golden.size != OUTPUT_ELEMENT_COUNT:38 if output.size != OUTPUT_ELEMENT_COUNT or golden.size != OUTPUT_ELEMENT_COUNT:
39- print(39+ print(f"unexpected element count: output={output.size}, golden={golden.size}, expected={OUTPUT_ELEMENT_COUNT}")
40- f"unexpected element count: output={output.size}, golden={golden.size}, "
41- f"expected={OUTPUT_ELEMENT_COUNT}"
42- )
43 print("Output result check: FAIL")40 print("Output result check: FAIL")
44 return False41 return False
45 42 
@@ -64,11 +61,7 @@ def parse_dump(text, desc, expected_count, value_type=int, expected_fields=()):
64 for field in expected_fields:61 for field in expected_fields:
65 if field not in header:62 if field not in header:
66 raise RuntimeError(f"DumpTensor desc={desc} missing header field: {field}")63 raise RuntimeError(f"DumpTensor desc={desc} missing header field: {field}")
67- values = [64+ values = [value_type(item.strip()) for item in body.replace("\n", " ").split(",") if item.strip()]
68- value_type(item.strip())
69- for item in body.replace("\n", " ").split(",")
70- if item.strip()
71- ]
72 if len(values) != expected_count:65 if len(values) != expected_count:
73 raise RuntimeError(66 raise RuntimeError(
74 f"unexpected DumpTensor desc={desc} element count: {len(values)}, expected: {expected_count}"67 f"unexpected DumpTensor desc={desc} element count: {len(values)}, expected: {expected_count}"
@@ -102,82 +95,45 @@ def check_equal(name, actual, expected):
102 for index, (actual_value, expected_value) in enumerate(zip(actual, expected)):95 for index, (actual_value, expected_value) in enumerate(zip(actual, expected)):
103 if actual_value != expected_value:96 if actual_value != expected_value:
104 if isinstance(actual_value, int) and isinstance(expected_value, int):97 if isinstance(actual_value, int) and isinstance(expected_value, int):
105- print(98+ print(f" [{index:02d}] actual=0x{actual_value:016x}, expected=0x{expected_value:016x}")
106- f" [{index:02d}] actual=0x{actual_value:016x}, expected=0x{expected_value:016x}"
107- )
108 else:99 else:
109- print(100+ print(f" [{index:02d}] actual={actual_value}, expected={expected_value}")
110- f" [{index:02d}] actual={actual_value}, expected={expected_value}"
111- )
112 return False101 return False
113 102 
114 103 
115def main():104def main():
116 if len(sys.argv) != 7:105 if len(sys.argv) != 7:
117- print(106+ print(f"Usage: {sys.argv[0]} <output.bin> <golden.bin> <run.log> <a.bin> <bias.bin> <scale.bin>")
118- f"Usage: {sys.argv[0]} <output.bin> <golden.bin> <run.log> "
119- "<a.bin> <bias.bin> <scale.bin>"
120- )
121 return 1107 return 1
122 108 
123 text = Path(sys.argv[3]).read_text(encoding="utf-8")109 text = Path(sys.argv[3]).read_text(encoding="utf-8")
124- a_input, bias_input, fixpipe_input = read_dump_input(110+ a_input, bias_input, fixpipe_input = read_dump_input(Path(sys.argv[4]), Path(sys.argv[5]), Path(sys.argv[6]))
125- Path(sys.argv[4]), Path(sys.argv[5]), Path(sys.argv[6])
126- )
127 fixpipe_expected = [value & FIXPIPE_QUANT_VISIBLE_MASK for value in fixpipe_input]111 fixpipe_expected = [value & FIXPIPE_QUANT_VISIBLE_MASK for value in fixpipe_input]
128 checks = [112 checks = [
129 verify_result(sys.argv[1], sys.argv[2]),113 verify_result(sys.argv[1], sys.argv[2]),
130 check_equal(114 check_equal(
131 "HiFloat8 A L1 data check",115 "HiFloat8 A L1 data check",
132- parse_dump(116+ parse_dump(text, 100, A_L1_DUMP_ELEMENT_COUNT, float, ("data_type=hifloat8", "position=L1")),
133- text,
134- 100,
135- A_L1_DUMP_ELEMENT_COUNT,
136- float,
137- ("data_type=hifloat8", "position=L1"),
138- ),
139 a_input,117 a_input,
140 ),118 ),
141 check_equal(119 check_equal(
142 "Bias L1 raw input check",120 "Bias L1 raw input check",
143- parse_dump(121+ parse_dump(text, 101, BIAS_ELEMENT_COUNT, float, ("data_type=float32", "position=L1")),
144- text,
145- 101,
146- BIAS_ELEMENT_COUNT,
147- float,
148- ("data_type=float32", "position=L1"),
149- ),
150 bias_input,122 bias_input,
151 ),123 ),
152 check_equal(124 check_equal(
153 "Bias Table raw data check",125 "Bias Table raw data check",
154- parse_dump(126+ parse_dump(text, 201, BIAS_ELEMENT_COUNT, float, ("data_type=float32", "position=BIAS")),
155- text,
156- 201,
157- BIAS_ELEMENT_COUNT,
158- float,
159- ("data_type=float32", "position=BIAS"),
160- ),
161 bias_input,127 bias_input,
162 ),128 ),
163 check_equal(129 check_equal(
164 "Fixpipe L1 raw input check",130 "Fixpipe L1 raw input check",
165- parse_dump(131+ parse_dump(text, 102, QUANT_ELEMENT_COUNT, expected_fields=("data_type=uint64", "position=L1")),
166- text,
167- 102,
168- QUANT_ELEMENT_COUNT,
169- expected_fields=("data_type=uint64", "position=L1"),
170- ),
171 fixpipe_input,132 fixpipe_input,
172 ),133 ),
173 check_equal(134 check_equal(
174 "Fixpipe quant visible-mask check",135 "Fixpipe quant visible-mask check",
175- parse_dump(136+ parse_dump(text, 301, QUANT_ELEMENT_COUNT, expected_fields=("data_type=uint64", "position=FIXBUF")),
176- text,
177- 301,
178- QUANT_ELEMENT_COUNT,
179- expected_fields=("data_type=uint64", "position=FIXBUF"),
180- ),
181 fixpipe_expected,137 fixpipe_expected,
182 ),138 ),
183 ]139 ]
@@ -24,9 +24,7 @@ ERROR_TOL = 1e-4
24def verify_result(output, golden):24def verify_result(output, golden):
25 output = np.fromfile(output, dtype=np.float32).reshape(-1)25 output = np.fromfile(output, dtype=np.float32).reshape(-1)
26 golden = np.fromfile(golden, dtype=np.float32).reshape(-1)26 golden = np.fromfile(golden, dtype=np.float32).reshape(-1)
27- different_element_results = np.isclose(27+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
28- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
29- )
30 different_element_indexes = np.where(different_element_results == False)[0]28 different_element_indexes = np.where(different_element_results == False)[0]
31 for index in range(len(different_element_indexes)):29 for index in range(len(different_element_indexes)):
32 real_index = different_element_indexes[index]30 real_index = different_element_indexes[index]
@@ -34,12 +32,7 @@ def verify_result(output, golden):
34 output_data = output[real_index]32 output_data = output[real_index]
35 print(33 print(
36 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"34 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"
37- % (35+ % (real_index, golden_data, output_data, abs(output_data - golden_data) / golden_data)
38- real_index,
39- golden_data,
40- output_data,
41- abs(output_data - golden_data) / golden_data,
42- )
43 )36 )
44 if index == 100:37 if index == 100:
45 break38 break
@@ -22,9 +22,7 @@ def gen_golden_data():
22 n = 25622 n = 256
23 x1_gm = np.random.randint(1, 10, [m, k]).astype(np.float16)23 x1_gm = np.random.randint(1, 10, [m, k]).astype(np.float16)
24 x2_gm = np.random.randint(1, 10, [k, n]).astype(np.float16)24 x2_gm = np.random.randint(1, 10, [k, n]).astype(np.float16)
25- golden = np.matmul(x1_gm.astype(np.float32), x2_gm.astype(np.float32)).astype(25+ golden = np.matmul(x1_gm.astype(np.float32), x2_gm.astype(np.float32)).astype(np.float16)
26- np.float16
27- )
28 os.makedirs("input", exist_ok=True)26 os.makedirs("input", exist_ok=True)
29 os.makedirs("output", exist_ok=True)27 os.makedirs("output", exist_ok=True)
30 x1_gm.tofile("./input/x1_gm.bin")28 x1_gm.tofile("./input/x1_gm.bin")
@@ -24,9 +24,7 @@ ERROR_TOL = 1e-4
24def verify_result(output, golden):24def verify_result(output, golden):
25 output = np.fromfile(output, dtype=np.float16).reshape(-1)25 output = np.fromfile(output, dtype=np.float16).reshape(-1)
26 golden = np.fromfile(golden, dtype=np.float16).reshape(-1)26 golden = np.fromfile(golden, dtype=np.float16).reshape(-1)
27- different_element_results = np.isclose(27+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
28- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
29- )
30 different_element_indexes = np.where(different_element_results == False)[0]28 different_element_indexes = np.where(different_element_results == False)[0]
31 for index in range(len(different_element_indexes)):29 for index in range(len(different_element_indexes)):
32 real_index = different_element_indexes[index]30 real_index = different_element_indexes[index]
@@ -34,12 +32,7 @@ def verify_result(output, golden):
34 output_data = output[real_index]32 output_data = output[real_index]
35 print(33 print(
36 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"34 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"
37- % (35+ % (real_index, golden_data, output_data, abs(output_data - golden_data) / golden_data)
38- real_index,
39- golden_data,
40- output_data,
41- abs(output_data - golden_data) / golden_data,
42- )
43 )36 )
44 if index == 100:37 if index == 100:
45 break38 break
@@ -22,9 +22,7 @@ def gen_golden_data():
22 k = 12822 k = 128
23 x1_gm = np.random.randint(-10, 10, [m, k]).astype(np.float16)23 x1_gm = np.random.randint(-10, 10, [m, k]).astype(np.float16)
24 x2_gm = np.random.randint(-10, 10, [k, n]).astype(np.float16)24 x2_gm = np.random.randint(-10, 10, [k, n]).astype(np.float16)
25- golden = np.matmul(x1_gm.astype(np.float32), x2_gm.astype(np.float32)).astype(25+ golden = np.matmul(x1_gm.astype(np.float32), x2_gm.astype(np.float32)).astype(np.float16)
26- np.float16
27- )
28 golden = np.where(golden >= 0, golden, golden * 0.001)26 golden = np.where(golden >= 0, golden, golden * 0.001)
29 os.makedirs("input", exist_ok=True)27 os.makedirs("input", exist_ok=True)
30 os.makedirs("output", exist_ok=True)28 os.makedirs("output", exist_ok=True)
@@ -24,9 +24,7 @@ ERROR_TOL = 1e-4
24def verify_result(output, golden):24def verify_result(output, golden):
25 output = np.fromfile(output, dtype=np.float16).reshape(-1)25 output = np.fromfile(output, dtype=np.float16).reshape(-1)
26 golden = np.fromfile(golden, dtype=np.float16).reshape(-1)26 golden = np.fromfile(golden, dtype=np.float16).reshape(-1)
27- different_element_results = np.isclose(27+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
28- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
29- )
30 different_element_indexes = np.where(different_element_results == False)[0]28 different_element_indexes = np.where(different_element_results == False)[0]
31 for index in range(len(different_element_indexes)):29 for index in range(len(different_element_indexes)):
32 real_index = different_element_indexes[index]30 real_index = different_element_indexes[index]
@@ -22,9 +22,7 @@ def gen_golden_data():
22 k = 6422 k = 64
23 x1_gm = np.random.randint(-10, 10, [m, k]).astype(np.float16)23 x1_gm = np.random.randint(-10, 10, [m, k]).astype(np.float16)
24 x2_gm = np.random.randint(-10, 10, [k, n]).astype(np.float16)24 x2_gm = np.random.randint(-10, 10, [k, n]).astype(np.float16)
25- golden = np.matmul(x1_gm.astype(np.float32), x2_gm.astype(np.float32)).astype(25+ golden = np.matmul(x1_gm.astype(np.float32), x2_gm.astype(np.float32)).astype(np.float16)
26- np.float16
27- )
28 os.makedirs("input", exist_ok=True)26 os.makedirs("input", exist_ok=True)
29 os.makedirs("output", exist_ok=True)27 os.makedirs("output", exist_ok=True)
30 x1_gm.tofile("./input/x1_gm.bin")28 x1_gm.tofile("./input/x1_gm.bin")
@@ -24,9 +24,7 @@ ERROR_TOL = 1e-4
24def verify_result(output, golden):24def verify_result(output, golden):
25 output = np.fromfile(output, dtype=np.float16).reshape(-1)25 output = np.fromfile(output, dtype=np.float16).reshape(-1)
26 golden = np.fromfile(golden, dtype=np.float16).reshape(-1)26 golden = np.fromfile(golden, dtype=np.float16).reshape(-1)
27- different_element_results = np.isclose(27+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
28- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
29- )
30 different_element_indexes = np.where(different_element_results == False)[0]28 different_element_indexes = np.where(different_element_results == False)[0]
31 for index in range(len(different_element_indexes)):29 for index in range(len(different_element_indexes)):
32 real_index = different_element_indexes[index]30 real_index = different_element_indexes[index]
@@ -34,12 +32,7 @@ def verify_result(output, golden):
34 output_data = output[real_index]32 output_data = output[real_index]
35 print(33 print(
36 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"34 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"
37- % (35+ % (real_index, golden_data, output_data, abs(output_data - golden_data) / golden_data)
38- real_index,
39- golden_data,
40- output_data,
41- abs(output_data - golden_data) / golden_data,
42- )
43 )36 )
44 if index == 100:37 if index == 100:
45 break38 break
@@ -28,17 +28,8 @@ torch.ops.load_library(LIB_PATH)
28 28 
29# 注册TorchAir converter:torch.compile捕获到ascendc_add节点后,由该函数转换成GE AddCustom节点29# 注册TorchAir converter:torch.compile捕获到ascendc_add节点后,由该函数转换成GE AddCustom节点
30@torchair.register_fx_node_ge_converter(torch.ops.ascendc_ops.ascendc_add.default)30@torchair.register_fx_node_ge_converter(torch.ops.ascendc_ops.ascendc_add.default)
31-def convert_ascendc_add(31+def convert_ascendc_add(x: Tensor, y: Tensor, z: Tensor = None, meta_outputs: Any = None):
32- x: Tensor, y: Tensor, z: Tensor = None, meta_outputs: Any = None32+ return torchair.ge.custom_op("AddCustom", inputs={"x": x, "y": y}, outputs=["z"])
33-):
34- return torchair.ge.custom_op(
35- "AddCustom",
36- inputs={
37- "x": x,
38- "y": y,
39- },
40- outputs=["z"],
41- )
42 33 
43 34 
44class AddCustomModel(torch.nn.Module):35class AddCustomModel(torch.nn.Module):
@@ -59,9 +50,7 @@ class TestGeTorchairAdd(TestCase):
59 config = torchair.CompilerConfig()50 config = torchair.CompilerConfig()
60 npu_backend = torchair.get_npu_backend(compiler_config=config)51 npu_backend = torchair.get_npu_backend(compiler_config=config)
61 # 使用TorchAir后端编译模型,fullgraph=True确保forward中的ascendc_add被整图捕获52 # 使用TorchAir后端编译模型,fullgraph=True确保forward中的ascendc_add被整图捕获
62- opt_model = torch.compile(53+ opt_model = torch.compile(model, fullgraph=True, backend=npu_backend, dynamic=False)
63- model, fullgraph=True, backend=npu_backend, dynamic=False
64- )
65 54 
66 # 在NPU上执行编译后的图,执行链路会经过TorchAir转换出的GE AddCustom节点55 # 在NPU上执行编译后的图,执行链路会经过TorchAir转换出的GE AddCustom节点
67 output = opt_model(x_cpu.npu(), y_cpu.npu()).cpu()56 output = opt_model(x_cpu.npu(), y_cpu.npu()).cpu()
@@ -22,9 +22,7 @@ import numpy as np
22 22 
23 23 
24tf.compat.v1.disable_v2_behavior()24tf.compat.v1.disable_v2_behavior()
25-tf.compat.v1.flags.DEFINE_string(25+tf.compat.v1.flags.DEFINE_string("local_log_dir", "output/train_logs.txt", "Log file path")
26- "local_log_dir", "output/train_logs.txt", "Log file path"
27-)
28FLAGS = tf.compat.v1.flags.FLAGS26FLAGS = tf.compat.v1.flags.FLAGS
29ATOL = 0.00127ATOL = 0.001
30RTOL = 0.00128RTOL = 0.001
@@ -32,9 +30,7 @@ RTOL = 0.001
32 30 
33def config(execute_type):31def config(execute_type):
34 if execute_type == "ai_core":32 if execute_type == "ai_core":
35- session_config = tf.compat.v1.ConfigProto(33+ session_config = tf.compat.v1.ConfigProto(allow_soft_placement=True, log_device_placement=False)
36- allow_soft_placement=True, log_device_placement=False
37- )
38 custom_op = session_config.graph_options.rewrite_options.custom_optimizers.add()34 custom_op = session_config.graph_options.rewrite_options.custom_optimizers.add()
39 custom_op.name = "NpuOptimizer"35 custom_op.name = "NpuOptimizer"
40 custom_op.parameter_map["enable_data_pre_proc"].b = True36 custom_op.parameter_map["enable_data_pre_proc"].b = True
@@ -43,9 +39,7 @@ def config(execute_type):
43 custom_op.parameter_map["min_group_size"].b = 139 custom_op.parameter_map["min_group_size"].b = 1
44 40 
45 elif execute_type == "cpu":41 elif execute_type == "cpu":
46- session_config = tf.compat.v1.ConfigProto(42+ session_config = tf.compat.v1.ConfigProto(allow_soft_placement=True, log_device_placement=False)
47- allow_soft_placement=True, log_device_placement=False
48- )
49 43 
50 return session_config44 return session_config
51 45 
@@ -28,9 +28,7 @@ RTOL = 0.001
28 28 
29def main(unused_argv):29def main(unused_argv):
30 base_dir = os.path.dirname(os.path.abspath(__file__))30 base_dir = os.path.dirname(os.path.abspath(__file__))
31- custom_op_lib = tf.load_op_library(31+ custom_op_lib = tf.load_op_library(os.path.join(base_dir, "outputs", "libcustom_ops.so"))
32- os.path.join(base_dir, "outputs", "libcustom_ops.so")
33- )
34 32 
35 shape_params = (8, 2048)33 shape_params = (8, 2048)
36 dtype_params = np.float1634 dtype_params = np.float16
@@ -17,12 +17,7 @@ import argparse
17import numpy as np17import numpy as np
18 18 
19 19 
20-SCENARIO_LENGTHS = {20+SCENARIO_LENGTHS = {0: 256000, 1: 260096, 2: 256064, 3: 258112}
21- 0: 256000,
22- 1: 260096,
23- 2: 256064,
24- 3: 258112,
25-}
26 21 
27 22 
28def gen_golden_data_simple(scenario):23def gen_golden_data_simple(scenario):
@@ -26,13 +26,9 @@ def verify_result(output, golden):
26 output_data = np.fromfile(output, dtype=np.float16).reshape(-1)26 output_data = np.fromfile(output, dtype=np.float16).reshape(-1)
27 golden_data = np.fromfile(golden, dtype=np.float16).reshape(-1)27 golden_data = np.fromfile(golden, dtype=np.float16).reshape(-1)
28 if output_data.size != golden_data.size:28 if output_data.size != golden_data.size:
29- raise ValueError(29+ raise ValueError(f"size mismatch: output={output_data.size}, golden={golden_data.size}")
30- f"size mismatch: output={output_data.size}, golden={golden_data.size}"
31- )
32 30 
33- compare = np.isclose(31+ compare = np.isclose(output_data, golden_data, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
34- output_data, golden_data, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
35- )
36 diff_indexes = np.where(compare == False)[0]32 diff_indexes = np.where(compare == False)[0]
37 for index, real_index in enumerate(diff_indexes[:100]):33 for index, real_index in enumerate(diff_indexes[:100]):
38 expect = golden_data[real_index]34 expect = golden_data[real_index]
@@ -25,18 +25,13 @@ def verify_result(output, golden):
25 output_type = np.float3225 output_type = np.float32
26 output = np.fromfile(output, dtype=output_type).reshape(-1)26 output = np.fromfile(output, dtype=output_type).reshape(-1)
27 golden = np.fromfile(golden, dtype=output_type).reshape(-1)27 golden = np.fromfile(golden, dtype=output_type).reshape(-1)
28- different_element_results = np.isclose(28+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
29- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
30- )
31 different_element_indexes = np.where(different_element_results == False)[0]29 different_element_indexes = np.where(different_element_results == False)[0]
32 for index in range(len(different_element_indexes)):30 for index in range(len(different_element_indexes)):
33 real_index = different_element_indexes[index]31 real_index = different_element_indexes[index]
34 golden_data = golden[real_index]32 golden_data = golden[real_index]
35 output_data = output[real_index]33 output_data = output[real_index]
36- print(34+ print("data index: %06d, expected: %-.9f, actual: %-.9f" % (real_index, golden_data, output_data))
37- "data index: %06d, expected: %-.9f, actual: %-.9f"
38- % (real_index, golden_data, output_data)
39- )
40 if index == 100:35 if index == 100:
41 break36 break
42 error_ratio = float(different_element_indexes.size) / golden.size37 error_ratio = float(different_element_indexes.size) / golden.size
@@ -25,18 +25,13 @@ def verify_result(output, golden):
25 output_type = np.int3225 output_type = np.int32
26 output = np.fromfile(output, dtype=output_type).reshape(-1)26 output = np.fromfile(output, dtype=output_type).reshape(-1)
27 golden = np.fromfile(golden, dtype=output_type).reshape(-1)27 golden = np.fromfile(golden, dtype=output_type).reshape(-1)
28- different_element_results = np.isclose(28+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
29- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
30- )
31 different_element_indexes = np.where(different_element_results == False)[0]29 different_element_indexes = np.where(different_element_results == False)[0]
32 for index in range(len(different_element_indexes)):30 for index in range(len(different_element_indexes)):
33 real_index = different_element_indexes[index]31 real_index = different_element_indexes[index]
34 golden_data = golden[real_index]32 golden_data = golden[real_index]
35 output_data = output[real_index]33 output_data = output[real_index]
36- print(34+ print("data index: %06d, expected: %-.9f, actual: %-.9f" % (real_index, golden_data, output_data))
37- "data index: %06d, expected: %-.9f, actual: %-.9f"
38- % (real_index, golden_data, output_data)
39- )
40 if index == 100:35 if index == 100:
41 break36 break
42 error_ratio = float(different_element_indexes.size) / golden.size37 error_ratio = float(different_element_indexes.size) / golden.size
@@ -22,16 +22,12 @@ np.random.seed(9)
22 22 
23def extract_relu_params(relu_pre):23def extract_relu_params(relu_pre):
24 relu_pre = int(relu_pre)24 relu_pre = int(relu_pre)
25- relu_alpha_bits = (25+ relu_alpha_bits = (relu_pre >> 13) & 0xFFFFF # 提取M2的20位[31:13],0xFFFFF是20位掩码
26- relu_pre >> 13
27- ) & 0xFFFFF # 提取M2的20位[31:13],0xFFFFF是20位掩码
28 sign_bit = (relu_alpha_bits >> 18) & 0x126 sign_bit = (relu_alpha_bits >> 18) & 0x1
29 exponent = (relu_alpha_bits >> 10) & 0xFF27 exponent = (relu_alpha_bits >> 10) & 0xFF
30 mantissa = relu_alpha_bits & 0x3FF28 mantissa = relu_alpha_bits & 0x3FF
31 exponent_bias = 127 # 假设指数偏倚量为127,与float32一致29 exponent_bias = 127 # 假设指数偏倚量为127,与float32一致
32- relu_alpha = (30+ relu_alpha = (-1) ** sign_bit * (1 + mantissa / 1024) * (2 ** (exponent - exponent_bias))
33- (-1) ** sign_bit * (1 + mantissa / 1024) * (2 ** (exponent - exponent_bias))
34- )
35 return relu_alpha31 return relu_alpha
36 32 
37 33 
@@ -46,12 +42,8 @@ def extract_quant_params(quant_pre):
46 sign:1位布尔值(0或1)42 sign:1位布尔值(0或1)
47 """43 """
48 quant_pre = int(quant_pre)44 quant_pre = int(quant_pre)
49- quant_alpha_bits = (45+ quant_alpha_bits = (quant_pre >> 13) & 0xFFFFF # 提取M1的20位[31:13],0xFFFFF是20位掩码
50- quant_pre >> 1346+ mode_control_bit = (quant_pre >> 36) & 0x1 # 提取mode_ctrl bit的一位[36],0x1是1位掩码
51- ) & 0xFFFFF # 提取M1的20位[31:13],0xFFFFF是20位掩码
52- mode_control_bit = (
53- quant_pre >> 36
54- ) & 0x1 # 提取mode_ctrl bit的一位[36],0x1是1位掩码
55 offset = (quant_pre >> 37) & 0x1FF # 提取offset的9位[45:37],0x1FF是9位掩码47 offset = (quant_pre >> 37) & 0x1FF # 提取offset的9位[45:37],0x1FF是9位掩码
56 sign = (quant_pre >> 46) & 0x1 # 提取sign的一位[46],0x1是1位掩码48 sign = (quant_pre >> 46) & 0x1 # 提取sign的一位[46],0x1是1位掩码
57 n = (quant_pre >> 32) & 0xF49 n = (quant_pre >> 32) & 0xF
@@ -60,9 +52,7 @@ def extract_quant_params(quant_pre):
60 exponent = (quant_alpha_bits >> 10) & 0xFF52 exponent = (quant_alpha_bits >> 10) & 0xFF
61 mantissa = quant_alpha_bits & 0x3FF53 mantissa = quant_alpha_bits & 0x3FF
62 exponent_bias = 127 # 假设指数偏倚量为127,与float32一致54 exponent_bias = 127 # 假设指数偏倚量为127,与float32一致
63- quant_alpha = (55+ quant_alpha = (-1) ** sign_bit * (1 + mantissa / 1024) * (2 ** (exponent - exponent_bias))
64- (-1) ** sign_bit * (1 + mantissa / 1024) * (2 ** (exponent - exponent_bias))
65- )
66 return quant_alpha, offset, sign, n, mode_control_bit56 return quant_alpha, offset, sign, n, mode_control_bit
67 57 
68 58 
@@ -78,9 +68,7 @@ def qf322b8_pre(data, quant_pre, relu_pre):
78 """68 """
79 float32 -> int8/uint869 float32 -> int8/uint8
80 """70 """
81- quant_alpha, offset, sign, n_shift, mode_control_bit = extract_quant_params(71+ quant_alpha, offset, sign, n_shift, mode_control_bit = extract_quant_params(quant_pre)
82- quant_pre
83- )
84 # sign = 072 # sign = 0
85 relu_alpha = extract_relu_params(relu_pre)73 relu_alpha = extract_relu_params(relu_pre)
86 if mode_control_bit == 1:74 if mode_control_bit == 1:
@@ -110,19 +98,13 @@ def pre_quant_relu(golden, dst_type, m, n, pre_quant_mode, pre_relu_mode):
110 relu_alpha = struct.unpack("!I", struct.pack("!f", relu_alpha))[0]98 relu_alpha = struct.unpack("!I", struct.pack("!f", relu_alpha))[0]
111 99 
112 # 1 * n 量化系数为全为quant scalar的量化tensor100 # 1 * n 量化系数为全为quant scalar的量化tensor
113- temp_quant_tensor = (101+ temp_quant_tensor = ((quant_scalar * np.ones((1, n), dtype=np.float32)).astype(np.float32))[0]
114- (quant_scalar * np.ones((1, n), dtype=np.float32)).astype(np.float32)
115- )[0]
116 temp_quant_tensor_api = copy.deepcopy(temp_quant_tensor).astype(np.uint64)102 temp_quant_tensor_api = copy.deepcopy(temp_quant_tensor).astype(np.uint64)
117 for i, _ in enumerate(temp_quant_tensor_api):103 for i, _ in enumerate(temp_quant_tensor_api):
118- temp_quant_tensor_api[i] = struct.unpack(104+ temp_quant_tensor_api[i] = struct.unpack("!I", struct.pack("!f", temp_quant_tensor[i]))[0]
119- "!I", struct.pack("!f", temp_quant_tensor[i])
120- )[0]
121 if dst_type == np.int8:105 if dst_type == np.int8:
122 # np.int8 sign is true106 # np.int8 sign is true
123- temp_quant_tensor_api[i] = temp_quant_tensor_api[i] | np.uint64(107+ temp_quant_tensor_api[i] = temp_quant_tensor_api[i] | np.uint64(0x400000000000)
124- 0x400000000000
125- )
126 quant_tensor = np.frombuffer(temp_quant_tensor_api, np.uint64)108 quant_tensor = np.frombuffer(temp_quant_tensor_api, np.uint64)
127 quant_tensor = quant_tensor.astype(np.uint64)109 quant_tensor = quant_tensor.astype(np.uint64)
128 if pre_quant_mode == 2:110 if pre_quant_mode == 2:
@@ -144,9 +126,7 @@ def gen_golden_data(scenarioNum=1):
144 x1_gm = np.random.uniform(-1, 3, [M, K]).astype(input_type)126 x1_gm = np.random.uniform(-1, 3, [M, K]).astype(input_type)
145 x2_gm = np.random.uniform(-1, 3, [K, N]).astype(input_type)127 x2_gm = np.random.uniform(-1, 3, [K, N]).astype(input_type)
146 bias_gm = np.random.uniform(-1, 3, [N]).astype(np.float32)128 bias_gm = np.random.uniform(-1, 3, [N]).astype(np.float32)
147- golden = np.matmul(x1_gm.astype(np.float32), x2_gm.astype(np.float32)).astype(129+ golden = np.matmul(x1_gm.astype(np.float32), x2_gm.astype(np.float32)).astype(np.float32)
148- np.float32
149- )
150 # print(golden)130 # print(golden)
151 os.makedirs("input", exist_ok=True)131 os.makedirs("input", exist_ok=True)
152 os.makedirs("output", exist_ok=True)132 os.makedirs("output", exist_ok=True)
@@ -154,14 +134,10 @@ def gen_golden_data(scenarioNum=1):
154 if scenarioNum == 1:134 if scenarioNum == 1:
155 block_cols = 16135 block_cols = 16
156 x1_gm = (136 x1_gm = (
157- x1_gm.reshape((int(M / 16), 16, int(K / block_cols), block_cols))137+ x1_gm.reshape((int(M / 16), 16, int(K / block_cols), block_cols)).transpose(2, 0, 1, 3).astype(input_type)
158- .transpose(2, 0, 1, 3)
159- .astype(input_type)
160 )138 )
161 x2_gm = (139 x2_gm = (
162- x2_gm.reshape((int(K / 16), 16, int(N / block_cols), block_cols))140+ x2_gm.reshape((int(K / 16), 16, int(N / block_cols), block_cols)).transpose(2, 0, 1, 3).astype(input_type)
163- .transpose(2, 0, 1, 3)
164- .astype(input_type)
165 )141 )
166 elif scenarioNum == 2:142 elif scenarioNum == 2:
167 pass143 pass
@@ -24,9 +24,7 @@ error_tol = 1e-4
24def verify_result(output, golden):24def verify_result(output, golden):
25 output = np.fromfile(output, dtype=np.float32).reshape(-1)25 output = np.fromfile(output, dtype=np.float32).reshape(-1)
26 golden = np.fromfile(golden, dtype=np.float32).reshape(-1)26 golden = np.fromfile(golden, dtype=np.float32).reshape(-1)
27- different_element_results = np.isclose(27+ different_element_results = np.isclose(output, golden, rtol=relative_tol, atol=absolute_tol, equal_nan=True)
28- output, golden, rtol=relative_tol, atol=absolute_tol, equal_nan=True
29- )
30 different_element_indexes = np.where(different_element_results == False)[0]28 different_element_indexes = np.where(different_element_results == False)[0]
31 for index in range(len(different_element_indexes)):29 for index in range(len(different_element_indexes)):
32 real_index = different_element_indexes[index]30 real_index = different_element_indexes[index]
@@ -34,12 +32,7 @@ def verify_result(output, golden):
34 output_data = output[real_index]32 output_data = output[real_index]
35 print(33 print(
36 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"34 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"
37- % (35+ % (real_index, golden_data, output_data, abs(output_data - golden_data) / golden_data)
38- real_index,
39- golden_data,
40- output_data,
41- abs(output_data - golden_data) / golden_data,
42- )
43 )36 )
44 if index == 100:37 if index == 100:
45 break38 break
@@ -25,18 +25,13 @@ def verify_result(output, golden):
25 output_type = np.float3225 output_type = np.float32
26 output = np.fromfile(output, dtype=output_type).reshape(-1)26 output = np.fromfile(output, dtype=output_type).reshape(-1)
27 golden = np.fromfile(golden, dtype=output_type).reshape(-1)27 golden = np.fromfile(golden, dtype=output_type).reshape(-1)
28- different_element_results = np.isclose(28+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
29- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
30- )
31 different_element_indexes = np.where(different_element_results == False)[0]29 different_element_indexes = np.where(different_element_results == False)[0]
32 for index in range(len(different_element_indexes)):30 for index in range(len(different_element_indexes)):
33 real_index = different_element_indexes[index]31 real_index = different_element_indexes[index]
34 golden_data = golden[real_index]32 golden_data = golden[real_index]
35 output_data = output[real_index]33 output_data = output[real_index]
36- print(34+ print("data index: %06d, expected: %-.9f, actual: %-.9f" % (real_index, golden_data, output_data))
37- "data index: %06d, expected: %-.9f, actual: %-.9f"
38- % (real_index, golden_data, output_data)
39- )
40 if index == 100:35 if index == 100:
41 break36 break
42 error_ratio = float(different_element_indexes.size) / golden.size37 error_ratio = float(different_element_indexes.size) / golden.size
@@ -23,9 +23,7 @@ error_tol = 1e-3
23def verify_result(output, golden):23def verify_result(output, golden):
24 output = np.fromfile(output, dtype=np.float32).reshape(-1)24 output = np.fromfile(output, dtype=np.float32).reshape(-1)
25 golden = np.fromfile(golden, dtype=np.float32).reshape(-1)25 golden = np.fromfile(golden, dtype=np.float32).reshape(-1)
26- different_element_results = np.isclose(26+ different_element_results = np.isclose(output, golden, rtol=relative_tol, atol=absolute_tol, equal_nan=True)
27- output, golden, rtol=relative_tol, atol=absolute_tol, equal_nan=True
28- )
29 different_element_indexes = np.where(different_element_results == False)[0]27 different_element_indexes = np.where(different_element_results == False)[0]
30 for index in range(len(different_element_indexes)):28 for index in range(len(different_element_indexes)):
31 real_index = different_element_indexes[index]29 real_index = different_element_indexes[index]
@@ -33,12 +31,7 @@ def verify_result(output, golden):
33 output_data = output[real_index]31 output_data = output[real_index]
34 print(32 print(
35 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"33 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"
36- % (34+ % (real_index, golden_data, output_data, abs(output_data - golden_data) / golden_data)
37- real_index,
38- golden_data,
39- output_data,
40- abs(output_data - golden_data) / golden_data,
41- )
42 )35 )
43 if index == 100:36 if index == 100:
44 break37 break
@@ -22,16 +22,12 @@ np.random.seed(9)
22 22 
23def extract_relu_params(relu_pre):23def extract_relu_params(relu_pre):
24 relu_pre = int(relu_pre)24 relu_pre = int(relu_pre)
25- relu_alpha_bits = (25+ relu_alpha_bits = (relu_pre >> 13) & 0xFFFFF # 提取M2的20位[31:13],0xFFFFF是20位掩码
26- relu_pre >> 13
27- ) & 0xFFFFF # 提取M2的20位[31:13],0xFFFFF是20位掩码
28 sign_bit = (relu_alpha_bits >> 18) & 0x126 sign_bit = (relu_alpha_bits >> 18) & 0x1
29 exponent = (relu_alpha_bits >> 10) & 0xFF27 exponent = (relu_alpha_bits >> 10) & 0xFF
30 mantissa = relu_alpha_bits & 0x3FF28 mantissa = relu_alpha_bits & 0x3FF
31 exponent_bias = 127 # 假设指数偏倚量为127,与float32一致29 exponent_bias = 127 # 假设指数偏倚量为127,与float32一致
32- relu_alpha = (30+ relu_alpha = (-1) ** sign_bit * (1 + mantissa / 1024) * (2 ** (exponent - exponent_bias))
33- (-1) ** sign_bit * (1 + mantissa / 1024) * (2 ** (exponent - exponent_bias))
34- )
35 return relu_alpha31 return relu_alpha
36 32 
37 33 
@@ -46,12 +42,8 @@ def extract_quant_params(quant_pre):
46 sign:1位布尔值(0或1)42 sign:1位布尔值(0或1)
47 """43 """
48 quant_pre = int(quant_pre)44 quant_pre = int(quant_pre)
49- quant_alpha_bits = (45+ quant_alpha_bits = (quant_pre >> 13) & 0xFFFFF # 提取M1的20位[31:13],0xFFFFF是20位掩码
50- quant_pre >> 1346+ mode_control_bit = (quant_pre >> 36) & 0x1 # 提取mode_ctrl bit的一位[36],0x1是1位掩码
51- ) & 0xFFFFF # 提取M1的20位[31:13],0xFFFFF是20位掩码
52- mode_control_bit = (
53- quant_pre >> 36
54- ) & 0x1 # 提取mode_ctrl bit的一位[36],0x1是1位掩码
55 offset = (quant_pre >> 37) & 0x1FF # 提取offset的9位[45:37],0x1FF是9位掩码47 offset = (quant_pre >> 37) & 0x1FF # 提取offset的9位[45:37],0x1FF是9位掩码
56 sign = (quant_pre >> 46) & 0x1 # 提取sign的一位[46],0x1是1位掩码48 sign = (quant_pre >> 46) & 0x1 # 提取sign的一位[46],0x1是1位掩码
57 n = (quant_pre >> 32) & 0xF49 n = (quant_pre >> 32) & 0xF
@@ -60,9 +52,7 @@ def extract_quant_params(quant_pre):
60 exponent = (quant_alpha_bits >> 10) & 0xFF52 exponent = (quant_alpha_bits >> 10) & 0xFF
61 mantissa = quant_alpha_bits & 0x3FF53 mantissa = quant_alpha_bits & 0x3FF
62 exponent_bias = 127 # 假设指数偏倚量为127,与float32一致54 exponent_bias = 127 # 假设指数偏倚量为127,与float32一致
63- quant_alpha = (55+ quant_alpha = (-1) ** sign_bit * (1 + mantissa / 1024) * (2 ** (exponent - exponent_bias))
64- (-1) ** sign_bit * (1 + mantissa / 1024) * (2 ** (exponent - exponent_bias))
65- )
66 return quant_alpha, offset, sign, n, mode_control_bit56 return quant_alpha, offset, sign, n, mode_control_bit
67 57 
68 58 
@@ -86,38 +76,27 @@ def deqf16(data, quant_pre, relu_pre):
86 """76 """
87 int32 -> half77 int32 -> half
88 """78 """
89- quant_alpha, offset, sign, n_shift, mode_control_bit = extract_quant_params(79+ quant_alpha, offset, sign, n_shift, mode_control_bit = extract_quant_params(quant_pre)
90- quant_pre
91- )
92 # sign = 080 # sign = 0
93 relu_alpha = extract_relu_params(relu_pre)81 relu_alpha = extract_relu_params(relu_pre)
94 if mode_control_bit == 1:82 if mode_control_bit == 1:
95 data = data >> n_shift83 data = data >> n_shift
96- data = saturation(84+ data = saturation(data, np.finfo(np.float16).min, np.finfo(np.float16).max, np.float16)
97- data, np.finfo(np.float16).min, np.finfo(np.float16).max, np.float16
98- )
99 data = data.astype(np.float32)85 data = data.astype(np.float32)
100 if data >= 0:86 if data >= 0:
101 data = data * quant_alpha87 data = data * quant_alpha
102 else:88 else:
103 data = data * relu_alpha89 data = data * relu_alpha
104- quant_data = (90+ quant_data = saturation(data, np.finfo(np.float16).min, np.finfo(np.float16).max, np.float16) + offset
105- saturation(data, np.finfo(np.float16).min, np.finfo(np.float16).max, np.float16)
106- + offset
107- )
108 91 
109- return saturation(92+ return saturation(quant_data, np.finfo(np.float16).min, np.finfo(np.float16).max, np.float16)
110- quant_data, np.finfo(np.float16).min, np.finfo(np.float16).max, np.float16
111- )
112 93 
113 94 
114def qf322b8_pre(data, quant_pre, relu_pre):95def qf322b8_pre(data, quant_pre, relu_pre):
115 """96 """
116 float32 -> int8/uint897 float32 -> int8/uint8
117 """98 """
118- quant_alpha, offset, sign, n_shift, mode_control_bit = extract_quant_params(99+ quant_alpha, offset, sign, n_shift, mode_control_bit = extract_quant_params(quant_pre)
119- quant_pre
120- )
121 # sign = 0100 # sign = 0
122 relu_alpha = extract_relu_params(relu_pre)101 relu_alpha = extract_relu_params(relu_pre)
123 if mode_control_bit == 1:102 if mode_control_bit == 1:
@@ -140,9 +119,7 @@ def req8_pre(data, quant_pre, relu_pre):
140 """119 """
141 int32 ->int8/uint8120 int32 ->int8/uint8
142 """121 """
143- quant_alpha, offset, sign, n_shift, mode_control_bit = extract_quant_params(122+ quant_alpha, offset, sign, n_shift, mode_control_bit = extract_quant_params(quant_pre)
144- quant_pre
145- )
146 relu_alpha = extract_relu_params(relu_pre)123 relu_alpha = extract_relu_params(relu_pre)
147 if mode_control_bit == 1:124 if mode_control_bit == 1:
148 data = data >> n_shift125 data = data >> n_shift
@@ -173,14 +150,10 @@ def pre_quant_relu(golden, dst_type, m, n, scenarioNum):
173 relu_alpha = struct.unpack("!I", struct.pack("!f", relu_alpha))[0]150 relu_alpha = struct.unpack("!I", struct.pack("!f", relu_alpha))[0]
174 151 
175 # 1 * n 量化系数为全为quant scalar的量化tensor152 # 1 * n 量化系数为全为quant scalar的量化tensor
176- temp_quant_tensor = (153+ temp_quant_tensor = ((quant_scalar * np.ones((1, n), dtype=np.float32)).astype(np.float32))[0]
177- (quant_scalar * np.ones((1, n), dtype=np.float32)).astype(np.float32)
178- )[0]
179 temp_quant_tensor_api = copy.deepcopy(temp_quant_tensor).astype(np.uint64)154 temp_quant_tensor_api = copy.deepcopy(temp_quant_tensor).astype(np.uint64)
180 for i, _ in enumerate(temp_quant_tensor_api):155 for i, _ in enumerate(temp_quant_tensor_api):
181- temp_quant_tensor_api[i] = struct.unpack(156+ temp_quant_tensor_api[i] = struct.unpack("!I", struct.pack("!f", temp_quant_tensor[i]))[0]
182- "!I", struct.pack("!f", temp_quant_tensor[i])
183- )[0]
184 temp_quant_tensor_api[i] = temp_quant_tensor_api[i] | np.uint64(0x400000000000)157 temp_quant_tensor_api[i] = temp_quant_tensor_api[i] | np.uint64(0x400000000000)
185 quant_tensor = np.frombuffer(temp_quant_tensor_api, np.uint64)158 quant_tensor = np.frombuffer(temp_quant_tensor_api, np.uint64)
186 quant_tensor = quant_tensor.astype(np.uint64)159 quant_tensor = quant_tensor.astype(np.uint64)
@@ -195,9 +168,7 @@ def pre_quant_relu(golden, dst_type, m, n, scenarioNum):
195 elif scenarioNum in (3, 4):168 elif scenarioNum in (3, 4):
196 for i in range(m):169 for i in range(m):
197 for j in range(n):170 for j in range(n):
198- quant_golden[i, j] = qf322b8_pre(171+ quant_golden[i, j] = qf322b8_pre(golden[i, j], quant_tensor[j], relu_alpha)
199- golden[i, j], quant_tensor[j], relu_alpha
200- )
201 else:172 else:
202 for i in range(m):173 for i in range(m):
203 for j in range(n):174 for j in range(n):
@@ -240,9 +211,7 @@ def gen_golden_data(scenarioNum):
240 # NZ output211 # NZ output
241 if scenarioNum in (2, 3, 6):212 if scenarioNum in (2, 3, 6):
242 golden = (213 golden = (
243- golden.reshape((int(M / 16), 16, int(N / block_cols), block_cols))214+ golden.reshape((int(M / 16), 16, int(N / block_cols), block_cols)).transpose(2, 0, 1, 3).astype(output_type)
244- .transpose(2, 0, 1, 3)
245- .astype(output_type)
246 )215 )
247 216 
248 if kRound > 1:217 if kRound > 1:
@@ -27,18 +27,13 @@ def verify_result(scenarioNum, output, golden):
27 output_type = np.int827 output_type = np.int8
28 output = np.fromfile(output, dtype=output_type).reshape(-1)28 output = np.fromfile(output, dtype=output_type).reshape(-1)
29 golden = np.fromfile(golden, dtype=output_type).reshape(-1)29 golden = np.fromfile(golden, dtype=output_type).reshape(-1)
30- different_element_results = np.isclose(30+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
31- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
32- )
33 different_element_indexes = np.where(different_element_results == False)[0]31 different_element_indexes = np.where(different_element_results == False)[0]
34 for index in range(len(different_element_indexes)):32 for index in range(len(different_element_indexes)):
35 real_index = different_element_indexes[index]33 real_index = different_element_indexes[index]
36 golden_data = golden[real_index]34 golden_data = golden[real_index]
37 output_data = output[real_index]35 output_data = output[real_index]
38- print(36+ print("data index: %06d, expected: %-.9f, actual: %-.9f" % (real_index, golden_data, output_data))
39- "data index: %06d, expected: %-.9f, actual: %-.9f"
40- % (real_index, golden_data, output_data)
41- )
42 if index == 100:37 if index == 100:
43 break38 break
44 error_ratio = float(different_element_indexes.size) / golden.size39 error_ratio = float(different_element_indexes.size) / golden.size
@@ -102,9 +102,7 @@ def gen_golden_data(scenarioNum=1):
102 data_src_start = outer // 288 * 160 + inner * 80102 data_src_start = outer // 288 * 160 + inner * 80
103 data_src_end = data_src_start + 80103 data_src_end = data_src_start + 80
104 104 
105- golden[dst_start:dst_data_end] = input_flatten[105+ golden[dst_start:dst_data_end] = input_flatten[data_src_start:data_src_end]
106- data_src_start:data_src_end
107- ]
108 golden[dst_data_end:dst_fill_end] = -1106 golden[dst_data_end:dst_fill_end] = -1
109 golden = golden.reshape(1, dst_cols)107 golden = golden.reshape(1, dst_cols)
110 elif scenarioNum == 5:108 elif scenarioNum == 5:
@@ -160,9 +158,7 @@ def gen_golden_data(scenarioNum=1):
160 for dim2 in range(2): # 第2维,搬运2次(loop1)158 for dim2 in range(2): # 第2维,搬运2次(loop1)
161 for dim3 in range(64): # 第3维,搬运64次(从128中选择前64个)159 for dim3 in range(64): # 第3维,搬运64次(从128中选择前64个)
162 # 从第3维的128个中选择第dim3个,搬运第4维的126个数据160 # 从第3维的128个中选择第dim3个,搬运第4维的126个数据
163- golden[golden_row_idx, :126] = input_5d[161+ golden[golden_row_idx, :126] = input_5d[dim0, dim1, dim2, dim3, :]
164- dim0, dim1, dim2, dim3, :
165- ]
166 # 补2个0162 # 补2个0
167 golden[golden_row_idx, 126:128] = 0163 golden[golden_row_idx, 126:128] = 0
168 golden_row_idx += 1164 golden_row_idx += 1
@@ -36,11 +36,7 @@ def verify_result(output, golden, scenarioNum=1):
36 golden = np.fromfile(golden, dtype=output_type).reshape(-1)36 golden = np.fromfile(golden, dtype=output_type).reshape(-1)
37 37 
38 different_element_results = np.isclose(38 different_element_results = np.isclose(
39- output.flatten(),39+ output.flatten(), golden.flatten(), rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
40- golden.flatten(),
41- rtol=RELATIVE_TOL,
42- atol=ABSOLUTE_TOL,
43- equal_nan=True,
44 )40 )
45 different_element_indexes = np.where(different_element_results == False)[0]41 different_element_indexes = np.where(different_element_results == False)[0]
46 42 
@@ -48,10 +44,7 @@ def verify_result(output, golden, scenarioNum=1):
48 real_index = different_element_indexes[index]44 real_index = different_element_indexes[index]
49 golden_data = golden.flatten()[real_index]45 golden_data = golden.flatten()[real_index]
50 output_data = output.flatten()[real_index]46 output_data = output.flatten()[real_index]
51- print(47+ print("data index: %06d, expected: %-.9f, actual: %-.9f" % (real_index, golden_data, output_data))
52- "data index: %06d, expected: %-.9f, actual: %-.9f"
53- % (real_index, golden_data, output_data)
54- )
55 if index == 100:48 if index == 100:
56 break49 break
57 50 
@@ -64,13 +57,7 @@ if __name__ == "__main__":
64 parser = argparse.ArgumentParser()57 parser = argparse.ArgumentParser()
65 parser.add_argument("output_file", help="Output file path")58 parser.add_argument("output_file", help="Output file path")
66 parser.add_argument("golden_file", help="Golden file path")59 parser.add_argument("golden_file", help="Golden file path")
67- parser.add_argument(60+ parser.add_argument("-scenarioNum", type=int, default=1, choices=[1, 2, 3, 4, 5, 6], help="Scenario number")
68- "-scenarioNum",
69- type=int,
70- default=1,
71- choices=[1, 2, 3, 4, 5, 6],
72- help="Scenario number",
73- )
74 args = parser.parse_args()61 args = parser.parse_args()
75 62 
76 try:63 try:
@@ -22,9 +22,7 @@ def nd_to_nz(data_nd, c0size=16):
22 与仓库中其他matmul样例使用相同的标准NZ转换。22 与仓库中其他matmul样例使用相同的标准NZ转换。
23 """23 """
24 rows, cols = data_nd.shape24 rows, cols = data_nd.shape
25- data_nz = data_nd.reshape(25+ data_nz = data_nd.reshape((int(rows / 16), 16, int(cols / c0size), c0size)).transpose(2, 0, 1, 3)
26- (int(rows / 16), 16, int(cols / c0size), c0size)
27- ).transpose(2, 0, 1, 3)
28 return data_nz26 return data_nz
29 27 
30 28 
@@ -45,9 +43,7 @@ def gen_golden_data_simple(scenarioNum=1):
45 x2_nz = nd_to_nz(x2_nd)43 x2_nz = nd_to_nz(x2_nd)
46 44 
47 # 计算golden数据(使用ND格式计算)45 # 计算golden数据(使用ND格式计算)
48- golden = (np.matmul(x1_nd.astype(np.float32), x2_nd.astype(np.float32))).astype(46+ golden = (np.matmul(x1_nd.astype(np.float32), x2_nd.astype(np.float32))).astype(np.float32)
49- np.float32
50- )
51 47 
52 if scenarioNum == 1:48 if scenarioNum == 1:
53 # 保存NZ格式的输入数据49 # 保存NZ格式的输入数据
@@ -41,17 +41,13 @@ def gen_golden_data_gatherb_scenario2():
41 num_output_blocks = total_length // elements_per_block41 num_output_blocks = total_length // elements_per_block
42 num_src_blocks = src_length // elements_per_block42 num_src_blocks = src_length // elements_per_block
43 x = np.random.uniform(0, 1, [1, src_length]).astype(data_type)43 x = np.random.uniform(0, 1, [1, src_length]).astype(data_type)
44- block_indices = np.random.randint(0, num_src_blocks, num_output_blocks).astype(44+ block_indices = np.random.randint(0, num_src_blocks, num_output_blocks).astype(np.uint32)
45- np.uint32
46- )
47 index = block_indices * 3245 index = block_indices * 32
48 x_flat = x.flatten()46 x_flat = x.flatten()
49 golden = np.zeros(total_length, dtype=data_type)47 golden = np.zeros(total_length, dtype=data_type)
50 for i, block_idx in enumerate(block_indices):48 for i, block_idx in enumerate(block_indices):
51 start = block_idx * elements_per_block49 start = block_idx * elements_per_block
52- golden[i * elements_per_block : (i + 1) * elements_per_block] = x_flat[50+ golden[i * elements_per_block : (i + 1) * elements_per_block] = x_flat[start : start + elements_per_block]
53- start : start + elements_per_block
54- ]
55 os.makedirs("input", exist_ok=True)51 os.makedirs("input", exist_ok=True)
56 os.makedirs("output", exist_ok=True)52 os.makedirs("output", exist_ok=True)
57 x.tofile("./input/input_x.bin")53 x.tofile("./input/input_x.bin")
@@ -74,12 +74,6 @@ def gen_golden_data(scenario_num):
74 74 
75if __name__ == "__main__":75if __name__ == "__main__":
76 parser = argparse.ArgumentParser()76 parser = argparse.ArgumentParser()
77- parser.add_argument(77+ parser.add_argument("-scenarioNum", type=int, default=1, choices=[1, 2, 3, 4, 5, 6], help="Scenario number: 1-6")
78- "-scenarioNum",
79- type=int,
80- default=1,
81- choices=[1, 2, 3, 4, 5, 6],
82- help="Scenario number: 1-6",
83- )
84 args = parser.parse_args()78 args = parser.parse_args()
85 gen_golden_data(args.scenarioNum)79 gen_golden_data(args.scenarioNum)
@@ -69,12 +69,6 @@ def gen_golden_data(scenario_num):
69 69 
70if __name__ == "__main__":70if __name__ == "__main__":
71 parser = argparse.ArgumentParser()71 parser = argparse.ArgumentParser()
72- parser.add_argument(72+ parser.add_argument("-scenarioNum", type=int, default=1, choices=[1, 2], help="Scenario number: 1-2")
73- "-scenarioNum",
74- type=int,
75- default=1,
76- choices=[1, 2],
77- help="Scenario number: 1-2",
78- )
79 args = parser.parse_args()73 args = parser.parse_args()
80 gen_golden_data(args.scenarioNum)74 gen_golden_data(args.scenarioNum)
@@ -33,9 +33,7 @@ def gen_golden_data_scenario1():
33 golden = np.zeros(count, dtype=np.float32)33 golden = np.zeros(count, dtype=np.float32)
34 golden[1:129] = x[1:129].copy() # indices 1-128 inclusive34 golden[1:129] = x[1:129].copy() # indices 1-128 inclusive
35 golden.tofile("./output/golden.bin")35 golden.tofile("./output/golden.bin")
36- print(36+ print(f"Scenario 1: Generated input_x.bin ({count} floats) and golden.bin ({count} floats)")
37- f"Scenario 1: Generated input_x.bin ({count} floats) and golden.bin ({count} floats)"
38- )
39 37 
40 38 
41def gen_golden_data_scenario2():39def gen_golden_data_scenario2():
@@ -70,9 +68,7 @@ def gen_golden_data_scenario2():
70 golden[row * results_per_row + block_idx] = block_max68 golden[row * results_per_row + block_idx] = block_max
71 69 
72 golden.tofile("./output/golden.bin")70 golden.tofile("./output/golden.bin")
73- print(71+ print(f"Scenario 2: Generated input_x.bin ({total_elements} uint16) and golden.bin ({len(golden)} uint16)")
74- f"Scenario 2: Generated input_x.bin ({total_elements} uint16) and golden.bin ({len(golden)} uint16)"
75- )
76 72 
77 73 
78if __name__ == "__main__":74if __name__ == "__main__":
@@ -53,9 +53,7 @@ def gen_data_1():
53 os.makedirs("output", exist_ok=True)53 os.makedirs("output", exist_ok=True)
54 cond.tofile("./input/input_cond.bin")54 cond.tofile("./input/input_cond.bin")
55 golden.tofile("./output/golden.bin")55 golden.tofile("./output/golden.bin")
56- print(56+ print(f"Scenario 1: Generated input_cond.bin ({total_length} int32) and golden.bin (32 uint8)")
57- f"Scenario 1: Generated input_cond.bin ({total_length} int32) and golden.bin (32 uint8)"
58- )
59 57 
60 58 
61def gen_data_2():59def gen_data_2():
@@ -85,9 +83,7 @@ def gen_data_2():
85 y.tofile("./input/input_y.bin")83 y.tofile("./input/input_y.bin")
86 mask.tofile("./input/input_mask.bin")84 mask.tofile("./input/input_mask.bin")
87 golden.tofile("./output/golden.bin")85 golden.tofile("./output/golden.bin")
88- print(86+ print(f"Scenario 2: Generated input_x.bin, input_y.bin, input_mask.bin and golden.bin ({total_length} float)")
89- f"Scenario 2: Generated input_x.bin, input_y.bin, input_mask.bin and golden.bin ({total_length} float)"
90- )
91 87 
92 88 
93def gen_golden_data(scenario_num):89def gen_golden_data(scenario_num):
@@ -18,11 +18,7 @@ import numpy as np
18 18 
19def get_range_by_dtype(input_type):19def get_range_by_dtype(input_type):
20 try:20 try:
21- if (21+ if input_type == np.float16 or input_type == np.float32 or input_type == np.float64:
22- input_type == np.float16
23- or input_type == np.float32
24- or input_type == np.float64
25- ):
26 return np.finfo(input_type).min, np.finfo(input_type).max22 return np.finfo(input_type).min, np.finfo(input_type).max
27 else:23 else:
28 return np.iinfo(input_type).min, np.iinfo(input_type).max24 return np.iinfo(input_type).min, np.iinfo(input_type).max
@@ -43,8 +39,7 @@ def gen_golden_data_simple():
43 golden = np.zeros(output_shape).astype(output_type)39 golden = np.zeros(output_shape).astype(output_type)
44 for i in range(len(input_x)):40 for i in range(len(input_x)):
45 golden[41 golden[
46- i * (one_data_block_size // input_type.itemsize) : (i + 1)42+ i * (one_data_block_size // input_type.itemsize) : (i + 1) * (one_data_block_size // input_type.itemsize)
47- * (one_data_block_size // input_type.itemsize)
48 ] = input_x[i]43 ] = input_x[i]
49 os.makedirs("input", exist_ok=True)44 os.makedirs("input", exist_ok=True)
50 os.makedirs("output", exist_ok=True)45 os.makedirs("output", exist_ok=True)
@@ -25,18 +25,13 @@ def verify_result(output, golden):
25 output_type = np.uint1625 output_type = np.uint16
26 output = np.fromfile(output, dtype=output_type).reshape(-1)26 output = np.fromfile(output, dtype=output_type).reshape(-1)
27 golden = np.fromfile(golden, dtype=output_type).reshape(-1)27 golden = np.fromfile(golden, dtype=output_type).reshape(-1)
28- different_element_results = np.isclose(28+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
29- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
30- )
31 different_element_indexes = np.where(different_element_results == False)[0]29 different_element_indexes = np.where(different_element_results == False)[0]
32 for index in range(len(different_element_indexes)):30 for index in range(len(different_element_indexes)):
33 real_index = different_element_indexes[index]31 real_index = different_element_indexes[index]
34 golden_data = golden[real_index]32 golden_data = golden[real_index]
35 output_data = output[real_index]33 output_data = output[real_index]
36- print(34+ print("data index: %06d, expected: %-.9f, actual: %-.9f" % (real_index, golden_data, output_data))
37- "data index: %06d, expected: %-.9f, actual: %-.9f"
38- % (real_index, golden_data, output_data)
39- )
40 if index == 100:35 if index == 100:
41 break36 break
42 error_ratio = float(different_element_indexes.size) / golden.size37 error_ratio = float(different_element_indexes.size) / golden.size
@@ -19,11 +19,7 @@ import argparse
19 19 
20def get_range_by_dtype(input_type):20def get_range_by_dtype(input_type):
21 try:21 try:
22- if (22+ if input_type == np.float16 or input_type == np.float32 or input_type == np.float64:
23- input_type == np.float16
24- or input_type == np.float32
25- or input_type == np.float64
26- ):
27 return np.finfo(input_type).min, np.finfo(input_type).max23 return np.finfo(input_type).min, np.finfo(input_type).max
28 else:24 else:
29 return np.iinfo(input_type).min, np.iinfo(input_type).max25 return np.iinfo(input_type).min, np.iinfo(input_type).max
@@ -25,18 +25,13 @@ def verify_result(output, golden):
25 output_type = np.int3225 output_type = np.int32
26 output = np.fromfile(output, dtype=output_type).reshape(-1)26 output = np.fromfile(output, dtype=output_type).reshape(-1)
27 golden = np.fromfile(golden, dtype=output_type).reshape(-1)27 golden = np.fromfile(golden, dtype=output_type).reshape(-1)
28- different_element_results = np.isclose(28+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
29- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
30- )
31 different_element_indexes = np.where(different_element_results == False)[0]29 different_element_indexes = np.where(different_element_results == False)[0]
32 for index in range(len(different_element_indexes)):30 for index in range(len(different_element_indexes)):
33 real_index = different_element_indexes[index]31 real_index = different_element_indexes[index]
34 golden_data = golden[real_index]32 golden_data = golden[real_index]
35 output_data = output[real_index]33 output_data = output[real_index]
36- print(34+ print("data index: %06d, expected: %-.9f, actual: %-.9f" % (real_index, golden_data, output_data))
37- "data index: %06d, expected: %-.9f, actual: %-.9f"
38- % (real_index, golden_data, output_data)
39- )
40 if index == 100:35 if index == 100:
41 break36 break
42 error_ratio = float(different_element_indexes.size) / golden.size37 error_ratio = float(different_element_indexes.size) / golden.size
@@ -29,11 +29,7 @@ class CMPMODE(Enum):
29 29 
30def get_range_by_dtype(input_type):30def get_range_by_dtype(input_type):
31 try:31 try:
32- if (32+ if input_type == np.float16 or input_type == np.float32 or input_type == np.float64:
33- input_type == np.float16
34- or input_type == np.float32
35- or input_type == np.float64
36- ):
37 return np.finfo(input_type).min, np.finfo(input_type).max33 return np.finfo(input_type).min, np.finfo(input_type).max
38 else:34 else:
39 return np.iinfo(input_type).min, np.iinfo(input_type).max35 return np.iinfo(input_type).min, np.iinfo(input_type).max
@@ -30,18 +30,13 @@ def verify_result(output, golden, scenario_num):
30 if scenario_num == 2:30 if scenario_num == 2:
31 output = output[0:8]31 output = output[0:8]
32 golden = golden[0:8]32 golden = golden[0:8]
33- different_element_results = np.isclose(33+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
34- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
35- )
36 different_element_indexes = np.where(different_element_results == False)[0]34 different_element_indexes = np.where(different_element_results == False)[0]
37 for index in range(len(different_element_indexes)):35 for index in range(len(different_element_indexes)):
38 real_index = different_element_indexes[index]36 real_index = different_element_indexes[index]
39 golden_data = golden[real_index]37 golden_data = golden[real_index]
40 output_data = output[real_index]38 output_data = output[real_index]
41- print(39+ print("data index: %06d, expected: %-.9f, actual: %-.9f" % (real_index, golden_data, output_data))
42- "data index: %06d, expected: %-.9f, actual: %-.9f"
43- % (real_index, golden_data, output_data)
44- )
45 if index == 100:40 if index == 100:
46 break41 break
47 error_ratio = float(different_element_indexes.size) / golden.size42 error_ratio = float(different_element_indexes.size) / golden.size
@@ -24,9 +24,7 @@ error_tol = 1e-3
24def verify_result(output, golden):24def verify_result(output, golden):
25 output = np.fromfile(output, dtype=np.int16).reshape(-1)25 output = np.fromfile(output, dtype=np.int16).reshape(-1)
26 golden = np.fromfile(golden, dtype=np.int16).reshape(-1)26 golden = np.fromfile(golden, dtype=np.int16).reshape(-1)
27- different_element_results = np.isclose(27+ different_element_results = np.isclose(output, golden, rtol=relative_tol, atol=absolute_tol, equal_nan=True)
28- output, golden, rtol=relative_tol, atol=absolute_tol, equal_nan=True
29- )
30 different_element_indexes = np.where(different_element_results == False)[0]28 different_element_indexes = np.where(different_element_results == False)[0]
31 for index in range(len(different_element_indexes)):29 for index in range(len(different_element_indexes)):
32 real_index = different_element_indexes[index]30 real_index = different_element_indexes[index]
@@ -34,12 +32,7 @@ def verify_result(output, golden):
34 output_data = output[real_index]32 output_data = output[real_index]
35 print(33 print(
36 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"34 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"
37- % (35+ % (real_index, golden_data, output_data, abs(output_data - golden_data) / golden_data)
38- real_index,
39- golden_data,
40- output_data,
41- abs(output_data - golden_data) / golden_data,
42- )
43 )36 )
44 if index == 100:37 if index == 100:
45 break38 break
@@ -18,11 +18,7 @@ import numpy as np
18 18 
19def get_range_by_dtype(input_type):19def get_range_by_dtype(input_type):
20 try:20 try:
21- if (21+ if input_type == np.float16 or input_type == np.float32 or input_type == np.float64:
22- input_type == np.float16
23- or input_type == np.float32
24- or input_type == np.float64
25- ):
26 return np.finfo(input_type).min, np.finfo(input_type).max22 return np.finfo(input_type).min, np.finfo(input_type).max
27 else:23 else:
28 return np.iinfo(input_type).min, np.iinfo(input_type).max24 return np.iinfo(input_type).min, np.iinfo(input_type).max
@@ -25,18 +25,13 @@ def verify_result(output, golden):
25 output_type = np.float1625 output_type = np.float16
26 output = np.fromfile(output, dtype=output_type).reshape(-1)26 output = np.fromfile(output, dtype=output_type).reshape(-1)
27 golden = np.fromfile(golden, dtype=output_type).reshape(-1)27 golden = np.fromfile(golden, dtype=output_type).reshape(-1)
28- different_element_results = np.isclose(28+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
29- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
30- )
31 different_element_indexes = np.where(different_element_results == False)[0]29 different_element_indexes = np.where(different_element_results == False)[0]
32 for index in range(len(different_element_indexes)):30 for index in range(len(different_element_indexes)):
33 real_index = different_element_indexes[index]31 real_index = different_element_indexes[index]
34 golden_data = golden[real_index]32 golden_data = golden[real_index]
35 output_data = output[real_index]33 output_data = output[real_index]
36- print(34+ print("data index: %06d, expected: %-.9f, actual: %-.9f" % (real_index, golden_data, output_data))
37- "data index: %06d, expected: %-.9f, actual: %-.9f"
38- % (real_index, golden_data, output_data)
39- )
40 if index == 100:35 if index == 100:
41 break36 break
42 error_ratio = float(different_element_indexes.size) / golden.size37 error_ratio = float(different_element_indexes.size) / golden.size
@@ -24,9 +24,7 @@ error_tol = 1e-3
24def verify_result(output, golden):24def verify_result(output, golden):
25 output = np.fromfile(output, dtype=np.float16).reshape(-1)25 output = np.fromfile(output, dtype=np.float16).reshape(-1)
26 golden = np.fromfile(golden, dtype=np.float16).reshape(-1)26 golden = np.fromfile(golden, dtype=np.float16).reshape(-1)
27- different_element_results = np.isclose(27+ different_element_results = np.isclose(output, golden, rtol=relative_tol, atol=absolute_tol, equal_nan=True)
28- output, golden, rtol=relative_tol, atol=absolute_tol, equal_nan=True
29- )
30 different_element_indexes = np.where(different_element_results == False)[0]28 different_element_indexes = np.where(different_element_results == False)[0]
31 for index in range(len(different_element_indexes)):29 for index in range(len(different_element_indexes)):
32 real_index = different_element_indexes[index]30 real_index = different_element_indexes[index]
@@ -34,12 +32,7 @@ def verify_result(output, golden):
34 output_data = output[real_index]32 output_data = output[real_index]
35 print(33 print(
36 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"34 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"
37- % (35+ % (real_index, golden_data, output_data, abs(output_data - golden_data) / golden_data)
38- real_index,
39- golden_data,
40- output_data,
41- abs(output_data - golden_data) / golden_data,
42- )
43 )36 )
44 if index == 100:37 if index == 100:
45 break38 break
@@ -56,19 +56,11 @@ def gen_golden_data(scenario=1):
56 input1.tofile("./input/input1.bin")56 input1.tofile("./input/input1.bin")
57 golden.tofile("./output/golden.bin")57 golden.tofile("./output/golden.bin")
58 58 
59- print(59+ print(f"Generated data for scenario {scenario}: input0 shape={input0.shape}, dtype={input0.dtype}")
60- f"Generated data for scenario {scenario}: input0 shape={input0.shape}, dtype={input0.dtype}"
61- )
62 60 
63 61 
64if __name__ == "__main__":62if __name__ == "__main__":
65 parser = argparse.ArgumentParser()63 parser = argparse.ArgumentParser()
66- parser.add_argument(64+ parser.add_argument("-scenarioNum", type=int, default=1, choices=[1, 2], help="Scenario number: 1=AddRelu, 2=Axpy")
67- "-scenarioNum",
68- type=int,
69- default=1,
70- choices=[1, 2],
71- help="Scenario number: 1=AddRelu, 2=Axpy",
72- )
73 args = parser.parse_args()65 args = parser.parse_args()
74 gen_golden_data(args.scenarioNum)66 gen_golden_data(args.scenarioNum)
@@ -31,17 +31,12 @@ def verify_result(output_path, golden_path, dtype=np.float16):
31 different_element_indexes = np.where(output != golden)[0]31 different_element_indexes = np.where(output != golden)[0]
32 for index in range(len(different_element_indexes)):32 for index in range(len(different_element_indexes)):
33 real_index = different_element_indexes[index]33 real_index = different_element_indexes[index]
34- print(34+ print("data index: %06d, expected: %d, actual: %d" % (real_index, golden[real_index], output[real_index]))
35- "data index: %06d, expected: %d, actual: %d"
36- % (real_index, golden[real_index], output[real_index])
37- )
38 if index == 100:35 if index == 100:
39 break36 break
40 else:37 else:
41 # 浮点类型使用isclose比较38 # 浮点类型使用isclose比较
42- different_element_results = np.isclose(39+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
43- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
44- )
45 different_element_indexes = np.where(different_element_results == False)[0]40 different_element_indexes = np.where(different_element_results == False)[0]
46 for index in range(len(different_element_indexes)):41 for index in range(len(different_element_indexes)):
47 real_index = different_element_indexes[index]42 real_index = different_element_indexes[index]
@@ -68,13 +63,7 @@ def verify_result(output_path, golden_path, dtype=np.float16):
68 63 
69if __name__ == "__main__":64if __name__ == "__main__":
70 parser = argparse.ArgumentParser()65 parser = argparse.ArgumentParser()
71- parser.add_argument(66+ parser.add_argument("-scenario", type=int, default=1, choices=[1, 2], help="Scenario number: 1=AddRelu, 2=Axpy")
72- "-scenario",
73- type=int,
74- default=1,
75- choices=[1, 2],
76- help="Scenario number: 1=AddRelu, 2=Axpy",
77- )
78 parser.add_argument("output", help="Output file path")67 parser.add_argument("output", help="Output file path")
79 parser.add_argument("golden", help="Golden file path")68 parser.add_argument("golden", help="Golden file path")
80 args = parser.parse_args()69 args = parser.parse_args()
@@ -27,9 +27,7 @@ def verify_result(output, golden):
27 output = np.fromfile(output, dtype=output_type).reshape(-1)27 output = np.fromfile(output, dtype=output_type).reshape(-1)
28 golden = np.fromfile(golden, dtype=output_type).reshape(-1)28 golden = np.fromfile(golden, dtype=output_type).reshape(-1)
29 29 
30- different = np.isclose(30+ different = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
31- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
32- )
33 31 
34 diff_idx = np.where(different == False)[0]32 diff_idx = np.where(different == False)[0]
35 33 
@@ -19,11 +19,7 @@ import numpy as np
19 19 
20def get_range_by_dtype(input_type):20def get_range_by_dtype(input_type):
21 try:21 try:
22- if (22+ if input_type == np.float16 or input_type == np.float32 or input_type == np.float64:
23- input_type == np.float16
24- or input_type == np.float32
25- or input_type == np.float64
26- ):
27 return np.finfo(input_type).min, np.finfo(input_type).max23 return np.finfo(input_type).min, np.finfo(input_type).max
28 else:24 else:
29 return np.iinfo(input_type).min, np.iinfo(input_type).max25 return np.iinfo(input_type).min, np.iinfo(input_type).max
@@ -30,25 +30,16 @@ def verify_result(output, golden, scenarioNum):
30 golden = np.fromfile(golden, dtype=output_type).reshape(-1)30 golden = np.fromfile(golden, dtype=output_type).reshape(-1)
31 if scenarioNum == 1:31 if scenarioNum == 1:
32 different_element_results = np.isclose(32 different_element_results = np.isclose(
33- output[0:128],33+ output[0:128], golden[0:128], rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
34- golden[0:128],
35- rtol=RELATIVE_TOL,
36- atol=ABSOLUTE_TOL,
37- equal_nan=True,
38 )34 )
39 else:35 else:
40- different_element_results = np.isclose(36+ different_element_results = np.isclose(output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
41- output, golden, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
42- )
43 different_element_indexes = np.where(different_element_results == False)[0]37 different_element_indexes = np.where(different_element_results == False)[0]
44 for index in range(len(different_element_indexes)):38 for index in range(len(different_element_indexes)):
45 real_index = different_element_indexes[index]39 real_index = different_element_indexes[index]
46 golden_data = golden[real_index]40 golden_data = golden[real_index]
47 output_data = output[real_index]41 output_data = output[real_index]
48- print(42+ print("data index: %06d, expected: %-.9f, actual: %-.9f" % (real_index, golden_data, output_data))
49- "data index: %06d, expected: %-.9f, actual: %-.9f"
50- % (real_index, golden_data, output_data)
51- )
52 if index == 100:43 if index == 100:
53 break44 break
54 error_ratio = float(different_element_indexes.size) / golden.size45 error_ratio = float(different_element_indexes.size) / golden.size
@@ -28,26 +28,16 @@ def verify_result(output0, output1, golden0, golden1):
28 golden0 = np.fromfile(golden0, dtype=output_type).reshape(-1)28 golden0 = np.fromfile(golden0, dtype=output_type).reshape(-1)
29 golden1 = np.fromfile(golden1, dtype=output_type).reshape(-1)29 golden1 = np.fromfile(golden1, dtype=output_type).reshape(-1)
30 30 
31- different0 = np.isclose(31+ different0 = np.isclose(output0, golden0, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
32- output0, golden0, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True32+ different1 = np.isclose(output1, golden1, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True)
33- )
34- different1 = np.isclose(
35- output1, golden1, rtol=RELATIVE_TOL, atol=ABSOLUTE_TOL, equal_nan=True
36- )
37 33 
38 diff_idx0 = np.where(different0 == False)[0]34 diff_idx0 = np.where(different0 == False)[0]
39 diff_idx1 = np.where(different1 == False)[0]35 diff_idx1 = np.where(different1 == False)[0]
40 36 
41 for idx in diff_idx0[:100]:37 for idx in diff_idx0[:100]:
42- print(38+ print("dst0 index: %06d, expected: %-.9f, actual: %-.9f" % (idx, golden0[idx], output0[idx]))
43- "dst0 index: %06d, expected: %-.9f, actual: %-.9f"
44- % (idx, golden0[idx], output0[idx])
45- )
46 for idx in diff_idx1[:100]:39 for idx in diff_idx1[:100]:
47- print(40+ print("dst1 index: %06d, expected: %-.9f, actual: %-.9f" % (idx, golden1[idx], output1[idx]))
48- "dst1 index: %06d, expected: %-.9f, actual: %-.9f"
49- % (idx, golden1[idx], output1[idx])
50- )
51 41 
52 error_ratio = float(diff_idx0.size + diff_idx1.size) / (golden0.size + golden1.size)42 error_ratio = float(diff_idx0.size + diff_idx1.size) / (golden0.size + golden1.size)
53 print("error ratio: %.4f, tolerance: %.4f" % (error_ratio, ERROR_TOL))43 print("error ratio: %.4f, tolerance: %.4f" % (error_ratio, ERROR_TOL))
@@ -70,12 +70,6 @@ def gen_golden_data(scenarioNum=1):
70 70 
71if __name__ == "__main__":71if __name__ == "__main__":
72 parser = argparse.ArgumentParser()72 parser = argparse.ArgumentParser()
73- parser.add_argument(73+ parser.add_argument("-scenarioNum", type=int, default=1, choices=range(1, 4), help="scenario number (1-3)")
74- "-scenarioNum",
75- type=int,
76- default=1,
77- choices=range(1, 4),
78- help="scenario number (1-3)",
79- )
80 args = parser.parse_args()74 args = parser.parse_args()
81 gen_golden_data(args.scenarioNum)75 gen_golden_data(args.scenarioNum)
@@ -41,19 +41,13 @@ def verify_result(scenario_num, output_path, golden_path):
41 output = np.fromfile(output_path, dtype=np.float32).reshape(-1)41 output = np.fromfile(output_path, dtype=np.float32).reshape(-1)
42 golden = np.fromfile(golden_path, dtype=np.float32).reshape(-1)42 golden = np.fromfile(golden_path, dtype=np.float32).reshape(-1)
43 43 
44- assert output.size >= total_floats, (44+ assert output.size >= total_floats, f"output size {output.size} < expected {total_floats}"
45- f"output size {output.size} < expected {total_floats}"45+ assert golden.size >= total_floats, f"golden size {golden.size} < expected {total_floats}"
46- )
47- assert golden.size >= total_floats, (
48- f"golden size {golden.size} < expected {total_floats}"
49- )
50 46 
51 output = output[:total_floats]47 output = output[:total_floats]
52 golden = golden[:total_floats]48 golden = golden[:total_floats]
53 49 
54- different_element_results = np.isclose(50+ different_element_results = np.isclose(output, golden, rtol=relative_tol, atol=absolute_tol, equal_nan=True)
55- output, golden, rtol=relative_tol, atol=absolute_tol, equal_nan=True
56- )
57 different_element_indexes = np.where(different_element_results == False)[0]51 different_element_indexes = np.where(different_element_results == False)[0]
58 for index in range(len(different_element_indexes)):52 for index in range(len(different_element_indexes)):
59 real_index = different_element_indexes[index]53 real_index = different_element_indexes[index]
@@ -61,12 +55,7 @@ def verify_result(scenario_num, output_path, golden_path):
61 output_data = output[real_index]55 output_data = output[real_index]
62 print(56 print(
63 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"57 "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f"
64- % (58+ % (real_index, golden_data, output_data, abs(output_data - golden_data) / golden_data)
65- real_index,
66- golden_data,
67- output_data,
68- abs(output_data - golden_data) / golden_data,
69- )
70 )59 )
71 if index == 100:60 if index == 100:
72 break61 break
@@ -77,13 +66,7 @@ def verify_result(scenario_num, output_path, golden_path):
77 66 
78if __name__ == "__main__":67if __name__ == "__main__":
79 parser = argparse.ArgumentParser()68 parser = argparse.ArgumentParser()
80- parser.add_argument(69+ parser.add_argument("-scenarioNum", type=int, default=1, choices=range(1, 4), help="scenario number (1-3)")
81- "-scenarioNum",
82- type=int,
83- default=1,
84- choices=range(1, 4),
85- help="scenario number (1-3)",
86- )
87 parser.add_argument("output", type=str, help="output bin file path")70 parser.add_argument("output", type=str, help="output bin file path")
88 parser.add_argument("golden", type=str, help="golden bin file path")71 parser.add_argument("golden", type=str, help="golden bin file path")
89 args = parser.parse_args()72 args = parser.parse_args()
@@ -18,11 +18,7 @@ import argparse
18 18 
19def get_range_by_dtype(input_type):19def get_range_by_dtype(input_type):
20 try:20 try:
21- if (21+ if input_type == np.float16 or input_type == np.float32 or input_type == np.float64:
22- input_type == np.float16
23- or input_type == np.float32
24- or input_type == np.float64
25- ):
26 return np.finfo(input_type).min, np.finfo(input_type).max22 return np.finfo(input_type).min, np.finfo(input_type).max
27 else:23 else:
28 return np.iinfo(input_type).min, np.iinfo(input_type).max24 return np.iinfo(input_type).min, np.iinfo(input_type).max