Skip to content

Commit dcf1e5c

Browse files
authored
Update Python Matmul examples (#434)
* feat: Add utility function to compile GLSL source to SPIR-V Signed-off-by: n-jay <nuwanjayawardene@gmail.com> * refactor: Replace kp.Shader.compile_source with utility function compile_source Signed-off-by: n-jay <nuwanjayawardene@gmail.com> --------- Signed-off-by: n-jay <nuwanjayawardene@gmail.com>
1 parent 5b8558b commit dcf1e5c

4 files changed

Lines changed: 16 additions & 8 deletions

File tree

‎examples/python_naive_matmul/imp1_naive.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import kp
44
import numpy as np
5+
from utils import compile_source
56

67

78
class MatMulOp:
@@ -63,7 +64,7 @@ def __init__(self, manager: kp.Manager, local_size_x: int = -1, local_size_y: in
6364
acc += in_tensor_1[(k * tensor_size) + globalRow] * in_tensor_2[(globalCol * tensor_size) + k];
6465
out_tensor[(globalCol * tensor_size) + globalRow] = acc;
6566
}}'''
66-
self.compiled_shader = kp.Shader.compile_source(self.shader.format(
67+
self.compiled_shader = compile_source(self.shader.format(
6768
local_size_x=self.local_size_x, local_size_y=self.local_size_y))
6869
self.tensor_shape: tuple[int, int] = (0, 0)
6970
self.params: list[kp.Tensor] = []
@@ -78,7 +79,7 @@ def __call__(self, tensor_shape: tuple[int, int], tensor_in_1: kp.Tensor, tensor
7879
self.params = params
7980
local_size_x = min(self.local_size_x, tensor_shape[0])
8081
local_size_y = min(self.local_size_y, tensor_shape[1])
81-
self.compiled_shader = kp.Shader.compile_source(self.shader.format(
82+
self.compiled_shader = compile_source(self.shader.format(
8283
local_size_x=local_size_x, local_size_y=local_size_y))
8384
workgroup = (tensor_shape[0] // local_size_x, tensor_shape[1] // local_size_y, 1)
8485
print(f'{workgroup=} {self.local_size_x=} {self.local_size_y=}')

‎examples/python_naive_matmul/imp2_tiled.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import kp
44
import numpy as np
5-
5+
from utils import compile_source
66

77
class MatMulOp:
88
def __init__(self, manager: kp.Manager, tile_size: int = -1):
@@ -66,7 +66,7 @@ def __init__(self, manager: kp.Manager, tile_size: int = -1):
6666
}}
6767
out_tensor[tensor_size * globalCol + globalRow] = acc;
6868
}}'''
69-
self.compiled_shader = kp.Shader.compile_source(self.shader.format(tile_size=tile_size))
69+
self.compiled_shader = compile_source(self.shader.format(tile_size=tile_size))
7070
self.tensor_shape: tuple[int, int] = (0, 0)
7171
self.params: list[kp.Tensor] = []
7272
self.algo = None
@@ -79,7 +79,7 @@ def __call__(self, tensor_shape: tuple[int, int], tensor_in_1: kp.Tensor, tensor
7979
self.tensor_shape = tensor_shape
8080
self.params = params
8181
tile_size = min(tensor_shape[0], tensor_shape[1], self.tile_size)
82-
self.compiled_shader = kp.Shader.compile_source(self.shader.format(tile_size=tile_size))
82+
self.compiled_shader = compile_source(self.shader.format(tile_size=tile_size))
8383
workgroup = [tensor_shape[0] // tile_size, tensor_shape[1] // tile_size, 1]
8484
self.algo = self.mgr.algorithm(
8585
params, # params

‎examples/python_naive_matmul/imp3_better_tiling.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import kp
44
import numpy as np
5-
5+
from utils import compile_source
66

77
class MatMulOp:
88
def __init__(self, manager: kp.Manager, tile_size: int = -1, thread_work_ratio: int = 16):
@@ -83,7 +83,7 @@ def __init__(self, manager: kp.Manager, tile_size: int = -1, thread_work_ratio:
8383
for(uint w = 0u; w < {thread_work_ratio}; w++)
8484
out_tensor[(globalCol + w * {local_size_y}) * tensor_size + globalRow] = acc[w];
8585
}}'''
86-
self.compiled_shader = kp.Shader.compile_source(self.shader.format(
86+
self.compiled_shader = compile_source(self.shader.format(
8787
tile_size=tile_size, thread_work_ratio=thread_work_ratio, local_size_y=local_size_y))
8888
self.tensor_shape: tuple[int, int] = (0, 0)
8989
self.params: list[kp.Tensor] = []
@@ -99,7 +99,7 @@ def __call__(self, tensor_shape: tuple[int, int], tensor_in_1: kp.Tensor, tensor
9999
tile_size = min(self.tensor_shape[0], self.tile_size)
100100
thread_work_ratio = min(self.tensor_shape[1] // self.tile_size, self.thread_work_ratio)
101101
local_size_y = tile_size // thread_work_ratio
102-
self.compiled_shader = kp.Shader.compile_source(self.shader.format(
102+
self.compiled_shader = compile_source(self.shader.format(
103103
tile_size=tile_size, thread_work_ratio=thread_work_ratio, local_size_y=local_size_y))
104104
workgroup = (tensor_shape[0] // self.local_size_x, tensor_shape[1] // self.local_size_y, 1)
105105
self.algo = self.mgr.algorithm(
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import os
2+
3+
4+
def compile_source(source):
5+
open("tmp_kp_shader.comp", "w").write(source)
6+
os.system("glslangValidator -V tmp_kp_shader.comp -o tmp_kp_shader.comp.spv")
7+
return open("tmp_kp_shader.comp.spv", "rb").read()

0 commit comments

Comments
 (0)