-
Notifications
You must be signed in to change notification settings - Fork 325
[Feat] Add support for T.serial with step and negative step
#1188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5a6204e
8fe6fc2
328bf52
c7c9a49
ff229bd
3d10d99
54f2da3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| """The language interface for tl programs.""" | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
| from tvm import tir | ||
| from tvm.tir import IntImm | ||
| import tvm.script.ir_builder.tir as tb_tir | ||
| from .v2.builder import SerialForWithStep | ||
| from tilelang import _ffi_api | ||
|
|
||
|
|
||
| def Parallel(*extents: tir.PrimExpr, coalesced_width: int | None = None): | ||
| """Tools to construct nested parallel for loop. | ||
| This can be used to create element-wise tensor expression. | ||
| Parameters | ||
| ---------- | ||
| extents : PrimExpr | ||
| The extents of the iteration. | ||
| coalesced_width : Optional[int] | ||
| The coalesced width of the parallel loop. | ||
| Returns | ||
| ------- | ||
| res : frame.ForFrame | ||
| The ForFrame. | ||
| """ | ||
| annotations: dict[str, Any] = {} | ||
| if coalesced_width is not None: | ||
| annotations.update({"coalesced_width": coalesced_width}) | ||
| return _ffi_api.Parallel(extents, annotations) # type: ignore[attr-defined] # pylint: disable=no-member | ||
|
|
||
|
|
||
| def Persistent( | ||
| domain: list[tir.PrimExpr], | ||
| wave_size: tir.PrimExpr, | ||
| index: tir.PrimExpr, | ||
| group_size: tir.PrimExpr | None = 8, | ||
| ): | ||
| """Tools to construct persistent for loop. | ||
| Parameters | ||
| ---------- | ||
| domain : List[tir.PrimExpr] | ||
| The list of dominators. | ||
| wave_size : int | ||
| The wave size. | ||
| index : int | ||
| The tile index in one wave. | ||
| group_size : tir.PrimExpr | ||
| The group size. | ||
| """ | ||
| return _ffi_api.Persistent(domain, wave_size, index, group_size) | ||
|
|
||
|
|
||
| def Pipelined( | ||
| start: tir.PrimExpr, | ||
| stop: tir.PrimExpr = None, | ||
| num_stages: int = 0, | ||
| order: list[int] | None = None, | ||
| stage: list[int] | None = None, | ||
| sync: list[list[int]] | None = None, | ||
| group: list[list[int]] | None = None, | ||
| ): | ||
| """Tools to construct pipelined for loop. | ||
| Parameters | ||
| ---------- | ||
| start : PrimExpr | ||
| The minimum value of iteration. | ||
| stop : PrimExpr | ||
| The maximum value of iteration. | ||
| num_stages : int | ||
| The max number of buffer used between pipeline producers and consumers. | ||
| if num_stages is 0, pipeline will not be enabled. | ||
| Returns | ||
| ------- | ||
| res : frame.ForFrame | ||
| The ForFrame. | ||
| """ | ||
| if stop is None: | ||
| stop = start | ||
| start = IntImm(start.dtype, 0) if hasattr(start, "dtype") else 0 | ||
| if order is None: | ||
| order = [] | ||
| if stage is None: | ||
| stage = [] | ||
| if sync is None: | ||
| sync = [] | ||
| if group is None: | ||
| group = [] | ||
| # type: ignore[attr-defined] # pylint: disable=no-member | ||
| return _ffi_api.Pipelined(start, stop, num_stages, order, stage, sync, group) | ||
|
|
||
|
|
||
| def serial(start: tir.PrimExpr, | ||
| stop: tir.PrimExpr | None = None, | ||
| step: tir.PrimExpr | None = None, | ||
| *, | ||
| annotations: dict[str, Any] | None = None): | ||
| step_is_one = False | ||
| step_is_one |= isinstance(step, int) and step == 1 | ||
| step_is_one |= isinstance(step, IntImm) and step.value == 1 | ||
| if step is None or step_is_one: | ||
| return tb_tir.serial(start, stop, annotations=annotations) | ||
| else: | ||
| if stop is None: | ||
| stop = start | ||
| start = IntImm(start.dtype, 0) if hasattr(start, "dtype") else 0 | ||
| return SerialForWithStep(start, stop, step, annotations=annotations) | ||
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -100,6 +100,14 @@ class BreakFrame(Frame): | |||||||||||||||||||||||||||||||||
| ... | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @dataclass | ||||||||||||||||||||||||||||||||||
| class SerialForWithStep: | ||||||||||||||||||||||||||||||||||
| start: PrimExpr | ||||||||||||||||||||||||||||||||||
| stop: PrimExpr | ||||||||||||||||||||||||||||||||||
| step: PrimExpr | ||||||||||||||||||||||||||||||||||
| annotations: dict[str, Any] | None = None | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # Python 3.9 compatibility: avoid PEP 604 unions at runtime | ||||||||||||||||||||||||||||||||||
| # Use tuple for isinstance checks and typing.Union for annotations/aliases | ||||||||||||||||||||||||||||||||||
| ContinueOrBreak = (ContinueFrame, BreakFrame) | ||||||||||||||||||||||||||||||||||
|
|
@@ -243,12 +251,32 @@ def eval(self, val: Any): | |||||||||||||||||||||||||||||||||
| def ctx_for(self, it): | ||||||||||||||||||||||||||||||||||
| self.check_continue_break() | ||||||||||||||||||||||||||||||||||
| it = unwrap_expr(it) | ||||||||||||||||||||||||||||||||||
| if not isinstance(it, tir.frame.ForFrame): | ||||||||||||||||||||||||||||||||||
| raise TypeError( | ||||||||||||||||||||||||||||||||||
| f"Invalid for loop, got {it}({type(it)}), expect one of the following: " | ||||||||||||||||||||||||||||||||||
| "range, T.serial, T.grid, T.parallel, T.vectorized, T.unroll, T.thread_binding") | ||||||||||||||||||||||||||||||||||
| with self.with_frame(it) as v: | ||||||||||||||||||||||||||||||||||
| yield v | ||||||||||||||||||||||||||||||||||
| if isinstance(it, SerialForWithStep): | ||||||||||||||||||||||||||||||||||
| # Validate and compute the trip count before constructing the frame | ||||||||||||||||||||||||||||||||||
| if isinstance(it.step, (int, IntImm)): | ||||||||||||||||||||||||||||||||||
| step_value = it.step if isinstance(it.step, int) else it.step.value | ||||||||||||||||||||||||||||||||||
| if step_value == 0: | ||||||||||||||||||||||||||||||||||
| raise ValueError('Invalid stepped serial: step must be non-zero') | ||||||||||||||||||||||||||||||||||
| if step_value > 0: | ||||||||||||||||||||||||||||||||||
| real_stop = tir.ceildiv(it.stop - it.start, step_value) | ||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||
| real_stop = tir.ceildiv(it.start - it.stop, -step_value) | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+256
to
+263
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Handle empty iteration range for negative steps. When Python's Apply this diff to clamp if step_value > 0:
- real_stop = tir.ceildiv(it.stop - it.start, step_value)
+ real_stop = tir.max(tir.ceildiv(it.stop - it.start, step_value), 0)
else:
- real_stop = tir.ceildiv(it.start - it.stop, -step_value)
+ real_stop = tir.max(tir.ceildiv(it.start - it.stop, -step_value), 0)📝 Committable suggestion
Suggested change
🧰 Tools🪛 Ruff (0.14.3)259-259: Avoid specifying long messages outside the exception class (TRY003) 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||
| logger.warning( | ||||||||||||||||||||||||||||||||||
| f'Using a non-constant step `{it.step}` in stepped serial may lead to undefined behavior in tilelang' | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| real_stop = tir.ceildiv(it.stop - it.start, it.step) | ||||||||||||||||||||||||||||||||||
| real_frame = tir.serial(real_stop, annotations=it.annotations) | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+256
to
+269
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The new stepped loop path computes Useful? React with 👍 / 👎. |
||||||||||||||||||||||||||||||||||
| with self.with_frame(real_frame) as v: | ||||||||||||||||||||||||||||||||||
| IRBuilder.name('_tmp', v) | ||||||||||||||||||||||||||||||||||
| yield it.start + v * it.step | ||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||
| if not isinstance(it, tir.frame.ForFrame): | ||||||||||||||||||||||||||||||||||
| raise TypeError( | ||||||||||||||||||||||||||||||||||
| f"Invalid for loop, got {it}({type(it)}), expect one of the following: " | ||||||||||||||||||||||||||||||||||
| "range, T.serial, T.grid, T.parallel, T.vectorized, T.unroll, T.thread_binding") | ||||||||||||||||||||||||||||||||||
| with self.with_frame(it) as v: | ||||||||||||||||||||||||||||||||||
| yield v | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def ctx_continue(self): | ||||||||||||||||||||||||||||||||||
| self.check_continue_break() | ||||||||||||||||||||||||||||||||||
|
|
@@ -459,8 +487,9 @@ def arg(self, name, value): | |||||||||||||||||||||||||||||||||
| f"Unsupported argument type: {value}({type(value)}) for argument `{name}`.") | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def override(self, name: str): | ||||||||||||||||||||||||||||||||||
| from tilelang.language import serial | ||||||||||||||||||||||||||||||||||
| if name == 'range': | ||||||||||||||||||||||||||||||||||
| return tir.serial | ||||||||||||||||||||||||||||||||||
| return serial | ||||||||||||||||||||||||||||||||||
| raise ValueError(f'Unknown override: {name}') | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing zero-step validation.
The function correctly routes step values to either
tb_tir.serial(forstep=Noneorstep=1) orSerialForWithStep, and properly normalizes start/stop. However, zero step is not validated before creatingSerialForWithStep, which will cause division by zero inbuilder.py::ctx_forline 255.Apply this diff to validate step:
def serial(start: tir.PrimExpr, stop: tir.PrimExpr | None = None, step: tir.PrimExpr | None = None, *, annotations: dict[str, Any] | None = None): + # Validate non-zero step for constant values + if isinstance(step, int) and step == 0: + raise ValueError("Serial loop step must not be zero") + if isinstance(step, IntImm) and step.value == 0: + raise ValueError("Serial loop step must not be zero") + step_is_one = False step_is_one |= isinstance(step, int) and step == 1 step_is_one |= isinstance(step, IntImm) and step.value == 1Minor: Consider using logical
orinstead of bitwise|=.While lines 103-104 work correctly, the bitwise OR pattern is unconventional for boolean accumulation. Consider:
📝 Committable suggestion
🤖 Prompt for AI Agents