Skip to content

Commit d35509c

Browse files
committed
[SPEC] Fix spec language init
1 parent a90f20c commit d35509c

4 files changed

Lines changed: 16 additions & 93 deletions

File tree

python/triton/__init__.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@
2727
from . import testing
2828
from . import tools
2929

30-
# flagtree backend language extension
31-
from .flagtree_spec import spec
32-
33-
spec("init_language")
34-
3530
must_use_result = language.core.must_use_result
3631

3732
__all__ = [

python/triton/language/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""isort:skip_file"""
22
# Import order is significant here.
33

4-
# flagtree backend path specialization
5-
from triton.flagtree_spec import spec_path
4+
from triton.flagtree_spec import spec_path, spec
65

6+
# flagtree backend path specialization
77
spec_path(__path__)
88

9+
# flagtree backend specialization
10+
spec("language_extend_globals", globals())
11+
912
from . import math
1013
from . import extra
1114
from ..backends import language_extensions as ext
@@ -115,7 +118,6 @@
115118
store,
116119
sub,
117120
tensor,
118-
to_tensor,
119121
trans,
120122
tuple,
121123
tuple_type,
@@ -270,7 +272,6 @@
270272
"swizzle2d",
271273
"target_info",
272274
"tensor",
273-
"to_tensor",
274275
"topk",
275276
"trans",
276277
"tuple",
Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,3 @@
1-
"""Mthreads backend spec module.
2-
3-
This module is loaded by flagtree_spec.py via
4-
importlib.import_module("triton.backends.mthreads.spec") when
5-
FLAGTREE_BACKEND=mthreads. Functions defined here are callable
6-
via flagtree_spec.spec("function_name", ...).
7-
"""
8-
9-
10-
def init_language():
11-
"""Add mthreads-specific symbols to triton.language.
12-
13-
Called explicitly from the main triton/__init__.py after
14-
triton.language is fully initialized, via:
15-
spec("init_language")
16-
"""
17-
# Delay importing language.core until Triton's language module is ready.
18-
# Keep constexpr global so the JIT can resolve the annotation by name.
19-
global constexpr
20-
from triton.flagtree_spec import bind_language_extension_symbols_to_tl
21-
from triton.runtime.jit import jit as _jit
22-
from triton.language.core import (
23-
constexpr,
24-
builtin as _builtin,
25-
static_assert as _static_assert,
26-
_unwrap_if_constexpr,
27-
)
28-
29-
class _Ext:
30-
__all__ = [
31-
"squeeze",
32-
"unsqueeze",
33-
"_experimental_descriptor_load",
34-
"_experimental_descriptor_store",
35-
]
36-
37-
_ext = _Ext()
38-
39-
@_jit
40-
def squeeze(x, dim: constexpr):
41-
_static_assert(x.shape[dim] == 1)
42-
return x.reshape(x.shape[:dim] + x.shape[dim + 1:])
43-
44-
@_jit
45-
def unsqueeze(x, dim: constexpr):
46-
return x.reshape(x.shape[:dim] + (1, ) + x.shape[dim:])
47-
48-
@_builtin
49-
def _experimental_descriptor_load(desc_pointer, offsets, shape, dtype, _semantic=None):
50-
"""Legacy compatibility API for descriptor load.
51-
52-
New code should prefer ``load_tensor_descriptor``. We keep this symbol
53-
so migrated tests can exercise the same backend descriptor path without
54-
monkeypatching triton.language internals in conftest.
55-
"""
56-
_ = shape
57-
dtype = _unwrap_if_constexpr(dtype)
58-
value = desc_pointer.load(offsets, _semantic=_semantic)
59-
if value.dtype == dtype:
60-
return value
61-
if value.dtype.primitive_bitwidth == dtype.primitive_bitwidth:
62-
return value.to(dtype, bitcast=True, _semantic=_semantic)
63-
return value.to(dtype, _semantic=_semantic)
64-
65-
@_builtin
66-
def _experimental_descriptor_store(desc_pointer, value, offsets, _semantic=None):
67-
"""Legacy compatibility API for descriptor store.
68-
69-
New code should prefer ``store_tensor_descriptor``.
70-
"""
71-
value = _semantic.to_tensor(value)
72-
desc_dtype = desc_pointer.dtype
73-
if value.dtype != desc_dtype and value.dtype.primitive_bitwidth == desc_dtype.primitive_bitwidth:
74-
value = value.to(desc_dtype, bitcast=True, _semantic=_semantic)
75-
return desc_pointer.store(offsets, value, _semantic=_semantic)
76-
77-
_ext.squeeze = squeeze
78-
_ext.unsqueeze = unsqueeze
79-
_ext._experimental_descriptor_load = _experimental_descriptor_load
80-
_ext._experimental_descriptor_store = _experimental_descriptor_store
81-
82-
bind_language_extension_symbols_to_tl(_ext)
83-
84-
851
from ._filecheck import spec_get_stub_target
862
from ._utils import apply_with_path, _tuple_create
3+
from .language import language_extend_globals
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def language_extend_globals(globals_dict):
2+
# NOTE: Must use absolute path import.
3+
from triton.language.standard import squeeze, unsqueeze
4+
from triton.language.core import _experimental_descriptor_load, _experimental_descriptor_store
5+
from triton.language.core import to_tensor
6+
globals_dict["squeeze"] = squeeze
7+
globals_dict["unsqueeze"] = unsqueeze
8+
globals_dict["_experimental_descriptor_load"] = _experimental_descriptor_load
9+
globals_dict["_experimental_descriptor_store"] = _experimental_descriptor_store
10+
globals_dict["to_tensor"] = to_tensor

0 commit comments

Comments
 (0)