kernel.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from typing import Tuple
  2. import torch
  3. import triton
  4. import triton.language as tl
  5. from triton import Config
  6. @triton.jit
  7. def act_quant_kernel(x_ptr, y_ptr, s_ptr, BLOCK_SIZE: tl.constexpr):
  8. pid = tl.program_id(axis=0)
  9. offs = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
  10. x = tl.load(x_ptr + offs).to(tl.float32)
  11. s = tl.max(tl.abs(x)) / 448.
  12. y = x / s
  13. y = y.to(y_ptr.dtype.element_ty)
  14. tl.store(y_ptr + offs, y)
  15. tl.store(s_ptr + pid, s)
  16. def act_quant(x: torch.Tensor, block_size: int = 128) -> Tuple[torch.Tensor, torch.Tensor]:
  17. assert x.is_contiguous()
  18. assert x.size(-1) % block_size == 0
  19. y = torch.empty_like(x, dtype=torch.float8_e4m3fn)
  20. s = x.new_empty(*x.size()[:-1], x.size(-1) // block_size, dtype=torch.float32)
  21. grid = lambda meta: (triton.cdiv(x.numel(), meta['BLOCK_SIZE']), )
  22. act_quant_kernel[grid](x, y, s, BLOCK_SIZE=block_size)
  23. return y, s
  24. @triton.jit
  25. def weight_dequant_kernel(x_ptr, s_ptr, y_ptr, M, N, BLOCK_SIZE: tl.constexpr):
  26. pid_m = tl.program_id(axis=0)
  27. pid_n = tl.program_id(axis=1)
  28. n = tl.cdiv(N, BLOCK_SIZE)
  29. offs_m = pid_m * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
  30. offs_n = pid_n * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
  31. offs = offs_m[:, None] * N + offs_n[None, :]
  32. mask = (offs_m[:, None] < M) & (offs_n[None, :] < N)
  33. x = tl.load(x_ptr + offs, mask=mask).to(tl.float32)
  34. s = tl.load(s_ptr + pid_m * n + pid_n)
  35. y = x * s
  36. tl.store(y_ptr + offs, y, mask=mask)
  37. def weight_dequant(x: torch.Tensor, s: torch.Tensor, block_size: int = 128) -> torch.Tensor:
  38. assert x.is_contiguous() and s.is_contiguous()
  39. assert x.dim() == 2 and s.dim() == 2
  40. M, N = x.size()
  41. y = torch.empty_like(x, dtype=torch.get_default_dtype())
  42. grid = lambda meta: (triton.cdiv(M, meta['BLOCK_SIZE']), triton.cdiv(N, meta['BLOCK_SIZE']))
  43. weight_dequant_kernel[grid](x, s, y, M, N, BLOCK_SIZE=block_size)
  44. return y
  45. fp8_gemm_configs = [
  46. Config({'BLOCK_SIZE_M': block_m, 'BLOCK_SIZE_N': block_n, 'BLOCK_SIZE_K': 128}, num_stages=num_stages, num_warps=8)
  47. for block_m in [16, 32, 64] for block_n in [32, 64, 128] for num_stages in [3, 4, 5, 6]
  48. ]
  49. @triton.autotune(configs=fp8_gemm_configs, key=['N', 'K'])
  50. @triton.jit
  51. def fp8_gemm_kernel(a_ptr, b_ptr, c_ptr,
  52. a_s_ptr, b_s_ptr,
  53. M, N: tl.constexpr, K: tl.constexpr,
  54. BLOCK_SIZE_M: tl.constexpr,
  55. BLOCK_SIZE_N: tl.constexpr,
  56. BLOCK_SIZE_K: tl.constexpr):
  57. pid_m = tl.program_id(axis=0)
  58. pid_n = tl.program_id(axis=1)
  59. k = tl.cdiv(K, BLOCK_SIZE_K)
  60. offs_m = (pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)) % M
  61. offs_n = (pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)) % N
  62. offs_k = tl.arange(0, BLOCK_SIZE_K)
  63. a_ptrs = a_ptr + offs_m[:, None] * K + offs_k[None, :]
  64. b_ptrs = b_ptr + offs_n[None, :] * K + offs_k[:, None]
  65. a_s_ptrs = a_s_ptr + offs_m * k
  66. b_s_ptrs = b_s_ptr + (offs_n // BLOCK_SIZE_K) * k
  67. accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32)
  68. for i in range(k):
  69. a = tl.load(a_ptrs, mask=offs_k[None, :] < K - i * BLOCK_SIZE_K, other=0.0)
  70. b = tl.load(b_ptrs, mask=offs_k[:, None] < K - i * BLOCK_SIZE_K, other=0.0)
  71. a_s = tl.load(a_s_ptrs)
  72. b_s = tl.load(b_s_ptrs)
  73. accumulator += tl.dot(a, b) * a_s[:, None] * b_s[None, :]
  74. a_ptrs += BLOCK_SIZE_K
  75. b_ptrs += BLOCK_SIZE_K
  76. a_s_ptrs += 1
  77. b_s_ptrs += 1
  78. c = accumulator.to(c_ptr.dtype.element_ty)
  79. offs_m = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)
  80. offs_n = pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)
  81. c_ptrs = c_ptr + offs_m[:, None] * N + offs_n[None, :]
  82. mask = (offs_m[:, None] < M) & (offs_n[None, :] < N)
  83. tl.store(c_ptrs, c, mask=mask)
  84. def fp8_gemm(a: torch.Tensor, a_s: torch.Tensor, b: torch.Tensor, b_s: torch.Tensor):
  85. assert a.is_contiguous() and b.is_contiguous()
  86. assert a_s.is_contiguous() and b_s.is_contiguous()
  87. K = a.size(-1)
  88. M = a.numel() // K
  89. N = b.size(0)
  90. c = a.new_empty(*a.size()[:-1], N, dtype=torch.get_default_dtype())
  91. grid = lambda META: (triton.cdiv(M, META['BLOCK_SIZE_M']), triton.cdiv(N, META['BLOCK_SIZE_N']))
  92. fp8_gemm_kernel[grid](a, b, c, a_s, b_s, M, N, K)
  93. return c