Skip to content

Commit ff67bc6

Browse files
committed
add hash key for code in libtuner
1 parent c6da816 commit ff67bc6

2 files changed

Lines changed: 80 additions & 7 deletions

File tree

src/flag_gems/utils/libentry.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import builtins
2+
import hashlib
23
import inspect
34
import math
45
import os
@@ -27,6 +28,17 @@
2728
version = triton.__version__.split(".")
2829
major_version, minor_version = eval(version[0]), eval(version[1])
2930

31+
32+
def get_kernel_hash(func):
33+
if hasattr(func, "fn"):
34+
original_func = func.fn
35+
else:
36+
original_func = func
37+
38+
source_code = inspect.getsource(original_func)
39+
return hashlib.md5(source_code.encode("utf-8")).hexdigest()[:8]
40+
41+
3042
if major_version == 2:
3143

3244
def all_kwargs(self):
@@ -192,16 +204,23 @@ def __init__(
192204
self.cache = libcache[share] if share else libcache[self.__name__]
193205
if strategy:
194206
assert len(self.strategy) == len(self.keys), "Invalid number of strategies"
207+
self.base_fn = fn
208+
while not inspect.isfunction(self.base_fn):
209+
self.base_fn = self.base_fn.fn
210+
self.kernel_hash = get_kernel_hash(self.base_fn)
195211

196212
def get_key(self, args):
197213
if self.strategy is None:
198214
key = [args[k] for k in self.keys if k in args]
199-
return key
200-
key = []
201-
for i, k in enumerate(self.keys):
202-
s = STRATEGY[self.strategy[i]]
203-
v = s(args[k])
204-
key.append(v)
215+
else:
216+
key = []
217+
for i, k in enumerate(self.keys):
218+
s = STRATEGY[self.strategy[i]]
219+
v = s(args[k])
220+
key.append(v)
221+
222+
key.append(f"hash_{self.kernel_hash}")
223+
205224
return key
206225

207226
def run(self, *args, **kwargs):

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)