Merged
feat(pypto-pro): Support infer kernel type in pypto_compile_op #6614
poorwillAdd on Sep 22
feat(pypto-pro): Support infer kernel type in pypto_compile_op #6614
Merged
Merged fromwangbing110/pyptomasterintocann/pyptomaster
Total: 2 files changed+299-31
| @@ -29,8 +29,8 @@ concrete key, while **reusing the real ``asc_op_compile_base`` backend leaves** | |||
| 29 | - ``fatbin_objs`` — the fat ``.o`` link; | 29 | - ``fatbin_objs`` — the fat ``.o`` link; |
| 30 | - ``CommonUtility.get_kernel_meta_dir`` / ``get_distinct_filename_tag`` — the real flat ``kernel_meta`` layout. | 30 | - ``CommonUtility.get_kernel_meta_dir`` / ``get_distinct_filename_tag`` — the real flat ``kernel_meta`` layout. |
| 31 | 31 | ||
| 32 | -Artifacts land flat in ``kernel_meta`` exactly like the real compiler: ``<kernel>_mix_aic_<tk>.o`` / | 32 | +Artifacts land flat in ``kernel_meta`` exactly like the real compiler: one per-key object for every |
| 33 | -``<kernel>_mix_aiv_<tk>.o`` per (tilingkey, core), one fat ``<kernel>.o`` and ``<kernel>.json``. Per-key | 33 | +physical core required by the inferred kernel type, one fat ``<kernel>.o`` and ``<kernel>.json``. Per-key |
| 34 | codegen output (``kernel.cpp`` + the per-key wrapper ``.cpp``) is kept on disk for debugging (not cleaned). | 34 | codegen output (``kernel.cpp`` + the per-key wrapper ``.cpp``) is kept on disk for debugging (not cleaned). |
| 35 | The json path is recorded via ``op_context.add_build_res("json_file_path", ...)`` so the reused | 35 | The json path is recorded via ``op_context.add_build_res("json_file_path", ...)`` so the reused |
| 36 | ``SingleOpCompile`` picks it up and ``SingleOpPostCompile`` appends ``supportInfo``. | 36 | ``SingleOpCompile`` picks it up and ``SingleOpPostCompile`` appends ``supportInfo``. |
| @@ -73,6 +73,7 @@ from asc_op_compile_base.asc_op_compiler.ascendc_constants import ( | |||
| 73 | MIX_CORE_MACRO, | 73 | MIX_CORE_MACRO, |
| 74 | TILING_KEY_MACRO, | 74 | TILING_KEY_MACRO, |
| 75 | CompileOptionTuple, | 75 | CompileOptionTuple, |
| 76 | + KernelMetaType, | ||
| 76 | ) | 77 | ) |
| 77 | from asc_op_compile_base.asc_op_compiler.compile_op import ( | 78 | from asc_op_compile_base.asc_op_compiler.compile_op import ( |
| 78 | _add_op_compile_options_by_customized_json, | 79 | _add_op_compile_options_by_customized_json, |
| @@ -90,6 +91,7 @@ from asc_op_compile_base.common.context import op_context | |||
| 90 | from asc_op_compile_base.common.utils import log as logger | 91 | from asc_op_compile_base.common.utils import log as logger |
| 91 | 92 | ||
| 92 | from pypto_pro import DataType | 93 | from pypto_pro import DataType |
| 94 | +from pypto_pro.runtime.compile_config import get_jit_compile_config | ||
| 93 | 95 | ||
| 94 | from ..._errors import ( | 96 | from ..._errors import ( |
| 95 | InvalidArgument, | 97 | InvalidArgument, |
| @@ -101,11 +103,17 @@ from ..._errors import ( | |||
| 101 | message_of, | 103 | message_of, |
| 102 | ) | 104 | ) |
| 103 | 105 | ||
| 104 | -# (AscendC core channel, kernel symbol/meta suffix, compile-make suffix) | 106 | +# (AscendC core channel, object suffix, compile-make suffix) |
| 105 | -_MIX_CORE_COMPILE_TARGETS = ( | 107 | +_CORE_COMPILE_TARGETS = { |
| 106 | - (CORE_TYPE_CUBE, "mix_aic", "aic"), | 108 | + "cube": (CORE_TYPE_CUBE, "mix_aic", "aic"), |
| 107 | - (CORE_TYPE_VEC, "mix_aiv", "aiv"), | 109 | + "vec": (CORE_TYPE_VEC, "mix_aiv", "aiv"), |
| 108 | -) | 110 | +} |
| 111 | +_KERNEL_TYPE_BY_CORE_RATIO = { | ||
| 112 | + (1, 0): KernelMetaType.KERNEL_TYPE_AIC_ONLY, | ||
| 113 | + (0, 1): KernelMetaType.KERNEL_TYPE_AIV_ONLY, | ||
| 114 | + (1, 1): KernelMetaType.KERNEL_TYPE_MIX_AIC_1_1, | ||
| 115 | + (1, 2): KernelMetaType.KERNEL_TYPE_MIX_AIC_1_2, | ||
| 116 | +} | ||
| 109 | _ORIG_DTYPE_TO_PYPTO = { | 117 | _ORIG_DTYPE_TO_PYPTO = { |
| 110 | "dt_bool": DataType.BOOL, | 118 | "dt_bool": DataType.BOOL, |
| 111 | "dt_int8": DataType.INT8, | 119 | "dt_int8": DataType.INT8, |
| @@ -239,7 +247,65 @@ def _signature_parts(entry_params): | |||
| 239 | return ", ".join(decls), names | 247 | return ", ".join(decls), names |
| 240 | 248 | ||
| 241 | 249 | ||
| 242 | -def _gen_infer_cpp(cg, tilingkey_header: str, kernel_cpp: str) -> str: | 250 | +def _kernel_type_from_target(target) -> KernelMetaType: |
| 251 | + kernel_type = _KERNEL_TYPE_BY_CORE_RATIO.get((target.aic_per_block, target.aiv_per_block)) | ||
| 252 | + if kernel_type is None: | ||
| 253 | + raise InvalidVal( | ||
| 254 | + "PyPTO OPC does not support the resolved core ratio " | ||
| 255 | + f"{target.aic_per_block}:{target.aiv_per_block}" | ||
| 256 | + ) | ||
| 257 | + return kernel_type | ||
| 258 | + | ||
| 259 | + | ||
| 260 | +def _kernel_type_from_codegen(cg, arch: str) -> KernelMetaType: | ||
| 261 | + target = get_jit_compile_config().resolve_kernel_target( | ||
| 262 | + arch, | ||
| 263 | + has_cube=cg.has_cube, | ||
| 264 | + has_vector=cg.has_vector, | ||
| 265 | + ) | ||
| 266 | + return _kernel_type_from_target(target) | ||
| 267 | + | ||
| 268 | + | ||
| 269 | +def _compile_core_names(kernel_type: KernelMetaType) -> tuple[str, ...]: | ||
| 270 | + if kernel_type == KernelMetaType.KERNEL_TYPE_AIC_ONLY: | ||
| 271 | + return ("cube",) | ||
| 272 | + if kernel_type == KernelMetaType.KERNEL_TYPE_AIV_ONLY: | ||
| 273 | + return ("vec",) | ||
| 274 | + if kernel_type in { | ||
| 275 | + KernelMetaType.KERNEL_TYPE_MIX_AIC_1_1, | ||
| 276 | + KernelMetaType.KERNEL_TYPE_MIX_AIC_1_2, | ||
| 277 | + }: | ||
| 278 | + return ("cube", "vec") | ||
| 279 | + raise InvalidVal(f"PyPTO OPC does not support kernel type {kernel_type.name}") | ||
| 280 | + | ||
| 281 | + | ||
| 282 | +def _compile_target_name(kernel_name: str, tiling_key: str, core_name: str, kernel_type: KernelMetaType) -> str: | ||
| 283 | + target_name = f"{kernel_name}_{tiling_key}" | ||
| 284 | + if kernel_type in { | ||
| 285 | + KernelMetaType.KERNEL_TYPE_MIX_AIC_1_1, | ||
| 286 | + KernelMetaType.KERNEL_TYPE_MIX_AIC_1_2, | ||
| 287 | + }: | ||
| 288 | + target_name += "_mix_aic" if core_name == "cube" else "_mix_aiv" | ||
| 289 | + return target_name | ||
| 290 | + | ||
| 291 | + | ||
| 292 | +def _compile_target_options(kernel_type: KernelMetaType, enable_c310_rules: bool) -> list[str]: | ||
| 293 | + options = [] | ||
| 294 | + if kernel_type in { | ||
| 295 | + KernelMetaType.KERNEL_TYPE_MIX_AIC_1_1, | ||
| 296 | + KernelMetaType.KERNEL_TYPE_MIX_AIC_1_2, | ||
| 297 | + }: | ||
| 298 | + options.append(f"-D{MIX_CORE_MACRO}=1") | ||
| 299 | + if kernel_type == KernelMetaType.KERNEL_TYPE_MIX_AIC_1_1: | ||
| 300 | + options.append("-D__MIX_CORE_AIC_RATION__=1") | ||
| 301 | + if enable_c310_rules and kernel_type == KernelMetaType.KERNEL_TYPE_MIX_AIC_1_2: | ||
| 302 | + options.append("-D__ASCENDC_ENABLE_VEC_TAIL_TILING_COPY__") | ||
| 303 | + if enable_c310_rules and kernel_type == KernelMetaType.KERNEL_TYPE_AIC_ONLY: | ||
| 304 | + options.append("-DRAW_AIC_ONLY_DUMP_TENSOR") | ||
| 305 | + return options | ||
| 306 | + | ||
| 307 | + | ||
| 308 | +def _gen_infer_cpp(cg, tilingkey_header: str, kernel_cpp: str, kernel_type: KernelMetaType) -> str: | ||
| 243 | sig, names = _signature_parts(cg.entry_params) | 309 | sig, names = _signature_parts(cg.entry_params) |
| 244 | ws_idx = len(names) - 2 if len(names) >= 2 else None | 310 | ws_idx = len(names) - 2 if len(names) >= 2 else None |
| 245 | inner = list(names) | 311 | inner = list(names) |
| @@ -256,7 +322,7 @@ def _gen_infer_cpp(cg, tilingkey_header: str, kernel_cpp: str) -> str: | |||
| 256 | f"{kernel_cpp}\n" | 322 | f"{kernel_cpp}\n" |
| 257 | f'extern "C" __global__ AICORE void {cg.kernel_name}({sig})\n' | 323 | f'extern "C" __global__ AICORE void {cg.kernel_name}({sig})\n' |
| 258 | "{\n" | 324 | "{\n" |
| 259 | - f" KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_MIX_AIC_1_2);\n" | 325 | + f" KERNEL_TASK_TYPE_DEFAULT({kernel_type.name});\n" |
| 260 | f"{ws_lines}" | 326 | f"{ws_lines}" |
| 261 | f" {cg.kernel_name}_impl({', '.join(inner)});\n" | 327 | f" {cg.kernel_name}_impl({', '.join(inner)});\n" |
| 262 | "}\n" | 328 | "}\n" |
| @@ -271,7 +337,7 @@ def _prepare_infer_cpp( | |||
| 271 | cce_file: str, | 337 | cce_file: str, |
| 272 | kernel_name: str, | 338 | kernel_name: str, |
| 273 | kernel_meta_dir: str, | 339 | kernel_meta_dir: str, |
| 274 | -) -> str: | 340 | +) -> tuple[str, KernelMetaType]: |
| 275 | """Generate the infer source from one default legal TilingKey and prepare its headers.""" | 341 | """Generate the infer source from one default legal TilingKey and prepare its headers.""" |
| 276 | from pypto_pro.runtime.jit import _codegen | 342 | from pypto_pro.runtime.jit import _codegen |
| 277 | 343 | ||
| @@ -289,6 +355,7 @@ def _prepare_infer_cpp( | |||
| 289 | ) | 355 | ) |
| 290 | if infer_cg is None: | 356 | if infer_cg is None: |
| 291 | raise RuntimeFailure(f"pypto codegen failed for default tilingkey {default_tiling_key}") | 357 | raise RuntimeFailure(f"pypto codegen failed for default tilingkey {default_tiling_key}") |
| 358 | + kernel_type = _kernel_type_from_codegen(infer_cg, arch) | ||
| 292 | for header in (name for name in os.listdir(infer_cg.build_dir) if name.endswith(".h")): | 359 | for header in (name for name in os.listdir(infer_cg.build_dir) if name.endswith(".h")): |
| 293 | shutil.copyfile(os.path.join(infer_cg.build_dir, header), os.path.join(kernel_meta_dir, header)) | 360 | shutil.copyfile(os.path.join(infer_cg.build_dir, header), os.path.join(kernel_meta_dir, header)) |
| 294 | tilingkey_header = f"{schema.cls_name}_tilingkey.h" | 361 | tilingkey_header = f"{schema.cls_name}_tilingkey.h" |
| @@ -298,8 +365,8 @@ def _prepare_infer_cpp( | |||
| 298 | ) | 365 | ) |
| 299 | kernel_cpp = Path(infer_cg.build_dir, "kernel.cpp").read_text(encoding="utf-8") | 366 | kernel_cpp = Path(infer_cg.build_dir, "kernel.cpp").read_text(encoding="utf-8") |
| 300 | infer_cpp_path = os.path.join(kernel_meta_dir, f"{kernel_name}_pypto_infer.cpp") | 367 | infer_cpp_path = os.path.join(kernel_meta_dir, f"{kernel_name}_pypto_infer.cpp") |
| 301 | - _write(infer_cpp_path, _gen_infer_cpp(infer_cg, tilingkey_header, kernel_cpp)) | 368 | + _write(infer_cpp_path, _gen_infer_cpp(infer_cg, tilingkey_header, kernel_cpp, kernel_type)) |
| 302 | - return infer_cpp_path | 369 | + return infer_cpp_path, kernel_type |
| 303 | 370 | ||
| 304 | 371 | ||
| 305 | def generate_binary_headers(kernel, arch="a5") -> str: | 372 | def generate_binary_headers(kernel, arch="a5") -> str: |
| @@ -458,7 +525,27 @@ def _prepare_dfx(op_info, tiling_info: TilingInfo, compile_info: CompileInfo) -> | |||
| 458 | return dfx_op_info | 525 | return dfx_op_info |
| 459 | 526 | ||
| 460 | 527 | ||
| 461 | -def _build_compile_info(cce_file, kernel_name, origin_func_name, op_info, infered_info_from_ifile, compile_log_path): | 528 | +def _build_tiling_info(op_info, infered_info_from_ifile, value_depend, origin_func_name): |
| 529 | + tiling_info = get_tiling_info_by_tiling( | ||
| 530 | + op_info, | ||
| 531 | + infered_info_from_ifile, | ||
| 532 | + value_depend, | ||
| 533 | + origin_func_name, | ||
| 534 | + ) | ||
| 535 | + if infered_info_from_ifile.default_kernel_type == KernelMetaType.KERNEL_TYPE_MIX_AIC_1_1: | ||
| 536 | + tiling_info.task_ration = 1 | ||
| 537 | + return tiling_info | ||
| 538 | + | ||
| 539 | + | ||
| 540 | +def _build_compile_info( | ||
| 541 | + cce_file, | ||
| 542 | + kernel_name, | ||
| 543 | + origin_func_name, | ||
| 544 | + op_info, | ||
| 545 | + infered_info_from_ifile, | ||
| 546 | + tiling_key_list, | ||
| 547 | + compile_log_path, | ||
| 548 | +): | ||
| 462 | compile_info = CompileInfo() | 549 | compile_info = CompileInfo() |
| 463 | compile_info.src_file = cce_file | 550 | compile_info.src_file = cce_file |
| 464 | compile_info.dst_file = os.path.join(CommonUtility.get_kernel_meta_dir(), f"{kernel_name}.o") | 551 | compile_info.dst_file = os.path.join(CommonUtility.get_kernel_meta_dir(), f"{kernel_name}.o") |
| @@ -466,7 +553,7 @@ def _build_compile_info(cce_file, kernel_name, origin_func_name, op_info, infere | |||
| 466 | compile_info.origin_func_name = origin_func_name | 553 | compile_info.origin_func_name = origin_func_name |
| 467 | compile_info.op_type = _op_info_get(op_info, "op_type") | 554 | compile_info.op_type = _op_info_get(op_info, "op_type") |
| 468 | compile_info.code_channel = infered_info_from_ifile.code_channel | 555 | compile_info.code_channel = infered_info_from_ifile.code_channel |
| 469 | - compile_info.tiling_key_list = infered_info_from_ifile.tiling_key_list | 556 | + compile_info.tiling_key_list = tiling_key_list |
| 470 | compile_info.tiling_key_group_map = infered_info_from_ifile.tiling_key_group_map | 557 | compile_info.tiling_key_group_map = infered_info_from_ifile.tiling_key_group_map |
| 471 | compile_info.compile_log_path = compile_log_path | 558 | compile_info.compile_log_path = compile_log_path |
| 472 | compile_info.hard_sync = infered_info_from_ifile.hard_sync | 559 | compile_info.hard_sync = infered_info_from_ifile.hard_sync |
| @@ -505,13 +592,16 @@ def _gen_meta_sections(kernel_name, packed, tiling_info: TilingInfo, compile_inf | |||
| 505 | dfx_generator = DFXSectionGenerator() | 592 | dfx_generator = DFXSectionGenerator() |
| 506 | dfx_generator.gen_dfx_struct_flag = False | 593 | dfx_generator.gen_dfx_struct_flag = False |
| 507 | old_sub_core_type = compile_info.sub_core_type | 594 | old_sub_core_type = compile_info.sub_core_type |
| 508 | - for core_type, channel, _short in _MIX_CORE_COMPILE_TARGETS: | 595 | + tiling_key = str(packed) |
| 509 | - section_kernel = f"{kernel_name}_{packed}_{channel}" | 596 | + kernel_type = compile_info.tiling_key_kernel_type[tiling_key] |
| 597 | + for core_name in _compile_core_names(kernel_type): | ||
| 598 | + core_type, _channel, _short = _CORE_COMPILE_TARGETS[core_name] | ||
| 599 | + section_kernel = _compile_target_name(kernel_name, tiling_key, core_name, kernel_type) | ||
| 510 | out.append( | 600 | out.append( |
| 511 | get_ktype_section_variable( | 601 | get_ktype_section_variable( |
| 512 | f"{section_kernel}_section", | 602 | f"{section_kernel}_section", |
| 513 | section_kernel, | 603 | section_kernel, |
| 514 | - compile_info.tiling_key_kernel_type[str(packed)], | 604 | + kernel_type, |
| 515 | ) | 605 | ) |
| 516 | ) | 606 | ) |
| 517 | compile_info.sub_core_type = core_type | 607 | compile_info.sub_core_type = core_type |
| @@ -528,6 +618,7 @@ def _gen_key_src(kernel_cpp_path, origin_func, impl_name, entry_params, kernel_n | |||
| 528 | sig, names = _signature_parts(entry_params) | 618 | sig, names = _signature_parts(entry_params) |
| 529 | _ws_idx = len(names) - 2 if len(names) >= 2 else None | 619 | _ws_idx = len(names) - 2 if len(names) >= 2 else None |
| 530 | inner = list(names) | 620 | inner = list(names) |
| 621 | + kernel_type = compile_info.tiling_key_kernel_type[str(packed)] | ||
| 531 | return ( | 622 | return ( |
| 532 | '#include "kernel_operator.h"\n' | 623 | '#include "kernel_operator.h"\n' |
| 533 | f'#include "{kernel_cpp_path}"\n' | 624 | f'#include "{kernel_cpp_path}"\n' |
| @@ -537,7 +628,7 @@ def _gen_key_src(kernel_cpp_path, origin_func, impl_name, entry_params, kernel_n | |||
| 537 | "}\n" | 628 | "}\n" |
| 538 | f'extern "C" __global__ AICORE void auto_gen_{origin_func}_kernel({sig})\n' | 629 | f'extern "C" __global__ AICORE void auto_gen_{origin_func}_kernel({sig})\n' |
| 539 | "{\n" | 630 | "{\n" |
| 540 | - f" KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_MIX_AIC_1_2);\n" | 631 | + f" KERNEL_TASK_TYPE_DEFAULT({kernel_type.name});\n" |
| 541 | f" ascendc_auto_gen_{origin_func}_kernel({', '.join(names)});\n" | 632 | f" ascendc_auto_gen_{origin_func}_kernel({', '.join(names)});\n" |
| 542 | "}\n" + _gen_meta_sections(kernel_name, packed, tiling_info, compile_info) | 633 | "}\n" + _gen_meta_sections(kernel_name, packed, tiling_info, compile_info) |
| 543 | ) | 634 | ) |
| @@ -618,7 +709,7 @@ def pypto_compile_op( | |||
| 618 | if global_var_storage.get_variable("ascendc_compile_debug_config"): | 709 | if global_var_storage.get_variable("ascendc_compile_debug_config"): |
| 619 | compile_log_path = os.path.join(kernel_meta_dir, kernel_name + distinct_tag + ".log") | 710 | compile_log_path = os.path.join(kernel_meta_dir, kernel_name + distinct_tag + ".log") |
| 620 | 711 | ||
| 621 | - infer_cpp_path = _prepare_infer_cpp( | 712 | + infer_cpp_path, kernel_type = _prepare_infer_cpp( |
| 622 | kernel, | 713 | kernel, |
| 623 | schema, | 714 | schema, |
| 624 | dtype_consts, | 715 | dtype_consts, |
| @@ -640,7 +731,7 @@ def pypto_compile_op( | |||
| 640 | ) | 731 | ) |
| 641 | is_const_propagation = "-DFORCE_TILING_CONST_PROPAGATION" in opt.compile_options | 732 | is_const_propagation = "-DFORCE_TILING_CONST_PROPAGATION" in opt.compile_options |
| 642 | global_var_storage.set_variable("ascendc_tiling_const_propagation", is_const_propagation) | 733 | global_var_storage.set_variable("ascendc_tiling_const_propagation", is_const_propagation) |
| 643 | - tiling_info = get_tiling_info_by_tiling( | 734 | + tiling_info = _build_tiling_info( |
| 644 | op_info, | 735 | op_info, |
| 645 | infered_info, | 736 | infered_info, |
| 646 | extend_options.get("valueDepend"), | 737 | extend_options.get("valueDepend"), |
| @@ -650,8 +741,15 @@ def pypto_compile_op( | |||
| 650 | tiling_info.save_file(tiling_data_file_path) | 741 | tiling_info.save_file(tiling_data_file_path) |
| 651 | global_var_storage.set_variable("ascendc_is_static_op", tiling_info.static_shape_flag) | 742 | global_var_storage.set_variable("ascendc_is_static_op", tiling_info.static_shape_flag) |
| 652 | tiling_keys = _filter_tiling_keys(infered_info.tiling_key_list, extend_options, ctx, kernel_name) | 743 | tiling_keys = _filter_tiling_keys(infered_info.tiling_key_list, extend_options, ctx, kernel_name) |
| 653 | - compile_info = _build_compile_info(cce_file, kernel_name, origin_func_name, op_info, infered_info, compile_log_path) | 744 | + compile_info = _build_compile_info( |
| 654 | - compile_info.tiling_key_list = tiling_keys | 745 | + cce_file, |
| 746 | + kernel_name, | ||
| 747 | + origin_func_name, | ||
| 748 | + op_info, | ||
| 749 | + infered_info, | ||
| 750 | + tiling_keys, | ||
| 751 | + compile_log_path, | ||
| 752 | + ) | ||
| 655 | logger.info( | 753 | logger.info( |
| 656 | "pypto_compile_op: op=%s kernel_name=%s arch=%s keys=%d dtype=%s -> %s", | 754 | "pypto_compile_op: op=%s kernel_name=%s arch=%s keys=%d dtype=%s -> %s", |
| 657 | _op_info_get(op_info, "op_type"), | 755 | _op_info_get(op_info, "op_type"), |
| @@ -664,7 +762,8 @@ def pypto_compile_op( | |||
| 664 | 762 | ||
| 665 | op_info = _prepare_dfx(op_info, tiling_info, compile_info) | 763 | op_info = _prepare_dfx(op_info, tiling_info, compile_info) |
| 666 | obj_files: list[str] = [] | 764 | obj_files: list[str] = [] |
| 667 | - cmds_by_core: dict[str, list] = {ch: [] for _, ch, _short in _MIX_CORE_COMPILE_TARGETS} | 765 | + cmds_by_core: dict[str, list] = {core: [] for core in _CORE_COMPILE_TARGETS} |
| 766 | + tiling_keys_by_core: dict[str, list] = {core: [] for core in _CORE_COMPILE_TARGETS} | ||
| 668 | compile_options_ready = False | 767 | compile_options_ready = False |
| 669 | for tiling_key in tiling_keys: | 768 | for tiling_key in tiling_keys: |
| 670 | packed = int(tiling_key) | 769 | packed = int(tiling_key) |
| @@ -684,6 +783,11 @@ def pypto_compile_op( | |||
| 684 | ) | 783 | ) |
| 685 | if cg is None: | 784 | if cg is None: |
| 686 | raise RuntimeFailure(f"pypto codegen failed for tilingkey {packed}") | 785 | raise RuntimeFailure(f"pypto codegen failed for tilingkey {packed}") |
| 786 | + if _kernel_type_from_codegen(cg, arch) != kernel_type: | ||
| 787 | + raise InvalidOperation( | ||
| 788 | + "PyPTO OPC requires one kernel type for all tiling keys, but codegen " | ||
| 789 | + f"resolved a different type for tiling key {packed}" | ||
| 790 | + ) | ||
| 687 | origin_func = cg.kernel_name | 791 | origin_func = cg.kernel_name |
| 688 | impl_name = f"{origin_func}_impl" | 792 | impl_name = f"{origin_func}_impl" |
| 689 | if not compile_options_ready: | 793 | if not compile_options_ready: |
| @@ -708,24 +812,31 @@ def pypto_compile_op( | |||
| 708 | src_path = os.path.join(kernel_meta_dir, f"{kernel_name}_{packed}_kernel.cpp") | 812 | src_path = os.path.join(kernel_meta_dir, f"{kernel_name}_{packed}_kernel.cpp") |
| 709 | _write(src_path, src) | 813 | _write(src_path, src) |
| 710 | 814 | ||
| 711 | - for core_type, channel, _short in _MIX_CORE_COMPILE_TARGETS: | 815 | + for core_name in _compile_core_names(kernel_type): |
| 816 | + core_type, channel, _short = _CORE_COMPILE_TARGETS[core_name] | ||
| 712 | dst_o = os.path.join(kernel_meta_dir, f"{kernel_name}_{channel}_{packed}.o") | 817 | dst_o = os.path.join(kernel_meta_dir, f"{kernel_name}_{channel}_{packed}.o") |
| 713 | sub_arch = _core_arch(core_type) | 818 | sub_arch = _core_arch(core_type) |
| 714 | cmd = gen_compile_cmd_v220(src_path, dst_o, opt, sub_arch, "") # kernel.cpp self-includes its tiling.h | 819 | cmd = gen_compile_cmd_v220(src_path, dst_o, opt, sub_arch, "") # kernel.cpp self-includes its tiling.h |
| 715 | cmd += [f"-D{TILING_KEY_MACRO}={packed}UL"] | 820 | cmd += [f"-D{TILING_KEY_MACRO}={packed}UL"] |
| 716 | - cmd += [f"-D{MIX_CORE_MACRO}=1"] | 821 | + cmd += _compile_target_options(kernel_type, CommonUtility.is_c310()) |
| 717 | - if CommonUtility.is_c310(): | 822 | + target_name = _compile_target_name(kernel_name, str(packed), core_name, kernel_type) |
| 718 | - cmd += ["-D__ASCENDC_ENABLE_VEC_TAIL_TILING_COPY__"] | 823 | + cmd += [f"-Dauto_gen_{origin_func}_kernel={target_name}"] |
| 719 | - cmd += [f"-Dauto_gen_{origin_func}_kernel={kernel_name}_{packed}_{channel}"] | ||
| 720 | cmd += [f"-D{impl_name}={impl_name}_{packed}"] | 824 | cmd += [f"-D{impl_name}={impl_name}_{packed}"] |
| 721 | - cmds_by_core[channel].append(cmd) | 825 | + cmds_by_core[core_name].append(cmd) |
| 826 | + tiling_keys_by_core[core_name].append(packed) | ||
| 722 | obj_files.append(dst_o) | 827 | obj_files.append(dst_o) |
| 723 | 828 | ||
| 724 | # Per-key compile via the real backend: compile_multi_tilingkey writes <op>_tmp_<core>_<pid>.mk (one | 829 | # Per-key compile via the real backend: compile_multi_tilingkey writes <op>_tmp_<core>_<pid>.mk (one |
| 725 | # target per tilingkey, +a -E precompile line under dump_cce) and runs `make -j`, exactly as | 830 | # target per tilingkey, +a -E precompile line under dump_cce) and runs `make -j`, exactly as |
| 726 | # compile_op's compile_kernel_and_meta does. One .mk per core (aic/aiv), mirroring the golden layout. | 831 | # compile_op's compile_kernel_and_meta does. One .mk per core (aic/aiv), mirroring the golden layout. |
| 727 | - for _core_type, channel, short in _MIX_CORE_COMPILE_TARGETS: | 832 | + for core_name, (_core_type, _channel, short) in _CORE_COMPILE_TARGETS.items(): |
| 728 | - compile_multi_tilingkey(tiling_keys, cmds_by_core[channel], f"{kernel_name}_tmp_{short}", compile_log_path) | 833 | + if cmds_by_core[core_name]: |
| 834 | + compile_multi_tilingkey( | ||
| 835 | + tiling_keys_by_core[core_name], | ||
| 836 | + cmds_by_core[core_name], | ||
| 837 | + f"{kernel_name}_tmp_{short}", | ||
| 838 | + compile_log_path, | ||
| 839 | + ) | ||
| 729 | # compile_multi_tilingkey swallows make failures unless build-log is enabled; verify every obj landed. | 840 | # compile_multi_tilingkey swallows make failures unless build-log is enabled; verify every obj landed. |
| 730 | missing = [o for o in obj_files if not os.path.exists(o)] | 841 | missing = [o for o in obj_files if not os.path.exists(o)] |
| 731 | if missing: | 842 | if missing: |
| @@ -16,10 +16,20 @@ from dataclasses import dataclass | |||
| 16 | import importlib | 16 | import importlib |
| 17 | import os | 17 | import os |
| 18 | from pathlib import Path | 18 | from pathlib import Path |
| 19 | +from types import SimpleNamespace | ||
| 19 | 20 | ||
| 21 | +from asc_op_compile_base.asc_op_compiler.ascendc_constants import KernelMetaType | ||
| 20 | from pypto_pro._errors import InvalidArgument | 22 | from pypto_pro._errors import InvalidArgument |
| 21 | import pypto_pro.language as pl | 23 | import pypto_pro.language as pl |
| 22 | from pypto_pro.runtime.opc.pypto_compile import ( | 24 | from pypto_pro.runtime.opc.pypto_compile import ( |
| 25 | + _build_compile_info, | ||
| 26 | + _build_tiling_info, | ||
| 27 | + _compile_core_names, | ||
| 28 | + _compile_target_name, | ||
| 29 | + _compile_target_options, | ||
| 30 | + _gen_infer_cpp, | ||
| 31 | + _kernel_type_from_codegen, | ||
| 32 | + _kernel_type_from_target, | ||
| 23 | _load_kernel, | 33 | _load_kernel, |
| 24 | generate_binary_headers, | 34 | generate_binary_headers, |
| 25 | prepare_binary_headers, | 35 | prepare_binary_headers, |
| @@ -127,6 +137,153 @@ def test_pypto_compile_op_sets_up_explicit_arch(monkeypatch): | |||
| 127 | assert received_arch == ["a5"] | 137 | assert received_arch == ["a5"] |
| 128 | 138 | ||
| 129 | 139 | ||
| 140 | +@pytest.mark.parametrize( | ||
| 141 | + "aic_per_block,aiv_per_block,expected", | ||
| 142 | + [ | ||
| 143 | + (1, 0, KernelMetaType.KERNEL_TYPE_AIC_ONLY), | ||
| 144 | + (0, 1, KernelMetaType.KERNEL_TYPE_AIV_ONLY), | ||
| 145 | + (1, 1, KernelMetaType.KERNEL_TYPE_MIX_AIC_1_1), | ||
| 146 | + (1, 2, KernelMetaType.KERNEL_TYPE_MIX_AIC_1_2), | ||
| 147 | + ], | ||
| 148 | +) | ||
| 149 | +def test_kernel_type_from_target_uses_configured_core_ratio(aic_per_block, aiv_per_block, expected): | ||
| 150 | + target = SimpleNamespace(aic_per_block=aic_per_block, aiv_per_block=aiv_per_block) | ||
| 151 | + | ||
| 152 | + assert _kernel_type_from_target(target) is expected | ||
| 153 | + | ||
| 154 | + | ||
| 155 | +@pytest.mark.parametrize( | ||
| 156 | + "has_cube,has_vector,expected", | ||
| 157 | + [ | ||
| 158 | + (True, False, KernelMetaType.KERNEL_TYPE_AIC_ONLY), | ||
| 159 | + (False, True, KernelMetaType.KERNEL_TYPE_AIV_ONLY), | ||
| 160 | + (True, True, KernelMetaType.KERNEL_TYPE_MIX_AIC_1_2), | ||
| 161 | + ], | ||
| 162 | +) | ||
| 163 | +def test_kernel_type_from_codegen_reuses_a5_target_config(has_cube, has_vector, expected): | ||
| 164 | + codegen_result = SimpleNamespace(has_cube=has_cube, has_vector=has_vector) | ||
| 165 | + | ||
| 166 | + assert _kernel_type_from_codegen(codegen_result, "a5") is expected | ||
| 167 | + | ||
| 168 | + | ||
| 169 | +@pytest.mark.parametrize( | ||
| 170 | + "kernel_type,expected", | ||
| 171 | + [ | ||
| 172 | + (KernelMetaType.KERNEL_TYPE_AIC_ONLY, ("cube",)), | ||
| 173 | + (KernelMetaType.KERNEL_TYPE_AIV_ONLY, ("vec",)), | ||
| 174 | + (KernelMetaType.KERNEL_TYPE_MIX_AIC_1_1, ("cube", "vec")), | ||
| 175 | + (KernelMetaType.KERNEL_TYPE_MIX_AIC_1_2, ("cube", "vec")), | ||
| 176 | + ], | ||
| 177 | +) | ||
| 178 | +def test_compile_core_names_follow_kernel_type(kernel_type, expected): | ||
| 179 | + assert _compile_core_names(kernel_type) == expected | ||
| 180 | + | ||
| 181 | + | ||
| 182 | +def test_compile_target_name_uses_mix_suffix_only_for_mixed_kernel(): | ||
| 183 | + assert _compile_target_name("probe", "3", "cube", KernelMetaType.KERNEL_TYPE_AIC_ONLY) == "probe_3" | ||
| 184 | + assert ( | ||
| 185 | + _compile_target_name("probe", "3", "cube", KernelMetaType.KERNEL_TYPE_MIX_AIC_1_2) | ||
| 186 | + == "probe_3_mix_aic" | ||
| 187 | + ) | ||
| 188 | + assert ( | ||
| 189 | + _compile_target_name("probe", "3", "vec", KernelMetaType.KERNEL_TYPE_MIX_AIC_1_2) | ||
| 190 | + == "probe_3_mix_aiv" | ||
| 191 | + ) | ||
| 192 | + | ||
| 193 | + | ||
| 194 | +@pytest.mark.parametrize( | ||
| 195 | + "kernel_type,expected", | ||
| 196 | + [ | ||
| 197 | + (KernelMetaType.KERNEL_TYPE_AIC_ONLY, ["-DRAW_AIC_ONLY_DUMP_TENSOR"]), | ||
| 198 | + (KernelMetaType.KERNEL_TYPE_AIV_ONLY, []), | ||
| 199 | + ( | ||
| 200 | + KernelMetaType.KERNEL_TYPE_MIX_AIC_1_1, | ||
| 201 | + ["-D__MIX_CORE_MACRO__=1", "-D__MIX_CORE_AIC_RATION__=1"], | ||
| 202 | + ), | ||
| 203 | + ( | ||
| 204 | + KernelMetaType.KERNEL_TYPE_MIX_AIC_1_2, | ||
| 205 | + ["-D__MIX_CORE_MACRO__=1", "-D__ASCENDC_ENABLE_VEC_TAIL_TILING_COPY__"], | ||
| 206 | + ), | ||
| 207 | + ], | ||
| 208 | +) | ||
| 209 | +def test_compile_target_options_match_c310_compile_op(kernel_type, expected): | ||
| 210 | + assert _compile_target_options(kernel_type, enable_c310_rules=True) == expected | ||
| 211 | + | ||
| 212 | + | ||
| 213 | +def test_infer_cpp_uses_resolved_kernel_type(): | ||
| 214 | + codegen_result = SimpleNamespace(entry_params=[], kernel_name="probe") | ||
| 215 | + | ||
| 216 | + source = _gen_infer_cpp( | ||
| 217 | + codegen_result, | ||
| 218 | + "ProbeKey_tilingkey.h", | ||
| 219 | + "", | ||
| 220 | + KernelMetaType.KERNEL_TYPE_AIV_ONLY, | ||
| 221 | + ) | ||
| 222 | + | ||
| 223 | + assert "KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_AIV_ONLY);" in source | ||
| 224 | + assert "KERNEL_TYPE_MIX_AIC_1_2" not in source | ||
| 225 | + | ||
| 226 | + | ||
| 227 | +@pytest.mark.parametrize( | ||
| 228 | + "kernel_type,expected_task_ration", | ||
| 229 | + [ | ||
| 230 | + (KernelMetaType.KERNEL_TYPE_MIX_AIC_1_1, 1), | ||
| 231 | + (KernelMetaType.KERNEL_TYPE_MIX_AIC_1_2, 2), | ||
| 232 | + (KernelMetaType.KERNEL_TYPE_AIV_ONLY, 2), | ||
| 233 | + ], | ||
| 234 | +) | ||
| 235 | +def test_build_tiling_info_uses_inferred_kernel_type(monkeypatch, kernel_type, expected_task_ration): | ||
| 236 | + compile_module = importlib.import_module("pypto_pro.runtime.opc.pypto_compile") | ||
| 237 | + tiling_info = SimpleNamespace(task_ration=2) | ||
| 238 | + monkeypatch.setattr(compile_module, "get_tiling_info_by_tiling", lambda *args: tiling_info) | ||
| 239 | + | ||
| 240 | + result = _build_tiling_info({}, SimpleNamespace(default_kernel_type=kernel_type), None, "probe") | ||
| 241 | + | ||
| 242 | + assert result.task_ration == expected_task_ration | ||
| 243 | + | ||
| 244 | + | ||
| 245 | +def test_build_compile_info_uses_inferred_kernel_types_and_filtered_keys(monkeypatch, tmp_path): | ||
| 246 | + compile_module = importlib.import_module("pypto_pro.runtime.opc.pypto_compile") | ||
| 247 | + inferred_kernel_types = { | ||
| 248 | + "1": KernelMetaType.KERNEL_TYPE_AIV_ONLY, | ||
| 249 | + "2": KernelMetaType.KERNEL_TYPE_AIV_ONLY, | ||
| 250 | + } | ||
| 251 | + inferred_info = SimpleNamespace( | ||
| 252 | + code_channel=1, | ||
| 253 | + tiling_key_list=["1", "2"], | ||
| 254 | + tiling_key_group_map={}, | ||
| 255 | + hard_sync=False, | ||
| 256 | + enable_deterministic=False, | ||
| 257 | + tiling_key_deterministic={}, | ||
| 258 | + tiling_key_kernel_type=inferred_kernel_types, | ||
| 259 | + no_set_kernel_type=False, | ||
| 260 | + default_kernel_type=KernelMetaType.KERNEL_TYPE_AIV_ONLY, | ||
| 261 | + template_tiling_info=None, | ||
| 262 | + tiling_key_struct_map={}, | ||
| 263 | + register_tiling_struct=set(), | ||
| 264 | + tpl_tiling_struct=set(), | ||
| 265 | + ) | ||
| 266 | + monkeypatch.setattr(compile_module.CommonUtility, "get_kernel_meta_dir", lambda: str(tmp_path)) | ||
| 267 | + monkeypatch.setattr(compile_module, "handle_sk_codegen_options", lambda *args: None) | ||
| 268 | + | ||
| 269 | + compile_info = _build_compile_info( | ||
| 270 | + "probe.cpp", | ||
| 271 | + "probe", | ||
| 272 | + "probe_origin", | ||
| 273 | + {"op_type": "Probe"}, | ||
| 274 | + inferred_info, | ||
| 275 | + ["2"], | ||
| 276 | + None, | ||
| 277 | + ) | ||
| 278 | + | ||
| 279 | + assert compile_info.tiling_key_list == ["2"] | ||
| 280 | + assert compile_info.default_kernel_type == KernelMetaType.KERNEL_TYPE_AIV_ONLY | ||
| 281 | + assert compile_info.no_set_kernel_type is False | ||
| 282 | + assert compile_info.tiling_key_kernel_type is inferred_kernel_types | ||
| 283 | + assert compile_info.raw_tiling_key_kernel_type == inferred_kernel_types | ||
| 284 | + assert compile_info.raw_tiling_key_kernel_type is not inferred_kernel_types | ||
| 285 | + | ||
| 286 | + | ||
| 130 | @pytest.mark.parametrize("kernel_count", [0, 2]) | 287 | @pytest.mark.parametrize("kernel_count", [0, 2]) |
| 131 | def test_load_kernel_requires_exactly_one_jit_kernel(tmp_path, kernel_count): | 288 | def test_load_kernel_requires_exactly_one_jit_kernel(tmp_path, kernel_count): |
| 132 | definitions = "\n".join( | 289 | definitions = "\n".join( |