merge feat/deepseek-v41-dual-entry into master
50 file(s) changed+10585-5
Mcmake/scripts/pypto_codegen.py+39-2
| @@ -11,13 +11,14 @@ | |||
| 11 | """ | 11 | """ |
| 12 | PyPTO kernel codegen driver. | 12 | PyPTO kernel codegen driver. |
| 13 | 13 | ||
| 14 | -This script is invoked from CMake configure stage for operators marked with enable_pypto_kernel(<op_file>). | 14 | +Generate AI Core binary headers or a native AICPU fast-launch library for CMake. |
| 15 | -It runs host-side binary artifact preparation and copies all generated artifacts into --out-dir. | 15 | +Generated artifacts go into --out-dir. |
| 16 | """ | 16 | """ |
| 17 | 17 | ||
| 18 | import argparse | 18 | import argparse |
| 19 | import importlib.util | 19 | import importlib.util |
| 20 | import logging | 20 | import logging |
| 21 | +import re | ||
| 21 | import shutil | 22 | import shutil |
| 22 | import sys | 23 | import sys |
| 23 | from pathlib import Path | 24 | from pathlib import Path |
| @@ -75,6 +76,37 @@ def _prepare_binary_headers(py_file: Path, arch: str) -> Path: | |||
| 75 | ).resolve() | 76 | ).resolve() |
| 76 | 77 | ||
| 77 | 78 | ||
| 79 | +def _build_native_aicpu(py_file: Path, out_dir: Path, op_file: str, arch: str): | ||
| 80 | + """Compile the AICPU launcher and emit its C ABI declaration.""" | ||
| 81 | + from pypto_pro.runtime.aicpu_compile import ( | ||
| 82 | + _parse_aicpu_program, build_aicpu_jit_so, generate_aicpu_bare_body, generate_aicpu_sources, | ||
| 83 | + ) | ||
| 84 | + from pypto_pro.runtime.platform import normalize_arch | ||
| 85 | + | ||
| 86 | + spec = importlib.util.spec_from_file_location(py_file.stem, py_file) | ||
| 87 | + module = importlib.util.module_from_spec(spec) | ||
| 88 | + sys.modules[spec.name] = module | ||
| 89 | + spec.loader.exec_module(module) | ||
| 90 | + kernel = getattr(module, op_file) | ||
| 91 | + arch = normalize_arch(arch) | ||
| 92 | + _, program, params = _parse_aicpu_program(kernel) | ||
| 93 | + body = generate_aicpu_bare_body(program, arch, kernel.__name__) | ||
| 94 | + sources = generate_aicpu_sources(body, params, kernel.__name__) | ||
| 95 | + | ||
| 96 | + kernel_source = out_dir / f"{op_file}_aicpu.cpp" | ||
| 97 | + host_source = out_dir / f"{op_file}_aicpu_host.cpp" | ||
| 98 | + kernel_source.write_text(sources["kernel_source"], encoding="utf-8") | ||
| 99 | + host_source.write_text(sources["host_source"], encoding="utf-8") | ||
| 100 | + declaration = re.search(r'extern "C" uint32_t \w+Run\([^{}]+\)', sources["host_source"]) | ||
| 101 | + if declaration is None: | ||
| 102 | + raise RuntimeError("native AICPU codegen did not emit its Run ABI") | ||
| 103 | + (out_dir / f"{op_file}_native.h").write_text( | ||
| 104 | + "// Generated from the PyPTO native host ABI.\n#pragma once\n#include <stdint.h>\n" | ||
| 105 | + + declaration.group() + ";\n", encoding="utf-8") | ||
| 106 | + build_aicpu_jit_so(str(out_dir), op_file, kernel_source, host_source, | ||
| 107 | + out_dir / f"lib{op_file}.so", arch, 600) | ||
| 108 | + | ||
| 109 | + | ||
| 78 | def main(): | 110 | def main(): |
| 79 | parser = argparse.ArgumentParser(description="PyPTO kernel codegen driver") | 111 | parser = argparse.ArgumentParser(description="PyPTO kernel codegen driver") |
| 80 | parser.add_argument("--py-file", required=True, help="kernel python source file") | 112 | parser.add_argument("--py-file", required=True, help="kernel python source file") |
| @@ -87,6 +119,7 @@ def main(): | |||
| 87 | parser.add_argument( | 119 | parser.add_argument( |
| 88 | "--soc", required=True, help="soc version (e.g. ascend950), mapped to pypto arch" | 120 | "--soc", required=True, help="soc version (e.g. ascend950), mapped to pypto arch" |
| 89 | ) | 121 | ) |
| 122 | + parser.add_argument("--aicpu-native", action="store_true", help="build a native AICPU library") | ||
| 90 | args = parser.parse_args() | 123 | args = parser.parse_args() |
| 91 | 124 | ||
| 92 | arch = _ARCH_MAP.get(args.soc) | 125 | arch = _ARCH_MAP.get(args.soc) |
| @@ -100,6 +133,10 @@ def main(): | |||
| 100 | out_dir = Path(args.out_dir) | 133 | out_dir = Path(args.out_dir) |
| 101 | out_dir.mkdir(parents=True, exist_ok=True) | 134 | out_dir.mkdir(parents=True, exist_ok=True) |
| 102 | 135 | ||
| 136 | + if args.aicpu_native: | ||
| 137 | + _build_native_aicpu(py_file, out_dir.resolve(), args.op_file, arch) | ||
| 138 | + return | ||
| 139 | + | ||
| 103 | _install_build_only_torch_stub() | 140 | _install_build_only_torch_stub() |
| 104 | binary_dir = _prepare_binary_headers(py_file, arch) | 141 | binary_dir = _prepare_binary_headers(py_file, arch) |
| 105 | shutil.copytree(binary_dir, out_dir, dirs_exist_ok=True) | 142 | shutil.copytree(binary_dir, out_dir, dirs_exist_ok=True) |
Mops/pro/build_ops.yaml+3-0
Aops/pro/deepseek_v4_1/attn_prologue/CMakeLists.txt+21-0
Aops/pro/deepseek_v4_1/attn_prologue/README.md+306-0
Aops/pro/deepseek_v4_1/attn_prologue/__init__.py+20-0
Aops/pro/deepseek_v4_1/attn_prologue/op_host/CMakeLists.txt+51-0
Aops/pro/deepseek_v4_1/attn_prologue/op_host/attn_prologue_def.cpp+136-0
Aops/pro/deepseek_v4_1/attn_prologue/op_host/attn_prologue_infershape.cpp+85-0
Aops/pro/deepseek_v4_1/attn_prologue/op_host/attn_prologue_tiling.cpp+336-0
Aops/pro/deepseek_v4_1/attn_prologue/op_host/attn_prologue_tiling.h+42-0
Aops/pro/deepseek_v4_1/attn_prologue/op_kernel/__init__.py+11-0
Aops/pro/deepseek_v4_1/attn_prologue/op_kernel/attn_prologue.py+1909-0
Aops/pro/deepseek_v4_1/attn_prologue/tests/CMakeLists.txt+11-0
Aops/pro/deepseek_v4_1/attn_prologue/tests/aclnn_bridge/CMakeLists.txt+31-0
Aops/pro/deepseek_v4_1/attn_prologue/tests/aclnn_bridge/aclnn_bridge_attn_prologue.cpp+118-0
Aops/pro/deepseek_v4_1/attn_prologue/tests/assets/attn_prologue_golden_cpu.py+1028-0
Aops/pro/deepseek_v4_1/attn_prologue/tests/assets/attn_prologue_precision.py+870-0
Aops/pro/deepseek_v4_1/attn_prologue/tests/test_attn_prologue.py+501-0
Aops/pro/deepseek_v4_1/attn_prologue/torch_extension/CMakeLists.txt+12-0
Aops/pro/deepseek_v4_1/attn_prologue/torch_extension/attn_prologue.cpp+261-0
Comments
Sign in to comment