You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by tq...@apache.org on 2026/09/22 22:07:54 UTC
(tvm) branch script/canonical-parser-df created (now af6dbafed2)
This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a change to branch script/canonical-parser-df
in repository https://gitbox.apache.org/repos/asf/tvm.git
at af6dbafed2 [FR][Relax] Preserve local function regions and recursive signatures
This branch includes the following new commits:
new e883105962 [D][TVMScript] Establish canonical construction and definition contexts
new 07124f2485 [F][TVMScript] Adapt source helpers and tests to explicit construction
new 77ffe0934b [FR][TVMScript] Preserve public validation and diagnostic boundaries
new af6dbafed2 [FR][Relax] Preserve local function regions and recursive signatures
The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(tvm) 02/04: [F][TVMScript] Adapt source helpers and tests to explicit construction
Posted by tq...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a commit to branch script/canonical-parser-df
in repository https://gitbox.apache.org/repos/asf/tvm.git
commit 07124f2485bf4fd8e836fb44fe2d6b2f683430ca
Author: Tianqi Chen <tq...@users.noreply.github.com>
AuthorDate: Tue Sep 22 22:02:04 2026 +0000
[F][TVMScript] Adapt source helpers and tests to explicit construction
Make host predicates and symbolic declarations explicit, retain public script imports, and migrate internal parser probes and dtype expectations to the canonical contracts.
---
python/tvm/backend/cuda/lang/warp_role.py | 2 +-
.../backend/trn/tile_primitive/binary/default.py | 2 +-
.../trn/tile_primitive/compose_op/binary_reduce.py | 4 +-
.../trn/tile_primitive/compose_op/unary_reduce.py | 4 +-
.../tvm/backend/trn/tile_primitive/gemm/default.py | 2 +-
.../tvm/backend/trn/tile_primitive/unary/utils.py | 6 +-
python/tvm/relax/backend/gpu_generic/sampling.py | 2 +-
python/tvm/relax/frontend/nn/llm/_page_kernels.py | 12 +-
.../tvm/s_tir/tensor_intrin/dot_product_common.py | 2 +-
python/tvm/s_tir/tensor_intrin/metal.py | 4 +-
python/tvm/s_tir/tensor_intrin/rocm.py | 8 +-
tests/python/codegen/test_target_codegen_vulkan.py | 20 +--
tests/python/relax/test_analysis_type_analysis.py | 2 -
tests/python/relax/test_frontend_onnx.py | 24 +--
.../s_tir/dlight/test_gpu_matmul_tensorize.py | 180 ++++++++++++++-------
.../test_meta_schedule_trace_apply.py | 14 +-
tests/python/te/test_te_create_primfunc.py | 6 +-
.../tirx-transform/test_tir_transform_vectorize.py | 2 +-
.../operator/tile_primitive/trn/test_binary_trn.py | 38 ++---
.../operator/tile_primitive/trn/test_unary_trn.py | 12 +-
tests/python/tirx/test_inline.py | 2 +-
tests/python/tirx/test_jit.py | 22 +--
tests/python/tirx/test_op_namespace_cleanup.py | 3 +-
tests/python/tirx/test_parser_printer.py | 2 +-
.../tvmscript/test_tvmscript_error_report.py | 4 +-
.../tvmscript/test_tvmscript_parser_evaluator.py | 14 +-
.../tvmscript/test_tvmscript_parser_source.py | 6 +-
.../python/tvmscript/test_tvmscript_parser_tir.py | 15 +-
tests/python/tvmscript/test_tvmscript_roundtrip.py | 15 +-
29 files changed, 242 insertions(+), 187 deletions(-)
diff --git a/python/tvm/backend/cuda/lang/warp_role.py b/python/tvm/backend/cuda/lang/warp_role.py
index b61cd8822d..51974e2ee9 100644
--- a/python/tvm/backend/cuda/lang/warp_role.py
+++ b/python/tvm/backend/cuda/lang/warp_role.py
@@ -132,7 +132,7 @@ class WarpgroupRole:
def __enter__(self):
if isinstance(self.wg_id_val, tuple):
start, stop = self.wg_id_val
- self._if_frame = T.If(start <= self.wg_id_var and self.wg_id_var < stop)
+ self._if_frame = T.If(T.And(T.LE(start, self.wg_id_var), self.wg_id_var < stop))
else:
self._if_frame = T.If(self.wg_id_var == self.wg_id_val)
self._if_frame.__enter__()
diff --git a/python/tvm/backend/trn/tile_primitive/binary/default.py b/python/tvm/backend/trn/tile_primitive/binary/default.py
index 85f19d11c5..9cf669d892 100644
--- a/python/tvm/backend/trn/tile_primitive/binary/default.py
+++ b/python/tvm/backend/trn/tile_primitive/binary/default.py
@@ -84,7 +84,7 @@ def binary_trn(
if inst_gen.make_guard(_dst):
dst_indices = T.meta_var(inst_gen.generate_indices(_dst))
src1_indices = T.meta_var(inst_gen.generate_indices(_src1))
- if CONST is None:
+ if T.constexpr(CONST is None):
src2_indices = T.meta_var(inst_gen.generate_indices(_src2))
T.evaluate(
func(
diff --git a/python/tvm/backend/trn/tile_primitive/compose_op/binary_reduce.py b/python/tvm/backend/trn/tile_primitive/compose_op/binary_reduce.py
index a3a83bb3a4..fd00bfc0a1 100644
--- a/python/tvm/backend/trn/tile_primitive/compose_op/binary_reduce.py
+++ b/python/tvm/backend/trn/tile_primitive/compose_op/binary_reduce.py
@@ -113,7 +113,7 @@ def binary_reduce_trn(op: TilePrimitiveCall, sctx: DispatchContext) -> PrimFunc
vec_dst_idx = T.meta_var(inst_gen.generate_indices(binary_output))
reduce_dst_idx = T.meta_var(inst_gen.generate_indices(reduce_output))
if inst_gen.make_guard(binary_output):
- if CONST is None:
+ if T.constexpr(CONST is None):
src_2_indices = T.meta_var(inst_gen.generate_indices(binary_input2)) # noqa: E501
T.nki.tensorscalar_reduce(dst2[tuple(reduce_dst_idx)], dst1[tuple(vec_dst_idx)], src1[tuple(src_1_indices)], src2[tuple(src_2_indices)], binary_opcode, reduce_opcode, reverse[0]) # noqa: E501
else:
@@ -134,7 +134,7 @@ def binary_reduce_trn(op: TilePrimitiveCall, sctx: DispatchContext) -> PrimFunc
if inst_gen.make_guard(binary_output):
src_1_indices = T.meta_var(inst_gen.generate_indices(binary_input1)) # noqa: E501
vec_dst_idx = T.meta_var(inst_gen.generate_indices(binary_output)) # noqa: E501
- if CONST is None:
+ if T.constexpr(CONST is None):
src_2_indices = T.meta_var(inst_gen.generate_indices(binary_input2)) # noqa: E501
T.nki.tensorscalar_reduce(intermediate_buffer[p_loop, reduction_b_loop], dst1[tuple(vec_dst_idx)], src1[tuple(src_1_indices)], src2[tuple(src_2_indices)], binary_opcode, reduce_opcode, reverse[0]) # noqa: E501
else:
diff --git a/python/tvm/backend/trn/tile_primitive/compose_op/unary_reduce.py b/python/tvm/backend/trn/tile_primitive/compose_op/unary_reduce.py
index 2cc80e57c2..8b56df56b1 100644
--- a/python/tvm/backend/trn/tile_primitive/compose_op/unary_reduce.py
+++ b/python/tvm/backend/trn/tile_primitive/compose_op/unary_reduce.py
@@ -112,7 +112,7 @@ def unary_reduce_trn(op: TilePrimitiveCall, sctx: DispatchContext) -> PrimFunc |
dst_1_indices = T.meta_var(inst_gen.generate_indices(unary_output))
dst_2_indices = T.meta_var(inst_gen.generate_indices(reduce_output))
if inst_gen.make_guard(unary_output):
- if isinstance(bias, TensorRegion):
+ if T.constexpr(isinstance(bias, TensorRegion)):
src_bias_indices = T.meta_var(inst_gen.generate_indices(bias))
T.evaluate(T.nki.activation_reduce(dst2[tuple(dst_2_indices)], dst1[tuple(dst_1_indices)], src[tuple(src_1_indices)], unary_opcode, reduce_opcode, bias_buffer[tuple(src_bias_indices)], scale)) # noqa: E501
else:
@@ -138,7 +138,7 @@ def unary_reduce_trn(op: TilePrimitiveCall, sctx: DispatchContext) -> PrimFunc |
src_1_indices = T.meta_var(inst_gen.generate_indices(unary_input))
dst_1_indices = T.meta_var(inst_gen.generate_indices(unary_output))
if inst_gen.make_guard(unary_output):
- if isinstance(bias, TensorRegion):
+ if T.constexpr(isinstance(bias, TensorRegion)):
src_bias_indices = T.meta_var(inst_gen.generate_indices(bias)) # noqa: E501
T.evaluate(T.nki.activation_reduce(intermediate_buffer[p_loop, reduction_b_loop], dst1[tuple(dst_1_indices)], src[tuple(src_1_indices)], unary_opcode, reduce_opcode, bias_buffer[tuple(src_bias_indices)], scale)) # noqa: E501
else:
diff --git a/python/tvm/backend/trn/tile_primitive/gemm/default.py b/python/tvm/backend/trn/tile_primitive/gemm/default.py
index 9935a38970..133a314327 100644
--- a/python/tvm/backend/trn/tile_primitive/gemm/default.py
+++ b/python/tvm/backend/trn/tile_primitive/gemm/default.py
@@ -235,7 +235,7 @@ def matmul_trn(op: TilePrimitiveCall, sctx: DispatchContext) -> PrimFunc | None:
rhs_indices = T.meta_var(inst_gen.generate_indices(B_buffer_region))
C_indices = T.meta_var(inst_gen.generate_indices(C_buffer_region))
if inst_gen.make_guard(A_buffer_region) and inst_gen.make_guard(B_buffer_region): # noqa: E501
- if C_as_output:
+ if T.constexpr(C_as_output):
T.evaluate(T.nki.matmul(acc[C_indices], A[lhs_indices], B[rhs_indices])) # noqa: E501
else:
T.evaluate(T.nki.matmul(acc[b_idx % max_psum_slots, lhs_f_loop, rhs_f_loop], A[lhs_indices], B[rhs_indices])) # noqa: E501
diff --git a/python/tvm/backend/trn/tile_primitive/unary/utils.py b/python/tvm/backend/trn/tile_primitive/unary/utils.py
index 106648b17d..ff635350a0 100644
--- a/python/tvm/backend/trn/tile_primitive/unary/utils.py
+++ b/python/tvm/backend/trn/tile_primitive/unary/utils.py
@@ -177,13 +177,13 @@ def generate_unary_func(
inst_gen.set_bind_map_all({p_var: p_loop, f_var: f_loop, b_var: b_loop})
dst_indices = T.meta_var(inst_gen.generate_indices(dst_buffer_region))
if inst_gen.make_guard(dst_buffer_region):
- if unary_op == MapOpType.FILL:
+ if T.constexpr(unary_op == MapOpType.FILL):
T.evaluate(T.nki.memset(dst[tuple(dst_indices)], _src))
else:
src_indices = T.meta_var(inst_gen.generate_indices(_src))
- if unary_op == MapOpType.RECIPROCAL:
+ if T.constexpr(unary_op == MapOpType.RECIPROCAL):
T.evaluate(T.nki.reciprocal(dst[tuple(dst_indices)], src[tuple(src_indices)])) # noqa: E501
- elif isinstance(bias, TensorRegion):
+ elif T.constexpr(isinstance(bias, TensorRegion)):
bias_indices = T.meta_var(inst_gen.generate_indices(bias))
T.evaluate(T.nki.activation(dst[tuple(dst_indices)], src[tuple(src_indices)], opcode, scale=scale, bias=bias_buffer[tuple(bias_indices)])) # noqa: E501
else:
diff --git a/python/tvm/relax/backend/gpu_generic/sampling.py b/python/tvm/relax/backend/gpu_generic/sampling.py
index 487027ce7b..302b8b7e8c 100644
--- a/python/tvm/relax/backend/gpu_generic/sampling.py
+++ b/python/tvm/relax/backend/gpu_generic/sampling.py
@@ -174,7 +174,7 @@ def gpu_multinomial_from_uniform(
local_sum[()] = T.Cast(dtype, init_value)
for i in T.unroll(thread_elem):
- if mask_local is not None:
+ if T.constexpr(mask_local is not None):
if mask_local[i]:
local_sum[()] = reduce_op(local_sum[()], data_local[i])
else:
diff --git a/python/tvm/relax/frontend/nn/llm/_page_kernels.py b/python/tvm/relax/frontend/nn/llm/_page_kernels.py
index 6682e5f18c..882de88e45 100644
--- a/python/tvm/relax/frontend/nn/llm/_page_kernels.py
+++ b/python/tvm/relax/frontend/nn/llm/_page_kernels.py
@@ -48,7 +48,7 @@ def _kv_cache_transpose_append(num_key_value_heads, head_dim, dtype, page_size:
var_position_map: T.handle,
):
T.func_attr({"tirx.noalias": True})
- ntoken = T.Var("num_tokens_excluding_cache", "int64")
+ ntoken = T.int64()
num_pages = T.int64()
pages_elem_offset = T.int64()
position_map_elem_offset = T.int32()
@@ -84,7 +84,7 @@ def _kv_cache_transpose_append_mla(d_qk: int, dtype, page_size: int = 16):
var_position_map: T.handle,
):
T.func_attr({"tirx.noalias": True})
- ntoken = T.Var("num_tokens_excluding_cache", "int64")
+ ntoken = T.int64()
num_pages = T.int64()
pages_elem_offset = T.int64()
position_map_elem_offset = T.int32()
@@ -115,8 +115,8 @@ def _kv_cache_debug_get_kv(num_hidden_layers, num_key_value_heads, head_dim, dty
layer_id: T.int64,
):
T.func_attr({"tirx.noalias": True})
- seqlen = T.Var("num_tokens_including_cache", "int64")
- page_size = T.Var("page_size", "int64")
+ seqlen = T.int64()
+ page_size = T.int64()
num_pages = T.int64()
pages_elem_offset = T.int64()
position_map_elem_offset = T.int64()
@@ -147,8 +147,8 @@ def _kv_cache_debug_get_kv_mla(num_hidden_layers, d_qk, dtype):
layer_id: T.int64,
):
T.func_attr({"tirx.noalias": True})
- seqlen = T.Var("num_tokens_including_cache", "int64")
- page_size = T.Var("page_size", "int64")
+ seqlen = T.int64()
+ page_size = T.int64()
num_pages = T.int64()
pages_elem_offset = T.int64()
position_map_elem_offset = T.int64()
diff --git a/python/tvm/s_tir/tensor_intrin/dot_product_common.py b/python/tvm/s_tir/tensor_intrin/dot_product_common.py
index 7272477406..74b1acebf0 100644
--- a/python/tvm/s_tir/tensor_intrin/dot_product_common.py
+++ b/python/tvm/s_tir/tensor_intrin/dot_product_common.py
@@ -56,7 +56,7 @@ def get_dp4a_intrin(dtype_a, dtype_b, dtype_c):
"__dp4a",
A.vload([0], vec_type_a),
B.vload([0], vec_type_b),
- T.uint32(0) if dtype_c == "uint32" else T.int32(0),
+ T.uint32(0) if T.constexpr(dtype_c == "uint32") else T.int32(0),
dtype=dtype_c,
)
diff --git a/python/tvm/s_tir/tensor_intrin/metal.py b/python/tvm/s_tir/tensor_intrin/metal.py
index 1750338044..8c63e9b46f 100644
--- a/python/tvm/s_tir/tensor_intrin/metal.py
+++ b/python/tvm/s_tir/tensor_intrin/metal.py
@@ -93,7 +93,7 @@ def get_simdgroup_load_intrin(
for i, j in T.grid(col, row):
with T.sblock("load"):
vii, vjj = T.axis.remap("SS", [i, j])
- if transpose_matrix:
+ if T.constexpr(transpose_matrix):
# C[vii, vjj] = A[vjj, vii]
C[vjj, vii] = A[vii, vjj]
else:
@@ -157,7 +157,7 @@ def get_simdgroup_store_intrin(
for i, j in T.grid(col, row):
with T.sblock("store"):
vii, vjj = T.axis.remap("SS", [i, j])
- if transpose_matrix:
+ if T.constexpr(transpose_matrix):
C[vjj, vii] = A[vii, vjj]
else:
C[vii, vjj] = A[vii, vjj]
diff --git a/python/tvm/s_tir/tensor_intrin/rocm.py b/python/tvm/s_tir/tensor_intrin/rocm.py
index 29749dd443..60b94ce192 100644
--- a/python/tvm/s_tir/tensor_intrin/rocm.py
+++ b/python/tvm/s_tir/tensor_intrin/rocm.py
@@ -336,8 +336,8 @@ def get_mfma_intrin(k_dim, in_dtype="float32", out_dtype="float32", b_transposed
T.launch_thread(tx, WARP_SIZE)
C[tx, T.ramp(0, 1, local_size_out)] = T.call_llvm_pure_intrin(
T.llvm_lookup_intrinsic_id(mfma_intrin),
- A[tx, T.ramp(0, 1, local_size) if local_size > 1 else 0],
- B[tx, T.ramp(0, 1, local_size) if local_size > 1 else 0],
+ A[tx, T.ramp(0, 1, local_size) if T.constexpr(local_size > 1) else 0],
+ B[tx, T.ramp(0, 1, local_size) if T.constexpr(local_size > 1) else 0],
C[tx, T.ramp(0, 1, local_size_out)],
T.int32(0),
T.int32(0),
@@ -366,12 +366,12 @@ def get_mfma_intrin(k_dim, in_dtype="float32", out_dtype="float32", b_transposed
T.call_intrin(
"int32",
"tirx.reinterpret",
- A[tx, T.ramp(0, 1, local_size) if local_size > 1 else 0],
+ A[tx, T.ramp(0, 1, local_size) if T.constexpr(local_size > 1) else 0],
),
T.call_intrin(
"int32",
"tirx.reinterpret",
- B[tx, T.ramp(0, 1, local_size) if local_size > 1 else 0],
+ B[tx, T.ramp(0, 1, local_size) if T.constexpr(local_size > 1) else 0],
),
C[tx, T.ramp(0, 1, local_size_out)],
T.int32(0),
diff --git a/tests/python/codegen/test_target_codegen_vulkan.py b/tests/python/codegen/test_target_codegen_vulkan.py
index d3213b4dcb..9faa2eb250 100644
--- a/tests/python/codegen/test_target_codegen_vulkan.py
+++ b/tests/python/codegen/test_target_codegen_vulkan.py
@@ -488,7 +488,7 @@ def test_cooperative_matrix(out_dtype):
v_j_o = T.axis.spatial(1, 0)
T.reads()
T.writes(compute_wmma_accumulator[0:16, 0:16])
- C = T.match_buffer(compute_wmma_accumulator[0:16, 0:16], (16, 16), out_dtype, strides=("C_s0", "C_s1"), scope="wmma.accumulator", offset_factor=16)
+ C = T.match_buffer(compute_wmma_accumulator[0:16, 0:16], (16, 16), out_dtype, strides=("C_0_s0", "C_0_s1"), scope="wmma.accumulator", offset_factor=16)
T.tvm_fill_fragment(C.data, 16, 16, 16, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, T.float32(0.0))
for k_0 in range(2):
for ax0_ax1_fused_0 in range(2):
@@ -516,8 +516,8 @@ def test_cooperative_matrix(out_dtype):
v1_o = T.axis.spatial(2, k_0 + ax1_0)
T.reads(X_shared[0:16, v1_o * 16:v1_o * 16 + 16])
T.writes(X_shared_wmma_matrix_a[0:16, v1_o * 16:v1_o * 16 + 16])
- A = T.match_buffer(X_shared[0:16, v1_o * 16:v1_o * 16 + 16], (16, 16), "float16", strides=("A_s0", "A_s1"), scope="shared", offset_factor=16)
- C = T.match_buffer(X_shared_wmma_matrix_a[0:16, v1_o * 16:v1_o * 16 + 16], (16, 16), "float16", strides=("C_s0", "C_s1"), scope="wmma.matrix_a", offset_factor=16)
+ A = T.match_buffer(X_shared[0:16, v1_o * 16:v1_o * 16 + 16], (16, 16), "float16", strides=("A_0_s0", "A_0_s1"), scope="shared", offset_factor=16)
+ C = T.match_buffer(X_shared_wmma_matrix_a[0:16, v1_o * 16:v1_o * 16 + 16], (16, 16), "float16", strides=("C_1_s0", "C_1_s1"), scope="wmma.matrix_a", offset_factor=16)
T.tvm_load_matrix_sync(C.data, 16, 16, 16, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, T.tvm_access_ptr(T.type_annotation("float16"), A.data, A.elem_offset, A.strides[0] * 16, 1), A.strides[0], "row_major")
for ax0_0 in T.unroll(1):
for ax1_0 in T.unroll(1):
@@ -526,8 +526,8 @@ def test_cooperative_matrix(out_dtype):
v1_o = T.axis.spatial(1, ax1_0)
T.reads(W_shared[v0_o * 16:v0_o * 16 + 16, 0:16])
T.writes(W_shared_wmma_matrix_b[v0_o * 16:v0_o * 16 + 16, 0:16])
- A = T.match_buffer(W_shared[v0_o * 16:v0_o * 16 + 16, 0:16], (16, 16), "float16", strides=("A_s0", "A_s1"), scope="shared", offset_factor=16)
- C = T.match_buffer(W_shared_wmma_matrix_b[v0_o * 16:v0_o * 16 + 16, 0:16], (16, 16), "float16", strides=("C_s0", "C_s1"), scope="wmma.matrix_b", offset_factor=16)
+ A = T.match_buffer(W_shared[v0_o * 16:v0_o * 16 + 16, 0:16], (16, 16), "float16", strides=("A_1_s0", "A_1_s1"), scope="shared", offset_factor=16)
+ C = T.match_buffer(W_shared_wmma_matrix_b[v0_o * 16:v0_o * 16 + 16, 0:16], (16, 16), "float16", strides=("C_2_s0", "C_2_s1"), scope="wmma.matrix_b", offset_factor=16)
T.tvm_load_matrix_sync(C.data, 16, 16, 16, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, T.tvm_access_ptr(T.type_annotation("float16"), A.data, A.elem_offset, A.strides[0] * 16, 1), A.strides[0], "row_major")
with T.sblock("compute_update_o"):
v_i_o = T.axis.spatial(1, 0)
@@ -535,17 +535,17 @@ def test_cooperative_matrix(out_dtype):
v_k_o = T.axis.reduce(2, k_0)
T.reads(compute_wmma_accumulator[0:16, 0:16], X_shared_wmma_matrix_a[0:16, v_k_o * 16:v_k_o * 16 + 16], W_shared_wmma_matrix_b[v_k_o * 16:v_k_o * 16 + 16, 0:16])
T.writes(compute_wmma_accumulator[0:16, 0:16])
- A = T.match_buffer(X_shared_wmma_matrix_a[0:16, v_k_o * 16:v_k_o * 16 + 16], (16, 16), "float16", strides=("A_s0", "A_s1"), scope="wmma.matrix_a", offset_factor=16)
- B = T.match_buffer(W_shared_wmma_matrix_b[v_k_o * 16:v_k_o * 16 + 16, 0:16], (16, 16), "float16", strides=("B_s0", "B_s1"), scope="wmma.matrix_b", offset_factor=16)
- C = T.match_buffer(compute_wmma_accumulator[0:16, 0:16], (16, 16), out_dtype, strides=("C_s0", "C_s1"), scope="wmma.accumulator", offset_factor=16)
+ A = T.match_buffer(X_shared_wmma_matrix_a[0:16, v_k_o * 16:v_k_o * 16 + 16], (16, 16), "float16", strides=("A_2_s0", "A_2_s1"), scope="wmma.matrix_a", offset_factor=16)
+ B = T.match_buffer(W_shared_wmma_matrix_b[v_k_o * 16:v_k_o * 16 + 16, 0:16], (16, 16), "float16", strides=("B_0_s0", "B_0_s1"), scope="wmma.matrix_b", offset_factor=16)
+ C = T.match_buffer(compute_wmma_accumulator[0:16, 0:16], (16, 16), out_dtype, strides=("C_3_s0", "C_3_s1"), scope="wmma.accumulator", offset_factor=16)
T.tvm_mma_sync(C.data, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, A.data, A.elem_offset // A.strides[0] // 16 * (A.strides[0] // 16) + A.elem_offset % A.strides[0] // 16, B.data, B.elem_offset // B.strides[0] // 16 * (B.strides[0] // 16) + B.elem_offset % B.strides[0] // 16, C.data, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16)
with T.sblock("compute_wmma.accumulator_o"):
v0_o = T.axis.spatial(1, 0)
v1_o = T.axis.spatial(1, 0)
T.reads(compute_wmma_accumulator[0:16, 0:16])
T.writes(compute[0:16, 0:16])
- A = T.match_buffer(compute_wmma_accumulator[0:16, 0:16], (16, 16), out_dtype, strides=("A_s0", "A_s1"), scope="wmma.accumulator", offset_factor=16)
- C = T.match_buffer(compute[0:16, 0:16], (16, 16), out_dtype, strides=("C_s0", "C_s1"), offset_factor=16)
+ A = T.match_buffer(compute_wmma_accumulator[0:16, 0:16], (16, 16), out_dtype, strides=("A_3_s0", "A_3_s1"), scope="wmma.accumulator", offset_factor=16)
+ C = T.match_buffer(compute[0:16, 0:16], (16, 16), out_dtype, strides=("C_4_s0", "C_4_s1"), offset_factor=16)
T.tvm_store_matrix_sync(A.data, 16, 16, 16, A.elem_offset // A.strides[0] // 16 * (A.strides[0] // 16) + A.elem_offset % A.strides[0] // 16, T.tvm_access_ptr(T.type_annotation(out_dtype), C.data, C.elem_offset, C.strides[0] * 16, 2), C.strides[0], "row_major")
# fmt: on
diff --git a/tests/python/relax/test_analysis_type_analysis.py b/tests/python/relax/test_analysis_type_analysis.py
index b0b6a54aa0..ca6a595320 100644
--- a/tests/python/relax/test_analysis_type_analysis.py
+++ b/tests/python/relax/test_analysis_type_analysis.py
@@ -649,8 +649,6 @@ def test_prim_type_lca(test_case):
def _normalize_ty(ty):
if isinstance(ty, tvm.relax.Type):
return ty
- elif isinstance(ty, tvm.script.parser.relax.entry.TypeProxy):
- return ty.as_ty()
elif callable(ty):
return ty()
else:
diff --git a/tests/python/relax/test_frontend_onnx.py b/tests/python/relax/test_frontend_onnx.py
index d7aa987c05..0efb8030a6 100644
--- a/tests/python/relax/test_frontend_onnx.py
+++ b/tests/python/relax/test_frontend_onnx.py
@@ -40,7 +40,7 @@ from onnx import ModelProto, TensorProto, helper, numpy_helper
import tvm
import tvm.testing
-from tvm import relax
+from tvm import relax, tirx
from tvm.relax.frontend.onnx import from_onnx
from tvm.script import ir as I
from tvm.script import relax as R
@@ -1053,15 +1053,14 @@ def _make_expected_broadcast_ir_min(
Returns:
Expected IR module for the Min operation.
"""
- output_shape = (x_shape[0], 4)
@I.ir_module
class ExpectedMin:
@R.function
def main(
- x: R.Tensor(x_shape, dtype="float32"),
- y: R.Tensor(y_shape, dtype="float32"),
- ) -> R.Tensor(output_shape, dtype="float32"):
+ x: R.Tensor(("n", x_shape[1]), dtype="float32"),
+ y: R.Tensor(("n", y_shape[1]), dtype="float32"),
+ ) -> R.Tensor(("n", 4), dtype="float32"):
n = T.int64()
R.func_attr({"num_input": 2})
with R.dataflow():
@@ -1088,15 +1087,14 @@ def _make_expected_broadcast_ir_max(
Returns:
Expected IR module for the Max operation.
"""
- output_shape = (x_shape[0], 4)
@I.ir_module
class ExpectedMax:
@R.function
def main(
- x: R.Tensor(x_shape, dtype="float32"),
- y: R.Tensor(y_shape, dtype="float32"),
- ) -> R.Tensor(output_shape, dtype="float32"):
+ x: R.Tensor(("n", x_shape[1]), dtype="float32"),
+ y: R.Tensor(("n", y_shape[1]), dtype="float32"),
+ ) -> R.Tensor(("n", 4), dtype="float32"):
n = T.int64()
R.func_attr({"num_input": 2})
with R.dataflow():
@@ -6846,7 +6844,7 @@ def _make_reduce_expected_ir(
def expected_input_shape(shape):
if not dynamic:
return tuple(shape)
- return tuple(f"reduce_dim_{i}" for i in range(len(shape)))
+ return tuple(tirx.Var(f"reduce_dim_{i}", "int64") for i in range(len(shape)))
axis = None if not axes else tuple(axes)
parser_vars = {
@@ -8784,7 +8782,7 @@ def test_split():
shape = shape_tuple(shape)
if not dynamic:
return shape
- return tuple(f"split_input_dim_{i}" for i in range(len(shape)))
+ return tuple(tirx.Var(f"split_input_dim_{i}", "int64") for i in range(len(shape)))
dtype = np.dtype(fp_arith).name
input_shape = expected_input_shape(indata_shape)
@@ -9078,7 +9076,9 @@ def test_tile_dynamic_repeats():
def make_expected(dynamic_input, in_shape):
rank = len(in_shape)
input_shape = (
- tuple(f"tile_data_dim_{i}" for i in range(rank)) if dynamic_input else tuple(in_shape)
+ tuple(tirx.Var(f"tile_data_dim_{i}", "int64") for i in range(rank))
+ if dynamic_input
+ else tuple(in_shape)
)
if rank == 2:
diff --git a/tests/python/s_tir/dlight/test_gpu_matmul_tensorize.py b/tests/python/s_tir/dlight/test_gpu_matmul_tensorize.py
index d12d713d3b..f3b971d0e6 100644
--- a/tests/python/s_tir/dlight/test_gpu_matmul_tensorize.py
+++ b/tests/python/s_tir/dlight/test_gpu_matmul_tensorize.py
@@ -65,7 +65,8 @@ def test_matmul_tensorize():
v2_i_init_o = T.axis.spatial(1, 0)
T.reads()
T.writes(compute_reindex_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
- C = T.match_buffer(compute_reindex_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=("C_s0", "C_s1"), scope="wmma.accumulator", offset_factor=16)
+ C_s0, C_s1 = T.int32(), T.int32()
+ C = T.match_buffer(compute_reindex_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=(C_s0, C_s1), scope="wmma.accumulator", offset_factor=16)
T.tvm_fill_fragment(C.data, 16, 16, 16, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, T.float32(0))
for ax3_0_0 in range(4, annotations={"software_pipeline_order": [0, 3, 1, 4, 5, 2, 6], "software_pipeline_stage": [0, 0, 0, 0, 0, 1, 1]}):
for ax0_ax1_fused_0 in range(4):
@@ -101,8 +102,10 @@ def test_matmul_tensorize():
v2_o = T.axis.spatial(16, ax3_0_0 * 4 + ax3_0_1 + ax1_0)
T.reads(X_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
T.writes(X_reindex_shared_dyn_wmma_matrix_a[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
- A = T.match_buffer(X_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=("A_s0", "A_s1"), scope="shared.dyn", offset_factor=16)
- C = T.match_buffer(X_reindex_shared_dyn_wmma_matrix_a[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=("C_s0", "C_s1"), scope="wmma.matrix_a", offset_factor=16)
+ A_s0, A_s1 = T.int32(), T.int32()
+ A = T.match_buffer(X_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=(A_s0, A_s1), scope="shared.dyn", offset_factor=16)
+ C_1_s0, C_1_s1 = T.int32(), T.int32()
+ C = T.match_buffer(X_reindex_shared_dyn_wmma_matrix_a[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=(C_1_s0, C_1_s1), scope="wmma.matrix_a", offset_factor=16)
T.tvm_load_matrix_sync(C.data, 16, 16, 16, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, T.tvm_access_ptr(T.type_annotation("float16"), A.data, A.elem_offset, A.strides[0] * 16, 1), A.strides[0], "row_major")
for ax0_0 in T.unroll(2):
for ax1_0 in T.unroll(1):
@@ -112,8 +115,10 @@ def test_matmul_tensorize():
v2_o = T.axis.spatial(16, ax3_0_0 * 4 + ax3_0_1 + ax1_0)
T.reads(W_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
T.writes(W_reindex_shared_dyn_wmma_matrix_b[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
- A = T.match_buffer(W_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=("A_s0", "A_s1"), scope="shared.dyn", offset_factor=16)
- C = T.match_buffer(W_reindex_shared_dyn_wmma_matrix_b[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=("C_s0", "C_s1"), scope="wmma.matrix_b", offset_factor=16)
+ A_1_s0, A_1_s1 = T.int32(), T.int32()
+ A = T.match_buffer(W_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=(A_1_s0, A_1_s1), scope="shared.dyn", offset_factor=16)
+ C_2_s0, C_2_s1 = T.int32(), T.int32()
+ C = T.match_buffer(W_reindex_shared_dyn_wmma_matrix_b[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=(C_2_s0, C_2_s1), scope="wmma.matrix_b", offset_factor=16)
T.tvm_load_matrix_sync(C.data, 16, 16, 16, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, T.tvm_access_ptr(T.type_annotation("float16"), A.data, A.elem_offset, A.strides[0] * 16, 1), A.strides[0], "col_major")
for ax1_0_3, ax2_0_3 in T.grid(2, 2):
with T.sblock("compute_o_update"):
@@ -129,9 +134,12 @@ def test_matmul_tensorize():
v3_i_o = T.axis.reduce(1, 0)
T.reads(compute_reindex_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], X_reindex_shared_dyn_wmma_matrix_a[0, v1_o * 16:v1_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], W_reindex_shared_dyn_wmma_matrix_b[0, v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16])
T.writes(compute_reindex_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
- A = T.match_buffer(X_reindex_shared_dyn_wmma_matrix_a[0, v1_o * 16:v1_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "float16", strides=("A_s0", "A_s1"), scope="wmma.matrix_a", offset_factor=16)
- B = T.match_buffer(W_reindex_shared_dyn_wmma_matrix_b[0, v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "float16", strides=("B_s0", "B_s1"), scope="wmma.matrix_b", offset_factor=16)
- C = T.match_buffer(compute_reindex_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=("C_s0", "C_s1"), scope="wmma.accumulator", offset_factor=16)
+ A_2_s0, A_2_s1 = T.int32(), T.int32()
+ A = T.match_buffer(X_reindex_shared_dyn_wmma_matrix_a[0, v1_o * 16:v1_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "float16", strides=(A_2_s0, A_2_s1), scope="wmma.matrix_a", offset_factor=16)
+ B_s0, B_s1 = T.int32(), T.int32()
+ B = T.match_buffer(W_reindex_shared_dyn_wmma_matrix_b[0, v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "float16", strides=(B_s0, B_s1), scope="wmma.matrix_b", offset_factor=16)
+ C_3_s0, C_3_s1 = T.int32(), T.int32()
+ C = T.match_buffer(compute_reindex_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=(C_3_s0, C_3_s1), scope="wmma.accumulator", offset_factor=16)
T.tvm_mma_sync(C.data, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, A.data, A.elem_offset // A.strides[0] // 16 * (A.strides[0] // 16) + A.elem_offset % A.strides[0] // 16, B.data, B.elem_offset // B.strides[0] // 16 * (B.strides[0] // 16) + B.elem_offset % B.strides[0] // 16, C.data, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16)
for ax0_0, ax1_0 in T.grid(2, 2):
with T.sblock("compute_reindex_shared.dyn_wmma.accumulator_o"):
@@ -140,8 +148,10 @@ def test_matmul_tensorize():
v2_o = T.axis.spatial(16, ax1_0_1_ax2_0_1_fused * 8 + ax2_0_2_ax1_0_2_fused // 4 * 2 + ax1_0)
T.reads(compute_reindex_shared_dyn_wmma_accumulator[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
T.writes(compute_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
- A = T.match_buffer(compute_reindex_shared_dyn_wmma_accumulator[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=("A_s0", "A_s1"), scope="wmma.accumulator", offset_factor=16)
- C = T.match_buffer(compute_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=("C_s0", "C_s1"), scope="shared.dyn", offset_factor=16)
+ A_3_s0, A_3_s1 = T.int32(), T.int32()
+ A = T.match_buffer(compute_reindex_shared_dyn_wmma_accumulator[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=(A_3_s0, A_3_s1), scope="wmma.accumulator", offset_factor=16)
+ C_4_s0, C_4_s1 = T.int32(), T.int32()
+ C = T.match_buffer(compute_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=(C_4_s0, C_4_s1), scope="shared.dyn", offset_factor=16)
T.tvm_store_matrix_sync(A.data, 16, 16, 16, A.elem_offset // A.strides[0] // 16 * (A.strides[0] // 16) + A.elem_offset % A.strides[0] // 16, T.tvm_access_ptr(T.type_annotation("float16"), C.data, C.elem_offset, C.strides[0] * 16, 2), C.strides[0], "row_major")
for ax0_ax1_fused_0 in range(8):
for ax0_ax1_fused_1 in T.thread_binding(32, thread="threadIdx.x"):
@@ -329,7 +339,8 @@ def test_matmul_tensorize_epilogue():
v2_i_init_o = T.axis.spatial(1, 0)
T.reads()
T.writes(var_NT_matmul_intermediate_reindex_pad_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
- C = T.match_buffer(var_NT_matmul_intermediate_reindex_pad_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=("C_s0", "C_s1"), scope="wmma.accumulator", offset_factor=16)
+ C_s0, C_s1 = T.int32(), T.int32()
+ C = T.match_buffer(var_NT_matmul_intermediate_reindex_pad_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=(C_s0, C_s1), scope="wmma.accumulator", offset_factor=16)
T.tvm_fill_fragment(C.data, 16, 16, 16, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, T.float32(0))
for ax3_0_0 in range(32, annotations={"software_pipeline_order": [0, 3, 1, 4, 5, 2, 6], "software_pipeline_stage": [0, 0, 0, 0, 0, 1, 1]}):
for ax0_ax1_fused_0 in range(4):
@@ -365,8 +376,10 @@ def test_matmul_tensorize_epilogue():
v2_o = T.axis.spatial(128, ax3_0_0 * 4 + ax3_0_1 + ax1_0)
T.reads(lv42_reindex_pad_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
T.writes(lv42_reindex_pad_shared_dyn_wmma_matrix_a[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
- A = T.match_buffer(lv42_reindex_pad_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=("A_s0", "A_s1"), scope="shared.dyn", offset_factor=16)
- C = T.match_buffer(lv42_reindex_pad_shared_dyn_wmma_matrix_a[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=("C_s0", "C_s1"), scope="wmma.matrix_a", offset_factor=16)
+ A_s0, A_s1 = T.int32(), T.int32()
+ A = T.match_buffer(lv42_reindex_pad_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=(A_s0, A_s1), scope="shared.dyn", offset_factor=16)
+ C_1_s0, C_1_s1 = T.int32(), T.int32()
+ C = T.match_buffer(lv42_reindex_pad_shared_dyn_wmma_matrix_a[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=(C_1_s0, C_1_s1), scope="wmma.matrix_a", offset_factor=16)
T.tvm_load_matrix_sync(C.data, 16, 16, 16, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, T.tvm_access_ptr(T.type_annotation("float16"), A.data, A.elem_offset, A.strides[0] * 16, 1), A.strides[0], "row_major")
for ax0_0 in T.unroll(2):
for ax1_0 in T.unroll(1):
@@ -376,8 +389,10 @@ def test_matmul_tensorize_epilogue():
v2_o = T.axis.spatial(128, ax3_0_0 * 4 + ax3_0_1 + ax1_0)
T.reads(p_output0_intermediate_1_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
T.writes(p_output0_intermediate_1_reindex_shared_dyn_wmma_matrix_b[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
- A = T.match_buffer(p_output0_intermediate_1_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=("A_s0", "A_s1"), scope="shared.dyn", offset_factor=16)
- C = T.match_buffer(p_output0_intermediate_1_reindex_shared_dyn_wmma_matrix_b[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=("C_s0", "C_s1"), scope="wmma.matrix_b", offset_factor=16)
+ A_1_s0, A_1_s1 = T.int32(), T.int32()
+ A = T.match_buffer(p_output0_intermediate_1_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=(A_1_s0, A_1_s1), scope="shared.dyn", offset_factor=16)
+ C_2_s0, C_2_s1 = T.int32(), T.int32()
+ C = T.match_buffer(p_output0_intermediate_1_reindex_shared_dyn_wmma_matrix_b[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=(C_2_s0, C_2_s1), scope="wmma.matrix_b", offset_factor=16)
T.tvm_load_matrix_sync(C.data, 16, 16, 16, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, T.tvm_access_ptr(T.type_annotation("float16"), A.data, A.elem_offset, A.strides[0] * 16, 1), A.strides[0], "col_major")
for ax1_0_3, ax2_0_3 in T.grid(2, 2):
with T.sblock("NT_matmul_o_update"):
@@ -393,9 +408,12 @@ def test_matmul_tensorize_epilogue():
v3_i_o = T.axis.reduce(1, 0)
T.reads(var_NT_matmul_intermediate_reindex_pad_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], lv42_reindex_pad_shared_dyn_wmma_matrix_a[0, v1_o * 16:v1_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], p_output0_intermediate_1_reindex_shared_dyn_wmma_matrix_b[0, v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16])
T.writes(var_NT_matmul_intermediate_reindex_pad_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
- A = T.match_buffer(lv42_reindex_pad_shared_dyn_wmma_matrix_a[0, v1_o * 16:v1_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "float16", strides=("A_s0", "A_s1"), scope="wmma.matrix_a", offset_factor=16)
- B = T.match_buffer(p_output0_intermediate_1_reindex_shared_dyn_wmma_matrix_b[0, v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "float16", strides=("B_s0", "B_s1"), scope="wmma.matrix_b", offset_factor=16)
- C = T.match_buffer(var_NT_matmul_intermediate_reindex_pad_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=("C_s0", "C_s1"), scope="wmma.accumulator", offset_factor=16)
+ A_2_s0, A_2_s1 = T.int32(), T.int32()
+ A = T.match_buffer(lv42_reindex_pad_shared_dyn_wmma_matrix_a[0, v1_o * 16:v1_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "float16", strides=(A_2_s0, A_2_s1), scope="wmma.matrix_a", offset_factor=16)
+ B_s0, B_s1 = T.int32(), T.int32()
+ B = T.match_buffer(p_output0_intermediate_1_reindex_shared_dyn_wmma_matrix_b[0, v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "float16", strides=(B_s0, B_s1), scope="wmma.matrix_b", offset_factor=16)
+ C_3_s0, C_3_s1 = T.int32(), T.int32()
+ C = T.match_buffer(var_NT_matmul_intermediate_reindex_pad_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=(C_3_s0, C_3_s1), scope="wmma.accumulator", offset_factor=16)
T.tvm_mma_sync(C.data, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, A.data, A.elem_offset // A.strides[0] // 16 * (A.strides[0] // 16) + A.elem_offset % A.strides[0] // 16, B.data, B.elem_offset // B.strides[0] // 16 * (B.strides[0] // 16) + B.elem_offset % B.strides[0] // 16, C.data, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16)
for ax0_0, ax1_0 in T.grid(2, 2):
with T.sblock("var_NT_matmul_intermediate_reindex_pad_shared.dyn_wmma.accumulator_o"):
@@ -404,8 +422,10 @@ def test_matmul_tensorize_epilogue():
v2_o = T.axis.spatial(256, ax1_0_1_ax2_0_1_fused * 8 + ax2_0_2_ax1_0_2_fused // 4 * 2 + ax1_0)
T.reads(var_NT_matmul_intermediate_reindex_pad_shared_dyn_wmma_accumulator[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
T.writes(var_NT_matmul_intermediate_reindex_pad_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
- A = T.match_buffer(var_NT_matmul_intermediate_reindex_pad_shared_dyn_wmma_accumulator[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=("A_s0", "A_s1"), scope="wmma.accumulator", offset_factor=16)
- C = T.match_buffer(var_NT_matmul_intermediate_reindex_pad_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=("C_s0", "C_s1"), scope="shared.dyn", offset_factor=16)
+ A_3_s0, A_3_s1 = T.int32(), T.int32()
+ A = T.match_buffer(var_NT_matmul_intermediate_reindex_pad_shared_dyn_wmma_accumulator[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=(A_3_s0, A_3_s1), scope="wmma.accumulator", offset_factor=16)
+ C_4_s0, C_4_s1 = T.int32(), T.int32()
+ C = T.match_buffer(var_NT_matmul_intermediate_reindex_pad_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "float16", strides=(C_4_s0, C_4_s1), scope="shared.dyn", offset_factor=16)
T.tvm_store_matrix_sync(A.data, 16, 16, 16, A.elem_offset // A.strides[0] // 16 * (A.strides[0] // 16) + A.elem_offset % A.strides[0] // 16, T.tvm_access_ptr(T.type_annotation("float16"), C.data, C.elem_offset, C.strides[0] * 16, 2), C.strides[0], "row_major")
for ax0_ax1_fused_0 in range(8):
for ax0_ax1_fused_1 in T.thread_binding(32, thread="threadIdx.x"):
@@ -468,7 +488,8 @@ def test_matmul_int8_tensorize():
v2_i_init_o = T.axis.spatial(1, 0)
T.reads()
T.writes(compute_reindex_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
- C = T.match_buffer(compute_reindex_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int32", strides=("C_s0", "C_s1"), scope="wmma.accumulator", offset_factor=16)
+ C_s0, C_s1 = T.int32(), T.int32()
+ C = T.match_buffer(compute_reindex_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int32", strides=(C_s0, C_s1), scope="wmma.accumulator", offset_factor=16)
T.tvm_fill_fragment(C.data, 16, 16, 16, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, T.float32(0))
for ax3_0_0 in T.serial(16, annotations={"software_pipeline_order": [0, 3, 1, 4, 5, 2, 6], "software_pipeline_stage": [0, 0, 0, 0, 0, 1, 1]}):
for ax0_ax1_fused_0 in range(1):
@@ -504,8 +525,10 @@ def test_matmul_int8_tensorize():
v2_o = T.axis.spatial(16, ax3_0_0 + ax1_0)
T.reads(X_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
T.writes(X_reindex_shared_dyn_wmma_matrix_a[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
- A = T.match_buffer(X_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int8", strides=("A_s0", "A_s1"), scope="shared.dyn", offset_factor=16)
- C = T.match_buffer(X_reindex_shared_dyn_wmma_matrix_a[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int8", strides=("C_s0", "C_s1"), scope="wmma.matrix_a", offset_factor=16)
+ A_s0, A_s1 = T.int32(), T.int32()
+ A = T.match_buffer(X_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int8", strides=(A_s0, A_s1), scope="shared.dyn", offset_factor=16)
+ C_1_s0, C_1_s1 = T.int32(), T.int32()
+ C = T.match_buffer(X_reindex_shared_dyn_wmma_matrix_a[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int8", strides=(C_1_s0, C_1_s1), scope="wmma.matrix_a", offset_factor=16)
T.tvm_load_matrix_sync(C.data, 16, 16, 16, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, T.tvm_access_ptr(T.type_annotation("int8"), A.data, A.elem_offset, A.strides[0] * 16, 1), A.strides[0], "row_major")
for ax0_0 in T.unroll(2):
for ax1_0 in T.unroll(1):
@@ -515,8 +538,10 @@ def test_matmul_int8_tensorize():
v2_o = T.axis.spatial(16, ax3_0_0 + ax1_0)
T.reads(W_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
T.writes(W_reindex_shared_dyn_wmma_matrix_b[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
- A = T.match_buffer(W_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int8", strides=("A_s0", "A_s1"), scope="shared.dyn", offset_factor=16)
- C = T.match_buffer(W_reindex_shared_dyn_wmma_matrix_b[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int8", strides=("C_s0", "C_s1"), scope="wmma.matrix_b", offset_factor=16)
+ A_1_s0, A_1_s1 = T.int32(), T.int32()
+ A = T.match_buffer(W_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int8", strides=(A_1_s0, A_1_s1), scope="shared.dyn", offset_factor=16)
+ C_2_s0, C_2_s1 = T.int32(), T.int32()
+ C = T.match_buffer(W_reindex_shared_dyn_wmma_matrix_b[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int8", strides=(C_2_s0, C_2_s1), scope="wmma.matrix_b", offset_factor=16)
T.tvm_load_matrix_sync(C.data, 16, 16, 16, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, T.tvm_access_ptr(T.type_annotation("int8"), A.data, A.elem_offset, A.strides[0] * 16, 1), A.strides[0], "col_major")
for ax1_0_3, ax2_0_3 in T.grid(2, 2):
with T.sblock("compute_o_update"):
@@ -532,9 +557,12 @@ def test_matmul_int8_tensorize():
v3_i_o = T.axis.reduce(1, 0)
T.reads(compute_reindex_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], X_reindex_shared_dyn_wmma_matrix_a[0, v1_o * 16:v1_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], W_reindex_shared_dyn_wmma_matrix_b[0, v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16])
T.writes(compute_reindex_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
- A = T.match_buffer(X_reindex_shared_dyn_wmma_matrix_a[0, v1_o * 16:v1_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "int8", strides=("A_s0", "A_s1"), scope="wmma.matrix_a", offset_factor=16)
- B = T.match_buffer(W_reindex_shared_dyn_wmma_matrix_b[0, v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "int8", strides=("B_s0", "B_s1"), scope="wmma.matrix_b", offset_factor=16)
- C = T.match_buffer(compute_reindex_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int32", strides=("C_s0", "C_s1"), scope="wmma.accumulator", offset_factor=16)
+ A_2_s0, A_2_s1 = T.int32(), T.int32()
+ A = T.match_buffer(X_reindex_shared_dyn_wmma_matrix_a[0, v1_o * 16:v1_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "int8", strides=(A_2_s0, A_2_s1), scope="wmma.matrix_a", offset_factor=16)
+ B_s0, B_s1 = T.int32(), T.int32()
+ B = T.match_buffer(W_reindex_shared_dyn_wmma_matrix_b[0, v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "int8", strides=(B_s0, B_s1), scope="wmma.matrix_b", offset_factor=16)
+ C_3_s0, C_3_s1 = T.int32(), T.int32()
+ C = T.match_buffer(compute_reindex_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int32", strides=(C_3_s0, C_3_s1), scope="wmma.accumulator", offset_factor=16)
T.tvm_mma_sync(C.data, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, A.data, A.elem_offset // A.strides[0] // 16 * (A.strides[0] // 16) + A.elem_offset % A.strides[0] // 16, B.data, B.elem_offset // B.strides[0] // 16 * (B.strides[0] // 16) + B.elem_offset % B.strides[0] // 16, C.data, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16)
for ax0_0, ax1_0 in T.grid(2, 2):
with T.sblock("compute_reindex_shared.dyn_wmma.accumulator_o"):
@@ -543,8 +571,10 @@ def test_matmul_int8_tensorize():
v2_o = T.axis.spatial(16, ax1_0_1_ax2_0_1_fused * 8 + ax2_0_2_ax1_0_2_fused // 4 * 2 + ax1_0)
T.reads(compute_reindex_shared_dyn_wmma_accumulator[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
T.writes(compute_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
- A = T.match_buffer(compute_reindex_shared_dyn_wmma_accumulator[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int32", strides=("A_s0", "A_s1"), scope="wmma.accumulator", offset_factor=16)
- C = T.match_buffer(compute_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int32", strides=("C_s0", "C_s1"), scope="shared.dyn", offset_factor=16)
+ A_3_s0, A_3_s1 = T.int32(), T.int32()
+ A = T.match_buffer(compute_reindex_shared_dyn_wmma_accumulator[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int32", strides=(A_3_s0, A_3_s1), scope="wmma.accumulator", offset_factor=16)
+ C_4_s0, C_4_s1 = T.int32(), T.int32()
+ C = T.match_buffer(compute_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int32", strides=(C_4_s0, C_4_s1), scope="shared.dyn", offset_factor=16)
T.tvm_store_matrix_sync(A.data, 16, 16, 16, A.elem_offset // A.strides[0] // 16 * (A.strides[0] // 16) + A.elem_offset % A.strides[0] // 16, T.tvm_access_ptr(T.type_annotation("int32"), C.data, C.elem_offset, C.strides[0] * 16, 2), C.strides[0], "row_major")
for ax0_ax1_fused_0 in range(8):
for ax0_ax1_fused_1 in T.thread_binding(32, thread="threadIdx.x"):
@@ -612,7 +642,8 @@ def test_matmul_int8_tensorize_3d2d_dyn():
v2_i_init_o = T.axis.spatial(1, 0)
T.reads()
T.writes(matmul_1_reindex_pad_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
- C = T.match_buffer(matmul_1_reindex_pad_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int32", strides=("C_s0", "C_s1"), scope="wmma.accumulator", offset_factor=16)
+ C_s0, C_s1 = T.int32(), T.int32()
+ C = T.match_buffer(matmul_1_reindex_pad_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int32", strides=(C_s0, C_s1), scope="wmma.accumulator", offset_factor=16)
T.tvm_fill_fragment(C.data, 16, 16, 16, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, T.float32(0))
for ax3_0_0 in T.serial(1376, annotations={"software_pipeline_order": [0, 3, 1, 4, 5, 2, 6], "software_pipeline_stage": [0, 0, 0, 0, 0, 1, 1]}):
for ax0_ax1_fused_0 in range(1):
@@ -648,8 +679,10 @@ def test_matmul_int8_tensorize_3d2d_dyn():
v2_o = T.axis.spatial(1376, ax3_0_0 + ax1_0)
T.reads(A_reindex_pad_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
T.writes(A_reindex_pad_shared_dyn_wmma_matrix_a[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
- A_1 = T.match_buffer(A_reindex_pad_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int8", strides=("A_s0", "A_s1"), scope="shared.dyn", offset_factor=16)
- C = T.match_buffer(A_reindex_pad_shared_dyn_wmma_matrix_a[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int8", strides=("C_s0", "C_s1"), scope="wmma.matrix_a", offset_factor=16)
+ A_s0, A_s1 = T.int32(), T.int32()
+ A_1 = T.match_buffer(A_reindex_pad_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int8", strides=(A_s0, A_s1), scope="shared.dyn", offset_factor=16)
+ C_1_s0, C_1_s1 = T.int32(), T.int32()
+ C = T.match_buffer(A_reindex_pad_shared_dyn_wmma_matrix_a[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int8", strides=(C_1_s0, C_1_s1), scope="wmma.matrix_a", offset_factor=16)
T.tvm_load_matrix_sync(C.data, 16, 16, 16, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, T.tvm_access_ptr(T.type_annotation("int8"), A_1.data, A_1.elem_offset, A_1.strides[0] * 16, 1), A_1.strides[0], "row_major")
for ax0_0 in T.unroll(2):
for ax1_0 in T.unroll(1):
@@ -659,8 +692,10 @@ def test_matmul_int8_tensorize_3d2d_dyn():
v2_o = T.axis.spatial(1376, ax3_0_0 + ax1_0)
T.reads(B_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
T.writes(B_reindex_shared_dyn_wmma_matrix_b[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
- A_1 = T.match_buffer(B_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int8", strides=("A_s0", "A_s1"), scope="shared.dyn", offset_factor=16)
- C = T.match_buffer(B_reindex_shared_dyn_wmma_matrix_b[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int8", strides=("C_s0", "C_s1"), scope="wmma.matrix_b", offset_factor=16)
+ A_1_s0, A_1_s1 = T.int32(), T.int32()
+ A_1 = T.match_buffer(B_reindex_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int8", strides=(A_1_s0, A_1_s1), scope="shared.dyn", offset_factor=16)
+ C_2_s0, C_2_s1 = T.int32(), T.int32()
+ C = T.match_buffer(B_reindex_shared_dyn_wmma_matrix_b[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int8", strides=(C_2_s0, C_2_s1), scope="wmma.matrix_b", offset_factor=16)
T.tvm_load_matrix_sync(C.data, 16, 16, 16, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, T.tvm_access_ptr(T.type_annotation("int8"), A_1.data, A_1.elem_offset, A_1.strides[0] * 16, 1), A_1.strides[0], "col_major")
for ax1_0_3, ax2_0_3 in T.grid(2, 2):
with T.sblock("matmul_o_update"):
@@ -676,9 +711,12 @@ def test_matmul_int8_tensorize_3d2d_dyn():
v3_i_o = T.axis.reduce(1, 0)
T.reads(matmul_1_reindex_pad_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], A_reindex_pad_shared_dyn_wmma_matrix_a[0, v1_o * 16:v1_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], B_reindex_shared_dyn_wmma_matrix_b[0, v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16])
T.writes(matmul_1_reindex_pad_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
- A_1 = T.match_buffer(A_reindex_pad_shared_dyn_wmma_matrix_a[0, v1_o * 16:v1_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "int8", strides=("A_s0", "A_s1"), scope="wmma.matrix_a", offset_factor=16)
- B_1 = T.match_buffer(B_reindex_shared_dyn_wmma_matrix_b[0, v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "int8", strides=("B_s0", "B_s1"), scope="wmma.matrix_b", offset_factor=16)
- C = T.match_buffer(matmul_1_reindex_pad_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int32", strides=("C_s0", "C_s1"), scope="wmma.accumulator", offset_factor=16)
+ A_2_s0, A_2_s1 = T.int32(), T.int32()
+ A_1 = T.match_buffer(A_reindex_pad_shared_dyn_wmma_matrix_a[0, v1_o * 16:v1_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "int8", strides=(A_2_s0, A_2_s1), scope="wmma.matrix_a", offset_factor=16)
+ B_s0, B_s1 = T.int32(), T.int32()
+ B_1 = T.match_buffer(B_reindex_shared_dyn_wmma_matrix_b[0, v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "int8", strides=(B_s0, B_s1), scope="wmma.matrix_b", offset_factor=16)
+ C_3_s0, C_3_s1 = T.int32(), T.int32()
+ C = T.match_buffer(matmul_1_reindex_pad_shared_dyn_wmma_accumulator[0, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int32", strides=(C_3_s0, C_3_s1), scope="wmma.accumulator", offset_factor=16)
T.tvm_mma_sync(C.data, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, A_1.data, A_1.elem_offset // A_1.strides[0] // 16 * (A_1.strides[0] // 16) + A_1.elem_offset % A_1.strides[0] // 16, B_1.data, B_1.elem_offset // B_1.strides[0] // 16 * (B_1.strides[0] // 16) + B_1.elem_offset % B_1.strides[0] // 16, C.data, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides [...]
for ax0_0, ax1_0 in T.grid(2, 2):
with T.sblock("matmul_1_reindex_pad_shared.dyn_wmma.accumulator_o"):
@@ -687,8 +725,10 @@ def test_matmul_int8_tensorize_3d2d_dyn():
v2_o = T.axis.spatial(256, ax1_0_1_ax2_0_1_fused * 8 + ax2_0_2_ax1_0_2_fused // 4 * 2 + ax1_0)
T.reads(matmul_1_reindex_pad_shared_dyn_wmma_accumulator[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
T.writes(matmul_1_reindex_pad_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16])
- A_1 = T.match_buffer(matmul_1_reindex_pad_shared_dyn_wmma_accumulator[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int32", strides=("A_s0", "A_s1"), scope="wmma.accumulator", offset_factor=16)
- C = T.match_buffer(matmul_1_reindex_pad_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int32", strides=("C_s0", "C_s1"), scope="shared.dyn", offset_factor=16)
+ A_3_s0, A_3_s1 = T.int32(), T.int32()
+ A_1 = T.match_buffer(matmul_1_reindex_pad_shared_dyn_wmma_accumulator[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int32", strides=(A_3_s0, A_3_s1), scope="wmma.accumulator", offset_factor=16)
+ C_4_s0, C_4_s1 = T.int32(), T.int32()
+ C = T.match_buffer(matmul_1_reindex_pad_shared_dyn[v0_o, v1_o * 16:v1_o * 16 + 16, v2_o * 16:v2_o * 16 + 16], (16, 16), "int32", strides=(C_4_s0, C_4_s1), scope="shared.dyn", offset_factor=16)
T.tvm_store_matrix_sync(A_1.data, 16, 16, 16, A_1.elem_offset // A_1.strides[0] // 16 * (A_1.strides[0] // 16) + A_1.elem_offset % A_1.strides[0] // 16, T.tvm_access_ptr(T.type_annotation("int32"), C.data, C.elem_offset, C.strides[0] * 16, 2), C.strides[0], "row_major")
for ax0_ax1_fused_0 in range(8):
for ax0_ax1_fused_1 in T.thread_binding(32, thread="threadIdx.x"):
@@ -754,7 +794,8 @@ def test_matmul_metal():
v2_o = T.axis.spatial(3584, ax2_0 * 8 + ax2_1 * 2 + ax2_2_init + ax2_3_init_0)
T.reads()
T.writes(C_reindex_pad_metal_simdgroup[0, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8])
- A_1 = T.match_buffer(C_reindex_pad_metal_simdgroup[0, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=("A_s0", "A_s1"), scope="metal.simdgroup", offset_factor=1)
+ A_s0, A_s1 = T.int32(), T.int32()
+ A_1 = T.match_buffer(C_reindex_pad_metal_simdgroup[0, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=(A_s0, A_s1), scope="metal.simdgroup", offset_factor=1)
T.metal.make_filled_simdgroup_matrix(A_1.data, A_1.elem_offset // A_1.strides[0] // 8 * (A_1.strides[0] // 8) + A_1.elem_offset % A_1.strides[0] // 8, T.float32(0), 8, 8)
for ax3_0 in range(128):
for ax0_1, ax1_ax2_fused_0 in T.grid(1, 1):
@@ -789,8 +830,10 @@ def test_matmul_metal():
v2_o = T.axis.spatial(512, ax3_0 * 4 + ax3_1 + ax1_0_1)
T.reads(A_reindex_pad_shared[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8])
T.writes(A_reindex_pad_shared_metal_simdgroup[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8])
- A_1 = T.match_buffer(A_reindex_pad_shared[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=("A_s0", "A_s1"), scope="shared", offset_factor=1)
- C_1 = T.match_buffer(A_reindex_pad_shared_metal_simdgroup[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=("C_s0", "C_s1"), scope="metal.simdgroup", offset_factor=1)
+ A_1_s0, A_1_s1 = T.int32(), T.int32()
+ A_1 = T.match_buffer(A_reindex_pad_shared[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=(A_1_s0, A_1_s1), scope="shared", offset_factor=1)
+ C_s0, C_s1 = T.int32(), T.int32()
+ C_1 = T.match_buffer(A_reindex_pad_shared_metal_simdgroup[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=(C_s0, C_s1), scope="metal.simdgroup", offset_factor=1)
T.metal.simdgroup_load(C_1.data, C_1.elem_offset // C_1.strides[0] // 8 * (C_1.strides[0] // 8) + C_1.elem_offset % C_1.strides[0] // 8, T.tvm_access_ptr(T.type_annotation("float16"), A_1.data, A_1.elem_offset, A_1.strides[0] * 8, 1), A_1.strides[0], 8, 8, T.bool(False))
for ax0_0, ax1_0_1 in T.grid(2, 1):
with T.sblock("B_reindex_shared_metal.simdgroup_o"):
@@ -799,8 +842,10 @@ def test_matmul_metal():
v2_o = T.axis.spatial(512, ax3_0 * 4 + ax3_1 + ax1_0_1)
T.reads(B_reindex_shared[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8])
T.writes(B_reindex_shared_metal_simdgroup[v0_o, v2_o * 8:v2_o * 8 + 8, v1_o * 8:v1_o * 8 + 8])
- A_1 = T.match_buffer(B_reindex_shared[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=("A_s0", "A_s1"), scope="shared", offset_factor=1)
- C_1 = T.match_buffer(B_reindex_shared_metal_simdgroup[v0_o, v2_o * 8:v2_o * 8 + 8, v1_o * 8:v1_o * 8 + 8], (8, 8), "float16", strides=("C_s0", "C_s1"), scope="metal.simdgroup", offset_factor=1)
+ A_2_s0, A_2_s1 = T.int32(), T.int32()
+ A_1 = T.match_buffer(B_reindex_shared[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=(A_2_s0, A_2_s1), scope="shared", offset_factor=1)
+ C_1_s0, C_1_s1 = T.int32(), T.int32()
+ C_1 = T.match_buffer(B_reindex_shared_metal_simdgroup[v0_o, v2_o * 8:v2_o * 8 + 8, v1_o * 8:v1_o * 8 + 8], (8, 8), "float16", strides=(C_1_s0, C_1_s1), scope="metal.simdgroup", offset_factor=1)
T.metal.simdgroup_load(C_1.data, C_1.elem_offset // C_1.strides[0] // 8 * (C_1.strides[0] // 8) + C_1.elem_offset % C_1.strides[0] // 8, T.tvm_access_ptr(T.type_annotation("float16"), A_1.data, A_1.elem_offset, A_1.strides[0] * 8, 1), A_1.strides[0], 8, 8, T.bool(True))
for ax1_2, ax2_2 in T.grid(2, 2):
with T.sblock("C_update_o"):
@@ -810,9 +855,12 @@ def test_matmul_metal():
v3_o = T.axis.reduce(512, ax3_0 * 4 + ax3_1)
T.reads(C_reindex_pad_metal_simdgroup[0, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], A_reindex_pad_shared_metal_simdgroup[0, v1_o * 8:v1_o * 8 + 8, v3_o * 8:v3_o * 8 + 8], B_reindex_shared_metal_simdgroup[0, v3_o * 8:v3_o * 8 + 8, v2_o * 8:v2_o * 8 + 8])
T.writes(C_reindex_pad_metal_simdgroup[0, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8])
- A_1 = T.match_buffer(A_reindex_pad_shared_metal_simdgroup[0, v1_o * 8:v1_o * 8 + 8, v3_o * 8:v3_o * 8 + 8], (8, 8), "float16", strides=("A_s0", "A_s1"), scope="metal.simdgroup", offset_factor=1)
- B_1 = T.match_buffer(B_reindex_shared_metal_simdgroup[0, v3_o * 8:v3_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=("B_s0", "B_s1"), scope="metal.simdgroup", offset_factor=1)
- C_1 = T.match_buffer(C_reindex_pad_metal_simdgroup[0, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=("C_s0", "C_s1"), scope="metal.simdgroup", offset_factor=1)
+ A_3_s0, A_3_s1 = T.int32(), T.int32()
+ A_1 = T.match_buffer(A_reindex_pad_shared_metal_simdgroup[0, v1_o * 8:v1_o * 8 + 8, v3_o * 8:v3_o * 8 + 8], (8, 8), "float16", strides=(A_3_s0, A_3_s1), scope="metal.simdgroup", offset_factor=1)
+ B_s0, B_s1 = T.int32(), T.int32()
+ B_1 = T.match_buffer(B_reindex_shared_metal_simdgroup[0, v3_o * 8:v3_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=(B_s0, B_s1), scope="metal.simdgroup", offset_factor=1)
+ C_2_s0, C_2_s1 = T.int32(), T.int32()
+ C_1 = T.match_buffer(C_reindex_pad_metal_simdgroup[0, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=(C_2_s0, C_2_s1), scope="metal.simdgroup", offset_factor=1)
T.metal.simdgroup_multiply_accumulate(C_1.data, C_1.elem_offset // C_1.strides[0] // 8 * (C_1.strides[0] // 8) + C_1.elem_offset % C_1.strides[0] // 8, A_1.data, A_1.elem_offset // A_1.strides[0] // 8 * (A_1.strides[0] // 8) + A_1.elem_offset % A_1.strides[0] // 8, B_1.data, B_1.elem_offset // B_1.strides[0] // 8 * (B_1.strides[0] // 8) + B_1.elem_offset % B_1.strides[0] // 8, C_1.data, C_1.elem_offset // C_1.strides[0] // 8 * (C_1.strides[0] / [...]
for ax0_1, ax1_0_1, ax2_0_1 in T.grid(1, 2, 2):
with T.sblock("C_reindex_pad_metal.simdgroup_o"):
@@ -821,8 +869,10 @@ def test_matmul_metal():
v2_o = T.axis.spatial(3584, ax2_0 * 8 + ax2_1 * 2 + ax2_0_1)
T.reads(C_reindex_pad_metal_simdgroup[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8])
T.writes(C_reindex_pad_shared[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8])
- A_1 = T.match_buffer(C_reindex_pad_metal_simdgroup[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=("A_s0", "A_s1"), scope="metal.simdgroup", offset_factor=1)
- C_1 = T.match_buffer(C_reindex_pad_shared[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=("C_s0", "C_s1"), scope="shared", offset_factor=1)
+ A_4_s0, A_4_s1 = T.int32(), T.int32()
+ A_1 = T.match_buffer(C_reindex_pad_metal_simdgroup[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=(A_4_s0, A_4_s1), scope="metal.simdgroup", offset_factor=1)
+ C_3_s0, C_3_s1 = T.int32(), T.int32()
+ C_1 = T.match_buffer(C_reindex_pad_shared[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=(C_3_s0, C_3_s1), scope="shared", offset_factor=1)
T.metal.simdgroup_store(A_1.data, A_1.elem_offset // A_1.strides[0] // 8 * (A_1.strides[0] // 8) + A_1.elem_offset % A_1.strides[0] // 8, T.tvm_access_ptr(T.type_annotation("float16"), C_1.data, C_1.elem_offset, C_1.strides[0] * 8, 2), C_1.strides[0], 8, 8, T.bool(False))
for ax0_1, ax1_ax2_fused_0 in T.grid(1, 2):
for ax1_ax2_fused_1 in T.thread_binding(4, thread="threadIdx.z"):
@@ -899,7 +949,8 @@ def test_matmul_metal_int4_quant():
v2_o = T.axis.spatial(3584, ax2_0 * 8 + ax2_1 * 2 + ax2_2_init + ax2_3_init_0)
T.reads()
T.writes(C_reindex_pad_metal_simdgroup[0, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8])
- A_1 = T.match_buffer(C_reindex_pad_metal_simdgroup[0, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=("A_s0", "A_s1"), scope="metal.simdgroup", offset_factor=1)
+ A_s0, A_s1 = T.int32(), T.int32()
+ A_1 = T.match_buffer(C_reindex_pad_metal_simdgroup[0, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=(A_s0, A_s1), scope="metal.simdgroup", offset_factor=1)
T.metal.make_filled_simdgroup_matrix(A_1.data, A_1.elem_offset // A_1.strides[0] // 8 * (A_1.strides[0] // 8) + A_1.elem_offset % A_1.strides[0] // 8, T.float32(0), 8, 8)
for ax3_0 in range(128):
for ax0_1, ax1_ax2_fused_0 in T.grid(1, 1):
@@ -934,8 +985,10 @@ def test_matmul_metal_int4_quant():
v2_o = T.axis.spatial(512, ax3_0 * 4 + ax3_1 + ax1_0_1)
T.reads(A_reindex_pad_shared[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8])
T.writes(A_reindex_pad_shared_metal_simdgroup[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8])
- A_1 = T.match_buffer(A_reindex_pad_shared[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=("A_s0", "A_s1"), scope="shared", offset_factor=1)
- C_1 = T.match_buffer(A_reindex_pad_shared_metal_simdgroup[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=("C_s0", "C_s1"), scope="metal.simdgroup", offset_factor=1)
+ A_1_s0, A_1_s1 = T.int32(), T.int32()
+ A_1 = T.match_buffer(A_reindex_pad_shared[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=(A_1_s0, A_1_s1), scope="shared", offset_factor=1)
+ C_s0, C_s1 = T.int32(), T.int32()
+ C_1 = T.match_buffer(A_reindex_pad_shared_metal_simdgroup[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=(C_s0, C_s1), scope="metal.simdgroup", offset_factor=1)
T.metal.simdgroup_load(C_1.data, C_1.elem_offset // C_1.strides[0] // 8 * (C_1.strides[0] // 8) + C_1.elem_offset % C_1.strides[0] // 8, T.tvm_access_ptr(T.type_annotation("float16"), A_1.data, A_1.elem_offset, A_1.strides[0] * 8, 1), A_1.strides[0], 8, 8, T.bool(False))
for ax0_0, ax1_0_1 in T.grid(2, 1):
with T.sblock("B_reindex_shared_metal.simdgroup_o"):
@@ -944,8 +997,10 @@ def test_matmul_metal_int4_quant():
v2_o = T.axis.spatial(512, ax3_0 * 4 + ax3_1 + ax1_0_1)
T.reads(B_reindex_shared[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8])
T.writes(B_reindex_shared_metal_simdgroup[v0_o, v2_o * 8:v2_o * 8 + 8, v1_o * 8:v1_o * 8 + 8])
- A_1 = T.match_buffer(B_reindex_shared[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=("A_s0", "A_s1"), scope="shared", offset_factor=1)
- C_1 = T.match_buffer(B_reindex_shared_metal_simdgroup[v0_o, v2_o * 8:v2_o * 8 + 8, v1_o * 8:v1_o * 8 + 8], (8, 8), "float16", strides=("C_s0", "C_s1"), scope="metal.simdgroup", offset_factor=1)
+ A_2_s0, A_2_s1 = T.int32(), T.int32()
+ A_1 = T.match_buffer(B_reindex_shared[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=(A_2_s0, A_2_s1), scope="shared", offset_factor=1)
+ C_1_s0, C_1_s1 = T.int32(), T.int32()
+ C_1 = T.match_buffer(B_reindex_shared_metal_simdgroup[v0_o, v2_o * 8:v2_o * 8 + 8, v1_o * 8:v1_o * 8 + 8], (8, 8), "float16", strides=(C_1_s0, C_1_s1), scope="metal.simdgroup", offset_factor=1)
T.metal.simdgroup_load(C_1.data, C_1.elem_offset // C_1.strides[0] // 8 * (C_1.strides[0] // 8) + C_1.elem_offset % C_1.strides[0] // 8, T.tvm_access_ptr(T.type_annotation("float16"), A_1.data, A_1.elem_offset, A_1.strides[0] * 8, 1), A_1.strides[0], 8, 8, T.bool(True))
for ax1_2, ax2_2 in T.grid(2, 2):
with T.sblock("NT_matmul_update_o"):
@@ -955,9 +1010,12 @@ def test_matmul_metal_int4_quant():
v3_o = T.axis.reduce(512, ax3_0 * 4 + ax3_1)
T.reads(C_reindex_pad_metal_simdgroup[0, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], A_reindex_pad_shared_metal_simdgroup[0, v1_o * 8:v1_o * 8 + 8, v3_o * 8:v3_o * 8 + 8], B_reindex_shared_metal_simdgroup[0, v3_o * 8:v3_o * 8 + 8, v2_o * 8:v2_o * 8 + 8])
T.writes(C_reindex_pad_metal_simdgroup[0, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8])
- A_1 = T.match_buffer(A_reindex_pad_shared_metal_simdgroup[0, v1_o * 8:v1_o * 8 + 8, v3_o * 8:v3_o * 8 + 8], (8, 8), "float16", strides=("A_s0", "A_s1"), scope="metal.simdgroup", offset_factor=1)
- B = T.match_buffer(B_reindex_shared_metal_simdgroup[0, v3_o * 8:v3_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=("B_s0", "B_s1"), scope="metal.simdgroup", offset_factor=1)
- C_1 = T.match_buffer(C_reindex_pad_metal_simdgroup[0, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=("C_s0", "C_s1"), scope="metal.simdgroup", offset_factor=1)
+ A_3_s0, A_3_s1 = T.int32(), T.int32()
+ A_1 = T.match_buffer(A_reindex_pad_shared_metal_simdgroup[0, v1_o * 8:v1_o * 8 + 8, v3_o * 8:v3_o * 8 + 8], (8, 8), "float16", strides=(A_3_s0, A_3_s1), scope="metal.simdgroup", offset_factor=1)
+ B_s0, B_s1 = T.int32(), T.int32()
+ B = T.match_buffer(B_reindex_shared_metal_simdgroup[0, v3_o * 8:v3_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=(B_s0, B_s1), scope="metal.simdgroup", offset_factor=1)
+ C_2_s0, C_2_s1 = T.int32(), T.int32()
+ C_1 = T.match_buffer(C_reindex_pad_metal_simdgroup[0, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=(C_2_s0, C_2_s1), scope="metal.simdgroup", offset_factor=1)
T.metal.simdgroup_multiply_accumulate(C_1.data, C_1.elem_offset // C_1.strides[0] // 8 * (C_1.strides[0] // 8) + C_1.elem_offset % C_1.strides[0] // 8, A_1.data, A_1.elem_offset // A_1.strides[0] // 8 * (A_1.strides[0] // 8) + A_1.elem_offset % A_1.strides[0] // 8, B.data, B.elem_offset // B.strides[0] // 8 * (B.strides[0] // 8) + B.elem_offset % B.strides[0] // 8, C_1.data, C_1.elem_offset // C_1.strides[0] // 8 * (C_1.strides[0] // 8) + C_1.e [...]
for ax0_1, ax1_0_1, ax2_0_1 in T.grid(1, 2, 2):
with T.sblock("C_reindex_pad_metal.simdgroup_o"):
@@ -966,8 +1024,10 @@ def test_matmul_metal_int4_quant():
v2_o = T.axis.spatial(3584, ax2_0 * 8 + ax2_1 * 2 + ax2_0_1)
T.reads(C_reindex_pad_metal_simdgroup[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8])
T.writes(C_reindex_pad_shared[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8])
- A_1 = T.match_buffer(C_reindex_pad_metal_simdgroup[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=("A_s0", "A_s1"), scope="metal.simdgroup", offset_factor=1)
- C_1 = T.match_buffer(C_reindex_pad_shared[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=("C_s0", "C_s1"), scope="shared", offset_factor=1)
+ A_4_s0, A_4_s1 = T.int32(), T.int32()
+ A_1 = T.match_buffer(C_reindex_pad_metal_simdgroup[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=(A_4_s0, A_4_s1), scope="metal.simdgroup", offset_factor=1)
+ C_3_s0, C_3_s1 = T.int32(), T.int32()
+ C_1 = T.match_buffer(C_reindex_pad_shared[v0_o, v1_o * 8:v1_o * 8 + 8, v2_o * 8:v2_o * 8 + 8], (8, 8), "float16", strides=(C_3_s0, C_3_s1), scope="shared", offset_factor=1)
T.metal.simdgroup_store(A_1.data, A_1.elem_offset // A_1.strides[0] // 8 * (A_1.strides[0] // 8) + A_1.elem_offset % A_1.strides[0] // 8, T.tvm_access_ptr(T.type_annotation("float16"), C_1.data, C_1.elem_offset, C_1.strides[0] * 8, 2), C_1.strides[0], 8, 8, T.bool(False))
for ax0_1, ax1_ax2_fused_0 in T.grid(1, 2):
for ax1_ax2_fused_1 in T.thread_binding(4, thread="threadIdx.z"):
diff --git a/tests/python/s_tir/meta_schedule/test_meta_schedule_trace_apply.py b/tests/python/s_tir/meta_schedule/test_meta_schedule_trace_apply.py
index 42ec6b8384..09695063e6 100644
--- a/tests/python/s_tir/meta_schedule/test_meta_schedule_trace_apply.py
+++ b/tests/python/s_tir/meta_schedule/test_meta_schedule_trace_apply.py
@@ -689,7 +689,7 @@ class Conv2dInt8_tensorcore_scheduled:
T.reads(pad_temp_reindex_shared[v0_o * 16:v0_o * 16 + 16, v1_o * 16:v1_o * 16 + 16])
T.writes(pad_temp_reindex_shared_wmma_matrix_a[v0_o * 16:v0_o * 16 + 16, v1_o * 16:v1_o * 16 + 16])
A = T.match_buffer(pad_temp_reindex_shared[v0_o * 16:v0_o * 16 + 16, v1_o * 16:v1_o * 16 + 16], (16, 16), "int8", strides=("A_s0", "A_s1"), scope="shared", offset_factor=16)
- C = T.match_buffer(pad_temp_reindex_shared_wmma_matrix_a[v0_o * 16:v0_o * 16 + 16, v1_o * 16:v1_o * 16 + 16], (16, 16), "int8", strides=("C_s0", "C_s1"), scope="wmma.matrix_a", offset_factor=16)
+ C = T.match_buffer(pad_temp_reindex_shared_wmma_matrix_a[v0_o * 16:v0_o * 16 + 16, v1_o * 16:v1_o * 16 + 16], (16, 16), "int8", strides=("C_1_s0", "C_1_s1"), scope="wmma.matrix_a", offset_factor=16)
T.tvm_load_matrix_sync(C.data, 16, 16, 16, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, T.tvm_access_ptr(T.type_annotation("int8"), A.data, A.elem_offset, A.strides[0] * 16, 1), A.strides[0], "row_major")
for ax0, ax1, ax2_0, ax3_0 in T.grid(1, 1, 1, 2):
with T.sblock("p1_reindex_shared_wmma.matrix_b_o"):
@@ -698,8 +698,8 @@ class Conv2dInt8_tensorcore_scheduled:
v3_o = T.axis.spatial(4, ax4_0_0 * 2 + ax3_0)
T.reads(p1_reindex_shared[v0_o, v1_o, v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16])
T.writes(p1_reindex_shared_wmma_matrix_b[v0_o, v1_o, v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16])
- A = T.match_buffer(p1_reindex_shared[v0_o, v1_o, v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "int8", strides=("A_s0", "A_s1"), scope="shared", offset_factor=16)
- C = T.match_buffer(p1_reindex_shared_wmma_matrix_b[v0_o, v1_o, v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "int8", strides=("C_s0", "C_s1"), scope="wmma.matrix_b", offset_factor=16)
+ A = T.match_buffer(p1_reindex_shared[v0_o, v1_o, v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "int8", strides=("A_1_s0", "A_1_s1"), scope="shared", offset_factor=16)
+ C = T.match_buffer(p1_reindex_shared_wmma_matrix_b[v0_o, v1_o, v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "int8", strides=("C_2_s0", "C_2_s1"), scope="wmma.matrix_b", offset_factor=16)
T.tvm_load_matrix_sync(C.data, 16, 16, 16, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, T.tvm_access_ptr(T.type_annotation("int8"), A.data, A.elem_offset, A.strides[0] * 16, 1), A.strides[0], "col_major")
for ax2_0_3, ax3_0_3, ax0_2, ax1_2, ax4_0_2, ax2_0_4, ax3_0_4 in T.grid(1, 1, 1, 1, 2, 1, 1):
with T.sblock("conv2d_nhwc_o_update"):
@@ -711,9 +711,9 @@ class Conv2dInt8_tensorcore_scheduled:
T.reads(conv2d_nhwc_reindex_shared_wmma_accumulator[v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], pad_temp_reindex_shared_wmma_matrix_a[v2_o * 16:v2_o * 16 + 16, v4_o * 16:v4_o * 16 + 16], p1_reindex_shared_wmma_matrix_b[v0_o, v1_o, v3_o * 16:v3_o * 16 + 16, v4_o * 16:v4_o * 16 + 16])
T.writes(conv2d_nhwc_reindex_shared_wmma_accumulator[v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16])
T.sblock_attr({"meta_schedule.thread_extent_high_inclusive": 1024, "meta_schedule.thread_extent_low_inclusive": 32, "warp_execution": 1})
- A = T.match_buffer(pad_temp_reindex_shared_wmma_matrix_a[v2_o * 16:v2_o * 16 + 16, v4_o * 16:v4_o * 16 + 16], (16, 16), "int8", strides=("A_s0", "A_s1"), scope="wmma.matrix_a", offset_factor=16)
+ A = T.match_buffer(pad_temp_reindex_shared_wmma_matrix_a[v2_o * 16:v2_o * 16 + 16, v4_o * 16:v4_o * 16 + 16], (16, 16), "int8", strides=("A_2_s0", "A_2_s1"), scope="wmma.matrix_a", offset_factor=16)
B = T.match_buffer(p1_reindex_shared_wmma_matrix_b[v0_o, v1_o, v3_o * 16:v3_o * 16 + 16, v4_o * 16:v4_o * 16 + 16], (16, 16), "int8", strides=("B_s0", "B_s1"), scope="wmma.matrix_b", offset_factor=16)
- C = T.match_buffer(conv2d_nhwc_reindex_shared_wmma_accumulator[v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "int32", strides=("C_s0", "C_s1"), scope="wmma.accumulator", offset_factor=16)
+ C = T.match_buffer(conv2d_nhwc_reindex_shared_wmma_accumulator[v2_o * 16:v2_o * 16 + 16, v3_o * 16:v3_o * 16 + 16], (16, 16), "int32", strides=("C_3_s0", "C_3_s1"), scope="wmma.accumulator", offset_factor=16)
T.tvm_mma_sync(C.data, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16, A.data, A.elem_offset // A.strides[0] // 16 * (A.strides[0] // 16) + A.elem_offset % A.strides[0] // 16, B.data, B.elem_offset // B.strides[0] // 16 * (B.strides[0] // 16) + B.elem_offset % B.strides[0] // 16, C.data, C.elem_offset // C.strides[0] // 16 * (C.strides[0] // 16) + C.elem_offset % C.strides[0] // 16)
for ax0_0, ax1_0 in T.grid(1, 1):
with T.sblock("conv2d_nhwc_reindex_shared_wmma.accumulator_o"):
@@ -721,8 +721,8 @@ class Conv2dInt8_tensorcore_scheduled:
v1_o = T.axis.spatial(16, ax2_0_0_ax3_0_0_fused % 8 * 2 + ax2_0_2_ax3_0_2_fused % 2 + ax1_0)
T.reads(conv2d_nhwc_reindex_shared_wmma_accumulator[v0_o * 16:v0_o * 16 + 16, v1_o * 16:v1_o * 16 + 16])
T.writes(conv2d_nhwc_reindex_shared[v0_o * 16:v0_o * 16 + 16, v1_o * 16:v1_o * 16 + 16])
- A = T.match_buffer(conv2d_nhwc_reindex_shared_wmma_accumulator[v0_o * 16:v0_o * 16 + 16, v1_o * 16:v1_o * 16 + 16], (16, 16), "int32", strides=("A_s0", "A_s1"), scope="wmma.accumulator", offset_factor=16)
- C = T.match_buffer(conv2d_nhwc_reindex_shared[v0_o * 16:v0_o * 16 + 16, v1_o * 16:v1_o * 16 + 16], (16, 16), "int32", strides=("C_s0", "C_s1"), scope="shared", offset_factor=16)
+ A = T.match_buffer(conv2d_nhwc_reindex_shared_wmma_accumulator[v0_o * 16:v0_o * 16 + 16, v1_o * 16:v1_o * 16 + 16], (16, 16), "int32", strides=("A_3_s0", "A_3_s1"), scope="wmma.accumulator", offset_factor=16)
+ C = T.match_buffer(conv2d_nhwc_reindex_shared[v0_o * 16:v0_o * 16 + 16, v1_o * 16:v1_o * 16 + 16], (16, 16), "int32", strides=("C_4_s0", "C_4_s1"), scope="shared", offset_factor=16)
T.tvm_store_matrix_sync(A.data, 16, 16, 16, A.elem_offset // A.strides[0] // 16 * (A.strides[0] // 16) + A.elem_offset % A.strides[0] // 16, T.tvm_access_ptr(T.type_annotation("int32"), C.data, C.elem_offset, C.strides[0] * 16, 2), C.strides[0], "row_major")
for ax0, ax1_0 in T.grid(128, 2):
for ax1_1 in T.thread_binding(16, thread="threadIdx.x"):
diff --git a/tests/python/te/test_te_create_primfunc.py b/tests/python/te/test_te_create_primfunc.py
index ee1bc60498..6b92d02272 100644
--- a/tests/python/te/test_te_create_primfunc.py
+++ b/tests/python/te/test_te_create_primfunc.py
@@ -242,9 +242,9 @@ def te_extern():
@T.prim_func(s_tir=True)
def tir_extern(a: T.handle, b: T.handle, c: T.handle) -> None:
T.func_attr({"global_symbol": "main", "tirx.noalias": True})
- off1 = te.var("elem_offset")
- off2 = te.var("elem_offset_1")
- off3 = te.var("elem_offset_2")
+ off1 = T.int32()
+ off2 = T.int32()
+ off3 = T.int32()
A = T.match_buffer(a, (128, 128), elem_offset=off1)
B = T.match_buffer(b, (128, 128), elem_offset=off2)
C = T.match_buffer(c, (128, 128), elem_offset=off3)
diff --git a/tests/python/tirx-transform/test_tir_transform_vectorize.py b/tests/python/tirx-transform/test_tir_transform_vectorize.py
index 379bbfd4b2..73c62b8f8b 100644
--- a/tests/python/tirx-transform/test_tir_transform_vectorize.py
+++ b/tests/python/tirx-transform/test_tir_transform_vectorize.py
@@ -502,7 +502,7 @@ def test_illegal_extent():
class Mod:
@T.prim_func(s_tir=True)
def main(A: T.Buffer((25,), "int32")):
- n = T.Var("n", ty="int32")
+ n = T.int32()
for j in T.vectorized(n):
A[j] = 3
diff --git a/tests/python/tirx/operator/tile_primitive/trn/test_binary_trn.py b/tests/python/tirx/operator/tile_primitive/trn/test_binary_trn.py
index b2b27bafbf..4ddcd5f6bc 100644
--- a/tests/python/tirx/operator/tile_primitive/trn/test_binary_trn.py
+++ b/tests/python/tirx/operator/tile_primitive/trn/test_binary_trn.py
@@ -79,11 +79,13 @@ def test_simple_binary(op_type, operands_type):
A_sbuf = T.alloc_buffer(src1_shape, "float32", scope="trn.sbuf", layout=src1_layout)
B_sbuf = T.alloc_buffer(src2_shape, "float32", scope="trn.sbuf", layout=src2_layout)
C_sbuf = T.alloc_buffer(dst_shape, "float32", scope="trn.sbuf", layout=dst_layout)
- if operands_type == "region_region" or operands_type.startswith("region_broadcast"):
+ if T.constexpr(
+ operands_type == "region_region" or operands_type.startswith("region_broadcast")
+ ):
Tx_func(C_sbuf, A_sbuf, B_sbuf)
- elif operands_type == "const_region":
+ elif T.constexpr(operands_type == "const_region"):
Tx_func(C_sbuf, const, A_sbuf)
- elif operands_type == "region_const":
+ elif T.constexpr(operands_type == "region_const"):
Tx_func(C_sbuf, A_sbuf, const)
@T.prim_func
@@ -96,15 +98,15 @@ def test_simple_binary(op_type, operands_type):
T.attr(0, "tensorized_nki_instruction", 1)
for p_loop in T.serial(0, 128, annotations={"nki_dim":"P"}):
for f_loop in T.serial(0, 512, annotations={"nki_dim":"F"}):
- if operands_type == "region_region":
+ if T.constexpr(operands_type == "region_region"):
T.nki.tensortensor(C_sbuf[p_loop, f_loop], A_sbuf[p_loop, f_loop], B_sbuf[p_loop, f_loop], op_type) # noqa: E501
- elif operands_type == "region_const":
+ elif T.constexpr(operands_type == "region_const"):
T.nki.tensorscalar(C_sbuf[p_loop, f_loop], A_sbuf[p_loop, f_loop], T.float32(3.0), op_type, T.bool(False)) # noqa: E501
- elif operands_type == "const_region":
+ elif T.constexpr(operands_type == "const_region"):
T.nki.tensorscalar(C_sbuf[p_loop, f_loop], A_sbuf[p_loop, f_loop], T.float32(3.0), op_type, T.bool(True)) # noqa: E501
- elif operands_type == "region_broadcast_rhs":
+ elif T.constexpr(operands_type == "region_broadcast_rhs"):
T.nki.tensorscalar(C_sbuf[p_loop, f_loop], A_sbuf[p_loop, f_loop], B_sbuf[p_loop, 0], op_type, T.bool(False)) # noqa: E501
- elif operands_type == "region_broadcast_lhs":
+ elif T.constexpr(operands_type == "region_broadcast_lhs"):
T.nki.tensorscalar(C_sbuf[p_loop, f_loop], B_sbuf[p_loop, f_loop], A_sbuf[p_loop, 0], op_type, T.bool(True)) # noqa: E501
# fmt: on
with target:
@@ -156,15 +158,15 @@ def test_binary_complex(op_type, operands_type):
B_sbuf_view = B_sbuf.view(*src2_view_shape)
C_sbuf_view = C_sbuf.view(*dst_view_shape)
for i in range(4):
- if operands_type == "region_region":
+ if T.constexpr(operands_type == "region_region"):
Tx_func(C_sbuf_view[:, i, :], A_sbuf_view[:, i * 2, :], B_sbuf_view[:, i, :])
- elif operands_type == "region_const":
+ elif T.constexpr(operands_type == "region_const"):
Tx_func(C_sbuf_view[:, i, :], A_sbuf_view[:, i * 2, :], const)
- elif operands_type == "const_region":
+ elif T.constexpr(operands_type == "const_region"):
Tx_func(C_sbuf_view[:, i, :], const, A_sbuf_view[:, i * 2, :])
- elif operands_type == "region_broadcast_rhs":
+ elif T.constexpr(operands_type == "region_broadcast_rhs"):
Tx_func(C_sbuf_view[:, i, :], A_sbuf_view[:, i * 2, :], B_sbuf_view[:, 0, :])
- elif operands_type == "region_broadcast_lhs":
+ elif T.constexpr(operands_type == "region_broadcast_lhs"):
Tx_func(C_sbuf_view[:, i, :, :], A_sbuf_view[:, i*2,:, :], B_sbuf_view[:, i, :, :])
f_extent = 128 if operands_type == "region_broadcast_lhs" else 512
@@ -183,15 +185,15 @@ def test_binary_complex(op_type, operands_type):
T.attr(0, "tensorized_nki_instruction", 1)
for p_loop in T.serial(0, 128, annotations={"nki_dim":"P"}):
for f_loop in T.serial(0, f_extent, annotations={"nki_dim":"F"}):
- if operands_type == "region_region":
+ if T.constexpr(operands_type == "region_region"):
T.nki.tensortensor(C_sbuf_view[p_loop, i * 512 + f_loop], A_sbuf_view[p_loop, i * 1024 + f_loop], B_sbuf_view[p_loop, i * 512 + f_loop], op_type) # noqa: E501
- elif operands_type == "const_region":
+ elif T.constexpr(operands_type == "const_region"):
T.nki.tensorscalar(C_sbuf_view[p_loop, i * 512 + f_loop], A_sbuf_view[p_loop, i * 1024 + f_loop], T.float32(3.0), op_type, T.bool(True)) # noqa: E501
- elif operands_type == "region_const":
+ elif T.constexpr(operands_type == "region_const"):
T.nki.tensorscalar(C_sbuf_view[p_loop, i * 512 + f_loop], A_sbuf_view[p_loop, i * 1024 + f_loop], T.float32(3.0), op_type, T.bool(False)) # noqa: E501
- elif operands_type == "region_broadcast_lhs":
+ elif T.constexpr(operands_type == "region_broadcast_lhs"):
T.nki.tensorscalar(C_sbuf_view[p_loop, i * 512 + b_loop * 128 + f_loop], B_sbuf_view[p_loop, i * 512 + b_loop * 128 + f_loop], A_sbuf_view[p_loop, i * 8 + b_loop], op_type, T.bool(True)) # noqa: E501
- elif operands_type == "region_broadcast_rhs":
+ elif T.constexpr(operands_type == "region_broadcast_rhs"):
T.nki.tensortensor(C_sbuf_view[p_loop, i * 512 + f_loop], A_sbuf_view[p_loop, i * 1024 + f_loop], B_sbuf_view[p_loop, f_loop], op_type) # noqa: E501
# fmt: on
diff --git a/tests/python/tirx/operator/tile_primitive/trn/test_unary_trn.py b/tests/python/tirx/operator/tile_primitive/trn/test_unary_trn.py
index 077200fbe3..0773ea8132 100644
--- a/tests/python/tirx/operator/tile_primitive/trn/test_unary_trn.py
+++ b/tests/python/tirx/operator/tile_primitive/trn/test_unary_trn.py
@@ -65,7 +65,7 @@ def test_simple_unary(op_type):
T.device_entry()
A_sbuf = T.alloc_buffer(src_shape, "float32", scope="trn.sbuf", layout=src_layout)
B_sbuf = T.alloc_buffer(dst_shape, "float32", scope="trn.sbuf", layout=dst_layout)
- if op_type == "memset":
+ if T.constexpr(op_type == "memset"):
tx_func(B_sbuf, T.float32(0.0))
else:
tx_func(B_sbuf, A_sbuf)
@@ -79,11 +79,11 @@ def test_simple_unary(op_type):
T.attr(0, "tensorized_nki_instruction", 1)
for p_loop in T.serial(0, 128, annotations={"nki_dim":"P"}):
for f_loop in T.serial(0, 512, annotations={"nki_dim":"F"}):
- if op_type == "reciprocal":
+ if T.constexpr(op_type == "reciprocal"):
T.nki.reciprocal(
B_sbuf[p_loop, f_loop], A_sbuf[p_loop, f_loop]
)
- elif op_type == "memset":
+ elif T.constexpr(op_type == "memset"):
T.nki.memset(B_sbuf[p_loop, f_loop], 0.0)
# fmt: on
with target:
@@ -110,7 +110,7 @@ def test_unary_in_a_loop(op_type):
A_sbuf_view = A_sbuf.view(128, 8, 512)
B_sbuf_view = B_sbuf.view(128, 4, 512)
for i in range(4):
- if op_type == "memset":
+ if T.constexpr(op_type == "memset"):
Tx_func(B_sbuf_view[:, i, :], T.float32(0.0))
else:
Tx_func(B_sbuf_view[:, i, :], A_sbuf_view[:, i * 2, :])
@@ -126,9 +126,9 @@ def test_unary_in_a_loop(op_type):
T.attr(0, "tensorized_nki_instruction", 1)
for p_loop in T.serial(0, 128, annotations={"nki_dim":"P"}):
for f_loop in T.serial(0, 512, annotations={"nki_dim":"F"}):
- if op_type == "reciprocal":
+ if T.constexpr(op_type == "reciprocal"):
T.nki.reciprocal(B_sbuf_view[p_loop, i * 512 + f_loop], A_sbuf_view[p_loop, i * 1024 + f_loop]) # noqa: E501
- elif op_type == "memset":
+ elif T.constexpr(op_type == "memset"):
T.nki.memset(B_sbuf_view[p_loop, i * 512 + f_loop], 0.0)
# fmt: on
with target:
diff --git a/tests/python/tirx/test_inline.py b/tests/python/tirx/test_inline.py
index 438c187c6c..4e3c8f8aa2 100644
--- a/tests/python/tirx/test_inline.py
+++ b/tests/python/tirx/test_inline.py
@@ -207,7 +207,7 @@ def test_recursive_inline():
@T.inline
def add(x, c):
- if c > 0:
+ if T.constexpr(c > 0):
add(x, c - 1)
T.evaluate(x)
diff --git a/tests/python/tirx/test_jit.py b/tests/python/tirx/test_jit.py
index ca9a91846f..00af56df61 100644
--- a/tests/python/tirx/test_jit.py
+++ b/tests/python/tirx/test_jit.py
@@ -254,7 +254,7 @@ def test_optional_param_present_and_absent_ir():
@T.jit(private=True)
def kernel(a: T.Optional(T.handle), out_h: T.handle):
out = T.match_buffer(out_h, (1,), "int32")
- if a is not None:
+ if T.constexpr(a is not None):
A = T.match_buffer(a, (1,), "int32")
out[0] = A[0]
else:
@@ -285,7 +285,7 @@ def test_optional_specialization_cache_includes_presence():
@T.jit(private=True)
def kernel(a: T.Optional(T.handle), out_h: T.handle):
out = T.match_buffer(out_h, (1,), "int32")
- if a is not None:
+ if T.constexpr(a is not None):
A = T.match_buffer(a, (1,), "int32")
out[0] = A[0]
else:
@@ -310,10 +310,10 @@ def test_multiple_optional_params_preserve_runtime_order():
first = T.match_buffer(first_h, (1,), "int32")
out = T.match_buffer(out_h, (1,), "int32")
out[0] = first[0] * scale
- if a is not None:
+ if T.constexpr(a is not None):
A = T.match_buffer(a, (1,), "int32")
out[0] = out[0] + A[0]
- if b is not None:
+ if T.constexpr(b is not None):
B = T.match_buffer(b, (1,), "int32")
out[0] = out[0] + B[0]
@@ -340,7 +340,7 @@ def test_multiple_optional_params_preserve_runtime_order():
def test_optional_only_accepts_none_at_specialization_time():
@T.jit(private=True)
def kernel(a: T.Optional(T.handle), out_h: T.handle):
- if a is not None:
+ if T.constexpr(a is not None):
T.match_buffer(a, (1,), "int32")
T.match_buffer(out_h, (1,), "int32")
@@ -374,7 +374,7 @@ def test_t_optional_is_restricted_to_jit():
def test_compile_time_if_binding_uses_python_scope():
@T.jit(private=True)
def kernel(a: T.Optional(T.handle), out_h: T.handle):
- if a is None:
+ if T.constexpr(a is None):
selected = T.match_buffer(out_h, (1,), "int32")
else:
selected = T.match_buffer(a, (1,), "int32")
@@ -395,11 +395,11 @@ def test_compile_time_bool_ops_and_if_expression_short_circuit():
@T.jit(private=True)
def kernel(a: T.Optional(T.handle), out_h: T.handle):
out = T.match_buffer(out_h, (1,), "int32")
- if a is None or fail_if_evaluated():
+ if T.constexpr(a is None or fail_if_evaluated()):
out[0] = 1
- if a is not None and fail_if_evaluated():
+ if T.constexpr(a is not None and fail_if_evaluated()):
out[0] = 2
- out[0] = 3 if a is None else fail_if_evaluated()
+ out[0] = 3 if T.constexpr(a is None) else fail_if_evaluated()
absent = kernel.specialize(a=None)
assert [param.name for param in absent.params] == ["out"]
@@ -426,9 +426,9 @@ def test_runtime_tir_if_cannot_guard_absent_optional_param():
def test_unguarded_absent_optional_param_reports_source(operation, source_text):
@T.jit(private=True)
def kernel(a: T.Optional(T.handle)):
- if operation == "subscript":
+ if T.constexpr(operation == "subscript"):
a[10]
- elif operation == "attribute":
+ elif T.constexpr(operation == "attribute"):
a.ptr_to([0])
else:
T.match_buffer(a, (1,), "int32")
diff --git a/tests/python/tirx/test_op_namespace_cleanup.py b/tests/python/tirx/test_op_namespace_cleanup.py
index ba60b7f74c..15fd01448c 100644
--- a/tests/python/tirx/test_op_namespace_cleanup.py
+++ b/tests/python/tirx/test_op_namespace_cleanup.py
@@ -229,7 +229,8 @@ def test_backend_specific_wrappers_are_not_root_exports():
def test_backend_load_updates_tirx_alias_and_script_facades(monkeypatch):
- from tvm.tirx.script import builder, parser
+ from tvm.script.parser import tirx as parser
+ from tvm.tirx.script import builder
from tvm.tirx.script.builder import ir as builder_ir
backend_name = "unit_test_backend"
diff --git a/tests/python/tirx/test_parser_printer.py b/tests/python/tirx/test_parser_printer.py
index 01d5a1b12d..793e1d40c9 100644
--- a/tests/python/tirx/test_parser_printer.py
+++ b/tests/python/tirx/test_parser_printer.py
@@ -852,7 +852,7 @@ def test_macro_recursive():
@T.inline
def add(x, c):
- if c > 0:
+ if T.constexpr(c > 0):
add(x, c - 1)
T.evaluate(x)
diff --git a/tests/python/tvmscript/test_tvmscript_error_report.py b/tests/python/tvmscript/test_tvmscript_error_report.py
index a61350c1af..1d2ef09ba3 100644
--- a/tests/python/tvmscript/test_tvmscript_error_report.py
+++ b/tests/python/tvmscript/test_tvmscript_error_report.py
@@ -619,7 +619,7 @@ def test_format_source_snippet_multi_line():
"""Unit-level check that _format_source_snippet renders every line in a
multi-line span, with the underline covering start-col..EOL on the first
line, full interior lines, and col-1..end-col on the last line."""
- from tvm.script.parser.core.diagnostics import _format_source_snippet
+ from tvm.script.parser.diagnostics import _format_source_snippet
source_lines = [
"first ignored line\n",
@@ -647,7 +647,7 @@ def test_format_source_snippet_multi_line():
def test_format_source_snippet_single_line_unchanged():
"""A single-line span (end_lineno == lineno) underlines only the
[col_offset, end_col_offset) columns on that one line."""
- from tvm.script.parser.core.diagnostics import _format_source_snippet
+ from tvm.script.parser.diagnostics import _format_source_snippet
source_lines = ["ignored\n", " abc + def\n", "ignored\n"]
# Underline just 'abc' (cols 5..8 exclusive) on line 2.
diff --git a/tests/python/tvmscript/test_tvmscript_parser_evaluator.py b/tests/python/tvmscript/test_tvmscript_parser_evaluator.py
index 463b0f6d29..1c02b1b06c 100644
--- a/tests/python/tvmscript/test_tvmscript_parser_evaluator.py
+++ b/tests/python/tvmscript/test_tvmscript_parser_evaluator.py
@@ -20,19 +20,17 @@
import pytest
import tvm.testing
-from tvm.script.parser.core.diagnostics import Source
-from tvm.script.parser.core.evaluator import ExprEvaluator
+from tvm.script.parser.frontend import Compiler
+from tvm.tirx.script import builder as T
def _calc(expr, extra_vars=None):
if extra_vars is None:
extra_vars = {}
- source = Source(expr)
- mod_ast = source.as_ast()
- mod_body_ast = mod_ast.body
- expr_stmt_ast = mod_body_ast[0]
- expr_ast = expr_stmt_ast.value
- return ExprEvaluator.eval(None, extra_vars, expr_ast)
+ compiler = Compiler("def evaluate():\n return " + expr + "\n", extra_vars)
+ return compiler.run_statements(
+ compiler.tree.body[0].body, T, compiler.env, set(), preserve_return=True
+ )
def test_evaluator_basic():
diff --git a/tests/python/tvmscript/test_tvmscript_parser_source.py b/tests/python/tvmscript/test_tvmscript_parser_source.py
index 717e7bc5bf..6ab4dc552b 100644
--- a/tests/python/tvmscript/test_tvmscript_parser_source.py
+++ b/tests/python/tvmscript/test_tvmscript_parser_source.py
@@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.
# ruff: noqa: F401
-"""Unittests for tvm.script.parser.core"""
+"""Source and span tests for the canonical parser"""
import inspect
@@ -27,8 +27,8 @@ import tvm
import tvm.testing
from tvm.ir import Call, SequentialSpan, TensorLoad, assert_structural_equal
from tvm.script import tirx as T
-from tvm.script.parser.core import doc_core as doc
-from tvm.script.parser.core.diagnostics import Source
+import ast as doc
+from tvm.script.parser.source import Source
from tvm.script.tirx import tile as Tx
from tvm.tirx.stmt import TilePrimitiveCall
diff --git a/tests/python/tvmscript/test_tvmscript_parser_tir.py b/tests/python/tvmscript/test_tvmscript_parser_tir.py
index a4baa9b456..e7ba984464 100644
--- a/tests/python/tvmscript/test_tvmscript_parser_tir.py
+++ b/tests/python/tvmscript/test_tvmscript_parser_tir.py
@@ -118,16 +118,19 @@ def main(A: T.Buffer(("n",), "float32"), n: T.int64):
assert str(n.ty.dtype) == "int64"
-def test_tir_string_defined_symbol_does_not_take_dtype_from_body():
- with pytest.raises(tvm.error.DiagnosticError):
- tvm.script.from_source(
- """
+def test_tir_string_defined_symbol_uses_prescanned_body_dtype():
+ func = tvm.script.from_source(
+ """
@T.prim_func
def main(A: T.Buffer(("n",), "float32")):
n = T.int64()
T.evaluate(n)
"""
- )
+ )
+
+ n = func.params[0].ty.shape[0]
+ assert str(n.ty.dtype) == "int64"
+ assert func.body.value.same_as(n)
def test_tir_direct_use_before_string_definition_is_undefined():
@@ -690,7 +693,7 @@ def test_deterministic_branch():
def create_func(predicate: bool):
@T.prim_func(private=True, s_tir=True)
def func() -> None:
- if predicate:
+ if T.constexpr(predicate):
T.evaluate(0)
else:
T.evaluate(1)
diff --git a/tests/python/tvmscript/test_tvmscript_roundtrip.py b/tests/python/tvmscript/test_tvmscript_roundtrip.py
index 3466246b69..9c2051b84f 100644
--- a/tests/python/tvmscript/test_tvmscript_roundtrip.py
+++ b/tests/python/tvmscript/test_tvmscript_roundtrip.py
@@ -3163,7 +3163,7 @@ def relax_extern_func():
def relax_match_cast_ty_proxy():
- """TypeProxy subclasses may be used as expressions
+ """Default type constructors may be used as expressions
This is a regression test. The TVMScript parser allows Type
to be specified using a default-constructible class
@@ -3188,16 +3188,9 @@ def relax_match_cast_ty_proxy():
inner.__name__ = subclass.__name__
return inner
- # Not all subclasses of TypeProxy are default-constructible.
- # This list is a subset of `TypeProxy.__subclasses__()`,
- # excluding `PrimProxy` and `DTensorProxy`.
- subclasses = [
- tvm.script.parser.relax.entry.AnyProxy,
- tvm.script.parser.relax.entry.TensorProxy,
- tvm.script.parser.relax.entry.CallableProxy,
- tvm.script.parser.relax.entry.TupleProxy,
- tvm.script.parser.relax.entry.ShapeProxy,
- ]
+ # Prim and DTensor require arguments; the remaining public type
+ # constructors also work as bare values in match_cast expressions.
+ subclasses = [R.Any, R.Tensor, R.Callable, R.Tuple, R.Shape]
for subclass in subclasses:
yield make_ir_generator(subclass)
(tvm) 01/04: [D][TVMScript] Establish canonical construction and definition contexts
Posted by tq...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a commit to branch script/canonical-parser-df
in repository https://gitbox.apache.org/repos/asf/tvm.git
commit e883105962def2f954b5b1a4229e8acd7764cdcf
Author: Tianqi Chen <tq...@users.noreply.github.com>
AuthorDate: Tue Sep 22 22:02:04 2026 +0000
[D][TVMScript] Establish canonical construction and definition contexts
Use one syntax-driven frontend with definition-site annotation context, dialect binding and emission protocols, direct IR comparisons, and explicit symbolic dtype rules. Preserve public construction APIs and documentation.
---
docs/arch/tvmscript.rst | 316 ++--
include/tvm/relax/script/builder/frame.h | 18 +-
include/tvm/relax/script/builder/ir.h | 18 +
include/tvm/script/ir_builder/base.h | 44 +-
include/tvm/script/ir_builder/ir/ir.h | 3 +
include/tvm/tirx/script/builder/frame.h | 10 +-
include/tvm/tirx/script/builder/ir.h | 6 +-
python/tvm/relax/script/__init__.py | 22 +-
python/tvm/relax/script/builder/__init__.py | 508 ++++-
python/tvm/relax/script/builder/comparison.py | 61 +
python/tvm/relax/script/builder/distributed/ir.py | 32 +-
python/tvm/relax/script/builder/ir.py | 65 +-
python/tvm/relax/script/builder/protocol.py | 127 ++
python/tvm/relax/script/parser/__init__.py | 52 -
python/tvm/relax/script/parser/dist.py | 106 --
python/tvm/relax/script/parser/entry.py | 521 ------
python/tvm/relax/script/parser/parser.py | 506 -----
python/tvm/script/__init__.py | 82 +-
python/tvm/script/ir_builder/__init__.py | 17 +-
python/tvm/script/ir_builder/base.py | 138 +-
python/tvm/script/ir_builder/construction.py | 332 ++++
python/tvm/script/ir_builder/ir/__init__.py | 5 +
python/tvm/script/ir_builder/ir/ir.py | 118 +-
python/tvm/script/ir_builder/parser_support.py | 79 +
python/tvm/script/ir_builder/type_var_frame.py | 147 ++
python/tvm/script/parser/__init__.py | 100 +-
python/tvm/script/parser/_core.py | 24 -
python/tvm/script/parser/core/diagnostics.py | 324 ----
python/tvm/script/parser/core/dispatch.py | 157 --
python/tvm/script/parser/core/doc.py | 320 ----
python/tvm/script/parser/core/doc_core.py | 1123 -----------
python/tvm/script/parser/core/entry.py | 223 ---
python/tvm/script/parser/core/error.py | 27 -
python/tvm/script/parser/core/evaluator.py | 626 -------
python/tvm/script/parser/core/parser.py | 1071 -----------
python/tvm/script/parser/core/utils.py | 212 ---
python/tvm/script/parser/diagnostics.py | 157 ++
python/tvm/script/parser/expression.py | 283 +++
python/tvm/script/parser/frontend.py | 979 ++++++++++
.../tvm/script/parser/{core/__init__.py => ir.py} | 7 +-
python/tvm/script/parser/ir/__init__.py | 39 -
python/tvm/script/parser/ir/entry.py | 163 --
python/tvm/script/parser/ir/parser.py | 212 ---
python/tvm/script/parser/jit.py | 220 +++
python/tvm/script/parser/protocol.py | 409 ++++
python/tvm/script/parser/source.py | 121 ++
python/tvm/script/parser/transpile.py | 1954 ++++++++++++++++++++
python/tvm/tirx/script/__init__.py | 32 +-
python/tvm/tirx/script/builder/__init__.py | 774 +++++++-
python/tvm/tirx/script/builder/comparison.py | 105 ++
python/tvm/tirx/script/builder/frame.py | 2 +
python/tvm/tirx/script/builder/ir.py | 339 ++--
python/tvm/tirx/script/builder/protocol.py | 248 +++
python/tvm/tirx/script/builder/tirx.py | 78 +-
python/tvm/tirx/script/parser/__init__.py | 50 -
python/tvm/tirx/script/parser/entry.py | 581 ------
python/tvm/tirx/script/parser/operation.py | 167 --
python/tvm/tirx/script/parser/parser.py | 1217 ------------
src/relax/script/builder/frame.cc | 69 +-
src/relax/script/builder/ir.cc | 56 +-
src/relax/script/builder/utils.h | 13 +-
src/script/ir_builder/base.cc | 80 +-
src/script/ir_builder/ir/ir.cc | 20 +-
src/tirx/script/builder/frame.cc | 32 +-
src/tirx/script/builder/ir.cc | 13 +-
src/tirx/script/builder/utils.h | 4 +-
tests/script/test_parser_args_policy.py | 255 +++
tests/script/test_parser_backend_namespaces.py | 165 ++
tests/script/test_parser_buffer_views.py | 213 +++
tests/script/test_parser_call_scopes.py | 136 ++
tests/script/test_parser_captured_constructors.py | 58 +
.../script/test_parser_comparison_source_order.py | 298 +++
tests/script/test_parser_comparison_values.py | 203 ++
tests/script/test_parser_constexpr.py | 231 +++
.../script/test_parser_construction_regressions.py | 140 ++
tests/script/test_parser_definition_context.py | 391 ++++
tests/script/test_parser_distributed_aliases.py | 67 +
tests/script/test_parser_dtype_migrations.py | 145 ++
tests/script/test_parser_emission_contract.py | 112 ++
tests/script/test_parser_emitted_spans.py | 156 ++
tests/script/test_parser_frontend_regressions.py | 303 +++
tests/script/test_parser_helper_migrations.py | 200 ++
tests/script/test_parser_ir_namespace.py | 88 +
tests/script/test_parser_jit_specialization.py | 301 +++
tests/script/test_parser_local_function_types.py | 175 ++
tests/script/test_parser_nonlocal.py | 57 +
tests/script/test_parser_range_annotations.py | 102 +
tests/script/test_parser_scope_id_identity.py | 230 +++
tests/script/test_parser_scope_id_names.py | 113 ++
tests/script/test_parser_scoped_symbols.py | 125 ++
tests/script/test_parser_self_bind.py | 119 ++
tests/script/test_parser_support.py | 104 ++
tests/script/test_parser_tile_namespaces.py | 156 ++
93 files changed, 12177 insertions(+), 8430 deletions(-)
diff --git a/docs/arch/tvmscript.rst b/docs/arch/tvmscript.rst
index 0dc8fc63cb..d8e6d10429 100644
--- a/docs/arch/tvmscript.rst
+++ b/docs/arch/tvmscript.rst
@@ -22,22 +22,22 @@ TVMScript
TVMScript is a Python-based domain-specific language (DSL) for writing TVM IR. It lets users
define ``IRModule``\ s — containing both Relax functions and TIR ``PrimFunc``\ s — using
-familiar Python syntax. Although TVMScript *looks* like Python, it is **not executed by the
-Python interpreter**. Instead, Python decorators extract the AST from the source code and
-transform it into TVM IR through a dedicated parser and IR builder pipeline.
+familiar Python syntax. Decorators acquire the source and its definition context. A syntax
+transpiler translates the Python AST into a Python builder program; executing that program
+constructs TVM IR. The scripted function body is therefore translated before execution.
TVMScript serves two roles in the TVM stack:
- **Authoring**: users write TIR kernels and Relax programs directly in TVMScript.
-- **Roundtrip**: every ``IRModule`` can be printed back to TVMScript via ``mod.script()`` and
- re-parsed to produce an equivalent module. This makes TVMScript the primary tool for
- inspecting, debugging, and serializing IR.
+- **Roundtrip**: ``mod.script()`` prints IR as TVMScript, which can be re-parsed with any
+ required external symbols to reconstruct equivalent IR. This supports inspection,
+ debugging, and serialization of printable IR.
Overview
--------
-The TVMScript system has three components:
+TVMScript separates source acquisition, syntax translation, IR construction, and printing:
.. code-block:: text
@@ -45,13 +45,17 @@ The TVMScript system has three components:
Python source (TVMScript)
│
- ▼ ast.parse + convert
+ ▼ Frontend: source + definition context
│
- Doc AST (mirror of Python AST)
+ Python AST
│
- ▼ Parser (dispatch by token: ir / tirx / relax)
+ ▼ Syntax transpiler + construction protocol
│
- ▼ IR Builder (frame stack)
+ Generated Python builder program
+ │
+ ▼ Frontend: recompose and execute callable
+ │
+ ▼ IR builders (frame stack + dialect policy)
│
TVM IR (IRModule, PrimFunc, relax.Function)
@@ -68,14 +72,17 @@ The TVMScript system has three components:
│
TVMScript text
-- **Parser** (Python): reads Python source, converts it to a ``Doc AST`` (a mirror of
- Python's ``ast`` module), then walks the tree using dialect-specific handlers that call
- into the IR builder.
-- **IR Builder** (Python + C++): provides a frame-stack API where each ``with`` block or
- decorator pushes a frame. When the frame exits, the constructed IR is finalized. The builder
- is shared across dialects — TIR and Relax each register their own frame types.
-- **Printer** (C++): converts TVM IR objects to a ``Doc`` tree (an intermediate representation
- of Python syntax), then formats the tree into valid TVMScript text.
+- **Frontend** (Python): captures definition context, acquires source, and composes the
+ generated callable with its lexical environment. It owns decorators, helper entry points,
+ and execution of the builder program.
+- **Syntax transpiler** (Python): rewrites Python AST nodes into calls on the selected
+ construction namespace. It preserves source scopes, evaluation order, and locations.
+- **IR builders** (Python + C++): own typed values, symbol identity, dialect policy, and
+ construction frames. Exiting a frame finalizes its IR and attaches it to its parent.
+- **Printer** (C++): converts IR to a ``Doc`` tree and formats it as Python syntax. The
+ printer's Doc tree is separate from the Python AST used for parsing.
+
+All source entry points use the canonical ``tvm.script.parser`` implementation.
Decorators
@@ -88,6 +95,12 @@ TVMScript uses three import aliases by convention:
from tvm.script import ir as I # module-level constructs
from tvm.script import tirx as T # TIR constructs
from tvm.script import relax as R # Relax constructs
+ from tvm.script.parser.frontend import make_helper
+ from tvm.target import Target
+
+These are public authoring APIs. ``Target`` configures compilation targets; it remains in
+``tvm.target``. Public dialect namespaces expose decorators and source constructors, so
+scripted programs do not need direct imports from builder implementation packages.
The primary decorators are:
@@ -102,7 +115,7 @@ These can be composed:
@I.ir_module
class MyModule:
- @T.prim_func
+ @T.prim_func(s_tir=True)
def add_kernel(A: T.Buffer((128,), "float32"),
B: T.Buffer((128,), "float32"),
C: T.Buffer((128,), "float32")):
@@ -115,82 +128,124 @@ These can be composed:
def main(x: R.Tensor((128,), "float32"),
y: R.Tensor((128,), "float32")) -> R.Tensor((128,), "float32"):
with R.dataflow():
- out = R.call_tir(cls.add_kernel, (x, y),
+ out = R.call_tir(MyModule.add_kernel, (x, y),
out_ty=R.Tensor((128,), "float32"))
R.output(out)
return out
-When Python encounters ``@I.ir_module``, the decorator does **not** execute the class body.
-Instead, it calls ``tvm.script.parse()`` which extracts the source code of the class,
-builds a Doc AST, and hands it to the parser.
+Python creates the class before invoking ``@I.ir_module``. Function decorators inside an
+IR-module class retain method definitions for module construction. The module frontend then
+acquires the source and constructs declarations and bodies in the module's builder context.
+Standalone ``@T.prim_func`` and ``@R.function`` definitions construct their IR immediately.
+A helper can construct expressions or statements in the caller's active builder frames:
-Parser Architecture
--------------------
+.. code-block:: python
-The parser lives in ``python/tvm/script/parser/``.
+ @make_helper(T)
+ def add_one(value):
+ return value + 1
-Dispatch mechanism
-~~~~~~~~~~~~~~~~~~
+Unlike a function decorator, ``make_helper`` does not create an IR function for each helper.
+Its default return behavior is ordinary Python return from the generated helper program.
+``T.inline`` and the dialect macro decorators are supplied through the same frontend helper
+mechanism.
-Different IR dialects (TIR, Relax) need different handling for the same Python syntax. For
-example, ``if ... else`` inside ``@T.prim_func`` creates a TIR ``If`` branch, while the same
-syntax inside ``@R.function`` creates a Relax ``If`` node with different semantics.
-The parser maintains a **dispatch token** stack (``["default"]`` initially). When it encounters
-a decorated function, it inspects the decorator to determine the token — ``"tirx"`` for
-``@T.prim_func``, ``"relax"`` for ``@R.function`` — and pushes it onto the stack.
+Parser Architecture
+-------------------
-Each AST node type is dispatched via a virtual table:
+The parser lives in ``python/tvm/script/parser/``. The frontend manages source and callable
+composition; the transpiler handles syntax; the builders interpret concrete values.
-.. code-block:: text
+Syntax and construction protocol
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ParseVTable[(token, node_type)] → handler function
+A registered function decorator selects a construction namespace. The same Python syntax
+then lowers to operations supplied by that namespace: for example, unmarked ``if`` creates
+TIR control-flow frames in a primitive function and Relax control-flow frames in a Relax
+function. The transpiler emits these operations without implementing dialect IR semantics.
- Lookup order:
- 1. (current_token, node_type) e.g. ("tirx", "For")
- 2. ("default", node_type) e.g. ("default", "For")
- 3. generic_visit fallback
+``protocol.py`` records callable syntax metadata. Argument policies such as ``expr_str``
+translate symbolic strings written directly in source expressions, while ``global_info``
+preserves a module reference for builder-side lookup. Dtype and placement strings remain
+literal. Captured or computed symbolic shapes must already contain explicit IR variables;
+the parser does not interpret expression strings found inside captured values.
-Dialect-specific parsers (``parser/tirx/parser.py``, ``parser/relax/parser.py``) register
-handlers using ``@dispatch.register(token, type_name)`` decorators.
+Assignments become binding operations, standalone expressions become emission operations,
+and loops and scopes become builder contexts. Concrete binding, type checking, comparison
+construction, and frame finalization belong to the builders. Ordinary host calls and
+operator overloads execute as part of the generated Python program.
Parse flow
~~~~~~~~~~
-The entry point is ``parse(program, extra_vars)``:
-
-1. **Source extraction**: the program's source code is extracted (from a class, function, or
- string) and converted to a Doc AST via Python's ``ast`` module.
+The public entry points include ``tvm.script.parse`` and ``tvm.script.from_source``:
+
+1. **Acquire source and context**: the frontend accepts a function, class, or source string,
+ obtains its Python AST, and retains source locations. Decorators capture the definition
+ context; source-string callers can supply external bindings through ``extra_vars``.
+2. **Translate syntax**: ``IRBuilderTranspiler`` emits Python AST for a builder program,
+ using registered construction operations and argument policies. Name allocation avoids
+ collisions with user identifiers.
+3. **Compose the callable**: the single frontend helper ``recompose_builder`` compiles the
+ generated program with the source's globals, closure bindings, and retained annotation
+ context.
+4. **Execute construction**: the callable enters builder frames, reconstructs declarations
+ and annotations in their required context, and constructs function bodies. Builders own
+ module references and function-local symbolic identity.
+5. **Return IR**: the frontend extracts the constructed object and performs the requested
+ validation; public parsing validates well-formedness by default.
+
+Definition and symbol scopes
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Python lexical bindings and DSL symbolic declarations have different owners. The frontend
+captures the exact definition scope during decoration, including enclosing names used only
+in annotations that Python may omit from the function's closure. Ordinary body references
+retain their source globals and closure bindings. Unrelated callers' local variables do not
+become part of this environment.
+
+Annotations are reconstructed as builder operations within the module/function context.
+The generated program gives them a separate, hygienic annotation scope, so a body-local
+variable cannot shadow a name used by a signature annotation. Callable recomposition and
+lexical environment setup belong to the frontend. The transpiler preserves source scopes
+through Python syntax; it does not classify each name by membership in a runtime environment
+or search caller stacks for values.
+
+DSL expression strings such as ``R.Tensor(("n", 4), "float32")`` use a builder-owned
+``TypeVarFrame`` shared across one function's parameters, return annotation, and body.
+Repeated symbolic names refer to the same symbol. A Python name in ``R.Tensor((n, 4), ...)``
+instead follows its source lexical scope. Module global-info references resolve against
+the enclosing module's builder state.
+
+Shared construction support is exposed as ``I.parser_support``. Generated programs use its
+``at`` and ``with_at_scope`` helpers to attach locations and preserve caller/definition
+provenance while evaluating source calls once. These helpers retain returned object identity;
+builders handle results that already emitted a statement or introduced a binding.
+
+Explicit host control flow
+~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Mark compile-time Python selection with ``I.constexpr`` or the identical dialect alias:
-2. **AST walking**: the ``Parser`` (a subclass of ``doc.NodeVisitor``) walks the Doc AST.
- For each node, it looks up the handler in the dispatch table.
-
-3. **Expression evaluation**: expressions like ``T.grid(128, 128)`` are evaluated by the
- ``ExprEvaluator``, which resolves names against the variable table and the ``T.``/``R.``
- module namespaces.
-
-4. **Value binding**: assignment statements (``A = T.match_buffer(...)`` in TIR,
- ``lv = R.add(x, y)`` in Relax) go through dialect-specific ``bind_*_value()`` functions
- that register the resulting TVM objects in the parser's ``VarTable``.
-
-5. **Scoping**: the ``VarTable`` maintains a stack of frames. Entering a ``with`` block,
- ``for`` loop, or function body pushes a new frame; exiting pops it. This ensures variables
- are scoped correctly.
-
-Variable table
-~~~~~~~~~~~~~~
+.. code-block:: python
-The ``VarTable`` is the parser's symbol table:
+ if I.constexpr(enabled):
+ T.evaluate(1)
+ else:
+ T.evaluate(0)
-.. code-block:: text
+ value = T.ramp(0, 1, lanes) if T.constexpr(lanes > 1) else 0
- VarTable
- ├── frames: [VarTableFrame, ...] ← stack of scopes
- └── name2value: {str: [Any, ...]} ← name → value stack (for shadowing)
+The marker applies to the controlling value. The generated program evaluates it once and
+executes only the selected Python branch in the existing builder scope. Branch bodies and
+result expressions still translate normally. Marked ``and`` and ``or`` decisions retain
+Python short-circuiting; unmarked conditionals and logical expressions call dialect IR
+constructors. A Python boolean alone does not request compile-time branch selection.
-When a name is looked up, the most recent binding wins. When a frame is popped, all bindings
-introduced in that frame are removed.
+JIT supplies validated specialization bindings and optional-argument absence to builder
+execution. Syntax translation does not inspect those values to select a branch.
IR Builder Architecture
@@ -208,7 +263,7 @@ represents and attaches it to the parent frame.
.. code-block:: text
- IRBuilder (thread-local singleton)
+ IRBuilder (current construction context)
└── frame stack:
├── IRModuleFrame ← @I.ir_module
│ ├── PrimFuncFrame ← @T.prim_func
@@ -219,19 +274,19 @@ represents and attaches it to the parent frame.
│ └── BindingBlockFrame ← R.dataflow()
└── ...
-This design means the parser never needs to build a complete IR tree in memory — it
-constructs IR top-down by entering and exiting frames, and each frame handles its own
-finalization.
+The generated program constructs IR by entering and exiting frames. Each frame owns its
+construction policy and finalization; the syntax transpiler does not maintain an IR tree.
TIR builder
~~~~~~~~~~~
-The TIR builder (``ir_builder/tirx/ir.py``) provides functions that map directly to TVMScript
-syntax. Key categories:
+The TIR builder (``python/tvm/tirx/script/builder/``) implements the construction operations
+behind public ``T`` syntax. Builder modules also support direct programmatic construction;
+public decorators and internal frame-opening operations have distinct roles. Key categories:
**Function and block**:
-- ``T.prim_func()`` → ``PrimFuncFrame``
+- ``@T.prim_func`` selects TIR construction; its builder ``function()`` opens ``PrimFuncFrame``
- ``T.sblock(name)`` → ``SBlockFrame`` (spatial block)
- ``T.init()`` → ``BlockInitFrame`` (reduction initialization)
- ``T.reads(...)``, ``T.writes(...)`` → declare buffer access regions
@@ -257,11 +312,11 @@ syntax. Key categories:
Relax builder
~~~~~~~~~~~~~
-The Relax builder (``ir_builder/relax/ir.py``) provides:
+The Relax builder (``python/tvm/relax/script/builder/``) implements:
**Function and dataflow**:
-- ``R.function()`` → ``FunctionFrame``
+- ``@R.function`` selects Relax construction; its builder ``function()`` opens ``FunctionFrame``
- ``R.dataflow()`` → ``BindingBlockFrame``
- ``R.output(*vars)`` → expose variables from a dataflow block
@@ -270,6 +325,9 @@ The Relax builder (``ir_builder/relax/ir.py``) provides:
- ``R.emit(value)`` → emit a binding, returns a ``Var``
- ``R.emit_match_cast(value, ty)`` → emit with type assertion
+Generated standalone-statement emission uses the construction protocol's separate ``emit_``
+operation. Public ``R.emit`` retains its binding-returning imperative API.
+
**Type annotations**:
- ``R.Tensor(shape, dtype)`` — tensor type
@@ -325,17 +383,17 @@ It maintains:
Each IR dialect registers its own converters:
-- ``src/script/printer/tirx/`` — converts PrimFunc, Buffer, SBlock, loops, expressions.
-- ``src/script/printer/relax/`` — converts relax.Function, bindings, types, operators.
+- ``src/tirx/script/printer/`` — converts PrimFunc, Buffer, SBlock, loops, expressions.
+- ``src/relax/script/printer/`` — converts relax.Function, bindings, types, operators.
- ``src/script/printer/ir/`` — converts IRModule, shared types.
The final step calls ``DocToPythonScript()`` (``src/script/printer/doc_printer/python_doc_printer.cc``)
to format the Doc tree into properly indented Python text.
-Roundtrip guarantee
-~~~~~~~~~~~~~~~~~~~
+Roundtrip
+~~~~~~~~~
-For any ``IRModule`` constructed through the compiler:
+For printable IR with the required source context available:
.. code-block:: python
@@ -344,9 +402,9 @@ For any ``IRModule`` constructed through the compiler:
tvm.ir.assert_structural_equal(mod, reparsed)
This roundtrip property is relied upon by testing infrastructure and serialization workflows.
-Note that the printed text may differ from hand-written TVMScript — the printer uses canonical
-forms (e.g., explicit ``R.emit`` calls, fully qualified buffer annotations) that are not required
-in hand-written code.
+Printed text uses canonical forms and may differ from hand-written TVMScript. External
+objects or Python attachments require their corresponding context; text alone does not
+serialize arbitrary Python state.
Supported Python Syntax
@@ -364,7 +422,7 @@ and how each construct is interpreted:
- Relax
* - ``for i in range(n)``
- Serial loop nest
- - Not supported (no Relax-level ``for`` handler)
+ - No Relax IR loop construction
* - ``with T.sblock(...)``
- Spatial block scope
- N/A
@@ -372,8 +430,8 @@ and how each construct is interpreted:
- N/A
- Dataflow block
* - ``if ... else``
- - TIR ``If`` branch (PrimExpr condition) or static eval (Python bool)
- - Relax ``If`` node (plain Python ``if cond:`` syntax)
+ - TIR ``IfThenElse``; explicit ``I.constexpr`` selects a Python branch
+ - Relax ``If``; explicit ``I.constexpr`` selects a Python branch
* - ``while``
- ``T.While`` loop
- Not supported
@@ -385,9 +443,9 @@ and how each construct is interpreted:
- N/A
* - ``x: R.Tensor(...)``
- N/A
- - Struct info annotation
+ - Type annotation
* - ``return``
- - Not used
+ - Primitive return expression
- Function return value
* - ``A[i, j]``
- Buffer load
@@ -402,9 +460,9 @@ and how each construct is interpreted:
- ``T.*`` intrinsics
- ``R.*`` operators or ``call_tir`` / ``call_packed``
-**Not supported**: ``class`` definitions (except for ``@I.ir_module``), ``try/except``,
-``yield``, ``async/await``, list comprehensions, ``lambda``, ``import``, and ``global``
-statements.
+Inside scripted function bodies, unsupported statement forms include ``class``,
+``try/except``, ``import``, and ``global``. ``yield`` and ``async/await`` are also unsupported.
+Place imports and ordinary Python class definitions in the surrounding Python program.
TIR Syntax Reference
@@ -417,6 +475,8 @@ Function definition
@T.prim_func
def func_name(a: T.handle, b: T.handle):
+ m = T.int32()
+ n = T.int32()
A = T.match_buffer(a, (m, n), "float32")
B = T.match_buffer(b, (m,), "float32")
# function body
@@ -428,6 +488,8 @@ Function definition
Block and axes
~~~~~~~~~~~~~~
+Use ``@T.prim_func(s_tir=True)`` for functions with scheduled-TIR blocks and block axes.
+
.. code-block:: python
for i, j in T.grid(128, 128):
@@ -472,7 +534,7 @@ Common intrinsics
T.cast(x, "float16") # type cast
T.if_then_else(cond, true_val, false_val) # conditional expression
T.min(a, b), T.max(a, b) # min/max
- T.call_extern("func_name", *args) # external function call
+ T.call_extern("int32", "func_name", *args) # external function call
T.call_packed("func_name", *args) # packed function call
T.tvm_storage_sync("shared") # GPU memory fence
@@ -514,9 +576,9 @@ Calling TIR functions
.. code-block:: python
- out = R.call_tir(cls.my_kernel, (x, y), out_ty=R.Tensor((128,), "float32"))
+ out = R.call_tir(MyModule.my_kernel, (x, y), out_ty=R.Tensor((128,), "float32"))
-- ``cls.my_kernel`` — references a TIR ``PrimFunc`` in the same module.
+- ``MyModule.my_kernel`` — references a TIR ``PrimFunc`` in the same module.
- ``out_ty`` — the type (shape and dtype) of the output tensor.
Control flow
@@ -545,31 +607,41 @@ Source Code Map
* - Path
- Contents
- * - ``python/tvm/script/parser/core/``
- - Core parser: dispatch, expression evaluator, variable table, Doc AST
- * - ``python/tvm/script/parser/tirx/``
- - TIR-specific parser handlers and value binding
- * - ``python/tvm/script/parser/relax/``
- - Relax-specific parser handlers and value binding
- * - ``python/tvm/script/parser/ir/``
- - ``@I.ir_module`` entry point and module-level parsing
+ * - ``python/tvm/script/parser/frontend.py``
+ - Source acquisition, decorators/helpers, definition context, callable recomposition
+ * - ``python/tvm/script/parser/transpile.py``
+ - Python AST transformation and lexical syntax lowering
+ * - ``python/tvm/script/parser/expression.py`` and ``python/tvm/script/parser/protocol.py``
+ - Expression-string rewriting and registered callable syntax policies
+ * - ``python/tvm/script/parser/jit.py``
+ - JIT argument validation, specialization inputs, and caching
+ * - ``python/tvm/script/parser/ir.py``
+ - Public ``I`` namespace and module-level entry points
* - ``python/tvm/script/ir_builder/base.py``
- - IRBuilder base class and frame stack mechanism
- * - ``python/tvm/script/ir_builder/tirx/``
- - TIR frame types and builder functions (``T.*``)
- * - ``python/tvm/script/ir_builder/relax/``
- - Relax frame types and builder functions (``R.*``)
+ - IRBuilder context, frame stack, and source-span support
+ * - ``python/tvm/script/ir_builder/construction.py``
+ - Runtime function declarations, specialization context, and construction state
+ * - ``python/tvm/script/ir_builder/parser_support.py`` and ``python/tvm/script/ir_builder/type_var_frame.py``
+ - Shared parser-facing builder support and function-local symbolic identity
+ * - ``python/tvm/tirx/script/`` and ``python/tvm/relax/script/``
+ - Public dialect namespaces exposed through ``tvm.script``
+ * - ``python/tvm/tirx/script/builder/``
+ - TIR frame types and concrete construction operations
+ * - ``python/tvm/relax/script/builder/``
+ - Relax frame types and concrete construction operations
* - ``python/tvm/script/ir_builder/ir/``
- - IRModule builder (``I.*``)
+ - IRModule construction and module-owned global information
* - ``src/script/printer/``
- - C++ printer: Doc tree, IRDocsifier, Python code generation
- * - ``src/script/printer/tirx/``
- - TIR-specific IR-to-Doc converters
- * - ``src/script/printer/relax/``
- - Relax-specific IR-to-Doc converters
+ - Shared C++ printer: Doc tree, IRDocsifier, Python code generation
+ * - ``src/tirx/script/printer/`` and ``src/relax/script/printer/``
+ - Dialect-specific IR-to-Doc converters
* - ``src/script/ir_builder/``
- - C++ backend for frame stack and IR construction
+ - Shared C++ builder context and module construction
+ * - ``src/tirx/script/builder/`` and ``src/relax/script/builder/``
+ - C++ dialect construction frames and operations
* - ``include/tvm/script/printer/``
- - C++ headers: Doc classes, IRDocsifier, dispatch functor
+ - Shared C++ printer interfaces
* - ``include/tvm/script/ir_builder/``
- - C++ headers: builder base, dialect-specific frame types
+ - Shared C++ builder and module-frame interfaces
+ * - ``include/tvm/tirx/script/builder/`` and ``include/tvm/relax/script/builder/``
+ - C++ dialect builder interfaces
diff --git a/include/tvm/relax/script/builder/frame.h b/include/tvm/relax/script/builder/frame.h
index a2f663fb50..35429a1cb7 100644
--- a/include/tvm/relax/script/builder/frame.h
+++ b/include/tvm/relax/script/builder/frame.h
@@ -36,6 +36,10 @@ namespace relax {
/*! \brief The base ir_builder frame for the relax dialect. */
class RelaxFrameNode : public IRBuilderFrameNode {
public:
+ /*! \brief Source range captured when this frame is entered. */
+ Span span;
+
+ void EnterWithScope() override;
static void RegisterReflection() {
namespace refl = tvm::ffi::reflection;
refl::ObjectDef<RelaxFrameNode>();
@@ -117,6 +121,13 @@ class FunctionFrameNode : public SeqExprFrameNode {
ffi::Map<ffi::String, Any> attrs;
/*! \brief The block builder to create Relax function. */
tvm::relax::BlockBuilder block_builder;
+ /*! \brief Whether this frame constructs only a function signature. */
+ bool declaration = false;
+ bool local = false;
+ ffi::Optional<tvm::Var> local_var;
+ /*! \brief Finalized function and its stable module reference. */
+ ffi::Optional<tvm::relax::Function> function;
+ ffi::Optional<tvm::GlobalVar> global_var;
static void RegisterReflection() {
namespace refl = tvm::ffi::reflection;
@@ -125,7 +136,10 @@ class FunctionFrameNode : public SeqExprFrameNode {
.def_ro("params", &FunctionFrameNode::params)
.def_ro("ret_ty", &FunctionFrameNode::ret_ty)
.def_ro("is_pure", &FunctionFrameNode::is_pure)
- .def_ro("attrs", &FunctionFrameNode::attrs);
+ .def_ro("attrs", &FunctionFrameNode::attrs)
+ .def_ro("function", &FunctionFrameNode::function)
+ .def_ro("global_var", &FunctionFrameNode::global_var)
+ .def_ro("local_var", &FunctionFrameNode::local_var);
// `binding_blocks` and `output` are inherited from SeqExprFrameNode.
// `block_builder` is not registered as it's not visited.
}
@@ -163,6 +177,8 @@ class BindingBlockFrameNode : public RelaxFrameNode {
* \note Only used for a dataflow block.
*/
ffi::Array<tvm::Var> output_vars;
+ /*! \brief Statement ranges for explicitly emitted bindings. */
+ ffi::Map<tvm::Var, Span> binding_spans;
static void RegisterReflection() {
namespace refl = tvm::ffi::reflection;
diff --git a/include/tvm/relax/script/builder/ir.h b/include/tvm/relax/script/builder/ir.h
index 2516dc0134..0a1bbdf688 100644
--- a/include/tvm/relax/script/builder/ir.h
+++ b/include/tvm/relax/script/builder/ir.h
@@ -39,6 +39,15 @@ namespace relax {
*/
TVM_DLL FunctionFrame Function(bool is_pure, bool is_private);
+/*! \brief Start a bodyless declaration using the normal signature operations. */
+TVM_DLL FunctionFrame DeclFunction(bool is_pure, bool is_private, bool local);
+
+/*! \brief Define a local function under its cached declaration identity. */
+TVM_DLL FunctionFrame LocalFunction(bool is_pure, const tvm::Var& reference);
+
+/*! \brief Add a cached parameter without changing its identity. */
+TVM_DLL tvm::Var ArgVar(const ffi::String& name, const tvm::Var& var);
+
/*!
* \brief Add a parameter to the last function frame.
* \param name The name of the parameter.
@@ -117,6 +126,15 @@ TVM_DLL tvm::Var EmitMatchCast(const tvm::relax::Expr& value, const tvm::Type& t
*/
TVM_DLL tvm::Var EmitVarBinding(const tvm::relax::VarBinding& binding);
+/*! \brief Emit a binding with separate statement and variable-name ranges. */
+TVM_DLL tvm::Var EmitWithSpan(const tvm::relax::Expr& value,
+ const ffi::Optional<tvm::Type>& annotate_ty,
+ const ffi::Optional<Span>& name_span);
+
+/*! \brief Emit a match cast with separate statement and variable-name ranges. */
+TVM_DLL tvm::Var EmitMatchCastWithSpan(const tvm::relax::Expr& value, const tvm::Type& ty,
+ const ffi::Optional<Span>& name_span);
+
///////////////////////////// If Then Else /////////////////////////////
/*!
diff --git a/include/tvm/script/ir_builder/base.h b/include/tvm/script/ir_builder/base.h
index 417e9b0c38..ca17697682 100644
--- a/include/tvm/script/ir_builder/base.h
+++ b/include/tvm/script/ir_builder/base.h
@@ -66,10 +66,12 @@ class IRBuilderFrameNode : public ffi::Object {
public:
/*! \brief A list of callbacks used when exiting the frame. */
std::vector<ffi::TypedFunction<void()>> callbacks;
+ /*! \brief Source context retained until this frame constructs its result. */
+ mutable Span source_span;
static void RegisterReflection() {
namespace refl = tvm::ffi::reflection;
- refl::ObjectDef<IRBuilderFrameNode>();
+ refl::ObjectDef<IRBuilderFrameNode>().def_ro("source_span", &IRBuilderFrameNode::source_span);
// `callbacks` is not registered as it's not visited.
}
@@ -129,6 +131,29 @@ class IRBuilderFrame : public ffi::ObjectRef {
}
};
+/*! \brief Per-function symbols, retained across declaration and definition.
+ * The native builder stack owns active scope; the FunctionRecord owns this
+ * frame between entries. symbols maps source names to canonical primitive Vars.
+ * It starts empty, survives re-entry, and is never shared between functions.
+ */
+class TypeVarFrameNode : public IRBuilderFrameNode {
+ public:
+ ffi::Map<ffi::String, tvm::Var> symbols;
+
+ static void RegisterReflection() {
+ ffi::reflection::ObjectDef<TypeVarFrameNode>().def_ro("symbols", &TypeVarFrameNode::symbols);
+ }
+ TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.TypeVarFrame", TypeVarFrameNode,
+ IRBuilderFrameNode);
+};
+
+/*! \brief Managed reference to the shared function symbol frame. */
+class TypeVarFrame : public IRBuilderFrame {
+ public:
+ TypeVarFrame() : IRBuilderFrame(ffi::make_object<TypeVarFrameNode>()) {}
+ TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TypeVarFrame, IRBuilderFrame, TypeVarFrameNode);
+};
+
////////////////////////////// IRBuilder //////////////////////////////
/*!
@@ -183,7 +208,7 @@ class IRBuilderNode : public ffi::Object {
template <typename TFrame>
inline ffi::Optional<TFrame> FindFrame() const;
/*!
- * \brief Get the frame on top of the stack `this->frames` if its type is `TFrame`.
+ * \brief Get the top semantic frame if its type is `TFrame`; skip symbol-only frames.
* \tparam TFrame The assumed type of the last frame on stack.
* \return The frame if the stack is non-empty and the top of the stack is of type `TFrame`.
* Otherwise std::nullopt.
@@ -197,13 +222,20 @@ class IRBuilderNode : public ffi::Object {
*/
template <typename TObjectRef>
inline TObjectRef Get() const;
+ /*! \brief Whether any semantic construction frame remains (symbol frames are transparent). */
+ bool HasConstructionFrames() const {
+ for (const auto& frame : frames) {
+ if (!frame->IsInstance<TypeVarFrameNode>()) return true;
+ }
+ return false;
+ }
/*! \brief Push a frontend source span for IR constructed in the nested scope. */
void PushSourceSpan(Span span);
/*! \brief Pop the innermost frontend source span. */
void PopSourceSpan();
/*! \brief Return the normalized active source span, including expansion history. */
Span GetCurrentSourceSpan() const;
- /*! \brief Attach the active source span to an expression that has no span yet. */
+ /*! \brief Compose active source context onto a supported node or construction frame. */
ffi::ObjectRef SetCurrentSourceSpan(ffi::ObjectRef obj) const;
};
@@ -296,8 +328,10 @@ inline ffi::Optional<TFrame> IRBuilderNode::FindFrame() const {
template <typename TFrame>
inline ffi::Optional<TFrame> IRBuilderNode::GetLastFrame() const {
using TFrameNode = typename TFrame::ContainerType;
- if (!frames.empty() && frames.back()->IsInstance<TFrameNode>()) {
- return frames.back().as_or_throw<TFrame>();
+ for (auto it = frames.rbegin(); it != frames.rend(); ++it) {
+ if ((*it)->IsInstance<TFrameNode>()) return (*it).template as_or_throw<TFrame>();
+ // Symbol state is administrative, not a lexical IR construction scope.
+ if (!(*it)->IsInstance<TypeVarFrameNode>()) break;
}
return std::nullopt;
}
diff --git a/include/tvm/script/ir_builder/ir/ir.h b/include/tvm/script/ir_builder/ir/ir.h
index 761e2a995f..9abbdc2b07 100644
--- a/include/tvm/script/ir_builder/ir/ir.h
+++ b/include/tvm/script/ir_builder/ir/ir.h
@@ -36,6 +36,9 @@ namespace ir {
*/
TVM_DLL IRModuleFrame IRModule();
+/*! \brief Reserve a stable function identity before evaluating its signature. */
+TVM_DLL GlobalVar ReserveFunction(const ffi::String& func_name);
+
/*!
* \brief Declare a Function without given the specific function implementation.
* \note It is usually used in cross-function call. And we can specify the function by `DefFunction`
diff --git a/include/tvm/tirx/script/builder/frame.h b/include/tvm/tirx/script/builder/frame.h
index 0427b9def3..9617ae8b1b 100644
--- a/include/tvm/tirx/script/builder/frame.h
+++ b/include/tvm/tirx/script/builder/frame.h
@@ -93,6 +93,11 @@ class PrimFuncFrameNode : public TIRFrameNode {
bool s_tir;
/*! \brief Whether it is a persistent kernel. */
bool persistent;
+ /*! \brief Whether this frame declares a bodyless signature. */
+ bool is_declaration{false};
+ /*! \brief Finalized function and its module identity. */
+ ffi::Optional<tvm::tirx::PrimFunc> function;
+ ffi::Optional<GlobalVar> global_var;
static void RegisterReflection() {
namespace refl = tvm::ffi::reflection;
@@ -106,7 +111,10 @@ class PrimFuncFrameNode : public TIRFrameNode {
.def_ro("env_threads", &PrimFuncFrameNode::env_threads)
.def_ro("root_alloc_buffers", &PrimFuncFrameNode::root_alloc_buffers)
.def_ro("s_tir", &PrimFuncFrameNode::s_tir)
- .def_ro("persistent", &PrimFuncFrameNode::persistent);
+ .def_ro("persistent", &PrimFuncFrameNode::persistent)
+ .def_ro("is_declaration", &PrimFuncFrameNode::is_declaration)
+ .def_ro("function", &PrimFuncFrameNode::function)
+ .def_ro("global_var", &PrimFuncFrameNode::global_var);
}
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.tirx.PrimFuncFrame", PrimFuncFrameNode,
TIRFrameNode);
diff --git a/include/tvm/tirx/script/builder/ir.h b/include/tvm/tirx/script/builder/ir.h
index 537c23fb2b..055f5478c5 100644
--- a/include/tvm/tirx/script/builder/ir.h
+++ b/include/tvm/tirx/script/builder/ir.h
@@ -68,6 +68,9 @@ BufferVar BufferDecl(ffi::Array<PrimExpr> shape, PrimType dtype, ffi::String buf
*/
PrimFuncFrame PrimFunc(bool is_private, bool s_tir = false, bool persistent = false);
+/*! \brief Construct a bodyless function signature using the ordinary signature operations. */
+PrimFuncFrame DeclFunction(bool is_private = false, bool s_tir = false, bool persistent = false);
+
/*!
* \brief The PrimFunc variable arguments adding function.
* \param name The name of the variable.
@@ -494,8 +497,9 @@ void BufferStore(BufferVar buffer, PrimExpr value, ffi::Array<PrimExpr> indices)
/*!
* \brief Evaluate the input expression.
* \param value The input expression to evaluate.
+ * \return The same statement that was added to the parent frame.
*/
-void Evaluate(Expr value);
+tvm::tirx::Stmt Evaluate(Expr value);
/*!
* \brief Create a TIR var that represents a pointer
diff --git a/python/tvm/relax/script/__init__.py b/python/tvm/relax/script/__init__.py
index acbd9d2435..31cf1096ee 100644
--- a/python/tvm/relax/script/__init__.py
+++ b/python/tvm/relax/script/__init__.py
@@ -14,14 +14,18 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-"""Relax-layer TVMScript pieces (parser, builder).
+"""Public canonical TVMScript dialect namespace."""
+import importlib as _importlib
-After the per-dialect TVMScript restructure, the Relax layer owns its own
-``script/{parser,builder}`` subpackages. ``tvm.script.relax`` resolves to
-this module via the dialect registry, so the public parser surface
-(``function``, ``Tensor``, ``match_cast``, etc.) is re-exported here.
-"""
-# pylint: disable=redefined-builtin,wildcard-import,unused-wildcard-import
-from .parser import *
-from .parser import dist
+def __getattr__(name):
+ if name in ("builder", "tile"):
+ return _importlib.import_module(__name__ + "." + name)
+ if name.startswith("_") and name != "__all__":
+ raise AttributeError(name)
+ from tvm.script import parser as _parser
+ _parser._initialize()
+ if name in globals():
+ return globals()[name]
+ builder = _importlib.import_module(__name__ + ".builder")
+ return getattr(builder, name)
diff --git a/python/tvm/relax/script/builder/__init__.py b/python/tvm/relax/script/builder/__init__.py
index 021bcf9b1c..6ae73e36b7 100644
--- a/python/tvm/relax/script/builder/__init__.py
+++ b/python/tvm/relax/script/builder/__init__.py
@@ -14,12 +14,508 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-"""Package tvm.relax.script.builder.
+"""Concrete Relax construction operations over the shared native builder stack."""
-Holds the per-dialect ir_builder API for Relax. The legacy path
-``tvm.script.ir_builder.relax`` resolves here via the dialect registry.
-"""
+# pylint: disable=wildcard-import,redefined-builtin,invalid-name
+import builtins as _python
+import numbers as _numbers
-# pylint: disable=wildcard-import,redefined-builtin
-from . import distributed, frame, ir
+import tvm_ffi as _ffi
+
+from tvm import ir as _ir
+from tvm import relax as _relax
+from tvm import tirx as _tir
+from tvm.relax.distributed import DeviceMesh as _DeviceMesh
+from tvm.relax.distributed import DTensorType as _DTensorType
+from tvm.relax.distributed import Placement as _Placement
+from tvm.relax.distributed import device_mesh as device_mesh
+from tvm.script.ir_builder import IRBuilder as _IRBuilder
+from tvm.script.ir_builder import ir as _I
+from tvm.script.ir_builder.base import BypassBind as _BypassBind
+from tvm.script.ir_builder.base import _construction_span
+from tvm.script.ir_builder.base import _frame_result as _named_frame_result
+from tvm.script.ir_builder.base import at as _at
+from tvm.script.ir_builder.base import source_span as _source_span
+from tvm.script.ir_builder.parser_support import lookup_global_info as _lookup_global_info
+from tvm.script.ir_builder.type_var_frame import TypeVarDecl as _TypeVarDecl
+from tvm.script.ir_builder.type_var_frame import resolve_type_var
+from tvm.script.parser.protocol import args_policy as _args_policy
+from tvm.script.parser.protocol import constexpr as constexpr
+from tvm.script.parser.protocol import expr_str_args as _expr_str_args
+
+from . import _ffi_api
+from . import distributed as dist
+from . import frame as _frame
+from . import ir as _native
+from .comparison import eq as eq
+from .comparison import ge as ge
+from .comparison import gt as gt
+from .comparison import le as le
+from .comparison import lt as lt
+from .comparison import ne as ne
+from .distributed.ir import _lookup_device_mesh
from .ir import *
+from .protocol import bind_ as bind_
+from .protocol import emit_ as emit_
+
+
+@_args_policy({"shape": "expr_str", "vdevice": "global_info"}, scalar_strings=False)
+def Tensor(shape=None, dtype=None, vdevice=None, ndim=-1, *, span=None):
+ """Construct a tensor type from concrete shape dimensions."""
+ if isinstance(shape, _python.str) and dtype is None:
+ dtype, shape = shape, None
+ if isinstance(vdevice, _python.str) and not _IRBuilder.is_in_scope():
+ # Python evaluates annotations before the module decorator opens its
+ # frame. The generated declaration resolves this module-owned reference.
+ return _ir.Type.missing()
+ vdevice = _lookup_global_info(vdevice)
+ return _relax.TensorType(shape, dtype, vdevice, ndim, _source_span(span))
+
+
+@_args_policy({"shape": "expr_str", "device_mesh": "global_info"}, scalar_strings=False)
+def DTensor(shape=None, dtype=None, device_mesh=None, placement="", *, ndim=-1, span=None):
+ """Construct a distributed tensor type from concrete dimensions."""
+ if isinstance(device_mesh, _python.str) and not _IRBuilder.is_in_scope():
+ return _ir.Type.missing()
+ if device_mesh is None:
+ device_mesh = _DeviceMesh([], _ir.Range(0, 1))
+ else:
+ device_mesh = _lookup_global_info(device_mesh)
+ if isinstance(placement, _python.str):
+ placement = _Placement.from_text(placement)
+ return _DTensorType(Tensor(shape, dtype, ndim=ndim), device_mesh, placement, _source_span(span))
+
+
+# The distributed source spelling shares concrete constructors and argument policy.
+dist.DTensor = DTensor
+dist.device_mesh = device_mesh
+
+Range = _ir.Range
+
+
+@_expr_str_args("values", introduce=True, dtype="int64")
+def Shape(values=None, ndim=-1, *, span=None):
+ """Construct a shape type from concrete dimensions."""
+ return _relax.ShapeType(values, ndim, _source_span(span))
+
+
+def _type(value):
+ if value is None:
+ return _ir.TupleType([])
+ if callable(value):
+ value = value()
+ if _ir.is_prim_expr(value) or isinstance(value, _TypeVarDecl):
+ value = value.ty
+ if not isinstance(value, _ir.Type):
+ raise TypeError(f"Expected a concrete type, got {type(value).__name__}")
+ return value
+
+
+def Callable(params=None, ret=None, purity=None, derive_func=None, *, span=None):
+ """Construct a concrete or opaque Relax function type."""
+ if purity is None:
+ purity = params is not None
+ if params is None:
+ return _relax.FuncType.opaque_func(
+ ret=None if ret is None else _type(ret),
+ derive_func=derive_func,
+ purity=purity,
+ span=_source_span(span),
+ )
+ if derive_func is not None:
+ raise ValueError("A derivation function requires an opaque callable")
+ if not isinstance(params, list | _python.tuple):
+ params = [params]
+ return _relax.FuncType(
+ [_type(param) for param in params], _type(ret), purity, _source_span(span)
+ )
+
+
+def Tuple(*fields, span=None):
+ """Construct a tuple type while preserving missing component types."""
+ if len(fields) == 1 and isinstance(fields[0], list | _python.tuple):
+ fields = fields[0]
+ return _ir.TupleType([_type(field) for field in fields], _source_span(span))
+
+
+def Prim(dtype, *, span=None):
+ """Construct a primitive scalar type."""
+ return _ir.PrimType(dtype)
+
+
+Prim.__tvm_parameter_dtype__ = "dtype"
+
+
+def Object(*, span=None):
+ """Construct the unconstrained Relax value type."""
+ return _relax.AnyType(_source_span(span))
+
+
+Any = Object
+
+
+is_type_var = _ir.is_prim_var
+
+
+def type_var(name, *, dtype=None, span=None):
+ """Construct a fresh standalone primitive symbol."""
+ return _ir.Var(name, "int64" if dtype is None else dtype, _source_span(span))
+
+
+class _Frame:
+ """Retain source metadata and exports around an existing native frame."""
+
+ def __init__(self, native, span=None):
+ # Each wrapper owns one native construction frame and source location.
+ # result starts empty, records finalized lexical exports on exit, and
+ # never outlives its construction region or stores another function's
+ # symbols (those belong to the separate TypeVarFrame).
+ self.native = native
+ self.span = span
+ self.result = {}
+
+ def __getattr__(self, name):
+ return getattr(self.native, name)
+
+ @property
+ def reference(self):
+ """Return the stable module or local function reference after declaration."""
+ if isinstance(self.native, _frame.FunctionFrame):
+ local_var = self.native.local_var
+ return local_var if local_var is not None else self.native.global_var
+ raise AttributeError("This frame does not declare a function")
+
+ def _set_source_span(self, span):
+ _at(span, self.native)
+
+ def __enter__(self):
+ with _construction_span(self.span):
+ self.native.__enter__()
+ return self
+
+ def __exit__(self, exc_type, exc_value, traceback):
+ with _construction_span(self.span):
+ self.native.__exit__(exc_type, exc_value, traceback)
+ if exc_type is None:
+ if isinstance(self.native, _frame.BindingBlockFrame):
+ self.result = {var.name: var for var in self.native.output_vars}
+ elif (
+ isinstance(self.native, _frame.FunctionFrame) and self.native.local_var is not None
+ ):
+ self.result = {self.native.name: self.native.local_var}
+ elif isinstance(self.native, _frame.IfFrame):
+ self.result = {self.native.var_name: self.native.var}
+ return False
+
+
+def frame_result(completed_frame, name):
+ """Read one named export from an explicitly supplied completed region."""
+ return _named_frame_result(completed_frame, name)
+
+
+def function(is_pure=True, is_private=False, *, local=False, reference=None, span=None):
+ """Start a function frame.
+ Parameters
+ ----------
+ is_pure: bool
+ Whether the function is annotated as pure.
+
+ is_private : bool
+ Whether the function is annotated as private.
+
+ local : bool, optional
+ Whether to define a local function instead of a module function.
+ reference : Var, optional
+ The declared function variable required when local is True.
+ span : Span or source location, optional
+ Source location attached to the function frame.
+
+ Returns
+ -------
+ frame : context manager
+ Construction context for the native frame, retaining source metadata.
+ """
+ if local:
+ if reference is None:
+ raise ValueError("A local function requires its declared reference")
+ return _Frame(_ffi_api.LocalFunction(is_pure, reference), span)
+ return _Frame(_native.function(is_pure, is_private), span)
+
+
+def decl_function(is_pure=True, is_private=False, *, local=False, span=None):
+ """Create a bodyless Relax function declaration context."""
+ return _Frame(_ffi_api.DeclFunction(is_pure, is_private, local), span)
+
+
+def arg(name, ty, *, span=None):
+ """Add a parameter to the last function frame.
+ Parameters
+ ----------
+ name: str
+ The name of the parameter.
+ ty : Type, Var or callable
+ The parameter type or an existing variable. An existing variable
+ retains its native identity; callable type annotations are resolved
+ before creating the parameter.
+
+ span : Span or source location, optional
+ Source location attached to the constructed IR.
+
+ Returns
+ -------
+ var: Var
+ The created or retained function parameter variable.
+ """
+ with _construction_span(span):
+ if isinstance(ty, _ir.Var):
+ return _ffi_api.ArgVar(name, ty)
+ return _at(span, _native.arg(name, _type(ty)))
+
+
+def func_ret_type(ret_ty):
+ """Set the active function signature return type."""
+ return _native.func_ret_type(_type(ret_ty))
+
+
+func_ret_ty = func_ret_type
+
+
+def dataflow(*, span=None):
+ """Create a dataflow context with explicit finalized exports."""
+ return _Frame(_native.dataflow(), span)
+
+
+def If(condition, *, span=None):
+ """Create an if frame.
+
+ Parameters
+ ----------
+ condition : Expr
+
+ The condition of if statement, executes the true branch if the
+ condition is true, otherwise jump into the false branch.
+
+ span : Span or source location, optional
+ Source location attached to the constructed IR.
+
+ Returns
+ -------
+ frame : context manager
+ Construction context for the native frame, retaining source metadata.
+ """
+ return _Frame(_native.If(condition), span)
+
+
+def Then(*, span=None):
+ """Create the true branch of the active conditional."""
+ return _Frame(_native.Then(), span)
+
+
+def Else(*, span=None):
+ """Create the false branch of the active conditional."""
+ return _Frame(_native.Else(), span)
+
+
+def _value(value, ty=None):
+ if isinstance(value, _python.tuple):
+ return _relax.utils.convert_to_expr(value)
+ if isinstance(value, _numbers.Number):
+ if isinstance(ty, _ir.PrimType):
+ return _relax.prim_value(value, dtype=ty.dtype)
+ return _relax.const(value)
+ return value
+
+
+def return_(value=None, *, span=None):
+ """Record a function result without exiting Python construction."""
+ with _construction_span(span):
+ if value is None:
+ value = _relax.Tuple([])
+ _native.func_ret_value(_value(value))
+
+
+def match_cast(value, ty, *, span=None):
+ """Construct a match-cast descriptor for bind_ to consume."""
+ if value is None:
+ raise ValueError("The match-cast value cannot be None")
+ ty = _type(ty)
+ return _relax.MatchCast(_ir.Var("", ty), _value(value), ty, _source_span(span))
+
+
+def unpack(value):
+ """Project an IR tuple with known arity or preserve host iteration."""
+ if isinstance(value, _BypassBind):
+ return _python.tuple(_BypassBind(item) for item in unpack(value.value))
+ if isinstance(value, _relax.Tuple):
+ return _python.tuple(value.fields)
+ if isinstance(value, _relax.Expr) and isinstance(value.ty, _ir.TupleType):
+ return _python.tuple(_relax.TupleGetItem(value, i) for i in range(len(value.ty.fields)))
+ return value
+
+
+def assert_(condition, message="", *, span=None):
+ """Emit a runtime assertion with construction-time diagnostic text."""
+ if not isinstance(message, _python.str):
+ raise TypeError("An assertion message must be construction-time text")
+ with _construction_span(span):
+ emit_(_at(span, _native.assert_op(condition, format=message)), span=span)
+
+
+def For(*args, span=None, **kwargs):
+ """Reject imperative for loops in the Relax expression dialect."""
+ raise TypeError("Relax does not support imperative for loops")
+
+
+def break_(*, span=None):
+ """Reject break in the Relax expression dialect."""
+ raise TypeError("Relax does not support break")
+
+
+def continue_(*, span=None):
+ """Reject continue in the Relax expression dialect."""
+ raise TypeError("Relax does not support continue")
+
+
+def setitem(target, index, value, *, span=None):
+ """Reject indexed assignment in the Relax expression dialect."""
+ raise TypeError("Relax does not support indexed assignment")
+
+
+__all__ = [
+ *_native.__all__,
+ "Any",
+ "Callable",
+ "DTensor",
+ "For",
+ "for_",
+ "frame_result",
+ "resolve_type_var",
+ "Object",
+ "Prim",
+ "Range",
+ "Shape",
+ "Tensor",
+ "Tuple",
+ "assert_",
+ "break_",
+ "continue_",
+ "bind_",
+ "decl_function",
+ "device_mesh",
+ "dist",
+ "emit_",
+ "eq",
+ "ge",
+ "gt",
+ "le",
+ "lt",
+ "ne",
+ "is_type_var",
+ "match_cast",
+ "return_",
+ "setitem",
+ "type_var",
+ "unpack",
+]
+
+
+def _logical_pair(lhs, rhs, operation, primitive, python_operation):
+ if not isinstance(lhs, _ir.Expr) and not isinstance(rhs, _ir.Expr):
+ return python_operation(lhs, rhs)
+ if _ir.is_prim_expr(lhs) or _ir.is_prim_expr(rhs):
+ return primitive(lhs, rhs)
+ return operation(_value(lhs), _value(rhs))
+
+
+def logical_and(*values):
+ """Construct conjunction of concrete host, primitive, or tensor values."""
+ if not values:
+ raise TypeError("logical_and requires at least one operand")
+ result = values[0]
+ for value in values[1:]:
+ result = _logical_pair(result, value, _relax.op.logical_and, _tir.And, lambda a, b: a and b)
+ return result
+
+
+def logical_or(*values):
+ """Construct disjunction of concrete host, primitive, or tensor values."""
+ if not values:
+ raise TypeError("logical_or requires at least one operand")
+ result = values[0]
+ for value in values[1:]:
+ result = _logical_pair(result, value, _relax.op.logical_or, _tir.Or, lambda a, b: a or b)
+ return result
+
+
+def logical_not(value):
+ """Negate a host or IR boolean without coercing IR to Python bool."""
+ if _ir.is_prim_expr(value):
+ return _tir.Not(value)
+ if isinstance(value, _ir.Expr):
+ return _relax.op.logical_not(value)
+ return not value
+
+
+def select(condition, true_value, false_value):
+ """Construct elementwise selection or select already-built host values."""
+ if _ir.is_prim_expr(condition):
+ return _tir.Select(condition, true_value, false_value)
+ if isinstance(condition, _ir.Expr):
+ return _relax.op.where(condition, _value(true_value), _value(false_value))
+ return true_value if condition else false_value
+
+
+__all__ += ["logical_and", "logical_not", "logical_or", "select"]
+
+
+for_ = For
+
+
+def if_then_else_(condition, true_value, false_value):
+ """Construct scalar conditional evaluation from eagerly constructed operands."""
+ if isinstance(condition, _ffi.ObjectConvertible):
+ condition = condition.asobject()
+ if not isinstance(condition, _ir.Expr):
+ return true_value if condition else false_value
+ true_value = (
+ true_value.asobject() if isinstance(true_value, _ffi.ObjectConvertible) else true_value
+ )
+ false_value = (
+ false_value.asobject() if isinstance(false_value, _ffi.ObjectConvertible) else false_value
+ )
+ if _ir.is_prim_expr(condition) and all(
+ _ir.is_prim_expr(value)
+ if isinstance(value, _ir.Expr)
+ else isinstance(value, _numbers.Number)
+ for value in (true_value, false_value)
+ ):
+ return _tir.if_then_else(condition, true_value, false_value)
+ return _relax.If(condition, _value(true_value), _value(false_value))
+
+
+def _chain_binding(variable, value, body):
+ if _ir.is_prim_expr(value) and _ir.is_prim_expr(body):
+ return _tir.Let(variable, value, body)
+ return _relax.SeqExpr([_relax.BindingBlock([_relax.VarBinding(variable, value)])], body)
+
+
+def and_(*values, chain=None):
+ """Construct the dialect's logical conjunction from evaluated values."""
+ if chain is not None:
+ from tvm.tirx.script.builder.comparison import _comparison_chain
+
+ return _comparison_chain(values, chain, and_, _chain_binding)
+ return logical_and(*values)
+
+
+def or_(*values):
+ """Construct the dialect's logical disjunction from evaluated values."""
+ return logical_or(*values)
+
+
+def not_(value):
+ """Negate a host or IR boolean without coercing IR to Python bool."""
+ return logical_not(value)
+
+
+__all__ += ["and_", "if_then_else_", "not_", "or_"]
diff --git a/python/tvm/relax/script/builder/comparison.py b/python/tvm/relax/script/builder/comparison.py
new file mode 100644
index 0000000000..ec409cef99
--- /dev/null
+++ b/python/tvm/relax/script/builder/comparison.py
@@ -0,0 +1,61 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Concrete Relax tensor and primitive comparison construction."""
+
+import numbers
+
+from tvm import ir, relax
+from tvm.script.ir_builder.base import at
+from tvm.tirx.script.builder import comparison as primitive
+
+
+def _comparison(lhs, rhs, tensor_operation, primitive_operation, span):
+ if any(isinstance(value, ir.Expr) and not ir.is_prim_expr(value) for value in (lhs, rhs)):
+ lhs = relax.const(lhs) if isinstance(lhs, numbers.Number) else lhs
+ rhs = relax.const(rhs) if isinstance(rhs, numbers.Number) else rhs
+ return at(span, tensor_operation(lhs, rhs))
+ return primitive_operation(lhs, rhs, span=span)
+
+
+def lt(lhs, rhs, *, span=None):
+ """Construct a Relax less-than comparison in written operand order."""
+ return _comparison(lhs, rhs, relax.op.less, primitive.lt, span)
+
+
+def le(lhs, rhs, *, span=None):
+ """Construct a Relax less-than-or-equal comparison in written operand order."""
+ return _comparison(lhs, rhs, relax.op.less_equal, primitive.le, span)
+
+
+def gt(lhs, rhs, *, span=None):
+ """Construct a Relax greater-than comparison in written operand order."""
+ return _comparison(lhs, rhs, relax.op.greater, primitive.gt, span)
+
+
+def ge(lhs, rhs, *, span=None):
+ """Construct a Relax greater-than-or-equal comparison in written operand order."""
+ return _comparison(lhs, rhs, relax.op.greater_equal, primitive.ge, span)
+
+
+def eq(lhs, rhs, *, span=None):
+ """Construct a Relax equality comparison in written operand order."""
+ return _comparison(lhs, rhs, relax.op.equal, primitive.eq, span)
+
+
+def ne(lhs, rhs, *, span=None):
+ """Construct a Relax inequality comparison in written operand order."""
+ return _comparison(lhs, rhs, relax.op.not_equal, primitive.ne, span)
diff --git a/python/tvm/relax/script/builder/distributed/ir.py b/python/tvm/relax/script/builder/distributed/ir.py
index 6192862473..827dea67fd 100644
--- a/python/tvm/relax/script/builder/distributed/ir.py
+++ b/python/tvm/relax/script/builder/distributed/ir.py
@@ -45,6 +45,7 @@ from tvm.relax.utils import convert_to_expr
from tvm.runtime import _tensor
from tvm.script.ir_builder import IRBuilder
from tvm.script.ir_builder.ir import IRModuleFrame
+from tvm.script.ir_builder.ir.ir import lookup_global_info
from . import _ffi_api
@@ -93,24 +94,26 @@ def const(
value: bool | int | float | _np.ndarray | tvm.runtime.Tensor,
ty: DTensorType,
) -> GenericConst:
- """Create a constant value.
+ """Create a distributed constant value with the specified tensor type.
Parameters
----------
- value: Union[bool, int, float, numpy.ndarray, tvm.runtime.Tensor]
+ value : bool, int, float, numpy.ndarray or tvm.runtime.Tensor
The constant value.
- dtype: Optional[str]
- The data type of the resulting constant.
+ ty : DTensorType
+ The distributed tensor type, including its tensor dtype, device mesh
+ and placement.
- Note
- ----
- When dtype is None, we use the following rule:
+ Returns
+ -------
+ res : GenericConst
+ The constant carrying the supplied distributed tensor type.
- - int maps to "int32"
- - float maps to "float32"
- - bool maps to "bool"
- - other using the same default rule as numpy.
+ Notes
+ -----
+ Python scalars and NumPy values are converted to the dtype specified by
+ ``ty.tensor_ty``. The dtype is not inferred from the Python value.
"""
ty = tvm.runtime.convert(ty)
if not isinstance(ty, DTensorType):
@@ -131,10 +134,13 @@ def const(
def _lookup_device_mesh(device_mesh_str: py_str) -> DeviceMesh:
- if not IRBuilder.is_in_scope():
- raise ValueError("device_mesh cannot be found in global info")
name, index_str = device_mesh_str.split("[")
index = int(index_str[:-1])
+ if not IRBuilder.is_in_scope():
+ device_mesh = lookup_global_info(name, index)
+ if not isinstance(device_mesh, DeviceMesh):
+ raise TypeError("The device_mesh global info must be a DeviceMesh.")
+ return device_mesh
frames = IRBuilder.current().frames
for f in frames:
if isinstance(f, IRModuleFrame):
diff --git a/python/tvm/relax/script/builder/ir.py b/python/tvm/relax/script/builder/ir.py
index a3d7d3c038..2a2c207ba9 100644
--- a/python/tvm/relax/script/builder/ir.py
+++ b/python/tvm/relax/script/builder/ir.py
@@ -229,7 +229,7 @@ def to_vdevice(data: Expr, dst_vdevice: py_str | VDevice) -> Expr:
data : Expr
The tensor to be copied.
- dst_device : Union[py_str, VDevice]
+ dst_vdevice : Union[py_str, VDevice]
The destination device where the data is copied to.
Returns
@@ -289,32 +289,17 @@ def arg(name: py_str, ty: Type) -> Var:
def func_name(name: py_str) -> None:
- """Specify the name of the last function frame.
- Parameters
- ----------
- name: str
- The function name.
- """
+ """Specify the name of the last function frame."""
return _ffi_api.FuncName(name) # type: ignore[attr-defined] # pylint: disable=no-member
def func_attr(attrs: dict[py_str, tvm_Object]) -> None:
- """Specify the attrs of the last function frame.
- Parameters
- ----------
- attrs: Dict[str, Object]
- The function attrs.
- """
+ """Specify the attrs of the last function frame."""
return _ffi_api.FuncAttrs(attrs) # type: ignore[attr-defined] # pylint: disable=no-member
def func_ret_type(ret_ty: Type) -> None:
- """Specify the return type of the last function frame.
- Parameters
- ----------
- ret_ty: Type
- The function return type.
- """
+ """Specify the return type of the last function frame."""
return _ffi_api.FuncRetType(ret_ty) # type: ignore[attr-defined] # pylint: disable=no-member
@@ -324,12 +309,7 @@ def func_ret_ty(ret_ty: Type) -> None:
def func_ret_value(value: Expr) -> None:
- """Specify the return value of the last function frame.
- Parameters
- ----------
- value: Expr
- The function return value.
- """
+ """Specify the return value of the last function frame."""
return _ffi_api.FuncRetValue(value) # type: ignore[attr-defined] # pylint: disable=no-member
@@ -379,22 +359,12 @@ def rewriter(rewriter_mod: IRModule | type) -> PatternMatchingRewriter:
def dataflow() -> frame.BindingBlockFrame:
- """Start a dataflow binding block frame.
- Returns
- -------
- frame: frame.BindingBlockFrame
- The created ir_builder Block frame.
- """
+ """Start a dataflow binding block frame."""
return _ffi_api.Dataflow() # type: ignore[attr-defined] # pylint: disable=no-member
def output(*vars: tuple[Var]) -> None:
- """Expose the dataflow block output variables as global ones.
- Parameters
- ----------
- vars: Tuple[Var]
- The output variables of a dataflow block.
- """
+ """Expose the dataflow block output variables as global ones."""
return _ffi_api.DataflowBlockOutput(vars) # type: ignore[attr-defined] # pylint: disable=no-member
@@ -648,12 +618,7 @@ def emit_with_ty(
def SeqExpr() -> frame.SeqExprFrame: # pylint: disable=invalid-name
- """Create a SeqExpr frame.
- Returns
- -------
- res : frame.SeqExprFrame
- The result SeqExprFrame
- """
+ """Create a SeqExpr frame."""
return _ffi_api.SeqExpr() # type: ignore[attr-defined] # pylint: disable=no-member
@@ -683,22 +648,12 @@ def If(condition: Expr) -> frame.IfFrame: # pylint: disable=invalid-name
def Then() -> frame.ThenFrame: # pylint: disable=invalid-name
- """Create a then frame.
- Returns
- -------
- res : frame.ThenFrame
- The result ThenFrame.
- """
+ """Create a then frame."""
return _ffi_api.Then() # type: ignore[attr-defined] # pylint: disable=no-member
def Else() -> frame.ElseFrame: # pylint: disable=invalid-name
- """Create an else frame.
- Returns
- -------
- res : frame.ElseFrame
- The result ElseFrame.
- """
+ """Create an else frame."""
return _ffi_api.Else() # type: ignore[attr-defined] # pylint: disable=no-member
diff --git a/python/tvm/relax/script/builder/protocol.py b/python/tvm/relax/script/builder/protocol.py
new file mode 100644
index 0000000000..031ad35fec
--- /dev/null
+++ b/python/tvm/relax/script/builder/protocol.py
@@ -0,0 +1,127 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Parser-generated binding and statement operations for relax."""
+
+import builtins as _python
+
+import tvm_ffi as _ffi
+
+from tvm import ir as _ir
+from tvm import relax as _relax
+from tvm.script.ir_builder import IRBuilder as _IRBuilder
+from tvm.script.ir_builder import ir as _I
+from tvm.script.ir_builder.base import MISSING as _MISSING
+from tvm.script.ir_builder.base import BypassBind as _BypassBind
+from tvm.script.ir_builder.base import _construction_span
+from tvm.script.ir_builder.base import at as _at
+from tvm.script.ir_builder.base import source_span as _source_span
+from tvm.script.ir_builder.type_var_frame import TypeVarDecl as _TypeVarDecl
+from tvm.script.ir_builder.type_var_frame import TypeVarFrame as _TypeVarFrame
+
+from .. import builder as _builder
+from . import _ffi_api
+
+
+def bind_(
+ value=_MISSING,
+ *,
+ ty=None,
+ name=None,
+ span=None,
+ name_span=None,
+ previous=_MISSING,
+ declaration=False,
+ frame_value=False,
+):
+ """Emit a Relax binding or retain a named frame-owned value."""
+ if isinstance(value, _BypassBind):
+ return value.value
+ name_span = _source_span(span if name_span is None else name_span)
+ # Shared dtype constructors return anonymous primitive Vars. Reuse the
+ # signature's canonical symbol for declarations, while named aliases and
+ # computed primitive expressions retain ordinary Relax binding semantics.
+ if not frame_value and _ir.is_prim_var(value) and not value.name:
+ # A matching explicit annotation still denotes the same declaration.
+ # Mismatches follow the existing native binding validation below.
+ ty = None if ty is None else _builder._type(ty)
+ if ty is None or _ffi.structural_equal(ty, value.ty):
+ return _TypeVarFrame.current().resolve(name, value.ty, span=name_span)
+ if isinstance(value, _TypeVarDecl):
+ return _TypeVarFrame.current().resolve(name, value.ty, span=name_span)
+ if frame_value:
+ if isinstance(value, _python.list | _python.tuple | _ir.Array):
+ for index, item in enumerate(value):
+ bind_(
+ item,
+ name=None if name is None else f"{name}_{index}",
+ span=_source_span(span),
+ name_span=name_span,
+ frame_value=True,
+ )
+ elif isinstance(value, _ir.Var):
+ if name is not None:
+ _IRBuilder.name(name, value)
+ _at(name_span if name_span is not None else span, value)
+ return value
+ if declaration:
+ if not _ir.is_prim_var(value):
+ raise TypeError("A symbol declaration requires a concrete primitive variable")
+ if ty is not None and not _ffi.structural_equal(_builder._type(ty), value.ty):
+ raise TypeError("The symbol declaration has an incompatible type")
+ if previous is not _MISSING:
+ if not _ir.is_prim_var(previous) or not _ffi.structural_equal(previous.ty, value.ty):
+ raise TypeError("The symbol declaration has an incompatible signature dtype")
+ return previous
+ if name is not None:
+ _IRBuilder.name(name, value)
+ return _at(name_span if name_span is not None else span, value)
+ if value is _MISSING:
+ raise ValueError("Relax bindings require an initializer")
+ if isinstance(value, _I.meta_var):
+ return value.value
+ ty = None if ty is None else _builder._type(ty)
+ value = _builder._value(value, ty)
+ with _construction_span(span):
+ if isinstance(value, _relax.MatchCast):
+ if ty is not None and not _ffi.structural_equal(ty, value.ty):
+ raise TypeError("The binding annotation differs from the match-cast type")
+ result = _ffi_api.EmitMatchCastWithSpan(value.value, value.ty, name_span)
+ elif isinstance(value, _relax.Expr):
+ result = _ffi_api.EmitWithSpan(value, ty, name_span)
+ else:
+ return value
+ if name is not None:
+ _IRBuilder.name(name, result)
+ return _at(name_span if name_span is not None else span, result)
+
+
+def emit_(value, *, span=None):
+ """Emit a void expression statement."""
+ from tvm.script.ir_builder.base import BypassEmit
+
+ if isinstance(value, BypassEmit):
+ return None
+ if value is None:
+ return
+ if not isinstance(value, _relax.Expr):
+ raise TypeError(f"Unsupported expression statement value: {type(value).__name__}")
+ result = bind_(value, name="_", span=span)
+ if not isinstance(result.ty, _ir.TupleType) or len(result.ty.fields) != 0:
+ raise ValueError(
+ "Non-void expressions must be bound to a variable; "
+ f"expression of type {result.ty} was used as a statement"
+ )
diff --git a/python/tvm/relax/script/parser/__init__.py b/python/tvm/relax/script/parser/__init__.py
deleted file mode 100644
index 9795764cb4..0000000000
--- a/python/tvm/relax/script/parser/__init__.py
+++ /dev/null
@@ -1,52 +0,0 @@
-# isort: skip_file
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-# ruff: noqa: RUF005
-"""Initial impl of relax parser for sugars"""
-
-from typing import TYPE_CHECKING
-
-from tvm.relax.script.builder import * # pylint: disable=redefined-builtin
-from tvm.relax.script.builder import ir as _relax
-
-from . import parser as _parser
-from .entry import Any, Callable, Object, Prim, Shape, Tensor, Tuple, match_cast
-
-from . import dist
-from .dist import * # pylint: disable=wildcard-import,redefined-builtin
-
-if TYPE_CHECKING:
- # pylint: disable=invalid-name
- # Define prim_func and make it type check as static method
- # so most tvmscript won't trigger pylint error here.
- function = staticmethod
-else:
- from .entry import function, macro
-
-__all__ = _relax.__all__ + [
- "dist",
- "Any",
- "Callable",
- "Object",
- "Prim",
- "Shape",
- "Tensor",
- "Tuple",
- "function",
- "macro",
- "match_cast",
-]
diff --git a/python/tvm/relax/script/parser/dist.py b/python/tvm/relax/script/parser/dist.py
deleted file mode 100644
index 71030be905..0000000000
--- a/python/tvm/relax/script/parser/dist.py
+++ /dev/null
@@ -1,106 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-# pylint: disable=redefined-builtin,missing-docstring, invalid-name, unused-import, redefined-outer-name
-# ruff: noqa: F401
-
-from typing import Any, Optional, Union
-
-from tvm.ir import Range
-from tvm.relax import TensorType
-from tvm.relax.distributed import DeviceMesh, DTensorType, Placement, device_mesh
-from tvm.relax.script.builder.distributed import (
- annotate_sharding,
- call_tir,
- call_tir_local_view,
- const,
- redistribute,
- redistribute_replica_to_shard,
-)
-from tvm.script.ir_builder import IRBuilder
-from tvm.script.ir_builder.ir import IRModuleFrame
-from tvm.tirx import Expr
-
-from .entry import TensorProxy, TypeProxy
-
-############################### R.DTensor ###############################
-
-
-class DTensorProxy(TypeProxy):
- tensor_ty_proxy: TensorProxy
- device_mesh: DeviceMesh
- placement: Placement
-
- def __init__(
- self,
- tensor_ty_proxy: TensorProxy,
- device_mesh: DeviceMesh,
- placement: Placement,
- ) -> None:
- self.device_mesh = device_mesh
- self.placement = placement
- self.tensor_ty_proxy = tensor_ty_proxy
- super().__init__()
-
- def get_symbolic_vars(self) -> set[str]:
- return self.tensor_ty_proxy.get_symbolic_vars()
-
- def as_ty(self, dict_globals: dict[str, Any] | None = None) -> DTensorType:
- return DTensorType(
- self.tensor_ty_proxy.as_ty(dict_globals),
- self.device_mesh,
- self.placement,
- )
-
-
-def DTensor(
- shape: list[Expr | str] | None = None,
- dtype: str | None = None,
- device_mesh: DeviceMesh | str = DeviceMesh([], Range(0, 1)),
- placement: Placement | str = "",
- *,
- ndim: int = -1,
-) -> DTensorProxy:
- # scalar tensor case
- if shape is not None and len(shape) == 0:
- shape = []
- if isinstance(shape, str) and dtype is None:
- dtype = shape
- shape = None
-
- if shape is not None and not isinstance(shape, tuple | list):
- raise ValueError(f"shape must be a list or tuple, but got: {shape}")
- if isinstance(device_mesh, str):
- if not IRBuilder.is_in_scope():
- return (
- DTensorProxy(
- TensorProxy(shape, dtype, None, ndim), DeviceMesh([], Range(0, 1)), ""
- ),
- )
- name, index = device_mesh.split("[")
- index = int(index[:-1])
- frames = IRBuilder.current().frames
- for f in frames:
- if isinstance(f, IRModuleFrame):
- device_mesh = f.global_infos[name][index]
- break
- assert isinstance(device_mesh, DeviceMesh)
- if isinstance(placement, str):
- placement = Placement.from_text(placement)
- return DTensorProxy(TensorProxy(shape, dtype, None, ndim), device_mesh, placement)
-
-
-__all__ = ["DTensor", "device_mesh"]
diff --git a/python/tvm/relax/script/parser/entry.py b/python/tvm/relax/script/parser/entry.py
deleted file mode 100644
index 20605840f6..0000000000
--- a/python/tvm/relax/script/parser/entry.py
+++ /dev/null
@@ -1,521 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-# pylint: disable=missing-docstring, invalid-name
-import inspect
-from collections.abc import Callable as _Callable
-from typing import Any, TypeVar
-
-import tvm
-from tvm.ir import PrimType
-from tvm.relax import (
- AnyType,
- Expr,
- Function,
- FuncType,
- SeqExpr,
- ShapeExpr,
- ShapeType,
- TensorType,
- TupleType,
- Type,
-)
-from tvm.relax.expr import Var
-from tvm.relax.script import builder as R
-from tvm.runtime import ObjectConvertible
-from tvm.script.ir_builder.ir import lookup_vdevice
-from tvm.script.parser._core import doc, parse, utils
-from tvm.script.parser.core.entry import scan_macro
-from tvm.script.parser.core.parser import Parser, ScriptMacro
-
-FType = TypeVar("FType", bound=_Callable)
-
-############################## R.function ##############################
-
-
-# this formulation allows us to support having @R.function
-# appear as a decorator by itself or to have optional arguments
-# like @R.function(pure=False)
-def function(
- f: FType | None = None, pure: bool = True, private: bool = False, check_well_formed=True
-) -> Function | FType:
- # pylint: disable=unused-argument
- # (pure and private aren't used here, but are used later in parsing)
-
- # need to inspect the stack first because is_defined_in_class expects the outer class
- # to be in a particular position in the stack
- orig_stack = inspect.stack()
-
- def decorator_wrapper(f):
- if not inspect.isfunction(f):
- raise TypeError(f"Expect a function, but got: {f}")
- if utils.is_defined_in_class(orig_stack, f):
- return f
- return parse(f, utils.inspect_function_capture(f), check_well_formed=check_well_formed)
-
- if f is not None:
- # if there are no optional args given, this will directly invoke the wrapper
- return decorator_wrapper(f)
- else:
- # if there is a optional arg given, it returns the wrapper function
- # as a new decorator and applies it
- setattr(decorator_wrapper, "dispatch_token", "relax")
- return decorator_wrapper
-
-
-setattr(function, "dispatch_token", "relax")
-
-
-############################## R.macro ##############################
-
-
-class RelaxMacro(ScriptMacro):
- """Specialization of the ScriptMacro class for Relax."""
-
- def parse_macro(self, parser: Parser) -> Expr:
- macro_def = self.get_macro_def()
- ret_value = None
-
- with R.SeqExpr() as seq:
- for idx, stmt in enumerate(macro_def.body):
- # Normally, a "return" statement is only allowed in a R.function. We don't
- # want to parse the macro's body as if it was a body of a function, because
- # the latter imposes some constraints that we want to avoid.
- # At the same time, we want to use "return" to indicate the value of the
- # macro (since in Relax everything is an expression), so add special handling
- # of "return".
- if isinstance(stmt, doc.Return):
- ret_value = parser.eval_expr(stmt.value)
- if idx + 1 != len(macro_def.body):
- parser.report_error(macro_def, "'return' should be the last statement")
- break
- parser.visit(stmt)
-
- if ret_value is None:
- parser.report_error(macro_def, "Macros must end with a return statement")
-
- return SeqExpr(seq.binding_blocks, ret_value)
-
-
-def macro(*args, hygienic: bool = True) -> _Callable:
- """Decorator for macro definitions.
-
- Parameters
- ----------
- hygienic: bool
- Specifies whether the macro is hygienic or not.
- A macro is hygienic if all symbols used in the macro's body are resolved
- to values from the location of the macro definition. A non-hygienic macro
- will have its symbols resolved to values at the time of the macro's use.
- """
-
- def _decorator(func: _Callable) -> ScriptMacro:
- source, closure_vars = scan_macro(func, utils.inspect_function_capture(func))
- obj = RelaxMacro(source, closure_vars, func, hygienic)
-
- def wrapper(*args, **kwargs):
- return obj(*args, **kwargs)
-
- return wrapper
-
- if len(args) == 0:
- return _decorator
- if len(args) == 1 and inspect.isfunction(args[0]):
- return _decorator(args[0])
-
- raise ValueError(
- "Invalid use of R.macro. Usage: @R.macro, @R.macro(), @R.macro(hygienic=[True|False])"
- )
-
-
-############################# Type ##############################
-
-
-class TypeProxy(ObjectConvertible):
- def as_ty(self, dict_globals: dict[str, Any] | None = None) -> Type:
- raise NotImplementedError()
-
- def get_symbolic_vars(self) -> set[str]:
- return {}
-
- def asobject(self):
- return self.as_ty(None)
-
-
-############################### R.Any ################################
-
-
-class AnyProxy(TypeProxy):
- """The proxy for AnyType.
-
- Parameters
- ----------
- values : Optional[List[Expr]]
- The symbolic shape values if known.
-
- ndim : Optional[int]
- The size of the shape.
- """
-
- def __init__(self) -> None:
- pass
-
- def get_symbolic_vars(self) -> set[str]:
- return set()
-
- def as_ty(self, dict_globals: dict[str, Any] | None = None) -> AnyType:
- return AnyType()
-
-
-def Any() -> AnyProxy:
- return AnyProxy()
-
-
-ObjectProxy = AnyProxy
-
-
-def Object() -> AnyProxy:
- return AnyProxy()
-
-
-############################### R.Tensor ###############################
-
-
-def _eval_shape(expr: str | Expr, dict_globals: dict[str, Any] | None) -> Expr:
- if isinstance(expr, str):
- code = compile(expr, "<string>", "eval")
- return eval(code, dict_globals or {}) # pylint: disable=eval-used
- else:
- return expr
-
-
-class TensorProxy(TypeProxy):
- shape: list[str | Expr] | None
- dtype: str
- vdevice: str | None
- ndim: int
-
- def __init__(
- self,
- shape: list[Expr | str] | Expr | None = None,
- dtype: str | None = None,
- vdevice: str | None = None,
- ndim: int = -1,
- ) -> None:
- if isinstance(shape, Expr):
- if not isinstance(shape, ShapeExpr | Var):
- raise ValueError(
- "When the shape is an Expr, it must be a ShapeExpr or a Var with ShapeExpr "
- f"value. But got: {shape} with type: {type(shape)}"
- )
- if isinstance(shape, Var) and not isinstance(shape.ty, ShapeType):
- raise ValueError(
- "When the shape is a Var, it must have shape ty. But got "
- f"{shape} with ty: {shape.ty}"
- )
- self.shape = shape
- self.dtype = dtype
- self.vdevice = vdevice
- self.ndim = ndim
-
- def get_symbolic_vars(self) -> set[str]:
- if self.shape is None or isinstance(self.shape, Expr):
- return {}
- else:
- return {s for s in self.shape if isinstance(s, str) and s.isidentifier()}
-
- def as_ty(self, dict_globals: dict[str, Any] | None = None) -> TensorType:
- vdev = self.vdevice
- if isinstance(self.vdevice, str):
- if ":" in self.vdevice:
- split_vdev = self.vdevice.split(":")
- vdev = lookup_vdevice(split_vdev[0], int(split_vdev[1]))
- else:
- vdev = lookup_vdevice(self.vdevice, 0)
-
- if self.shape is None:
- return TensorType(None, self.dtype, vdev, self.ndim)
- elif isinstance(self.shape, ShapeExpr | Var):
- return TensorType(self.shape, self.dtype, vdev, self.ndim)
- else:
- if dict_globals is None and any([isinstance(s, str) for s in self.shape]):
- raise ValueError(
- "String-defined shape expr is only allowed when parsing function parameters "
- "and return annotations for TVMScript."
- )
- shape = [_eval_shape(s, dict_globals) for s in self.shape]
- return TensorType(shape, self.dtype, vdev, self.ndim)
-
-
-def Tensor(
- shape: list[Expr | str] | Expr | None = None,
- dtype: str | None = None,
- vdevice: str | None = None,
- ndim: int = -1,
-) -> TensorProxy:
- # scalar tensor case
- if shape is not None and not isinstance(shape, Var) and len(shape) == 0:
- shape = []
- if isinstance(shape, str) and dtype is None:
- dtype = shape
- shape = None
-
- if shape is not None and not isinstance(shape, tuple | list) and not isinstance(shape, Expr):
- raise ValueError(f"shape must be a list/tuple or an Expr, but got: {shape}")
- return TensorProxy(shape, dtype, vdevice, ndim)
-
-
-############################## R.Callable ##############################
-
-
-class CallableProxy(TypeProxy):
- params: list[TypeProxy]
- ret: TypeProxy
- purity: bool
- derive_func: str | tvm.ir.EnvFunc | None
-
- """Function type.
-
- A function type consists of a list of type parameters to enable
- the definition of generic functions,
- a set of type constraints which we omit for the time being,
- a sequence of argument types, the purity of the function, and a return type.
-
- Parameters
- ----------
- params : List[TypeProxy]
- The argument TypeProxy
-
- ret : TypeProxy
- The return TypeProxy.
-
- purity : bool
- Whether the callable is pure.
-
- derive_func: Optional[Union[str, tvm.ir.EnvFunc]]
- The derivation function to determine the output Type,
- based on the arguments provided to the function. The
- specified function should be accessible using
- `tvm.get_global_func`, and should have a signature
- `Callable[[relax.Call, relax.BlockBuilder], relax.Type]`.
-
- """
-
- def __init__(
- self,
- params: TypeProxy | list[TypeProxy] | None = None,
- ret: TypeProxy | None = None,
- purity: bool | None = None,
- derive_func: str | tvm.ir.EnvFunc | None = None,
- ) -> None:
- if params is None:
- self.params = params
- else:
- if not isinstance(params, list | tuple):
- params = [params]
- # convert `R.Callable` to `R.Callable()`
- self.params = [param() if callable(param) else param for param in params]
-
- # Mimic the C++ defaults, where an opaque function is assumed
- # to be impure, and a non-opaque function is assumed to be
- # pure.
- if purity is None:
- purity = params is not None
-
- self.ret = ret() if callable(ret) else ret
- self.purity = purity
- self.derive_func = derive_func
-
- def get_symbolic_vars(self) -> set[str]:
- if self.params is None:
- return set()
- else:
- return set().union(*[p.get_symbolic_vars() for p in self.params])
-
- def as_ty(self, dict_globals: dict[str, Any] | None = None) -> FuncType:
- if self.ret is None:
- ret = None
- else:
- ret = self.ret.as_ty(dict_globals)
-
- if self.params is None:
- params = None
- else:
- params = [param.as_ty(dict_globals) for param in self.params]
-
- if params is None:
- return FuncType.opaque_func(ret=ret, derive_func=self.derive_func, purity=self.purity)
- else:
- return FuncType(params, ret, purity=self.purity)
-
-
-def Callable(
- params: TypeProxy | list[TypeProxy] | None = None,
- ret: TypeProxy | None = None,
- purity: bool | None = None,
- derive_func: str | tvm.ir.EnvFunc | None = None,
-) -> CallableProxy:
- return CallableProxy(params, ret, purity=purity, derive_func=derive_func)
-
-
-############################### R.Tuple ################################
-
-
-class TupleProxy(TypeProxy):
- fields: list[TypeProxy]
- """The type of tuple values.
-
- Parameters
- ----------
- fields : List[TypeProxy]
- The fields in the tuple
- """
-
- def __init__(
- self,
- *fields: list[TypeProxy],
- ) -> None:
- if len(fields) == 1 and isinstance(fields[0], tuple | list):
- fields = fields[0]
- # convert `R.Tensor` to `R.Tensor()`
- self.fields = [field() if callable(field) else field for field in fields]
-
- def get_symbolic_vars(self) -> set[str]:
- return set().union(*[f.get_symbolic_vars() for f in self.fields])
-
- def as_ty(self, dict_globals: dict[str, Any] | None = None) -> TupleType:
- fields = [field.as_ty(dict_globals) for field in self.fields]
- return TupleType(fields)
-
-
-def Tuple(*fields: list[TypeProxy]) -> TupleProxy:
- return TupleProxy(*fields)
-
-
-############################### R.Shape ################################
-
-
-class ShapeProxy(TypeProxy):
- values: list[Expr] | None
- ndim: int
- """The type of shape values.
-
- Parameters
- ----------
- values : Optional[List[Expr]]
- The symbolic shape values if known.
-
- ndim : Optional[int]
- The size of the shape.
- """
-
- def __init__(
- self,
- values: list[Expr] | None = None,
- ndim: int = -1,
- ) -> None:
- self.values = values
- self.ndim = ndim
-
- def get_symbolic_vars(self) -> set[str]:
- if self.values is None:
- return set()
- else:
- return {v for v in self.values if isinstance(v, str) and v.isidentifier()}
-
- def as_ty(self, dict_globals: dict[str, Any] | None = None) -> ShapeType:
- values = [_eval_shape(v, dict_globals) for v in self.values] if self.values else None
- return ShapeType(values, self.ndim)
-
-
-def Shape(values: list[Expr] | None = None, ndim: int = -1) -> ShapeProxy:
- return ShapeProxy(values, ndim)
-
-
-################################ R.Prim ################################
-
-
-class PrimProxy(TypeProxy):
- dtype: str
-
- """The type of TIR-representable values.
-
- Parameters
- ----------
- dtype : str
- The data type.
-
- """
-
- def __init__(
- self,
- dtype: str,
- ) -> None:
- self.dtype = dtype
-
- def get_symbolic_vars(self) -> set[str]:
- return set()
-
- def as_ty(self, dict_globals: dict[str, Any] | None = None) -> PrimType:
- return PrimType(self.dtype)
-
-
-def Prim(
- dtype: str,
-) -> PrimProxy:
- return PrimProxy(dtype)
-
-
-############################ R.match_cast #############################
-class MatchCastPair:
- value: Expr
- ty: Type
-
- def __init__(self, value: Expr, ty: Type) -> None:
- self.value = value
- self.ty = ty
-
-
-def match_cast(value: Expr, ty: Type):
- ty = _normalize_ty(ty)
-
- if value is None:
- raise ValueError("value of match_cast cannot be None")
- if ty is None:
- raise ValueError("ty of match_cast cannot be None")
- return MatchCastPair(value, ty)
-
-
-def _normalize_ty_proxy(annotation) -> TypeProxy:
- if annotation is None:
- return TupleProxy([])
- elif callable(annotation):
- annotation = annotation()
- if tvm.ir.is_prim_expr(annotation):
- return PrimProxy(annotation.ty.dtype)
- return annotation
- elif isinstance(annotation, TypeProxy):
- return annotation
- else:
- raise TypeError(f"Expected TypeProxy but got {type(annotation)}.")
-
-
-def _normalize_ty(ty, dict_globals: dict[str, Any] | None = None) -> Type:
- if isinstance(ty, Type):
- return ty
- else:
- proxy = _normalize_ty_proxy(ty)
- return proxy.as_ty(dict_globals)
diff --git a/python/tvm/relax/script/parser/parser.py b/python/tvm/relax/script/parser/parser.py
deleted file mode 100644
index 083aa3ea93..0000000000
--- a/python/tvm/relax/script/parser/parser.py
+++ /dev/null
@@ -1,506 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-# pylint: disable=missing-docstring, unused-argument
-
-import functools
-import numbers
-from typing import Any
-
-import numpy as np
-import tvm_ffi
-
-import tvm
-from tvm import relax
-from tvm.ir import GlobalVar
-from tvm.relax import Expr, Type
-from tvm.relax.script import builder as R
-from tvm.relax.script.builder.frame import BindingBlockFrame
-from tvm.relax.utils import convert_to_expr
-from tvm.script.ir_builder import ir as I
-from tvm.script.ir_builder.base import IRBuilder
-from tvm.script.parser._core import Parser, collect_signature_type_vars, dispatch, doc
-from tvm.tirx.script import builder as T
-
-from .entry import (
- MatchCastPair,
- PrimProxy,
- TypeProxy,
- _normalize_ty,
- _normalize_ty_proxy,
-)
-
-relax.Expr._dispatch_type = relax.Expr # pylint: disable=protected-access
-dispatch.register_op(relax.Expr, doc.GtE, 0)(lambda lhs, rhs: lhs >= rhs)
-dispatch.register_op(relax.Expr, doc.Gt, 0)(lambda lhs, rhs: lhs > rhs)
-dispatch.register_op(relax.Expr, doc.LtE, 0)(lambda lhs, rhs: lhs <= rhs)
-dispatch.register_op(relax.Expr, doc.Lt, 0)(lambda lhs, rhs: lhs < rhs)
-
-
-def bind_assign_value(
- self: Parser,
- node: doc.expr,
- var_name: str,
- value: Any,
- anno_ty: Type | None = None,
- symbolic_declarations: set[str] | None = None,
-) -> Any:
- var_table = self.var_table.get()
-
- # Primitive assignments emit Relax bindings by default, reversing the old default.
- # Explicit I.meta_var values and literal T.dtype() declarations are the two
- # parser-time exceptions.
- if isinstance(value, I.meta_var):
- return value.value
-
- if tvm.ir.is_prim_var(value) and var_name in (symbolic_declarations or set()):
- if anno_ty is not None and not tvm_ffi.structural_equal(anno_ty, value.ty):
- self.report_error(
- node,
- f"Expected the declared symbolic variable to have type {anno_ty}, "
- f"but got {value.ty}",
- )
- if value.name and var_name != value.name:
- self.report_error(
- node,
- "Cannot define PrimType values with different names. The LHS of binding "
- "must have the same name provided in the RHS.",
- )
- if var_name in var_table:
- prev_value = var_table[var_name]
- if not tvm.ir.is_prim_var(prev_value):
- self.report_error(
- node,
- "Cannot redefine a non-PrimType object to a PrimType value. Please "
- "define the PrimType value with another name.",
- )
- if prev_value.ty != value.ty:
- self.report_error(
- node,
- f"Expected the same dtype for PrimType values but got "
- f"{value.ty} vs {prev_value.ty}",
- )
- value = prev_value
- IRBuilder.name(var_name, value)
- return value
-
- if isinstance(value, tuple):
- value = convert_to_expr(value)
- if isinstance(value, numbers.Number | np.bool_):
- if isinstance(anno_ty, tvm.ir.PrimType):
- value = relax.prim_value(value, dtype=anno_ty.dtype)
- else:
- value = R.const(value)
-
- if isinstance(value, relax.Expr):
- var = R.emit(value, anno_ty)
- elif isinstance(value, MatchCastPair):
- if anno_ty is not None and not tvm_ffi.structural_equal(anno_ty, value.ty):
- self.report_error(
- node, "Cannot specify inconsistent annotation for a match cast pair. "
- )
- var = R.emit_match_cast(value.value, value.ty)
- else:
- return value
-
- IRBuilder.name(var_name, var)
- return var
-
-
-def is_symbolic_var_declaration(node: doc.expr) -> bool:
- """Return whether an expression is literal ``T.dtype()`` declaration syntax.
-
- Detection is syntactic so declaration semantics do not depend on evaluating its RHS.
- """
- if not (
- isinstance(node, doc.Call)
- and not node.args
- and not node.keywords
- and isinstance(node.func, doc.Attribute)
- and isinstance(node.func.value, doc.Name)
- and node.func.value.id == "T"
- ):
- return False
- constructor = getattr(T, node.func.attr, None)
- return isinstance(constructor, T.DtypeConstructor) or constructor is T.bool
-
-
-def collect_symbolic_var_declaration_nodes(
- target: doc.expr, value: doc.expr
-) -> dict[str, doc.expr]:
- """Pair targets with literal ``T.dtype()`` declarations.
-
- Declaration-vs-binding is per target, allowing tuple unpacking to mix both.
- """
- if isinstance(target, doc.Name):
- return {target.id: value} if is_symbolic_var_declaration(value) else {}
- if isinstance(target, doc.Tuple | doc.List) and isinstance(value, doc.Tuple | doc.List):
- if len(target.elts) != len(value.elts):
- return {}
- declarations = {}
- for lhs, rhs in zip(target.elts, value.elts):
- declarations.update(collect_symbolic_var_declaration_nodes(lhs, rhs))
- return declarations
- return {}
-
-
-def collect_symbolic_var_declarations(target: doc.expr, value: doc.expr) -> set[str]:
- """Collect assignment targets whose matching RHS is ``T.dtype()``."""
- return set(collect_symbolic_var_declaration_nodes(target, value))
-
-
-def eval_ty_proxy(self: Parser, node: doc.expr) -> TypeProxy:
- try:
- annotation = self.eval_expr(node)
- return _normalize_ty_proxy(annotation)
- except Exception as err: # pylint: disable=broad-except
- self.report_error(node, err)
- raise
-
-
-def eval_ty(self: Parser, node: doc.expr, eval_str: bool = False) -> Type:
- var_table = self.var_table.get() if eval_str else None
- try:
- ty = self.eval_expr(node)
- return _normalize_ty(ty, var_table)
- except Exception as err: # pylint: disable=broad-except
- self.report_error(node, err)
- raise
-
-
-def is_called(node: Any, func_name: str) -> bool:
- # Check if it calls into a func
- if isinstance(node, doc.Call):
- # Recursive call was found
- if isinstance(node.func, doc.Name) and node.func.id == func_name:
- return True
- elif isinstance(node, list | tuple):
- for stmt in node:
- if is_called(stmt, func_name):
- return True
- elif isinstance(node, doc.AnnAssign | doc.Assign | doc.Return | doc.Expr):
- return is_called(node.value, func_name)
- elif isinstance(node, doc.With):
- return is_called(node.body, func_name)
- elif isinstance(node, doc.If):
- smts = []
- if node.body is not None:
- smts = smts + list(node.body)
- if node.orelse is not None:
- smts = smts + list(node.orelse)
- return is_called(smts, func_name)
- return False
-
-
-def is_recursive(node: doc.FunctionDef) -> bool:
- # Check if it is a recursive function
- for stmt in node.body:
- if is_called(stmt, node.name):
- return True
- return False
-
-
-def collect_symbolic_var_from_prelude(
- self: Parser, node: doc.FunctionDef, symbolic_vars: dict[str, tvm.ir.Var]
-) -> dict[str, tvm.ir.Var]:
- prelude_vars = {}
- for stmt in node.body:
- if isinstance(stmt, doc.Assign) and len(stmt.targets) == 1:
- declarations = collect_symbolic_var_declaration_nodes(stmt.targets[0], stmt.value)
- for name, value_node in declarations.items():
- if name not in symbolic_vars:
- continue
- declared_var = self.eval_expr(value_node)
- if tvm.ir.is_prim_var(declared_var) and declared_var.ty == symbolic_vars[name].ty:
- prelude_vars[name] = declared_var
-
- return {**symbolic_vars, **prelude_vars}
-
-
-def collect_symbolic_var_from_params(self: Parser, node: doc.FunctionDef) -> None:
- symbolic_vars = collect_signature_type_vars(self, node)
- prim_params = set()
- with self.var_table.with_frame():
- for var_name, var in symbolic_vars.items():
- self.var_table.add(var_name, var)
- for arg in node.args.args:
- if arg.annotation is None:
- self.report_error(arg, "Type annotation is required for function parameters.")
- param_ty_proxy = eval_ty_proxy(self, arg.annotation)
-
- for var_name in param_ty_proxy.get_symbolic_vars():
- if var_name not in prim_params and var_name not in symbolic_vars:
- symbolic_vars[var_name] = tvm.ir.Var(var_name, "int64")
-
- if isinstance(param_ty_proxy, PrimProxy):
- temp_param = tvm.ir.Var(arg.arg, param_ty_proxy.as_ty())
- self.var_table.add(arg.arg, temp_param)
- prim_params.add(arg.arg)
-
- # Prelude declarations select each scope symbol's canonical Var object.
- # Per-assignment declaration classification separately controls emission.
- symbolic_vars = collect_symbolic_var_from_prelude(self, node, symbolic_vars)
-
- # Define symbolic vars to the current var_table frame
- for var_name, var in symbolic_vars.items():
- self.var_table.add(var_name, var, allow_shadowing=False)
-
-
-def parse_function_params(self: Parser, node: doc.FunctionDef, param_factory) -> list[tvm.ir.Var]:
- collect_symbolic_var_from_params(self, node)
- params = []
- for arg in node.args.args:
- if arg.annotation is None:
- self.report_error(arg, "Type annotation is required for function parameters.")
- param_ty = eval_ty(self, arg.annotation, eval_str=True)
- param = param_factory(arg.arg, param_ty)
- self.var_table.add(arg.arg, param)
- params.append(param)
-
- return params
-
-
-@dispatch.register(token="relax", type_name="FunctionDef")
-def visit_function_def(self: Parser, node: doc.FunctionDef) -> None:
- is_inner_function = self.inside_function
- self.inside_function = True
-
- # reserve a var for local function
- func_val = self.var_table.get().get(node.name)
- if func_val is None and is_recursive(node):
- with self.var_table.with_frame():
- provisional_params = parse_function_params(self, node, tvm.ir.Var)
- if node.returns is None:
- ret_ty = relax.TupleType([])
- else:
- ret_ty = eval_ty(self, node.returns, eval_str=True)
- params_ty = [param.ty for param in provisional_params]
- # created a var for the local function, the same var could be used for recursive call
- local_func_var = tvm.ir.Var(node.name, relax.FuncType(params_ty, ret_ty))
- self.var_table.add(node.name, local_func_var)
-
- purity = find_decorator_annotation(node, "pure")
- # treat the function as private if we are inside another function
- # or if it has a privacy annotation
- privacy = is_inner_function or find_decorator_annotation(node, "private", default=False)
-
- with self.var_table.with_frame():
- with self.with_dispatch_token("relax"):
- with R.function(is_pure=purity, is_private=privacy):
- R.func_name(node.name)
- parse_function_params(self, node, R.arg)
-
- if node.returns is not None:
- ann_ty = eval_ty(self, node.returns, eval_str=True)
- R.func_ret_ty(ann_ty)
-
- for stmt in node.body:
- if isinstance(stmt, doc.FunctionDef):
- if not stmt.decorator_list:
- self.report_error(stmt, "Function must be decorated")
- dec = self.eval_expr(stmt.decorator_list[-1])
- # inline prim_func was found
- if dec.dispatch_token == "tirx":
- self.report_error(stmt, "inline prim_func is disallowed in Relax IR")
-
- self.visit_body(node.body)
- self.inside_function = is_inner_function
-
-
-def find_decorator_annotation(node: doc.FunctionDef, annotation: str, default: bool = True) -> bool:
- """
- Check the value of given annotation (argument name) in the function decorator.
- Returns the value of the annotation if present, otherwise giving the default value.
- """
- # look for the named argument in the function decorator
- for dec in node.decorator_list:
- if not isinstance(dec, doc.Call) or dec.func.attr != "function":
- continue
- for keyword in dec.keywords:
- if keyword.arg == annotation:
- return keyword.value.value
- return default
-
-
-@dispatch.register(token="relax", type_name="tvm_declare_function")
-def visit_tvm_declare_function(self: Parser, node: doc.FunctionDef) -> GlobalVar:
- with self.var_table.with_frame():
- params = parse_function_params(self, node, tvm.ir.Var)
-
- if node.returns is None:
- # Use AnyType as unknown return type
- # NOTE: Cannot use VoidType here because the return type can be refined later.
- ret_ty = relax.AnyType()
- else:
- ret_ty = eval_ty(self, node.returns, eval_str=True)
- is_pure = find_decorator_annotation(node, "pure")
-
- func_signature = relax.Function.create_empty(params, ret_ty, is_pure=is_pure)
- return I.decl_function(node.name, func_signature)
-
-
-@dispatch.register(token="relax", type_name="pre_visit_local_function")
-def pre_visit_local_function(self: Parser, node: doc.Expr) -> None:
- ir_builder = IRBuilder()
- ir_builder.__enter__()
-
-
-@dispatch.register(token="relax", type_name="post_visit_local_function")
-def post_visit_local_function(self: Parser, node: doc.Expr) -> None:
- ir_builder = IRBuilder.current()
- result = ir_builder.get()
- ir_builder.__exit__(None, None, None)
- # reuse var if it is reserved
- reserved_var = self.var_table.get().get(node.name)
- if reserved_var is not None:
- var = R.emit_var_binding(relax.VarBinding(reserved_var, result))
- else:
- var = R.emit(result)
- IRBuilder.name(node.name, var)
- self.var_table.add(node.name, var, allow_shadowing=False)
-
-
-@dispatch.register(token="relax", type_name="Expr")
-def visit_expr_stmt(self: Parser, node: doc.Expr) -> None:
- value = self.eval_expr(node.value)
- if isinstance(value, relax.Expr):
- var = R.emit(value)
- IRBuilder.name("_", var)
- is_void_value = isinstance(var.ty, relax.TupleType) and len(var.ty.fields) == 0
-
- if not is_void_value:
- self.report_error(
- node,
- f"Non-void relax expressions must be bound to a variable, "
- f"but expression of type {var.ty} was used as a statement.",
- )
-
- elif value is not None:
- self.report_error(node, f"Unsupported Expr stmt type {value}.")
-
-
-@dispatch.register(token="relax", type_name="arguments")
-def visit_arguments(self: Parser, node: doc.arguments) -> None:
- arg: doc.arg
- for arg in node.args:
- if arg.annotation is None:
- self.report_error(arg, "Type annotation is required for function parameters.")
- param_ty = eval_ty(self, arg.annotation, eval_str=True)
- param = R.arg(arg.arg, param_ty)
-
- self.var_table.add(arg.arg, param)
-
-
-@dispatch.register(token="relax", type_name="tvm_annotation")
-def visit_tvm_annotation(self: Parser, node: doc.expr) -> Type:
- return eval_ty(self, node, eval_str=False)
-
-
-@dispatch.register(token="relax", type_name="With")
-def visit_with(self: Parser, node: doc.With) -> None:
- # Currently only `with R.dataflow()` is supported
- if len(node.items) != 1:
- self.report_error(node, "Only one item is allowed.")
- item = node.items[0]
- if item.optional_vars is not None:
- self.report_error(
- item.context_expr,
- "Relax syntax doesn't allow binding expressions in `with` to variables",
- )
- frame = self.eval_expr(item.context_expr)
- with self.var_table.with_frame():
- with frame:
- self.visit(node.body)
- if isinstance(frame, BindingBlockFrame) and frame.is_dataflow:
- output_vars = frame.output_vars
- for var in output_vars:
- self.var_table.add(var.name, var, allow_shadowing=True)
-
-
-@dispatch.register(token="relax", type_name="Assign")
-def visit_assign(self: Parser, node: doc.Assign) -> None:
- if len(node.targets) != 1:
- self.report_error(node, "Consequential assignments like 'a = b = c' are not supported.")
- lhs = node.targets[0]
- rhs = self.eval_expr(node.value)
- self.eval_assign(
- target=lhs,
- source=rhs,
- bind_value=functools.partial(
- bind_assign_value,
- symbolic_declarations=collect_symbolic_var_declarations(lhs, node.value),
- ),
- allow_shadowing=True,
- )
-
-
-@dispatch.register(token="relax", type_name="AnnAssign")
-def visit_ann_assign(self: Parser, node: doc.AnnAssign) -> None:
- lhs = node.target
- rhs = self.eval_expr(node.value)
- anno_ty = self.visit_tvm_annotation(node.annotation)
- self.eval_assign(
- target=lhs,
- source=rhs,
- bind_value=functools.partial(
- bind_assign_value,
- anno_ty=anno_ty,
- symbolic_declarations=collect_symbolic_var_declarations(lhs, node.value),
- ),
- allow_shadowing=True,
- )
-
-
-@dispatch.register(token="relax", type_name="Return")
-def visit_return(self: Parser, node: doc.Assign) -> None:
- value = self.eval_expr(node.value)
- value = convert_to_expr(value)
- R.func_ret_value(value)
-
-
-@dispatch.register(token="relax", type_name="If")
-def visit_if(self: Parser, node: doc.If) -> None:
- if node.orelse is None:
- raise ValueError("Else statements are required for relax dialect.")
- with R.If(self.eval_expr(node.test)) as if_frame:
- with self.var_table.with_frame():
- with R.Then():
- self.visit_body(node.body)
- with self.var_table.with_frame():
- with R.Else():
- self.visit_body(node.orelse)
- self.var_table.add(if_frame.var_name, if_frame.var, allow_shadowing=True)
-
-
-@dispatch.register(token="relax", type_name="enter_token")
-def enter_token(self: Parser) -> dict[str, Any]:
- def relax_call(self, *args) -> Expr:
- args = [convert_to_expr(arg) if isinstance(arg, tuple) else arg for arg in args]
-
- if all(isinstance(x, Expr) for x in args):
- return relax.Call(self, args)
- arg_types = [type(x) for x in args]
- raise RuntimeError(f"Do not know how to handle GlobalVar.__call__ for types {arg_types}")
-
- context = {"GlobalVar.__call__": GlobalVar.__call__}
- GlobalVar.__call__ = relax_call
- return context
-
-
-@dispatch.register(token="relax", type_name="exit_token")
-def exit_token(self: Parser, context: dict[str, Any]) -> None:
- assert "GlobalVar.__call__" in context
- GlobalVar.__call__ = context.get("GlobalVar.__call__")
diff --git a/python/tvm/script/__init__.py b/python/tvm/script/__init__.py
index 940739df07..c1ee933b69 100644
--- a/python/tvm/script/__init__.py
+++ b/python/tvm/script/__init__.py
@@ -14,55 +14,11 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-"""
-TVMScript public namespace.
-
-Dialect resolution mechanism
-----------------------------
-
-``tvm.script`` is a virtual namespace: dialect names like ``tirx`` and
-``relax`` are not bound as static attributes here. Instead:
-
-- ``register_dialect(name, module_path)`` writes an entry to
- ``_DIALECT_REGISTRY: dict[str, str]``. Each in-tree dialect's
- ``__init__.py`` calls this on import (e.g., ``tvm.tirx.__init__.py``
- calls ``tvm.script.register_dialect("tirx", "tvm.tirx.script")``).
- Out-of-tree dialects can register themselves the same way.
-
-- ``__getattr__(name)`` (PEP 562) fires on missing attribute access.
- If ``name`` is in ``_DIALECT_REGISTRY``, the listed module is imported
- and cached as a normal module attribute. Subsequent accesses
- skip ``__getattr__`` (cached in ``globals()``).
-
-- Subpackages ``tvm.script.parser``, ``tvm.script.ir_builder``, etc.
- each define their own ``__getattr__`` that consults the SAME
- ``_DIALECT_REGISTRY`` and appends their suffix. So
- ``tvm.script.parser.tirx`` resolves to ``tvm.tirx.script.parser`` via
- the dialect registry + ``.parser`` suffix.
-
-- For deep statement-form imports like
- ``from tvm.script.parser.tirx.entry import ObjectProxy``, PEP 562's
- ``__getattr__`` is not enough — it only handles one-level
- ``from X import Y``. A ``sys.meta_path`` finder (see
- ``_DialectRedirectFinder``) intercepts the import machinery to
- register the real module under the legacy name in ``sys.modules``,
- so subsequent attribute walks resolve correctly.
-
-Each dialect's ``tvm.<dialect>.script`` package MUST expose ``parser``,
-``ir_builder``, and (where applicable) ``printer`` as submodules. This
-convention is what makes the suffix-append redirect work uniformly.
-IR is foundational (script depends on ir) and is NOT a dialect; its
-script handlers live in the shared core, not via this registry.
-
-Bootstrap order
----------------
-
-``python/tvm/__init__.py`` imports ``tvm.script`` BEFORE importing any
-dialect package (``tvm.tirx``, ``tvm.relax``, …). This guarantees that
-``tvm.script.register_dialect`` is reachable the moment a dialect's own
-``__init__.py`` runs and calls it. The ``tvm.script`` module itself
-stays dialect-agnostic at load time (no dialect submodules are eagerly
-imported here), so there is no circular dependency.
+"""TVMScript public namespace and registered dialect exports.
+
+All source parsing uses :mod:`tvm.script.parser`; dialect packages expose source
+constructors and concrete builders from the same canonical implementation.
+The registry permits out-of-tree dialects to expose the same public spellings.
"""
import importlib
@@ -73,10 +29,10 @@ from typing import Any
_DIALECT_REGISTRY: dict[str, str] = {}
# Subpackages of `tvm.script` whose per-dialect children are redirected to a
-# matching subpackage under `tvm.<dialect>.script`. The values are the
-# subpackage name on the dialect side (e.g. `tvm.<dialect>.script.parser`).
+# canonical package under `tvm.<dialect>.script`. An empty suffix names
+# the public dialect namespace itself.
_REDIRECTED_SUBPACKAGES = {
- "tvm.script.parser": "parser",
+ "tvm.script.parser": "",
"tvm.script.ir_builder": "builder",
}
@@ -87,10 +43,9 @@ def register_dialect(name: str, module_path: str) -> None:
Writes ``name -> module_path`` into ``_DIALECT_REGISTRY``. After
registration, ``tvm.script.<name>`` resolves to ``module_path`` via
``__getattr__``, and ``tvm.script.parser.<name>`` / ``tvm.script.ir_builder.<name>``
- resolve to ``module_path + ".parser"`` / ``module_path + ".builder"`` etc.
- via each subpackage's own ``__getattr__``. Deep statement-form imports
- (e.g., ``from tvm.script.parser.<name>.entry import X``) are handled
- by ``_DialectRedirectFinder`` on ``sys.meta_path``.
+ resolve to the same script namespace and ``module_path + ".builder"``
+ respectively. Deep builder imports are handled by
+ ``_DialectRedirectFinder`` on ``sys.meta_path``.
This function is idempotent — re-registering the same name with the same
path is harmless.
@@ -109,9 +64,8 @@ def register_dialect(name: str, module_path: str) -> None:
The short name exposed under ``tvm.script.<name>`` (e.g. ``"tirx"``).
module_path : str
The full dotted module path of the dialect's script package, e.g.
- ``"tvm.tirx.script"``. That package must expose ``parser`` and
- ``ir_builder`` as submodules (and ``printer`` where applicable) so
- that the suffix-append redirect works uniformly.
+ ``"tvm.tirx.script"``. That package exposes its public construction
+ entry points directly and its imperative APIs in a ``builder`` submodule.
"""
_DIALECT_REGISTRY[name] = module_path
@@ -135,7 +89,7 @@ def _redirect_target(fullname: str) -> str | None:
rest = fullname[len(prefix) + 1 :]
head, _, tail = rest.partition(".")
if head in _DIALECT_REGISTRY:
- target = f"{_DIALECT_REGISTRY[head]}.{sub}"
+ target = _DIALECT_REGISTRY[head] + (f".{sub}" if sub else "")
return f"{target}.{tail}" if tail else target
return None
@@ -147,16 +101,16 @@ class _DialectRedirectFinder:
(``from tvm.script import tirx``). It cannot intercept deep
statement-form imports such as::
- from tvm.script.parser.tirx.entry import ObjectProxy
+ import tvm.script.tirx.tile
import tvm.script.ir_builder.relax.ir
This finder is installed on ``sys.meta_path`` to cover those cases.
When the import machinery asks for a module whose full name starts with
``tvm.script.<dialect>`` (or ``tvm.script.parser.<dialect>``, etc.) and
that dialect is in ``_DIALECT_REGISTRY``, :meth:`find_spec` imports the
- real target module (e.g. ``tvm.tirx.script.parser.entry``) and returns an
+ real target module (e.g. ``tvm.tirx.script.tile``) and returns an
alias spec whose loader hands back that module, so the import machinery
- registers it in ``sys.modules`` under the legacy name and all subsequent
+ registers it in ``sys.modules`` under the public alias and all subsequent
imports and attribute walks resolve without going through the redirect
again.
"""
@@ -222,7 +176,7 @@ def __getattr__(name: str) -> Any:
globals()["ir"] = ir_parser
return ir_parser
if name in ("from_source", "parse"):
- from .parser._core import parse # pylint: disable=import-outside-toplevel
+ from .parser import parse # pylint: disable=import-outside-toplevel
globals()["from_source"] = parse
globals()["parse"] = parse
diff --git a/python/tvm/script/ir_builder/__init__.py b/python/tvm/script/ir_builder/__init__.py
index ee114fd1d8..578d549950 100644
--- a/python/tvm/script/ir_builder/__init__.py
+++ b/python/tvm/script/ir_builder/__init__.py
@@ -14,22 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-"""The ir_builder subpackage of TVMScript.
-
-Per-dialect builder submodules (``tvm.script.ir_builder.tirx``, etc.) are
-resolved lazily via :data:`tvm.script._DIALECT_REGISTRY`. When a dialect
-is accessed (e.g. ``tvm.script.ir_builder.tirx``), this subpackage's
-``__getattr__`` looks up the dialect in ``_DIALECT_REGISTRY`` and imports
-``<dialect_module_path>.builder`` (e.g. ``tvm.tirx.script.builder``),
-caching the result so subsequent accesses skip ``__getattr__``.
-
-The IR layer is foundational and is NOT registered as a dialect — its
-builder lives as a real submodule ``tvm.script.ir_builder.ir``.
-
-See :mod:`tvm.script` for a full description of the dialect resolution
-mechanism, including the ``_DialectRedirectFinder`` that handles
-deep statement-form imports.
-"""
+"""The ir_builder subpackage of TVMScript."""
import importlib
from typing import Any
diff --git a/python/tvm/script/ir_builder/base.py b/python/tvm/script/ir_builder/base.py
index 56e78b9072..088b7ce197 100644
--- a/python/tvm/script/ir_builder/base.py
+++ b/python/tvm/script/ir_builder/base.py
@@ -17,11 +17,12 @@
"""A generic IRBuilder across the TVM stack"""
from collections.abc import Callable
-from contextlib import contextmanager
+from contextlib import contextmanager, nullcontext
from typing import Any
from tvm_ffi import register_object as _register_object
+from tvm import ir
from tvm.runtime import Object as _Object
from . import _ffi_api
@@ -37,7 +38,7 @@ class IRBuilderFrame(_Object):
Examples
--------
- The `T.match_buffer` below instead an element in the buffer map of `PrimFuncFrame`:
+ The `T.match_buffer` below matches a function parameter in the active `PrimFuncFrame`:
.. code-block:: python
@@ -60,29 +61,24 @@ class IRBuilderFrame(_Object):
with IRBuilder() as builder:
with T.prim_func(...): # pushes a PrimFuncFrame (subclass of IRBuilderFrame)
# to `builder`'s stack of frames
- with T.sblock(...): # pushes a BlockFrame (subclass of IRBuilderFrame)
+ with T.sblock(...): # pushes an SBlockFrame (subclass of IRBuilderFrame)
# to `builder`'s stack of frames
buffer = T.match_buffer(...)
-
"""
def __enter__(self) -> "IRBuilderFrame":
- _ffi_api.IRBuilderFrameEnter(self) # type: ignore[attr-defined] # pylint: disable=no-member
+ with _construction_span(self.source_span):
+ _ffi_api.IRBuilderFrameEnter(self) # type: ignore[attr-defined] # pylint: disable=no-member
return self
def __exit__(self, exc_type, exc_value, trace) -> None: # pylint: disable=unused-argument
if exc_type is None and exc_value is None:
# Do not execute `FrameExit` if the with scope exits because of exceptions
- _ffi_api.IRBuilderFrameExit(self) # type: ignore[attr-defined] # pylint: disable=no-member
+ with _construction_span(self.source_span):
+ _ffi_api.IRBuilderFrameExit(self) # type: ignore[attr-defined] # pylint: disable=no-member
def add_callback(self, callback: Callable[[], None]) -> None:
- """Add a callback method invoked when exiting the with-scope.
-
- Parameters
- ----------
- callback : Callable[[], None]
- The callback method to be invoked.
- """
+ """Add a callback method invoked when exiting the with-scope."""
_ffi_api.IRBuilderFrameAddCallback( # type: ignore[attr-defined] # pylint: disable=no-member
self, callback
)
@@ -138,24 +134,12 @@ class IRBuilder(_Object):
@staticmethod
def current() -> "IRBuilder":
- """Get the current IRBuilder put in the with-scope.
-
- Returns
- -------
- builder : IRBuilder
- The current IRBuilder.
- """
+ """Get the current IRBuilder put in the with-scope."""
return _ffi_api.IRBuilderCurrent() # type: ignore[attr-defined] # pylint: disable=no-member
@staticmethod
def is_in_scope() -> bool:
- """See if the current thread-local scope has an IRBuilder.
-
- Returns
- -------
- bool
- Whether the current thread-local scope has an IRBuilder
- """
+ """See if the current thread-local scope has an IRBuilder."""
return _ffi_api.IRBuilderIsInScope() # type: ignore[attr-defined] # pylint: disable=no-member
def get(self) -> _Object:
@@ -185,7 +169,7 @@ class IRBuilder(_Object):
)
def _set_current_source_span(self, value):
- """Attach the active source span to an expression without one."""
+ """Compose the active source span onto the same supported IR node or frame."""
return _ffi_api.IRBuilderSetCurrentSourceSpan( # type: ignore[attr-defined] # pylint: disable=no-member
self, value
)
@@ -229,3 +213,101 @@ class IRBuilder(_Object):
"""
assert len(s) == len(vs)
return [IRBuilder.name(i, v) for i, v in zip(s, vs)]
+
+
+# Absence is an explicit value; no function data is retained.
+class _Missing:
+ def __repr__(self):
+ return "MISSING"
+
+
+MISSING = _Missing()
+
+
+def source_span(location):
+ """Materialize a source range without retaining source-unit state."""
+ if location is None or isinstance(location, (ir.Span, ir.SequentialSpan)):
+ return location
+ source_name, line, end_line, column, end_column = location
+ if isinstance(source_name, str):
+ source_name = ir.SourceName(source_name)
+ return ir.Span(source_name, line, end_line, column, end_column)
+
+
+@contextmanager
+def _construction_span(span):
+ """Apply a builder operation's span through the existing native span stack."""
+ if span is None:
+ yield
+ return
+ span = source_span(span)
+ context = (
+ IRBuilder.current().with_source_span(span)
+ if span is not None and IRBuilder.is_in_scope()
+ else nullcontext()
+ )
+ try:
+ with context:
+ yield
+ except Exception as error:
+ if span is not None and not hasattr(error, "__tvm_script_location__"):
+ diagnostic_span = span.spans[-1] if isinstance(span, ir.SequentialSpan) else span
+ error.__tvm_script_location__ = (
+ str(diagnostic_span.source_name.name),
+ diagnostic_span.line,
+ diagnostic_span.end_line,
+ diagnostic_span.column,
+ diagnostic_span.end_column,
+ )
+ raise
+
+
+class BypassBind:
+ """Carry an already-constructed value through assignment without binding it."""
+
+ __slots__ = ("value",)
+
+ def __init__(self, value):
+ self.value = value
+
+
+class BypassEmit:
+ """Reference an already-emitted statement without emitting it again."""
+
+ __slots__ = ("stmt",)
+
+ def __init__(self, stmt):
+ self.stmt = stmt
+
+
+def at(span, value):
+ """Attach source context to the same IR node, emission receipt, or frame.
+
+ Native mutation annotates the statement held by the builder itself. Keep
+ the original Python facade as well, including callable objects and frames.
+ Unsupported objects and ordinary Python values pass through unchanged.
+ """
+ if span is None or not IRBuilder.is_in_scope():
+ return value
+ target = value.stmt if isinstance(value, BypassEmit) else value
+ if isinstance(target, BypassBind):
+ if isinstance(target.value, (list, tuple)):
+ for item in target.value:
+ at(span, item)
+ else:
+ at(span, target.value)
+ return value
+ if isinstance(target, _Object):
+ with _construction_span(span):
+ IRBuilder.current()._set_current_source_span(target)
+ elif callable(set_source_span := getattr(target, "_set_source_span", None)):
+ set_source_span(span)
+ return value
+
+
+def _frame_result(frame, name):
+ """Read one explicit export without consulting ambient construction state."""
+ if not isinstance(name, str):
+ raise TypeError("A frame result name must be a string")
+ exports = frame if isinstance(frame, dict) else getattr(frame, "result", {})
+ return exports.get(name, MISSING)
diff --git a/python/tvm/script/ir_builder/construction.py b/python/tvm/script/ir_builder/construction.py
new file mode 100644
index 0000000000..69a8b091c6
--- /dev/null
+++ b/python/tvm/script/ir_builder/construction.py
@@ -0,0 +1,332 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Runtime owners for generated builder programs."""
+
+from contextlib import contextmanager
+from contextvars import ContextVar
+from types import SimpleNamespace
+from typing import TypeVar
+
+import tvm_ffi
+
+from tvm import ir
+from tvm.script.parser.protocol import is_constexpr_marker
+
+from . import ir as I
+from .base import MISSING, IRBuilder, source_span
+from .parser_support import TypeVarFrame
+from .type_var_frame import TypeVarDecl
+
+# Process-wide owner-supplied callbacks, initialized unset and replaced only by
+# explicit entry-module registration. Neither table contains per-parse state.
+_OPAQUE_FACTORY = None
+_SPECIALIZATION = ContextVar("tvm_builder_specialization", default=None)
+_ABSENT_PARAMETERS = ContextVar("tvm_builder_absent_parameters", default=None)
+
+
+@contextmanager
+def specialization_context(name, bindings):
+ """Pass validated JIT bindings to one root builder execution."""
+ token = _SPECIALIZATION.set((name, dict(bindings)))
+ try:
+ yield
+ finally:
+ _SPECIALIZATION.reset(token)
+
+
+@contextmanager
+def absent_parameters(name, parameters):
+ """Transport the JIT's explicit absent-parameter map to execution.
+
+ JIT owns argument validation/defaults/cache. This input path only carries
+ previously selected absences for a single root function, and never infers
+ absence from ordinary lexical captures.
+ """
+ parameters = dict(parameters or {})
+ if any(value is not None for value in parameters.values()):
+ raise TypeError("absent_params values must be None")
+ token = _ABSENT_PARAMETERS.set((name, parameters))
+ try:
+ yield
+ finally:
+ _ABSENT_PARAMETERS.reset(token)
+
+
+class _FunctionReference(ir.GlobalVar):
+ """Keep a module-owned GlobalVar callable without changing its native object."""
+
+ def __init__(self, reference):
+ # The FFI constructor convention retains the returned existing handle;
+ # it does not create, clone, or replace the underlying native GlobalVar.
+ # The temporary identity callback owns no registration or persistent state.
+ self.__init_handle_by_constructor__(tvm_ffi.convert_func(lambda: reference))
+
+ def asobject(self):
+ """Return the original native reference without copying or changing it."""
+ return self
+
+ def __call__(self, *args):
+ # Imports are deferred until execution to avoid the builder namespaces'
+ # initialization cycle. Only the nearest function owns call semantics;
+ # module/symbol/block frames do not select a dialect.
+ if IRBuilder.is_in_scope():
+ from tvm.relax.script.builder.frame import FunctionFrame
+ from tvm.tirx.script.builder.frame import PrimFuncFrame
+
+ for frame in reversed(IRBuilder.current().frames):
+ if isinstance(frame, PrimFuncFrame):
+ from tvm.tirx.script.builder.ir import _call_global
+
+ return _call_global(self, *args)
+ if isinstance(frame, FunctionFrame):
+ from tvm import relax
+
+ return relax.Call(self, [relax.utils.convert_to_expr(arg) for arg in args])
+ return ir.GlobalVar.__call__(self, *args)
+
+
+def _function_reference(value):
+ """Expose a module GlobalVar as a callable while preserving other values."""
+ if isinstance(value, ir.GlobalVar) and not isinstance(value, _FunctionReference):
+ return _FunctionReference(value)
+ return value
+
+
+def capture_value(value):
+ """Retain lexical values, adapting native function references uniformly."""
+ return _function_reference(value)
+
+
+def register_opaque_factory(factory):
+ """Register the constructor for opaque Python module functions."""
+ global _OPAQUE_FACTORY
+ if not callable(factory):
+ raise TypeError("The module construction callback must be callable")
+ _OPAQUE_FACTORY = factory
+
+
+class FunctionRecord:
+ """Own one function's declaration and definition construction state."""
+
+ def __init__(
+ self,
+ builder,
+ name,
+ options,
+ location=None,
+ *,
+ local=False,
+ captures=None,
+ parameters=(),
+ capture_names=(),
+ ):
+ self.builder, self.name, self.options = builder, name, dict(options)
+ self.span, self.local = source_span(location), local
+ self.symbols = TypeVarFrame()
+ # Explicit lexical primitive symbols are captures, not new declarations.
+ # Parameter names shadow them; all concrete inspection stays builder-side.
+ for captured_name in capture_names:
+ value = (captures or {}).get(captured_name)
+ if (
+ captured_name not in parameters
+ and ir.is_prim_var(value)
+ and value.name in ("", captured_name)
+ ):
+ self.symbols.bind(captured_name, value)
+ context = _SPECIALIZATION.get()
+ self.specialization = (
+ context[1] if context is not None and context[0] == name and not local else {}
+ )
+ self.captured_bindings = dict(captures or {}) if not local else {}
+ absent = _ABSENT_PARAMETERS.get()
+ self.absent_parameters = (
+ absent[1] if absent is not None and absent[0] == name and not local else {}
+ )
+ self.params = {}
+ self.runtime_params = {}
+ self.signature_symbols = set(self.symbols.symbols)
+ self.return_type = MISSING
+ self.reference = self.function = None
+
+ @contextmanager
+ def declaration(self):
+ """Enter the retained symbol frame and a native declaration frame."""
+ mode = {"local": True} if self.local else {}
+ with self.symbols:
+ with self.builder.decl_function(**self.options, **mode, span=self.span) as frame:
+ self.builder.func_name(self.name)
+ yield self
+ self.reference = _function_reference(frame.reference)
+
+ def parameter_lazy(self, name, annotation, location=None):
+ """Resolve a declaration at execution time, omitting specialized ABI slots."""
+ if name in self.specialization:
+ value = self.specialization[name]
+ self.params[name] = value
+ return value
+ if name in self.absent_parameters:
+ self.params[name] = None
+ return None
+ annotation = annotation()
+ if is_constexpr_marker(annotation):
+ if name not in self.captured_bindings:
+ raise TypeError(f"constexpr parameter {name!r} requires a specialization binding")
+ value = self.captured_bindings[name]
+ self.params[name] = value
+ return value
+ # Optional is an annotation wrapper, owned by the JIT entry point.
+ unwrap = getattr(annotation, "__tvm_optional_annotation__", None)
+ if unwrap is not None:
+ annotation = unwrap()
+ return self.parameter(name, annotation, location)
+
+ def parameter(self, name, annotation, location=None):
+ """Construct and retain a parameter in the active declaration."""
+ if callable(annotation) and not isinstance(annotation, ir.Expr | ir.Type):
+ annotation = annotation()
+ if isinstance(annotation, TypeVarDecl):
+ annotation = self.symbols.resolve(name, annotation.ty, span=source_span(location))
+ if isinstance(annotation, ir.PrimType):
+ annotation = self.symbols.resolve(name, annotation, span=source_span(location))
+ elif ir.is_prim_var(annotation):
+ annotation = self.symbols.bind(name, annotation)
+ value = self.builder.arg(name, annotation, span=source_span(location))
+ self.params[name] = value
+ self.runtime_params[name] = value
+ self.signature_symbols = set(self.symbols.symbols)
+ return value
+
+ def predeclare(self, name, annotation, location=None):
+ """Reserve an explicit symbol type before dependent signature shapes."""
+ if callable(annotation) and not isinstance(annotation, ir.Expr | ir.Type):
+ annotation = annotation()
+ if isinstance(annotation, TypeVarDecl):
+ annotation = annotation.ty
+ if ir.is_prim_var(annotation):
+ value = self.symbols.bind(name, annotation)
+ else:
+ value = self.symbols.resolve(name, annotation, span=source_span(location))
+ # A body declaration is already an explicit binding, even in a function
+ # with no parameters. Only symbols newly introduced by the return
+ # annotation itself are unbound return-only symbols.
+ self.signature_symbols = set(self.symbols.symbols)
+ return value
+
+ def symbol(self, name):
+ """Read a declared symbol after signature construction."""
+ return self.symbols.symbols[name]
+
+ def capture(self, name, fallback):
+ """Resolve a free lexical name against retained signature symbols."""
+ if isinstance(fallback, TypeVar):
+ return self.symbols.resolve(name)
+ return self.symbols.symbols.get(name, _function_reference(fallback))
+
+ def returns(self, annotation):
+ """Set the return annotation on the record and active native frame."""
+ introduced = set(self.symbols.symbols) - self.signature_symbols
+ if introduced:
+ raise ValueError(
+ f"Return annotation introduces unbound symbol {sorted(introduced)[0]!r}"
+ )
+ self.return_type = annotation
+ self.builder.func_ret_type(annotation)
+
+ def define(self, body):
+ """Run a generated body with retained parameters under a definition frame."""
+ mode = {"local": True, "reference": self.reference} if self.local else {}
+ with self.symbols:
+ with self.builder.function(**self.options, **mode, span=self.span) as frame:
+ self.builder.func_name(self.name)
+ for name, value in self.runtime_params.items():
+ self.builder.arg(name, value)
+ if self.return_type is not MISSING:
+ self.builder.func_ret_type(self.return_type)
+ body(**self.params)
+ self.function = frame.function
+ self.function.__name__ = self.name
+ return self.function
+
+
+class ModuleProgram:
+ """Own module construction while a generated builder program executes."""
+
+ def __init__(self, name=None, original=None, bases=()):
+ self.name, self.original, self.bases = name, original, bases
+ self.builder = IRBuilder()
+ self.frame = None
+ self.namespace = SimpleNamespace()
+ self.python_functions = {}
+ self.result = None
+
+ def __enter__(self):
+ self.builder.__enter__()
+ self.frame = I.ir_module()
+ self.frame.__enter__()
+ return self
+
+ def __exit__(self, *error):
+ try:
+ self.frame.__exit__(*error)
+ if error[0] is None:
+ self.result = self.builder.get()
+ if self.python_functions:
+ self.result.pyfuncs = self.python_functions
+ if self.name is not None:
+ self.result.__name__ = self.name
+ finally:
+ self.builder.__exit__(*error)
+
+ def reserve(self, name):
+ """Reserve and expose a stable named module function reference."""
+ reference = _function_reference(I.reserve_function(name))
+ setattr(self.namespace, name, reference)
+ return reference
+
+ def member(self, name, value):
+ """Register a class assignment in the generated module namespace."""
+ if isinstance(value, ir.BaseFunc):
+ reference = I.decl_function(name, value)
+ I.def_function(name, value)
+ value = reference
+ value = _function_reference(value)
+ setattr(self.namespace, name, value)
+ return value
+
+ def python(self, name, function, source, location=None):
+ """Register a host function without executing its body."""
+ if _OPAQUE_FACTORY is None:
+ raise ValueError("No opaque Python function constructor has been registered")
+ opaque = _OPAQUE_FACTORY(name, function, source, source_span(location))
+ reference = I.decl_function(name, opaque)
+ I.def_function(name, opaque)
+ self.python_functions[name] = function
+ reference = _function_reference(reference)
+ setattr(self.namespace, name, reference)
+ return reference
+
+
+def require_defined(value, name):
+ """Return an exported value or report an undefined source name."""
+ if value is MISSING:
+ raise NameError(f"name {name!r} is not defined")
+ return value
+
+
+def is_python_bool(value):
+ """Check for a host boolean without invoking truth conversion."""
+ return isinstance(value, bool)
diff --git a/python/tvm/script/ir_builder/ir/__init__.py b/python/tvm/script/ir_builder/ir/__init__.py
index 86c6f1bf27..24fa21bff5 100644
--- a/python/tvm/script/ir_builder/ir/__init__.py
+++ b/python/tvm/script/ir_builder/ir/__init__.py
@@ -17,8 +17,13 @@
# under the License.
"""Package tvm.script.ir_builder.ir"""
+from tvm.ir import GenericConst, Range, StringImm, StringType
+from tvm.script.parser.protocol import constexpr as constexpr
+
+from .. import parser_support
from .frame import IRModuleFrame
from .ir import (
+ reserve_function,
decl_function,
def_function,
ir_module,
diff --git a/python/tvm/script/ir_builder/ir/ir.py b/python/tvm/script/ir_builder/ir/ir.py
index e2810ce698..d568450338 100644
--- a/python/tvm/script/ir_builder/ir/ir.py
+++ b/python/tvm/script/ir_builder/ir/ir.py
@@ -16,11 +16,13 @@
# under the License.
"""Package tvm.script.ir_builder.ir.ir"""
+import inspect
from typing import TYPE_CHECKING, Any, TypeVar
from tvm.ir import BaseFunc, GlobalInfo, GlobalVar
from tvm.runtime import Object as tvm_Object
+from ..base import IRBuilder
from . import _ffi_api
from .frame import IRModuleFrame
@@ -36,18 +38,7 @@ if TYPE_CHECKING:
else:
class meta_var: # pylint: disable=invalid-name
- """A value used only for TVMScript parser-time metaprogramming.
-
- Assignments unwrap this object without emitting an IR binding. The
- shared wrapper is exposed as ``I.meta_var``; dialect namespaces may
- provide compatibility aliases to the same implementation.
- For Relax, this is the explicit opt-out from default primitive binding emission.
-
- Parameters
- ----------
- value : Any
- The parser-time value.
- """
+ """A value used only for TVMScript parser-time metaprogramming."""
def __init__(self, value: Any) -> None:
self.value = value
@@ -57,15 +48,15 @@ else:
def ir_module() -> IRModuleFrame:
- """Start a ir_module frame.
- Returns
- -------
- frame: IRModuleFrame
- The constructed frame.
- """
+ """Start a ir_module frame."""
return _ffi_api.IRModule() # type: ignore[attr-defined] # pylint: disable=no-member
+def reserve_function(func_name: str) -> GlobalVar:
+ """Reserve a module identity before a declaration frame evaluates its signature."""
+ return _ffi_api.ReserveFunction(func_name)
+
+
def decl_function(func_name: str, func_signature: BaseFunc) -> GlobalVar:
"""Declare a Function without given the specific function implementation.
@@ -152,25 +143,65 @@ def module_set_attr(
def module_global_infos(global_infos: dict[str, list[GlobalInfo]]) -> None:
- """Specify the global infos of the ir_module frame.
- Parameters
- ----------
- global_infos: Dict[str, List[GlobalInfo]]
- The module global infos.
- """
- return _ffi_api.ModuleGlobalInfos(global_infos) # type: ignore[attr-defined] # pylint: disable=no-member
+ """Specify the global infos of the ir_module frame."""
+ if IRBuilder.is_in_scope():
+ return _ffi_api.ModuleGlobalInfos(global_infos)
+ # Keep native argument validation even before Python has applied the module decorator.
+ _ffi_api.ModuleGlobalInfos(global_infos)
+ frame = inspect.currentframe().f_back
+ try:
+ if _is_class_frame(frame):
+ previous = frame.f_locals.get("__tvm_script_global_infos__")
+ if previous:
+ raise ValueError(f"Duplicate module global_infos, previous one is:\n{previous}")
+ frame.f_locals["__tvm_script_global_infos__"] = {
+ name: tuple(values) for name, values in global_infos.items()
+ }
+ finally:
+ del frame
+
+
+def _is_class_frame(frame):
+ return (
+ not frame.f_code.co_flags & inspect.CO_NEWLOCALS
+ and frame.f_locals is not frame.f_globals
+ and "__module__" in frame.f_locals
+ and frame.f_locals.get("__qualname__", "").split(".")[-1] == frame.f_code.co_name
+ )
+
+
+def _class_global_infos():
+ # Only a still-executing class owns eager signature context. Never retain its frame,
+ # and do not fall through an inner class to an unrelated outer module declaration.
+ frame = inspect.currentframe().f_back
+ try:
+ while frame is not None:
+ if _is_class_frame(frame):
+ return frame.f_locals.get("__tvm_script_global_infos__", {})
+ frame = frame.f_back
+ finally:
+ del frame
+ return {}
+
+
+def lookup_global_info(name: str, index: int) -> GlobalInfo:
+ """Resolve a concrete global info in a builder or an executing module class."""
+ if IRBuilder.is_in_scope():
+ for frame in reversed(IRBuilder.current().frames):
+ if isinstance(frame, IRModuleFrame):
+ return frame.global_infos[name][index]
+ raise ValueError("The GlobalInfos in the IRModule is not defined.")
+ infos = _class_global_infos()
+ if not infos:
+ raise ValueError("The GlobalInfos in the IRModule is not defined.")
+ return infos[name][index]
############################### GlobalInfo ###############################
def dummy_global_info() -> "DummyGlobalInfo":
- """Create a dummy global info expression.
- Returns
- -------
- res : DummyGlobalInfo
- The result dummy global info.
- """
+ """Create a dummy global info expression."""
from tvm.relax import DummyGlobalInfo # pylint: disable=import-outside-toplevel
return DummyGlobalInfo() # type: ignore[attr-defined] # pylint: disable=no-member
@@ -202,16 +233,35 @@ def lookup_vdevice(target_kind: str | None = None, device_index: int = -1) -> "V
Parameters
----------
target_kind: str
- The target device kind, for example 'llvm' or 'cuda'.
+ The target device kind, for example 'llvm' or 'cuda'. Use 'vdevice'
+ to index the complete virtual-device list.
device_index: int
- The virtual device index.
+ The zero-based index among devices of the selected target kind, or
+ among all devices when target_kind is 'vdevice'.
Returns
-------
res : VDevice
The result virtual device.
"""
- return _ffi_api.LookupVDevice(target_kind, device_index) # type: ignore[attr-defined] # pylint: disable=no-member
+ if IRBuilder.is_in_scope():
+ return _ffi_api.LookupVDevice(target_kind, device_index)
+ from tvm.relax import VDevice # pylint: disable=import-outside-toplevel
+
+ infos = _class_global_infos()
+ if not infos:
+ raise ValueError("The GlobalInfos in the IRModule is not defined.")
+ vdevices = infos["vdevice"]
+ if device_index < 0 or device_index >= len(vdevices):
+ raise ValueError("The target VDevice in the GlobalInfos was not found.")
+ if not all(isinstance(value, VDevice) for value in vdevices):
+ raise TypeError("The vdevice global infos must contain VDevice values.")
+ if target_kind == "vdevice":
+ return vdevices[device_index]
+ matches = [value for value in vdevices if value.target.kind.name == target_kind]
+ if device_index >= len(matches):
+ raise ValueError("The target VDevice in the GlobalInfos was not found.")
+ return matches[device_index]
def lookup_name(name: str) -> bool:
diff --git a/python/tvm/script/ir_builder/parser_support.py b/python/tvm/script/ir_builder/parser_support.py
new file mode 100644
index 0000000000..d4b3bb6369
--- /dev/null
+++ b/python/tvm/script/ir_builder/parser_support.py
@@ -0,0 +1,79 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Shared construction helpers used by generated TVMScript programs.
+
+Generated programs bind ``_PS = I.parser_support`` with a collision-free alias.
+``TypeVarFrame`` belongs to one function and remains shared by its signature and
+body. ``lookup_global_info`` resolves unchanged module reference strings; dtype
+and placement strings are not expression syntax.
+
+Explicit constexpr conditions select Python branches in generated programs;
+unmarked expressions use dialect IR constructors directly. Source calls use
+``with_at_scope(loc, thunk)``;
+non-call values use ``at(loc, value)``. Both preserve returned object identity,
+and the former restores caller source context even when evaluation raises.
+
+A standalone value is consumed by ``X.emit_(value)``. Self-emitting builders
+return ``BypassEmit(stmt)`` so source attachment reaches the stored statement
+without emitting it a second time. Bindings and generated keyword operations
+retain explicit ``span=`` for their separate source locations.
+"""
+
+import re
+
+from .base import IRBuilder, _construction_span, at
+from .type_var_frame import TypeVarFrame as TypeVarFrame
+
+
+def lookup_global_info(content):
+ """Resolve an unchanged module reference, or pass a concrete value through."""
+ if not isinstance(content, str):
+ return content
+ from .ir.frame import IRModuleFrame
+
+ if not IRBuilder.is_in_scope():
+ raise ValueError("Global-info lookup requires an enclosing module frame")
+ frame = next(
+ (
+ frame
+ for frame in reversed(IRBuilder.current().frames)
+ if isinstance(frame, IRModuleFrame)
+ ),
+ None,
+ )
+ if frame is None:
+ raise ValueError("Global-info lookup requires an enclosing module frame")
+ match = re.fullmatch(r"([^\[\]]+)\[(\d+)\]", content)
+ if match:
+ name, index = match.groups()
+ return frame.global_infos[name][int(index)]
+ from .ir.ir import lookup_vdevice
+
+ selector = re.fullmatch(r"([^:\[\]]+)(?::(\d+)(?::([^:]+))?)?", content)
+ if selector is None:
+ raise ValueError(f"Invalid global-info reference: {content!r}")
+ target, index, _scope = selector.groups()
+ # Printed references include the selected device's memory scope. Selection
+ # uses its ordinal among devices of this target kind and retains that exact
+ # module-owned object, including its stored scope.
+ return lookup_vdevice(target, int(index) if index is not None else 0)
+
+
+def with_at_scope(location, thunk):
+ """Evaluate a source call once under its location and preserve its result."""
+ with _construction_span(location):
+ return at(location, thunk())
diff --git a/python/tvm/script/ir_builder/type_var_frame.py b/python/tvm/script/ir_builder/type_var_frame.py
new file mode 100644
index 0000000000..1536df70d1
--- /dev/null
+++ b/python/tvm/script/ir_builder/type_var_frame.py
@@ -0,0 +1,147 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Function-owned symbolic type variables on the native builder frame stack."""
+
+from dataclasses import dataclass
+from functools import wraps
+from typing import TypeVar
+
+import tvm_ffi
+
+from tvm import ir
+
+from . import _ffi_api
+from .base import IRBuilder, IRBuilderFrame, source_span
+
+
+@dataclass(frozen=True)
+class TypeVarDecl:
+ """Describe an unbound primitive symbolic declaration."""
+
+ ty: object
+
+ def __post_init__(self):
+ ty = ir.PrimType(self.ty) if isinstance(self.ty, str) else self.ty
+ if not isinstance(ty, ir.PrimType):
+ raise TypeError("A symbolic declaration requires a primitive type")
+ object.__setattr__(self, "ty", ty)
+
+
+@tvm_ffi.register_object("script.ir_builder.TypeVarFrame")
+class TypeVarFrame(IRBuilderFrame):
+ """Retain canonical symbols across one function declaration and definition."""
+
+ def __init__(self):
+ self.__init_handle_by_constructor__(_ffi_api.TypeVarFrame)
+
+ @staticmethod
+ def current():
+ """Find the nearest active function symbol frame."""
+ for frame in reversed(IRBuilder.current().frames):
+ if isinstance(frame, TypeVarFrame):
+ return frame
+ raise ValueError("Symbol resolution requires an active TypeVarFrame")
+
+ def resolve(self, name, ty=None, *, span=None):
+ """Resolve or introduce one canonical primitive symbol."""
+ if not isinstance(name, str) or not name:
+ raise ValueError("A symbolic variable requires a nonempty string name")
+ if isinstance(ty, str):
+ ty = ir.PrimType(ty)
+ if ty is not None and not isinstance(ty, ir.PrimType):
+ raise TypeError("A symbolic variable requires a primitive type")
+ if name in self.symbols:
+ return self.symbols[name]
+ value = ir.Var(name, ir.PrimType("int64") if ty is None else ty, source_span(span))
+ _ffi_api.TypeVarFrameSetSymbol(self, name, value)
+ return value
+
+ def bind(self, name, symbol):
+ """Register an existing primitive variable or reuse its canonical binding."""
+ if not isinstance(name, str) or not name:
+ raise ValueError("A symbolic variable requires a nonempty string name")
+ if not ir.is_prim_var(symbol):
+ raise TypeError("A symbolic binding requires a primitive Var")
+ if name in self.symbols:
+ return self.resolve(name, symbol.ty)
+ if not symbol.name:
+ IRBuilder.name(name, symbol)
+ _ffi_api.TypeVarFrameSetSymbol(self, name, symbol)
+ return symbol
+
+
+def resolve_type_var(name, *, dtype=None, span=None):
+ """Resolve a symbol in the nearest active function frame."""
+ return TypeVarFrame.current().resolve(name, dtype, span=span)
+
+
+def wrap_expression_constructor(constructor, call_signature, policy, *, as_type=False):
+ """Adapt an eager constructor using parser-owned expression-string metadata."""
+ fields = policy.fields
+
+ def unresolved(value, nested=False):
+ if isinstance(value, str):
+ return nested or policy.scalar_strings
+ if isinstance(value, TypeVar):
+ return True
+ if isinstance(value, tuple | list):
+ return any(unresolved(item, True) for item in value)
+ return False
+
+ @wraps(constructor)
+ def invoke(*args, **kwargs):
+ bound = call_signature.bind(*args, **kwargs)
+ if IRBuilder.is_in_scope():
+ # typing.TypeVar is ordinary eager Python metadata. Resolve it
+ # here, never in the syntax-only transpiler.
+ def resolve(value):
+ if isinstance(value, TypeVar):
+ if value.__bound__ is not None or value.__constraints__:
+ raise TypeError("A symbolic TypeVar cannot have constraints or a bound")
+ return TypeVarFrame.current().resolve(value.__name__)
+ if isinstance(value, tuple):
+ return tuple(resolve(item) for item in value)
+ if isinstance(value, list):
+ return [resolve(item) for item in value]
+ return value
+
+ for field in fields:
+ if field in bound.arguments:
+ bound.arguments[field] = resolve(bound.arguments[field])
+ if any(unresolved(bound.arguments[field]) for field in fields if field in bound.arguments):
+ if IRBuilder.is_in_scope():
+ raise TypeError(
+ "Builder expression arguments require concrete symbols, not strings"
+ )
+ return ir.Type.missing()
+ return constructor(*bound.args, **bound.kwargs)
+
+ result = invoke
+ if as_type:
+ # The class is an annotation surface, not an IR or proxy type.
+ # __new__ returns the concrete construction result (or MissingType).
+ result = type(
+ constructor.__name__,
+ (),
+ {
+ "__new__": lambda cls, *args, **kwargs: invoke(*args, **kwargs),
+ "__signature__": call_signature,
+ "__doc__": constructor.__doc__,
+ "__module__": constructor.__module__,
+ },
+ )
+ return result
diff --git a/python/tvm/script/parser/__init__.py b/python/tvm/script/parser/__init__.py
index d9911322a0..a35d4a0135 100644
--- a/python/tvm/script/parser/__init__.py
+++ b/python/tvm/script/parser/__init__.py
@@ -14,38 +14,84 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-"""The parser subpackage of TVMScript.
+"""Canonical TVMScript AST parser and public construction namespaces."""
-Per-dialect parser submodules (``tvm.script.parser.tirx``, etc.) are
-resolved lazily via :data:`tvm.script._DIALECT_REGISTRY`. When a dialect
-is accessed (e.g. ``tvm.script.parser.tirx``), this subpackage's
-``__getattr__`` looks up the dialect in ``_DIALECT_REGISTRY`` and imports
-``<dialect_module_path>.parser`` (e.g. ``tvm.tirx.script.parser``),
-caching the result so subsequent accesses skip ``__getattr__``.
+import importlib
+import sys
+from typing import TypeVar
-The IR layer is foundational and is NOT registered as a dialect — its
-parser lives as a real submodule ``tvm.script.parser.ir``, with
-``ir_module`` re-exported at this level for convenience.
+_FRONTEND_EXPORTS = (
+ "_NAMESPACES", "from_source", "ir_module", "make_decorator", "make_helper",
+ "parse", "pyfunc", "register_namespace",
+)
+__all__ = [name for name in _FRONTEND_EXPORTS if not name.startswith("_")]
+_initialized = False
+_initializing = False
-See :mod:`tvm.script` for a full description of the dialect resolution
-mechanism, including the ``_DialectRedirectFinder`` that handles
-deep statement-form imports.
-"""
-import importlib
-from typing import Any
+def _initialize():
+ global _initialized, _initializing
+ if _initialized or _initializing:
+ return
+ _initializing = True
+ try:
+ from tvm import relax
+ from tvm.relax import script as relax_namespace
+ from tvm.relax.script import builder as relax_builder
+ from tvm.script.ir_builder import construction
+ from tvm.tirx import script as tir_namespace
+ from tvm.tirx.layout import Axis
+ from tvm.tirx.script import builder as tir_builder
+ from tvm.tirx.script import tile
+ from . import frontend, ir
+ from .jit import OptionalAnnotation, make_jit
+
+ for namespace, builder in ((tir_namespace, tir_builder), (relax_namespace, relax_builder)):
+ namespace.__dict__.update(
+ (name, value) for name, value in vars(builder).items() if not name.startswith("_")
+ )
+ # Source assignment consumes a receipt; imperative bind returns the Var.
+ tir_namespace.bind = tir_builder._native.bind
+ tir_namespace.prim_func = frontend.make_decorator(
+ tir_builder, option_map={"private": "private", "s_tir": "s_tir", "persistent": "persistent"}
+ )
+ tir_namespace.jit = make_jit(tir_builder)
+ tir_namespace.Optional = OptionalAnnotation
+ tir_namespace.inline = frontend.make_helper(tir_builder, preserve_return=True, late_binding=True)
+ tir_namespace.macro = frontend.make_helper(tir_builder, preserve_return=False)
+ tir_namespace.tile = tile
+ for name in ("cluster", "cta", "thread", "warp", "warpgroup", "wg"):
+ setattr(tir_namespace, name, getattr(tile, name))
+ relax_namespace.function = frontend.make_decorator(
+ relax_builder, option_map={"pure": "is_pure", "private": "is_private"}
+ )
+ relax_namespace.macro = frontend.make_helper(relax_builder, preserve_return=True)
+ ir.ir_module = frontend.ir_module
+ ir.pyfunc = frontend.pyfunc
+ for namespace in (tir_namespace, relax_namespace, ir):
+ namespace.__all__ = [name for name in vars(namespace) if not name.startswith("_")]
+ frontend._NAMESPACES.update(
+ I=ir, ir=ir, T=tir_namespace, tir=tir_namespace, tirx=tir_namespace,
+ R=relax_namespace, relax=relax_namespace, Tx=tile, Axis=Axis, TypeVar=TypeVar,
+ )
-from . import _core, ir, tirx
-from ._core import parse
-from .ir import ir_module
+ def opaque(name, function, source, span):
+ return relax.ExternFunc(name, span=span).with_attrs({
+ "is_pyfunc": True, "function_type": "python", "python_function_name": name,
+ "python_source": source, "python_packed_func": function,
+ })
+ construction.register_opaque_factory(opaque)
+ _initialized = True
+ finally:
+ _initializing = False
-def __getattr__(name: str) -> Any:
- # Lazy import to avoid loading tvm.script during dialect bootstrap.
- from tvm.script import _DIALECT_REGISTRY # pylint: disable=import-outside-toplevel
- if name in _DIALECT_REGISTRY:
- module = importlib.import_module(f"{_DIALECT_REGISTRY[name]}.parser")
- globals()[name] = module
- return module
- raise AttributeError(f"module 'tvm.script.parser' has no attribute {name!r}")
+def __getattr__(name):
+ if name in _FRONTEND_EXPORTS:
+ _initialize()
+ return getattr(importlib.import_module(f"{__name__}.frontend"), name)
+ if name in ("tirx", "tir", "relax", "I", "T", "R", "Tx"):
+ _initialize()
+ return sys.modules[f"{__name__}.frontend"]._NAMESPACES[name]
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
diff --git a/python/tvm/script/parser/_core.py b/python/tvm/script/parser/_core.py
deleted file mode 100644
index d87214f22f..0000000000
--- a/python/tvm/script/parser/_core.py
+++ /dev/null
@@ -1,24 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the Licens.
-# ruff: noqa: F401
-"""The core parser infra"""
-
-# pylint: disable=unused-import
-from .core import dispatch, doc, utils
-from .core.dispatch import OpMethod, register_op
-from .core.entry import parse, scan_macro
-from .core.parser import Parser, collect_signature_type_vars
diff --git a/python/tvm/script/parser/core/diagnostics.py b/python/tvm/script/parser/core/diagnostics.py
deleted file mode 100644
index 974c5c593f..0000000000
--- a/python/tvm/script/parser/core/diagnostics.py
+++ /dev/null
@@ -1,324 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-# ruff: noqa: E741
-"""TVM Script Parser Source and diagnostics"""
-
-import inspect
-import sys
-
-from tvm.error import DiagnosticError
-from tvm.ir import SourceName, Span
-
-from . import doc
-
-
-class Source:
- """Source code class for TVMScript.
-
- It is constructed by source code str or doc AST tree.
-
- Parameters
- ----------
- source_name : str
- The filename of the file where the source code locates.
-
- start_line : int
- The first line number of the source code.
-
- start_column : int
- The first column number of the first line of the source code.
-
- source : str
- The source code str of source code.
-
- full_source : str
- The complete source code of the file where the source code locates.
- """
-
- source_name: str
- start_line: int
- start_column: int
- source: str
- full_source: str
-
- def __init__(self, program: str | doc.AST):
- if isinstance(program, str):
- self.source_name = "<str>"
- self.start_line = 1
- self.start_column = 0
- self.source = program
- self.full_source = program
- return
-
- self.source_name = inspect.getsourcefile(program) # type: ignore
- lines, self.start_line = getsourcelines(program) # type: ignore
- if lines:
- self.start_column = len(lines[0]) - len(lines[0].lstrip())
- else:
- self.start_column = 0
- if self.start_column and lines:
- self.source = "\n".join([l[self.start_column :].rstrip() for l in lines])
- else:
- self.source = "".join(lines)
- try:
- # It will cause a problem when running in Jupyter Notebook.
- # `mod` will be <module '__main__'>, which is a built-in module
- # and `getsource` will throw a TypeError
- mod = inspect.getmodule(program)
- if mod:
- self.full_source = inspect.getsource(mod)
- else:
- self.full_source = self.source
- except TypeError:
- # It's a work around for Jupyter problem.
- # Since `findsource` is an internal API of inspect, we just use it
- # as a fallback method.
- src, _ = inspect.findsource(program) # type: ignore
- self.full_source = "".join(src)
-
- def as_ast(self) -> doc.AST:
- """Parse the source code into AST.
-
- Returns
- -------
- res : doc.AST
- The AST of source code.
- """
- return doc.parse(self.source)
-
- def location(self, node: doc.AST) -> tuple[int, int, int, int]:
- """Return the absolute 1-based source range of an AST node."""
- lineno = getattr(node, "lineno", 1) or 1
- col_offset = getattr(node, "col_offset", self.start_column)
- col_offset = self.start_column if col_offset is None else col_offset
- end_lineno = getattr(node, "end_lineno", lineno) or lineno
- end_col_offset = getattr(node, "end_col_offset", col_offset)
- end_col_offset = col_offset if end_col_offset is None else end_col_offset
- lineno += self.start_line - 1
- end_lineno += self.start_line - 1
- col_offset += self.start_column + 1
- end_col_offset += self.start_column + 1
- return lineno, col_offset, end_lineno, end_col_offset
-
- def to_span(self, node: doc.AST) -> Span:
- """Convert an AST node to the canonical IR source span."""
- lineno, col_offset, end_lineno, end_col_offset = self.location(node)
- return Span(
- SourceName(self.source_name or "<unknown>"),
- lineno,
- end_lineno,
- col_offset,
- end_col_offset,
- )
-
-
-_getfile = inspect.getfile # pylint: disable=invalid-name
-_findsource = inspect.findsource # pylint: disable=invalid-name
-
-
-def _patched_inspect_getfile(obj):
- """Work out which source or compiled file an object was defined in."""
- if not inspect.isclass(obj):
- return _getfile(obj)
- mod = getattr(obj, "__module__", None)
- if mod is not None:
- file = getattr(sys.modules[mod], "__file__", None)
- if file is not None:
- return file
- for _, member in inspect.getmembers(obj):
- if inspect.isfunction(member):
- if obj.__qualname__ + "." + member.__name__ == member.__qualname__:
- return inspect.getfile(member)
- raise TypeError(f"Source for {obj!r} not found")
-
-
-def findsource(obj):
- """Return the entire source file and starting line number for an object."""
- import linecache # pylint: disable=import-outside-toplevel
-
- if not inspect.isclass(obj):
- return _findsource(obj)
-
- file = inspect.getsourcefile(obj)
- if file:
- linecache.checkcache(file)
- else:
- file = inspect.getfile(obj)
- if not (file.startswith("<") and file.endswith(">")):
- raise OSError("source code not available")
-
- module = inspect.getmodule(obj, file)
- if module:
- lines = linecache.getlines(file, module.__dict__)
- else:
- lines = linecache.getlines(file)
- if not lines:
- raise OSError("could not get source code")
- qual_names = obj.__qualname__.replace(".<locals>", "<locals>").split(".")
- in_comment = 0
- scope_stack = []
- indent_info = {}
- for i, line in enumerate(lines):
- n_comment = line.count('"""')
- if n_comment:
- # update multi-line comments status
- in_comment = in_comment ^ (n_comment & 1)
- continue
- if in_comment:
- # skip lines within multi-line comments
- continue
- indent = len(line) - len(line.lstrip())
- tokens = line.split()
- if len(tokens) > 1:
- name = None
- if tokens[0] == "def":
- name = tokens[1].split(":")[0].split("(")[0].split("[")[0] + "<locals>"
- elif tokens[0] == "class":
- name = tokens[1].split(":")[0].split("(")[0]
- # pop scope if we are less indented
- while scope_stack and indent_info[scope_stack[-1]] >= indent:
- scope_stack.pop()
- if name:
- scope_stack.append(name)
- indent_info[name] = indent
- if scope_stack == qual_names:
- return lines, i
-
- raise OSError("could not find class definition")
-
-
-def getsourcelines(obj):
- """Extract the block of code at the top of the given list of lines."""
- obj = inspect.unwrap(obj)
- lines, l_num = findsource(obj)
- return inspect.getblock(lines[l_num:]), l_num + 1
-
-
-inspect.getfile = _patched_inspect_getfile
-
-
-def _format_source_snippet(
- source_lines: list,
- lineno: int,
- col_offset: int,
- end_lineno: int,
- end_col_offset: int,
-) -> str:
- """Format a source code snippet with a column/span marker.
-
- Renders every source line spanned by the diagnostic (``lineno`` through
- ``end_lineno``, inclusive) with a per-line gutter, followed by a caret
- underline covering the offending span. For a single-line span
- (``end_lineno == lineno``) only the columns ``col_offset`` (inclusive)
- through ``end_col_offset`` (exclusive) are underlined; for a multi-line
- span the underline covers the start column to end-of-line on the first
- line, the full text of interior lines, and the start of the final line up
- to ``end_col_offset``.
-
- Parameters
- ----------
- source_lines : list of str
- Lines of the source code.
-
- lineno : int
- 1-based starting line number in the source.
-
- col_offset : int
- 1-based starting column (inclusive) on ``lineno``.
-
- end_lineno : int
- 1-based ending line number in the source (>= ``lineno``).
-
- end_col_offset : int
- 1-based ending column (exclusive) on ``end_lineno``.
-
- Returns
- -------
- snippet : str
- Formatted source snippet with caret-marker line(s).
- """
- if end_lineno < lineno:
- end_lineno = lineno
-
- # Determine the gutter width so that all line numbers line up.
- header_width = len(f" {end_lineno} ")
- no_line_header = " " * header_width
-
- parts = [f"{no_line_header}| "]
- for cur_lineno in range(lineno, end_lineno + 1):
- idx = cur_lineno - 1
- if not 0 <= idx < len(source_lines):
- continue
- line_text = source_lines[idx].rstrip("\n")
- line_header = f" {cur_lineno} ".rjust(header_width)
-
- # Compute the underline span [start_col, stop_col) for this line.
- start_col = col_offset if cur_lineno == lineno else 1
- stop_col = end_col_offset if cur_lineno == end_lineno else len(line_text) + 1
-
- marker = ""
- for i in range(1, len(line_text) + 1):
- if start_col <= i < stop_col:
- marker += "^"
- else:
- marker += " "
- parts.append(f"{line_header}| {line_text}")
- parts.append(f"{no_line_header}| {marker}")
-
- if len(parts) == 1:
- return ""
- return "\n".join(parts)
-
-
-class Diagnostics:
- """Diagnostics class for error reporting in parser.
-
- Formats parse errors with source location context and raises directly,
- without going through DiagnosticContext.
-
- Parameters
- ----------
- source : Source
- The source code.
- """
-
- source: Source
-
- def __init__(self, source: Source):
- self.source = source
-
- def error(self, node: doc.AST, message: str) -> None:
- """Emit a diagnostic error by raising with source location context.
-
- Parameters
- ----------
- node : doc.AST
- The node with diagnostic error.
-
- message : str
- The diagnostic message.
- """
- lineno, col_offset, end_lineno, end_col_offset = self.source.location(node)
-
- source_lines = self.source.full_source.splitlines(keepends=True)
- snippet = _format_source_snippet(
- source_lines, lineno, col_offset, end_lineno, end_col_offset
- )
-
- location = f"{self.source.source_name}:{lineno}:{col_offset}"
- formatted = f"error: {message}\n --> {location}\n{snippet}"
- raise DiagnosticError(formatted)
diff --git a/python/tvm/script/parser/core/dispatch.py b/python/tvm/script/parser/core/dispatch.py
deleted file mode 100644
index 5f880be833..0000000000
--- a/python/tvm/script/parser/core/dispatch.py
+++ /dev/null
@@ -1,157 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-"""Parser dispatching infrastructure"""
-
-from collections.abc import Callable
-from typing import TYPE_CHECKING, Any
-
-from .doc import AST
-
-if TYPE_CHECKING:
- from .parser import Parser
-
-
-ParseMethod = Callable[["Parser", AST], None]
-ParseVTable: dict[tuple[str, str], ParseMethod] = {}
-
-OpMethod = Callable[..., Any]
-OpVTable: dict[tuple[type, AST, int], OpMethod] = {}
-
-
-def register(token: str, type_name: str):
- """Register a method for a dispatch token and type name.
-
- Parameters
- ----------
- token : str
- The token for IR, e.g., T for TIR and R for Relax.
-
- type_name : str
- The type name of AST node, e.g., FunctionDef, With, For.
-
- Returns
- -------
- func : callable
- The function to register dispatched method of parsing
- corresponding token and AST node type.
- """
-
- def func(method: ParseMethod):
- """Register a method in parser virtual table.
-
- Parameters
- ----------
- method : ParseMethod
- The dispatched method to be registered in parser virtual table.
- """
- ParseVTable[(token, type_name)] = method
-
- return func
-
-
-def get(
- token: str,
- type_name: str,
- default: ParseMethod | None = None,
-) -> ParseMethod | None:
- """Get a registered method for a dispatch token and type name,
- or return a default method if no registered methods with this dispatch token and type name.
-
- Parameters
- ----------
- token : str
- The token for IR, e.g., T for TIR and R for Relax.
-
- type_name : str
- The type name of AST node, e.g., FunctionDef, With, For.
-
- default : Optional[ParseMethod]
- The default method when no registered methods with this dispatch token and type name.
-
- Returns
- -------
- func : Optional[ParseMethod]
- The dispatched method of parsing corresponding token and AST node type.
- """
- return ParseVTable.get((token, type_name), default)
-
-
-def register_op(operand_type: type, op_node_type: AST, operand_index: int):
- """Register a method for a operand type, AST operator node and operand index.
-
- Parameters
- ----------
- operand_type : Type
- The type of operands, e.g., tirx.Expr, tirx.IterVar.
-
- op_node_type : AST
- The doc AST operator node type, e.g., doc.Add, doc.Eq.
-
- operand_index : int
- The operand index, i.e., 0 for left operand and 1 for right operand.
-
- Returns
- -------
- func : callable
- The function to register dispatched method of parsing
- corresponding a operand type, AST operator node and operand index.
- """
-
- def func(method: OpMethod):
- """Register a method in parser operator virtual table.
-
- Parameters
- ----------
- method : ParseMethod
- The dispatched method to be registered in parser operator virtual table.
- """
- OpVTable[(operand_type, op_node_type, operand_index)] = method
-
- return func
-
-
-def get_op(
- operand_type: type,
- op_node_type: type,
- operand_index: int,
- default: OpMethod | None = None,
-) -> OpMethod | None:
- """Register a method for a operand type, AST operator node and operand index.
-
- Parameters
- ----------
- operand_type : Type
- The type of operands, e.g., tirx.Expr, tirx.IterVar.
-
- op_node_type : AST
- The doc AST operator node type, e.g., doc.Add, doc.Eq.
-
- operand_index : int
- The operand index, i.e., 0 for left operand and 1 for right operand.
-
-
- default : Optional[OpMethod]
- The default method when no registered methods with this operand type,
- AST operator node and operand index.
-
- Returns
- -------
- func : Optional[OpMethod]
- The function to register dispatched method of parsing
- corresponding a operand type, AST operator node and operand index.
- """
- return OpVTable.get((operand_type, op_node_type, operand_index), default)
diff --git a/python/tvm/script/parser/core/doc.py b/python/tvm/script/parser/core/doc.py
deleted file mode 100644
index 7d44dd50d6..0000000000
--- a/python/tvm/script/parser/core/doc.py
+++ /dev/null
@@ -1,320 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-# ruff: noqa: E722, F403
-"""TVM Script Parser doc AST"""
-
-import ast
-import inspect
-import typing
-from collections import defaultdict
-
-from . import doc_core as doc
-from .doc_core import * # pylint: disable=unused-import,wildcard-import,redefined-builtin,W0614
-
-FnToDoc = typing.Callable[[ast.AST], doc.AST]
-FnFromDoc = typing.Callable[[doc.AST], ast.AST]
-
-
-class Entry:
- """Mapping entry between python AST node type str and doc AST.
-
- Parameters
- ----------
- to_doc : typing.Optional[FnToDoc]
- The callable methods for converting python AST node to doc AST.
-
- from_doc : typing.Optional[FnFromDoc]
- The callable methods for converting doc AST to python AST node.
- """
-
- to_doc: FnToDoc | None
- from_doc: FnFromDoc | None
-
- def __init__(self):
- self.to_doc = None
- self.from_doc = None
-
-
-class Registry:
- """Registration map from python AST node type str to methods of conversion
- between python AST node and doc AST node.
-
- Parameters
- ----------
- _inst : typing.Optional["Registry"]
- The instance of Registry.
-
- table : typing.Dict[str, Entry]
- The registration map from python AST node type str to methods of conversion
- between python AST node and doc AST node.
- """
-
- _inst: typing.Optional["Registry"] = None
- table: dict[str, Entry]
-
- def __init__(self):
- self.table = defaultdict(Entry)
-
-
-def register_to_doc(name: str):
- """Register the to_doc method for python AST node type.
-
- Parameters
- ----------
- name : str
- The type of python AST node.
-
- Returns
- -------
- f : Callable[[FnToDoc], None]
- The function of registering the to_doc method for python AST node type.
- """
-
- def f(to_doc: FnToDoc): # pylint: disable=redefined-outer-name
- reg = Registry._inst # pylint: disable=protected-access
- reg.table[name].to_doc = to_doc
-
- return f
-
-
-def register_from_doc(name: str):
- """Register the from_doc method for python AST node type.
-
- Parameters
- ----------
- name : str
- The type of python AST node.
-
- Returns
- -------
- f : Callable[[FnFromDoc], None]
- The function of registering the from_doc method for python AST node type.
- """
-
- def f(to_doc: FnFromDoc): # pylint: disable=redefined-outer-name
- reg = Registry._inst # pylint: disable=protected-access
- reg.table[name].from_doc = to_doc
-
- return f
-
-
-def _is_atomic_type(node):
- return (
- node is None
- or node in [..., True, False]
- or isinstance(
- node,
- int | float | str | bool | bytes | complex,
- )
- )
-
-
-def _get_registry_entry(cls_name, attr):
- cls_name = cls_name.split(".")[-1]
- reg = Registry._inst # pylint: disable=protected-access
- if cls_name in reg.table:
- entry = reg.table[cls_name]
- return getattr(entry, attr, None)
- return None
-
-
-def from_doc(node):
- """Get original python AST node from doc AST node.
-
- Parameters
- ----------
- node : doc.AST
- The doc AST node.
-
- Returns
- -------
- res : ast.AST
- The corresponding AST node.
- """
- if _is_atomic_type(node):
- return node
- if isinstance(node, tuple):
- return tuple(from_doc(n) for n in node)
- if isinstance(node, list):
- return [from_doc(n) for n in node]
- func = _get_registry_entry(node.__class__.__name__, "from_doc")
- if not func:
- raise NotImplementedError(f"from_doc is not implemented for: {node.__class__.__name__}")
- return func(node)
-
-
-def to_doc(node):
- """Get doc AST node from python AST node.
-
- Parameters
- ----------
- node : ast.AST
- The AST node.
-
- Returns
- -------
- res : doc.AST
- The corresponding doc AST node.
- """
- if _is_atomic_type(node):
- return node
- if isinstance(node, tuple):
- return tuple(to_doc(n) for n in node)
- if isinstance(node, list):
- return [to_doc(n) for n in node]
- func = _get_registry_entry(node.__class__.__name__, "to_doc")
- if not func:
- raise NotImplementedError(f"to_doc is not implemented for: {node.__class__.__name__}")
- return func(node)
-
-
-def parse(
- source: str,
- filename: str = "<unknown>",
- mode: str = "exec",
-) -> doc.AST:
- """Parse TVMScript source code str to doc AST.
-
- Its interface is consistent with python built-in ast.parse.
- And it will parse by python 3.8 first if possible,
- or it will parse with python version in current environment.
-
- Parameters
- ----------
- source : str
- The TVMScript source code.
-
- filename : str
- The optional filename of the file where source code locates.
-
- mode : str
- The parsing mode for ast.parse.
-
- Returns
- -------
- res : doc.AST
- The parsed doc AST.
- """
- try:
- program = ast.parse( # pylint: disable=unexpected-keyword-arg
- source=source,
- filename=filename,
- mode=mode,
- feature_version=(3, 8),
- )
- except: # pylint: disable=bare-except
- program = ast.parse(
- source=source,
- filename=filename,
- mode=mode,
- )
- return to_doc(program)
-
-
-class NodeVisitor:
- """Node visitor for doc AST"""
-
- def visit(self, node: doc.AST) -> None:
- if isinstance(node, list | tuple):
- for item in node:
- self.visit(item)
- return
- if not isinstance(node, doc.AST):
- return
- getattr(
- self,
- "visit_" + node.__class__.__name__.split(".")[-1],
- self.generic_visit,
- )(node)
-
- def generic_visit(self, node: doc.AST) -> None:
- for field in node.__class__._FIELDS: # pylint: disable=protected-access
- value = getattr(node, field, None)
- if value is None:
- pass
- elif isinstance(value, doc.AST | list | tuple):
- self.visit(value)
-
-
-class NodeTransformer:
- """Node transformer for doc AST"""
-
- def visit(self, node: doc.AST) -> doc.AST:
- if isinstance(node, list):
- return [self.visit(item) for item in node]
- if isinstance(node, tuple):
- return tuple(self.visit(item) for item in node)
- if not isinstance(node, doc.AST):
- return node
- return getattr(
- self,
- "visit_" + node.__class__.__name__.split(".")[-1],
- self.generic_visit,
- )(node)
-
- def generic_visit(self, node: doc.AST) -> doc.AST:
- kv: dict[str, typing.Any] = {}
- for field in node.__class__._FIELDS: # pylint: disable=protected-access
- value = getattr(node, field, None)
- if value is None:
- pass
- elif isinstance(value, doc.AST | list | tuple):
- value = self.visit(value)
- kv[field] = value
- return node.__class__(**kv)
-
-
-def _register_default():
- class DefaultTranslator:
- def __init__(self, doc_cls, func, fields):
- self.doc_cls = doc_cls # getattr(doc, name)
- self.func = func
- self.fields = fields
-
- def __call__(self, node):
- kv = {attr: self.func(getattr(node, attr, None)) for attr in self.fields}
- return self.doc_cls(**kv)
-
- Registry._inst = Registry() # pylint: disable=protected-access
- for cls_name in dir(doc):
- doc_cls = getattr(doc, cls_name)
- if not hasattr(ast, cls_name):
- continue
- if inspect.isclass(doc_cls) and issubclass(doc_cls, doc.AST):
- assert "." not in cls_name
- register_to_doc(cls_name)(
- DefaultTranslator(
- getattr(doc, cls_name),
- to_doc,
- doc_cls._FIELDS, # pylint: disable=protected-access
- )
- )
- register_from_doc(cls_name)(
- DefaultTranslator(
- getattr(ast, cls_name),
- from_doc,
- [
- field
- for field in doc_cls._FIELDS # pylint: disable=protected-access
- if field in getattr(getattr(ast, cls_name), "_fields", ())
- or field in {"lineno", "col_offset", "end_lineno", "end_col_offset"}
- ],
- )
- )
-
-
-_register_default()
diff --git a/python/tvm/script/parser/core/doc_core.py b/python/tvm/script/parser/core/doc_core.py
deleted file mode 100644
index 74b7e57e3d..0000000000
--- a/python/tvm/script/parser/core/doc_core.py
+++ /dev/null
@@ -1,1123 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-# pylint: disable=redefined-outer-name,missing-docstring,invalid-name
-# pylint: disable=useless-super-delegation,redefined-builtin
-# pylint: disable=too-few-public-methods,too-many-arguments
-# ruff: noqa: RUF012
-
-
-# minimal python version is 3.9
-class AST:
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class mod(AST):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class Module(mod):
- _FIELDS = ["body"]
-
- def __init__(self, body):
- super().__init__()
- self.body = body
-
-
-class Interactive(mod):
- _FIELDS = ["body"]
-
- def __init__(self, body):
- super().__init__()
- self.body = body
-
-
-class Expression(mod):
- _FIELDS = ["body"]
-
- def __init__(self, body):
- super().__init__()
- self.body = body
-
-
-class stmt(AST):
- _FIELDS = ["lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__()
- self.lineno = lineno
- self.col_offset = col_offset
- self.end_lineno = end_lineno
- self.end_col_offset = end_col_offset
-
-
-class FunctionDef(stmt):
- _FIELDS = [
- "name",
- "args",
- "body",
- "decorator_list",
- "returns",
- "type_params",
- "lineno",
- "col_offset",
- "end_lineno",
- "end_col_offset",
- ]
-
- def __init__(
- self,
- name,
- args,
- body,
- decorator_list,
- returns,
- type_params,
- lineno,
- col_offset,
- end_lineno,
- end_col_offset,
- ):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.name = name
- self.args = args
- self.body = body
- self.decorator_list = decorator_list
- self.returns = returns
- self.type_params = type_params
-
-
-class ClassDef(stmt):
- _FIELDS = [
- "name",
- "bases",
- "keywords",
- "body",
- "decorator_list",
- "lineno",
- "col_offset",
- "end_lineno",
- "end_col_offset",
- ]
-
- def __init__(
- self,
- name,
- bases,
- keywords,
- body,
- decorator_list,
- lineno,
- col_offset,
- end_lineno,
- end_col_offset,
- ):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.name = name
- self.bases = bases
- self.keywords = keywords
- self.body = body
- self.decorator_list = decorator_list
-
-
-class Return(stmt):
- _FIELDS = ["value", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, value, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.value = value
-
-
-class Delete(stmt):
- _FIELDS = ["targets", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, targets, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.targets = targets
-
-
-class Assign(stmt):
- _FIELDS = ["targets", "value", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, targets, value, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.targets = targets
- self.value = value
-
-
-class AugAssign(stmt):
- _FIELDS = ["target", "op", "value", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, target, op, value, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.target = target
- self.op = op
- self.value = value
-
-
-class AnnAssign(stmt):
- _FIELDS = [
- "target",
- "annotation",
- "value",
- "simple",
- "lineno",
- "col_offset",
- "end_lineno",
- "end_col_offset",
- ]
-
- def __init__(
- self, target, annotation, value, simple, lineno, col_offset, end_lineno, end_col_offset
- ):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.target = target
- self.annotation = annotation
- self.value = value
- self.simple = simple
-
-
-class For(stmt):
- _FIELDS = [
- "target",
- "iter",
- "body",
- "orelse",
- "lineno",
- "col_offset",
- "end_lineno",
- "end_col_offset",
- ]
-
- def __init__(self, target, iter, body, orelse, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.target = target
- self.iter = iter
- self.body = body
- self.orelse = orelse
-
-
-class While(stmt):
- _FIELDS = ["test", "body", "orelse", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, test, body, orelse, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.test = test
- self.body = body
- self.orelse = orelse
-
-
-class If(stmt):
- _FIELDS = ["test", "body", "orelse", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, test, body, orelse, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.test = test
- self.body = body
- self.orelse = orelse
-
-
-class With(stmt):
- _FIELDS = ["items", "body", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, items, body, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.items = items
- self.body = body
-
-
-class Raise(stmt):
- _FIELDS = ["exc", "cause", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, exc, cause, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.exc = exc
- self.cause = cause
-
-
-class Try(stmt):
- _FIELDS = [
- "body",
- "handlers",
- "orelse",
- "finalbody",
- "lineno",
- "col_offset",
- "end_lineno",
- "end_col_offset",
- ]
-
- def __init__(
- self, body, handlers, orelse, finalbody, lineno, col_offset, end_lineno, end_col_offset
- ):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.body = body
- self.handlers = handlers
- self.orelse = orelse
- self.finalbody = finalbody
-
-
-class Assert(stmt):
- _FIELDS = ["test", "msg", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, test, msg, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.test = test
- self.msg = msg
-
-
-class Import(stmt):
- _FIELDS = ["names", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, names, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.names = names
-
-
-class ImportFrom(stmt):
- _FIELDS = ["module", "names", "level", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, module, names, level, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.module = module
- self.names = names
- self.level = level
-
-
-class Global(stmt):
- _FIELDS = ["names", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, names, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.names = names
-
-
-class Nonlocal(stmt):
- _FIELDS = ["names", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, names, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.names = names
-
-
-class Expr(stmt):
- _FIELDS = ["value", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, value, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.value = value
-
-
-class Pass(stmt):
- _FIELDS = ["lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
-
-
-class Break(stmt):
- _FIELDS = ["lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
-
-
-class Continue(stmt):
- _FIELDS = ["lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
-
-
-class expr(AST):
- _FIELDS = ["lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__()
- self.lineno = lineno
- self.col_offset = col_offset
- self.end_lineno = end_lineno
- self.end_col_offset = end_col_offset
-
-
-class type_param(AST):
- _FIELDS = ["lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__()
- self.lineno = lineno
- self.col_offset = col_offset
- self.end_lineno = end_lineno
- self.end_col_offset = end_col_offset
-
-
-class TypeVar(type_param):
- _FIELDS = [
- "name",
- "bound",
- "default_value",
- "lineno",
- "col_offset",
- "end_lineno",
- "end_col_offset",
- ]
-
- def __init__(self, name, bound, default_value, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.name = name
- self.bound = bound
- self.default_value = default_value
-
-
-class ParamSpec(type_param):
- _FIELDS = [
- "name",
- "default_value",
- "lineno",
- "col_offset",
- "end_lineno",
- "end_col_offset",
- ]
-
- def __init__(self, name, default_value, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.name = name
- self.default_value = default_value
-
-
-class TypeVarTuple(type_param):
- _FIELDS = [
- "name",
- "default_value",
- "lineno",
- "col_offset",
- "end_lineno",
- "end_col_offset",
- ]
-
- def __init__(self, name, default_value, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.name = name
- self.default_value = default_value
-
-
-class BoolOp(expr):
- _FIELDS = ["op", "values", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, op, values, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.op = op
- self.values = values
-
-
-class BinOp(expr):
- _FIELDS = ["left", "op", "right", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, left, op, right, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.left = left
- self.op = op
- self.right = right
-
-
-class UnaryOp(expr):
- _FIELDS = ["op", "operand", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, op, operand, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.op = op
- self.operand = operand
-
-
-class Lambda(expr):
- _FIELDS = ["args", "body", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, args, body, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.args = args
- self.body = body
-
-
-class IfExp(expr):
- _FIELDS = ["test", "body", "orelse", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, test, body, orelse, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.test = test
- self.body = body
- self.orelse = orelse
-
-
-class Dict(expr):
- _FIELDS = ["keys", "values", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, keys, values, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.keys = keys
- self.values = values
-
-
-class Set(expr):
- _FIELDS = ["elts", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, elts, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.elts = elts
-
-
-class ListComp(expr):
- _FIELDS = ["elt", "generators", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, elt, generators, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.elt = elt
- self.generators = generators
-
-
-class SetComp(expr):
- _FIELDS = ["elt", "generators", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, elt, generators, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.elt = elt
- self.generators = generators
-
-
-class DictComp(expr):
- _FIELDS = ["key", "value", "generators", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, key, value, generators, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.key = key
- self.value = value
- self.generators = generators
-
-
-class GeneratorExp(expr):
- _FIELDS = ["elt", "generators", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, elt, generators, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.elt = elt
- self.generators = generators
-
-
-class Yield(expr):
- _FIELDS = ["value", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, value, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.value = value
-
-
-class YieldFrom(expr):
- _FIELDS = ["value", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, value, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.value = value
-
-
-class Compare(expr):
- _FIELDS = ["left", "ops", "comparators", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, left, ops, comparators, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.left = left
- self.ops = ops
- self.comparators = comparators
-
-
-class Call(expr):
- _FIELDS = ["func", "args", "keywords", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, func, args, keywords, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.func = func
- self.args = args
- self.keywords = keywords
-
-
-class FormattedValue(expr):
- _FIELDS = [
- "value",
- "conversion",
- "format_spec",
- "lineno",
- "col_offset",
- "end_lineno",
- "end_col_offset",
- ]
-
- def __init__(
- self, value, conversion, format_spec, lineno, col_offset, end_lineno, end_col_offset
- ):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.value = value
- self.conversion = conversion
- self.format_spec = format_spec
-
-
-class JoinedStr(expr):
- _FIELDS = ["values", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, values, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.values = values
-
-
-class Constant(expr):
- _FIELDS = ["value", "kind", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, value, kind, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.value = value
- self.kind = kind
-
-
-class NamedExpr(expr):
- _FIELDS = ["target", "value", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, target, value, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.target = target
- self.value = value
-
-
-class Attribute(expr):
- _FIELDS = ["value", "attr", "ctx", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, value, attr, ctx, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.value = value
- self.attr = attr
- self.ctx = ctx
-
-
-class Subscript(expr):
- _FIELDS = ["value", "slice", "ctx", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, value, slice, ctx, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.value = value
- self.slice = slice
- self.ctx = ctx
-
-
-class Starred(expr):
- _FIELDS = ["value", "ctx", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, value, ctx, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.value = value
- self.ctx = ctx
-
-
-class Name(expr):
- _FIELDS = ["id", "ctx", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, id, ctx, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.id = id
- self.ctx = ctx
-
-
-class List(expr):
- _FIELDS = ["elts", "ctx", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, elts, ctx, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.elts = elts
- self.ctx = ctx
-
-
-class Tuple(expr):
- _FIELDS = ["elts", "ctx", "lineno", "col_offset", "end_lineno", "end_col_offset"]
-
- def __init__(self, elts, ctx, lineno, col_offset, end_lineno, end_col_offset):
- super().__init__(lineno, col_offset, end_lineno, end_col_offset)
- self.elts = elts
- self.ctx = ctx
-
-
-class Slice(AST):
- _FIELDS = ["lower", "upper", "step"]
-
- def __init__(self, lower, upper, step):
- super().__init__()
- self.lower = lower
- self.upper = upper
- self.step = step
-
-
-class expr_context(AST):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class Del(expr_context):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class Load(expr_context):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class Store(expr_context):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class boolop(AST):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class And(boolop):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class Or(boolop):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class operator(AST):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class Add(operator):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class BitAnd(operator):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class BitOr(operator):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class BitXor(operator):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class Div(operator):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class FloorDiv(operator):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class LShift(operator):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class Mod(operator):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class Mult(operator):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class MatMult(operator):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class Pow(operator):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class RShift(operator):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class Sub(operator):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class unaryop(AST):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class Invert(unaryop):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class Not(unaryop):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class UAdd(unaryop):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class USub(unaryop):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class cmpop(AST):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class Eq(cmpop):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class Gt(cmpop):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class GtE(cmpop):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class In(cmpop):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class Is(cmpop):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class IsNot(cmpop):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class Lt(cmpop):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class LtE(cmpop):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class NotEq(cmpop):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class NotIn(cmpop):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class comprehension(AST):
- _FIELDS = ["target", "iter", "ifs", "is_async"]
-
- def __init__(self, target, iter, ifs, is_async):
- super().__init__()
- self.target = target
- self.iter = iter
- self.ifs = ifs
- self.is_async = is_async
-
-
-class excepthandler(AST):
- _FIELDS = []
-
- def __init__(self):
- super().__init__()
-
-
-class ExceptHandler(excepthandler):
- _FIELDS = ["type", "name", "body"]
-
- def __init__(self, type, name, body):
- super().__init__()
- self.type = type
- self.name = name
- self.body = body
-
-
-class arguments(AST):
- _FIELDS = ["args", "vararg", "kwonlyargs", "kw_defaults", "kwarg", "defaults", "posonlyargs"]
-
- def __init__(
- self,
- args,
- vararg,
- kwonlyargs,
- kw_defaults,
- kwarg,
- defaults,
- posonlyargs,
- ):
- super().__init__()
- self.args = args
- self.vararg = vararg
- self.kwonlyargs = kwonlyargs
- self.kw_defaults = kw_defaults
- self.kwarg = kwarg
- self.defaults = defaults
- self.posonlyargs = posonlyargs
-
-
-class arg(AST):
- _FIELDS = ["arg", "annotation"]
-
- def __init__(self, arg, annotation):
- super().__init__()
- self.arg = arg
- self.annotation = annotation
-
-
-class keyword(AST):
- _FIELDS = ["arg", "value"]
-
- def __init__(self, arg, value):
- super().__init__()
- self.arg = arg
- self.value = value
-
-
-class alias(AST):
- _FIELDS = ["name", "asname"]
-
- def __init__(self, name, asname):
- super().__init__()
- self.name = name
- self.asname = asname
-
-
-class withitem(AST):
- _FIELDS = ["context_expr", "optional_vars"]
-
- def __init__(self, context_expr, optional_vars):
- super().__init__()
- self.context_expr = context_expr
- self.optional_vars = optional_vars
-
-
-__all__ = [
- "AST",
- "Add",
- "And",
- "AnnAssign",
- "Assert",
- "Assign",
- "Attribute",
- "AugAssign",
- "BinOp",
- "BitAnd",
- "BitOr",
- "BitXor",
- "BoolOp",
- "Break",
- "Call",
- "ClassDef",
- "Compare",
- "Constant",
- "Continue",
- "Del",
- "Delete",
- "Dict",
- "DictComp",
- "Div",
- "Eq",
- "ExceptHandler",
- "Expr",
- "Expression",
- "FloorDiv",
- "For",
- "FormattedValue",
- "FunctionDef",
- "GeneratorExp",
- "Global",
- "Gt",
- "GtE",
- "If",
- "IfExp",
- "Import",
- "ImportFrom",
- "In",
- "Interactive",
- "Invert",
- "Is",
- "IsNot",
- "JoinedStr",
- "LShift",
- "Lambda",
- "List",
- "ListComp",
- "Load",
- "Lt",
- "LtE",
- "MatMult",
- "Mod",
- "Module",
- "Mult",
- "Name",
- "NamedExpr",
- "Nonlocal",
- "Not",
- "NotEq",
- "NotIn",
- "Or",
- "ParamSpec",
- "Pass",
- "Pow",
- "RShift",
- "Raise",
- "Return",
- "Set",
- "SetComp",
- "Slice",
- "Starred",
- "Store",
- "Sub",
- "Subscript",
- "Try",
- "Tuple",
- "TypeVar",
- "TypeVarTuple",
- "UAdd",
- "USub",
- "UnaryOp",
- "While",
- "With",
- "Yield",
- "YieldFrom",
- "alias",
- "arg",
- "arguments",
- "boolop",
- "cmpop",
- "comprehension",
- "excepthandler",
- "expr",
- "expr_context",
- "keyword",
- "mod",
- "operator",
- "stmt",
- "type_param",
- "unaryop",
- "withitem",
-]
diff --git a/python/tvm/script/parser/core/entry.py b/python/tvm/script/parser/core/entry.py
deleted file mode 100644
index afd155f3be..0000000000
--- a/python/tvm/script/parser/core/entry.py
+++ /dev/null
@@ -1,223 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-"""The entry point of TVM parser."""
-
-import inspect
-from typing import Any, TypeVar
-
-import tvm
-
-from ....ir.module import IRModule
-from ...ir_builder import IRBuilder
-from . import doc
-from .diagnostics import Source
-from .error import ParserError
-from .parser import Parser
-
-WELL_FORMED_ERROR_MESSAGE = (
- "Program is not well-formed. If this is deliberate, consider "
- "setting check_well_formed in the top-level decorator to False "
- "(e.g., @I.ir_module(check_well_formed=False) or "
- "@R.function(check_well_formed=False))."
-)
-
-
-def _default_globals() -> dict[str, Any]:
- # lazy import here to avoid circular deps
- from tvm.script import tirx as _tirx_dsl # pylint: disable=import-outside-toplevel
- from tvm.script.parser import (
- ir, # pylint: disable=import-outside-toplevel
- relax, # pylint: disable=import-outside-toplevel
- )
- from tvm.script.parser import tirx as _tirx_parser # pylint: disable=import-outside-toplevel
- from tvm.script.tirx import tile as _tirx_tile # pylint: disable=import-outside-toplevel
- from tvm.tirx import layout as _tirx_layout # pylint: disable=import-outside-toplevel
-
- # Expose the layout `Axis` class so printed layout sugar like
- # `4 @ Axis.laneid` round-trips without per-script imports. Injecting just
- # `Axis` (one short symbol) avoids name collisions with common user shape
- # vars like `m`, `P`, `F` that registered axes happen to share names with.
- return {
- "tvm": tvm,
- "I": ir,
- "ir": ir,
- "T": _tirx_parser,
- "tir": _tirx_parser,
- "R": relax,
- "relax": relax,
- "Tx": _tirx_tile,
- "tirx": _tirx_dsl,
- "Axis": _tirx_layout.Axis,
- "TypeVar": TypeVar,
- }
-
-
-def scan_macro(program: Any | str, extra_vars: dict[str, Any] | None = None) -> Any:
- """Generate the AST, and the source code for __repr__."""
- # The AST will be converted into TIR at the time of expansion.
- source = Source(program)
- closure_vars = extra_vars or _default_globals()
- return source, closure_vars
-
-
-def parse(
- program: doc.AST | Any | str,
- extra_vars: dict[str, Any] | None = None,
- check_well_formed: bool = True,
- s_tir: bool = False,
- absent_params: dict[str, None] | None = None,
-) -> Any:
- """Register a method for a operand type, AST operator node and operand index.
-
- Parameters
- ----------
- program : Union[doc.AST, Any, str]
- The TVMScript code to parse.
-
- extra_vars : Dict[str, Any]
- The extra variable table for parsing.
-
- check_well_formed : bool
- Whether to check well-formedness after parsing.
-
- s_tir : bool
- Compatibility argument accepted by parse/from_source. It no longer
- selects verification: each PrimFunc's s_tir attribute determines its
- dialect checks, and common well-formedness checks cover the full module.
-
- absent_params : Optional[Dict[str, None]]
- Function parameters removed by a compile-time specialization. The
- dialect-specific function parser decides how to bind these names.
-
- Returns
- -------
- func : Any
- The parsed TVMScript program.
- """
- if extra_vars is None:
- extra_vars = _default_globals()
-
- ann = {}
- all_pyfuncs = {}
- if inspect.isfunction(program):
- ann = {program.__name__: program.__annotations__}
- elif inspect.isclass(program):
- for name, func in program.__dict__.items():
- if inspect.isfunction(func):
- ann[name] = func.__annotations__
- all_pyfuncs[name] = func
-
- source = Source(program)
- parser = Parser(source, ann, absent_params=absent_params)
- with IRBuilder() as builder:
- try:
- parser.parse(extra_vars=extra_vars)
- except ParserError as err:
- parser.report_error(err.node, err.args[0])
- ret = builder.get()
- # Attach pyfuncs to the IRModule
- if inspect.isclass(program) and isinstance(ret, IRModule):
- _attach_pyfuncs_to_irmodule(ret, all_pyfuncs)
-
- # check well-formedness in both Relax and TIR
- if check_well_formed:
- check_ret = ret
- if not isinstance(check_ret, IRModule):
- check_ret = IRModule.from_expr(ret)
-
- source_ast = source.as_ast()
-
- if isinstance(
- ret, IRModule | tvm.relax.Function
- ) and not tvm.relax.analysis.check_well_formed(ret):
- parser.report_error(source_ast, err=WELL_FORMED_ERROR_MESSAGE)
-
- try:
- # Keep shared-definition checks across both dialects, then apply
- # ordinary TIRX execution restrictions only to its own functions.
- tvm.s_tir.analysis.verify_well_formed(check_ret)
- for func in check_ret.functions.values():
- if isinstance(func, tvm.tirx.PrimFunc) and not func.attrs.get("s_tir", False):
- tvm.tirx.analysis.verify_tirx_well_formed(func)
- except Exception as err: # pylint: disable=broad-exception-caught
- parser.report_error(
- source_ast,
- err=f"{WELL_FORMED_ERROR_MESSAGE}\n\nTraceback: {err!s}",
- )
- return ret
-
-
-def _create_python_packed_func(pyfunc):
- """Create a PackedFunc wrapper for a Python function.
-
- This function creates a PackedFunc that can be called from TVM runtime
- and will execute the original Python function.
-
- Parameters
- ----------
- pyfunc : Callable
- The Python function to wrap.
-
- Returns
- -------
- PackedFunc
- A PackedFunc that wraps the Python function.
- """
-
- def packed_func_wrapper(*args, **kwargs):
- """Wrapper function that calls the original Python function."""
- try:
- result = pyfunc(*args, **kwargs)
- return result
- except Exception as error:
- print(f"Error calling Python function {pyfunc.__name__}: {error}")
- raise
-
- return packed_func_wrapper
-
-
-def _attach_pyfuncs_to_irmodule(irmodule, all_pyfuncs):
- """Attach Python functions to IRModule with reduced nesting."""
- if not all_pyfuncs:
- return
-
- if not hasattr(irmodule, "pyfuncs"):
- irmodule.pyfuncs = {}
-
- for global_var, func in irmodule.functions_items():
- if not isinstance(func, tvm.relax.ExternFunc):
- continue
- if not func.attrs.get("is_pyfunc", False):
- continue
-
- pyfunc_name = global_var.name_hint
- if pyfunc_name not in all_pyfuncs:
- continue
-
- pyfunc = all_pyfuncs[pyfunc_name]
- irmodule.pyfuncs[pyfunc_name] = pyfunc
-
- try:
- source_code = inspect.getsource(pyfunc)
- func = func.with_attr("python_source", source_code)
- except (OSError, TypeError):
- func = func.with_attr("python_source", f"# Source unavailable for {pyfunc_name}")
-
- packed_func = _create_python_packed_func(pyfunc)
- func = func.with_attr("python_packed_func", packed_func)
-
- irmodule[global_var] = func
diff --git a/python/tvm/script/parser/core/error.py b/python/tvm/script/parser/core/error.py
deleted file mode 100644
index a28a8daf74..0000000000
--- a/python/tvm/script/parser/core/error.py
+++ /dev/null
@@ -1,27 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-"""Error classes for diagnostics."""
-
-from . import doc
-
-
-class ParserError(Exception):
- """Error class for diagnostics."""
-
- def __init__(self, node: doc.AST, msg: str):
- super().__init__(msg)
- self.node = node
diff --git a/python/tvm/script/parser/core/evaluator.py b/python/tvm/script/parser/core/evaluator.py
deleted file mode 100644
index 71a1b3bc8f..0000000000
--- a/python/tvm/script/parser/core/evaluator.py
+++ /dev/null
@@ -1,626 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-"""AST Evaluation"""
-
-import ast
-from collections.abc import Callable
-from typing import TYPE_CHECKING, Any
-
-import tvm
-
-from . import dispatch, doc
-from .error import ParserError
-
-if TYPE_CHECKING:
- from .parser import Parser
-
-DEFAULT_OP: dict[type, Callable[..., Any]] = {
- doc.Add: lambda a, b: a + b,
- doc.Sub: lambda a, b: a - b,
- doc.Mult: lambda a, b: a * b,
- doc.Div: lambda a, b: a / b,
- doc.FloorDiv: lambda a, b: a // b,
- doc.Mod: lambda a, b: a % b,
- doc.LShift: lambda a, b: a << b,
- doc.RShift: lambda a, b: a >> b,
- doc.BitOr: lambda a, b: a | b,
- doc.BitXor: lambda a, b: a ^ b,
- doc.BitAnd: lambda a, b: a & b,
- doc.MatMult: lambda a, b: a @ b,
- doc.Pow: lambda a, b: a**b,
- doc.Eq: lambda a, b: a == b,
- doc.NotEq: lambda a, b: a != b,
- doc.Lt: lambda a, b: a < b,
- doc.LtE: lambda a, b: a <= b,
- doc.Gt: lambda a, b: a > b,
- doc.GtE: lambda a, b: a >= b,
- doc.Is: lambda a, b: a is b,
- doc.IsNot: lambda a, b: a is not b,
- doc.In: lambda a, b: a in b,
- doc.NotIn: lambda a, b: a not in b,
- doc.And: lambda a, b: a and b,
- doc.Or: lambda a, b: a or b,
- doc.Invert: lambda a: ~a,
- doc.Not: lambda a: not a,
- doc.UAdd: lambda a: +a,
- doc.USub: lambda a: -a,
-}
-
-
-def _get_builtin_or_none(name: str):
- builtins = globals().get("__builtins__")
- if not builtins:
- return None
- if not isinstance(builtins, dict) and hasattr(builtins, "__dict__"):
- builtins = builtins.__dict__
- if isinstance(builtins, dict):
- return builtins.get(name)
- return None
-
-
-class ExprEvaluator:
- """Expression evaluator for TVMScript parser.
-
- Parameters
- ----------
- parser : Parser
- The parser bound with the evaluator.
-
- value_table : Dict[str, Any]
- The value table for expression evaluation.
-
- new_value_count : int
- The count for intermediate result added during evaluation.
- """
-
- parser: "Parser"
- value_table: dict[str, Any]
- new_value_count: int
-
- def __init__(self, parser: "Parser", value_table: dict[str, Any]) -> None:
- super().__init__()
- self.parser = parser
- self.value_table = value_table
- self.new_value_count = 0
-
- @staticmethod
- def eval(parser: "Parser", value_table: dict[str, Any], node: doc.AST) -> Any:
- """Expression evaluation for TVMScript parser.
-
- Parameters
- ----------
- parser : Parser
- The parser bound with the evaluator.
-
- value_table : Dict[str, Any]
- The value table for expression evaluation.
-
- node : doc.AST
- The root node of AST tree node of expression to evaluate.
-
- Returns
- -------
- res : Any
- The evaluation result.
- """
- self = ExprEvaluator(parser, value_table)
- result = self._visit(node) # pylint: disable=protected-access
- if isinstance(result, doc.Name):
- if result.id in self.value_table:
- return self.value_table[result.id]
- else:
- builtin = _get_builtin_or_none(result.id)
- if builtin:
- return builtin
- raise ParserError(result, f"Undefined variable: {result.id}")
- if isinstance(result, doc.Constant):
- return result.value
- raise TypeError(f"Unexpected result type: {type(result)}")
-
- def _add_intermediate_result(self, value: Any) -> doc.Name:
- """Add intermediate result during evaluation into value table.
-
- Parameters
- ----------
- value : Any
- The intermediate result.
-
- Returns
- -------
- name : doc.Name
- The doc AST name node with intermediate name for intermediate result.
- """
- if self.parser is not None:
- value = self.parser.annotate_current_source_span(value)
- name = f"__tvm_tmp_value_{self.new_value_count}"
- self.new_value_count += 1
- self.value_table[name] = value
- lineno = 0
- col_offset = 0
- return doc.Name(
- id=name,
- ctx=doc.Load(),
- lineno=lineno,
- col_offset=col_offset,
- end_lineno=None,
- end_col_offset=None,
- )
-
- def _visit(self, node: doc.AST) -> Any:
- """General doc AST node visiting method for expression evaluation.
-
- Parameters
- ----------
- node : doc.AST
- The root node of AST tree node of expression to evaluate.
-
- Returns
- -------
- res : Any
- The evaluation result.
- """
- if isinstance(node, list):
- return [self._visit(n) for n in node]
- if isinstance(node, tuple):
- return tuple(self._visit(n) for n in node)
- assert isinstance(node, doc.AST)
- if self.parser is None:
- return self._visit_node(node)
- with self.parser.with_source_span(node):
- return self._visit_node(node)
-
- def _visit_node(self, node: doc.AST) -> Any:
- """Evaluate one AST node while its source span is active."""
- # Boolean operators and conditional expressions require AST-level
- # control over operand evaluation. Walking all fields first would
- # eagerly evaluate dead compile-time branches.
- if isinstance(node, doc.BoolOp):
- try:
- value = self._eval_bool_op(node)
- except Exception as err: # pylint: disable=broad-except
- self.parser.report_error(node, err)
- return self._add_intermediate_result(value)
- if isinstance(node, doc.IfExp):
- try:
- value = self._eval_if_exp(node)
- except Exception as err: # pylint: disable=broad-except
- self.parser.report_error(node, err)
- return self._add_intermediate_result(value)
-
- args = []
- if (
- isinstance(node, doc.Call)
- and hasattr(node.func, "attr")
- and node.func.attr not in ["reads", "writes", "match_buffer"]
- ) or isinstance(node, doc.BinOp | doc.UnaryOp | doc.Compare):
- if isinstance(node, doc.BinOp):
- args = [node.left, node.right]
- elif isinstance(node, doc.UnaryOp):
- args = [node.operand]
- elif isinstance(node, doc.Compare):
- args = [node.left, *node.comparators]
- elif isinstance(node, doc.Call):
- args = node.args
- for arg in args:
- if isinstance(arg, doc.Subscript) and isinstance(arg.slice, doc.Slice | doc.Tuple):
- if isinstance(arg.slice, doc.Slice):
- check_slices = [arg.slice]
- else:
- check_slices = []
- for p in arg.slice.elts:
- if isinstance(p, doc.Slice):
- check_slices.append(p)
- for s in check_slices:
- if not s.step and s.upper and s.lower:
- s.step = doc.Constant(
- 1,
- None,
- s.upper.lineno,
- s.upper.end_col_offset + 1,
- s.upper.lineno,
- s.upper.end_col_offset + 2,
- )
- if isinstance(node, doc.Name):
- if node.id not in self.value_table and not _get_builtin_or_none(node.id):
- raise ParserError(node, f"Undefined variable: {node.id}")
- return node
- if isinstance(
- node,
- doc.Constant | doc.expr_context | doc.operator | doc.boolop | doc.unaryop | doc.cmpop,
- ):
- return node
- if isinstance(node, doc.keyword):
- return doc.keyword(arg=node.arg, value=self._visit(node.value))
- if not isinstance(node, doc.expr | doc.Slice):
- return node
- if isinstance(node, doc.Lambda):
- return self._eval_lambda(node)
- if isinstance(node, doc.Starred):
- value = self._visit(node.value)
- return doc.Starred(
- value=value,
- ctx=node.ctx,
- lineno=node.lineno,
- col_offset=node.col_offset,
- end_lineno=node.end_lineno,
- end_col_offset=node.end_col_offset,
- )
-
- if isinstance(node, doc.ListComp | doc.SetComp | doc.DictComp):
- value = self._eval_expr(node)
- return self._add_intermediate_result(value)
-
- fields = {}
- for field in node.__class__._FIELDS: # pylint: disable=protected-access
- attr = getattr(node, field)
- if isinstance(attr, doc.AST | tuple | list):
- fields[field] = self._visit(attr)
- else:
- fields[field] = attr
- try:
- if isinstance(node, doc.Compare):
- value = self._eval_compare(fields)
- elif isinstance(node, doc.UnaryOp):
- value = self._eval_unary_op(fields)
- elif isinstance(node, doc.BinOp):
- value = self._eval_bin_op(fields)
- elif isinstance(node, doc.Slice):
- value = self._eval_slice(fields)
- else:
- value = self._eval_expr(node.__class__(**fields))
- except Exception as err: # pylint: disable=broad-except
- self.parser.report_error(node, err)
- return self._add_intermediate_result(value)
-
- def _eval_lambda(self, node: doc.Lambda) -> Any:
- """The doc AST lambda node evaluating method.
-
- Parameters
- ----------
- node : doc.Lambda
- The root node of AST tree node of expression to evaluate.
-
- Returns
- -------
- res : Any
- The evaluation result.
- """
- try:
- value = self._eval_expr(node)
- except Exception as err: # pylint: disable=broad-except
- self.parser.report_error(node, err)
- return self._add_intermediate_result(value)
-
- def _eval_bool_op(self, node: doc.BoolOp) -> Any:
- """The doc AST boolean operator node evaluating method.
-
- Parameters
- ----------
- node : doc.BoolOp
- The boolean operation whose operands are evaluated in order.
-
- Returns
- -------
- res : Any
- The evaluation result.
- """
- op = node.op
- if not isinstance(op, doc.And | doc.Or):
- raise TypeError(f"Unexpected operator: {op}")
- value = self._eval_expr(self._visit(node.values[0]))
- for rhs_node in node.values[1:]:
- if isinstance(value, bool):
- if isinstance(op, doc.And) and not value:
- return value
- if isinstance(op, doc.Or) and value:
- return value
- rhs = self._eval_expr(self._visit(rhs_node))
- value = _eval_op(op, values=[value, rhs])
- return value
-
- def _eval_compare(self, fields: dict[str, Any]) -> Any:
- """The doc AST comparison operation node evaluating method.
-
- Parameters
- ----------
- fields : Dict[str, Any]
- The dictionary of comparison operation information,
- e.g., operator types, operand values.
-
- Returns
- -------
- res : Any
- The evaluation result.
- """
- values = [self._eval_expr(fields["left"])]
- values.extend([self._eval_expr(rhs) for rhs in fields["comparators"]])
- result = None
- assert len(fields["ops"]) == len(values) - 1
-
- for index, op in enumerate(fields["ops"]):
- sub_result = _eval_op(op, values=[values[index], values[index + 1]])
- if result is None:
- result = sub_result
- else:
- result = _eval_op(doc.And(), values=[result, sub_result])
- return result
-
- def _eval_unary_op(self, fields: dict[str, Any]) -> Any:
- """The doc AST unary operation node evaluating method.
-
- Parameters
- ----------
- fields : Dict[str, Any]
- The dictionary of unary operation information,
- e.g., operator types, operand values.
-
- Returns
- -------
- res : Any
- The evaluation result.
- """
- value = self._eval_expr(fields["operand"])
- value = _eval_op(fields["op"], values=[value])
- return value
-
- def _eval_bin_op(self, fields: dict[str, Any]) -> Any:
- """The doc AST binary operation node evaluating method.
-
- Parameters
- ----------
- fields : Dict[str, Any]
- The dictionary of binary operation information,
- e.g., operator types, operand values.
-
- Returns
- -------
- res : Any
- The evaluation result.
- """
- return _eval_op(
- fields["op"],
- values=[
- self._eval_expr(fields["left"]),
- self._eval_expr(fields["right"]),
- ],
- )
-
- def _eval_if_exp(self, node: doc.IfExp) -> Any:
- """The doc AST if-else expression node evaluating method.
-
- Parameters
- ----------
- node : doc.IfExp
- The conditional expression to evaluate.
-
- Returns
- -------
- res : Any
- The evaluation result.
- """
- test = self._eval_expr(self._visit(node.test))
- if isinstance(test, bool):
- selected = node.body if test else node.orelse
- return self._eval_expr(self._visit(selected))
- elif tvm.ir.is_prim_expr(test) and test.ty.matches_code(tvm.DataTypeCode.BOOL):
- body = self._eval_expr(self._visit(node.body))
- orelse = self._eval_expr(self._visit(node.orelse))
- return tvm.tirx.op.if_then_else(test, body, orelse)
- else:
- raise TypeError(f"Expected Python bool or TIR bool, but got {type(test)}")
-
- def _eval_slice(self, fields: dict[str, Any]) -> slice:
- """The doc AST slice node evaluating method.
-
- Parameters
- ----------
- fields : Dict[str, Any]
- The dictionary of slice information,
- e.g., lower bound, upper bound, step.
-
- Returns
- -------
- res : slice
- The evaluation result.
- """
- lower, upper, step = fields["lower"], fields["upper"], fields["step"]
-
- lower = self._eval_expr(lower) if lower is not None else None
- upper = self._eval_expr(upper) if upper is not None else None
- step = self._eval_expr(step) if step is not None else None
-
- return slice(lower, upper, step)
-
- def _eval_expr(self, v: Any) -> Any:
- """The doc AST expression node evaluating method.
-
- Parameters
- ----------
- v : Any
- The root node of AST tree node of expression to evaluate.
-
- Returns
- -------
- res : Any
- The evaluation result.
- """
- return _eval_expr(v, self.value_table)
-
-
-def eval_expr(
- parser: "Parser",
- node: doc.expr | doc.Expression,
- dict_globals: dict[str, Any] | None,
-) -> Any:
- """Expression evaluation for TVMScript parser.
-
- Parameters
- ----------
- parser : Parser
- The parser bound with the evaluator.
-
- node : Union[doc.expr, doc.Expression]
- The root node of AST tree node of expression to evaluate.
-
- dict_globals : Optional[Dict[str, Any]]
- The optional global value table for expression evaluation.
-
- Returns
- -------
- res : Any
- The evaluation result.
- """
- value_table = {}
- if dict_globals is not None:
- value_table.update(dict_globals)
- return ExprEvaluator.eval(parser, value_table, node)
-
-
-def eval_assign(
- parser: "Parser",
- target: doc.expr,
- source: Any,
-) -> dict[str, Any]:
- """Expression assignment evaluation for TVMScript parser.
-
- Parameters
- ----------
- parser : Parser
- The parser bound with the evaluator.
-
- target : doc.expr
- The root node of AST tree node of assigned expression to evaluate.
-
- source : Any
- The source to be assigned with evaluated expression.
-
- Returns
- -------
- res : Any
- The evaluation result.
- """
- try:
- return _eval_assign(target, source)
- except Exception as err: # pylint: disable=broad-except
- parser.report_error(target, err)
- raise
-
-
-def _eval_expr(
- node: doc.expr | doc.Expression,
- dict_globals: dict[str, Any] | None,
-) -> Any:
- """Expression evaluation implementation for TVMScript parser.
-
- Parameters
- ----------
- node : Union[doc.expr, doc.Expression]
- The root node of AST tree node of expression to evaluate.
-
- dict_globals : Optional[Dict[str, Any]]
- The optional global value table for expression evaluation.
-
- Returns
- -------
- res : Any
- The evaluation result.
- """
- node = doc.from_doc(node)
- if isinstance(node, ast.expr):
- node = ast.Expression(body=node)
- assert isinstance(node, ast.Expression), "Expects an ast.Expression, but gets: " + str(node)
- if dict_globals is None:
- dict_globals = {}
- node = ast.fix_missing_locations(node)
- exe = compile(node, filename="<ast>", mode="eval")
- return eval(exe, dict_globals) # pylint: disable=eval-used
-
-
-def _eval_op(
- op: doc.AST,
- values: list[Any],
-):
- """Operation expression evaluation implementation for TVMScript parser.
-
- Parameters
- ----------
- op : doc.AST
- The root node of AST tree node of operation expression to evaluate.
-
- values : List[Any]
- The list of values of operands.
-
- Returns
- -------
- res : Any
- The evaluation result.
- """
- op_type = type(op) # pylint: disable=protected-access
- for i, v in enumerate(values):
- v_type = getattr(type(v), "_dispatch_type", None)
- if v_type is None:
- continue
- f = dispatch.get_op(
- operand_type=v_type, op_node_type=op_type, operand_index=i, default=None
- )
- if f is not None:
- return f(*values)
- return DEFAULT_OP[op_type](*values)
-
-
-def _eval_assign(
- target: doc.expr,
- source: Any,
-) -> dict[str, Any]:
- """Expression assignment evaluation implementation for TVMScript parser.
-
- Parameters
- ----------
- target : doc.expr
- The root node of AST tree node of assigned expression to evaluate.
-
- source : Any
- The source to be assigned with evaluated expression.
-
- Returns
- -------
- res : Any
- The evaluation result.
- """
- target = doc.from_doc(target)
- assert isinstance(target, ast.expr)
- RHS_VAR_NAME = "__tvm_rhs_var__" # pylint: disable=invalid-name
- rhs_var_name = RHS_VAR_NAME
- dict_locals = {rhs_var_name: source}
- mod = ast.fix_missing_locations(
- ast.Module(
- body=[
- ast.Assign(
- targets=[target],
- value=ast.Name(
- id=rhs_var_name,
- ctx=ast.Load(),
- ),
- )
- ],
- type_ignores=[],
- )
- )
- exe = compile(mod, filename="<ast>", mode="exec")
- exec(exe, {}, dict_locals) # pylint: disable=exec-used
- del dict_locals[rhs_var_name]
- return dict_locals
diff --git a/python/tvm/script/parser/core/parser.py b/python/tvm/script/parser/core/parser.py
deleted file mode 100644
index 6319eacb34..0000000000
--- a/python/tvm/script/parser/core/parser.py
+++ /dev/null
@@ -1,1071 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-"""The core parser"""
-
-import abc
-import ast
-import inspect
-from collections import defaultdict
-from collections.abc import Callable
-from contextlib import contextmanager
-from typing import Any, TypeVar
-
-import numpy as np
-
-from tvm.error import DiagnosticError
-from tvm.ir import GlobalVar, Var
-from tvm.runtime import Object
-
-from ...ir_builder import IRBuilder
-from . import dispatch, doc
-from .diagnostics import Diagnostics, Source
-from .evaluator import eval_assign, eval_expr
-
-DEFAULT_VISIT = {
- "Interactive",
- "Module",
- "Expression",
- "Pass",
-}
-
-
-def collect_signature_type_vars(parser: "Parser", node: doc.FunctionDef) -> dict[str, Var]:
- """Collect symbolic variables declared by function type-parameter syntax."""
-
- annotation_names = set()
- parameter_names = {arg.arg for arg in node.args.args}
-
- class AnnotationNameCollector(doc.NodeVisitor):
- def visit_Name(self, name): # pylint: disable=invalid-name
- annotation_names.add(name.id)
-
- def visit_Constant(self, constant): # pylint: disable=invalid-name
- if not isinstance(constant.value, str):
- return
- try:
- expression = ast.parse(constant.value, mode="eval")
- except SyntaxError:
- return
- annotation_names.update(
- child.id for child in ast.walk(expression) if isinstance(child, ast.Name)
- )
-
- name_collector = AnnotationNameCollector()
- for arg in node.args.args:
- name_collector.visit(arg.annotation)
- name_collector.visit(node.returns)
-
- symbolic_vars = {}
- for binding_name, value in parser.var_table.get().items():
- if (
- binding_name not in annotation_names
- or binding_name in parameter_names
- or not isinstance(value, TypeVar)
- ):
- continue
- if value.__name__ != binding_name:
- parser.report_error(
- node,
- f"TypeVar binding {binding_name!r} must match its declared name {value.__name__!r}",
- )
- if value.__constraints__ or value.__bound__ is not None:
- parser.report_error(
- node,
- f"Symbolic TypeVar {binding_name!r} must not have constraints or a bound",
- )
- symbolic_vars[binding_name] = Var(binding_name, "int64")
-
- for type_param in node.type_params or []:
- if not isinstance(type_param, doc.TypeVar):
- parser.report_error(type_param, "Only PEP 695 TypeVar parameters are supported")
- # Both a bare parameter and the optional ``int`` bound declare the
- # int64 PrimVar represented by this type parameter. The printer uses
- # the bare spelling, while accepting the bound as explicit input.
- if type_param.bound is not None and not (
- isinstance(type_param.bound, doc.Name) and type_param.bound.id == "int"
- ):
- parser.report_error(
- type_param,
- f"Symbolic TypeVar {type_param.name!r} must be unannotated or annotated as int",
- )
- if type_param.default_value is not None:
- parser.report_error(
- type_param,
- f"Symbolic TypeVar {type_param.name!r} must not have a default",
- )
- symbolic_vars[type_param.name] = Var(type_param.name, "int64")
- return symbolic_vars
-
-
-def _deferred(exit_f: Callable[[], None]):
- """Created context with certain exit function.
-
- Parameters
- ----------
- exit_f : Callable[[], None]
- The function to call when exiting the context.
-
- Returns
- -------
- res : Any
- The created context.
- """
-
- @contextmanager
- def context():
- try:
- yield
- finally:
- exit_f()
-
- return context()
-
-
-def _do_nothing(*args, **kwargs): # pylint: disable=unused-argument
- pass
-
-
-class ScriptMacro(abc.ABC):
- """Representation of a script macro.
-
- This is a callable object, intended to be called from the expression evaluator.
- The evaluator is expected to insert the current parser into the environment
- undef the name given by "parser_object_name".
-
- Once called, the ScriptMacro object will locate the current parser, and use it
- to parse the macro's body and produce the result.
-
- There were two major considerations for this design:
- 1. Implementing hygienic and non-hygienic macros.
- 2. Implementing macros that return values.
-
- Macro uses in TIR are only allowed at a statement-level, and they don't produce
- any values. Parsing of such macros could easily be done by intercepting doc.Call
- nodes in the TIR parser. If a macro is a value-producing expression, then there
- may not be a direct way to intercept calls to it if it's embedded in a complex
- expression. Because macros use function-call syntax, the evaluator will try to
- call the macro object, which this design relies on to parse and evaluate the macro.
- """
-
- parser_object_name = "__current_script_parser__"
-
- def __init__(
- self,
- source: Source,
- closure_vars: dict[str, Any],
- func: Callable,
- hygienic: bool,
- ) -> None:
- self.source = source
- self.closure_vars = closure_vars
- self.func = func
- self.hygienic = hygienic
-
- def __repr__(self):
- return self.source.source
-
- @abc.abstractmethod
- def parse_macro(self, parser: "Parser") -> Any:
- """The main macro parsing function. Different scripts may have different
- ways to parse a macro, and to return a value to the evaluator.
-
- Parameters
- ----------
- parser : Parser
- The parser with the appropriate frame already created and populated depending
- macro's hygiene settings,
-
- Returns
- -------
- The return value depends on the specifics of the particular script. It can be
- "None" or any other value or any type.
- """
-
- def _find_parser_def(self):
- outer_frame_infos = inspect.getouterframes(inspect.currentframe())
- for finfo in outer_frame_infos:
- parser = finfo.frame.f_globals.get(ScriptMacro.parser_object_name)
- if parser is not None:
- return parser
- raise RuntimeError(f"{ScriptMacro.parser_object_name} not available")
-
- def get_macro_def(self):
- ast_module = self.source.as_ast()
- for decl in ast_module.body:
- if isinstance(decl, doc.FunctionDef) and decl.name == self.func.__name__:
- return decl
- raise RuntimeError(f"cannot find macro definition for {self.func.__name__}")
-
- def __call__(self, *args, **kwargs):
- param_binding = inspect.signature(self.func).bind(*args, **kwargs)
- param_binding.apply_defaults()
- local_vars = param_binding.arguments
- parser = self._find_parser_def()
-
- with parser.with_diag_source(self.source):
- if self.hygienic:
- saved_var_table = parser.var_table
- parser.var_table = VarTable()
-
- with parser.var_table.with_frame():
- for k, v in self.closure_vars.items():
- parser.var_table.add(k, v)
- for k, v in local_vars.items():
- parser.var_table.add(k, v)
-
- parse_result = self.parse_macro(parser)
-
- parser.var_table = saved_var_table
-
- else:
- with parser.var_table.with_frame():
- for k, v in local_vars.items():
- parser.var_table.add(k, v)
-
- parse_result = self.parse_macro(parser)
-
- return parse_result
-
-
-class VarTableFrame:
- """The variable table frame.
- A frame of variable table stores the variables created in one block or scope.
-
- Parameters
- ----------
- vars : Set[str]
- The set of variable names in the variable table frame.
- """
-
- vars: set[str]
-
- def __init__(self):
- self.vars = set()
-
- def add(self, var: str):
- """Add a new variable into variable table frame.
-
- Parameters
- ----------
- var : str
- The name of new variable.
- """
- if var in self.vars:
- raise ValueError(f"Variable {var} already defined in current scope")
- self.vars.add(var)
-
- def pop_all(self, fn_pop: Callable[[str], None]):
- """Pop out all variable in variable table frame.
-
- Parameters
- ----------
- fn_pop : Callable[[str], None]
- The methods to call when popping each variable.
- """
- for var in self.vars:
- fn_pop(var)
- self.vars.clear()
-
-
-class VarTable:
- """The variable table.
- A variable table stores the all variables when parsing TVMScript.
-
- Parameters
- ----------
- frames : List[VarTableFrame]
- The list or stack of variable table frame.
-
- name2value : Dict[str, List[Any]]
- The dictionary for variable table name-based query.
- """
-
- frames: list[VarTableFrame]
- name2value: dict[str, list[Any]]
-
- def __init__(self):
- self.frames = []
- self.name2value = defaultdict(list)
-
- def with_frame(self):
- """Create a new variable table frame as with statement.
-
- Returns
- -------
- res : Any
- The context with new variable table frame.
- """
-
- def pop_frame():
- frame = self.frames.pop()
- frame.pop_all(lambda name: self.name2value[name].pop())
-
- self.frames.append(VarTableFrame())
- return _deferred(pop_frame)
-
- def add(self, var: str, value: Any, allow_shadowing: bool = False):
- """Add a new variable to variable table.
-
- Parameters
- ----------
- var : str
- The name of variable.
-
- value : Any
- The value of variable.
-
- allow_shadowing : bool
- The options of whether variable shadowing allowed for this variable.
- """
- # Skip if the key and value are equal to those in the var_table
- if self.name2value[var] and isinstance(self.name2value[var][-1], type(value)):
- old_value = self.name2value[var][-1]
- if isinstance(value, np.ndarray) and (old_value == value).all():
- return
- if isinstance(old_value, Object):
- if old_value.same_as(value):
- return
- elif old_value == value:
- return
- if allow_shadowing and var in self.frames[-1].vars:
- # Shadowing
- self.name2value[var][-1] = value
- else:
- self.frames[-1].add(var)
- self.name2value[var].append(value)
-
- def get(self) -> dict[str, Any]:
- """Get a variable dictionary of latest variables.
-
- Returns
- -------
- res : Any
- The variable dictionary copy of latest variables.
- """
- return {key: values[-1] for key, values in self.name2value.items() if values}
-
- def contains_in_current_frame(self, name: str) -> bool:
- """Check whether a variable name exists in the current frame."""
- return bool(self.frames) and name in self.frames[-1].vars
-
- def get_at_depth(self, depth: int) -> dict[str, Any]:
- """Get variables visible at the given frame depth, using current values.
-
- For each variable name that appears in frames 0..depth-1, count how many
- times it was pushed (to handle shadowing), then index into name2value at
- count-1 to retrieve the latest value visible at that depth.
-
- Parameters
- ----------
- depth : int
- The frame depth (number of frames visible).
-
- Returns
- -------
- res : dict[str, Any]
- Variable dictionary of values visible at the given depth.
- """
- result: dict[str, Any] = {}
- name_count: dict[str, int] = defaultdict(int)
- for frame_idx in range(min(depth, len(self.frames))):
- for name in self.frames[frame_idx].vars:
- name_count[name] += 1
- for name, count in name_count.items():
- if self.name2value[name]:
- result[name] = self.name2value[name][count - 1]
- return result
-
- def exist(self, value: Any) -> bool:
- """Check if any value exists in variable table.
-
- Parameters
- ----------
- value : Any
- The value of variable.
-
- Returns
- -------
- res : bool
- The existence of the value.
- """
- return any(
- value.same_as(known_value)
- for known_value_stack in self.name2value.values()
- for known_value in known_value_stack
- )
-
-
-def _dispatch_wrapper(func: dispatch.ParseMethod) -> dispatch.ParseMethod:
- def _wrapper(self: "Parser", node: doc.AST) -> None:
- try:
- return func(self, node)
- except Exception as err: # pylint: disable=broad-except
- self.report_error(node, err)
- raise
-
- return _wrapper
-
-
-def _dispatch(self: "Parser", type_name: str) -> dispatch.ParseMethod:
- for token in [self.dispatch_tokens[-1], "default"]:
- func = dispatch.get(token=token, type_name=type_name, default=None)
- if func is not None:
- return _dispatch_wrapper(func)
- return _dispatch_wrapper(lambda self, node: self.generic_visit(node))
-
-
-class Parser(doc.NodeVisitor):
- """The TVMScript parser
-
- Parameters
- ----------
- diag : Diagnostics
- The diagnostics for error reporting.
-
- dispatch_tokens : List[str]
- The list of dispatching tokens to dispatching parsing method
- of different IRs and different doc AST structure.
-
- var_table : VarTable
- The variable table for parsing.
-
- absent_params : Dict[str, None]
- Function parameters removed by a compile-time specialization.
- """
-
- diag: Diagnostics
- dispatch_tokens: list[str]
- function_annotations: dict[str, dict[str, Any]] | None
- absent_params: dict[str, None]
- var_table: VarTable
- inside_function: bool # whether we are within a function
- current_class: str | None = None # current class being parsed
- base_py_module_context: bool = False # whether current class inherits from BasePyModule
-
- def __init__(
- self,
- source: Source,
- function_annotations: dict[str, dict[str, Any]],
- absent_params: dict[str, None] | None = None,
- ) -> None:
- self.diag = Diagnostics(source)
- self.dispatch_tokens = ["default"]
- self.function_annotations = function_annotations
- self.absent_params = absent_params or {}
- self.var_table = VarTable()
- self.inside_function = False
-
- def parse(self, extra_vars: dict[str, Any] | None = None) -> Any:
- """The main parse method for parser.
-
- Parameters
- ----------
- extra_vars : Optional[Dict[str, Any]]
- The optional global value table for parsing.
-
- Returns
- -------
- res : Any
- The doc AST node visiting result.
- """
- if extra_vars is None:
- extra_vars = {}
- with self.var_table.with_frame():
- for k, v in extra_vars.items():
- self.var_table.add(k, v)
- node = self.diag.source.as_ast()
- self.visit(node)
-
- def get_dispatch_token(self, node: doc.FunctionDef) -> str:
- if not isinstance(node, doc.FunctionDef):
- self.report_error(node, "Only can get dispatch token for function.")
- if not node.decorator_list:
- self.report_error(node, "Function must be decorated")
- # TODO: only the last decorator is parsed
- decorator = self.eval_expr(node.decorator_list[-1])
- if not hasattr(decorator, "dispatch_token"):
- self.report_error(node, "The parser does not understand the decorator")
- return decorator.dispatch_token
-
- def with_dispatch_token(self, token: str):
- """Add a new dispatching token as with statement.
-
- Parameters
- ----------
- token : str
- The dispatching token.
-
- Returns
- -------
- res : Any
- The context with new dispatching token.
- """
-
- self.dispatch_tokens.append(token)
- enter_func = dispatch.get(token=token, type_name="enter_token", default=lambda *args: None)
- context = enter_func(self)
-
- def pop_token():
- exit_func = dispatch.get(
- token=token, type_name="exit_token", default=lambda *args: None
- )
- exit_func(self, context)
- self.dispatch_tokens.pop()
-
- return _deferred(pop_token)
-
- def set_class_context(self, class_name: str, is_base_py_module: bool = False):
- """Set the current class context for parsing.
-
- Parameters
- ----------
- class_name : str
- The name of the current class being parsed.
- is_base_py_module : bool
- Whether the current class inherits from BasePyModule.
- """
- self.current_class = class_name
- self.base_py_module_context = is_base_py_module
-
- def _get_current_class_context(self) -> str | None:
- """Get the current class context.
-
- Returns
- -------
- Optional[str]
- The name of the current class, or None if not in a class context.
- """
- return self.current_class
-
- def _is_base_py_module_context(self) -> bool:
- """Check if the current class context allows Python functions.
-
- Returns
- -------
- bool
- True if Python functions are allowed in the current context.
- """
- return self.base_py_module_context
-
- def with_diag_source(self, source: Source):
- """Add a new source as with statement.
-
- Parameters
- ----------
- source : Source
- The source for diagnostics.
-
- Returns
- -------
- res : Any
- The context with new source.
- """
-
- last_diag = self.diag
- self.diag = Diagnostics(source)
-
- def pop_source():
- self.diag = last_diag
-
- return _deferred(pop_source)
-
- @contextmanager
- def with_source_span(self, node: doc.AST):
- """Make ``node``'s source range active while constructing its IR."""
- if (
- not IRBuilder.is_in_scope()
- or getattr(node, "lineno", None) is None
- or getattr(node, "col_offset", None) is None
- ):
- yield
- return
- with IRBuilder.current().with_source_span(self.diag.source.to_span(node)):
- yield
-
- def annotate_current_source_span(self, value: Any) -> Any:
- """Attach the active parser span to an expression result, when applicable."""
- if isinstance(value, Object) and IRBuilder.is_in_scope():
- return IRBuilder.current()._set_current_source_span(value) # pylint: disable=protected-access
- return value
-
- def eval_expr(
- self,
- node: doc.Expression | doc.expr,
- extra_vars: dict[str, Any] | None = None,
- ) -> Any:
- """Expression evaluation when parsing.
-
- Parameters
- ----------
- node : Union[doc.expr, doc.Expression]
- The root node of AST tree node of expression to evaluate.
-
- extra_vars : Optional[Dict[str, Any]]
- The optional global value table for expression evaluation.
-
- Returns
- -------
- res : Any
- The evaluation result.
- """
- var_values = self.var_table.get()
- if extra_vars is not None:
- for k, v in extra_vars.items():
- var_values[k] = v
- var_values[ScriptMacro.parser_object_name] = self
- value = eval_expr(self, node, var_values)
-
- return self.annotate_current_source_span(value)
-
- def _duplicate_lhs_check(self, target: doc.expr) -> bool | set[str]:
- """Check whether duplicate lhs exists in assignment.
-
- Parameters
- ----------
- target : doc.expr
- The doc AST expr node for lhs.
-
- Returns
- -------
- res : Union[bool, Set[str]]
- The result of true if duplicate lhs exists,
- or the set of lhs names if no duplicate lhs exists.
- """
- if isinstance(target, doc.Tuple | doc.List):
- vars: set[str] = set() # pylint: disable=redefined-builtin
- for i in target.elts:
- res = self._duplicate_lhs_check(i)
- if isinstance(res, bool) and res:
- return True
- assert isinstance(res, set)
- if vars & res:
- return True
- vars = vars.union(res)
- return vars
- elif isinstance(target, doc.Name):
- return {target.id}
- elif isinstance(target, doc.Starred):
- return self._duplicate_lhs_check(target.value)
- elif isinstance(target, doc.Attribute):
- # Attribute assignment like packedB.data = ..., treated as rebinding.
- return {target.attr}
- else:
- self.report_error(target, "Invalid type in assign statement")
- raise NotImplementedError
-
- def eval_assign(
- self,
- target: doc.expr,
- source: Any,
- bind_value: Callable[["Parser", doc.expr, str, Any], Any],
- allow_shadowing: bool = False,
- ) -> dict[str, Any]:
- """Expression assignment evaluation when parsing.
-
- Parameters
- ----------
- target : doc.expr
- The root node of AST tree node of assigned expression to evaluate.
-
- source : Any
- The source to be assigned with evaluated expression.
-
- bind_value : Callable[["Parser", doc.expr, str, Any], Any]
- The value binding method when assigning the values to variables.
-
- allow_shadowing : bool
- The options of whether variable shadowing allowed for assignment.
-
- Returns
- -------
- res : Dict[str, Any]
- The dictionary of assignment result.
- """
- if self._duplicate_lhs_check(target) is True:
- self.report_error(target, "Duplicate vars assigned.")
- var_values = eval_assign(self, target, source)
- for k, v in var_values.items():
- var = bind_value(self, target, k, v)
- self.var_table.add(k, var, allow_shadowing)
- return var_values
-
- def report_error(self, node: doc.AST, err: Exception | str) -> None: # pylint: disable=no-self-use
- """The error reporting when parsing.
-
- Parameters
- ----------
- node : doc.AST
- The doc AST node with errors.
-
- err: Union[Exception, str]
- The error to report.
- """
-
- # If the error is already being raised as a DiagnosticError,
- # re-raise it without wrapping it in a DiagnosticContext.
- if isinstance(err, DiagnosticError):
- raise err
-
- # Only take the last line of the error message
- if isinstance(err, RuntimeError):
- lines = list(filter(None, str(err).split("\n")))
- msg = lines[-1] if lines else (str(err) or type(err).__name__)
- elif isinstance(err, KeyError):
- msg = "KeyError: " + str(err)
- else:
- msg = str(err)
-
- try:
- self.diag.error(node, msg)
- except Exception as diag_err:
- # Calling self.diag.error is guaranteed to throw an
- # exception. When shown to a user, this error should
- # reference the point of error within the provided
- # TVMScript. However, when caught in pdb, the full
- # traceback should be available for debugging.
- if isinstance(err, Exception):
- diag_err = diag_err.with_traceback(err.__traceback__)
- raise diag_err
-
- def visit(self, node: doc.AST) -> None:
- """The general visiting method.
-
- Parameters
- ----------
- node : doc.AST
- The doc AST node.
-
- Returns
- -------
- res : Any
- The visiting result.
- """
- if isinstance(node, list | tuple):
- for item in node:
- self.visit(item)
- return
- if not isinstance(node, doc.AST):
- return
- name = node.__class__.__name__.split(".")[-1]
- if name in DEFAULT_VISIT:
- func = self.generic_visit
- else:
- func = getattr(self, "visit_" + name, None)
- if func is None:
- raise NotImplementedError(f"Visitor of AST node is not implemented: {name}")
- try:
- with self.with_source_span(node):
- func(node)
- except Exception as err: # pylint: disable=broad-except
- self.report_error(node, err)
-
- def visit_body(self, node: list[doc.stmt]) -> Any:
- """The general body visiting method.
-
- Parameters
- ----------
- node : List[doc.stmt]
- The list of statements in body.
-
- Returns
- -------
- res : Any
- The visiting result.
- """
- for stmt in node:
- self.visit(stmt)
-
- def visit_tvm_annotation(self, node: doc.expr) -> Any:
- """The general TVM annotation visiting method.
-
- Parameters
- ----------
- node : doc.expr
- The doc AST expr node.
-
- Returns
- -------
- res : Any
- The visiting result.
- """
- return _dispatch(self, "tvm_annotation")(self, node)
-
- def visit_FunctionDef(self, node: doc.FunctionDef) -> None: # pylint: disable=invalid-name
- """The general function definition visit method.
-
- Parameters
- ----------
- node : doc.FunctionDef
- The doc FunctionDef node.
- """
- token = self.get_dispatch_token(node)
- func = dispatch.get(token=token, type_name="FunctionDef", default=None)
- if func is None:
- self.report_error(
- node,
- """The parser does not understand the decorator,
- or visit_FunctionDef is not implemented for the decorator with token: """
- + token,
- )
- _dispatch(self, "pre_visit_local_function")(self, node)
- _dispatch_wrapper(func)(self, node)
- _dispatch(self, "post_visit_local_function")(self, node)
-
- def visit_tvm_declare_function(self, node: doc.FunctionDef) -> GlobalVar:
- token = self.get_dispatch_token(node)
- with self.with_dispatch_token(token):
- return _dispatch(self, "tvm_declare_function")(self, node)
-
- def visit_ClassDef(self, node: doc.ClassDef) -> Any: # pylint: disable=invalid-name
- """The general class definition visiting method.
-
- Parameters
- ----------
- node : doc.ClassDef
- The doc AST class definition node.
-
- Returns
- -------
- res : Any
- The visiting result.
- """
- func = dispatch.get(token="ir", type_name="ClassDef", default=None)
- if func is None:
- self.report_error(node, "The parser does not understand the decorator")
- _dispatch_wrapper(func)(self, node)
-
- def visit_ImportFrom(self, node: doc.ImportFrom) -> None: # pylint: disable=invalid-name
- """Accept postponed annotations emitted by the PEP 695 printer."""
- is_future_annotations = (
- node.module == "__future__"
- and node.level == 0
- and len(node.names) == 1
- and node.names[0].name == "annotations"
- and node.names[0].asname is None
- )
- if not is_future_annotations:
- self.report_error(node, "Only 'from __future__ import annotations' is supported")
-
- def visit_arguments(self, node: doc.arguments) -> Any:
- """The general arguments visiting method.
-
- Parameters
- ----------
- node : doc.arguments
- The doc AST arguments node.
-
- Returns
- -------
- res : Any
- The visiting result.
- """
- return _dispatch(self, "arguments")(self, node)
-
- def visit_For(self, node: doc.For) -> Any: # pylint: disable=invalid-name
- """The general for visiting method.
-
- Parameters
- ----------
- node : doc.For
- The doc AST for node.
-
- Returns
- -------
- res : Any
- The visiting result.
- """
- return _dispatch(self, "For")(self, node)
-
- def visit_While(self, node: doc.While) -> Any: # pylint: disable=invalid-name
- """The general while visiting method.
-
- Parameters
- ----------
- node : doc.While
- The doc AST while node.
-
- Returns
- -------
- res : Any
- The visiting result.
- """
- return _dispatch(self, "While")(self, node)
-
- def visit_With(self, node: doc.With) -> Any: # pylint: disable=invalid-name
- """The general with visiting method.
-
- Parameters
- ----------
- node : doc.With
- The doc AST with node.
-
- Returns
- -------
- res : Any
- The visiting result.
- """
- return _dispatch(self, "With")(self, node)
-
- def visit_Assign(self, node: doc.Assign) -> Any: # pylint: disable=invalid-name
- """The general assign visiting method.
-
- Parameters
- ----------
- node : doc.Assign
- The doc AST assign node.
-
- Returns
- -------
- res : Any
- The visiting result.
- """
- return _dispatch(self, "Assign")(self, node)
-
- def visit_AnnAssign(self, node: doc.AnnAssign) -> Any: # pylint: disable=invalid-name
- """The general annotated assign visiting method.
-
- Parameters
- ----------
- node : doc.Assign
- The doc AST annotated assign node.
-
- Returns
- -------
- res : Any
- The visiting result.
- """
- return _dispatch(self, "AnnAssign")(self, node)
-
- def visit_Expr(self, node: doc.Expr) -> Any: # pylint: disable=invalid-name
- """The general expression visiting method.
-
- Parameters
- ----------
- node : doc.Expr
- The doc AST expression node.
-
- Returns
- -------
- res : Any
- The visiting result.
- """
- return _dispatch(self, "Expr")(self, node)
-
- def visit_If(self, node: doc.If) -> Any: # pylint: disable=invalid-name
- """The general if visiting method.
-
- Parameters
- ----------
- node : doc.If
- The doc AST if node.
-
- Returns
- -------
- res : Any
- The visiting result.
- """
- return _dispatch(self, "If")(self, node)
-
- def visit_AugAssign(self, node: doc.AugAssign) -> Any: # pylint: disable=invalid-name
- """The general augmented assignment visiting method.
-
- Parameters
- ----------
- node : doc.AugAssign
- The doc AST augmented assignment node.
-
- Returns
- -------
- res : Any
- The visiting result.
- """
- return _dispatch(self, "AugAssign")(self, node)
-
- def visit_Assert(self, node: doc.Assert) -> Any: # pylint: disable=invalid-name
- """The general assert visiting method.
-
- Parameters
- ----------
- node : doc.Assert
- The doc AST assert node.
-
- Returns
- -------
- res : Any
- The visiting result.
- """
- return _dispatch(self, "Assert")(self, node)
-
- def visit_Return(self, node: doc.Return) -> Any: # pylint: disable=invalid-name
- """The general return visiting method.
-
- Parameters
- ----------
- node : doc.Return
- The doc AST return node.
-
- Returns
- -------
- res : Any
- The visiting result.
- """
- return _dispatch(self, "Return")(self, node)
-
- def visit_Continue(self, node: doc.Continue) -> Any: # pylint: disable=invalid-name
- """The general continue visiting method.
-
- Parameters
- ----------
- node : doc.Continue
- The doc AST continue node.
-
- Returns
- -------
- res : Any
- The visiting result.
- """
- return _dispatch(self, "Continue")(self, node)
-
- def visit_Break(self, node: doc.Break) -> Any: # pylint: disable=invalid-name
- """The general break visiting method.
-
- Parameters
- ----------
- node : doc.Break
- The doc AST break node.
-
- Returns
- -------
- res : Any
- The visiting result.
- """
- return _dispatch(self, "Break")(self, node)
-
- def visit_Nonlocal(self, node: doc.Nonlocal) -> Any: # pylint: disable=invalid-name
- """The general nonlocal visiting method.
-
- Parameters
- ----------
- node : doc.Nonlocal
- The doc AST nonlocal node.
-
- Returns
- -------
- res : Any
- The visiting result.
- """
- return _dispatch(self, "Nonlocal")(self, node)
diff --git a/python/tvm/script/parser/core/utils.py b/python/tvm/script/parser/core/utils.py
deleted file mode 100644
index fc8a928e05..0000000000
--- a/python/tvm/script/parser/core/utils.py
+++ /dev/null
@@ -1,212 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-"""TVM Script Parser utils"""
-
-import inspect
-from collections.abc import Callable
-from types import FrameType
-from typing import Any
-
-from .diagnostics import findsource
-
-
-def get_func_nonlocals(func):
- """A modified version of `inspect.getclosurevars`"""
-
- if inspect.ismethod(func):
- func = func.__func__
-
- if not inspect.isfunction(func):
- raise TypeError(f"{func!r} is not a Python function")
-
- code = func.__code__
- # Nonlocal references are named in co_freevars and resolved
- # by looking them up in __closure__ by positional index
- nonlocal_vars = {}
- if func.__closure__ is not None:
- for var, cell in zip(code.co_freevars, func.__closure__):
- try:
- nonlocal_vars[var] = cell.cell_contents
- except ValueError as err:
- # cell_contents may raise ValueError if the cell is empty.
- if "empty" not in str(err):
- raise
- return nonlocal_vars
-
-
-def inspect_function_capture(func: Callable) -> dict[str, Any]:
- """Capture function non-locals and global variables.
-
- Parameters
- ----------
- func : Callable
- The function to inspect.
-
- Returns
- -------
- res : Dict[str, Any]
- The function variables map with non-local or global variables.
- """
- captured = {
- **func.__globals__, # type: ignore
- **get_func_nonlocals(func),
- }
- return captured
-
-
-def inspect_class_capture(cls: type) -> dict[str, Any]:
- """Capture class non-locals and global variables.
-
- Parameters
- ----------
- cls : type
- The class to inspect.
-
- Returns
- -------
- res : Dict[str, Any]
- The class variables map with non-local or global variables.
- """
- result: dict[str, Any] = {}
- for _, v in cls.__dict__.items():
- if inspect.isfunction(v):
- func_vars = inspect_function_capture(v)
- result.update(**func_vars)
- return result
-
-
-def _collect_annotation_names(source_obj: type | Callable) -> set[str]:
- """Parse source AST to find names used in function annotations.
-
- Returns the set of ``ast.Name`` identifiers found inside argument
- annotations and return annotations of any function definitions in
- *source_obj*.
- """
- import ast
- import textwrap
-
- try:
- source = textwrap.dedent(inspect.getsource(source_obj))
- tree = ast.parse(source)
- except (OSError, TypeError):
- return set()
-
- names: set[str] = set()
- for node in ast.walk(tree):
- if isinstance(node, ast.FunctionDef | ast.AsyncFunctionDef):
- for arg in node.args.args + node.args.posonlyargs + node.args.kwonlyargs:
- if arg.annotation:
- for n in ast.walk(arg.annotation):
- if isinstance(n, ast.Name):
- names.add(n.id)
- if node.returns:
- for n in ast.walk(node.returns):
- if isinstance(n, ast.Name):
- names.add(n.id)
- return names
-
-
-def _has_string_annotations(source_obj: type | Callable) -> bool:
- """Check if *source_obj* has stringified annotations (PEP 563)."""
- if inspect.isclass(source_obj):
- return any(
- isinstance(a, str)
- for v in source_obj.__dict__.values()
- if inspect.isfunction(v)
- for a in v.__annotations__.values()
- )
- return any(isinstance(a, str) for a in getattr(source_obj, "__annotations__", {}).values())
-
-
-def _get_enclosing_scope_names(qualname: str) -> set[str]:
- """Extract lexically enclosing scope names from ``__qualname__``.
-
- For ``outer.<locals>.inner.<locals>.func`` this returns ``{"outer", "inner"}``.
- """
- parts = qualname.split(".")
- return {p for p in parts[:-1] if p != "<locals>"}
-
-
-def resolve_closure_vars(
- source_obj: type | Callable, extra_vars: dict[str, Any], outer_stack: list
-) -> None:
- """Resolve closure variables hidden by PEP 563.
-
- With ``from __future__ import annotations``, variables used only in
- annotations are not captured in ``__closure__``. This function parses
- the source AST to find names used in function annotations, then looks
- them up in lexically enclosing scope frames identified via
- ``__qualname__``.
-
- Only triggered when annotations are actually strings (PEP 563 active).
- Only annotation-referenced names are added, and only from enclosing
- scopes — not from arbitrary caller frames.
-
- Works for both classes (``@I.ir_module``) and functions (``@T.prim_func``).
- """
- if not _has_string_annotations(source_obj):
- return
- ann_names = _collect_annotation_names(source_obj)
- enclosing = _get_enclosing_scope_names(source_obj.__qualname__)
- for name in ann_names:
- if name not in extra_vars:
- for frame_info in outer_stack[1:]:
- if frame_info.frame.f_code.co_name in enclosing:
- if name in frame_info.frame.f_locals:
- extra_vars[name] = frame_info.frame.f_locals[name]
- break
-
-
-def is_defined_in_class(frames: list[FrameType], obj: Any) -> bool:
- """Check whether a object is defined in a class scope.
-
- Parameters
- ----------
- frames : List[FrameType]
- The frame stack of the object, obtained by `inspect.stack()`.
-
- Returns
- -------
- res : bool
- The result if the object is defined in a class scope.
- """
-
- def _is_tvmscript_class_annotator(line: str) -> bool:
- """Checks if the line contains a TVMScript annotator for a class
-
- These match either `@I.ir_module` or `@R.rewriter`, or their
- imported names `@ir_module` or `@rewriter`.
- """
-
- return line.startswith("@") and ("ir_module" in line or "rewriter" in line)
-
- if len(frames) > 2:
- frame_info = frames[2]
- code_context = frame_info.code_context
- if code_context is None:
- return False
- line = code_context[0].strip()
- if _is_tvmscript_class_annotator(line):
- return True
- if line.startswith("class"):
- lineno = frame_info.lineno
- if lineno >= 2:
- source, _ = findsource(obj)
- line = source[lineno - 2].strip()
- if _is_tvmscript_class_annotator(line):
- return True
- return False
diff --git a/python/tvm/script/parser/diagnostics.py b/python/tvm/script/parser/diagnostics.py
new file mode 100644
index 0000000000..af25eebc35
--- /dev/null
+++ b/python/tvm/script/parser/diagnostics.py
@@ -0,0 +1,157 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Source-located construction errors without replacing their original traceback."""
+
+import ast
+import linecache
+import traceback
+
+from tvm.error import DiagnosticError
+
+
+def diagnostic_error(error, compiler):
+ """Return a DiagnosticError highlighting the original source operation.
+
+ error is the caught Python/builder exception. compiler supplies the source
+ filename and original AST for fallback coordinates. Builder errors carry
+ plain location tuples; otherwise Python traceback/SyntaxError coordinates
+ identify the innermost original operation. Python 3.11+ supplies precise
+ traceback columns, while earlier versions fall back to the source line.
+ No frame or IR object is inspected; the caller attaches error as __cause__.
+ """
+ filename = compiler.filename
+ location = getattr(error, "__tvm_script_location__", None)
+ if location is not None:
+ filename, start, end, column, end_column = location
+ elif isinstance(error, SyntaxError) and error.lineno:
+ start = error.lineno
+ end = error.end_lineno or start
+ column = max((error.offset or 1) - 1, 0)
+ end_column = max((error.end_offset or column + 2) - 1, column + 1)
+ else:
+ frames = [
+ frame
+ for frame in traceback.extract_tb(error.__traceback__)
+ if frame.filename == filename
+ ]
+ if frames:
+ frame = frames[-1]
+ start, end = frame.lineno, getattr(frame, "end_lineno", None) or frame.lineno
+ column = getattr(frame, "colno", None) or 0
+ end_column = getattr(frame, "end_colno", None)
+ if getattr(frame, "end_lineno", None) is None:
+ # Python before 3.11 supplies only a line. Recover the original
+ # statement range from source AST, without an IR span context or
+ # executing/reparsing a generated program. Prefer the narrowest
+ # statement beginning on that line (a nested body over its def).
+ candidates = [
+ node
+ for node in ast.walk(compiler.tree)
+ if isinstance(node, ast.stmt) and node.lineno == start
+ ]
+ if candidates:
+ node = min(candidates, key=lambda item: item.end_lineno - item.lineno)
+ end, column, end_column = node.end_lineno, node.col_offset, node.end_col_offset
+ else:
+ node = compiler.tree.body[-1]
+ start, end = node.lineno, node.lineno
+ column, end_column = node.col_offset, None
+ lines = [f"{filename}:{start}: {type(error).__name__}: {error}"]
+ for number in range(start, end + 1):
+ source = linecache.getline(filename, number).rstrip("\n")
+ first = column if number == start else len(source) - len(source.lstrip())
+ last = end_column if number == end and end_column is not None else len(source)
+ lines.extend(
+ (
+ f" {number} | {source}",
+ " " * (len(str(number)) + 4 + first) + "^" * max(last - first, 1),
+ )
+ )
+ return DiagnosticError("\n".join(lines))
+
+
+def _format_source_snippet(
+ source_lines: list,
+ lineno: int,
+ col_offset: int,
+ end_lineno: int,
+ end_col_offset: int,
+) -> str:
+ """Format a source code snippet with a column/span marker.
+
+ Renders every source line spanned by the diagnostic (``lineno`` through
+ ``end_lineno``, inclusive) with a per-line gutter, followed by a caret
+ underline covering the offending span. For a single-line span
+ (``end_lineno == lineno``) only the columns ``col_offset`` (inclusive)
+ through ``end_col_offset`` (exclusive) are underlined; for a multi-line
+ span the underline covers the start column to end-of-line on the first
+ line, the full text of interior lines, and the start of the final line up
+ to ``end_col_offset``.
+
+ Parameters
+ ----------
+ source_lines : list of str
+ Lines of the source code.
+
+ lineno : int
+ 1-based starting line number in the source.
+
+ col_offset : int
+ 1-based starting column (inclusive) on ``lineno``.
+
+ end_lineno : int
+ 1-based ending line number in the source (>= ``lineno``).
+
+ end_col_offset : int
+ 1-based ending column (exclusive) on ``end_lineno``.
+
+ Returns
+ -------
+ snippet : str
+ Formatted source snippet with caret-marker line(s).
+ """
+ if end_lineno < lineno:
+ end_lineno = lineno
+
+ # Determine the gutter width so that all line numbers line up.
+ header_width = len(f" {end_lineno} ")
+ no_line_header = " " * header_width
+
+ parts = [f"{no_line_header}| "]
+ for cur_lineno in range(lineno, end_lineno + 1):
+ idx = cur_lineno - 1
+ if not 0 <= idx < len(source_lines):
+ continue
+ line_text = source_lines[idx].rstrip("\n")
+ line_header = f" {cur_lineno} ".rjust(header_width)
+
+ # Compute the underline span [start_col, stop_col) for this line.
+ start_col = col_offset if cur_lineno == lineno else 1
+ stop_col = end_col_offset if cur_lineno == end_lineno else len(line_text) + 1
+
+ marker = ""
+ for i in range(1, len(line_text) + 1):
+ if start_col <= i < stop_col:
+ marker += "^"
+ else:
+ marker += " "
+ parts.append(f"{line_header}| {line_text}")
+ parts.append(f"{no_line_header}| {marker}")
+
+ if len(parts) == 1:
+ return ""
+ return "\n".join(parts)
diff --git a/python/tvm/script/parser/expression.py b/python/tvm/script/parser/expression.py
new file mode 100644
index 0000000000..2ccb4e205e
--- /dev/null
+++ b/python/tvm/script/parser/expression.py
@@ -0,0 +1,283 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Syntax-only rewriting of registered constructor argument policies.
+
+Existing AST nodes retain all four Python location fields. Parsed strings use a
+UTF-8 byte-offset map through the original literal spelling, including escapes
+and physical newlines. If a literal cannot be recovered (for example a synthetic
+AST or adjacent concatenated literals), ranges conservatively fall back to the
+literal's complete original range. Rewriting never evaluates source expressions.
+"""
+
+import ast
+import copy
+import inspect
+import linecache
+import re
+
+from . import protocol
+
+
+class _LiteralParser:
+ # filename is immutable for one expression rewrite and owns no source/IR cache.
+ def __init__(self, filename):
+ self.filename = filename
+
+ def _string_expression(self, node):
+ try:
+ expression = ast.parse(node.value, mode="eval").body
+ except SyntaxError as error:
+ raise SyntaxError(f"Invalid annotation expression: {error.msg}") from error
+ # Example: in R.Tensor(("n + 1",), "float32"), the generated resolve
+ # call for n retains the byte range of n inside the quoted literal, not
+ # the whole constructor. A triple-quoted physical newline advances lineno;
+ # an escaped \n advances decoded input but maps back to the escape's real
+ # source bytes. Generated call wrappers copy these four mapped fields.
+ source = "".join(linecache.getlines(self.filename))
+ literal = ast.get_source_segment(source, node) if source else None
+ positions = self._literal_positions(literal, node) if literal else None
+ lines = node.value.splitlines(keepends=True)
+ for inner in ast.walk(expression):
+ if not hasattr(inner, "lineno"):
+ continue
+ for line_field, column_field in (
+ ("lineno", "col_offset"),
+ ("end_lineno", "end_col_offset"),
+ ):
+ line, column = getattr(inner, line_field), getattr(inner, column_field)
+ offset = len("".join(lines[: line - 1]).encode("utf-8")) + column
+ if positions is not None and offset in positions:
+ line, column = positions[offset]
+ else:
+ # Without a reliable spelling map (synthetic or concatenated
+ # literals), preserve the literal's entire real source range.
+ # Never invent physical lines from decoded escape sequences.
+ line = getattr(node, line_field)
+ column = getattr(node, column_field)
+ setattr(inner, line_field, line)
+ setattr(inner, column_field, column)
+ return expression
+
+ @staticmethod
+ def _literal_positions(literal, node):
+ """Map decoded expression byte offsets back through the literal's escapes."""
+ match = re.match("(?i:([rub]*))([\"'])", literal)
+ if match is None:
+ return None
+ prefix, quote = match.groups()
+ width = 3 if literal[len(prefix) :].startswith(quote * 3) else 1
+ start, stop = len(prefix) + width, len(literal) - width
+ delimiter = quote * width
+ positions, decoded, offset = {}, "", 0
+ index = start
+
+ def location(raw_index):
+ before = literal[:raw_index]
+ line = node.lineno + before.count("\n")
+ column = len(before.rsplit("\n", 1)[-1].encode("utf-8"))
+ return line, column + (node.col_offset if line == node.lineno else 0)
+
+ while index < stop:
+ end = index + 1
+ if literal[index] == "\\" and "r" not in prefix.lower():
+ escape = re.match(
+ r"\\(?:N\{[^}]*\}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|x[0-9a-fA-F]{2}|[0-7]{1,3}|\r?\n|.)",
+ literal[index:stop],
+ )
+ if escape:
+ end = index + len(escape.group())
+ piece = literal[index:end]
+ try:
+ value = ast.literal_eval(prefix + delimiter + piece + delimiter)
+ except (SyntaxError, ValueError):
+ return None
+ positions[offset] = location(index)
+ for char in value:
+ offset += len(char.encode("utf-8"))
+ positions[offset] = location(end)
+ decoded += value
+ index = end
+ return positions if decoded == node.value else None
+
+
+def rewrite_expression(
+ node, resolve, namespace, filename, *, annotation=False, parser_support="_PS"
+):
+ """Copy an expression and rewrite registered constructor arguments.
+
+ Parameters
+ ----------
+ node : ast.expr
+ Original source expression. It is never mutated.
+ resolve : callable
+ Static namespace lookup for Name and Attribute nodes. Returns host
+ namespaces or callables, or None, without executing descriptors.
+ namespace : str
+ Generated builder binding used for ``resolve_type_var`` calls.
+ filename : str
+ Original compilation filename and linecache source key.
+ annotation : bool, optional
+ Parse a quoted whole annotation before constructor-field rewriting.
+ Default is False. True also emits lexical bindings for names
+ introduced by signature expression strings.
+ parser_support : str, optional
+ Hygienically allocated shared parser-support binding. Global-info
+ arguments use its ``lookup_global_info`` helper. Default is ``"_PS"``.
+
+ Returns
+ -------
+ ast.expr
+ New expression AST preserving original source ranges.
+
+ Raises
+ ------
+ SyntaxError
+ If a marked string is not a Python expression.
+ TypeError or ValueError
+ If a registered constructor's signature cannot be inspected.
+
+ Notes
+ -----
+ Rewriting never evaluates annotations or enters builder frames. Temporary
+ literal and symbol transformers belong to this call only. String-derived
+ nodes map decoded UTF-8 offsets back through the original literal spelling;
+ unrecoverable spellings use the literal's full source range.
+
+ Examples
+ --------
+ A registered shape string becomes a builder resolution call::
+
+ R.Tensor(("n + 1",), "float32")
+ # Rewritten shape: (X.resolve_type_var("n") + 1,)
+
+ The unmarked dtype string remains unchanged.
+ """
+ literals = _LiteralParser(filename)
+
+ class Symbols(ast.NodeTransformer):
+ # Pattern: "n + m * 2" -> X.resolve_type_var("n") + X.resolve_type_var("m") * 2.
+ def __init__(self, dtype):
+ self.dtype = dtype
+
+ def visit_Attribute(self, current):
+ # A recognized namespace in T.max/T.Cast is a Python namespace,
+ # not an expression-string symbol. Keep its attribute path intact.
+ if resolve(current.value) is not None:
+ return current
+ return self.generic_visit(current)
+
+ def visit_Call(self, current):
+ # Constructor/operator names keep normal Python callable resolution;
+ # symbol-bearing operands still resolve through the active builder.
+ if resolve(current.func) is None:
+ current.func = self.visit(current.func)
+ current.args = [self.visit(value) for value in current.args]
+ for keyword in current.keywords:
+ keyword.value = self.visit(keyword.value)
+ return current
+
+ def visit_Name(self, current):
+ call = ast.copy_location(
+ ast.Call(
+ ast.Attribute(ast.Name(namespace, ast.Load()), "resolve_type_var", ast.Load()),
+ [ast.Constant(current.id)],
+ [] if self.dtype is None else [ast.keyword("dtype", ast.Constant(self.dtype))],
+ ),
+ current,
+ )
+ call._tvm_parser_support = True
+ if annotation:
+ # Signature strings introduce lexical names at their actual
+ # evaluation point: ("n + 1", n) works; (n, "n") stays unbound.
+ # This generated assignment expression contains only a builder
+ # call and preserves the original symbol's source range.
+ result = ast.copy_location(
+ ast.NamedExpr(ast.Name(current.id, ast.Store()), call), current
+ )
+ result._tvm_signature_binding = True
+ return result
+ return call
+
+ class Rewrite(ast.NodeTransformer):
+ def argument(self, current, policy, name):
+ kind = policy.fields.get(name) if policy is not None else None
+ if kind == "expr_str":
+ return self.field(current, policy.expression)
+ if kind == "global_info":
+ # Keep reference spelling intact: the enclosing module owns
+ # interpretation, and concrete values also pass through here.
+ result = ast.copy_location(
+ ast.Call(
+ ast.Attribute(
+ ast.Name(parser_support, ast.Load()), "lookup_global_info", ast.Load()
+ ),
+ [self.visit(current)],
+ [],
+ ),
+ current,
+ )
+ result._tvm_parser_support = True
+ return result
+ return self.visit(current)
+
+ def field(self, current, policy, nested=False):
+ if isinstance(current, ast.Tuple | ast.List):
+ current.elts = [self.field(value, policy, True) for value in current.elts]
+ return current
+ if isinstance(current, ast.Constant) and isinstance(current.value, str):
+ if nested or policy.scalar_strings:
+ return Symbols(policy.dtype).visit(literals._string_expression(current))
+ return self.visit(current)
+
+ def visit_Call(self, current):
+ # Pattern: R.Tensor(("n + 1",), dtype) ->
+ # R.Tensor((X.resolve_type_var("n") + 1,), dtype).
+ if getattr(current, "_tvm_args_policy_applied", False):
+ # The enclosing expression and its children are translated in
+ # separate passes. Keep each argument policy applied once while
+ # still visiting children that need ordinary syntax rewriting.
+ return self.generic_visit(current)
+ constructor = resolve(current.func)
+ policy = protocol.get_args_policy(constructor)
+ parameters = []
+ if policy is not None:
+ parameters = [
+ p.name
+ for p in inspect.signature(constructor).parameters.values()
+ if p.kind in (p.POSITIONAL_ONLY, p.POSITIONAL_OR_KEYWORD)
+ ]
+ current.func = self.visit(current.func)
+ positional = []
+ known_position = True
+ for index, value in enumerate(current.args):
+ # An unpacked argument has no statically known parameter or
+ # width. Preserve it and subsequent positional arguments;
+ # explicit keyword parameters still have known policies.
+ known_position = known_position and not isinstance(value, ast.Starred)
+ name = parameters[index] if known_position and index < len(parameters) else None
+ positional.append(self.argument(value, policy, name))
+ current.args = positional
+ for keyword in current.keywords:
+ keyword.value = self.argument(keyword.value, policy, keyword.arg)
+ if policy is not None:
+ current._tvm_args_policy_applied = True
+ return current
+
+ result = copy.deepcopy(node)
+ if annotation and isinstance(result, ast.Constant) and isinstance(result.value, str):
+ result = literals._string_expression(result)
+ return ast.fix_missing_locations(Rewrite().visit(result))
diff --git a/python/tvm/script/parser/frontend.py b/python/tvm/script/parser/frontend.py
new file mode 100644
index 0000000000..9a40aa60ad
--- /dev/null
+++ b/python/tvm/script/parser/frontend.py
@@ -0,0 +1,979 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Source acquisition and declaration/body execution for registered builders."""
+
+import __future__
+
+import ast
+import copy
+import dis
+import inspect
+import linecache
+import sys
+import textwrap
+from functools import wraps
+from types import SimpleNamespace
+from typing import TypeVar
+
+from tvm.error import DiagnosticError
+from tvm.ir import SourceName
+from tvm.script.ir_builder import base, construction
+from tvm.script.ir_builder import ir as builder_ir
+
+from . import protocol as syntax_protocol
+from .diagnostics import diagnostic_error
+from .transpile import IRBuilderTranspiler, NameCollector
+
+# Runtime callables are injected by identity; this namespace owns no IR state.
+_EXECUTION = SimpleNamespace(
+ MISSING=base.MISSING,
+ require_defined=construction.require_defined,
+ is_python_bool=construction.is_python_bool,
+ slice=slice,
+ locals=locals,
+ globals=globals,
+)
+_NAMESPACES = {}
+
+
+def register_namespace(alias, namespace):
+ """Register a host namespace for source-text entry points.
+
+ Parameters
+ ----------
+ alias : str
+ Python identifier used to refer to the namespace in source text.
+ namespace : object
+ Module or object bound to ``alias``.
+
+ Returns
+ -------
+ None
+
+ Notes
+ -----
+ Registration replaces the process-wide alias entry. Each new `Compiler`
+ copies this table; existing compilations are unaffected. This operation
+ enters no builder frame.
+ """
+ _NAMESPACES[alias] = namespace
+
+
+def _closure_values(function):
+ values = {}
+ for name, cell in zip(function.__code__.co_freevars, function.__closure__ or ()):
+ try:
+ values[name] = cell.cell_contents
+ except ValueError:
+ # Recursive and later-bound locals are empty until the helper is used.
+ pass
+ return values
+
+
+def _lexical_environment(obj):
+ """Retain Python globals and closure bindings without inspecting callers."""
+ if inspect.isfunction(obj):
+ return {**obj.__globals__, **_closure_values(obj)}
+ module = inspect.getmodule(obj)
+ return dict(vars(module)) if module is not None else {}
+
+
+def _definition_scope(frame):
+ """Snapshot immediate locals and active enclosing Python function scopes.
+
+ Postponed annotations do not necessarily create closure cells. Retain their
+ active lexical ancestors only when each caller directly owns the child's
+ code object. An unrelated caller ends this chain, even in the same file.
+ Class locals belong only to the immediate definition context; enclosing
+ classes are not Python lexical scopes. No frame survives this snapshot.
+ """
+ scopes = [dict(frame.f_locals)]
+ while frame.f_back is not None:
+ parent = frame.f_back
+ if parent.f_globals is not frame.f_globals or not any(
+ constant is frame.f_code for constant in parent.f_code.co_consts
+ ):
+ break
+ if parent.f_code.co_flags & inspect.CO_NEWLOCALS:
+ scopes.append(dict(parent.f_locals))
+ frame = parent
+ return {name: value for scope in reversed(scopes) for name, value in scope.items()}
+
+
+def recompose_builder(
+ translated,
+ *,
+ source_fn,
+ definition_scope,
+ filename,
+ flags,
+ name,
+ environment,
+ result=None,
+ definition_scopes_name=None,
+):
+ """Compile one builder callable with source lexical and annotation scopes.
+
+ Python code objects identify the source's globals and closure cells. The
+ generated body keeps those lexical bindings, while annotation expressions
+ execute in separate definition-site scopes inside builder declaration frames.
+ """
+ namespace = {key: construction.capture_value(value) for key, value in environment.items()}
+ originals = (
+ {key: value for key, value in vars(source_fn).items() if inspect.isfunction(value)}
+ if inspect.isclass(source_fn)
+ else {source_fn.__name__: source_fn}
+ if inspect.isfunction(source_fn)
+ else {}
+ )
+ scopes = {
+ key: {
+ **_closure_values(function),
+ **definition_scope,
+ **getattr(function, "__tvm_definition_scope__", {}),
+ }
+ for key, function in originals.items()
+ }
+ if definition_scopes_name is not None:
+ namespace[definition_scopes_name] = scopes
+
+ reserved = set(namespace)
+ reserved.update(node.id for node in ast.walk(translated) if isinstance(node, ast.Name))
+ reserved.update(node.arg for node in ast.walk(translated) if isinstance(node, ast.arg))
+ for body in ast.walk(translated):
+ original = originals.get(getattr(body, "_tvm_source_name", None))
+ if original is None:
+ continue
+ retained = body._tvm_signature_names
+ parameters = {argument.arg for argument in body.args.args}
+ captures = {argument.arg for argument in body.args.kwonlyargs}
+ # co_names also contains attribute spellings. Only actual global
+ # instructions describe a Python global binding; an attribute may have
+ # the same spelling as a captured closure cell (for example C.dtype).
+ global_names = {
+ instruction.argval
+ for instruction in dis.get_instructions(original)
+ if instruction.opname in ("LOAD_GLOBAL", "STORE_GLOBAL", "DELETE_GLOBAL")
+ }
+ global_names -= retained | captures | parameters
+ if global_names:
+ body.body.insert(0, ast.copy_location(ast.Global(sorted(global_names)), body))
+ source_closure = _closure_values(original)
+ for captured in sorted((captures | source_closure.keys()) - retained - parameters):
+ alias = f"{name}_lexical_{len(namespace)}"
+ while alias in reserved:
+ alias += "_"
+ reserved.add(alias)
+ value = (
+ environment.get(captured, source_closure[captured])
+ if captured in source_closure and inspect.isfunction(source_fn)
+ else source_closure.get(captured, environment.get(captured, base.MISSING))
+ )
+ # Builtin defaults remain ordinary Python lookup when no explicit
+ # lexical binding exists. Missing global names are never supplied by
+ # the annotation definition scope.
+ if value is base.MISSING:
+ import builtins
+
+ value = getattr(builtins, captured, base.MISSING)
+ namespace[alias] = construction.capture_value(value)
+ reference = ast.copy_location(ast.Name(alias, ast.Load()), body)
+ if captured in captures:
+ index = next(i for i, arg in enumerate(body.args.kwonlyargs) if arg.arg == captured)
+ default = body.args.kw_defaults[index]
+
+ class LexicalDefault(ast.NodeTransformer):
+ def visit_Name(self, node):
+ return (
+ ast.copy_location(copy.deepcopy(reference), node)
+ if node.id == captured
+ else node
+ )
+
+ body.args.kw_defaults[index] = LexicalDefault().visit(default)
+ else:
+ body.args.kwonlyargs.append(ast.arg(captured))
+ body.args.kw_defaults.append(reference)
+
+ if result is not None:
+ location = translated.body[-1]
+ definition = ast.copy_location(
+ ast.FunctionDef(
+ name,
+ ast.arguments(posonlyargs=[], args=[], kwonlyargs=[], kw_defaults=[], defaults=[]),
+ [
+ *translated.body,
+ ast.copy_location(ast.Return(ast.Name(result, ast.Load())), location),
+ ],
+ [],
+ None,
+ ),
+ location,
+ )
+ if "type_params" in ast.FunctionDef._fields:
+ definition.type_params = []
+ translated = ast.Module([definition], [])
+ exec(
+ compile(
+ ast.fix_missing_locations(translated), filename, "exec", flags=flags, dont_inherit=True
+ ),
+ namespace,
+ )
+ return namespace[name]
+
+
+def _inside_class(function, frame):
+ """Defer only in the exact class frame of a registered module decorator."""
+ local = frame.f_locals
+ if local.get("__module__") != function.__module__ or "__qualname__" not in local:
+ return False
+ text = "".join(linecache.getlines(frame.f_code.co_filename))
+ if not text:
+ return False
+ classes = [
+ node
+ for node in ast.walk(ast.parse(text))
+ if isinstance(node, ast.ClassDef)
+ and node.name == frame.f_code.co_name
+ and node.lineno <= frame.f_lineno <= node.end_lineno
+ ]
+ if not classes:
+ return False
+ node = min(classes, key=lambda item: item.end_lineno - item.lineno)
+ environment = dict(frame.f_globals)
+ if frame.f_back is not None:
+ environment.update(frame.f_back.f_locals)
+
+ def resolve(expr):
+ if isinstance(expr, ast.Name):
+ return environment.get(expr.id)
+ if isinstance(expr, ast.Attribute):
+ return getattr(resolve(expr.value), expr.attr, None)
+ return None
+
+ from tvm.relax.script.builder.ir import rewriter
+
+ return any(
+ resolve(item.func if isinstance(item, ast.Call) else item) in (ir_module, rewriter)
+ for item in node.decorator_list
+ )
+
+
+def make_decorator(builder, *, option_map=None, defaults=None):
+ """Create and register a function decorator for a construction namespace.
+
+ Parameters
+ ----------
+ builder : object
+ Namespace implementing the function construction protocol.
+ option_map : mapping of str to str, optional
+ Public option names mapped to builder keyword names. Default is None,
+ interpreted as an empty mapping; supplied entries are copied.
+ defaults : mapping of str to object, optional
+ Default builder keyword values. Default is None, interpreted as an
+ empty mapping; supplied entries are copied.
+
+ Returns
+ -------
+ decorator : callable
+ Callable supporting ``@decorator``, ``@decorator(**options)``, and
+ ``decorator(function)``.
+
+ Raises
+ ------
+ ValueError
+ When the returned decorator receives a non-function positional value.
+ DiagnosticError
+ When standalone construction fails during `parse` execution.
+
+ Notes
+ -----
+ Class members retain their Python functions until module construction.
+ Standalone functions immediately transpile and execute a builder program.
+ Annotations must be safe to re-evaluate: eager MissingType placeholders
+ are not cached, and source annotations execute in declaration frames.
+ Registration persists for the lifetime of the returned decorator.
+ """
+ mapping, default_options = dict(option_map or {}), dict(defaults or {})
+
+ def decorator(function=None, **options):
+ if function is not None and not inspect.isfunction(function):
+ raise ValueError("Construction decorators require a function or keyword options")
+
+ def apply(function):
+ frame = inspect.currentframe().f_back
+ try:
+ if frame.f_code is decorator.__code__:
+ frame = frame.f_back
+ function.__tvm_definition_scope__ = _definition_scope(frame)
+ deferred = _inside_class(function, frame)
+ finally:
+ del frame
+ function.__tvm_function_info__ = decorator.__tvm_function_info__
+ function.__tvm_function_options__ = options
+ if deferred:
+ return function
+ return parse(function)
+
+ return apply(function) if function is not None else apply
+
+ return syntax_protocol.register_function(
+ decorator, builder, option_map=mapping, defaults=default_options
+ )
+
+
+def make_helper(builder, *, preserve_return=True, late_binding=False):
+ """Create a decorator for helpers executed in a caller's builder frames.
+
+ Parameters
+ ----------
+ builder : object
+ Namespace implementing construction operations for the helper body.
+ preserve_return : bool, optional
+ Keep helper returns as ordinary Python control flow. Default is True.
+ late_binding : bool, optional
+ Refresh captured closure cells on each call. Default is False.
+
+ Returns
+ -------
+ decorator : callable
+ Accepts a function directly or keyword options. The ``hygienic``
+ option defaults to True and snapshots the definition environment;
+ False captures the calling environment on each invocation. Other
+ options remain metadata for the namespace consumer.
+
+ Raises
+ ------
+ ValueError
+ When the returned decorator receives a non-function positional value.
+ TypeError
+ When a helper invocation cannot bind its Python signature.
+
+ Notes
+ -----
+ Each invocation binds arguments and defaults, transpiles the original
+ body, and returns its result. It shares the caller's active construction
+ frames instead of declaring an IR function. Source acquisition,
+ compilation, and builder exceptions propagate to the caller.
+ """
+
+ def decorator(function=None, **options):
+ if function is not None and not inspect.isfunction(function):
+ raise ValueError("Construction decorators require a function or keyword options")
+
+ def apply(function):
+ frame = inspect.currentframe().f_back
+ try:
+ if frame.f_code is decorator.__code__:
+ frame = frame.f_back
+ function.__tvm_definition_scope__ = _definition_scope(frame)
+ finally:
+ del frame
+ definition_env = _lexical_environment(function)
+
+ @wraps(function)
+ def invoke(*args, **kwargs):
+ bound = inspect.signature(function).bind(*args, **kwargs)
+ bound.apply_defaults()
+ environment = (
+ {**definition_env, **(_closure_values(function) if late_binding else {})}
+ if options.get("hygienic", True)
+ else {**function.__globals__, **inspect.currentframe().f_back.f_locals}
+ )
+ compiler = Compiler(function, environment)
+ node = compiler.tree.body[0]
+ return compiler.run_statements(
+ node.body,
+ builder,
+ {**compiler.env, **bound.arguments},
+ set(bound.arguments),
+ preserve_return=preserve_return,
+ )
+
+ invoke.__tvm_construction_helper__ = (builder, options)
+ return invoke
+
+ return apply(function) if function is not None else apply
+
+ return decorator
+
+
+def pyfunc(function):
+ """Mark a Python function for opaque registration in a module.
+
+ Parameters
+ ----------
+ function : callable
+ Python function supporting attribute assignment.
+
+ Returns
+ -------
+ callable
+ The same function, with its registration marker attached.
+
+ Raises
+ ------
+ AttributeError
+ If the supplied object does not support the marker attribute.
+
+ Notes
+ -----
+ The function body remains ordinary Python. Module construction delegates
+ registration to the builder runtime. This decorator enters no frame and
+ preserves callable identity for the function's lifetime.
+ """
+ function.__tvm_python_function__ = True
+ return function
+
+
+syntax_protocol.register_function(pyfunc, None, python=True)
+
+
+class Compiler:
+ """Acquire source and execute a location-preserving builder program.
+
+ Parameters
+ ----------
+ source : str or function or type
+ Source text, Python function, or Python class to compile.
+ env : mapping of str to object, optional
+ Opaque lexical bindings overriding registered source aliases.
+ Default is None, interpreted as an empty mapping.
+ filename : str, optional
+ Override the original filename. Default is None, which uses the
+ inspected filename for objects and ``"<str>"`` for source text.
+ track_span : bool, optional
+ Emit shared source metadata and IR location instrumentation.
+ Default is True. False preserves Python AST locations and tracebacks.
+
+ Raises
+ ------
+ OSError
+ If inspection cannot recover the object's source.
+ TypeError
+ If the source object cannot be inspected.
+ SyntaxError
+ If the acquired text is not valid Python syntax.
+
+ Notes
+ -----
+ ``env``, ``original``, and the fresh source ``tree`` belong to this
+ compilation. ``filename`` and ``compile_flags`` retain the source's file
+ and annotation mode. ``name_map`` holds reserved identifiers and prefix
+ counters shared by all generated functions. ``builder_name`` and
+ ``infrastructure_name`` are reserved aliases.
+
+ With tracking enabled, one SourceName metadata object is held in ``env``
+ under ``source_name_binding`` for the complete source unit. Disabling
+ tracking skips that object and generated span instrumentation. No
+ concrete IR construction state is retained; `build` passes its result
+ opaquely to the caller.
+ """
+
+ def __init__(
+ self, source, env=None, filename=None, *, track_span: bool = True, definition_scope=None
+ ):
+ self.env = {"TypeVar": TypeVar, "tvm": sys.modules.get("tvm"), **_NAMESPACES, **(env or {})}
+ self.original = source
+ self.definition_scope = dict(definition_scope or {})
+ self.track_span = track_span
+ members = vars(source).values() if inspect.isclass(source) else (source,)
+ self.compile_flags = 0
+ for member in members:
+ code = getattr(member, "__code__", None)
+ if code is not None:
+ self.compile_flags |= code.co_flags & __future__.annotations.compiler_flag
+ if isinstance(source, str):
+ text = source
+ self.filename = filename or "<str>"
+ start, indent = 1, 0
+ linecache.cache[self.filename] = (
+ len(text),
+ None,
+ text.splitlines(keepends=True),
+ self.filename,
+ )
+ else:
+ lines, start = inspect.getsourcelines(source)
+ text = "".join(lines)
+ self.filename = filename or inspect.getsourcefile(source)
+ indent = len(lines[0]) - len(lines[0].lstrip())
+ self.tree = ast.parse(textwrap.dedent(text), self.filename)
+ if start != 1:
+ ast.increment_lineno(self.tree, start - 1)
+ if indent:
+ for node in ast.walk(self.tree):
+ if hasattr(node, "col_offset"):
+ node.col_offset += indent
+ node.end_col_offset += indent
+ # env is the opaque execution namespace; the transformer receives only
+ # host namespace/callable bindings needed to identify registered metadata.
+ # name_map is shared by every nested factory/body in this compilation unit.
+ self.name_map = dict.fromkeys(self.env, 0)
+ NameCollector(self.name_map).visit(self.tree)
+ self.definition_scopes_name = self.fresh("_definition_scopes")
+ self.builder_name = self.fresh()
+ self.infrastructure_name = self.fresh()
+ self.parser_support_name = "_PS" if "_PS" not in self.name_map else self.fresh("_PS")
+ self.name_map[self.parser_support_name] = 0
+ self.ir_builder_name = self.fresh("_I")
+ # One source metadata object is shared by this unit and nested factories.
+ # This narrow metadata exception never constructs Expr, Type or Span.
+ # The hygienic binding cannot collide with source or captured names.
+ self.source_name_binding = self.fresh() if track_span else None
+ if track_span:
+ self.env[self.source_name_binding] = SourceName(self.filename)
+
+ def parser_support_binding(self, location):
+ """Bind the shared parser helpers once under a hygienic source-unit name."""
+ return ast.copy_location(
+ ast.Assign(
+ [ast.Name(self.parser_support_name, ast.Store())],
+ ast.Attribute(
+ ast.Name(self.ir_builder_name, ast.Load()), "parser_support", ast.Load()
+ ),
+ ),
+ location,
+ )
+
+ def fresh(self, prefix="_t"):
+ """Allocate a generated identifier without changing any source name.
+
+ Parameters
+ ----------
+ prefix : str, optional
+ Identifier prefix. Default is ``"_t"``.
+
+ Returns
+ -------
+ str
+ An unused generated identifier.
+
+ Notes
+ -----
+ Updates this compilation's shared ``name_map``. Reserved names have
+ value zero; prefix entries hold the next counter. The allocator remains
+ shared across every function in the source unit.
+ """
+ counter = self.name_map.get(prefix, 0)
+ while f"{prefix}{counter}" in self.name_map:
+ counter += 1
+ name = f"{prefix}{counter}"
+ self.name_map[prefix] = counter + 1
+ self.name_map[name] = 0
+ return name
+
+ def span_ast(self, node):
+ """Emit location data for builders to materialize during execution.
+
+ Parameters
+ ----------
+ node : ast.AST
+ Original node carrying line, end-line, and UTF-8 column ranges.
+
+ Returns
+ -------
+ ast.expr
+ Tuple expression referencing the unit's shared SourceName binding,
+ or a None constant when span tracking is disabled.
+
+ Notes
+ -----
+ The generated expression inherits the full source range through
+ ``ast.copy_location``. This method constructs no IR Span object.
+ """
+ if not self.track_span:
+ return ast.copy_location(ast.Constant(None), node)
+ return ast.copy_location(
+ ast.Tuple(
+ [
+ ast.Name(self.source_name_binding, ast.Load()),
+ *[
+ ast.Constant(value)
+ for value in (
+ node.lineno,
+ node.end_lineno,
+ node.col_offset,
+ node.end_col_offset,
+ )
+ ],
+ ],
+ ast.Load(),
+ ),
+ node,
+ )
+
+ def transformer(self, builder_name=None, **options):
+ """Create a syntax transformer sharing this unit's name allocator.
+
+ Parameters
+ ----------
+ builder_name : str, optional
+ Injected builder alias. Default is None, which selects this unit's
+ ``builder_name``.
+ **options
+ Additional `IRBuilderTranspiler` syntax-handler configuration.
+
+ Returns
+ -------
+ IRBuilderTranspiler
+ New transformer with local binding and export analysis.
+
+ Raises
+ ------
+ TypeError
+ If options are unsupported or duplicate internally supplied options.
+
+ Notes
+ -----
+ Only host namespace and callable metadata are exposed to the transformer.
+ The name allocator is shared with this compiler; no IR state is shared.
+ """
+ # Registered host namespace metadata can occur only in annotations,
+ # with no corresponding Python closure cell. This metadata is solely
+ # for syntax policies; body binding never depends on membership here.
+ metadata_environment = {**self.env, **self.definition_scope}
+ if inspect.isclass(self.original):
+ metadata_environment.update(vars(self.original))
+ metadata = {
+ name: value
+ if (
+ inspect.ismodule(value)
+ or inspect.isfunction(value)
+ or inspect.isclass(value)
+ or type(value).__module__ == "types"
+ )
+ else None
+ for name, value in metadata_environment.items()
+ }
+ return IRBuilderTranspiler(
+ self.filename,
+ metadata,
+ builder_name or self.builder_name,
+ self.infrastructure_name,
+ self.span_ast,
+ None,
+ name_map=self.name_map,
+ track_span=self.track_span,
+ parser_support_name=self.parser_support_name,
+ definition_scopes_name=self.definition_scopes_name,
+ **options,
+ )
+
+ def function_kind(self, node, env=None, *, allow_python=False):
+ """Read registered function metadata and unevaluated option syntax.
+
+ Parameters
+ ----------
+ node : ast.FunctionDef
+ Original function definition with construction decorator syntax.
+ env : mapping, optional
+ Unused compatibility argument. Default is None.
+ allow_python : bool, optional
+ Permit unregistered ordinary Python helpers. Default is False.
+
+ Returns
+ -------
+ info : FunctionDecoratorInfo
+ Registered metadata or an ordinary-Python fallback when permitted.
+ options : ast.Dict
+ Unevaluated construction options.
+
+ Raises
+ ------
+ SyntaxError
+ If construction metadata is absent and Python helpers are disallowed,
+ or a construction decorator supplies positional options.
+
+ Notes
+ -----
+ This lookup evaluates neither annotations nor option expressions.
+ """
+ return self.transformer().function_metadata(node, allow_python=allow_python)
+
+ def run_statements(self, body, builder, env, bound_names, *, preserve_return=False):
+ """Compile and execute a helper body through AST-only lowering.
+
+ Parameters
+ ----------
+ body : list of ast.stmt
+ Nonempty original helper body.
+ builder : object
+ Registered construction namespace for the helper.
+ env : mapping of str to object
+ Opaque execution bindings, copied for this invocation.
+ bound_names : iterable of str
+ Names of supplied Python parameters.
+ preserve_return : bool, optional
+ Retain ordinary Python return semantics. Default is False.
+
+ Returns
+ -------
+ object
+ Opaque result of the generated helper.
+
+ Raises
+ ------
+ SyntaxError
+ If the helper body cannot be translated or compiled.
+
+ Notes
+ -----
+ Execution shares the caller's active builder frames and this unit's name
+ allocator. Source nodes are copied before translation. Builder and host
+ execution exceptions propagate unchanged.
+ """
+ namespace = dict(env)
+ namespace.update(
+ {
+ self.builder_name: builder,
+ self.infrastructure_name: _EXECUTION,
+ self.ir_builder_name: builder_ir,
+ }
+ )
+ transformer = self.transformer(signature_names=bound_names, preserve_return=preserve_return)
+ statements = [self.parser_support_binding(body[0]), *transformer.transform_statements(body)]
+ names = sorted(name for name in bound_names if name in namespace)
+ helper_name = self.fresh()
+ helper = ast.copy_location(
+ ast.FunctionDef(
+ helper_name,
+ ast.arguments(
+ posonlyargs=[],
+ args=[ast.arg(name) for name in names],
+ kwonlyargs=[],
+ kw_defaults=[],
+ defaults=[],
+ ),
+ statements or [ast.Pass()],
+ [],
+ None,
+ ),
+ body[0],
+ )
+ if "type_params" in ast.FunctionDef._fields:
+ helper.type_params = []
+ module = ast.fix_missing_locations(ast.Module([helper], []))
+ helper = recompose_builder(
+ module,
+ source_fn=self.original,
+ definition_scope=self.definition_scope,
+ filename=self.filename,
+ flags=self.compile_flags,
+ name=helper_name,
+ environment=namespace,
+ )
+ return helper(*(namespace[name] for name in names))
+
+ def build(self):
+ """Compile original-location builder AST and execute it.
+
+ Returns
+ -------
+ object
+ Opaque result produced by the builder program.
+
+ Raises
+ ------
+ SyntaxError
+ If the source cannot be lowered to supported construction syntax
+ or the generated program cannot be compiled.
+
+ Notes
+ -----
+ Imports establish host namespace bindings before metadata lookup. The
+ remaining source expressions execute in the generated builder program;
+ execution exceptions propagate to `parse` for diagnostic conversion.
+
+ Compilation uses the original filename and AST coordinates, never
+ unparse/reparse. Replaced nodes inherit all four location fields, while
+ ``fix_missing_locations`` fills only absent fields. Python exceptions
+ therefore retain the source file and original expression range; column
+ detail requires Python 3.11 or later. Generated names and injected
+ bindings remain local to this compiler and its execution namespace.
+ """
+ # Source-text imports establish host namespace bindings before static
+ # metadata lookup. Only import statements execute here; annotations and
+ # construction expressions remain exclusively in the generated program.
+ imports = [
+ copy.deepcopy(node)
+ for node in self.tree.body[:-1]
+ if isinstance(node, ast.Import | ast.ImportFrom)
+ ]
+ if imports:
+ exec(
+ compile(ast.Module(imports, []), self.filename, "exec", dont_inherit=True), self.env
+ )
+ if (
+ inspect.isfunction(self.original)
+ and syntax_protocol.function_info(self.original) is not None
+ ):
+ # Direct decorator application (T.prim_func(host_function)) has no
+ # decorator in source AST. Inject only its registered host metadata;
+ # already evaluated option values stay opaque execution bindings.
+ decorator_name = self.fresh()
+ self.env[decorator_name] = self.original
+ keywords = []
+ for key, value in getattr(self.original, "__tvm_function_options__", {}).items():
+ option_name = self.fresh()
+ self.env[option_name] = value
+ keywords.append(ast.keyword(key, ast.Name(option_name, ast.Load())))
+ root = self.tree.body[-1]
+ root.decorator_list = [
+ ast.copy_location(
+ ast.Call(ast.Name(decorator_name, ast.Load()), [], keywords), root
+ )
+ ]
+ runtime, original = self.fresh(), self.fresh()
+ bindings = {
+ runtime: construction,
+ original: self.original if inspect.isclass(self.original) else None,
+ self.infrastructure_name: _EXECUTION,
+ self.ir_builder_name: builder_ir,
+ }
+ transformed, result = self.transformer().program(self.tree, runtime, original, bindings)
+ transformed.body.insert(0, self.parser_support_binding(self.tree.body[-1]))
+ ast.fix_missing_locations(transformed)
+ namespace = {**self.env, **bindings}
+ builder = recompose_builder(
+ transformed,
+ source_fn=self.original,
+ definition_scope=self.definition_scope,
+ definition_scopes_name=self.definition_scopes_name,
+ filename=self.filename,
+ flags=self.compile_flags,
+ name=self.fresh("_builder"),
+ environment=namespace,
+ result=result,
+ )
+ return builder()
+
+
+def parse(source, extra_vars=None, *, filename=None, track_span: bool = True, **options):
+ """Transpile and execute a source string, Python function, or Python class.
+
+ Parameters
+ ----------
+ source : str or function or type
+ Original source text or inspectable Python object.
+ extra_vars : mapping of str to object, optional
+ Lexical bindings overriding captured values. Default is None,
+ interpreted as an empty mapping.
+ filename : str, optional
+ Source filename override. Default is None, which uses the inspected
+ filename for objects and ``"<str>"`` for text.
+ track_span : bool, optional
+ Enable shared source metadata and IR location instrumentation.
+ Default is True. False retains Python source locations only.
+ **options
+ ``absent_params`` transports the legacy JIT's explicit mapping of
+ absent parameter names to None for the root function. Bare constexpr
+ annotations consume their captured bindings during builder execution.
+ Other options are accepted for entry-point compatibility; construction
+ policy comes from registered source decorators.
+
+ Returns
+ -------
+ object
+ Opaque result of the generated builder program.
+
+ Raises
+ ------
+ DiagnosticError
+ If transpilation or host/builder execution fails. An existing
+ DiagnosticError is preserved; other execution errors gain original
+ source ranges.
+ OSError
+ If source inspection cannot recover the supplied object's text.
+ TypeError
+ If the source object cannot be inspected.
+ SyntaxError
+ If initial source parsing fails before program execution.
+
+ Notes
+ -----
+ Each call owns a fresh compiler and lexical environment. Declaration and
+ definition frames are entered only during generated execution. Source
+ acquisition errors propagate directly, before diagnostic conversion.
+ """
+ env = {} if isinstance(source, str) else _lexical_environment(source)
+ env.update(extra_vars or {})
+ definition_scope = options.pop(
+ "_definition_scope", getattr(source, "__tvm_definition_scope__", {})
+ )
+ compiler = Compiler(
+ source, env, filename, track_span=track_span, definition_scope=definition_scope
+ )
+ try:
+ root = compiler.tree.body[-1]
+ root_name = root.name if isinstance(root, ast.FunctionDef) else None
+ with construction.specialization_context(
+ root_name, options.get("_specialization_bindings", {})
+ ):
+ with construction.absent_parameters(root_name, options.get("absent_params")):
+ return compiler.build()
+ except DiagnosticError:
+ raise
+ except Exception as error:
+ raise diagnostic_error(error, compiler) from error
+
+
+def ir_module(module=None, **options):
+ """Decorate a Python class with two-phase module construction.
+
+ Parameters
+ ----------
+ module : type, optional
+ Class to compile immediately. Default is None, which returns a
+ decorator awaiting a class.
+ **options
+ Keyword arguments forwarded to `parse`.
+
+ Returns
+ -------
+ object or callable
+ Generated module result when a class is supplied, otherwise a class
+ decorator.
+
+ Raises
+ ------
+ DiagnosticError
+ If module transpilation or builder execution fails.
+
+ Notes
+ -----
+ Class host bindings are captured before transpilation. Generated execution
+ declares all registered signatures before defining their bodies. Source
+ acquisition errors and frame lifetime follow `parse`.
+ """
+
+ def apply(module):
+ if not inspect.isclass(module):
+ raise TypeError(f"Expect a class, but got: {module}")
+ frame = inspect.currentframe().f_back
+ try:
+ if frame.f_code is ir_module.__code__:
+ frame = frame.f_back
+ definition_scope = _definition_scope(frame)
+ finally:
+ del frame
+ return parse(module, _definition_scope=definition_scope, **options)
+
+ return apply(module) if module is not None else apply
+
+
+from_source = parse
diff --git a/python/tvm/script/parser/core/__init__.py b/python/tvm/script/parser/ir.py
similarity index 84%
rename from python/tvm/script/parser/core/__init__.py
rename to python/tvm/script/parser/ir.py
index 01bcd86ae4..bad463555a 100644
--- a/python/tvm/script/parser/core/__init__.py
+++ b/python/tvm/script/parser/ir.py
@@ -1,4 +1,3 @@
-# isort: skip_file
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
@@ -15,6 +14,6 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-"""The core parser infra"""
-
-from . import diagnostics, dispatch, doc, doc_core, entry, evaluator, parser, utils
+"""Module construction and shared parser support."""
+from tvm.script.ir_builder.ir import * # noqa: F403
+from .frontend import ir_module, pyfunc
diff --git a/python/tvm/script/parser/ir/__init__.py b/python/tvm/script/parser/ir/__init__.py
deleted file mode 100644
index 063712f33d..0000000000
--- a/python/tvm/script/parser/ir/__init__.py
+++ /dev/null
@@ -1,39 +0,0 @@
-# isort: skip_file
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-"""The ir module parser"""
-
-from tvm.ir import Range, StringType, StringImm, GenericConst
-from ...ir_builder.ir import * # pylint: disable=redefined-builtin
-from . import parser as _parser
-from .entry import ir_module, pyfunc
-
-
-__all__ = [
- "GenericConst",
- "Range",
- "StringImm",
- "StringType",
- "dummy_global_info",
- "ir_module",
- "lookup_vdevice",
- "meta_var",
- "module_attrs",
- "module_global_infos",
- "pyfunc",
- "vdevice",
-]
diff --git a/python/tvm/script/parser/ir/entry.py b/python/tvm/script/parser/ir/entry.py
deleted file mode 100644
index 5191ce0f87..0000000000
--- a/python/tvm/script/parser/ir/entry.py
+++ /dev/null
@@ -1,163 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-# pylint: disable=import-outside-toplevel
-"""The entry point of TVM parser for ir module."""
-
-import inspect
-from collections.abc import Callable
-
-from tvm import cpu, ir
-from tvm.ir import GlobalVar, IRModule
-
-from .._core import parse, utils
-
-
-# this formulation allows us to support having @I.ir_module
-# appear as a decorator by itself or to have optional arguments
-# like @I.ir_module(check_well_formed=False)
-def ir_module(
- mod: type | None = None, check_well_formed: bool = True, s_tir: bool = False
-) -> IRModule:
- """The parsing method for ir module, by using `@ir_module` as decorator.
-
- Parameters
- ----------
... 12526 lines suppressed ...
(tvm) 04/04: [FR][Relax] Preserve local function regions and recursive signatures
Posted by tq...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a commit to branch script/canonical-parser-df
in repository https://gitbox.apache.org/repos/asf/tvm.git
commit af6dbafed2faa43eac7ce43d115dcceb271adb5a
Author: Tianqi Chen <tq...@users.noreply.github.com>
AuthorDate: Tue Sep 22 22:02:05 2026 +0000
[FR][Relax] Preserve local function regions and recursive signatures
Keep nested function declarations in their owning binding region and initialize recursive local function signatures before lowering their bodies.
---
src/relax/script/builder/frame.cc | 39 +++++++++-
tests/script/test_parser_local_functions.py | 117 ++++++++++++++++++++++++++++
2 files changed, 154 insertions(+), 2 deletions(-)
diff --git a/src/relax/script/builder/frame.cc b/src/relax/script/builder/frame.cc
index a443f2de86..c8e5678cd5 100644
--- a/src/relax/script/builder/frame.cc
+++ b/src/relax/script/builder/frame.cc
@@ -85,7 +85,10 @@ void FunctionFrameNode::ExitWithScope() {
function = tvm::relax::Function::CreateEmpty(params, ret_ty.value_or(tvm::relax::AnyType()),
is_pure.value_or(true), DictAttrs(attrs), span);
if (local) {
- local_var = tvm::Var(name.value(), tvm::relax::GetType(function.value()), span);
+ auto ty = tvm::relax::GetType(function.value());
+ local_var = CheckBindingBlockFrameExistAndUnended()->is_dataflow
+ ? tvm::relax::DataflowVar(name.value(), ty, span)
+ : tvm::Var(name.value(), ty, span);
} else {
global_var = ir::DeclFunction(name.value(), function.value());
}
@@ -115,9 +118,38 @@ void FunctionFrameNode::ExitWithScope() {
if (local) {
TVM_FFI_CHECK(local_var.has_value(), ValueError)
<< "A local function definition requires its declared reference";
+ bool recursive = false;
+ for (const tvm::Var& var : tvm::relax::FreeVars(func)) {
+ recursive = recursive || var.same_as(local_var.value());
+ }
// Retain the declared reference identity while publishing its inferred type,
// just as DefFunction refines a module's global reference after definition.
- local_var.value()->ty = tvm::relax::GetType(func);
+ Type reference_type = tvm::relax::GetType(func);
+ if (recursive) {
+ // A recursive reference has its own provisional signature. Its formal
+ // primitive parameters must not bind the definition's parameter objects.
+ // Keep lexical captures intact while renewing these signature binders.
+ ffi::Map<tvm::Var, tvm::Expr> signature_params;
+ for (const tvm::Var& param : params) {
+ if (param.as<PrimVar>()) {
+ signature_params.Set(param, param.CopyWithName(param->name));
+ }
+ }
+ if (!signature_params.empty()) {
+ auto signature = reference_type.as_or_throw<tvm::relax::FuncType>();
+ auto bind_param = [&](const Type& ty) { return tvm::relax::Bind(ty, signature_params); };
+ reference_type =
+ tvm::relax::FuncType(signature->params.value().Map(bind_param),
+ bind_param(signature->ret), signature->purity, signature->span);
+ }
+ if (local_var.value()->IsInstance<tvm::relax::DataflowVarNode>()) {
+ // The self-reference is captured by the nested function, so it must
+ // survive the dataflow region. Region finalization rewrites every use
+ // together, including recursive calls, to the same ordinary Var.
+ CheckBindingBlockFrameExistAndUnended()->output_vars.push_back(local_var.value());
+ }
+ }
+ local_var.value()->ty = reference_type;
EmitVarBinding(tvm::relax::VarBinding(local_var.value(), func, span));
} else if (!builder->HasConstructionFrames()) {
// Case 0. No outer frame, return function directly
@@ -204,6 +236,9 @@ void BindingBlockFrameNode::ExitWithScope() {
ffi::Array<tvm::Var> new_output_vars;
std::unordered_map<tvm::Var, tvm::Var, ffi::ObjectPtrHash, ffi::ObjectPtrEqual> var_remap;
for (const auto& output_var : output_vars) {
+ if (var_remap.count(output_var)) {
+ continue;
+ }
tvm::Var new_output_var(output_var->name, tvm::relax::GetType(output_var), output_var->span);
new_output_vars.push_back(new_output_var);
if (auto span = binding_spans.Get(output_var)) {
diff --git a/tests/script/test_parser_local_functions.py b/tests/script/test_parser_local_functions.py
new file mode 100644
index 0000000000..ad1a6aacb4
--- /dev/null
+++ b/tests/script/test_parser_local_functions.py
@@ -0,0 +1,117 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Local Relax functions retain their enclosing binding region and identities."""
+
+import textwrap
+
+import pytest
+
+import tvm
+from tvm import relax
+from tvm.script.parser import parse
+
+
+def _local_binding(function):
+ return next(
+ binding
+ for block in function.body.blocks
+ for binding in block.bindings
+ if isinstance(binding.value, relax.Function)
+ )
+
+
+@pytest.mark.parametrize("dataflow", [False, True])
+@pytest.mark.parametrize("recursive", [False, True])
+def test_local_function_binding_region_and_reference_identity(dataflow, recursive):
+ inner_return = "inner(y)" if recursive else "R.add(x, y)"
+ region = f"""\
+@R.function
+def inner(y: R.Tensor((2,), "float32")) -> R.Tensor((2,), "float32"):
+ return {inner_return}
+result = inner(x)
+"""
+ if dataflow:
+ region = "with R.dataflow():\n" + textwrap.indent(region + "R.output(result)\n", " ")
+ source = (
+ '@R.function\ndef main(x: R.Tensor((2,), "float32")):\n'
+ + textwrap.indent(region, " ")
+ + " return result\n"
+ )
+ function = parse(source)
+ binding = _local_binding(function)
+ assert isinstance(binding.var, relax.DataflowVar) == (dataflow and not recursive)
+ call = function.body.blocks[0].bindings[-1].value
+ assert call.op.same_as(binding.var)
+ inner_call = binding.value.body.blocks[0].bindings[0].value
+ if recursive:
+ assert inner_call.op.same_as(binding.var)
+ else:
+ assert inner_call.args[0].same_as(function.params[0])
+ assert inner_call.args[1].same_as(binding.value.params[0])
+ relax.analysis.well_formed(tvm.IRModule({"main": function}))
+
+
+def test_explicitly_output_local_function_keeps_recursive_reference():
+ function = parse("""
+@R.function
+def main():
+ with R.dataflow():
+ @R.function
+ def inner(y: R.Tensor((2,), "float32")) -> R.Tensor((2,), "float32"):
+ return inner(y)
+ R.output(inner)
+ return inner
+""")
+ binding = _local_binding(function)
+ assert not isinstance(binding.var, relax.DataflowVar)
+ assert function.body.body.same_as(binding.var)
+ inner_call = binding.value.body.blocks[0].bindings[0].value
+ assert inner_call.op.same_as(binding.var)
+ relax.analysis.well_formed(tvm.IRModule({"main": function}))
+
+
+@pytest.mark.parametrize("recursive", [False, True])
+def test_local_dependent_signature_preserves_parameter_and_capture_scopes(recursive):
+ inner_return = "inner(current, value)" if recursive else "value"
+ function = parse(f"""
+@R.function
+def main(n: R.Prim("int64"), m: R.Prim("int64"), x: R.Tensor((n, m), "float32")):
+ @R.function
+ def inner(current: R.Prim("int64"), value: R.Tensor((current, m), "float32")) -> R.Tensor(
+ (current, m), "float32"
+ ):
+ return {inner_return}
+ return inner(n, x)
+""")
+ binding = _local_binding(function)
+ current, value = binding.value.params
+ assert value.ty.shape[0].same_as(current)
+ assert binding.value.ret_ty.shape[0].same_as(current)
+ signature_current = binding.var.ty.params[1].shape[0]
+ assert signature_current.same_as(current) != recursive
+ assert binding.var.ty.ret.shape[0].same_as(signature_current)
+ assert binding.var.ty.params[1].shape[1].same_as(function.params[1])
+ assert binding.var.ty.ret.shape[1].same_as(function.params[1])
+ tvm.ir.assert_structural_equal(binding.var.ty, binding.value.ty)
+ call = function.body.blocks[0].bindings[-1].value
+ assert call.op.same_as(binding.var)
+ assert call.ty.shape[0].same_as(function.params[0])
+ if recursive:
+ recursive_call = binding.value.body.blocks[0].bindings[0].value
+ assert recursive_call.op.same_as(binding.var)
+ assert recursive_call.ty.shape[0].same_as(current)
+ relax.analysis.well_formed(tvm.IRModule({"main": function}))
(tvm) 03/04: [FR][TVMScript] Preserve public validation and diagnostic boundaries
Posted by tq...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a commit to branch script/canonical-parser-df
in repository https://gitbox.apache.org/repos/asf/tvm.git
commit 77ffe0934b4fb44ad903fabc41ce9320519da49e
Author: Tianqi Chen <tq...@users.noreply.github.com>
AuthorDate: Tue Sep 22 22:02:05 2026 +0000
[FR][TVMScript] Preserve public validation and diagnostic boundaries
Retain decorator factories, function validation, Optional specialization boundaries, and source diagnostic coordinates through canonical construction.
---
python/tvm/script/ir_builder/construction.py | 9 +--
python/tvm/script/parser/diagnostics.py | 1 +
python/tvm/script/parser/frontend.py | 91 +++++++++++++++++++++++++---
python/tvm/script/parser/jit.py | 8 +--
4 files changed, 90 insertions(+), 19 deletions(-)
diff --git a/python/tvm/script/ir_builder/construction.py b/python/tvm/script/ir_builder/construction.py
index 69a8b091c6..3c3096af44 100644
--- a/python/tvm/script/ir_builder/construction.py
+++ b/python/tvm/script/ir_builder/construction.py
@@ -41,7 +41,7 @@ _ABSENT_PARAMETERS = ContextVar("tvm_builder_absent_parameters", default=None)
@contextmanager
def specialization_context(name, bindings):
"""Pass validated JIT bindings to one root builder execution."""
- token = _SPECIALIZATION.set((name, dict(bindings)))
+ token = _SPECIALIZATION.set(None if bindings is None else (name, dict(bindings)))
try:
yield
finally:
@@ -148,9 +148,8 @@ class FunctionRecord:
):
self.symbols.bind(captured_name, value)
context = _SPECIALIZATION.get()
- self.specialization = (
- context[1] if context is not None and context[0] == name and not local else {}
- )
+ self.is_specialization = context is not None and context[0] == name and not local
+ self.specialization = context[1] if self.is_specialization else {}
self.captured_bindings = dict(captures or {}) if not local else {}
absent = _ABSENT_PARAMETERS.get()
self.absent_parameters = (
@@ -191,6 +190,8 @@ class FunctionRecord:
# Optional is an annotation wrapper, owned by the JIT entry point.
unwrap = getattr(annotation, "__tvm_optional_annotation__", None)
if unwrap is not None:
+ if not self.is_specialization:
+ raise TypeError("T.Optional is only supported by @T.jit")
annotation = unwrap()
return self.parameter(name, annotation, location)
diff --git a/python/tvm/script/parser/diagnostics.py b/python/tvm/script/parser/diagnostics.py
index af25eebc35..35987d38d3 100644
--- a/python/tvm/script/parser/diagnostics.py
+++ b/python/tvm/script/parser/diagnostics.py
@@ -37,6 +37,7 @@ def diagnostic_error(error, compiler):
location = getattr(error, "__tvm_script_location__", None)
if location is not None:
filename, start, end, column, end_column = location
+ column, end_column = column - 1, end_column - 1
elif isinstance(error, SyntaxError) and error.lineno:
start = error.lineno
end = error.end_lineno or start
diff --git a/python/tvm/script/parser/frontend.py b/python/tvm/script/parser/frontend.py
index 9a40aa60ad..347148d809 100644
--- a/python/tvm/script/parser/frontend.py
+++ b/python/tvm/script/parser/frontend.py
@@ -327,7 +327,12 @@ def make_decorator(builder, *, option_map=None, defaults=None):
function.__tvm_function_options__ = options
if deferred:
return function
- return parse(function)
+ result = parse(
+ function,
+ check_well_formed=options.get("check_well_formed", True),
+ )
+ result.__name__ = function.__name__
+ return result
return apply(function) if function is not None else apply
@@ -607,8 +612,8 @@ class Compiler:
for value in (
node.lineno,
node.end_lineno,
- node.col_offset,
- node.end_col_offset,
+ node.col_offset + 1,
+ node.end_col_offset + 1,
)
],
],
@@ -921,17 +926,81 @@ def parse(source, extra_vars=None, *, filename=None, track_span: bool = True, **
try:
root = compiler.tree.body[-1]
root_name = root.name if isinstance(root, ast.FunctionDef) else None
- with construction.specialization_context(
- root_name, options.get("_specialization_bindings", {})
- ):
+ specialization = options.get("_specialization_bindings")
+ if specialization is None and options.get("absent_params") is not None:
+ specialization = {}
+ check_well_formed = options.get("check_well_formed")
+ if check_well_formed is None:
+ check_well_formed = True
+ for decorator in getattr(root, "decorator_list", ()):
+ if isinstance(decorator, ast.Call):
+ for keyword in decorator.keywords:
+ if keyword.arg == "check_well_formed":
+ check_well_formed = eval(
+ compile(ast.Expression(keyword.value), compiler.filename, "eval"),
+ compiler.env,
+ )
+ with construction.specialization_context(root_name, specialization):
with construction.absent_parameters(root_name, options.get("absent_params")):
- return compiler.build()
+ result = compiler.build()
+ if check_well_formed:
+ _check_well_formed(result)
+ return result
except DiagnosticError:
raise
except Exception as error:
raise diagnostic_error(error, compiler) from error
+def _check_well_formed(result):
+ """Apply the public entry point's default validation to constructed IR."""
+ from tvm import ir, relax, s_tir, tirx
+
+ message = (
+ "Program is not well-formed. If this is deliberate, set "
+ "check_well_formed=False in the top-level decorator."
+ )
+ if isinstance(result, ir.IRModule | relax.Function):
+ if not relax.analysis.check_well_formed(result):
+ raise ValueError(message)
+ if not isinstance(result, ir.IRModule | relax.Function | tirx.PrimFunc):
+ return
+ module = result if isinstance(result, ir.IRModule) else ir.IRModule.from_expr(result)
+ try:
+ s_tir.analysis.verify_well_formed(module)
+ for function in module.functions.values():
+ if isinstance(function, tirx.PrimFunc) and not function.attrs.get("s_tir", False):
+ tirx.analysis.verify_tirx_well_formed(function)
+ except Exception as error:
+ raise ValueError(f"{message}\n{error}") from error
+
+
+class _PyModuleFactory:
+ """Keep executable Python attachments on each fresh module instance."""
+
+ def __init__(self, module, original_class):
+ self.ir_module = module
+ self.original_class = original_class
+ self.pyfunc_methods = list(getattr(module, "pyfuncs", {}))
+ self.__name__ = original_class.__name__
+
+ def __call__(self, device=None, target=None):
+ from tvm import cpu, ir
+ from tvm.relax.base_py_module import BasePyModule
+
+ source = self.ir_module
+ instance_module = ir.IRModule(
+ source.functions, attrs=source.attrs, global_infos=source.global_infos
+ )
+ instance = BasePyModule(instance_module, device or cpu(0), target)
+ for name in self.pyfunc_methods:
+ instance.add_python_function(name, getattr(self.original_class, name))
+ return instance
+
+ def __getattr__(self, name):
+ return getattr(self.ir_module, name)
+
+
def ir_module(module=None, **options):
"""Decorate a Python class with two-phase module construction.
@@ -971,7 +1040,13 @@ def ir_module(module=None, **options):
definition_scope = _definition_scope(frame)
finally:
del frame
- return parse(module, _definition_scope=definition_scope, **options)
+ result = parse(module, _definition_scope=definition_scope, **options)
+ from tvm.relax.base_py_module import BasePyModule
+
+ if issubclass(module, BasePyModule):
+ return _PyModuleFactory(result, module)
+ result.__name__ = module.__name__
+ return result
return apply(module) if module is not None else apply
diff --git a/python/tvm/script/parser/jit.py b/python/tvm/script/parser/jit.py
index 655726e74c..df522d5d6a 100644
--- a/python/tvm/script/parser/jit.py
+++ b/python/tvm/script/parser/jit.py
@@ -207,14 +207,8 @@ class TIRJit:
self._closure_vars,
_definition_scope=self._definition_scope,
_specialization_bindings={**effective, **absent_params},
+ check_well_formed=self.check_well_formed,
)
- if self.check_well_formed:
- from tvm.s_tir.analysis import verify_well_formed
- from tvm.tirx.analysis import verify_tirx_well_formed
-
- verify_well_formed(prim_func)
- if not prim_func.attrs.get("s_tir", False):
- verify_tirx_well_formed(prim_func)
setattr(prim_func, "__name__", self.func.__name__)
self._cache[cache_key] = prim_func
return prim_func