-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix_Multiplication_Triton.py
More file actions
56 lines (46 loc) · 1.73 KB
/
Matrix_Multiplication_Triton.py
File metadata and controls
56 lines (46 loc) · 1.73 KB
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
import torch
import triton
import triton.language as tl
@triton.jit
def matmul_triton_kernel(
A, B, C,
M, N, K,
stride_A_row, stride_A_col,
stride_B_col, stride_B_k,
stride_C_row, stride_C_col,
TILE_M: tl.constexpr,
TILE_N: tl.constexpr,
TILE_K: tl.constexpr
):
pid_row = tl.program_id(0)
pid_col = tl.program_id(1)
offs_row = pid_row * TILE_M + tl.arange(0, TILE_M)
offs_col = pid_col * TILE_K + tl.arange(0, TILE_K)
out_ptrs = C + (offs_row[:, None] * stride_C_row + offs_col[None, :] * stride_C_col)
acc = tl.zeros((TILE_M, TILE_K), dtype=tl.float32)
for n in range(0, N, TILE_N):
offs_mid = n + tl.arange(0, TILE_N)
A_tile = A + (offs_row[:, None] * stride_A_row + offs_mid[None, :] * stride_A_col)
B_tile = B + (offs_mid[:, None] * stride_B_col + offs_col[None, :] * stride_B_k)
A_mask = (offs_row[:, None] < M) & (offs_mid[None, :] < N)
B_mask = (offs_mid[:, None] < N) & (offs_col[None, :] < K)
a_block = tl.load(A_tile, mask=A_mask, other=0.0)
b_block = tl.load(B_tile, mask=B_mask, other=0.0)
acc += tl.dot(a_block, b_block)
out_mask = (offs_row[:, None] < M) & (offs_col[None, :] < K)
tl.store(out_ptrs, acc, mask=out_mask)
def run_matmul(A: torch.Tensor, B: torch.Tensor, C: torch.Tensor, M: int, N: int, K: int):
sA_row, sA_col = A.stride()
sB_col, sB_k = B.stride()
sC_row, sC_col = C.stride()
TILE_M, TILE_N, TILE_K = 64, 32, 64
grid = (triton.cdiv(M, TILE_M), triton.cdiv(K, TILE_K))
matmul_triton_kernel[grid](
A, B, C,
M, N, K,
sA_row, sA_col,
sB_col, sB_k,
sC_row, sC_col,
TILE_M, TILE_N, TILE_K,
num_warps=4, num_stages=2
)