Skip to content

Commit d457bff

Browse files
committed
[SPEC] Fix spec _filecheck.py, _utils.py
1 parent 64c2287 commit d457bff

6 files changed

Lines changed: 28 additions & 138 deletions

File tree

python/triton/_utils.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ def set_iterable_path(iterable: IterableType, path: tuple[int, ...], val: Any):
2323
prev._setitem(path[-1], val)
2424

2525

26-
def find_paths_if(iterable: Union[IterableType, Any], pred: Callable[[ObjPath, Any], bool]) -> list[ObjPath]:
26+
# flagtree: This function was originally defined in find_paths_if
27+
def is_iterable(x):
2728
from .language import core
28-
is_iterable: Callable[[Any], bool] = lambda x: isinstance(x, (list, tuple, core.tuple, core.tuple_type))
29+
return isinstance(x, (list, tuple, core.tuple, core.tuple_type))
30+
31+
32+
def find_paths_if(iterable: Union[IterableType, Any], pred: Callable[[ObjPath, Any], bool]) -> list[ObjPath]:
2933
# We need to use dict so that ordering is maintained, while set doesn't guarantee order
3034
ret: dict[ObjPath, None] = {}
3135

@@ -132,3 +136,9 @@ def get_primitive_bitwidth(dtype: str) -> int:
132136

133137
def is_namedtuple(val):
134138
return isinstance(val, type) and issubclass(val, tuple) and hasattr(val, "_fields")
139+
140+
141+
# flagtree backend path specialization
142+
from triton.flagtree_spec import spec_func
143+
spec_func("apply_with_path")
144+
spec_func("_tuple_create")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from ._filecheck import spec_get_stub_target
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from triton.backends.compiler import GPUTarget
2+
3+
def spec_get_stub_target() -> GPUTarget:
4+
return GPUTarget("hip", "gfx942", 64)

third_party/mthreads/backend/spec/triton/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,5 @@ def _experimental_descriptor_store(desc_pointer, value, offsets, _semantic=None)
8383

8484
bind_language_extension_symbols_to_tl(_ext)
8585

86-
87-
def spec_get_stub_target() -> GPUTarget:
88-
arch = os.environ.get("TRITON_OVERRIDE_ARCH") or os.environ.get("TRITON_MUSA_ARCH") or "ph1"
89-
return GPUTarget("musa", arch, 32)
86+
from ._filecheck import spec_get_stub_target
87+
from ._utils import apply_with_path, _tuple_create
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import os
2+
3+
from triton.backends.compiler import GPUTarget
4+
5+
def spec_get_stub_target() -> GPUTarget:
6+
arch = os.environ.get("TRITON_OVERRIDE_ARCH") or os.environ.get("TRITON_MUSA_ARCH") or "ph1"
7+
return GPUTarget("musa", arch, 32)

third_party/mthreads/backend/spec/triton/_utils.py

Lines changed: 2 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,153 +1,23 @@
1-
from __future__ import annotations
2-
3-
from functools import reduce
4-
from typing import Any, Callable, TYPE_CHECKING, Union, List, Dict
1+
from typing import Any, Callable, TYPE_CHECKING, Union
52

63
if TYPE_CHECKING:
74
from .language import core
85
IterableType = Union[list[Any], tuple[Any, ...], core.tuple, core.tuple_type]
96
ObjPath = tuple[int, ...]
107

11-
TRITON_MAX_TENSOR_NUMEL = 1048576
12-
13-
14-
def get_iterable_path(iterable: IterableType, path: ObjPath) -> Any:
15-
return reduce(lambda a, idx: a[idx], path, iterable) # type: ignore[index]
16-
17-
18-
def set_iterable_path(iterable: IterableType, path: tuple[int, ...], val: Any):
19-
from .language import core
20-
assert len(path) != 0
21-
prev = iterable if len(path) == 1 else get_iterable_path(iterable, path[:-1])
22-
assert isinstance(prev, core.tuple)
23-
prev._setitem(path[-1], val)
24-
25-
26-
def is_iterable(x):
27-
from .language import core
28-
return isinstance(x, (list, tuple, core.tuple, core.tuple_type))
29-
308

319
def apply_with_path(value: Any, fn: Callable[[ObjPath, Any], None], _path=None) -> None:
3210
if _path is None:
3311
_path = ()
3412

13+
from triton._utils import is_iterable
3514
if is_iterable(value):
3615
for idx, item in enumerate(value):
3716
apply_with_path(item, fn, _path=(*_path, idx))
3817
else:
3918
fn(_path, value)
4019

