Instructions to use replicate/aiter-kernels with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Kernels
How to use replicate/aiter-kernels with Kernels:
# !pip install kernels from kernels import get_kernel # a version (or an explicit revision) is required; see the "Files and versions" tab for the available ones kernel = get_kernel("replicate/aiter-kernels", version=1) - Notebooks
- Google Colab
- Kaggle
File size: 8,279 Bytes
2976eec | 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | # SPDX-License-Identifier: MIT
# Copyright (C) 2024-2026, Advanced Micro Devices, Inc. All rights reserved.
from typing import Optional
import torch
import triton
from ..._triton_kernels.common.splitk_reduce import (
_gemm_splitk_reduce_kernel,
)
from ..._triton_kernels.gemm.basic.gemm_a16w8_blockscale import (
_gemm_a16w8_blockscale_kernel,
_gemm_a16w8_blockscale_preshuffle_kernel,
_get_config,
)
from ...utils.logger import AiterTritonLogger
from ...utils.gemm_config_utils import compute_splitk_params
_LOGGER = AiterTritonLogger()
def gemm_a16w8_blockscale(
x: torch.Tensor,
w: torch.Tensor,
w_scale: torch.Tensor,
dtype: Optional[float] = torch.bfloat16,
y: Optional[torch.Tensor] = None,
prequant: Optional[bool] = False,
config: Optional[dict] = None,
skip_reduce: Optional[bool] = False,
):
"""
Computes the 8 bit matmul Y = X x WT using the block-scale quantization approach.
Key parameters:
- X: Matrix X with shape (M, K).
- W: Matrix W with shape (N, K).
- W_scale: Scale tensor for W with shape (**scale_n, *scale_k).
Returns:
- Y: The output matrix with shape (M, N).
*scale_k = (K + scale_block_size_k - 1) // scale_block_size_k
**scale_n = (N + scale_block_size_n - 1) // scale_block_size_n
"""
_LOGGER.info(
f"GEMM_A8W8_BLOCKSCALE: x={tuple(x.shape)} w={tuple(w.shape)} w_scale={tuple(w_scale.shape)}"
)
M, K = x.shape
N, K = w.shape
# Check constraints.
assert x.shape[1] == w.shape[1], "Incompatible dimensions!!!"
# Transpose w and w_scale
w = w.T
w_scale = w_scale.T
if config is None:
config, _ = _get_config(M, N, K)
return_y_pp = config["NUM_KSPLIT"] > 1 and skip_reduce
if config["NUM_KSPLIT"] > 1:
y_pp = torch.empty(
(config["NUM_KSPLIT"], M, N), dtype=torch.float32, device=x.device
)
else:
y_pp = None
if y is None and not return_y_pp:
y = torch.empty((M, N), dtype=dtype, device=x.device)
compute_splitk_params(config, K)
# Scale block sizes
# TODO: need a better way to pass scale block sizes around
config["GROUP_K"] = triton.next_power_of_2(triton.cdiv(K, w_scale.shape[0]))
config["GROUP_N"] = triton.next_power_of_2(triton.cdiv(N, w_scale.shape[1]))
DTYPE_MAX = (
torch.finfo(w.dtype).max
if torch.is_floating_point(w)
else torch.iinfo(w.dtype).max
)
# grid = (config["NUM_KSPLIT"], triton.cdiv(M, config["BLOCK_SIZE_M"]) * triton.cdiv(N, config["BLOCK_SIZE_N"]),)
grid = lambda META: ( # noqa: E731
(
META["NUM_KSPLIT"]
* triton.cdiv(M, META["BLOCK_SIZE_M"])
* triton.cdiv(N, META["BLOCK_SIZE_N"])
),
)
_gemm_a16w8_blockscale_kernel[grid](
x,
w,
y if config["NUM_KSPLIT"] == 1 else y_pp,
w_scale,
M,
N,
K,
x.stride(0),
x.stride(1),
w.stride(0),
w.stride(1),
0 if config["NUM_KSPLIT"] == 1 else y_pp.stride(0),
y.stride(0) if config["NUM_KSPLIT"] == 1 else y_pp.stride(1),
y.stride(1) if config["NUM_KSPLIT"] == 1 else y_pp.stride(2),
w_scale.stride(0),
w_scale.stride(1),
PREQUANT=prequant,
DTYPE_MAX=DTYPE_MAX,
DTYPE_MIN=-DTYPE_MAX,
**config,
)
if return_y_pp:
return y_pp
elif config["NUM_KSPLIT"] > 1:
REDUCE_BLOCK_SIZE_M = 32
REDUCE_BLOCK_SIZE_N = 32
ACTUAL_KSPLIT = triton.cdiv(K, config["SPLITK_BLOCK_SIZE"])
grid_reduce = (
triton.cdiv(M, REDUCE_BLOCK_SIZE_M),
triton.cdiv(N, REDUCE_BLOCK_SIZE_N),
)
_gemm_splitk_reduce_kernel[grid_reduce](
y_pp,
y,
None,
M,
N,
y_pp.stride(0),
y_pp.stride(1),
y_pp.stride(2),
y.stride(0),
y.stride(1),
REDUCE_BLOCK_SIZE_M,
REDUCE_BLOCK_SIZE_N,
ACTUAL_KSPLIT,
triton.next_power_of_2(config["NUM_KSPLIT"]),
ADD_BIAS=False,
activation="",
use_activation=False,
KERNEL_NAME="_gemm_a8w8_blockscale_reduce_kernel",
)
return y
def gemm_a16w8_blockscale_preshuffle(
x: torch.Tensor,
w: torch.Tensor,
w_scale: torch.Tensor,
dtype: Optional[float] = torch.bfloat16,
y: Optional[torch.Tensor] = None,
prequant: Optional[bool] = False,
config: Optional[dict] = None,
skip_reduce: Optional[bool] = False,
):
"""
Computes the 8 bit matmul Y = X x WT using the block-scale quantization approach.
Key parameters:
- X: Matrix X with shape (M, K).
- W: Matrix W with shape (N, K).
- W_scale: Scale tensor for W with shape (**scale_n, *scale_k).
Returns:
- Y: The output matrix with shape (M, N).
*scale_k = (K + scale_block_size_k - 1) // scale_block_size_k
**scale_n = (N + scale_block_size_n - 1) // scale_block_size_n
"""
_LOGGER.info(
f"GEMM_A8W8_BLOCKSCALE: x={tuple(x.shape)} w={tuple(w.shape)} w_scale={tuple(w_scale.shape)}"
)
M, K = x.shape
N, K = w.shape
N = N * 16
K = K // 16
# Check constraints.
assert x.shape[1] == w.shape[1] // 16, "Incompatible dimensions!!!"
if config is None:
config, _ = _get_config(M, N, K, True)
return_y_pp = config["NUM_KSPLIT"] > 1 and skip_reduce
if config["NUM_KSPLIT"] > 1:
y_pp = torch.empty(
(config["NUM_KSPLIT"], M, N), dtype=torch.float32, device=x.device
)
else:
y_pp = None
if y is None and not return_y_pp:
y = torch.empty((M, N), dtype=dtype, device=x.device)
compute_splitk_params(config, K)
# Scale block sizes
# TODO: need a better way to pass scale block sizes around
config["GROUP_K"] = triton.next_power_of_2(
triton.cdiv(K, w_scale.shape[1])
) # scale_block_size_k
config["GROUP_N"] = triton.next_power_of_2(
triton.cdiv(N, w_scale.shape[0])
) # scale_block_size_n
assert (
config["GROUP_K"] == config["BLOCK_SIZE_K"]
), "GROUP_K must equal BLOCK_SIZE_K"
DTYPE_MAX = (
torch.finfo(w.dtype).max
if torch.is_floating_point(w)
else torch.iinfo(w.dtype).max
)
# grid = (config["NUM_KSPLIT"], triton.cdiv(M, config["BLOCK_SIZE_M"]) * triton.cdiv(N, config["BLOCK_SIZE_N"]),)
grid = lambda META: ( # noqa: E731
(
META["NUM_KSPLIT"]
* triton.cdiv(M, META["BLOCK_SIZE_M"])
* triton.cdiv(N, META["BLOCK_SIZE_N"])
),
)
_gemm_a16w8_blockscale_preshuffle_kernel[grid](
x,
w,
y if config["NUM_KSPLIT"] == 1 else y_pp,
w_scale,
M,
N,
K,
x.stride(0),
x.stride(1),
w.stride(0),
w.stride(1),
0 if config["NUM_KSPLIT"] == 1 else y_pp.stride(0),
y.stride(0) if config["NUM_KSPLIT"] == 1 else y_pp.stride(1),
y.stride(1) if config["NUM_KSPLIT"] == 1 else y_pp.stride(2),
w_scale.stride(0),
w_scale.stride(1),
PREQUANT=prequant,
DTYPE_MAX=DTYPE_MAX,
DTYPE_MIN=-DTYPE_MAX,
**config,
)
if return_y_pp:
return y_pp
elif config["NUM_KSPLIT"] > 1:
REDUCE_BLOCK_SIZE_M = 32
REDUCE_BLOCK_SIZE_N = 32
ACTUAL_KSPLIT = triton.cdiv(K, config["SPLITK_BLOCK_SIZE"])
grid_reduce = (
triton.cdiv(M, REDUCE_BLOCK_SIZE_M),
triton.cdiv(N, REDUCE_BLOCK_SIZE_N),
)
_gemm_splitk_reduce_kernel[grid_reduce](
y_pp,
y,
None,
M,
N,
y_pp.stride(0),
y_pp.stride(1),
y_pp.stride(2),
y.stride(0),
y.stride(1),
REDUCE_BLOCK_SIZE_M,
REDUCE_BLOCK_SIZE_N,
ACTUAL_KSPLIT,
triton.next_power_of_2(config["NUM_KSPLIT"]),
ADD_BIAS=False,
activation="",
use_activation=False,
KERNEL_NAME="_gemm_a8w8_blockscale_reduce_kernel",
)
return y
|