Skip to content

Commit b9b4d9f

Browse files
committed
fix(pr): resolve pre-commit failures for #1955
- pyright: assert external kernel source(s) non-None in the specializer's _render_external so `str | None` narrows to `str` - pyright: assert pl.parse(...) returns ir.Program before run_passes in the JIT extern test - ruff PLC0415: hoist in-function imports to top level in the JIT extern test - ruff PLR0913: mark build_specialize_context (pass-through assembler) noqa - cpplint build/include_subdir: NOLINT the runtime headers in the ST kernel fixture (they resolve via the runtime include path) - ruff-format / clang-format: apply formatting
1 parent 11bace4 commit b9b4d9f

8 files changed

Lines changed: 27 additions & 32 deletions

File tree

docs/zh-cn/dev/language/01-external-kernels.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Python stub 未变,kernel 变更也会触发重新编译。
9595
仅豁免 `ReturnParamsExplicit` 属性 —— 该属性要求存在 `ReturnStmt`,而函数头本就没有。
9696
- **Orchestration 代码生成**:为该 kernel 分配 `func_id`,并像 DSL kernel 一样生成派发
9797
—— 单核 AIC/AIV 的 `rt_submit_*_task`,或 group 的 `MixedKernels{aic_id, aiv_id, ...}`
98-
+ `rt_submit_task`
98+
- `rt_submit_task`
9999
- **Backend**:对外部 kernel 跳过 ptoas,并把其 `.cpp` 复制到
100100
`kernels/<aic|aiv>/<name>.cpp`,因此生成的 `kernel_config.py` manifest 会像列出生成
101101
kernel 一样列出它(`func_id``source``core_type``signature``arg_index`)。

python/pypto/jit/specializer.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1679,14 +1679,19 @@ def _member(member_name: str, core_upper: str, source: str) -> list[str]:
16791679
" ...",
16801680
]
16811681

1682+
# The @pl.jit.extern decorator guarantees the source(s) for the selected
1683+
# core_type are set; assert to narrow str | None -> str for the type checker.
16821684
core = ctx.external_core_type
16831685
if core == "aic":
1686+
assert ctx.external_aic_source is not None
16841687
return _member(name, "AIC", ctx.external_aic_source)
16851688
if core == "aiv":
1689+
assert ctx.external_aiv_source is not None
16861690
return _member(name, "AIV", ctx.external_aiv_source)
16871691

16881692
# Mixed: two members + a Group wrapper named after the extern so the
16891693
# entry's ``self.<name>(...)`` call resolves to the group.
1694+
assert ctx.external_aic_source is not None and ctx.external_aiv_source is not None
16901695
lines = _member(f"{name}_aic", "AIC", ctx.external_aic_source)
16911696
lines += _member(f"{name}_aiv", "AIV", ctx.external_aiv_source)
16921697
lines.append("@pl.function(type=pl.FunctionType.Group)")
@@ -1825,7 +1830,7 @@ def specialize(class_name: str, contexts: list[SpecializeContext]) -> str:
18251830
return Specializer(class_name, contexts).specialize()
18261831

18271832

