File size: 5,093 Bytes
a3f2e4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from tvm import te
from tvm import IRModule
from tvm.tir import PrimFunc
from typing import Optional, Union, Callable, List, Dict
from dataclasses import dataclass, field
from tvm.tl.transform import Simplify
from abc import ABC, abstractmethod
from bitblas.base.arch import TileDevice, is_volta_arch, is_ampere_arch, is_cdna_arch, auto_infer_current_arch
from bitblas.base.roller.hint import Hint
from bitblas.tl.base_hint import BaseTLHint


# Decorator to simplify the output of a function
def maybe_simplify(self, func: Callable) -> Callable:

    def wrapper(*args, **kwargs):
        stmt: Union[PrimFunc, IRModule] = (func)(*args, **kwargs)
        if self._enable_simplify:
            return self.Simplify(stmt)
        return stmt

    return wrapper


@dataclass
class BaseScheduler(ABC):

    _arch: TileDevice = field(default=auto_infer_current_arch(), init=False, repr=False)

    _enable_simplify: bool = field(default=True, init=False, repr=False)

    _dynamic_range: Dict[str, int] = field(default_factory=dict, init=False, repr=False)

    @staticmethod
    def Simplify(stmt: Union[PrimFunc, IRModule]) -> Union[PrimFunc, IRModule]:
        if isinstance(stmt, PrimFunc):
            mod = Simplify()(IRModule.from_expr(stmt))
            assert len(mod.functions) == 1, "Simplify should return a single function"
            return list(mod.functions.values()).pop()
        elif isinstance(stmt, IRModule):
            return Simplify()(stmt)
        else:
            raise ValueError(f"Unsupported type: {type(stmt)}")

    def get_hardware_aware_configs(self,
                                   arch: TileDevice = None,
                                   topk: int = 10) -> List[BaseTLHint]:
        raise NotImplementedError(
            f"{self.__class__.__name__} does not support hardware-aware tuning for {arch} with topk={topk}"
        )

    def activate_simplify(self) -> "BaseScheduler":
        self._enable_simplify = True
        return self

    def deactivate_simplify(self) -> "BaseScheduler":
        self._enable_simplify = False
        return self

    def maybe_simplify(self, stmt: Union[PrimFunc, IRModule]) -> Union[PrimFunc, IRModule]:
        if self._enable_simplify:
            return self.Simplify(stmt)
        return stmt

    def with_self_attrs(self, func: PrimFunc) -> PrimFunc:
        if self._dynamic_range:
            func = func.with_attr("opt_shapes", self._dynamic_range)
        return func

    def post_process(self, func: PrimFunc) -> PrimFunc:
        func = self.with_self_attrs(func)
        func = self.maybe_simplify(func)
        return func

    def set_dynamic_range(self, dynamic_range: Dict[str, int]) -> "BaseScheduler":
        self._dynamic_range = dynamic_range
        return self

    def has_dynamic_range(self) -> bool:
        return bool(self._dynamic_range)

    def with_arch(self, arch: TileDevice) -> "BaseScheduler":
        self._arch = arch
        return self

    def has_arch(self) -> bool:
        return self._arch is not None

    def is_volta_arch(self) -> bool:
        return is_volta_arch(self._arch) if self._arch is not None else False

    def is_ampere_arch(self) -> bool:
        return is_ampere_arch(self._arch) if self._arch is not None else False

    def is_cdna_arch(self) -> bool:
        return is_cdna_arch(self._arch) if self._arch is not None else False

    @staticmethod
    def maybe_dynamic(arg: Union[int, List[int]], dynamic_symbol: str = "m") -> PrimFunc:
        if isinstance(arg, int):
            return arg
        return te.var(dynamic_symbol)

    @abstractmethod
    def with_default_config(self, *args, **kwargs) -> PrimFunc:
        pass

    @abstractmethod
    def apply_config(
        self,
        *args,
        **kwargs,
    ) -> PrimFunc:
        pass

    def get_hint_type(self) -> str:
        raise NotImplementedError("Get Hint type is not implemented")

    def serialize_hints_to_configs(self, hints: List[Hint]) -> List[BaseTLHint]:
        # Convert Roller Hints to TileLang Hints
        raise NotImplementedError("Serialization of hints to configs is not implemented")

    def specialize_from_dynamic_range(self,
                                      dynamic_range: Optional[Dict[str,
                                                                   int]] = None) -> "BaseScheduler":
        raise NotImplementedError("Specialization from dynamic range is not implemented")

    @property
    def common_header(self) -> str:
        # TODO(lei): For HIP Backend it should be different
        common_header = "#include <tl_templates/cuda/common.h>\n"
        return common_header

    @property
    def global_symbol(self):
        # For kernel name generation
        return "default"

    @property
    def arch(self) -> TileDevice:
        return self._arch


# Decorator to simplify the output of a function
def simplify_prim_func(func: Callable) -> Callable:

    def wrapper(*args, **kwargs):
        stmt: Union[PrimFunc, IRModule] = (func)(*args, **kwargs)
        return BaseScheduler.Simplify(stmt)

    return wrapper