-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathtest_materialize_constants.py
More file actions
126 lines (108 loc) · 3.8 KB
/
Copy pathtest_materialize_constants.py
File metadata and controls
126 lines (108 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# SPDX-FileCopyrightText: Copyright (c) <2026> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
import cuda.tile as ct
from cuda.tile._cext import CallingConvention
from cuda.tile._compile import compile_tile
from cuda.tile._ir.core_ops import TypedConst
from cuda.tile._ir.ops import MakeTensorView
from cuda.tile._ir.ops_utils import get_dtype
from cuda.tile.compilation import (
ArrayConstraint,
ConstantConstraint,
KernelSignature,
ListConstraint,
)
def _get_defined_op(body, name: str):
definitions = {
var.name: op
for op in body.traverse()
for var in op.result_vars
}
return definitions[name]
def test_unannotated_unit_stride_is_materialized():
def kernel(x):
ct.store(x, (0, 0), 0)
constraint = ArrayConstraint(
ct.float32,
2,
index_dtype=ct.int32,
base_addr_divisible_by=1,
stride_lower_bound_incl=0,
stride_constant=(None, 1),
stride_divisible_by=(1, 1),
shape_divisible_by=(1, 1),
alias_groups=[],
may_alias_internally=False,
)
signature = KernelSignature((constraint,), CallingConvention.cutile_python_v2())
[body] = compile_tile(
kernel, [signature], return_final_ir=True, return_cubin=False
).final_ir
[view] = [op for op in body.traverse() if isinstance(op, MakeTensorView)]
# No ArrayAnnotation is present, so the array strides stays dynamic.
assert view.result_var.get_type().strides == (None, None)
inner_stride_defined_op = _get_defined_op(body, view.strides[1].name)
assert isinstance(inner_stride_defined_op, TypedConst)
assert inner_stride_defined_op.value == 1
def test_unannotated_unit_stride_is_materialized_in_nested_block():
def kernel(xs, count: ct.Constant[int]):
for i in range(count):
item = xs[i]
ct.store(item, (0,), 0)
element_constraint = ArrayConstraint(
ct.float32,
1,
index_dtype=ct.int32,
base_addr_divisible_by=1,
stride_lower_bound_incl=0,
stride_constant=(1,),
stride_divisible_by=(1,),
shape_divisible_by=(1,),
alias_groups=[],
may_alias_internally=False,
)
list_constraint = ListConstraint(
element_constraint, alias_groups=[], elements_may_alias=False
)
signature = KernelSignature(
(list_constraint, ConstantConstraint(2)), CallingConvention.cutile_python_v2()
)
[body] = compile_tile(
kernel, [signature], return_final_ir=True, return_cubin=False
).final_ir
[view] = [op for op in body.traverse() if isinstance(op, MakeTensorView)]
assert view not in body.operations
assert view.result_var.get_type().strides == (None,)
stride_defined_op = _get_defined_op(body, view.strides[0].name)
assert isinstance(stride_defined_op, TypedConst)
assert stride_defined_op.value == 1
def test_wrapping_integral_cast_is_materialized():
def kernel(x):
index = ct.astype(x.shape[0], ct.int8)
ct.store(x, (index,), 0)
signature = KernelSignature(
(ArrayConstraint(
ct.float32,
1,
index_dtype=ct.int32,
base_addr_divisible_by=1,
stride_lower_bound_incl=0,
stride_constant=(1,),
shape_constant=(260,),
stride_divisible_by=(1,),
shape_divisible_by=(1,),
alias_groups=[],
may_alias_internally=False,
),),
CallingConvention.cutile_python_v2(),
)
[body] = compile_tile(
kernel, [signature], return_final_ir=True, return_cubin=False
).final_ir
assert any(
isinstance(op, TypedConst)
and op.value == 4
and get_dtype(op.result_var.get_type()) == ct.int8
for op in body.traverse()
)