1828-
def build_specialize_context(
1833+
def build_specialize_context( # noqa: PLR0913 — pass-through assembler; each arg maps to a SpecializeContext field
18291834
func: Any,
18301835
func_name: str,
18311836
func_type: str | None,

python/pypto/language/parser/ast_parser.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,7 @@ def _is_empty_body(body: list[ast.stmt]) -> bool:
9292
only = non_doc[0]
9393
if isinstance(only, ast.Pass):
9494
return True
95-
return (
96-
isinstance(only, ast.Expr)
97-
and isinstance(only.value, ast.Constant)
98-
and only.value.value is ...
99-
)
95+
return isinstance(only, ast.Expr) and isinstance(only.value, ast.Constant) and only.value.value is ...
10096

10197

10298
def _is_pld_call(node: object, attr_name: str) -> TypeGuard[ast.Call]:

runtime

Submodule runtime updated 401 files

tests/st/runtime/external_kernel/kernels/aiv/spmd_write.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include <cstdint>
2727
#include <pto/pto-inst.hpp>
2828

29-
#include "tensor.h"
29+
#include "tensor.h" // NOLINT(build/include_subdir)
3030

3131
#ifndef __gm__
3232
#define __gm__
@@ -36,14 +36,14 @@
3636
#define __aicore__ [aicore] // NOLINT(whitespace/braces)
3737
#endif
3838

39-
#include "intrinsic.h"
39+
#include "intrinsic.h" // NOLINT(build/include_subdir)
4040

4141
static constexpr int32_t FLOATS_PER_CACHE_LINE = 16;
4242

4343
#ifdef PTO_CPUSTUB_HPP
4444
#define dcci(...) \
45-
do { \
46-
} while (0)
45+
do { \
46+
} while (0)
4747
#endif
4848
#ifndef SINGLE_CACHE_LINE
4949
#define SINGLE_CACHE_LINE 0
@@ -52,15 +52,15 @@ static constexpr int32_t FLOATS_PER_CACHE_LINE = 16;
5252
#define CACHELINE_OUT 0
5353
#endif
5454

55-
extern "C" __aicore__ void kernel_entry(__gm__ int64_t *args) {
56-
__gm__ Tensor *out_tensor = reinterpret_cast<__gm__ Tensor *>(args[0]);
57-
__gm__ float *out = reinterpret_cast<__gm__ float *>(out_tensor->buffer.addr) + out_tensor->start_offset;
55+
extern "C" __aicore__ void kernel_entry(__gm__ int64_t* args) {
56+
__gm__ Tensor* out_tensor = reinterpret_cast<__gm__ Tensor*>(args[0]);
57+
__gm__ float* out = reinterpret_cast<__gm__ float*>(out_tensor->buffer.addr) + out_tensor->start_offset;
5858

59-
int32_t base_cl = static_cast<int32_t>(args[1]);
60-
int32_t block_idx = get_block_idx(args);
61-
int32_t offset = (base_cl + block_idx) * FLOATS_PER_CACHE_LINE;
59+
int32_t base_cl = static_cast<int32_t>(args[1]);
60+
int32_t block_idx = get_block_idx(args);
61+
int32_t offset = (base_cl + block_idx) * FLOATS_PER_CACHE_LINE;
6262

63-
out[offset] = static_cast<float>(block_idx);
63+
out[offset] = static_cast<float>(block_idx);
6464

65-
dcci(&out[offset], SINGLE_CACHE_LINE, CACHELINE_OUT);
65+
dcci(&out[offset], SINGLE_CACHE_LINE, CACHELINE_OUT);
6666
}

tests/st/runtime/external_kernel/test_external_kernel.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ def SPMD_WRITE_AIV(
4747
self,
4848
out: pl.InOut[pl.Tensor[[_TOTAL], pl.FP32]],
4949
base_cl: pl.Scalar[pl.INDEX],
50-
) -> pl.Tensor[[_TOTAL], pl.FP32]:
51-
... # implementation lives in kernels/aiv/spmd_write.cpp
50+
) -> pl.Tensor[[_TOTAL], pl.FP32]: ... # implementation lives in kernels/aiv/spmd_write.cpp
5251

5352
@pl.function(type=pl.FunctionType.Orchestration)
5453
def main(self, out: pl.Out[pl.Tensor[[_TOTAL], pl.FP32]]) -> pl.Tensor[[_TOTAL], pl.FP32]:

tests/ut/codegen/test_external_kernel.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
import pytest
2424
from pypto.ir.compile import compile as ir_compile
2525

26-
_KERNEL_SRC = (
27-
"#include <cstdint>\n"
28-
'extern "C" void kernel_entry(int64_t* args) { (void)args; }\n'
29-
)
26+
_KERNEL_SRC = '#include <cstdint>\nextern "C" void kernel_entry(int64_t* args) { (void)args; }\n'
3027

3128

3229
def _write_kernel(tmp_path: Path) -> Path:

tests/ut/jit/test_extern_kernel.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@
2121
import pypto.language as pl
2222
import pytest
2323
import torch
24+
from pypto.ir.pass_manager import OptimizationStrategy, PassManager
2425
from pypto.jit.specializer import Specializer
26+
from pypto.pypto_core import ir
2527

26-
_KERNEL_SRC = (
27-
"#include <cstdint>\n"
28-
'extern "C" void kernel_entry(int64_t* args) { (void)args; }\n'
29-
)
28+
_KERNEL_SRC = '#include <cstdint>\nextern "C" void kernel_entry(int64_t* args) { (void)args; }\n'
3029

3130

3231
def _write_kernel(tmp_path: Path, name: str = "ext.cpp") -> Path:
@@ -71,9 +70,8 @@ def entry(a: pl.Tensor, out: pl.Out[pl.Tensor]) -> pl.Tensor:
7170
assert "self.pa(a, out)" in src
7271

7372
# The generated program parses and survives the full pass pipeline.
74-
from pypto.ir.pass_manager import OptimizationStrategy, PassManager
75-
7673
program = pl.parse(src)
74+
assert isinstance(program, ir.Program)
7775
after = PassManager.get_strategy(OptimizationStrategy.Default).run_passes(program)
7876
names = {f.name for f in after.functions.values()}
7977
assert {"entry", "pa", "pa_aic", "pa_aiv"} <= names

0 commit comments

Comments
 (0)