@@ -140,6 +140,22 @@ def store(self):
140140
141141
142142libcache = LibCache ()
143+ SEARCH_STRATEGIES = {}
144+
145+
146+ def register_search_strategy (name ):
147+ def decorator (fn ):
148+ SEARCH_STRATEGIES [name ] = fn
149+ return fn
150+
151+ return decorator
152+
153+
154+ @register_search_strategy ("brute" )
155+ def default_search_strategy (bench_fn , configs , args , kwargs ):
156+ timings = {config : bench_fn (config ) for config in configs }
157+ best_config = builtins .min (timings , key = timings .get )
158+ return best_config , timings
143159
144160
145161class LibTuner (triton .runtime .Autotuner ):
@@ -159,6 +175,7 @@ def __init__(
159175 use_cuda_graph = False ,
160176 do_bench = None ,
161177 strategy = None ,
178+ search_strategy = None ,
162179 ):
163180 # NOTE(zhengyang): See discussion in https://github.com/triton-lang/triton/pull/4496
164181 if major_version == 2 or (major_version == 3 and minor_version <= 1 ):
@@ -205,6 +222,10 @@ def __init__(
205222 self .cache = libcache [self .table_name ]
206223 if strategy :
207224 assert len (self .strategy ) == len (self .keys ), "Invalid number of strategies"
225+ assert (
226+ isinstance (search_strategy , str ) and search_strategy in SEARCH_STRATEGIES
227+ ), "Invalid search strategy"
228+ self .search_strategy = SEARCH_STRATEGIES [search_strategy ]
208229
209230 def get_kernel_hash (self ):
210231 if self .kernel_hash is None :
@@ -245,13 +266,16 @@ def run(self, *args, **kwargs):
245266 used_cached_result = False
246267 pruned_configs = self .prune_configs (kwargs )
247268 bench_start = time .time ()
248- timings = {
249- config : self ._bench (* args , config = config , ** kwargs )
250- for config in pruned_configs
251- }
269+
270+ def bench_fn (config ):
271+ return self ._bench (* args , config = config , ** kwargs )
272+
273+ best_config , timings = self .search_strategy (
274+ bench_fn , pruned_configs , args , kwargs
275+ )
252276 bench_end = time .time ()
253277 self .bench_time = bench_end - bench_start
254- self .cache [key ] = builtins . min ( timings , key = timings . get )
278+ self .cache [key ] = best_config
255279 full_nargs = {
256280 ** self .nargs ,
257281 ** kwargs ,
@@ -293,6 +317,7 @@ def libtuner(
293317 use_cuda_graph = False ,
294318 do_bench = None ,
295319 strategy = None ,
320+ search_strategy = "brute" ,
296321):
297322 """
298323 Decorator for triton library autotuner.
@@ -314,6 +339,7 @@ def decorator(fn):
314339 use_cuda_graph = use_cuda_graph ,
315340 do_bench = do_bench ,
316341 strategy = strategy ,
342+ search_strategy = search_strategy ,
317343 )
318344
319345 return decorator
0 commit comments