Skip to content

Commit e095a25

Browse files
committed
add hash key for code in libtuner
1 parent 7b16ab7 commit e095a25

2 files changed

Lines changed: 80 additions & 2 deletions

File tree

src/flag_gems/utils/libentry.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import builtins
2+
import hashlib
23
import inspect
34
import logging
45
import math
@@ -31,6 +32,19 @@
3132
version = triton.__version__.split(".")
3233
major_version, minor_version = eval(version[0]), eval(version[1])
3334

35+
36+
def get_kernel_hash(func, configs):
37+
if hasattr(func, "fn"):
38+
original_func = func.fn
39+
else:
40+
original_func = func
41+
42+
source_code = inspect.getsource(original_func)
43+
config_strs = [str(config) for config in configs]
44+
combined_content = f"{source_code}{config_strs}"
45+
return hashlib.md5(combined_content.encode("utf-8")).hexdigest()[:8]
46+
47+
3448
if major_version == 2:
3549

3650
def all_kwargs(self):
@@ -199,9 +213,19 @@ def __init__(
199213
self.keys = key
200214
self.strategy = strategy
201215
self.share = share
202-
self.cache = libcache[share] if share else libcache[self.__name__]
216+
self.kernel_hash = get_kernel_hash(self.base_fn, self.configs)
217+
# Use table name with hash instead of hash in key
218+
self.table_name = (
219+
f"{share}_{self.kernel_hash}"
220+
if share
221+
else f"{self.__name__}_{self.kernel_hash}"
222+
)
223+
self.cache = libcache[self.table_name]
203224
if strategy:
204225
assert len(self.strategy) == len(self.keys), "Invalid number of strategies"
226+
self.base_fn = fn
227+
while not inspect.isfunction(self.base_fn):
228+
self.base_fn = self.base_fn.fn
205229

206230
def get_key(self, args):
207231
if self.strategy is None:

tests/test_libentry.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import flag_gems
1010
from flag_gems.runtime import torch_device_fn
11-
from flag_gems.utils import libentry
11+
from flag_gems.utils import libentry, libtuner
1212

1313

1414
# not_raises is copied from https://gist.github.com/oisinmulvihill/45c14271fad7794a4a52516ecb784e69
@@ -235,3 +235,57 @@ def test_threadsafety():
235235
for i in range(100):
236236
with not_raises(Exception):
237237
run_two_threads()
238+
239+
240+
@pytest.mark.skipif(
241+
flag_gems.vendor_name == "kunlunxin",
242+
reason="Test Files for Operators Not Pending Testing",
243+
)
244+
def test_hash_generation():
245+
@libtuner(
246+
configs=[
247+
triton.Config({"TILE_N": 32}),
248+
triton.Config({"TILE_N": 64}),
249+
triton.Config({"TILE_N": 128}),
250+
triton.Config({"TILE_N": 256}),
251+
triton.Config({"TILE_N": 512}),
252+
triton.Config({"TILE_N": 1024}),
253+
],
254+
key=["x"],
255+
)
256+
@triton.jit
257+
def kernel_a(x, y):
258+
return x + y + 1
259+
260+
@libtuner(
261+
configs=[
262+
triton.Config({"TILE_N": 32}),
263+
triton.Config({"TILE_N": 64}),
264+
triton.Config({"TILE_N": 128}),
265+
triton.Config({"TILE_N": 256}),
266+
triton.Config({"TILE_N": 512}),
267+
triton.Config({"TILE_N": 1024}),
268+
],
269+
key=["x"],
270+
)
271+
@triton.jit
272+
def kernel_b(x, y):
273+
return x + y
274+
275+
@libtuner(
276+
configs=[
277+
triton.Config({"TILE_N": 32}),
278+
triton.Config({"TILE_N": 64}),
279+
triton.Config({"TILE_N": 128}),
280+
triton.Config({"TILE_N": 256}),
281+
triton.Config({"TILE_N": 512}),
282+
triton.Config({"TILE_N": 1024}),
283+
],
284+
key=["x"],
285+
)
286+
@triton.jit
287+
def kernel_a_copy(x, y):
288+
return x + y + 1
289+
290+
assert kernel_a.kernel_hash != kernel_a_copy.kernel_hash
291+
assert kernel_a.kernel_hash != kernel_b.kernel_hash

0 commit comments

Comments
 (0)