-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitk_dequant_gemm.py
141 lines (105 loc) · 4.95 KB
/
splitk_dequant_gemm.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import torch
import triton
from triton import language as tl
# from actual_base_gptq_4 import triton_matmul4
@triton.jit()
def swizzle_tile(pid,
m, n,
block_m: tl.constexpr, block_n: tl.constexpr, group_m: tl.constexpr):
grid_m = tl.cdiv(m, block_m)
grid_n = tl.cdiv(n, block_n)
width = group_m * grid_n
group_id = pid // width
group_size = tl.minimum(grid_m - group_id * group_m, group_m)
pid_m = group_id * group_m + (pid % group_size)
pid_n = (pid % width) // group_size
return pid_m, pid_n
@triton.jit()
def matmul_split_k_kernel(a_ptr, b_ptr, c_ptr, scales_ptr, zeros_ptr,
stride_am, stride_ak,
stride_bk, stride_bn,
stride_cm, stride_cn,
stride_scales_g, stride_scales_n,
stride_zeros_g, stride_zeros_n,
groupsize,
m, n, k,
block_m: tl.constexpr, block_n: tl.constexpr, block_k: tl.constexpr,
group_m: tl.constexpr, split_k: tl.constexpr):
pid = tl.program_id(0)
pid_k = tl.program_id(1)
total_blocks_k = tl.cdiv(k, block_k*split_k)
pid_m, pid_n = swizzle_tile(pid,
m, n,
block_m, block_n, group_m)
offs_m = pid_m*block_m + tl.arange(0, block_m)
offs_n = pid_n*block_n + tl.arange(0, block_n)
offs_k = pid_k*block_k + tl.arange(0, block_k)
offs_am = tl.max_contiguous(tl.multiple_of(offs_m, block_m), block_m)
offs_bn = tl.max_contiguous(tl.multiple_of(offs_n, block_n), block_n)
a_ptrs = a_ptr + (offs_am[:, None] * stride_am + offs_k[None, :] * stride_ak)
b_ptrs = b_ptr + ((offs_k[:, None] // 8) * stride_bk + offs_bn[None, :] * stride_bn)
scales_ptrs = scales_ptr + offs_bn * stride_scales_n
zeros_ptrs = zeros_ptr + ((offs_bn // 8) * stride_zeros_n)
shifter = (offs_k % 8) * 4
zeros_shifter = (offs_bn % 8) * 4
acc = tl.zeros((block_m, block_n), dtype=tl.float32)
for k in range(0, total_blocks_k):
a = tl.load(a_ptrs)
b = tl.load(b_ptrs)
g_id = (k * split_k + pid_k) // (groupsize // block_k)
ptr = scales_ptrs + g_id * stride_scales_g
scales = tl.load(ptr)
ptr = zeros_ptrs + g_id * stride_zeros_g
zeros = tl.load(ptr)
zeros = (zeros >> zeros_shifter) & 0xF
zeros = (zeros + 1) * scales
b = (b >> shifter[:, None]) & 0xF
b = b * scales[None, :] - zeros[None, :]
acc += tl.dot(a, b)
a_ptrs += block_k * split_k * stride_ak
b_ptrs += (block_k // 8) * split_k * stride_bk
acc.to(tl.float16)
offs_m = pid_m*block_m + tl.arange(0, block_m)
offs_n = pid_n*block_n + tl.arange(0, block_n)
c_ptrs = c_ptr + (offs_m[:, None] * stride_cm + offs_n[None, :] * stride_cn)
tl.atomic_add(c_ptrs, acc, sem='release')
def matmul_split_k(a, b, scales, zeros):
m, k = a.shape
_, n = b.shape
quant_groupsize = 128
block_m = 16
block_n = 32
block_k = 128
group_m = 8
num_stages = 3
num_warps = 4
split_k = 4
total_blocks_m = triton.cdiv(m, block_m)
total_blocks_n = triton.cdiv(n, block_n)
total_programs_mn = total_blocks_m * total_blocks_n
total_programs_k = split_k
grid = (total_programs_mn, total_programs_k)
# print(f"problem m size: {m}, tile size m: {block_m}, total blocks m: {total_blocks_m}")
# print(f"problem n size: {n}, tile size n: {block_n}, total blocks n: {total_blocks_n}")
# print(f"problem k size: {k}, tile size k: {block_k}, total thread blocks k: {split_k}")
# print(f"total thread blocks k: {k}, total thread blocks m and total thread blocks n = {total_blocks_m=} x {total_blocks_n} = {total_programs_mn}")
# print(f"{total_programs_mn=}, {total_programs_k=}")
c = torch.zeros((m, n), device=a.device, dtype=torch.float16)
k = matmul_split_k_kernel[grid](a, b, c, scales, zeros,
a.stride(0), a.stride(1),
b.stride(0), b.stride(1),
c.stride(0), c.stride(1),
scales.stride(0), scales.stride(1),
zeros.stride(0), zeros.stride(1),
quant_groupsize,
m, n, k,
block_m, block_n, block_k,
group_m, split_k, num_stages=num_stages, num_warps=num_warps)
# print(f"{k.n_regs} registers used, {k.n_spills} spills, {k.shared/1000} kB shared memory\n")
# with open('matmul_split_k.txt', 'w') as f:
# print(f"{k.n_regs} registers used, {k.n_spills} spills, {k.shared/1000} kB shared memory\n", file=f)
# print("IR", k.asm['ttir'], file=f)
# print("TTGIR", k.asm['ttgir'], file=f)
# print("PTX", k.asm['ptx'], file=f)
# print(f"{k.n_regs} registers used, {k.n_spills} spills, {k.shared/1000} kB shared memory\n", file=f)
return c