Delete kernel.py
Browse files
kernel.py
DELETED
|
@@ -1,136 +0,0 @@
|
|
| 1 |
-
from typing import Tuple
|
| 2 |
-
|
| 3 |
-
import torch
|
| 4 |
-
import triton
|
| 5 |
-
import triton.language as tl
|
| 6 |
-
from triton import Config
|
| 7 |
-
|
| 8 |
-
@triton.jit
|
| 9 |
-
def act_quant_kernel(x_ptr, y_ptr, s_ptr, BLOCK_SIZE: tl.constexpr):
|
| 10 |
-
pid = tl.program_id(axis=0)
|
| 11 |
-
offs = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
|
| 12 |
-
x = tl.load(x_ptr + offs).to(tl.float32)
|
| 13 |
-
s = tl.max(tl.abs(x)) / 448.
|
| 14 |
-
y = x / s
|
| 15 |
-
y = y.to(y_ptr.dtype.element_ty)
|
| 16 |
-
tl.store(y_ptr + offs, y)
|
| 17 |
-
tl.store(s_ptr + pid, s)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def act_quant(x: torch.Tensor, block_size: int = 128) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 21 |
-
assert x.is_contiguous()
|
| 22 |
-
assert x.size(-1) % block_size == 0
|
| 23 |
-
y = torch.empty_like(x, dtype=torch.float8_e4m3fn)
|
| 24 |
-
s = x.new_empty(*x.size()[:-1], x.size(-1) // block_size, dtype=torch.float32)
|
| 25 |
-
grid = lambda meta: (triton.cdiv(x.numel(), meta['BLOCK_SIZE']), )
|
| 26 |
-
act_quant_kernel[grid](x, y, s, BLOCK_SIZE=block_size)
|
| 27 |
-
return y, s
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
@triton.jit
|
| 31 |
-
def weight_dequant_kernel(x_ptr, s_ptr, y_ptr, M, N, BLOCK_SIZE: tl.constexpr):
|
| 32 |
-
pid_m = tl.program_id(axis=0)
|
| 33 |
-
pid_n = tl.program_id(axis=1)
|
| 34 |
-
n = tl.cdiv(N, BLOCK_SIZE)
|
| 35 |
-
offs_m = pid_m * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
|
| 36 |
-
offs_n = pid_n * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
|
| 37 |
-
offs = offs_m[:, None] * N + offs_n[None, :]
|
| 38 |
-
mask = (offs_m[:, None] < M) & (offs_n[None, :] < N)
|
| 39 |
-
x = tl.load(x_ptr + offs, mask=mask).to(tl.float32)
|
| 40 |
-
s = tl.load(s_ptr + pid_m * n + pid_n)
|
| 41 |
-
y = x * s
|
| 42 |
-
tl.store(y_ptr + offs, y, mask=mask)
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
def weight_dequant(x: torch.Tensor, s: torch.Tensor, block_size: int = 128) -> torch.Tensor:
|
| 46 |
-
assert x.is_contiguous() and s.is_contiguous()
|
| 47 |
-
assert x.dim() == 2 and s.dim() == 2
|
| 48 |
-
M, N = x.size()
|
| 49 |
-
y = torch.empty_like(x, dtype=torch.get_default_dtype())
|
| 50 |
-
grid = lambda meta: (triton.cdiv(M, meta['BLOCK_SIZE']), triton.cdiv(N, meta['BLOCK_SIZE']))
|
| 51 |
-
weight_dequant_kernel[grid](x, s, y, M, N, BLOCK_SIZE=block_size)
|
| 52 |
-
return y
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
@triton.jit
|
| 56 |
-
def weight_quant_kernel(x_ptr, y_ptr, s_ptr, M, N, BLOCK_SIZE: tl.constexpr):
|
| 57 |
-
pid_m = tl.program_id(axis=0)
|
| 58 |
-
pid_n = tl.program_id(axis=1)
|
| 59 |
-
n = tl.cdiv(N, BLOCK_SIZE)
|
| 60 |
-
offs_m = pid_m * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
|
| 61 |
-
offs_n = pid_n * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
|
| 62 |
-
offs = offs_m[:, None] * N + offs_n[None, :]
|
| 63 |
-
mask = (offs_m[:, None] < M) & (offs_n[None, :] < N)
|
| 64 |
-
x = tl.load(x_ptr + offs, mask=mask).to(tl.float32)
|
| 65 |
-
s = tl.max(tl.abs(x)) / 127.#int8
|
| 66 |
-
y = x / s
|
| 67 |
-
y = y.to(y_ptr.dtype.element_ty)
|
| 68 |
-
tl.store(y_ptr + offs, y, mask=mask)
|
| 69 |
-
tl.store(s_ptr + pid_m * n + pid_n, s)
|
| 70 |
-
|
| 71 |
-
# quant to block int8
|
| 72 |
-
def weight_quant(x: torch.Tensor, block_size: int = 128) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 73 |
-
assert x.is_contiguous()
|
| 74 |
-
assert x.dim() == 2
|
| 75 |
-
M, N = x.size()
|
| 76 |
-
y = torch.empty_like(x, dtype=torch.int8)
|
| 77 |
-
sM, sN = torch.tensor(1.0*M/block_size).ceil().int(), torch.tensor(1.0*N/block_size).ceil().int()
|
| 78 |
-
s = x.new_empty(sM, sN, dtype=torch.float32)
|
| 79 |
-
grid = lambda meta: (triton.cdiv(M, meta['BLOCK_SIZE']), triton.cdiv(N, meta['BLOCK_SIZE']))
|
| 80 |
-
weight_quant_kernel[grid](x, y, s, M, N, BLOCK_SIZE=block_size)
|
| 81 |
-
return y, s
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
fp8_gemm_configs = [
|
| 85 |
-
Config({'BLOCK_SIZE_M': block_m, 'BLOCK_SIZE_N': block_n, 'BLOCK_SIZE_K': 128}, num_stages=num_stages, num_warps=8)
|
| 86 |
-
for block_m in [16, 32, 64] for block_n in [32, 64, 128] for num_stages in [3, 4, 5, 6]
|
| 87 |
-
]
|
| 88 |
-
|
| 89 |
-
@triton.autotune(configs=fp8_gemm_configs, key=['N', 'K'])
|
| 90 |
-
@triton.jit
|
| 91 |
-
def fp8_gemm_kernel(a_ptr, b_ptr, c_ptr,
|
| 92 |
-
a_s_ptr, b_s_ptr,
|
| 93 |
-
M, N: tl.constexpr, K: tl.constexpr,
|
| 94 |
-
BLOCK_SIZE_M: tl.constexpr,
|
| 95 |
-
BLOCK_SIZE_N: tl.constexpr,
|
| 96 |
-
BLOCK_SIZE_K: tl.constexpr):
|
| 97 |
-
pid_m = tl.program_id(axis=0)
|
| 98 |
-
pid_n = tl.program_id(axis=1)
|
| 99 |
-
k = tl.cdiv(K, BLOCK_SIZE_K)
|
| 100 |
-
offs_m = (pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)) % M
|
| 101 |
-
offs_n = (pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)) % N
|
| 102 |
-
offs_k = tl.arange(0, BLOCK_SIZE_K)
|
| 103 |
-
a_ptrs = a_ptr + offs_m[:, None] * K + offs_k[None, :]
|
| 104 |
-
b_ptrs = b_ptr + offs_n[None, :] * K + offs_k[:, None]
|
| 105 |
-
a_s_ptrs = a_s_ptr + offs_m * k
|
| 106 |
-
b_s_ptrs = b_s_ptr + (offs_n // BLOCK_SIZE_K) * k
|
| 107 |
-
|
| 108 |
-
accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32)
|
| 109 |
-
for i in range(k):
|
| 110 |
-
a = tl.load(a_ptrs, mask=offs_k[None, :] < K - i * BLOCK_SIZE_K, other=0.0)
|
| 111 |
-
b = tl.load(b_ptrs, mask=offs_k[:, None] < K - i * BLOCK_SIZE_K, other=0.0)
|
| 112 |
-
a_s = tl.load(a_s_ptrs)
|
| 113 |
-
b_s = tl.load(b_s_ptrs)
|
| 114 |
-
accumulator += tl.dot(a, b) * a_s[:, None] * b_s[None, :]
|
| 115 |
-
a_ptrs += BLOCK_SIZE_K
|
| 116 |
-
b_ptrs += BLOCK_SIZE_K
|
| 117 |
-
a_s_ptrs += 1
|
| 118 |
-
b_s_ptrs += 1
|
| 119 |
-
c = accumulator.to(c_ptr.dtype.element_ty)
|
| 120 |
-
offs_m = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)
|
| 121 |
-
offs_n = pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)
|
| 122 |
-
c_ptrs = c_ptr + offs_m[:, None] * N + offs_n[None, :]
|
| 123 |
-
mask = (offs_m[:, None] < M) & (offs_n[None, :] < N)
|
| 124 |
-
tl.store(c_ptrs, c, mask=mask)
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
def fp8_gemm(a: torch.Tensor, a_s: torch.Tensor, b: torch.Tensor, b_s: torch.Tensor):
|
| 128 |
-
assert a.is_contiguous() and b.is_contiguous()
|
| 129 |
-
assert a_s.is_contiguous() and b_s.is_contiguous()
|
| 130 |
-
K = a.size(-1)
|
| 131 |
-
M = a.numel() // K
|
| 132 |
-
N = b.size(0)
|
| 133 |
-
c = a.new_empty(*a.size()[:-1], N, dtype=torch.get_default_dtype())
|
| 134 |
-
grid = lambda META: (triton.cdiv(M, META['BLOCK_SIZE_M']), triton.cdiv(N, META['BLOCK_SIZE_N']))
|
| 135 |
-
fp8_gemm_kernel[grid](a, b, c, a_s, b_s, M, N, K)
|
| 136 |
-
return c
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|