Skip to content

Commit aeddd94

Browse files
gbonikxiaoqiqi177
authored andcommitted
Allow plain tuple as shorthand for TupleConstraint
Also allow plain int/float/bool inside a TupleConstraint and update the docs for KernelSignature. Signed-off-by: Greg Bonik <gbonik@nvidia.com>
1 parent 9dee58a commit aeddd94

2 files changed

Lines changed: 24 additions & 22 deletions

File tree

src/cuda/tile/compilation/_signature.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,8 @@ class TupleConstraint:
231231
"""
232232
items: "tuple[ParameterConstraint, ...]"
233233

234-
def __init__(self,
235-
items: "Sequence[ParameterConstraint]"):
236-
items = tuple(items)
237-
for i, item_constraint in enumerate(items):
238-
if not isinstance(item_constraint, ParameterConstraint):
239-
raise TypeError(
240-
f"TupleConstraint item #{i} must be a ParameterConstraint,"
241-
f" got {type(item_constraint).__name__}")
234+
def __init__(self, items: "Sequence[ParameterConstraint]"):
235+
items = tuple(_to_constraint(x) for x in items)
242236
object.__setattr__(self, "items", items)
243237

244238

@@ -277,11 +271,13 @@ def __eq__(self, other):
277271
| TupleConstraint | ConstantConstraint)
278272

279273

280-
def _to_constraint(c: ParameterConstraint | bool | int | float):
274+
def _to_constraint(c: ParameterConstraint | bool | int | float | tuple) -> ParameterConstraint:
281275
if isinstance(c, ParameterConstraint):
282276
return c
283277
elif isinstance(c, bool | int | float):
284278
return ConstantConstraint(c)
279+
elif isinstance(c, tuple):
280+
return TupleConstraint(c)
285281
else:
286282
raise TypeError(f"Can't interpret {c!r} as a parameter constraint")
287283

@@ -292,17 +288,24 @@ class KernelSignature:
292288
Signature of a compiled kernel.
293289
294290
Args:
295-
parameters (Sequence[ParameterConstraint | bool | int | float]):
291+
parameters (Sequence[ParameterConstraint | bool | int | float | tuple]):
296292
For each parameter of the kernel's Python function, a corresponding
297-
:py:class:`ParameterConstraint` instance. If a parameter is marked with
298-
:py:class:`ct.Constant <cuda.tile.Constant>`, the corresponding constraint must be
299-
a :py:class:`ConstantConstraint` or a ``bool``, ``int`` or ``float`` value
300-
that specifies the compile-time constant.
301-
Otherwise, it must be either a :py:class:`ScalarConstraint`,
302-
:py:class:`ArrayConstraint` or :py:class:`ListConstraint`.
303-
Passing a ``bool``, ``int`` or ``float`` value as a constraint is convenience notation
304-
that is equivalent to passing an instance of :py:class:`ConstantConstraint`
305-
that wraps said value.
293+
:py:class:`ParameterConstraint` instance.
294+
295+
Possible constraint classes are: :py:class:`ScalarConstraint`,
296+
:py:class:`ArrayConstraint`, :py:class:`ListConstraint`, :py:class:`TupleConstraint`,
297+
:py:class:`ConstantConstraint`.
298+
299+
A plain ``bool``, ``int`` or ``float`` value can be used as shorthand for
300+
:py:class:`ConstantConstraint` wrapping the given value.
301+
Similarly, a plain ``tuple`` is shorthand for a :py:class:`TupleConstraint` .
302+
303+
Each constraint must be compatible with annotations on the corresponding kernel
304+
parameter. For example, if a parameter is marked with
305+
:py:class:`ct.Constant <cuda.tile.Constant>`, then the corresponding constraint must be
306+
a :py:class:`ConstantConstraint`, or a nested :py:class:`TupleConstraint` thereof
307+
(or a plain ``bool``, ``int``, ``float`` or ``tuple``, according to the shorthand
308+
notation described above).
306309
calling_convention (CallingConvention):
307310
|Calling convention| to use.
308311
symbol (str | None):
@@ -316,7 +319,7 @@ class KernelSignature:
316319
symbol: str | None
317320

318321
def __init__(self,
319-
parameters: Sequence[ParameterConstraint | bool | int | float],
322+
parameters: Sequence[ParameterConstraint | bool | int | float | tuple],
320323
calling_convention: CallingConvention,
321324
symbol: str | None = None):
322325
if symbol is not None and not isinstance(symbol, str):

test/test_export_compat.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ def test_export_compat_cutile_python_v2():
187187
ct.compilation.ScalarConstraint(ct.int32),
188188
ct.compilation.ScalarConstraint(ct.int32),
189189
]),
190-
ct.compilation.TupleConstraint(
191-
[ct.compilation.ConstantConstraint(10)]),
190+
(10,),
192191
ct.compilation.ArrayConstraint(ct.int32, 0, index_dtype=ct.int32,
193192
stride_lower_bound_incl=0,
194193
alias_groups=(), may_alias_internally=False),

0 commit comments

Comments
 (0)