Add full Marlin support and tests for Marlin/CUTLASS
Browse filesFiles changed (26)
- build.toml+19-4
- ext-torch/__init__.py+30-177
- ext-torch/compressed_tensors.pyAdded+110
- ext-torch/cutlass.pyAdded+75
- ext-torch/marlin.pyAdded+208
- ext-torch/scalar_type.pyAdded+330
- ext-torch/torch_binding.cpp+29-3
- ext-torch/torch_binding.h+23
- ext-torch/utils/marlin_utils.pyAdded+391
- ext-torch/utils/marlin_utils_fp8.pyAdded+100
- ext-torch/utils/marlin_utils_test.pyAdded+162
- ext-torch/utils/marlin_utils_test_24.pyAdded+473
- ext-torch/utils/marlin_utils_test_qqq.pyAdded+125
- ext-torch/utils/quant_utils.pyAdded+470
- marlin/dense/LICENSEAdded+209
- marlin/dense/common/base.hAdded+32
- marlin/dense/common/mem.hAdded+89
- marlin/dense/marlin_cuda_kernel.cuAdded+1068
- marlin/qqq/marlin_qqq_gemm_kernel.cuAdded+1243
- marlin/sparse/LICENSEAdded+203
- marlin/sparse/common/base.hAdded+51
- marlin/sparse/common/mem.hAdded+136
- marlin/sparse/common/mma.hAdded+191
- marlin/sparse/marlin_24_cuda_kernel.cuAdded+1140
- tests/kernels/test_marlin_gemm.pyAdded+733
- tests/kernels/utils.py+38-27
9 unmodified lines1011121314151314151640 unmodified lines5758596260616217 unmodified lines80818283848586878889909192939495969798991009 unmodified lines "ext-torch/torch_binding.h"]include = [ "." ]pysrc = [ "ext-torch/__init__.py"]pyroot = "ext-torch"
[kernel.cutlass_w8a8]capabilities = [ "7.5", "8.0", "8.6", "8.7", "8.9", "9.0", "9.0a" ]40 unmodified lines "gptq_marlin/marlin.cuh", "gptq_marlin/marlin_dtypes.cuh",]#include = [ "." ]depends = [ "torch" ]
[kernel.int8_common]17 unmodified lines]include = [ "." ]depends = [ "torch" ]
[kernel.marlin]capabilities = [ "8.0", "8.6", "8.7", "8.9", "9.0", "9.0a" ]src = [ "core/scalar_type.hpp", "marlin/dense/common/base.h", "marlin/dense/common/mem.h", "marlin/dense/marlin_cuda_kernel.cu", "marlin/qqq/marlin_qqq_gemm_kernel.cu", "marlin/sparse/common/base.h", "marlin/sparse/common/mem.h", "marlin/sparse/common/mma.h", "marlin/sparse/marlin_24_cuda_kernel.cu"]include = [ "." ]depends = [ "torch" ]
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177123456789101112131415161718192021222324252627282930from typing import Optional, Tuple
import torch
try: from ._ops import opsexcept ImportError as e: # Fallback for local development. try: import _quantization ops = torch.ops._quantization except ImportError: raise e
def cutlass_scaled_mm_supports_fp8(cuda_device_capability: int) -> bool: return ops.cutlass_scaled_mm_supports_fp8(cuda_device_capability)
def cutlass_scaled_mm(a: torch.Tensor, b: torch.Tensor, scale_a: torch.Tensor, scale_b: torch.Tensor, out_dtype: torch.dtype, bias: Optional[torch.Tensor] = None) -> torch.Tensor: assert (b.shape[0] % 16 == 0 and b.shape[1] % 16 == 0) assert (out_dtype is torch.bfloat16 or out_dtype is torch.float16) assert bias is None or bias.shape[0] == b.shape[ 1] and bias.dtype == out_dtype
m = a.shape[0] n = b.shape[1]
#if current_platform.is_rocm(): # triton_scaled_mm_module = importlib.import_module( # "vllm.model_executor.layers.quantization.compressed_tensors." # "triton_scaled_mm") # triton_scaled_mm = triton_scaled_mm_module.triton_scaled_mm # return triton_scaled_mm(a, b, scale_a, scale_b, out_dtype, bias)
out = torch.empty((m, n), dtype=out_dtype, device=a.device)
ops.cutlass_scaled_mm(out, a, b, scale_a, scale_b, bias)
return out
def cutlass_scaled_mm_azp(a: torch.Tensor, b: torch.Tensor, scale_a: torch.Tensor, scale_b: torch.Tensor, out_dtype: torch.dtype, azp_adj: torch.Tensor, azp: Optional[torch.Tensor] = None, bias: Optional[torch.Tensor] = None) -> torch.Tensor: """ :param azp_adj: In the per-tensor case, this should include the azp. Always per-channel. :param azp: Only set in the per-token case. Per-token if set. """ assert (b.shape[0] % 16 == 0 and b.shape[1] % 16 == 0) assert (out_dtype is torch.bfloat16 or out_dtype is torch.float16) assert bias is None or bias.numel( ) == b.shape[1] and bias.dtype == out_dtype assert azp is None or azp.numel() == a.shape[0]
m = a.shape[0] n = b.shape[1] out = torch.empty((m, n), dtype=out_dtype, device=a.device)
ops.cutlass_scaled_mm_azp(out, a, b, scale_a, scale_b, azp_adj, azp, bias) return out
# fp8def scaled_fp8_quant( input: torch.Tensor, scale: Optional[torch.Tensor] = None, num_token_padding: Optional[int] = None, scale_ub: Optional[torch.Tensor] = None, use_per_token_if_dynamic: bool = False,) -> Tuple[torch.Tensor, torch.Tensor]: """ Quantize input tensor to FP8 and return quantized tensor and scale.
This function supports both static and dynamic quantization: If you provide the scale, it will use static scaling and if you omit it, the scale will be determined dynamically. The function also allows optional padding of the output tensors for downstream kernels that will benefit from padding.
Args: input: The input tensor to be quantized to FP8 scale: Optional scaling factor for the FP8 quantization scale_ub: Optional upper bound for scaling factor in dynamic per token case num_token_padding: If specified, pad the first dimension of the output to at least this value. use_per_token_if_dynamic: Whether to do per_tensor or per_token in the dynamic quantization case.
Returns: Tuple[torch.Tensor, torch.Tensor]: The output tensor in FP8 and scaling factor. """ # This code assumes batch_dim and num_tokens are flattened assert (input.ndim == 2) shape: Union[Tuple[int, int], torch.Size] = input.shape # For rocm, the output fp8 dtype is torch.float_e3m3fnuz #out_dtype: torch.dtype = torch.float8_e4m3fnuz \ # if current_platform.is_rocm() else torch.float8_e4m3fn out_dtype = torch.float8_e4m3fn if num_token_padding: shape = (max(num_token_padding, input.shape[0]), shape[1]) output = torch.empty(shape, device=input.device, dtype=out_dtype)
if scale is None: if use_per_token_if_dynamic: scale = torch.empty((shape[0], 1), device=input.device, dtype=torch.float32) ops.dynamic_per_token_scaled_fp8_quant( output, input, scale, scale_ub) else: scale = torch.zeros(1, device=input.device, dtype=torch.float32) ops.dynamic_scaled_fp8_quant(output, input, scale) else: # num_token_padding not implemented for this case assert (scale.numel() == 1 or num_token_padding is None) ops.static_scaled_fp8_quant(output, input, scale)
return output, scale
# int8def scaled_int8_quant( input: torch.Tensor, scale: Optional[torch.Tensor] = None, azp: Optional[torch.Tensor] = None, symmetric: bool = True) -> Tuple[torch.Tensor, torch.Tensor, Optional[torch.Tensor]]: """ Quantize the input tensor to int8 and return the quantized tensor and scale, and maybe azp.
Args: input: The input tensor to be quantized to int8. scale: Optional scaling factor for the int8 quantization. When not provided, we invoke dynamic-per-token quantization. azp: Optional zero-point for the int8 quantization. Must be provided for asymmetric quantization if `scale` is provided. symmetric: Whether to use symmetric quantization (scale only, azp ignored).
Returns: Tuple[torch.Tensor, torch.Tensor, Optional[torch.Tensor]] : Output int8 tensor, scales, and optionally azp. """ output = torch.empty_like(input, dtype=torch.int8) if scale is not None: # static-per-tensor quantization. assert symmetric == ( azp is None), "azp must only be provided for asymmetric quantization." ops.static_scaled_int8_quant(output, input, scale, azp) return output, scale, azp
# dynamic-per-token quantization. input_scales = torch.empty((input.numel() // input.shape[-1], 1), device=input.device, dtype=torch.float32) input_azp = None if symmetric else torch.empty_like(input_scales, dtype=torch.int32) ops.dynamic_scaled_int8_quant(output, input, input_scales, input_azp) return output, input_scales, input_azp
# fp8 marlindef fp8_marlin_gemm(a: torch.Tensor, b_q_weight: torch.Tensor, b_scales: torch.Tensor, workspace: torch.Tensor, num_bits: int, size_m: int, size_n: int, size_k: int) -> torch.Tensor: return ops.fp8_marlin_gemm(a, b_q_weight, b_scales, workspace, num_bits, size_m, size_n, size_k)from .compressed_tensors import scaled_fp8_quant, scaled_int8_quantfrom .cutlass import ( cutlass_scaled_mm_supports_fp8, cutlass_scaled_mm, cutlass_scaled_mm_azp,)from .marlin import ( awq_marlin_repack, fp8_marlin_gemm, gptq_marlin_gemm, gptq_marlin_repack, gptq_marlin_24_gemm, marlin_qqq_gemm, marlin_gemm,)
__all__ = [ "awq_marlin_repack", "cutlass_scaled_mm", "cutlass_scaled_mm_azp", "cutlass_scaled_mm_supports_fp8", "fp8_marlin_gemm", "gptq_marlin_24_gemm", "gptq_marlin_gemm", "gptq_marlin_repack", "marlin_gemm", "marlin_qqq_gemm", "scaled_fp8_quant", "scaled_int8_quant",]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110from typing import Optional, Tuple
import torch
try: from ._ops import opsexcept ImportError as e: # Fallback for local development. try: import _quantization
ops = torch.ops._quantization except ImportError: raise e
# fp8def scaled_fp8_quant( input: torch.Tensor, scale: Optional[torch.Tensor] = None, num_token_padding: Optional[int] = None, scale_ub: Optional[torch.Tensor] = None, use_per_token_if_dynamic: bool = False,) -> Tuple[torch.Tensor, torch.Tensor]: """ Quantize input tensor to FP8 and return quantized tensor and scale.
This function supports both static and dynamic quantization: If you provide the scale, it will use static scaling and if you omit it, the scale will be determined dynamically. The function also allows optional padding of the output tensors for downstream kernels that will benefit from padding.
Args: input: The input tensor to be quantized to FP8 scale: Optional scaling factor for the FP8 quantization scale_ub: Optional upper bound for scaling factor in dynamic per token case num_token_padding: If specified, pad the first dimension of the output to at least this value. use_per_token_if_dynamic: Whether to do per_tensor or per_token in the dynamic quantization case.
Returns: Tuple[torch.Tensor, torch.Tensor]: The output tensor in FP8 and scaling factor. """ # This code assumes batch_dim and num_tokens are flattened assert input.ndim == 2 shape: Union[Tuple[int, int], torch.Size] = input.shape # For rocm, the output fp8 dtype is torch.float_e3m3fnuz # out_dtype: torch.dtype = torch.float8_e4m3fnuz \ # if current_platform.is_rocm() else torch.float8_e4m3fn out_dtype = torch.float8_e4m3fn if num_token_padding: shape = (max(num_token_padding, input.shape[0]), shape[1]) output = torch.empty(shape, device=input.device, dtype=out_dtype)
if scale is None: if use_per_token_if_dynamic: scale = torch.empty((shape[0], 1), device=input.device, dtype=torch.float32) ops.dynamic_per_token_scaled_fp8_quant(output, input, scale, scale_ub) else: scale = torch.zeros(1, device=input.device, dtype=torch.float32) ops.dynamic_scaled_fp8_quant(output, input, scale) else: # num_token_padding not implemented for this case assert scale.numel() == 1 or num_token_padding is None ops.static_scaled_fp8_quant(output, input, scale)
return output, scale
# int8def scaled_int8_quant( input: torch.Tensor, scale: Optional[torch.Tensor] = None, azp: Optional[torch.Tensor] = None, symmetric: bool = True,) -> Tuple[torch.Tensor, torch.Tensor, Optional[torch.Tensor]]: """ Quantize the input tensor to int8 and return the quantized tensor and scale, and maybe azp.
Args: input: The input tensor to be quantized to int8. scale: Optional scaling factor for the int8 quantization. When not provided, we invoke dynamic-per-token quantization. azp: Optional zero-point for the int8 quantization. Must be provided for asymmetric quantization if `scale` is provided. symmetric: Whether to use symmetric quantization (scale only, azp ignored).
Returns: Tuple[torch.Tensor, torch.Tensor, Optional[torch.Tensor]] : Output int8 tensor, scales, and optionally azp. """ output = torch.empty_like(input, dtype=torch.int8) if scale is not None: # static-per-tensor quantization. assert symmetric == ( azp is None ), "azp must only be provided for asymmetric quantization." ops.static_scaled_int8_quant(output, input, scale, azp) return output, scale, azp
# dynamic-per-token quantization. input_scales = torch.empty( (input.numel() // input.shape[-1], 1), device=input.device, dtype=torch.float32 ) input_azp = None if symmetric else torch.empty_like(input_scales, dtype=torch.int32) ops.dynamic_scaled_int8_quant(output, input, input_scales, input_azp) return output, input_scales, input_azp123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475from typing import Optional
import torch
try: from ._ops import opsexcept ImportError as e: # Fallback for local development. try: import _quantization
ops = torch.ops._quantization except ImportError: raise e
def cutlass_scaled_mm_supports_fp8(cuda_device_capability: int) -> bool: return ops.cutlass_scaled_mm_supports_fp8(cuda_device_capability)
def cutlass_scaled_mm( a: torch.Tensor, b: torch.Tensor, scale_a: torch.Tensor, scale_b: torch.Tensor, out_dtype: torch.dtype, bias: Optional[torch.Tensor] = None,) -> torch.Tensor: assert b.shape[0] % 16 == 0 and b.shape[1] % 16 == 0 assert out_dtype is torch.bfloat16 or out_dtype is torch.float16 assert bias is None or bias.shape[0] == b.shape[1] and bias.dtype == out_dtype
m = a.shape[0] n = b.shape[1]
# if current_platform.is_rocm(): # triton_scaled_mm_module = importlib.import_module( # "vllm.model_executor.layers.quantization.compressed_tensors." # "triton_scaled_mm") # triton_scaled_mm = triton_scaled_mm_module.triton_scaled_mm # return triton_scaled_mm(a, b, scale_a, scale_b, out_dtype, bias)
out = torch.empty((m, n), dtype=out_dtype, device=a.device)
ops.cutlass_scaled_mm(out, a, b, scale_a, scale_b, bias)
return out
def cutlass_scaled_mm_azp( a: torch.Tensor, b: torch.Tensor, scale_a: torch.Tensor, scale_b: torch.Tensor, out_dtype: torch.dtype, azp_adj: torch.Tensor, azp: Optional[torch.Tensor] = None, bias: Optional[torch.Tensor] = None,) -> torch.Tensor: """ :param azp_adj: In the per-tensor case, this should include the azp. Always per-channel. :param azp: Only set in the per-token case. Per-token if set. """ assert b.shape[0] % 16 == 0 and b.shape[1] % 16 == 0 assert out_dtype is torch.bfloat16 or out_dtype is torch.float16 assert bias is None or bias.numel() == b.shape[1] and bias.dtype == out_dtype assert azp is None or azp.numel() == a.shape[0]
m = a.shape[0] n = b.shape[1] out = torch.empty((m, n), dtype=out_dtype, device=a.device)
ops.cutlass_scaled_mm_azp(out, a, b, scale_a, scale_b, azp_adj, azp, bias) return out123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208from typing import TYPE_CHECKING
import torch
# neuron has torch version that doesn't even have impl_abstractif TYPE_CHECKING: def register_fake(fn): return lambda name: fnelse: try: from torch.library import register_fake except ImportError: from torch.library import impl_abstract as register_fake
try: from ._ops import ops, add_op_namespace_prefixexcept ImportError as e: # Fallback for local development. try: import _quantization
ops = torch.ops._quantization
def add_op_namespace_prefix(op_name: str): return f"_quantization::{op_name}" except ImportError: raise e
from .scalar_type import ScalarType
# fp8 marlindef fp8_marlin_gemm( a: torch.Tensor, b_q_weight: torch.Tensor, b_scales: torch.Tensor, workspace: torch.Tensor, num_bits: int, size_m: int, size_n: int, size_k: int,) -> torch.Tensor: return ops.fp8_marlin_gemm( a, b_q_weight, b_scales, workspace, num_bits, size_m, size_n, size_k )
# gptq_marlindef gptq_marlin_gemm( a: torch.Tensor, b_q_weight: torch.Tensor, b_scales: torch.Tensor, b_zeros: torch.Tensor, g_idx: torch.Tensor, perm: torch.Tensor, workspace: torch.Tensor, b_q_type: ScalarType, size_m: int, size_n: int, size_k: int, is_k_full: bool, has_zp: bool = False, use_fp32_reduce: bool = False, is_zp_float: bool = False,) -> torch.Tensor: return ops.gptq_marlin_gemm( a, b_q_weight, b_scales, b_zeros, g_idx, perm, workspace, b_q_type.id, size_m, size_n, size_k, is_k_full, has_zp, use_fp32_reduce, is_zp_float, )
# gptq_marlindef gptq_marlin_repack( b_q_weight: torch.Tensor, perm: torch.Tensor, size_k: int, size_n: int, num_bits: int,) -> torch.Tensor: return ops.gptq_marlin_repack(b_q_weight, perm, size_k, size_n, num_bits)
# gptq_marlindef awq_marlin_repack( b_q_weight: torch.Tensor, size_k: int, size_n: int, num_bits: int) -> torch.Tensor: return ops.awq_marlin_repack(b_q_weight, size_k, size_n, num_bits)
# marlindef marlin_gemm( a: torch.Tensor, b_q_weight: torch.Tensor, b_scales: torch.Tensor, workspace: torch.Tensor, size_m: int, size_n: int, size_k: int,) -> torch.Tensor: return ops.marlin_gemm( a, b_q_weight, b_scales, workspace, size_m, size_n, size_k )
# marlin_24def gptq_marlin_24_gemm( a: torch.Tensor, b_q_weight: torch.Tensor, b_meta: torch.Tensor, b_scales: torch.Tensor, workspace: torch.Tensor, b_q_type: ScalarType, size_m: int, size_n: int, size_k: int,) -> torch.Tensor: return ops.gptq_marlin_24_gemm( a, b_q_weight, b_meta, b_scales, workspace, b_q_type.id, size_m, size_n, size_k )
# qqq opsdef marlin_qqq_gemm( a: torch.Tensor, b_q_weight: torch.Tensor, s_tok: torch.Tensor, s_ch: torch.Tensor, s_group: torch.Tensor, workspace: torch.Tensor, size_m: int, size_n: int, size_k: int,) -> torch.Tensor: return ops.marlin_qqq_gemm( a, b_q_weight, s_tok, s_ch, s_group, workspace, size_m, size_n, size_k )
# Fake ops
if hasattr(ops, "gptq_marlin_24_gemm"): @register_fake(add_op_namespace_prefix("fp8_marlin_gemm")) def _fp8_marlin_gemm_fake(a: torch.Tensor, b_q_weight: torch.Tensor, b_scales: torch.Tensor, workspace: torch.Tensor, num_bits: int, size_m: torch.SymInt, size_n: torch.SymInt, size_k: torch.SymInt) -> torch.Tensor: return torch.empty((size_m, size_n), dtype=a.dtype, device=a.device)
@register_fake(add_op_namespace_prefix("gptq_marlin_24_gemm")) def _gptq_marlin_24_gemm_fake(a: torch.Tensor, b_q_weight: torch.Tensor, b_meta: torch.Tensor, b_scales: torch.Tensor, workspace: torch.Tensor, b_q_type: ScalarType, size_m: torch.SymInt, size_n: torch.SymInt, size_k: torch.SymInt) -> torch.Tensor: return torch.empty((size_m, size_n), device=a.device, dtype=a.dtype)
@register_fake(add_op_namespace_prefix("gptq_marlin_gemm")) def _gptq_marlin_gemm_fake(a: torch.Tensor, b_q_weight: torch.Tensor, b_scales: torch.Tensor, b_zeros: torch.Tensor, g_idx: torch.Tensor, perm: torch.Tensor, workspace: torch.Tensor, b_q_type: ScalarType, size_m: torch.SymInt, size_n: torch.SymInt, size_k: torch.SymInt, is_k_full: bool, has_zp: bool = False, use_fp32_reduce: bool = False, is_zp_float: bool = False) -> torch.Tensor: return torch.empty((size_m, size_n), device=a.device, dtype=a.dtype)
@register_fake(add_op_namespace_prefix("marlin_qqq_gemm")) def _marlin_qqq_gemm_fake(a: torch.Tensor, b_q_weight: torch.Tensor, s_tok: torch.Tensor, s_ch: torch.Tensor, s_group: torch.Tensor, workspace: torch.Tensor, size_m: torch.SymInt, size_n: torch.SymInt, size_k: torch.SymInt) -> torch.Tensor: return torch.empty((size_m, size_n), dtype=torch.float16, device=a.device)
@register_fake(add_op_namespace_prefix("marlin_gemm")) def _marlin_gemm_fake(a: torch.Tensor, b_q_weight: torch.Tensor, b_scales: torch.Tensor, workspace: torch.Tensor, size_m: torch.SymInt, size_n: torch.SymInt, size_k: torch.SymInt) -> torch.Tensor: return torch.empty((size_m, size_n), dtype=torch.float16, device=a.device)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330import functoolsimport structfrom dataclasses import dataclassfrom enum import Enumfrom typing import Optional, Union
# Mirrors enum in `core/scalar_type.hpp`class NanRepr(Enum): NONE = 0 # nans are not supported IEEE_754 = 1 # nans are: Exp all 1s, mantissa not all 0s EXTD_RANGE_MAX_MIN = 2 # nans are: Exp all 1s, mantissa all 1s
# This ScalarType class is a parallel implementation of the C++ ScalarType# class found in csrc/core/scalar_type.hpp. These two classes should be kept# in sync until the inductor fully supports custom C++ classes.@dataclass(frozen=True)class ScalarType: """ ScalarType can represent a wide range of floating point and integer types, in particular it can be used to represent sub-byte data types (something that torch.dtype currently does not support). It is also capable of representing types with a bias, i.e.: `stored_value = value + bias`, this is useful for quantized types (e.g. standard GPTQ 4bit uses a bias of 8). The implementation for this class can be found in csrc/core/scalar_type.hpp, these type signatures should be kept in sync with that file. """
exponent: int """ Number of bits in the exponent if this is a floating point type (zero if this an integer type) """
mantissa: int """ Number of bits in the mantissa if this is a floating point type, or the number bits representing an integer excluding the sign bit if this an integer type. """
signed: bool "If the type is signed (i.e. has a sign bit)"
bias: int """ bias used to encode the values in this scalar type (value = stored_value - bias, default 0) for example if we store the type as an unsigned integer with a bias of 128 then the value 0 will be stored as 128 and -1 will be stored as 127 and 1 will be stored as 129. """
_finite_values_only: bool = False """ Private: if infs are supported, used `has_infs()` instead. """
nan_repr: NanRepr = NanRepr.IEEE_754 """ How NaNs are represent in this scalar type, returns NanRepr value. (not applicable for integer types) """
def _floating_point_max_int(self) -> int: assert ( self.mantissa <= 52 and self.exponent <= 11 ), f"Cannot represent max/min as a double for type {self.__str__()}"
max_mantissa = (1 << self.mantissa) - 1 if self.nan_repr == NanRepr.EXTD_RANGE_MAX_MIN: max_mantissa = max_mantissa - 1
max_exponent = (1 << self.exponent) - 2 if (self.nan_repr == NanRepr.EXTD_RANGE_MAX_MIN or self.nan_repr == NanRepr.NONE): assert ( self.exponent < 11 ), f"Cannot represent max/min as a double for type {self.__str__()}" max_exponent = max_exponent + 1
# adjust the exponent to match that of a double # for now we assume the exponent bias is the standard 2^(e-1) -1, (where # e is the exponent bits), there is some precedent for non-standard # biases, example `float8_e4m3b11fnuz` here: # https://github.com/jax-ml/ml_dtypes but to avoid premature over # complication we are just assuming the standard exponent bias until # there is a need to support non-standard biases exponent_bias = (1 << (self.exponent - 1)) - 1 exponent_bias_double = (1 << 10) - 1 # double e = 11
max_exponent_double = (max_exponent - exponent_bias + exponent_bias_double)
# shift the mantissa and exponent into the proper positions for an # IEEE double and bitwise-or them together. return (max_mantissa << (52 - self.mantissa)) | (max_exponent_double << 52)
def _floating_point_max(self) -> float: double_raw = self._floating_point_max_int() return struct.unpack('!d', struct.pack('!Q', double_raw))[0]
def _raw_max(self) -> Union[int, float]: if self.is_floating_point(): return self._floating_point_max() else: assert (self.size_bits < 64 or self.size_bits == 64 and self.is_signed()), "Cannot represent max as an int" return (1 << self.mantissa) - 1
def _raw_min(self) -> Union[int, float]: if self.is_floating_point(): assert self.is_signed( ), "We currently assume all floating point types are signed" sign_bit_double = 1 << 63
max_raw = self._floating_point_max_int() min_raw = max_raw | sign_bit_double return struct.unpack('!d', struct.pack('!Q', min_raw))[0] else: assert (not self.is_signed() or self.size_bits <= 64), "Cannot represent min as a int64_t"
if self.is_signed(): return -(1 << (self.size_bits - 1)) else: return 0
@functools.cached_property def id(self) -> int: """ Convert the ScalarType to an int which can be passed to pytorch custom ops. This layout of the int must be kept in sync with the C++ ScalarType's from_id method. """ val = 0 offset = 0
def or_and_advance(member, bit_width): nonlocal val nonlocal offset bit_mask = (1 << bit_width) - 1 val = val | (int(member) & bit_mask) << offset offset = offset + bit_width
or_and_advance(self.exponent, 8) or_and_advance(self.mantissa, 8) or_and_advance(self.signed, 1) or_and_advance(self.bias, 32) or_and_advance(self._finite_values_only, 1) or_and_advance(self.nan_repr.value, 8)
assert offset <= 64, \ f"ScalarType fields too big {offset} to fit into an int64"
return val
@property def size_bits(self) -> int: return self.exponent + self.mantissa + int(self.signed)
def min(self) -> Union[int, float]: """ Min representable value for this scalar type. (accounting for bias if there is one) """ return self._raw_min() - self.bias
def max(self) -> Union[int, float]: """ Max representable value for this scalar type. (accounting for bias if there is one) """ return self._raw_max() - self.bias
def is_signed(self) -> bool: """ If the type is signed (i.e. has a sign bit), same as `signed` added for consistency with: https://pytorch.org/docs/stable/generated/torch.Tensor.is_signed.html """ return self.signed
def is_floating_point(self) -> bool: "If the type is a floating point type" return self.exponent != 0
def is_integer(self) -> bool: "If the type is an integer type" return self.exponent == 0
def has_bias(self) -> bool: "If the type has a non-zero bias" return self.bias != 0
def has_infs(self) -> bool: "If the type is floating point and supports infinity" return not self._finite_values_only
def has_nans(self) -> bool: return self.nan_repr != NanRepr.NONE.value
def is_ieee_754(self) -> bool: """ If the type is a floating point type that follows IEEE 754 conventions """ return self.nan_repr == NanRepr.IEEE_754.value and \ not self._finite_values_only
def __str__(self) -> str: """ naming generally follows: https://github.com/jax-ml/ml_dtypes for floating point types (leading f) the scheme is: `float<size_bits>_e<exponent_bits>m<mantissa_bits>[flags]` flags: - no-flags: means it follows IEEE 754 conventions - f: means finite values only (no infinities) - n: means nans are supported (non-standard encoding) for integer types the scheme is: `[u]int<size_bits>[b<bias>]` - if bias is not present it means its zero """ if self.is_floating_point(): ret = "float" + str(self.size_bits) + "_e" + str( self.exponent) + "m" + str(self.mantissa)
if not self.is_ieee_754(): if self._finite_values_only: ret = ret + "f" if self.nan_repr != NanRepr.NONE: ret = ret + "n"
return ret else: ret = ("int" if self.is_signed() else "uint") + str(self.size_bits) if self.has_bias(): ret = ret + "b" + str(self.bias) return ret
def __repr__(self) -> str: return "ScalarType." + self.__str__()
# __len__ needs to be defined (and has to throw TypeError) for pytorch's # opcheck to work. def __len__(self) -> int: raise TypeError
# # Convenience Constructors #
@classmethod def int_(cls, size_bits: int, bias: Optional[int]) -> 'ScalarType': "Create a signed integer scalar type (size_bits includes sign-bit)." ret = cls(0, size_bits - 1, True, bias if bias else 0) ret.id # noqa B018: make sure the id is cached return ret
@classmethod def uint(cls, size_bits: int, bias: Optional[int]) -> 'ScalarType': """Create a unsigned integer scalar type.""" ret = cls(0, size_bits, False, bias if bias else 0) ret.id # noqa B018: make sure the id is cached return ret
@classmethod def float_IEEE754(cls, exponent: int, mantissa: int) -> 'ScalarType': """ Create a standard floating point type (i.e. follows IEEE 754 conventions). """ assert (mantissa > 0 and exponent > 0) ret = cls(exponent, mantissa, True, 0) ret.id # noqa B018: make sure the id is cached return ret
@classmethod def float_(cls, exponent: int, mantissa: int, finite_values_only: bool, nan_repr: NanRepr) -> 'ScalarType': """ Create a non-standard floating point type (i.e. does not follow IEEE 754 conventions). """ assert (mantissa > 0 and exponent > 0) assert (nan_repr != NanRepr.IEEE_754), ( "use `float_IEEE754` constructor for floating point types that " "follow IEEE 754 conventions") ret = cls(exponent, mantissa, True, 0, finite_values_only, nan_repr) ret.id # noqa B018: make sure the id is cached return ret
# naming generally follows: https://github.com/jax-ml/ml_dtypes# for floating point types (leading f) the scheme is:# `float<size_bits>_e<exponent_bits>m<mantissa_bits>[flags]`# flags:# - no-flags: means it follows IEEE 754 conventions# - f: means finite values only (no infinities)# - n: means nans are supported (non-standard encoding)# for integer types the scheme is:# `[u]int<size_bits>[b<bias>]`# - if bias is not present it means its zero
class scalar_types: int4 = ScalarType.int_(4, None) uint4 = ScalarType.uint(4, None) int8 = ScalarType.int_(8, None) uint8 = ScalarType.uint(8, None) float8_e4m3fn = ScalarType.float_(4, 3, True, NanRepr.EXTD_RANGE_MAX_MIN) float8_e5m2 = ScalarType.float_IEEE754(5, 2) float16_e8m7 = ScalarType.float_IEEE754(8, 7) float16_e5m10 = ScalarType.float_IEEE754(5, 10)
# fp6, https://github.com/usyd-fsalab/fp6_llm/tree/main float6_e3m2f = ScalarType.float_(3, 2, True, NanRepr.NONE)
# "gptq" types uint2b2 = ScalarType.uint(2, 2) uint3b4 = ScalarType.uint(3, 4) uint4b8 = ScalarType.uint(4, 8) uint8b128 = ScalarType.uint(8, 128)
# colloquial names bfloat16 = float16_e8m7 float16 = float16_e5m1064 unmodified lines656667686869707172747374777576775 unmodified lines838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711864 unmodified lines "fp8_marlin_gemm(Tensor a, Tensor b_q_weight, Tensor b_scales, " "Tensor! workspace, int num_bits, SymInt size_m, SymInt size_n, " "SymInt size_k) -> Tensor"); ops.impl("fp8_marlin_gemm", &fp8_marlin_gemm);
// awq_marlin repack from AWQ. ops.def( "awq_marlin_repack(Tensor b_q_weight, SymInt size_k, " "SymInt size_n, int num_bits) -> Tensor"); ops.impl("awq_marlin_repack", &awq_marlin_repack);
// gptq_marlin Optimized Quantized GEMM for GPTQ. ops.impl("gptq_marlin_gemm", &gptq_marlin_gemm); ops.def( "gptq_marlin_gemm(Tensor a, Tensor b_q_weight, Tensor b_scales, " "Tensor b_zeros, Tensor g_idx, Tensor perm, Tensor workspace, "5 unmodified lines ops.def( "gptq_marlin_repack(Tensor b_q_weight, Tensor perm, " "SymInt size_k, SymInt size_n, int num_bits) -> Tensor");
// Marlin (Dense) Optimized Quantized GEMM for GPTQ. ops.def( "marlin_gemm(Tensor a, Tensor b_q_weight, Tensor b_scales, " "Tensor! workspace, SymInt size_m, SymInt size_n, SymInt size_k) -> " "Tensor");
// Marlin_24 (Sparse) Optimized Quantized GEMM for GPTQ. ops.def( "gptq_marlin_24_gemm(Tensor a, Tensor b_q_weight, Tensor b_meta, " "Tensor b_scales, Tensor workspace, " "int b_q_type, " "SymInt size_m, SymInt size_n, SymInt size_k) -> Tensor");
// marlin_qqq_gemm for QQQ. ops.def( "marlin_qqq_gemm(Tensor a, Tensor b_q_weight, " "Tensor s_tok, Tensor s_ch, Tensor s_group, " "Tensor! workspace, SymInt size_m, SymInt size_n, " "SymInt size_k) -> Tensor");}
TORCH_LIBRARY_IMPL_EXPAND(TORCH_EXTENSION_NAME, CUDA, ops) { ops.impl("awq_marlin_repack", &awq_marlin_repack); ops.impl("fp8_marlin_gemm", &fp8_marlin_gemm); ops.impl("gptq_marlin_24_gemm", &gptq_marlin_24_gemm); ops.impl("gptq_marlin_gemm", &gptq_marlin_gemm); ops.impl("gptq_marlin_repack", &gptq_marlin_repack); ops.impl("marlin_gemm", &marlin_gemm); ops.impl("marlin_qqq_gemm", &marlin_qqq_gemm);}
TORCH_LIBRARY_IMPL_EXPAND(TORCH_EXTENSION_NAME, Meta, ops) {73 unmodified lines747576777879808182838485868788899091929394959697989973 unmodified linestorch::Tensor gptq_marlin_repack_meta(torch::Tensor& b_q_weight, torch::Tensor& perm, c10::SymInt size_k, c10::SymInt size_n, int64_t num_bits);
// Marlin
torch::Tensor marlin_gemm(torch::Tensor& a, torch::Tensor& b_q_weight, torch::Tensor& b_scales, torch::Tensor& workspace, int64_t size_m, int64_t size_n, int64_t size_k);
torch::Tensor gptq_marlin_24_gemm(torch::Tensor& a, torch::Tensor& b_q_weight, torch::Tensor& b_meta, torch::Tensor& b_scales, torch::Tensor& workspace, vllm::ScalarTypeId const b_q_type_id, int64_t size_m, int64_t size_n, int64_t size_k);
torch::Tensor marlin_qqq_gemm(torch::Tensor const& a, torch::Tensor const& b_q_weight, torch::Tensor const& s_tok, torch::Tensor const& s_ch, torch::Tensor const& s_group, torch::Tensor& workspace, int64_t size_m, int64_t size_n, int64_t size_k);123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391from typing import List, Optional, Tuple
import numpyimport torch
import quantization as opsfrom quantization.scalar_type import ScalarType, scalar_types
from .quant_utils import pack_cols, unpack_cols
GPTQ_MARLIN_TILE = 16GPTQ_MARLIN_MIN_THREAD_N = 64GPTQ_MARLIN_MIN_THREAD_K = 128GPTQ_MARLIN_MAX_PARALLEL = 16
GPTQ_MARLIN_24_TILE = 16GPTQ_MARLIN_24_MIN_THREAD_N = 128GPTQ_MARLIN_24_MIN_THREAD_K = 128GPTQ_MARLIN_24_MAX_PARALLEL = 64
GPTQ_MARLIN_24_SUPPORTED_QUANT_TYPES = [scalar_types.uint4b8, scalar_types.uint8b128]GPTQ_MARLIN_24_SUPPORTED_GROUP_SIZES = [-1, 128]
MARLIN_QQQ_TILE = 16MARLIN_QQQ_MIN_THREAD_N = 64MARLIN_QQQ_MIN_THREAD_K = 128MARLIN_QQQ_MAX_PARALLEL = 16
MARLIN_QQQ_SUPPORTED_NUM_BITS = [4]MARLIN_QQQ_SUPPORTED_GROUP_SIZES = [-1, 128]MARLIN_QQQ_SUPPORTED_SYM = [True]
MARLIN_SUPPORTED_GROUP_SIZES = [-1, 32, 64, 128]
# In case there is a performance issue with Marlin, the variable below can be# changed to False, which allows Marlin to perform global reductions in fp16# precision (instead of fp32), and therefore, save on some memory movements.USE_FP32_REDUCE_DEFAULT = True
# For binary size and compile time, we don't support the same types for with and# without runtime zero-point. We support common cases, i.e. AWQ and GPTQ.# TODO: we may want to move this into the C++ so its closer to the actual impldef query_marlin_supported_quant_types( has_zp: bool, device_capability: Optional[int] = None): if device_capability is None: capability_tuple = torch.cuda.get_device_capability() device_capability = capability_tuple[0] * 10 + capability_tuple[1]
if device_capability < 80: return []
if has_zp: # AWQ style, unsigned + runtime zero-point return [scalar_types.uint4, scalar_types.uint8] else: # GPTQ style, unsigned + symmetric bias # TODO: once fp8_marlin is merged into "gptq_marlin" we should be able # to add `scalar_types.float8_e4m3fn` here return [scalar_types.uint4b8, scalar_types.uint8b128]
def _check_marlin_supported( quant_type: ScalarType, group_size: Optional[int], has_zp: bool, device_capability: Optional[int] = None,) -> Tuple[bool, Optional[str]]:
if device_capability is None: capability_tuple = torch.cuda.get_device_capability() device_capability = capability_tuple[0] * 10 + capability_tuple[1]
supported_types = query_marlin_supported_quant_types(has_zp, device_capability)
if quant_type not in supported_types: return ( False, f"Marlin does not support weight_bits = {quant_type}. " f"Only types = {supported_types} " f"are supported (for group_size = {group_size}, " f"device_capability = {device_capability}, zp = {has_zp}).", ) if group_size is None or group_size not in MARLIN_SUPPORTED_GROUP_SIZES: return ( False, f"Marlin does not support group_size = {group_size}. " f"Only group_sizes = {MARLIN_SUPPORTED_GROUP_SIZES} " "are supported.", )
return True, None
def check_marlin_supported( quant_type: ScalarType, group_size: int, has_zp: bool = False, device_capability: Optional[int] = None,) -> bool: cond, _ = _check_marlin_supported(quant_type, group_size, has_zp, device_capability) return cond
def verify_marlin_supported( quant_type: ScalarType, group_size: int, has_zp: bool = False) -> None: cond, err_msg = _check_marlin_supported(quant_type, group_size, has_zp) if not cond: assert err_msg is not None raise ValueError(err_msg)
def verify_marlin_supports_shape( output_size_per_partition: int, input_size_per_partition: int, input_size: int, group_size: int,) -> None:
# Validate output_size_per_partition if output_size_per_partition % GPTQ_MARLIN_MIN_THREAD_N != 0: raise ValueError( f"Weight output_size_per_partition = " f"{output_size_per_partition} is not divisible by " f" min_thread_n = {GPTQ_MARLIN_MIN_THREAD_N}. " "Consider reducing tensor_parallel_size or running " "with --quantization gptq." )
# Validate input_size_per_partition if input_size_per_partition % GPTQ_MARLIN_MIN_THREAD_K != 0: raise ValueError( f"Weight input_size_per_partition = " f"{input_size_per_partition} is not divisible " f"by min_thread_k = {GPTQ_MARLIN_MIN_THREAD_K}. " "Consider reducing tensor_parallel_size or running " "with --quantization gptq." )
if group_size < input_size and input_size_per_partition % group_size != 0: raise ValueError( f"Weight input_size_per_partition = {input_size_per_partition}" f" is not divisible by group_size = {group_size}." "Consider reducing tensor_parallel_size or running " "with --quantization gptq." )
def check_marlin_supports_shape( output_size_per_partition: int, input_size_per_partition: int, input_size: int, group_size: int,) -> Tuple[bool, Optional[str]]: try: verify_marlin_supports_shape( output_size_per_partition, input_size_per_partition, input_size, group_size ) except ValueError as e: return False, e.__str__() return True, None
def marlin_make_workspace( output_size_per_partition: int, device: torch.device) -> torch.Tensor: max_workspace_size = ( output_size_per_partition // GPTQ_MARLIN_MIN_THREAD_N ) * GPTQ_MARLIN_MAX_PARALLEL
return torch.zeros( max_workspace_size, dtype=torch.int, device=device, requires_grad=False )
def marlin_is_k_full(act_order: bool, is_row_parallel: bool) -> bool: return (not act_order) or (act_order and not is_row_parallel)
def marlin_repeat_scales_on_all_ranks( act_order: bool, group_size: int, is_row_parallel: bool) -> bool: # Need to repeat scales on every rank if act_ordering or # channelwise and RowParallelLinear is_channelwise = group_size == -1 return act_order or (is_channelwise and is_row_parallel)
def marlin_make_empty_g_idx(device: torch.device) -> torch.Tensor: return torch.nn.Parameter( torch.empty(0, dtype=torch.int, device=device), requires_grad=False )
def marlin_make_empty_zp(device: torch.device) -> torch.Tensor: return torch.nn.Parameter( torch.empty(0, dtype=torch.int, device=device), requires_grad=False )
def marlin_sort_g_idx(g_idx: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: g_idx_sort_indices = torch.argsort(g_idx).to(torch.int) return g_idx[g_idx_sort_indices], g_idx_sort_indices
def get_scale_perms(): scale_perm: List[int] = [] for i in range(8): scale_perm.extend([i + 8 * j for j in range(8)]) scale_perm_single: List[int] = [] for i in range(4): scale_perm_single.extend([2 * i + j for j in [0, 1, 8, 9, 16, 17, 24, 25]]) return scale_perm, scale_perm_single
def marlin_permute_scales( s: torch.Tensor, size_k: int, size_n: int, group_size: int) -> torch.Tensor:
scale_perm, scale_perm_single = get_scale_perms() if group_size < size_k and group_size != -1: s = s.reshape((-1, len(scale_perm)))[:, scale_perm] else: s = s.reshape((-1, len(scale_perm_single)))[:, scale_perm_single] s = s.reshape((-1, size_n)).contiguous()
return s
def marlin_moe_permute_scales( s: torch.Tensor, size_k: int, size_n: int, group_size: int,): num_experts = s.shape[0] output = torch.empty( (num_experts, s.shape[1], s.shape[2]), device=s.device, dtype=s.dtype, )
for e in range(num_experts): output[e] = marlin_permute_scales(s[e], size_k, size_n, group_size) return output
def marlin_zero_points( zp: torch.Tensor, size_k: int, size_n: int, num_bits: int) -> torch.Tensor: # Permute zero-points in a similar way to scales, but do not use the # "single" permutation, since zero-points are applied on every MMA scale_perm, _ = get_scale_perms() zp = zp.reshape((-1, len(scale_perm)))[:, scale_perm]
# Interleave column dim (for the dequantize code) and pack it to int32 if num_bits == 4: interleave = numpy.array([0, 2, 4, 6, 1, 3, 5, 7]) elif num_bits == 8: interleave = numpy.array([0, 2, 1, 3]) else: raise Exception("num_bits must be 4 or 8, got {}".format(num_bits))
zp = zp.reshape((-1, len(interleave)))[:, interleave].ravel() zp = zp.reshape((-1, size_n)).contiguous() zp = pack_cols(zp, num_bits, size_k, size_n)
return zp
def awq_to_marlin_zero_points( q_zp_packed: torch.Tensor, size_k: int, size_n: int, num_bits: int) -> torch.Tensor: # AWQ zero-points are quantized and packed on the column dim. # In addition, the values are permuted based on dequantizer. # Here we undo both of these, and then apply marlin permutation # and pack it back. q_zp = unpack_cols(q_zp_packed, num_bits, size_k, size_n)
# Undo interleaving (use argsort(..) to get inverse perm) if num_bits == 4: undo_interleave = numpy.argsort(numpy.array([0, 2, 4, 6, 1, 3, 5, 7])) elif num_bits == 8: undo_interleave = numpy.argsort(numpy.array([0, 2, 1, 3])) else: raise Exception("num_bits must be 4 or 8, got {}".format(num_bits))
q_zp = q_zp.reshape((-1, len(undo_interleave)))[:, undo_interleave].ravel() q_zp = q_zp.reshape((-1, size_n)).contiguous()
marlin_zp = marlin_zero_points(q_zp, size_k, size_n, num_bits) return marlin_zp
def moe_awq_to_marlin_zero_points( q_zp_packed: torch.Tensor, size_k: int, size_n: int, num_bits: int): num_experts = q_zp_packed.shape[0] output = torch.empty( (num_experts, q_zp_packed.shape[1], q_zp_packed.shape[2]), device=q_zp_packed.device, dtype=q_zp_packed.dtype, ) for e in range(num_experts): output[e] = awq_to_marlin_zero_points(q_zp_packed[e], size_k, size_n, num_bits) return output
def apply_gptq_marlin_linear( input: torch.Tensor, weight: torch.Tensor, weight_scale: torch.Tensor, weight_zp: torch.Tensor, g_idx: torch.Tensor, g_idx_sort_indices: torch.Tensor, workspace: torch.Tensor, wtype: ScalarType, output_size_per_partition: int, input_size_per_partition: int, is_k_full: bool, bias: Optional[torch.Tensor] = None, use_fp32_reduce: bool = USE_FP32_REDUCE_DEFAULT,) -> torch.Tensor: reshaped_x = input.reshape(-1, input.shape[-1]) out_shape = input.shape[:-1] + (output_size_per_partition,)
output = ops.gptq_marlin_gemm( reshaped_x, weight, weight_scale, weight_zp, g_idx, g_idx_sort_indices, workspace, wtype, size_m=reshaped_x.shape[0], size_n=output_size_per_partition, size_k=input_size_per_partition, is_k_full=is_k_full, has_zp=False, use_fp32_reduce=use_fp32_reduce, is_zp_float=False, )
if bias is not None: output.add_(bias) # In-place add
return output.reshape(out_shape)
def apply_awq_marlin_linear( input: torch.Tensor, weight: torch.Tensor, weight_scale: torch.Tensor, weight_zp: torch.Tensor, g_idx: torch.Tensor, g_idx_sort_indices: torch.Tensor, workspace: torch.Tensor, quant_type: ScalarType, output_size_per_partition: int, input_size_per_partition: int, bias: Optional[torch.Tensor] = None, use_fp32_reduce: bool = USE_FP32_REDUCE_DEFAULT,) -> torch.Tensor: reshaped_x = input.reshape(-1, input.shape[-1]) out_shape = input.shape[:-1] + (output_size_per_partition,)
output = ops.gptq_marlin_gemm( reshaped_x, weight, weight_scale, weight_zp, g_idx, g_idx_sort_indices, workspace, quant_type, size_m=reshaped_x.shape[0], size_n=output_size_per_partition, size_k=input_size_per_partition, is_k_full=True, has_zp=True, use_fp32_reduce=use_fp32_reduce, is_zp_float=False, )
if bias is not None: output.add_(bias) # In-place add
return output.reshape(out_shape)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100from typing import Optional
import torch
import quantization as ops
from .marlin_utils import marlin_make_workspace, marlin_permute_scales
def is_fp8_marlin_supported(): capability = torch.cuda.get_device_capability() capability = capability[0] * 10 + capability[1] return capability >= 80
def apply_fp8_marlin_linear( input: torch.Tensor, weight: torch.Tensor, weight_scale: torch.Tensor, workspace: torch.Tensor, size_n: int, size_k: int, bias: Optional[torch.Tensor],) -> torch.Tensor: # For GPUs that lack FP8 hardware support, we can leverage the # Marlin kernel for fast weight-only FP8 quantization
reshaped_x = input.reshape(-1, input.shape[-1]) out_shape = input.shape[:-1] + (size_n,)
output = ops.fp8_marlin_gemm( a=reshaped_x, b_q_weight=weight, b_scales=weight_scale, workspace=workspace, num_bits=8, size_m=reshaped_x.shape[0], size_n=size_n, size_k=size_k, )
if bias is not None: output.add_(bias) # In-place add
return output.reshape(out_shape)
def prepare_fp8_layer_for_marlin( layer: torch.nn.Module, strategy: str = "tensor") -> None: part_size_n = layer.output_size_per_partition part_size_k = layer.input_size_per_partition
device = layer.weight.device
# WORKSPACE layer.workspace = marlin_make_workspace(part_size_n, device)
# WEIGHT # Repack weights to marlin format marlin_qweight = ops.gptq_marlin_repack( b_q_weight=pack_fp8_to_int32(layer.weight), perm=torch.empty(0, dtype=torch.int, device=device), size_k=part_size_k, size_n=part_size_n, num_bits=8, ) layer.weight = torch.nn.Parameter(marlin_qweight, requires_grad=False)
# WEIGHT SCALES scales = layer.weight_scale.to(layer.orig_dtype) # Permute scales marlin_scales = marlin_permute_scales( s=scales, size_k=part_size_k, size_n=part_size_n, group_size=-1 ) layer.weight_scale = torch.nn.Parameter(marlin_scales, requires_grad=False)
def pack_fp8_to_int32(fp8_tensor: torch.Tensor) -> torch.Tensor: """ Repack FP8 weights to gptq format (packed int32 elements) """ assert fp8_tensor.dtype == torch.float8_e4m3fn assert fp8_tensor.shape[0] % 4 == 0
# Reshape to prepare for packing reshaped = fp8_tensor.reshape(-1, 4, *fp8_tensor.shape[1:])
# Convert fp8 to uint8 (byte) representation byte_tensor = reshaped.view(torch.uint8)
# Pack 4 uint8 values into one int32 packed = ( byte_tensor[:, 0].to(torch.int32) | (byte_tensor[:, 1].to(torch.int32) << 8) | (byte_tensor[:, 2].to(torch.int32) << 16) | (byte_tensor[:, 3].to(torch.int32) << 24) )
return packed.view(fp8_tensor.shape[0] // 4, *fp8_tensor.shape[1:]).contiguous()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162"""Utility functions used for tests and benchmarks"""
from typing import List, Optional
import numpy as npimport torch
from quantization.scalar_type import ScalarType
from .marlin_utils import GPTQ_MARLIN_TILE, marlin_permute_scales, marlin_zero_pointsfrom .quant_utils import ( get_pack_factor, gptq_quantize_weights, quantize_weights, sort_weights,)
class MarlinWorkspace:
def __init__(self, out_features, min_thread_n, max_parallel): assert ( out_features % min_thread_n == 0 ), "out_features = {} is undivisible by min_thread_n = {}".format( out_features, min_thread_n )
max_workspace_size = (out_features // min_thread_n) * max_parallel
self.scratch = torch.zeros(max_workspace_size, dtype=torch.int, device="cuda")
def marlin_permute_weights(q_w, size_k, size_n, perm, tile=GPTQ_MARLIN_TILE): assert q_w.shape == (size_k, size_n) assert size_k % tile == 0, f"size_k = {size_k}, tile = {tile}" assert size_n % tile == 0, f"size_k = {size_n}, tile = {tile}"
# Permute weights to 16x64 marlin tiles q_w = q_w.reshape((size_k // tile, tile, size_n // tile, tile)) q_w = q_w.permute((0, 2, 1, 3)) q_w = q_w.reshape((size_k // tile, size_n * tile))
q_w = q_w.reshape((-1, perm.numel()))[:, perm].reshape(q_w.shape)
return q_w
def marlin_weights(q_w, size_k, size_n, num_bits, perm): # Permute q_w = marlin_permute_weights(q_w, size_k, size_n, perm)
# Pack pack_factor = get_pack_factor(num_bits) orig_device = q_w.device
q_w = q_w.cpu().numpy().astype(np.uint32)
q_packed = np.zeros((q_w.shape[0], q_w.shape[1] // pack_factor), dtype=np.uint32) for i in range(pack_factor): q_packed |= q_w[:, i::pack_factor] << num_bits * i
q_packed = torch.from_numpy(q_packed.astype(np.int32)).to(orig_device)
return q_packed
def get_weight_perm(num_bits: int): perm_list: List[int] = [] for i in range(32): perm1: List[int] = [] col = i // 4 for block in [0, 1]: for row in [ 2 * (i % 4), 2 * (i % 4) + 1, 2 * (i % 4 + 4), 2 * (i % 4 + 4) + 1, ]: perm1.append(16 * row + col + 8 * block) for j in range(4): perm_list.extend([p + 256 * j for p in perm1])
perm = np.array(perm_list)
if num_bits == 4: interleave = np.array([0, 2, 4, 6, 1, 3, 5, 7]) elif num_bits == 8: interleave = np.array([0, 2, 1, 3]) else: raise Exception("num_bits must be 4 or 8, got {}".format(num_bits))
perm = perm.reshape((-1, len(interleave)))[:, interleave].ravel() perm = torch.from_numpy(perm) return perm
def marlin_quantize( w: torch.Tensor, quant_type: ScalarType, group_size: int, act_order: bool, test_perm: Optional[torch.Tensor] = None,): size_k, size_n = w.shape num_bits = quant_type.size_bits
# Normalize group_size if group_size == -1: group_size = size_k assert group_size <= size_k
# Quantize (and apply act_order if provided) w_ref, q_w, s, g_idx, rand_perm = gptq_quantize_weights( w, quant_type, group_size, act_order, test_perm )
# For act_order, sort the "weights" and "g_idx" so that group ids are # increasing sort_indices = torch.empty(0, dtype=torch.int, device=w.device) if act_order: q_w, g_idx, sort_indices = sort_weights(q_w, g_idx)
# Reformat to marlin weight_perm = get_weight_perm(num_bits) marlin_q_w = marlin_weights(q_w, size_k, size_n, num_bits, weight_perm) marlin_s = marlin_permute_scales(s, size_k, size_n, group_size)
# Create result res_list = [w_ref, marlin_q_w, marlin_s, g_idx, sort_indices, rand_perm] for i in range(len(res_list)): res_list[i] = res_list[i].to(w.device)
return res_list
def awq_marlin_quantize(w: torch.Tensor, quant_type: ScalarType, group_size: int): size_k, size_n = w.shape
# Normalize group_size if group_size == -1: group_size = size_k assert group_size <= size_k
# Detect num groups assert size_k % group_size == 0 num_groups = size_k // group_size
# Quantize with zp w_ref, q_w, s, zp = quantize_weights(w, quant_type, group_size, zero_points=True)
# Reformat to marlin weight_perm = get_weight_perm(quant_type.size_bits) marlin_q_w = marlin_weights(q_w, size_k, size_n, quant_type.size_bits, weight_perm) marlin_s = marlin_permute_scales(s, size_k, size_n, group_size) marlin_zp = marlin_zero_points(zp, num_groups, size_n, quant_type.size_bits)
# Create result res_list = [w_ref, marlin_q_w, marlin_s, marlin_zp] for i in range(len(res_list)): res_list[i] = res_list[i].to(w.device)
return res_list123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473"""Utility functions used for tests and benchmarks"""
import randomfrom typing import List
import numpyimport torch
from quantization.scalar_type import ScalarType
from .marlin_utils_test import marlin_weightsfrom .quant_utils import gptq_quantize_weights
# This is PyTorch implementation of main part of reorder_meta()# function, from tools/util/include/cutlass/util/host_reorder.h file# of CUTLASS source tree. Furthermore, CUTLASS template for sparse# GEMM decides upon layout of this matrix, and at the moment for the# sparse GEMM executed on tensor cores, this is layout described by# ColumnMajorInterleaved<2> data structure, in# include/cutlass/layout/matrix.h of CUTLASS source tree. The# reordering of meta matrix into meta_reordered matrix calculated# according to these segments of CUTLASS code is re-implemented here.# Note that this calculation produces offsets for scattering metadata# matrix elements into reordered metadata matrix elements (or,# equivalently, for gathering reordered metadata matrix element back# into metadata matrix elements).def _calculate_meta_reordering_scatter_offsets(m, meta_ncols, meta_dtype, device): dst_rows = torch.arange(0, m, device=device)[:, None].repeat(1, meta_ncols) dst_cols = torch.arange(0, meta_ncols, device=device).repeat(m, 1)
# Reorder the rows, then swizzle the 2x2 blocks. group_x = 64 group_y = 32 if meta_dtype.itemsize == 2 else 16
dst_rows = ( dst_rows // group_x * group_x + (dst_rows % 2) * 2 + (dst_rows % 8) // 4 + ((dst_rows % group_y) % 4) // 2 * 32 + ((dst_rows % group_x) // 8) * 4 )
topright = ((dst_rows % 2 == 0) & (dst_cols % 2 == 1)).to(torch.int8) bottomleft = ((dst_rows % 2 == 1) & (dst_cols % 2 == 0)).to(torch.int8) dst_rows += topright - bottomleft dst_cols -= topright - bottomleft
# Assumed that meta tensor is to be stored in CUTLASS # InterleavedColumnMajor layout, and reverse engineered # corresponding code to store values into this tensor. interleave = 2 cols_maj = dst_cols // interleave cols_min = dst_cols % interleave return (cols_maj * m * interleave + dst_rows * interleave + cols_min).view(-1)
# This function converts dense matrix into sparse semi-structured# representation, producing "compressed" matrix, in the layout used by# CUTLASS backend, and corresponding metadata matrix.def sparse_semi_structured_from_dense_cutlass(dense): if dense.dim() != 2: raise RuntimeError( f"Expected 2-dimensional dense tensor, got {dense.dim()}-dimensional tensor" # noqa: E501 )
m, k = dense.shape device = dense.device
meta_dtype = torch.int8 if dense.dtype == torch.int8: meta_dtype = torch.int32 elif dense.dtype in [torch.half, torch.bfloat16, torch.float, torch.int32]: meta_dtype = torch.int16 else: raise RuntimeError(f"Invalid datatype {dense.dtype} of dense matrix") quadbits_per_meta_elem = meta_dtype.itemsize * 8 // 4 if quadbits_per_meta_elem not in (4, 8): raise RuntimeError("Invalid number of elements per meta element calculated")
if meta_dtype == torch.int32: if m % 16 != 0: raise RuntimeError( f"Number of rows of dense matrix {m} must be divisible by 16" ) else: if m % 32 != 0: raise RuntimeError( f"Number of rows of dense matrix {m} must be divisible by 32" ) if k % (4 * quadbits_per_meta_elem) != 0: raise RuntimeError( f"Number of columns of dense matrix {k} must be divisible by {4 * quadbits_per_meta_elem}" # noqa: E501 )
if dense.dtype != torch.float: ksparse = 4 dense_4 = dense.view(-1, k // ksparse, ksparse) m0, m1, m2, m3 = (dense_4 != 0).unbind(-1) else: ksparse = 2 dense_2 = dense.view(-1, k // ksparse, ksparse) m0, m2 = m1, m3 = (dense_2 != 0).unbind(-1) meta_ncols = k // (ksparse * quadbits_per_meta_elem)
# Encoding quadruples of True/False values as follows: # [True, True, False, False] -> 0b0100 # [True, False, True, False] -> 0b1000 # [False, True, True, False] -> 0b1001 # [True, False, False, True ] -> 0b1100 # [False, True, False, True ] -> 0b1101 # [False, False, True, True ] -> 0b1110 # Thus, lower two bits in the encoding are index of the True value # at the lowest index in the quadruple, and the higher two bits in # the encoding are index of the other True value in the quadruple. # In case there are less than two True values, than False value or # values at some index or indices are considered True for the # encoding. In case there are more than two True values, then the # excess True value(s) at some indices are considered False for # the encoding. The exact encodings used for these cases are as # follows: # [False, False, False, False] -> 0b1110 # [False, False, False, True ] -> 0b1110 # [False, False, True, False] -> 0b1110 # [False, True, False, False] -> 0b1001 # [False, True, True, True ] -> 0b1101 # [True, False, False, False] -> 0b1000 # [True, False, True, True ] -> 0b1100 # [True, True, False, True ] -> 0b0100 # [True, True, True, False] -> 0b0100 # [True, True, True, True ] -> 0b0100 # These particular encodings are chosen, with the help of Espresso # logic minimizer software, for the purpose of minimization of # corresponding Boolean functions, that translate non-zero flags # into encoding bits. Note also possible choices for the first # and last of these encodings were limited only to (0b0100, # 0b1110), in order to produce valid encodings for 1:2 sparsity # case.
expr0 = m0 & m1 expr1 = ~m0 & m1 expr2 = ~m0 & ~m1 bit0 = expr1 bit1 = expr2 bit2 = expr0 | expr2 | m3 bit3 = expr1 | ~m1 idxs0 = bit0 | (bit1.to(torch.int64) << 1) idxs1 = bit2 | (bit3.to(torch.int64) << 1)
if dense.dtype != torch.float: sparse0 = dense_4.gather( -1, idxs0.unsqueeze(-1) ) # type: ignore[possibly-undefined] sparse1 = dense_4.gather(-1, idxs1.unsqueeze(-1)) sparse = torch.stack((sparse0, sparse1), dim=-1).view(m, k // 2) else: sparse = dense_2.gather(-1, idxs0.unsqueeze(-1) // 2).view( m, k // 2 ) # type: ignore[possibly-undefined]
meta_4 = idxs0 | (idxs1 << 2) meta_n = meta_4.view((-1, meta_ncols, quadbits_per_meta_elem)).to(meta_dtype)
if quadbits_per_meta_elem == 4: meta = ( meta_n[:, :, 0] | (meta_n[:, :, 1] << 4) | (meta_n[:, :, 2] << 8) | (meta_n[:, :, 3] << 12) ) elif quadbits_per_meta_elem == 8: meta = ( meta_n[:, :, 0] | (meta_n[:, :, 1] << 4) | (meta_n[:, :, 2] << 8) | (meta_n[:, :, 3] << 12) | (meta_n[:, :, 4] << 16) | (meta_n[:, :, 5] << 20) | (meta_n[:, :, 6] << 24) | (meta_n[:, :, 7] << 28) )
# Reorder meta tensor elements. meta_reordered = meta.new_empty( (m * meta_ncols,) ) # type: ignore[possibly-undefined] meta_offsets = _calculate_meta_reordering_scatter_offsets( m, meta_ncols, meta_dtype, device ) meta_reordered.scatter_(0, meta_offsets, meta.view(-1))
return (sparse, meta_reordered.view(m, meta_ncols))
# This function performs reverse of the function above - it# reconstructs dense matrix from a pair of "compressed" matrix, given# in the layout used by CUTLASS backend, and accompanying metadata# matrix.def sparse_semi_structured_to_dense_cutlass(sparse, meta_reordered): if sparse.dim() != 2: raise RuntimeError( f"Expected 2-dimensional sparse tensor, got {sparse.dim()}-dimensional tensor" # noqa: E501 )
m, k = sparse.shape device = sparse.device
if meta_reordered.dim() != 2: raise RuntimeError( f"Expected 2-dimensional meta tensor, got {meta_reordered.dim()}-dimensional tensor" # noqa: E501 ) if meta_reordered.device != device: raise RuntimeError( f"Expected meta matrix to be on {device} device, got matrix on {meta_reordered.device} device" # noqa: E501 )
meta_dtype = meta_reordered.dtype if meta_dtype not in (torch.int16, torch.int32): raise RuntimeError(f"Invalid datatype {meta_dtype} of meta matrix") quadbits_per_meta_elem = meta_dtype.itemsize * 8 // 4
ksparse = 4 if sparse.dtype != torch.float else 2
meta_nrows, meta_ncols = meta_reordered.shape if meta_nrows != m: raise RuntimeError( f"Number of rows of meta matrix {meta_nrows} must be equal to number of columns of spase matrix {m}" # noqa: E501 ) if meta_ncols * ksparse * quadbits_per_meta_elem != 2 * k: raise RuntimeError( f"Number of columns of sparse matrix {k} different from the {meta_ncols * ksparse * quadbits_per_meta_elem // 2}, " # noqa: E501 "expected according to the number of columns of meta matrix" )
# Undo meta tensor elements reordering. meta_offsets = _calculate_meta_reordering_scatter_offsets( m, meta_ncols, meta_dtype, device ) meta = torch.gather(meta_reordered.view(-1), 0, meta_offsets).view(m, meta_ncols)
# Unpack sparse tensor back to original dense tensor, using # information provided by meta tensor. Note that torch.float # datatype is handled pretty much the same as # torch.half/torch.bfloat16, as metadata for a pair of torch.float # value is encoded as if underlying 8 bytes contain four # torch.half/torch.bfloat16 values, where either first two or last # two are zeros. meta_2 = torch.empty( (m, meta_ncols, 2 * quadbits_per_meta_elem), dtype=meta_dtype, device=device, ) if quadbits_per_meta_elem == 4: meta_2[:, :, 0] = meta & 0b11 meta_2[:, :, 1] = (meta >> 2) & 0b11 meta_2[:, :, 2] = (meta >> 4) & 0b11 meta_2[:, :, 3] = (meta >> 6) & 0b11 meta_2[:, :, 4] = (meta >> 8) & 0b11 meta_2[:, :, 5] = (meta >> 10) & 0b11 meta_2[:, :, 6] = (meta >> 12) & 0b11 meta_2[:, :, 7] = (meta >> 14) & 0b11 elif quadbits_per_meta_elem == 8: meta_2[:, :, 0] = meta & 0b11 meta_2[:, :, 1] = (meta >> 2) & 0b11 meta_2[:, :, 2] = (meta >> 4) & 0b11 meta_2[:, :, 3] = (meta >> 6) & 0b11 meta_2[:, :, 4] = (meta >> 8) & 0b11 meta_2[:, :, 5] = (meta >> 10) & 0b11 meta_2[:, :, 6] = (meta >> 12) & 0b11 meta_2[:, :, 7] = (meta >> 14) & 0b11 meta_2[:, :, 8] = (meta >> 16) & 0b11 meta_2[:, :, 9] = (meta >> 18) & 0b11 meta_2[:, :, 10] = (meta >> 20) & 0b11 meta_2[:, :, 11] = (meta >> 22) & 0b11 meta_2[:, :, 12] = (meta >> 24) & 0b11 meta_2[:, :, 13] = (meta >> 26) & 0b11 meta_2[:, :, 14] = (meta >> 28) & 0b11 meta_2[:, :, 15] = (meta >> 30) & 0b11
dense_offsets = meta_2.view(-1) + ( torch.arange(0, 2 * m * k // ksparse, device=device) * 4 ).view(-1, 1).repeat(1, 2).view(-1)
dense = torch.zeros((m * 2 * k,), dtype=sparse.dtype, device=device) if sparse.dtype != torch.float: # dense.scatter_(0, dense_offsets, sparse.view(-1)) dense.scatter_(0, dense_offsets, sparse.reshape(-1)) else: dense.view(torch.half).scatter_( 0, dense_offsets, sparse.view(torch.half).view(-1) )
return dense.view(m, 2 * k)
def mask_creator(tensor): """ Class for creating N:M sparsity masks. Masks will be created using the N:M ratio, where for every block of M weights, N will be pruned based on ranked weight value. Each mask will correspond to the given tensor.
:param N: The number of weights in a group to keep :param M: The size of a weight group """ N = 2 M = 4
mask = None # for i, tensor in enumerate(tensors): if tensor.numel() % M != 0: raise ValueError( f"Tensor of size {tensor.shape} can't be evenly divided into " f"{M} groups" )
num_groups = tensor.numel() // M
# N:M sparsity for linear layers tensor_temp = tensor.detach().abs().reshape(num_groups, M) index = torch.argsort(tensor_temp, dim=1)[:, : int(M - N)]
w_b = torch.ones(tensor_temp.shape, device=tensor_temp.device) mask = w_b.scatter_(dim=1, index=index, value=0).reshape(tensor.shape)
return mask
def inject_24(w, size_k, size_n): assert w.shape == (size_k, size_n)
mask = mask_creator(w.t()).t().cuda().bool()
return (mask * w).contiguous(), mask.contiguous()
def check_24(w, num_rows_to_sample=50, _verbose=False): BLOCK_SIZE = 4 MAX_NON_ZEROS = 2
w = w.t().contiguous()
print("check_24: w.shape = {}".format(w.shape))
num_rows, num_cols = w.shape sampled_row_idxs = random.choices(range(num_rows), k=num_rows_to_sample) if _verbose: print(f"Sampled row idxs = {sampled_row_idxs}")
total_segments = 0 non_24_segments = 0 for i in sampled_row_idxs: for j in range(0, num_cols - BLOCK_SIZE, BLOCK_SIZE): total_segments += 1 block = w[i, j : j + BLOCK_SIZE] num_nonzero = torch.count_nonzero(block) if num_nonzero > MAX_NON_ZEROS: print("i = {} j = {} block = {}".format(i, j, block)) non_24_segments += 1
print(f"{non_24_segments} / {total_segments} do not have 2:4 structure.")
def compress_quantized_24_weight(q_24, size_k, size_n, wtype: ScalarType): assert q_24.shape == (size_k, size_n)
# Remove bias to normalize over 0 q_24_no_zp = q_24 - wtype.bias
# Compress q_24_no_zp = q_24_no_zp.t().contiguous() q_24_no_zp_comp, meta = sparse_semi_structured_from_dense_cutlass(q_24_no_zp) q_24_no_zp_comp = q_24_no_zp_comp.t().contiguous()
# Restore bias q_24_comp = q_24_no_zp_comp + wtype.bias
# Resize meta to its actual shape (without moving any data) meta = meta.resize_(meta.shape[1] // 2, meta.shape[0] * 2)
return q_24_comp, meta
def get_scale_perms_24(): scale_perm: List[int] = [] for i in range(8): scale_perm.extend([i * 8 + j for j in [0, 4, 1, 5, 2, 6, 3, 7]]) scale_perm_single: List[int] = [] for i in range(8): scale_perm_single.extend([8 * i + j for j in [0, 1, 2, 3, 4, 5, 6, 7]]) return scale_perm, scale_perm_single
def get_weight_perm_24(num_bits: int): perm_list: List[int] = [] for i in range(32): perm1: List[int] = [] col = i // 4 col_o = col // 2 for block in [0, 1]: for row in [ 2 * (i % 4), 2 * (i % 4) + 1, 2 * (i % 4 + 4), 2 * (i % 4 + 4) + 1, ]: perm1.append(16 * row + col_o * 256 + 8 * (col % 2) + 4 * block) for j in range(4): perm_list.extend([p + 1 * j for p in perm1]) perm = numpy.array(perm_list)
if num_bits == 4: interleave = numpy.array([0, 2, 4, 6, 1, 3, 5, 7]) elif num_bits == 8: interleave = numpy.array([0, 2, 1, 3]) else: raise ValueError("num_bits must be 4 or 8, got {}".format(num_bits))
perm = perm.reshape((-1, len(interleave)))[:, interleave].ravel() perm = torch.from_numpy(perm) return perm
def marlin_permute_scales_24( s: torch.Tensor, size_k: int, size_n: int, group_size: int) -> torch.Tensor:
scale_perm, scale_perm_single = get_scale_perms_24() if group_size < size_k and group_size != -1: s = s.reshape((-1, len(scale_perm)))[:, scale_perm] else: s = s.reshape((-1, len(scale_perm_single)))[:, scale_perm_single] s = s.reshape((-1, size_n)).contiguous()
return s
def marlin_24_quantize( w: torch.Tensor, quant_type: ScalarType, group_size: int,): size_k, size_n = w.shape
# Normalize group_size if group_size == -1: group_size = size_k assert group_size <= size_k
# Inject 2:4 sparsity w_24, mask_24 = inject_24(w, size_k, size_n)
# Quantize w_24_ref, q_w_24, s, g_idx, rand_perm = gptq_quantize_weights( w_24, quant_type, group_size, act_order=False )
# Compress quantized weight q_w_24_comp, meta = compress_quantized_24_weight(q_w_24, size_k, size_n, quant_type) size_k_comp = size_k // 2
# Reformat to marlin weight_perm = get_weight_perm_24(quant_type.size_bits) marlin_24_q_w_comp = marlin_weights( q_w_24_comp, size_k_comp, size_n, quant_type.size_bits, weight_perm ) marlin_24_s = marlin_permute_scales_24(s, size_k, size_n, group_size)
# Create result res_list = [w_24_ref, marlin_24_q_w_comp, meta, marlin_24_s] for i in range(len(res_list)): res_list[i] = res_list[i].to(w.device)
return res_list123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125from typing import List
import numpyimport torch
from .marlin_utils_test import marlin_permute_weightsfrom .quant_utils import get_pack_factor, qqq_quantize_weights
def marlin_qqq_weights(q_w, size_k, size_n, num_bits, perm, group_size): # Permute q_w = marlin_permute_weights(q_w, size_k, size_n, perm)
# Pack pack_factor = get_pack_factor(num_bits) orig_device = q_w.device
q_w = q_w.cpu().numpy().astype(numpy.uint32)
q_packed = numpy.zeros((q_w.shape[0], q_w.shape[1] // pack_factor), dtype=numpy.uint32) if group_size == size_k: for i in range(pack_factor): q_packed |= (q_w[:, i::pack_factor] & 0xF) << num_bits * i else: for i in range(pack_factor): q_packed |= q_w[:, i::pack_factor] << num_bits * i
q_packed = torch.from_numpy(q_packed.astype(numpy.int32)).to(orig_device)
return q_packed
def get_qqq_scale_perms(): scale_perm: List[int] = [] for i in range(8): scale_perm.extend([i + 8 * j for j in range(8)]) scale_perm_single: List[int] = [] for i in range(4): scale_perm_single.extend( [2 * i + j for j in [0, 1, 8, 9, 16, 17, 24, 25]]) return scale_perm, scale_perm_single
# NOTE(HandH1998): QQQ employs different perms for per-group and per-channel weight quantization. # noqa: E501def get_qqq_weight_perm(num_bits: int, quant_type: str): perm_list: List[int] = [] for i in range(32): perm1: List[int] = [] col = i // 4 for block in [0, 1]: for row in [ 4 * (i % 4), 4 * (i % 4) + 1, 4 * (i % 4) + 2, 4 * (i % 4) + 3, ]: perm1.append(16 * row + col + 8 * block) for j in range(4): perm_list.extend([p + 256 * j for p in perm1])
perm = numpy.array(perm_list)
assert quant_type in ["per-channel", "per-group"], "not supported quantization type" if num_bits == 4: if quant_type == "per-channel": interleave = numpy.array([4, 0, 5, 1, 6, 2, 7, 3]) else: interleave = numpy.array([0, 2, 4, 6, 1, 3, 5, 7]) else: raise Exception("num_bits must be 4, got {}".format(num_bits))
perm = perm.reshape((-1, len(interleave)))[:, interleave].ravel() perm = torch.from_numpy(perm) return perm
def marlin_qqq_permute_scales(s_group, s_channel, size_k, size_n, group_size): scale_perm, scale_perm_single = get_qqq_scale_perms() if group_size < size_k and group_size != -1: s_group = s_group.reshape((-1, len(scale_perm)))[:, scale_perm] s_channel = s_channel.reshape( (-1, len(scale_perm_single)))[:, scale_perm_single] s_group = s_group.reshape((-1, size_n)).contiguous() else: s_channel = s_channel.reshape( (-1, len(scale_perm_single)))[:, scale_perm_single] s_channel = s_channel.reshape((-1, size_n)).contiguous()
return s_group, s_channel
def marlin_qqq_quantize( w: torch.Tensor, num_bits: int, group_size: int,): size_k, size_n = w.shape
# Normalize group_size if group_size == -1: group_size = size_k assert group_size <= size_k quant_type = "per-channel" if group_size == size_k else "per-group"
# Quantize w_ref, q_w, s_group, s_channel = qqq_quantize_weights( w, num_bits, group_size)
# Reformat to marlin_qqq weight_perm = get_qqq_weight_perm(num_bits, quant_type) marlin_qqq_q_w = marlin_qqq_weights(q_w, size_k, size_n, num_bits, weight_perm, group_size) marlin_qqq_s_group, marlin_qqq_s_channel = marlin_qqq_permute_scales( s_group, s_channel, size_k, size_n, group_size)
# Create result res_list = [ w_ref, marlin_qqq_q_w, marlin_qqq_s_group, marlin_qqq_s_channel ] for i in range(len(res_list)): res_list[i] = res_list[i].to(w.device)
return res_list123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470"""This file is used for /tests and /benchmarks"""
from typing import List, Optional
import numpyimport torch
from quantization.scalar_type import ScalarType, scalar_types
SUPPORTED_GPTQ_QUANT_TYPES = [scalar_types.uint4b8, scalar_types.uint8b128]SUPPORTED_GROUP_SIZES = [-1, 32, 64, 128]
MARLIN_QQQ_SUPPORTED_NUM_BITS = [4]
# Note: this is a hack. We should update each model to register the# stacked params and get it from there instead in a future PR.# fused_name: List[shard_name]FUSED_LAYER_NAME_MAPPING = { "qkv_proj": ["q_proj", "k_proj", "v_proj"], "gate_up_proj": ["gate_proj", "up_proj"],}
def pack_quantized_values_into_int32( w_q: torch.Tensor, wtype: ScalarType, packed_dim: int = 0): # move dim to pack to the end perm = (*[i for i in range(len(w_q.shape)) if i != packed_dim], packed_dim) inv_perm = tuple(perm.index(i) for i in range(len(perm))) w_q_perm = w_q.permute(perm)
pack_factor = 32 // wtype.size_bits mask = (1 << wtype.size_bits) - 1
new_shape_perm = list(w_q_perm.shape) assert w_q_perm.shape[-1] % pack_factor == 0 new_shape_perm[-1] //= pack_factor
res = torch.zeros(new_shape_perm, dtype=torch.int32, device=w_q.device) for i in range(pack_factor): res |= (w_q_perm[..., i::pack_factor] & mask) << wtype.size_bits * i
return res.permute(inv_perm)
def unpack_quantized_values_into_int32( w_q: torch.Tensor, wtype: ScalarType, packed_dim: int = 0): # move dim to pack to the end perm = (*[i for i in range(len(w_q.shape)) if i != packed_dim], packed_dim) inv_perm = tuple(perm.index(i) for i in range(len(perm))) w_q_perm = w_q.permute(perm)
pack_factor = 32 // wtype.size_bits mask = (1 << wtype.size_bits) - 1
new_shape_perm = list(w_q_perm.shape) new_shape_perm[-1] *= pack_factor
res = torch.zeros(new_shape_perm, dtype=torch.int32, device=w_q.device) for i in range(pack_factor): res[..., i::pack_factor] = (w_q_perm >> wtype.size_bits * i) & mask
return res.permute(inv_perm)
def is_layer_skipped(prefix: str, ignored_layers: List[str]) -> bool: # prefix: model.layers.0.self_attn.q_proj # proj_name: q_proj proj_name = prefix.split(".")[-1] if proj_name in FUSED_LAYER_NAME_MAPPING: shard_prefixes = [ prefix.replace(proj_name, shard_proj_name) for shard_proj_name in FUSED_LAYER_NAME_MAPPING[proj_name] ]
is_skipped = None for shard_prefix in shard_prefixes: is_shard_skipped = shard_prefix in ignored_layers
if is_skipped is None: is_skipped = is_shard_skipped elif is_shard_skipped != is_skipped: raise ValueError( f"Detected some but not all shards of {prefix} " "are quantized. All shards of fused layers " "to have the same precision." ) else: is_skipped = prefix in ignored_layers
assert is_skipped is not None return is_skipped
def get_pack_factor(num_bits): assert 32 % num_bits == 0, f"Unsupported num_bits = {num_bits}" return 32 // num_bits
def permute_rows( q_w: torch.Tensor, w_ref: torch.Tensor, group_size: int, test_perm: Optional[torch.Tensor] = None,): assert q_w.shape == w_ref.shape
orig_device = q_w.device k_size, _ = q_w.shape
g_idx = torch.zeros((k_size,), dtype=torch.int32) for i in range(k_size): g_idx[i] = i // group_size
# Simulate act_order by doing a random permutation on K rand_perm = test_perm if test_perm is not None else torch.randperm(k_size)
g_idx = g_idx[rand_perm].contiguous() q_w = q_w[rand_perm, :].contiguous() w_ref = w_ref[rand_perm, :].contiguous()
return ( w_ref.to(device=orig_device), q_w.to(device=orig_device), g_idx.to(device=orig_device), rand_perm.to(device=orig_device), )
def quantize_weights( w: torch.Tensor, quant_type: ScalarType, group_size: Optional[int], zero_points: bool = False, ref_zero_points_after_scales: bool = False,): assert ( quant_type.is_integer() ), "Floating point quantization may work but has not been tested" assert not zero_points or group_size is not None, ( "to have group zero points, group_size must be provided " "(-1 group_size is channelwise)" )
orig_device = w.device orig_type = w.dtype size_k, size_n = w.shape
assert w.is_floating_point(), "w must be float"
if group_size == -1: group_size = size_k
# Reshape to [groupsize, -1] if group_size is not None and group_size < size_k: w = w.reshape((-1, group_size, size_n)) w = w.permute(1, 0, 2) w = w.reshape((group_size, -1))
# Compute scale for each group max_val = torch.max(w, 0, keepdim=True).values min_val = torch.min(w, 0, keepdim=True).values
max_q_val = quant_type.max() min_q_val = quant_type.min()
w_s = torch.Tensor([1.0]).to(w.device) # unscaled case maybe_w_zp = None if group_size is not None: if zero_points: assert not quant_type.is_signed() and quant_type.max() > 0 w_s = (max_val - min_val).clamp(min=1e-5) / quant_type.max() maybe_w_zp = ( torch.round(torch.abs(min_val / w_s)).clamp(min_q_val, max_q_val).int() ) else: # If the bias is such that there are no possible negative/positive # values, set the max value to inf to avoid divide by 0 w_s = torch.max( abs(max_val / (max_q_val if max_q_val != 0 else torch.inf)), abs(min_val / (min_q_val if min_q_val != 0 else torch.inf)), )
# Quantize w_q = torch.round(w / w_s).int() + (maybe_w_zp if zero_points else 0) w_q = torch.clamp(w_q, min_q_val, max_q_val)
# Compute ref (dequantized) # For some kernels (namely Machete) the zero-points are applied after the # scales are applied, for this case computing the reference in similar way # allows us to use tighter error tolerances in our unit tests. if ref_zero_points_after_scales and maybe_w_zp is not None: w_ref = w_q.to(orig_type) * w_s - maybe_w_zp.to(orig_type) * w_s else: w_ref = (w_q - (maybe_w_zp if zero_points else 0)).to(orig_type) * w_s
if quant_type.has_bias(): w_q += quant_type.bias
# Restore original shapes if group_size is not None and group_size < size_k:
def reshape_w(w): w = w.reshape((group_size, -1, size_n)) w = w.permute(1, 0, 2) w = w.reshape((size_k, size_n)).contiguous() return w
w_q = reshape_w(w_q) w_ref = reshape_w(w_ref) w_s = w_s.reshape((-1, size_n)).contiguous()
if maybe_w_zp is not None: maybe_w_zp = maybe_w_zp.reshape((-1, size_n)).contiguous() maybe_w_zp = maybe_w_zp.to(device=orig_device)
return ( w_ref.to(device=orig_device), w_q.to(device=orig_device), w_s if group_size is not None else None, maybe_w_zp, )
def gptq_quantize_weights( w: torch.Tensor, quant_type: ScalarType, group_size: int, act_order: bool, test_perm: Optional[torch.Tensor] = None,): size_k, _ = w.shape
assert w.is_floating_point(), "w must be float" assert ( quant_type in SUPPORTED_GPTQ_QUANT_TYPES ), f"Unsupported gptq type = {quant_type}" assert group_size in SUPPORTED_GROUP_SIZES + [ size_k ], f"Unsupported groupsize = {group_size}"
w_ref, w_q, w_s, _ = quantize_weights(w, quant_type, group_size)
# Apply act_order g_idx = torch.empty(0, dtype=torch.int, device=w.device) rand_perm = torch.empty(0, dtype=torch.int, device=w.device) if act_order: assert ( group_size < size_k ), "For act_order, groupsize = {} must be less than size_k = {}".format( group_size, size_k )
w_ref, w_q, g_idx, rand_perm = permute_rows(w_q, w_ref, group_size, test_perm)
return w_ref, w_q, w_s, g_idx, rand_perm
# QQQ employs different quant schemes for per-group and# per-channel quantization.def qqq_quantize_weights(w: torch.Tensor, num_bits: int, group_size: int): orig_device = w.device size_k, size_n = w.shape
assert w.is_floating_point(), "w must be float" assert ( num_bits in MARLIN_QQQ_SUPPORTED_NUM_BITS ), f"Unsupported num_bits = {num_bits}" assert group_size in SUPPORTED_GROUP_SIZES + [ size_k ], f"Unsupported groupsize = {group_size}"
if group_size == -1: group_size = size_k assert group_size <= size_k
if group_size < size_k: # Reshape to [groupsize, -1] w = w.reshape((-1, group_size, size_n)) w = w.permute(1, 0, 2) w = w.reshape((group_size, -1))
max_q_val = 2**num_bits - 1 half_q_val = (max_q_val + 1) // 2
# Compute scale for each group s_group = torch.max(torch.abs(w), 0, keepdim=True)[0] s_group *= 2 / max_q_val # 2 => symmetric
# Quantize q_w = torch.round(w / s_group).int() q_w += half_q_val q_w = torch.clamp(q_w, 0, max_q_val) # Compute ref (dequantized) w_ref = (q_w - half_q_val).half() * s_group
# Restore original shapes def reshape_w(w): w = w.reshape((group_size, -1, size_n)) w = w.permute(1, 0, 2) w = w.reshape((size_k, size_n)).contiguous() return w
q_w = reshape_w(q_w) w_ref = reshape_w(w_ref)
# Compute int8 quantization scale for each channel s_channel = torch.max(torch.abs(w_ref), 0, keepdim=True)[0] s_channel /= 127.0 t_int8 = (w_ref / s_channel).round().clamp(-128, 127).to(torch.int8) w_ref = t_int8.half() * s_channel s_channel = s_channel.reshape(1, -1).to(dtype=torch.float)
# Fuse scales s_group = (s_group.reshape(-1, size_n).contiguous() / s_channel).to( dtype=torch.half ) else: max_q_val = 2 ** (num_bits - 1) - 1
# Compute scale for each channel s_channel = torch.max(torch.abs(w), 0, keepdim=True)[0] s_channel /= max_q_val
# Quantize q_w = torch.round(w / s_channel).int() q_w = torch.clamp(q_w, -max_q_val, max_q_val) # Compute ref (dequantized) w_ref = q_w.half() * s_channel
s_group = torch.tensor([], dtype=torch.half) # div 2 ** (8 - self.bits)) to offset right shift in unpacking s_channel /= 2 ** (8 - num_bits) s_channel = s_channel.reshape(-1, size_n).contiguous().to(torch.float)
return ( w_ref.to(device=orig_device), q_w.to(device=orig_device), s_group.to(device=orig_device), s_channel.to(device=orig_device), )
def sort_weights(q_w: torch.Tensor, g_idx: torch.Tensor): orig_device = q_w.device
sort_indices = torch.argsort(g_idx).to(dtype=torch.int32) # Sort based on g_idx
g_idx = g_idx[sort_indices].contiguous() q_w = q_w[sort_indices, :].contiguous()
return ( q_w.to(device=orig_device), g_idx.to(device=orig_device), sort_indices.to(device=orig_device), )
def pack_rows( q_w: torch.Tensor, num_bits: int, size_k: int, size_n: int,): assert q_w.shape == (size_k, size_n)
pack_factor = get_pack_factor(num_bits) assert size_k % pack_factor == 0
orig_device = q_w.device
q_w = q_w.cpu().numpy().astype(numpy.uint32)
q_res = numpy.zeros((size_k // pack_factor, size_n), dtype=numpy.uint32)
for i in range(pack_factor): q_res |= q_w[i::pack_factor, :] << num_bits * i
q_res = torch.from_numpy(q_res.astype(numpy.int32)).to(orig_device) return q_res
def pack_cols( q_w: torch.Tensor, num_bits: int, size_k: int, size_n: int,): assert q_w.shape == (size_k, size_n)
pack_factor = get_pack_factor(num_bits) assert size_n % pack_factor == 0
orig_device = q_w.device
q_w = q_w.cpu().numpy().astype(numpy.uint32)
q_res = numpy.zeros((size_k, size_n // pack_factor), dtype=numpy.uint32)
for i in range(pack_factor): q_res |= q_w[:, i::pack_factor] << num_bits * i
q_res = torch.from_numpy(q_res.astype(numpy.int32)).to(orig_device) q_res = q_res.contiguous()
return q_res
def unpack_cols( packed_q_w: torch.Tensor, num_bits: int, size_k: int, size_n: int,): pack_factor = get_pack_factor(num_bits) assert size_n % pack_factor == 0 assert packed_q_w.shape == ( size_k, size_n // pack_factor, ), "packed_q_w.shape = {} size_k = {}, size_n = {} pack_Factor = {}".format( packed_q_w.shape, size_k, size_n, pack_factor )
orig_device = packed_q_w.device
packed_q_w_cpu = packed_q_w.cpu().numpy().astype(numpy.uint32) q_res = numpy.zeros((size_k, size_n), dtype=numpy.uint32)
mask = (1 << num_bits) - 1 for i in range(pack_factor): vals = packed_q_w_cpu & mask packed_q_w_cpu >>= num_bits q_res[:, i::pack_factor] = vals
q_res = torch.from_numpy(q_res.astype(numpy.int32)).to(orig_device) q_res = q_res.contiguous()
return q_res
def gptq_pack( q_w: torch.Tensor, num_bits: int, size_k: int, size_n: int,): return pack_rows(q_w, num_bits, size_k, size_n)
def awq_pack( q_w: torch.Tensor, num_bits: int, size_k: int, size_n: int,): assert q_w.shape == (size_k, size_n)
# Interleave column dim (for the dequantize code) and pack it to int32 if num_bits == 4: interleave = numpy.array([0, 2, 4, 6, 1, 3, 5, 7]) elif num_bits == 8: interleave = numpy.array([0, 2, 1, 3]) else: raise Exception("num_bits must be 4 or 8, got {}".format(num_bits))
q_w = q_w.reshape((-1, len(interleave)))[:, interleave].ravel() q_w = q_w.reshape((-1, size_n)).contiguous()
return pack_cols(q_w, num_bits, size_k, size_n)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209Contains code from https://github.com/IST-DASLab/marlin
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
Copyright {yyyy} {name of copyright owner}
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
------------------------------------------------------------------------------------
This product bundles various third-party components under other open source licenses.This section summarizes those components and their licenses. See licenses/for text of these licenses.1234567891011121314151617181920212223242526272829303132/* * Modified by HandH1998 * Modified by Neural Magic * Copyright (C) Marlin.2024 Elias Frantar * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
#pragma once
constexpr int ceildiv(int a, int b) { return (a + b - 1) / b; }
// Instances of `Vec` are used to organize groups of >>registers<<, as needed// for instance as inputs to tensor core operations. Consequently, all// corresponding index accesses must be compile-time constants, which is why we// extensively use `#pragma unroll` throughout the kernel code to guarantee// this.template <typename T, int n>struct Vec { T elems[n]; __device__ T& operator[](int i) { return elems[i]; }};1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889/* * Modified by HandH1998 * Modified by Neural Magic * Copyright (C) Marlin.2024 Elias Frantar * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
#pragma once
// Predicated asynchronous global->shared copy; used for inputs A where we apply// predication to handle batchsizes that are not multiples of 16.__device__ inline void cp_async4_pred(void* smem_ptr, const void* glob_ptr, bool pred = true) { const int BYTES = 16; uint32_t smem = static_cast<uint32_t>(__cvta_generic_to_shared(smem_ptr)); asm volatile( "{\n" " .reg .pred p;\n" " setp.ne.b32 p, %0, 0;\n" " @p cp.async.cg.shared.global [%1], [%2], %3;\n" "}\n" ::"r"((int)pred), "r"(smem), "l"(glob_ptr), "n"(BYTES));}
// Asynchronous global->shared copy__device__ inline void cp_async4(void* smem_ptr, const void* glob_ptr) { const int BYTES = 16; uint32_t smem = static_cast<uint32_t>(__cvta_generic_to_shared(smem_ptr)); asm volatile( "{\n" " cp.async.cg.shared.global [%0], [%1], %2;\n" "}\n" ::"r"(smem), "l"(glob_ptr), "n"(BYTES));}
// Async copy fence.__device__ inline void cp_async_fence() { asm volatile("cp.async.commit_group;\n" ::);}
// Wait until at most `n` async copy stages are still pending.template <int n>__device__ inline void cp_async_wait() { asm volatile("cp.async.wait_group %0;\n" ::"n"(n));}
// Wait until barrier reaches `count`, then lock for current threadblock.__device__ inline void barrier_acquire(int* lock, int count) { if (threadIdx.x == 0) { int state = -1; do // Guarantee that subsequent writes by this threadblock will be visible // globally. asm volatile("ld.global.acquire.gpu.b32 %0, [%1];\n" : "=r"(state) : "l"(lock)); while (state != count); } __syncthreads();}
// Release barrier and increment visitation count.__device__ inline void barrier_release(int* lock, bool reset = false) { __syncthreads(); if (threadIdx.x == 0) { if (reset) { lock[0] = 0; return; } int val = 1; // Make sure that all writes since acquiring this barrier are visible // globally, while releasing the barrier. asm volatile("fence.acq_rel.gpu;\n"); asm volatile("red.relaxed.gpu.global.add.s32 [%0], %1;\n" : : "l"(lock), "r"(val)); }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068/* * Modified by Neural Magic * Copyright (C) Marlin.2024 Elias Frantar * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
#include <torch/all.h>
#include <ATen/cuda/CUDAContext.h>#include <c10/cuda/CUDAGuard.h>#include <cuda.h>#include <cuda_fp16.h>#include <cuda_runtime.h>
#include <iostream>
#include "common/base.h"
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800 #include "common/mem.h"#endif
template <typename T>inline std::string str(T x) { return std::to_string(x);}
namespace marlin_dense {
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800
using I4 = Vec<int, 4>;// Matrix fragments for tensor core instructions; their precise layout is// documented here:// https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#matrix-fragments-for-mma-m16n8k16-with-floating-point-typeusing FragA = Vec<half2, 4>;using FragB = Vec<half2, 2>;using FragC = Vec<float, 4>;using FragS = Vec<half2, 1>; // quantization scales
// m16n8k16 tensor core mma instruction with fp16 inputs and fp32// output/accumulation.__device__ inline void mma(const FragA& a_frag, const FragB& frag_b, FragC& frag_c) { const uint32_t* a = reinterpret_cast<const uint32_t*>(&a_frag); const uint32_t* b = reinterpret_cast<const uint32_t*>(&frag_b); float* c = reinterpret_cast<float*>(&frag_c); asm volatile( "mma.sync.aligned.m16n8k16.row.col.f32.f16.f16.f32 " "{%0,%1,%2,%3}, {%4,%5,%6,%7}, {%8,%9}, {%10,%11,%12,%13};\n" : "=f"(c[0]), "=f"(c[1]), "=f"(c[2]), "=f"(c[3]) : "r"(a[0]), "r"(a[1]), "r"(a[2]), "r"(a[3]), "r"(b[0]), "r"(b[1]), "f"(c[0]), "f"(c[1]), "f"(c[2]), "f"(c[3]));}
// Instruction for loading a full 16x16 matrix fragment of operand A from shared// memory, directly in tensor core layout.__device__ inline void ldsm4(FragA& frag_a, const void* smem_ptr) { uint32_t* a = reinterpret_cast<uint32_t*>(&frag_a); uint32_t smem = static_cast<uint32_t>(__cvta_generic_to_shared(smem_ptr)); asm volatile("ldmatrix.sync.aligned.m8n8.x4.shared.b16 {%0,%1,%2,%3}, [%4];\n" : "=r"(a[0]), "=r"(a[1]), "=r"(a[2]), "=r"(a[3]) : "r"(smem));}
// Lookup-table based 3-input logical operation; explicitly used for// dequantization as the compiler does not seem to automatically recognize it in// all cases.template <int lut>__device__ inline int lop3(int a, int b, int c) { int res; asm volatile("lop3.b32 %0, %1, %2, %3, %4;\n" : "=r"(res) : "r"(a), "r"(b), "r"(c), "n"(lut)); return res;}
// Efficiently dequantize an int32 value into a full B-fragment of 4 fp16// values. We mostly follow the strategy in the link below, with some small// changes:// https://github.com/NVIDIA/FasterTransformer/blob/main/src/fastertransformer/cutlass_extensions/include/cutlass_extensions/interleaved_numeric_conversion.h__device__ inline FragB dequant(int q) { const int LO = 0x000f000f; const int HI = 0x00f000f0; const int EX = 0x64006400; // Guarantee that the `(a & b) | c` operations are LOP3s. int lo = lop3<(0xf0 & 0xcc) | 0xaa>(q, LO, EX); int hi = lop3<(0xf0 & 0xcc) | 0xaa>(q, HI, EX); // We want signed int4 outputs, hence we fuse the `-8` symmetric zero point // directly into `SUB` and `ADD`. const int SUB = 0x64086408; const int MUL = 0x2c002c00; const int ADD = 0xd480d480; FragB frag_b; frag_b[0] = __hsub2(*reinterpret_cast<half2*>(&lo), *reinterpret_cast<const half2*>(&SUB)); frag_b[1] = __hfma2(*reinterpret_cast<half2*>(&hi), *reinterpret_cast<const half2*>(&MUL), *reinterpret_cast<const half2*>(&ADD)); return frag_b;}
// Multiply dequantized values by the corresponding quantization scale; used// only for grouped quantization.__device__ inline void scale(FragB& frag_b, FragS& frag_s, int i) { half2 s = __half2half2(reinterpret_cast<__half*>(&frag_s)[i]); frag_b[0] = __hmul2(frag_b[0], s); frag_b[1] = __hmul2(frag_b[1], s);}
template <const int threads, // number of threads in a threadblock const int thread_m_blocks, // number of 16x16 blocks in the m // dimension (batchsize) of the // threadblock const int thread_n_blocks, // same for n dimension (output) const int thread_k_blocks, // same for k dimension (reduction) const int stages, // number of stages for the async global->shared // fetch pipeline const int group_blocks = -1 // number of consecutive 16x16 blocks // with a separate quantization scale >__global__ void Marlin( const int4* __restrict__ A, // fp16 input matrix of shape mxk const int4* __restrict__ B, // 4bit quantized weight matrix of shape kxn int4* __restrict__ C, // fp16 output buffer of shape mxn const int4* __restrict__ s, // fp16 quantization scales of shape // (k/groupsize)xn int prob_m, // batch dimension m int prob_n, // output dimension n int prob_k, // reduction dimension k int* locks // extra global storage for barrier synchronization) { // Each threadblock processes one "stripe" of the B matrix with (roughly) the // same size, which might involve multiple column "slices" (of width 16 * // `thread_n_blocks`). Stripes are defined as shown in the 3x3 matrix 5 SM // example: // 0 1 3 // 0 2 3 // 1 2 4 // While this kind of partitioning makes things somewhat more complicated, it // ensures good utilization of all SMs for many kinds of shape and GPU // configurations, while requiring as few slow global cross-threadblock // reductions as possible.
// For larger GEMMs we run multiple batchsize 64 versions in parallel for a // better partitioning with less reductions int parallel = 1; if (prob_m > 16 * thread_m_blocks) { parallel = prob_m / (16 * thread_m_blocks); prob_m = 16 * thread_m_blocks; }
int k_tiles = prob_k / 16 / thread_k_blocks; int n_tiles = prob_n / 16 / thread_n_blocks; int iters = ceildiv(k_tiles * n_tiles * parallel, gridDim.x); // Ensure that the number of tiles in each stripe is a multiple of the // groupsize; this avoids an annoying special case where a stripe starts in // the middle of group. if (group_blocks != -1) iters = (group_blocks / thread_k_blocks) * ceildiv(iters, (group_blocks / thread_k_blocks));
int slice_row = (iters * blockIdx.x) % k_tiles; int slice_col_par = (iters * blockIdx.x) / k_tiles; int slice_col = slice_col_par; int slice_iters; // number of threadblock tiles in the current slice int slice_count = 0; // total number of active threadblocks in the current slice int slice_idx; // index of threadblock in current slice; numbered bottom to // top
// We can easily implement parallel problem execution by just remapping // indices and advancing global pointers if (slice_col_par >= n_tiles) { A += (slice_col_par / n_tiles) * 16 * thread_m_blocks * prob_k / 8; C += (slice_col_par / n_tiles) * 16 * thread_m_blocks * prob_n / 8; locks += (slice_col_par / n_tiles) * n_tiles; slice_col = slice_col_par % n_tiles; }
// Compute all information about the current slice which is required for // synchronization. auto init_slice = [&]() { slice_iters = iters * (blockIdx.x + 1) - (k_tiles * slice_col_par + slice_row); if (slice_iters < 0 || slice_col_par >= n_tiles * parallel) slice_iters = 0; if (slice_iters == 0) return; if (slice_row + slice_iters > k_tiles) slice_iters = k_tiles - slice_row; slice_count = 1; slice_idx = 0; int col_first = iters * ceildiv(k_tiles * slice_col_par, iters); if (col_first <= k_tiles * (slice_col_par + 1)) { int col_off = col_first - k_tiles * slice_col_par; slice_count = ceildiv(k_tiles - col_off, iters); if (col_off > 0) slice_count++; int delta_first = iters * blockIdx.x - col_first; if (delta_first < 0 || (col_off == 0 && delta_first == 0)) slice_idx = slice_count - 1; else { slice_idx = slice_count - 1 - delta_first / iters; if (col_off > 0) slice_idx--; } } if (slice_col == n_tiles) { A += 16 * thread_m_blocks * prob_k / 8; C += 16 * thread_m_blocks * prob_n / 8; locks += n_tiles; slice_col = 0; } }; init_slice();
int a_gl_stride = prob_k / 8; // stride of the A matrix in global memory // We typically use `constexpr` to indicate that this value is a compile-time // constant constexpr int a_sh_stride = 16 * thread_k_blocks / 8; // stride of an A matrix tile in shared memory constexpr int a_gl_rd_delta_o = 16 * thread_k_blocks / 8; // delta between subsequent A tiles in global memory int a_gl_rd_delta_i = a_gl_stride * (threads / a_gl_rd_delta_o); // between subsequent accesses within a tile constexpr int a_sh_wr_delta = a_sh_stride * (threads / a_gl_rd_delta_o); // between shared memory writes constexpr int a_sh_rd_delta_o = 2 * ((threads / 32) / (thread_n_blocks / 4)); // between shared memory tile reads constexpr int a_sh_rd_delta_i = a_sh_stride * 16; // within a shared memory tile constexpr int a_sh_stage = a_sh_stride * (16 * thread_m_blocks); // overall size of a tile constexpr int a_sh_wr_iters = ceildiv(a_sh_stage, a_sh_wr_delta); // number of shared write iterations for a tile
int b_gl_stride = 16 * prob_n / 32; constexpr int b_sh_stride = 32 * thread_n_blocks / 4; int b_gl_rd_delta_o = b_gl_stride * thread_k_blocks; int b_gl_rd_delta_i = b_gl_stride * (threads / b_sh_stride); constexpr int b_sh_wr_delta = threads; constexpr int b_sh_rd_delta = threads; constexpr int b_sh_stage = b_sh_stride * thread_k_blocks; constexpr int b_sh_wr_iters = b_sh_stage / b_sh_wr_delta;
int s_gl_stride = prob_n / 8; constexpr int s_sh_stride = 16 * thread_n_blocks / 8; constexpr int s_sh_stage = s_sh_stride; int s_gl_rd_delta = s_gl_stride;
// Global A read index of current thread. int a_gl_rd = a_gl_stride * (threadIdx.x / a_gl_rd_delta_o) + (threadIdx.x % a_gl_rd_delta_o); a_gl_rd += a_gl_rd_delta_o * slice_row; // Shared write index of current thread. int a_sh_wr = a_sh_stride * (threadIdx.x / a_gl_rd_delta_o) + (threadIdx.x % a_gl_rd_delta_o); // Shared read index. int a_sh_rd = a_sh_stride * ((threadIdx.x % 32) % 16) + (threadIdx.x % 32) / 16; a_sh_rd += 2 * ((threadIdx.x / 32) / (thread_n_blocks / 4));
int b_gl_rd = b_gl_stride * (threadIdx.x / b_sh_stride) + (threadIdx.x % b_sh_stride); b_gl_rd += b_sh_stride * slice_col; b_gl_rd += b_gl_rd_delta_o * slice_row; int b_sh_wr = threadIdx.x; int b_sh_rd = threadIdx.x;
int s_gl_rd = s_gl_stride * ((thread_k_blocks * slice_row) / group_blocks) + s_sh_stride * slice_col + threadIdx.x; int s_sh_wr = threadIdx.x; int s_sh_rd; // We use a different scale layout for grouped and column-wise quantization as // we scale a `half2` tile in column-major layout in the former and in // row-major in the latter case. if (group_blocks != -1) s_sh_rd = 8 * ((threadIdx.x / 32) % (thread_n_blocks / 4)) + (threadIdx.x % 32) / 4; else s_sh_rd = 8 * ((threadIdx.x / 32) % (thread_n_blocks / 4)) + (threadIdx.x % 32) % 4;
// Precompute which thread should not read memory in which iterations; this is // needed if there are more threads than required for a certain tilesize or // when the batchsize is not a multiple of 16. bool a_sh_wr_pred[a_sh_wr_iters]; #pragma unroll for (int i = 0; i < a_sh_wr_iters; i++) a_sh_wr_pred[i] = a_sh_wr_delta * i + a_sh_wr < a_sh_stride * prob_m; bool s_sh_wr_pred = threadIdx.x < s_sh_stride;
// To ensure that writing and reading A tiles to/from shared memory, the // latter in fragment format, is fully bank conflict free, we need to use a // rather fancy XOR-based layout. The key here is that neither reads nor // writes of the 16-byte `int4` blocks of 8 consecutive threads involve the // same shared memory banks. Further, it seems (based on NSight-Compute) that // each warp must also write a consecutive memory segment? auto transform_a = [&](int i) { int row = i / a_gl_rd_delta_o; return a_gl_rd_delta_o * row + (i % a_gl_rd_delta_o) ^ row; }; // Since the computation of this remapping is non-trivial and, due to our main // loop unrolls, all shared memory accesses are static, we simply precompute // both transformed reads and writes. int a_sh_wr_trans[a_sh_wr_iters]; #pragma unroll for (int i = 0; i < a_sh_wr_iters; i++) a_sh_wr_trans[i] = transform_a(a_sh_wr_delta * i + a_sh_wr); int a_sh_rd_trans[b_sh_wr_iters][thread_m_blocks]; #pragma unroll for (int i = 0; i < b_sh_wr_iters; i++) { #pragma unroll for (int j = 0; j < thread_m_blocks; j++) a_sh_rd_trans[i][j] = transform_a(a_sh_rd_delta_o * i + a_sh_rd_delta_i * j + a_sh_rd); }
// Since B-accesses have non-constant stride they have to be computed at // runtime; we break dependencies between subsequent accesses with a tile by // maintining multiple pointers (we have enough registers), a tiny // optimization. const int4* B_ptr[b_sh_wr_iters]; #pragma unroll for (int i = 0; i < b_sh_wr_iters; i++) B_ptr[i] = B + b_gl_rd_delta_i * i + b_gl_rd;
extern __shared__ int4 sh[]; // Shared memory storage for global fetch pipelines. int4* sh_a = sh; int4* sh_b = sh_a + (stages * a_sh_stage); int4* sh_s = sh_b + (stages * b_sh_stage); // Register storage for double buffer of shared memory reads. FragA frag_a[2][thread_m_blocks]; I4 frag_b_quant[2]; FragC frag_c[thread_m_blocks][4][2]; FragS frag_s[2][4];
// Zero accumulators. auto zero_accums = [&]() { #pragma unroll for (int i = 0; i < thread_m_blocks * 4 * 2 * 4; i++) reinterpret_cast<float*>(frag_c)[i] = 0; };
// Asynchronously fetch the next A, B and s tile from global to the next // shared memory pipeline location. auto fetch_to_shared = [&](int pipe, int a_off, bool pred = true) { if (pred) { int4* sh_a_stage = sh_a + a_sh_stage * pipe; #pragma unroll for (int i = 0; i < a_sh_wr_iters; i++) { cp_async4_pred( &sh_a_stage[a_sh_wr_trans[i]], &A[a_gl_rd_delta_i * i + a_gl_rd + a_gl_rd_delta_o * a_off], a_sh_wr_pred[i]); } int4* sh_b_stage = sh_b + b_sh_stage * pipe; #pragma unroll for (int i = 0; i < b_sh_wr_iters; i++) { cp_async4(&sh_b_stage[b_sh_wr_delta * i + b_sh_wr], B_ptr[i]); B_ptr[i] += b_gl_rd_delta_o; } // Only fetch scales if this tile starts a new group if constexpr (group_blocks != -1) { // This assumes group_blocks >= thread_k_blocks // and would need to be modified to support smaller groups. static_assert(group_blocks >= thread_k_blocks); if (pipe % (group_blocks / thread_k_blocks) == 0) { int4* sh_s_stage = sh_s + s_sh_stage * pipe; if (s_sh_wr_pred) cp_async4(&sh_s_stage[s_sh_wr], &s[s_gl_rd]); s_gl_rd += s_gl_rd_delta; } } } // Insert a fence even when we are winding down the pipeline to ensure that // waiting is also correct at this point. cp_async_fence(); };
// Wait until the next thread tile has been loaded to shared memory. auto wait_for_stage = [&]() { // We only have `stages - 2` active fetches since we are double buffering // and can only issue the next fetch when it is guaranteed that the previous // shared memory load is fully complete (as it may otherwise be // overwritten). cp_async_wait<stages - 2>(); __syncthreads(); };
// Load the next sub-tile from the current location in the shared memory pipe // into the current register buffer. auto fetch_to_registers = [&](int k, int pipe) { // It may seem inefficient that we reload the groups for every sub-tile; // however, this does not seem to be a significant bottleneck, while some // theoretically better attempts have lead to bad instruction ordering by // the compiler and correspondingly a noticeable drop in performance. if constexpr (group_blocks != -1) { // This assumes group_blocks >= thread_k_blocks // and would need to be modified to support smaller groups. static_assert(group_blocks >= thread_k_blocks); int4* sh_s_stage = sh_s + s_sh_stage * ((group_blocks / thread_k_blocks) * (pipe / (group_blocks / thread_k_blocks))); reinterpret_cast<int4*>(&frag_s[k % 2])[0] = sh_s_stage[s_sh_rd]; } int4* sh_a_stage = sh_a + a_sh_stage * pipe; #pragma unroll for (int i = 0; i < thread_m_blocks; i++) ldsm4(frag_a[k % 2][i], &sh_a_stage[a_sh_rd_trans[k % b_sh_wr_iters][i]]); int4* sh_b_stage = sh_b + b_sh_stage * pipe; frag_b_quant[k % 2] = *reinterpret_cast<I4*>( &sh_b_stage[b_sh_rd_delta * (k % b_sh_wr_iters) + b_sh_rd]); };
// Execute the actual tensor core matmul of a sub-tile. auto matmul = [&](int k) { // We have the m dimension as the inner loop in order to encourage overlapping // dequantization and matmul operations. #pragma unroll for (int j = 0; j < 4; j++) { int b_quant = frag_b_quant[k % 2][j]; int b_quant_shift = b_quant >> 8; FragB frag_b0 = dequant(b_quant); // If there are no groups, we can just scale the final output once and can // avoid doing so for each weight. if (group_blocks != -1) scale(frag_b0, frag_s[k % 2][j], 0); FragB frag_b1 = dequant(b_quant_shift); if (group_blocks != -1) scale(frag_b1, frag_s[k % 2][j], 1); #pragma unroll for (int i = 0; i < thread_m_blocks; i++) { mma(frag_a[k % 2][i], frag_b0, frag_c[i][j][0]); mma(frag_a[k % 2][i], frag_b1, frag_c[i][j][1]); } } };
// Since we slice across the k dimension of a tile in order to increase the // number of warps while keeping the n dimension of a tile reasonable, we have // multiple warps that accumulate their partial sums of the same output // location; which we have to reduce over in the end. We do in shared memory. auto thread_block_reduce = [&]() { constexpr int red_off = threads / b_sh_stride / 2; if (red_off >= 1) { int red_idx = threadIdx.x / b_sh_stride; constexpr int red_sh_stride = b_sh_stride * 4 * 2; constexpr int red_sh_delta = b_sh_stride; int red_sh_rd = red_sh_stride * (threadIdx.x / b_sh_stride) + (threadIdx.x % b_sh_stride);
// Parallel logarithmic shared memory reduction. We make sure to avoid any // unnecessary read or write iterations, e.g., for two warps we write only // once by warp 1 and read only once by warp 0.
#pragma unroll for (int m_block = 0; m_block < thread_m_blocks; m_block++) { #pragma unroll for (int i = red_off; i > 0; i /= 2) { if (i <= red_idx && red_idx < 2 * i) { #pragma unroll for (int j = 0; j < 4 * 2; j++) { int red_sh_wr = red_sh_delta * j + (red_sh_rd - red_sh_stride * i); if (i < red_off) { float* c_rd = reinterpret_cast<float*>(&sh[red_sh_delta * j + red_sh_rd]); float* c_wr = reinterpret_cast<float*>(&sh[red_sh_wr]); #pragma unroll for (int k = 0; k < 4; k++) reinterpret_cast<FragC*>(frag_c)[4 * 2 * m_block + j][k] += c_rd[k] + c_wr[k]; } sh[red_sh_wr] = reinterpret_cast<int4*>(&frag_c)[4 * 2 * m_block + j]; } } __syncthreads(); } if (red_idx == 0) { #pragma unroll for (int i = 0; i < 4 * 2; i++) { float* c_rd = reinterpret_cast<float*>(&sh[red_sh_delta * i + red_sh_rd]); #pragma unroll for (int j = 0; j < 4; j++) reinterpret_cast<FragC*>(frag_c)[4 * 2 * m_block + i][j] += c_rd[j]; } } __syncthreads(); } } };
// Since multiple threadblocks may process parts of the same column slice, we // finally have to globally reduce over the results. As the striped // partitioning minimizes the number of such reductions and our outputs are // usually rather small, we perform this reduction serially in L2 cache. auto global_reduce = [&](bool first = false, bool last = false) { // We are very careful here to reduce directly in the output buffer to // maximize L2 cache utilization in this step. To do this, we write out // results in FP16 (but still reduce with FP32 compute). constexpr int active_threads = 32 * thread_n_blocks / 4; if (threadIdx.x < active_threads) { int c_gl_stride = prob_n / 8; int c_gl_wr_delta_o = 8 * c_gl_stride; int c_gl_wr_delta_i = 4 * (active_threads / 32); int c_gl_wr = c_gl_stride * ((threadIdx.x % 32) / 4) + 4 * (threadIdx.x / 32) + threadIdx.x % 4; c_gl_wr += (2 * thread_n_blocks) * slice_col; constexpr int c_sh_wr_delta = active_threads; int c_sh_wr = threadIdx.x;
int row = (threadIdx.x % 32) / 4;
if (!first) { // Interestingly, doing direct global accesses here really seems to mess up // the compiler and lead to slowdowns, hence we also use async-copies even // though these fetches are not actually asynchronous. #pragma unroll for (int i = 0; i < thread_m_blocks * 4; i++) { cp_async4_pred( &sh[c_sh_wr + c_sh_wr_delta * i], &C[c_gl_wr + c_gl_wr_delta_o * (i / 2) + c_gl_wr_delta_i * (i % 2)], i < (thread_m_blocks - 1) * 4 || 8 * (i / 2) + row < prob_m); } cp_async_fence(); cp_async_wait<0>(); }
#pragma unroll for (int i = 0; i < thread_m_blocks * 4; i++) { if (i < (thread_m_blocks - 1) * 4 || 8 * (i / 2) + row < prob_m) { if (!first) { int4 c_red = sh[c_sh_wr + i * c_sh_wr_delta]; #pragma unroll for (int j = 0; j < 2 * 4; j++) { reinterpret_cast<float*>( &frag_c)[4 * 2 * 4 * (i / 4) + 4 * j + (i % 4)] += __half2float(reinterpret_cast<__half*>(&c_red)[j]); } } if (!last) { int4 c; #pragma unroll for (int j = 0; j < 2 * 4; j++) { reinterpret_cast<__half*>(&c)[j] = __float2half(reinterpret_cast<float*>( &frag_c)[4 * 2 * 4 * (i / 4) + 4 * j + (i % 4)]); } C[c_gl_wr + c_gl_wr_delta_o * (i / 2) + c_gl_wr_delta_i * (i % 2)] = c; } } } } };
// Write out the reduce final result in the correct layout. We only actually // reshuffle matrix fragments in this step, the reduction above is performed // in fragment layout. auto write_result = [&]() { int c_gl_stride = prob_n / 8; constexpr int c_sh_stride = 2 * thread_n_blocks + 1; int c_gl_wr_delta = c_gl_stride * (threads / (2 * thread_n_blocks)); constexpr int c_sh_rd_delta = c_sh_stride * (threads / (2 * thread_n_blocks));
int c_gl_wr = c_gl_stride * (threadIdx.x / (2 * thread_n_blocks)) + (threadIdx.x % (2 * thread_n_blocks)); c_gl_wr += (2 * thread_n_blocks) * slice_col; int c_sh_wr = (4 * c_sh_stride) * ((threadIdx.x % 32) / 4) + (threadIdx.x % 32) % 4; c_sh_wr += 32 * (threadIdx.x / 32); int c_sh_rd = c_sh_stride * (threadIdx.x / (2 * thread_n_blocks)) + (threadIdx.x % (2 * thread_n_blocks));
int c_gl_wr_end = c_gl_stride * prob_m;
// We first reorder in shared memory to guarantee the most efficient final // global write patterns auto write = [&](int idx, float c0, float c1, FragS& s) { half2 res = __halves2half2(__float2half(c0), __float2half(c1)); if (group_blocks == -1) // for per-column quantization we finally apply the scale here res = __hmul2(res, s[0]); ((half2*)sh)[idx] = res; }; if (threadIdx.x / 32 < thread_n_blocks / 4) { #pragma unroll for (int i = 0; i < thread_m_blocks; i++) { #pragma unroll for (int j = 0; j < 4; j++) { int wr = c_sh_wr + 8 * j; write(wr + (4 * c_sh_stride) * 0 + 0, frag_c[i][j][0][0], frag_c[i][j][0][1], frag_s[j / 2][2 * (j % 2) + 0]); write(wr + (4 * c_sh_stride) * 8 + 0, frag_c[i][j][0][2], frag_c[i][j][0][3], frag_s[j / 2][2 * (j % 2) + 0]); write(wr + (4 * c_sh_stride) * 0 + 4, frag_c[i][j][1][0], frag_c[i][j][1][1], frag_s[j / 2][2 * (j % 2) + 1]); write(wr + (4 * c_sh_stride) * 8 + 4, frag_c[i][j][1][2], frag_c[i][j][1][3], frag_s[j / 2][2 * (j % 2) + 1]); } c_sh_wr += 16 * (4 * c_sh_stride); } } __syncthreads();
#pragma unroll for (int i = 0; i < ceildiv(16 * thread_m_blocks, threads / (2 * thread_n_blocks)); i++) { if (c_gl_wr < c_gl_wr_end) { C[c_gl_wr] = sh[c_sh_rd]; c_gl_wr += c_gl_wr_delta; c_sh_rd += c_sh_rd_delta; } } };
// Start global fetch and register load pipelines. auto start_pipes = [&]() { #pragma unroll for (int i = 0; i < stages - 1; i++) fetch_to_shared(i, i, i < slice_iters); zero_accums(); wait_for_stage(); fetch_to_registers(0, 0); a_gl_rd += a_gl_rd_delta_o * (stages - 1); }; start_pipes();
// Main loop. while (slice_iters) { // We unroll over both the global fetch and the register load pipeline to // ensure all shared memory accesses are static. Note that both pipelines have // even length meaning that the next iteration will always start at index 0. #pragma unroll for (int pipe = 0; pipe < stages;) { #pragma unroll for (int k = 0; k < b_sh_wr_iters; k++) { fetch_to_registers(k + 1, pipe % stages); if (k == b_sh_wr_iters - 2) { fetch_to_shared((pipe + stages - 1) % stages, pipe, slice_iters >= stages); pipe++; wait_for_stage(); } matmul(k); } slice_iters--; if (slice_iters == 0) break; } a_gl_rd += a_gl_rd_delta_o * stages;
// Process results and, if necessary, proceed to the next column slice. // While this pattern may not be the most readable, other ways of writing // the loop seemed to noticeably worse performance after compilation. if (slice_iters == 0) { cp_async_wait<0>(); bool last = slice_idx == slice_count - 1; // For per-column scales, we only fetch them here in the final step before // write-out if (group_blocks == -1 && last) { if (s_sh_wr_pred) cp_async4(&sh_s[s_sh_wr], &s[s_gl_rd]); cp_async_fence(); } thread_block_reduce(); if (group_blocks == -1 && last) { cp_async_wait<0>(); __syncthreads(); if (threadIdx.x / 32 < thread_n_blocks / 4) { reinterpret_cast<int4*>(&frag_s)[0] = sh_s[s_sh_rd + 0]; reinterpret_cast<int4*>(&frag_s)[1] = sh_s[s_sh_rd + 4]; } } if (slice_count > 1) { // only globally reduce if there is more than one // block in a slice barrier_acquire(&locks[slice_col], slice_idx); global_reduce(slice_idx == 0, last); barrier_release(&locks[slice_col], last); } if (last) // only the last block in a slice actually writes the result write_result(); slice_row = 0; slice_col_par++; slice_col++; init_slice(); if (slice_iters) { a_gl_rd = a_gl_stride * (threadIdx.x / a_gl_rd_delta_o) + (threadIdx.x % a_gl_rd_delta_o); #pragma unroll for (int i = 0; i < b_sh_wr_iters; i++) B_ptr[i] += b_sh_stride - b_gl_rd_delta_o * k_tiles; if (slice_col == 0) { #pragma unroll for (int i = 0; i < b_sh_wr_iters; i++) B_ptr[i] -= b_gl_stride; } s_gl_rd = s_sh_stride * slice_col + threadIdx.x; start_pipes(); } } }}
#else
template <const int threads, // number of threads in a threadblock const int thread_m_blocks, // number of 16x16 blocks in the m // dimension (batchsize) of the // threadblock const int thread_n_blocks, // same for n dimension (output) const int thread_k_blocks, // same for k dimension (reduction) const int stages, // number of stages for the async global->shared // fetch pipeline const int group_blocks = -1 // number of consecutive 16x16 blocks // with a separate quantization scale >__global__ void Marlin( const int4* __restrict__ A, // fp16 input matrix of shape mxk const int4* __restrict__ B, // 4bit quantized weight matrix of shape kxn int4* __restrict__ C, // fp16 output buffer of shape mxn const int4* __restrict__ s, // fp16 quantization scales of shape // (k/groupsize)xn int prob_m, // batch dimension m int prob_n, // output dimension n int prob_k, // reduction dimension k int* locks // extra global storage for barrier synchronization) { // Marlin is not implemented yet for SM < 8.0 assert(false); return;}
#endif
// 8 warps are a good choice since every SM has 4 schedulers and having more// than 1 warp per schedule allows some more latency hiding. At the same time,// we want relatively few warps to have many registers per warp and small tiles.const int USER_THREADS = 256; // Note: This is only used with user-provided thread_k/nconst int STAGES = 4; // 4 pipeline stages fit into shared memoryconst int SHARED_MEM = 96 * 1024; // max shared memory on compute capability 8.6 (< 8.0)
static constexpr int min_thread_n = 64;static constexpr int min_thread_k = 64;
static constexpr int tile_size = 16;static constexpr int max_par = 16;
static constexpr int pack_factor_4bit = 8; // We have 8 4-bit vals inside a 32 bit
#define __CALL_IF(THREAD_M_BLOCKS, THREAD_N_BLOCKS, THREAD_K_BLOCKS, \ GROUP_BLOCKS, NUM_THREADS) \ else if (thread_m_blocks == THREAD_M_BLOCKS && \ thread_n_blocks == THREAD_N_BLOCKS && \ thread_k_blocks == THREAD_K_BLOCKS && \ group_blocks == GROUP_BLOCKS && num_threads == NUM_THREADS) { \ cudaFuncSetAttribute(Marlin<NUM_THREADS, THREAD_M_BLOCKS, THREAD_N_BLOCKS, \ THREAD_K_BLOCKS, STAGES, GROUP_BLOCKS>, \ cudaFuncAttributeMaxDynamicSharedMemorySize, \ SHARED_MEM); \ Marlin<NUM_THREADS, THREAD_M_BLOCKS, THREAD_N_BLOCKS, THREAD_K_BLOCKS, \ STAGES, GROUP_BLOCKS><<<blocks, NUM_THREADS, SHARED_MEM, stream>>>( \ A_ptr, B_ptr, C_ptr, s_ptr, prob_m, prob_n, prob_k, locks); \ }
typedef struct { int thread_k; int thread_n; int num_threads;} thread_config_t;
thread_config_t small_batch_thread_configs[] = { // Ordered by priority
// thread_k, thread_n, num_threads {128, 128, 256}, // Default {128, 64, 128}, // Reduce N 2X, same K {64, 256, 256}, // Reduce K 2X, increase N 2X {64, 128, 128}, // Reduce K 2X, same N};
thread_config_t large_batch_thread_configs[] = { // Ordered by priority
// thread_k, thread_n, num_threads {64, 256, 256}, // Default {128, 128, 256}, // Reduce N 2X, increase K 2X {64, 128, 128}, // Reduce N 2X, same K {128, 64, 128}, // Reduce N 4X, increase K 2X};
bool is_valid_config(thread_config_t const& th_config, int prob_m, int prob_n, int prob_k) { // Sanity if (th_config.thread_k == -1 || th_config.thread_n == -1 || th_config.num_threads == -1) { return false; }
// Verify K/N are divisible by thread K/N if (prob_k % th_config.thread_k != 0 || prob_n % th_config.thread_n != 0) { return false; }
// thread_k can be only 128 or 64 (because it must be less than groupsize // which is 128) if (th_config.thread_k != 128 && th_config.thread_k != 64) { return false; }
// Verify min for thread K/N if (th_config.thread_n < min_thread_n || th_config.thread_k < min_thread_k) { return false; }
// num_threads must be at least 128 (= 4 warps) if (th_config.num_threads < 128) { return false; }
return true;}
thread_config_t determine_thread_config(int prob_m, int prob_n, int prob_k) { if (prob_m <= 16) { for (auto th_config : small_batch_thread_configs) { if (is_valid_config(th_config, prob_m, prob_n, prob_k)) { return th_config; } }
} else { for (auto th_config : large_batch_thread_configs) { if (is_valid_config(th_config, prob_m, prob_n, prob_k)) { return th_config; } } }
return thread_config_t{-1, -1, -1};}
#define CALL_IF(N_BLOCKS, K_BLOCKS, NUM_THREADS) \ __CALL_IF(1, N_BLOCKS, K_BLOCKS, -1, NUM_THREADS) \ __CALL_IF(1, N_BLOCKS, K_BLOCKS, 8, NUM_THREADS) \ __CALL_IF(1, N_BLOCKS, K_BLOCKS, -1, NUM_THREADS) \ __CALL_IF(1, N_BLOCKS, K_BLOCKS, 8, NUM_THREADS) \ __CALL_IF(2, N_BLOCKS, K_BLOCKS, -1, NUM_THREADS) \ __CALL_IF(2, N_BLOCKS, K_BLOCKS, 8, NUM_THREADS) \ __CALL_IF(3, N_BLOCKS, K_BLOCKS, -1, NUM_THREADS) \ __CALL_IF(3, N_BLOCKS, K_BLOCKS, 8, NUM_THREADS) \ __CALL_IF(4, N_BLOCKS, K_BLOCKS, -1, NUM_THREADS) \ __CALL_IF(4, N_BLOCKS, K_BLOCKS, 8, NUM_THREADS)
void marlin_cuda(const void* A, const void* B, void* C, void* s, int prob_m, int prob_n, int prob_k, void* workspace, int groupsize = -1, int dev = 0, cudaStream_t stream = 0, int thread_k = -1, int thread_n = -1, int sms = -1, int max_par = 16) { int tot_m = prob_m; int tot_m_blocks = ceildiv(tot_m, 16); int pad = 16 * tot_m_blocks - tot_m;
if (sms == -1) cudaDeviceGetAttribute(&sms, cudaDevAttrMultiProcessorCount, dev);
// Set thread config thread_config_t th_config; if (thread_k != -1 && thread_n != -1) { // User-defined config th_config = thread_config_t{thread_k, thread_n, USER_THREADS}; } else { // Auto config th_config = determine_thread_config(prob_m, prob_n, prob_k); }
if (!is_valid_config(th_config, prob_m, prob_n, prob_k)) { throw std::runtime_error( "Invalid thread config: thread_k = " + str(th_config.thread_k) + ", thread_n = " + str(th_config.thread_n) + ", num_threads = " + str(th_config.num_threads) + " for MKN = [" + str(prob_m) + ", " + str(prob_k) + ", " + str(prob_n) + "]"); }
// Uncomment for debug // std::cout << "Using thread_config: thread_k = " + str(th_config.thread_k) + // ", thread_n = " + str(th_config.thread_n) + // ", num_threads = " + str(th_config.num_threads) + " for // MKN = [" + str(prob_m) + // ", " + str(prob_k) + ", " + str(prob_n) + "]\n";
int num_threads = th_config.num_threads; thread_k = th_config.thread_k; thread_n = th_config.thread_n;
int thread_k_blocks = thread_k / 16; int thread_n_blocks = thread_n / 16; int group_blocks = (groupsize == -1) ? -1 : groupsize / 16; int blocks = sms;
if (prob_m == 0 || prob_n == 0 || prob_k == 0) { return; }
TORCH_CHECK(prob_n % thread_n == 0, "prob_n = ", prob_n, " is not divisible by thread_n = ", thread_n); TORCH_CHECK(prob_k % thread_k == 0, "prob_k = ", prob_k, " is not divisible by thread_k = ", thread_k); if (group_blocks != -1) { TORCH_CHECK(prob_k % group_blocks == 0, "prob_k = ", prob_k, " is not divisible by group_blocks = ", group_blocks); }
const int4* A_ptr = (const int4*)A; const int4* B_ptr = (const int4*)B; int4* C_ptr = (int4*)C; const int4* s_ptr = (const int4*)s;
int* locks = (int*)workspace;
for (int i = 0; i < tot_m_blocks; i += 4) { int thread_m_blocks = tot_m_blocks - i; prob_m = tot_m - 16 * i; int par = 1; if (thread_m_blocks > 4) { // Note that parallel > 1 currently only works for inputs without any // padding par = (16 * thread_m_blocks - pad) / 64; if (par > max_par) par = max_par; prob_m = 64 * par; i += 4 * (par - 1); thread_m_blocks = 4; }
// For compilation speed, we only define the kernel configurations that have // seemed useful (in terms of performance) in our testing, however many more // are, in principle, possible. if (false) { } CALL_IF(8, 8, 256) CALL_IF(16, 4, 256) CALL_IF(8, 4, 128) CALL_IF(4, 8, 128) else { throw std::runtime_error("Unsupported shapes: MKN = [" + str(prob_m) + ", " + str(prob_k) + ", " + str(prob_n) + "]" + ", groupsize = " + str(groupsize) + ", thread_m_blocks = " + str(thread_m_blocks) + ", thread_n_blocks = " + str(thread_n_blocks) + ", thread_k_blocks = " + str(thread_k_blocks)); }
A_ptr += 16 * thread_m_blocks * (prob_k / 8) * par; C_ptr += 16 * thread_m_blocks * (prob_n / 8) * par; }}
} // namespace marlin_dense
torch::Tensor marlin_gemm(torch::Tensor& a, torch::Tensor& b_q_weight, torch::Tensor& b_scales, torch::Tensor& workspace, int64_t size_m, int64_t size_n, int64_t size_k) { // Verify M TORCH_CHECK(size_m == a.size(0), "Shape mismatch: a.size(0) = " + str(a.size(0)) + ", size_m = " + str(size_m));
// Verify K TORCH_CHECK(size_k == a.size(1), "Shape mismatch: a.size(1) = " + str(a.size(1)) + ", size_k = " + str(size_k)); TORCH_CHECK(size_k % marlin_dense::tile_size == 0, "size_k = " + str(size_k) + " is not divisible by tile_size = " + str(marlin_dense::tile_size)); TORCH_CHECK((size_k / marlin_dense::tile_size) == b_q_weight.size(0), "Shape mismatch: b_q_weight.size(0) = " + str(b_q_weight.size(0)) + ", size_k = " + str(size_k) + ", tile_size = " + str(marlin_dense::tile_size));
// Verify N TORCH_CHECK(b_scales.size(1) == size_n, "b_scales.size(1) = " + str(b_scales.size(1)) + ", size_n = " + str(size_n)); TORCH_CHECK( b_q_weight.size(1) % marlin_dense::tile_size == 0, "b_q_weight.size(1) = " + str(b_q_weight.size(1)) + " is not divisible by tile_size = " + str(marlin_dense::tile_size));
int actual_size_n = (b_q_weight.size(1) / marlin_dense::tile_size) * marlin_dense::pack_factor_4bit; TORCH_CHECK( size_n == actual_size_n, "size_n = " + str(size_n) + ", actual_size_n = " + str(actual_size_n));
// Verify A device and strides TORCH_CHECK(a.device().is_cuda(), "A is not on GPU"); TORCH_CHECK(a.is_contiguous(), "A is not contiguous");
// Verify B device and strides TORCH_CHECK(b_q_weight.device().is_cuda(), "b_q_weight is not on GPU"); TORCH_CHECK(b_q_weight.is_contiguous(), "b_q_weight is not contiguous");
// Verify scales device and strides TORCH_CHECK(b_scales.device().is_cuda(), "b_scales is not on GPU"); TORCH_CHECK(b_scales.is_contiguous(), "b_scales is not contiguous");
// Alloc C matrix const at::cuda::OptionalCUDAGuard device_guard(device_of(a)); auto options = torch::TensorOptions().dtype(a.dtype()).device(a.device()); torch::Tensor c = torch::empty({size_m, size_n}, options);
// thread_k: `k` size of a thread_tile in `weights` (can usually be left as // auto -1) int thread_k = -1; // thread_n: `n` size of a thread_tile in `weights` (can usually be left as // auto -1) int thread_n = -1; // sms: number of SMs to use for the kernel (can usually be left as auto -1) int sms = -1;
// Detect groupsize if (b_scales.size(0) != 1) { TORCH_CHECK(size_k % b_scales.size(0) == 0, "size_k = " + str(size_k) + ", is not divisible by b_scales.size(0) = " + str(b_scales.size(0))); } int groupsize = b_scales.size(0) == 1 ? -1 : size_k / b_scales.size(0);
// Verify groupsize TORCH_CHECK(groupsize == -1 || groupsize == 128, "Unexpected groupsize = " + str(groupsize));
// Verify workspace size TORCH_CHECK(size_n % marlin_dense::min_thread_n == 0, "size_n = " + str(size_n) + ", is not divisible by min_thread_n = " + str(marlin_dense::min_thread_n)); int min_workspace_size = (size_n / marlin_dense::min_thread_n) * marlin_dense::max_par; TORCH_CHECK(workspace.numel() >= min_workspace_size, "workspace.numel = " + str(workspace.numel()) + " is below min_workspace_size = " + str(min_workspace_size));
int dev = a.get_device(); marlin_dense::marlin_cuda(a.data_ptr(), b_q_weight.data_ptr(), c.data_ptr(), b_scales.data_ptr(), size_m, size_n, size_k, workspace.data_ptr(), groupsize, dev, at::cuda::getCurrentCUDAStream(dev), thread_k, thread_n, sms, marlin_dense::max_par);
return c;}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243/* * Adapted from * https://github.com/IST-DASLab/marlin/blob/master/marlin/marlin_cuda_kernel.cu * https://github.com/IST-DASLab/marlin/blob/master/marlin/marlin_cuda.cpp * Modified by HandH1998 * Copyright (C) 2024 HandH1998 * Copyright (C) Marlin.2024 Elias Frantar * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
#include <torch/all.h>
#include <ATen/cuda/CUDAContext.h>#include <c10/cuda/CUDAGuard.h>#include <cuda.h>#include <cuda_fp16.h>#include <cuda_runtime.h>
#include <iostream>
#include "../dense/common/base.h"
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800 #include "../dense/common/mem.h"#endif
template <typename T>inline std::string str(T x) { return std::to_string(x);}
namespace {
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800
using I4 = Vec<int, 4>;// Matrix fragments for tensor core instructions; their precise layout is// documented here:// https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#matrix-fragments-for-mma-m16n8k16-with-integer-typeusing FragA = Vec<uint32_t, 2>;using FragB = Vec<uint32_t, 1>;using FragC = Vec<int, 4>;using FragS_GROUP = Vec<half2, 1>; // weight per-group quantization scalesusing FragS_CHANNEL = Vec<float, 2>; // weight per-channel quantization scales or activaton // per-token quantization scales
// NOTE(HandH1998): cp.async.cg only support BYTES = 16, however,// cp.async.ca can support BYTES = 4, 8, 16;// as s_tok's shape is equal to prob_m, we need set s_tok to float type,// and cp_size = 1 float, i.e., 4 BYTES// Asynchronous global->shared copy for activation quantizaton scales s_tok__device__ inline void cp_async1(void* smem_ptr, const void* glob_ptr) { const int BYTES = 4; uint32_t smem = static_cast<uint32_t>(__cvta_generic_to_shared(smem_ptr)); asm volatile( "{\n" " cp.async.ca.shared.global [%0], [%1], %2;\n" "}\n" ::"r"(smem), "l"(glob_ptr), "n"(BYTES));}
// m16n8k16 tensor core mma instruction with int8 inputs and int32// output/accumulation.__device__ inline void mma(const FragA& a_frag, const FragB& frag_b, FragC& frag_c) { const uint32_t* a = reinterpret_cast<const uint32_t*>(&a_frag); const uint32_t* b = reinterpret_cast<const uint32_t*>(&frag_b); int* c = reinterpret_cast<int*>(&frag_c); asm volatile( "mma.sync.aligned.m16n8k16.row.col.satfinite.s32.s8.s8.s32 " "{%0,%1,%2,%3}, {%4,%5}, {%6}, {%7,%8,%9,%10};\n" : "=r"(c[0]), "=r"(c[1]), "=r"(c[2]), "=r"(c[3]) : "r"(a[0]), "r"(a[1]), "r"(b[0]), "r"(c[0]), "r"(c[1]), "r"(c[2]), "r"(c[3]));}
// Instruction for loading a full 16x16 matrix fragment of operand A from shared// memory, directly in int8 tensor core layout.__device__ inline void ldsm4(FragA& frag_a, const void* smem_ptr) { uint32_t* a = reinterpret_cast<uint32_t*>(&frag_a); uint32_t smem = static_cast<uint32_t>(__cvta_generic_to_shared(smem_ptr)); asm volatile("ldmatrix.sync.aligned.m8n8.x2.shared.b16 {%0,%1}, [%2];\n" : "=r"(a[0]), "=r"(a[1]) : "r"(smem));}
inline __device__ half2 float2_to_half2(float2 f) { uint32_t res; // NOTE(HandH1998): h0,h1 should be uint16_t, not half uint16_t h0, h1; asm volatile("cvt.rn.f16.f32 %0, %1;\n" : "=h"(h0) : "f"(f.x)); asm volatile("cvt.rn.f16.f32 %0, %1;\n" : "=h"(h1) : "f"(f.y)); asm volatile("mov.b32 %0, {%1, %2};\n" : "=r"(res) : "h"(h0), "h"(h1)); return reinterpret_cast<half2&>(res);}
inline __device__ float int32_to_float(int h) { float res; asm volatile("cvt.rn.f32.s32 %0, %1;\n" : "=f"(res) : "r"(h)); return res;}
// Lookup-table based 3-input logical operation; explicitly used for// dequantization as the compiler does not seem to automatically recognize it in// all cases.template <int lut>__device__ inline int lop3(int a, int b, int c) { int res; asm volatile("lop3.b32 %0, %1, %2, %3, %4;\n" : "=r"(res) : "r"(a), "r"(b), "r"(c), "n"(lut)); return res;}
// Efficiently dequantize an int32 value into a full B-fragment of 4 int8 values// for weight per channel dequant.__device__ inline FragB dequant_per_channel(int q) { static constexpr int MASK = 0xf0f0f0f0; FragB frag_b; frag_b[0] = (q & MASK); return frag_b;}
// Efficiently dequantize an int32 value into a full B-fragment of 4 int8 values// for weight per group dequant.__device__ inline FragB dequant_per_group(int q, FragS_GROUP& frag_s, int i) { static constexpr uint32_t LO = 0x000f000f; static constexpr uint32_t HI = 0x00f000f0; static constexpr uint32_t EX = 0x64006400; // Guarantee that the `(a & b) | c` operations are LOP3s. uint32_t t0 = lop3<(0xf0 & 0xcc) | 0xaa>(q, LO, EX); uint32_t t1 = lop3<(0xf0 & 0xcc) | 0xaa>(q, HI, EX); // We want signed int4 outputs, hence we fuse the `-8` symmetric zero point // directly into `SUB` and `ADD`. static constexpr uint32_t SUB = 0x64086408; static constexpr uint32_t MUL = 0x2c002c00; static constexpr uint32_t ADD = 0xd480d480; *reinterpret_cast<half2*>(&t0) = __hsub2( *reinterpret_cast<half2*>(&t0), *reinterpret_cast<const half2*>(&SUB)); *reinterpret_cast<half2*>(&t1) = __hfma2( *reinterpret_cast<half2*>(&t1), *reinterpret_cast<const half2*>(&MUL), *reinterpret_cast<const half2*>(&ADD));
uint16_t s = reinterpret_cast<uint16_t*>(&frag_s)[i]; uint32_t double_s; // pack 2xfp16 to half2 asm volatile("mov.b32 %0, {%1, %2};\n" : "=r"(double_s) : "h"(s), "h"(s)); // dequant and convert 4 half to 4 uint8 (be placed at the low 8 bits of 4 // half, respectively) static constexpr uint32_t MAGIC_NUM = 0x64806480; *reinterpret_cast<half2*>(&t0) = __hfma2( *reinterpret_cast<half2*>(&t0), *reinterpret_cast<half2*>(&double_s), *reinterpret_cast<const half2*>(&MAGIC_NUM)); *reinterpret_cast<half2*>(&t1) = __hfma2( *reinterpret_cast<half2*>(&t1), *reinterpret_cast<half2*>(&double_s), *reinterpret_cast<const half2*>(&MAGIC_NUM)); // take out the 4 uint8 from 4 half, then convert them to 4 int8 and pack 4 // int8 into 1 uint32 FragB frag_b; uint32_t uint8s; static constexpr uint32_t MASK_0246 = 0x6420; static constexpr uint32_t UINT8s_TO_INT8s_MASK = 0x80808080; asm volatile("prmt.b32 %0,%1,%2,%3;\n" : "=r"(uint8s) : "r"(t0), "r"(t1), "n"(MASK_0246)); frag_b[0] = (uint8s ^ UINT8s_TO_INT8s_MASK); return frag_b;}
template <const int threads, // number of threads in a threadblock const int thread_m_blocks, // number of 16x16 blocks in the m // dimension (batchsize) of the // threadblock const int thread_n_blocks, // same for n dimension (output) const int thread_k_blocks, // same for k dimension (reduction) const int stages, // number of stages for the async global->shared // fetch pipeline const int group_blocks = -1 // number of consecutive 16x16 blocks // with a separate quantization scale >__global__ void Marlin( const int4* __restrict__ A, // int8 input matrix of shape mxk const int4* __restrict__ B, // 4bit quantized weight matrix of shape kxn int4* __restrict__ C, // int32 global_reduce buffer of shape // (max_par*16*4)xn, as int8 tensor core's output is // int32 dtype int4* __restrict__ D, // fp16 output buffer of shape mxn const float* __restrict__ s_tok, // fp32 activation per-token quantization // scales of shape mx1 const int4* __restrict__ s_ch, // fp32 weight per-channel quantization // scales of shape 1xn const int4* __restrict__ s_group, // fp16 weight per-group quantization // scales of shape (k/groupsize)xn, when // group_blocks=-1, it should be nullptr int prob_m, // batch dimension m int prob_n, // output dimension n int prob_k, // reduction dimension k int* locks // extra global storage for barrier synchronization) { // Each threadblock processes one "stripe" of the B matrix with (roughly) the // same size, which might involve multiple column "slices" (of width 16 * // `thread_n_blocks`). Stripes are defined as shown in the 3x3 matrix 5 SM // example: // 0 1 3 // 0 2 3 // 1 2 4 // While this kind of partitioning makes things somewhat more complicated, it // ensures good utilization of all SMs for many kinds of shape and GPU // configurations, while requiring as few slow global cross-threadblock // reductions as possible.
// For larger GEMMs we run multiple batchsize 64 versions in parallel for a // better partitioning with less reductions int parallel = 1; if (prob_m > 16 * thread_m_blocks) { parallel = prob_m / (16 * thread_m_blocks); prob_m = 16 * thread_m_blocks; }
int k_tiles = prob_k / 16 / thread_k_blocks; int n_tiles = prob_n / 16 / thread_n_blocks; int iters = ceildiv(k_tiles * n_tiles * parallel, gridDim.x); // Ensure that the number of tiles in each stripe is a multiple of the // groupsize; this avoids an annoying special case where a stripe starts in // the middle of group. if constexpr (group_blocks != -1) iters = (group_blocks / thread_k_blocks) * ceildiv(iters, (group_blocks / thread_k_blocks));
int slice_row = (iters * blockIdx.x) % k_tiles; int slice_col_par = (iters * blockIdx.x) / k_tiles; int slice_col = slice_col_par; int slice_iters; // number of threadblock tiles in the current slice int slice_count = 0; // total number of active threadblocks in the current slice int slice_idx; // index of threadblock in current slice; numbered bottom to // top
// We can easily implement parallel problem execution by just remapping // indices and advancing global pointers if (slice_col_par >= n_tiles) { A += (slice_col_par / n_tiles) * 16 * thread_m_blocks * prob_k / 16; C += (slice_col_par / n_tiles) * 16 * thread_m_blocks * prob_n / 4; D += (slice_col_par / n_tiles) * 16 * thread_m_blocks * prob_n / 8; s_tok += (slice_col_par / n_tiles) * 16 * thread_m_blocks; locks += (slice_col_par / n_tiles) * n_tiles; slice_col = slice_col_par % n_tiles; }
// Compute all information about the current slice which is required for // synchronization. auto init_slice = [&]() { slice_iters = iters * (blockIdx.x + 1) - (k_tiles * slice_col_par + slice_row); if (slice_iters < 0 || slice_col_par >= n_tiles * parallel) slice_iters = 0; if (slice_iters == 0) return; if (slice_row + slice_iters > k_tiles) slice_iters = k_tiles - slice_row; slice_count = 1; slice_idx = 0; int col_first = iters * ceildiv(k_tiles * slice_col_par, iters); if (col_first <= k_tiles * (slice_col_par + 1)) { int col_off = col_first - k_tiles * slice_col_par; slice_count = ceildiv(k_tiles - col_off, iters); if (col_off > 0) slice_count++; int delta_first = iters * blockIdx.x - col_first; if (delta_first < 0 || (col_off == 0 && delta_first == 0)) slice_idx = slice_count - 1; else { slice_idx = slice_count - 1 - delta_first / iters; if (col_off > 0) slice_idx--; } } if (slice_col == n_tiles) { A += 16 * thread_m_blocks * prob_k / 16; C += 16 * thread_m_blocks * prob_n / 4; D += 16 * thread_m_blocks * prob_n / 8; s_tok += 16 * thread_m_blocks; locks += n_tiles; slice_col = 0; } }; init_slice();
int a_gl_stride = prob_k / 16; // stride of the A matrix in global memory // We typically use `constexpr` to indicate that this value is a compile-time // constant constexpr int a_sh_stride = 16 * thread_k_blocks / 16; // stride of an A matrix tile in shared memory constexpr int a_gl_rd_delta_o = 16 * thread_k_blocks / 16; // delta between subsequent A tiles in global memory int a_gl_rd_delta_i = a_gl_stride * (threads / a_gl_rd_delta_o); // between subsequent accesses within a tile constexpr int a_sh_wr_delta = a_sh_stride * (threads / a_gl_rd_delta_o); // between shared memory writes constexpr int a_sh_rd_delta_o = 1 * ((threads / 32) / (thread_n_blocks / 4)); // between shared memory tile reads constexpr int a_sh_rd_delta_i = a_sh_stride * 16; // within a shared memory tile constexpr int a_sh_stage = a_sh_stride * (16 * thread_m_blocks); // overall size of a tile constexpr int a_sh_wr_iters = ceildiv(a_sh_stage, a_sh_wr_delta); // number of shared write iterations for a tile
int b_gl_stride = 16 * prob_n / 32; constexpr int b_sh_stride = 32 * thread_n_blocks / 4; int b_gl_rd_delta_o = b_gl_stride * thread_k_blocks; int b_gl_rd_delta_i = b_gl_stride * (threads / b_sh_stride); constexpr int b_sh_wr_delta = threads; constexpr int b_sh_rd_delta = threads; constexpr int b_sh_stage = b_sh_stride * thread_k_blocks; constexpr int b_sh_wr_iters = b_sh_stage / b_sh_wr_delta;
constexpr int s_tok_sh_stride = 16 * thread_m_blocks;
constexpr int s_ch_sh_stride = 16 * thread_n_blocks / 4;
int s_group_gl_stride = prob_n / 8; constexpr int s_group_sh_stride = 16 * thread_n_blocks / 8; constexpr int s_group_sh_stage = s_group_sh_stride; int s_group_gl_rd_delta = s_group_gl_stride;
// Global A read index of current thread. int a_gl_rd = a_gl_stride * (threadIdx.x / a_gl_rd_delta_o) + (threadIdx.x % a_gl_rd_delta_o); a_gl_rd += a_gl_rd_delta_o * slice_row; // Shared write index of current thread. int a_sh_wr = a_sh_stride * (threadIdx.x / a_gl_rd_delta_o) + (threadIdx.x % a_gl_rd_delta_o); // Shared read index. // NOTE(HandH1998): int8 input a only need 16 threads to load 16x16 matrix int a_sh_rd = a_sh_stride * ((threadIdx.x % 32) % 16); a_sh_rd += 1 * ((threadIdx.x / 32) / (thread_n_blocks / 4));
int b_gl_rd = b_gl_stride * (threadIdx.x / b_sh_stride) + (threadIdx.x % b_sh_stride); b_gl_rd += b_sh_stride * slice_col; b_gl_rd += b_gl_rd_delta_o * slice_row; int b_sh_wr = threadIdx.x; int b_sh_rd = threadIdx.x;
int s_tok_gl_rd = threadIdx.x; // NOTE(HandH1998): activation scale s_tok need shuffle to [0, 8, 1, 9, 2, 10, // 3, 11, 4, 12, 5, 13, 6, 14, 7, 15] for example, 0, 8 row scales serve for // thread 0, 1, 2, 3. For more details, refer to mma operand A layout as // s_tok's size is not fixed, we can not shuffle before inference we shuffle // it when fetching s_tok from global memory to shared memory, that's why // s_tok_sh_wr is like this int s_tok_sh_wr = (threadIdx.x / 16) * 16 + (threadIdx.x % 8) * 2 + (threadIdx.x % 16) / 8; int s_tok_sh_rd = (threadIdx.x % 32) / 4; bool s_tok_sh_wr_pred = threadIdx.x < prob_m;
int s_ch_gl_rd = s_ch_sh_stride * slice_col + threadIdx.x; int s_ch_sh_wr = threadIdx.x; int s_ch_sh_rd = 16 * ((threadIdx.x / 32) % (thread_n_blocks / 4)) + 2 * ((threadIdx.x % 32) % 4); bool s_ch_sh_wr_pred = threadIdx.x < s_ch_sh_stride;
int s_group_gl_rd, s_group_sh_wr, s_group_sh_rd; bool s_group_sh_wr_pred; if constexpr (group_blocks != -1) { s_group_gl_rd = s_group_gl_stride * ((thread_k_blocks * slice_row) / group_blocks) + s_group_sh_stride * slice_col + threadIdx.x; s_group_sh_wr = threadIdx.x; // NOTE(HandH1998): s_group_sh_rd is related to mma output C s_group_sh_rd = 8 * ((threadIdx.x / 32) % (thread_n_blocks / 4)) + (threadIdx.x % 32) / 4; s_group_sh_wr_pred = threadIdx.x < s_group_sh_stride; }
// Precompute which thread should not read memory in which iterations; this is // needed if there are more threads than required for a certain tilesize or // when the batchsize is not a multiple of 16. bool a_sh_wr_pred[a_sh_wr_iters]; #pragma unroll for (int i = 0; i < a_sh_wr_iters; i++) a_sh_wr_pred[i] = a_sh_wr_delta * i + a_sh_wr < a_sh_stride * prob_m;
// To ensure that writing and reading A tiles to/from shared memory, the // latter in fragment format, is fully bank conflict free, we need to use a // rather fancy XOR-based layout. The key here is that neither reads nor // writes of the 16-byte `int4` blocks of 8 consecutive threads involve the // same shared memory banks. Further, it seems (based on NSight-Compute) that // each warp must also write a consecutive memory segment? auto transform_a = [&](int i) { int row = i / a_gl_rd_delta_o; return a_gl_rd_delta_o * row + (i % a_gl_rd_delta_o) ^ row; }; // Since the computation of this remapping is non-trivial and, due to our main // loop unrolls, all shared memory accesses are static, we simply precompute // both transformed reads and writes. int a_sh_wr_trans[a_sh_wr_iters]; #pragma unroll for (int i = 0; i < a_sh_wr_iters; i++) a_sh_wr_trans[i] = transform_a(a_sh_wr_delta * i + a_sh_wr); int a_sh_rd_trans[b_sh_wr_iters][thread_m_blocks]; #pragma unroll for (int i = 0; i < b_sh_wr_iters; i++) { #pragma unroll for (int j = 0; j < thread_m_blocks; j++) a_sh_rd_trans[i][j] = transform_a(a_sh_rd_delta_o * i + a_sh_rd_delta_i * j + a_sh_rd); }
// Since B-accesses have non-constant stride they have to be computed at // runtime; we break dependencies between subsequent accesses with a tile by // maintining multiple pointers (we have enough registers), a tiny // optimization. const int4* B_ptr[b_sh_wr_iters]; #pragma unroll for (int i = 0; i < b_sh_wr_iters; i++) B_ptr[i] = B + b_gl_rd_delta_i * i + b_gl_rd;
extern __shared__ int4 sh[]; // Shared memory storage for global fetch pipelines. // NOTE(HandH1998): stages need >= 4, otherwise, sh_s_tok = sh + max(stages * // a_sh_stage + stages * b_sh_stage, 4 * stages * a_sh_stage) int4* sh_a = sh; int4* sh_b = sh_a + (stages * a_sh_stage); int4* sh_s_tok = sh_b + (stages * b_sh_stage); int4* sh_s_ch = sh_s_tok + s_tok_sh_stride; int4* sh_s_group = sh_s_ch + s_ch_sh_stride;
// Register storage for double buffer of shared memory reads. FragA frag_a[2][thread_m_blocks]; I4 frag_b_quant[2]; FragC frag_c[thread_m_blocks][4][2]; FragS_GROUP frag_s_group[2][4]; FragS_CHANNEL frag_s_tok[thread_m_blocks]; FragS_CHANNEL frag_s_ch[2][4];
// Zero accumulators. auto zero_accums = [&]() { #pragma unroll for (int i = 0; i < thread_m_blocks * 4 * 2 * 4; i++) reinterpret_cast<int*>(frag_c)[i] = 0; };
// Asynchronously fetch the next A, B and s tile from global to the next // shared memory pipeline location. auto fetch_to_shared = [&](int pipe, int a_off, bool pred = true) { if (pred) { int4* sh_a_stage = sh_a + a_sh_stage * pipe; #pragma unroll for (int i = 0; i < a_sh_wr_iters; i++) { cp_async4_pred( &sh_a_stage[a_sh_wr_trans[i]], &A[a_gl_rd_delta_i * i + a_gl_rd + a_gl_rd_delta_o * a_off], a_sh_wr_pred[i]); } int4* sh_b_stage = sh_b + b_sh_stage * pipe; #pragma unroll for (int i = 0; i < b_sh_wr_iters; i++) { cp_async4(&sh_b_stage[b_sh_wr_delta * i + b_sh_wr], B_ptr[i]); B_ptr[i] += b_gl_rd_delta_o; } // Only fetch scales if this tile starts a new group if constexpr (group_blocks != -1) { if (pipe % (group_blocks / thread_k_blocks) == 0) { int4* sh_s_group_stage = sh_s_group + s_group_sh_stage * pipe; if (s_group_sh_wr_pred) cp_async4(&sh_s_group_stage[s_group_sh_wr], &s_group[s_group_gl_rd]); s_group_gl_rd += s_group_gl_rd_delta; } } } // Insert a fence even when we are winding down the pipeline to ensure that // waiting is also correct at this point. cp_async_fence(); };
// Wait until the next thread tile has been loaded to shared memory. auto wait_for_stage = [&]() { // We only have `stages - 2` active fetches since we are double buffering // and can only issue the next fetch when it is guaranteed that the previous // shared memory load is fully complete (as it may otherwise be // overwritten). cp_async_wait<stages - 2>(); __syncthreads(); };
// Load the next sub-tile from the current location in the shared memory pipe // into the current register buffer. auto fetch_to_registers = [&](int k, int pipe) { // It may seem inefficient that we reload the groups for every sub-tile; // however, this does not seem to be a significant bottleneck, while some // theoretically better attempts have lead to bad instruction ordering by // the compiler and correspondingly a noticeable drop in performance. if constexpr (group_blocks != -1) { int4* sh_s_group_stage = sh_s_group + s_group_sh_stage * ((group_blocks / thread_k_blocks) * (pipe / (group_blocks / thread_k_blocks))); reinterpret_cast<int4*>(&frag_s_group[k % 2])[0] = sh_s_group_stage[s_group_sh_rd]; } int4* sh_a_stage = sh_a + a_sh_stage * pipe; #pragma unroll for (int i = 0; i < thread_m_blocks; i++) ldsm4(frag_a[k % 2][i], &sh_a_stage[a_sh_rd_trans[k % b_sh_wr_iters][i]]); int4* sh_b_stage = sh_b + b_sh_stage * pipe; frag_b_quant[k % 2] = *reinterpret_cast<I4*>( &sh_b_stage[b_sh_rd_delta * (k % b_sh_wr_iters) + b_sh_rd]); };
// Execute the actual tensor core matmul of a sub-tile. auto matmul = [&](int k) { // We have the m dimension as the inner loop in order to encourage overlapping // dequantization and matmul operations. #pragma unroll for (int j = 0; j < 4; j++) { int b_quant = frag_b_quant[k % 2][j]; // int b_quant_shift = b_quant << 4; FragB frag_b0, frag_b1; // If there are no groups, we can just scale the final output once and can // avoid doing so for each weight. if constexpr (group_blocks != -1) { int b_quant_shift = b_quant >> 8; frag_b0 = dequant_per_group(b_quant, frag_s_group[k % 2][j], 0); frag_b1 = dequant_per_group(b_quant_shift, frag_s_group[k % 2][j], 1); } else { int b_quant_shift = b_quant << 4; frag_b0 = dequant_per_channel(b_quant); frag_b1 = dequant_per_channel(b_quant_shift); } #pragma unroll for (int i = 0; i < thread_m_blocks; i++) { mma(frag_a[k % 2][i], frag_b0, frag_c[i][j][0]); mma(frag_a[k % 2][i], frag_b1, frag_c[i][j][1]); } } };
// Since we slice across the k dimension of a tile in order to increase the // number of warps while keeping the n dimension of a tile reasonable, we have // multiple warps that accumulate their partial sums of the same output // location; which we have to reduce over in the end. We do in shared memory. auto thread_block_reduce = [&]() { constexpr int red_off = threads / b_sh_stride / 2; if (red_off >= 1) { int red_idx = threadIdx.x / b_sh_stride; constexpr int red_sh_stride = b_sh_stride * 4 * 2; constexpr int red_sh_delta = b_sh_stride; int red_sh_rd = red_sh_stride * (threadIdx.x / b_sh_stride) + (threadIdx.x % b_sh_stride);
// Parallel logarithmic shared memory reduction. We make sure to avoid any // unnecessary read or write iterations, e.g., for two warps we write only // once by warp 1 and read only once by warp 0.
#pragma unroll for (int m_block = 0; m_block < thread_m_blocks; m_block++) { #pragma unroll for (int i = red_off; i > 0; i /= 2) { if (i <= red_idx && red_idx < 2 * i) { #pragma unroll for (int j = 0; j < 4 * 2; j++) { int red_sh_wr = red_sh_delta * j + (red_sh_rd - red_sh_stride * i); if (i < red_off) { int* c_rd = reinterpret_cast<int*>(&sh[red_sh_delta * j + red_sh_rd]); int* c_wr = reinterpret_cast<int*>(&sh[red_sh_wr]); #pragma unroll for (int k = 0; k < 4; k++) reinterpret_cast<FragC*>(frag_c)[4 * 2 * m_block + j][k] += c_rd[k] + c_wr[k]; } sh[red_sh_wr] = reinterpret_cast<int4*>(&frag_c)[4 * 2 * m_block + j]; } } __syncthreads(); } if (red_idx == 0) { #pragma unroll for (int i = 0; i < 4 * 2; i++) { int* c_rd = reinterpret_cast<int*>(&sh[red_sh_delta * i + red_sh_rd]); #pragma unroll for (int j = 0; j < 4; j++) reinterpret_cast<FragC*>(frag_c)[4 * 2 * m_block + i][j] += c_rd[j]; } } __syncthreads(); } } };
// Since multiple threadblocks may process parts of the same column slice, we // finally have to globally reduce over the results. As the striped // partitioning minimizes the number of such reductions and our outputs are // usually rather small, we perform this reduction serially in L2 cache. // global_reduce works on INT32 elements, which are the results of INT8 GEMM. // This is why we need another INT32 maxtrix `C` to reduce instead of the // original half matrix `D`. auto global_reduce = [&](bool first = false, bool last = false) { // We are very careful here to reduce directly in the output buffer to // maximize L2 cache utilization in this step. To do this, we write out // results in FP16 (but still reduce with FP32 compute). constexpr int active_threads = 32 * thread_n_blocks / 4; if (threadIdx.x < active_threads) { int c_gl_stride = prob_n / 4; int c_gl_wr_delta_o = 8 * c_gl_stride; int c_gl_wr_delta_i = 8 * (active_threads / 32); int c_gl_wr = c_gl_stride * ((threadIdx.x % 32) / 4) + 8 * (threadIdx.x / 32) + (threadIdx.x % 4) * 2; c_gl_wr += (4 * thread_n_blocks) * slice_col; constexpr int c_sh_wr_delta = active_threads * 2; int c_sh_wr = 2 * threadIdx.x;
int row = (threadIdx.x % 32) / 4;
if (!first) { // Interestingly, doing direct global accesses here really seems to mess up // the compiler and lead to slowdowns, hence we also use async-copies even // though these fetches are not actually asynchronous. #pragma unroll for (int i = 0; i < thread_m_blocks * 4; i++) { cp_async4_pred( &sh[c_sh_wr + c_sh_wr_delta * i], &C[c_gl_wr + c_gl_wr_delta_o * (i / 2) + c_gl_wr_delta_i * (i % 2)], i < (thread_m_blocks - 1) * 4 || 8 * (i / 2) + row < prob_m); cp_async4_pred( &sh[c_sh_wr + c_sh_wr_delta * i + 1], &C[c_gl_wr + c_gl_wr_delta_o * (i / 2) + c_gl_wr_delta_i * (i % 2) + 1], i < (thread_m_blocks - 1) * 4 || 8 * (i / 2) + row < prob_m); } cp_async_fence(); cp_async_wait<0>(); }
#pragma unroll for (int i = 0; i < thread_m_blocks * 4; i++) { if (i < (thread_m_blocks - 1) * 4 || 8 * (i / 2) + row < prob_m) { if (!first) { int4 d_red1 = sh[c_sh_wr + i * c_sh_wr_delta]; int4 d_red2 = sh[c_sh_wr + i * c_sh_wr_delta + 1]; #pragma unroll for (int j = 0; j < 4; j++) { reinterpret_cast<int*>( &frag_c)[4 * 2 * 4 * (i / 4) + 4 * j + (i % 4)] += reinterpret_cast<int*>(&d_red1)[j]; } #pragma unroll for (int j = 0; j < 4; j++) { reinterpret_cast<int*>( &frag_c)[4 * 2 * 4 * (i / 4) + 4 * (j + 4) + (i % 4)] += reinterpret_cast<int*>(&d_red2)[j]; } } if (!last) { int4 d1, d2; #pragma unroll for (int j = 0; j < 4; j++) { reinterpret_cast<int*>(&d1)[j] = reinterpret_cast<int*>( &frag_c)[4 * 2 * 4 * (i / 4) + 4 * j + (i % 4)]; } #pragma unroll for (int j = 0; j < 4; j++) { reinterpret_cast<int*>(&d2)[j] = reinterpret_cast<int*>( &frag_c)[4 * 2 * 4 * (i / 4) + 4 * (j + 4) + (i % 4)]; } C[c_gl_wr + c_gl_wr_delta_o * (i / 2) + c_gl_wr_delta_i * (i % 2)] = d1; C[c_gl_wr + c_gl_wr_delta_o * (i / 2) + c_gl_wr_delta_i * (i % 2) + 1] = d2; } } } } };
// Write out the reduce final result in the correct layout. We only actually // reshuffle matrix fragments in this step, the reduction above is performed // in fragment layout. auto write_result = [&]() { int d_gl_stride = prob_n / 8; constexpr int d_sh_stride = 2 * thread_n_blocks + 1; int d_gl_wr_delta = d_gl_stride * (threads / (2 * thread_n_blocks)); constexpr int d_sh_rd_delta = d_sh_stride * (threads / (2 * thread_n_blocks));
int d_gl_wr = d_gl_stride * (threadIdx.x / (2 * thread_n_blocks)) + (threadIdx.x % (2 * thread_n_blocks)); d_gl_wr += (2 * thread_n_blocks) * slice_col; int d_sh_wr = (4 * d_sh_stride) * ((threadIdx.x % 32) / 4) + (threadIdx.x % 32) % 4; d_sh_wr += 32 * (threadIdx.x / 32); int d_sh_rd = d_sh_stride * (threadIdx.x / (2 * thread_n_blocks)) + (threadIdx.x % (2 * thread_n_blocks));
int d_gl_wr_end = d_gl_stride * prob_m;
// We first reorder in shared memory to guarantee the most efficient final // global write patterns auto write = [&](int idx, int c0, int c1, float a_s, FragS_CHANNEL& w_s) { float2 deq_res; deq_res.x = int32_to_float(c0) * w_s[0] * a_s; deq_res.y = int32_to_float(c1) * w_s[1] * a_s; ((half2*)sh)[idx] = float2_to_half2(deq_res); };
if (threadIdx.x / 32 < thread_n_blocks / 4) { #pragma unroll for (int i = 0; i < thread_m_blocks; i++) { #pragma unroll for (int j = 0; j < 4; j++) { int wr = d_sh_wr + 8 * j; write(wr + (4 * d_sh_stride) * 0 + 0, frag_c[i][j][0][0], frag_c[i][j][0][1], frag_s_tok[i][0], frag_s_ch[j / 2][2 * (j % 2) + 0]); write(wr + (4 * d_sh_stride) * 8 + 0, frag_c[i][j][0][2], frag_c[i][j][0][3], frag_s_tok[i][1], frag_s_ch[j / 2][2 * (j % 2) + 0]); write(wr + (4 * d_sh_stride) * 0 + 4, frag_c[i][j][1][0], frag_c[i][j][1][1], frag_s_tok[i][0], frag_s_ch[j / 2][2 * (j % 2) + 1]); write(wr + (4 * d_sh_stride) * 8 + 4, frag_c[i][j][1][2], frag_c[i][j][1][3], frag_s_tok[i][1], frag_s_ch[j / 2][2 * (j % 2) + 1]); } d_sh_wr += 16 * (4 * d_sh_stride); } } __syncthreads();
#pragma unroll for (int i = 0; i < ceildiv(16 * thread_m_blocks, threads / (2 * thread_n_blocks)); i++) { if (d_gl_wr < d_gl_wr_end) { D[d_gl_wr] = sh[d_sh_rd]; d_gl_wr += d_gl_wr_delta; d_sh_rd += d_sh_rd_delta; } } };
// Start global fetch and register load pipelines. auto start_pipes = [&]() { #pragma unroll for (int i = 0; i < stages - 1; i++) fetch_to_shared(i, i, i < slice_iters); zero_accums(); wait_for_stage(); fetch_to_registers(0, 0); a_gl_rd += a_gl_rd_delta_o * (stages - 1); }; start_pipes();
// Main loop. while (slice_iters) { // We unroll over both the global fetch and the register load pipeline to // ensure all shared memory accesses are static. Note that both pipelines have // even length meaning that the next iteration will always start at index 0. #pragma unroll for (int pipe = 0; pipe < stages;) { #pragma unroll for (int k = 0; k < b_sh_wr_iters; k++) { fetch_to_registers(k + 1, pipe % stages); if (k == b_sh_wr_iters - 2) { fetch_to_shared((pipe + stages - 1) % stages, pipe, slice_iters >= stages); pipe++; wait_for_stage(); } matmul(k); } slice_iters--; if (slice_iters == 0) break; } a_gl_rd += a_gl_rd_delta_o * stages;
// Process results and, if necessary, proceed to the next column slice. // While this pattern may not be the most readable, other ways of writing // the loop seemed to noticeably worse performance after compilation. if (slice_iters == 0) { cp_async_wait<0>(); bool last = slice_idx == slice_count - 1; // For per-column scales, we only fetch them here in the final step before // write-out if (last) { if (s_tok_sh_wr_pred) { cp_async1(&sh_s_tok[s_tok_sh_wr], &s_tok[s_tok_gl_rd]); } if (s_ch_sh_wr_pred) { cp_async4(&sh_s_ch[s_ch_sh_wr], &s_ch[s_ch_gl_rd]); } cp_async_fence(); } thread_block_reduce(); if (last) { cp_async_wait<0>(); __syncthreads(); if (threadIdx.x / 32 < thread_n_blocks / 4) { #pragma unroll for (int i = 0; i < thread_m_blocks; i++) { frag_s_tok[i][0] = *reinterpret_cast<float*>(&sh_s_tok[16 * i + 2 * s_tok_sh_rd]); frag_s_tok[i][1] = *reinterpret_cast<float*>( &sh_s_tok[16 * i + 2 * s_tok_sh_rd + 1]); } reinterpret_cast<int4*>(&frag_s_ch)[0] = sh_s_ch[s_ch_sh_rd + 0]; reinterpret_cast<int4*>(&frag_s_ch)[1] = sh_s_ch[s_ch_sh_rd + 1]; reinterpret_cast<int4*>(&frag_s_ch)[2] = sh_s_ch[s_ch_sh_rd + 8]; reinterpret_cast<int4*>(&frag_s_ch)[3] = sh_s_ch[s_ch_sh_rd + 9]; } } if (slice_count > 1) { // only globally reduce if there is more than one // block in a slice barrier_acquire(&locks[slice_col], slice_idx); global_reduce(slice_idx == 0, last); barrier_release(&locks[slice_col], last); } if (last) // only the last block in a slice actually writes the result write_result(); slice_row = 0; slice_col_par++; slice_col++; init_slice(); if (slice_iters) { a_gl_rd = a_gl_stride * (threadIdx.x / a_gl_rd_delta_o) + (threadIdx.x % a_gl_rd_delta_o); #pragma unroll for (int i = 0; i < b_sh_wr_iters; i++) B_ptr[i] += b_sh_stride - b_gl_rd_delta_o * k_tiles; if (slice_col == 0) { #pragma unroll for (int i = 0; i < b_sh_wr_iters; i++) B_ptr[i] -= b_gl_stride; } s_group_gl_rd = s_group_sh_stride * slice_col + threadIdx.x; s_ch_gl_rd = s_ch_sh_stride * slice_col + threadIdx.x; start_pipes(); } } }}
#else
template <const int threads, // number of threads in a threadblock const int thread_m_blocks, // number of 16x16 blocks in the m // dimension (batchsize) of the // threadblock const int thread_n_blocks, // same for n dimension (output) const int thread_k_blocks, // same for k dimension (reduction) const int stages, // number of stages for the async global->shared // fetch pipeline const int group_blocks = -1 // number of consecutive 16x16 blocks // with a separate quantization scale >__global__ void Marlin( const int4* __restrict__ A, // int8 input matrix of shape mxk const int4* __restrict__ B, // 4bit quantized weight matrix of shape kxn int4* __restrict__ C, // int32 global_reduce buffer of shape // (max_par*16*4)xn, as int8 tensor core's output is // int32 dtype int4* __restrict__ D, // fp16 output buffer of shape mxn const float* __restrict__ s_tok, // fp32 activation per-token quantization // scales of shape mx1 const int4* __restrict__ s_ch, // fp32 weight per-channel quantization // scales of shape 1xn const int4* __restrict__ s_group, // fp16 weight per-group quantization // scales of shape (k/groupsize)xn, when // group_blocks=-1, it should be nullptr int prob_m, // batch dimension m int prob_n, // output dimension n int prob_k, // reduction dimension k int* locks // extra global storage for barrier synchronization) { // Marlin is not implemented yet for SM < 8.0 assert(false); return;}
#endif
// 8 warps are a good choice since every SM has 4 schedulers and having more// than 1 warp per schedule allows some more latency hiding. At the same time,// we want relatively few warps to have many registers per warp and small tiles.const int USER_THREADS = 256; // Note: This is only used with user-provided thread_k/nconst int STAGES = 4; // 4 pipeline stages fit into shared memory
static constexpr int min_thread_n = 64;static constexpr int min_thread_k = 64;
static constexpr int tile_size = 16;static constexpr int max_par = 16;
static constexpr int pack_factor_4bit = 8; // We have 8 4-bit vals inside a 32 bit
#define __CALL_IF(THREAD_M_BLOCKS, THREAD_N_BLOCKS, THREAD_K_BLOCKS, \ GROUP_BLOCKS, NUM_THREADS) \ else if (thread_m_blocks == THREAD_M_BLOCKS && \ thread_n_blocks == THREAD_N_BLOCKS && \ thread_k_blocks == THREAD_K_BLOCKS && \ group_blocks == GROUP_BLOCKS && num_threads == NUM_THREADS) { \ cudaFuncSetAttribute(Marlin<NUM_THREADS, THREAD_M_BLOCKS, THREAD_N_BLOCKS, \ THREAD_K_BLOCKS, STAGES, GROUP_BLOCKS>, \ cudaFuncAttributeMaxDynamicSharedMemorySize, \ max_shared_mem); \ Marlin<NUM_THREADS, THREAD_M_BLOCKS, THREAD_N_BLOCKS, THREAD_K_BLOCKS, \ STAGES, GROUP_BLOCKS> \ <<<blocks, NUM_THREADS, max_shared_mem, stream>>>( \ A_ptr, B_ptr, C_ptr, D_ptr, s_tok_ptr, s_ch_ptr, s_group_ptr, \ prob_m, prob_n, prob_k, locks); \ }
typedef struct { int thread_k; int thread_n; int num_threads;} thread_config_t;
thread_config_t small_batch_thread_configs[] = { // Ordered by priority
// thread_k, thread_n, num_threads {128, 128, 256}, // Default {128, 64, 128}, // Reduce N 2X, same K {64, 256, 256}, // Reduce K 2X, increase N 2X {64, 128, 128}, // Reduce K 2X, same N};
thread_config_t large_batch_thread_configs[] = { // Ordered by priority
// thread_k, thread_n, num_threads {64, 256, 256}, // Default {128, 128, 256}, // Reduce N 2X, increase K 2X {64, 128, 128}, // Reduce N 2X, same K {128, 64, 128}, // Reduce N 4X, increase K 2X};
bool is_valid_config(thread_config_t const& th_config, int prob_m, int prob_n, int prob_k) { // Sanity if (th_config.thread_k == -1 || th_config.thread_n == -1 || th_config.num_threads == -1) { return false; }
// Verify K/N are divisible by thread K/N if (prob_k % th_config.thread_k != 0 || prob_n % th_config.thread_n != 0) { return false; }
// thread_k can be only 128 or 64 (because it must be less than groupsize // which is 128) if (th_config.thread_k != 128 && th_config.thread_k != 64) { return false; }
// Verify min for thread K/N if (th_config.thread_n < min_thread_n || th_config.thread_k < min_thread_k) { return false; }
// num_threads must be at least 128 (= 4 warps) if (th_config.num_threads < 128) { return false; }
return true;}
thread_config_t determine_thread_config(int prob_m, int prob_n, int prob_k) { if (prob_m <= 16) { for (auto th_config : small_batch_thread_configs) { if (is_valid_config(th_config, prob_m, prob_n, prob_k)) { return th_config; } }
} else { for (auto th_config : large_batch_thread_configs) { if (is_valid_config(th_config, prob_m, prob_n, prob_k)) { return th_config; } } }
return thread_config_t{-1, -1, -1};}
#define CALL_IF(N_BLOCKS, K_BLOCKS, NUM_THREADS) \ __CALL_IF(1, N_BLOCKS, K_BLOCKS, -1, NUM_THREADS) \ __CALL_IF(1, N_BLOCKS, K_BLOCKS, 8, NUM_THREADS) \ __CALL_IF(1, N_BLOCKS, K_BLOCKS, -1, NUM_THREADS) \ __CALL_IF(1, N_BLOCKS, K_BLOCKS, 8, NUM_THREADS) \ __CALL_IF(2, N_BLOCKS, K_BLOCKS, -1, NUM_THREADS) \ __CALL_IF(2, N_BLOCKS, K_BLOCKS, 8, NUM_THREADS) \ __CALL_IF(3, N_BLOCKS, K_BLOCKS, -1, NUM_THREADS) \ __CALL_IF(3, N_BLOCKS, K_BLOCKS, 8, NUM_THREADS) \ __CALL_IF(4, N_BLOCKS, K_BLOCKS, -1, NUM_THREADS) \ __CALL_IF(4, N_BLOCKS, K_BLOCKS, 8, NUM_THREADS)
void marlin_qqq_cuda(const void* A, const void* B, void* C, void* D, void* s_tok, void* s_ch, void* s_group, int prob_m, int prob_n, int prob_k, void* workspace, int groupsize = -1, int dev = 0, cudaStream_t stream = 0, int thread_k = -1, int thread_n = -1, int sms = -1, int max_par = 16) { int tot_m = prob_m; int tot_m_blocks = ceildiv(tot_m, 16); int pad = 16 * tot_m_blocks - tot_m;
if (sms == -1) cudaDeviceGetAttribute(&sms, cudaDevAttrMultiProcessorCount, dev);
int max_shared_mem = 0; cudaDeviceGetAttribute(&max_shared_mem, cudaDevAttrMaxSharedMemoryPerBlockOptin, dev); TORCH_CHECK(max_shared_mem > 0);
// Set thread config thread_config_t th_config; if (thread_k != -1 && thread_n != -1) { // User-defined config th_config = thread_config_t{thread_k, thread_n, USER_THREADS}; } else { // Auto config th_config = determine_thread_config(prob_m, prob_n, prob_k); }
if (!is_valid_config(th_config, prob_m, prob_n, prob_k)) { throw std::runtime_error( "Invalid thread config: thread_k = " + str(th_config.thread_k) + ", thread_n = " + str(th_config.thread_n) + ", num_threads = " + str(th_config.num_threads) + " for MKN = [" + str(prob_m) + ", " + str(prob_k) + ", " + str(prob_n) + "]"); }
int num_threads = th_config.num_threads; thread_k = th_config.thread_k; thread_n = th_config.thread_n;
int thread_k_blocks = thread_k / 16; int thread_n_blocks = thread_n / 16; int group_blocks = (groupsize == -1) ? -1 : groupsize / 16; int blocks = sms;
if (prob_m == 0 || prob_n == 0 || prob_k == 0) { return; }
TORCH_CHECK(prob_n % thread_n == 0, "prob_n = ", prob_n, " is not divisible by thread_n = ", thread_n); TORCH_CHECK(prob_k % thread_k == 0, "prob_k = ", prob_k, " is not divisible by thread_k = ", thread_k); if (group_blocks != -1) { TORCH_CHECK(prob_k % group_blocks == 0, "prob_k = ", prob_k, " is not divisible by group_blocks = ", group_blocks); }
const int4* A_ptr = (const int4*)A; const int4* B_ptr = (const int4*)B; int4* C_ptr = (int4*)C; int4* D_ptr = (int4*)D; const float* s_tok_ptr = (const float*)s_tok; const int4* s_ch_ptr = (const int4*)s_ch; const int4* s_group_ptr = (const int4*)s_group;
int* locks = (int*)workspace;
for (int i = 0; i < tot_m_blocks; i += 4) { int thread_m_blocks = tot_m_blocks - i; prob_m = tot_m - 16 * i; int par = 1; if (thread_m_blocks > 4) { // Note that parallel > 1 currently only works for inputs without any // padding par = (16 * thread_m_blocks - pad) / 64; if (par > max_par) par = max_par; prob_m = 64 * par; i += 4 * (par - 1); thread_m_blocks = 4; }
// For compilation speed, we only define the kernel configurations that have // seemed useful (in terms of performance) in our testing, however many more // are, in principle, possible. if (false) { } CALL_IF(8, 8, 256) CALL_IF(16, 4, 256) CALL_IF(8, 4, 128) CALL_IF(4, 8, 128) else { throw std::runtime_error("Unsupported shapes: MKN = [" + str(prob_m) + ", " + str(prob_k) + ", " + str(prob_n) + "]" + ", groupsize = " + str(groupsize) + ", thread_m_blocks = " + str(thread_m_blocks) + ", thread_n_blocks = " + str(thread_n_blocks) + ", thread_k_blocks = " + str(thread_k_blocks)); }
A_ptr += 16 * thread_m_blocks * (prob_k / 16) * par; D_ptr += 16 * thread_m_blocks * (prob_n / 8) * par; s_tok_ptr += 16 * thread_m_blocks * par; }}} // anonymous namespace
torch::Tensor marlin_qqq_gemm(torch::Tensor const& a, torch::Tensor const& b_q_weight, torch::Tensor const& s_tok, torch::Tensor const& s_ch, torch::Tensor const& s_group, torch::Tensor& workspace, int64_t size_m, int64_t size_n, int64_t size_k) { // Verify M TORCH_CHECK(size_m == a.size(0), "Shape mismatch: a.size(0) = " + str(a.size(0)) + ", size_m = " + str(size_m)); TORCH_CHECK(size_m == s_tok.numel(), "Shape mismatch: s_tok.numel() = " + str(s_tok.numel()) + ", size_m = " + str(size_m));
// Verify K TORCH_CHECK(size_k == a.size(1), "Shape mismatch: a.size(1) = " + str(a.size(1)) + ", size_k = " + str(size_k)); TORCH_CHECK(size_k % tile_size == 0, "size_k = " + str(size_k) + " is not divisible by tile_size = " + str(tile_size)); TORCH_CHECK( (size_k / tile_size) == b_q_weight.size(0), "Shape mismatch: b_q_weight.size(0) = " + str(b_q_weight.size(0)) + ", size_k = " + str(size_k) + ", tile_size = " + str(tile_size));
int groupsize = (s_group.numel() == 0) ? -1 : size_k / s_group.size(0); // Verify groupsize TORCH_CHECK(groupsize == -1 || groupsize == 128, "Unexpected groupsize = " + str(groupsize));
// Verify N TORCH_CHECK(s_ch.numel() == size_n, "Shape mismatch: s_ch.numel() = " + str(s_ch.numel()) + ", size_n = " + str(size_n)); TORCH_CHECK(b_q_weight.size(1) % tile_size == 0, "b_q_weight.size(1) = " + str(b_q_weight.size(1)) + " is not divisible by tile_size = " + str(tile_size)); if (groupsize != -1) { TORCH_CHECK(s_group.size(1) == size_n, "Shape mismatch: s_group.size(1) = " + str(s_group.size(1)) + ", size_n = " + str(size_n)); TORCH_CHECK( size_k % s_group.size(0) == 0, "size_k = " + str(size_k) + ", is not divisible by s_group.size(0) = " + str(s_group.size(0))); }
int actual_size_n = (b_q_weight.size(1) / tile_size) * pack_factor_4bit; TORCH_CHECK(size_n == actual_size_n, "Shape mismatch: size_n = " + str(size_n) + ", actual_size_n = " + str(actual_size_n));
// Verify A device and strides TORCH_CHECK(a.device().is_cuda(), "A is not on GPU"); TORCH_CHECK(a.is_contiguous(), "A is not contiguous");
// Verify B device and strides TORCH_CHECK(b_q_weight.device().is_cuda(), "b_q_weight is not on GPU"); TORCH_CHECK(b_q_weight.is_contiguous(), "b_q_weight is not contiguous");
// Verify s_tok device, strides and dtype TORCH_CHECK(s_tok.device().is_cuda(), "s_tok is not on GPU"); TORCH_CHECK(s_tok.is_contiguous(), "s_tok is not contiguous"); TORCH_CHECK(s_tok.dtype() == torch::kFloat32, "s_tok's dtype is not float32");
// Verify s_ch device, strides and dtype TORCH_CHECK(s_ch.device().is_cuda(), "s_ch is not on GPU"); TORCH_CHECK(s_ch.is_contiguous(), "s_ch is not contiguous"); TORCH_CHECK(s_ch.dtype() == torch::kFloat32, "s_ch's dtype is not float32");
// Verify s_group device, strides and dtype TORCH_CHECK(s_group.device().is_cuda(), "s_group is not on GPU"); TORCH_CHECK(s_group.is_contiguous(), "s_group is not contiguous"); TORCH_CHECK(s_group.dtype() == torch::kFloat16, "s_group's dtype is not float16");
// Verify workspace size TORCH_CHECK(size_n % min_thread_n == 0, "size_n = " + str(size_n) + ", is not divisible by min_thread_n = " + str(min_thread_n)); int min_workspace_size = (size_n / min_thread_n) * max_par; TORCH_CHECK(workspace.numel() >= min_workspace_size, "workspace.numel = " + str(workspace.numel()) + " is below min_workspace_size = " + str(min_workspace_size));
// Alloc C matrix const at::cuda::OptionalCUDAGuard device_guard(device_of(a)); auto options_c = torch::TensorOptions().dtype(torch::kInt).device(a.device()); torch::Tensor c = torch::empty({max_par * 64, size_n}, options_c);
// Alloc D matrix auto options_d = torch::TensorOptions().dtype(torch::kFloat16).device(a.device()); torch::Tensor d = torch::empty({size_m, size_n}, options_d);
// thread_k: `k` size of a thread_tile in `weights` (can usually be left as // auto -1) int thread_k = -1; // thread_n: `n` size of a thread_tile in `weights` (can usually be left as // auto -1) int thread_n = -1; // sms: number of SMs to use for the kernel (can usually be left as auto -1) int sms = -1;
int dev = a.get_device(); marlin_qqq_cuda( a.data_ptr(), b_q_weight.data_ptr(), c.data_ptr(), d.data_ptr(), s_tok.data_ptr(), s_ch.data_ptr(), s_group.data_ptr(), size_m, size_n, size_k, workspace.data_ptr(), groupsize, dev, at::cuda::getCurrentCUDAStream(dev), thread_k, thread_n, sms, max_par);
return d;}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203Contains code from https://github.com/IST-DASLab/Sparse-Marlin/
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051/* * Copyright (C) 2024 Roberto Lopez Castro (roberto.lopez.castro@udc.es). All * Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
#pragma once
namespace marlin_24 {
constexpr int ceildiv(int a, int b) { return (a + b - 1) / b; }
// Instances of `Vec` are used to organize groups of >>registers<<, as needed// for instance as inputs to tensor core operations. Consequently, all// corresponding index accesses must be compile-time constants, which is why we// extensively use `#pragma unroll` throughout the kernel code to guarantee// this.template <typename T, int n>struct Vec { T elems[n]; __device__ T& operator[](int i) { return elems[i]; }};
template <int M_, int N_, int K_>struct ShapeBase { static constexpr int M = M_, N = N_, K = K_;};
using I4 = Vec<int, 4>;
// Matrix fragments for tensor core instructions; their precise layout is// documented here:// https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#matrix-fragments-for-mma-m16n8k16-with-floating-point-typeusing FragA = Vec<half2, 4>;using FragB = Vec<half2, 2>;using FragM = Vec<uint, 1>;using FragC = Vec<float, 4>;using FragS = Vec<half2, 1>; // quantization scales
} // namespace marlin_24123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136/* * Copyright (C) 2024 Roberto Lopez Castro (roberto.lopez.castro@udc.es). All * Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
#pragma once#include "base.h"
namespace marlin_24 {// Predicated asynchronous global->shared copy; used for inputs A where we apply// predication to handle batchsizes that are not multiples of 16.__device__ inline void cp_async4_pred_zfill(void* smem_ptr, const void* glob_ptr, bool pred = true, const bool zfill = false) { const int BYTES = 16; int src_in_bytes = (zfill ? 0 : BYTES); uint32_t smem = static_cast<uint32_t>(__cvta_generic_to_shared(smem_ptr)); asm volatile( "{\n" " .reg .pred p;\n" " setp.ne.b32 p, %0, 0;\n" " @p cp.async.cg.shared.global [%1], [%2], %3;\n" "}\n" ::"r"((int)pred), "r"(smem), "l"(glob_ptr), "n"(BYTES), "r"(src_in_bytes));}
__device__ inline void cp_async4_pred(void* smem_ptr, const void* glob_ptr, bool pred = true) { const int BYTES = 16; uint32_t smem = static_cast<uint32_t>(__cvta_generic_to_shared(smem_ptr)); asm volatile( "{\n" " .reg .pred p;\n" " setp.ne.b32 p, %0, 0;\n" " @p cp.async.cg.shared.global [%1], [%2], %3;\n" "}\n" ::"r"((int)pred), "r"(smem), "l"(glob_ptr), "n"(BYTES));}
// Asynchronous global->shared copy__device__ inline void cp_async4(void* smem_ptr, const void* glob_ptr) { const int BYTES = 16; uint32_t smem = static_cast<uint32_t>(__cvta_generic_to_shared(smem_ptr)); asm volatile( "{\n" " cp.async.cg.shared.global [%0], [%1], %2;\n" "}\n" ::"r"(smem), "l"(glob_ptr), "n"(BYTES));}
// Async copy fence.__device__ inline void cp_async_fence() { asm volatile("cp.async.commit_group;\n" ::);}
// Wait until at most `n` async copy stages are still pending.template <int n>__device__ inline void cp_async_wait() { asm volatile("cp.async.wait_group %0;\n" ::"n"(n));}
// Instruction for loading a full 16x16 matrix fragment of operand A from shared// memory, directly in tensor core layout.__device__ inline void ldsm4(FragA& frag_a, const void* smem_ptr) { uint32_t* a = reinterpret_cast<uint32_t*>(&frag_a); uint32_t smem = static_cast<uint32_t>(__cvta_generic_to_shared(smem_ptr)); asm volatile("ldmatrix.sync.aligned.m8n8.x4.shared.b16 {%0,%1,%2,%3}, [%4];\n" : "=r"(a[0]), "=r"(a[1]), "=r"(a[2]), "=r"(a[3]) : "r"(smem));}
__device__ inline void ldsm4_m(FragM& frag_m, const void* smem_ptr) { uint32_t* a = reinterpret_cast<uint32_t*>(&frag_m); uint32_t smem = static_cast<uint32_t>(__cvta_generic_to_shared(smem_ptr)); asm volatile("ldmatrix.sync.aligned.m8n8.x2.shared.b16 {%0,%1}, [%2];\n" : "=r"(a[0]), "=r"(a[1]) : "r"(smem));}
// Instruction for loading a full 16x16 matrix fragment of operand A from shared// memory, directly in tensor core layout.__device__ inline void ldsm4_t(FragA& frag_a, const void* smem_ptr) { uint32_t* a = reinterpret_cast<uint32_t*>(&frag_a); uint32_t smem = static_cast<uint32_t>(__cvta_generic_to_shared(smem_ptr)); asm volatile( "ldmatrix.sync.aligned.m8n8.x4.trans.shared.b16 {%0,%1,%2,%3}, [%4];\n" : "=r"(a[0]), "=r"(a[1]), "=r"(a[2]), "=r"(a[3]) : "r"(smem));}
// Wait until barrier reaches `count`, then lock for current threadblock.__device__ inline void barrier_acquire(int* lock, int count) { if (threadIdx.x == 0) { int state = -1; do // Guarantee that subsequent writes by this threadblock will be visible // globally. asm volatile("ld.global.acquire.gpu.b32 %0, [%1];\n" : "=r"(state) : "l"(lock)); while (state != count); } __syncthreads();}
// Release barrier and increment visitation count.__device__ inline void barrier_release(int* lock, bool reset = false) { __syncthreads(); if (threadIdx.x == 0) { if (reset) { lock[0] = 0; return; } int val = 1; // Make sure that all writes since acquiring this barrier are visible // globally, while releasing the barrier. asm volatile("fence.acq_rel.gpu;\n"); asm volatile("red.relaxed.gpu.global.add.s32 [%0], %1;\n" : : "l"(lock), "r"(val)); }}} // namespace marlin_24123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191/* * Copyright (C) 2024 Roberto Lopez Castro (roberto.lopez.castro@udc.es). All * Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
#pragma once#include "base.h"#include <cudaTypedefs.h>
namespace marlin_24 {
// On CUDA earlier than 12.5, the ordered_metadata version of this instruction// is not supported. On later versions of CUDA the version without ordered// metadata results in the following warning:// | Advisory: Modifier ‘.sp::ordered_metadata’ should be used on instruction// | ‘mma’ instead of modifier ‘.sp’ as it is expected to have substantially// | reduced performance on some future architectures#if defined CUDA_VERSION && CUDA_VERSION >= 12050 #define MMA_SP_INST \ "mma.sp::ordered_metadata.sync.aligned.m16n8k32.row.col.f32.f16.f16.f32 "#else #define MMA_SP_INST "mma.sp.sync.aligned.m16n8k32.row.col.f32.f16.f16.f32 "#endif
// m16n8k32 sparse tensor core mma instruction with fp16 inputs and fp32// output/accumulation.__device__ inline void mma_sp(const FragB& a_frag0, const FragB& a_frag1, const FragA& frag_b, FragC& frag_c, FragM& frag_m, const int psel) { const uint32_t* a0 = reinterpret_cast<const uint32_t*>(&a_frag0); const uint32_t* a1 = reinterpret_cast<const uint32_t*>(&a_frag1); const uint32_t* b = reinterpret_cast<const uint32_t*>(&frag_b); const uint32_t* e = reinterpret_cast<const uint32_t*>(&frag_m);
float* c = reinterpret_cast<float*>(&frag_c); if (psel == 0) { asm volatile(MMA_SP_INST "{%0, %1, %2, %3}, {%4, %5, %6, %7}, {%8, %9, %10,%11}, " "{%12,%13,%14,%15}, %16, 0x0;\n" : "=f"(c[0]), "=f"(c[1]), "=f"(c[2]), "=f"(c[3]) : "r"(a0[0]), "r"(a1[0]), "r"(a0[1]), "r"(a1[1]), "r"(b[0]), "r"(b[2]), "r"(b[4]), "r"(b[6]), "f"(c[0]), "f"(c[1]), "f"(c[2]), "f"(c[3]), "r"(e[0])); asm volatile(MMA_SP_INST "{%0, %1, %2, %3}, {%4, %5, %6, %7}, {%8, %9, %10,%11}, " "{%12,%13,%14,%15}, %16, 0x0;\n" : "=f"(c[4]), "=f"(c[5]), "=f"(c[6]), "=f"(c[7]) : "r"(a0[0]), "r"(a1[0]), "r"(a0[1]), "r"(a1[1]), "r"(b[1]), "r"(b[3]), "r"(b[5]), "r"(b[7]), "f"(c[4]), "f"(c[5]), "f"(c[6]), "f"(c[7]), "r"(e[0])); } else { asm volatile(MMA_SP_INST "{%0, %1, %2, %3}, {%4, %5, %6, %7}, {%8, %9, %10,%11}, " "{%12,%13,%14,%15}, %16, 0x1;\n" : "=f"(c[0]), "=f"(c[1]), "=f"(c[2]), "=f"(c[3]) : "r"(a0[0]), "r"(a1[0]), "r"(a0[1]), "r"(a1[1]), "r"(b[0]), "r"(b[2]), "r"(b[4]), "r"(b[6]), "f"(c[0]), "f"(c[1]), "f"(c[2]), "f"(c[3]), "r"(e[0])); asm volatile(MMA_SP_INST "{%0, %1, %2, %3}, {%4, %5, %6, %7}, {%8, %9, %10,%11}, " "{%12,%13,%14,%15}, %16, 0x1;\n" : "=f"(c[4]), "=f"(c[5]), "=f"(c[6]), "=f"(c[7]) : "r"(a0[0]), "r"(a1[0]), "r"(a0[1]), "r"(a1[1]), "r"(b[1]), "r"(b[3]), "r"(b[5]), "r"(b[7]), "f"(c[4]), "f"(c[5]), "f"(c[6]), "f"(c[7]), "r"(e[0])); }}
// Lookup-table based 3-input logical operation; explicitly used for// dequantization as the compiler does not seem to automatically recognize it in// all cases.template <int lut>__device__ inline int lop3(int a, int b, int c) { int res; asm volatile("lop3.b32 %0, %1, %2, %3, %4;\n" : "=r"(res) : "r"(a), "r"(b), "r"(c), "n"(lut)); return res;}
__device__ __forceinline__ uint2 to_half4(float c0, float c1, float c2, float c3) { uint2 r; asm("{\n\t" ".reg .f16 a, b, c, d; \n\t" "cvt.rn.f16.f32 a, %2; \n\t" "cvt.rn.f16.f32 b, %3; \n\t" "cvt.rn.f16.f32 c, %4; \n\t" "cvt.rn.f16.f32 d, %5; \n\t" "mov.b32 %0, {a, b}; \n\t" "mov.b32 %1, {c, d}; \n\t" "}" : "=r"(r.x), "=r"(r.y) : "f"(c0), "f"(c1), "f"(c2), "f"(c3)); return r;}
// Constructs destination register by taking bytes from 2 sources (based on// mask)template <int start_byte, int mask>__device__ inline uint32_t prmt(uint32_t a) { uint32_t res; asm volatile("prmt.b32 %0, %1, %2, %3;\n" : "=r"(res) : "r"(a), "n"(start_byte), "n"(mask)); return res;}
// Efficiently dequantize an int32 value into a full B-fragment of 4 fp16// values. We mostly follow the strategy in the link below, with some small// changes:// https://github.com/NVIDIA/FasterTransformer/blob/main/src/fastertransformer/cutlass_extensions/include/cutlass_extensions/interleaved_numeric_conversion.h__device__ inline FragB dequant_4bit(int q) { const int LO = 0x000f000f; const int HI = 0x00f000f0; const int EX = 0x64006400; // Guarantee that the `(a & b) | c` operations are LOP3s. int lo = lop3<(0xf0 & 0xcc) | 0xaa>(q, LO, EX); int hi = lop3<(0xf0 & 0xcc) | 0xaa>(q, HI, EX); // We want signed int4 outputs, hence we fuse the `-8` symmetric zero point // directly into `SUB` and `ADD`. const int SUB = 0x64086408; const int MUL = 0x2c002c00; const int ADD = 0xd480d480;
FragB frag_b; frag_b[0] = __hsub2(*reinterpret_cast<half2*>(&lo), *reinterpret_cast<const half2*>(&SUB)); frag_b[1] = __hfma2(*reinterpret_cast<half2*>(&hi), *reinterpret_cast<const half2*>(&MUL), *reinterpret_cast<const half2*>(&ADD)); return frag_b;}
// Efficiently dequantize an int32 value into a full B-fragment of 4 fp16// values. We mostly follow the strategy in the link below, with some small// changes:// https://github.com/NVIDIA/FasterTransformer/blob/main/src/fastertransformer/cutlass_extensions/include/cutlass_extensions/interleaved_numeric_conversion.h__device__ inline FragB dequant_8bit(int q) { static constexpr uint32_t mask_for_elt_01 = 0x5250; static constexpr uint32_t mask_for_elt_23 = 0x5351; static constexpr uint32_t start_byte_for_fp16 = 0x64646464;
uint32_t lo = prmt<start_byte_for_fp16, mask_for_elt_01>(q); uint32_t hi = prmt<start_byte_for_fp16, mask_for_elt_23>(q);
static constexpr uint32_t I8s_TO_F16s_MAGIC_NUM = 0x64806480;
FragB frag_b; frag_b[0] = __hsub2(*reinterpret_cast<half2*>(&lo), *reinterpret_cast<const half2*>(&I8s_TO_F16s_MAGIC_NUM)); frag_b[1] = __hsub2(*reinterpret_cast<half2*>(&hi), *reinterpret_cast<const half2*>(&I8s_TO_F16s_MAGIC_NUM)); return frag_b;}
// Multiply dequantized values by the corresponding quantization scale; used// only for grouped quantization.__device__ inline void scale(FragB& frag_b, FragS& frag_s, int i) { half2 s = __half2half2(reinterpret_cast<__half*>(&frag_s)[i]); frag_b[0] = __hmul2(frag_b[0], s); frag_b[1] = __hmul2(frag_b[1], s);}
__device__ inline void scale_floats(float* c0, float* c1, float* c2, float* c3, FragS& s0, float* c4, float* c5, float* c6, float* c7, FragS& s1) { *c0 = __fmul_rn(*c0, __half2float(s0[0].x)); *c1 = __fmul_rn(*c1, __half2float(s0[0].y)); *c2 = __fmul_rn(*c2, __half2float(s0[1].x)); *c3 = __fmul_rn(*c3, __half2float(s0[1].y));
*c4 = __fmul_rn(*c4, __half2float(s1[0].x)); *c5 = __fmul_rn(*c5, __half2float(s1[0].y)); *c6 = __fmul_rn(*c6, __half2float(s1[1].x)); *c7 = __fmul_rn(*c7, __half2float(s1[1].y));}
} // namespace marlin_24123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140/* * Notice: This file was modified by Neuralmagic inc to include 8-bit support * * Copyright (C) 2024 Roberto Lopez Castro (roberto.lopez.castro@udc.es). All * Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */#include <torch/all.h>
#include <ATen/cuda/CUDAContext.h>#include <c10/cuda/CUDAGuard.h>#include <cuda.h>#include <cuda_fp16.h>#include <cuda_runtime.h>
#include <iostream>
#include "common/base.h"#include "core/scalar_type.hpp"
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 800
#else
#include "common/mem.h" #include "common/mma.h"
#endif
template <typename T>inline std::string str(T x) { return std::to_string(x);}
namespace marlin_24 {
// 8 warps are a good choice since every SM has 4 schedulers and having more// than 1 warp per schedule allows some more latency hiding. At the same time,// we want relatively few warps to have many registers per warp and small tiles.static constexpr int THREADS = 256;static constexpr int STAGES = 4;
static constexpr int min_thread_n = 128;
static constexpr int tile_size = 16;static constexpr int max_par = 64;
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 800
template <const int num_bits, // weight bits const int threads, // number of threads in a threadblock const int thread_m_blocks, // number of 16x16 blocks in the m // dimension (batchsize) of the // threadblock const int thread_n_blocks, // same for n dimension (output) const int thread_k_blocks, // same for k dimension (reduction) const int stages, // number of stages for the async global->shared // fetch pipeline const int group_blocks = -1 // number of consecutive 16x16 blocks // with a separate quantization scale >__global__ void Marlin_24( const int4* __restrict__ A, // fp16 input matrix of shape mxk const int4* __restrict__ B, // 4bit quantized weight matrix of shape kxn const int4* __restrict__ meta, // 2bit metadata information about 2:4 // format on B int4* __restrict__ C, // fp16 output buffer of shape mxn const int4* __restrict__ s, // fp16 quantization scales of shape // (k/groupsize)xn int prob_m, // batch dimension m int prob_n, // output dimension n int prob_k, // reduction dimension k int* locks // extra global storage for barrier synchronization) {}
torch::Tensor gptq_marlin_24_gemm(torch::Tensor& a, torch::Tensor& b_q_weight, torch::Tensor& b_meta, torch::Tensor& b_scales, torch::Tensor& workspace, vllm::ScalarTypeId const b_q_type_id, int64_t size_m, int64_t size_n, int64_t size_k) { TORCH_CHECK_NOT_IMPLEMENTED( false, "gptq_marlin_24_gemm(..) requires CUDA_ARCH >= 8.0"); return torch::empty({1, 1});}
#else
template <const int num_bits, // weight bits const int threads, // number of threads in a threadblock const int thread_m_blocks, // number of 16x16 blocks in the m // dimension (batchsize) of the // threadblock const int thread_n_blocks, // same for n dimension (output) const int thread_k_blocks, // same for k dimension (reduction) const int stages, // number of stages for the async global->shared // fetch pipeline const int group_blocks = -1 // number of consecutive 16x16 blocks // with a separate quantization scale >__global__ void Marlin_24( const int4* __restrict__ A, // fp16 input matrix of shape mxk const int4* __restrict__ B, // 4bit quantized weight matrix of shape kxn const int4* __restrict__ meta, // 2bit metadata information about 2:4 // format on B int4* __restrict__ C, // fp16 output buffer of shape mxn const int4* __restrict__ s, // fp16 quantization scales of shape // (k/groupsize)xn int prob_m, // batch dimension m int prob_n, // output dimension n int prob_k, // reduction dimension k int* locks // extra global storage for barrier synchronization) { // Each threadblock processes one "stripe" of the B matrix with (roughly) the // same size, which might involve multiple column "slices" (of width 16 * // `thread_n_blocks`). Stripes are defined as shown in the 3x3 matrix 5 SM // example: // 0 1 3 // 0 2 3 // 1 2 4 // While this kind of partitioning makes things somewhat more complicated, it // ensures good utilization of all SMs for many kinds of shape and GPU // configurations, while requiring as few slow global cross-threadblock // reductions as possible.
// For larger GEMMs we run multiple batchsize 64 versions in parallel for a // better partitioning with less reductions int parallel = 1; if (prob_m > 16 * thread_m_blocks) { parallel = prob_m / (16 * thread_m_blocks); prob_m = 16 * thread_m_blocks; }
// number of thread_k_blocks in k-dim int k_tiles = prob_k / 32 / thread_k_blocks; // number of thread_n_blocks in n-dim int n_tiles = prob_n / 16 / thread_n_blocks; // iters needed to cover all slices int iters = ceildiv(k_tiles * n_tiles * parallel, gridDim.x);
// Ensure that the number of tiles in each stripe is a multiple of the // groupsize; this avoids an annoying special case where a stripe starts in // the middle of group. if (group_blocks != -1) iters = (group_blocks / thread_k_blocks) * ceildiv(iters, (group_blocks / thread_k_blocks));
int slice_row = (iters * blockIdx.x) % k_tiles; int slice_col_par = (iters * blockIdx.x) / k_tiles; int slice_col = slice_col_par; // number of threadblock tiles in the current slice int slice_iters; // total number of active threadblocks in the current slice int slice_count = 0; // index of threadblock in current slice; numbered bottom to top int slice_idx;
// We can easily implement parallel problem execution by just remapping // indices and advancing global pointers if (slice_col_par >= n_tiles) { A += (slice_col_par / n_tiles) * 16 * thread_m_blocks * prob_k / 8; C += (slice_col_par / n_tiles) * 16 * thread_m_blocks * prob_n / 8; locks += (slice_col_par / n_tiles) * n_tiles; slice_col = slice_col_par % n_tiles; }
// Compute all information about the current slice which is required for // synchronization. auto init_slice = [&]() { slice_iters = iters * (blockIdx.x + 1) - (k_tiles * slice_col_par + slice_row); if (slice_iters < 0 || slice_col_par >= n_tiles * parallel) slice_iters = 0; if (slice_iters == 0) return; if (slice_row + slice_iters > k_tiles) slice_iters = k_tiles - slice_row; slice_count = 1; slice_idx = 0; int col_first = iters * ceildiv(k_tiles * slice_col_par, iters); if (col_first <= k_tiles * (slice_col_par + 1)) { int col_off = col_first - k_tiles * slice_col_par; slice_count = ceildiv(k_tiles - col_off, iters); if (col_off > 0) slice_count++; int delta_first = iters * blockIdx.x - col_first; if (delta_first < 0 || (col_off == 0 && delta_first == 0)) slice_idx = slice_count - 1; else { slice_idx = slice_count - 1 - delta_first / iters; if (col_off > 0) slice_idx--; } } if (slice_col == n_tiles) { A += 16 * thread_m_blocks * prob_k / 8; C += 16 * thread_m_blocks * prob_n / 8; locks += n_tiles; slice_col = 0; } }; init_slice();
// RLC: 8 is vec_size -> 128-bit instructions, 8 fp16 elements int a_gl_stride = prob_k / 8; // stride of the A matrix in global memory
// stride of an A matrix tile in shared memory constexpr int a_sh_stride = 32 * thread_k_blocks / 8; // delta between subsequent A tiles in global memory constexpr int a_gl_rd_delta_o = 32 * thread_k_blocks / 8; // between subsequent accesses within a tile int a_gl_rd_delta_i = a_gl_stride * (threads / a_gl_rd_delta_o); // between shared memory writes constexpr int a_sh_wr_delta = a_sh_stride * (threads / a_gl_rd_delta_o); // between shared memory tile reads //RLC: 2 * #warps k-dim constexpr int a_sh_rd_delta_o = 4 * ((threads / 32) / (thread_n_blocks / 4)); // within a shared memory tile constexpr int a_sh_rd_delta_i = a_sh_stride * 16; // overall size of a tile constexpr int a_sh_stage = a_sh_stride * (16 * thread_m_blocks); // number of shared write iterations for a tile constexpr int a_sh_wr_iters = ceildiv(a_sh_stage, a_sh_wr_delta);
constexpr int pack_factor = 32 / num_bits;
int b_gl_stride = 16 * prob_n / (pack_factor * 4); constexpr int b_sh_stride = ((thread_n_blocks * 16) * 16 / pack_factor) / 4; constexpr int b_thread_vecs = num_bits == 4 ? 1 : 2; constexpr int b_sh_stride_threads = b_sh_stride / b_thread_vecs; int b_gl_rd_delta_o = b_gl_stride * thread_k_blocks; int b_gl_rd_delta_i = b_gl_stride * (threads / b_sh_stride_threads); constexpr int b_sh_wr_delta = threads * b_thread_vecs; constexpr int b_sh_rd_delta = threads * b_thread_vecs; constexpr int b_sh_stage = b_sh_stride * thread_k_blocks; constexpr int b_sh_wr_iters = b_sh_stage / b_sh_wr_delta;
int m_gl_stride = 2 * prob_n / 8; // (16*2*4 / 8) = 16 constexpr int m_sh_stride = (16 * thread_n_blocks) / 4; // #warps n-dim * threads/warp int m_gl_rd_delta_o = m_gl_stride * thread_k_blocks; int m_gl_rd_delta_i = m_gl_stride * (threads / m_sh_stride); constexpr int m_sh_wr_delta = threads / 2; constexpr int m_sh_rd_delta = threads / 2; constexpr int m_sh_stage = m_sh_stride * thread_k_blocks; constexpr int m_sh_iters = ceildiv(m_sh_stage, m_sh_wr_delta);
int s_gl_stride = prob_n / 8; constexpr int s_sh_stride = 16 * thread_n_blocks / 8; constexpr int s_sh_stage = s_sh_stride; int s_gl_rd_delta = s_gl_stride;
// Global A read index of current thread. int a_gl_rd = a_gl_stride * (threadIdx.x / a_gl_rd_delta_o) + (threadIdx.x % a_gl_rd_delta_o); a_gl_rd += a_gl_rd_delta_o * slice_row; // Shared write index of current thread. int a_sh_wr = a_sh_stride * (threadIdx.x / a_gl_rd_delta_o) + (threadIdx.x % a_gl_rd_delta_o); // Shared read index. int a_sh_rd = a_sh_stride * ((threadIdx.x % 32) % 16) + (threadIdx.x % 32) / 16; a_sh_rd += 4 * ((threadIdx.x / 32) / (thread_n_blocks / 4));
int b_gl_rd = b_gl_stride * (threadIdx.x / b_sh_stride_threads) + (threadIdx.x % b_sh_stride_threads) * b_thread_vecs; b_gl_rd += b_sh_stride * slice_col; b_gl_rd += b_gl_rd_delta_o * slice_row; int b_sh_wr = threadIdx.x * b_thread_vecs; int b_sh_rd = threadIdx.x * b_thread_vecs;
int m_gl_rd = m_gl_stride * (threadIdx.x / (m_sh_stride)) + (threadIdx.x % (m_sh_stride)); m_gl_rd += (m_sh_stride)*slice_col; m_gl_rd += m_gl_rd_delta_o * slice_row; int m_sh_wr = threadIdx.x; int m_sh_rd = threadIdx.x % 16 + (threadIdx.x / 32) * 16;
int s_gl_rd; if constexpr (group_blocks == -1) { s_gl_rd = s_sh_stride * slice_col + threadIdx.x; } else { s_gl_rd = s_gl_stride * ((thread_k_blocks * slice_row) / group_blocks) + s_sh_stride * slice_col + threadIdx.x; }
int s_sh_wr = threadIdx.x; int s_sh_rd; // We use a different scale layout for grouped and column-wise quantization as // we scale a `half2` tile in column-major layout in the former and in // row-major in the latter case. s_sh_rd = 8 * ((threadIdx.x / 32) % (thread_n_blocks / 4)) + (threadIdx.x % 32) / 4; // Note that in the original Marlin kernel // this is (threadIdx.x % 32) / 4
// Precompute which thread should not read memory in which iterations; this is // needed if there are more threads than required for a certain tilesize or // when the batchsize is not a multiple of 16. bool a_sh_wr_pred[a_sh_wr_iters]; #pragma unroll for (int i = 0; i < a_sh_wr_iters; i++) { a_sh_wr_pred[i] = a_sh_wr_delta * i + a_sh_wr < a_sh_stride * prob_m; } bool s_sh_wr_pred = threadIdx.x < s_sh_stride;
// To ensure that writing and reading A tiles to/from shared memory, the // latter in fragment format, is fully bank conflict free, we need to use a // rather fancy XOR-based layout. The key here is that neither reads nor // writes of the 16-byte `int4` blocks of 8 consecutive threads involve the // same shared memory banks. Further, it seems (based on NSight-Compute) that // each warp must also write a consecutive memory segment? auto transform_a = [&](int i) { int row = i / a_gl_rd_delta_o; return a_gl_rd_delta_o * row + (i % a_gl_rd_delta_o) ^ row; }; // Since the computation of this remapping is non-trivial and, due to our main // loop unrolls, all shared memory accesses are static, we simply precompute // both transformed reads and writes. int a_sh_wr_trans[a_sh_wr_iters]; #pragma unroll for (int i = 0; i < a_sh_wr_iters; i++) a_sh_wr_trans[i] = transform_a(a_sh_wr_delta * i + a_sh_wr); int a_sh_rd_trans[2][b_sh_wr_iters][thread_m_blocks]; #pragma unroll for (int i = 0; i < b_sh_wr_iters; i++) { #pragma unroll for (int j = 0; j < thread_m_blocks; j++) { a_sh_rd_trans[0][i][j] = transform_a(a_sh_rd_delta_o * i + a_sh_rd_delta_i * j + a_sh_rd); a_sh_rd_trans[1][i][j] = transform_a(a_sh_rd_delta_o * i + a_sh_rd_delta_i * j + a_sh_rd + 2); } }
// Since B-accesses have non-constant stride they have to be computed at // runtime; we break dependencies between subsequent accesses with a tile by // maintining multiple pointers (we have enough registers), a tiny // optimization. const int4* B_ptr[b_sh_wr_iters]; #pragma unroll for (int i = 0; i < b_sh_wr_iters; i++) B_ptr[i] = B + b_gl_rd_delta_i * i + b_gl_rd;
bool m_sh_wr_pred = threadIdx.x < m_sh_wr_delta; const int4* meta_ptr[m_sh_iters]; #pragma unroll for (int i = 0; i < m_sh_iters; i++) meta_ptr[i] = meta + m_gl_rd_delta_i * i + m_gl_rd;
extern __shared__ int4 sh[]; // Shared memory storage for global fetch pipelines. int4* sh_a = sh; int4* sh_b = sh_a + (stages * a_sh_stage); int4* sh_s = sh_b + (stages * b_sh_stage); int4* sh_m = sh_s + (stages * s_sh_stage); // Register storage for double buffer of shared memory reads. FragA frag_a[2][thread_m_blocks][2]; I4 frag_b_quant[2][b_thread_vecs]; FragM frag_m[2][2]; FragC frag_c[thread_m_blocks][4][2]; FragS frag_s[2][4];
// Zero accumulators. auto zero_accums = [&]() { #pragma unroll for (int i = 0; i < thread_m_blocks * 4 * 2 * 4; i++) reinterpret_cast<float*>(frag_c)[i] = 0; };
// Asynchronously fetch the next A, B and s tile from global to the next // shared memory pipeline location. auto fetch_to_shared = [&](int pipe, int a_off, bool pred = true) { if (pred) { int4* sh_a_stage = sh_a + a_sh_stage * pipe; #pragma unroll for (int i = 0; i < a_sh_wr_iters; i++) { cp_async4_pred( &sh_a_stage[a_sh_wr_trans[i]], &A[a_gl_rd_delta_i * i + a_gl_rd + a_gl_rd_delta_o * a_off], a_sh_wr_pred[i]); } int4* sh_b_stage = sh_b + b_sh_stage * pipe; #pragma unroll for (int i = 0; i < b_sh_wr_iters; i++) { #pragma unroll for (int j = 0; j < b_thread_vecs; j++) { cp_async4(&sh_b_stage[b_sh_wr_delta * i + b_sh_wr + j], B_ptr[i] + j); } B_ptr[i] += b_gl_rd_delta_o; } int4* sh_meta_stage = sh_m + m_sh_stage * pipe; #pragma unroll for (int i = 0; i < m_sh_iters; i++) { if (m_sh_wr_pred) cp_async4(&sh_meta_stage[m_sh_wr_delta * i + m_sh_wr], meta_ptr[i]); meta_ptr[i] += m_gl_rd_delta_o; } // Only fetch scales if this tile starts a new group if constexpr (group_blocks != -1) { // This assumes group_blocks >= thread_k_blocks // and would need to be modified to support smaller groups. static_assert(group_blocks >= thread_k_blocks); if (pipe % (group_blocks / thread_k_blocks) == 0) { int4* sh_s_stage = sh_s + s_sh_stage * pipe; if (s_sh_wr_pred) cp_async4(&sh_s_stage[s_sh_wr], &s[s_gl_rd]); s_gl_rd += s_gl_rd_delta; } } } // Insert a fence even when we are winding down the pipeline to ensure that // waiting is also correct at this point. cp_async_fence(); };
// Wait until the next thread tile has been loaded to shared memory. auto wait_for_stage = [&]() { // We only have `stages - 2` active fetches since we are double buffering // and can only issue the next fetch when it is guaranteed that the previous // shared memory load is fully complete (as it may otherwise be // overwritten). cp_async_wait<stages - 2>(); __syncthreads(); };
// Load the next sub-tile from the current location in the shared memory pipe // into the current register buffer. auto fetch_to_registers = [&](int k, int pipe) { // It may seem inefficient that we reload the groups for every sub-tile; // however, this does not seem to be a significant bottleneck, while some // theoretically better attempts have lead to bad instruction ordering by // the compiler and correspondingly a noticeable drop in performance. if constexpr (group_blocks != -1) { // This assumes group_blocks >= thread_k_blocks // and would need to be modified to support smaller groups. static_assert(group_blocks >= thread_k_blocks); int4* sh_s_stage = sh_s + s_sh_stage * ((group_blocks / thread_k_blocks) * (pipe / (group_blocks / thread_k_blocks))); reinterpret_cast<int4*>(&frag_s[k % 2])[0] = sh_s_stage[s_sh_rd]; } int4* sh_a_stage = sh_a + a_sh_stage * pipe; #pragma unroll for (int i = 0; i < thread_m_blocks; i++) { ldsm4(frag_a[k % 2][i][0], &sh_a_stage[a_sh_rd_trans[0][k % b_sh_wr_iters][i]]); ldsm4(frag_a[k % 2][i][1], &sh_a_stage[a_sh_rd_trans[1][k % b_sh_wr_iters][i]]); }
int4* sh_b_stage = sh_b + b_sh_stage * pipe; #pragma unroll for (int i = 0; i < b_thread_vecs; i++) { frag_b_quant[k % 2][i] = *reinterpret_cast<I4*>( &sh_b_stage[b_sh_rd_delta * (k % b_sh_wr_iters) + b_sh_rd + i]); }
// Load meta with ldsm4 int4* sh_m_stage = sh_m + m_sh_stage * pipe; ldsm4_m(frag_m[k % 2][0], &sh_m_stage[m_sh_rd_delta * (k % m_sh_iters) + m_sh_rd]); };
// Execute the actual tensor core matmul of a sub-tile. auto matmul = [&](int k) { // We have the m dimension as the inner loop in order to encourage overlapping // dequantization and matmul operations. #pragma unroll for (int j = 0; j < 4; j++) { FragB frag_b0; FragB frag_b1;
if constexpr (num_bits == 4) { int b_quant = frag_b_quant[k % 2][0][j]; int b_quant_shift = b_quant >> 8;
frag_b0 = dequant_4bit(b_quant); frag_b1 = dequant_4bit(b_quant_shift);
} else { int* frag_b_quant_ptr = reinterpret_cast<int*>(frag_b_quant[k % 2]); int b_quant_0 = frag_b_quant_ptr[j * 2 + 0]; int b_quant_1 = frag_b_quant_ptr[j * 2 + 1];
frag_b0 = dequant_8bit(b_quant_0); frag_b1 = dequant_8bit(b_quant_1); }
// If there are no groups, we can just scale the final output once and can // avoid doing so for each weight. if constexpr (group_blocks != -1) { scale(frag_b0, frag_s[k % 2][j], 0); } if constexpr (group_blocks != -1) { scale(frag_b1, frag_s[k % 2][j], 1); }
#pragma unroll for (int i = 0; i < thread_m_blocks; i++) { mma_sp(frag_b0, frag_b1, frag_a[k % 2][i][0], frag_c[i][j][0], frag_m[k % 2][j / 2], j % 2); } } };
// Since we slice across the k dimension of a tile in order to increase the // number of warps while keeping the n dimension of a tile reasonable, we have // multiple warps that accumulate their partial sums of the same output // location; which we have to reduce over in the end. We do in shared memory. auto thread_block_reduce = [&]() { constexpr int red_off = threads / b_sh_stride_threads / 2; if (red_off >= 1) { int red_idx = threadIdx.x / b_sh_stride_threads; constexpr int red_sh_stride = b_sh_stride_threads * 4 * 2; constexpr int red_sh_delta = b_sh_stride_threads; int red_sh_rd = red_sh_stride * (threadIdx.x / b_sh_stride_threads) + (threadIdx.x % b_sh_stride_threads);
// Parallel logarithmic shared memory reduction. We make sure to avoid any // unnecessary read or write iterations, e.g., for two warps we write only // once by warp 1 and read only once by warp 0. #pragma unroll for (int m_block = 0; m_block < thread_m_blocks; m_block++) { #pragma unroll for (int i = red_off; i > 0; i /= 2) { if (i <= red_idx && red_idx < 2 * i) { #pragma unroll for (int j = 0; j < 4 * 2; j++) { int red_sh_wr = red_sh_delta * j + (red_sh_rd - red_sh_stride * i); if (i < red_off) { float* c_rd = reinterpret_cast<float*>(&sh[red_sh_delta * j + red_sh_rd]); float* c_wr = reinterpret_cast<float*>(&sh[red_sh_wr]); #pragma unroll for (int k = 0; k < 4; k++) reinterpret_cast<FragC*>(frag_c)[4 * 2 * m_block + j][k] += c_rd[k] + c_wr[k]; } sh[red_sh_wr] = reinterpret_cast<int4*>(&frag_c)[4 * 2 * m_block + j]; } } __syncthreads(); } if (red_idx == 0) { #pragma unroll for (int i = 0; i < 4 * 2; i++) { float* c_rd = reinterpret_cast<float*>(&sh[red_sh_delta * i + red_sh_rd]); #pragma unroll for (int j = 0; j < 4; j++) reinterpret_cast<FragC*>(frag_c)[4 * 2 * m_block + i][j] += c_rd[j]; } } __syncthreads(); } } };
// Since multiple threadblocks may process parts of the same column slice, we // finally have to globally reduce over the results. As the striped // partitioning minimizes the number of such reductions and our outputs are // usually rather small, we perform this reduction serially in L2 cache. auto global_reduce = [&](bool first = false, bool last = false) { // We are very careful here to reduce directly in the output buffer to // maximize L2 cache utilization in this step. To do this, we write out // results in FP16 (but still reduce with FP32 compute). constexpr int active_threads = 32 * thread_n_blocks / 4; if (threadIdx.x < active_threads) { int c_gl_stride = prob_n / 8; int c_gl_wr_delta_o = 2 * 4 * c_gl_stride; int c_gl_wr_delta_i = c_gl_stride; // 8 threads (e.g., 0,4,8,12,16,20,24,28) int c_gl_wr = 2 * c_gl_stride * (threadIdx.x % 4) + 8 * (threadIdx.x / 32) + (threadIdx.x % 32) / 4; c_gl_wr += (2 * thread_n_blocks) * slice_col; constexpr int c_sh_wr_delta = active_threads; int c_sh_wr = threadIdx.x;
int col = 2 * ((threadIdx.x % 32) % 4);
if (!first) { // Interestingly, doing direct global accesses here really seems to mess up // the compiler and lead to slowdowns, hence we also use async-copies even // though these fetches are not actually asynchronous. #pragma unroll for (int i = 0; i < thread_m_blocks * 4; i++) { cp_async4_pred(&sh[c_sh_wr + c_sh_wr_delta * i], &C[c_gl_wr + c_gl_wr_delta_o * (i / 2) + c_gl_wr_delta_i * (i % 2)], i < (thread_m_blocks - 1) * 4 || 8 * (i / 2) + col + (i % 2) < prob_m); } cp_async_fence(); cp_async_wait<0>(); }
#pragma unroll for (int i = 0; i < thread_m_blocks * 4; i++) { if (i < (thread_m_blocks - 1) * 4 || 8 * (i / 2) + col + (i % 2) < prob_m) { if (!first) { int4 c_red = sh[c_sh_wr + i * c_sh_wr_delta]; #pragma unroll for (int j2 = 0; j2 < 2; j2++) { #pragma unroll for (int j1 = 0; j1 < 4; j1++) { reinterpret_cast<float*>( &frag_c)[4 * 2 * 4 * (i / 4) + 8 * j1 + 2 * j2 + 4 * ((i % 4) / 2) + i % 2] += __half2float( reinterpret_cast<__half*>(&c_red)[(j2 * 4 + j1)]); } } } if (!last) { int4 c; #pragma unroll for (int j2 = 0; j2 < 2; j2++) { #pragma unroll for (int j1 = 0; j1 < 4; j1++) { reinterpret_cast<__half*>(&c)[(j2 * 4 + j1)] = __float2half(reinterpret_cast<float*>( &frag_c)[4 * 2 * 4 * (i / 4) + 8 * j1 + 2 * j2 + 4 * ((i % 4) / 2) + i % 2]); } } C[c_gl_wr + c_gl_wr_delta_o * (i / 2) + c_gl_wr_delta_i * (i % 2)] = c; } } } } };
// Write out the reduce final result in the correct layout. We only actually // reshuffle matrix fragments in this step, the reduction above is performed // in fragment layout. auto write_result = [&]() { int c_gl_stride = prob_n / 8;
constexpr int c_sh_stride = 2 * thread_n_blocks; // RLC: constexpr int c_sh_stride_2 = 2 * c_sh_stride + 2; // RLC: constexpr int c_sh_stride_3 = 2 * (2 * thread_n_blocks) + 2; // RLC:
int c_gl_wr_delta = c_gl_stride * (threads / (2 * thread_n_blocks));
int c_gl_wr = c_gl_stride * (threadIdx.x / (2 * thread_n_blocks)) + (threadIdx.x % (2 * thread_n_blocks)); c_gl_wr += (2 * thread_n_blocks) * slice_col;
int c_sh_wr = c_sh_stride_2 * ((threadIdx.x % 32) % 4) + ((threadIdx.x % 32) / 4); // RLC: c_sh_wr += 8 * (threadIdx.x / 32); // 128/4(half4)
constexpr int c_sh_rd_delta = c_sh_stride_3 * (threads / (2 * 2 * thread_n_blocks)); // RLC: int c_sh_rd = c_sh_stride_3 * (threadIdx.x / (2 * 2 * thread_n_blocks)) + (threadIdx.x % (2 * 2 * thread_n_blocks));
int c_gl_wr_end = c_gl_stride * prob_m;
auto write = [&](int idx, float c0, float c1, float c2, float c3, FragS& s0, float c4, float c5, float c6, float c7, FragS& s1) { uint2 res[2]; res[0] = to_half4(c0, c1, c2, c3); res[1] = to_half4(c4, c5, c6, c7); half2* tmp = (half2*)&res; // for per-column quantization we finally apply the scale here if constexpr (group_blocks == -1 && num_bits == 4) { tmp[0] = __hmul2(tmp[0], s0[0]); tmp[1] = __hmul2(tmp[1], s0[1]); tmp[2] = __hmul2(tmp[2], s1[0]); tmp[3] = __hmul2(tmp[3], s1[1]); } ((int4*)sh)[idx] = *((int4*)&res[0]); };
// RLC: only warp 0 and 1 baseline example if (threadIdx.x / 32 < thread_n_blocks / 4) { #pragma unroll for (int i = 0; i < thread_m_blocks; i++) { int wr = c_sh_wr; write(wr, frag_c[i][0][0][0], frag_c[i][1][0][0], frag_c[i][2][0][0], frag_c[i][3][0][0], frag_s[0][0], frag_c[i][0][0][2], frag_c[i][1][0][2], frag_c[i][2][0][2], frag_c[i][3][0][2], frag_s[0][2]); write(wr + c_sh_stride, frag_c[i][0][0][1], frag_c[i][1][0][1], frag_c[i][2][0][1], frag_c[i][3][0][1], frag_s[0][0], frag_c[i][0][0][3], frag_c[i][1][0][3], frag_c[i][2][0][3], frag_c[i][3][0][3], frag_s[0][2]); write(wr + 4 * c_sh_stride_2, frag_c[i][0][1][0], frag_c[i][1][1][0], frag_c[i][2][1][0], frag_c[i][3][1][0], frag_s[0][0], frag_c[i][0][1][2], frag_c[i][1][1][2], frag_c[i][2][1][2], frag_c[i][3][1][2], frag_s[0][2]); write(wr + 4 * c_sh_stride_2 + c_sh_stride, frag_c[i][0][1][1], frag_c[i][1][1][1], frag_c[i][2][1][1], frag_c[i][3][1][1], frag_s[0][0], frag_c[i][0][1][3], frag_c[i][1][1][3], frag_c[i][2][1][3], frag_c[i][3][1][3], frag_s[0][2]);
c_sh_wr += 8 * c_sh_stride_2; } } __syncthreads();
#pragma unroll for (int i = 0; i < ceildiv(16 * thread_m_blocks, threads / (2 * thread_n_blocks)); i++) { if (c_gl_wr < c_gl_wr_end) { C[c_gl_wr] = sh[c_sh_rd]; c_gl_wr += c_gl_wr_delta; c_sh_rd += c_sh_rd_delta; } } };
// Start global fetch and register load pipelines. auto start_pipes = [&]() { #pragma unroll for (int i = 0; i < stages - 1; i++) fetch_to_shared(i, i, i < slice_iters); zero_accums(); wait_for_stage(); fetch_to_registers(0, 0); a_gl_rd += a_gl_rd_delta_o * (stages - 1); }; start_pipes();
// Main loop. while (slice_iters) { // We unroll over both the global fetch and the register load pipeline to // ensure all shared memory accesses are static. Note that both pipelines have // even length meaning that the next iteration will always start at index 0. #pragma unroll for (int pipe = 0; pipe < stages;) { fetch_to_shared((pipe + stages - 1) % stages, pipe, slice_iters >= stages); matmul(pipe); wait_for_stage();
fetch_to_registers(pipe + 1, (pipe + 1) % stages);
pipe++; slice_iters--; if (slice_iters == 0) break; } a_gl_rd += a_gl_rd_delta_o * stages;
// Process results and, if necessary, proceed to the next column slice. // While this pattern may not be the most readable, other ways of writing // the loop seemed to noticeably worse performance after compilation. if (slice_iters == 0) { cp_async_wait<0>(); bool last = slice_idx == slice_count - 1; // For per-column scales, we only fetch them here in the final step before // write-out if constexpr (group_blocks == -1) { if constexpr (num_bits == 8) { if (s_sh_wr_pred) cp_async4(&sh_s[s_sh_wr], &s[s_gl_rd]); cp_async_fence(); } else { if (last) { if (s_sh_wr_pred) cp_async4(&sh_s[s_sh_wr], &s[s_gl_rd]); cp_async_fence(); } } } thread_block_reduce();
if constexpr (group_blocks == -1) { if constexpr (num_bits == 8) { cp_async_wait<0>(); __syncthreads(); if (threadIdx.x / 32 < thread_n_blocks / 4) { *(float4*)(frag_s) = *(float4*)(&sh_s[s_sh_rd]); } } else { if (last) { cp_async_wait<0>(); __syncthreads(); if (threadIdx.x / 32 < thread_n_blocks / 4) { *(float4*)(frag_s) = *(float4*)(&sh_s[s_sh_rd]); } } } }
// For 8-bit channelwise, we apply the scale before the global reduction // that converts the fp32 results to fp16 (so that we avoid possible // overflow in fp16) if constexpr (group_blocks == -1 && num_bits == 8) { if (threadIdx.x / 32 < thread_n_blocks / 4) { #pragma unroll for (int i = 0; i < thread_m_blocks; i++) { scale_floats(&frag_c[i][0][0][0], &frag_c[i][1][0][0], &frag_c[i][2][0][0], &frag_c[i][3][0][0], frag_s[0][0], &frag_c[i][0][0][2], &frag_c[i][1][0][2], &frag_c[i][2][0][2], &frag_c[i][3][0][2], frag_s[0][2]);
scale_floats(&frag_c[i][0][0][1], &frag_c[i][1][0][1], &frag_c[i][2][0][1], &frag_c[i][3][0][1], frag_s[0][0], &frag_c[i][0][0][3], &frag_c[i][1][0][3], &frag_c[i][2][0][3], &frag_c[i][3][0][3], frag_s[0][2]);
scale_floats(&frag_c[i][0][1][0], &frag_c[i][1][1][0], &frag_c[i][2][1][0], &frag_c[i][3][1][0], frag_s[0][0], &frag_c[i][0][1][2], &frag_c[i][1][1][2], &frag_c[i][2][1][2], &frag_c[i][3][1][2], frag_s[0][2]);
scale_floats(&frag_c[i][0][1][1], &frag_c[i][1][1][1], &frag_c[i][2][1][1], &frag_c[i][3][1][1], frag_s[0][0], &frag_c[i][0][1][3], &frag_c[i][1][1][3], &frag_c[i][2][1][3], &frag_c[i][3][1][3], frag_s[0][2]); } } }
if (slice_count > 1) { // only globally reduce if there is more than one // block in a slice barrier_acquire(&locks[slice_col], slice_idx); global_reduce(slice_idx == 0, last); barrier_release(&locks[slice_col], last); } if (last) // only the last block in a slice actually writes the result write_result();
slice_row = 0; slice_col_par++; slice_col++; init_slice(); if (slice_iters) { a_gl_rd = a_gl_stride * (threadIdx.x / a_gl_rd_delta_o) + (threadIdx.x % a_gl_rd_delta_o); #pragma unroll for (int i = 0; i < b_sh_wr_iters; i++) B_ptr[i] += b_sh_stride - b_gl_rd_delta_o * k_tiles; #pragma unroll for (int i = 0; i < m_sh_iters; i++) meta_ptr[i] += (m_sh_stride)-m_gl_rd_delta_o * k_tiles; if (slice_col == 0) { #pragma unroll for (int i = 0; i < b_sh_wr_iters; i++) B_ptr[i] -= b_gl_stride; #pragma unroll for (int i = 0; i < m_sh_iters; i++) meta_ptr[i] -= m_gl_stride; } s_gl_rd = s_sh_stride * slice_col + threadIdx.x; start_pipes(); } } }}
#endif
#define CALL_IF_2_4(NUM_BITS, THREAD_M_BLOCKS, THREAD_N_BLOCKS, \ THREAD_K_BLOCKS, GROUP_BLOCKS) \ else if (num_bits == NUM_BITS && thread_m_blocks == THREAD_M_BLOCKS && \ thread_n_blocks == THREAD_N_BLOCKS && \ thread_k_blocks == THREAD_K_BLOCKS && \ group_blocks == GROUP_BLOCKS) { \ cudaFuncSetAttribute( \ Marlin_24<NUM_BITS, THREADS, THREAD_N_BLOCKS, THREAD_M_BLOCKS, \ THREAD_K_BLOCKS, STAGES, GROUP_BLOCKS>, \ cudaFuncAttributeMaxDynamicSharedMemorySize, max_shared_mem); \ Marlin_24<NUM_BITS, THREADS, THREAD_N_BLOCKS, THREAD_M_BLOCKS, \ THREAD_K_BLOCKS, STAGES, GROUP_BLOCKS> \ <<<blocks, THREADS, max_shared_mem, stream>>>(A_ptr, B_ptr, meta_ptr, \ C_ptr, s_ptr, prob_n, \ prob_m, prob_k, locks); \ }
void marlin_cuda_2_4(const void* A, const void* B, const void* meta, void* C, void* s, int prob_m, int prob_n, int prob_k, void* workspace, int num_bits, int groupsize = -1, int dev = 0, cudaStream_t stream = 0, int thread_k = -1, int thread_m = -1, int sms = -1, int max_par = 16) { int tot_n = prob_n; int tot_n_blocks = ceildiv(tot_n, 16); int pad = 16 * tot_n_blocks - tot_n;
if (sms == -1) { cudaDeviceGetAttribute(&sms, cudaDevAttrMultiProcessorCount, dev); } TORCH_CHECK(sms > 0);
int max_shared_mem = 0; cudaDeviceGetAttribute(&max_shared_mem, cudaDevAttrMaxSharedMemoryPerBlockOptin, dev); TORCH_CHECK(max_shared_mem > 0);
if (thread_k == -1 || thread_m == -1) { if (prob_n <= 16) { // For small batchizes, better partitioningif is slightly more important // than better compute utilization thread_k = 128; thread_m = 128; } else { thread_k = 64; thread_m = 256; } // Also had // if prob_n > 256 // thread_k = 32; // thread_m = 512; // but this is broken, // TODO(Lucas, Alex M): figure out why }
int thread_k_blocks = thread_k / 32; // 2:4 version with m16n8k32 instruction int thread_m_blocks = thread_m / 16; int group_blocks = (groupsize == -1) ? -1 : groupsize / 16; int blocks = sms;
TORCH_CHECK(prob_m % thread_m == 0, "prob_m = ", prob_m, " is not divisible by thread_m = ", thread_m); TORCH_CHECK(prob_k % thread_k == 0, "prob_k = ", prob_k, " is not divisible by thread_k = ", thread_k); if (group_blocks != -1) { TORCH_CHECK((prob_k / 2) % group_blocks == 0, "prob_k/2 = ", prob_k / 2, " is not divisible by group_blocks = ", group_blocks); }
TORCH_CHECK(prob_m > 0 && prob_n > 0 && prob_k > 0, "Invalid MNK = [", prob_m, ", ", prob_n, ", ", prob_k, "]");
const int4* A_ptr = (const int4*)A; const int4* B_ptr = (const int4*)B; const int4* meta_ptr = (const int4*)meta; int4* C_ptr = (int4*)C; const int4* s_ptr = (const int4*)s;
constexpr int max_m_blocks = 4;
int* locks = (int*)workspace; for (int i = 0; i < tot_n_blocks; i += max_m_blocks) { int thread_n_blocks = tot_n_blocks - i; prob_n = tot_n - 16 * i; int par = 1; if (thread_n_blocks > max_m_blocks) { // Note that parallel > 1 currently only works for inputs without any // padding par = (16 * thread_n_blocks - pad) / (max_m_blocks * 16); if (par > max_par) par = max_par; prob_n = (max_m_blocks * 16) * par; i += max_m_blocks * (par - 1); thread_n_blocks = max_m_blocks; }
// For compilation speed, we only define the kernel configurations that have // seemed useful (in terms of performance) in our testing, however many more // are, in principle, possible.
// the false is start of the CALL_IF macros if (false) { } // BMxBNxBK, group // 4-bit CALL_IF_2_4(4, 8, 1, 4, -1) // e.g., 16x128x128 CALL_IF_2_4(4, 8, 1, 4, 4) // e.g., 16x128x128, 64
CALL_IF_2_4(4, 16, 1, 2, -1) // e.g., 16x256x64 CALL_IF_2_4(4, 16, 1, 2, 4) // e.g., 16x256x64, 64 CALL_IF_2_4(4, 16, 2, 2, -1) // e.g.. 32x256x64 CALL_IF_2_4(4, 16, 2, 2, 4) CALL_IF_2_4(4, 16, 3, 2, -1) CALL_IF_2_4(4, 16, 3, 2, 4) CALL_IF_2_4(4, 16, 4, 2, -1) CALL_IF_2_4(4, 16, 4, 2, 4)
CALL_IF_2_4(4, 32, 1, 1, -1) // e.g., 16x256x64 CALL_IF_2_4(4, 32, 1, 1, 4) // e.g., 16x256x64, 64 CALL_IF_2_4(4, 32, 2, 1, -1) // e.g.. 32x256x64 CALL_IF_2_4(4, 32, 2, 1, 4) CALL_IF_2_4(4, 32, 3, 1, -1) CALL_IF_2_4(4, 32, 3, 1, 4) CALL_IF_2_4(4, 32, 4, 1, -1) CALL_IF_2_4(4, 32, 4, 1, 4)
// 8-bit CALL_IF_2_4(8, 8, 1, 4, -1) // e.g., 16x128x128 CALL_IF_2_4(8, 8, 1, 4, 4) // e.g., 16x128x128, 64
CALL_IF_2_4(8, 16, 1, 2, -1) // e.g., 16x256x64 CALL_IF_2_4(8, 16, 1, 2, 4) // e.g., 16x256x64, 64 CALL_IF_2_4(8, 16, 2, 2, -1) // e.g.. 32x256x64 CALL_IF_2_4(8, 16, 2, 2, 4) CALL_IF_2_4(8, 16, 3, 2, -1) CALL_IF_2_4(8, 16, 3, 2, 4) CALL_IF_2_4(8, 16, 4, 2, -1) CALL_IF_2_4(8, 16, 4, 2, 4)
CALL_IF_2_4(8, 32, 1, 1, -1) // e.g., 16x256x64 CALL_IF_2_4(8, 32, 1, 1, 4) // e.g., 16x256x64, 64 CALL_IF_2_4(8, 32, 2, 1, -1) // e.g.. 32x256x64 CALL_IF_2_4(8, 32, 2, 1, 4) CALL_IF_2_4(8, 32, 3, 1, -1) CALL_IF_2_4(8, 32, 3, 1, 4) CALL_IF_2_4(8, 32, 4, 1, -1) CALL_IF_2_4(8, 32, 4, 1, 4) else { throw std::runtime_error("Unsupported shapes: MKN = [" + str(prob_m) + ", " + str(prob_k) + ", " + str(prob_n) + "]" + ", groupsize = " + str(groupsize) + ", thread_m_blocks = " + str(thread_m_blocks) + ", thread_n_blocks = " + str(thread_n_blocks) + ", thread_k_blocks = " + str(thread_k_blocks)); }
A_ptr += 16 * thread_n_blocks * (prob_k / 8) * par; C_ptr += 16 * thread_n_blocks * (prob_m / 8) * par; }}
} // namespace marlin_24
torch::Tensor gptq_marlin_24_gemm(torch::Tensor& a, torch::Tensor& b_q_weight, torch::Tensor& b_meta, torch::Tensor& b_scales, torch::Tensor& workspace, vllm::ScalarTypeId const b_q_type_id, int64_t size_m, int64_t size_n, int64_t size_k) { vllm::ScalarType const b_q_type = vllm::ScalarType::from_id(b_q_type_id); // Verify num_bits TORCH_CHECK(b_q_type == vllm::kU4B8 || b_q_type == vllm::kU8B128, "num_bits must be uint4b8 or uint8b128. Got = ", b_q_type.str()); int pack_factor = 32 / b_q_type.size_bits();
// Verify M TORCH_CHECK(size_m == a.size(0), "Shape mismatch: a.size(0) = " + str(a.size(0)) + ", size_m = " + str(size_m));
// Verify K TORCH_CHECK(size_k == a.size(1), "Shape mismatch: a.size(1) = " + str(a.size(1)) + ", size_k = " + str(size_k)); TORCH_CHECK(size_k % marlin_24::tile_size == 0, "size_k = " + str(size_k) + " is not divisible by tile_size = " + str(marlin_24::tile_size)); TORCH_CHECK((size_k / marlin_24::tile_size / 2) == b_q_weight.size(0), "Shape mismatch: b_q_weight.size(0) = " + str(b_q_weight.size(0)) + ", size_k = " + str(size_k) + ", tile_size = " + str(marlin_24::tile_size));
// Verify N TORCH_CHECK(b_scales.size(1) == size_n, "b_scales.size(1) = " + str(b_scales.size(1)) + ", size_n = " + str(size_n)); TORCH_CHECK( b_q_weight.size(1) % marlin_24::tile_size == 0, "b_q_weight.size(1) = " + str(b_q_weight.size(1)) + " is not divisible by tile_size = " + str(marlin_24::tile_size));
int actual_size_n = (b_q_weight.size(1) / marlin_24::tile_size) * pack_factor; TORCH_CHECK( size_n == actual_size_n, "size_n = " + str(size_n) + ", actual_size_n = " + str(actual_size_n));
// Verify meta TORCH_CHECK(b_meta.size(0) == size_k / 8 / 2 / 2, "b_meta.size(0) = ", b_meta.size(0), " is not size_k / 8 / 2 / 2 = ", size_k / 8 / 2 / 2); TORCH_CHECK(b_meta.size(1) == size_n * 2, "b_meta.size(1) = ", b_meta.size(1), " is not size_n * 2 = ", size_n * 2);
// Verify A device and strides TORCH_CHECK(a.device().is_cuda(), "A is not on GPU"); TORCH_CHECK(a.is_contiguous(), "A is not contiguous"); TORCH_CHECK(a.dtype() == torch::kFloat16, "A is not float16, currently only float16 is supported");
// Verify B device and strides TORCH_CHECK(b_q_weight.device().is_cuda(), "b_q_weight is not on GPU"); TORCH_CHECK(b_q_weight.is_contiguous(), "b_q_weight is not contiguous");
// Verify b_meta device and strides TORCH_CHECK(b_meta.device().is_cuda(), "b_meta is not on GPU"); TORCH_CHECK(b_meta.is_contiguous(), "b_meta is not contiguous");
// Verify scales device and strides TORCH_CHECK(b_scales.device().is_cuda(), "b_scales is not on GPU"); TORCH_CHECK(b_scales.is_contiguous(), "b_scales is not contiguous"); TORCH_CHECK(b_scales.dtype() == torch::kFloat16, "A is not float16, currently only float16 is supported");
// Alloc C matrix const at::cuda::OptionalCUDAGuard device_guard(device_of(a)); auto options = torch::TensorOptions().dtype(a.dtype()).device(a.device()); torch::Tensor c = torch::empty({size_m, size_n}, options);
int thread_k = -1; int thread_m = -1; int sms = -1; int max_par = marlin_24::max_par;
int groupsize = -1; if (b_scales.size(0) > 1) { TORCH_CHECK(size_k % b_scales.size(0) == 0, "size_k = " + str(size_k) + ", is not divisible by b_scales.size(0) = " + str(b_scales.size(0))); groupsize = size_k / b_scales.size(0); groupsize /= 2; // Because of 24 }
// Verify groupsize TORCH_CHECK(groupsize == -1 || groupsize == 64, "Unexpected groupsize = " + str(groupsize));
// Verify workspace size TORCH_CHECK(size_n % marlin_24::min_thread_n == 0, "size_n = " + str(size_n) + ", is not divisible by min_thread_n = " + str(marlin_24::min_thread_n)); int min_workspace_size = (size_n / marlin_24::min_thread_n) * marlin_24::max_par; TORCH_CHECK(workspace.numel() >= min_workspace_size, "workspace.numel = " + str(workspace.numel()) + " is below min_workspace_size = " + str(min_workspace_size));
int dev = a.get_device(); marlin_24::marlin_cuda_2_4( a.data_ptr(), b_q_weight.data_ptr(), b_meta.data_ptr(), c.data_ptr(), b_scales.data_ptr(), size_n, size_m, size_k, workspace.data_ptr(), b_q_type.size_bits(), groupsize, dev, at::cuda::getCurrentCUDAStream(dev), thread_k, thread_m, sms, max_par);
return c;}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733"""Tests for the marlin kernel.
Run `pytest tests/kernels/marlin/test_marlin_gemm.py`."""
import pytestimport torch
import quantization
from tests.kernels.utils import DEFAULT_OPCHECK_TEST_UTILS, opcheck
from quantization.utils.marlin_utils import ( GPTQ_MARLIN_24_MAX_PARALLEL, GPTQ_MARLIN_24_MIN_THREAD_N, GPTQ_MARLIN_24_SUPPORTED_GROUP_SIZES, GPTQ_MARLIN_24_SUPPORTED_QUANT_TYPES, GPTQ_MARLIN_MAX_PARALLEL, GPTQ_MARLIN_MIN_THREAD_N, MARLIN_SUPPORTED_GROUP_SIZES, MARLIN_QQQ_MAX_PARALLEL, MARLIN_QQQ_MIN_THREAD_N, MARLIN_QQQ_SUPPORTED_GROUP_SIZES, MARLIN_QQQ_SUPPORTED_NUM_BITS, marlin_make_empty_g_idx, marlin_permute_scales, query_marlin_supported_quant_types,)from quantization.utils.marlin_utils_fp8 import ( pack_fp8_to_int32,)from quantization.utils.quant_utils import ( awq_pack, gptq_pack, gptq_quantize_weights, quantize_weights, sort_weights,)from quantization.scalar_type import scalar_types
from quantization.utils.marlin_utils_test import ( MarlinWorkspace, awq_marlin_quantize, get_weight_perm, marlin_quantize, marlin_weights,)from quantization.utils.marlin_utils_test_24 import ( marlin_24_quantize,)from quantization.utils.marlin_utils_test_qqq import ( # noqa: E501 marlin_qqq_quantize,)
# Avoid torch._dynamo.exc.Unsupported: cache_size_limit reachedtorch._dynamo.config.cache_size_limit = 128
capability = torch.cuda.get_device_capability()capability = capability[0] * 10 + capability[1]
ACT_ORDER_OPTS = [False, True]K_FULL_OPTS = [False, True]USE_FP32_REDUCE_OPTS = [False, True]
MARLIN_K_CHUNKS = [128]MARLIN_N_CHUNKS = [64, 256]
MARLIN_24_K_CHUNKS = [128]MARLIN_24_N_CHUNKS = [512]
HQQ_SUPPORTED_GROUP_SIZES = [64]
MNK_FACTORS = [ (1, 1, 1), (1, 4, 8), (1, 7, 5), (13, 17, 67), (26, 37, 13), (67, 13, 11), (257, 13, 11), (658, 13, 11),]
DTYPES = [torch.float16, torch.bfloat16]
def compute_max_diff(output, output_ref): return torch.mean(torch.abs(output - output_ref)) / torch.mean( torch.abs(output_ref) )
def rand_data(shape, dtype=torch.float16): return torch.randn(shape, dtype=dtype, device="cuda")
@pytest.mark.skipif( capability < 80, reason="Marlin is not supported on this GPU type.",)@pytest.mark.parametrize("k_chunk", MARLIN_K_CHUNKS)@pytest.mark.parametrize("n_chunk", MARLIN_N_CHUNKS)@pytest.mark.parametrize("quant_type", query_marlin_supported_quant_types(False))@pytest.mark.parametrize("group_size", MARLIN_SUPPORTED_GROUP_SIZES)@pytest.mark.parametrize("act_order", ACT_ORDER_OPTS)@pytest.mark.parametrize("mnk_factors", MNK_FACTORS)def test_gptq_marlin_repack( k_chunk, n_chunk, quant_type, group_size, act_order, mnk_factors): m_factor, n_factor, k_factor = mnk_factors
size_k = k_chunk * k_factor size_n = n_chunk * n_factor
# Filter act_order if act_order: if group_size == -1: return if group_size == size_k: return
# Normalize group_size if group_size == -1: group_size = size_k assert group_size <= size_k
# Create input b_weight = rand_data((size_k, size_n))
# Quantize (and apply act_order if provided) w_ref, q_w, s, g_idx, rand_perm = gptq_quantize_weights( b_weight, quant_type, group_size, act_order )
# Pack to GPTQ format q_w_gptq = gptq_pack(q_w, quant_type.size_bits, size_k, size_n)
# For act_order, sort the "weights" and "g_idx" so that group ids are # increasing sort_indices = torch.empty(0, dtype=torch.int, device=b_weight.device) if act_order: q_w, g_idx, sort_indices = sort_weights(q_w, g_idx)
# Pack to Marlin format weight_perm = get_weight_perm(quant_type.size_bits) marlin_q_w_1 = marlin_weights( q_w, size_k, size_n, quant_type.size_bits, weight_perm )
opcheck( quantization._ops.ops.gptq_marlin_repack, (q_w_gptq, sort_indices, size_k, size_n, quant_type.size_bits), )
# Run Marlin repack GPU kernel marlin_q_w_2 = quantization.gptq_marlin_repack( q_w_gptq, sort_indices, size_k, size_n, quant_type.size_bits, ) torch.cuda.synchronize()
torch.testing.assert_close(marlin_q_w_1, marlin_q_w_2)
@pytest.mark.skipif( capability < 80, reason="Marlin is not supported on this GPU type.",)@pytest.mark.parametrize("k_chunk", MARLIN_K_CHUNKS)@pytest.mark.parametrize("n_chunk", MARLIN_N_CHUNKS)@pytest.mark.parametrize("quant_type", query_marlin_supported_quant_types(False))@pytest.mark.parametrize("group_size", MARLIN_SUPPORTED_GROUP_SIZES)@pytest.mark.parametrize("mnk_factors", MNK_FACTORS)def test_awq_marlin_repack(k_chunk, n_chunk, quant_type, group_size, mnk_factors): m_factor, n_factor, k_factor = mnk_factors
size_k = k_chunk * k_factor size_n = n_chunk * n_factor
# Normalize group_size if group_size == -1: group_size = size_k assert group_size <= size_k
# Create input b_weight = rand_data((size_k, size_n))
# Quantize w_ref, q_w, s, zp = quantize_weights( b_weight, quant_type, group_size, zero_points=True )
# Pack to AWQ format q_w_awq = awq_pack(q_w, quant_type.size_bits, size_k, size_n)
# Pack to Marlin format weight_perm = get_weight_perm(quant_type.size_bits) marlin_q_w_1 = marlin_weights( q_w, size_k, size_n, quant_type.size_bits, weight_perm )
opcheck( quantization._ops.ops.awq_marlin_repack, (q_w_awq, size_k, size_n, quant_type.size_bits) )
# Run Marlin repack GPU kernel marlin_q_w_2 = quantization.awq_marlin_repack( q_w_awq, size_k, size_n, quant_type.size_bits, ) torch.cuda.synchronize()
torch.testing.assert_close(marlin_q_w_1, marlin_q_w_2)
@pytest.mark.skipif( capability < 80, reason="Marlin is not supported on this GPU type.",)@pytest.mark.parametrize("k_chunk", MARLIN_K_CHUNKS)@pytest.mark.parametrize("n_chunk", MARLIN_N_CHUNKS)@pytest.mark.parametrize("quant_type", query_marlin_supported_quant_types(False))@pytest.mark.parametrize("group_size", MARLIN_SUPPORTED_GROUP_SIZES)@pytest.mark.parametrize("mnk_factors", MNK_FACTORS)@pytest.mark.parametrize("act_order", ACT_ORDER_OPTS)@pytest.mark.parametrize("is_k_full", K_FULL_OPTS)@pytest.mark.parametrize("use_fp32_reduce", USE_FP32_REDUCE_OPTS)def test_gptq_marlin_gemm( k_chunk, n_chunk, quant_type, group_size, mnk_factors, act_order, is_k_full, use_fp32_reduce,): m_factor, n_factor, k_factor = mnk_factors
size_m = m_factor size_k = k_chunk * k_factor size_n = n_chunk * n_factor
if act_order: if group_size == -1: return if group_size == size_k: return
a_input = rand_data((size_m, size_k)) b_weight = rand_data((size_k, size_n))
w_ref, marlin_q_w, marlin_s, g_idx, sort_indices, _ = marlin_quantize( b_weight, quant_type, group_size, act_order )
marlin_zp = marlin_make_empty_g_idx(marlin_s.device)
workspace = MarlinWorkspace( size_n, GPTQ_MARLIN_MIN_THREAD_N, GPTQ_MARLIN_MAX_PARALLEL )
opcheck( quantization._ops.ops.gptq_marlin_gemm, ( a_input, marlin_q_w, marlin_s, marlin_zp, g_idx, sort_indices, workspace.scratch, quant_type.id, a_input.shape[0], b_weight.shape[1], a_input.shape[1], is_k_full, False, use_fp32_reduce, False, ), test_utils=DEFAULT_OPCHECK_TEST_UTILS, )
output = quantization.gptq_marlin_gemm( a_input, marlin_q_w, marlin_s, marlin_zp, g_idx, sort_indices, workspace.scratch, quant_type, a_input.shape[0], b_weight.shape[1], a_input.shape[1], is_k_full=is_k_full, has_zp=False, use_fp32_reduce=use_fp32_reduce, is_zp_float=False, ) output_ref = torch.matmul(a_input, w_ref)
torch.cuda.synchronize()
max_diff = compute_max_diff(output, output_ref)
assert max_diff < 0.04
# TODO: find better way to test this?@torch.compile(fullgraph=True)def marlin_24_gemm_tester( a_input, marlin_24_q_w_comp, marlin_24_meta, marlin_24_s, scratch, quant_type, size_m, size_n, size_k,): return quantization.gptq_marlin_24_gemm( a_input, marlin_24_q_w_comp, marlin_24_meta, marlin_24_s, scratch, quant_type, size_m, size_n, size_k, )
@pytest.mark.skipif( capability < 80, reason="Marlin is not supported on this GPU type.",)@pytest.mark.parametrize("k_chunk", MARLIN_24_K_CHUNKS)@pytest.mark.parametrize("n_chunk", MARLIN_24_N_CHUNKS)@pytest.mark.parametrize("quant_type", GPTQ_MARLIN_24_SUPPORTED_QUANT_TYPES)@pytest.mark.parametrize("group_size", GPTQ_MARLIN_24_SUPPORTED_GROUP_SIZES)@pytest.mark.parametrize("mnk_factors", MNK_FACTORS)def test_gptq_marlin_24_gemm(k_chunk, n_chunk, quant_type, group_size, mnk_factors): m_factor, n_factor, k_factor = mnk_factors
size_m = m_factor size_k = k_chunk * k_factor size_n = n_chunk * n_factor
a_input = rand_data((size_m, size_k)) b_weight = rand_data((size_k, size_n))
(w_24_ref, marlin_24_q_w_comp, marlin_24_meta, marlin_24_s) = marlin_24_quantize( b_weight, quant_type, group_size )
workspace_24 = MarlinWorkspace( size_n, GPTQ_MARLIN_24_MIN_THREAD_N, GPTQ_MARLIN_24_MAX_PARALLEL )
output_ref = torch.matmul(a_input, w_24_ref)
opcheck( quantization._ops.ops.gptq_marlin_24_gemm, ( a_input, marlin_24_q_w_comp, marlin_24_meta, marlin_24_s, workspace_24.scratch, quant_type.id, a_input.shape[0], b_weight.shape[1], a_input.shape[1], ), test_utils=DEFAULT_OPCHECK_TEST_UTILS, )
output = marlin_24_gemm_tester( a_input, marlin_24_q_w_comp, marlin_24_meta, marlin_24_s, workspace_24.scratch, quant_type, a_input.shape[0], b_weight.shape[1], a_input.shape[1], )
torch.cuda.synchronize()
max_diff = compute_max_diff(output, output_ref)
assert max_diff < 0.04
@pytest.mark.skipif( capability < 80, reason="Marlin is not supported on this GPU type.",)@pytest.mark.parametrize("k_chunk", MARLIN_K_CHUNKS)@pytest.mark.parametrize("n_chunk", MARLIN_N_CHUNKS)@pytest.mark.parametrize("num_bits", [8])@pytest.mark.parametrize("group_size", [-1])@pytest.mark.parametrize("mnk_factors", MNK_FACTORS)@pytest.mark.parametrize("dtype", DTYPES)def test_fp8_marlin_gemm( k_chunk, n_chunk, num_bits, group_size, mnk_factors, dtype,): m_factor, n_factor, k_factor = mnk_factors
size_m = m_factor size_k = k_chunk * k_factor size_n = n_chunk * n_factor
a_input = rand_data((size_m, size_k), dtype=dtype) b_weight = rand_data((size_k, size_n), dtype=dtype)
# WEIGHTS fp8_weight, weight_scale = quantization.scaled_fp8_quant(b_weight, scale=None) # Repack weights to gptq format (packed int32 elements) packed_gptq_qweight = pack_fp8_to_int32(fp8_weight) # Repack weights to marlin format marlin_qweight = quantization.gptq_marlin_repack( b_q_weight=packed_gptq_qweight, perm=torch.empty(0, dtype=torch.int, device="cuda"), size_k=size_k, size_n=size_n, num_bits=8, )
# WEIGHT SCALES # Currently Marlin doesn't support per-tensor scales, so we # expand it to channelwise scales = weight_scale.repeat(1, size_n).to(a_input.dtype).to("cuda") # Permute scales marlin_scales = marlin_permute_scales( s=scales, size_k=size_k, size_n=size_n, group_size=-1 )
workspace = MarlinWorkspace( size_n, GPTQ_MARLIN_MIN_THREAD_N, GPTQ_MARLIN_MAX_PARALLEL )
opcheck( quantization._ops.ops.fp8_marlin_gemm, ( a_input, marlin_qweight, marlin_scales, workspace.scratch, num_bits, a_input.shape[0], b_weight.shape[1], a_input.shape[1], ), )
output = quantization.fp8_marlin_gemm( a=a_input, b_q_weight=marlin_qweight, b_scales=marlin_scales, workspace=workspace.scratch, num_bits=num_bits, size_m=a_input.shape[0], size_n=b_weight.shape[1], size_k=a_input.shape[1], ) output_ref = torch.matmul(a_input, b_weight)
torch.cuda.synchronize()
max_diff = compute_max_diff(output, output_ref)
assert max_diff < 0.04
@pytest.mark.skipif( capability < 80, reason="Marlin is not supported on this GPU type.",)@pytest.mark.parametrize("k_chunk", MARLIN_K_CHUNKS)@pytest.mark.parametrize("n_chunk", MARLIN_N_CHUNKS)@pytest.mark.parametrize("quant_type", query_marlin_supported_quant_types(True))@pytest.mark.parametrize("group_size", MARLIN_SUPPORTED_GROUP_SIZES)@pytest.mark.parametrize("mnk_factors", MNK_FACTORS)@pytest.mark.parametrize("use_fp32_reduce", USE_FP32_REDUCE_OPTS)def test_awq_marlin_gemm( k_chunk, n_chunk, quant_type, group_size, mnk_factors, use_fp32_reduce,): m_factor, n_factor, k_factor = mnk_factors
size_m = m_factor size_k = k_chunk * k_factor size_n = n_chunk * n_factor
a_input = rand_data((size_m, size_k)) b_weight = rand_data((size_k, size_n))
w_ref, marlin_q_w, marlin_s, marlin_zp = awq_marlin_quantize( b_weight, quant_type, group_size )
g_idx = torch.empty(0, dtype=torch.int, device=marlin_q_w.device) sort_indices = torch.empty(0, dtype=torch.int, device=marlin_q_w.device) is_k_full = True has_zp = True
workspace = MarlinWorkspace( size_n, GPTQ_MARLIN_MIN_THREAD_N, GPTQ_MARLIN_MAX_PARALLEL )
output = quantization.gptq_marlin_gemm( a_input, marlin_q_w, marlin_s, marlin_zp, g_idx, sort_indices, workspace.scratch, quant_type, a_input.shape[0], b_weight.shape[1], a_input.shape[1], is_k_full=is_k_full, has_zp=has_zp, use_fp32_reduce=use_fp32_reduce, is_zp_float=False, ) output_ref = torch.matmul(a_input, w_ref)
torch.cuda.synchronize()
max_diff = compute_max_diff(output, output_ref)
assert max_diff < 0.04
@pytest.mark.skipif( capability < 80, reason="Marlin is not supported on this GPU type.",)@pytest.mark.parametrize("k_chunk", MARLIN_K_CHUNKS)@pytest.mark.parametrize("n_chunk", MARLIN_N_CHUNKS)@pytest.mark.parametrize("group_size", HQQ_SUPPORTED_GROUP_SIZES)@pytest.mark.parametrize("mnk_factors", MNK_FACTORS)@pytest.mark.parametrize("use_fp32_reduce", USE_FP32_REDUCE_OPTS)def test_hqq_marlin_gemm( k_chunk, n_chunk, group_size, mnk_factors, use_fp32_reduce,): m_factor, n_factor, k_factor = mnk_factors
size_m = m_factor size_k = k_chunk * k_factor size_n = n_chunk * n_factor
quant_type = scalar_types.uint4
a_input = rand_data((size_m, size_k)) dev = a_input.device
b_weight = torch.randint(0, 10, (size_n, size_k), dtype=torch.uint8, device=dev) scale = rand_data((size_n, size_k // group_size)) zero = rand_data((size_n, size_k // group_size))
gptq_w_q = gptq_pack(b_weight.transpose(1, 0), 4, size_k, size_n)
sort_indices = torch.empty(0, dtype=torch.int, device=dev) marlin_w_q = quantization.gptq_marlin_repack(gptq_w_q, sort_indices, size_k, size_n, 4).to( dev ) marlin_s = marlin_permute_scales( scale.transpose(1, 0), size_k, size_n, group_size ).to(dev) marlin_zp = marlin_permute_scales( zero.transpose(1, 0), size_k, size_n, group_size ).to(dev)
g_idx = marlin_make_empty_g_idx(dev) g_idx_sort_indices = marlin_make_empty_g_idx(dev)
workspace = MarlinWorkspace( size_n, GPTQ_MARLIN_MIN_THREAD_N, GPTQ_MARLIN_MAX_PARALLEL )
output = quantization.gptq_marlin_gemm( a_input, marlin_w_q, marlin_s, marlin_zp, g_idx, g_idx_sort_indices, workspace.scratch, quant_type, a_input.shape[0], b_weight.shape[0], a_input.shape[1], is_k_full=True, has_zp=True, use_fp32_reduce=use_fp32_reduce, is_zp_float=True, )
b_flat = b_weight.reshape(-1, group_size) zp_flat = zero.reshape(-1, 1) s_flat = scale.reshape(-1, 1) dequant = (b_flat - zp_flat) * s_flat
output_ref = torch.matmul(a_input, dequant.reshape(b_weight.shape).transpose(1, 0))
torch.cuda.synchronize()
max_diff = compute_max_diff(output, output_ref)
assert max_diff < 0.04
@pytest.mark.skipif( capability < 80, reason="Marlin is not supported on this GPU type.",)@pytest.mark.parametrize("k_chunk", MARLIN_K_CHUNKS)@pytest.mark.parametrize("n_chunk", MARLIN_N_CHUNKS)@pytest.mark.parametrize("num_bits", MARLIN_QQQ_SUPPORTED_NUM_BITS)@pytest.mark.parametrize("group_size", MARLIN_QQQ_SUPPORTED_GROUP_SIZES)@pytest.mark.parametrize("mnk_factors", MNK_FACTORS)def test_marlin_qqq_gemm( k_chunk, n_chunk, num_bits, group_size, mnk_factors,): int8_traits = torch.iinfo(torch.int8) m_factor, n_factor, k_factor = mnk_factors
size_m = m_factor size_k = k_chunk * k_factor size_n = n_chunk * n_factor
a_input = rand_data((size_m, size_k)) b_weight = rand_data((size_k, size_n))
# Quantize activations s_a = ( a_input.abs().max(dim=-1, keepdim=True)[0].div(int8_traits.max).to(torch.float) ) q_a = (a_input / s_a).round().clamp(int8_traits.min, int8_traits.max).to(torch.int8)
# Quantize weights w_ref, marlin_qqq_q_w, marlin_qqq_s_group, marlin_qqq_s_channel = ( marlin_qqq_quantize(b_weight, num_bits, group_size) )
workspace = MarlinWorkspace( size_n, MARLIN_QQQ_MIN_THREAD_N, MARLIN_QQQ_MAX_PARALLEL )
opcheck( quantization._ops.ops.marlin_qqq_gemm, ( q_a, marlin_qqq_q_w, s_a, marlin_qqq_s_channel, marlin_qqq_s_group, workspace.scratch, a_input.shape[0], b_weight.shape[1], a_input.shape[1], ), )
output = quantization.marlin_qqq_gemm( q_a, marlin_qqq_q_w, s_a, marlin_qqq_s_channel, marlin_qqq_s_group, workspace.scratch, a_input.shape[0], b_weight.shape[1], a_input.shape[1], ) output_ref = torch.matmul(q_a.half() * s_a.half(), w_ref)
torch.cuda.synchronize()
max_diff = compute_max_diff(output, output_ref)
assert max_diff < 0.04
def test_marlin_gemm_opcheck(): size_m = 2048 size_n = 4096 size_k = 4096 a = torch.rand((size_m, size_n), device="cuda", dtype=torch.float16) w = torch.randint(-5, 5, (256, 8192), device="cuda", dtype=torch.int32) s = torch.full((32, size_k), 0.125, device="cuda", dtype=torch.float16) wk = MarlinWorkspace( size_n, GPTQ_MARLIN_MIN_THREAD_N, GPTQ_MARLIN_MAX_PARALLEL ).scratch x = quantization._ops.ops.marlin_gemm(a, w, s, wk, size_m, size_n, size_k) y = quantization._ops.ops.marlin_gemm(a, w, s, wk, size_m, size_n, size_k) torch.testing.assert_close(x, y) opcheck(quantization._ops.ops.marlin_gemm, (a, w, s, wk, size_m, size_n, size_k))3 unmodified lines4567878910111213141516171819202122231 unmodified line252627282930315 unmodified lines37383932333435364041424340414243444445464748495051524849505152535455565758596061625354555657585960616263646566676869707172733 unmodified linesimport randomimport unittestfrom numbers import Numberfrom typing import (Any, Dict, List, NamedTuple, Optional, Sequence, Tuple, Union)from typing import Any, Dict, List, NamedTuple, Optional, Sequence, Tuple, Union
import pytestimport torchfrom torch._prims_common import TensorLikeType
# For now, disable "test_aot_dispatch_dynamic" since there are some# bugs related to this test in PyTorch 2.4.DEFAULT_OPCHECK_TEST_UTILS: Tuple[str, ...] = ( "test_schema", "test_autograd_registration", "test_faketensor",)
ALL_OPCHECK_TEST_UTILS: Tuple[str, ...] = ( "test_schema", "test_autograd_registration",1 unmodified line "test_aot_dispatch_dynamic",)
# Copied/modified from torch._refs.__init__.pydef fp8_allclose( a: TensorLikeType,5 unmodified lines """ Reference implementation of torch.allclose """ torch._refs._check_close_args(name="torch.allclose", a=a, b=b, rtol=rtol, atol=atol) torch._refs._check_close_args(name="torch.allclose", a=a, b=b, rtol=rtol, atol=atol)
return bool( torch.all( torch.isclose(a.double(), b.double(), rtol=rtol, atol=atol, equal_nan=equal_nan)).item()) torch.isclose( a.double(), b.double(), rtol=rtol, atol=atol, equal_nan=equal_nan ) ).item() )
# A special version of op check that has a restricted default set of test_utils# and a patched version of allclose that supports fp8 types.def opcheck(op: Union[torch._ops.OpOverload, torch._ops.OpOverloadPacket, torch._library.custom_ops.CustomOpDef], args: Tuple[Any, ...], kwargs: Optional[Dict[str, Any]] = None, *, test_utils: Union[str, Sequence[str]] = ALL_OPCHECK_TEST_UTILS, raise_exception: bool = True, cond: bool = True) -> Dict[str, str]: with unittest.mock.patch('torch.allclose', new=fp8_allclose): return torch.library.opcheck( op, args, kwargs, test_utils=test_utils, raise_exception=raise_exception) if cond else {}def opcheck( op: Union[ torch._ops.OpOverload, torch._ops.OpOverloadPacket, torch._library.custom_ops.CustomOpDef, ], args: Tuple[Any, ...], kwargs: Optional[Dict[str, Any]] = None, *, test_utils: Union[str, Sequence[str]] = ALL_OPCHECK_TEST_UTILS, raise_exception: bool = True, cond: bool = True) -> Dict[str, str]: with unittest.mock.patch("torch.allclose", new=fp8_allclose): return ( torch.library.opcheck( op, args, kwargs, test_utils=test_utils, raise_exception=raise_exception ) if cond else {} )build.toml+19-4
ext-torch/__init__.py+30-177
ext-torch/compressed_tensors.pyAdded+110
ext-torch/cutlass.pyAdded+75
ext-torch/marlin.pyAdded+208
ext-torch/scalar_type.pyAdded+330
ext-torch/torch_binding.cpp+29-3
ext-torch/torch_binding.h+23
ext-torch/utils/marlin_utils.pyAdded+391
ext-torch/utils/marlin_utils_fp8.pyAdded+100
ext-torch/utils/marlin_utils_test.pyAdded+162
ext-torch/utils/marlin_utils_test_24.pyAdded+473
ext-torch/utils/marlin_utils_test_qqq.pyAdded+125
ext-torch/utils/quant_utils.pyAdded+470
marlin/dense/LICENSEAdded+209
marlin/dense/common/base.hAdded+32
marlin/dense/common/mem.hAdded+89
marlin/dense/marlin_cuda_kernel.cuAdded+1068
marlin/qqq/marlin_qqq_gemm_kernel.cuAdded+1243
marlin/sparse/LICENSEAdded+203
marlin/sparse/common/base.hAdded+51
marlin/sparse/common/mem.hAdded+136
marlin/sparse/common/mma.hAdded+191
marlin/sparse/marlin_24_cuda_kernel.cuAdded+1140
tests/kernels/test_marlin_gemm.pyAdded+733
tests/kernels/utils.py+38-27