4120

42-
def find_paths_if(iterable: Union[IterableType, Any], pred: Callable[[ObjPath, Any], bool]) -> list[ObjPath]:
43-
# We need to use dict so that ordering is maintained, while set doesn't guarantee order
44-
ret: dict[ObjPath, None] = {}
45-
46-
def _impl(path: tuple[int, ...], current: Any):
47-
if is_iterable(current):
48-
for idx, item in enumerate(current):
49-
_impl((*path, idx), item)
50-
elif pred(path, current):
51-
ret[path] = None
52-
53-
_impl((), iterable)
54-
55-
return list(ret.keys())
56-
57-
58-
def is_power_of_two(x):
59-
return (x & (x - 1)) == 0
60-
61-
62-
def validate_block_shape(shape: List[int]):
63-
numel = 1
64-
for i, d in enumerate(shape):
65-
if not isinstance(d, int):
66-
raise TypeError(f"Shape element {i} must have type `constexpr[int]`, got `constexpr[{type(d)}]")
67-
if not is_power_of_two(d):
68-
raise ValueError(f"Shape element {i} must be a power of 2")
69-
numel *= d
70-
71-
if numel > TRITON_MAX_TENSOR_NUMEL:
72-
raise ValueError(f"numel ({numel}) exceeds triton maximum tensor numel ({TRITON_MAX_TENSOR_NUMEL})")
73-
return numel
74-
75-
76-
type_canonicalisation_dict = {
77-
# we canonicalise all bools to be unsigned:
78-
"bool": "u1",
79-
"int1": "u1",
80-
"uint1": "u1",
81-
"i1": "u1",
82-
# floating-point dtypes:
83-
"float8e4nv": "fp8e4nv",
84-
"float8e5": "fp8e5",
85-
"float8e4b15": "fp8e4b15",
86-
"float8_e4m3fn": "fp8e4nv",
87-
"float8e4b8": "fp8e4b8",
88-
"float8_e4m3fnuz": "fp8e4b8",
89-
"float8_e5m2": "fp8e5",
90-
"float8e5b16": "fp8e5b16",
91-
"float8_e5m2fnuz": "fp8e5b16",
92-
"half": "fp16",
93-
"float16": "fp16",
94-
"bfloat16": "bf16",
95-
"float": "fp32",
96-
"float32": "fp32",
97-
"double": "fp64",
98-
"float64": "fp64",
99-
# signed integers:
100-
"int8": "i8",
101-
"int16": "i16",
102-
"int": "i32",
103-
"int32": "i32",
104-
"int64": "i64",
105-
# unsigned integers:
106-
"uint8": "u8",
107-
"uint16": "u16",
108-
"uint32": "u32",
109-
"uint64": "u64",
110-
"void": "void",
111-
}
112-
113-
for v in list(type_canonicalisation_dict.values()):
114-
type_canonicalisation_dict[v] = v
115-
116-
117-
def canonicalize_dtype(dtype):
118-
dtype_str = str(dtype).split(".")[-1]
119-
return type_canonicalisation_dict[dtype_str]
120-
121-
122-
def canonicalize_ptr_dtype(dtype, is_const):
123-
return f"{'*k' if is_const else '*'}{canonicalize_dtype(dtype)}"
124-
125-
126-
BITWIDTH_DICT: Dict[str, int] = {
127-
**{f"u{n}": n
128-
for n in (1, 8, 16, 32, 64)},
129-
**{f"i{n}": n
130-
for n in (1, 8, 16, 32, 64)},
131-
**{f"fp{n}": n
132-
for n in (16, 32, 64)},
133-
**{f"fp8{suffix}": 8
134-
for suffix in ("e4nv", "e4b15", "e4b8", "e5", "e5b16")},
135-
"bf16": 16,
136-
"void": 0,
137-
}
138-
139-
for k, v in type_canonicalisation_dict.items():
140-
BITWIDTH_DICT[k] = BITWIDTH_DICT[v]
141-
142-
143-
def get_primitive_bitwidth(dtype: str) -> int:
144-
return BITWIDTH_DICT[dtype]
145-
146-
147-
def is_namedtuple(val):
148-
return isinstance(val, type) and issubclass(val, tuple) and hasattr(val, "_fields")
149-
150-
15121
def _tuple_create(arg, contents):
15222
# NamedTuples and tuples have different construction semantics. NamedTuple
15323
# has a constructor that takes individual arguments, while tuple takes an

0 commit comments

Comments
 (0)