-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathflagtree_spec.py
More file actions
76 lines (61 loc) · 2.09 KB
/
Copy pathflagtree_spec.py
File metadata and controls
76 lines (61 loc) · 2.09 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
import importlib
import os
_spec_module = None
def _triton_root() -> str | None:
current_path = os.path.abspath(__file__).replace(os.sep, "/")
marker = "/triton"
idx = current_path.find(marker)
if idx == -1:
return None
return current_path[:idx + len(marker)]
def _get_spec_module():
global _spec_module
from ._flagtree_backend import FLAGTREE_BACKEND
if _spec_module is not None:
return _spec_module
if not FLAGTREE_BACKEND:
return None
try:
_spec_module = importlib.import_module(f"triton.spec.{FLAGTREE_BACKEND}")
except ImportError:
return None
return _spec_module
# flagtree backend path specialization
def spec_path(path_list: list):
from ._flagtree_backend import FLAGTREE_BACKEND
if not path_list or not FLAGTREE_BACKEND:
return
current_path = path_list[0].replace(os.sep, "/")
marker = "/triton"
idx = current_path.find(marker)
if idx == -1:
return
triton_root = current_path[:idx + len(marker)]
rel_path = current_path[idx + 1 + len(marker):]
backend_path = os.path.join(triton_root, "spec", FLAGTREE_BACKEND, rel_path)
if os.path.isdir(backend_path) and backend_path not in path_list:
path_list.insert(0, backend_path)
# flagtree backend specialization
def spec(function_name: str, *args, **kwargs):
mod = _get_spec_module()
if mod is not None and hasattr(mod, function_name):
return getattr(mod, function_name)(*args, **kwargs)
return None
# flagtree backend func specialization
def spec_func(function_name: str):
mod = _get_spec_module()
if mod is not None and hasattr(mod, function_name):
return getattr(mod, function_name)
return None
# flagtree language extension
def bind_language_extension_symbols_to_tl(extension):
import triton.language as tl
names = getattr(extension, "__all__", None)
if not names:
return
for name in names:
if not hasattr(extension, name):
continue
if hasattr(tl, name):
continue
setattr(tl, name, getattr(extension, name))