Merged
feat(pypto_pro): Remove pl.function and pl.program #5628
wuzhongxinAdd on Aug 24
feat(pypto_pro): Remove pl.function and pl.program #5628
Merged
Merged fromwuzhongxin/cann_pyptofunctionintocann/pyptomaster
Total: 65 files changed+3066-4056
| @@ -1090,17 +1090,12 @@ void IRPrinter::VisitFunctionBody(const StmtPtr& body) | |||
| 1090 | 1090 | ||
| 1091 | void IRPrinter::VisitFunction(const FunctionPtr& func) | 1091 | void IRPrinter::VisitFunction(const FunctionPtr& func) |
| 1092 | { | 1092 | { |
| 1093 | - // Print decorator | 1093 | + // Print decorator: only SIMT functions carry a decorator |
| 1094 | if (func->funcType_ == FunctionType::SIMT_VF || func->funcType_ == FunctionType::SIMT_CALLEE) { | 1094 | if (func->funcType_ == FunctionType::SIMT_VF || func->funcType_ == FunctionType::SIMT_CALLEE) { |
| 1095 | stream_ << GetIndent() << "@" << prefix_ << ".simt.function(type=" << prefix_ << ".FunctionType." | 1095 | stream_ << GetIndent() << "@" << prefix_ << ".simt.function(type=" << prefix_ << ".FunctionType." |
| 1096 | << FunctionTypeToString(func->funcType_) << ")"; | 1096 | << FunctionTypeToString(func->funcType_) << ")"; |
| 1097 | - } else { | 1097 | + stream_ << "\n"; |
| 1098 | - stream_ << GetIndent() << "@" << prefix_ << ".function"; | ||
| 1099 | - if (func->funcType_ != FunctionType::OPAQUE) { | ||
| 1100 | - stream_ << "(type=" << prefix_ << ".FunctionType." << FunctionTypeToString(func->funcType_) << ")"; | ||
| 1101 | - } | ||
| 1102 | } | 1098 | } |
| 1103 | - stream_ << "\n"; | ||
| 1104 | 1099 | ||
| 1105 | // Print function signature | 1100 | // Print function signature |
| 1106 | stream_ << GetIndent() << "def " << func->name_ << "("; | 1101 | stream_ << GetIndent() << "def " << func->name_ << "("; |
| @@ -369,7 +369,7 @@ TEST_F(IRStmtStrTest, TestFunctionStr) | |||
| 369 | auto func = std::make_shared<Function>("test_func", std::vector<VarPtr>{x}, std::vector<TypePtr>{st}, assign, sp); | 369 | auto func = std::make_shared<Function>("test_func", std::vector<VarPtr>{x}, std::vector<TypePtr>{st}, assign, sp); |
| 370 | 370 | ||
| 371 | ASSERT_EQ(PythonPrint(std::static_pointer_cast<const IRNode>(func)), | 371 | ASSERT_EQ(PythonPrint(std::static_pointer_cast<const IRNode>(func)), |
| 372 | - "@ir.function\ndef test_func(x: ir.Scalar[ir.INT32]) -> ir.Scalar[ir.INT32]:\n" | 372 | + "def test_func(x: ir.Scalar[ir.INT32]) -> ir.Scalar[ir.INT32]:\n" |
| 373 | " x: ir.Scalar[ir.INT32] = 42"); | 373 | " x: ir.Scalar[ir.INT32] = 42"); |
| 374 | } | 374 | } |
| 375 | 375 | ||
| @@ -383,10 +383,8 @@ TEST_F(IRStmtStrTest, TestProgramStr) | |||
| 383 | auto prog = std::make_shared<Program>(std::vector<FunctionPtr>{func1, func2}, "test_prog", sp); | 383 | auto prog = std::make_shared<Program>(std::vector<FunctionPtr>{func1, func2}, "test_prog", sp); |
| 384 | 384 | ||
| 385 | std::string expected = "# ir.program: test_prog\n" | 385 | std::string expected = "# ir.program: test_prog\n" |
| 386 | - "@ir.function\n" | ||
| 387 | "def test_func(x: ir.Scalar[ir.INT32]) -> ir.Scalar[ir.INT32]:\n" | 386 | "def test_func(x: ir.Scalar[ir.INT32]) -> ir.Scalar[ir.INT32]:\n" |
| 388 | " x: ir.Scalar[ir.INT32] = 42\n" | 387 | " x: ir.Scalar[ir.INT32] = 42\n" |
| 389 | - "@ir.function\n" | ||
| 390 | "def test_func2(x: ir.Scalar[ir.INT32]) -> ir.Scalar[ir.INT32]:\n" | 388 | "def test_func2(x: ir.Scalar[ir.INT32]) -> ir.Scalar[ir.INT32]:\n" |
| 391 | " x: ir.Scalar[ir.INT32] = 42"; | 389 | " x: ir.Scalar[ir.INT32] = 42"; |
| 392 | ASSERT_EQ(PythonPrint(std::static_pointer_cast<const IRNode>(prog)), expected); | 390 | ASSERT_EQ(PythonPrint(std::static_pointer_cast<const IRNode>(prog)), expected); |
| @@ -402,7 +402,7 @@ TEST_F(IRPrinterTest, TestPrintFunction) | |||
| 402 | auto funcStr = Print(std::make_shared<Function>("f", std::vector<VarPtr>{x}, | 402 | auto funcStr = Print(std::make_shared<Function>("f", std::vector<VarPtr>{x}, |
| 403 | std::vector<TypePtr>{Scalar(DataType::INT32)}, body, Sp())); | 403 | std::vector<TypePtr>{Scalar(DataType::INT32)}, body, Sp())); |
| 404 | EXPECT_NE(funcStr.find("def f"), std::string::npos); | 404 | EXPECT_NE(funcStr.find("def f"), std::string::npos); |
| 405 | - EXPECT_NE(funcStr.find("@ir.function"), std::string::npos); | 405 | + EXPECT_EQ(funcStr.find("@ir.function"), std::string::npos); |
| 406 | 406 | ||
| 407 | // Multi params | 407 | // Multi params |
| 408 | auto yield = std::make_shared<YieldStmt>(std::vector<ExprPtr>{}, Sp()); | 408 | auto yield = std::make_shared<YieldStmt>(std::vector<ExprPtr>{}, Sp()); |
| @@ -10,15 +10,9 @@ | |||
| 10 | """PyPTO Language module - Type-safe DSL API for writing IR functions.""" | 10 | """PyPTO Language module - Type-safe DSL API for writing IR functions.""" |
| 11 | 11 | ||
| 12 | __all__ = [ | 12 | __all__ = [ |
| 13 | - "function", | ||
| 14 | "inline", | 13 | "inline", |
| 15 | - "program", | ||
| 16 | "vector_function", | 14 | "vector_function", |
| 17 | - "parse", | ||
| 18 | "parser", | 15 | "parser", |
| 19 | - "loads", | ||
| 20 | - "parse_program", | ||
| 21 | - "loads_program", | ||
| 22 | "Tensor", | 16 | "Tensor", |
| 23 | "Ptr", | 17 | "Ptr", |
| 24 | "Scalar", | 18 | "Scalar", |
| @@ -203,8 +197,6 @@ __all__ = [ | |||
| 203 | "get_subblock_num", | 197 | "get_subblock_num", |
| 204 | "make_tile_group", | 198 | "make_tile_group", |
| 205 | # Frontend (runtime) aliases | 199 | # Frontend (runtime) aliases |
| 206 | - "kernel", | ||
| 207 | - "KernelDef", | ||
| 208 | "jit", | 200 | "jit", |
| 209 | "pipeline", | 201 | "pipeline", |
| 210 | # VF enums | 202 | # VF enums |
| @@ -279,7 +271,7 @@ from pypto_pro.ir.op.block_ops import FillPadMode, TileType | |||
| 279 | from pypto_pro.ir.op.ptr_ops import addptr, make_ptr, make_tensor | 271 | from pypto_pro.ir.op.ptr_ops import addptr, make_ptr, make_tensor |
| 280 | from pypto_pro.ir.op.system_ops import mutex_lock as _mutex_lock | 272 | from pypto_pro.ir.op.system_ops import mutex_lock as _mutex_lock |
| 281 | from pypto_pro.ir.op.system_ops import mutex_unlock as _mutex_unlock | 273 | from pypto_pro.ir.op.system_ops import mutex_unlock as _mutex_unlock |
| 282 | -from pypto_pro.runtime import KernelDef, jit, kernel, pipeline | 274 | +from pypto_pro.runtime import jit, pipeline |
| 283 | 275 | ||
| 284 | from . import parser | 276 | from . import parser |
| 285 | from ._api import ( | 277 | from ._api import ( |
| @@ -389,8 +381,7 @@ from ._api import ( | |||
| 389 | ) | 381 | ) |
| 390 | from ._simt_api import Simt as simt # noqa: N813 | 382 | from ._simt_api import Simt as simt # noqa: N813 |
| 391 | from ._vf_api import Vf | 383 | from ._vf_api import Vf |
| 392 | -from .parser.decorator import function, inline, program, vector_function | 384 | +from .parser.decorator import inline, vector_function |
| 393 | -from .parser.text_parser import loads, loads_program, parse, parse_program | ||
| 394 | from .typing import DYNAMIC, STATIC, Ptr, Scalar, Tensor | 385 | from .typing import DYNAMIC, STATIC, Ptr, Scalar, Tensor |
| 395 | 386 | ||
| 396 | mutex = SimpleNamespace(mutex_lock=_mutex_lock, mutex_unlock=_mutex_unlock) | 387 | mutex = SimpleNamespace(mutex_lock=_mutex_lock, mutex_unlock=_mutex_unlock) |
| @@ -17,7 +17,7 @@ builder programs. | |||
| 17 | Part of the pypto_pro.language package - use via: | 17 | Part of the pypto_pro.language package - use via: |
| 18 | import pypto_pro.language as pl | 18 | import pypto_pro.language as pl |
| 19 | 19 | ||
| 20 | - @pl.function | 20 | + @pl.jit |
| 21 | def my_func(...): | 21 | def my_func(...): |
| 22 | ... | 22 | ... |
| 23 | """ | 23 | """ |
| @@ -25,19 +25,12 @@ Part of the pypto_pro.language package - use via: | |||
| 25 | from __future__ import annotations | 25 | from __future__ import annotations |
| 26 | 26 | ||
| 27 | __all__ = [ | 27 | __all__ = [ |
| 28 | - "function", | ||
| 29 | "inline", | 28 | "inline", |
| 30 | - "program", | ||
| 31 | - "parse", | ||
| 32 | - "loads", | ||
| 33 | - "parse_program", | ||
| 34 | - "loads_program", | ||
| 35 | "Tensor", | 29 | "Tensor", |
| 36 | "Scalar", | 30 | "Scalar", |
| 37 | ] | 31 | ] |
| 38 | 32 | ||
| 39 | 33 | ||
| 40 | from ..typing import Scalar, Tensor | 34 | from ..typing import Scalar, Tensor |
| 41 | -from .decorator import function, inline, program | 35 | +from .decorator import inline |
| 42 | from .diagnostics import ParserError # noqa: F401 | 36 | from .diagnostics import ParserError # noqa: F401 |
| 43 | -from .text_parser import loads, loads_program, parse, parse_program | ||
| @@ -93,8 +93,6 @@ class ASTParser( | |||
| 93 | target: ir.SectionKind, | 93 | target: ir.SectionKind, |
| 94 | line_offset: int = 0, | 94 | line_offset: int = 0, |
| 95 | col_offset: int = 0, | 95 | col_offset: int = 0, |
| 96 | - global_vars: set[str] | None = None, | ||
| 97 | - gvar_to_func: dict[str, ir.Function] | None = None, | ||
| 98 | strict_ssa: bool = False, | 96 | strict_ssa: bool = False, |
| 99 | closure_vars: dict[str, Any] | None = None, | 97 | closure_vars: dict[str, Any] | None = None, |
| 100 | auto_mutex: bool = False, | 98 | auto_mutex: bool = False, |
| @@ -113,16 +111,14 @@ class ASTParser( | |||
| 113 | source_lines: Lines of source code (dedented for parsing) | 111 | source_lines: Lines of source code (dedented for parsing) |
| 114 | line_offset: Line number offset to add to AST line numbers (for dedented code) | 112 | line_offset: Line number offset to add to AST line numbers (for dedented code) |
| 115 | col_offset: Column offset to add to AST column numbers (for dedented code) | 113 | col_offset: Column offset to add to AST column numbers (for dedented code) |
| 116 | - global_vars: Optional set of function names for cross-function calls | ||
| 117 | - gvar_to_func: Optional map of function names to parsed Functions for type inference | ||
| 118 | strict_ssa: If True, enforce SSA (single assignment). If False (default), allow reassignment. | 114 | strict_ssa: If True, enforce SSA (single assignment). If False (default), allow reassignment. |
| 119 | closure_vars: Optional variables from the enclosing scope for dynamic shape resolution | 115 | closure_vars: Optional variables from the enclosing scope for dynamic shape resolution |
| 120 | auto_mutex: If True, automatically insert mutex lock/unlock around buffer-managed tile ops. | 116 | auto_mutex: If True, automatically insert mutex lock/unlock around buffer-managed tile ops. |
| 121 | void_return_only: If True, reject return values and non-None return annotations. | 117 | void_return_only: If True, reject return values and non-None return annotations. |
| 122 | void_return_context: User-facing name used only in diagnostics | 118 | void_return_context: User-facing name used only in diagnostics |
| 123 | - (for example, "@pl.jit/@pl.kernel" or "@pl.vector_function"). | 119 | + (for example, "@pl.jit" or "@pl.vector_function"). |
| 124 | allow_early_return: If True, skip the single-tail-return restriction. | 120 | allow_early_return: If True, skip the single-tail-return restriction. |
| 125 | - This is intentionally separate from void_return_only: @pl.jit/@pl.kernel | 121 | + This is intentionally separate from void_return_only: @pl.jit |
| 126 | are void-only but allow early/multiple returns, while @pl.vector_function | 122 | are void-only but allow early/multiple returns, while @pl.vector_function |
| 127 | and @pl.pipeline.stage are void-only and still require a single tail return. | 123 | and @pl.pipeline.stage are void-only and still require a single tail return. |
| 128 | target: Required Cube/Vector target for target-specific parsing. | 124 | target: Required Cube/Vector target for target-specific parsing. |
| @@ -169,8 +165,6 @@ class ASTParser( | |||
| 169 | # by the caller (kernel / decorator). Not created here; may be None for parses that | 165 | # by the caller (kernel / decorator). Not created here; may be None for parses that |
| 170 | # do not feed a Program. | 166 | # do not feed a Program. |
| 171 | self.debug_info = debug_info | 167 | self.debug_info = debug_info |
| 172 | - self.global_vars = global_vars or set() # Track function names for cross-function calls | ||
| 173 | - self.gvar_to_func = gvar_to_func or {} # Track parsed functions for type inference | ||
| 174 | self.external_funcs: dict[str, ir.Function] = {} # Track external functions referenced | 168 | self.external_funcs: dict[str, ir.Function] = {} # Track external functions referenced |
| 175 | 169 | ||
| 176 | # Track active control-flow builders while parsing nested statements. | 170 | # Track active control-flow builders while parsing nested statements. |
| @@ -15,7 +15,10 @@ import ast | |||
| 15 | from collections import namedtuple | 15 | from collections import namedtuple |
| 16 | import copy | 16 | import copy |
| 17 | from dataclasses import dataclass | 17 | from dataclasses import dataclass |
| 18 | +import inspect | ||
| 19 | +import linecache | ||
| 18 | import logging | 20 | import logging |
| 21 | +import sys | ||
| 19 | from typing import Any, Callable | 22 | from typing import Any, Callable |
| 20 | 23 | ||
| 21 | from pypto.pypto_impl import ir | 24 | from pypto.pypto_impl import ir |
| @@ -30,14 +33,192 @@ from .diagnostics import ( | |||
| 30 | FinalRejectionError, | 33 | FinalRejectionError, |
| 31 | ParserSyntaxError, | 34 | ParserSyntaxError, |
| 32 | ParserTypeError, | 35 | ParserTypeError, |
| 33 | - UndefinedVariableError, | ||
| 34 | UnsupportedFeatureError, | 36 | UnsupportedFeatureError, |
| 35 | ) | 37 | ) |
| 36 | 38 | ||
| 37 | logger = logging.getLogger(__name__) | 39 | logger = logging.getLogger(__name__) |
| 38 | 40 | ||
| 39 | 41 | ||
| 40 | -# Tile reference for auto mutex (optional mutex metadata, memory, dedup id). | 42 | +def _get_source_file(entity: Callable | type) -> str: |
| 43 | + """Get source filename for an entity, with fallback to code object attributes. | ||
| 44 | + | ||
| 45 | + Args: | ||
| 46 | + entity: Function or class to get source file for | ||
| 47 | + | ||
| 48 | + Returns: | ||
| 49 | + Source filename string | ||
| 50 | + """ | ||
| 51 | + try: | ||
| 52 | + return inspect.getfile(entity) | ||
| 53 | + except (OSError, TypeError): | ||
| 54 | + pass | ||
| 55 | + | ||
| 56 | + # Fallback: extract from code object | ||
| 57 | + if callable(entity) and hasattr(entity, "__code__"): | ||
| 58 | + return entity.__code__.co_filename | ||
| 59 | + | ||
| 60 | + # For classes, find a method with a code object | ||
| 61 | + if isinstance(entity, type): | ||
| 62 | + for attr in entity.__dict__.values(): | ||
| 63 | + if callable(attr) and hasattr(attr, "__code__"): | ||
| 64 | + return attr.__code__.co_filename | ||
| 65 | + | ||
| 66 | + return "<unknown>" | ||
| 67 | + | ||
| 68 | + | ||
| 69 | +def _find_entity_in_source( | ||
| 70 | + all_lines: list[str], name: str, entity_type: str, start_line_hint: int | None = None | ||
| 71 | +) -> tuple[list[str], int] | None: | ||
| 72 | + """Find an entity definition in source lines using AST parsing. | ||
| 73 | + | ||
| 74 | + Args: | ||
| 75 | + all_lines: All source lines from the file | ||
| 76 | + name: Name of the entity to find | ||
| 77 | + entity_type: "function" or "class" | ||
| 78 | + start_line_hint: Optional line number to disambiguate entities with the same name | ||
| 79 | + | ||
| 80 | + Returns: | ||
| 81 | + Tuple of (source_lines, starting_line_1based) or None if not found | ||
| 82 | + """ | ||
| 83 | + source_text = "".join(all_lines) | ||
| 84 | + try: | ||
| 85 | + tree = ast.parse(source_text) | ||
| 86 | + except SyntaxError: | ||
| 87 | + return None | ||
| 88 | + | ||
| 89 | + node_type = ast.FunctionDef if entity_type == "function" else ast.ClassDef | ||
| 90 | + candidates = [node for node in ast.walk(tree) if isinstance(node, node_type) and node.name == name] | ||
| 91 | + | ||
| 92 | + if not candidates: | ||
| 93 | + return None | ||
| 94 | + | ||
| 95 | + if len(candidates) == 1: | ||
| 96 | + node = candidates[0] | ||
| 97 | + elif start_line_hint is not None: | ||
| 98 | + # Disambiguate using the code object's line number | ||
| 99 | + node = min(candidates, key=lambda n: abs(n.lineno - start_line_hint)) | ||
| 100 | + else: | ||
| 101 | + node = candidates[0] | ||
| 102 | + | ||
| 103 | + # Start from the first decorator line if present | ||
| 104 | + start_line = node.decorator_list[0].lineno if node.decorator_list else node.lineno | ||
| 105 | + end_line = node.end_lineno or node.lineno | ||
| 106 | + # Lines are 1-based in AST | ||
| 107 | + source_lines = all_lines[start_line - 1:end_line] | ||
| 108 | + return source_lines, start_line | ||
| 109 | + | ||
| 110 | + | ||
| 111 | +def _source_from_inspect(entity: Callable | type) -> tuple[str, list[str], int] | None: | ||
| 112 | + """Get source information via inspect, returning None when unavailable.""" | ||
| 113 | + try: | ||
| 114 | + source_file = inspect.getfile(entity) | ||
| 115 | + source_lines_raw, starting_line = inspect.getsourcelines(entity) | ||
| 116 | + return source_file, source_lines_raw, starting_line | ||
| 117 | + except (OSError, TypeError): | ||
| 118 | + return None | ||
| 119 | + | ||
| 120 | + | ||
| 121 | +def _source_from_linecache( | ||
| 122 | + source_file: str, | ||
| 123 | + name: str, | ||
| 124 | + entity_type: str, | ||
| 125 | + start_line_hint: int | None, | ||
| 126 | +) -> tuple[str, list[str], int] | None: | ||
| 127 | + """Get source information from linecache.""" | ||
| 128 | + all_lines = linecache.getlines(source_file) | ||
| 129 | + if not all_lines: | ||
| 130 | + return None | ||
| 131 | + result = _find_entity_in_source(all_lines, name, entity_type, start_line_hint) | ||
| 132 | + return None if result is None else (source_file, result[0], result[1]) | ||
| 133 | + | ||
| 134 | + | ||
| 135 | +def _restore_linecache_entry(prev_entry) -> None: | ||
| 136 | + """Restore the synthetic <string> linecache entry used for python -c parsing.""" | ||
| 137 | + if prev_entry is not None: | ||
| 138 | + linecache.cache["<string>"] = prev_entry | ||
| 139 | + else: | ||
| 140 | + linecache.cache.pop("<string>", None) | ||
| 141 | + | ||
| 142 | + | ||
| 143 | +def _source_from_python_c( | ||
| 144 | + source_file: str, | ||
| 145 | + name: str, | ||
| 146 | + entity_type: str, | ||
| 147 | + start_line_hint: int | None, | ||
| 148 | +) -> tuple[str, list[str], int] | None: | ||
| 149 | + """Get source information from sys.orig_argv for python -c invocations.""" | ||
| 150 | + if source_file != "<string>" or not hasattr(sys, "orig_argv"): | ||
| 151 | + return None | ||
| 152 | + try: | ||
| 153 | + c_index = sys.orig_argv.index("-c") | ||
| 154 | + except ValueError: | ||
| 155 | + return None | ||
| 156 | + if c_index + 1 >= len(sys.orig_argv): | ||
| 157 | + return None | ||
| 158 | + | ||
| 159 | + code_str = sys.orig_argv[c_index + 1] | ||
| 160 | + code_lines = code_str.splitlines(keepends=True) | ||
| 161 | + prev_entry = linecache.cache.get("<string>") | ||
| 162 | + linecache.cache["<string>"] = (len(code_str), None, code_lines, "<string>") | ||
| 163 | + try: | ||
| 164 | + result = _find_entity_in_source(code_lines, name, entity_type, start_line_hint) | ||
| 165 | + return None if result is None else (source_file, result[0], result[1]) | ||
| 166 | + finally: | ||
| 167 | + _restore_linecache_entry(prev_entry) | ||
| 168 | + | ||
| 169 | + | ||
| 170 | +def _get_source_info(entity: Callable | type, entity_type: str) -> tuple[str, list[str], int]: | ||
| 171 | + """Get source file, source lines, and starting line for an entity. | ||
| 172 | + | ||
| 173 | + Tries multiple strategies: | ||
| 174 | + 1. Standard inspect.getsourcelines() | ||
| 175 | + 2. linecache fallback (handles IPython, pre-populated cache) | ||
| 176 | + 3. sys.orig_argv for `python -c` invocations | ||
| 177 | + 4. Clear error with actionable hint | ||
| 178 | + | ||
| 179 | + Args: | ||
| 180 | + entity: Function or class to get source for | ||
| 181 | + entity_type: "function" or "class" | ||
| 182 | + | ||
| 183 | + Returns: | ||
| 184 | + Tuple of (source_file, source_lines_raw, starting_line) | ||
| 185 | + | ||
| 186 | + Raises: | ||
| 187 | + ParserSyntaxError: If source cannot be retrieved by any strategy | ||
| 188 | + """ | ||
| 189 | + name = entity.__name__ if hasattr(entity, "__name__") else str(entity) | ||
| 190 | + | ||
| 191 | + # Get a line number hint from the code object to disambiguate same-name entities | ||
| 192 | + start_line_hint: int | None = None | ||
| 193 | + if callable(entity) and hasattr(entity, "__code__"): | ||
| 194 | + start_line_hint = entity.__code__.co_firstlineno | ||
| 195 | + | ||
| 196 | + # Strategy 1: Standard inspect | ||
| 197 | + inspect_result = _source_from_inspect(entity) | ||
| 198 | + if inspect_result is not None: | ||
| 199 | + return inspect_result | ||
| 200 | + | ||
| 201 | + # Get source file via fallback for strategies 2-3 | ||
| 202 | + source_file = _get_source_file(entity) | ||
| 203 | + | ||
| 204 | + # Strategy 2: linecache fallback | ||
| 205 | + linecache_result = _source_from_linecache(source_file, name, entity_type, start_line_hint) | ||
| 206 | + if linecache_result is not None: | ||
| 207 | + return linecache_result | ||
| 208 | + | ||
| 209 | + # Strategy 3: sys.orig_argv for `python -c` | ||
| 210 | + python_c_result = _source_from_python_c(source_file, name, entity_type, start_line_hint) | ||
| 211 | + if python_c_result is not None: | ||
| 212 | + return python_c_result | ||
| 213 | + | ||
| 214 | + # Strategy 4: Clear error | ||
| 215 | + raise ParserSyntaxError( | ||
| 216 | + f"Cannot retrieve source code for {entity_type} '{name}'", | ||
| 217 | + hint="Define the callable in a Python source file so its AST can be inspected", | ||
| 218 | + ) | ||
| 219 | + | ||
| 220 | + | ||
| 221 | +# Mutex carrier for tile-group tiles (buf_id IR expr tuple, candidate values, memory, dedup id). | ||
| 41 | _MutexRef = namedtuple("_MutexRef", "buf_ids mutex_ids memory slot_id") | 222 | _MutexRef = namedtuple("_MutexRef", "buf_ids mutex_ids memory slot_id") |
| 42 | 223 | ||
| 43 | 224 | ||
| @@ -604,8 +785,6 @@ class CallParserMixin: | |||
| 604 | """ | 785 | """ |
| 605 | import textwrap as _tw | 786 | import textwrap as _tw |
| 606 | 787 | ||
| 607 | - from .decorator import _get_source_info | ||
| 608 | - | ||
| 609 | try: | 788 | try: |
| 610 | source_file, source_lines_raw, starting_line = _get_source_info(fn, "function") | 789 | source_file, source_lines_raw, starting_line = _get_source_info(fn, "function") |
| 611 | except Exception as e: | 790 | except Exception as e: |
| @@ -737,28 +916,7 @@ class CallParserMixin: | |||
| 737 | """ | 916 | """ |
| 738 | func = call.func | 917 | func = call.func |
| 739 | 918 | ||
| 740 | - # Handle cross-function calls via self.method_name() in @pl.program classes | ||
| 741 | if isinstance(func, ast.Attribute): | 919 | if isinstance(func, ast.Attribute): |
| 742 | - # Check for self.method_name pattern | ||
| 743 | - if isinstance(func.value, ast.Name) and func.value.id == "self": | ||
| 744 | - method_name = func.attr | ||
| 745 | - if method_name in self.global_vars: | ||
| 746 | - func_obj = self.gvar_to_func.get(method_name) | ||
| 747 | - args = [self.parse_expression(arg) for arg in call.args] | ||
| 748 | - span = self.span_tracker.get_span(call) | ||
| 749 | - | ||
| 750 | - # Use return type from the parsed function if available | ||
| 751 | - return_types = func_obj.return_types if func_obj else [] | ||
| 752 | - op = ir.Op(method_name) | ||
| 753 | - return self._make_call_with_return_type(op, args, return_types, span) | ||
| 754 | - else: | ||
| 755 | - raise UndefinedVariableError( | ||
| 756 | - f"Function '{method_name}' not defined in program", | ||
| 757 | - span=self.span_tracker.get_span(call), | ||
| 758 | - hint=f"Available functions: {sorted(self.global_vars)}", | ||
| 759 | - ) | ||
| 760 | - | ||
| 761 | - # Handle pl.tensor.*, pl.system.*, and pl.* operation calls | ||
| 762 | return self.parse_op_call(call) | 920 | return self.parse_op_call(call) |
| 763 | 921 | ||
| 764 | # Handle bare-name calls to external IR functions or inline Python callables. | 922 | # Handle bare-name calls to external IR functions or inline Python callables. |
| @@ -778,7 +936,7 @@ class CallParserMixin: | |||
| 778 | raise UnsupportedFeatureError( | 936 | raise UnsupportedFeatureError( |
| 779 | f"Unsupported function call: {ast.unparse(call)}", | 937 | f"Unsupported function call: {ast.unparse(call)}", |
| 780 | span=self.span_tracker.get_span(call), | 938 | span=self.span_tracker.get_span(call), |
| 781 | - hint="Use pl.* operations, self.method() for cross-function calls, or call an inline Python helper by name", | 939 | + hint="Use pl.* operations or call an inline Python helper by name", |
| 782 | ) | 940 | ) |
| 783 | 941 | ||
| 784 | def parse_op_call(self, call: ast.Call) -> Any: | 942 | def parse_op_call(self, call: ast.Call) -> Any: |
| @@ -918,40 +1076,6 @@ class CallParserMixin: | |||
| 918 | return self._parse_vf_op(op_name[3:], call) | 1076 | return self._parse_vf_op(op_name[3:], call) |
| 919 | return self._parse_block_default(op_name, call) | 1077 | return self._parse_block_default(op_name, call) |
| 920 | 1078 | ||
| 921 | - def _parse_external_function_call(self, _local_name: str, ext_func: ir.Function, call: ast.Call) -> ir.Expr: | ||
| 922 | - """Parse a call to an externally-defined ir.Function. | ||
| 923 | - | ||
| 924 | - Args: | ||
| 925 | - _local_name: The name used in the caller's scope (may be aliased) | ||
| 926 | - ext_func: The external ir.Function object | ||
| 927 | - call: The AST Call node | ||
| 928 | - """ | ||
| 929 | - func_name = ext_func.name | ||
| 930 | - span = self.span_tracker.get_span(call) | ||
| 931 | - | ||
| 932 | - # Validate no naming conflict with internal program functions | ||
| 933 | - if func_name in self.global_vars: | ||
| 934 | - raise ParserSyntaxError( | ||
| 935 | - f"External function '{func_name}' conflicts with program function '{func_name}'", | ||
| 936 | - span=span, | ||
| 937 | - hint="Rename either the external or program function to avoid the name conflict", | ||
| 938 | - ) | ||
| 939 | - | ||
| 940 | - # Check for conflicting externals with same .name but different objects | ||
| 941 | - if func_name in self.external_funcs and self.external_funcs[func_name] is not ext_func: | ||
| 942 | - raise ParserSyntaxError( | ||
| 943 | - f"Conflicting external functions with name '{func_name}'", | ||
| 944 | - span=span, | ||
| 945 | - hint="External functions must have unique names; rename one of the functions", | ||
| 946 | - ) | ||
| 947 | - | ||
| 948 | - # Track the external function | ||
| 949 | - self.external_funcs[func_name] = ext_func | ||
| 950 | - | ||
| 951 | - args = [self.parse_expression(arg) for arg in call.args] | ||
| 952 | - op = ir.Op(func_name) | ||
| 953 | - return self._make_call_with_return_type(op, args, ext_func.return_types, span) | ||
| 954 | - | ||
| 955 | def _parse_simt_template_call(self, local_name: str, fn: Callable, call: ast.Call) -> ir.Expr: | 1079 | def _parse_simt_template_call(self, local_name: str, fn: Callable, call: ast.Call) -> ir.Expr: |
| 956 | """Instantiate and call one helper @pl.simt.function template.""" | 1080 | """Instantiate and call one helper @pl.simt.function template.""" |
| 957 | span = self.span_tracker.get_span(call) | 1081 | span = self.span_tracker.get_span(call) |
| @@ -1030,7 +1154,6 @@ class CallParserMixin: | |||
| 1030 | ir.SectionKind.Vector, | 1154 | ir.SectionKind.Vector, |
| 1031 | line_offset, | 1155 | line_offset, |
| 1032 | col_offset, | 1156 | col_offset, |
| 1033 | - global_vars=self.global_vars, | ||
| 1034 | closure_vars=self._build_function_closure(fn), | 1157 | closure_vars=self._build_function_closure(fn), |
| 1035 | debug_info=self.debug_info, | 1158 | debug_info=self.debug_info, |
| 1036 | tilingkey_consts=self._tilingkey_consts, | 1159 | tilingkey_consts=self._tilingkey_consts, |
| @@ -1096,11 +1219,6 @@ class CallParserMixin: | |||
| 1096 | 1219 | ||
| 1097 | def _register_simt_external(self, func: ir.Function, span) -> None: | 1220 | def _register_simt_external(self, func: ir.Function, span) -> None: |
| 1098 | """Register one instantiated SIMT function in the enclosing Program.""" | 1221 | """Register one instantiated SIMT function in the enclosing Program.""" |
| 1099 | - if func.name in self.global_vars: | ||
| 1100 | - raise ParserSyntaxError( | ||
| 1101 | - f"SIMT function '{func.name}' conflicts with an internal program function", | ||
| 1102 | - span=span, | ||
| 1103 | - ) | ||
| 1104 | if func.name in self.external_funcs and self.external_funcs[func.name] is not func: | 1222 | if func.name in self.external_funcs and self.external_funcs[func.name] is not func: |
| 1105 | raise ParserSyntaxError( | 1223 | raise ParserSyntaxError( |
| 1106 | f"Conflicting external functions with name '{func.name}'", | 1224 | f"Conflicting external functions with name '{func.name}'", |
| @@ -86,7 +86,7 @@ def validate_single_tail_return(func_def: ast.FunctionDef, context: str) -> tupl | |||
| 86 | return ( | 86 | return ( |
| 87 | returns[1], | 87 | returns[1], |
| 88 | f"{context} only supports a single return statement.", | 88 | f"{context} only supports a single return statement.", |
| 89 | - "Keep one top-level return at the end of the function. Only @pl.jit/@pl.kernel supports early return.", | 89 | + "Keep one top-level return at the end of the function. Only @pl.jit supports early return.", |
| 90 | ) | 90 | ) |
| 91 | 91 | ||
| 92 | body = _body_without_docstring(func_def) | 92 | body = _body_without_docstring(func_def) |
| @@ -95,7 +95,7 @@ def validate_single_tail_return(func_def: ast.FunctionDef, context: str) -> tupl | |||
| 95 | return ( | 95 | return ( |
| 96 | returns[0], | 96 | returns[0], |
| 97 | f"{context} only supports return as the final top-level statement.", | 97 | f"{context} only supports return as the final top-level statement.", |
| 98 | - "Move the return to the end of the function body, or use @pl.jit/@pl.kernel when early return is required.", | 98 | + "Move the return to the end of the function body, or use @pl.jit when early return is required.", |
| 99 | ) | 99 | ) |
| 100 | 100 | ||
| 101 | return None | 101 | return None |
| @@ -1,253 +0,0 @@ | |||
| 1 | -# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 2 | -# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 3 | -# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 4 | -# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 5 | -# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 6 | -# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 7 | -# See LICENSE in the root of the software repository for the full text of the License. | ||
| 8 | -# ----------------------------------------------------------------------------------------------------------- | ||
| 9 | - | ||
| 10 | -"""Parse DSL functions from text or files without requiring decorator syntax.""" | ||
| 11 | - | ||
| 12 | -from __future__ import annotations | ||
| 13 | - | ||
| 14 | -__all__ = ["parse", "loads", "parse_program", "loads_program"] | ||
| 15 | - | ||
| 16 | - | ||
| 17 | -import linecache | ||
| 18 | -import sys | ||
| 19 | -import types | ||
| 20 | - | ||
| 21 | -from pypto.pypto_impl import ir | ||
| 22 | - | ||
| 23 | -from .diagnostics._exceptions import ParserError | ||
| 24 | - | ||
| 25 | - | ||
| 26 | -def parse(code: str, filename: str = "<string>") -> ir.Function | ir.Program: | ||
| 27 | - """Parse a DSL function or program from a string. | ||
| 28 | - | ||
| 29 | - This function takes Python source code containing a @pl.function decorated | ||
| 30 | - function or @pl.program decorated class and parses it into an IR Function | ||
| 31 | - or Program object. The code is executed dynamically, automatically importing | ||
| 32 | - pypto_pro.language as pl if not already present. | ||
| 33 | - | ||
| 34 | - Args: | ||
| 35 | - code: Python source code containing @pl.function or @pl.program | ||
| 36 | - filename: Optional filename for error reporting (default: "<string>") | ||
| 37 | - | ||
| 38 | - Returns: | ||
| 39 | - Parsed ir.Function or ir.Program object (auto-detected) | ||
| 40 | - | ||
| 41 | - Raises: | ||
| 42 | - ValueError: If the code contains nothing to parse or multiple items | ||
| 43 | - ParserError: If parsing fails (syntax errors, type errors, etc.) | ||
| 44 | - | ||
| 45 | - Warning: | ||
| 46 | - This function uses `exec()` to execute the provided code string. | ||
| 47 | - It should only be used with trusted input, as executing untrusted | ||
| 48 | - code can lead to arbitrary code execution vulnerabilities. | ||
| 49 | - | ||
| 50 | - Examples: | ||
| 51 | - >>> import pypto_pro.language as pl | ||
| 52 | - | ||
| 53 | - >>> # Parse a function | ||
| 54 | - >>> func_code = ''' | ||
| 55 | - ... @pl.function | ||
| 56 | - ... def add(x: pl.Tensor[[64], pl.DT_FP32]) -> pl.Tensor[[64], pl.DT_FP32]: | ||
| 57 | - ... result: pl.Tensor[[64], pl.DT_FP32] = pl.add(x, 1.0) | ||
| 58 | - ... return result | ||
| 59 | - ... ''' | ||
| 60 | - >>> func = pl.parse(func_code) | ||
| 61 | - >>> print(func.name) | ||
| 62 | - add | ||
| 63 | - | ||
| 64 | - >>> # Parse a program | ||
| 65 | - >>> prog_code = ''' | ||
| 66 | - ... @pl.program | ||
| 67 | - ... class MyProgram: | ||
| 68 | - ... @pl.function | ||
| 69 | - ... def add(self, x: pl.Tensor[[64], pl.DT_FP32]) -> pl.Tensor[[64], pl.DT_FP32]: | ||
| 70 | - ... return pl.add(x, 1.0) | ||
| 71 | - ... ''' | ||
| 72 | - >>> prog = pl.parse(prog_code) | ||
| 73 | - >>> print(prog.name) | ||
| 74 | - MyProgram | ||
| 75 | - """ | ||
| 76 | - # Import pypto_pro.language here to avoid circular imports | ||
| 77 | - import pypto_pro.language as pl | ||
| 78 | - | ||
| 79 | - # Make the source code available to inspect.getsourcelines() via linecache | ||
| 80 | - # Store ORIGINAL code (not modified) for accurate line numbers | ||
| 81 | - code_lines = code.splitlines(keepends=True) | ||
| 82 | - linecache.cache[filename] = ( | ||
| 83 | - len(code), | ||
| 84 | - None, # mtime | ||
| 85 | - code_lines, | ||
| 86 | - filename, | ||
| 87 | - ) | ||
| 88 | - | ||
| 89 | - # Compile the code with the specified filename for proper error reporting | ||
| 90 | - try: | ||
| 91 | - compiled_code = compile(code, filename, "exec") | ||
| 92 | - except SyntaxError as e: | ||
| 93 | - raise SyntaxError(f"Failed to compile code from {filename}: {e}") from e | ||
| 94 | - | ||
| 95 | - # Create a temporary module for execution | ||
| 96 | - # This ensures inspect.getfile() works correctly for @pl.program | ||
| 97 | - module_name = f"__pypto_parse_{id(code)}__" | ||
| 98 | - temp_module = types.ModuleType(module_name) | ||
| 99 | - temp_module.__file__ = filename | ||
| 100 | - temp_module.__setattr__("pl", pl) | ||
| 101 | - | ||
| 102 | - # Add module to sys.modules so inspect can find it | ||
| 103 | - sys.modules[module_name] = temp_module | ||
| 104 | - | ||
| 105 | - # Execute the code in the module's namespace | ||
| 106 | - try: | ||
| 107 | - exec(compiled_code, temp_module.__dict__) | ||
| 108 | - except ParserError as e: | ||
| 109 | - # Re-raise ParserError as-is, it already has source lines | ||
| 110 | - raise e | ||
| 111 | - except Exception as e: | ||
| 112 | - # Re-raise with context about where the error occurred | ||
| 113 | - raise RuntimeError(f"Error executing code from {filename}: {e}") from e | ||
| 114 | - finally: | ||
| 115 | - # Clean up linecache entry | ||
| 116 | - if filename in linecache.cache: | ||
| 117 | - del linecache.cache[filename] | ||
| 118 | - # Clean up temporary module | ||
| 119 | - if module_name in sys.modules: | ||
| 120 | - del sys.modules[module_name] | ||
| 121 | - | ||
| 122 | - # Get namespace from executed module | ||
| 123 | - namespace = temp_module.__dict__ | ||
| 124 | - | ||
| 125 | - # Scan namespace for ir.Function and ir.Program instances | ||
| 126 | - functions = [] | ||
| 127 | - programs = [] | ||
| 128 | - for name, value in namespace.items(): | ||
| 129 | - if isinstance(value, ir.Function): | ||
| 130 | - functions.append(value) | ||
| 131 | - elif isinstance(value, ir.Program): | ||
| 132 | - programs.append((name, value)) | ||
| 133 | - | ||
| 134 | - # Determine what we found and return appropriate type | ||
| 135 | - total_items = len(functions) + len(programs) | ||
| 136 | - | ||
| 137 | - if total_items == 0: | ||
| 138 | - raise ValueError( | ||
| 139 | - f"No @pl.function or @pl.program found in {filename}. " | ||
| 140 | - "Make sure your code contains a function decorated with @pl.function " | ||
| 141 | - "or a class decorated with @pl.program." | ||
| 142 | - ) | ||
| 143 | - elif total_items > 1: | ||
| 144 | - item_names = [f.name for f in functions] + [name for name, _ in programs] | ||
| 145 | - raise ValueError( | ||
| 146 | - f"Multiple functions/programs found in {filename}: {item_names}. " | ||
| 147 | - f"pl.parse() can only parse code containing a single function or program. " | ||
| 148 | - f"Consider using separate calls or parsing from separate files." | ||
| 149 | - ) | ||
| 150 | - | ||
| 151 | - # Return the single item we found | ||
| 152 | - if functions: | ||
| 153 | - return functions[0] | ||
| 154 | - else: | ||
| 155 | - return programs[0][1] | ||
| 156 | - | ||
| 157 | - | ||
| 158 | -def loads(filepath: str) -> ir.Function | ir.Program: | ||
| 159 | - """Load a DSL function or program from a file. | ||
| 160 | - | ||
| 161 | - This function reads a Python file containing a @pl.function decorated | ||
| 162 | - function or @pl.program decorated class and parses it into an IR Function | ||
| 163 | - or Program object (auto-detected). | ||
| 164 | - | ||
| 165 | - Args: | ||
| 166 | - filepath: Path to Python file containing @pl.function or @pl.program | ||
| 167 | - | ||
| 168 | - Returns: | ||
| 169 | - Parsed ir.Function or ir.Program object (auto-detected) | ||
| 170 | - | ||
| 171 | - Raises: | ||
| 172 | - FileNotFoundError: If the file does not exist | ||
| 173 | - ValueError: If the file contains nothing to parse or multiple items | ||
| 174 | - ParserError: If parsing fails (syntax errors, type errors, etc.) | ||
| 175 | - | ||
| 176 | - Warning: | ||
| 177 | - This function reads a file and executes its contents. It should only | ||
| 178 | - be used with trusted files, as executing code from untrusted sources | ||
| 179 | - can lead to arbitrary code execution vulnerabilities. | ||
| 180 | - | ||
| 181 | - Examples: | ||
| 182 | - >>> import pypto_pro.language as pl | ||
| 183 | - | ||
| 184 | - >>> # Load a function | ||
| 185 | - >>> func = pl.loads('my_kernel.py') | ||
| 186 | - >>> print(func.name) | ||
| 187 | - | ||
| 188 | - >>> # Load a program | ||
| 189 | - >>> prog = pl.loads('my_program.py') | ||
| 190 | - >>> print(prog.name) | ||
| 191 | - """ | ||
| 192 | - # Read file content | ||
| 193 | - with open(filepath, encoding="utf-8") as f: | ||
| 194 | - code = f.read() | ||
| 195 | - | ||
| 196 | - # Parse using parse() with the filepath for proper error reporting | ||
| 197 | - return parse(code, filename=filepath) | ||
| 198 | - | ||
| 199 | - | ||
| 200 | -def parse_program(code: str, filename: str = "<string>") -> ir.Program: | ||
| 201 | - """Parse a DSL program from a string. | ||
| 202 | - | ||
| 203 | - .. deprecated:: | ||
| 204 | - Use :func:`parse` instead, which auto-detects functions and programs. | ||
| 205 | - | ||
| 206 | - This is now an alias for :func:`parse` that validates the result is a Program. | ||
| 207 | - | ||
| 208 | - Args: | ||
| 209 | - code: Python source code containing a @pl.program decorated class | ||
| 210 | - filename: Optional filename for error reporting (default: "<string>") | ||
| 211 | - | ||
| 212 | - Returns: | ||
| 213 | - Parsed ir.Program object | ||
| 214 | - | ||
| 215 | - Raises: | ||
| 216 | - ValueError: If the code contains a function instead of a program | ||
| 217 | - ParserError: If parsing fails (syntax errors, type errors, etc.) | ||
| 218 | - """ | ||
| 219 | - result = parse(code, filename) | ||
| 220 | - if not isinstance(result, ir.Program): | ||
| 221 | - raise ValueError( | ||
| 222 | - f"Expected @pl.program but found @pl.function in {filename}. " | ||
| 223 | - f"Use pl.parse() for auto-detection or ensure your code contains @pl.program." | ||
| 224 | - ) | ||
| 225 | - return result | ||
| 226 | - | ||
| 227 | - | ||
| 228 | -def loads_program(filepath: str) -> ir.Program: | ||
| 229 | - """Load a DSL program from a file. | ||
| 230 | - | ||
| 231 | - .. deprecated:: | ||
| 232 | - Use :func:`loads` instead, which auto-detects functions and programs. | ||
| 233 | - | ||
| 234 | - This is now an alias for :func:`loads` that validates the result is a Program. | ||
| 235 | - | ||
| 236 | - Args: | ||
| 237 | - filepath: Path to Python file containing @pl.program decorated class | ||
| 238 | - | ||
| 239 | - Returns: | ||
| 240 | - Parsed ir.Program object | ||
| 241 | - | ||
| 242 | - Raises: | ||
| 243 | - FileNotFoundError: If the file does not exist | ||
| 244 | - ValueError: If the file contains a function instead of a program | ||
| 245 | - ParserError: If parsing fails (syntax errors, type errors, etc.) | ||
| 246 | - """ | ||
| 247 | - result = loads(filepath) | ||
| 248 | - if not isinstance(result, ir.Program): | ||
| 249 | - raise ValueError( | ||
| 250 | - f"Expected @pl.program but found @pl.function in {filepath}. " | ||
| 251 | - f"Use pl.loads() for auto-detection or ensure your file contains @pl.program." | ||
| 252 | - ) | ||
| 253 | - return result | ||
| @@ -29,7 +29,7 @@ class Ptr: | |||
| 29 | 29 | ||
| 30 | Example:: | 30 | Example:: |
| 31 | 31 | ||
| 32 | - @pl.function | 32 | + @pl.jit |
| 33 | def kernel(ptr: pl.Ptr[pl.DT_FP32]): | 33 | def kernel(ptr: pl.Ptr[pl.DT_FP32]): |
| 34 | view: pl.Tensor[[32, 32], pl.DT_FP32] = pl.make_tensor(ptr, [32, 32], [32, 1]) | 34 | view: pl.Tensor[[32, 32], pl.DT_FP32] = pl.make_tensor(ptr, [32, 32], [32, 1]) |
| 35 | 35 | ||
| @@ -33,13 +33,14 @@ class Scalar: | |||
| 33 | Examples: | 33 | Examples: |
| 34 | >>> import pypto_pro.language as pl | 34 | >>> import pypto_pro.language as pl |
| 35 | >>> | 35 | >>> |
| 36 | - >>> @pl.function | 36 | + >>> @pl.jit |
| 37 | ... def add_scalar( | 37 | ... def add_scalar( |
| 38 | ... x: pl.Tensor[[64], pl.DT_FP32], | 38 | ... x: pl.Tensor[[64], pl.DT_FP32], |
| 39 | - ... scalar: pl.DT_FP32 | 39 | + ... scalar: pl.DT_FP32, |
| 40 | - ... ) -> pl.Tensor[[64], pl.DT_FP32]: | 40 | + ... out: pl.Tensor[[64], pl.DT_FP32], |
| 41 | - ... result: pl.Tensor[[64], pl.DT_FP32] = pl.add(x, scalar) | 41 | + ... ): |
| 42 | - ... return result | 42 | + ... # Build the kernel body with explicit tile operations. |
| 43 | + ... ... | ||
| 43 | """ | 44 | """ |
| 44 | 45 | ||
| 45 | def __init__( | 46 | def __init__( |
| @@ -40,10 +40,13 @@ class Tensor: | |||
| 40 | Examples: | 40 | Examples: |
| 41 | >>> import pypto_pro.language as pl | 41 | >>> import pypto_pro.language as pl |
| 42 | >>> | 42 | >>> |
| 43 | - >>> @pl.function | 43 | + >>> @pl.jit |
| 44 | - ... def my_func(x: pl.Tensor[[64, 128], pl.DT_FP16, pl.NZ]) -> pl.Tensor[[64, 128], pl.DT_FP32]: | 44 | + ... def my_func( |
| 45 | - ... result: pl.Tensor[[64, 128], pl.DT_FP32] = pl.tensor.create_tensor([64, 128], dtype=pl.DT_FP32) | 45 | + ... x: pl.Tensor[[64, 128], pl.DT_FP16, pl.NZ], |
| 46 | - ... return result | 46 | + ... out: pl.Tensor[[64, 128], pl.DT_FP32], |
| 47 | + ... ): | ||
| 48 | + ... # Build the kernel body with explicit tile operations. | ||
| 49 | + ... ... | ||
| 47 | """ | 50 | """ |
| 48 | 51 | ||
| 49 | def __init__( | 52 | def __init__( |
| @@ -12,8 +12,7 @@ | |||
| 12 | """ | 12 | """ |
| 13 | PyPTO Runtime module - High-level kernel programming API. | 13 | PyPTO Runtime module - High-level kernel programming API. |
| 14 | 14 | ||
| 15 | -This module provides: | 15 | +This module provides the @pl.jit kernel frontend and: |
| 16 | -- @kernel decorator: combines @pl.program + @pl.function for single-kernel use cases | ||
| 17 | - Extended Tile/Tensor types with manual MemRef specification for address control | 16 | - Extended Tile/Tensor types with manual MemRef specification for address control |
| 18 | - Re-exports of common pypto_pro.language symbols for convenience | 17 | - Re-exports of common pypto_pro.language symbols for convenience |
| 19 | 18 | ||
| @@ -21,19 +20,13 @@ Typical usage: | |||
| 21 | import pypto_pro.language as pl | 20 | import pypto_pro.language as pl |
| 22 | 21 | ||
| 23 | @pl.jit | 22 | @pl.jit |
| 24 | - def my_kernel(x: pl.Tensor[[64, 128], pl.DT_FP16]) -> pl.Tensor[[64, 128], pl.DT_FP32]: | 23 | + def my_kernel(x: pl.Tensor[[64, 128], pl.DT_FP16]): |
| 25 | - tile = pl.load(x, [0, 0], [64, 64]) | 24 | + pl.system.bar_all() |
| 26 | - result = pl.add(tile, tile) | ||
| 27 | - return pl.store(result, [0, 0], [64, 64], x) | ||
| 28 | 25 | ||
| 29 | - # my_kernel is an ir.Program with a single function | 26 | + # my_kernel is lazily compiled on first launch. |
| 30 | """ | 27 | """ |
| 31 | 28 | ||
| 32 | -__all__ = [ | 29 | +__all__ = ["jit"] |
| 33 | - "kernel", | ||
| 34 | - "KernelDef", | ||
| 35 | - "jit", | ||
| 36 | -] | ||
| 37 | 30 | ||
| 38 | 31 | ||
| 39 | from pypto.pypto_impl.ir import MemorySpace # noqa: F401 | 32 | from pypto.pypto_impl.ir import MemorySpace # noqa: F401 |
| @@ -42,4 +35,3 @@ from pypto.pypto_impl.ir import MemorySpace # noqa: F401 | |||
| 42 | # (e.g. pl.pipeline.PipelineConfig), not exposed as top-level framework API. | 35 | # (e.g. pl.pipeline.PipelineConfig), not exposed as top-level framework API. |
| 43 | from . import pipeline # noqa: F401 | 36 | from . import pipeline # noqa: F401 |
| 44 | from .jit import jit | 37 | from .jit import jit |
| 45 | -from .kernel import KernelDef, kernel | ||
| @@ -1697,9 +1697,8 @@ class _TileJitKernel: | |||
| 1697 | and the bound signature reaches codegen through ``parse_target_program`` and | 1697 | and the bound signature reaches codegen through ``parse_target_program`` and |
| 1698 | ``probe_kernel_facts`` instead (see :func:`_codegen`). | 1698 | ``probe_kernel_facts`` instead (see :func:`_codegen`). |
| 1699 | """ | 1699 | """ |
| 1700 | - # Create KernelDef directly with the captured closure_vars | 1700 | + # Create KernelDef directly with the closure captured by @pl.jit at |
| 1701 | - # (cannot call @kernel decorator here because inspect.currentframe().f_back | 1701 | + # the user's decoration site. |
| 1702 | - # would point to this method instead of the user's module scope) | ||
| 1703 | from pypto_pro.runtime.kernel import KernelDef, extract_func_source_info | 1702 | from pypto_pro.runtime.kernel import KernelDef, extract_func_source_info |
| 1704 | 1703 | ||
| 1705 | f = self._func | 1704 | f = self._func |
| @@ -10,54 +10,119 @@ | |||
| 10 | # ----------------------------------------------------------------------------------------------------------- | 10 | # ----------------------------------------------------------------------------------------------------------- |
| 11 | 11 | ||
| 12 | """ | 12 | """ |
| 13 | -Kernel decorator for PyPTO Runtime. | 13 | +Internal kernel representation used by the PyPTO Pro JIT. |
| 14 | 14 | ||
| 15 | -The @kernel decorator provides a simplified API that wraps a single function | 15 | +Public kernels are defined with @pl.jit. KernelDef stores the captured source |
| 16 | -into a KernelDef, deferring AST parsing to compile time. | 16 | +and parser state for one specialization after the JIT frontend has resolved its |
| 17 | - | 17 | +compile-time configuration. |
| 18 | -Usage: | ||
| 19 | - import pypto_pro.language as pl | ||
| 20 | - | ||
| 21 | - @pl.jit() | ||
| 22 | - def vector_add( | ||
| 23 | - x: pl.Tensor[[1024], pl.DT_FP32], | ||
| 24 | - y: pl.Tensor[[1024], pl.DT_FP32], | ||
| 25 | - ) -> pl.Tensor[[1024], pl.DT_FP32]: | ||
| 26 | - tile_x = pl.load(x, [0], [1024]) | ||
| 27 | - tile_y = pl.load(y, [0], [1024]) | ||
| 28 | - result = pl.add(tile_x, tile_y) | ||
| 29 | - out = pl.create([1024], dtype=pl.DT_FP32) | ||
| 30 | - return pl.store(result, [0], [1024], out) | ||
| 31 | - | ||
| 32 | - # Direct call (default stream, block_dim=1): | ||
| 33 | - vector_add(x, y) | ||
| 34 | - | ||
| 35 | - # Bracket-launch syntax: | ||
| 36 | - vector_add[stream, block_dim](x, y) | ||
| 37 | """ | 18 | """ |
| 38 | 19 | ||
| 39 | from __future__ import annotations | 20 | from __future__ import annotations |
| 40 | 21 | ||
| 41 | -__all__ = ["kernel"] | 22 | +__all__ = ["KernelDef"] |
| 42 | 23 | ||
| 43 | 24 | ||
| 44 | import ast | 25 | import ast |
| 45 | import inspect | 26 | import inspect |
| 46 | -import sys | ||
| 47 | import textwrap | 27 | import textwrap |
| 48 | -from typing import Any, Callable, Optional | 28 | +from typing import Any, Callable, TypeVar |
| 49 | 29 | ||
| 50 | from pypto.pypto_impl import ir | 30 | from pypto.pypto_impl import ir |
| 51 | from pypto_pro.language.parser._ast_parser import ASTParser | 31 | from pypto_pro.language.parser._ast_parser import ASTParser |
| 52 | -from pypto_pro.language.parser.decorator import ( | ||
| 53 | - _attach_source_lines_to_error, | ||
| 54 | - _calculate_col_offset, | ||
| 55 | - _find_ast_node, | ||
| 56 | - _parse_ast_tree, | ||
| 57 | -) | ||
| 58 | from pypto_pro.language.parser.diagnostics import ParserError, ParserSyntaxError | 32 | from pypto_pro.language.parser.diagnostics import ParserError, ParserSyntaxError |
| 59 | 33 | ||
| 60 | 34 | ||
| 35 | +def _calculate_col_offset(source_lines: list[str]) -> int: | ||
| 36 | + """Calculate the column offset (indentation) of the first non-empty line. | ||
| 37 | + | ||
| 38 | + This is needed because ast.parse() requires code starting at column 0, | ||
| 39 | + but we need to report errors at the correct column in the original file. | ||
| 40 | + | ||
| 41 | + Args: | ||
| 42 | + source_lines: List of source code lines | ||
| 43 | + | ||
| 44 | + Returns: | ||
| 45 | + Column offset (number of leading spaces/tabs in first non-empty line) | ||
| 46 | + """ | ||
| 47 | + for line in source_lines: | ||
| 48 | + if line.strip(): # Skip empty lines | ||
| 49 | + return len(line) - len(line.lstrip()) | ||
| 50 | + return 0 | ||
| 51 | + | ||
| 52 | + | ||
| 53 | +def _parse_ast_tree(source_code: str, entity_type: str) -> ast.AST: | ||
| 54 | + """Parse source code into an AST tree with proper error handling. | ||
| 55 | + | ||
| 56 | + Args: | ||
| 57 | + source_code: Python source code to parse | ||
| 58 | + entity_type: Type of entity being parsed ("function" or "class") for error messages | ||
| 59 | + | ||
| 60 | + Returns: | ||
| 61 | + Parsed AST tree | ||
| 62 | + | ||
| 63 | + Raises: | ||
| 64 | + ParserSyntaxError: If the source code has syntax errors | ||
| 65 | + """ | ||
| 66 | + try: | ||
| 67 | + return ast.parse(source_code) | ||
| 68 | + except SyntaxError as e: | ||
| 69 | + raise ParserSyntaxError( | ||
| 70 | + f"Failed to parse {entity_type} source: {e.msg}", | ||
| 71 | + hint=f"Check for Python syntax errors in your {entity_type}", | ||
| 72 | + ) from e | ||
| 73 | + | ||
| 74 | + | ||
| 75 | +TypeASTNode = TypeVar("TypeASTNode", ast.FunctionDef, ast.ClassDef) | ||
| 76 | + | ||
| 77 | + | ||
| 78 | +def _find_ast_node(tree: ast.AST, node_type: type[TypeASTNode], name: str, entity_type: str) -> TypeASTNode: | ||
| 79 | + """Find a specific AST node by type and name. | ||
| 80 | + | ||
| 81 | + Args: | ||
| 82 | + tree: AST tree to search | ||
| 83 | + node_type: Type of AST node to find (ast.FunctionDef or ast.ClassDef) | ||
| 84 | + name: Name of the node to find | ||
| 85 | + entity_type: Type of entity for error messages ("function" or "class") | ||
| 86 | + | ||
| 87 | + Returns: | ||
| 88 | + Found AST node | ||
| 89 | + | ||
| 90 | + Raises: | ||
| 91 | + ParserSyntaxError: If the node cannot be found | ||
| 92 | + """ | ||
| 93 | + for node in ast.walk(tree): | ||
| 94 | + if isinstance(node, node_type) and node.name == name: | ||
| 95 | + return node | ||
| 96 | + | ||
| 97 | + raise ParserSyntaxError( | ||
| 98 | + f"Could not find {entity_type} definition for {name}", | ||
| 99 | + hint=f"Ensure the {entity_type} is properly defined", | ||
| 100 | + ) | ||
| 101 | + | ||
| 102 | + | ||
| 103 | +def _attach_source_lines_to_error(error: ParserError, source_file: str, source_lines_raw: list[str]) -> None: | ||
| 104 | + """Attach source lines to a ParserError if not already present. | ||
| 105 | + | ||
| 106 | + Args: | ||
| 107 | + error: ParserError to attach source lines to | ||
| 108 | + source_file: Path to the source file | ||
| 109 | + source_lines_raw: Raw source lines as fallback | ||
| 110 | + """ | ||
| 111 | + if error.source_lines is None: | ||
| 112 | + # Use the span's filename if it differs (e.g., error in an inline function) | ||
| 113 | + target_file = source_file | ||
| 114 | + if error.span and isinstance(error.span, dict): | ||
| 115 | + span_file = error.span.get("filename") | ||
| 116 | + if span_file and span_file != source_file: | ||
| 117 | + target_file = span_file | ||
| 118 | + try: | ||
| 119 | + with open(target_file, encoding="utf-8") as f: | ||
| 120 | + error.source_lines = f.read().split("\n") | ||
| 121 | + except Exception: | ||
| 122 | + # Fallback to the raw source lines if we can't read the file | ||
| 123 | + error.source_lines = source_lines_raw | ||
| 124 | + | ||
| 125 | + | ||
| 61 | def extract_func_source_info(f: Callable): | 126 | def extract_func_source_info(f: Callable): |
| 62 | """Extract source file, lines, offsets, and parsed func_def from a function. | 127 | """Extract source file, lines, offsets, and parsed func_def from a function. |
| 63 | 128 | ||
| @@ -180,7 +245,7 @@ class KernelDef: | |||
| 180 | bound_signature=bound_signature, | 245 | bound_signature=bound_signature, |
| 181 | # Kernels use a void ABI: they may early-return, but cannot return values. | 246 | # Kernels use a void ABI: they may early-return, but cannot return values. |
| 182 | void_return_only=True, | 247 | void_return_only=True, |
| 183 | - void_return_context="@pl.jit/@pl.kernel", | 248 | + void_return_context="@pl.jit", |
| 184 | allow_early_return=True, | 249 | allow_early_return=True, |
| 185 | ) | 250 | ) |
| 186 | 251 | ||
| @@ -218,91 +283,3 @@ class KernelDef: | |||
| 218 | except ParserError as e: | 283 | except ParserError as e: |
| 219 | _attach_source_lines_to_error(e, self._source_file, self._source_lines_raw) | 284 | _attach_source_lines_to_error(e, self._source_file, self._source_lines_raw) |
| 220 | raise | 285 | raise |
| 221 | - | ||
| 222 | - | ||
| 223 | -def _call_meta_and_capture_env(meta_fn): | ||
| 224 | - """Run meta_fn() and capture its local namespace (for types etc.). Returns (return_value, env dict).""" | ||
| 225 | - env = {} | ||
| 226 | - | ||
| 227 | - if meta_fn is None: | ||
| 228 | - return None, env | ||
| 229 | - | ||
| 230 | - def trace(frame, event, arg): | ||
| 231 | - if event == "return": | ||
| 232 | - env.clear() | ||
| 233 | - env.update(frame.f_locals) | ||
| 234 | - return trace | ||
| 235 | - | ||
| 236 | - old_trace = sys.gettrace() | ||
| 237 | - sys.settrace(trace) | ||
| 238 | - try: | ||
| 239 | - result = meta_fn() | ||
| 240 | - finally: | ||
| 241 | - sys.settrace(old_trace) | ||
| 242 | - return result, env | ||
| 243 | - | ||
| 244 | - | ||
| 245 | -def kernel( | ||
| 246 | - func: Optional[Callable] = None, | ||
| 247 | - meta_data=None, | ||
| 248 | - *, | ||
| 249 | - name: Optional[str] = None, | ||
| 250 | - func_type: ir.FunctionType = ir.FunctionType.Opaque, | ||
| 251 | - strict_ssa: bool = False, | ||
| 252 | - auto_mutex: bool = True, | ||
| 253 | - pipeline=None, | ||
| 254 | -) -> "KernelDef": | ||
| 255 | - """Decorator that captures a DSL function for deferred compilation. | ||
| 256 | - | ||
| 257 | - The decorated function becomes a :class:`KernelDef` — a lazy definition | ||
| 258 | - that records the source code, AST, and closure at decoration time but | ||
| 259 | - defers parsing until JIT codegen runs. This allows the target | ||
| 260 | - architecture to be specified once at compile time. | ||
| 261 | - | ||
| 262 | - Args: | ||
| 263 | - func: Python function to capture. | ||
| 264 | - name: Optional program name (defaults to function name). | ||
| 265 | - func_type: Function type (Opaque, InCore, or Helper). | ||
| 266 | - strict_ssa: If True, enforce SSA (single assignment per variable). | ||
| 267 | - | ||
| 268 | - Returns: | ||
| 269 | - KernelDef consumed by the JIT codegen path. | ||
| 270 | - | ||
| 271 | - Example: | ||
| 272 | - >>> @pl.kernel | ||
| 273 | - ... def my_kernel(x: pl.Tensor[[64], pl.DT_FP32]) -> pl.Tensor[[64], pl.DT_FP32]: | ||
| 274 | - ... tile = pl.load(x, [0], [64]) | ||
| 275 | - ... result = pl.add(tile, tile) | ||
| 276 | - ... return pl.store(result, [0], [64], x) | ||
| 277 | - >>> isinstance(my_kernel, KernelDef) | ||
| 278 | - True | ||
| 279 | - """ | ||
| 280 | - # Capture caller's scope so the parser can resolve names like `pl`, etc. | ||
| 281 | - # Must be captured here (not inside _decorator) to get the correct call-site frame. | ||
| 282 | - caller_frame = inspect.currentframe().f_back | ||
| 283 | - closure_vars = {**caller_frame.f_globals, **caller_frame.f_locals} | ||
| 284 | - | ||
| 285 | - def _decorator(f: Callable) -> KernelDef: | ||
| 286 | - (source_file, source_lines, source_lines_raw, line_offset, col_offset, func_def) = extract_func_source_info(f) | ||
| 287 | - | ||
| 288 | - return KernelDef( | ||
| 289 | - func=f, | ||
| 290 | - source_file=source_file, | ||
| 291 | - source_lines=source_lines, | ||
| 292 | - source_lines_raw=source_lines_raw, | ||
| 293 | - line_offset=line_offset, | ||
| 294 | - col_offset=col_offset, | ||
| 295 | - func_def=func_def, | ||
| 296 | - closure_vars=closure_vars, | ||
| 297 | - name=name, | ||
| 298 | - func_type=func_type, | ||
| 299 | - strict_ssa=strict_ssa, | ||
| 300 | - meta_data=meta_data, | ||
| 301 | - auto_mutex=auto_mutex, | ||
| 302 | - pipeline=pipeline, | ||
| 303 | - ) | ||
| 304 | - | ||
| 305 | - if func is None: | ||
| 306 | - return _decorator | ||
| 307 | - else: | ||
| 308 | - return _decorator(func) | ||
| @@ -871,7 +871,7 @@ def probe_kernel_facts(kernel_def, bound_signature=None) -> tuple[dict, dict]: | |||
| 871 | datatype_consts=kernel_def._datatype_consts, | 871 | datatype_consts=kernel_def._datatype_consts, |
| 872 | bound_signature=bound_signature, | 872 | bound_signature=bound_signature, |
| 873 | void_return_only=True, | 873 | void_return_only=True, |
| 874 | - void_return_context="@pl.jit/@pl.kernel", | 874 | + void_return_context="@pl.jit", |
| 875 | allow_early_return=True, | 875 | allow_early_return=True, |
| 876 | ) | 876 | ) |
| 877 | parser.collect_if_const = True | 877 | parser.collect_if_const = True |
| @@ -34,10 +34,10 @@ ROWS = 64 | |||
| 34 | COLS = 64 | 34 | COLS = 64 |
| 35 | 35 | ||
| 36 | 36 | ||
| 37 | -def _compile_to_cce(kernel_def) -> str: | 37 | +def _compile_to_cce(kernel) -> str: |
| 38 | from pypto_pro.runtime.jit import _assemble_cv_source, _parse_and_codegen_targets | 38 | from pypto_pro.runtime.jit import _assemble_cv_source, _parse_and_codegen_targets |
| 39 | 39 | ||
| 40 | - cube, vector = _parse_and_codegen_targets(kernel_def, "a5", "") | 40 | + cube, vector = _parse_and_codegen_targets(kernel.to_kernel_def(), "a5", "") |
| 41 | return _assemble_cv_source(cube, vector).content | 41 | return _assemble_cv_source(cube, vector).content |
| 42 | 42 | ||
| 43 | 43 | ||
| @@ -55,7 +55,7 @@ def _bare_tile_decl(source: str, var: str) -> str: | |||
| 55 | return match.group(1) | 55 | return match.group(1) |
| 56 | 56 | ||
| 57 | 57 | ||
| 58 | -@pl.kernel | 58 | +@pl.jit |
| 59 | def _compact_tiles_kernel(x: pl.Tensor[[ROWS, COLS], pl.DT_FP32]): | 59 | def _compact_tiles_kernel(x: pl.Tensor[[ROWS, COLS], pl.DT_FP32]): |
| 60 | """One column tile and one ordinary tile, both asking for compact=1.""" | 60 | """One column tile and one ordinary tile, both asking for compact=1.""" |
| 61 | column_type = pl.TileType( | 61 | column_type = pl.TileType( |
| @@ -71,7 +71,7 @@ def _compact_tiles_kernel(x: pl.Tensor[[ROWS, COLS], pl.DT_FP32]): | |||
| 71 | pl.load(column.current(), x, [0, 0]) | 71 | pl.load(column.current(), x, [0, 0]) |
| 72 | 72 | ||
| 73 | 73 | ||
| 74 | -@pl.kernel | 74 | +@pl.jit |
| 75 | def _column_tile_declared_nd_kernel(x: pl.Tensor[[ROWS, COLS], pl.DT_FP32]): | 75 | def _column_tile_declared_nd_kernel(x: pl.Tensor[[ROWS, COLS], pl.DT_FP32]): |
| 76 | """A column tile asking for pl.ND, which the ISA cannot honour for a 1-wide tile.""" | 76 | """A column tile asking for pl.ND, which the ISA cannot honour for a 1-wide tile.""" |
| 77 | column_type = pl.TileType( | 77 | column_type = pl.TileType( |
| @@ -82,7 +82,7 @@ def _column_tile_declared_nd_kernel(x: pl.Tensor[[ROWS, COLS], pl.DT_FP32]): | |||
| 82 | pl.load(column.current(), x, [0, 0]) | 82 | pl.load(column.current(), x, [0, 0]) |
| 83 | 83 | ||
| 84 | 84 | ||
| 85 | -@pl.kernel | 85 | +@pl.jit |
| 86 | def _if_merged_column_tile_kernel(x: pl.Tensor[[ROWS, COLS], pl.DT_FP32], flag: pl.Tensor[[1, 1], pl.DT_INT32]): | 86 | def _if_merged_column_tile_kernel(x: pl.Tensor[[ROWS, COLS], pl.DT_FP32], flag: pl.Tensor[[1, 1], pl.DT_INT32]): |
| 87 | """A column tile merged out of an if, which has no memref of its own.""" | 87 | """A column tile merged out of an if, which has no memref of its own.""" |
| 88 | column_type = pl.TileType(shape=[ROWS, 1], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | 88 | column_type = pl.TileType(shape=[ROWS, 1], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) |
| @@ -145,7 +145,7 @@ def test_fractal_layout_on_a_column_tile_is_rejected(): | |||
| 145 | """ | 145 | """ |
| 146 | with pytest.raises(Exception, match="requires layout ND or DN"): | 146 | with pytest.raises(Exception, match="requires layout ND or DN"): |
| 147 | 147 | ||
| 148 | - @pl.kernel | 148 | + @pl.jit |
| 149 | def _fractal_column_kernel(x: pl.Tensor[[ROWS, COLS], pl.DT_FP16]): | 149 | def _fractal_column_kernel(x: pl.Tensor[[ROWS, COLS], pl.DT_FP16]): |
| 150 | column_type = pl.TileType( | 150 | column_type = pl.TileType( |
| 151 | shape=[ROWS, 1], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat, layout=pl.ZN | 151 | shape=[ROWS, 1], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat, layout=pl.ZN |
| @@ -15,10 +15,10 @@ import pypto_pro.language as pl | |||
| 15 | import pytest | 15 | import pytest |
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | -def _compile_to_cce(kernel_def) -> str: | 18 | +def _compile_to_cce(kernel) -> str: |
| 19 | from pypto_pro.runtime.jit import _assemble_cv_source, _parse_and_codegen_targets | 19 | from pypto_pro.runtime.jit import _assemble_cv_source, _parse_and_codegen_targets |
| 20 | 20 | ||
| 21 | - cube, vector = _parse_and_codegen_targets(kernel_def, "a5", "") | 21 | + cube, vector = _parse_and_codegen_targets(kernel.to_kernel_def(), "a5", "") |
| 22 | return _assemble_cv_source(cube, vector).content | 22 | return _assemble_cv_source(cube, vector).content |
| 23 | 23 | ||
| 24 | 24 | ||
| @@ -59,7 +59,7 @@ def _consume_pair(bundle, index): | |||
| 59 | return | 59 | return |
| 60 | 60 | ||
| 61 | 61 | ||
| 62 | -@pl.kernel | 62 | +@pl.jit |
| 63 | def _inline_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): | 63 | def _inline_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): |
| 64 | extent = _choose_extent(_choose_in_loop(a.shape[0])) | 64 | extent = _choose_extent(_choose_in_loop(a.shape[0])) |
| 65 | start, stop = _pair(0, extent) | 65 | start, stop = _pair(0, extent) |
| @@ -251,7 +251,7 @@ def _ret_void_twice(value): | |||
| 251 | # its single use, or clobbered on the way out of the wrapper, would show up here. | 251 | # its single use, or clobbered on the way out of the wrapper, would show up here. |
| 252 | 252 | ||
| 253 | 253 | ||
| 254 | -@pl.kernel | 254 | +@pl.jit |
| 255 | def _named_tuple_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): | 255 | def _named_tuple_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): |
| 256 | bounds = _ret_named_tuple(a.shape[0]) | 256 | bounds = _ret_named_tuple(a.shape[0]) |
| 257 | total = bounds.lo + bounds.hi | 257 | total = bounds.lo + bounds.hi |
| @@ -259,7 +259,7 @@ def _named_tuple_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16 | |||
| 259 | pl.system.bar_all() | 259 | pl.system.bar_all() |
| 260 | 260 | ||
| 261 | 261 | ||
| 262 | -@pl.kernel | 262 | +@pl.jit |
| 263 | def _struct_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): | 263 | def _struct_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): |
| 264 | bounds = _ret_struct(a.shape[0]) | 264 | bounds = _ret_struct(a.shape[0]) |
| 265 | total = bounds.v + bounds.w | 265 | total = bounds.v + bounds.w |
| @@ -267,7 +267,7 @@ def _struct_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): | |||
| 267 | pl.system.bar_all() | 267 | pl.system.bar_all() |
| 268 | 268 | ||
| 269 | 269 | ||
| 270 | -@pl.kernel | 270 | +@pl.jit |
| 271 | def _struct_in_tuple_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): | 271 | def _struct_in_tuple_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): |
| 272 | bundle = _ret_struct_in_tuple(a.shape[0]) | 272 | bundle = _ret_struct_in_tuple(a.shape[0]) |
| 273 | bounds = bundle[0] | 273 | bounds = bundle[0] |
| @@ -276,7 +276,7 @@ def _struct_in_tuple_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_ | |||
| 276 | pl.system.bar_all() | 276 | pl.system.bar_all() |
| 277 | 277 | ||
| 278 | 278 | ||
| 279 | -@pl.kernel | 279 | +@pl.jit |
| 280 | def _struct_array_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): | 280 | def _struct_array_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): |
| 281 | first, second = _ret_struct_array(a.shape[0]) | 281 | first, second = _ret_struct_array(a.shape[0]) |
| 282 | total = first.v + second.w | 282 | total = first.v + second.w |
| @@ -284,7 +284,7 @@ def _struct_array_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP1 | |||
| 284 | pl.system.bar_all() | 284 | pl.system.bar_all() |
| 285 | 285 | ||
| 286 | 286 | ||
| 287 | -@pl.kernel | 287 | +@pl.jit |
| 288 | def _tile_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): | 288 | def _tile_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): |
| 289 | tile_type = pl.TileType(shape=[64, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 289 | tile_type = pl.TileType(shape=[64, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 290 | tile_a = pl.make_tile(tile_type, addr=0x0000, size=16384) | 290 | tile_a = pl.make_tile(tile_type, addr=0x0000, size=16384) |
| @@ -294,7 +294,7 @@ def _tile_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): | |||
| 294 | pl.store(a, picked, [0, 0]) | 294 | pl.store(a, picked, [0, 0]) |
| 295 | 295 | ||
| 296 | 296 | ||
| 297 | -@pl.kernel | 297 | +@pl.jit |
| 298 | def _void_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): | 298 | def _void_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): |
| 299 | _ret_void_twice(a.shape[0]) | 299 | _ret_void_twice(a.shape[0]) |
| 300 | for _i in pl.range(0, a.shape[0], 1): | 300 | for _i in pl.range(0, a.shape[0], 1): |
| @@ -440,7 +440,7 @@ def _both_branches_return(value): | |||
| 440 | 440 | ||
| 441 | 441 | ||
| 442 | def _make_shape_kernel(helper): | 442 | def _make_shape_kernel(helper): |
| 443 | - @pl.kernel | 443 | + @pl.jit |
| 444 | def kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): | 444 | def kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): |
| 445 | limit = helper(a.shape[0]) | 445 | limit = helper(a.shape[0]) |
| 446 | doubled = limit + limit | 446 | doubled = limit + limit |
| @@ -450,7 +450,7 @@ def _make_shape_kernel(helper): | |||
| 450 | return kernel | 450 | return kernel |
| 451 | 451 | ||
| 452 | 452 | ||
| 453 | -@pl.kernel | 453 | +@pl.jit |
| 454 | def _for_if_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): | 454 | def _for_if_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): |
| 455 | limit = _for_if_return(a.shape[0]) | 455 | limit = _for_if_return(a.shape[0]) |
| 456 | doubled = limit + limit | 456 | doubled = limit + limit |
| @@ -458,7 +458,7 @@ def _for_if_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): | |||
| 458 | pl.system.bar_all() | 458 | pl.system.bar_all() |
| 459 | 459 | ||
| 460 | 460 | ||
| 461 | -@pl.kernel | 461 | +@pl.jit |
| 462 | def _if_for_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): | 462 | def _if_for_return_kernel(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): |
| 463 | limit = _if_for_return(a.shape[0], a.shape[1]) | 463 | limit = _if_for_return(a.shape[0], a.shape[1]) |
| 464 | doubled = limit + limit | 464 | doubled = limit + limit |
| @@ -13,14 +13,14 @@ | |||
| 13 | import pypto_pro.language as pl | 13 | import pypto_pro.language as pl |
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | -def _compile_to_cce(kernel_def) -> str: | 16 | +def _compile_to_cce(kernel) -> str: |
| 17 | from pypto_pro.runtime.jit import _assemble_cv_source, _parse_and_codegen_targets | 17 | from pypto_pro.runtime.jit import _assemble_cv_source, _parse_and_codegen_targets |
| 18 | 18 | ||
| 19 | - cube, vector = _parse_and_codegen_targets(kernel_def, "a5", "") | 19 | + cube, vector = _parse_and_codegen_targets(kernel.to_kernel_def(), "a5", "") |
| 20 | return _assemble_cv_source(cube, vector).content | 20 | return _assemble_cv_source(cube, vector).content |
| 21 | 21 | ||
| 22 | 22 | ||
| 23 | -@pl.kernel(auto_mutex=True) | 23 | +@pl.jit(auto_mutex=True) |
| 24 | def _mixed_multi_mutex_ids_kernel(x: pl.Tensor[[64, 32], pl.DT_FP16]): | 24 | def _mixed_multi_mutex_ids_kernel(x: pl.Tensor[[64, 32], pl.DT_FP16]): |
| 25 | tile_type = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 25 | tile_type = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 26 | source_group = pl.make_tile_group(type=tile_type, addrs=0x0000, mutex_ids=[0, 1]) | 26 | source_group = pl.make_tile_group(type=tile_type, addrs=0x0000, mutex_ids=[0, 1]) |
| @@ -16,10 +16,10 @@ import re | |||
| 16 | import pypto_pro.language as pl | 16 | import pypto_pro.language as pl |
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | -def _compile_to_cce(kernel_def) -> str: | 19 | +def _compile_to_cce(kernel) -> str: |
| 20 | from pypto_pro.runtime.jit import _add_kernel_header, _assemble_cv_source, _parse_and_codegen_targets | 20 | from pypto_pro.runtime.jit import _add_kernel_header, _assemble_cv_source, _parse_and_codegen_targets |
| 21 | 21 | ||
| 22 | - cube, vector = _parse_and_codegen_targets(kernel_def, "a5", "") | 22 | + cube, vector = _parse_and_codegen_targets(kernel.to_kernel_def(), "a5", "") |
| 23 | return _add_kernel_header(_assemble_cv_source(cube, vector)).content | 23 | return _add_kernel_header(_assemble_cv_source(cube, vector)).content |
| 24 | 24 | ||
| 25 | 25 | ||
| @@ -40,7 +40,7 @@ def _scalar_cast_intrinsics( | |||
| 40 | plain_out[0, 0] = pl.simt.cast(integer, pl.DT_INT32) | 40 | plain_out[0, 0] = pl.simt.cast(integer, pl.DT_INT32) |
| 41 | 41 | ||
| 42 | 42 | ||
| 43 | -@pl.kernel | 43 | +@pl.jit |
| 44 | def _scalar_cast_codegen_kernel(value: pl.DT_FP32, integer: pl.DT_INT64): | 44 | def _scalar_cast_codegen_kernel(value: pl.DT_FP32, integer: pl.DT_INT64): |
| 45 | half_type = pl.TileType(shape=[1, 1], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 45 | half_type = pl.TileType(shape=[1, 1], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 46 | bfloat_type = pl.TileType(shape=[1, 1], dtype=pl.DT_BF16, target_memory=pl.MemorySpace.Vec) | 46 | bfloat_type = pl.TileType(shape=[1, 1], dtype=pl.DT_BF16, target_memory=pl.MemorySpace.Vec) |
| @@ -125,7 +125,7 @@ def _scalar_cast_all_intrinsics( | |||
| 125 | int32_out[0, 0] = pl.simt.cast(int64_value, pl.DT_INT32) | 125 | int32_out[0, 0] = pl.simt.cast(int64_value, pl.DT_INT32) |
| 126 | 126 | ||
| 127 | 127 | ||
| 128 | -@pl.kernel | 128 | +@pl.jit |
| 129 | def _scalar_cast_all_codegen_kernel( | 129 | def _scalar_cast_all_codegen_kernel( |
| 130 | value: pl.DT_FP32, | 130 | value: pl.DT_FP32, |
| 131 | int32_value: pl.DT_INT32, | 131 | int32_value: pl.DT_INT32, |
| @@ -14,10 +14,10 @@ | |||
| 14 | import pypto_pro.language as pl | 14 | import pypto_pro.language as pl |
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | -def _compile_to_cce(kernel_def) -> str: | 17 | +def _compile_to_cce(kernel) -> str: |
| 18 | from pypto_pro.runtime.jit import _add_kernel_header, _assemble_cv_source, _parse_and_codegen_targets | 18 | from pypto_pro.runtime.jit import _add_kernel_header, _assemble_cv_source, _parse_and_codegen_targets |
| 19 | 19 | ||
| 20 | - cube, vector = _parse_and_codegen_targets(kernel_def, "a5", "") | 20 | + cube, vector = _parse_and_codegen_targets(kernel.to_kernel_def(), "a5", "") |
| 21 | return _add_kernel_header(_assemble_cv_source(cube, vector)).content | 21 | return _add_kernel_header(_assemble_cv_source(cube, vector)).content |
| 22 | 22 | ||
| 23 | 23 | ||
| @@ -50,7 +50,7 @@ def _fp32_math_intrinsics( | |||
| 50 | flags[0, 1] = pl.simt.isinf(value) | 50 | flags[0, 1] = pl.simt.isinf(value) |
| 51 | 51 | ||
| 52 | 52 | ||
| 53 | -@pl.kernel | 53 | +@pl.jit |
| 54 | def _fp32_math_codegen_kernel(value: pl.DT_FP32): | 54 | def _fp32_math_codegen_kernel(value: pl.DT_FP32): |
| 55 | out_type = pl.TileType(shape=[1, 19], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | 55 | out_type = pl.TileType(shape=[1, 19], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) |
| 56 | flags_type = pl.TileType(shape=[1, 2], dtype=pl.DT_BOOL, target_memory=pl.MemorySpace.Vec) | 56 | flags_type = pl.TileType(shape=[1, 2], dtype=pl.DT_BOOL, target_memory=pl.MemorySpace.Vec) |
| @@ -89,8 +89,8 @@ def _fp16_math_intrinsics( | |||
| 89 | flags[0, 1] = pl.simt.isinf(value) | 89 | flags[0, 1] = pl.simt.isinf(value) |
| 90 | 90 | ||
| 91 | 91 | ||
| 92 | -@pl.kernel | 92 | +@pl.jit |
| 93 | -def _fp16_math_codegen_kernel(): | 93 | +def _fp16_math_codegen_kernel(_jit_entry: pl.DT_INT64): |
| 94 | out_type = pl.TileType(shape=[1, 18], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 94 | out_type = pl.TileType(shape=[1, 18], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 95 | flags_type = pl.TileType(shape=[1, 2], dtype=pl.DT_BOOL, target_memory=pl.MemorySpace.Vec) | 95 | flags_type = pl.TileType(shape=[1, 2], dtype=pl.DT_BOOL, target_memory=pl.MemorySpace.Vec) |
| 96 | source_type = pl.TileType(shape=[1, 1], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 96 | source_type = pl.TileType(shape=[1, 1], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| @@ -130,8 +130,8 @@ def _bf16_math_intrinsics( | |||
| 130 | flags[0, 1] = pl.simt.isinf(value) | 130 | flags[0, 1] = pl.simt.isinf(value) |
| 131 | 131 | ||
| 132 | 132 | ||
| 133 | -@pl.kernel | 133 | +@pl.jit |
| 134 | -def _bf16_math_codegen_kernel(): | 134 | +def _bf16_math_codegen_kernel(_jit_entry: pl.DT_INT64): |
| 135 | out_type = pl.TileType(shape=[1, 18], dtype=pl.DT_BF16, target_memory=pl.MemorySpace.Vec) | 135 | out_type = pl.TileType(shape=[1, 18], dtype=pl.DT_BF16, target_memory=pl.MemorySpace.Vec) |
| 136 | flags_type = pl.TileType(shape=[1, 2], dtype=pl.DT_BOOL, target_memory=pl.MemorySpace.Vec) | 136 | flags_type = pl.TileType(shape=[1, 2], dtype=pl.DT_BOOL, target_memory=pl.MemorySpace.Vec) |
| 137 | source_type = pl.TileType(shape=[1, 1], dtype=pl.DT_BF16, target_memory=pl.MemorySpace.Vec) | 137 | source_type = pl.TileType(shape=[1, 1], dtype=pl.DT_BF16, target_memory=pl.MemorySpace.Vec) |
| @@ -152,8 +152,8 @@ def _int64_math_intrinsics( | |||
| 152 | out[0, 2] = pl.simt.max(source[0, 0], source[0, 1]) | 152 | out[0, 2] = pl.simt.max(source[0, 0], source[0, 1]) |
| 153 | 153 | ||
| 154 | 154 | ||
| 155 | -@pl.kernel | 155 | +@pl.jit |
| 156 | -def _int64_math_codegen_kernel(): | 156 | +def _int64_math_codegen_kernel(_jit_entry: pl.DT_INT64): |
| 157 | out_type = pl.TileType(shape=[1, 3], dtype=pl.DT_INT64, target_memory=pl.MemorySpace.Vec) | 157 | out_type = pl.TileType(shape=[1, 3], dtype=pl.DT_INT64, target_memory=pl.MemorySpace.Vec) |
| 158 | source_type = pl.TileType(shape=[1, 2], dtype=pl.DT_INT64, target_memory=pl.MemorySpace.Vec) | 158 | source_type = pl.TileType(shape=[1, 2], dtype=pl.DT_INT64, target_memory=pl.MemorySpace.Vec) |
| 159 | out = pl.make_tile(out_type, addr=0x0000, size=24) | 159 | out = pl.make_tile(out_type, addr=0x0000, size=24) |
| @@ -23,14 +23,14 @@ import logging | |||
| 23 | import pypto_pro.language as pl | 23 | import pypto_pro.language as pl |
| 24 | 24 | ||
| 25 | 25 | ||
| 26 | -def _compile_to_cce(kernel_def) -> str: | 26 | +def _compile_to_cce(kernel) -> str: |
| 27 | from pypto_pro.runtime.jit import _assemble_cv_source, _parse_and_codegen_targets | 27 | from pypto_pro.runtime.jit import _assemble_cv_source, _parse_and_codegen_targets |
| 28 | 28 | ||
| 29 | - cube, vector = _parse_and_codegen_targets(kernel_def, "a5", "") | 29 | + cube, vector = _parse_and_codegen_targets(kernel.to_kernel_def(), "a5", "") |
| 30 | return _assemble_cv_source(cube, vector).content | 30 | return _assemble_cv_source(cube, vector).content |
| 31 | 31 | ||
| 32 | 32 | ||
| 33 | -@pl.kernel | 33 | +@pl.jit |
| 34 | def _sel_kernel(x: pl.Tensor[[64, 128], pl.DT_FP16]): | 34 | def _sel_kernel(x: pl.Tensor[[64, 128], pl.DT_FP16]): |
| 35 | tt = pl.TileType(shape=[64, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 35 | tt = pl.TileType(shape=[64, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 36 | lhs = pl.make_tile(tt, addr=0x0000, size=16384) | 36 | lhs = pl.make_tile(tt, addr=0x0000, size=16384) |
| @@ -43,7 +43,7 @@ def _sel_kernel(x: pl.Tensor[[64, 128], pl.DT_FP16]): | |||
| 43 | pl.select(out, mask, lhs, rhs, tmp) | 43 | pl.select(out, mask, lhs, rhs, tmp) |
| 44 | 44 | ||
| 45 | 45 | ||
| 46 | -@pl.kernel | 46 | +@pl.jit |
| 47 | def _sels_kernel(x: pl.Tensor[[64, 128], pl.DT_FP16]): | 47 | def _sels_kernel(x: pl.Tensor[[64, 128], pl.DT_FP16]): |
| 48 | tt = pl.TileType(shape=[64, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 48 | tt = pl.TileType(shape=[64, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 49 | src = pl.make_tile(tt, addr=0x0000, size=16384) | 49 | src = pl.make_tile(tt, addr=0x0000, size=16384) |
| @@ -55,7 +55,7 @@ def _sels_kernel(x: pl.Tensor[[64, 128], pl.DT_FP16]): | |||
| 55 | pl.select(out, mask, src, 0.0, tmp) | 55 | pl.select(out, mask, src, 0.0, tmp) |
| 56 | 56 | ||
| 57 | 57 | ||
| 58 | -@pl.kernel(auto_mutex=True) | 58 | +@pl.jit(auto_mutex=True) |
| 59 | def _sel_auto_mutex_kernel( | 59 | def _sel_auto_mutex_kernel( |
| 60 | x: pl.Tensor[[64, 128], pl.DT_FP16], | 60 | x: pl.Tensor[[64, 128], pl.DT_FP16], |
| 61 | ): | 61 | ): |
| @@ -17,10 +17,10 @@ import pypto_pro.language as pl | |||
| 17 | import pytest | 17 | import pytest |
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | -def _compile_to_cce(kernel_def, arch: str = "a5") -> str: | 20 | +def _compile_to_cce(kernel, arch: str = "a5") -> str: |
| 21 | from pypto_pro.runtime.jit import _assemble_cv_source, _parse_and_codegen_targets | 21 | from pypto_pro.runtime.jit import _assemble_cv_source, _parse_and_codegen_targets |
| 22 | 22 | ||
| 23 | - cube, vector = _parse_and_codegen_targets(kernel_def, arch, "") | 23 | + cube, vector = _parse_and_codegen_targets(kernel.to_kernel_def(), arch, "") |
| 24 | return _assemble_cv_source(cube, vector).content | 24 | return _assemble_cv_source(cube, vector).content |
| 25 | 25 | ||
| 26 | 26 | ||
| @@ -127,7 +127,7 @@ def _tile_valid_shape_access( | |||
| 127 | dst[row, col] = src[row, col] | 127 | dst[row, col] = src[row, col] |
| 128 | 128 | ||
| 129 | 129 | ||
| 130 | -@pl.kernel | 130 | +@pl.jit |
| 131 | def _simt_tile_codegen_kernel( | 131 | def _simt_tile_codegen_kernel( |
| 132 | x: pl.Tensor[[1, 256], pl.DT_FP32], | 132 | x: pl.Tensor[[1, 256], pl.DT_FP32], |
| 133 | out: pl.Tensor[[1, 256], pl.DT_FP32], | 133 | out: pl.Tensor[[1, 256], pl.DT_FP32], |
| @@ -148,7 +148,7 @@ def _simt_tile_codegen_kernel( | |||
| 148 | pl.store(out, dst, [0, 0]) | 148 | pl.store(out, dst, [0, 0]) |
| 149 | 149 | ||
| 150 | 150 | ||
| 151 | -@pl.kernel | 151 | +@pl.jit |
| 152 | def _simt_gm_codegen_kernel( | 152 | def _simt_gm_codegen_kernel( |
| 153 | x: pl.Tensor[[1, 256], pl.DT_FP32], | 153 | x: pl.Tensor[[1, 256], pl.DT_FP32], |
| 154 | out: pl.Tensor[[1, 256], pl.DT_FP32], | 154 | out: pl.Tensor[[1, 256], pl.DT_FP32], |
| @@ -159,15 +159,15 @@ def _simt_gm_codegen_kernel( | |||
| 159 | pl.simt.launch(_gm_add, threads=256, args=(out, x, n, delta)) | 159 | pl.simt.launch(_gm_add, threads=256, args=(out, x, n, delta)) |
| 160 | 160 | ||
| 161 | 161 | ||
| 162 | -@pl.kernel | 162 | +@pl.jit |
| 163 | -def _simt_context_codegen_kernel(): | 163 | +def _simt_context_codegen_kernel(_jit_entry: pl.DT_INT64): |
| 164 | tile_type = pl.TileType(shape=[1, 256], dtype=pl.DT_UINT32, target_memory=pl.MemorySpace.Vec) | 164 | tile_type = pl.TileType(shape=[1, 256], dtype=pl.DT_UINT32, target_memory=pl.MemorySpace.Vec) |
| 165 | dst = pl.make_tile(tile_type, addr=0x0000, size=1024) | 165 | dst = pl.make_tile(tile_type, addr=0x0000, size=1024) |
| 166 | with pl.section_vector(): | 166 | with pl.section_vector(): |
| 167 | pl.simt.launch(_context_probe, threads=(8, 4, 8), args=(dst,)) | 167 | pl.simt.launch(_context_probe, threads=(8, 4, 8), args=(dst,)) |
| 168 | 168 | ||
| 169 | 169 | ||
| 170 | -@pl.kernel | 170 | +@pl.jit |
| 171 | def _simt_callee_codegen_kernel(src: pl.Tensor[[1, 32], pl.DT_INT32], delta: pl.DT_INT32): | 171 | def _simt_callee_codegen_kernel(src: pl.Tensor[[1, 32], pl.DT_INT32], delta: pl.DT_INT32): |
| 172 | tile_type = pl.TileType(shape=[1, 32], dtype=pl.DT_INT32, target_memory=pl.MemorySpace.Vec) | 172 | tile_type = pl.TileType(shape=[1, 32], dtype=pl.DT_INT32, target_memory=pl.MemorySpace.Vec) |
| 173 | dst = pl.make_tile(tile_type, addr=0x0000, size=128) | 173 | dst = pl.make_tile(tile_type, addr=0x0000, size=128) |
| @@ -175,7 +175,7 @@ def _simt_callee_codegen_kernel(src: pl.Tensor[[1, 32], pl.DT_INT32], delta: pl. | |||
| 175 | pl.simt.launch(_callee_entry, threads=32, args=(dst, src, delta)) | 175 | pl.simt.launch(_callee_entry, threads=32, args=(dst, src, delta)) |
| 176 | 176 | ||
| 177 | 177 | ||
| 178 | -@pl.kernel | 178 | +@pl.jit |
| 179 | def _simt_valid_shape_codegen_kernel(valid_rows: pl.DT_UINT32, valid_cols: pl.DT_UINT32): | 179 | def _simt_valid_shape_codegen_kernel(valid_rows: pl.DT_UINT32, valid_cols: pl.DT_UINT32): |
| 180 | tile_type = pl.TileType( | 180 | tile_type = pl.TileType( |
| 181 | shape=[8, 64], | 181 | shape=[8, 64], |
| @@ -251,7 +251,7 @@ def _atomic_rmw_discard( | |||
| 251 | pl.simt.atomic_dec(counter[0, 0], limit) | 251 | pl.simt.atomic_dec(counter[0, 0], limit) |
| 252 | 252 | ||
| 253 | 253 | ||
| 254 | -@pl.kernel | 254 | +@pl.jit |
| 255 | def _atomic_add_ub_codegen_kernel(value: pl.DT_INT32): | 255 | def _atomic_add_ub_codegen_kernel(value: pl.DT_INT32): |
| 256 | tile_type = pl.TileType(shape=[1, 32], dtype=pl.DT_INT32, target_memory=pl.MemorySpace.Vec) | 256 | tile_type = pl.TileType(shape=[1, 32], dtype=pl.DT_INT32, target_memory=pl.MemorySpace.Vec) |
| 257 | dst = pl.make_tile(tile_type, addr=0x0000, size=128) | 257 | dst = pl.make_tile(tile_type, addr=0x0000, size=128) |
| @@ -260,7 +260,7 @@ def _atomic_add_ub_codegen_kernel(value: pl.DT_INT32): | |||
| 260 | pl.simt.launch(_atomic_add_ub, threads=32, args=(dst, old_values, value)) | 260 | pl.simt.launch(_atomic_add_ub, threads=32, args=(dst, old_values, value)) |
| 261 | 261 | ||
| 262 | 262 | ||
| 263 | -@pl.kernel | 263 | +@pl.jit |
| 264 | def _atomic_add_gm_codegen_kernel( | 264 | def _atomic_add_gm_codegen_kernel( |
| 265 | dst: pl.Tensor[[1, 32], pl.DT_INT64], | 265 | dst: pl.Tensor[[1, 32], pl.DT_INT64], |
| 266 | old_values: pl.Tensor[[1, 32], pl.DT_INT64], | 266 | old_values: pl.Tensor[[1, 32], pl.DT_INT64], |
| @@ -270,7 +270,7 @@ def _atomic_add_gm_codegen_kernel( | |||
| 270 | pl.simt.launch(_atomic_add_gm, threads=32, args=(dst, old_values, value)) | 270 | pl.simt.launch(_atomic_add_gm, threads=32, args=(dst, old_values, value)) |
| 271 | 271 | ||
| 272 | 272 | ||
| 273 | -@pl.kernel | 273 | +@pl.jit |
| 274 | def _atomic_add_discard_codegen_kernel(value: pl.DT_INT32): | 274 | def _atomic_add_discard_codegen_kernel(value: pl.DT_INT32): |
| 275 | tile_type = pl.TileType(shape=[1, 1], dtype=pl.DT_INT32, target_memory=pl.MemorySpace.Vec) | 275 | tile_type = pl.TileType(shape=[1, 1], dtype=pl.DT_INT32, target_memory=pl.MemorySpace.Vec) |
| 276 | dst = pl.make_tile(tile_type, addr=0x0000, size=4) | 276 | dst = pl.make_tile(tile_type, addr=0x0000, size=4) |
| @@ -278,7 +278,7 @@ def _atomic_add_discard_codegen_kernel(value: pl.DT_INT32): | |||
| 278 | pl.simt.launch(_atomic_add_discard, threads=1, args=(dst, value)) | 278 | pl.simt.launch(_atomic_add_discard, threads=1, args=(dst, value)) |
| 279 | 279 | ||
| 280 | 280 | ||
| 281 | -@pl.kernel | 281 | +@pl.jit |
| 282 | def _atomic_half_discard_codegen_kernel(gm: pl.Tensor[[1, 3], pl.DT_BF16]): | 282 | def _atomic_half_discard_codegen_kernel(gm: pl.Tensor[[1, 3], pl.DT_BF16]): |
| 283 | tile_type = pl.TileType(shape=[1, 3], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 283 | tile_type = pl.TileType(shape=[1, 3], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 284 | ub = pl.make_tile(tile_type, addr=0x0000, size=32) | 284 | ub = pl.make_tile(tile_type, addr=0x0000, size=32) |
| @@ -286,7 +286,7 @@ def _atomic_half_discard_codegen_kernel(gm: pl.Tensor[[1, 3], pl.DT_BF16]): | |||
| 286 | pl.simt.launch(_atomic_half_discard, threads=1, args=(ub, gm)) | 286 | pl.simt.launch(_atomic_half_discard, threads=1, args=(ub, gm)) |
| 287 | 287 | ||
| 288 | 288 | ||
| 289 | -@pl.kernel | 289 | +@pl.jit |
| 290 | def _atomic_rmw_discard_codegen_kernel( | 290 | def _atomic_rmw_discard_codegen_kernel( |
| 291 | compare: pl.DT_INT32, | 291 | compare: pl.DT_INT32, |
| 292 | replacement: pl.DT_INT32, | 292 | replacement: pl.DT_INT32, |
| @@ -493,7 +493,7 @@ def _simt_inplace_add(data, delta: pl.DT_FP32): | |||
| 493 | data[0, tid] = data[0, tid] + delta | 493 | data[0, tid] = data[0, tid] + delta |
| 494 | 494 | ||
| 495 | 495 | ||
| 496 | -@pl.kernel | 496 | +@pl.jit |
| 497 | def _simt_auto_mutex_kernel( | 497 | def _simt_auto_mutex_kernel( |
| 498 | x: pl.Tensor[[1, 256], pl.DT_FP32], | 498 | x: pl.Tensor[[1, 256], pl.DT_FP32], |
| 499 | out: pl.Tensor[[1, 256], pl.DT_FP32], | 499 | out: pl.Tensor[[1, 256], pl.DT_FP32], |
| @@ -33,10 +33,10 @@ DIM = 16 | |||
| 33 | TILE = 64 | 33 | TILE = 64 |
| 34 | 34 | ||
| 35 | 35 | ||
| 36 | -def _compile_to_cce(kernel_def) -> str: | 36 | +def _compile_to_cce(kernel) -> str: |
| 37 | from pypto_pro.runtime.jit import _assemble_cv_source, _parse_and_codegen_targets | 37 | from pypto_pro.runtime.jit import _assemble_cv_source, _parse_and_codegen_targets |
| 38 | 38 | ||
| 39 | - cube, vector = _parse_and_codegen_targets(kernel_def, "a5", "") | 39 | + cube, vector = _parse_and_codegen_targets(kernel.to_kernel_def(), "a5", "") |
| 40 | return _assemble_cv_source(cube, vector).content | 40 | return _assemble_cv_source(cube, vector).content |
| 41 | 41 | ||
| 42 | 42 | ||
| @@ -57,7 +57,7 @@ def _instance_strides(source: str, name: str) -> str: | |||
| 57 | return match.group(1) | 57 | return match.group(1) |
| 58 | 58 | ||
| 59 | 59 | ||
| 60 | -@pl.kernel | 60 | +@pl.jit |
| 61 | def _mixed_row_axis_kernel( | 61 | def _mixed_row_axis_kernel( |
| 62 | x: pl.Tensor[[DIM, DIM, DIM, DIM], pl.DT_FP16], | 62 | x: pl.Tensor[[DIM, DIM, DIM, DIM], pl.DT_FP16], |
| 63 | inner: pl.Tensor[[DIM, DIM, DIM, DIM], pl.DT_FP16], | 63 | inner: pl.Tensor[[DIM, DIM, DIM, DIM], pl.DT_FP16], |
| @@ -75,7 +75,7 @@ def _mixed_row_axis_kernel( | |||
| 75 | pl.store(middle, middle_tile, [0, 0, 0, 0], order=[2, 3]) | 75 | pl.store(middle, middle_tile, [0, 0, 0, 0], order=[2, 3]) |
| 76 | 76 | ||
| 77 | 77 | ||
| 78 | -@pl.kernel | 78 | +@pl.jit |
| 79 | def _single_order_kernel( | 79 | def _single_order_kernel( |
| 80 | x: pl.Tensor[[DIM, DIM, DIM, DIM], pl.DT_FP16], | 80 | x: pl.Tensor[[DIM, DIM, DIM, DIM], pl.DT_FP16], |
| 81 | out: pl.Tensor[[DIM, DIM, DIM, DIM], pl.DT_FP16], | 81 | out: pl.Tensor[[DIM, DIM, DIM, DIM], pl.DT_FP16], |
| @@ -89,7 +89,7 @@ def _single_order_kernel( | |||
| 89 | pl.store(out, tile, [0, 0, 0, 0], order=[2, 3]) | 89 | pl.store(out, tile, [0, 0, 0, 0], order=[2, 3]) |
| 90 | 90 | ||
| 91 | 91 | ||
| 92 | -@pl.kernel | 92 | +@pl.jit |
| 93 | def _transposed_only_kernel(x: pl.Tensor[[TILE, TILE], pl.DT_FP16], out: pl.Tensor[[TILE, TILE], pl.DT_FP32]): | 93 | def _transposed_only_kernel(x: pl.Tensor[[TILE, TILE], pl.DT_FP16], out: pl.Tensor[[TILE, TILE], pl.DT_FP32]): |
| 94 | l1 = pl.make_tile_group( | 94 | l1 = pl.make_tile_group( |
| 95 | type=pl.TileType(shape=[TILE, TILE], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat), | 95 | type=pl.TileType(shape=[TILE, TILE], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat), |
| @@ -21,10 +21,10 @@ import re | |||
| 21 | import pypto_pro.language as pl | 21 | import pypto_pro.language as pl |
| 22 | 22 | ||
| 23 | 23 | ||
| 24 | -def _compile_to_cce(kernel_def) -> str: | 24 | +def _compile_to_cce(kernel) -> str: |
| 25 | from pypto_pro.runtime.jit import _assemble_cv_source, _parse_and_codegen_targets | 25 | from pypto_pro.runtime.jit import _assemble_cv_source, _parse_and_codegen_targets |
| 26 | 26 | ||
| 27 | - cube, vector = _parse_and_codegen_targets(kernel_def, "a5", "") | 27 | + cube, vector = _parse_and_codegen_targets(kernel.to_kernel_def(), "a5", "") |
| 28 | return _assemble_cv_source(cube, vector).content | 28 | return _assemble_cv_source(cube, vector).content |
| 29 | 29 | ||
| 30 | 30 | ||
| @@ -55,7 +55,7 @@ def _assert_materialized_jump_guard(cpp: str, condition_fragment: str, jump: str | |||
| 55 | # --------------------------------------------------------------------------- | 55 | # --------------------------------------------------------------------------- |
| 56 | 56 | ||
| 57 | 57 | ||
| 58 | -@pl.kernel | 58 | +@pl.jit |
| 59 | def _while_basic_kernel( | 59 | def _while_basic_kernel( |
| 60 | a: pl.Tensor[[64, 128], pl.DT_FP16], | 60 | a: pl.Tensor[[64, 128], pl.DT_FP16], |
| 61 | b: pl.Tensor[[64, 128], pl.DT_FP16], | 61 | b: pl.Tensor[[64, 128], pl.DT_FP16], |
| @@ -74,7 +74,7 @@ def _while_basic_kernel( | |||
| 74 | i = i + 1 | 74 | i = i + 1 |
| 75 | 75 | ||
| 76 | 76 | ||
| 77 | -@pl.kernel | 77 | +@pl.jit |
| 78 | def _while_continue_kernel( | 78 | def _while_continue_kernel( |
| 79 | a: pl.Tensor[[64, 128], pl.DT_FP16], | 79 | a: pl.Tensor[[64, 128], pl.DT_FP16], |
| 80 | b: pl.Tensor[[64, 128], pl.DT_FP16], | 80 | b: pl.Tensor[[64, 128], pl.DT_FP16], |
| @@ -96,7 +96,7 @@ def _while_continue_kernel( | |||
| 96 | i = i + 1 | 96 | i = i + 1 |
| 97 | 97 | ||
| 98 | 98 | ||
| 99 | -@pl.kernel | 99 | +@pl.jit |
| 100 | def _while_break_kernel( | 100 | def _while_break_kernel( |
| 101 | a: pl.Tensor[[64, 128], pl.DT_FP16], | 101 | a: pl.Tensor[[64, 128], pl.DT_FP16], |
| 102 | b: pl.Tensor[[64, 128], pl.DT_FP16], | 102 | b: pl.Tensor[[64, 128], pl.DT_FP16], |
| @@ -117,7 +117,7 @@ def _while_break_kernel( | |||
| 117 | i = i + 1 | 117 | i = i + 1 |
| 118 | 118 | ||
| 119 | 119 | ||
| 120 | -@pl.kernel | 120 | +@pl.jit |
| 121 | def _while_break_carry_kernel( | 121 | def _while_break_carry_kernel( |
| 122 | a: pl.Tensor[[64, 128], pl.DT_FP16], | 122 | a: pl.Tensor[[64, 128], pl.DT_FP16], |
| 123 | b: pl.Tensor[[64, 128], pl.DT_FP16], | 123 | b: pl.Tensor[[64, 128], pl.DT_FP16], |
| @@ -137,7 +137,7 @@ def _while_break_carry_kernel( | |||
| 137 | acc = acc + i | 137 | acc = acc + i |
| 138 | 138 | ||
| 139 | 139 | ||
| 140 | -@pl.kernel | 140 | +@pl.jit |
| 141 | def _while_accumulate_kernel( | 141 | def _while_accumulate_kernel( |
| 142 | a: pl.Tensor[[64, 128], pl.DT_FP16], | 142 | a: pl.Tensor[[64, 128], pl.DT_FP16], |
| 143 | b: pl.Tensor[[64, 128], pl.DT_FP16], | 143 | b: pl.Tensor[[64, 128], pl.DT_FP16], |
| @@ -158,7 +158,7 @@ def _while_accumulate_kernel( | |||
| 158 | i = i + 1 | 158 | i = i + 1 |
| 159 | 159 | ||
| 160 | 160 | ||
| 161 | -@pl.kernel(auto_mutex=True) | 161 | +@pl.jit(auto_mutex=True) |
| 162 | def _while_getval_condition_kernel( | 162 | def _while_getval_condition_kernel( |
| 163 | a: pl.Tensor[[64, 128], pl.DT_FP16], | 163 | a: pl.Tensor[[64, 128], pl.DT_FP16], |
| 164 | ): | 164 | ): |
| @@ -174,7 +174,7 @@ def _while_getval_condition_kernel( | |||
| 174 | break | 174 | break |
| 175 | 175 | ||
| 176 | 176 | ||
| 177 | -@pl.kernel | 177 | +@pl.jit |
| 178 | def _for_continue_kernel( | 178 | def _for_continue_kernel( |
| 179 | a: pl.Tensor[[64, 128], pl.DT_FP16], | 179 | a: pl.Tensor[[64, 128], pl.DT_FP16], |
| 180 | b: pl.Tensor[[64, 128], pl.DT_FP16], | 180 | b: pl.Tensor[[64, 128], pl.DT_FP16], |
| @@ -186,7 +186,7 @@ def _for_continue_kernel( | |||
| 186 | acc = acc + i | 186 | acc = acc + i |
| 187 | 187 | ||
| 188 | 188 | ||
| 189 | -@pl.kernel | 189 | +@pl.jit |
| 190 | def _for_break_kernel( | 190 | def _for_break_kernel( |
| 191 | a: pl.Tensor[[64, 128], pl.DT_FP16], | 191 | a: pl.Tensor[[64, 128], pl.DT_FP16], |
| 192 | b: pl.Tensor[[64, 128], pl.DT_FP16], | 192 | b: pl.Tensor[[64, 128], pl.DT_FP16], |
| @@ -13,14 +13,14 @@ | |||
| 13 | import pypto_pro.language as pl | 13 | import pypto_pro.language as pl |
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | -def _compile_to_cce(kernel_def) -> str: | 16 | +def _compile_to_cce(kernel) -> str: |
| 17 | from pypto_pro.runtime.jit import _assemble_cv_source, _parse_and_codegen_targets | 17 | from pypto_pro.runtime.jit import _assemble_cv_source, _parse_and_codegen_targets |
| 18 | 18 | ||
| 19 | - cube, vector = _parse_and_codegen_targets(kernel_def, "a5", "") | 19 | + cube, vector = _parse_and_codegen_targets(kernel.to_kernel_def(), "a5", "") |
| 20 | return _assemble_cv_source(cube, vector).content | 20 | return _assemble_cv_source(cube, vector).content |
| 21 | 21 | ||
| 22 | 22 | ||
| 23 | -@pl.kernel | 23 | +@pl.jit |
| 24 | def _make_ptr_cce_kernel(p: pl.Ptr[pl.DT_UINT8], out: pl.Tensor[[64, 128], pl.DT_FP16]): | 24 | def _make_ptr_cce_kernel(p: pl.Ptr[pl.DT_UINT8], out: pl.Tensor[[64, 128], pl.DT_FP16]): |
| 25 | fp16_ptr = pl.make_ptr(p, dtype=pl.DT_FP16) | 25 | fp16_ptr = pl.make_ptr(p, dtype=pl.DT_FP16) |
| 26 | ws = pl.make_tensor(fp16_ptr, [64, 128], [128, 1]) | 26 | ws = pl.make_tensor(fp16_ptr, [64, 128], [128, 1]) |
| @@ -34,7 +34,7 @@ def _make_ptr_cce_kernel(p: pl.Ptr[pl.DT_UINT8], out: pl.Tensor[[64, 128], pl.DT | |||
| 34 | pl.store(out, tile, [0, 0]) | 34 | pl.store(out, tile, [0, 0]) |
| 35 | 35 | ||
| 36 | 36 | ||
| 37 | -@pl.kernel | 37 | +@pl.jit |
| 38 | def _retensor_cce_kernel(src: pl.Tensor[[64, 128], pl.DT_FP16], out: pl.Tensor[[64, 128], pl.DT_FP16]): | 38 | def _retensor_cce_kernel(src: pl.Tensor[[64, 128], pl.DT_FP16], out: pl.Tensor[[64, 128], pl.DT_FP16]): |
| 39 | reshaped = pl.make_tensor(src, [64, 128], [128, 1]) | 39 | reshaped = pl.make_tensor(src, [64, 128], [128, 1]) |
| 40 | tile = pl.make_tile( | 40 | tile = pl.make_tile( |
| @@ -47,7 +47,7 @@ def _retensor_cce_kernel(src: pl.Tensor[[64, 128], pl.DT_FP16], out: pl.Tensor[[ | |||
| 47 | pl.store(out, tile, [0, 0]) | 47 | pl.store(out, tile, [0, 0]) |
| 48 | 48 | ||
| 49 | 49 | ||
| 50 | -@pl.kernel | 50 | +@pl.jit |
| 51 | def _retensor_cce_dtype_kernel(src: pl.Tensor[[64, 128], pl.DT_FP16], out: pl.Tensor[[64, 256], pl.DT_UINT8]): | 51 | def _retensor_cce_dtype_kernel(src: pl.Tensor[[64, 128], pl.DT_FP16], out: pl.Tensor[[64, 256], pl.DT_UINT8]): |
| 52 | as_u8 = pl.make_tensor(src, [64, 256], [256, 1], dtype=pl.DT_UINT8) | 52 | as_u8 = pl.make_tensor(src, [64, 256], [256, 1], dtype=pl.DT_UINT8) |
| 53 | tile = pl.make_tile( | 53 | tile = pl.make_tile( |
| @@ -10,7 +10,7 @@ | |||
| 10 | # ----------------------------------------------------------------------------------------------------------- | 10 | # ----------------------------------------------------------------------------------------------------------- |
| 11 | """Unit tests for compact mode propagation through Block tile creation.""" | 11 | """Unit tests for compact mode propagation through Block tile creation.""" |
| 12 | 12 | ||
| 13 | -from pypto_pro import DataType | 13 | +from pypto_pro import DataType, ir |
| 14 | from pypto_pro.ir.op.block_ops import make_tile_expr | 14 | from pypto_pro.ir.op.block_ops import make_tile_expr |
| 15 | import pypto_pro.language as pl | 15 | import pypto_pro.language as pl |
| 16 | import pytest | 16 | import pytest |
| @@ -78,7 +78,7 @@ def test_compact_stored_in_make_tile_type(compact, expected): | |||
| 78 | ) | 78 | ) |
| 79 | def test_compact_propagated_by_parser(compact, expected): | 79 | def test_compact_propagated_by_parser(compact, expected): |
| 80 | if compact is None: | 80 | if compact is None: |
| 81 | - @pl.function | 81 | + @pl.jit(auto_mutex=False) |
| 82 | def func(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): | 82 | def func(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): |
| 83 | tile_type = pl.TileType( | 83 | tile_type = pl.TileType( |
| 84 | shape=[128, 128], | 84 | shape=[128, 128], |
| @@ -87,8 +87,11 @@ def test_compact_propagated_by_parser(compact, expected): | |||
| 87 | layout=pl.NZ, | 87 | layout=pl.NZ, |
| 88 | ) | 88 | ) |
| 89 | tile_a = pl.make_tile(tile_type, addr=0x00000, size=32768) # noqa: F841 | 89 | tile_a = pl.make_tile(tile_type, addr=0x00000, size=32768) # noqa: F841 |
| 90 | + | ||
| 91 | + func_program, _ = func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 92 | + func = func_program.get_function(func.__name__) | ||
| 90 | else: | 93 | else: |
| 91 | - @pl.function | 94 | + @pl.jit(auto_mutex=False) |
| 92 | def func(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): | 95 | def func(a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16]): |
| 93 | tile_type = pl.TileType( | 96 | tile_type = pl.TileType( |
| 94 | shape=[128, 128], | 97 | shape=[128, 128], |
| @@ -99,6 +102,9 @@ def test_compact_propagated_by_parser(compact, expected): | |||
| 99 | ) | 102 | ) |
| 100 | tile_a = pl.make_tile(tile_type, addr=0x00000, size=32768) # noqa: F841 | 103 | tile_a = pl.make_tile(tile_type, addr=0x00000, size=32768) # noqa: F841 |
| 101 | 104 | ||
| 105 | + func_program, _ = func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 106 | + func = func_program.get_function(func.__name__) | ||
| 107 | + | ||
| 102 | stmt = func.body.stmts[0] if hasattr(func.body, "stmts") else func.body | 108 | stmt = func.body.stmts[0] if hasattr(func.body, "stmts") else func.body |
| 103 | tile_type = stmt.var.type | 109 | tile_type = stmt.var.type |
| 104 | assert isinstance(tile_type.hardware_info, _ir.HardwareInfo) | 110 | assert isinstance(tile_type.hardware_info, _ir.HardwareInfo) |
| @@ -16,203 +16,200 @@ from pypto_pro.language.parser.diagnostics import ParserSyntaxError, ParserTypeE | |||
| 16 | import pytest | 16 | import pytest |
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | -@pl.program | 19 | +@pl.jit(auto_mutex=False) |
| 20 | -class AddKernelDynamic: | 20 | +def add_kernel_dynamic( |
| 21 | - """Add kernel with dynamic shape tensor parameters.""" | 21 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], |
| 22 | + b: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 23 | + output: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 24 | +): | ||
| 25 | + """Adds two tensors element-wise with dynamic shapes: result = a + b""" | ||
| 26 | + tile_type = pl.TileType(shape=[128, 128], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 27 | + a_tile = pl.make_tile(tile_type, addr=0x0000, size=65536) | ||
| 28 | + b_tile = pl.make_tile(tile_type, addr=0x10000, size=65536) | ||
| 29 | + result = pl.make_tile(tile_type, addr=0x20000, size=65536) | ||
| 30 | + pl.load(a_tile, a, [0, 0]) | ||
| 31 | + pl.load(b_tile, b, [0, 0]) | ||
| 32 | + pl.add(result, a_tile, b_tile) | ||
| 33 | + out = pl.store(output, result, [0, 0]) | ||
| 34 | + _test_result = out | ||
| 22 | 35 | ||
| 23 | - @pl.function(type=pl.FunctionType.InCore) | 36 | + |
| 24 | - def add_kernel( | 37 | +add_kernel_dynamic_program, _ = add_kernel_dynamic.to_kernel_def().parse_target_program(ir.SectionKind.Vector) |
| 25 | - self, | 38 | +add_kernel_dynamic = add_kernel_dynamic_program.get_function(add_kernel_dynamic.__name__) |
| 26 | - a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | 39 | + |
| 27 | - b: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | 40 | + |
| 28 | - output: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | 41 | +@pl.jit(auto_mutex=False) |
| 29 | - ): | 42 | +def add_kernel_valid_shape( |
| 30 | - """Adds two tensors element-wise with dynamic shapes: result = a + b""" | 43 | + a: pl.Tensor[[128, 128], pl.DT_FP32], |
| 31 | - tile_type = pl.TileType(shape=[128, 128], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | 44 | + b: pl.Tensor[[128, 128], pl.DT_FP32], |
| 32 | - a_tile = pl.make_tile(tile_type, addr=0x0000, size=65536) | 45 | + output: pl.Tensor[[128, 128], pl.DT_FP32], |
| 33 | - b_tile = pl.make_tile(tile_type, addr=0x10000, size=65536) | 46 | + m_var: pl.DT_INT64, |
| 34 | - result = pl.make_tile(tile_type, addr=0x20000, size=65536) | 47 | + n_var: pl.DT_INT64, |
| 35 | - pl.load(a_tile, a, [0, 0]) | 48 | +): |
| 36 | - pl.load(b_tile, b, [0, 0]) | 49 | + """Loads 128x128 tiles but marks only [M, N] as valid: result = a + b""" |
| 50 | + tile_type = pl.TileType( | ||
| 51 | + shape=[128, 128], | ||
| 52 | + dtype=pl.DT_FP32, | ||
| 53 | + target_memory=pl.MemorySpace.Vec, | ||
| 54 | + valid_shape=[-1, -1], | ||
| 55 | + ) | ||
| 56 | + a_tile = pl.make_tile(tile_type, addr=0x0000, size=65536) | ||
| 57 | + b_tile = pl.make_tile(tile_type, addr=0x10000, size=65536) | ||
| 58 | + result = pl.make_tile(tile_type, addr=0x20000, size=65536) | ||
| 59 | + pl.set_validshape(a_tile, [m_var, n_var]) | ||
| 60 | + pl.set_validshape(b_tile, [m_var, n_var]) | ||
| 61 | + pl.set_validshape(result, [m_var, n_var]) | ||
| 62 | + pl.load(a_tile, a, [0, 0]) | ||
| 63 | + pl.load(b_tile, b, [0, 0]) | ||
| 64 | + pl.add(result, a_tile, b_tile) | ||
| 65 | + pl.set_validshape(a_tile, [m_var, n_var]) | ||
| 66 | + pl.set_validshape(b_tile, [m_var, n_var]) | ||
| 67 | + pl.set_validshape(result, [m_var, n_var]) | ||
| 68 | + out = pl.store(output, result, [0, 0]) | ||
| 69 | + _test_result = out | ||
| 70 | + | ||
| 71 | + | ||
| 72 | +add_kernel_valid_shape_program, _ = add_kernel_valid_shape.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 73 | +add_kernel_valid_shape = add_kernel_valid_shape_program.get_function(add_kernel_valid_shape.__name__) | ||
| 74 | + | ||
| 75 | + | ||
| 76 | +@pl.jit(auto_mutex=False) | ||
| 77 | +def add_kernel_shape_subscript( | ||
| 78 | + a: pl.Tensor[[pl.DYNAMIC, 128], pl.DT_FP32], | ||
| 79 | + b: pl.Tensor[[pl.DYNAMIC, 128], pl.DT_FP32], | ||
| 80 | + output: pl.Tensor[[pl.DYNAMIC, 128], pl.DT_FP32], | ||
| 81 | +): | ||
| 82 | + """Uses the canonical dynamic dimension stored in a TensorType.""" | ||
| 83 | + m_var = a.shape[0] | ||
| 84 | + tile_type = pl.TileType(shape=[2, 128], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 85 | + a_tile = pl.make_tile(tile_type, addr=0x0000, size=1024) | ||
| 86 | + b_tile = pl.make_tile(tile_type, addr=0x0400, size=1024) | ||
| 87 | + result = pl.make_tile(tile_type, addr=0x0800, size=1024) | ||
| 88 | + for i in pl.range(0, m_var, 2): | ||
| 89 | + offset_1 = i * 2 | ||
| 90 | + pl.load(a_tile, a, [offset_1, 0]) | ||
| 91 | + pl.load(b_tile, b, [offset_1, 0]) | ||
| 37 | pl.add(result, a_tile, b_tile) | 92 | pl.add(result, a_tile, b_tile) |
| 38 | - out = pl.store(output, result, [0, 0]) | 93 | + out = pl.store(output, result, [offset_1, 0]) |
| 39 | - return out | 94 | + _test_result = out |
| 40 | 95 | ||
| 41 | 96 | ||
| 42 | -@pl.program | 97 | +add_kernel_shape_subscript_program, _ = add_kernel_shape_subscript.to_kernel_def().parse_target_program( |
| 43 | -class AddKernelValidShape: | 98 | + ir.SectionKind.Vector |
| 44 | - """Add kernel with static tensors but dynamic valid_shapes passed at runtime.""" | 99 | +) |
| 45 | - | 100 | +add_kernel_shape_subscript = add_kernel_shape_subscript_program.get_function(add_kernel_shape_subscript.__name__) |
| 46 | - @pl.function(type=pl.FunctionType.InCore) | ||
| 47 | - def add_kernel( | ||
| 48 | - self, | ||
| 49 | - a: pl.Tensor[[128, 128], pl.DT_FP32], | ||
| 50 | - b: pl.Tensor[[128, 128], pl.DT_FP32], | ||
| 51 | - output: pl.Tensor[[128, 128], pl.DT_FP32], | ||
| 52 | - m_var: pl.DT_INT64, | ||
| 53 | - n_var: pl.DT_INT64, | ||
| 54 | - ) -> pl.Tensor[[128, 128], pl.DT_FP32]: | ||
| 55 | - """Loads 128x128 tiles but marks only [M, N] as valid: result = a + b""" | ||
| 56 | - tile_type = pl.TileType( | ||
| 57 | - shape=[128, 128], | ||
| 58 | - dtype=pl.DT_FP32, | ||
| 59 | - target_memory=pl.MemorySpace.Vec, | ||
| 60 | - valid_shape=[-1, -1], | ||
| 61 | - ) | ||
| 62 | - a_tile = pl.make_tile(tile_type, addr=0x0000, size=65536) | ||
| 63 | - b_tile = pl.make_tile(tile_type, addr=0x10000, size=65536) | ||
| 64 | - result = pl.make_tile(tile_type, addr=0x20000, size=65536) | ||
| 65 | - pl.set_validshape(a_tile, [m_var, n_var]) | ||
| 66 | - pl.set_validshape(b_tile, [m_var, n_var]) | ||
| 67 | - pl.set_validshape(result, [m_var, n_var]) | ||
| 68 | - pl.load(a_tile, a, [0, 0]) | ||
| 69 | - pl.load(b_tile, b, [0, 0]) | ||
| 70 | - pl.add(result, a_tile, b_tile) | ||
| 71 | - pl.set_validshape(a_tile, [m_var, n_var]) | ||
| 72 | - pl.set_validshape(b_tile, [m_var, n_var]) | ||
| 73 | - pl.set_validshape(result, [m_var, n_var]) | ||
| 74 | - out = pl.store(output, result, [0, 0]) | ||
| 75 | - return out | ||
| 76 | 101 | ||
| 77 | 102 | ||
| 78 | -@pl.program | 103 | +@pl.jit(auto_mutex=False) |
| 79 | -class AddKernelShapeSubscript: | 104 | +def static_shape_negative_subscript( |
| 80 | - """Add kernel using tensor.shape[i] for a dynamic loop bound.""" | 105 | + a: pl.Tensor[[2, 128], pl.DT_FP32], |
| 81 | - | 106 | + output: pl.Tensor[[2, 128], pl.DT_FP32], |
| 82 | - @pl.function(type=pl.FunctionType.InCore) | 107 | +): |
| 83 | - def add_kernel( | 108 | + n = a.shape[-1] |
| 84 | - self, | 109 | + for _ in pl.range(0, n, 1): |
| 85 | - a: pl.Tensor[[pl.DYNAMIC, 128], pl.DT_FP32], | 110 | + out = output |
| 86 | - b: pl.Tensor[[pl.DYNAMIC, 128], pl.DT_FP32], | 111 | + _test_result = out |
| 87 | - output: pl.Tensor[[pl.DYNAMIC, 128], pl.DT_FP32], | ||
| 88 | - ): | ||
| 89 | - """Uses the canonical dynamic dimension stored in a TensorType.""" | ||
| 90 | - m_var = a.shape[0] | ||
| 91 | - tile_type = pl.TileType(shape=[2, 128], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 92 | - a_tile = pl.make_tile(tile_type, addr=0x0000, size=1024) | ||
| 93 | - b_tile = pl.make_tile(tile_type, addr=0x0400, size=1024) | ||
| 94 | - result = pl.make_tile(tile_type, addr=0x0800, size=1024) | ||
| 95 | - for i in pl.range(0, m_var, 2): | ||
| 96 | - offset_1 = i * 2 | ||
| 97 | - pl.load(a_tile, a, [offset_1, 0]) | ||
| 98 | - pl.load(b_tile, b, [offset_1, 0]) | ||
| 99 | - pl.add(result, a_tile, b_tile) | ||
| 100 | - out = pl.store(output, result, [offset_1, 0]) | ||
| 101 | - return out | ||
| 102 | 112 | ||
| 103 | 113 | ||
| 104 | -@pl.program | 114 | +static_shape_negative_subscript_program, _ = static_shape_negative_subscript.to_kernel_def().parse_target_program( |
| 105 | -class StaticShapeNegativeSubscript: | 115 | + ir.SectionKind.Vector |
| 106 | - """Exercise Python-style negative indexing for a fixed tensor shape.""" | 116 | +) |
| 107 | - | 117 | +static_shape_negative_subscript = static_shape_negative_subscript_program.get_function( |
| 108 | - @pl.function(type=pl.FunctionType.InCore) | 118 | + static_shape_negative_subscript.__name__ |
| 109 | - def shape_kernel( | 119 | +) |
| 110 | - self, | ||
| 111 | - a: pl.Tensor[[2, 128], pl.DT_FP32], | ||
| 112 | - output: pl.Tensor[[2, 128], pl.DT_FP32], | ||
| 113 | - ) -> pl.Tensor[[2, 128], pl.DT_FP32]: | ||
| 114 | - n = a.shape[-1] | ||
| 115 | - for _ in pl.range(0, n, 1): | ||
| 116 | - out = output | ||
| 117 | - return out | ||
| 118 | 120 | ||
| 119 | 121 | ||
| 120 | -@pl.program | 122 | +@pl.jit(auto_mutex=False) |
| 121 | -class DynamicShapeTupleUnpack: | 123 | +def dynamic_shape_tuple_unpack( |
| 122 | - """Exercise complete tensor.shape tuple access and repeated discard targets.""" | 124 | + a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC, 128, 64], pl.DT_FP32], |
| 123 | - | 125 | +): |
| 124 | - @pl.function(type=pl.FunctionType.InCore) | 126 | + m, n, _, _ = a.shape |
| 125 | - def shape_kernel( | 127 | + for _ in pl.range(0, m, 1): |
| 126 | - self, | 128 | + out = a |
| 127 | - a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC, 128, 64], pl.DT_FP32], | 129 | + _test_result = out |
| 128 | - ): | ||
| 129 | - m, n, _, _ = a.shape | ||
| 130 | - for _ in pl.range(0, m, 1): | ||
| 131 | - out = a | ||
| 132 | - return out | ||
| 133 | 130 | ||
| 134 | 131 | ||
| 135 | -@pl.program | 132 | +dynamic_shape_tuple_unpack_program, _ = dynamic_shape_tuple_unpack.to_kernel_def().parse_target_program( |
| 136 | -class CompileTimeShapeAxis: | 133 | + ir.SectionKind.Vector |
| 137 | - """Exercise a compile-time integer expression as a shape index.""" | 134 | +) |
| 135 | +dynamic_shape_tuple_unpack = dynamic_shape_tuple_unpack_program.get_function(dynamic_shape_tuple_unpack.__name__) | ||
| 138 | 136 | ||
| 139 | - @pl.function(type=pl.FunctionType.InCore) | 137 | + |
| 140 | - def shape_kernel( | 138 | +@pl.jit(auto_mutex=False) |
| 141 | - self, | 139 | +def compile_time_shape_axis( |
| 142 | - a: pl.Tensor[[2, 128], pl.DT_FP32], | 140 | + a: pl.Tensor[[2, 128], pl.DT_FP32], |
| 143 | - output: pl.Tensor[[2, 128], pl.DT_FP32], | 141 | + output: pl.Tensor[[2, 128], pl.DT_FP32], |
| 144 | - ) -> pl.Tensor[[2, 128], pl.DT_FP32]: | 142 | +): |
| 145 | - n = a.shape[0] | 143 | + n = a.shape[0] |
| 146 | - for _ in pl.range(0, n, 1): | 144 | + for _ in pl.range(0, n, 1): |
| 147 | - out = output | 145 | + out = output |
| 148 | - return out | 146 | + _test_result = out |
| 147 | + | ||
| 148 | + | ||
| 149 | +compile_time_shape_axis_program, _ = compile_time_shape_axis.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 150 | +compile_time_shape_axis = compile_time_shape_axis_program.get_function(compile_time_shape_axis.__name__) | ||
| 149 | 151 | ||
| 150 | 152 | ||
| 151 | def test_dynamic_shape_subscript_uses_parameter_shape_var(): | 153 | def test_dynamic_shape_subscript_uses_parameter_shape_var(): |
| 152 | - func = AddKernelShapeSubscript.functions["add_kernel"] | 154 | + dynamic_dim = add_kernel_shape_subscript.params[0].type.shape[0] |
| 153 | - dynamic_dim = func.params[0].type.shape[0] | ||
| 154 | 155 | ||
| 155 | assert isinstance(dynamic_dim, ir.Var) | 156 | assert isinstance(dynamic_dim, ir.Var) |
| 156 | assert dynamic_dim.name == "__pypto_dyn_a_0" | 157 | assert dynamic_dim.name == "__pypto_dyn_a_0" |
| 157 | assert dynamic_dim.type.dtype == DataType.INDEX | 158 | assert dynamic_dim.type.dtype == DataType.INDEX |
| 158 | - assert dynamic_dim.name in ir.python_print(AddKernelShapeSubscript) | 159 | + assert dynamic_dim.name in ir.python_print(add_kernel_shape_subscript) |
| 159 | 160 | ||
| 160 | 161 | ||
| 161 | def test_negative_shape_subscript_uses_fixed_dimension(): | 162 | def test_negative_shape_subscript_uses_fixed_dimension(): |
| 162 | - func = StaticShapeNegativeSubscript.functions["shape_kernel"] | 163 | + assert isinstance(static_shape_negative_subscript.params[0].type.shape[-1], ir.ConstInt) |
| 163 | - | 164 | + assert static_shape_negative_subscript.params[0].type.shape[-1].value == 128 |
| 164 | - assert isinstance(func.params[0].type.shape[-1], ir.ConstInt) | ||
| 165 | - assert func.params[0].type.shape[-1].value == 128 | ||
| 166 | 165 | ||
| 167 | 166 | ||
| 168 | def test_shape_tuple_unpack_uses_canonical_tensor_dimensions(): | 167 | def test_shape_tuple_unpack_uses_canonical_tensor_dimensions(): |
| 169 | - func = DynamicShapeTupleUnpack.functions["shape_kernel"] | 168 | + assert isinstance(dynamic_shape_tuple_unpack.params[0].type.shape[0], ir.Var) |
| 170 | - | 169 | + assert dynamic_shape_tuple_unpack.params[0].type.shape[0].name == "__pypto_dyn_a_0" |
| 171 | - assert isinstance(func.params[0].type.shape[0], ir.Var) | ||
| 172 | - assert func.params[0].type.shape[0].name == "__pypto_dyn_a_0" | ||
| 173 | 170 | ||
| 174 | 171 | ||
| 175 | def test_shape_tuple_unpack_rejects_arity_mismatch(): | 172 | def test_shape_tuple_unpack_rejects_arity_mismatch(): |
| 176 | - code = """ | ||
| 177 | -@pl.function | ||
| 178 | -def shape_unpack(x: pl.Tensor[[2, 128], pl.DT_FP32]) -> pl.Tensor[[2, 128], pl.DT_FP32]: | ||
| 179 | - m, n, k = x.shape | ||
| 180 | - return x | ||
| 181 | -""" | ||
| 182 | with pytest.raises(ParserTypeError, match="unpack"): | 173 | with pytest.raises(ParserTypeError, match="unpack"): |
| 183 | - pl.parse(code) | 174 | + |
| 175 | + @pl.jit(auto_mutex=False) | ||
| 176 | + def shape_unpack(x: pl.Tensor[[2, 128], pl.DT_FP32]): | ||
| 177 | + m, n, k = x.shape | ||
| 178 | + _test_result = x | ||
| 179 | + | ||
| 180 | + shape_unpack.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 184 | 181 | ||
| 185 | 182 | ||
| 186 | -@pytest.mark.parametrize("index", ["True", "0.0", "2", "-3"]) | 183 | +@pytest.mark.parametrize("index", [True, 0.0, 2, -3]) |
| 187 | def test_shape_subscript_rejects_invalid_constant_index(index): | 184 | def test_shape_subscript_rejects_invalid_constant_index(index): |
| 188 | - code = f""" | ||
| 189 | -@pl.function | ||
| 190 | -def shape_index(x: pl.Tensor[[2, 128], pl.DT_FP32]) -> pl.Tensor[[2, 128], pl.DT_FP32]: | ||
| 191 | - n = x.shape[{index}] | ||
| 192 | - return x | ||
| 193 | -""" | ||
| 194 | with pytest.raises((ParserSyntaxError, ParserTypeError)): | 185 | with pytest.raises((ParserSyntaxError, ParserTypeError)): |
| 195 | - pl.parse(code) | 186 | + |
| 187 | + @pl.jit(auto_mutex=False) | ||
| 188 | + def shape_index(x: pl.Tensor[[2, 128], pl.DT_FP32]): | ||
| 189 | + n = x.shape[index] # noqa: F841 | ||
| 190 | + _test_result = x | ||
| 191 | + | ||
| 192 | + shape_index.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 196 | 193 | ||
| 197 | 194 | ||
| 198 | def test_shape_subscript_rejects_non_tensor_base(): | 195 | def test_shape_subscript_rejects_non_tensor_base(): |
| 199 | - code = """ | ||
| 200 | -@pl.function | ||
| 201 | -def shape_index(x: pl.DT_INT64) -> pl.DT_INT64: | ||
| 202 | - return x.shape[0] | ||
| 203 | -""" | ||
| 204 | with pytest.raises((ParserSyntaxError, ParserTypeError, UnsupportedFeatureError)): | 196 | with pytest.raises((ParserSyntaxError, ParserTypeError, UnsupportedFeatureError)): |
| 205 | - pl.parse(code) | 197 | + |
| 198 | + @pl.jit(auto_mutex=False) | ||
| 199 | + def shape_index(x: pl.DT_INT64): | ||
| 200 | + _test_result = x.shape[0] | ||
| 201 | + | ||
| 202 | + shape_index.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 206 | 203 | ||
| 207 | 204 | ||
| 208 | def test_shape_subscript_rejects_runtime_axis(): | 205 | def test_shape_subscript_rejects_runtime_axis(): |
| 209 | - code = """ | ||
| 210 | -@pl.function | ||
| 211 | -def shape_index( | ||
| 212 | - x: pl.Tensor[[2, 128], pl.DT_FP32], | ||
| 213 | - axis: pl.DT_INT64, | ||
| 214 | -) -> pl.DT_INT64: | ||
| 215 | - return x.shape[axis] | ||
| 216 | -""" | ||
| 217 | with pytest.raises(ParserSyntaxError, match="tensor.shape index must be a compile-time integer"): | 206 | with pytest.raises(ParserSyntaxError, match="tensor.shape index must be a compile-time integer"): |
| 218 | - pl.parse(code) | 207 | + |
| 208 | + @pl.jit(auto_mutex=False) | ||
| 209 | + def shape_index( | ||
| 210 | + x: pl.Tensor[[2, 128], pl.DT_FP32], | ||
| 211 | + axis: pl.DT_INT64, | ||
| 212 | + ): | ||
| 213 | + _test_result = x.shape[axis] | ||
| 214 | + | ||
| 215 | + shape_index.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| @@ -49,7 +49,7 @@ def _assert_in_order(source: str, *snippets: str) -> None: | |||
| 49 | assert position >= 0, f"missing or reordered CCE snippet: {snippet}\n{source}" | 49 | assert position >= 0, f"missing or reordered CCE snippet: {snippet}\n{source}" |
| 50 | 50 | ||
| 51 | 51 | ||
| 52 | -@pl.kernel | 52 | +@pl.jit |
| 53 | def _vector_pipeline_kernel( | 53 | def _vector_pipeline_kernel( |
| 54 | x: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | 54 | x: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], |
| 55 | out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | 55 | out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], |
| @@ -74,7 +74,7 @@ def _vector_pipeline_kernel( | |||
| 74 | pl.system.sync_dst(set_pipe=pl.PipeType.MTE3, wait_pipe=pl.PipeType.MTE2, event_id=4) | 74 | pl.system.sync_dst(set_pipe=pl.PipeType.MTE3, wait_pipe=pl.PipeType.MTE2, event_id=4) |
| 75 | 75 | ||
| 76 | 76 | ||
| 77 | -@pl.kernel | 77 | +@pl.jit |
| 78 | def _cube_pipeline_kernel( | 78 | def _cube_pipeline_kernel( |
| 79 | a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16], | 79 | a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16], |
| 80 | b: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16], | 80 | b: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP16], |
| @@ -113,7 +113,7 @@ def _cube_pipeline_kernel( | |||
| 113 | pl.system.sync_dst(set_pipe=pl.PipeType.MTE1, wait_pipe=pl.PipeType.FIX, event_id=6) | 113 | pl.system.sync_dst(set_pipe=pl.PipeType.MTE1, wait_pipe=pl.PipeType.FIX, event_id=6) |
| 114 | 114 | ||
| 115 | 115 | ||
| 116 | -@pl.kernel | 116 | +@pl.jit |
| 117 | def _scalar_pipeline_kernel( | 117 | def _scalar_pipeline_kernel( |
| 118 | x: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_INT32], | 118 | x: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_INT32], |
| 119 | out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_INT32], | 119 | out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_INT32], |
| @@ -133,7 +133,7 @@ def _scalar_pipeline_kernel( | |||
| 133 | pl.system.sync_dst(set_pipe=pl.PipeType.MTE3, wait_pipe=pl.PipeType.MTE2, event_id=0) | 133 | pl.system.sync_dst(set_pipe=pl.PipeType.MTE3, wait_pipe=pl.PipeType.MTE2, event_id=0) |
| 134 | 134 | ||
| 135 | 135 | ||
| 136 | -@pl.kernel | 136 | +@pl.jit |
| 137 | def _static_event_kernel( | 137 | def _static_event_kernel( |
| 138 | x: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | 138 | x: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], |
| 139 | ): | 139 | ): |
| @@ -179,7 +179,7 @@ def _datatype_static_event_kernel(x: pl.Ptr[pl.DT_UINT8]): | |||
| 179 | pl.system.sync_dst(set_pipe=pl.PipeType.MTE2, wait_pipe=pl.PipeType.V, event_id=6) | 179 | pl.system.sync_dst(set_pipe=pl.PipeType.MTE2, wait_pipe=pl.PipeType.V, event_id=6) |
| 180 | 180 | ||
| 181 | 181 | ||
| 182 | -@pl.kernel | 182 | +@pl.jit |
| 183 | def _dynamic_event_kernel( | 183 | def _dynamic_event_kernel( |
| 184 | x: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | 184 | x: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], |
| 185 | event_id: pl.DT_INT64, | 185 | event_id: pl.DT_INT64, |
| @@ -207,7 +207,7 @@ def _dynamic_event_kernel( | |||
| 207 | pl.system.sync_dst(set_pipe=pl.PipeType.MTE2, wait_pipe=pl.PipeType.V, event_id=ping_pong) | 207 | pl.system.sync_dst(set_pipe=pl.PipeType.MTE2, wait_pipe=pl.PipeType.V, event_id=ping_pong) |
| 208 | 208 | ||
| 209 | 209 | ||
| 210 | -@pl.kernel | 210 | +@pl.jit |
| 211 | def _control_flow_kernel( | 211 | def _control_flow_kernel( |
| 212 | x: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | 212 | x: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], |
| 213 | condition: pl.DT_BOOL, | 213 | condition: pl.DT_BOOL, |
| @@ -238,7 +238,7 @@ def _control_flow_kernel( | |||
| 238 | index = index + 1 | 238 | index = index + 1 |
| 239 | 239 | ||
| 240 | 240 | ||
| 241 | -@pl.kernel | 241 | +@pl.jit |
| 242 | def _barrier_kernel( | 242 | def _barrier_kernel( |
| 243 | x: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | 243 | x: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], |
| 244 | condition: pl.DT_BOOL, | 244 | condition: pl.DT_BOOL, |
| @@ -258,7 +258,7 @@ def _barrier_kernel( | |||
| 258 | pl.system.sync_dst(set_pipe=pl.PipeType.MTE2, wait_pipe=pl.PipeType.V, event_id=0) | 258 | pl.system.sync_dst(set_pipe=pl.PipeType.MTE2, wait_pipe=pl.PipeType.V, event_id=0) |
| 259 | 259 | ||
| 260 | 260 | ||
| 261 | -@pl.kernel | 261 | +@pl.jit |
| 262 | def _sync_all_hard_kernel( | 262 | def _sync_all_hard_kernel( |
| 263 | x: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | 263 | x: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], |
| 264 | ): | 264 | ): |
| @@ -270,7 +270,7 @@ def _sync_all_hard_kernel( | |||
| 270 | pl.system.sync_all(core_type=pl.SyncCoreType.MIX) | 270 | pl.system.sync_all(core_type=pl.SyncCoreType.MIX) |
| 271 | 271 | ||
| 272 | 272 | ||
| 273 | -@pl.kernel | 273 | +@pl.jit |
| 274 | def _mutex_kernel( | 274 | def _mutex_kernel( |
| 275 | x: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | 275 | x: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], |
| 276 | out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | 276 | out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], |
| @@ -313,7 +313,7 @@ def _mutex_kernel( | |||
| 313 | pl.system.mutex_unlock(pipe=pl.PipeType.M, mutex_id=11) | 313 | pl.system.mutex_unlock(pipe=pl.PipeType.M, mutex_id=11) |
| 314 | 314 | ||
| 315 | 315 | ||
| 316 | -@pl.kernel(auto_mutex=True) | 316 | +@pl.jit(auto_mutex=True) |
| 317 | def _auto_mutex_combination_kernel( | 317 | def _auto_mutex_combination_kernel( |
| 318 | x: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | 318 | x: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], |
| 319 | out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | 319 | out: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], |
| @@ -343,8 +343,8 @@ def _auto_mutex_combination_kernel( | |||
| 343 | 343 | ||
| 344 | 344 | ||
| 345 | def test_a01_a03_a04_a06_a07_a08_vector_and_scalar_pipeline_order(): | 345 | def test_a01_a03_a04_a06_a07_a08_vector_and_scalar_pipeline_order(): |
| 346 | - vector = _compile_to_cce(_vector_pipeline_kernel) | 346 | + vector = _compile_to_cce(_vector_pipeline_kernel.to_kernel_def()) |
| 347 | - scalar = _compile_to_cce(_scalar_pipeline_kernel) | 347 | + scalar = _compile_to_cce(_scalar_pipeline_kernel.to_kernel_def()) |
| 348 | 348 | ||
| 349 | _assert_in_order( | 349 | _assert_in_order( |
| 350 | vector, | 350 | vector, |
| @@ -371,7 +371,7 @@ def test_a01_a03_a04_a06_a07_a08_vector_and_scalar_pipeline_order(): | |||
| 371 | 371 | ||
| 372 | 372 | ||
| 373 | def test_a02_a05_a09_a10_a11_cube_forward_and_reverse_reuse_order(): | 373 | def test_a02_a05_a09_a10_a11_cube_forward_and_reverse_reuse_order(): |
| 374 | - cpp = _compile_to_cce(_cube_pipeline_kernel) | 374 | + cpp = _compile_to_cce(_cube_pipeline_kernel.to_kernel_def()) |
| 375 | _assert_in_order( | 375 | _assert_in_order( |
| 376 | cpp, | 376 | cpp, |
| 377 | "set_flag(PIPE_MTE2, PIPE_MTE1, (event_t)0);", | 377 | "set_flag(PIPE_MTE2, PIPE_MTE1, (event_t)0);", |
| @@ -404,7 +404,7 @@ def test_a02_a05_a09_a10_a11_cube_forward_and_reverse_reuse_order(): | |||
| 404 | ], | 404 | ], |
| 405 | ) | 405 | ) |
| 406 | def test_b01_b07_static_event_id_generalization(event_token): | 406 | def test_b01_b07_static_event_id_generalization(event_token): |
| 407 | - cpp = _compile_to_cce(_static_event_kernel) | 407 | + cpp = _compile_to_cce(_static_event_kernel.to_kernel_def()) |
| 408 | assert event_token in cpp | 408 | assert event_token in cpp |
| 409 | if event_token == "(event_t)2": | 409 | if event_token == "(event_t)2": |
| 410 | assert cpp.count("(event_t)2") >= 2 | 410 | assert cpp.count("(event_t)2") >= 2 |
| @@ -431,7 +431,7 @@ def test_b07_datatype_branch_folds_selected_event_id(dtype, event_id): | |||
| 431 | 431 | ||
| 432 | 432 | ||
| 433 | def test_b06_b10_runtime_event_id_generalization(): | 433 | def test_b06_b10_runtime_event_id_generalization(): |
| 434 | - cpp = _compile_to_cce(_dynamic_event_kernel) | 434 | + cpp = _compile_to_cce(_dynamic_event_kernel.to_kernel_def()) |
| 435 | sync_calls = re.findall(r"(?:set_flag|wait_flag)\([^;\n]+\);", cpp) | 435 | sync_calls = re.findall(r"(?:set_flag|wait_flag)\([^;\n]+\);", cpp) |
| 436 | assert sum(call.startswith("set_flag(") for call in sync_calls) >= 4 | 436 | assert sum(call.startswith("set_flag(") for call in sync_calls) >= 4 |
| 437 | assert sum(call.startswith("wait_flag(") for call in sync_calls) >= 4 | 437 | assert sum(call.startswith("wait_flag(") for call in sync_calls) >= 4 |
| @@ -452,7 +452,7 @@ def test_b06_b10_runtime_event_id_generalization(): | |||
| 452 | ids=["C01-C02-runtime-if-else", "C03-C05-nested-for-if", "C06-while"], | 452 | ids=["C01-C02-runtime-if-else", "C03-C05-nested-for-if", "C06-while"], |
| 453 | ) | 453 | ) |
| 454 | def test_c01_c06_sync_is_preserved_in_control_flow(control_token, inner_sync): | 454 | def test_c01_c06_sync_is_preserved_in_control_flow(control_token, inner_sync): |
| 455 | - cpp = _compile_to_cce(_control_flow_kernel) | 455 | + cpp = _compile_to_cce(_control_flow_kernel.to_kernel_def()) |
| 456 | assert control_token in cpp | 456 | assert control_token in cpp |
| 457 | assert cpp.count("set_flag(") == cpp.count("wait_flag(") | 457 | assert cpp.count("set_flag(") == cpp.count("wait_flag(") |
| 458 | _assert_in_order(cpp, control_token, inner_sync) | 458 | _assert_in_order(cpp, control_token, inner_sync) |
| @@ -471,7 +471,7 @@ def test_c01_c06_sync_is_preserved_in_control_flow(control_token, inner_sync): | |||
| 471 | ) | 471 | ) |
| 472 | def test_d02_d11_barrier_codegen(case_id, barrier, minimum_count, container): | 472 | def test_d02_d11_barrier_codegen(case_id, barrier, minimum_count, container): |
| 473 | del case_id | 473 | del case_id |
| 474 | - cpp = _compile_to_cce(_barrier_kernel) | 474 | + cpp = _compile_to_cce(_barrier_kernel.to_kernel_def()) |
| 475 | assert cpp.count(barrier) >= minimum_count | 475 | assert cpp.count(barrier) >= minimum_count |
| 476 | if container is not None: | 476 | if container is not None: |
| 477 | _assert_in_order(cpp, container, barrier) | 477 | _assert_in_order(cpp, container, barrier) |
| @@ -493,11 +493,11 @@ def test_d02_d11_barrier_codegen(case_id, barrier, minimum_count, container): | |||
| 493 | ids=["E01-hard-aiv", "E02-hard-aic", "E03-hard-mix"], | 493 | ids=["E01-hard-aiv", "E02-hard-aic", "E03-hard-mix"], |
| 494 | ) | 494 | ) |
| 495 | def test_e01_e03_hard_sync_all(syncall): | 495 | def test_e01_e03_hard_sync_all(syncall): |
| 496 | - assert syncall in _compile_to_cce(_sync_all_hard_kernel) | 496 | + assert syncall in _compile_to_cce(_sync_all_hard_kernel.to_kernel_def()) |
| 497 | 497 | ||
| 498 | 498 | ||
| 499 | def test_f01_f09_static_dynamic_and_control_flow_mutex(): | 499 | def test_f01_f09_static_dynamic_and_control_flow_mutex(): |
| 500 | - cpp = _compile_to_cce(_mutex_kernel) | 500 | + cpp = _compile_to_cce(_mutex_kernel.to_kernel_def()) |
| 501 | assert "get_buf(PIPE_MTE2, 0, 0);" in cpp | 501 | assert "get_buf(PIPE_MTE2, 0, 0);" in cpp |
| 502 | assert "rls_buf(PIPE_MTE2, 0, 0);" in cpp | 502 | assert "rls_buf(PIPE_MTE2, 0, 0);" in cpp |
| 503 | assert "get_buf(PIPE_MTE3" in cpp | 503 | assert "get_buf(PIPE_MTE3" in cpp |
| @@ -515,7 +515,7 @@ def test_f01_f09_static_dynamic_and_control_flow_mutex(): | |||
| 515 | 515 | ||
| 516 | 516 | ||
| 517 | def test_h01_h05_auto_mutex_with_all_manual_sync_forms(): | 517 | def test_h01_h05_auto_mutex_with_all_manual_sync_forms(): |
| 518 | - cpp = _compile_to_cce(_auto_mutex_combination_kernel) | 518 | + cpp = _compile_to_cce(_auto_mutex_combination_kernel.to_kernel_def()) |
| 519 | assert "get_buf(" in cpp | 519 | assert "get_buf(" in cpp |
| 520 | assert "rls_buf(" in cpp | 520 | assert "rls_buf(" in cpp |
| 521 | assert "set_flag(PIPE_MTE2, PIPE_V, (event_t)4);" in cpp | 521 | assert "set_flag(PIPE_MTE2, PIPE_V, (event_t)4);" in cpp |
| @@ -10,7 +10,7 @@ | |||
| 10 | # ----------------------------------------------------------------------------------------------------------- | 10 | # ----------------------------------------------------------------------------------------------------------- |
| 11 | """CCE codegen smoke tests for system.sync_all. | 11 | """CCE codegen smoke tests for system.sync_all. |
| 12 | 12 | ||
| 13 | -Each test compiles a @pl.kernel with pl.system.sync_all() and verifies that the | 13 | +Each test compiles a @pl.jit with pl.system.sync_all() and verifies that the |
| 14 | generated CCE C++ output contains the expected SYNCALL calls with the correct | 14 | generated CCE C++ output contains the expected SYNCALL calls with the correct |
| 15 | mode and core_type attributes. Covered cases include the default Mix/hard mode | 15 | mode and core_type attributes. Covered cases include the default Mix/hard mode |
| 16 | plus explicit AIVOnly/Mix and soft mode variants. | 16 | plus explicit AIVOnly/Mix and soft mode variants. |
| @@ -21,10 +21,10 @@ import logging | |||
| 21 | import pypto_pro.language as pl | 21 | import pypto_pro.language as pl |
| 22 | 22 | ||
| 23 | 23 | ||
| 24 | -def _compile_to_cce(kernel_def) -> str: | 24 | +def _compile_to_cce(kernel) -> str: |
| 25 | from pypto_pro.runtime.jit import _assemble_cv_source, _parse_and_codegen_targets | 25 | from pypto_pro.runtime.jit import _assemble_cv_source, _parse_and_codegen_targets |
| 26 | 26 | ||
| 27 | - cube, vector = _parse_and_codegen_targets(kernel_def, "a5", "") | 27 | + cube, vector = _parse_and_codegen_targets(kernel.to_kernel_def(), "a5", "") |
| 28 | return _assemble_cv_source(cube, vector).content | 28 | return _assemble_cv_source(cube, vector).content |
| 29 | 29 | ||
| 30 | 30 | ||
| @@ -33,7 +33,7 @@ def _compile_to_cce(kernel_def) -> str: | |||
| 33 | # --------------------------------------------------------------------------- | 33 | # --------------------------------------------------------------------------- |
| 34 | 34 | ||
| 35 | 35 | ||
| 36 | -@pl.kernel | 36 | +@pl.jit |
| 37 | def _sync_all_mix_kernel( | 37 | def _sync_all_mix_kernel( |
| 38 | a: pl.Tensor[[64, 128], pl.DT_FP16], | 38 | a: pl.Tensor[[64, 128], pl.DT_FP16], |
| 39 | ): | 39 | ): |
| @@ -43,7 +43,7 @@ def _sync_all_mix_kernel( | |||
| 43 | pl.system.sync_all() | 43 | pl.system.sync_all() |
| 44 | 44 | ||
| 45 | 45 | ||
| 46 | -@pl.kernel | 46 | +@pl.jit |
| 47 | def _sync_all_aiv_only_kernel( | 47 | def _sync_all_aiv_only_kernel( |
| 48 | a: pl.Tensor[[64, 128], pl.DT_FP16], | 48 | a: pl.Tensor[[64, 128], pl.DT_FP16], |
| 49 | ): | 49 | ): |
| @@ -58,7 +58,7 @@ def _sync_all_aiv_only_kernel( | |||
| 58 | # --------------------------------------------------------------------------- | 58 | # --------------------------------------------------------------------------- |
| 59 | 59 | ||
| 60 | 60 | ||
| 61 | -@pl.kernel | 61 | +@pl.jit |
| 62 | def _sync_all_soft_aiv_only_kernel( | 62 | def _sync_all_soft_aiv_only_kernel( |
| 63 | a: pl.Tensor[[64, 128], pl.DT_FP16], | 63 | a: pl.Tensor[[64, 128], pl.DT_FP16], |
| 64 | sync_gm: pl.Tensor[[384], pl.DT_INT32], | 64 | sync_gm: pl.Tensor[[384], pl.DT_INT32], |
| @@ -71,7 +71,7 @@ def _sync_all_soft_aiv_only_kernel( | |||
| 71 | pl.system.sync_all([sync_gm, sync_ub], mode=pl.SyncAllMode.SOFT, core_type=pl.SyncCoreType.AIV_ONLY) | 71 | pl.system.sync_all([sync_gm, sync_ub], mode=pl.SyncAllMode.SOFT, core_type=pl.SyncCoreType.AIV_ONLY) |
| 72 | 72 | ||
| 73 | 73 | ||
| 74 | -@pl.kernel | 74 | +@pl.jit |
| 75 | def _sync_all_soft_mix_kernel( | 75 | def _sync_all_soft_mix_kernel( |
| 76 | a: pl.Tensor[[64, 128], pl.DT_FP16], | 76 | a: pl.Tensor[[64, 128], pl.DT_FP16], |
| 77 | sync_gm: pl.Tensor[[384], pl.DT_INT32], | 77 | sync_gm: pl.Tensor[[384], pl.DT_INT32], |
| @@ -10,6 +10,7 @@ | |||
| 10 | # ----------------------------------------------------------------------------------------------------------- | 10 | # ----------------------------------------------------------------------------------------------------------- |
| 11 | """Shared fixtures for unit tests.""" | 11 | """Shared fixtures for unit tests.""" |
| 12 | 12 | ||
| 13 | +from __future__ import annotations | ||
| 13 | 14 | ||
| 14 | 15 | ||
| 15 | def duration_estimate(seconds: float): | 16 | def duration_estimate(seconds: float): |
| @@ -52,14 +52,14 @@ def _dynamic_tuple() -> ir.MakeTuple: | |||
| 52 | 52 | ||
| 53 | 53 | ||
| 54 | def _parse_kernel(kernel_def) -> ir.Program: | 54 | def _parse_kernel(kernel_def) -> ir.Program: |
| 55 | - return kernel_def.parse_target_program(ir.SectionKind.Vector)[0] | 55 | + return kernel_def.to_kernel_def().parse_target_program(ir.SectionKind.Vector)[0] |
| 56 | 56 | ||
| 57 | 57 | ||
| 58 | def _kernel_ir(kernel_def) -> str: | 58 | def _kernel_ir(kernel_def) -> str: |
| 59 | return str(_parse_kernel(kernel_def)) | 59 | return str(_parse_kernel(kernel_def)) |
| 60 | 60 | ||
| 61 | 61 | ||
| 62 | -# ``@pl.kernel`` re-raises a builder ValueError (alignment, underivable size) as a | 62 | +# ``@pl.jit`` re-raises a builder ValueError (alignment, underivable size) as a |
| 63 | # ParserSyntaxError that carries the original message, so those cases are matched | 63 | # ParserSyntaxError that carries the original message, so those cases are matched |
| 64 | # on the message rather than on ValueError itself. | 64 | # on the message rather than on ValueError itself. |
| 65 | _WRAPPED = ParserSyntaxError | 65 | _WRAPPED = ParserSyntaxError |
| @@ -189,7 +189,7 @@ def test_builder_leaves_unaligned_spaces_alone(): | |||
| 189 | 189 | ||
| 190 | 190 | ||
| 191 | def test_addr_zero_is_accepted_in_a_kernel(): | 191 | def test_addr_zero_is_accepted_in_a_kernel(): |
| 192 | - @pl.kernel | 192 | + @pl.jit |
| 193 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): | 193 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): |
| 194 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 194 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 195 | t = pl.make_tile(tt, addr=0) | 195 | t = pl.make_tile(tt, addr=0) |
| @@ -201,7 +201,7 @@ def test_addr_zero_is_accepted_in_a_kernel(): | |||
| 201 | def test_addr_none_is_rejected(): | 201 | def test_addr_none_is_rejected(): |
| 202 | """``addr=None`` parses to IR, not to Python None, so it fails the int check.""" | 202 | """``addr=None`` parses to IR, not to Python None, so it fails the int check.""" |
| 203 | 203 | ||
| 204 | - @pl.kernel | 204 | + @pl.jit |
| 205 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): | 205 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): |
| 206 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 206 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 207 | t = pl.make_tile(tt, addr=None) | 207 | t = pl.make_tile(tt, addr=None) |
| @@ -214,7 +214,7 @@ def test_addr_none_is_rejected(): | |||
| 214 | def test_bool_addr_is_rejected(): | 214 | def test_bool_addr_is_rejected(): |
| 215 | """bool subclasses int, but ``addr=False`` is a mistake, not address 0.""" | 215 | """bool subclasses int, but ``addr=False`` is a mistake, not address 0.""" |
| 216 | 216 | ||
| 217 | - @pl.kernel | 217 | + @pl.jit |
| 218 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): | 218 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): |
| 219 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 219 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 220 | t = pl.make_tile(tt, addr=False) | 220 | t = pl.make_tile(tt, addr=False) |
| @@ -225,7 +225,7 @@ def test_bool_addr_is_rejected(): | |||
| 225 | 225 | ||
| 226 | 226 | ||
| 227 | def test_float_addr_is_rejected(): | 227 | def test_float_addr_is_rejected(): |
| 228 | - @pl.kernel | 228 | + @pl.jit |
| 229 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): | 229 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): |
| 230 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 230 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 231 | t = pl.make_tile(tt, addr=0.0) | 231 | t = pl.make_tile(tt, addr=0.0) |
| @@ -238,7 +238,7 @@ def test_float_addr_is_rejected(): | |||
| 238 | def test_loop_index_addr_is_rejected(): | 238 | def test_loop_index_addr_is_rejected(): |
| 239 | """A tile's address is fixed while parsing, so it cannot follow a loop.""" | 239 | """A tile's address is fixed while parsing, so it cannot follow a loop.""" |
| 240 | 240 | ||
| 241 | - @pl.kernel | 241 | + @pl.jit |
| 242 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): | 242 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): |
| 243 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 243 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 244 | for i in pl.range(2): | 244 | for i in pl.range(2): |
| @@ -254,7 +254,7 @@ def test_loop_index_addr_is_rejected(): | |||
| 254 | def test_addr_from_a_kernel_local_constant_is_folded(): | 254 | def test_addr_from_a_kernel_local_constant_is_folded(): |
| 255 | """addr accepts any expression with a parse-time value, not just literals.""" | 255 | """addr accepts any expression with a parse-time value, not just literals.""" |
| 256 | 256 | ||
| 257 | - @pl.kernel | 257 | + @pl.jit |
| 258 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): | 258 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): |
| 259 | base = 0x100 | 259 | base = 0x100 |
| 260 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 260 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| @@ -270,7 +270,7 @@ def test_addr_from_a_kernel_local_constant_is_folded(): | |||
| 270 | 270 | ||
| 271 | 271 | ||
| 272 | def test_size_is_derived_when_omitted(): | 272 | def test_size_is_derived_when_omitted(): |
| 273 | - @pl.kernel | 273 | + @pl.jit |
| 274 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): | 274 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): |
| 275 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 275 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 276 | t = pl.make_tile(tt, addr=0) | 276 | t = pl.make_tile(tt, addr=0) |
| @@ -282,7 +282,7 @@ def test_size_is_derived_when_omitted(): | |||
| 282 | def test_size_none_is_rejected_rather_than_derived(): | 282 | def test_size_none_is_rejected_rather_than_derived(): |
| 283 | """``size=None`` is a value that is not an integer, not an omitted argument.""" | 283 | """``size=None`` is a value that is not an integer, not an omitted argument.""" |
| 284 | 284 | ||
| 285 | - @pl.kernel | 285 | + @pl.jit |
| 286 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): | 286 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): |
| 287 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 287 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 288 | t = pl.make_tile(tt, addr=0, size=None) | 288 | t = pl.make_tile(tt, addr=0, size=None) |
| @@ -293,7 +293,7 @@ def test_size_none_is_rejected_rather_than_derived(): | |||
| 293 | 293 | ||
| 294 | 294 | ||
| 295 | def test_negative_size_is_rejected(): | 295 | def test_negative_size_is_rejected(): |
| 296 | - @pl.kernel | 296 | + @pl.jit |
| 297 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): | 297 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): |
| 298 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 298 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 299 | t = pl.make_tile(tt, addr=0, size=-32) | 299 | t = pl.make_tile(tt, addr=0, size=-32) |
| @@ -306,7 +306,7 @@ def test_negative_size_is_rejected(): | |||
| 306 | def test_zero_size_is_rejected(): | 306 | def test_zero_size_is_rejected(): |
| 307 | """Zero is held to the same contract as a negative byte count.""" | 307 | """Zero is held to the same contract as a negative byte count.""" |
| 308 | 308 | ||
| 309 | - @pl.kernel | 309 | + @pl.jit |
| 310 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): | 310 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): |
| 311 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 311 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 312 | t = pl.make_tile(tt, addr=0, size=0) | 312 | t = pl.make_tile(tt, addr=0, size=0) |
| @@ -317,7 +317,7 @@ def test_zero_size_is_rejected(): | |||
| 317 | 317 | ||
| 318 | 318 | ||
| 319 | def test_runtime_size_is_rejected(): | 319 | def test_runtime_size_is_rejected(): |
| 320 | - @pl.kernel | 320 | + @pl.jit |
| 321 | def k(x: pl.Tensor[[pl.DYNAMIC, 64], pl.DT_FP16]): | 321 | def k(x: pl.Tensor[[pl.DYNAMIC, 64], pl.DT_FP16]): |
| 322 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 322 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 323 | t = pl.make_tile(tt, addr=0, size=x.shape[0] * 2) | 323 | t = pl.make_tile(tt, addr=0, size=x.shape[0] * 2) |
| @@ -335,7 +335,7 @@ def test_runtime_size_is_rejected(): | |||
| 335 | def test_a_shape_in_place_of_a_tile_type_is_rejected(): | 335 | def test_a_shape_in_place_of_a_tile_type_is_rejected(): |
| 336 | """A tile's shape only reaches make_tile through a TileType.""" | 336 | """A tile's shape only reaches make_tile through a TileType.""" |
| 337 | 337 | ||
| 338 | - @pl.kernel | 338 | + @pl.jit |
| 339 | def k(x: pl.Tensor[[64], pl.DT_FP16]): | 339 | def k(x: pl.Tensor[[64], pl.DT_FP16]): |
| 340 | t = pl.make_tile([64], pl.DT_FP16, pl.MemorySpace.Vec, 0, 128) | 340 | t = pl.make_tile([64], pl.DT_FP16, pl.MemorySpace.Vec, 0, 128) |
| 341 | pl.load(t, x, [0]) | 341 | pl.load(t, x, [0]) |
| @@ -350,7 +350,7 @@ def test_a_shape_in_place_of_a_tile_type_is_rejected(): | |||
| 350 | def test_tile_type_may_be_built_inline(): | 350 | def test_tile_type_may_be_built_inline(): |
| 351 | """The leading TileType is recognised by value, not by being a named variable.""" | 351 | """The leading TileType is recognised by value, not by being a named variable.""" |
| 352 | 352 | ||
| 353 | - @pl.kernel | 353 | + @pl.jit |
| 354 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): | 354 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): |
| 355 | t = pl.make_tile( | 355 | t = pl.make_tile( |
| 356 | pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec), addr=0x40 | 356 | pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec), addr=0x40 |
| @@ -365,7 +365,7 @@ def test_tile_type_may_be_built_inline(): | |||
| 365 | def test_a_tile_type_is_required_even_when_addr_is_given(): | 365 | def test_a_tile_type_is_required_even_when_addr_is_given(): |
| 366 | """The builder's spread-out form is not a DSL spelling of make_tile.""" | 366 | """The builder's spread-out form is not a DSL spelling of make_tile.""" |
| 367 | 367 | ||
| 368 | - @pl.kernel | 368 | + @pl.jit |
| 369 | def k(x: pl.Tensor[[64], pl.DT_FP16]): | 369 | def k(x: pl.Tensor[[64], pl.DT_FP16]): |
| 370 | t = pl.make_tile([64], pl.DT_FP16, pl.MemorySpace.Vec, 0x40) | 370 | t = pl.make_tile([64], pl.DT_FP16, pl.MemorySpace.Vec, 0x40) |
| 371 | pl.load(t, x, [0]) | 371 | pl.load(t, x, [0]) |
| @@ -375,7 +375,7 @@ def test_a_tile_type_is_required_even_when_addr_is_given(): | |||
| 375 | 375 | ||
| 376 | 376 | ||
| 377 | def test_a_call_with_no_positional_argument_is_rejected(): | 377 | def test_a_call_with_no_positional_argument_is_rejected(): |
| 378 | - @pl.kernel | 378 | + @pl.jit |
| 379 | def k(x: pl.Tensor[[64], pl.DT_FP16]): | 379 | def k(x: pl.Tensor[[64], pl.DT_FP16]): |
| 380 | t = pl.make_tile(addr=0x40) | 380 | t = pl.make_tile(addr=0x40) |
| 381 | pl.load(t, x, [0]) | 381 | pl.load(t, x, [0]) |
| @@ -387,7 +387,7 @@ def test_a_call_with_no_positional_argument_is_rejected(): | |||
| 387 | def test_addr_and_size_given_positionally_are_rejected(): | 387 | def test_addr_and_size_given_positionally_are_rejected(): |
| 388 | """A bare pair of ints reads the same swapped, so neither position binds.""" | 388 | """A bare pair of ints reads the same swapped, so neither position binds.""" |
| 389 | 389 | ||
| 390 | - @pl.kernel | 390 | + @pl.jit |
| 391 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): | 391 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): |
| 392 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 392 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 393 | t = pl.make_tile(tt, 0, 128) | 393 | t = pl.make_tile(tt, 0, 128) |
| @@ -402,7 +402,7 @@ def test_addr_and_size_given_positionally_are_rejected(): | |||
| 402 | def test_a_positional_addr_is_not_silently_shadowed_by_a_keyword_one(): | 402 | def test_a_positional_addr_is_not_silently_shadowed_by_a_keyword_one(): |
| 403 | """Two addrs used to resolve to the positional one, dropping the keyword.""" | 403 | """Two addrs used to resolve to the positional one, dropping the keyword.""" |
| 404 | 404 | ||
| 405 | - @pl.kernel | 405 | + @pl.jit |
| 406 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): | 406 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): |
| 407 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 407 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 408 | t = pl.make_tile(tt, 0x100, addr=0x2000) | 408 | t = pl.make_tile(tt, 0x100, addr=0x2000) |
| @@ -416,7 +416,7 @@ def test_a_positional_addr_is_not_silently_shadowed_by_a_keyword_one(): | |||
| 416 | def test_a_non_tile_type_first_argument_is_rejected(): | 416 | def test_a_non_tile_type_first_argument_is_rejected(): |
| 417 | """Anything that is not a TileType is reported, not silently treated as addr.""" | 417 | """Anything that is not a TileType is reported, not silently treated as addr.""" |
| 418 | 418 | ||
| 419 | - @pl.kernel | 419 | + @pl.jit |
| 420 | def k(x: pl.Tensor[[64], pl.DT_FP16]): | 420 | def k(x: pl.Tensor[[64], pl.DT_FP16]): |
| 421 | t = pl.make_tile(0x40) | 421 | t = pl.make_tile(0x40) |
| 422 | pl.load(t, x, [0]) | 422 | pl.load(t, x, [0]) |
| @@ -431,7 +431,7 @@ def test_a_non_tile_type_first_argument_is_rejected(): | |||
| 431 | 431 | ||
| 432 | 432 | ||
| 433 | def test_valid_shape_pad_and_compact_are_spread_from_the_tile_type(): | 433 | def test_valid_shape_pad_and_compact_are_spread_from_the_tile_type(): |
| 434 | - @pl.kernel | 434 | + @pl.jit |
| 435 | def k(x: pl.Tensor[[128, 128], pl.DT_FP16]): | 435 | def k(x: pl.Tensor[[128, 128], pl.DT_FP16]): |
| 436 | tt = pl.TileType( | 436 | tt = pl.TileType( |
| 437 | shape=[128, 128], | 437 | shape=[128, 128], |
| @@ -451,7 +451,7 @@ def test_valid_shape_pad_and_compact_are_spread_from_the_tile_type(): | |||
| 451 | 451 | ||
| 452 | 452 | ||
| 453 | def test_layout_and_fractal_are_spread_from_the_tile_type(): | 453 | def test_layout_and_fractal_are_spread_from_the_tile_type(): |
| 454 | - @pl.kernel | 454 | + @pl.jit |
| 455 | def k(x: pl.Tensor[[128, 128], pl.DT_FP16]): | 455 | def k(x: pl.Tensor[[128, 128], pl.DT_FP16]): |
| 456 | tt = pl.TileType( | 456 | tt = pl.TileType( |
| 457 | shape=[128, 128], | 457 | shape=[128, 128], |
| @@ -471,7 +471,7 @@ def test_layout_and_fractal_are_spread_from_the_tile_type(): | |||
| 471 | 471 | ||
| 472 | 472 | ||
| 473 | def test_an_explicit_kwarg_wins_over_the_tile_type_field(): | 473 | def test_an_explicit_kwarg_wins_over_the_tile_type_field(): |
| 474 | - @pl.kernel | 474 | + @pl.jit |
| 475 | def k(x: pl.Tensor[[128, 128], pl.DT_FP16]): | 475 | def k(x: pl.Tensor[[128, 128], pl.DT_FP16]): |
| 476 | tt = pl.TileType( | 476 | tt = pl.TileType( |
| 477 | shape=[128, 128], | 477 | shape=[128, 128], |
| @@ -490,7 +490,7 @@ def test_an_explicit_kwarg_wins_over_the_tile_type_field(): | |||
| 490 | def test_size_is_derived_from_the_shape_not_the_valid_shape(): | 490 | def test_size_is_derived_from_the_shape_not_the_valid_shape(): |
| 491 | """valid_shape narrows what the tile reads; the tile still occupies its full shape.""" | 491 | """valid_shape narrows what the tile reads; the tile still occupies its full shape.""" |
| 492 | 492 | ||
| 493 | - @pl.kernel | 493 | + @pl.jit |
| 494 | def k(x: pl.Tensor[[128, 128], pl.DT_FP16]): | 494 | def k(x: pl.Tensor[[128, 128], pl.DT_FP16]): |
| 495 | tt = pl.TileType( | 495 | tt = pl.TileType( |
| 496 | shape=[128, 128], | 496 | shape=[128, 128], |
| @@ -507,7 +507,7 @@ def test_size_is_derived_from_the_shape_not_the_valid_shape(): | |||
| 507 | def test_sub_byte_tile_reserves_one_byte_per_element(): | 507 | def test_sub_byte_tile_reserves_one_byte_per_element(): |
| 508 | """INT4 has no Vec op to consume it, so only the reservation is checked.""" | 508 | """INT4 has no Vec op to consume it, so only the reservation is checked.""" |
| 509 | 509 | ||
| 510 | - @pl.kernel | 510 | + @pl.jit |
| 511 | def k(x: pl.Tensor[[64], pl.DT_FP16]): | 511 | def k(x: pl.Tensor[[64], pl.DT_FP16]): |
| 512 | tt = pl.TileType(shape=[64, 64], dtype=pl.DT_INT4, target_memory=pl.MemorySpace.Vec) | 512 | tt = pl.TileType(shape=[64, 64], dtype=pl.DT_INT4, target_memory=pl.MemorySpace.Vec) |
| 513 | t = pl.make_tile(tt, addr=0) # noqa: F841 | 513 | t = pl.make_tile(tt, addr=0) # noqa: F841 |
| @@ -518,7 +518,7 @@ def test_sub_byte_tile_reserves_one_byte_per_element(): | |||
| 518 | def test_a_tile_type_with_a_runtime_shape_cannot_derive_its_size(): | 518 | def test_a_tile_type_with_a_runtime_shape_cannot_derive_its_size(): |
| 519 | """A tuple shape skips the TileType const check, so make_tile_expr reports it.""" | 519 | """A tuple shape skips the TileType const check, so make_tile_expr reports it.""" |
| 520 | 520 | ||
| 521 | - @pl.kernel | 521 | + @pl.jit |
| 522 | def k(x: pl.Tensor[[pl.DYNAMIC, 64], pl.DT_FP16]): | 522 | def k(x: pl.Tensor[[pl.DYNAMIC, 64], pl.DT_FP16]): |
| 523 | tt = pl.TileType(shape=(x.shape[0], 64), dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 523 | tt = pl.TileType(shape=(x.shape[0], 64), dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 524 | t = pl.make_tile(tt, addr=0) | 524 | t = pl.make_tile(tt, addr=0) |
| @@ -534,7 +534,7 @@ def test_a_tile_type_with_a_runtime_shape_cannot_derive_its_size(): | |||
| 534 | 534 | ||
| 535 | 535 | ||
| 536 | def test_misaligned_acc_addr_is_rejected(): | 536 | def test_misaligned_acc_addr_is_rejected(): |
| 537 | - @pl.kernel | 537 | + @pl.jit |
| 538 | def k(x: pl.Tensor[[128, 128], pl.DT_FP32]): | 538 | def k(x: pl.Tensor[[128, 128], pl.DT_FP32]): |
| 539 | tt = pl.TileType(shape=[128, 128], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Acc) | 539 | tt = pl.TileType(shape=[128, 128], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Acc) |
| 540 | t = pl.make_tile(tt, addr=0x20) | 540 | t = pl.make_tile(tt, addr=0x20) |
| @@ -545,7 +545,7 @@ def test_misaligned_acc_addr_is_rejected(): | |||
| 545 | 545 | ||
| 546 | 546 | ||
| 547 | def test_misaligned_left_addr_is_rejected(): | 547 | def test_misaligned_left_addr_is_rejected(): |
| 548 | - @pl.kernel | 548 | + @pl.jit |
| 549 | def k(x: pl.Tensor[[128, 128], pl.DT_FP16]): | 549 | def k(x: pl.Tensor[[128, 128], pl.DT_FP16]): |
| 550 | tt = pl.TileType(shape=[128, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Left) | 550 | tt = pl.TileType(shape=[128, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Left) |
| 551 | t = pl.make_tile(tt, addr=0x100) | 551 | t = pl.make_tile(tt, addr=0x100) |
| @@ -558,7 +558,7 @@ def test_misaligned_left_addr_is_rejected(): | |||
| 558 | def test_a_folded_addr_is_checked_for_alignment(): | 558 | def test_a_folded_addr_is_checked_for_alignment(): |
| 559 | """The alignment check sees the folded value, not the source expression.""" | 559 | """The alignment check sees the folded value, not the source expression.""" |
| 560 | 560 | ||
| 561 | - @pl.kernel | 561 | + @pl.jit |
| 562 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): | 562 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): |
| 563 | base = 0x40 | 563 | base = 0x40 |
| 564 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 564 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| @@ -575,7 +575,7 @@ def test_a_folded_addr_is_checked_for_alignment(): | |||
| 575 | 575 | ||
| 576 | 576 | ||
| 577 | def test_tile_type_shape_accepts_a_constant_expression(): | 577 | def test_tile_type_shape_accepts_a_constant_expression(): |
| 578 | - @pl.kernel | 578 | + @pl.jit |
| 579 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): | 579 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): |
| 580 | rows = 8 | 580 | rows = 8 |
| 581 | tt = pl.TileType(shape=[1, rows * 8], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 581 | tt = pl.TileType(shape=[1, rows * 8], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| @@ -586,7 +586,7 @@ def test_tile_type_shape_accepts_a_constant_expression(): | |||
| 586 | 586 | ||
| 587 | 587 | ||
| 588 | def test_tile_type_shape_rejects_a_bool_element(): | 588 | def test_tile_type_shape_rejects_a_bool_element(): |
| 589 | - @pl.kernel | 589 | + @pl.jit |
| 590 | def k(x: pl.Tensor[[64], pl.DT_FP16]): | 590 | def k(x: pl.Tensor[[64], pl.DT_FP16]): |
| 591 | tt = pl.TileType(shape=[True, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 591 | tt = pl.TileType(shape=[True, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 592 | t = pl.make_tile(tt, addr=0) | 592 | t = pl.make_tile(tt, addr=0) |
| @@ -597,7 +597,7 @@ def test_tile_type_shape_rejects_a_bool_element(): | |||
| 597 | 597 | ||
| 598 | 598 | ||
| 599 | def test_pad_need_compile_time_value(): | 599 | def test_pad_need_compile_time_value(): |
| 600 | - @pl.kernel | 600 | + @pl.jit |
| 601 | def k(x: pl.Tensor[[64], pl.DT_FP16], pad_mode: pl.DT_INT64): | 601 | def k(x: pl.Tensor[[64], pl.DT_FP16], pad_mode: pl.DT_INT64): |
| 602 | tt = pl.TileType( | 602 | tt = pl.TileType( |
| 603 | shape=[64], | 603 | shape=[64], |
| @@ -614,7 +614,7 @@ def test_pad_need_compile_time_value(): | |||
| 614 | def test_tile_type_valid_shape_rejects_a_runtime_element(): | 614 | def test_tile_type_valid_shape_rejects_a_runtime_element(): |
| 615 | """set_validshape() is the runtime path; the TileType field is parse-time only.""" | 615 | """set_validshape() is the runtime path; the TileType field is parse-time only.""" |
| 616 | 616 | ||
| 617 | - @pl.kernel | 617 | + @pl.jit |
| 618 | def k(x: pl.Tensor[[pl.DYNAMIC, 64], pl.DT_FP16]): | 618 | def k(x: pl.Tensor[[pl.DYNAMIC, 64], pl.DT_FP16]): |
| 619 | tt = pl.TileType( | 619 | tt = pl.TileType( |
| 620 | shape=[64, 64], | 620 | shape=[64, 64], |
| @@ -10,6 +10,8 @@ | |||
| 10 | # ----------------------------------------------------------------------------------------------------------- | 10 | # ----------------------------------------------------------------------------------------------------------- |
| 11 | """Tests for function type attribute feature.""" | 11 | """Tests for function type attribute feature.""" |
| 12 | 12 | ||
| 13 | +import inspect | ||
| 14 | + | ||
| 13 | from pypto_pro import DataType, ir | 15 | from pypto_pro import DataType, ir |
| 14 | from pypto_pro.ir import IRBuilder | 16 | from pypto_pro.ir import IRBuilder |
| 15 | import pypto_pro.language as pl | 17 | import pypto_pro.language as pl |
| @@ -38,7 +40,7 @@ def test_function_type_python_print(): | |||
| 38 | span = ir.Span.unknown() | 40 | span = ir.Span.unknown() |
| 39 | dtype = DataType.INT64 | 41 | dtype = DataType.INT64 |
| 40 | 42 | ||
| 41 | - # Opaque function should not print type parameter | 43 | + # Ordinary functions print without a frontend decorator. |
| 42 | with ib.function("default_func", span=span) as f: | 44 | with ib.function("default_func", span=span) as f: |
| 43 | x = f.param("x", ir.ScalarType(dtype), span=span) | 45 | x = f.param("x", ir.ScalarType(dtype), span=span) |
| 44 | f.return_type(ir.ScalarType(dtype)) | 46 | f.return_type(ir.ScalarType(dtype)) |
| @@ -46,10 +48,11 @@ def test_function_type_python_print(): | |||
| 46 | 48 | ||
| 47 | func_opaque = f.get_result() | 49 | func_opaque = f.get_result() |
| 48 | printed = ir.python_print(func_opaque, "pl") | 50 | printed = ir.python_print(func_opaque, "pl") |
| 49 | - assert "@pl.function\n" in printed | 51 | + assert printed.startswith("def default_func") |
| 50 | - assert "type=" not in printed # Opaque should not print type parameter | 52 | + assert "@pl.function" not in printed |
| 53 | + assert "type=" not in printed | ||
| 51 | 54 | ||
| 52 | - # InCore function should print type parameter | 55 | + # FunctionType remains IR metadata and is not serialized as a removed decorator. |
| 53 | with ib.function("kernel", span=span, func_type=ir.FunctionType.InCore) as f: | 56 | with ib.function("kernel", span=span, func_type=ir.FunctionType.InCore) as f: |
| 54 | x = f.param("x", ir.ScalarType(dtype), span=span) | 57 | x = f.param("x", ir.ScalarType(dtype), span=span) |
| 55 | f.return_type(ir.ScalarType(dtype)) | 58 | f.return_type(ir.ScalarType(dtype)) |
| @@ -57,27 +60,24 @@ def test_function_type_python_print(): | |||
| 57 | 60 | ||
| 58 | func_incore = f.get_result() | 61 | func_incore = f.get_result() |
| 59 | printed_incore = ir.python_print(func_incore, "pl") | 62 | printed_incore = ir.python_print(func_incore, "pl") |
| 60 | - assert "@pl.function(type=pl.FunctionType.InCore)" in printed_incore | 63 | + assert printed_incore.startswith("def kernel") |
| 64 | + assert "@pl.function" not in printed_incore | ||
| 65 | + assert "type=" not in printed_incore | ||
| 61 | 66 | ||
| 62 | 67 | ||
| 63 | -def test_function_type_decorator_parsing(): | 68 | +def test_jit_kernel_uses_opaque_function_type(): |
| 64 | - """Test parsing functions with type parameter in decorator.""" | 69 | + """pl.jit captures only Opaque kernel entries.""" |
| 70 | + assert "func_type" not in inspect.signature(pl.jit).parameters | ||
| 65 | 71 | ||
| 66 | - # Test Opaque (default) | 72 | + @pl.jit(auto_mutex=False) |
| 67 | - @pl.function | 73 | + def kernel(x: pl.Tensor[[4], pl.DT_INT64]): |
| 68 | - def default_func(x: pl.Tensor[[4], pl.DT_INT64]) -> pl.Tensor[[4], pl.DT_INT64]: | 74 | + _test_result = x |
| 69 | - return x | ||
| 70 | 75 | ||
| 71 | - assert default_func.name == "default_func" | 76 | + program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) |
| 72 | - assert default_func.func_type == ir.FunctionType.Opaque | 77 | + kernel = program.get_function(kernel.__name__) |
| 73 | - | ||
| 74 | - # Test InCore | ||
| 75 | - @pl.function(type=pl.FunctionType.InCore) | ||
| 76 | - def kernel(x: pl.Tensor[[4], pl.DT_INT64]) -> pl.Tensor[[4], pl.DT_INT64]: | ||
| 77 | - return x | ||
| 78 | 78 | ||
| 79 | assert kernel.name == "kernel" | 79 | assert kernel.name == "kernel" |
| 80 | - assert kernel.func_type == ir.FunctionType.InCore | 80 | + assert kernel.func_type == ir.FunctionType.Opaque |
| 81 | 81 | ||
| 82 | 82 | ||
| 83 | def test_function_type_language_export(): | 83 | def test_function_type_language_export(): |
| @@ -37,7 +37,7 @@ def test_python_print_block_tensor_and_tile_types(): | |||
| 37 | assert "ir.MemRef(ir.MemorySpace.Vec" in printed_tile | 37 | assert "ir.MemRef(ir.MemorySpace.Vec" in printed_tile |
| 38 | 38 | ||
| 39 | 39 | ||
| 40 | -def test_python_print_program_preserves_block_function_type(): | 40 | +def test_python_print_program_omits_block_function_decorator(): |
| 41 | span = ir.Span.unknown() | 41 | span = ir.Span.unknown() |
| 42 | x = ir.Var("x", ir.ScalarType(DataType.INT64), span) | 42 | x = ir.Var("x", ir.ScalarType(DataType.INT64), span) |
| 43 | fn = ir.Function( | 43 | fn = ir.Function( |
| @@ -52,8 +52,9 @@ def test_python_print_program_preserves_block_function_type(): | |||
| 52 | 52 | ||
| 53 | result = ir.python_print(program) | 53 | result = ir.python_print(program) |
| 54 | 54 | ||
| 55 | + assert fn.func_type == ir.FunctionType.InCore | ||
| 55 | assert "# ir.program: BlockProgram" in result | 56 | assert "# ir.program: BlockProgram" in result |
| 56 | - assert "@ir.function(type=ir.FunctionType.InCore)" in result | 57 | + assert "@ir.function" not in result |
| 57 | assert "def f" in result | 58 | assert "def f" in result |
| 58 | 59 | ||
| 59 | 60 | ||
| @@ -32,11 +32,11 @@ def _ir_to_str(prog: ir.Program) -> str: | |||
| 32 | 32 | ||
| 33 | 33 | ||
| 34 | def _parse_kernel(kernel_def) -> ir.Program: | 34 | def _parse_kernel(kernel_def) -> ir.Program: |
| 35 | - return kernel_def.parse_target_program(ir.SectionKind.Vector)[0] | 35 | + return kernel_def.to_kernel_def().parse_target_program(ir.SectionKind.Vector)[0] |
| 36 | 36 | ||
| 37 | 37 | ||
| 38 | def test_constant_mutex_lock_unlock_uses_dynamic_ir(): | 38 | def test_constant_mutex_lock_unlock_uses_dynamic_ir(): |
| 39 | - @pl.kernel | 39 | + @pl.jit |
| 40 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): | 40 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): |
| 41 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 41 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 42 | t = pl.make_tile(tt, addr=0, size=128) | 42 | t = pl.make_tile(tt, addr=0, size=128) |
| @@ -50,7 +50,7 @@ def test_constant_mutex_lock_unlock_uses_dynamic_ir(): | |||
| 50 | 50 | ||
| 51 | 51 | ||
| 52 | def test_keyword_form(): | 52 | def test_keyword_form(): |
| 53 | - @pl.kernel | 53 | + @pl.jit |
| 54 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): | 54 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): |
| 55 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 55 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 56 | t = pl.make_tile(tt, addr=0, size=128) | 56 | t = pl.make_tile(tt, addr=0, size=128) |
| @@ -64,7 +64,7 @@ def test_keyword_form(): | |||
| 64 | 64 | ||
| 65 | 65 | ||
| 66 | def test_contiguous_addr_auto_offset(): | 66 | def test_contiguous_addr_auto_offset(): |
| 67 | - @pl.kernel(auto_mutex=True) | 67 | + @pl.jit(auto_mutex=True) |
| 68 | def k(a: pl.Tensor[[128, 128], pl.DT_FP16]): | 68 | def k(a: pl.Tensor[[128, 128], pl.DT_FP16]): |
| 69 | tt = pl.TileType(shape=[128, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 69 | tt = pl.TileType(shape=[128, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 70 | db = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) | 70 | db = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) |
| @@ -80,7 +80,7 @@ def test_contiguous_addr_auto_offset(): | |||
| 80 | 80 | ||
| 81 | 81 | ||
| 82 | def test_discrete_addrs_list(): | 82 | def test_discrete_addrs_list(): |
| 83 | - @pl.kernel(auto_mutex=True) | 83 | + @pl.jit(auto_mutex=True) |
| 84 | def k(a: pl.Tensor[[128, 128], pl.DT_FP16]): | 84 | def k(a: pl.Tensor[[128, 128], pl.DT_FP16]): |
| 85 | tt = pl.TileType(shape=[128, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 85 | tt = pl.TileType(shape=[128, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 86 | db = pl.make_tile_group(type=tt, addrs=[0, 0x10000], mutex_ids=[0, 1]) | 86 | db = pl.make_tile_group(type=tt, addrs=[0, 0x10000], mutex_ids=[0, 1]) |
| @@ -97,7 +97,7 @@ def test_discrete_addrs_list(): | |||
| 97 | def test_addrs_from_kernel_local_constant(): | 97 | def test_addrs_from_kernel_local_constant(): |
| 98 | """addrs=/mutex_ids= accept kernel-local constants, not just literals.""" | 98 | """addrs=/mutex_ids= accept kernel-local constants, not just literals.""" |
| 99 | 99 | ||
| 100 | - @pl.kernel(auto_mutex=True) | 100 | + @pl.jit(auto_mutex=True) |
| 101 | def k(a: pl.Tensor[[128, 128], pl.DT_FP16]): | 101 | def k(a: pl.Tensor[[128, 128], pl.DT_FP16]): |
| 102 | tt = pl.TileType(shape=[128, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 102 | tt = pl.TileType(shape=[128, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 103 | base_addr = 0x10000 | 103 | base_addr = 0x10000 |
| @@ -114,7 +114,7 @@ def test_addrs_from_kernel_local_constant(): | |||
| 114 | 114 | ||
| 115 | 115 | ||
| 116 | def test_discrete_addrs_list_from_kernel_local_constant(): | 116 | def test_discrete_addrs_list_from_kernel_local_constant(): |
| 117 | - @pl.kernel(auto_mutex=True) | 117 | + @pl.jit(auto_mutex=True) |
| 118 | def k(a: pl.Tensor[[128, 128], pl.DT_FP16]): | 118 | def k(a: pl.Tensor[[128, 128], pl.DT_FP16]): |
| 119 | tt = pl.TileType(shape=[128, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 119 | tt = pl.TileType(shape=[128, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 120 | base_addr = 0x10000 | 120 | base_addr = 0x10000 |
| @@ -131,7 +131,7 @@ def test_discrete_addrs_list_from_kernel_local_constant(): | |||
| 131 | 131 | ||
| 132 | 132 | ||
| 133 | def test_runtime_addr_rejected_with_compile_time_hint(): | 133 | def test_runtime_addr_rejected_with_compile_time_hint(): |
| 134 | - @pl.kernel(auto_mutex=True) | 134 | + @pl.jit(auto_mutex=True) |
| 135 | def k(a: pl.Tensor[[pl.DYNAMIC, 128], pl.DT_FP16]): | 135 | def k(a: pl.Tensor[[pl.DYNAMIC, 128], pl.DT_FP16]): |
| 136 | tt = pl.TileType(shape=[128, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 136 | tt = pl.TileType(shape=[128, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 137 | addr = a.shape[0] * 2 | 137 | addr = a.shape[0] * 2 |
| @@ -147,7 +147,7 @@ def test_runtime_addr_rejected_with_compile_time_hint(): | |||
| 147 | def test_slot_stride_matches_a_standalone_tile_of_the_same_type(): | 147 | def test_slot_stride_matches_a_standalone_tile_of_the_same_type(): |
| 148 | """A group slot is exactly as wide as pl.make_tile() of the same TileType.""" | 148 | """A group slot is exactly as wide as pl.make_tile() of the same TileType.""" |
| 149 | 149 | ||
| 150 | - @pl.kernel(auto_mutex=True) | 150 | + @pl.jit(auto_mutex=True) |
| 151 | def k(a: pl.Tensor[[64, 96], pl.DT_FP32]): | 151 | def k(a: pl.Tensor[[64, 96], pl.DT_FP32]): |
| 152 | tt = pl.TileType(shape=[64, 96], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Mat) | 152 | tt = pl.TileType(shape=[64, 96], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Mat) |
| 153 | standalone = pl.make_tile(tt, addr=0x40000) | 153 | standalone = pl.make_tile(tt, addr=0x40000) |
| @@ -166,7 +166,7 @@ def test_slot_stride_matches_a_standalone_tile_of_the_same_type(): | |||
| 166 | def test_slot_stride_rounds_a_sub_byte_dtype_up_to_one_byte_per_element(): | 166 | def test_slot_stride_rounds_a_sub_byte_dtype_up_to_one_byte_per_element(): |
| 167 | """A 4-bit element cannot reserve half a byte, so each slot over-reserves.""" | 167 | """A 4-bit element cannot reserve half a byte, so each slot over-reserves.""" |
| 168 | 168 | ||
| 169 | - @pl.kernel(auto_mutex=True) | 169 | + @pl.jit(auto_mutex=True) |
| 170 | def k(a: pl.Tensor[[64, 64], pl.DT_FP16]): | 170 | def k(a: pl.Tensor[[64, 64], pl.DT_FP16]): |
| 171 | tt = pl.TileType(shape=[64, 64], dtype=pl.DT_INT4, target_memory=pl.MemorySpace.Mat) | 171 | tt = pl.TileType(shape=[64, 64], dtype=pl.DT_INT4, target_memory=pl.MemorySpace.Mat) |
| 172 | db = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) # noqa: F841 | 172 | db = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) # noqa: F841 |
| @@ -178,7 +178,7 @@ def test_slot_stride_rounds_a_sub_byte_dtype_up_to_one_byte_per_element(): | |||
| 178 | def test_tile_type_with_a_runtime_shape_rejected(): | 178 | def test_tile_type_with_a_runtime_shape_rejected(): |
| 179 | """The slot stride is a compile-time byte count, so the shape has to be one too.""" | 179 | """The slot stride is a compile-time byte count, so the shape has to be one too.""" |
| 180 | 180 | ||
| 181 | - @pl.kernel(auto_mutex=True) | 181 | + @pl.jit(auto_mutex=True) |
| 182 | def k(a: pl.Tensor[[pl.DYNAMIC, 128], pl.DT_FP16]): | 182 | def k(a: pl.Tensor[[pl.DYNAMIC, 128], pl.DT_FP16]): |
| 183 | # A tuple (not a list) skips the TileType compile-time check, so the | 183 | # A tuple (not a list) skips the TileType compile-time check, so the |
| 184 | # runtime dimension survives into the TileType. | 184 | # runtime dimension survives into the TileType. |
| @@ -191,7 +191,7 @@ def test_tile_type_with_a_runtime_shape_rejected(): | |||
| 191 | 191 | ||
| 192 | 192 | ||
| 193 | def test_auto_mutex_single_tile(): | 193 | def test_auto_mutex_single_tile(): |
| 194 | - @pl.kernel(auto_mutex=True) | 194 | + @pl.jit(auto_mutex=True) |
| 195 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): | 195 | def k(x: pl.Tensor[[1, 64], pl.DT_FP16]): |
| 196 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 196 | tt = pl.TileType(shape=[1, 64], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 197 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[3]) | 197 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[3]) |
| @@ -205,7 +205,7 @@ def test_auto_mutex_single_tile(): | |||
| 205 | 205 | ||
| 206 | 206 | ||
| 207 | def test_auto_mutex_group_loop(): | 207 | def test_auto_mutex_group_loop(): |
| 208 | - @pl.kernel(auto_mutex=True) | 208 | + @pl.jit(auto_mutex=True) |
| 209 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): | 209 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): |
| 210 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 210 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 211 | q_l1_db = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) | 211 | q_l1_db = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) |
| @@ -220,7 +220,7 @@ def test_auto_mutex_group_loop(): | |||
| 220 | 220 | ||
| 221 | 221 | ||
| 222 | def test_next_advances(): | 222 | def test_next_advances(): |
| 223 | - @pl.kernel(auto_mutex=True) | 223 | + @pl.jit(auto_mutex=True) |
| 224 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): | 224 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): |
| 225 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 225 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 226 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) | 226 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) |
| @@ -237,7 +237,7 @@ def test_next_advances(): | |||
| 237 | 237 | ||
| 238 | 238 | ||
| 239 | def test_current_does_not_advance(): | 239 | def test_current_does_not_advance(): |
| 240 | - @pl.kernel(auto_mutex=True) | 240 | + @pl.jit(auto_mutex=True) |
| 241 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): | 241 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): |
| 242 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 242 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 243 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) | 243 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) |
| @@ -253,7 +253,7 @@ def test_current_does_not_advance(): | |||
| 253 | 253 | ||
| 254 | 254 | ||
| 255 | def test_previous_no_advance(): | 255 | def test_previous_no_advance(): |
| 256 | - @pl.kernel(auto_mutex=True) | 256 | + @pl.jit(auto_mutex=True) |
| 257 | def k(gm_q: pl.Tensor[[64, 32], pl.DT_FP16]): | 257 | def k(gm_q: pl.Tensor[[64, 32], pl.DT_FP16]): |
| 258 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 258 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 259 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) | 259 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) |
| @@ -269,7 +269,7 @@ def test_previous_no_advance(): | |||
| 269 | 269 | ||
| 270 | 270 | ||
| 271 | def test_single_tile_group_fast_path(): | 271 | def test_single_tile_group_fast_path(): |
| 272 | - @pl.kernel(auto_mutex=True) | 272 | + @pl.jit(auto_mutex=True) |
| 273 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): | 273 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): |
| 274 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 274 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 275 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[5]) | 275 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[5]) |
| @@ -286,7 +286,7 @@ def test_single_tile_group_fast_path(): | |||
| 286 | 286 | ||
| 287 | 287 | ||
| 288 | def test_depth_creates_group_without_mutex_ids(): | 288 | def test_depth_creates_group_without_mutex_ids(): |
| 289 | - @pl.kernel(auto_mutex=True) | 289 | + @pl.jit(auto_mutex=True) |
| 290 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): | 290 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): |
| 291 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 291 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 292 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=None, depth=2) | 292 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=None, depth=2) |
| @@ -298,7 +298,7 @@ def test_depth_creates_group_without_mutex_ids(): | |||
| 298 | 298 | ||
| 299 | 299 | ||
| 300 | def test_depth_keyword_with_explicit_none_mutex_ids(): | 300 | def test_depth_keyword_with_explicit_none_mutex_ids(): |
| 301 | - @pl.kernel(auto_mutex=True) | 301 | + @pl.jit(auto_mutex=True) |
| 302 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): | 302 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): |
| 303 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 303 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 304 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=None, depth=2) | 304 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=None, depth=2) |
| @@ -310,7 +310,7 @@ def test_depth_keyword_with_explicit_none_mutex_ids(): | |||
| 310 | 310 | ||
| 311 | 311 | ||
| 312 | def test_depth_must_equal_mutex_ids_length(): | 312 | def test_depth_must_equal_mutex_ids_length(): |
| 313 | - @pl.kernel(auto_mutex=True) | 313 | + @pl.jit(auto_mutex=True) |
| 314 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): | 314 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): |
| 315 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 315 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 316 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1], depth=3) | 316 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1], depth=3) |
| @@ -328,7 +328,7 @@ def test_depth_must_equal_mutex_ids_length(): | |||
| 328 | def test_subscript_const_index_materializes_mutex_companions(): | 328 | def test_subscript_const_index_materializes_mutex_companions(): |
| 329 | """A literal index keeps its slot while mutex IDs use companion variables.""" | 329 | """A literal index keeps its slot while mutex IDs use companion variables.""" |
| 330 | 330 | ||
| 331 | - @pl.kernel(auto_mutex=True) | 331 | + @pl.jit(auto_mutex=True) |
| 332 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): | 332 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): |
| 333 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 333 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 334 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[6, 7]) | 334 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[6, 7]) |
| @@ -345,7 +345,7 @@ def test_subscript_const_index_materializes_mutex_companions(): | |||
| 345 | def test_subscript_negative_index_rejected(): | 345 | def test_subscript_negative_index_rejected(): |
| 346 | """Negative indices are outside the supported [0, depth) interval.""" | 346 | """Negative indices are outside the supported [0, depth) interval.""" |
| 347 | 347 | ||
| 348 | - @pl.kernel(auto_mutex=True) | 348 | + @pl.jit(auto_mutex=True) |
| 349 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): | 349 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): |
| 350 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 350 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 351 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[6, 7, 8]) | 351 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[6, 7, 8]) |
| @@ -358,7 +358,7 @@ def test_subscript_negative_index_rejected(): | |||
| 358 | def test_subscript_does_not_touch_cursor(): | 358 | def test_subscript_does_not_touch_cursor(): |
| 359 | """Subscript is random access: the rotation state stays where next() left it.""" | 359 | """Subscript is random access: the rotation state stays where next() left it.""" |
| 360 | 360 | ||
| 361 | - @pl.kernel(auto_mutex=True) | 361 | + @pl.jit(auto_mutex=True) |
| 362 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): | 362 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): |
| 363 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 363 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 364 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) | 364 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) |
| @@ -377,7 +377,7 @@ def test_subscript_does_not_touch_cursor(): | |||
| 377 | def test_subscript_dynamic_index_is_not_implicitly_wrapped(): | 377 | def test_subscript_dynamic_index_is_not_implicitly_wrapped(): |
| 378 | """A runtime index is used unchanged and locked through the dyn if-chain.""" | 378 | """A runtime index is used unchanged and locked through the dyn if-chain.""" |
| 379 | 379 | ||
| 380 | - @pl.kernel(auto_mutex=True) | 380 | + @pl.jit(auto_mutex=True) |
| 381 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): | 381 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): |
| 382 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 382 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 383 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) | 383 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) |
| @@ -396,7 +396,7 @@ def test_subscript_dynamic_index_is_not_implicitly_wrapped(): | |||
| 396 | def test_subscript_dynamic_index_expression(): | 396 | def test_subscript_dynamic_index_expression(): |
| 397 | """The index may be any scalar expression, e.g. a look-ahead slot.""" | 397 | """The index may be any scalar expression, e.g. a look-ahead slot.""" |
| 398 | 398 | ||
| 399 | - @pl.kernel(auto_mutex=True) | 399 | + @pl.jit(auto_mutex=True) |
| 400 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): | 400 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): |
| 401 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 401 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 402 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) | 402 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) |
| @@ -413,7 +413,7 @@ def test_subscript_dynamic_index_expression(): | |||
| 413 | def test_subscript_single_tile_group_requires_bounded_index(): | 413 | def test_subscript_single_tile_group_requires_bounded_index(): |
| 414 | """A dynamic index for a one-slot group must explicitly reduce to zero.""" | 414 | """A dynamic index for a one-slot group must explicitly reduce to zero.""" |
| 415 | 415 | ||
| 416 | - @pl.kernel(auto_mutex=True) | 416 | + @pl.jit(auto_mutex=True) |
| 417 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): | 417 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): |
| 418 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 418 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 419 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[5]) | 419 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[5]) |
| @@ -433,7 +433,7 @@ def test_subscript_two_slots_in_one_op_dedup(): | |||
| 433 | ids go into one mutex_lock_dyn whose codegen guards the second with `!=`. | 433 | ids go into one mutex_lock_dyn whose codegen guards the second with `!=`. |
| 434 | """ | 434 | """ |
| 435 | 435 | ||
| 436 | - @pl.kernel(auto_mutex=True) | 436 | + @pl.jit(auto_mutex=True) |
| 437 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): | 437 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): |
| 438 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 438 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 439 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[10, 11]) | 439 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[10, 11]) |
| @@ -449,7 +449,7 @@ def test_subscript_two_slots_in_one_op_dedup(): | |||
| 449 | def test_subscript_mixes_with_next(): | 449 | def test_subscript_mixes_with_next(): |
| 450 | """Subscript and cursor access address the same slots of the same group.""" | 450 | """Subscript and cursor access address the same slots of the same group.""" |
| 451 | 451 | ||
| 452 | - @pl.kernel(auto_mutex=True) | 452 | + @pl.jit(auto_mutex=True) |
| 453 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): | 453 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): |
| 454 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 454 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 455 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) | 455 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) |
| @@ -464,7 +464,7 @@ def test_subscript_mixes_with_next(): | |||
| 464 | 464 | ||
| 465 | 465 | ||
| 466 | def test_subscript_const_index_out_of_range(): | 466 | def test_subscript_const_index_out_of_range(): |
| 467 | - @pl.kernel(auto_mutex=True) | 467 | + @pl.jit(auto_mutex=True) |
| 468 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): | 468 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): |
| 469 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 469 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 470 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) | 470 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) |
| @@ -475,7 +475,7 @@ def test_subscript_const_index_out_of_range(): | |||
| 475 | 475 | ||
| 476 | 476 | ||
| 477 | def test_subscript_slice_rejected(): | 477 | def test_subscript_slice_rejected(): |
| 478 | - @pl.kernel(auto_mutex=True) | 478 | + @pl.jit(auto_mutex=True) |
| 479 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): | 479 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): |
| 480 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 480 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 481 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) | 481 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) |
| @@ -486,7 +486,7 @@ def test_subscript_slice_rejected(): | |||
| 486 | 486 | ||
| 487 | 487 | ||
| 488 | def test_subscript_multi_dim_index_rejected(): | 488 | def test_subscript_multi_dim_index_rejected(): |
| 489 | - @pl.kernel(auto_mutex=True) | 489 | + @pl.jit(auto_mutex=True) |
| 490 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): | 490 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): |
| 491 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 491 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 492 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) | 492 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) |
| @@ -504,7 +504,7 @@ def test_subscript_multi_dim_index_rejected(): | |||
| 504 | def test_dynamic_slot_assignment_does_not_leak_from_branch(): | 504 | def test_dynamic_slot_assignment_does_not_leak_from_branch(): |
| 505 | """A branch-local tile assignment does not replace the outer tile binding.""" | 505 | """A branch-local tile assignment does not replace the outer tile binding.""" |
| 506 | 506 | ||
| 507 | - @pl.kernel(auto_mutex=True) | 507 | + @pl.jit(auto_mutex=True) |
| 508 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16], n: pl.DT_INT32): | 508 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16], n: pl.DT_INT32): |
| 509 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 509 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 510 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) | 510 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) |
| @@ -520,7 +520,7 @@ def test_dynamic_slot_assignment_does_not_leak_from_branch(): | |||
| 520 | 520 | ||
| 521 | 521 | ||
| 522 | def test_dynamic_slot_assignment_does_not_leak_from_loop(): | 522 | def test_dynamic_slot_assignment_does_not_leak_from_loop(): |
| 523 | - @pl.kernel(auto_mutex=True) | 523 | + @pl.jit(auto_mutex=True) |
| 524 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): | 524 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): |
| 525 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 525 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 526 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) | 526 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) |
| @@ -538,7 +538,7 @@ def test_dynamic_slot_assignment_does_not_leak_from_loop(): | |||
| 538 | def test_const_slot_assignment_merges_from_branch(): | 538 | def test_const_slot_assignment_merges_from_branch(): |
| 539 | """A reassigned tile and its mutex ID are merged across control flow.""" | 539 | """A reassigned tile and its mutex ID are merged across control flow.""" |
| 540 | 540 | ||
| 541 | - @pl.kernel(auto_mutex=True) | 541 | + @pl.jit(auto_mutex=True) |
| 542 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16], n: pl.DT_INT32): | 542 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16], n: pl.DT_INT32): |
| 543 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 543 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 544 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) | 544 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) |
| @@ -554,7 +554,7 @@ def test_const_slot_assignment_merges_from_branch(): | |||
| 554 | 554 | ||
| 555 | 555 | ||
| 556 | def test_dynamic_slot_used_inside_its_own_block_is_fine(): | 556 | def test_dynamic_slot_used_inside_its_own_block_is_fine(): |
| 557 | - @pl.kernel(auto_mutex=True) | 557 | + @pl.jit(auto_mutex=True) |
| 558 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16], n: pl.DT_INT32): | 558 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16], n: pl.DT_INT32): |
| 559 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 559 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 560 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) | 560 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) |
| @@ -569,7 +569,7 @@ def test_dynamic_slot_used_inside_its_own_block_is_fine(): | |||
| 569 | def test_dynamic_slot_selected_outside_and_used_inside_a_loop(): | 569 | def test_dynamic_slot_selected_outside_and_used_inside_a_loop(): |
| 570 | """An enclosing block dominates the loop body, so the index is still in scope.""" | 570 | """An enclosing block dominates the loop body, so the index is still in scope.""" |
| 571 | 571 | ||
| 572 | - @pl.kernel(auto_mutex=True) | 572 | + @pl.jit(auto_mutex=True) |
| 573 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): | 573 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): |
| 574 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 574 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 575 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) | 575 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) |
| @@ -584,7 +584,7 @@ def test_dynamic_slot_selected_outside_and_used_inside_a_loop(): | |||
| 584 | def test_next_inside_loop_still_accepted(): | 584 | def test_next_inside_loop_still_accepted(): |
| 585 | """A cursor-selected tile remains usable inside the loop scope that defines it.""" | 585 | """A cursor-selected tile remains usable inside the loop scope that defines it.""" |
| 586 | 586 | ||
| 587 | - @pl.kernel(auto_mutex=True) | 587 | + @pl.jit(auto_mutex=True) |
| 588 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): | 588 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): |
| 589 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 589 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 590 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) | 590 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) |
| @@ -605,7 +605,7 @@ def test_subscript_inside_inline_helper(): | |||
| 605 | def take(group, idx): | 605 | def take(group, idx): |
| 606 | return group[idx] | 606 | return group[idx] |
| 607 | 607 | ||
| 608 | - @pl.kernel(auto_mutex=True) | 608 | + @pl.jit(auto_mutex=True) |
| 609 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): | 609 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): |
| 610 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 610 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 611 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) | 611 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1]) |
| @@ -623,7 +623,7 @@ def test_set_validshape_covers_every_tile_of_the_group(): | |||
| 623 | set_validshape per slot, not just for slot 0. | 623 | set_validshape per slot, not just for slot 0. |
| 624 | """ | 624 | """ |
| 625 | 625 | ||
| 626 | - @pl.kernel(auto_mutex=True) | 626 | + @pl.jit(auto_mutex=True) |
| 627 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): | 627 | def k(gm_q: pl.Tensor[[128, 32], pl.DT_FP16]): |
| 628 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 628 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 629 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1, 2, 3]) | 629 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1, 2, 3]) |
| @@ -640,7 +640,7 @@ def test_set_validshape_covers_every_tile_of_the_group(): | |||
| 640 | 640 | ||
| 641 | 641 | ||
| 642 | def test_const_multi_mutex_ids_use_one_dedup_op(): | 642 | def test_const_multi_mutex_ids_use_one_dedup_op(): |
| 643 | - @pl.kernel(auto_mutex=True) | 643 | + @pl.jit(auto_mutex=True) |
| 644 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): | 644 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16]): |
| 645 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 645 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 646 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[[2, 3]]) | 646 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[[2, 3]]) |
| @@ -654,7 +654,7 @@ def test_const_multi_mutex_ids_use_one_dedup_op(): | |||
| 654 | 654 | ||
| 655 | 655 | ||
| 656 | def test_dynamic_multi_mutex_ids_use_one_dedup_op(): | 656 | def test_dynamic_multi_mutex_ids_use_one_dedup_op(): |
| 657 | - @pl.kernel(auto_mutex=True) | 657 | + @pl.jit(auto_mutex=True) |
| 658 | def k(gm_q: pl.Tensor[[96, 32], pl.DT_FP16]): | 658 | def k(gm_q: pl.Tensor[[96, 32], pl.DT_FP16]): |
| 659 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 659 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 660 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[[0, 1], [2, 3], [4, 5]]) | 660 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[[0, 1], [2, 3], [4, 5]]) |
| @@ -669,7 +669,7 @@ def test_dynamic_multi_mutex_ids_use_one_dedup_op(): | |||
| 669 | 669 | ||
| 670 | 670 | ||
| 671 | def test_single_and_multi_id_groups_can_share_one_op(): | 671 | def test_single_and_multi_id_groups_can_share_one_op(): |
| 672 | - @pl.kernel(auto_mutex=True) | 672 | + @pl.jit(auto_mutex=True) |
| 673 | def k(gm_q: pl.Tensor[[96, 32], pl.DT_FP16]): | 673 | def k(gm_q: pl.Tensor[[96, 32], pl.DT_FP16]): |
| 674 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 674 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 675 | g1 = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1, 2]) | 675 | g1 = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, 1, 2]) |
| @@ -683,7 +683,7 @@ def test_single_and_multi_id_groups_can_share_one_op(): | |||
| 683 | 683 | ||
| 684 | 684 | ||
| 685 | def test_multi_mutex_ids_survive_ternary_merge(): | 685 | def test_multi_mutex_ids_survive_ternary_merge(): |
| 686 | - @pl.kernel(auto_mutex=True) | 686 | + @pl.jit(auto_mutex=True) |
| 687 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16], choose: pl.DT_INT32): | 687 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16], choose: pl.DT_INT32): |
| 688 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 688 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 689 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[[0, 1], [2, 3]]) | 689 | g = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[[0, 1], [2, 3]]) |
| @@ -697,7 +697,7 @@ def test_multi_mutex_ids_survive_ternary_merge(): | |||
| 697 | 697 | ||
| 698 | 698 | ||
| 699 | def test_same_name_control_flow_rejects_different_mutex_id_counts(): | 699 | def test_same_name_control_flow_rejects_different_mutex_id_counts(): |
| 700 | - @pl.kernel(auto_mutex=True) | 700 | + @pl.jit(auto_mutex=True) |
| 701 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16], choose: pl.DT_INT32): | 701 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16], choose: pl.DT_INT32): |
| 702 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 702 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 703 | g1 = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0]) | 703 | g1 = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0]) |
| @@ -715,7 +715,7 @@ def test_same_name_control_flow_rejects_different_mutex_id_counts(): | |||
| 715 | 715 | ||
| 716 | 716 | ||
| 717 | def test_same_name_control_flow_merges_equal_mutex_id_counts(): | 717 | def test_same_name_control_flow_merges_equal_mutex_id_counts(): |
| 718 | - @pl.kernel(auto_mutex=True) | 718 | + @pl.jit(auto_mutex=True) |
| 719 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16], choose: pl.DT_INT32): | 719 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16], choose: pl.DT_INT32): |
| 720 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 720 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 721 | g1 = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[[0, 1]]) | 721 | g1 = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[[0, 1]]) |
| @@ -732,7 +732,7 @@ def test_same_name_control_flow_merges_equal_mutex_id_counts(): | |||
| 732 | 732 | ||
| 733 | 733 | ||
| 734 | def test_ternary_rejects_different_mutex_id_counts(): | 734 | def test_ternary_rejects_different_mutex_id_counts(): |
| 735 | - @pl.kernel(auto_mutex=True) | 735 | + @pl.jit(auto_mutex=True) |
| 736 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16], choose: pl.DT_INT32): | 736 | def k(gm_q: pl.Tensor[[32, 32], pl.DT_FP16], choose: pl.DT_INT32): |
| 737 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 737 | tt = pl.TileType(shape=[32, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 738 | g1 = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0]) | 738 | g1 = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0]) |
| @@ -748,7 +748,7 @@ def test_ternary_rejects_different_mutex_id_counts(): | |||
| 748 | 748 | ||
| 749 | 749 | ||
| 750 | def test_make_tile_group_rejects_different_mutex_id_counts(): | 750 | def test_make_tile_group_rejects_different_mutex_id_counts(): |
| 751 | - @pl.kernel(auto_mutex=True) | 751 | + @pl.jit(auto_mutex=True) |
| 752 | def k(gm_q: pl.Tensor[[1, 64], pl.DT_FP16]): | 752 | def k(gm_q: pl.Tensor[[1, 64], pl.DT_FP16]): |
| 753 | tt = pl.TileType(shape=[1, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 753 | tt = pl.TileType(shape=[1, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 754 | pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, [1, 2]]) | 754 | pl.make_tile_group(type=tt, addrs=0, mutex_ids=[0, [1, 2]]) |
| @@ -761,7 +761,7 @@ def test_make_tile_group_rejects_different_mutex_id_counts(): | |||
| 761 | 761 | ||
| 762 | 762 | ||
| 763 | def test_make_tile_group_rejects_duplicate_id_for_one_tile(): | 763 | def test_make_tile_group_rejects_duplicate_id_for_one_tile(): |
| 764 | - @pl.kernel(auto_mutex=True) | 764 | + @pl.jit(auto_mutex=True) |
| 765 | def k(gm_q: pl.Tensor[[1, 32], pl.DT_FP16]): | 765 | def k(gm_q: pl.Tensor[[1, 32], pl.DT_FP16]): |
| 766 | tt = pl.TileType(shape=[1, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 766 | tt = pl.TileType(shape=[1, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 767 | pl.make_tile_group(type=tt, addrs=0, mutex_ids=[[2, 2]]) | 767 | pl.make_tile_group(type=tt, addrs=0, mutex_ids=[[2, 2]]) |
| @@ -772,7 +772,7 @@ def test_make_tile_group_rejects_duplicate_id_for_one_tile(): | |||
| 772 | 772 | ||
| 773 | @pytest.mark.parametrize("mutex_ids", [[True], [[0, False]]]) | 773 | @pytest.mark.parametrize("mutex_ids", [[True], [[0, False]]]) |
| 774 | def test_make_tile_group_rejects_bool_mutex_id(mutex_ids): | 774 | def test_make_tile_group_rejects_bool_mutex_id(mutex_ids): |
| 775 | - @pl.kernel(auto_mutex=True) | 775 | + @pl.jit(auto_mutex=True) |
| 776 | def k(gm_q: pl.Tensor[[32], pl.DT_FP16]): | 776 | def k(gm_q: pl.Tensor[[32], pl.DT_FP16]): |
| 777 | tt = pl.TileType(shape=[32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 777 | tt = pl.TileType(shape=[32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 778 | pl.make_tile_group(type=tt, addrs=0, mutex_ids=mutex_ids) | 778 | pl.make_tile_group(type=tt, addrs=0, mutex_ids=mutex_ids) |
| @@ -783,7 +783,7 @@ def test_make_tile_group_rejects_bool_mutex_id(mutex_ids): | |||
| 783 | 783 | ||
| 784 | @pytest.mark.parametrize("mutex_ids", [1, {0, 1}], ids=["integer", "set"]) | 784 | @pytest.mark.parametrize("mutex_ids", [1, {0, 1}], ids=["integer", "set"]) |
| 785 | def test_make_tile_group_rejects_wrong_mutex_ids_container_type(mutex_ids): | 785 | def test_make_tile_group_rejects_wrong_mutex_ids_container_type(mutex_ids): |
| 786 | - @pl.kernel(auto_mutex=True) | 786 | + @pl.jit(auto_mutex=True) |
| 787 | def k(gm_q: pl.Tensor[[32], pl.DT_FP16]): | 787 | def k(gm_q: pl.Tensor[[32], pl.DT_FP16]): |
| 788 | tt = pl.TileType(shape=[32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 788 | tt = pl.TileType(shape=[32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 789 | pl.make_tile_group(type=tt, addrs=0, mutex_ids=mutex_ids) | 789 | pl.make_tile_group(type=tt, addrs=0, mutex_ids=mutex_ids) |
| @@ -11,7 +11,7 @@ | |||
| 11 | """Unit tests for closure variable resolution in DSL function bodies (issue #276). | 11 | """Unit tests for closure variable resolution in DSL function bodies (issue #276). |
| 12 | 12 | ||
| 13 | Verifies that Python globals/closure variables used as positional arguments | 13 | Verifies that Python globals/closure variables used as positional arguments |
| 14 | -in function calls inside @pl.function bodies are resolved correctly. | 14 | +in function calls inside kernel bodies are resolved correctly. |
| 15 | """ | 15 | """ |
| 16 | 16 | ||
| 17 | from pypto_pro import ir | 17 | from pypto_pro import ir |
| @@ -25,15 +25,18 @@ def test_list_closure_var_as_positional_arg(): | |||
| 25 | offset_value = [0, 0] | 25 | offset_value = [0, 0] |
| 26 | tile_shape = [64, 64] | 26 | tile_shape = [64, 64] |
| 27 | 27 | ||
| 28 | - @pl.function | 28 | + @pl.jit(auto_mutex=False) |
| 29 | def func( | 29 | def func( |
| 30 | t: pl.Tensor[[128, 128], pl.DT_FP32], out: pl.Tensor[[128, 128], pl.DT_FP32] | 30 | t: pl.Tensor[[128, 128], pl.DT_FP32], out: pl.Tensor[[128, 128], pl.DT_FP32] |
| 31 | - ) -> pl.Tensor[[128, 128], pl.DT_FP32]: | 31 | + ): |
| 32 | tile_type = pl.TileType(shape=tile_shape, dtype=pl.DT_FP32) | 32 | tile_type = pl.TileType(shape=tile_shape, dtype=pl.DT_FP32) |
| 33 | a = pl.make_tile(tile_type, addr=0, size=16384) | 33 | a = pl.make_tile(tile_type, addr=0, size=16384) |
| 34 | pl.load(a, t, offset_value) | 34 | pl.load(a, t, offset_value) |
| 35 | result: pl.Tensor[[128, 128], pl.DT_FP32] = pl.store(out, a, offset_value) | 35 | result: pl.Tensor[[128, 128], pl.DT_FP32] = pl.store(out, a, offset_value) |
| 36 | - return result | 36 | + _test_result = result |
| 37 | + | ||
| 38 | + func_program, _ = func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 39 | + func = func_program.get_function(func.__name__) | ||
| 37 | 40 | ||
| 38 | assert isinstance(func, ir.Function) | 41 | assert isinstance(func, ir.Function) |
| 39 | 42 | ||
| @@ -42,10 +45,13 @@ def test_int_closure_var_as_positional_arg(): | |||
| 42 | """Int closure variable resolves to ConstInt in function body.""" | 45 | """Int closure variable resolves to ConstInt in function body.""" |
| 43 | axis_value = 1 | 46 | axis_value = 1 |
| 44 | 47 | ||
| 45 | - @pl.function | 48 | + @pl.jit(auto_mutex=False) |
| 46 | - def func(x: pl.Tensor[[64, 128], pl.DT_FP32]) -> pl.Tensor[[128, 64], pl.DT_FP32]: | 49 | + def func(x: pl.Tensor[[64, 128], pl.DT_FP32]): |
| 47 | result: pl.Tensor[[128, 64], pl.DT_FP32] = pl.tensor.transpose(x, axis1=0, axis2=axis_value) | 50 | result: pl.Tensor[[128, 64], pl.DT_FP32] = pl.tensor.transpose(x, axis1=0, axis2=axis_value) |
| 48 | - return result | 51 | + _test_result = result |
| 52 | + | ||
| 53 | + func_program, _ = func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 54 | + func = func_program.get_function(func.__name__) | ||
| 49 | 55 | ||
| 50 | assert isinstance(func, ir.Function) | 56 | assert isinstance(func, ir.Function) |
| 51 | 57 | ||
| @@ -54,10 +60,13 @@ def test_float_closure_var_as_positional_arg(): | |||
| 54 | """Float closure variable resolves to ConstFloat in function body.""" | 60 | """Float closure variable resolves to ConstFloat in function body.""" |
| 55 | scale_value = 2.0 | 61 | scale_value = 2.0 |
| 56 | 62 | ||
| 57 | - @pl.function | 63 | + @pl.jit(auto_mutex=False) |
| 58 | - def func(x: pl.Tensor[[64], pl.DT_FP32]) -> pl.Tensor[[64], pl.DT_FP32]: | 64 | + def func(x: pl.Tensor[[64], pl.DT_FP32]): |
| 59 | result: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.mul(x, scale_value) | 65 | result: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.mul(x, scale_value) |
| 60 | - return result | 66 | + _test_result = result |
| 67 | + | ||
| 68 | + func_program, _ = func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 69 | + func = func_program.get_function(func.__name__) | ||
| 61 | 70 | ||
| 62 | assert isinstance(func, ir.Function) | 71 | assert isinstance(func, ir.Function) |
| 63 | 72 | ||
| @@ -66,10 +75,13 @@ def test_bool_closure_var_as_positional_arg(): | |||
| 66 | """Bool closure variable resolves to ConstBool in function body.""" | 75 | """Bool closure variable resolves to ConstBool in function body.""" |
| 67 | flag_value = True | 76 | flag_value = True |
| 68 | 77 | ||
| 69 | - @pl.function | 78 | + @pl.jit(auto_mutex=False) |
| 70 | - def func(x: pl.Tensor[[64], pl.DT_FP32]) -> pl.Tensor[[64], pl.DT_FP32]: | 79 | + def func(x: pl.Tensor[[64], pl.DT_FP32]): |
| 71 | result: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.mul(x, flag_value) | 80 | result: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.mul(x, flag_value) |
| 72 | - return result | 81 | + _test_result = result |
| 82 | + | ||
| 83 | + func_program, _ = func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 84 | + func = func_program.get_function(func.__name__) | ||
| 73 | 85 | ||
| 74 | assert isinstance(func, ir.Function) | 86 | assert isinstance(func, ir.Function) |
| 75 | 87 | ||
| @@ -79,15 +91,18 @@ def test_tuple_closure_var_as_positional_arg(): | |||
| 79 | offset_value = (0, 0) | 91 | offset_value = (0, 0) |
| 80 | tile_shape = (64, 64) | 92 | tile_shape = (64, 64) |
| 81 | 93 | ||
| 82 | - @pl.function | 94 | + @pl.jit(auto_mutex=False) |
| 83 | def func( | 95 | def func( |
| 84 | t: pl.Tensor[[128, 128], pl.DT_FP32], out: pl.Tensor[[128, 128], pl.DT_FP32] | 96 | t: pl.Tensor[[128, 128], pl.DT_FP32], out: pl.Tensor[[128, 128], pl.DT_FP32] |
| 85 | - ) -> pl.Tensor[[128, 128], pl.DT_FP32]: | 97 | + ): |
| 86 | tile_type = pl.TileType(shape=tile_shape, dtype=pl.DT_FP32) | 98 | tile_type = pl.TileType(shape=tile_shape, dtype=pl.DT_FP32) |
| 87 | a = pl.make_tile(tile_type, addr=0, size=16384) | 99 | a = pl.make_tile(tile_type, addr=0, size=16384) |
| 88 | pl.load(a, t, offset_value) | 100 | pl.load(a, t, offset_value) |
| 89 | result: pl.Tensor[[128, 128], pl.DT_FP32] = pl.store(out, a, offset_value) | 101 | result: pl.Tensor[[128, 128], pl.DT_FP32] = pl.store(out, a, offset_value) |
| 90 | - return result | 102 | + _test_result = result |
| 103 | + | ||
| 104 | + func_program, _ = func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 105 | + func = func_program.get_function(func.__name__) | ||
| 91 | 106 | ||
| 92 | assert isinstance(func, ir.Function) | 107 | assert isinstance(func, ir.Function) |
| 93 | 108 | ||
| @@ -95,10 +110,13 @@ def test_tuple_closure_var_as_positional_arg(): | |||
| 95 | def test_closure_tuple_has_entry_anchor_and_folded_reads(): | 110 | def test_closure_tuple_has_entry_anchor_and_folded_reads(): |
| 96 | values = (3, 7) | 111 | values = (3, 7) |
| 97 | 112 | ||
| 98 | - @pl.function | 113 | + @pl.jit(auto_mutex=False) |
| 99 | def func(idx: pl.DT_INT64): | 114 | def func(idx: pl.DT_INT64): |
| 100 | selected = values[idx] # noqa: F841 | 115 | selected = values[idx] # noqa: F841 |
| 101 | 116 | ||
| 117 | + func_program, _ = func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 118 | + func = func_program.get_function(func.__name__) | ||
| 119 | + | ||
| 102 | assignments = [stmt for stmt in func.body.stmts if isinstance(stmt, ir.AssignStmt)] | 120 | assignments = [stmt for stmt in func.body.stmts if isinstance(stmt, ir.AssignStmt)] |
| 103 | anchor = next(stmt for stmt in assignments if stmt.var.name == "values") | 121 | anchor = next(stmt for stmt in assignments if stmt.var.name == "values") |
| 104 | selected = next(stmt for stmt in assignments if stmt.var.name == "selected") | 122 | selected = next(stmt for stmt in assignments if stmt.var.name == "selected") |
| @@ -111,27 +129,33 @@ def test_nested_list_closure_var(): | |||
| 111 | """Nested list closure variable recursively converts to nested MakeTuple.""" | 129 | """Nested list closure variable recursively converts to nested MakeTuple.""" |
| 112 | offsets_value = [0, 0] | 130 | offsets_value = [0, 0] |
| 113 | 131 | ||
| 114 | - @pl.function | 132 | + @pl.jit(auto_mutex=False) |
| 115 | def func( | 133 | def func( |
| 116 | t: pl.Tensor[[128, 128], pl.DT_FP32], out: pl.Tensor[[128, 128], pl.DT_FP32] | 134 | t: pl.Tensor[[128, 128], pl.DT_FP32], out: pl.Tensor[[128, 128], pl.DT_FP32] |
| 117 | - ) -> pl.Tensor[[128, 128], pl.DT_FP32]: | 135 | + ): |
| 118 | tile_type = pl.TileType(shape=[64, 64], dtype=pl.DT_FP32) | 136 | tile_type = pl.TileType(shape=[64, 64], dtype=pl.DT_FP32) |
| 119 | a = pl.make_tile(tile_type, addr=0, size=16384) | 137 | a = pl.make_tile(tile_type, addr=0, size=16384) |
| 120 | pl.load(a, t, offsets_value) # type: ignore[arg-type] | 138 | pl.load(a, t, offsets_value) # type: ignore[arg-type] |
| 121 | result: pl.Tensor[[128, 128], pl.DT_FP32] = pl.store(out, a, [0, 0]) | 139 | result: pl.Tensor[[128, 128], pl.DT_FP32] = pl.store(out, a, [0, 0]) |
| 122 | - return result | 140 | + _test_result = result |
| 141 | + | ||
| 142 | + func_program, _ = func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 143 | + func = func_program.get_function(func.__name__) | ||
| 123 | 144 | ||
| 124 | assert isinstance(func, ir.Function) | 145 | assert isinstance(func, ir.Function) |
| 125 | 146 | ||
| 126 | 147 | ||
| 127 | def test_dynamic_tensor_shape_value(): | 148 | def test_dynamic_tensor_shape_value(): |
| 128 | """A DYNAMIC parameter dimension is read through tensor.shape.""" | 149 | """A DYNAMIC parameter dimension is read through tensor.shape.""" |
| 129 | - @pl.function | 150 | + @pl.jit(auto_mutex=False) |
| 130 | def func( | 151 | def func( |
| 131 | x: pl.Tensor[[pl.DYNAMIC], pl.DT_FP32], | 152 | x: pl.Tensor[[pl.DYNAMIC], pl.DT_FP32], |
| 132 | ): | 153 | ): |
| 133 | result = pl.tensor.mul(x, x.shape[0]) | 154 | result = pl.tensor.mul(x, x.shape[0]) |
| 134 | - return result | 155 | + _test_result = result |
| 156 | + | ||
| 157 | + func_program, _ = func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 158 | + func = func_program.get_function(func.__name__) | ||
| 135 | 159 | ||
| 136 | assert isinstance(func, ir.Function) | 160 | assert isinstance(func, ir.Function) |
| 137 | 161 | ||
| @@ -140,11 +164,14 @@ def test_dsl_scope_shadows_closure(): | |||
| 140 | """Variable defined in DSL body shadows same-named closure variable.""" | 164 | """Variable defined in DSL body shadows same-named closure variable.""" |
| 141 | x_scale = 999.0 # noqa: F841 -deliberately shadowed by DSL assignment | 165 | x_scale = 999.0 # noqa: F841 -deliberately shadowed by DSL assignment |
| 142 | 166 | ||
| 143 | - @pl.function | 167 | + @pl.jit(auto_mutex=False) |
| 144 | - def func(x: pl.Tensor[[64], pl.DT_FP32]) -> pl.Tensor[[64], pl.DT_FP32]: | 168 | + def func(x: pl.Tensor[[64], pl.DT_FP32]): |
| 145 | x_scale: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, x) | 169 | x_scale: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, x) |
| 146 | result: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.mul(x_scale, x) | 170 | result: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.mul(x_scale, x) |
| 147 | - return result | 171 | + _test_result = result |
| 172 | + | ||
| 173 | + func_program, _ = func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 174 | + func = func_program.get_function(func.__name__) | ||
| 148 | 175 | ||
| 149 | assert isinstance(func, ir.Function) | 176 | assert isinstance(func, ir.Function) |
| 150 | 177 | ||
| @@ -153,10 +180,12 @@ def test_undefined_variable_still_raises(): | |||
| 153 | """Variable not in scope or closure raises UndefinedVariableError.""" | 180 | """Variable not in scope or closure raises UndefinedVariableError.""" |
| 154 | with pytest.raises(UndefinedVariableError, match="Undefined variable"): | 181 | with pytest.raises(UndefinedVariableError, match="Undefined variable"): |
| 155 | 182 | ||
| 156 | - @pl.function | 183 | + @pl.jit(auto_mutex=False) |
| 157 | - def func(x: pl.Tensor[[64], pl.DT_FP32]) -> pl.Tensor[[64], pl.DT_FP32]: | 184 | + def func(x: pl.Tensor[[64], pl.DT_FP32]): |
| 158 | result: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, totally_undefined) # noqa: F821 # type: ignore | 185 | result: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, totally_undefined) # noqa: F821 # type: ignore |
| 159 | - return result | 186 | + _test_result = result |
| 187 | + | ||
| 188 | + func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 160 | 189 | ||
| 161 | 190 | ||
| 162 | def test_unsupported_closure_type_raises(): | 191 | def test_unsupported_closure_type_raises(): |
| @@ -165,7 +194,9 @@ def test_unsupported_closure_type_raises(): | |||
| 165 | 194 | ||
| 166 | with pytest.raises(ParserTypeError, match="Unsupported closure variable type: str"): | 195 | with pytest.raises(ParserTypeError, match="Unsupported closure variable type: str"): |
| 167 | 196 | ||
| 168 | - @pl.function | 197 | + @pl.jit(auto_mutex=False) |
| 169 | - def func(x: pl.Tensor[[64], pl.DT_FP32]) -> pl.Tensor[[64], pl.DT_FP32]: | 198 | + def func(x: pl.Tensor[[64], pl.DT_FP32]): |
| 170 | result: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, bad_value) # type: ignore[arg-type] | 199 | result: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, bad_value) # type: ignore[arg-type] |
| 171 | - return result | 200 | + _test_result = result |
| 201 | + | ||
| 202 | + func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| @@ -41,8 +41,8 @@ def _assert_constant(expr, expected, dtype): | |||
| 41 | 41 | ||
| 42 | 42 | ||
| 43 | def test_all_binary_and_comparison_operators_fold(): | 43 | def test_all_binary_and_comparison_operators_fold(): |
| 44 | - @pl.function(type=pl.FunctionType.Opaque) | 44 | + @pl.jit(auto_mutex=False) |
| 45 | - def folded_ops(): | 45 | + def folded_ops(_jit_entry: pl.DT_INT64): |
| 46 | add = 7 + 3 | 46 | add = 7 + 3 |
| 47 | sub = 7 - 3 | 47 | sub = 7 - 3 |
| 48 | mul = 7 * 3 | 48 | mul = 7 * 3 |
| @@ -61,6 +61,9 @@ def test_all_binary_and_comparison_operators_fold(): | |||
| 61 | gt = 4 > 3 | 61 | gt = 4 > 3 |
| 62 | ge = 4 >= 4 | 62 | ge = 4 >= 4 |
| 63 | 63 | ||
| 64 | + folded_ops_program, _ = folded_ops.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 65 | + folded_ops = folded_ops_program.get_function(folded_ops.__name__) | ||
| 66 | + | ||
| 64 | values = _assignments(folded_ops) | 67 | values = _assignments(folded_ops) |
| 65 | int_expected = { | 68 | int_expected = { |
| 66 | "add": 10, | 69 | "add": 10, |
| @@ -82,8 +85,8 @@ def test_all_binary_and_comparison_operators_fold(): | |||
| 82 | 85 | ||
| 83 | 86 | ||
| 84 | def test_mixed_numeric_constants_fold_to_fp32(): | 87 | def test_mixed_numeric_constants_fold_to_fp32(): |
| 85 | - @pl.function(type=pl.FunctionType.Opaque) | 88 | + @pl.jit(auto_mutex=False) |
| 86 | - def folded_numeric(): | 89 | + def folded_numeric(_jit_entry: pl.DT_INT64): |
| 87 | float_add = 1.25 + 2.5 | 90 | float_add = 1.25 + 2.5 |
| 88 | mixed_add = pl.const(2, pl.DT_INT32) + 0.5 | 91 | mixed_add = pl.const(2, pl.DT_INT32) + 0.5 |
| 89 | mixed_mul = 2 * pl.const(1.5, pl.DT_FP16) | 92 | mixed_mul = 2 * pl.const(1.5, pl.DT_FP16) |
| @@ -91,6 +94,9 @@ def test_mixed_numeric_constants_fold_to_fp32(): | |||
| 91 | typed_float = pl.const(2, pl.DT_FP32) | 94 | typed_float = pl.const(2, pl.DT_FP32) |
| 92 | typed_bool = pl.const(0, pl.DT_BOOL) | 95 | typed_bool = pl.const(0, pl.DT_BOOL) |
| 93 | 96 | ||
| 97 | + folded_numeric_program, _ = folded_numeric.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 98 | + folded_numeric = folded_numeric_program.get_function(folded_numeric.__name__) | ||
| 99 | + | ||
| 94 | values = _assignments(folded_numeric) | 100 | values = _assignments(folded_numeric) |
| 95 | _assert_constant(values["float_add"], 3.75, ir.DataType.FP32) | 101 | _assert_constant(values["float_add"], 3.75, ir.DataType.FP32) |
| 96 | _assert_constant(values["mixed_add"], 2.5, ir.DataType.FP32) | 102 | _assert_constant(values["mixed_add"], 2.5, ir.DataType.FP32) |
| @@ -101,8 +107,8 @@ def test_mixed_numeric_constants_fold_to_fp32(): | |||
| 101 | 107 | ||
| 102 | 108 | ||
| 103 | def test_bool_numeric_and_unary_result_dtypes(): | 109 | def test_bool_numeric_and_unary_result_dtypes(): |
| 104 | - @pl.function(type=pl.FunctionType.Opaque) | 110 | + @pl.jit(auto_mutex=False) |
| 105 | - def folded_bool_numeric(): | 111 | + def folded_bool_numeric(_jit_entry: pl.DT_INT64): |
| 106 | add = True + True | 112 | add = True + True |
| 107 | mixed_add = True + 0.5 | 113 | mixed_add = True + 0.5 |
| 108 | truediv = True / 2 | 114 | truediv = True / 2 |
| @@ -121,6 +127,9 @@ def test_bool_numeric_and_unary_result_dtypes(): | |||
| 121 | int_invert = ~3 | 127 | int_invert = ~3 |
| 122 | compare = False < 1 | 128 | compare = False < 1 |
| 123 | 129 | ||
| 130 | + folded_bool_numeric_program, _ = folded_bool_numeric.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 131 | + folded_bool_numeric = folded_bool_numeric_program.get_function(folded_bool_numeric.__name__) | ||
| 132 | + | ||
| 124 | values = _assignments(folded_bool_numeric) | 133 | values = _assignments(folded_bool_numeric) |
| 125 | _assert_constant(values["add"], 2, ir.DataType.INT64) | 134 | _assert_constant(values["add"], 2, ir.DataType.INT64) |
| 126 | _assert_constant(values["mixed_add"], 1.5, ir.DataType.FP32) | 135 | _assert_constant(values["mixed_add"], 1.5, ir.DataType.FP32) |
| @@ -142,8 +151,8 @@ def test_bool_numeric_and_unary_result_dtypes(): | |||
| 142 | 151 | ||
| 143 | 152 | ||
| 144 | def test_bool_ops_accept_bool_int_and_float_truthiness_but_return_bool(): | 153 | def test_bool_ops_accept_bool_int_and_float_truthiness_but_return_bool(): |
| 145 | - @pl.function(type=pl.FunctionType.Opaque) | 154 | + @pl.jit(auto_mutex=False) |
| 146 | - def folded_truth(): | 155 | + def folded_truth(_jit_entry: pl.DT_INT64): |
| 147 | int_and = 2 and 0 | 156 | int_and = 2 and 0 |
| 148 | int_or = 0 or 3 | 157 | int_or = 0 or 3 |
| 149 | float_and = 1.5 and 0.0 | 158 | float_and = 1.5 and 0.0 |
| @@ -151,6 +160,9 @@ def test_bool_ops_accept_bool_int_and_float_truthiness_but_return_bool(): | |||
| 151 | short_and = False and missing_name # noqa: F821 | 160 | short_and = False and missing_name # noqa: F821 |
| 152 | short_or = True or missing_name # noqa: F821 | 161 | short_or = True or missing_name # noqa: F821 |
| 153 | 162 | ||
| 163 | + folded_truth_program, _ = folded_truth.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 164 | + folded_truth = folded_truth_program.get_function(folded_truth.__name__) | ||
| 165 | + | ||
| 154 | values = _assignments(folded_truth) | 166 | values = _assignments(folded_truth) |
| 155 | _assert_constant(values["int_and"], False, ir.DataType.BOOL) | 167 | _assert_constant(values["int_and"], False, ir.DataType.BOOL) |
| 156 | _assert_constant(values["int_or"], True, ir.DataType.BOOL) | 168 | _assert_constant(values["int_or"], True, ir.DataType.BOOL) |
| @@ -161,13 +173,16 @@ def test_bool_ops_accept_bool_int_and_float_truthiness_but_return_bool(): | |||
| 161 | 173 | ||
| 162 | 174 | ||
| 163 | def test_pl_and_builtin_min_max_fold_with_numeric_promotion(): | 175 | def test_pl_and_builtin_min_max_fold_with_numeric_promotion(): |
| 164 | - @pl.function(type=pl.FunctionType.Opaque) | 176 | + @pl.jit(auto_mutex=False) |
| 165 | - def folded_min_max(): | 177 | + def folded_min_max(_jit_entry: pl.DT_INT64): |
| 166 | pl_min = pl.min(3, 2) | 178 | pl_min = pl.min(3, 2) |
| 167 | pl_max = pl.max(1.5, 2) | 179 | pl_max = pl.max(1.5, 2) |
| 168 | builtin_min = min(True, 2) | 180 | builtin_min = min(True, 2) |
| 169 | builtin_max = max(pl.const(1, pl.DT_INT32), 2.5) | 181 | builtin_max = max(pl.const(1, pl.DT_INT32), 2.5) |
| 170 | 182 | ||
| 183 | + folded_min_max_program, _ = folded_min_max.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 184 | + folded_min_max = folded_min_max_program.get_function(folded_min_max.__name__) | ||
| 185 | + | ||
| 171 | values = _assignments(folded_min_max) | 186 | values = _assignments(folded_min_max) |
| 172 | _assert_constant(values["pl_min"], 2, ir.DataType.INDEX) | 187 | _assert_constant(values["pl_min"], 2, ir.DataType.INDEX) |
| 173 | _assert_constant(values["pl_max"], 2.0, ir.DataType.FP32) | 188 | _assert_constant(values["pl_max"], 2.0, ir.DataType.FP32) |
| @@ -176,14 +191,17 @@ def test_pl_and_builtin_min_max_fold_with_numeric_promotion(): | |||
| 176 | 191 | ||
| 177 | 192 | ||
| 178 | def test_unsafe_folds_keep_validated_ir_nodes(): | 193 | def test_unsafe_folds_keep_validated_ir_nodes(): |
| 179 | - @pl.function(type=pl.FunctionType.Opaque) | 194 | + @pl.jit(auto_mutex=False) |
| 180 | - def unsafe_constants(): | 195 | + def unsafe_constants(_jit_entry: pl.DT_INT64): |
| 181 | zero_div = 1 / 0 | 196 | zero_div = 1 / 0 |
| 182 | zero_floordiv = 1 // 0 | 197 | zero_floordiv = 1 // 0 |
| 183 | negative_shift = 1 << -1 | 198 | negative_shift = 1 << -1 |
| 184 | float_floordiv = 3.0 // 2.0 | 199 | float_floordiv = 3.0 // 2.0 |
| 185 | float_mod = 3.0 % 2.0 | 200 | float_mod = 3.0 % 2.0 |
| 186 | 201 | ||
| 202 | + unsafe_constants_program, _ = unsafe_constants.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 203 | + unsafe_constants = unsafe_constants_program.get_function(unsafe_constants.__name__) | ||
| 204 | + | ||
| 187 | values = _assignments(unsafe_constants) | 205 | values = _assignments(unsafe_constants) |
| 188 | assert isinstance(values["zero_div"], ir.FloatDiv) | 206 | assert isinstance(values["zero_div"], ir.FloatDiv) |
| 189 | assert isinstance(values["zero_floordiv"], ir.FloorDiv) | 207 | assert isinstance(values["zero_floordiv"], ir.FloorDiv) |
| @@ -193,7 +211,7 @@ def test_unsafe_folds_keep_validated_ir_nodes(): | |||
| 193 | 211 | ||
| 194 | 212 | ||
| 195 | def test_nonconstant_operands_keep_runtime_ir(): | 213 | def test_nonconstant_operands_keep_runtime_ir(): |
| 196 | - @pl.function(type=pl.FunctionType.Opaque) | 214 | + @pl.jit(auto_mutex=False) |
| 197 | def runtime_ops(value: pl.DT_INT32, flag: pl.DT_BOOL): | 215 | def runtime_ops(value: pl.DT_INT32, flag: pl.DT_BOOL): |
| 198 | add = value + 1 | 216 | add = value + 1 |
| 199 | minimum = pl.min(value, 2) | 217 | minimum = pl.min(value, 2) |
| @@ -201,6 +219,9 @@ def test_nonconstant_operands_keep_runtime_ir(): | |||
| 201 | positive = +value | 219 | positive = +value |
| 202 | positive_bool = +flag | 220 | positive_bool = +flag |
| 203 | 221 | ||
| 222 | + runtime_ops_program, _ = runtime_ops.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 223 | + runtime_ops = runtime_ops_program.get_function(runtime_ops.__name__) | ||
| 224 | + | ||
| 204 | values = _assignments(runtime_ops) | 225 | values = _assignments(runtime_ops) |
| 205 | assert isinstance(values["add"], ir.Add) | 226 | assert isinstance(values["add"], ir.Add) |
| 206 | assert isinstance(values["minimum"], ir.Min) | 227 | assert isinstance(values["minimum"], ir.Min) |
| @@ -213,10 +234,13 @@ def test_nonconstant_operands_keep_runtime_ir(): | |||
| 213 | 234 | ||
| 214 | 235 | ||
| 215 | def test_unary_plus_is_identity_for_non_scalar_operand(): | 236 | def test_unary_plus_is_identity_for_non_scalar_operand(): |
| 216 | - @pl.function(type=pl.FunctionType.Opaque) | 237 | + @pl.jit(auto_mutex=False) |
| 217 | def identity_pos(value: pl.Tensor[[1], pl.DT_FP32]): | 238 | def identity_pos(value: pl.Tensor[[1], pl.DT_FP32]): |
| 218 | result = +value | 239 | result = +value |
| 219 | 240 | ||
| 241 | + identity_pos_program, _ = identity_pos.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 242 | + identity_pos = identity_pos_program.get_function(identity_pos.__name__) | ||
| 243 | + | ||
| 220 | result = _assignments(identity_pos)["result"] | 244 | result = _assignments(identity_pos)["result"] |
| 221 | assert isinstance(result, ir.Var) | 245 | assert isinstance(result, ir.Var) |
| 222 | assert isinstance(result.type, ir.TensorType) | 246 | assert isinstance(result.type, ir.TensorType) |
| @@ -225,12 +249,16 @@ def test_unary_plus_is_identity_for_non_scalar_operand(): | |||
| 225 | def test_invalid_constant_operand_types_raise_from_ir_builders(): | 249 | def test_invalid_constant_operand_types_raise_from_ir_builders(): |
| 226 | with pytest.raises(pl.parser.ParserError, match="bit_not.*integer dtype"): | 250 | with pytest.raises(pl.parser.ParserError, match="bit_not.*integer dtype"): |
| 227 | 251 | ||
| 228 | - @pl.function(type=pl.FunctionType.Opaque) | 252 | + @pl.jit(auto_mutex=False) |
| 229 | - def invalid_invert(): | 253 | + def invalid_invert(_jit_entry: pl.DT_INT64): |
| 230 | value = ~1.5 | 254 | value = ~1.5 |
| 231 | 255 | ||
| 256 | + invalid_invert.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 257 | + | ||
| 232 | with pytest.raises(pl.parser.ParserError, match="bit_and.*integer dtype"): | 258 | with pytest.raises(pl.parser.ParserError, match="bit_and.*integer dtype"): |
| 233 | 259 | ||
| 234 | - @pl.function(type=pl.FunctionType.Opaque) | 260 | + @pl.jit(auto_mutex=False) |
| 235 | - def invalid_bit_and(): | 261 | + def invalid_bit_and(_jit_entry: pl.DT_INT64): |
| 236 | value = 1.5 & 1 | 262 | value = 1.5 & 1 |
| 263 | + | ||
| 264 | + invalid_bit_and.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| @@ -17,8 +17,8 @@ import pypto_pro.language as pl | |||
| 17 | def test_loop_without_iter_args(): | 17 | def test_loop_without_iter_args(): |
| 18 | """Test loop without iter_args.""" | 18 | """Test loop without iter_args.""" |
| 19 | 19 | ||
| 20 | - @pl.function | 20 | + @pl.jit(auto_mutex=False) |
| 21 | - def loop_without_iter_args(x: pl.Tensor[[64], pl.DT_FP32]) -> pl.Tensor[[64], pl.DT_FP32]: | 21 | + def loop_without_iter_args(x: pl.Tensor[[64], pl.DT_FP32]): |
| 22 | result: pl.Tensor[[64], pl.DT_FP32] = x | 22 | result: pl.Tensor[[64], pl.DT_FP32] = x |
| 23 | for i in pl.range(3): | 23 | for i in pl.range(3): |
| 24 | if i > 0: | 24 | if i > 0: |
| @@ -27,19 +27,27 @@ def test_loop_without_iter_args(): | |||
| 27 | else: | 27 | else: |
| 28 | temp = pl.tensor.add(result, 1.0) | 28 | temp = pl.tensor.add(result, 1.0) |
| 29 | result = temp | 29 | result = temp |
| 30 | - return result | 30 | + _test_result = result |
| 31 | + | ||
| 32 | + loop_without_iter_args_program, _ = loop_without_iter_args.to_kernel_def().parse_target_program( | ||
| 33 | + ir.SectionKind.Vector | ||
| 34 | + ) | ||
| 35 | + loop_without_iter_args = loop_without_iter_args_program.get_function(loop_without_iter_args.__name__) | ||
| 31 | 36 | ||
| 32 | assert isinstance(loop_without_iter_args, ir.Function) | 37 | assert isinstance(loop_without_iter_args, ir.Function) |
| 33 | 38 | ||
| 34 | 39 | ||
| 35 | def test_loop_carried_constant_is_not_folded_in_body(): | 40 | def test_loop_carried_constant_is_not_folded_in_body(): |
| 36 | - @pl.function | 41 | + @pl.jit(auto_mutex=False) |
| 37 | def func(n: pl.DT_INT64): | 42 | def func(n: pl.DT_INT64): |
| 38 | x = 0 | 43 | x = 0 |
| 39 | for _ in pl.range(n): | 44 | for _ in pl.range(n): |
| 40 | observed = x # noqa: F841 | 45 | observed = x # noqa: F841 |
| 41 | x += 1 | 46 | x += 1 |
| 42 | 47 | ||
| 48 | + func_program, _ = func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 49 | + func = func_program.get_function(func.__name__) | ||
| 50 | + | ||
| 43 | for_stmt = _find_for_stmt(func) | 51 | for_stmt = _find_for_stmt(func) |
| 44 | body = for_stmt.body.stmts | 52 | body = for_stmt.body.stmts |
| 45 | observed_assignment = next( | 53 | observed_assignment = next( |
| @@ -49,13 +57,16 @@ def test_loop_carried_constant_is_not_folded_in_body(): | |||
| 49 | 57 | ||
| 50 | 58 | ||
| 51 | def test_static_if_only_emits_selected_branch(): | 59 | def test_static_if_only_emits_selected_branch(): |
| 52 | - @pl.function | 60 | + @pl.jit(auto_mutex=False) |
| 53 | - def func(): | 61 | + def func(_jit_entry: pl.DT_INT64): |
| 54 | if True: | 62 | if True: |
| 55 | selected = 7 | 63 | selected = 7 |
| 56 | else: | 64 | else: |
| 57 | selected = (1, 2)[3] # noqa: F841 | 65 | selected = (1, 2)[3] # noqa: F841 |
| 58 | 66 | ||
| 67 | + func_program, _ = func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 68 | + func = func_program.get_function(func.__name__) | ||
| 69 | + | ||
| 59 | assert all(not isinstance(stmt, ir.IfStmt) for stmt in func.body.stmts) | 70 | assert all(not isinstance(stmt, ir.IfStmt) for stmt in func.body.stmts) |
| 60 | 71 | ||
| 61 | 72 | ||
| @@ -118,11 +129,14 @@ def _assert_lowered_while_guard(while_stmt: ir.WhileStmt, expr_type) -> None: | |||
| 118 | def test_scalar_param_as_stop(): | 129 | def test_scalar_param_as_stop(): |
| 119 | """Test pl.range(n) where n is a DT_INT64 scalar parameter.""" | 130 | """Test pl.range(n) where n is a DT_INT64 scalar parameter.""" |
| 120 | 131 | ||
| 121 | - @pl.function | 132 | + @pl.jit(auto_mutex=False) |
| 122 | - def scalar_stop(n: pl.DT_INT64, x: pl.Tensor[[64], pl.DT_FP32]) -> pl.Tensor[[64], pl.DT_FP32]: | 133 | + def scalar_stop(n: pl.DT_INT64, x: pl.Tensor[[64], pl.DT_FP32]): |
| 123 | for _ in pl.range(n): | 134 | for _ in pl.range(n): |
| 124 | y: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, 1.0) | 135 | y: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, 1.0) |
| 125 | - return y | 136 | + _test_result = y |
| 137 | + | ||
| 138 | + scalar_stop_program, _ = scalar_stop.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 139 | + scalar_stop = scalar_stop_program.get_function(scalar_stop.__name__) | ||
| 126 | 140 | ||
| 127 | assert isinstance(scalar_stop, ir.Function) | 141 | assert isinstance(scalar_stop, ir.Function) |
| 128 | for_stmt = _find_for_stmt(scalar_stop) | 142 | for_stmt = _find_for_stmt(scalar_stop) |
| @@ -135,13 +149,16 @@ def test_scalar_param_as_stop(): | |||
| 135 | def test_scalar_param_as_start_stop(): | 149 | def test_scalar_param_as_start_stop(): |
| 136 | """Test pl.range(0, n) where n is a DT_INT64 scalar parameter.""" | 150 | """Test pl.range(0, n) where n is a DT_INT64 scalar parameter.""" |
| 137 | 151 | ||
| 138 | - @pl.function | 152 | + @pl.jit(auto_mutex=False) |
| 139 | def scalar_start_stop( | 153 | def scalar_start_stop( |
| 140 | n: pl.DT_INT64, x: pl.Tensor[[64], pl.DT_FP32] | 154 | n: pl.DT_INT64, x: pl.Tensor[[64], pl.DT_FP32] |
| 141 | - ) -> pl.Tensor[[64], pl.DT_FP32]: | 155 | + ): |
| 142 | for _ in pl.range(0, n): | 156 | for _ in pl.range(0, n): |
| 143 | y: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, 1.0) | 157 | y: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, 1.0) |
| 144 | - return y | 158 | + _test_result = y |
| 159 | + | ||
| 160 | + scalar_start_stop_program, _ = scalar_start_stop.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 161 | + scalar_start_stop = scalar_start_stop_program.get_function(scalar_start_stop.__name__) | ||
| 145 | 162 | ||
| 146 | assert isinstance(scalar_start_stop, ir.Function) | 163 | assert isinstance(scalar_start_stop, ir.Function) |
| 147 | for_stmt = _find_for_stmt(scalar_start_stop) | 164 | for_stmt = _find_for_stmt(scalar_start_stop) |
| @@ -153,15 +170,18 @@ def test_scalar_param_as_start_stop(): | |||
| 153 | def test_scalar_param_as_start_stop_step(): | 170 | def test_scalar_param_as_start_stop_step(): |
| 154 | """Test pl.range(0, n, s) where n and s are DT_INT64 scalar parameters.""" | 171 | """Test pl.range(0, n, s) where n and s are DT_INT64 scalar parameters.""" |
| 155 | 172 | ||
| 156 | - @pl.function | 173 | + @pl.jit(auto_mutex=False) |
| 157 | def scalar_full_range( | 174 | def scalar_full_range( |
| 158 | n: pl.DT_INT64, | 175 | n: pl.DT_INT64, |
| 159 | s: pl.DT_INT64, | 176 | s: pl.DT_INT64, |
| 160 | x: pl.Tensor[[64], pl.DT_FP32], | 177 | x: pl.Tensor[[64], pl.DT_FP32], |
| 161 | - ) -> pl.Tensor[[64], pl.DT_FP32]: | 178 | + ): |
| 162 | for _ in pl.range(0, n, s): | 179 | for _ in pl.range(0, n, s): |
| 163 | y: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, 1.0) | 180 | y: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, 1.0) |
| 164 | - return y | 181 | + _test_result = y |
| 182 | + | ||
| 183 | + scalar_full_range_program, _ = scalar_full_range.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 184 | + scalar_full_range = scalar_full_range_program.get_function(scalar_full_range.__name__) | ||
| 165 | 185 | ||
| 166 | assert isinstance(scalar_full_range, ir.Function) | 186 | assert isinstance(scalar_full_range, ir.Function) |
| 167 | for_stmt = _find_for_stmt(scalar_full_range) | 187 | for_stmt = _find_for_stmt(scalar_full_range) |
| @@ -175,11 +195,14 @@ def test_scalar_param_as_start_stop_step(): | |||
| 175 | def test_scalar_expression_as_stop(): | 195 | def test_scalar_expression_as_stop(): |
| 176 | """Test pl.range(n * 2) where n is a DT_INT64 scalar parameter.""" | 196 | """Test pl.range(n * 2) where n is a DT_INT64 scalar parameter.""" |
| 177 | 197 | ||
| 178 | - @pl.function | 198 | + @pl.jit(auto_mutex=False) |
| 179 | - def scalar_expr_stop(n: pl.DT_INT64, x: pl.Tensor[[64], pl.DT_FP32]) -> pl.Tensor[[64], pl.DT_FP32]: | 199 | + def scalar_expr_stop(n: pl.DT_INT64, x: pl.Tensor[[64], pl.DT_FP32]): |
| 180 | for _ in pl.range(n * 2): # type: ignore[operator] | 200 | for _ in pl.range(n * 2): # type: ignore[operator] |
| 181 | y: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, 1.0) | 201 | y: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, 1.0) |
| 182 | - return y | 202 | + _test_result = y |
| 203 | + | ||
| 204 | + scalar_expr_stop_program, _ = scalar_expr_stop.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 205 | + scalar_expr_stop = scalar_expr_stop_program.get_function(scalar_expr_stop.__name__) | ||
| 183 | 206 | ||
| 184 | assert isinstance(scalar_expr_stop, ir.Function) | 207 | assert isinstance(scalar_expr_stop, ir.Function) |
| 185 | _assert_materialized_stop(scalar_expr_stop, ir.Mul) | 208 | _assert_materialized_stop(scalar_expr_stop, ir.Mul) |
| @@ -188,13 +211,16 @@ def test_scalar_expression_as_stop(): | |||
| 188 | def test_scalar_complex_expression_as_stop(): | 211 | def test_scalar_complex_expression_as_stop(): |
| 189 | """Test pl.range(n * 2 + 1) where n is a DT_INT64 scalar parameter.""" | 212 | """Test pl.range(n * 2 + 1) where n is a DT_INT64 scalar parameter.""" |
| 190 | 213 | ||
| 191 | - @pl.function | 214 | + @pl.jit(auto_mutex=False) |
| 192 | def scalar_complex_expr( | 215 | def scalar_complex_expr( |
| 193 | n: pl.DT_INT64, x: pl.Tensor[[64], pl.DT_FP32] | 216 | n: pl.DT_INT64, x: pl.Tensor[[64], pl.DT_FP32] |
| 194 | - ) -> pl.Tensor[[64], pl.DT_FP32]: | 217 | + ): |
| 195 | for _ in pl.range(n * 2 + 1): # type: ignore[operator] | 218 | for _ in pl.range(n * 2 + 1): # type: ignore[operator] |
| 196 | y: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, 1.0) | 219 | y: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, 1.0) |
| 197 | - return y | 220 | + _test_result = y |
| 221 | + | ||
| 222 | + scalar_complex_expr_program, _ = scalar_complex_expr.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 223 | + scalar_complex_expr = scalar_complex_expr_program.get_function(scalar_complex_expr.__name__) | ||
| 198 | 224 | ||
| 199 | assert isinstance(scalar_complex_expr, ir.Function) | 225 | assert isinstance(scalar_complex_expr, ir.Function) |
| 200 | _assert_materialized_stop(scalar_complex_expr, ir.Add) | 226 | _assert_materialized_stop(scalar_complex_expr, ir.Add) |
| @@ -203,13 +229,16 @@ def test_scalar_complex_expression_as_stop(): | |||
| 203 | def test_scalar_floordiv_expression_as_stop(): | 229 | def test_scalar_floordiv_expression_as_stop(): |
| 204 | """Test pl.range(n // 4) where n is a DT_INT64 scalar parameter.""" | 230 | """Test pl.range(n // 4) where n is a DT_INT64 scalar parameter.""" |
| 205 | 231 | ||
| 206 | - @pl.function | 232 | + @pl.jit(auto_mutex=False) |
| 207 | def scalar_floordiv_expr( | 233 | def scalar_floordiv_expr( |
| 208 | n: pl.DT_INT64, x: pl.Tensor[[64], pl.DT_FP32] | 234 | n: pl.DT_INT64, x: pl.Tensor[[64], pl.DT_FP32] |
| 209 | - ) -> pl.Tensor[[64], pl.DT_FP32]: | 235 | + ): |
| 210 | for _ in pl.range(n // 4): # type: ignore[operator] | 236 | for _ in pl.range(n // 4): # type: ignore[operator] |
| 211 | y: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, 1.0) | 237 | y: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, 1.0) |
| 212 | - return y | 238 | + _test_result = y |
| 239 | + | ||
| 240 | + scalar_floordiv_expr_program, _ = scalar_floordiv_expr.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 241 | + scalar_floordiv_expr = scalar_floordiv_expr_program.get_function(scalar_floordiv_expr.__name__) | ||
| 213 | 242 | ||
| 214 | assert isinstance(scalar_floordiv_expr, ir.Function) | 243 | assert isinstance(scalar_floordiv_expr, ir.Function) |
| 215 | _assert_materialized_stop(scalar_floordiv_expr, ir.FloorDiv) | 244 | _assert_materialized_stop(scalar_floordiv_expr, ir.FloorDiv) |
| @@ -218,12 +247,15 @@ def test_scalar_floordiv_expression_as_stop(): | |||
| 218 | def test_natural_while_loop(): | 247 | def test_natural_while_loop(): |
| 219 | """Test natural while loop syntax (non-SSA form).""" | 248 | """Test natural while loop syntax (non-SSA form).""" |
| 220 | 249 | ||
| 221 | - @pl.function | 250 | + @pl.jit(auto_mutex=False) |
| 222 | - def natural_while(n: pl.DT_INT64) -> pl.DT_INT64: | 251 | + def natural_while(n: pl.DT_INT64): |
| 223 | x: pl.DT_INT64 = 0 | 252 | x: pl.DT_INT64 = 0 |
| 224 | while x < n: | 253 | while x < n: |
| 225 | x = x + 1 | 254 | x = x + 1 |
| 226 | - return x | 255 | + _test_result = x |
| 256 | + | ||
| 257 | + natural_while_program, _ = natural_while.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 258 | + natural_while = natural_while_program.get_function(natural_while.__name__) | ||
| 227 | 259 | ||
| 228 | assert isinstance(natural_while, ir.Function) | 260 | assert isinstance(natural_while, ir.Function) |
| 229 | assert natural_while.name == "natural_while" | 261 | assert natural_while.name == "natural_while" |
| @@ -245,14 +277,17 @@ def test_natural_while_loop(): | |||
| 245 | def test_natural_while_loop_with_initialization(): | 277 | def test_natural_while_loop_with_initialization(): |
| 246 | """Test natural while loop with explicit initialization.""" | 278 | """Test natural while loop with explicit initialization.""" |
| 247 | 279 | ||
| 248 | - @pl.function | 280 | + @pl.jit(auto_mutex=False) |
| 249 | - def natural_while_init(limit: pl.DT_INT64) -> pl.DT_INT64: | 281 | + def natural_while_init(limit: pl.DT_INT64): |
| 250 | counter: pl.DT_INT64 = 0 | 282 | counter: pl.DT_INT64 = 0 |
| 251 | sum_val: pl.DT_INT64 = 0 | 283 | sum_val: pl.DT_INT64 = 0 |
| 252 | while counter < limit: | 284 | while counter < limit: |
| 253 | sum_val = sum_val + counter | 285 | sum_val = sum_val + counter |
| 254 | counter = counter + 1 | 286 | counter = counter + 1 |
| 255 | - return sum_val | 287 | + _test_result = sum_val |
| 288 | + | ||
| 289 | + natural_while_init_program, _ = natural_while_init.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 290 | + natural_while_init = natural_while_init_program.get_function(natural_while_init.__name__) | ||
| 256 | 291 | ||
| 257 | assert isinstance(natural_while_init, ir.Function) | 292 | assert isinstance(natural_while_init, ir.Function) |
| 258 | 293 | ||
| @@ -269,14 +304,17 @@ def test_natural_while_loop_with_initialization(): | |||
| 269 | def test_while_loop_with_tensors(): | 304 | def test_while_loop_with_tensors(): |
| 270 | """Test while loop with tensor operations.""" | 305 | """Test while loop with tensor operations.""" |
| 271 | 306 | ||
| 272 | - @pl.function | 307 | + @pl.jit(auto_mutex=False) |
| 273 | - def while_tensors(n: pl.DT_INT64, x: pl.Tensor[[64], pl.DT_FP32]) -> pl.Tensor[[64], pl.DT_FP32]: | 308 | + def while_tensors(n: pl.DT_INT64, x: pl.Tensor[[64], pl.DT_FP32]): |
| 274 | i: pl.DT_INT64 = 0 | 309 | i: pl.DT_INT64 = 0 |
| 275 | acc: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.create_tensor([64], dtype=pl.DT_FP32) | 310 | acc: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.create_tensor([64], dtype=pl.DT_FP32) |
| 276 | while i < n: | 311 | while i < n: |
| 277 | i = i + 1 | 312 | i = i + 1 |
| 278 | acc = pl.tensor.add(acc, x) | 313 | acc = pl.tensor.add(acc, x) |
| 279 | - return acc | 314 | + _test_result = acc |
| 315 | + | ||
| 316 | + while_tensors_program, _ = while_tensors.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 317 | + while_tensors = while_tensors_program.get_function(while_tensors.__name__) | ||
| 280 | 318 | ||
| 281 | assert isinstance(while_tensors, ir.Function) | 319 | assert isinstance(while_tensors, ir.Function) |
| 282 | 320 | ||
| @@ -293,15 +331,18 @@ def test_while_loop_with_tensors(): | |||
| 293 | def test_nested_while_loops(): | 331 | def test_nested_while_loops(): |
| 294 | """Test nested while loops.""" | 332 | """Test nested while loops.""" |
| 295 | 333 | ||
| 296 | - @pl.function | 334 | + @pl.jit(auto_mutex=False) |
| 297 | - def nested_while(n: pl.DT_INT64) -> pl.DT_INT64: | 335 | + def nested_while(n: pl.DT_INT64): |
| 298 | x: pl.DT_INT64 = 0 | 336 | x: pl.DT_INT64 = 0 |
| 299 | while x < n: | 337 | while x < n: |
| 300 | y: pl.DT_INT64 = 0 | 338 | y: pl.DT_INT64 = 0 |
| 301 | while y < 3: | 339 | while y < 3: |
| 302 | y = y + 1 | 340 | y = y + 1 |
| 303 | x = x + 1 | 341 | x = x + 1 |
| 304 | - return x | 342 | + _test_result = x |
| 343 | + | ||
| 344 | + nested_while_program, _ = nested_while.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 345 | + nested_while = nested_while_program.get_function(nested_while.__name__) | ||
| 305 | 346 | ||
| 306 | assert isinstance(nested_while, ir.Function) | 347 | assert isinstance(nested_while, ir.Function) |
| 307 | 348 | ||
| @@ -327,14 +368,19 @@ def test_nested_while_loops(): | |||
| 327 | def test_while_tensor_getval_condition_is_recomputed_in_body(): | 368 | def test_while_tensor_getval_condition_is_recomputed_in_body(): |
| 328 | """Tensor getval and its comparison must execute before the guard each iteration.""" | 369 | """Tensor getval and its comparison must execute before the guard each iteration.""" |
| 329 | 370 | ||
| 330 | - @pl.function | 371 | + @pl.jit(auto_mutex=False) |
| 331 | - def while_tensor_condition(values: pl.Tensor[[8], pl.DT_INT32], limit: pl.DT_INT64) -> pl.DT_INT64: | 372 | + def while_tensor_condition(values: pl.Tensor[[8], pl.DT_INT32], limit: pl.DT_INT64): |
| 332 | i: pl.DT_INT64 = 0 | 373 | i: pl.DT_INT64 = 0 |
| 333 | while values[i] > 0: | 374 | while values[i] > 0: |
| 334 | i = i + 1 | 375 | i = i + 1 |
| 335 | if i >= limit: | 376 | if i >= limit: |
| 336 | break | 377 | break |
| 337 | - return i | 378 | + _test_result = i |
| 379 | + | ||
| 380 | + while_tensor_condition_program, _ = while_tensor_condition.to_kernel_def().parse_target_program( | ||
| 381 | + ir.SectionKind.Vector | ||
| 382 | + ) | ||
| 383 | + while_tensor_condition = while_tensor_condition_program.get_function(while_tensor_condition.__name__) | ||
| 338 | 384 | ||
| 339 | while_stmt = _find_while_stmt(while_tensor_condition) | 385 | while_stmt = _find_while_stmt(while_tensor_condition) |
| 340 | _assert_lowered_while_guard(while_stmt, ir.Gt) | 386 | _assert_lowered_while_guard(while_stmt, ir.Gt) |
| @@ -343,12 +389,15 @@ def test_while_tensor_getval_condition_is_recomputed_in_body(): | |||
| 343 | def test_while_not_condition_uses_expression_parser(): | 389 | def test_while_not_condition_uses_expression_parser(): |
| 344 | """The source-level not condition is materialized inside the lowered loop guard.""" | 390 | """The source-level not condition is materialized inside the lowered loop guard.""" |
| 345 | 391 | ||
| 346 | - @pl.function | 392 | + @pl.jit(auto_mutex=False) |
| 347 | - def while_not_condition(n: pl.DT_INT64) -> pl.DT_INT64: | 393 | + def while_not_condition(n: pl.DT_INT64): |
| 348 | i: pl.DT_INT64 = 0 | 394 | i: pl.DT_INT64 = 0 |
| 349 | while not i >= n: | 395 | while not i >= n: |
| 350 | i = i + 1 | 396 | i = i + 1 |
| 351 | - return i | 397 | + _test_result = i |
| 398 | + | ||
| 399 | + while_not_condition_program, _ = while_not_condition.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 400 | + while_not_condition = while_not_condition_program.get_function(while_not_condition.__name__) | ||
| 352 | 401 | ||
| 353 | while_stmt = _find_while_stmt(while_not_condition) | 402 | while_stmt = _find_while_stmt(while_not_condition) |
| 354 | _assert_lowered_while_guard(while_stmt, ir.Not) | 403 | _assert_lowered_while_guard(while_stmt, ir.Not) |
| @@ -357,8 +406,8 @@ def test_while_not_condition_uses_expression_parser(): | |||
| 357 | def test_while_with_multiple_updates(): | 406 | def test_while_with_multiple_updates(): |
| 358 | """Test while loop with multiple variable updates.""" | 407 | """Test while loop with multiple variable updates.""" |
| 359 | 408 | ||
| 360 | - @pl.function | 409 | + @pl.jit(auto_mutex=False) |
| 361 | - def while_multi_update(n: pl.DT_INT64) -> pl.DT_INT64: | 410 | + def while_multi_update(n: pl.DT_INT64): |
| 362 | x: pl.DT_INT64 = 0 | 411 | x: pl.DT_INT64 = 0 |
| 363 | y: pl.DT_INT64 = 1 | 412 | y: pl.DT_INT64 = 1 |
| 364 | 413 | ||
| @@ -366,7 +415,10 @@ def test_while_with_multiple_updates(): | |||
| 366 | x = x + 1 | 415 | x = x + 1 |
| 367 | y = y * 2 | 416 | y = y * 2 |
| 368 | 417 | ||
| 369 | - return y | 418 | + _test_result = y |
| 419 | + | ||
| 420 | + while_multi_update_program, _ = while_multi_update.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 421 | + while_multi_update = while_multi_update_program.get_function(while_multi_update.__name__) | ||
| 370 | 422 | ||
| 371 | assert isinstance(while_multi_update, ir.Function) | 423 | assert isinstance(while_multi_update, ir.Function) |
| 372 | 424 | ||
| @@ -387,14 +439,17 @@ closure_allowed = [5, 6] | |||
| 387 | def test_in_operator_with_kernel_local_list(): | 439 | def test_in_operator_with_kernel_local_list(): |
| 388 | """``x in <list>`` accepts a list bound inside the function, not just a literal.""" | 440 | """``x in <list>`` accepts a list bound inside the function, not just a literal.""" |
| 389 | 441 | ||
| 390 | - @pl.function | 442 | + @pl.jit(auto_mutex=False) |
| 391 | - def func(n: pl.DT_INT64) -> pl.DT_INT64: | 443 | + def func(n: pl.DT_INT64): |
| 392 | allowed = [0, 1] | 444 | allowed = [0, 1] |
| 393 | total: pl.DT_INT64 = 0 | 445 | total: pl.DT_INT64 = 0 |
| 394 | for i in pl.range(n): | 446 | for i in pl.range(n): |
| 395 | if i in allowed: | 447 | if i in allowed: |
| 396 | total = total + 1 | 448 | total = total + 1 |
| 397 | - return total | 449 | + _test_result = total |
| 450 | + | ||
| 451 | + func_program, _ = func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 452 | + func = func_program.get_function(func.__name__) | ||
| 398 | 453 | ||
| 399 | assert isinstance(func, ir.Function) | 454 | assert isinstance(func, ir.Function) |
| 400 | 455 | ||
| @@ -402,14 +457,17 @@ def test_in_operator_with_kernel_local_list(): | |||
| 402 | def test_in_operator_kernel_local_list_shadows_closure(): | 457 | def test_in_operator_kernel_local_list_shadows_closure(): |
| 403 | """A function-local list wins over a same-named closure list, as in Python.""" | 458 | """A function-local list wins over a same-named closure list, as in Python.""" |
| 404 | 459 | ||
| 405 | - @pl.function | 460 | + @pl.jit(auto_mutex=False) |
| 406 | - def func(n: pl.DT_INT64) -> pl.DT_INT64: | 461 | + def func(n: pl.DT_INT64): |
| 407 | closure_allowed = [0, 1] # noqa: F841 - shadows the module-level list | 462 | closure_allowed = [0, 1] # noqa: F841 - shadows the module-level list |
| 408 | total: pl.DT_INT64 = 0 | 463 | total: pl.DT_INT64 = 0 |
| 409 | for i in pl.range(n): | 464 | for i in pl.range(n): |
| 410 | if i in closure_allowed: | 465 | if i in closure_allowed: |
| 411 | total = total + 1 | 466 | total = total + 1 |
| 412 | - return total | 467 | + _test_result = total |
| 468 | + | ||
| 469 | + func_program, _ = func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 470 | + func = func_program.get_function(func.__name__) | ||
| 413 | 471 | ||
| 414 | text = str(func) | 472 | text = str(func) |
| 415 | # The local values (0, 1) drive the eq-chain; the closure values (5, 6) do not. | 473 | # The local values (0, 1) drive the eq-chain; the closure values (5, 6) do not. |
| @@ -417,13 +475,16 @@ def test_in_operator_kernel_local_list_shadows_closure(): | |||
| 417 | 475 | ||
| 418 | 476 | ||
| 419 | def test_in_operator_with_closure_list_still_works(): | 477 | def test_in_operator_with_closure_list_still_works(): |
| 420 | - @pl.function | 478 | + @pl.jit(auto_mutex=False) |
| 421 | - def func(n: pl.DT_INT64) -> pl.DT_INT64: | 479 | + def func(n: pl.DT_INT64): |
| 422 | total: pl.DT_INT64 = 0 | 480 | total: pl.DT_INT64 = 0 |
| 423 | for i in pl.range(n): | 481 | for i in pl.range(n): |
| 424 | if i in closure_allowed: | 482 | if i in closure_allowed: |
| 425 | total = total + 1 | 483 | total = total + 1 |
| 426 | - return total | 484 | + _test_result = total |
| 485 | + | ||
| 486 | + func_program, _ = func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 487 | + func = func_program.get_function(func.__name__) | ||
| 427 | 488 | ||
| 428 | assert isinstance(func, ir.Function) | 489 | assert isinstance(func, ir.Function) |
| 429 | 490 | ||
| @@ -441,14 +502,14 @@ def _addr_for_not_in(dtype): | |||
| 441 | 502 | ||
| 442 | 503 | ||
| 443 | def _tile_group_addr_ir(dtype, helper) -> str: | 504 | def _tile_group_addr_ir(dtype, helper) -> str: |
| 444 | - @pl.kernel(auto_mutex=True) | 505 | + @pl.jit(auto_mutex=True) |
| 445 | def k(a: pl.Tensor[[128, 128], pl.DT_FP16]): | 506 | def k(a: pl.Tensor[[128, 128], pl.DT_FP16]): |
| 446 | tt = pl.TileType(shape=[128, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) | 507 | tt = pl.TileType(shape=[128, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Mat) |
| 447 | dt = dtype | 508 | dt = dtype |
| 448 | g = pl.make_tile_group(type=tt, addrs=helper(dt), mutex_ids=[0, 1]) | 509 | g = pl.make_tile_group(type=tt, addrs=helper(dt), mutex_ids=[0, 1]) |
| 449 | pl.load(g.next(), a, [0, 0]) | 510 | pl.load(g.next(), a, [0, 0]) |
| 450 | 511 | ||
| 451 | - return str(k.parse_target_program(ir.SectionKind.Vector)[0]) | 512 | + return str(k.to_kernel_def().parse_target_program(ir.SectionKind.Vector)[0]) |
| 452 | 513 | ||
| 453 | 514 | ||
| 454 | def test_enum_in_tuple_folds_to_selected_branch(): | 515 | def test_enum_in_tuple_folds_to_selected_branch(): |
| @@ -34,13 +34,15 @@ def test_inline_helper_multiple_value_returns_use_one_wrapper(): | |||
| 34 | return value + 1 | 34 | return value + 1 |
| 35 | return value - 1 | 35 | return value - 1 |
| 36 | 36 | ||
| 37 | - @pl.function | 37 | + @pl.jit(auto_mutex=False) |
| 38 | - def caller(value: pl.DT_INT64) -> pl.DT_INT64: | 38 | + def caller(value: pl.DT_INT64): |
| 39 | - return choose(value) | 39 | + _test_result = choose(value) |
| 40 | + | ||
| 41 | + caller_program, _ = caller.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 42 | + caller = caller_program.get_function(caller.__name__) | ||
| 40 | 43 | ||
| 41 | statements = list(_walk_statements(caller.body)) | 44 | statements = list(_walk_statements(caller.body)) |
| 42 | helper_loops = [stmt for stmt in statements if isinstance(stmt, ir.WhileStmt)] | 45 | helper_loops = [stmt for stmt in statements if isinstance(stmt, ir.WhileStmt)] |
| 43 | - helper_returns = [stmt for stmt in statements if isinstance(stmt, ir.ReturnStmt)] | ||
| 44 | return_assigns = [ | 46 | return_assigns = [ |
| 45 | stmt | 47 | stmt |
| 46 | for stmt in statements | 48 | for stmt in statements |
| @@ -48,7 +50,6 @@ def test_inline_helper_multiple_value_returns_use_one_wrapper(): | |||
| 48 | ] | 50 | ] |
| 49 | 51 | ||
| 50 | assert len(helper_loops) == 1 | 52 | assert len(helper_loops) == 1 |
| 51 | - assert len(helper_returns) == 1 | ||
| 52 | assert len(return_assigns) == 3 # Unknown init plus the two concrete return assignments. | 53 | assert len(return_assigns) == 3 # Unknown init plus the two concrete return assignments. |
| 53 | assert len({stmt.var.name for stmt in return_assigns}) == 1 | 54 | assert len({stmt.var.name for stmt in return_assigns}) == 1 |
| 54 | 55 | ||
| @@ -62,9 +63,12 @@ def test_inline_helper_return_inside_loop_adds_propagation_guard(): | |||
| 62 | index = index + 1 | 63 | index = index + 1 |
| 63 | return value | 64 | return value |
| 64 | 65 | ||
| 65 | - @pl.function | 66 | + @pl.jit(auto_mutex=False) |
| 66 | - def caller(value: pl.DT_INT64) -> pl.DT_INT64: | 67 | + def caller(value: pl.DT_INT64): |
| 67 | - return choose(value) | 68 | + _test_result = choose(value) |
| 69 | + | ||
| 70 | + caller_program, _ = caller.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 71 | + caller = caller_program.get_function(caller.__name__) | ||
| 68 | 72 | ||
| 69 | wrapper = next(stmt for stmt in caller.body.stmts if isinstance(stmt, ir.WhileStmt)) | 73 | wrapper = next(stmt for stmt in caller.body.stmts if isinstance(stmt, ir.WhileStmt)) |
| 70 | nested_loop_index = next(i for i, stmt in enumerate(wrapper.body.stmts) if isinstance(stmt, ir.WhileStmt)) | 74 | nested_loop_index = next(i for i, stmt in enumerate(wrapper.body.stmts) if isinstance(stmt, ir.WhileStmt)) |
| @@ -95,9 +99,12 @@ def test_inline_helper_loop_guard_folds_constant_false(): | |||
| 95 | return doubled | 99 | return doubled |
| 96 | return result | 100 | return result |
| 97 | 101 | ||
| 98 | - @pl.function | 102 | + @pl.jit(auto_mutex=False) |
| 99 | - def caller(value: pl.DT_INT64) -> pl.DT_INT64: | 103 | + def caller(value: pl.DT_INT64): |
| 100 | - return outer(value) | 104 | + _test_result = outer(value) |
| 105 | + | ||
| 106 | + caller_program, _ = caller.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 107 | + caller = caller_program.get_function(caller.__name__) | ||
| 101 | 108 | ||
| 102 | outer_wrapper = next(stmt for stmt in caller.body.stmts if isinstance(stmt, ir.WhileStmt)) | 109 | outer_wrapper = next(stmt for stmt in caller.body.stmts if isinstance(stmt, ir.WhileStmt)) |
| 103 | outer_for_index = next(i for i, stmt in enumerate(outer_wrapper.body.stmts) if isinstance(stmt, ir.ForStmt)) | 110 | outer_for_index = next(i for i, stmt in enumerate(outer_wrapper.body.stmts) if isinstance(stmt, ir.ForStmt)) |
| @@ -112,10 +119,13 @@ def test_inline_helper_bare_returns_share_return_state(): | |||
| 112 | return | 119 | return |
| 113 | return None | 120 | return None |
| 114 | 121 | ||
| 115 | - @pl.function | 122 | + @pl.jit(auto_mutex=False) |
| 116 | - def caller(value: pl.DT_INT64) -> pl.DT_INT64: | 123 | + def caller(value: pl.DT_INT64): |
| 117 | stop_early(value) | 124 | stop_early(value) |
| 118 | - return value | 125 | + _test_result = value |
| 126 | + | ||
| 127 | + caller_program, _ = caller.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 128 | + caller = caller_program.get_function(caller.__name__) | ||
| 119 | 129 | ||
| 120 | statements = list(_walk_statements(caller.body)) | 130 | statements = list(_walk_statements(caller.body)) |
| 121 | assert sum(isinstance(stmt, ir.WhileStmt) for stmt in statements) == 1 | 131 | assert sum(isinstance(stmt, ir.WhileStmt) for stmt in statements) == 1 |
| @@ -130,9 +140,12 @@ def test_inline_helper_mixed_bare_and_value_returns_share_return_val(): | |||
| 130 | return value | 140 | return value |
| 131 | return | 141 | return |
| 132 | 142 | ||
| 133 | - @pl.function | 143 | + @pl.jit(auto_mutex=False) |
| 134 | def caller(value: pl.DT_INT64): | 144 | def caller(value: pl.DT_INT64): |
| 135 | - return choose(value) | 145 | + _test_result = choose(value) |
| 146 | + | ||
| 147 | + caller_program, _ = caller.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 148 | + caller = caller_program.get_function(caller.__name__) | ||
| 136 | 149 | ||
| 137 | statements = list(_walk_statements(caller.body)) | 150 | statements = list(_walk_statements(caller.body)) |
| 138 | assert sum( | 151 | assert sum( |
| @@ -146,9 +159,12 @@ def test_inline_helper_without_return_uses_default_return_val(): | |||
| 146 | for _ in pl.range(1): | 159 | for _ in pl.range(1): |
| 147 | result = result + 1 | 160 | result = result + 1 |
| 148 | 161 | ||
| 149 | - @pl.function | 162 | + @pl.jit(auto_mutex=False) |
| 150 | def caller(value: pl.DT_INT64): | 163 | def caller(value: pl.DT_INT64): |
| 151 | - return update(value) | 164 | + _test_result = update(value) |
| 165 | + | ||
| 166 | + caller_program, _ = caller.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 167 | + caller = caller_program.get_function(caller.__name__) | ||
| 152 | 168 | ||
| 153 | statements = list(_walk_statements(caller.body)) | 169 | statements = list(_walk_statements(caller.body)) |
| 154 | wrapper = next(stmt for stmt in caller.body.stmts if isinstance(stmt, ir.WhileStmt)) | 170 | wrapper = next(stmt for stmt in caller.body.stmts if isinstance(stmt, ir.WhileStmt)) |
| @@ -164,36 +180,41 @@ def test_inline_helper_value_return_can_fall_through(): | |||
| 164 | if value > 0: | 180 | if value > 0: |
| 165 | return value | 181 | return value |
| 166 | 182 | ||
| 167 | - @pl.function | 183 | + @pl.jit(auto_mutex=False) |
| 168 | - def caller(value: pl.DT_INT64) -> pl.DT_INT64: | 184 | + def caller(value: pl.DT_INT64): |
| 169 | - return choose(value) | 185 | + _test_result = choose(value) |
| 186 | + | ||
| 187 | + caller_program, _ = caller.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 188 | + caller = caller_program.get_function(caller.__name__) | ||
| 170 | 189 | ||
| 171 | assert any(isinstance(stmt, ir.WhileStmt) for stmt in caller.body.stmts) | 190 | assert any(isinstance(stmt, ir.WhileStmt) for stmt in caller.body.stmts) |
| 172 | 191 | ||
| 173 | 192 | ||
| 174 | def test_kernel_uses_runtime_tile_valid_shape_ir(): | 193 | def test_kernel_uses_runtime_tile_valid_shape_ir(): |
| 175 | - @pl.function(type=pl.FunctionType.InCore) | 194 | + @pl.jit(auto_mutex=False) |
| 176 | - def caller(): | 195 | + def caller(_jit_entry: pl.DT_INT64): |
| 177 | tile_type = pl.TileType(shape=[16, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 196 | tile_type = pl.TileType(shape=[16, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 178 | tile = pl.make_tile(tile_type, addr=0, size=1024) | 197 | tile = pl.make_tile(tile_type, addr=0, size=1024) |
| 179 | _kernel_rows = tile.valid_shape[0] | 198 | _kernel_rows = tile.valid_shape[0] |
| 180 | _kernel_cols = tile.valid_shape[1] | 199 | _kernel_cols = tile.valid_shape[1] |
| 181 | 200 | ||
| 201 | + caller_program, _ = caller.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 202 | + caller = caller_program.get_function(caller.__name__) | ||
| 203 | + | ||
| 182 | function_ir = str(caller) | 204 | function_ir = str(caller) |
| 183 | assert function_ir.count("block.tile_valid_shape") == 2 | 205 | assert function_ir.count("block.tile_valid_shape") == 2 |
| 184 | 206 | ||
| 185 | 207 | ||
| 186 | -@pytest.mark.parametrize("index", ["True", "0.0", "2", "-3"]) | 208 | +@pytest.mark.parametrize("index", [True, 0.0, 2, -3]) |
| 187 | def test_tile_valid_shape_rejects_invalid_index(index): | 209 | def test_tile_valid_shape_rejects_invalid_index(index): |
| 188 | - code = f""" | ||
| 189 | -@pl.function(type=pl.FunctionType.InCore) | ||
| 190 | -def invalid_valid_shape(): | ||
| 191 | - tile_type = pl.TileType(shape=[16, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | ||
| 192 | - tile = pl.make_tile(tile_type, addr=0, size=1024) | ||
| 193 | - value = tile.valid_shape[{index}] | ||
| 194 | -""" | ||
| 195 | with pytest.raises((ParserSyntaxError, ParserTypeError)): | 210 | with pytest.raises((ParserSyntaxError, ParserTypeError)): |
| 196 | - pl.parse(code) | 211 | + @pl.jit(auto_mutex=False) |
| 212 | + def invalid_valid_shape(_jit_entry: pl.DT_INT64): | ||
| 213 | + tile_type = pl.TileType(shape=[16, 32], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | ||
| 214 | + tile = pl.make_tile(tile_type, addr=0, size=1024) | ||
| 215 | + value = tile.valid_shape[index] # noqa: F841 | ||
| 216 | + | ||
| 217 | + invalid_valid_shape.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 197 | 218 | ||
| 198 | 219 | ||
| 199 | def test_vector_function_rejects_explicit_return(): | 220 | def test_vector_function_rejects_explicit_return(): |
| @@ -203,10 +224,12 @@ def test_vector_function_rejects_explicit_return(): | |||
| 203 | 224 | ||
| 204 | with pytest.raises(ParserSyntaxError, match="cannot contain return"): | 225 | with pytest.raises(ParserSyntaxError, match="cannot contain return"): |
| 205 | 226 | ||
| 206 | - @pl.function | 227 | + @pl.jit(auto_mutex=False) |
| 207 | - def caller(value: pl.DT_INT64) -> pl.DT_INT64: | 228 | + def caller(value: pl.DT_INT64): |
| 208 | invalid_vf() | 229 | invalid_vf() |
| 209 | - return value | 230 | + _test_result = value |
| 231 | + | ||
| 232 | + caller.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 210 | 233 | ||
| 211 | 234 | ||
| 212 | def test_vector_function_rejects_non_vector_helper_call(): | 235 | def test_vector_function_rejects_non_vector_helper_call(): |
| @@ -219,7 +242,9 @@ def test_vector_function_rejects_non_vector_helper_call(): | |||
| 219 | 242 | ||
| 220 | with pytest.raises(ParserSyntaxError, match="cannot call non-vector inline function 'helper'"): | 243 | with pytest.raises(ParserSyntaxError, match="cannot call non-vector inline function 'helper'"): |
| 221 | 244 | ||
| 222 | - @pl.function | 245 | + @pl.jit(auto_mutex=False) |
| 223 | - def caller(value: pl.DT_INT64) -> pl.DT_INT64: | 246 | + def caller(value: pl.DT_INT64): |
| 224 | invalid_vf(value) | 247 | invalid_vf(value) |
| 225 | - return value | 248 | + _test_result = value |
| 249 | + | ||
| 250 | + caller.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| @@ -9,10 +9,8 @@ | |||
| 9 | # See LICENSE in the root of the software repository for the full text of the License. | 9 | # See LICENSE in the root of the software repository for the full text of the License. |
| 10 | # ----------------------------------------------------------------------------------------------------------- | 10 | # ----------------------------------------------------------------------------------------------------------- |
| 11 | """Unit tests for TypeResolver.""" | 11 | """Unit tests for TypeResolver.""" |
| 12 | -from __future__ import annotations | ||
| 13 | - | ||
| 14 | # DSL function bodies are parsed as AST, not executed -suppress pyright errors | 12 | # DSL function bodies are parsed as AST, not executed -suppress pyright errors |
| 15 | -# from type-checking the annotations and kwargs inside @pl.function bodies. | 13 | +# from type-checking the annotations and kwargs inside kernel bodies. |
| 16 | import ast | 14 | import ast |
| 17 | from typing import TYPE_CHECKING, Any | 15 | from typing import TYPE_CHECKING, Any |
| 18 | 16 | ||
| @@ -35,6 +33,7 @@ def _make_resolver( | |||
| 35 | return TypeResolver(expr_evaluator=ev, scope_lookup=scope_lookup) | 33 | return TypeResolver(expr_evaluator=ev, scope_lookup=scope_lookup) |
| 36 | 34 | ||
| 37 | 35 | ||
| 36 | + | ||
| 38 | @pytest.mark.parametrize( | 37 | @pytest.mark.parametrize( |
| 39 | "layout_str, expected_layout", | 38 | "layout_str, expected_layout", |
| 40 | [ | 39 | [ |
| @@ -131,13 +130,16 @@ def test_resolve_tensor_layout_with_shape_variable(): | |||
| 131 | 130 | ||
| 132 | 131 | ||
| 133 | def test_function_with_tensor_layout(): | 132 | def test_function_with_tensor_layout(): |
| 134 | - """@pl.function with layout in parameter and return type.""" | 133 | + """Kernel parameter annotations preserve tensor layout.""" |
| 135 | 134 | ||
| 136 | - @pl.function | 135 | + @pl.jit(auto_mutex=False) |
| 137 | def func( | 136 | def func( |
| 138 | x: pl.Tensor[[64, 128], pl.DT_FP16, pl.NZ], | 137 | x: pl.Tensor[[64, 128], pl.DT_FP16, pl.NZ], |
| 139 | - ) -> pl.Tensor[[64, 128], pl.DT_FP16, pl.NZ]: | 138 | + ): |
| 140 | - return x | 139 | + _test_result = x |
| 140 | + | ||
| 141 | + func_program, _ = func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 142 | + func = func_program.get_function(func.__name__) | ||
| 141 | 143 | ||
| 142 | assert isinstance(func, ir.Function) | 144 | assert isinstance(func, ir.Function) |
| 143 | param_type = func.params[0].type | 145 | param_type = func.params[0].type |
| @@ -146,21 +148,19 @@ def test_function_with_tensor_layout(): | |||
| 146 | assert param_type.tensor_view is not None | 148 | assert param_type.tensor_view is not None |
| 147 | assert param_type.tensor_view.layout == ir.TensorLayout.NZ | 149 | assert param_type.tensor_view.layout == ir.TensorLayout.NZ |
| 148 | 150 | ||
| 149 | - ret_type = func.return_types[0] | ||
| 150 | - assert isinstance(ret_type, ir.TensorType) | ||
| 151 | - assert ret_type.tensor_view is not None | ||
| 152 | - assert ret_type.tensor_view.layout == ir.TensorLayout.NZ | ||
| 153 | - | ||
| 154 | 151 | ||
| 155 | def test_function_mixed_layout_and_no_layout(): | 152 | def test_function_mixed_layout_and_no_layout(): |
| 156 | - """@pl.function with some params having layout and some not.""" | 153 | + """Kernel with some params having layout and some not.""" |
| 157 | 154 | ||
| 158 | - @pl.function | 155 | + @pl.jit(auto_mutex=False) |
| 159 | def func( | 156 | def func( |
| 160 | a: pl.Tensor[[64, 128], pl.DT_FP16, pl.NZ], | 157 | a: pl.Tensor[[64, 128], pl.DT_FP16, pl.NZ], |
| 161 | b: pl.Tensor[[64, 128], pl.DT_FP16], | 158 | b: pl.Tensor[[64, 128], pl.DT_FP16], |
| 162 | - ) -> pl.Tensor[[64, 128], pl.DT_FP16]: | 159 | + ): |
| 163 | - return a | 160 | + _test_result = a |
| 161 | + | ||
| 162 | + func_program, _ = func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 163 | + func = func_program.get_function(func.__name__) | ||
| 164 | 164 | ||
| 165 | a_type = func.params[0].type | 165 | a_type = func.params[0].type |
| 166 | b_type = func.params[1].type | 166 | b_type = func.params[1].type |
| @@ -172,34 +172,35 @@ def test_function_mixed_layout_and_no_layout(): | |||
| 172 | 172 | ||
| 173 | 173 | ||
| 174 | def test_program_with_tensor_layout(): | 174 | def test_program_with_tensor_layout(): |
| 175 | - """@pl.program with layout annotations.""" | 175 | + """Kernel with layout annotations.""" |
| 176 | 176 | ||
| 177 | - @pl.program | 177 | + @pl.jit(auto_mutex=False) |
| 178 | - class MyProgram: | 178 | + def compute( |
| 179 | - @pl.function | 179 | + x: pl.Tensor[[64, 128], pl.DT_FP16, pl.NZ], |
| 180 | - def compute( | 180 | + ): |
| 181 | - self, | 181 | + _test_result = x |
| 182 | - x: pl.Tensor[[64, 128], pl.DT_FP16, pl.NZ], | ||
| 183 | - ) -> pl.Tensor[[64, 128], pl.DT_FP16, pl.NZ]: | ||
| 184 | - return x | ||
| 185 | 182 | ||
| 186 | - assert isinstance(MyProgram, ir.Program) | 183 | + compute_program, _ = compute.to_kernel_def().parse_target_program(ir.SectionKind.Vector) |
| 187 | - func = list(MyProgram.functions.values())[0] | 184 | + compute = compute_program.get_function(compute.__name__) |
| 188 | - param_type = func.params[0].type | 185 | + |
| 186 | + param_type = compute.params[0].type | ||
| 189 | assert isinstance(param_type, ir.TensorType) | 187 | assert isinstance(param_type, ir.TensorType) |
| 190 | assert param_type.tensor_view is not None | 188 | assert param_type.tensor_view is not None |
| 191 | assert param_type.tensor_view.layout == ir.TensorLayout.NZ | 189 | assert param_type.tensor_view.layout == ir.TensorLayout.NZ |
| 192 | 190 | ||
| 193 | 191 | ||
| 194 | def test_function_layout_from_closure_variable(): | 192 | def test_function_layout_from_closure_variable(): |
| 195 | - """@pl.function with layout from closure variable.""" | 193 | + """Kernel with layout from closure variable.""" |
| 196 | layout = pl.NZ | 194 | layout = pl.NZ |
| 197 | 195 | ||
| 198 | - @pl.function | 196 | + @pl.jit(auto_mutex=False) |
| 199 | def func( | 197 | def func( |
| 200 | x: pl.Tensor[[64, 128], pl.DT_FP16, layout], | 198 | x: pl.Tensor[[64, 128], pl.DT_FP16, layout], |
| 201 | - ) -> pl.Tensor[[64, 128], pl.DT_FP16, layout]: | 199 | + ): |
| 202 | - return x | 200 | + _test_result = x |
| 201 | + | ||
| 202 | + func_program, _ = func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 203 | + func = func_program.get_function(func.__name__) | ||
| 203 | 204 | ||
| 204 | param_type = func.params[0].type | 205 | param_type = func.params[0].type |
| 205 | assert isinstance(param_type, ir.TensorType) | 206 | assert isinstance(param_type, ir.TensorType) |
| @@ -217,11 +218,14 @@ def test_function_layout_from_closure_variable(): | |||
| 217 | def test_parametrized_layout(layout, expected): | 218 | def test_parametrized_layout(layout, expected): |
| 218 | """pytest.mark.parametrize with layout.""" | 219 | """pytest.mark.parametrize with layout.""" |
| 219 | 220 | ||
| 220 | - @pl.function | 221 | + @pl.jit(auto_mutex=False) |
| 221 | def func( | 222 | def func( |
| 222 | x: pl.Tensor[[64, 128], pl.DT_FP16, layout], | 223 | x: pl.Tensor[[64, 128], pl.DT_FP16, layout], |
| 223 | - ) -> pl.Tensor[[64, 128], pl.DT_FP16, layout]: | 224 | + ): |
| 224 | - return x | 225 | + _test_result = x |
| 226 | + | ||
| 227 | + func_program, _ = func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 228 | + func = func_program.get_function(func.__name__) | ||
| 225 | 229 | ||
| 226 | param_type = func.params[0].type | 230 | param_type = func.params[0].type |
| 227 | assert isinstance(param_type, ir.TensorType) | 231 | assert isinstance(param_type, ir.TensorType) |
| @@ -230,13 +234,16 @@ def test_parametrized_layout(layout, expected): | |||
| 230 | 234 | ||
| 231 | 235 | ||
| 232 | def test_function_with_dn_layout(): | 236 | def test_function_with_dn_layout(): |
| 233 | - """@pl.function with DN layout for column-major tensors.""" | 237 | + """Kernel with DN layout for column-major tensors.""" |
| 234 | 238 | ||
| 235 | - @pl.function | 239 | + @pl.jit(auto_mutex=False) |
| 236 | def func( | 240 | def func( |
| 237 | x: pl.Tensor[[16, 1], pl.DT_FP16, pl.DN], | 241 | x: pl.Tensor[[16, 1], pl.DT_FP16, pl.DN], |
| 238 | - ) -> pl.Tensor[[16, 1], pl.DT_FP16, pl.DN]: | 242 | + ): |
| 239 | - return x | 243 | + _test_result = x |
| 244 | + | ||
| 245 | + func_program, _ = func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 246 | + func = func_program.get_function(func.__name__) | ||
| 240 | 247 | ||
| 241 | param_type = func.params[0].type | 248 | param_type = func.params[0].type |
| 242 | assert isinstance(param_type, ir.TensorType) | 249 | assert isinstance(param_type, ir.TensorType) |
| @@ -4,7 +4,7 @@ | |||
| 4 | # Please refer to the License for details. You may not use this file except in compliance with the License. | 4 | # Please refer to the License for details. You may not use this file except in compliance with the License. |
| 5 | # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | 5 | # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, |
| 6 | # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | 6 | # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. |
| 7 | -# See the License in the root of the software repository for the full text of the License. | 7 | +# See LICENSE in the root of the software repository for the full text of the License. |
| 8 | # ----------------------------------------------------------------------------------------------------------- | 8 | # ----------------------------------------------------------------------------------------------------------- |
| 9 | 9 | ||
| 10 | """UT for make_tuple chain attribute access: t.a.b and t.field.method().""" | 10 | """UT for make_tuple chain attribute access: t.a.b and t.field.method().""" |
| @@ -26,12 +26,15 @@ def _find_assign(func, name): | |||
| 26 | def test_make_tuple_chain_attr_const_fold(): | 26 | def test_make_tuple_chain_attr_const_fold(): |
| 27 | """t.a.b where inner is const MakeTuple — folds to element.""" | 27 | """t.a.b where inner is const MakeTuple — folds to element.""" |
| 28 | 28 | ||
| 29 | - @pl.function | 29 | + @pl.jit(auto_mutex=False) |
| 30 | - def kernel(): | 30 | + def kernel(_jit_entry: pl.DT_INT64): |
| 31 | inner = pl.make_tuple(x=10, y=20) | 31 | inner = pl.make_tuple(x=10, y=20) |
| 32 | outer = pl.make_tuple(a=inner) | 32 | outer = pl.make_tuple(a=inner) |
| 33 | val: pl.DT_INT64 = outer.a.x | 33 | val: pl.DT_INT64 = outer.a.x |
| 34 | - return val | 34 | + _test_result = val |
| 35 | + | ||
| 36 | + kernel_program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 37 | + kernel = kernel_program.get_function(kernel.__name__) | ||
| 35 | 38 | ||
| 36 | assert isinstance(kernel, ir.Function) | 39 | assert isinstance(kernel, ir.Function) |
| 37 | 40 | ||
| @@ -39,12 +42,15 @@ def test_make_tuple_chain_attr_const_fold(): | |||
| 39 | def test_make_tuple_chain_attr_getitem(): | 42 | def test_make_tuple_chain_attr_getitem(): |
| 40 | """t.a.b where inner is a runtime Var — lowers to nested GetItemExpr.""" | 43 | """t.a.b where inner is a runtime Var — lowers to nested GetItemExpr.""" |
| 41 | 44 | ||
| 42 | - @pl.function | 45 | + @pl.jit(auto_mutex=False) |
| 43 | def kernel(x: pl.DT_INT32): | 46 | def kernel(x: pl.DT_INT32): |
| 44 | s = pl.struct("S", f=0) | 47 | s = pl.struct("S", f=0) |
| 45 | t = pl.make_tuple(s=s) | 48 | t = pl.make_tuple(s=s) |
| 46 | val: pl.DT_INT32 = t.s.f | 49 | val: pl.DT_INT32 = t.s.f |
| 47 | - return val | 50 | + _test_result = val |
| 51 | + | ||
| 52 | + kernel_program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 53 | + kernel = kernel_program.get_function(kernel.__name__) | ||
| 48 | 54 | ||
| 49 | assert isinstance(kernel, ir.Function) | 55 | assert isinstance(kernel, ir.Function) |
| 50 | 56 | ||
| @@ -52,13 +58,16 @@ def test_make_tuple_chain_attr_getitem(): | |||
| 52 | def test_make_tuple_chain_three_level(): | 58 | def test_make_tuple_chain_three_level(): |
| 53 | """t.a.b.c — three-level chain.""" | 59 | """t.a.b.c — three-level chain.""" |
| 54 | 60 | ||
| 55 | - @pl.function | 61 | + @pl.jit(auto_mutex=False) |
| 56 | - def kernel(): | 62 | + def kernel(_jit_entry: pl.DT_INT64): |
| 57 | leaf = pl.make_tuple(v=42) | 63 | leaf = pl.make_tuple(v=42) |
| 58 | mid = pl.make_tuple(b=leaf) | 64 | mid = pl.make_tuple(b=leaf) |
| 59 | root = pl.make_tuple(a=mid) | 65 | root = pl.make_tuple(a=mid) |
| 60 | val: pl.DT_INT64 = root.a.b.v | 66 | val: pl.DT_INT64 = root.a.b.v |
| 61 | - return val | 67 | + _test_result = val |
| 68 | + | ||
| 69 | + kernel_program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 70 | + kernel = kernel_program.get_function(kernel.__name__) | ||
| 62 | 71 | ||
| 63 | assert isinstance(kernel, ir.Function) | 72 | assert isinstance(kernel, ir.Function) |
| 64 | 73 | ||
| @@ -75,12 +84,15 @@ def test_chain_const_fold_lowers_to_static_element(): | |||
| 75 | GetItemExpr over a MakeTuple. | 84 | GetItemExpr over a MakeTuple. |
| 76 | """ | 85 | """ |
| 77 | 86 | ||
| 78 | - @pl.function | 87 | + @pl.jit(auto_mutex=False) |
| 79 | - def kernel(): | 88 | + def kernel(_jit_entry: pl.DT_INT64): |
| 80 | inner = pl.make_tuple(x=10, y=20) | 89 | inner = pl.make_tuple(x=10, y=20) |
| 81 | outer = pl.make_tuple(a=inner) | 90 | outer = pl.make_tuple(a=inner) |
| 82 | val: pl.DT_INT64 = outer.a.x | 91 | val: pl.DT_INT64 = outer.a.x |
| 83 | - return val | 92 | + _test_result = val |
| 93 | + | ||
| 94 | + kernel_program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 95 | + kernel = kernel_program.get_function(kernel.__name__) | ||
| 84 | 96 | ||
| 85 | stmt = _find_assign(kernel, "val") | 97 | stmt = _find_assign(kernel, "val") |
| 86 | assert isinstance(stmt.value, ir.ConstInt) | 98 | assert isinstance(stmt.value, ir.ConstInt) |
| @@ -90,12 +102,15 @@ def test_chain_const_fold_lowers_to_static_element(): | |||
| 90 | def test_chain_const_fold_arithmetic(): | 102 | def test_chain_const_fold_arithmetic(): |
| 91 | """Chain reads fold to constants and participate in const arithmetic.""" | 103 | """Chain reads fold to constants and participate in const arithmetic.""" |
| 92 | 104 | ||
| 93 | - @pl.function | 105 | + @pl.jit(auto_mutex=False) |
| 94 | - def kernel(): | 106 | + def kernel(_jit_entry: pl.DT_INT64): |
| 95 | inner = pl.make_tuple(x=10, y=20) | 107 | inner = pl.make_tuple(x=10, y=20) |
| 96 | outer = pl.make_tuple(a=inner) | 108 | outer = pl.make_tuple(a=inner) |
| 97 | val: pl.DT_INT64 = outer.a.x + outer.a.y | 109 | val: pl.DT_INT64 = outer.a.x + outer.a.y |
| 98 | - return val | 110 | + _test_result = val |
| 111 | + | ||
| 112 | + kernel_program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 113 | + kernel = kernel_program.get_function(kernel.__name__) | ||
| 99 | 114 | ||
| 100 | stmt = _find_assign(kernel, "val") | 115 | stmt = _find_assign(kernel, "val") |
| 101 | assert isinstance(stmt.value, ir.ConstInt) | 116 | assert isinstance(stmt.value, ir.ConstInt) |
| @@ -105,12 +120,15 @@ def test_chain_const_fold_arithmetic(): | |||
| 105 | def test_chain_subscript_after_attr(): | 120 | def test_chain_subscript_after_attr(): |
| 106 | """Subscript on a folded chain element resolves to the constant.""" | 121 | """Subscript on a folded chain element resolves to the constant.""" |
| 107 | 122 | ||
| 108 | - @pl.function | 123 | + @pl.jit(auto_mutex=False) |
| 109 | - def kernel(): | 124 | + def kernel(_jit_entry: pl.DT_INT64): |
| 110 | inner = pl.make_tuple(x=10, y=20) | 125 | inner = pl.make_tuple(x=10, y=20) |
| 111 | outer = pl.make_tuple(a=inner) | 126 | outer = pl.make_tuple(a=inner) |
| 112 | val: pl.DT_INT64 = outer.a[0] | 127 | val: pl.DT_INT64 = outer.a[0] |
| 113 | - return val | 128 | + _test_result = val |
| 129 | + | ||
| 130 | + kernel_program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 131 | + kernel = kernel_program.get_function(kernel.__name__) | ||
| 114 | 132 | ||
| 115 | stmt = _find_assign(kernel, "val") | 133 | stmt = _find_assign(kernel, "val") |
| 116 | assert isinstance(stmt.value, ir.ConstInt) | 134 | assert isinstance(stmt.value, ir.ConstInt) |
| @@ -124,12 +142,15 @@ def test_chain_subscript_after_attr(): | |||
| 124 | def test_chain_runtime_lowers_to_nested_getitem(): | 142 | def test_chain_runtime_lowers_to_nested_getitem(): |
| 125 | """t.s.f folds the const t.s hop to the runtime struct Var, then GetItemExpr.""" | 143 | """t.s.f folds the const t.s hop to the runtime struct Var, then GetItemExpr.""" |
| 126 | 144 | ||
| 127 | - @pl.function | 145 | + @pl.jit(auto_mutex=False) |
| 128 | def kernel(x: pl.DT_INT32): | 146 | def kernel(x: pl.DT_INT32): |
| 129 | s = pl.struct("S", f=0) | 147 | s = pl.struct("S", f=0) |
| 130 | t = pl.make_tuple(s=s) | 148 | t = pl.make_tuple(s=s) |
| 131 | val: pl.DT_INT32 = t.s.f | 149 | val: pl.DT_INT32 = t.s.f |
| 132 | - return val | 150 | + _test_result = val |
| 151 | + | ||
| 152 | + kernel_program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 153 | + kernel = kernel_program.get_function(kernel.__name__) | ||
| 133 | 154 | ||
| 134 | stmt = _find_assign(kernel, "val") | 155 | stmt = _find_assign(kernel, "val") |
| 135 | assert isinstance(stmt.value, ir.GetItemExpr) | 156 | assert isinstance(stmt.value, ir.GetItemExpr) |
| @@ -141,12 +162,15 @@ def test_chain_runtime_lowers_to_nested_getitem(): | |||
| 141 | def test_make_tuple_chain_with_kernel_arg(): | 162 | def test_make_tuple_chain_with_kernel_arg(): |
| 142 | """Chain access still lowers when the kernel carries runtime args.""" | 163 | """Chain access still lowers when the kernel carries runtime args.""" |
| 143 | 164 | ||
| 144 | - @pl.function | 165 | + @pl.jit(auto_mutex=False) |
| 145 | def kernel(x: pl.DT_INT64): | 166 | def kernel(x: pl.DT_INT64): |
| 146 | inner = pl.make_tuple(x=10) | 167 | inner = pl.make_tuple(x=10) |
| 147 | outer = pl.make_tuple(a=inner) | 168 | outer = pl.make_tuple(a=inner) |
| 148 | val: pl.DT_INT64 = outer.a.x | 169 | val: pl.DT_INT64 = outer.a.x |
| 149 | - return val | 170 | + _test_result = val |
| 171 | + | ||
| 172 | + kernel_program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 173 | + kernel = kernel_program.get_function(kernel.__name__) | ||
| 150 | 174 | ||
| 151 | assert isinstance(kernel, ir.Function) | 175 | assert isinstance(kernel, ir.Function) |
| 152 | 176 | ||
| @@ -158,14 +182,17 @@ def test_make_tuple_chain_with_kernel_arg(): | |||
| 158 | def test_make_tuple_chain_four_level(): | 182 | def test_make_tuple_chain_four_level(): |
| 159 | """t.a.b.c.d — four-level const chain folds to the static element.""" | 183 | """t.a.b.c.d — four-level const chain folds to the static element.""" |
| 160 | 184 | ||
| 161 | - @pl.function | 185 | + @pl.jit(auto_mutex=False) |
| 162 | - def kernel(): | 186 | + def kernel(_jit_entry: pl.DT_INT64): |
| 163 | l1 = pl.make_tuple(w=1) | 187 | l1 = pl.make_tuple(w=1) |
| 164 | l2 = pl.make_tuple(c=l1) | 188 | l2 = pl.make_tuple(c=l1) |
| 165 | l3 = pl.make_tuple(b=l2) | 189 | l3 = pl.make_tuple(b=l2) |
| 166 | root = pl.make_tuple(a=l3) | 190 | root = pl.make_tuple(a=l3) |
| 167 | val: pl.DT_INT64 = root.a.b.c.w | 191 | val: pl.DT_INT64 = root.a.b.c.w |
| 168 | - return val | 192 | + _test_result = val |
| 193 | + | ||
| 194 | + kernel_program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 195 | + kernel = kernel_program.get_function(kernel.__name__) | ||
| 169 | 196 | ||
| 170 | stmt = _find_assign(kernel, "val") | 197 | stmt = _find_assign(kernel, "val") |
| 171 | assert isinstance(stmt.value, ir.ConstInt) | 198 | assert isinstance(stmt.value, ir.ConstInt) |
| @@ -178,13 +205,16 @@ def test_chain_parallel_branches(): | |||
| 178 | root.a.c.w folds to l1.w and root.b.w folds to l1.w; both are static. | 205 | root.a.c.w folds to l1.w and root.b.w folds to l1.w; both are static. |
| 179 | """ | 206 | """ |
| 180 | 207 | ||
| 181 | - @pl.function | 208 | + @pl.jit(auto_mutex=False) |
| 182 | - def kernel(): | 209 | + def kernel(_jit_entry: pl.DT_INT64): |
| 183 | l1 = pl.make_tuple(w=1) | 210 | l1 = pl.make_tuple(w=1) |
| 184 | l2 = pl.make_tuple(c=l1) | 211 | l2 = pl.make_tuple(c=l1) |
| 185 | root = pl.make_tuple(a=l2, b=l1) | 212 | root = pl.make_tuple(a=l2, b=l1) |
| 186 | val: pl.DT_INT64 = root.a.c.w + root.b.w | 213 | val: pl.DT_INT64 = root.a.c.w + root.b.w |
| 187 | - return val | 214 | + _test_result = val |
| 215 | + | ||
| 216 | + kernel_program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 217 | + kernel = kernel_program.get_function(kernel.__name__) | ||
| 188 | 218 | ||
| 189 | _expr_tmps = [ | 219 | _expr_tmps = [ |
| 190 | stmt for stmt in kernel.body.stmts | 220 | stmt for stmt in kernel.body.stmts |
| @@ -202,12 +232,15 @@ def test_chain_parallel_branches(): | |||
| 202 | def test_chain_make_tuple_of_struct_array_elem(): | 232 | def test_chain_make_tuple_of_struct_array_elem(): |
| 203 | """make_tuple wrapping a struct_array element: t.s.v lowers via slots[0].""" | 233 | """make_tuple wrapping a struct_array element: t.s.v lowers via slots[0].""" |
| 204 | 234 | ||
| 205 | - @pl.function | 235 | + @pl.jit(auto_mutex=False) |
| 206 | - def kernel(): | 236 | + def kernel(_jit_entry: pl.DT_INT64): |
| 207 | slots = pl.struct_array(2, "Slot", v=0) | 237 | slots = pl.struct_array(2, "Slot", v=0) |
| 208 | t = pl.make_tuple(s=slots[0]) | 238 | t = pl.make_tuple(s=slots[0]) |
| 209 | val: pl.DT_INT32 = t.s.v | 239 | val: pl.DT_INT32 = t.s.v |
| 210 | - return val | 240 | + _test_result = val |
| 241 | + | ||
| 242 | + kernel_program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 243 | + kernel = kernel_program.get_function(kernel.__name__) | ||
| 211 | 244 | ||
| 212 | stmt = _find_assign(kernel, "val") | 245 | stmt = _find_assign(kernel, "val") |
| 213 | assert isinstance(stmt.value, ir.GetItemExpr) | 246 | assert isinstance(stmt.value, ir.GetItemExpr) |
| @@ -218,11 +251,14 @@ def test_chain_make_tuple_of_struct_array_elem(): | |||
| 218 | def test_chain_same_level_multi_field_folds(): | 251 | def test_chain_same_level_multi_field_folds(): |
| 219 | """Same-level fields on a const MakeTuple fold to a constant.""" | 252 | """Same-level fields on a const MakeTuple fold to a constant.""" |
| 220 | 253 | ||
| 221 | - @pl.function | 254 | + @pl.jit(auto_mutex=False) |
| 222 | - def kernel(): | 255 | + def kernel(_jit_entry: pl.DT_INT64): |
| 223 | inner = pl.make_tuple(x=10, y=20) | 256 | inner = pl.make_tuple(x=10, y=20) |
| 224 | val: pl.DT_INT64 = inner.x + inner.y | 257 | val: pl.DT_INT64 = inner.x + inner.y |
| 225 | - return val | 258 | + _test_result = val |
| 259 | + | ||
| 260 | + kernel_program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 261 | + kernel = kernel_program.get_function(kernel.__name__) | ||
| 226 | 262 | ||
| 227 | stmt = _find_assign(kernel, "val") | 263 | stmt = _find_assign(kernel, "val") |
| 228 | assert isinstance(stmt.value, ir.ConstInt) | 264 | assert isinstance(stmt.value, ir.ConstInt) |
| @@ -236,13 +272,16 @@ def test_chain_same_level_multi_field_folds(): | |||
| 236 | def test_chain_tile_group_next(): | 272 | def test_chain_tile_group_next(): |
| 237 | """info.buf.next() lowers to a dynamic tile select + mutex id.""" | 273 | """info.buf.next() lowers to a dynamic tile select + mutex id.""" |
| 238 | 274 | ||
| 239 | - @pl.function | 275 | + @pl.jit(auto_mutex=False) |
| 240 | - def kernel(): | 276 | + def kernel(_jit_entry: pl.DT_INT64): |
| 241 | tt = pl.TileType(shape=[64, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 277 | tt = pl.TileType(shape=[64, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 242 | db = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[20, 21]) | 278 | db = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[20, 21]) |
| 243 | info = pl.make_tuple(buf=db) | 279 | info = pl.make_tuple(buf=db) |
| 244 | t = info.buf.next() | 280 | t = info.buf.next() |
| 245 | - return t | 281 | + _test_result = t |
| 282 | + | ||
| 283 | + kernel_program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 284 | + kernel = kernel_program.get_function(kernel.__name__) | ||
| 246 | 285 | ||
| 247 | idx = next( | 286 | idx = next( |
| 248 | stmt for stmt in kernel.body.stmts | 287 | stmt for stmt in kernel.body.stmts |
| @@ -260,13 +299,16 @@ def test_chain_tile_group_next(): | |||
| 260 | def test_chain_tile_group_current(): | 299 | def test_chain_tile_group_current(): |
| 261 | """info.buf.current() lowers without advancing the cursor.""" | 300 | """info.buf.current() lowers without advancing the cursor.""" |
| 262 | 301 | ||
| 263 | - @pl.function | 302 | + @pl.jit(auto_mutex=False) |
| 264 | - def kernel(): | 303 | + def kernel(_jit_entry: pl.DT_INT64): |
| 265 | tt = pl.TileType(shape=[64, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 304 | tt = pl.TileType(shape=[64, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 266 | db = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[20, 21]) | 305 | db = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[20, 21]) |
| 267 | info = pl.make_tuple(buf=db) | 306 | info = pl.make_tuple(buf=db) |
| 268 | t = info.buf.current() | 307 | t = info.buf.current() |
| 269 | - return t | 308 | + _test_result = t |
| 309 | + | ||
| 310 | + kernel_program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 311 | + kernel = kernel_program.get_function(kernel.__name__) | ||
| 270 | 312 | ||
| 271 | mutex = next( | 313 | mutex = next( |
| 272 | stmt | 314 | stmt |
| @@ -279,13 +321,16 @@ def test_chain_tile_group_current(): | |||
| 279 | def test_chain_tile_group_previous(): | 321 | def test_chain_tile_group_previous(): |
| 280 | """info.buf.previous() lowers to a dynamic tile select.""" | 322 | """info.buf.previous() lowers to a dynamic tile select.""" |
| 281 | 323 | ||
| 282 | - @pl.function | 324 | + @pl.jit(auto_mutex=False) |
| 283 | - def kernel(): | 325 | + def kernel(_jit_entry: pl.DT_INT64): |
| 284 | tt = pl.TileType(shape=[64, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 326 | tt = pl.TileType(shape=[64, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 285 | db = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[20, 21]) | 327 | db = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[20, 21]) |
| 286 | info = pl.make_tuple(buf=db) | 328 | info = pl.make_tuple(buf=db) |
| 287 | t = info.buf.previous() | 329 | t = info.buf.previous() |
| 288 | - return t | 330 | + _test_result = t |
| 331 | + | ||
| 332 | + kernel_program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 333 | + kernel = kernel_program.get_function(kernel.__name__) | ||
| 289 | 334 | ||
| 290 | mutex = next( | 335 | mutex = next( |
| 291 | stmt | 336 | stmt |
| @@ -298,15 +343,18 @@ def test_chain_tile_group_previous(): | |||
| 298 | def test_chain_tile_group_method_sequence(): | 343 | def test_chain_tile_group_method_sequence(): |
| 299 | """next/next/previous sequence keeps compiling and maintains the cursor.""" | 344 | """next/next/previous sequence keeps compiling and maintains the cursor.""" |
| 300 | 345 | ||
| 301 | - @pl.function | 346 | + @pl.jit(auto_mutex=False) |
| 302 | - def kernel(): | 347 | + def kernel(_jit_entry: pl.DT_INT64): |
| 303 | tt = pl.TileType(shape=[64, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 348 | tt = pl.TileType(shape=[64, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 304 | db = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[20, 21]) | 349 | db = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[20, 21]) |
| 305 | info = pl.make_tuple(buf=db) | 350 | info = pl.make_tuple(buf=db) |
| 306 | t1 = info.buf.next() | 351 | t1 = info.buf.next() |
| 307 | info.buf.next() | 352 | info.buf.next() |
| 308 | info.buf.previous() | 353 | info.buf.previous() |
| 309 | - return t1 | 354 | + _test_result = t1 |
| 355 | + | ||
| 356 | + kernel_program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 357 | + kernel = kernel_program.get_function(kernel.__name__) | ||
| 310 | 358 | ||
| 311 | assert isinstance(kernel, ir.Function) | 359 | assert isinstance(kernel, ir.Function) |
| 312 | 360 | ||
| @@ -314,13 +362,16 @@ def test_chain_tile_group_method_sequence(): | |||
| 314 | def test_chain_tile_group_single_slot(): | 362 | def test_chain_tile_group_single_slot(): |
| 315 | """Single-slot tile group: no cursor, static const mutex id.""" | 363 | """Single-slot tile group: no cursor, static const mutex id.""" |
| 316 | 364 | ||
| 317 | - @pl.function | 365 | + @pl.jit(auto_mutex=False) |
| 318 | - def kernel(): | 366 | + def kernel(_jit_entry: pl.DT_INT64): |
| 319 | tt = pl.TileType(shape=[64, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 367 | tt = pl.TileType(shape=[64, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 320 | db = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[20]) | 368 | db = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[20]) |
| 321 | info = pl.make_tuple(buf=db) | 369 | info = pl.make_tuple(buf=db) |
| 322 | t = info.buf.next() | 370 | t = info.buf.next() |
| 323 | - return t | 371 | + _test_result = t |
| 372 | + | ||
| 373 | + kernel_program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 374 | + kernel = kernel_program.get_function(kernel.__name__) | ||
| 324 | 375 | ||
| 325 | mutex = next( | 376 | mutex = next( |
| 326 | stmt | 377 | stmt |
| @@ -334,8 +385,8 @@ def test_chain_tile_group_single_slot(): | |||
| 334 | def test_chain_tile_group_acc_multi_slot(): | 385 | def test_chain_tile_group_acc_multi_slot(): |
| 335 | """Chained next() on an Acc-memory multi-slot tile_group with valid_shape/compact.""" | 386 | """Chained next() on an Acc-memory multi-slot tile_group with valid_shape/compact.""" |
| 336 | 387 | ||
| 337 | - @pl.function | 388 | + @pl.jit(auto_mutex=False) |
| 338 | - def kernel(): | 389 | + def kernel(_jit_entry: pl.DT_INT64): |
| 339 | tt = pl.TileType( | 390 | tt = pl.TileType( |
| 340 | shape=[64, 64], | 391 | shape=[64, 64], |
| 341 | dtype=pl.DT_FP32, | 392 | dtype=pl.DT_FP32, |
| @@ -346,7 +397,10 @@ def test_chain_tile_group_acc_multi_slot(): | |||
| 346 | acc_db = pl.make_tile_group(type=tt, addrs=0x20000, mutex_ids=[2, 3]) | 397 | acc_db = pl.make_tile_group(type=tt, addrs=0x20000, mutex_ids=[2, 3]) |
| 347 | tensor_info = pl.make_tuple(acc_db=acc_db) | 398 | tensor_info = pl.make_tuple(acc_db=acc_db) |
| 348 | acc = tensor_info.acc_db.next() | 399 | acc = tensor_info.acc_db.next() |
| 349 | - return acc | 400 | + _test_result = acc |
| 401 | + | ||
| 402 | + kernel_program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 403 | + kernel = kernel_program.get_function(kernel.__name__) | ||
| 350 | 404 | ||
| 351 | idx = next( | 405 | idx = next( |
| 352 | stmt for stmt in kernel.body.stmts | 406 | stmt for stmt in kernel.body.stmts |
| @@ -365,12 +419,14 @@ def test_chain_tile_group_unknown_method(): | |||
| 365 | """Unknown method on a chained tile_group raises a ParserSyntaxError.""" | 419 | """Unknown method on a chained tile_group raises a ParserSyntaxError.""" |
| 366 | with pytest.raises(ParserSyntaxError): | 420 | with pytest.raises(ParserSyntaxError): |
| 367 | 421 | ||
| 368 | - @pl.function | 422 | + @pl.jit(auto_mutex=False) |
| 369 | - def kernel(): | 423 | + def kernel(_jit_entry: pl.DT_INT64): |
| 370 | tt = pl.TileType(shape=[64, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) | 424 | tt = pl.TileType(shape=[64, 128], dtype=pl.DT_FP16, target_memory=pl.MemorySpace.Vec) |
| 371 | db = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[20]) | 425 | db = pl.make_tile_group(type=tt, addrs=0, mutex_ids=[20]) |
| 372 | info = pl.make_tuple(buf=db) | 426 | info = pl.make_tuple(buf=db) |
| 373 | - return info.buf.foo() | 427 | + _test_result = info.buf.foo() |
| 428 | + | ||
| 429 | + kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 374 | 430 | ||
| 375 | 431 | ||
| 376 | # ============================================================================= | 432 | # ============================================================================= |
| @@ -381,71 +437,83 @@ def test_err_chain_nonexistent_field(): | |||
| 381 | """Chained read of a nonexistent field raises UnsupportedFeatureError.""" | 437 | """Chained read of a nonexistent field raises UnsupportedFeatureError.""" |
| 382 | with pytest.raises(UnsupportedFeatureError): | 438 | with pytest.raises(UnsupportedFeatureError): |
| 383 | 439 | ||
| 384 | - @pl.function | 440 | + @pl.jit(auto_mutex=False) |
| 385 | - def kernel(): | 441 | + def kernel(_jit_entry: pl.DT_INT64): |
| 386 | inner = pl.make_tuple(x=10) | 442 | inner = pl.make_tuple(x=10) |
| 387 | outer = pl.make_tuple(a=inner) | 443 | outer = pl.make_tuple(a=inner) |
| 388 | val: pl.DT_INT64 = outer.a.non_exist | 444 | val: pl.DT_INT64 = outer.a.non_exist |
| 389 | - return val | 445 | + _test_result = val |
| 446 | + | ||
| 447 | + kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 390 | 448 | ||
| 391 | 449 | ||
| 392 | def test_err_chain_on_scalar_element(): | 450 | def test_err_chain_on_scalar_element(): |
| 393 | """Chaining onto a scalar element raises UnsupportedFeatureError.""" | 451 | """Chaining onto a scalar element raises UnsupportedFeatureError.""" |
| 394 | with pytest.raises(UnsupportedFeatureError): | 452 | with pytest.raises(UnsupportedFeatureError): |
| 395 | 453 | ||
| 396 | - @pl.function | 454 | + @pl.jit(auto_mutex=False) |
| 397 | - def kernel(): | 455 | + def kernel(_jit_entry: pl.DT_INT64): |
| 398 | inner = pl.make_tuple(x=10, y=20) | 456 | inner = pl.make_tuple(x=10, y=20) |
| 399 | outer = pl.make_tuple(a=inner) | 457 | outer = pl.make_tuple(a=inner) |
| 400 | val: pl.DT_INT64 = outer.a.x.foo | 458 | val: pl.DT_INT64 = outer.a.x.foo |
| 401 | - return val | 459 | + _test_result = val |
| 460 | + | ||
| 461 | + kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 402 | 462 | ||
| 403 | 463 | ||
| 404 | def test_err_chain_write(): | 464 | def test_err_chain_write(): |
| 405 | """make_tuple is immutable; chained write raises ParserSyntaxError.""" | 465 | """make_tuple is immutable; chained write raises ParserSyntaxError.""" |
| 406 | with pytest.raises(ParserSyntaxError, match="immutable named tuple field"): | 466 | with pytest.raises(ParserSyntaxError, match="immutable named tuple field"): |
| 407 | 467 | ||
| 408 | - @pl.function | 468 | + @pl.jit(auto_mutex=False) |
| 409 | - def kernel(): | 469 | + def kernel(_jit_entry: pl.DT_INT64): |
| 410 | inner = pl.make_tuple(x=10) | 470 | inner = pl.make_tuple(x=10) |
| 411 | outer = pl.make_tuple(a=inner) | 471 | outer = pl.make_tuple(a=inner) |
| 412 | outer.a.x = 99 | 472 | outer.a.x = 99 |
| 413 | val: pl.DT_INT64 = outer.a.x | 473 | val: pl.DT_INT64 = outer.a.x |
| 414 | - return val | 474 | + _test_result = val |
| 475 | + | ||
| 476 | + kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 415 | 477 | ||
| 416 | 478 | ||
| 417 | def test_err_named_tuple_array_field_element_write(): | 479 | def test_err_named_tuple_array_field_element_write(): |
| 418 | """make_tuple is immutable; array field element write raises ParserSyntaxError.""" | 480 | """make_tuple is immutable; array field element write raises ParserSyntaxError.""" |
| 419 | with pytest.raises(ParserSyntaxError, match="immutable named tuple field"): | 481 | with pytest.raises(ParserSyntaxError, match="immutable named tuple field"): |
| 420 | 482 | ||
| 421 | - @pl.function | 483 | + @pl.jit(auto_mutex=False) |
| 422 | - def kernel(): | 484 | + def kernel(_jit_entry: pl.DT_INT64): |
| 423 | t = pl.make_tuple(a=[1, 2, 3]) | 485 | t = pl.make_tuple(a=[1, 2, 3]) |
| 424 | t.a[0] = 99 | 486 | t.a[0] = 99 |
| 425 | val: pl.DT_INT64 = t.a[0] | 487 | val: pl.DT_INT64 = t.a[0] |
| 426 | - return val | 488 | + _test_result = val |
| 489 | + | ||
| 490 | + kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 427 | 491 | ||
| 428 | 492 | ||
| 429 | def test_err_chain_string_subscript(): | 493 | def test_err_chain_string_subscript(): |
| 430 | """String subscript on a make_tuple raises ParserSyntaxError.""" | 494 | """String subscript on a make_tuple raises ParserSyntaxError.""" |
| 431 | with pytest.raises(ParserSyntaxError, match="integer"): | 495 | with pytest.raises(ParserSyntaxError, match="integer"): |
| 432 | 496 | ||
| 433 | - @pl.function | 497 | + @pl.jit(auto_mutex=False) |
| 434 | - def kernel(): | 498 | + def kernel(_jit_entry: pl.DT_INT64): |
| 435 | inner = pl.make_tuple(x=10) | 499 | inner = pl.make_tuple(x=10) |
| 436 | outer = pl.make_tuple(a=inner) | 500 | outer = pl.make_tuple(a=inner) |
| 437 | val: pl.DT_INT64 = outer["a"] | 501 | val: pl.DT_INT64 = outer["a"] |
| 438 | - return val | 502 | + _test_result = val |
| 503 | + | ||
| 504 | + kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 439 | 505 | ||
| 440 | 506 | ||
| 441 | def test_err_struct_field_holding_make_tuple(): | 507 | def test_err_struct_field_holding_make_tuple(): |
| 442 | """Chained read through a struct field holding a make_tuple is unsupported.""" | 508 | """Chained read through a struct field holding a make_tuple is unsupported.""" |
| 443 | with pytest.raises(UnsupportedFeatureError): | 509 | with pytest.raises(UnsupportedFeatureError): |
| 444 | 510 | ||
| 445 | - @pl.function | 511 | + @pl.jit(auto_mutex=False) |
| 446 | - def kernel(): | 512 | + def kernel(_jit_entry: pl.DT_INT64): |
| 447 | inner = pl.make_tuple(x=10) | 513 | inner = pl.make_tuple(x=10) |
| 448 | s = pl.struct("S", t=0) | 514 | s = pl.struct("S", t=0) |
| 449 | s.t = inner | 515 | s.t = inner |
| 450 | val: pl.DT_INT32 = s.t.x | 516 | val: pl.DT_INT32 = s.t.x |
| 451 | - return val | 517 | + _test_result = val |
| 518 | + | ||
| 519 | + kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| @@ -10,27 +10,25 @@ | |||
| 10 | # ----------------------------------------------------------------------------------------------------------- | 10 | # ----------------------------------------------------------------------------------------------------------- |
| 11 | """Comprehensive tests for MemRef, MemorySpace, and TileView.""" | 11 | """Comprehensive tests for MemRef, MemorySpace, and TileView.""" |
| 12 | 12 | ||
| 13 | -import textwrap | ||
| 14 | - | ||
| 15 | from pypto_pro import ir | 13 | from pypto_pro import ir |
| 16 | import pypto_pro.language as pl | 14 | import pypto_pro.language as pl |
| 17 | 15 | ||
| 18 | 16 | ||
| 17 | +def _function_str(func: ir.Function) -> str: | ||
| 18 | + return ir.python_print(func) | ||
| 19 | + | ||
| 20 | + | ||
| 19 | def test_parse_tensor_with_memref(): | 21 | def test_parse_tensor_with_memref(): |
| 20 | """Parse pl.Tensor[[64], pl.DT_FP32, pl.MemRef(...)] annotation.""" | 22 | """Parse pl.Tensor[[64], pl.DT_FP32, pl.MemRef(...)] annotation.""" |
| 21 | - code = textwrap.dedent("""\ | ||
| 22 | - @pl.program | ||
| 23 | - class TestProg: | ||
| 24 | - @pl.function | ||
| 25 | - def test_fn(x: pl.Tensor[[64], pl.DT_FP32]) -> pl.Tensor[[64], pl.DT_FP32]: | ||
| 26 | - y: pl.Tensor[[64], pl.DT_FP32, pl.MemRef(pl.MemorySpace.DDR, 0, 256, 1)] = pl.tensor.add(x, 1.0) | ||
| 27 | - return y | ||
| 28 | - """) | ||
| 29 | - program = pl.parse(code) | ||
| 30 | - assert isinstance(program, ir.Program) | ||
| 31 | 23 | ||
| 32 | - # Verify the parsed IR contains memref by re-printing | 24 | + @pl.jit(auto_mutex=False) |
| 33 | - printed = ir.python_print(program) | 25 | + def test_fn(x: pl.Tensor[[64], pl.DT_FP32]): |
| 26 | + y: pl.Tensor[[64], pl.DT_FP32, pl.MemRef(pl.MemorySpace.DDR, 0, 256, 1)] = pl.tensor.add(x, 1.0) # noqa: F841 | ||
| 27 | + | ||
| 28 | + test_fn_program, _ = test_fn.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 29 | + test_fn = test_fn_program.get_function(test_fn.__name__) | ||
| 30 | + | ||
| 31 | + printed = _function_str(test_fn) | ||
| 34 | assert "ir.MemRef" in printed | 32 | assert "ir.MemRef" in printed |
| 35 | assert "ir.MemorySpace.DDR" in printed | 33 | assert "ir.MemorySpace.DDR" in printed |
| 36 | assert "256" in printed | 34 | assert "256" in printed |
| @@ -38,14 +36,12 @@ def test_parse_tensor_with_memref(): | |||
| 38 | 36 | ||
| 39 | def test_backwards_compat_three_args_layout(): | 37 | def test_backwards_compat_three_args_layout(): |
| 40 | """Existing 3-arg [shape, dtype, layout] still works for Tensor.""" | 38 | """Existing 3-arg [shape, dtype, layout] still works for Tensor.""" |
| 41 | - code = textwrap.dedent("""\ | 39 | + |
| 42 | - @pl.program | 40 | + @pl.jit(auto_mutex=False) |
| 43 | - class TestProg: | 41 | + def test_fn(x: pl.Tensor[[64], pl.DT_FP32]): |
| 44 | - @pl.function | 42 | + y: pl.Tensor[[64], pl.DT_FP32, pl.NZ] = pl.tensor.add(x, 1.0) # noqa: F841 |
| 45 | - def test_fn(x: pl.Tensor[[64], pl.DT_FP32]) -> pl.Tensor[[64], pl.DT_FP32]: | 43 | + |
| 46 | - y: pl.Tensor[[64], pl.DT_FP32, pl.NZ] = pl.tensor.add(x, 1.0) | 44 | + test_fn_program, _ = test_fn.to_kernel_def().parse_target_program(ir.SectionKind.Vector) |
| 47 | - return y | 45 | + test_fn = test_fn_program.get_function(test_fn.__name__) |
| 48 | - """) | 46 | + |
| 49 | - # Should parse without errors | 47 | + assert isinstance(test_fn, ir.Function) |
| 50 | - program = pl.parse(code) | ||
| 51 | - assert isinstance(program, ir.Program) | ||
| @@ -21,24 +21,24 @@ def _ir_str(kernel_def) -> str: | |||
| 21 | 21 | ||
| 22 | 22 | ||
| 23 | def _parse_kernel(kernel_def) -> ir.Program: | 23 | def _parse_kernel(kernel_def) -> ir.Program: |
| 24 | - return kernel_def.parse_target_program(ir.SectionKind.Vector)[0] | 24 | + return kernel_def.to_kernel_def().parse_target_program(ir.SectionKind.Vector)[0] |
| 25 | 25 | ||
| 26 | 26 | ||
| 27 | -@pl.kernel | 27 | +@pl.jit |
| 28 | def _make_ptr_change_dtype_kernel(p: pl.Ptr[pl.DT_UINT8]): | 28 | def _make_ptr_change_dtype_kernel(p: pl.Ptr[pl.DT_UINT8]): |
| 29 | # Reinterpret a raw uint8 GM pointer as fp16, then view it as a 2D tensor. | 29 | # Reinterpret a raw uint8 GM pointer as fp16, then view it as a 2D tensor. |
| 30 | fp16_ptr = pl.make_ptr(p, dtype=pl.DT_FP16) | 30 | fp16_ptr = pl.make_ptr(p, dtype=pl.DT_FP16) |
| 31 | view = pl.make_tensor(fp16_ptr, [64, 128], [128, 1]) # noqa: F841 | 31 | view = pl.make_tensor(fp16_ptr, [64, 128], [128, 1]) # noqa: F841 |
| 32 | 32 | ||
| 33 | 33 | ||
| 34 | -@pl.kernel | 34 | +@pl.jit |
| 35 | def _make_ptr_identity_kernel(p: pl.Ptr[pl.DT_FP16]): | 35 | def _make_ptr_identity_kernel(p: pl.Ptr[pl.DT_FP16]): |
| 36 | # No dtype kwarg -> identity reinterpret (keeps the source dtype). | 36 | # No dtype kwarg -> identity reinterpret (keeps the source dtype). |
| 37 | same = pl.make_ptr(p) | 37 | same = pl.make_ptr(p) |
| 38 | view = pl.make_tensor(same, [32, 32], [32, 1]) # noqa: F841 | 38 | view = pl.make_tensor(same, [32, 32], [32, 1]) # noqa: F841 |
| 39 | 39 | ||
| 40 | 40 | ||
| 41 | -@pl.kernel | 41 | +@pl.jit |
| 42 | def _make_ptr_then_addptr_kernel(p: pl.Ptr[pl.DT_UINT8]): | 42 | def _make_ptr_then_addptr_kernel(p: pl.Ptr[pl.DT_UINT8]): |
| 43 | # make_ptr first, then advance with element (fp16) semantics. | 43 | # make_ptr first, then advance with element (fp16) semantics. |
| 44 | fp16_ptr = pl.make_ptr(p, dtype=pl.DT_FP16) | 44 | fp16_ptr = pl.make_ptr(p, dtype=pl.DT_FP16) |
| @@ -79,7 +79,7 @@ def test_make_ptr_then_addptr_ir(): | |||
| 79 | assert "ptr.addptr" in s | 79 | assert "ptr.addptr" in s |
| 80 | 80 | ||
| 81 | 81 | ||
| 82 | -@pl.kernel | 82 | +@pl.jit |
| 83 | def _ptr_plus_offset_kernel(p: pl.Ptr[pl.DT_UINT8]): | 83 | def _ptr_plus_offset_kernel(p: pl.Ptr[pl.DT_UINT8]): |
| 84 | # `ptr + offset` is sugar for pl.addptr(ptr, offset). | 84 | # `ptr + offset` is sugar for pl.addptr(ptr, offset). |
| 85 | fp16_ptr = pl.make_ptr(p, dtype=pl.DT_FP16) | 85 | fp16_ptr = pl.make_ptr(p, dtype=pl.DT_FP16) |
| @@ -101,19 +101,19 @@ def test_ptr_plus_offset_result_type_is_ptr(): | |||
| 101 | assert found.var.type.dtype == pl.DT_FP16 | 101 | assert found.var.type.dtype == pl.DT_FP16 |
| 102 | 102 | ||
| 103 | 103 | ||
| 104 | -@pl.kernel | 104 | +@pl.jit |
| 105 | def _retensor_shape_kernel(src: pl.Tensor[[64, 128], pl.DT_FP16]): | 105 | def _retensor_shape_kernel(src: pl.Tensor[[64, 128], pl.DT_FP16]): |
| 106 | # Same dtype, new shape/stride, reusing src's pointer. | 106 | # Same dtype, new shape/stride, reusing src's pointer. |
| 107 | reshaped = pl.make_tensor(src, [128, 64], [64, 1]) # noqa: F841 | 107 | reshaped = pl.make_tensor(src, [128, 64], [64, 1]) # noqa: F841 |
| 108 | 108 | ||
| 109 | 109 | ||
| 110 | -@pl.kernel | 110 | +@pl.jit |
| 111 | def _retensor_dtype_kernel(src: pl.Tensor[[64, 128], pl.DT_FP16]): | 111 | def _retensor_dtype_kernel(src: pl.Tensor[[64, 128], pl.DT_FP16]): |
| 112 | # Reinterpret an fp16 tensor as a wider uint8 view (2x columns). | 112 | # Reinterpret an fp16 tensor as a wider uint8 view (2x columns). |
| 113 | as_u8 = pl.make_tensor(src, [64, 256], [256, 1], dtype=pl.DT_UINT8) # noqa: F841 | 113 | as_u8 = pl.make_tensor(src, [64, 256], [256, 1], dtype=pl.DT_UINT8) # noqa: F841 |
| 114 | 114 | ||
| 115 | 115 | ||
| 116 | -@pl.kernel | 116 | +@pl.jit |
| 117 | def _retensor_view_of_view_kernel(src: pl.Tensor[[64, 128], pl.DT_FP16]): | 117 | def _retensor_view_of_view_kernel(src: pl.Tensor[[64, 128], pl.DT_FP16]): |
| 118 | # A view of a view: both derive from the same underlying pointer. | 118 | # A view of a view: both derive from the same underlying pointer. |
| 119 | v1 = pl.make_tensor(src, [128, 64], [64, 1]) | 119 | v1 = pl.make_tensor(src, [128, 64], [64, 1]) |
| @@ -146,7 +146,7 @@ def test_make_tensor_view_of_view_ir(): | |||
| 146 | 146 | ||
| 147 | 147 | ||
| 148 | def test_make_ptr_accepts_tensor_argument(): | 148 | def test_make_ptr_accepts_tensor_argument(): |
| 149 | - @pl.kernel | 149 | + @pl.jit |
| 150 | def k(t: pl.Tensor[[64], pl.DT_FP16]): | 150 | def k(t: pl.Tensor[[64], pl.DT_FP16]): |
| 151 | bad = pl.make_ptr(t, dtype=pl.DT_FP32) # noqa: F841 | 151 | bad = pl.make_ptr(t, dtype=pl.DT_FP32) # noqa: F841 |
| 152 | 152 | ||
| @@ -183,17 +183,17 @@ def _find_assign_by_call(stmt, op_name): | |||
| 183 | return None | 183 | return None |
| 184 | 184 | ||
| 185 | 185 | ||
| 186 | -@pl.kernel | 186 | +@pl.jit |
| 187 | def _addptr_int4_kernel(p: pl.Ptr[pl.DT_INT4]): | 187 | def _addptr_int4_kernel(p: pl.Ptr[pl.DT_INT4]): |
| 188 | bumped = pl.addptr(p, 8) # noqa: F841 | 188 | bumped = pl.addptr(p, 8) # noqa: F841 |
| 189 | 189 | ||
| 190 | 190 | ||
| 191 | -@pl.kernel | 191 | +@pl.jit |
| 192 | def _plus_offset_fp4_kernel(p: pl.Ptr[pl.DT_FP4]): | 192 | def _plus_offset_fp4_kernel(p: pl.Ptr[pl.DT_FP4]): |
| 193 | bumped = p + 8 # noqa: F841 | 193 | bumped = p + 8 # noqa: F841 |
| 194 | 194 | ||
| 195 | 195 | ||
| 196 | -@pl.kernel | 196 | +@pl.jit |
| 197 | def _make_ptr_to_int4_then_addptr_kernel(p: pl.Ptr[pl.DT_UINT8]): | 197 | def _make_ptr_to_int4_then_addptr_kernel(p: pl.Ptr[pl.DT_UINT8]): |
| 198 | # A legitimate uint8 pointer reinterpreted as int4 must still reject addptr. | 198 | # A legitimate uint8 pointer reinterpreted as int4 must still reject addptr. |
| 199 | i4 = pl.make_ptr(p, dtype=pl.DT_INT4) | 199 | i4 = pl.make_ptr(p, dtype=pl.DT_INT4) |
| @@ -23,14 +23,17 @@ def _call_spans(func): | |||
| 23 | 23 | ||
| 24 | 24 | ||
| 25 | def test_parser_passes_valid_spans_to_tensor_operations(): | 25 | def test_parser_passes_valid_spans_to_tensor_operations(): |
| 26 | - @pl.function | 26 | + @pl.jit(auto_mutex=False) |
| 27 | def parsed_ops( | 27 | def parsed_ops( |
| 28 | x: pl.Tensor[[64], pl.DT_FP32], | 28 | x: pl.Tensor[[64], pl.DT_FP32], |
| 29 | y: pl.Tensor[[64], pl.DT_FP32], | 29 | y: pl.Tensor[[64], pl.DT_FP32], |
| 30 | - ) -> pl.Tensor[[64], pl.DT_FP32]: | 30 | + ): |
| 31 | a: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, y) | 31 | a: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, y) |
| 32 | b: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.mul(a, 2.0) | 32 | b: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.mul(a, 2.0) |
| 33 | - return b | 33 | + _test_result = b |
| 34 | + | ||
| 35 | + parsed_ops_program, _ = parsed_ops.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 36 | + parsed_ops = parsed_ops_program.get_function(parsed_ops.__name__) | ||
| 34 | 37 | ||
| 35 | spans = _call_spans(parsed_ops) | 38 | spans = _call_spans(parsed_ops) |
| 36 | 39 | ||
| @@ -18,10 +18,13 @@ import pypto_pro.language as pl | |||
| 18 | def test_function_printed_with_subscript_types(): | 18 | def test_function_printed_with_subscript_types(): |
| 19 | """Test that function parameters use subscript notation.""" | 19 | """Test that function parameters use subscript notation.""" |
| 20 | 20 | ||
| 21 | - @pl.function | 21 | + @pl.jit(auto_mutex=False) |
| 22 | - def test_func(x: pl.Tensor[[64, 128], pl.DT_FP16]) -> pl.Tensor[[64, 128], pl.DT_FP32]: | 22 | + def test_func(x: pl.Tensor[[64, 128], pl.DT_FP16]): |
| 23 | result: pl.Tensor[[64, 128], pl.DT_FP32] = pl.tensor.cast(x, target_type=pl.DT_FP32) | 23 | result: pl.Tensor[[64, 128], pl.DT_FP32] = pl.tensor.cast(x, target_type=pl.DT_FP32) |
| 24 | - return result | 24 | + _test_result = result |
| 25 | + | ||
| 26 | + test_func_program, _ = test_func.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 27 | + test_func = test_func_program.get_function(test_func.__name__) | ||
| 25 | 28 | ||
| 26 | # Print the function | 29 | # Print the function |
| 27 | printed = pypto_pro.ir.python_print(test_func) | 30 | printed = pypto_pro.ir.python_print(test_func) |
| @@ -36,14 +39,17 @@ def test_function_printed_with_subscript_types(): | |||
| 36 | def test_parsed_function_printer_round_trip(): | 39 | def test_parsed_function_printer_round_trip(): |
| 37 | """Test that parsed functions can be printed correctly.""" | 40 | """Test that parsed functions can be printed correctly.""" |
| 38 | 41 | ||
| 39 | - @pl.function | 42 | + @pl.jit(auto_mutex=False) |
| 40 | def round_trip( | 43 | def round_trip( |
| 41 | x: pl.Tensor[[64], pl.DT_FP32], | 44 | x: pl.Tensor[[64], pl.DT_FP32], |
| 42 | y: pl.Tensor[[64], pl.DT_FP32], | 45 | y: pl.Tensor[[64], pl.DT_FP32], |
| 43 | - ) -> pl.Tensor[[64], pl.DT_FP32]: | 46 | + ): |
| 44 | sum_val: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, y) | 47 | sum_val: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.add(x, y) |
| 45 | result: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.mul(sum_val, 2.0) | 48 | result: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.mul(sum_val, 2.0) |
| 46 | - return result | 49 | + _test_result = result |
| 50 | + | ||
| 51 | + round_trip_program, _ = round_trip.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 52 | + round_trip = round_trip_program.get_function(round_trip.__name__) | ||
| 47 | 53 | ||
| 48 | # Print and check syntax | 54 | # Print and check syntax |
| 49 | printed = pypto_pro.ir.python_print(round_trip) | 55 | printed = pypto_pro.ir.python_print(round_trip) |
| @@ -57,12 +63,15 @@ def test_parsed_function_printer_round_trip(): | |||
| 57 | def test_while_loop_natural_syntax(): | 63 | def test_while_loop_natural_syntax(): |
| 58 | """Test that natural while loop can be parsed and printed.""" | 64 | """Test that natural while loop can be parsed and printed.""" |
| 59 | 65 | ||
| 60 | - @pl.function | 66 | + @pl.jit(auto_mutex=False) |
| 61 | - def while_natural(n: pl.DT_INT64) -> pl.DT_INT64: | 67 | + def while_natural(n: pl.DT_INT64): |
| 62 | x: pl.DT_INT64 = 0 | 68 | x: pl.DT_INT64 = 0 |
| 63 | while x < n: | 69 | while x < n: |
| 64 | x = x + 1 | 70 | x = x + 1 |
| 65 | - return x | 71 | + _test_result = x |
| 72 | + | ||
| 73 | + while_natural_program, _ = while_natural.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 74 | + while_natural = while_natural_program.get_function(while_natural.__name__) | ||
| 66 | 75 | ||
| 67 | # Print the function | 76 | # Print the function |
| 68 | printed = pypto_pro.ir.python_print(while_natural) | 77 | printed = pypto_pro.ir.python_print(while_natural) |
| @@ -79,14 +88,17 @@ def test_while_loop_natural_syntax(): | |||
| 79 | def test_while_with_tensor_operations_round_trip(): | 88 | def test_while_with_tensor_operations_round_trip(): |
| 80 | """Test while loop with tensor operations.""" | 89 | """Test while loop with tensor operations.""" |
| 81 | 90 | ||
| 82 | - @pl.function | 91 | + @pl.jit(auto_mutex=False) |
| 83 | - def while_tensors(n: pl.DT_INT64, x: pl.Tensor[[64], pl.DT_FP32]) -> pl.Tensor[[64], pl.DT_FP32]: | 92 | + def while_tensors(n: pl.DT_INT64, x: pl.Tensor[[64], pl.DT_FP32]): |
| 84 | i: pl.DT_INT64 = 0 | 93 | i: pl.DT_INT64 = 0 |
| 85 | acc: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.create_tensor([64], dtype=pl.DT_FP32) | 94 | acc: pl.Tensor[[64], pl.DT_FP32] = pl.tensor.create_tensor([64], dtype=pl.DT_FP32) |
| 86 | while i < n: | 95 | while i < n: |
| 87 | i = i + 1 | 96 | i = i + 1 |
| 88 | acc = pl.tensor.add(acc, x) | 97 | acc = pl.tensor.add(acc, x) |
| 89 | - return acc | 98 | + _test_result = acc |
| 99 | + | ||
| 100 | + while_tensors_program, _ = while_tensors.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 101 | + while_tensors = while_tensors_program.get_function(while_tensors.__name__) | ||
| 90 | 102 | ||
| 91 | # Print the function | 103 | # Print the function |
| 92 | printed = pypto_pro.ir.python_print(while_tensors) | 104 | printed = pypto_pro.ir.python_print(while_tensors) |
Mpython/tests/ut/pypto_pro/language/parser/test_ptr_annotation.py+6-3File content is under review, please try again later
| @@ -10,6 +10,7 @@ | |||
| 10 | # ----------------------------------------------------------------------------------------------------------- | 10 | # ----------------------------------------------------------------------------------------------------------- |
| 11 | """Parser tests for pointer null-check syntax on optional pl.Ptr parameters.""" | 11 | """Parser tests for pointer null-check syntax on optional pl.Ptr parameters.""" |
| 12 | 12 | ||
| 13 | +from pypto_pro import ir | ||
| 13 | import pypto_pro.language as pl | 14 | import pypto_pro.language as pl |
| 14 | from pypto_pro.language.parser.diagnostics import ParserSyntaxError, ParserTypeError | 15 | from pypto_pro.language.parser.diagnostics import ParserSyntaxError, ParserTypeError |
| 15 | import pytest | 16 | import pytest |
| @@ -18,12 +19,14 @@ import pytest | |||
| 18 | def test_is_none_requires_simple_variable(): | 19 | def test_is_none_requires_simple_variable(): |
| 19 | """'is None' must be used on simple variable names, not complex expressions.""" | 20 | """'is None' must be used on simple variable names, not complex expressions.""" |
| 20 | with pytest.raises(ParserSyntaxError, match="only supported on simple variable names"): | 21 | with pytest.raises(ParserSyntaxError, match="only supported on simple variable names"): |
| 21 | - @pl.function | 22 | + @pl.jit(auto_mutex=False) |
| 22 | - def bad_is_none(): | 23 | + def bad_is_none(_jit_entry: pl.DT_INT64): |
| 23 | x: pl.DT_INT32 = 42 | 24 | x: pl.DT_INT32 = 42 |
| 24 | if (x + 1) is None: # Complex expression not allowed | 25 | if (x + 1) is None: # Complex expression not allowed |
| 25 | pass | 26 | pass |
| 26 | 27 | ||
| 28 | + bad_is_none.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 29 | + | ||
| 27 | 30 | ||
| 28 | def test_is_none_on_tensor_rejected(): | 31 | def test_is_none_on_tensor_rejected(): |
| 29 | """'is None' on a pl.Tensor parameter is rejected at parse time. | 32 | """'is None' on a pl.Tensor parameter is rejected at parse time. |
| @@ -34,10 +37,12 @@ def test_is_none_on_tensor_rejected(): | |||
| 34 | tensor with no argument has no meaningful shape). | 37 | tensor with no argument has no meaningful shape). |
| 35 | """ | 38 | """ |
| 36 | with pytest.raises(ParserTypeError, match="pl.Ptr"): | 39 | with pytest.raises(ParserTypeError, match="pl.Ptr"): |
| 37 | - @pl.function | 40 | + @pl.jit(auto_mutex=False) |
| 38 | def bad_tensor_none( | 41 | def bad_tensor_none( |
| 39 | src: pl.Tensor[[4], pl.DT_FP16], | 42 | src: pl.Tensor[[4], pl.DT_FP16], |
| 40 | bias: pl.Tensor[[4], pl.DT_FP16], # should be pl.Ptr[pl.DT_FP16] | 43 | bias: pl.Tensor[[4], pl.DT_FP16], # should be pl.Ptr[pl.DT_FP16] |
| 41 | ): | 44 | ): |
| 42 | if bias is None: | 45 | if bias is None: |
| 43 | pass | 46 | pass |
| 47 | + | ||
| 48 | + bad_tensor_none.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| @@ -11,17 +11,16 @@ | |||
| 11 | 11 | ||
| 12 | """Parser tests for the public ``pl.simt.cast`` API. | 12 | """Parser tests for the public ``pl.simt.cast`` API. |
| 13 | 13 | ||
| 14 | -Operation-level tests use eager SIMT IR parsing so diagnostics and generated IR | 14 | +Operation-level pl.simt.cast tests parse a captured kernel through |
| 15 | -are observable at function definition time. Delayed ``pl.simt.function`` parsing | 15 | +``KernelDef.parse_target_program`` so diagnostics and generated IR use the |
| 16 | -is covered by ``test_simt.py``. | 16 | +same parser entry as production kernels. |
| 17 | """ | 17 | """ |
| 18 | 18 | ||
| 19 | +from pypto_pro import ir | ||
| 19 | import pypto_pro.language as pl | 20 | import pypto_pro.language as pl |
| 20 | from pypto_pro.language.parser.diagnostics import ParserSyntaxError, ParserTypeError | 21 | from pypto_pro.language.parser.diagnostics import ParserSyntaxError, ParserTypeError |
| 21 | import pytest | 22 | import pytest |
| 22 | 23 | ||
| 23 | -from pypto.pypto_impl import ir | ||
| 24 | - | ||
| 25 | 24 | ||
| 26 | def test_simt_cast_is_not_exported_as_shared_language_api(): | 25 | def test_simt_cast_is_not_exported_as_shared_language_api(): |
| 27 | assert not hasattr(pl, "scalar_cast") | 26 | assert not hasattr(pl, "scalar_cast") |
| @@ -30,15 +29,25 @@ def test_simt_cast_is_not_exported_as_shared_language_api(): | |||
| 30 | def test_simt_cast_rejects_ordinary_function(): | 29 | def test_simt_cast_rejects_ordinary_function(): |
| 31 | with pytest.raises(ParserSyntaxError, match="can only be used inside a SIMT function"): | 30 | with pytest.raises(ParserSyntaxError, match="can only be used inside a SIMT function"): |
| 32 | 31 | ||
| 33 | - @pl.function(type=pl.FunctionType.Orchestration) | 32 | + @pl.jit(auto_mutex=False) |
| 34 | - def unsupported_context(value: pl.DT_INT64) -> pl.DT_INT32: | 33 | + def unsupported_context(value: pl.DT_INT64): |
| 35 | - return pl.simt.cast(value, pl.DT_INT32) | 34 | + _test_result = pl.simt.cast(value, pl.DT_INT32) |
| 35 | + | ||
| 36 | + unsupported_context.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 36 | 37 | ||
| 37 | 38 | ||
| 38 | def test_simt_cast_is_available_inside_simt_helper(): | 39 | def test_simt_cast_is_available_inside_simt_helper(): |
| 39 | - @pl.function(type=pl.FunctionType.SimtCallee) | 40 | + @pl.simt.function(max_threads=1) |
| 40 | - def cast_value(value: pl.DT_FP32) -> pl.DT_FP16: | 41 | + def cast_value(value: pl.DT_FP32): |
| 41 | - return pl.simt.cast(value, pl.DT_FP16, mode=pl.RoundMode.CAST_RINT) | 42 | + _test_result = pl.simt.cast(value, pl.DT_FP16, mode=pl.RoundMode.CAST_RINT) |
| 43 | + | ||
| 44 | + @pl.jit(auto_mutex=False) | ||
| 45 | + def kernel(value: pl.DT_FP32): | ||
| 46 | + with pl.section_vector(): | ||
| 47 | + pl.simt.launch(cast_value, threads=1, args=(value,)) | ||
| 48 | + | ||
| 49 | + program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 50 | + cast_value = program.get_function(cast_value.__name__) | ||
| 42 | 51 | ||
| 43 | function_ir = str(cast_value) | 52 | function_ir = str(cast_value) |
| 44 | assert "simt.cast" in function_ir | 53 | assert "simt.cast" in function_ir |
| @@ -47,17 +56,31 @@ def test_simt_cast_is_available_inside_simt_helper(): | |||
| 47 | def test_simt_cast_rejects_unsupported_dtype_pair(): | 56 | def test_simt_cast_rejects_unsupported_dtype_pair(): |
| 48 | with pytest.raises(ParserTypeError, match=r"pl\.simt\.cast\(\) does not support"): | 57 | with pytest.raises(ParserTypeError, match=r"pl\.simt\.cast\(\) does not support"): |
| 49 | 58 | ||
| 50 | - @pl.function(type=pl.FunctionType.SimtCallee) | 59 | + @pl.simt.function(max_threads=1) |
| 51 | - def unsupported(value: pl.DT_FP16) -> pl.DT_INT32: | 60 | + def unsupported(value: pl.DT_FP16): |
| 52 | - return pl.simt.cast(value, pl.DT_INT32) | 61 | + _test_result = pl.simt.cast(value, pl.DT_INT32) |
| 62 | + | ||
| 63 | + @pl.jit(auto_mutex=False) | ||
| 64 | + def kernel(value: pl.DT_FP16): | ||
| 65 | + with pl.section_vector(): | ||
| 66 | + pl.simt.launch(unsupported, threads=1, args=(value,)) | ||
| 67 | + | ||
| 68 | + kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 53 | 69 | ||
| 54 | 70 | ||
| 55 | def test_simt_cast_rejects_odd_rounding_for_bfloat16(): | 71 | def test_simt_cast_rejects_odd_rounding_for_bfloat16(): |
| 56 | with pytest.raises(ParserTypeError, match=r"pl\.simt\.cast\(\) does not support"): | 72 | with pytest.raises(ParserTypeError, match=r"pl\.simt\.cast\(\) does not support"): |
| 57 | 73 | ||
| 58 | - @pl.function(type=pl.FunctionType.SimtCallee) | 74 | + @pl.simt.function(max_threads=1) |
| 59 | - def unsupported_mode(value: pl.DT_FP32) -> pl.DT_BF16: | 75 | + def unsupported_mode(value: pl.DT_FP32): |
| 60 | - return pl.simt.cast(value, pl.DT_BF16, mode=pl.RoundMode.CAST_ODD) | 76 | + _test_result = pl.simt.cast(value, pl.DT_BF16, mode=pl.RoundMode.CAST_ODD) |
| 77 | + | ||
| 78 | + @pl.jit(auto_mutex=False) | ||
| 79 | + def kernel(value: pl.DT_FP32): | ||
| 80 | + with pl.section_vector(): | ||
| 81 | + pl.simt.launch(unsupported_mode, threads=1, args=(value,)) | ||
| 82 | + | ||
| 83 | + kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 61 | 84 | ||
| 62 | 85 | ||
| 63 | def test_simt_cast_rejects_tile_operand(): | 86 | def test_simt_cast_rejects_tile_operand(): |
| @@ -69,44 +92,72 @@ def test_simt_cast_rejects_tile_operand(): | |||
| 69 | def entry(value): | 92 | def entry(value): |
| 70 | tile_operand(value) | 93 | tile_operand(value) |
| 71 | 94 | ||
| 72 | - @pl.kernel | 95 | + @pl.jit |
| 73 | - def kernel(): | 96 | + def kernel(_jit_entry: pl.DT_INT64): |
| 74 | tile_type = pl.TileType(shape=[1, 1], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | 97 | tile_type = pl.TileType(shape=[1, 1], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) |
| 75 | value = pl.make_tile(tile_type, addr=0, size=4) | 98 | value = pl.make_tile(tile_type, addr=0, size=4) |
| 76 | with pl.section_vector(): | 99 | with pl.section_vector(): |
| 77 | pl.simt.launch(entry, threads=1, args=(value,)) | 100 | pl.simt.launch(entry, threads=1, args=(value,)) |
| 78 | 101 | ||
| 79 | with pytest.raises(ParserTypeError, match="value must be a scalar expression"): | 102 | with pytest.raises(ParserTypeError, match="value must be a scalar expression"): |
| 80 | - kernel.parse_target_program(ir.SectionKind.Vector) | 103 | + kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) |
| 81 | 104 | ||
| 82 | 105 | ||
| 83 | def test_simt_cast_rejects_plain_integer_round_mode(): | 106 | def test_simt_cast_rejects_plain_integer_round_mode(): |
| 84 | with pytest.raises(ParserTypeError, match="expects an enum value"): | 107 | with pytest.raises(ParserTypeError, match="expects an enum value"): |
| 85 | 108 | ||
| 86 | - @pl.function(type=pl.FunctionType.SimtCallee) | 109 | + @pl.simt.function(max_threads=1) |
| 87 | - def integer_mode(value: pl.DT_FP32) -> pl.DT_FP16: | 110 | + def integer_mode(value: pl.DT_FP32): |
| 88 | - return pl.simt.cast(value, pl.DT_FP16, mode=1) | 111 | + _test_result = pl.simt.cast(value, pl.DT_FP16, mode=1) |
| 112 | + | ||
| 113 | + @pl.jit(auto_mutex=False) | ||
| 114 | + def kernel(value: pl.DT_FP32): | ||
| 115 | + with pl.section_vector(): | ||
| 116 | + pl.simt.launch(integer_mode, threads=1, args=(value,)) | ||
| 117 | + | ||
| 118 | + kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 89 | 119 | ||
| 90 | 120 | ||
| 91 | def test_simt_cast_rejects_wrong_positional_arity(): | 121 | def test_simt_cast_rejects_wrong_positional_arity(): |
| 92 | with pytest.raises(ParserSyntaxError, match="requires exactly 2 positional arguments"): | 122 | with pytest.raises(ParserSyntaxError, match="requires exactly 2 positional arguments"): |
| 93 | 123 | ||
| 94 | - @pl.function(type=pl.FunctionType.SimtCallee) | 124 | + @pl.simt.function(max_threads=1) |
| 95 | - def missing_dtype(value: pl.DT_FP32) -> pl.DT_FP16: | 125 | + def missing_dtype(value: pl.DT_FP32): |
| 96 | - return pl.simt.cast(value) | 126 | + _test_result = pl.simt.cast(value) |
| 127 | + | ||
| 128 | + @pl.jit(auto_mutex=False) | ||
| 129 | + def kernel(value: pl.DT_FP32): | ||
| 130 | + with pl.section_vector(): | ||
| 131 | + pl.simt.launch(missing_dtype, threads=1, args=(value,)) | ||
| 132 | + | ||
| 133 | + kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 97 | 134 | ||
| 98 | 135 | ||
| 99 | def test_simt_cast_rejects_non_dtype_target(): | 136 | def test_simt_cast_rejects_non_dtype_target(): |
| 100 | with pytest.raises(ParserTypeError, match=r"dtype must be a pl\.DT_\* value"): | 137 | with pytest.raises(ParserTypeError, match=r"dtype must be a pl\.DT_\* value"): |
| 101 | 138 | ||
| 102 | - @pl.function(type=pl.FunctionType.SimtCallee) | 139 | + @pl.simt.function(max_threads=1) |
| 103 | - def invalid_dtype(value: pl.DT_FP32) -> pl.DT_FP16: | 140 | + def invalid_dtype(value: pl.DT_FP32): |
| 104 | - return pl.simt.cast(value, 1) | 141 | + _test_result = pl.simt.cast(value, 1) |
| 142 | + | ||
| 143 | + @pl.jit(auto_mutex=False) | ||
| 144 | + def kernel(value: pl.DT_FP32): | ||
| 145 | + with pl.section_vector(): | ||
| 146 | + pl.simt.launch(invalid_dtype, threads=1, args=(value,)) | ||
| 147 | + | ||
| 148 | + kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 105 | 149 | ||
| 106 | 150 | ||
| 107 | def test_simt_cast_rejects_unexpected_keyword(): | 151 | def test_simt_cast_rejects_unexpected_keyword(): |
| 108 | with pytest.raises(ParserSyntaxError, match="only accepts one optional keyword argument"): | 152 | with pytest.raises(ParserSyntaxError, match="only accepts one optional keyword argument"): |
| 109 | 153 | ||
| 110 | - @pl.function(type=pl.FunctionType.SimtCallee) | 154 | + @pl.simt.function(max_threads=1) |
| 111 | - def unexpected_keyword(value: pl.DT_FP32) -> pl.DT_FP16: | 155 | + def unexpected_keyword(value: pl.DT_FP32): |
| 112 | - return pl.simt.cast(value, pl.DT_FP16, saturate=True) | 156 | + _test_result = pl.simt.cast(value, pl.DT_FP16, saturate=True) |
| 157 | + | ||
| 158 | + @pl.jit(auto_mutex=False) | ||
| 159 | + def kernel(value: pl.DT_FP32): | ||
| 160 | + with pl.section_vector(): | ||
| 161 | + pl.simt.launch(unexpected_keyword, threads=1, args=(value,)) | ||
| 162 | + | ||
| 163 | + kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| @@ -22,16 +22,19 @@ from pypto.pypto_impl import ir | |||
| 22 | def test_scalar_min(): | 22 | def test_scalar_min(): |
| 23 | """Test pl.min(scalar, scalar) dispatches to ir.min_.""" | 23 | """Test pl.min(scalar, scalar) dispatches to ir.min_.""" |
| 24 | 24 | ||
| 25 | - @pl.function(type=pl.FunctionType.Opaque) | 25 | + @pl.jit(auto_mutex=False) |
| 26 | def test_min( | 26 | def test_min( |
| 27 | config: pl.Tensor[[2], pl.DT_INT64], | 27 | config: pl.Tensor[[2], pl.DT_INT64], |
| 28 | out: pl.Tensor[[2, 16, 128], pl.DT_FP32], | 28 | out: pl.Tensor[[2, 16, 128], pl.DT_FP32], |
| 29 | - ) -> pl.Tensor[[2, 16, 128], pl.DT_FP32]: | 29 | + ): |
| 30 | a: pl.DT_UINT64 = pl.getval(config, 0) | 30 | a: pl.DT_UINT64 = pl.getval(config, 0) |
| 31 | b: pl.DT_UINT64 = pl.getval(config, 1) | 31 | b: pl.DT_UINT64 = pl.getval(config, 1) |
| 32 | c = pl.min(a, b) | 32 | c = pl.min(a, b) |
| 33 | _ = c + 1 | 33 | _ = c + 1 |
| 34 | - return out | 34 | + _test_result = out |
| 35 | + | ||
| 36 | + test_min_program, _ = test_min.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 37 | + test_min = test_min_program.get_function(test_min.__name__) | ||
| 35 | 38 | ||
| 36 | assert isinstance(test_min, ir.Function) | 39 | assert isinstance(test_min, ir.Function) |
| 37 | ir_text = ir.python_print(test_min) | 40 | ir_text = ir.python_print(test_min) |
| @@ -41,16 +44,19 @@ def test_scalar_min(): | |||
| 41 | def test_scalar_max(): | 44 | def test_scalar_max(): |
| 42 | """Test pl.max(scalar, scalar) dispatches to ir.max_.""" | 45 | """Test pl.max(scalar, scalar) dispatches to ir.max_.""" |
| 43 | 46 | ||
| 44 | - @pl.function(type=pl.FunctionType.Opaque) | 47 | + @pl.jit(auto_mutex=False) |
| 45 | def test_max( | 48 | def test_max( |
| 46 | config: pl.Tensor[[2], pl.DT_INT64], | 49 | config: pl.Tensor[[2], pl.DT_INT64], |
| 47 | out: pl.Tensor[[2, 16, 128], pl.DT_FP32], | 50 | out: pl.Tensor[[2, 16, 128], pl.DT_FP32], |
| 48 | - ) -> pl.Tensor[[2, 16, 128], pl.DT_FP32]: | 51 | + ): |
| 49 | a: pl.DT_UINT64 = pl.getval(config, 0) | 52 | a: pl.DT_UINT64 = pl.getval(config, 0) |
| 50 | b: pl.DT_UINT64 = pl.getval(config, 1) | 53 | b: pl.DT_UINT64 = pl.getval(config, 1) |
| 51 | c = pl.max(a, b) | 54 | c = pl.max(a, b) |
| 52 | _ = c + 1 | 55 | _ = c + 1 |
| 53 | - return out | 56 | + _test_result = out |
| 57 | + | ||
| 58 | + test_max_program, _ = test_max.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 59 | + test_max = test_max_program.get_function(test_max.__name__) | ||
| 54 | 60 | ||
| 55 | assert isinstance(test_max, ir.Function) | 61 | assert isinstance(test_max, ir.Function) |
| 56 | ir_text = ir.python_print(test_max) | 62 | ir_text = ir.python_print(test_max) |
| @@ -60,15 +66,18 @@ def test_scalar_max(): | |||
| 60 | def test_scalar_min_with_literal(): | 66 | def test_scalar_min_with_literal(): |
| 61 | """Test pl.min(scalar, int_literal) -the paged_attention use case.""" | 67 | """Test pl.min(scalar, int_literal) -the paged_attention use case.""" |
| 62 | 68 | ||
| 63 | - @pl.function(type=pl.FunctionType.Opaque) | 69 | + @pl.jit(auto_mutex=False) |
| 64 | def test_min_lit( | 70 | def test_min_lit( |
| 65 | config: pl.Tensor[[2], pl.DT_INT64], | 71 | config: pl.Tensor[[2], pl.DT_INT64], |
| 66 | out: pl.Tensor[[2, 16, 128], pl.DT_FP32], | 72 | out: pl.Tensor[[2, 16, 128], pl.DT_FP32], |
| 67 | - ) -> pl.Tensor[[2, 16, 128], pl.DT_FP32]: | 73 | + ): |
| 68 | a: pl.DT_UINT64 = pl.getval(config, 0) | 74 | a: pl.DT_UINT64 = pl.getval(config, 0) |
| 69 | c = pl.min(a, 128) | 75 | c = pl.min(a, 128) |
| 70 | _ = c + 1 | 76 | _ = c + 1 |
| 71 | - return out | 77 | + _test_result = out |
| 78 | + | ||
| 79 | + test_min_lit_program, _ = test_min_lit.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 80 | + test_min_lit = test_min_lit_program.get_function(test_min_lit.__name__) | ||
| 72 | 81 | ||
| 73 | assert isinstance(test_min_lit, ir.Function) | 82 | assert isinstance(test_min_lit, ir.Function) |
| 74 | ir_text = ir.python_print(test_min_lit) | 83 | ir_text = ir.python_print(test_min_lit) |
| @@ -78,10 +87,10 @@ def test_scalar_min_with_literal(): | |||
| 78 | def test_tile_min_still_works(): | 87 | def test_tile_min_still_works(): |
| 79 | """Ensure pl.min(tile, axis=...) still works as tile reduction.""" | 88 | """Ensure pl.min(tile, axis=...) still works as tile reduction.""" |
| 80 | 89 | ||
| 81 | - @pl.function | 90 | + @pl.jit(auto_mutex=False) |
| 82 | def test_tile_min( | 91 | def test_tile_min( |
| 83 | x: pl.Tensor[[32, 32], pl.DT_FP32], | 92 | x: pl.Tensor[[32, 32], pl.DT_FP32], |
| 84 | - ) -> pl.Tensor[[32, 32], pl.DT_FP32]: | 93 | + ): |
| 85 | tile_type = pl.TileType(shape=[32, 32], dtype=pl.DT_FP32) | 94 | tile_type = pl.TileType(shape=[32, 32], dtype=pl.DT_FP32) |
| 86 | reduced_type = pl.TileType(shape=[32, 1], dtype=pl.DT_FP32) | 95 | reduced_type = pl.TileType(shape=[32, 1], dtype=pl.DT_FP32) |
| 87 | tile_a = pl.make_tile(tile_type, addr=0, size=4096) | 96 | tile_a = pl.make_tile(tile_type, addr=0, size=4096) |
| @@ -90,6 +99,9 @@ def test_tile_min_still_works(): | |||
| 90 | pl.load(tile_a, x, [0, 0]) | 99 | pl.load(tile_a, x, [0, 0]) |
| 91 | pl.minimum(tile_a, tmp, tile_c, dim=0) | 100 | pl.minimum(tile_a, tmp, tile_c, dim=0) |
| 92 | out: pl.Tensor[[32, 32], pl.DT_FP32] = pl.store(x, tile_c, [0, 0]) | 101 | out: pl.Tensor[[32, 32], pl.DT_FP32] = pl.store(x, tile_c, [0, 0]) |
| 93 | - return out | 102 | + _test_result = out |
| 103 | + | ||
| 104 | + test_tile_min_program, _ = test_tile_min.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 105 | + test_tile_min = test_tile_min_program.get_function(test_tile_min.__name__) | ||
| 94 | 106 | ||
| 95 | assert isinstance(test_tile_min, ir.Function) | 107 | assert isinstance(test_tile_min, ir.Function) |
| @@ -11,9 +11,9 @@ | |||
| 11 | 11 | ||
| 12 | """Parser tests for public ``pl.simt`` scalar math APIs. | 12 | """Parser tests for public ``pl.simt`` scalar math APIs. |
| 13 | 13 | ||
| 14 | -Operation-level tests use eager SIMT IR parsing so diagnostics and generated IR | 14 | +Operation-level pl.simt scalar math tests parse a captured kernel through |
| 15 | -are observable at function definition time. Delayed ``pl.simt.function`` parsing | 15 | +``KernelDef.parse_target_program`` so diagnostics and generated IR use the |
| 16 | -is covered by ``test_simt.py``. | 16 | +same parser entry as production kernels. |
| 17 | """ | 17 | """ |
| 18 | 18 | ||
| 19 | import pypto_pro.language as pl | 19 | import pypto_pro.language as pl |
| @@ -31,8 +31,8 @@ def _parse_tile_function(function, tile_specs): | |||
| 31 | def entry(tile0): | 31 | def entry(tile0): |
| 32 | function(tile0) | 32 | function(tile0) |
| 33 | 33 | ||
| 34 | - @pl.kernel | 34 | + @pl.jit |
| 35 | - def kernel(): | 35 | + def kernel(_jit_entry: pl.DT_INT64): |
| 36 | tile0 = pl.make_tile( | 36 | tile0 = pl.make_tile( |
| 37 | pl.TileType(shape=shape0, dtype=dtype0, target_memory=pl.MemorySpace.Vec), | 37 | pl.TileType(shape=shape0, dtype=dtype0, target_memory=pl.MemorySpace.Vec), |
| 38 | addr=0, | 38 | addr=0, |
| @@ -48,8 +48,8 @@ def _parse_tile_function(function, tile_specs): | |||
| 48 | def entry(tile0, tile1): | 48 | def entry(tile0, tile1): |
| 49 | function(tile0, tile1) | 49 | function(tile0, tile1) |
| 50 | 50 | ||
| 51 | - @pl.kernel | 51 | + @pl.jit |
| 52 | - def kernel(): | 52 | + def kernel(_jit_entry: pl.DT_INT64): |
| 53 | tile0 = pl.make_tile( | 53 | tile0 = pl.make_tile( |
| 54 | pl.TileType(shape=shape0, dtype=dtype0, target_memory=pl.MemorySpace.Vec), | 54 | pl.TileType(shape=shape0, dtype=dtype0, target_memory=pl.MemorySpace.Vec), |
| 55 | addr=0, | 55 | addr=0, |
| @@ -70,8 +70,8 @@ def _parse_tile_function(function, tile_specs): | |||
| 70 | def entry(tile0, tile1, tile2): | 70 | def entry(tile0, tile1, tile2): |
| 71 | function(tile0, tile1, tile2) | 71 | function(tile0, tile1, tile2) |
| 72 | 72 | ||
| 73 | - @pl.kernel | 73 | + @pl.jit |
| 74 | - def kernel(): | 74 | + def kernel(_jit_entry: pl.DT_INT64): |
| 75 | tile0 = pl.make_tile( | 75 | tile0 = pl.make_tile( |
| 76 | pl.TileType(shape=shape0, dtype=dtype0, target_memory=pl.MemorySpace.Vec), | 76 | pl.TileType(shape=shape0, dtype=dtype0, target_memory=pl.MemorySpace.Vec), |
| 77 | addr=0, | 77 | addr=0, |
| @@ -93,13 +93,13 @@ def _parse_tile_function(function, tile_specs): | |||
| 93 | else: | 93 | else: |
| 94 | raise ValueError("Only one to three Tile parameters are supported") | 94 | raise ValueError("Only one to three Tile parameters are supported") |
| 95 | 95 | ||
| 96 | - program, _ = kernel.parse_target_program(ir.SectionKind.Vector) | 96 | + program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) |
| 97 | return program.get_function(function.__name__) | 97 | return program.get_function(function.__name__) |
| 98 | 98 | ||
| 99 | 99 | ||
| 100 | def test_scalar_math_is_available_in_simt_functions(): | 100 | def test_scalar_math_is_available_in_simt_functions(): |
| 101 | - @pl.function(type=pl.FunctionType.SimtCallee) | 101 | + @pl.simt.function(max_threads=1) |
| 102 | - def scalar_math(value: pl.DT_FP32, rhs: pl.DT_FP32, addend: pl.DT_FP32) -> pl.DT_FP32: | 102 | + def scalar_math(value: pl.DT_FP32, rhs: pl.DT_FP32, addend: pl.DT_FP32): |
| 103 | transformed = pl.simt.max(pl.simt.min(value, rhs), pl.simt.abs(addend)) | 103 | transformed = pl.simt.max(pl.simt.min(value, rhs), pl.simt.abs(addend)) |
| 104 | transformed = pl.simt.rsqrt(pl.simt.sqrt(transformed)) | 104 | transformed = pl.simt.rsqrt(pl.simt.sqrt(transformed)) |
| 105 | transformed = pl.simt.exp2(pl.simt.exp(transformed)) | 105 | transformed = pl.simt.exp2(pl.simt.exp(transformed)) |
| @@ -111,7 +111,15 @@ def test_scalar_math_is_available_in_simt_functions(): | |||
| 111 | classified = pl.simt.isnan(transformed) or pl.simt.isinf(transformed) | 111 | classified = pl.simt.isnan(transformed) or pl.simt.isinf(transformed) |
| 112 | if classified: | 112 | if classified: |
| 113 | transformed = value | 113 | transformed = value |
| 114 | - return pl.simt.fma(transformed, rhs, addend) | 114 | + _test_result = pl.simt.fma(transformed, rhs, addend) |
| 115 | + | ||
| 116 | + @pl.jit(auto_mutex=False) | ||
| 117 | + def kernel(value: pl.DT_FP32, rhs: pl.DT_FP32, addend: pl.DT_FP32): | ||
| 118 | + with pl.section_vector(): | ||
| 119 | + pl.simt.launch(scalar_math, threads=1, args=(value, rhs, addend)) | ||
| 120 | + | ||
| 121 | + program, _ = kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 122 | + scalar_math = program.get_function(scalar_math.__name__) | ||
| 115 | 123 | ||
| 116 | function_ir = str(scalar_math) | 124 | function_ir = str(scalar_math) |
| 117 | for name in ( | 125 | for name in ( |
| @@ -192,9 +200,11 @@ def test_scalar_math_supports_int64_abs_and_integer_min_max(): | |||
| 192 | def test_scalar_math_rejects_ordinary_function(): | 200 | def test_scalar_math_rejects_ordinary_function(): |
| 193 | with pytest.raises(ParserSyntaxError, match="can only be used inside a SIMT function"): | 201 | with pytest.raises(ParserSyntaxError, match="can only be used inside a SIMT function"): |
| 194 | 202 | ||
| 195 | - @pl.function(type=pl.FunctionType.Orchestration) | 203 | + @pl.jit(auto_mutex=False) |
| 196 | - def unsupported(value: pl.DT_FP32) -> pl.DT_FP32: | 204 | + def unsupported(value: pl.DT_FP32): |
| 197 | - return pl.simt.exp(value) | 205 | + _test_result = pl.simt.exp(value) |
| 206 | + | ||
| 207 | + unsupported.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 198 | 208 | ||
| 199 | 209 | ||
| 200 | def test_scalar_math_rejects_unsupported_dtype_and_mixed_operands(): | 210 | def test_scalar_math_rejects_unsupported_dtype_and_mixed_operands(): |
| @@ -232,12 +242,26 @@ def test_scalar_math_rejects_tile_operand(): | |||
| 232 | def test_scalar_math_rejects_wrong_arity_and_keywords(): | 242 | def test_scalar_math_rejects_wrong_arity_and_keywords(): |
| 233 | with pytest.raises(ParserSyntaxError, match="requires exactly 3 positional arguments"): | 243 | with pytest.raises(ParserSyntaxError, match="requires exactly 3 positional arguments"): |
| 234 | 244 | ||
| 235 | - @pl.function(type=pl.FunctionType.SimtCallee) | 245 | + @pl.simt.function(max_threads=1) |
| 236 | - def missing_addend(value: pl.DT_FP32) -> pl.DT_FP32: | 246 | + def missing_addend(value: pl.DT_FP32): |
| 237 | - return pl.simt.fma(value, value) | 247 | + _test_result = pl.simt.fma(value, value) |
| 248 | + | ||
| 249 | + @pl.jit(auto_mutex=False) | ||
| 250 | + def kernel(value: pl.DT_FP32): | ||
| 251 | + with pl.section_vector(): | ||
| 252 | + pl.simt.launch(missing_addend, threads=1, args=(value,)) | ||
| 253 | + | ||
| 254 | + kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||
| 238 | 255 | ||
| 239 | with pytest.raises(ParserSyntaxError, match="requires exactly 1 positional argument"): | 256 | with pytest.raises(ParserSyntaxError, match="requires exactly 1 positional argument"): |
| 240 | 257 | ||
| 241 | - @pl.function(type=pl.FunctionType.SimtCallee) | 258 | + @pl.simt.function(max_threads=1) |
| 242 | - def keyword_argument(value: pl.DT_FP32) -> pl.DT_FP32: | 259 | + def keyword_argument(value: pl.DT_FP32): |
| 243 | - return pl.simt.exp(value=value) | 260 | + _test_result = pl.simt.exp(value=value) |
| 261 | + | ||
| 262 | + @pl.jit(auto_mutex=False) | ||
| 263 | + def kernel(value: pl.DT_FP32): | ||
| 264 | + with pl.section_vector(): | ||
| 265 | + pl.simt.launch(keyword_argument, threads=1, args=(value,)) | ||
| 266 | + | ||
| 267 | + kernel.to_kernel_def().parse_target_program(ir.SectionKind.Vector) | ||