Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- lib/python3.12/site-packages/deepspeed/inference/v2/__pycache__/engine_factory.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/__init__.pyAdded+6
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/__pycache__/ds_kernel.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/__init__.pyAdded+11
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/bias_activations/__init__.pyAdded+6
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/bias_activations/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/bias_activations/__pycache__/bias_activation.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/bias_activations/bias_activation.cppAdded+68
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/bias_activations/bias_activation.hAdded+22
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/bias_activations/bias_activation.pyAdded+62
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/bias_activations/bias_activation_cuda.cuAdded+140
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/blas_kernels/__init__.pyAdded+6
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/blas_kernels/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/blas_kernels/__pycache__/blas_linear.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/blas_kernels/blas.hAdded+138
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/blas_kernels/blas_linear.pyAdded+55
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/blas_kernels/blas_utils.hAdded+299
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/core_ops.cppAdded+42
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/__init__.pyAdded+8
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/__pycache__/cuda_fp_ln_base.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/__pycache__/cuda_ln.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/__pycache__/cuda_post_ln.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/__pycache__/cuda_pre_ln.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/cuda_fp_ln_base.pyAdded+37
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/cuda_ln.pyAdded+30
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/cuda_post_ln.pyAdded+34
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/cuda_pre_ln.pyAdded+39
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/layer_norm.cppAdded+102
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/layer_norm.hAdded+67
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/layer_norm_cuda.cuAdded+489
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/__init__.pyAdded+6
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/__pycache__/cuda_linear.cpython-312.pycAdded
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/cuda_linear.pyAdded+207
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/include/configs.hAdded+96
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/include/kernel_matmul.cuhAdded+272
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/include/kernel_reduction.cuhAdded+43
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/include/ptx_cp.async.cuhAdded+75
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/include/ptx_mma.cuhAdded+139
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/include/utils_core.cuhAdded+246
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/include/utils_gmem.cuhAdded+86
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/include/utils_paralleldequant.cuhAdded+127
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/include/weight_prepacking.hAdded+209
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/linear_kernels.cppAdded+224
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/linear_kernels.hAdded+28
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/linear_kernels_cuda.cuAdded+318
- lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/linear_kernels_cuda.hAdded+51
123456# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
from .ds_kernel import DSKernelBase1234567891011# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
from .bias_activations import *from .blas_kernels import *from .cuda_layer_norm import *from .cuda_rms_norm import *from .gated_activations import *from .cuda_linear import *123456# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
from .bias_activation import *1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
#include "bias_activation.h"#include <c10/cuda/CUDAStream.h>#include "ds_kernel_utils.h"
#ifdef BF16_AVAILABLE#define DTYPE_SWITCH(DTYPE, ...) \ [&] { \ if (DTYPE == torch::kFloat16) { \ using scalar_t = __half; \ return __VA_ARGS__(); \ } else if (DTYPE == torch::kBFloat16) { \ using scalar_t = __nv_bfloat16; \ return __VA_ARGS__(); \ } else { \ TORCH_CHECK(false, "Unsupported dtype for BiasActivation"); \ } \ }()#else#define DTYPE_SWITCH(DTYPE, ...) \ [&] { \ if (DTYPE == torch::kFloat16) { \ using scalar_t = __half; \ return __VA_ARGS__(); \ } else { \ TORCH_CHECK(false, "Unsupported dtype for BiasActivation"); \ } \ }()#endif
/*In-place bias and activation fusion kernel.*/void bias_activation(torch::Tensor& activation, c10::optional<torch::Tensor>& bias, const int32_t act_type){ const ActivationType atype = static_cast<ActivationType>(act_type); const int32_t rows = activation.size(0); const int32_t cols = activation.size(1);
TORCH_CHECK(atype == ActivationType::GELU || atype == ActivationType::RELU || atype == ActivationType::SILU || atype == ActivationType::IDENTITY, "Unsupported activation type for BiasActivation"); TORCH_CHECK(activation.dim() == 2, "BiasActivation only supports 2D activation tensors");
DTYPE_SWITCH(activation.scalar_type(), [&] { scalar_t* activation_ptr = reinterpret_cast<scalar_t*>(activation.data_ptr());
const scalar_t* bias_ptr; if (bias.has_value()) { TORCH_CHECK(activation.scalar_type() == bias.value().scalar_type(), "BiasActivation activation and bias must have same dtype"); bias_ptr = reinterpret_cast<const scalar_t*>(bias.value().data_ptr()); } else { bias_ptr = nullptr; }
if (atype == ActivationType::IDENTITY && bias_ptr == nullptr) { return; }
launch_bias_activation<scalar_t>( activation_ptr, bias_ptr, rows, cols, atype, c10::cuda::getCurrentCUDAStream()); });}12345678910111213141516171819202122// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
#pragma once
#include <c10/cuda/CUDAStream.h>#include <torch/extension.h>#include "activation_type.h"
template <typename T>void launch_bias_activation(T* activation, const T* bias, const int32_t n_rows, const int32_t n_cols, const ActivationType activation_type, cudaStream_t stream);
void bias_activation(torch::Tensor& activation, c10::optional<torch::Tensor>& bias, const int32_t activation_type);1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
from typing import Optional
import torch
from ....inference_utils import ActivationType, DtypeEnumfrom deepspeed.ops.op_builder import InferenceCoreBuilderfrom ... import DSKernelBase
class CUDABiasActivation(DSKernelBase): """ CUDA implementation of bias activation kernel. This kernel should be deprecated once we are fusing the bias activation into the linear kernel in all scenarios. """
supported_dtypes = [DtypeEnum.fp16, DtypeEnum.bf16] supported_act_fns = [ActivationType.IDENTITY, ActivationType.GELU, ActivationType.RELU, ActivationType.SILU]
def __init__(self, channels: int, dtype: DtypeEnum, act_fn: ActivationType) -> None: """ Compile and validate for the fused bias-activation kernel.
Parameters: channels (int): Number of channels to expect in the activation. dtype (torch.dtype): Data type for the input/output. Supported values are DtypeEnum.fp16 and DtypeEnum.bf16. act_fn (ActivationType): Activation function to use. Only IDENTITY, GELU, RELU, and SILU are supported. """
if channels % 8 != 0: raise ValueError("channels must be divisible by 8")
if DtypeEnum(dtype) not in CUDABiasActivation.supported_dtypes: raise ValueError("Unsupported data type: {}, supported_dtypes are {}".format( dtype, CUDABiasActivation.supported_dtypes))
act_fn = ActivationType(act_fn) if act_fn not in CUDABiasActivation.supported_act_fns: raise ValueError("Unsupported activation function: {}, supported_act_fns are {}".format( act_fn, CUDABiasActivation.supported_act_fns))
inf_module = InferenceCoreBuilder().load() self.kernel = inf_module.bias_activation self.act_fn = act_fn
def __call__(self, activation: torch.Tensor, bias: Optional[torch.Tensor] = None) -> torch.Tensor: """ Add an optional bias and perform the non-linear activation function.
Parameters: activation (torch.Tensor): Input tensor of shape [tokens, channels] bias (torch.Tensor): Optional bias tensor of shape [channels]
Returns: activation that has been updated in-place """ self.kernel(activation, bias, self.act_fn.value)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
#include <cassert>#include "activation_type.h"#include "conversion_utils.h"#include "ds_kernel_utils.h"#include "memory_access_utils.h"
// Default activation function will error outtemplate <ActivationType ActType>DS_D_INLINE float act_fn(float val);
template <>DS_D_INLINE float act_fn<ActivationType::IDENTITY>(float val){ return val;}
template <>DS_D_INLINE float act_fn<ActivationType::RELU>(float val){ return val > 0.0f ? val : 0.0f;}
template <>DS_D_INLINE float act_fn<ActivationType::GELU>(float val){ constexpr float sqrt_param = 0.79788456080286535587989211986876f; constexpr float mul_param = 0.044715f; return val * 0.5f * (1.0f + tanhf(sqrt_param * (val + mul_param * val * val * val)));}
template <>DS_D_INLINE float act_fn<ActivationType::SILU>(float val){ return val / (1.0f + expf(-val));}
namespace bias_act {
constexpr int access_size = 16;constexpr int threads = 512;constexpr int unroll = 4;
} // namespace bias_act
template <typename T, ActivationType ActType>__global__ void bias_activation_kernel(T* activation, const T* bias, const int32_t rows, const int32_t cols){ constexpr int vector_T = bias_act::access_size / sizeof(T);
const int32_t thread_offset = threadIdx.x * vector_T; const int32_t block_offset = blockIdx.x * vector_T * bias_act::unroll * bias_act::threads; const int32_t base_offset = block_offset + thread_offset;
const int32_t thread_stride = bias_act::threads * vector_T;
#pragma unroll for (int i = 0; i < bias_act::unroll; i++) { const int32_t iter_offset = base_offset + i * thread_stride;
const int32_t row = iter_offset / cols;
T buffer[vector_T]; T bias_buffer[vector_T];
if (row < rows) { const int32_t col = iter_offset % cols;
mem_access::load_global<bias_act::access_size>(buffer, activation + iter_offset); mem_access::load_global<bias_act::access_size>( bias_buffer, bias + col, bias != nullptr);
#pragma unroll for (int j = 0; j < vector_T; j++) { float val = conversion::to<float>(buffer[j]) + conversion::to<float>(bias_buffer[j]); buffer[j] = conversion::to<T>(act_fn<ActType>(val)); }
mem_access::store_global<bias_act::access_size>(activation + iter_offset, buffer); } }}
#define ACT_TYPE_SWITCH(ACT_TYPE, ...) \ if (ACT_TYPE == ActivationType::IDENTITY) { \ constexpr ActivationType act_fn_t = ActivationType::IDENTITY; \ return __VA_ARGS__(); \ } else if (ACT_TYPE == ActivationType::RELU) { \ constexpr ActivationType act_fn_t = ActivationType::RELU; \ return __VA_ARGS__(); \ } else if (ACT_TYPE == ActivationType::GELU) { \ constexpr ActivationType act_fn_t = ActivationType::GELU; \ return __VA_ARGS__(); \ } else if (ACT_TYPE == ActivationType::SILU) { \ constexpr ActivationType act_fn_t = ActivationType::SILU; \ return __VA_ARGS__(); \ } else { \ assert(false); \ }
template <typename T>void launch_bias_activation(T* activation, const T* bias, const int32_t n_rows, const int32_t n_cols, const ActivationType activation_type, cudaStream_t stream){ constexpr int32_t elems_per_block = bias_act::threads * bias_act::unroll * bias_act::access_size / sizeof(T); const int32_t total_elems = n_rows * n_cols;
const int32_t blocks = (total_elems + elems_per_block - 1) / elems_per_block;
const dim3 grid(blocks); const dim3 block(bias_act::threads);
ACT_TYPE_SWITCH(activation_type, [&] { bias_activation_kernel<T, act_fn_t> <<<grid, block, 0, stream>>>(activation, bias, n_rows, n_cols); });}
#define INSTANTIATE_FOR_T(T) \ template void launch_bias_activation<T>( \ T*, const T*, const int32_t, const int32_t, const ActivationType, cudaStream_t);
INSTANTIATE_FOR_T(__half);
#ifdef BF16_AVAILABLEINSTANTIATE_FOR_T(__nv_bfloat16);#endif123456# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
from .blas_linear import *123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
#pragma once
#include <c10/cuda/CUDAStream.h>#include <torch/extension.h>#include <cstdio>#include "blas_utils.h"
#define DISPATCH_BLAS_MATMUL(T_TYPE, C_TYPE) \ if (output.options().dtype() == torch::T_TYPE) { \ blas_gemm_ex(output.data_ptr(), \ (const void*)weights.data_ptr(), \ (const void*)hidden_states.data_ptr(), \ m, \ n, \ k, \ lda, \ ldb, \ ldc, \ trans_a, \ trans_b, \ &alpha, \ &beta, \ C_TYPE); \ }
void blas_linear(at::Tensor& output, at::Tensor& hidden_states, at::Tensor& weights){ /* Expected shape: output([total_tokens_across_dims], out_neurons) hidden_states([total_tokens_across_dims], in_neurons) weights(out_neurons, in_neurons)
We are going to assume contiguous for the above shapes.
The shapes are going to get messed with a little internally to handle column-major GEMMs. */
// Number of tokens is N (since the GEMM output is column-major but our Tensor // is row-major, we need to transpose the shapes) const int n = output.numel() / output.size(-1); const int k = weights.size(1); const int m = weights.size(0);
// A strides const bool trans_a = weights.stride(1) == 1; const int lda = (trans_a) ? weights.stride(0) : weights.stride(1);
// B strides const bool trans_b = hidden_states.stride(-1) != 1; const int ldb = (trans_b) ? hidden_states.stride(-1) : hidden_states.stride(-2);
// C strides const int ldc = output.stride(-2);
const float alpha = 1.0f; const float beta = 0.0f;
TORCH_CHECK(output.scalar_type() == hidden_states.scalar_type(), "Output and hidden states must have the same scalar type"); TORCH_CHECK(output.scalar_type() == weights.scalar_type(), "Output and weights must have the same scalar type");
// Dispatch the datatypes DISPATCH_BLAS_MATMUL(kFloat, BlasType::FP32); DISPATCH_BLAS_MATMUL(kHalf, BlasType::FP16);#ifdef BF16_AVAILABLE DISPATCH_BLAS_MATMUL(kBFloat16, BlasType::BF16);#endif}
#define DISPATCH_4D_BLAS(T_TYPE, C_TYPE) \ if (C.options().dtype() == torch::T_TYPE) { \ blas_strided_batched_gemm(C.data_ptr(), \ (const void*)A.data_ptr(), \ (const void*)B.data_ptr(), \ m, \ n, \ k, \ lda, \ ldb, \ ldc, \ trans_a, \ trans_b, \ &alpha, \ &beta, \ stride_a, \ stride_b, \ stride_c, \ batch, \ C_TYPE); \ }
void blas_4d_matmul(at::Tensor& C, at::Tensor& B, at::Tensor& A){ /* C shape: (batch_size, N, M) A shape: (batch_size, N, K) B shape: (batch_size, K, M) */
const int n = C.size(-2); const int k = C.size(-1); const int m = B.size(-1);
// A strides const bool trans_a = A.stride(-1) == 1; const int lda = (trans_a) ? A.stride(-2) : A.stride(-1); const int stride_a = A.stride(-3);
// B strides const bool trans_b = B.stride(-1) != 1; const int ldb = (trans_b) ? B.stride(-1) : B.stride(-2); const int stride_b = B.stride(-3);
// C strides const int ldc = C.stride(-2); const int stride_c = C.stride(-3);
const float alpha = 1.0f; const float beta = 0.0f;
const int batch = C.numel() / (n * m);
// Dispatch the datatypes DISPATCH_4D_BLAS(kFloat, BlasType::FP32); DISPATCH_4D_BLAS(kHalf, BlasType::FP16);#ifdef BF16_AVAILABLE DISPATCH_4D_BLAS(kBFloat16, BlasType::BF16);#endif}
void create_handle() { BlasContext::getInstance().get_handle(); }12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
import torch
from ....inference_utils import DtypeEnumfrom deepspeed.ops.op_builder import InferenceCoreBuilderfrom ... import DSKernelBase
class BlasLibLinear(DSKernelBase): """ Wrapper around the BLAS matmul kernel for FP16/BF16/FP32 for CUDA/RoCM.
Performs z = x @ y """
supported_dtypes = [DtypeEnum.fp16, DtypeEnum.bf16, DtypeEnum.fp32]
def __init__(self, fp_dtype: DtypeEnum): """ Parameters: fp_dtype (torch.dtype): Data type for the input/output. Supported values are torch.float16, torch.bfloat16, and torch.float32. """ fp_dtype = DtypeEnum(fp_dtype) if fp_dtype not in BlasLibLinear.supported_dtypes: raise ValueError("Unsupported data type: {}, supported_dtypes are {}".format( fp_dtype, BlasLibLinear.supported_dtypes))
self.inf_module = InferenceCoreBuilder().load() self.inf_module.create_handle() self.kernel = self.inf_module.blas_linear
def __call__(self, output: torch.Tensor, hidden_states: torch.Tensor, weights: torch.Tensor) -> torch.Tensor: """ Matmul kernel as implemented by platform BLAS library. The input must be 2D or larger. If n-dimensional, the leading dimensions are folded into each other: 2D: m = x.size(0) 3D: m = x.size(0) * x.size(1) 4D: m = x.size(0) * x.size(1) * x.size(2) (etc...) All inputs should be contiguous.
Parameters: output (torch.Tensor): Output tensor. Shape is of [*, out_features] hidden_states (torch.Tensor): Input tensor. Shape is of [*, in_features] weights (torch.Tensor): Input tensor. Shape is of [out_features, in_features]
Returns: z (torch.Tensor): Output tensor. Shape is of [m, n] """ self.kernel(output, hidden_states, weights) return output123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
#pragma once
#include <assert.h>#include <cublas_v2.h>#include <cuda.h>#ifdef BF16_AVAILABLE#include <cuda_bf16.h>#endif#include <cuda_fp16.h>#include <cuda_runtime.h>#ifndef __HIP_PLATFORM_AMD__#include <mma.h>#endif#include <stdio.h>#include <iostream>#include <stdexcept>
class BlasContext { /* Slim wrapper for managing the lifetime of the platform's BLAS handle. This should be hipified for ROCm. */public: BlasContext() { if (cublasCreate(&_handle) != CUBLAS_STATUS_SUCCESS) { auto message = std::string("Fail to create cublas handle."); std::cerr << message << std::endl; throw std::runtime_error(message); }#ifndef __HIP_PLATFORM_AMD__ cublasSetMathMode(_handle, CUBLAS_TENSOR_OP_MATH);#endif }
virtual ~BlasContext() { cublasDestroy(_handle); }
static BlasContext& getInstance() { // Should always access the singleton through this function. static BlasContext _instance; return _instance; }
cublasHandle_t get_handle() const { return _handle; }
private: cublasHandle_t _handle;};
enum class BlasType { FP32, FP16, BF16 };
// TODO HIP: Remove backward compatibility for torch<=2.0 in future#if defined(__HIP_PLATFORM_AMD__) && \ ((TORCH_VERSION_MAJOR < 2) || (TORCH_VERSION_MAJOR == 2 && TORCH_VERSION_MINOR == 0))rocblas_operation get_trans_op(bool do_trans){ return (do_trans) ? rocblas_operation_transpose : rocblas_operation_none;}
rocblas_datatype get_datatype(BlasType type){ switch (type) { case BlasType::FP32: return rocblas_datatype_f32_r; case BlasType::FP16: return rocblas_datatype_f16_r; case BlasType::BF16: return rocblas_datatype_bf16_r; default: throw std::runtime_error("Unsupported BlasType"); }}#elsecublasOperation_t get_trans_op(bool do_trans) { return (do_trans) ? CUBLAS_OP_T : CUBLAS_OP_N; }
cublasDataType_t get_datatype(BlasType type){ switch (type) {#ifdef __HIP_PLATFORM_AMD__ case BlasType::FP32: return HIPBLAS_R_32F; case BlasType::FP16: return HIPBLAS_R_16F; case BlasType::BF16: return HIPBLAS_R_16B;#else case BlasType::FP32: return CUDA_R_32F; case BlasType::FP16: return CUDA_R_16F; case BlasType::BF16: return CUDA_R_16BF;#endif default: throw std::runtime_error("Unsupported BlasType"); }}#endif
int blas_gemm_ex(void* C, const void* A, const void* B, int m, int n, int k, int lda, int ldb, int ldc, bool transa, bool transb, const float* alpha, const float* beta, BlasType type){#if defined(__HIP_PLATFORM_AMD__) && \ ((TORCH_VERSION_MAJOR < 2) || (TORCH_VERSION_MAJOR == 2 && TORCH_VERSION_MINOR == 0)) rocblas_operation_t transa_op = get_trans_op(transa); rocblas_operation_t transb_op = get_trans_op(transb);
rocblas_datatype_t abc_type = get_datatype(type);
rocblas_status status = rocblas_gemm_ex(BlasContext::getInstance().get_handle(), transa_op, transb_op, m, n, k, (const void*)alpha, A, abc_type, lda, B, abc_type, ldb, (const void*)beta, C, abc_type, ldc, C, abc_type, ldc, rocblas_datatype_f32_r, rocblas_gemm_algo_standard, 0, 0);#else cublasOperation_t transa_op = get_trans_op(transa); cublasOperation_t transb_op = get_trans_op(transb);
cublasDataType_t abc_type = get_datatype(type); cublasStatus_t status = cublasGemmEx(BlasContext::getInstance().get_handle(), transa_op, transb_op, m, n, k, (const void*)alpha, A, abc_type, lda, B, abc_type, ldb, (const void*)beta, C, abc_type, ldc,#if defined(__HIP_PLATFORM_AMD__) && defined(HIPBLAS_V2) HIPBLAS_COMPUTE_32F,#elif defined(__HIP_PLATFORM_AMD__) HIPBLAS_R_32F,#else CUDA_R_32F,#endif CUBLAS_GEMM_DEFAULT_TENSOR_OP);#endif
#if defined(__HIP_PLATFORM_AMD__) && \ ((TORCH_VERSION_MAJOR < 2) || (TORCH_VERSION_MAJOR == 2 && TORCH_VERSION_MINOR == 0)) if (status != rocblas_status_success) {#else if (status != CUBLAS_STATUS_SUCCESS) {#endif fprintf(stderr, "!!!! kernel execution error. (m: %d, n: %d, k: %d, error: %d) \n", m, n, k, (int)status); return EXIT_FAILURE; } return 0;}
int blas_strided_batched_gemm(void* C, const void* A, const void* B, int m, int n, int k, int lda, int ldb, int ldc, bool transa, bool transb, const float* alpha, const float* beta, int stride_A, int stride_B, int stride_C, int batch, BlasType type){#if defined(__HIP_PLATFORM_AMD__) && \ ((TORCH_VERSION_MAJOR < 2) || (TORCH_VERSION_MAJOR == 2 && TORCH_VERSION_MINOR == 0)) rocblas_operation_t transa_op = get_trans_op(transa); rocblas_operation_t transb_op = get_trans_op(transb);
rocblas_datatype_t abc_type = get_datatype(type);
rocblas_status status = rocblas_gemm_strided_batched_ex(BlasContext::getInstance()::get_handle(), transa_op, transb_op, m, n, k, (const void*)alpha, A, abc_type, lda, stride_A, B, abc_type, ldb, stride_B, (const void*)beta, C, abc_type, ldc, stride_C, C, abc_type, ldc, stride_C, batch, rocblas_datatype_f32_r, rocblas_gemm_algo_standard, 0, 0);#else cublasOperation_t transa_op = get_trans_op(transa); cublasOperation_t transb_op = get_trans_op(transb);
cublasDataType_t abc_type = get_datatype(type);
cublasStatus_t status = cublasGemmStridedBatchedEx(BlasContext::getInstance().get_handle(), transa_op, transb_op, m, n, k, (const void*)alpha, A, abc_type, lda, stride_A, B, abc_type, ldb, stride_B, (const void*)beta, C, abc_type, ldc, stride_C, batch,#if defined(__HIP_PLATFORM_AMD__) && defined(HIPBLAS_V2) HIPBLAS_COMPUTE_32F,#elif defined(__HIP_PLATFORM_AMD__) HIPBLAS_R_32F,#else CUDA_R_32F,#endif CUBLAS_GEMM_DEFAULT_TENSOR_OP);#endif
#if defined(__HIP_PLATFORM_AMD__) && \ ((TORCH_VERSION_MAJOR < 2) || (TORCH_VERSION_MAJOR == 2 && TORCH_VERSION_MINOR == 0)) if (status != rocblas_status_success) {#else if (status != CUBLAS_STATUS_SUCCESS) {#endif fprintf(stderr, "!!!! kernel execution error. (batch: %d, m: %d, n: %d, k: %d, error: %d) \n", batch, m, n, k, (int)status); return EXIT_FAILURE; } return 0;}123456789101112131415161718192021222324252627282930313233343536373839404142// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
#include <c10/cuda/CUDAStream.h>#include <torch/extension.h>
#include "bias_activation.h"#include "blas.h"#include "gated_activation_kernels.h"#include "layer_norm.h"#include "linear_kernels.h"#include "rms_norm.h"
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m){ // bias_activation.h m.def("bias_activation", &bias_activation, "DeepSpeed bias activation in CUDA");
// layer_norm.h m.def("layer_norm", &ds_layer_norm, "DeepSpeed layer norm in CUDA"); m.def("pre_layer_norm", &ds_pre_layer_norm, "DeepSpeed pre layer norm in CUDA"); m.def("post_layer_norm", &ds_post_layer_norm, "DeepSpeed pre layer norm in CUDA");
// blas.h m.def("blas_linear", &blas_linear, "Linear implemented by vendor BLAS"); m.def("blas_4d_matmul", &blas_4d_matmul, "4D matmul implemented by vendor BLAS"); m.def("create_handle", &create_handle, "Create a handle for vendor BLAS");
// gated_activation_kernels.h m.def("gated_activation", &ds_gated_activation, "DeepSpeed gated activation in CUDA");
// rms_norm.h m.def("rms_norm", &rms_norm, "DeepSpeed rms norm in CUDA"); m.def("rms_pre_norm", &rms_pre_norm, "DeepSpeed rms pre norm in CUDA");
// linear_kernels.h m.def("cuda_wf6af16_linear", &cuda_wf6af16_linear, "DeepSpeed Wf6Af16 linear in CUDA"); m.def( "preprocess_weight", &preprocess_weight, "preprocess the FP16 weight to be 2bit and 4 bit");}12345678# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
from .cuda_ln import *from .cuda_post_ln import *from .cuda_pre_ln import *12345678910111213141516171819202122232425262728293031323334353637# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
import torch
from ... import DSKernelBasefrom ....inference_utils import elem_sizefrom deepspeed.ops.op_builder import InferenceCoreBuilder
class CUDAFPLNBase(DSKernelBase): """ Base class for CUDA LN kernels. They all same the same validation logic, so we can share it here. """
supported_dtypes = [torch.float16, torch.bfloat16, torch.float32]
def __init__(self, channels: int, fp_dtype: torch.dtype, epsilon: float = 1e-5): """ Parameters: channels (int): Number of channels in the input tensor. Must be divisible to align to 16 bytes. fp_dtype (torch.dtype): Data type for the input/output/gamma. Supported values are torch.float16, torch.bfloat16, and torch.float32. """ if fp_dtype not in CUDAFPLNBase.supported_dtypes: raise ValueError("Unsupported data type: {}, supported_dtypes are {}".format( fp_dtype, CUDAFPLNBase.supported_dtypes))
if elem_size(fp_dtype) * channels % 16 != 0: raise ValueError("channels must be divisible by 16 bytes")
self.inf_module = InferenceCoreBuilder().load() self.epsilon = epsilon123456789101112131415161718192021222324252627282930# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
import torch
from .cuda_fp_ln_base import CUDAFPLNBase
class CUDAFPLN(CUDAFPLNBase): """ Floating point layer norm kernel for CUDA/RoCM.
Performs: z = ln(x) """
def __call__(self, output_z: torch.Tensor, input_x: torch.Tensor, gamma: torch.Tensor, beta: torch.Tensor) -> torch.Tensor: """ output_z may alias input_x directly. All Tensors should have the same shape.
Parameters: output_z (torch.Tensor): Output tensor. input_x (torch.Tensor): Input tensor. gamma (torch.Tensor): Gamma tensor. beta (torch.Tensor): Beta tensor. """ self.inf_module.layer_norm(output_z, input_x, gamma, beta, self.epsilon) return output_z12345678910111213141516171819202122232425262728293031323334# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
import torch
from .cuda_fp_ln_base import CUDAFPLNBase
class CUDAFPPostLN(CUDAFPLNBase): """ Floating point post-LayerNorm kernel for CUDA/RoCM.
Performs: z = ln(x + y) """
def __call__(self, output_z: torch.Tensor, input_x: torch.Tensor, input_y: torch.Tensor, gamma: torch.Tensor, beta: torch.Tensor) -> torch.Tensor: """ Either input_x or input_y can alias output_z.
Parameters: output_z (torch.Tensor): Output tensor. input_x (torch.Tensor): Input tensor. input_y (torch.Tensor): Input tensor. gamma (torch.Tensor): Gamma tensor. beta (torch.Tensor): Beta tensor.
Returns: output (torch.Tensor): Output tensor. """ self.inf_module.post_layer_norm(output_z, input_x, input_y, gamma, beta, self.epsilon) return output_z123456789101112131415161718192021222324252627282930313233343536373839# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
from typing import Tuple
import torch
from .cuda_fp_ln_base import CUDAFPLNBase
class CUDAFPPreLN(CUDAFPLNBase): """ Floating point pre-LayerNorm kernel for CUDA/RoCM.
Performs: z_res = x_res + y_hid z_hid = ln(z_hid) """
def __call__(self, z_res: torch.Tensor, z_hid: torch.Tensor, x_res: torch.Tensor, y_hid: torch.Tensor, gamma: torch.Tensor, beta: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: """ z_res can alias x_res. All non-parameter input/output tensors must have the same shape. z_hid can alias y_hid.
Parameters: z_res (torch.Tensor): Output residual. z_hid (torch.Tensor): Output hidden states. x_res (torch.Tensor): Input residual. y_hid (torch.Tensor): Input hidden states. gamma (torch.Tensor): Gamma tensor. beta (torch.Tensor): Beta tensor.
Returns: output (torch.Tensor): Output tensor. """ self.inf_module.pre_layer_norm(z_res, z_hid, x_res, y_hid, gamma, beta, self.epsilon) return z_res, z_hid123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
#include "layer_norm.h"
#define DISPATCH_LAYER_NORM(T_TYPE, C_TYPE) \ if (input.options().dtype() == torch::T_TYPE) { \ launch_fused_ln((C_TYPE*)output.data_ptr(), \ (const C_TYPE*)input.data_ptr(), \ (const C_TYPE*)gamma.data_ptr(), \ (const C_TYPE*)beta.data_ptr(), \ epsilon, \ rows, \ elems_per_row, \ at::cuda::getCurrentCUDAStream()); \ }
void ds_layer_norm(at::Tensor& output, at::Tensor& input, at::Tensor& gamma, at::Tensor& beta, float epsilon){ bool ragged_input = input.dim() == 2;
const int rows = ragged_input ? input.size(0) : input.size(0) * input.size(1); const int elems_per_row = ragged_input ? input.size(1) : input.size(2);
DISPATCH_LAYER_NORM(kFloat, float); DISPATCH_LAYER_NORM(kHalf, __half);#ifdef BF16_AVAILABLE DISPATCH_LAYER_NORM(kBFloat16, __nv_bfloat16);#endif}
#define DISPATCH_LAYER_NORM_RESIDUAL(T_TYPE, C_TYPE) \ if (input.options().dtype() == torch::T_TYPE) { \ launch_fused_post_ln((C_TYPE*)output.data_ptr(), \ (const C_TYPE*)input.data_ptr(), \ (const C_TYPE*)residual.data_ptr(), \ (const C_TYPE*)gamma.data_ptr(), \ (const C_TYPE*)beta.data_ptr(), \ epsilon, \ rows, \ elems_per_row, \ at::cuda::getCurrentCUDAStream()); \ }
void ds_post_layer_norm(at::Tensor& output, at::Tensor& input, at::Tensor& residual, at::Tensor& gamma, at::Tensor& beta, float epsilon){ bool ragged_input = input.dim() == 2;
const int rows = ragged_input ? input.size(0) : input.size(0) * input.size(1); const int elems_per_row = ragged_input ? input.size(1) : input.size(2);
DISPATCH_LAYER_NORM_RESIDUAL(kFloat, float); DISPATCH_LAYER_NORM_RESIDUAL(kHalf, __half);#ifdef BF16_AVAILABLE DISPATCH_LAYER_NORM_RESIDUAL(kBFloat16, __nv_bfloat16);#endif}
#define DISPATCH_PRE_LAYER_NORM_RESIDUAL(T_TYPE, C_TYPE) \ if (input.options().dtype() == torch::T_TYPE) { \ launch_fused_pre_ln((C_TYPE*)norm_output.data_ptr(), \ (C_TYPE*)res_output.data_ptr(), \ (const C_TYPE*)input.data_ptr(), \ (const C_TYPE*)residual.data_ptr(), \ (const C_TYPE*)gamma.data_ptr(), \ (const C_TYPE*)beta.data_ptr(), \ epsilon, \ rows, \ elems_per_row, \ at::cuda::getCurrentCUDAStream()); \ }
void ds_pre_layer_norm(at::Tensor& res_output, at::Tensor& norm_output, at::Tensor& input, at::Tensor& residual, at::Tensor& gamma, at::Tensor& beta, float epsilon){ bool ragged_input = input.dim() == 2;
const int rows = ragged_input ? input.size(0) : input.size(0) * input.size(1); const int elems_per_row = ragged_input ? input.size(1) : input.size(2);
DISPATCH_PRE_LAYER_NORM_RESIDUAL(kFloat, float); DISPATCH_PRE_LAYER_NORM_RESIDUAL(kHalf, __half);#ifdef BF16_AVAILABLE DISPATCH_PRE_LAYER_NORM_RESIDUAL(kBFloat16, __nv_bfloat16);#endif}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
#pragma once
#include <c10/cuda/CUDAStream.h>#include <torch/extension.h>#include "ds_kernel_utils.h"
/*Kernel launch methods for layer norm variants.*/
template <typename T>void launch_fused_ln(T* output, const T* vals, const T* gamma, const T* beta, float epsilon, int rows, int elems_per_row, cudaStream_t stream);
template <typename T>void launch_fused_post_ln(T* output, const T* vals, const T* residual, const T* gamma, const T* beta, float epsilon, int rows, int elems_per_row, cudaStream_t stream);template <typename T>void launch_fused_pre_ln(T* norm_output, T* res_output, const T* vals, const T* residual, const T* gamma, const T* beta, float epsilon, int rows, int elems_per_row, cudaStream_t stream);
void ds_layer_norm(at::Tensor& output, at::Tensor& input, at::Tensor& gamma, at::Tensor& beta, float epsilon);
void ds_post_layer_norm(at::Tensor& output, at::Tensor& input, at::Tensor& residual, at::Tensor& gamma, at::Tensor& beta, float epsilon);
void ds_pre_layer_norm(at::Tensor& res_output, at::Tensor& norm_output, at::Tensor& input, at::Tensor& residual, at::Tensor& gamma, at::Tensor& beta, float epsilon);123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
#include "conversion_utils.h"#include "ds_kernel_utils.h"#include "memory_access_utils.h"#include "reduction_utils.h"
namespace cg = cooperative_groups;using rop = reduce::ROpType;
namespace ln {constexpr int granularity = 16;} // namespace ln
/*Regular layer norm implementation. Assumes elems_per_row % 8is equal to 0.
Args: output: buffer for output data vals: buffer for input data gamma: gain for normalization beta: bias for normalization epsilon: numeric stability elems_per_row: number of elements each block will normalize*/template <typename T, int unRoll, int threadsPerGroup, int maxThreads>__global__ void fused_ln(T* output, const T* vals, const T* gamma, const T* beta, float epsilon, int elems_per_row){ constexpr int T_per_load = ln::granularity / sizeof(T);
cg::thread_block tb = cg::this_thread_block(); cg::thread_block_tile<hw_warp_size> warp = cg::tiled_partition<hw_warp_size>(tb);
// X-dimension of the block const int block_offset = (tb.group_index().x * (maxThreads / threadsPerGroup) * elems_per_row) + (tb.thread_index().y * elems_per_row); const int thread_offset = tb.thread_index().x * T_per_load; const int base_offset = block_offset + thread_offset; const int stride = blockDim.x * T_per_load;
float sum = reduce::init<rop::Add, float>();
const T* input_base = vals + base_offset;
T local_buffer[unRoll * T_per_load];
#pragma unRoll for (int i = 0; i < unRoll; i++) { T* iteration_buffer = local_buffer + i * T_per_load;
mem_access::load_global<ln::granularity>( iteration_buffer, input_base + i * stride, thread_offset + i * stride < elems_per_row);
#pragma unRoll for (int j = 0; j < T_per_load; j++) { float vals_up_cast = conversion::to<float>(iteration_buffer[j]); sum = reduce::element<rop::Add>(sum, vals_up_cast); } }
reduce::partitioned_block<rop::Add, threadsPerGroup>(tb, warp, sum); const float mean = sum / elems_per_row;
float mean_diff = reduce::init<rop::Add, float>();
#pragma unRoll for (int i = 0; i < unRoll; i++) {#pragma unRoll for (int j = 0; j < T_per_load; j++) { // Using a 0 value here skews the variance, have to if-guard if (thread_offset + i * stride < elems_per_row) { float diff = (conversion::to<float>(local_buffer[i * T_per_load + j]) - mean); mean_diff = reduce::element<rop::Add>(mean_diff, diff * diff); } } }
reduce::partitioned_block<rop::Add, threadsPerGroup>(tb, warp, mean_diff); const float variance = mean_diff / elems_per_row; const float denom = __frsqrt_rn(variance + epsilon);
T* block_output = output + block_offset;
#pragma unRoll for (int i = 0; i < unRoll; i++) { T* iteration_buffer = local_buffer + i * T_per_load; const int iter_idx = i * stride + thread_offset; const bool do_loads = iter_idx < elems_per_row;
T gamma_local[T_per_load], beta_local[T_per_load];
mem_access::load_global<ln::granularity>(gamma_local, gamma + iter_idx, do_loads); mem_access::load_global<ln::granularity>(beta_local, beta + iter_idx, do_loads);
#pragma unRoll for (int j = 0; j < T_per_load; j++) { float val = conversion::to<float>(iteration_buffer[j]); val = (val - mean) * denom; val = val * conversion::to<float>(gamma_local[j]) + conversion::to<float>(beta_local[j]); iteration_buffer[j] = conversion::to<T>(val); }
if (do_loads) { mem_access::store_global<ln::granularity>(block_output + iter_idx, iteration_buffer); } }}
#define LAUNCH_FUSED_LN(unRollFactor, threadsPerGroup, maxThreads) \ fused_ln<T, unRollFactor, threadsPerGroup, maxThreads> \ <<<grid, block, 0, stream>>>(output, vals, gamma, beta, epsilon, elems_per_row);
template <typename T>void launch_fused_ln(T* output, const T* vals, const T* gamma, const T* beta, float epsilon, int rows, int elems_per_row, cudaStream_t stream){ // 8 for __half, 4 for float constexpr int T_per_load = ln::granularity / sizeof(T);
constexpr int maxThreads = 256;
// For Flaoat, unRoll 4, for __half, unRoll 2 constexpr int internal_unRoll = sizeof(T) == 4 ? 4 : 2;
const bool is_subblock_schedule = (elems_per_row <= 128) ? true : false; const int h_per_step = is_subblock_schedule ? T_per_load : T_per_load * internal_unRoll;
// Scheduling concern: may be slightly faster for some inputs to assign multiple stages of // warp-sized blocks rather than stepping up to 64/96 threads const int one_step_threads = next_pow2((elems_per_row + h_per_step - 1) / h_per_step); const int threadsPerGroup = (one_step_threads < maxThreads) ? one_step_threads : maxThreads;
const int groups_per_block_max = is_subblock_schedule ? (maxThreads + threadsPerGroup - 1) / threadsPerGroup : 1; const int groups_per_block = (rows < groups_per_block_max) ? rows : groups_per_block_max; const int groups_launch = (groups_per_block + rows - 1) / groups_per_block;
dim3 block(threadsPerGroup, groups_per_block); dim3 grid(groups_launch);
const int elems_per_step = threadsPerGroup * h_per_step; const int external_unRoll = (elems_per_row + elems_per_step - 1) / elems_per_step;
if (is_subblock_schedule) { // <=128 if (threadsPerGroup == 1) { LAUNCH_FUSED_LN(1, 1, maxThreads); } else if (threadsPerGroup == 2) { LAUNCH_FUSED_LN(1, 2, maxThreads); } else if (threadsPerGroup == 4) { LAUNCH_FUSED_LN(1, 4, maxThreads); } else if (threadsPerGroup == 8) { LAUNCH_FUSED_LN(1, 8, maxThreads); } else if (threadsPerGroup == 16) { LAUNCH_FUSED_LN(1, 16, maxThreads); } } else if (external_unRoll == 1) { // 129 - 4096 elems // (this can launch with 1-7 warps as well) LAUNCH_FUSED_LN(1 * internal_unRoll, maxThreads, maxThreads); } else if (external_unRoll == 2) { // 4097 - 8192 elems LAUNCH_FUSED_LN(2 * internal_unRoll, maxThreads, maxThreads); } else if (external_unRoll == 3) { // 8193 - 12288 elems LAUNCH_FUSED_LN(3 * internal_unRoll, maxThreads, maxThreads); } else if (external_unRoll == 4) { // 12289 - 16384 elems LAUNCH_FUSED_LN(4 * internal_unRoll, maxThreads, maxThreads); }}
#define INSTANTIATE_FUSED_LN(T) \ template void launch_fused_ln(T*, const T*, const T*, const T*, float, int, int, cudaStream_t);
INSTANTIATE_FUSED_LN(__half);#ifdef BF16_AVAILABLEINSTANTIATE_FUSED_LN(__nv_bfloat16);#endifINSTANTIATE_FUSED_LN(float);
/*Fused resiual + bias + layer norm implementation. Assumes elems_per_row % 8is equal to 0.
TODO(cmikeh2): Goal is to deprecate this implementation. The bias + residualneed to be fused into compute-bound producer operations.
Args: output: buffer for output data res_output: output of residual addition vals: buffer for input data residual: residual data bias: bias of of input data gamma: gain for normalization beta: bias for normalization epsilon: numeric stability elems_per_row: number of elements each block will normalizeTemplate arg: StoreResidual: controls whether the residual calculation is stored or not. When set to false, the input `res_output` is unused.*/template <typename T, int unRoll, int threadsPerGroup, int maxThreads, bool preLnResidual>__global__ void fused_residual_ln(T* output, T* res_output, const T* vals, const T* residual, const T* gamma, const T* beta, float epsilon, int elems_per_row){ constexpr int T_per_load = ln::granularity / sizeof(T);
cg::thread_block tb = cg::this_thread_block(); cg::thread_block_tile<hw_warp_size> warp = cg::tiled_partition<hw_warp_size>(tb);
// X-dimension of the block const int block_offset = (tb.group_index().x * (maxThreads / threadsPerGroup) * elems_per_row) + (tb.thread_index().y * elems_per_row); const int thread_offset = tb.thread_index().x * T_per_load; const int base_offset = block_offset + thread_offset; const int stride = tb.size() * T_per_load;
float sum = reduce::init<rop::Add, float>();
const T* input_base = vals + base_offset; const T* residual_base = residual + base_offset;
T local_buffer[unRoll * T_per_load];
// Unlike a vanilla layernorm, since we're fusing the two adds as well // an inner unRoll seems to be less valuable. If anything, a double unRoll // makes the most sense if we find we are having performance issues.#pragma unRoll for (int i = 0; i < unRoll; i++) { T* iteration_buffer = local_buffer + i * T_per_load; T residual_buffer[T_per_load];
mem_access::load_global<ln::granularity>( iteration_buffer, input_base + i * stride, thread_offset + i * stride < elems_per_row); mem_access::load_global<ln::granularity>(residual_buffer, residual_base + i * stride, thread_offset + i * stride < elems_per_row);
#pragma unRoll for (int j = 0; j < T_per_load; j++) { float vals_up_cast = conversion::to<float>(iteration_buffer[j]); float res_up_cast = conversion::to<float>(residual_buffer[j]); vals_up_cast += res_up_cast; sum = reduce::element<rop::Add>(sum, vals_up_cast); iteration_buffer[j] = conversion::to<T>(vals_up_cast); }
if (preLnResidual && (thread_offset + i * stride < elems_per_row)) { mem_access::store_global<ln::granularity>(res_output + base_offset + i * stride, iteration_buffer); } }
reduce::partitioned_block<rop::Add, threadsPerGroup>(tb, warp, sum); const float mean = sum / elems_per_row;
float mean_diff = reduce::init<rop::Add, float>();#pragma unRoll for (int i = 0; i < unRoll; i++) {#pragma unRoll for (int j = 0; j < T_per_load; j++) { // Using a 0 value here skews the variance, have to if-guard if (thread_offset + i * stride < elems_per_row) { float diff = (conversion::to<float>(local_buffer[i * T_per_load + j]) - mean); mean_diff = reduce::element<rop::Add>(mean_diff, diff * diff); } } }
reduce::partitioned_block<rop::Add, threadsPerGroup>(tb, warp, mean_diff); const float variance = mean_diff / elems_per_row; const float denom = __frsqrt_rn(variance + epsilon);
T* block_output = output + block_offset;
#pragma unRoll for (int i = 0; i < unRoll; i++) { T* iteration_buffer = local_buffer + i * T_per_load; const int iter_idx = i * stride + thread_offset; const bool do_loads = iter_idx < elems_per_row;
T gamma_local[T_per_load], beta_local[T_per_load];
mem_access::load_global<ln::granularity>(gamma_local, gamma + iter_idx, do_loads); mem_access::load_global<ln::granularity>(beta_local, beta + iter_idx, do_loads);
#pragma unRoll for (int j = 0; j < T_per_load; j++) { float val = conversion::to<float>(iteration_buffer[j]); val = (val - mean) * denom; val = val * conversion::to<float>(gamma_local[j]) + conversion::to<float>(beta_local[j]); iteration_buffer[j] = conversion::to<T>(val); }
if (do_loads) { mem_access::store_global<ln::granularity>(block_output + iter_idx, iteration_buffer); } }}
// TODO(cmikeh2): There's a bunch of redundancy here that needs to be removed/simplified.#define LAUNCH_FUSED_RES_LN(unRollFactor, threadsPerGroup, maxThreads) \ fused_residual_ln<T, unRollFactor, threadsPerGroup, maxThreads, false> \ <<<grid, block, 0, stream>>>( \ output, nullptr, vals, residual, gamma, beta, epsilon, elems_per_row);
template <typename T>void launch_fused_post_ln(T* output, const T* vals, const T* residual, const T* gamma, const T* beta, float epsilon, int rows, int elems_per_row, cudaStream_t stream){ // 8 for __half, 4 for float constexpr int T_per_load = ln::granularity / sizeof(T);
constexpr int maxThreads = 256;
// For Flaoat, unRoll 4, for __half, unRoll 2 constexpr int internal_unRoll = sizeof(T) == 4 ? 4 : 2;
const bool is_subblock_schedule = (elems_per_row <= 128) ? true : false; const int h_per_step = is_subblock_schedule ? T_per_load : T_per_load * internal_unRoll;
// Scheduling concern: may be slightly faster for some inputs to assign multiple stages of // warp-sized blocks rather than stepping up to 64/96 threads const int one_step_threads = next_pow2((elems_per_row + h_per_step - 1) / h_per_step); const int threadsPerGroup = (one_step_threads < maxThreads) ? one_step_threads : maxThreads;
const int groups_per_block_max = is_subblock_schedule ? (maxThreads + threadsPerGroup - 1) / threadsPerGroup : 1; const int groups_per_block = (rows < groups_per_block_max) ? rows : groups_per_block_max; const int groups_launch = (groups_per_block + rows - 1) / groups_per_block;
dim3 block(threadsPerGroup, groups_per_block); dim3 grid(groups_launch);
const int elems_per_step = threadsPerGroup * h_per_step; const int external_unRoll = (elems_per_row + elems_per_step - 1) / elems_per_step;
if (is_subblock_schedule) { // <=128 if (threadsPerGroup == 1) { LAUNCH_FUSED_RES_LN(1, 1, maxThreads); } else if (threadsPerGroup == 2) { LAUNCH_FUSED_RES_LN(1, 2, maxThreads); } else if (threadsPerGroup == 4) { LAUNCH_FUSED_RES_LN(1, 4, maxThreads); } else if (threadsPerGroup == 8) { LAUNCH_FUSED_RES_LN(1, 8, maxThreads); } else if (threadsPerGroup == 16) { LAUNCH_FUSED_RES_LN(1, 16, maxThreads); } } else if (external_unRoll == 1) { // 129 - 4096 elems // (this can launch with 1-7 warps as well) LAUNCH_FUSED_RES_LN(1 * internal_unRoll, maxThreads, maxThreads); } else if (external_unRoll == 2) { // 4097 - 8192 elems LAUNCH_FUSED_RES_LN(2 * internal_unRoll, maxThreads, maxThreads); } else if (external_unRoll == 3) { // 8193 - 12288 elems LAUNCH_FUSED_RES_LN(3 * internal_unRoll, maxThreads, maxThreads); } else if (external_unRoll == 4) { // 12289 - 16384 elems LAUNCH_FUSED_RES_LN(4 * internal_unRoll, maxThreads, maxThreads); }}
#define LAUNCH_FUSED_RES_LN_STORE_PRE_LN_RES(unRollFactor, threadsPerGroup, maxThreads) \ fused_residual_ln<T, unRollFactor, threadsPerGroup, maxThreads, true> \ <<<grid, block, 0, stream>>>( \ norm_output, res_output, vals, residual, gamma, beta, epsilon, elems_per_row);
template <typename T>void launch_fused_pre_ln(T* norm_output, T* res_output, const T* vals, const T* residual, const T* gamma, const T* beta, float epsilon, int rows, int elems_per_row, cudaStream_t stream){ // 8 for __half, 4 for float constexpr int T_per_load = ln::granularity / sizeof(T);
constexpr int maxThreads = 256;
// For Flaoat, unRoll 4, for __half, unRoll 2 constexpr int internal_unRoll = sizeof(T) == 4 ? 4 : 2;
const bool is_subblock_schedule = (elems_per_row <= 128) ? true : false; const int h_per_step = is_subblock_schedule ? T_per_load : T_per_load * internal_unRoll;
// Scheduling concern: may be slightly faster for some inputs to assign multiple stages of // warp-sized blocks rather than stepping up to 64/96 threads const int one_step_threads = next_pow2((elems_per_row + h_per_step - 1) / h_per_step); const int threadsPerGroup = (one_step_threads < maxThreads) ? one_step_threads : maxThreads;
const int groups_per_block_max = is_subblock_schedule ? (maxThreads + threadsPerGroup - 1) / threadsPerGroup : 1; const int groups_per_block = (rows < groups_per_block_max) ? rows : groups_per_block_max; const int groups_launch = (groups_per_block + rows - 1) / groups_per_block;
dim3 block(threadsPerGroup, groups_per_block); dim3 grid(groups_launch);
const int elems_per_step = threadsPerGroup * h_per_step; const int external_unRoll = (elems_per_row + elems_per_step - 1) / elems_per_step;
if (is_subblock_schedule) { // <=128 if (threadsPerGroup == 1) { LAUNCH_FUSED_RES_LN_STORE_PRE_LN_RES(1, 1, maxThreads); } else if (threadsPerGroup == 2) { LAUNCH_FUSED_RES_LN_STORE_PRE_LN_RES(1, 2, maxThreads); } else if (threadsPerGroup == 4) { LAUNCH_FUSED_RES_LN_STORE_PRE_LN_RES(1, 4, maxThreads); } else if (threadsPerGroup == 8) { LAUNCH_FUSED_RES_LN_STORE_PRE_LN_RES(1, 8, maxThreads); } else if (threadsPerGroup == 16) { LAUNCH_FUSED_RES_LN_STORE_PRE_LN_RES(1, 16, maxThreads); } } else if (external_unRoll == 1) { // 129 - 4096 elems // (this can launch with 1-7 warps as well) LAUNCH_FUSED_RES_LN_STORE_PRE_LN_RES(1 * internal_unRoll, maxThreads, maxThreads); } else if (external_unRoll == 2) { // 4097 - 8192 elems LAUNCH_FUSED_RES_LN_STORE_PRE_LN_RES(2 * internal_unRoll, maxThreads, maxThreads); } else if (external_unRoll == 3) { // 8193 - 12288 elems LAUNCH_FUSED_RES_LN_STORE_PRE_LN_RES(3 * internal_unRoll, maxThreads, maxThreads); } else if (external_unRoll == 4) { // 12289 - 16384 elems LAUNCH_FUSED_RES_LN_STORE_PRE_LN_RES(4 * internal_unRoll, maxThreads, maxThreads); }}
#define INSTANTIATE_RES_LN(T) \ template void launch_fused_post_ln<T>( \ T*, const T*, const T*, const T*, const T*, float, int, int, cudaStream_t);
#define INSTANTIATE_PRE_LN_RES(T) \ template void launch_fused_pre_ln<T>( \ T*, T*, const T*, const T*, const T*, const T*, float, int, int, cudaStream_t);
INSTANTIATE_RES_LN(__half);INSTANTIATE_RES_LN(float);#ifdef BF16_AVAILABLEINSTANTIATE_RES_LN(__nv_bfloat16);#endif
INSTANTIATE_PRE_LN_RES(__half);INSTANTIATE_PRE_LN_RES(float);#ifdef BF16_AVAILABLEINSTANTIATE_PRE_LN_RES(__nv_bfloat16);#endif123456# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
from .cuda_linear import *123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207# Copyright (c) Microsoft Corporation.# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
import torch
from ....inference_utils import DtypeEnumfrom ....logging import inference_loggerfrom deepspeed.ops.op_builder import InferenceCoreBuilderfrom ... import DSKernelBase
class CUDAWf6Af16Linear(DSKernelBase): """ Wrapper around the CUDA kernel of Wf6Af16 quantized linear.
Performs z = x @ y """ supported_dtypes = [DtypeEnum.fp16]
def __init__(self): self.inf_module = InferenceCoreBuilder().load() self.inf_module.create_handle() self.kernel = self.inf_module.cuda_wf6af16_linear # The split_k_map is profiled on A100-80G GPU for some common shapes. # It is an array of dictionaries, where the array index is the tokens chunk id. # The dictionary is the mapping from the output channel to the split-K size. self.split_k_map = [ { # tokens: [1, 64] 3072: 18, 4096: 13, 5120: 10, 6144: 9, 8192: 6, 10240: 5, 14336: 7, 28672: 7, 57344: 7 }, { # tokens: [65:128] 3072: 9, 4096: 6, 5120: 5, 6144: 9, 8192: 3, 10240: 5, 14336: 7, 28672: 7, 57344: 6 }, { # tokens: [129:192] 3072: 6, 4096: 4, 5120: 7, 6144: 3, 8192: 2, 10240: 5, 14336: 5, 28672: 5, 57344: 4 }, { # tokens: [193:256] 3072: 9, 4096: 3, 5120: 5, 6144: 2, 8192: 5, 10240: 4, 14336: 8, 28672: 6, 57344: 4 }, { # tokens: [257:320] 3072: 7, 4096: 5, 5120: 2, 6144: 5, 8192: 4, 10240: 1, 14336: 3, 28672: 3, 57344: 4 }, { # tokens: [321:384] 3072: 3, 4096: 2, 5120: 5, 6144: 3, 8192: 1, 10240: 8, 14336: 3, 28672: 4, 57344: 3 }, { # tokens: [385:448] 3072: 5, 4096: 7, 5120: 3, 6144: 5, 8192: 7, 10240: 3, 14336: 1, 28672: 1, 57344: 3 }, { # tokens: [449:512] 3072: 2, 4096: 5, 5120: 4, 6144: 1, 8192: 5, 10240: 2, 14336: 6, 28672: 4, 57344: 1 }, { # tokens: [513:576] 3072: 2, 4096: 3, 5120: 1, 6144: 1, 8192: 3, 10240: 3, 14336: 3, 28672: 1, 57344: 1 }, { # tokens: [577:640] 3072: 5, 4096: 4, 5120: 1, 6144: 4, 8192: 2, 10240: 1, 14336: 1, 28672: 1, 57344: 1 }, { # tokens: [641:704] 3072: 3, 4096: 1, 5120: 2, 6144: 2, 8192: 1, 10240: 2, 14336: 1, 28672: 1, 57344: 1 }, { # tokens: [705:768] 3072: 3, 4096: 1, 5120: 3, 6144: 2, 8192: 1, 10240: 1, 14336: 1, 28672: 1, 57344: 1 } ]
def __call__(self, output: torch.Tensor, hidden_states: torch.Tensor, weights_2bit: torch.Tensor, weights_4bit: torch.Tensor, scale: torch.Tensor, out_channels, tokens, in_channels) -> torch.Tensor: """ Matmul kernel of FP6 weight-only quantized linear. All inputs should be contiguous. It does not support batched-matmul.
Parameters: output (torch.Tensor): Output tensor. Shape is of [token_number, out_features] hidden_states (torch.Tensor): Input tensor. Shape is of [token_number, in_features] weights_2bit (torch.Tensor): Input tensor of the 2-bit slice. Shape is of [out_features*2/8, in_features] weights_4bit (torch.Tensor): Input tensor of the 4-bit slice. Shape is of [out_features*4/8, in_features] scale (torch.Tensor): Input tensor. Shape is of [out_features], since the scale is per output channel out_channels (int): The number of output channels tokens (int): The number of tokens in_channels (int): The number of input channels """
if out_channels % 256 != 0 or in_channels % 64 != 0: raise ValueError("The out and in channel should be multiple of 256 and 64 respectively.")
# TODO: add a more general heuristic to determine the split-K. split_k = -1 # not initialized if tokens <= 768: # Try to find the split-K from the pre-profiled map. tokens_chunk_id = (tokens - 1) // 64 split_k = self.split_k_map[tokens_chunk_id].get(out_channels, -1) if split_k == -1: split_k = 1 inference_logger().warning( f"The split-K setting may be suboptimal for shape {tokens}x{in_channels}x{out_channels}...")
workspace = self.get_workspace(out_channels, tokens, in_channels, split_k, torch.float, hidden_states.device) self.kernel(output, hidden_states, weights_2bit, weights_4bit, scale, workspace, out_channels, tokens, in_channels, split_k)
def get_workspace(self, out_channels: int, tokens: int, in_channels: int, split_k: int, dtype, device) -> torch.Tensor: """ Allocate workspace for the kernel. The workspace is used to store the intermediate results of the matmul before split-K. The split-K size is determined by the size of the matmul. """ workspace = torch.empty((split_k, out_channels, tokens), dtype=dtype, device=device)
return workspace123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
// This is a copy of FP6-LLM kernel code: https://arxiv.org/abs/2401.14112
#ifndef CONFIGS_H#define CONFIGS_H
// #define DEBUG_MODE#define PIPELINE_LEVEL_GMEM 2#define PIPELINE_LEVEL_SMEM 2 // only support 2
/************************ Hardware Parameters ************************/#define WARP_SIZE 32#define REG_BIT_WIDTH 32// mma: M=16 K=16 N=8#define MMA_8 8#define MMA_16 16// for memory access#define THREAD_OPT_ACCESS_BIT_WIDTH_128 128 // LDS.128, cp_async.128, ...#define BIT_WIDTH_PER_HALF 16 // Half precision: FP16
/******************** Register Allocation For GEMM ********************/#define REG_PER_THREAD_C_TENSOR_16_16 8 // 8 for FP32 Accumulation/********************** Memory Padding Parameters **********************/// Eliminating bank-conflict#define PADDING_BYTES_16 16 // Padding 16 bytes each column#define PADDING_SHARED_MEM_FOR_B_8 \ 8 // Padding 8 half each column, during CopyFromGlobalToShared() for B#define PADDING_SHARED_MEM_FOR_C_4 \ 4 // Padding 4 float each column, during StoreToSharedMemoryFromRegister() for C/************************* WARP Tiling part-1 *************************/#define WARP_ROW_MMA_TENSORS 4#define WARP_M (WARP_ROW_MMA_TENSORS * MMA_16) // 64#define WARP_K_MMA_TENSORS 4#define WARP_K (WARP_K_MMA_TENSORS * MMA_16) // 64template <int BLOCK_ROW_WARPS_, int BLOCK_COL_WARPS_, int WARP_COL_MMA_TENSORS_>struct TilingConfig { // Depending on "n" dimension of the GEMM static constexpr int BLOCK_ROW_WARPS = BLOCK_ROW_WARPS_; static constexpr int BLOCK_COL_WARPS = BLOCK_COL_WARPS_; static constexpr int WARP_COL_MMA_TENSORS = WARP_COL_MMA_TENSORS_; /************************* WARP Tiling part-2 *************************/ static constexpr int WARP_N = WARP_COL_MMA_TENSORS * MMA_8; /*************************Thread Block Tiling *************************/ static constexpr int TILE_M = WARP_M * BLOCK_ROW_WARPS; static constexpr int TILE_N = MMA_8 * WARP_COL_MMA_TENSORS * BLOCK_COL_WARPS; static constexpr int TILE_K = WARP_K; /********************** #Thread per Thread Block **********************/ static constexpr int BLOCK_WARPS = BLOCK_ROW_WARPS * BLOCK_COL_WARPS; static constexpr int BLOCK_THREADS = BLOCK_WARPS * WARP_SIZE; /******************************* Others *******************************/ static constexpr int SMEM_SIZE_B_TILE = TILE_N * (TILE_K + PADDING_BYTES_16) * 2 * PIPELINE_LEVEL_GMEM; // sizeof(half)=2, doubleBuffer=2 static constexpr int SMEM_SIZE_C_TILE = TILE_N * (TILE_M + PADDING_BYTES_16) * 4; // sizeof(float)=4};
/************************ General Config for Quant-LLM **********************/#define WEIGHT_FRAG1_BIT_WIDTH 2#define WEIGHT_FRAG2_BIT_WIDTH 4#define WEIGHT_BIT_WIDTH (WEIGHT_FRAG1_BIT_WIDTH + WEIGHT_FRAG2_BIT_WIDTH) // 6// #define QUANT_GROUP_SIZE_DIVIDED_BY_64 4 //// QuantGroupSize: 4*64 = 256/*************************** 64*64 Weghts of A WARP *************************/#define WEIGHT_PER_UNIT (WARP_M * WARP_K) // 64*64#define SMEM_SIZE_IN_BYTES_PER_WARP_A1 \ (WEIGHT_PER_UNIT * WEIGHT_FRAG1_BIT_WIDTH / \ 8) // 1024 Bytes #doubleBuffer not takedn into consideration#define SMEM_SIZE_IN_BYTES_PER_WARP_A2 \ (WEIGHT_PER_UNIT * WEIGHT_FRAG2_BIT_WIDTH / \ 8) // 2048 Bytes #doubleBuffer not takedn into consideration#define SMEM_SIZE_A1_TILE \ (SMEM_SIZE_IN_BYTES_PER_WARP_A1 * 4 * \ PIPELINE_LEVEL_GMEM) // #WARP=4, #Trible-Buffer for 3-level pipeline for A = 12 KB; double // buffer for 2-level pipeline A= 8 KB.#define SMEM_SIZE_A2_TILE \ (SMEM_SIZE_IN_BYTES_PER_WARP_A2 * 4 * \ PIPELINE_LEVEL_GMEM) // #WARP=4, #Trible-Buffer for 3-level pipeline for A = 24 KB; double // buffer for 2-level pipeline A= 16 KB./******************** Global Memory Layout For QUANTIZED DATA ******************/#define NUM_INT4_PER_UNIT_2BIT_FRAG (WEIGHT_PER_UNIT * WEIGHT_FRAG1_BIT_WIDTH / 128) // 64#define NUM_INT4_PER_UNIT_4BIT_FRAG (WEIGHT_PER_UNIT * WEIGHT_FRAG2_BIT_WIDTH / 128) // 128/******************** Register Allocation For QUANTIZED DATA ******************/#define WEIGHT_PER_THREAD (WEIGHT_PER_UNIT / WARP_SIZE) // 128#define REG_PER_THREAD_2BIT_FRAG (WEIGHT_PER_THREAD / REG_BIT_WIDTH * 2) // 8#define REG_PER_THREAD_4BIT_FRAG (WEIGHT_PER_THREAD / REG_BIT_WIDTH * 4) // 16/******************** Register Allocation For QUANT Scales ******************/#define WARP_REG_QUANT_SCALE 4 // 8 rows per thread -> 8 FP16 scales -> 4 registers#define WARP_REG_QUANT_SCALE_DISTRIBUTED \ 1 // T0-T3, T4-T7, ..., T28-T31 share the same scales, using shfl to get all the scales for // each thread
#endif // CONFIGS_H123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
// This is a copy of FP6-LLM kernel code: https://arxiv.org/abs/2401.14112
#ifndef DEEPSPEED_CUDA_LINEAR_KERNEL_MATMUL_CUH#define DEEPSPEED_CUDA_LINEAR_KERNEL_MATMUL_CUH
#include "configs.h"#include "utils_core.cuh"#include "utils_gmem.cuh"
/* * C = A*B * A: row major with ahead-of-time layout transformation, FP6 * B: col major, FP16 * C: col major, FP16 */template <typename TilingConfig, typename OutputDataType>__global__ void QUANT_GEMM_Kernel(const uint4* Weight1, const uint4* Weight2, const half* Scales, const half* B, OutputDataType* C, const size_t M_Global, const size_t N_Global, const size_t K_Global, int Split_K){#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800 && __CUDA_ARCH__ < 900
#ifdef DEBUG_MODE assert(K_Global % TilingConfig::TILE_K == 0); assert(M_Global % TilingConfig::TILE_M == 0); assert(gridDim.y == Split_K * (M_Global / TilingConfig::TILE_M));#endif extern __shared__ __align__(128) half smem[]; // Dynamic shared memory for FP16 A tiles, 128 Bytes aligned half(*smem_array)[WARP_K + PADDING_SHARED_MEM_FOR_B_8] = reinterpret_cast<half(*)[WARP_K + PADDING_SHARED_MEM_FOR_B_8]>( smem + (SMEM_SIZE_A1_TILE + SMEM_SIZE_A2_TILE) / 2); // Dynamic shared memory for FP16 B tiles __shared__ half QuantScales[64 * TilingConfig::BLOCK_WARPS]; // static shared memory for // quantization scales, 64 row per // warp * 4 warps = 512 Bytes // Thread Block Mapping, considering SplitK const size_t BatchID = blockIdx.y / (M_Global / TilingConfig::TILE_M); const size_t x = blockIdx.x; // Output Block ID: (BlockID_Row = y; BlockID_Col = x ) const size_t y = blockIdx.y % (M_Global / TilingConfig::TILE_M); // Output Block ID: (BlockID_Row = y; BlockID_Col = x ) const size_t Tile_Start_M = y * TilingConfig::TILE_M; const size_t Tile_Start_N = x * TilingConfig::TILE_N; const size_t NumColumnToCopy = (N_Global - Tile_Start_N) < TilingConfig::TILE_N ? (N_Global - Tile_Start_N) : TilingConfig::TILE_N; const size_t NumBlock_K = K_Global / TilingConfig::TILE_K; const size_t AverageNumBlock_K = NumBlock_K / Split_K; const size_t ExtraNumBlock_K = NumBlock_K - AverageNumBlock_K * Split_K; size_t NumIter = AverageNumBlock_K; if (BatchID < ExtraNumBlock_K) NumIter++; size_t StartBlockID_K = AverageNumBlock_K * BatchID; if (BatchID < ExtraNumBlock_K) StartBlockID_K += BatchID; else StartBlockID_K += ExtraNumBlock_K; // Warp ID. const int warpId = threadIdx.x / WARP_SIZE; int WARP_i = warpId / TilingConfig::BLOCK_COL_WARPS; // WARP_i: row number; WARP_j: column number // int WARP_j = warpId % TilingConfig::BLOCK_COL_WARPS; // Global Memory Address for Matrix A (Weight) // ///////////////////////////////////////////////////////////////////////// StartPTR for each // ThreadBlock(TB) const uint4* TB_StartGPTR_A1 = Weight1 + (y * TilingConfig::BLOCK_ROW_WARPS) * NumBlock_K * NUM_INT4_PER_UNIT_2BIT_FRAG; const uint4* TB_StartGPTR_A2 = Weight2 + (y * TilingConfig::BLOCK_ROW_WARPS) * NumBlock_K * NUM_INT4_PER_UNIT_4BIT_FRAG; // StartPTR for each WARP. const uint4* WARP_StartGPTR_A1 = TB_StartGPTR_A1 + WARP_i * NumBlock_K * NUM_INT4_PER_UNIT_2BIT_FRAG; const uint4* WARP_StartGPTR_A2 = TB_StartGPTR_A2 + WARP_i * NumBlock_K * NUM_INT4_PER_UNIT_4BIT_FRAG; // StartPTR for each WARP, considering SplitK const size_t WARP_Start_UnitID_K = StartBlockID_K; WARP_StartGPTR_A1 += WARP_Start_UnitID_K * NUM_INT4_PER_UNIT_2BIT_FRAG; WARP_StartGPTR_A2 += WARP_Start_UnitID_K * NUM_INT4_PER_UNIT_4BIT_FRAG; // Copying A tile from Global to Shared, using double-buffer // ////////////////////////////////////////////////////////// StartSPTR for each ThreadBlock uint32_t* AFrag_2BIT_SPTR = reinterpret_cast<uint32_t*>(smem); uint32_t* AFrag_4BIT_SPTR = AFrag_2BIT_SPTR + SMEM_SIZE_IN_BYTES_PER_WARP_A1 / 4 * TilingConfig::BLOCK_WARPS * PIPELINE_LEVEL_GMEM; // 8 buffers including double buffers, 12 for trible buffers // StartSPTR for each WARP AFrag_2BIT_SPTR += warpId * SMEM_SIZE_IN_BYTES_PER_WARP_A1 / 4; AFrag_4BIT_SPTR += warpId * SMEM_SIZE_IN_BYTES_PER_WARP_A2 / 4; // Pre-fetch of A tile for (int i = 0; i < PIPELINE_LEVEL_GMEM - 1; i++) { CopyFromGlobalToShared_A<SMEM_SIZE_IN_BYTES_PER_WARP_A1>( AFrag_2BIT_SPTR + i * SMEM_SIZE_IN_BYTES_PER_WARP_A1 / 4 * 4, WARP_StartGPTR_A1); CopyFromGlobalToShared_A<SMEM_SIZE_IN_BYTES_PER_WARP_A2>( AFrag_4BIT_SPTR + i * SMEM_SIZE_IN_BYTES_PER_WARP_A2 / 4 * 4, WARP_StartGPTR_A2); WARP_StartGPTR_A1 += SMEM_SIZE_IN_BYTES_PER_WARP_A1 / 16; WARP_StartGPTR_A2 += SMEM_SIZE_IN_BYTES_PER_WARP_A2 / 16; } // Global Memory Address for Matrix A (QuantScale) // ///////////////////////////////////////////////////////////////////// const half* TB_StartGPTR_A_Scale = Scales + (y * TilingConfig::BLOCK_ROW_WARPS) * 64; const half* WARP_StartGPTR_A_Scales = TB_StartGPTR_A_Scale + WARP_i * 64; CopyFromGlobalToShared_Scales(QuantScales + WARP_i * 64, WARP_StartGPTR_A_Scales); // Copying B tile from Global to Shared, considering SplitK // ///////////////////////////////////////////////////////////// const half* BTile_GPTR = B + Tile_Start_N * K_Global + StartBlockID_K * TilingConfig::TILE_K; for (int i = 0; i < PIPELINE_LEVEL_GMEM - 1; i++) { CopyFromGlobalToShared<TilingConfig::TILE_N, TilingConfig::BLOCK_WARPS>( smem_array + i * TilingConfig::TILE_N, BTile_GPTR, K_Global, NumColumnToCopy); BTile_GPTR += TilingConfig::TILE_K; } // Register Allocation for A,B, and C, Initilazed to Zeros // ///////////////////////////////////////////////////////////////////// constexpr int NumRegSets_a = WARP_ROW_MMA_TENSORS; // 1 set = 4 registers, containing a 16*16 MMA block constexpr int NumRegSets_b = (TilingConfig::WARP_COL_MMA_TENSORS == 1) ? 1 : TilingConfig::WARP_COL_MMA_TENSORS / 2; // 1 set = 4 registers, containing a 16*16 MMA block#ifdef PIPELINE_LEVEL_SMEM uint32_t a[NumRegSets_a * PIPELINE_LEVEL_SMEM] [4]; // double/Trible buffer is used // Registers to store decompressed FP6 uint32_t b[NumRegSets_b * PIPELINE_LEVEL_SMEM] [4]; // double/Triple buffer is used // Register to store FP16 B matrix (a slice)#endif float c[NumRegSets_a * NumRegSets_b][REG_PER_THREAD_C_TENSOR_16_16]; for (int i = 0; i < NumRegSets_a * NumRegSets_b; i++) for (int j = 0; j < REG_PER_THREAD_C_TENSOR_16_16; j++) c[i][j] = 0.0f; // cp_async_wait_all(); __syncthreads();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// uint32_t Scales_RPTR[4]; // 4 Registers per thread for Quantization Scales ExtractFromSharedToReg_Scales(Scales_RPTR, QuantScales + WARP_i * 64);#ifdef PIPELINE_LEVEL_SMEM // Initializing the Software Pipeline: writing registers. // //////////////////////////////////////////////////////////////////////////////////////////////// initialize_mma_slice<TilingConfig>( a, b, AFrag_2BIT_SPTR, AFrag_4BIT_SPTR, smem_array, Scales_RPTR);#endif// The outer loop.// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////#pragma unroll(1) for (size_t tile_id_k = 0; tile_id_k < NumIter; tile_id_k++) { // Trible-Buffer for A Tile uint32_t* __restrict__ read_SPTR_Frag1 = AFrag_2BIT_SPTR + ((tile_id_k + 0) % PIPELINE_LEVEL_GMEM) * SMEM_SIZE_IN_BYTES_PER_WARP_A1 / 4 * 4; // 1024 (1)*4: 4 WARPs; (2)/4: int*+1 = char*+16 uint32_t* __restrict__ read_SPTR_Frag2 = AFrag_4BIT_SPTR + ((tile_id_k + 0) % PIPELINE_LEVEL_GMEM) * SMEM_SIZE_IN_BYTES_PER_WARP_A2 / 4 * 4; // 2048 (1)*4: 4 WARPs; (2)/4: int*+1 = char*+16#ifdef PIPELINE_LEVEL_SMEM uint32_t* __restrict__ read2_SPTR_Frag1 = AFrag_2BIT_SPTR + ((tile_id_k + 1) % PIPELINE_LEVEL_GMEM) * SMEM_SIZE_IN_BYTES_PER_WARP_A1 / 4 * 4; uint32_t* __restrict__ read2_SPTR_Frag2 = AFrag_4BIT_SPTR + ((tile_id_k + 1) % PIPELINE_LEVEL_GMEM) * SMEM_SIZE_IN_BYTES_PER_WARP_A2 / 4 * 4;#endif uint32_t* __restrict__ write_SPTR_Frag1 = AFrag_2BIT_SPTR + ((tile_id_k + (PIPELINE_LEVEL_GMEM - 1)) % PIPELINE_LEVEL_GMEM) * SMEM_SIZE_IN_BYTES_PER_WARP_A1 / 4 * 4; // 1024 (1)*4: 4 WARPs; (2)/4: int*+1 = char*+16 uint32_t* __restrict__ write_SPTR_Frag2 = AFrag_4BIT_SPTR + ((tile_id_k + (PIPELINE_LEVEL_GMEM - 1)) % PIPELINE_LEVEL_GMEM) * SMEM_SIZE_IN_BYTES_PER_WARP_A2 / 4 * 4; // 2048 (1)*4: 4 WARPs; (2)/4: int*+1 = char*+16 // Trible-Buffer for B Tile half(*__restrict__ read_SPTR)[WARP_K + PADDING_SHARED_MEM_FOR_B_8] = smem_array + ((tile_id_k + 0) % PIPELINE_LEVEL_GMEM) * TilingConfig::TILE_N;#ifdef PIPELINE_LEVEL_SMEM half(*__restrict__ read2_SPTR)[WARP_K + PADDING_SHARED_MEM_FOR_B_8] = smem_array + ((tile_id_k + 1) % PIPELINE_LEVEL_GMEM) * TilingConfig::TILE_N;#endif half(*__restrict__ write_SPTR)[WARP_K + PADDING_SHARED_MEM_FOR_B_8] = smem_array + ((tile_id_k + (PIPELINE_LEVEL_GMEM - 1)) % PIPELINE_LEVEL_GMEM) * TilingConfig::TILE_N; // bool GlobalCopy = (tile_id_k + PIPELINE_LEVEL_GMEM - 1) < NumIter; // Copying A tile from Global to Register, Bypassing L1, using double-buffer CopyFromGlobalToShared_A<SMEM_SIZE_IN_BYTES_PER_WARP_A1>( write_SPTR_Frag1, WARP_StartGPTR_A1, GlobalCopy); CopyFromGlobalToShared_A<SMEM_SIZE_IN_BYTES_PER_WARP_A2>( write_SPTR_Frag2, WARP_StartGPTR_A2, GlobalCopy); // copying B tile from GlobalMemory to SharedMemory CopyFromGlobalToShared<TilingConfig::TILE_N, TilingConfig::BLOCK_WARPS>( write_SPTR, BTile_GPTR, K_Global, NumColumnToCopy, GlobalCopy); cp_async_group_commit();#ifdef PIPELINE_LEVEL_SMEM core_mma_slice<TilingConfig>(c, a, b, read_SPTR_Frag1, read_SPTR_Frag2, read_SPTR, Scales_RPTR, 1); // read_SPTR_Frag1, read_SPTR_Frag2 are different for each // WARP; read_SPTR is shared among WARPs core_mma_slice<TilingConfig>( c, a, b, read_SPTR_Frag1, read_SPTR_Frag2, read_SPTR, Scales_RPTR, 2); core_mma_slice<TilingConfig>( c, a, b, read_SPTR_Frag1, read_SPTR_Frag2, read_SPTR, Scales_RPTR, 3); // Barriers and Synchronizations cp_async_wait_group<PIPELINE_LEVEL_GMEM - 2>(); __syncthreads(); core_mma_slice<TilingConfig>( c, a, b, read2_SPTR_Frag1, read2_SPTR_Frag2, read2_SPTR, Scales_RPTR, 0); // Updating global PTRs WARP_StartGPTR_A1 += SMEM_SIZE_IN_BYTES_PER_WARP_A1 / 16; // 4KB/16=256 (1)/16: int4*+1 = char*+16 WARP_StartGPTR_A2 += SMEM_SIZE_IN_BYTES_PER_WARP_A2 / 16; // 8KB/16=512 (1)/16: int4*+1 = char*+16 BTile_GPTR += TilingConfig::TILE_K;#else PipelinedCoreLoop<TilingConfig>( c, read_SPTR, read_SPTR_Frag1, read_SPTR_Frag2, Scales_RPTR); // read_SPTR_Frag1, read_SPTR_Frag2 are different for each WARP; // read_SPTR is shared among WARPs // Updating global PTRs WARP_StartGPTR_A1 += SMEM_SIZE_IN_BYTES_PER_WARP_A1 / 16; // 4KB/16=256 (1)/16: int4*+1 = char*+16 WARP_StartGPTR_A2 += SMEM_SIZE_IN_BYTES_PER_WARP_A2 / 16; // 8KB/16=512 (1)/16: int4*+1 = char*+16 BTile_GPTR += TilingConfig::TILE_K; // Barriers and Synchronizations cp_async_wait_group<PIPELINE_LEVEL_GMEM - 2>(); __syncthreads();#endif } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Store the C fragments to shared memory. float(*smem_CFrag)[TilingConfig::TILE_M + PADDING_SHARED_MEM_FOR_C_4] = reinterpret_cast<float(*)[TilingConfig::TILE_M + PADDING_SHARED_MEM_FOR_C_4]>(smem); StoreToSharedMemoryFromRegister<TilingConfig>(smem_CFrag, c); __syncthreads(); // Now that shared memory contains all the D tiles, stream them to global memory. OutputDataType* BlockGlobalPTR = C + BatchID * (M_Global * N_Global) + Tile_Start_M + Tile_Start_N * M_Global; for (size_t i = warpId; i < NumColumnToCopy; i += TilingConfig::BLOCK_WARPS) // i-th column#pragma unroll for (size_t j = threadIdx.x % WARP_SIZE; j < TilingConfig::TILE_M; j += WARP_SIZE) // j-th row { if constexpr (std::is_same<OutputDataType, half>::value) BlockGlobalPTR[j + i * M_Global] = __float2half_rn(smem_CFrag[i][j]); else BlockGlobalPTR[j + i * M_Global] = smem_CFrag[i][j]; }
#else assert(("The FP6 functions are only available on Ampere GPUs.", false));#endif}
#endif12345678910111213141516171819202122232425262728293031323334353637383940414243// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
// This is a copy of FP6-LLM kernel code: https://arxiv.org/abs/2401.14112
#ifndef DEEPSPEED_CUDA_LINEAR_KERNEL_REDUCTION_CUH#define DEEPSPEED_CUDA_LINEAR_KERNEL_REDUCTION_CUH
#include <cuda.h>#include <cuda_fp16.h>#include <cuda_runtime.h>
#define REDUCTION_ELEMENT_PER_THREADBLOCK 256#define HALF_PER_128BIT 8
__global__ void SplitK_Reduction(half* C, float* Reduction_Workspace, size_t M_Global, size_t N_Global, int Split_K){ half* WARP_GPTR_C = C + REDUCTION_ELEMENT_PER_THREADBLOCK * blockIdx.x; float* WARP_GPTR_R = Reduction_Workspace + REDUCTION_ELEMENT_PER_THREADBLOCK * blockIdx.x; half* THREAD_GPTR_C = WARP_GPTR_C + threadIdx.x * HALF_PER_128BIT; float* THREAD_GPTR_R = WARP_GPTR_R + threadIdx.x * HALF_PER_128BIT; // Initializing Thread-Local Results float Results[HALF_PER_128BIT];#pragma unroll for (int i = 0; i < HALF_PER_128BIT; i++) Results[i] = 0.0f; // Reduction for (int i = 0; i < Split_K; i++) {#pragma unroll for (int j = 0; j < HALF_PER_128BIT; j++) Results[j] += THREAD_GPTR_R[j]; THREAD_GPTR_R += M_Global * N_Global; }// Writing to global memory#pragma unroll for (int i = 0; i < HALF_PER_128BIT; i++) THREAD_GPTR_C[i] = __float2half_rn(Results[i]);}
#endif123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
// This is a copy of FP6-LLM kernel code: https://arxiv.org/abs/2401.14112
#ifndef DEEPSPEED_CUDA_LINEAR_PTX_CP_ASYNC_CUH#define DEEPSPEED_CUDA_LINEAR_PTX_CP_ASYNC_CUH
#include <cuda.h>#include <cuda_fp16.h>#include <cuda_runtime.h>
template <int SizeInBytes>__device__ __forceinline__ void cp_async(half* smem_ptr, const half* global_ptr, bool pred_guard = true){#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800 static_assert(SizeInBytes == 16, "Size is not supported"); unsigned smem_int_ptr = __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_guard), "r"(smem_int_ptr), "l"(global_ptr), "n"(SizeInBytes));#else assert( ("The async copy functions are only supported on Ampere and newer architectures", false));#endif}
/// Establishes an ordering w.r.t previously issued cp.async instructions. Does not block.__device__ __forceinline__ void cp_async_group_commit(){#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800 asm volatile("cp.async.commit_group;\n" ::);#else assert( ("The async copy functions are only supported on Ampere and newer architectures", false));#endif}
/// Blocks until all but <N> previous cp.async.commit_group operations have committed.template <int N>__device__ __forceinline__ void cp_async_wait_group(){#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800 asm volatile("cp.async.wait_group %0;\n" ::"n"(N));#else assert( ("The async copy functions are only supported on Ampere and newer architectures", false));#endif}
/// Blocks until all previous cp.async.commit_group operations have committed.// cp.async.wait_all is equivalent to :// cp.async.commit_group;// cp.async.wait_group 0;__device__ __forceinline__ void cp_async_wait_all(){#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800 asm volatile("cp.async.wait_all;\n" ::);#else assert( ("The async copy functions are only supported on Ampere and newer architectures", false));#endif}
#endif123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
// This is a copy of FP6-LLM kernel code: https://arxiv.org/abs/2401.14112
#ifndef DEEPSPEED_CUDA_LINEAR_PTX_MMA_CUH#define DEEPSPEED_CUDA_LINEAR_PTX_MMA_CUH
#include <cuda.h>#include <cuda_fp16.h>#include <cuda_runtime.h>
#include <assert.h>#include "configs.h"
#ifdef PIPELINE_LEVEL_SMEMtemplate <typename TilingConfig>__device__ __forceinline__ void B_FromSharedToReg( uint32_t (*__restrict__ Reg)[4], half (*__restrict__ read_SPTR)[WARP_K + PADDING_SHARED_MEM_FOR_B_8], int slice_id){#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800#ifdef DEBUG_MODE static_assert((TilingConfig::WARP_COL_MMA_TENSORS == 1) || (TilingConfig::WARP_COL_MMA_TENSORS % 2 == 0));#endif
const int warpId = threadIdx.x / WARP_SIZE; int lane_id = threadIdx.x % WARP_SIZE; int WARP_j = warpId % TilingConfig::BLOCK_COL_WARPS; int warp_start_col = TilingConfig::WARP_COL_MMA_TENSORS * MMA_8 * WARP_j; // each warp may start from reading warp_start_col'th column of // the B tile in shared memory#ifdef DEBUG_MODE assert(warp_start_col == 0);#endif
int col = (lane_id % 8) + (lane_id / 16) * 8; int row = (lane_id % 16) / 8 * 8; uint32_t smem_local_ptr = static_cast<uint32_t>( __cvta_generic_to_shared(&read_SPTR[warp_start_col + col][slice_id * MMA_16 + row])); if (TilingConfig::WARP_COL_MMA_TENSORS == 1) { asm volatile("ldmatrix.sync.aligned.x2.m8n8.shared.b16 {%0, %1}, [%2];\n" : "=r"(Reg[0][0]), "=r"(Reg[0][1]) : "r"(smem_local_ptr)); } else {#pragma unroll for (int i = 0; i < TilingConfig::WARP_COL_MMA_TENSORS / 2; i++) { asm volatile("ldmatrix.sync.aligned.x4.m8n8.shared.b16 {%0, %1, %2, %3}, [%4];\n" : "=r"(Reg[i][0]), "=r"(Reg[i][1]), "=r"(Reg[i][2]), "=r"(Reg[i][3]) : "r"(smem_local_ptr)); smem_local_ptr += 16 * (WARP_K + PADDING_SHARED_MEM_FOR_B_8) * sizeof(half); } }#else assert( ("The matrix load functions are only supported on Ampere and newer architectures", false));#endif}#else// Debug: Whether ldmatrix.trans is required???// B is in column-majortemplate <typename TilingConfig>__device__ __forceinline__ void B_FromSharedToReg( uint32_t (*__restrict__ Reg)[4], half (*__restrict__ read_SPTR)[WARP_K + PADDING_SHARED_MEM_FOR_B_8], int k_offset){#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800#ifdef DEBUG_MODE static_assert((TilingConfig::WARP_COL_MMA_TENSORS == 1) || (TilingConfig::WARP_COL_MMA_TENSORS % 2 == 0));#endif
const int warpId = threadIdx.x / WARP_SIZE; int lane_id = threadIdx.x % WARP_SIZE; int WARP_j = warpId % TilingConfig::BLOCK_COL_WARPS; int warp_start_col = TilingConfig::WARP_COL_MMA_TENSORS * MMA_8 * WARP_j; // each warp may start from reading warp_start_col'th column of // the B tile in shared memory#ifdef DEBUG_MODE assert(warp_start_col == 0);#endif
int col = (lane_id % 8) + (lane_id / 16) * 8; int row = (lane_id % 16) / 8 * 8; uint32_t smem_local_ptr = static_cast<uint32_t>( __cvta_generic_to_shared(&read_SPTR[warp_start_col + col][k_offset + row])); if (TilingConfig::WARP_COL_MMA_TENSORS == 1) { asm volatile("ldmatrix.sync.aligned.x2.m8n8.shared.b16 {%0, %1}, [%2];\n" : "=r"(Reg[0][0]), "=r"(Reg[0][1]) : "r"(smem_local_ptr)); } else {#pragma unroll for (int i = 0; i < TilingConfig::WARP_COL_MMA_TENSORS / 2; i++) { asm volatile("ldmatrix.sync.aligned.x4.m8n8.shared.b16 {%0, %1, %2, %3}, [%4];\n" : "=r"(Reg[i][0]), "=r"(Reg[i][1]), "=r"(Reg[i][2]), "=r"(Reg[i][3]) : "r"(smem_local_ptr)); smem_local_ptr += 16 * (WARP_K + PADDING_SHARED_MEM_FOR_B_8) * sizeof(half); } }#else assert( ("The matrix load functions are only supported on Ampere and newer architectures", false));#endif}#endif
__device__ __forceinline__ void MMA_FP16_M16N8K16(uint32_t* __restrict__ c, uint32_t* __restrict__ a, uint32_t* __restrict__ b){#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800 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 };" : "=r"(c[0]), "=r"(c[1]), "=r"(c[2]), "=r"(c[3]) : "r"(a[0]), "r"(a[1]), "r"(a[2]), "r"(a[3]), "r"(b[0]), "r"(b[1]), "r"(c[0]), "r"(c[1]), "r"(c[2]), "r"(c[3]));#else assert(("The mma functions are only implemented for Ampere and newer architectures", false));#endif}
#endif123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
// This is a copy of FP6-LLM kernel code: https://arxiv.org/abs/2401.14112
#ifndef DEEPSPEED_CUDA_LINEAR_UTILS_CORE_CUH#define DEEPSPEED_CUDA_LINEAR_UTILS_CORE_CUH
#include <assert.h>
#include "configs.h"#include "ptx_mma.cuh"#include "utils_paralleldequant.cuh"
#ifdef PIPELINE_LEVEL_SMEMtemplate <int NUM_INT_PER_THREAD>__device__ __forceinline__ void CopyFromSharedToRegister_AFrag(uint32_t Reg[], uint32_t* SPTR, int slice_id){ SPTR += slice_id * (NUM_INT_PER_THREAD * WARP_SIZE); int lane_id = threadIdx.x % WARP_SIZE;#pragma unroll for (int i = 0; i < NUM_INT_PER_THREAD; i++) { Reg[i] = SPTR[lane_id + i * WARP_SIZE]; }}
template <typename TilingConfig>__device__ __forceinline__ void initialize_mma_slice( uint32_t (*a)[4], uint32_t (*b)[4], uint32_t* __restrict__ A1_SPTR_read, uint32_t* __restrict__ A2_SPTR_read, half (*__restrict__ B_SPTR_read)[WARP_K + PADDING_SHARED_MEM_FOR_B_8], uint32_t* RPTR_Scales){ // Writing registers // Registers to store FP6 fragments for a slice (64*16) of A matrix => 32 FP6 per thread => 6 // register per thread; uint32_t a_1[2]; // NO double buffer uint32_t a_2[4]; // NO double buffer CopyFromSharedToRegister_AFrag<2>(a_1, A1_SPTR_read, 0); CopyFromSharedToRegister_AFrag<4>(a_2, A2_SPTR_read, 0); Dequant_32FP6_4Way(a, a_1, a_2, RPTR_Scales); // SIMT Dequant: dequantizing FP6 to FP16 at // register level, dequantizing a slice each time B_FromSharedToReg<TilingConfig>(b, B_SPTR_read, 0); // Loading B from shared to registers}
template <typename TilingConfig>__device__ __forceinline__ void core_mma_slice( float c[][REG_PER_THREAD_C_TENSOR_16_16], uint32_t (*a)[4], uint32_t (*b)[4], uint32_t* __restrict__ A1_SPTR_read, uint32_t* __restrict__ A2_SPTR_read, half (*__restrict__ B_SPTR_read)[WARP_K + PADDING_SHARED_MEM_FOR_B_8], uint32_t* RPTR_Scales, int slice_id) // writing slice[slice_id] to registers, k=0 -> slice_id=1 for prefetching{#ifdef DEBUG_MODE assert( (TilingConfig::WARP_COL_MMA_TENSORS == 1) || (TilingConfig::WARP_COL_MMA_TENSORS % 2 == 0)); // if WARP_COL_MMA_TENSORS == 1, B tile in registers is padded to a 16*16 MMA block#endif const int NumRegSets_a = WARP_ROW_MMA_TENSORS; // 1 set = 4 registers, containing a 16*16 MMA block const int NumRegSets_b = (TilingConfig::WARP_COL_MMA_TENSORS == 1) ? 1 : TilingConfig::WARP_COL_MMA_TENSORS / 2; // 1 set = 4 registers, containing a 16*16 MMA block uint32_t(*c_uint_ptr)[REG_PER_THREAD_C_TENSOR_16_16] = reinterpret_cast<uint32_t(*)[REG_PER_THREAD_C_TENSOR_16_16]>( c); // Registers for accumulated FP32 results
// Setting RPTRs for double buffers uint32_t(*a_read)[4] = a; uint32_t(*a_write)[4] = a; uint32_t(*b_read)[4] = b; uint32_t(*b_write)[4] = b; if (slice_id % 2 == 1) { b_write += NumRegSets_b; a_write += NumRegSets_a; } else { b_read += NumRegSets_b; a_read += NumRegSets_a; }
// Reading registers and issuing core tensor core computations (a slice of A and B tile in shared// memory)#pragma unroll for (int i = 0; i < WARP_ROW_MMA_TENSORS; i++) { if (TilingConfig::WARP_COL_MMA_TENSORS == 1) { MMA_FP16_M16N8K16(c_uint_ptr[i], a_read[i], b_read[0]); } else {#pragma unroll for (int j = 0; j < TilingConfig::WARP_COL_MMA_TENSORS / 2; j++) { MMA_FP16_M16N8K16(c_uint_ptr[i + j * WARP_ROW_MMA_TENSORS], a_read[i], b_read[j]); MMA_FP16_M16N8K16(c_uint_ptr[i + j * WARP_ROW_MMA_TENSORS] + 4, a_read[i], b_read[j] + 2); // c+4; b+2 } } }
// Writing registers // Registers to store FP6 fragments for a slice (64*16) of A matrix => 32 FP6 per thread => 6 // register per thread; uint32_t a_1[2]; // NO double buffer uint32_t a_2[4]; // NO double buffer CopyFromSharedToRegister_AFrag<2>(a_1, A1_SPTR_read, slice_id); CopyFromSharedToRegister_AFrag<4>(a_2, A2_SPTR_read, slice_id); Dequant_32FP6_4Way( a_write, a_1, a_2, RPTR_Scales); // SIMT Dequant: dequantizing FP6 to FP16 at register // level, dequantizing a slice each time B_FromSharedToReg<TilingConfig>( b_write, B_SPTR_read, slice_id); // Loading B from shared to registers}
#else// Old version with naive pipeline designtemplate <int NUM_INT_PER_THREAD>__device__ __forceinline__ void CopyFromSharedToRegister_AFrag(uint32_t Reg[], uint32_t* SPTR){ int lane_id = threadIdx.x % WARP_SIZE;#pragma unroll for (int i = 0; i < NUM_INT_PER_THREAD; i++) { Reg[i] = SPTR[lane_id + i * WARP_SIZE]; }}template <typename TilingConfig>__device__ __forceinline__ void PipelinedCoreLoop( float c[][REG_PER_THREAD_C_TENSOR_16_16], half __restrict__ (*read_SPTR)[WARP_K + PADDING_SHARED_MEM_FOR_B_8], uint32_t* __restrict__ read_SPTR_Frag1, uint32_t* __restrict__ read_SPTR_Frag2, uint32_t* RPTR_Scales){#ifdef DEBUG_MODE assert( (TilingConfig::WARP_COL_MMA_TENSORS == 1) || (TilingConfig::WARP_COL_MMA_TENSORS % 2 == 0)); // if WARP_COL_MMA_TENSORS == 1, B tile in registers is padded to a 16*16 MMA block#endif const int NumRegSets_a = WARP_ROW_MMA_TENSORS; // 1 set = 4 registers, containing a 16*16 MMA block const int NumRegSets_b = (TilingConfig::WARP_COL_MMA_TENSORS == 1) ? 1 : TilingConfig::WARP_COL_MMA_TENSORS / 2; // 1 set = 4 registers, containing a 16*16 MMA block
// Registers to store FP32 results uint32_t(*c_uint_ptr)[REG_PER_THREAD_C_TENSOR_16_16] = reinterpret_cast<uint32_t(*)[REG_PER_THREAD_C_TENSOR_16_16]>(c); // Registers to store FP6 fragments for a slice (64*16) of A matrix => 32 FP6 per thread => 6 // register per thread; uint32_t a_1[2 * 2]; // double buffer is used uint32_t a_2[4 * 2]; // double buffer is used // Registers to store decompressed FP6 uint32_t a[NumRegSets_a * 1][4]; // No double buffer // Register to store FP16 B matrix (a slice) uint32_t b[NumRegSets_b * 2][4]; // double buffer is used
// Overlapped Smem and TC pipeline: pre-loading from shared to registers CopyFromSharedToRegister_AFrag<2>(a_1, read_SPTR_Frag1); CopyFromSharedToRegister_AFrag<4>(a_2, read_SPTR_Frag2); B_FromSharedToReg<TilingConfig>(b, read_SPTR, 0);
#pragma unroll for (int k = 0; k < WARP_K_MMA_TENSORS; k++) { uint32_t(*b_read)[4] = b; uint32_t(*b_write)[4] = b; uint32_t* a_1_read = a_1; uint32_t* a_1_write = a_1; uint32_t* a_2_read = a_2; uint32_t* a_2_write = a_2; if (k % 2 == 0) { b_write += NumRegSets_b; a_1_write += 2; a_2_write += 4; } else { b_read += NumRegSets_b; a_1_read += 2; a_2_read += 4; } // data loading if (k + 1 < WARP_K_MMA_TENSORS) { // updating SPTR for fragment1 and fragment2 read_SPTR_Frag1 += 2 * WARP_SIZE; read_SPTR_Frag2 += 4 * WARP_SIZE; CopyFromSharedToRegister_AFrag<2>(a_1_write, read_SPTR_Frag1); CopyFromSharedToRegister_AFrag<4>(a_2_write, read_SPTR_Frag2); B_FromSharedToReg<TilingConfig>(b_write, read_SPTR, (k + 1) * MMA_16); } // SIMT Dequant + Tensor Core computations Dequant_32FP6_4Way( a, a_1_read, a_2_read, RPTR_Scales); // Dequantizing FP6 to FP16 at register level, // dequantizing a slice each time#pragma unroll for (int i = 0; i < WARP_ROW_MMA_TENSORS; i++) { if (TilingConfig::WARP_COL_MMA_TENSORS == 1) MMA_FP16_M16N8K16(c_uint_ptr[i], a[i], b_read[0]); else {#pragma unroll for (int j = 0; j < TilingConfig::WARP_COL_MMA_TENSORS / 2; j++) { MMA_FP16_M16N8K16(c_uint_ptr[i + j * WARP_ROW_MMA_TENSORS], a[i], b_read[j]); MMA_FP16_M16N8K16(c_uint_ptr[i + j * WARP_ROW_MMA_TENSORS] + 4, a[i], b_read[j] + 2); // c+4; b+2 } } } }}#endif // #ifdef PIPELINE_LEVEL_SMEM
template <typename TilingConfig>__device__ __forceinline__ void StoreToSharedMemoryFromRegister( float (*smem_CFrag)[TilingConfig::TILE_M + PADDING_SHARED_MEM_FOR_C_4], float c[][REG_PER_THREAD_C_TENSOR_16_16]){ const int lane_id = threadIdx.x % WARP_SIZE; const int warpId = threadIdx.x / WARP_SIZE; int warp_row_offset = warpId * (MMA_16 * WARP_ROW_MMA_TENSORS);#pragma unroll for (int i = 0; i < WARP_ROW_MMA_TENSORS; i++) {#pragma unroll for (int j = 0; j < TilingConfig::WARP_COL_MMA_TENSORS; j++) { // Dealing with one 16*8 Tensor int RegSetID = i + (j / 2) * WARP_ROW_MMA_TENSORS; int RegOffset = (j % 2) * (REG_PER_THREAD_C_TENSOR_16_16 / 2); int Tensor_row_offset = warp_row_offset + i * MMA_16; int Tensor_col_offset = j * MMA_8;#pragma unroll for (int r = 0; r < REG_PER_THREAD_C_TENSOR_16_16 / 2; r++) { int row_offset = lane_id / 4; if (r >= 2) row_offset += 8; int col_offset = (lane_id % 4) * 2; if (r % 2 == 1) col_offset += 1; smem_CFrag[Tensor_col_offset + col_offset][Tensor_row_offset + row_offset] = c[RegSetID][r + RegOffset]; } } }}
#endif1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
// This is a copy of FP6-LLM kernel code: https://arxiv.org/abs/2401.14112
#ifndef DEEPSPEED_CUDA_LINEAR_UTILS_GMEM_CUH#define DEEPSPEED_CUDA_LINEAR_UTILS_GMEM_CUH
#include <assert.h>#include "configs.h"#include "ptx_cp.async.cuh"
/* * Copying A1/A2 from global memory to shared memory. * Usually 1024 or 2048 Bytes */template <int SMEM_SIZE_IN_BYTES_PER_WARP>__device__ __forceinline__ void CopyFromGlobalToShared_A(uint32_t* SPTR, const uint4* GPTR, bool pred_guard = true){#ifdef DEBUG_MODE static_assert(SMEM_SIZE_IN_BYTES_PER_WARP / WARP_SIZE % 16 == 0);#endif int lane_id = threadIdx.x % WARP_SIZE; half* SPTR_HALF = reinterpret_cast<half*>(SPTR); const half* GPTR_HALF = reinterpret_cast<const half*>(GPTR); SPTR_HALF += lane_id * 8; GPTR_HALF += lane_id * 8;#pragma unroll for (int i = 0; i < SMEM_SIZE_IN_BYTES_PER_WARP / WARP_SIZE / 16; i++) { cp_async<16>(SPTR_HALF, GPTR_HALF, pred_guard); SPTR_HALF += 256; // Forward 512 Bytes GPTR_HALF += 256; // Forward 512 Bytes }}
/* * Copying 64 Quant Scales (FP16) from global memory to shared memory. */__device__ __forceinline__ void CopyFromGlobalToShared_Scales(half* SPTR_QuantScales, const half* GPTR_A_Scales){ int lane_id = threadIdx.x % WARP_SIZE; int Offset_Shared = lane_id * 2; int Offset_Global = lane_id / 4 + (lane_id % 4) * 16; for (int i = 0; i < 2; i++) SPTR_QuantScales[Offset_Shared + i] = GPTR_A_Scales[Offset_Global + i * 8];}
/* * (1) Copying X rows * 64 columns of FP16 values, originally in row major * (2) Copying 64 rows * X columns of FP16 values, originally in column major * 16 Bytes per thread -> 512 Bytes per WARP = 4 line per WARP = 1 line per 8 Threads */template <int MaxNumOfLinesToCopy, int BLOCK_WARPS>__device__ __forceinline__ void CopyFromGlobalToShared( half (*__restrict__ SharedPTR)[WARP_K + PADDING_SHARED_MEM_FOR_B_8], const half* GlobalPTR, const int GlobalStride, const int NumOfLinesLeft, // To support arbitrary N dimensions. bool Pred = true){ // static parameters: 1 Group (8 Threads) can copy 1 line (64 FP16) each time const int NumOfThreads = BLOCK_WARPS * WARP_SIZE; const int NumOfGroups = NumOfThreads / 8; const int MaxIteration = (MaxNumOfLinesToCopy - 1) / NumOfGroups + 1; // runtime variables const int line_id = threadIdx.x / 8; const int line_offset = (threadIdx.x % 8) * 8; // PTR for source global memory and target shared memory GlobalPTR += line_id * GlobalStride + line_offset; SharedPTR += line_id;#pragma unroll for (int i = 0; i < MaxIteration; i++) { bool AsyncCopyPred = (line_id + i * NumOfGroups) < NumOfLinesLeft && Pred; cp_async<16>(&(*SharedPTR)[line_offset], GlobalPTR, AsyncCopyPred); // GlobalPTR += NumOfGroups * GlobalStride; SharedPTR += NumOfGroups; }}
#endif123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
// This is a copy of FP6-LLM kernel code: https://arxiv.org/abs/2401.14112
#ifndef DEEPSPEED_CUDA_LINEAR_UTILS_PARALLELDEQUANT_CUH#define DEEPSPEED_CUDA_LINEAR_UTILS_PARALLELDEQUANT_CUH
#include <cuda.h>#include <cuda_fp16.h>#include <cuda_runtime.h>
/* * Input: R1 * Outputs: R1, R2 * Note: Simplified Exponent calculation is applied. */__device__ __forceinline__ void FP6_FP16_Cast_4Way(uint32_t* R1, uint32_t* R2){ *R2 = *R1 & 0x80808080; *R1 = *R1 >> 2; *R1 = *R1 & 0x1f1f1f1f; *R2 = *R2 | *R1; *R1 = *R2 & 0x9f009f00; *R2 = *R2 & 0x009f009f; *R2 = *R2 << 8;}
/* * Input: R1 * Outputs: R1, R2 * Note: Simplified Exponent calculation is NOT applied. */__device__ __forceinline__ void FP6_FP16_Cast_4Way_Naive(uint32_t* R1, uint32_t* R2){ //*R2 = *R1 & 0x80808080; *R2 = *R1 & 0xc0c0c0c0; *R1 = *R1 >> 2; //*R1 = *R1 & 0x1f1f1f1f; *R1 = *R1 & 0x0f0f0f0f; *R2 = *R2 | *R1; // //*R1 = *R2 & 0x9f009f00; //*R2 = *R2 & 0x009f009f; *R1 = *R2 & 0xcf00cf00; if (!(*R1 & 0x40000000) && (*R1 & 0x0c000000)) *R1 = *R1 | 0x30000000; if (!(*R1 & 0x00004000) && (*R1 & 0x00000c00)) *R1 = *R1 | 0x00003000; *R2 = *R2 & 0x00cf00cf; if (!(*R2 & 0x00400000) && (*R2 & 0x000c0000)) *R2 = *R2 | 0x00300000; if (!(*R2 & 0x00000040) && (*R2 & 0x0000000c)) *R2 = *R2 | 0x00000030; // *R2 = *R2 << 8; //*R1 = 0x3c003c00; //*R2 = 0x3c003c00;}
__device__ __forceinline__ uint32_t MultScale(uint32_t PackedFP16Pair, half Scale){ half* FP16_1 = reinterpret_cast<half*>(&PackedFP16Pair); half* FP16_2 = FP16_1 + 1; uint32_t output; half* output_half_ptr = reinterpret_cast<half*>(&output); output_half_ptr[0] = __hmul(__hmul(*FP16_1, __float2half(4096.0f)), Scale); output_half_ptr[1] = __hmul(__hmul(*FP16_2, __float2half(4096.0f)), Scale); return output;}
__device__ __forceinline__ void Dequant_32FP6_4Way(uint32_t (*__restrict__ Reg)[4], uint32_t* __restrict__ read_RPTR_Frag1, uint32_t* __restrict__ read_RPTR_Frag2, uint32_t* Scales){ uint32_t* OutputRegs = reinterpret_cast<uint32_t*>(Reg); uint32_t* Frag1_PTR = read_RPTR_Frag1; uint32_t* Frag2_PTR = read_RPTR_Frag2; half* Scale_RPTR = reinterpret_cast<half*>(Scales); uint32_t Packed_FP6 = 0; uint32_t tmp = 0;// Dequantizing 32 FP6, each Loop dequantizing 4 FP6#pragma unroll(8) for (int i = 0; i < 8; i++) { // Frag1 Packed_FP6 = (*Frag1_PTR) & 0xc0c0c0c0; if (i % 4 == 3) Frag1_PTR++; else (*Frag1_PTR) = (*Frag1_PTR) << 2; // Frag2 tmp = (*Frag2_PTR) & 0xf0f0f0f0; tmp = tmp >> 2; if (i % 2 == 1) Frag2_PTR++; else (*Frag2_PTR) = (*Frag2_PTR) << 4; // Packed_FP6 Packed_FP6 = Packed_FP6 | tmp; // FP6_FP16_Cast_4Way(&Packed_FP6, &tmp); // *OutputRegs = MultScale(Packed_FP6, Scale_RPTR[0]); // Muliply FP16 scales OutputRegs += 1; *OutputRegs = MultScale(tmp, Scale_RPTR[1]); // Muliply FP16 scales OutputRegs += 1; // Updating offset for FP16 scales for every two iterations if (i % 2 == 1) Scale_RPTR += 2; }}
/* * */__device__ __forceinline__ void ExtractFromSharedToReg_Scales(uint32_t* Scales, half* WARP_SPTR_Scales){ int lane_id = threadIdx.x % WARP_SIZE; uint32_t* SPTR_uint = reinterpret_cast<uint32_t*>(WARP_SPTR_Scales); uint32_t tmpReg = SPTR_uint[lane_id];#pragma unroll for (int i = 0; i < 4; i++) { // T __shfl_sync(unsigned mask, T var, int srcLane, int width=warpSize); Scales[i] = __shfl_sync(0xffffffff, tmpReg, i, 4); }}
#endif123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
// This is a copy of FP6-LLM kernel code: https://arxiv.org/abs/2401.14112
#ifndef DEEPSPEED_CUDA_LINEAR_WEIGHT_PREPACKING_H#define DEEPSPEED_CUDA_LINEAR_WEIGHT_PREPACKING_H
#include <assert.h>#include <stdio.h>#include <vector>
using namespace std;
void Padding_8_FP6_To_8_Bytes(unsigned char Padded_FP6[], unsigned char* FP6_Array) // padding 0 to the lowerest bit location{ Padded_FP6[0] = FP6_Array[0] & 0xfc; Padded_FP6[1] = (FP6_Array[0] << 6) | ((FP6_Array[1] >> 2) & 0xfc); Padded_FP6[2] = (FP6_Array[1] << 4) | ((FP6_Array[2] >> 4) & 0xfc); Padded_FP6[3] = FP6_Array[2] << 2; Padded_FP6[4] = FP6_Array[3] & 0xfc; Padded_FP6[5] = (FP6_Array[3] << 6) | ((FP6_Array[4] >> 2) & 0xfc); Padded_FP6[6] = (FP6_Array[4] << 4) | ((FP6_Array[5] >> 4) & 0xfc); Padded_FP6[7] = FP6_Array[5] << 2;}
unsigned char Extract_2_Bits_From_4_PaddedFP6(unsigned char B1, unsigned char B2, unsigned char B3, unsigned char B4){ unsigned char out; out = (B1 & 0xc0) | ((B2 & 0xc0) >> 2) | ((B3 & 0xc0) >> 4) | ((B4 & 0xc0) >> 6); return out;}
unsigned char Extract_4_Bits_From_2_PaddedFP6( unsigned char B1, unsigned char B2) // The highest two bits are already extracted by Extract_2_Bits_From_4_PaddedFP6();{ unsigned char out; out = ((B1 << 2) & 0xf0) | ((B2 >> 2) & 0x0f); return out;}
// dealing with 4 1*8 blocks of FP6void Assign_32_FP6_To_4_Thread(vector<unsigned char> Seg_2bit[], vector<unsigned char> Seg_4bit[], unsigned char* PTR_1, unsigned char* PTR_2, unsigned char* PTR_3, unsigned char* PTR_4){ unsigned char Padded_8_FP8[4][8]; Padding_8_FP6_To_8_Bytes(Padded_8_FP8[0], PTR_1); Padding_8_FP6_To_8_Bytes(Padded_8_FP8[1], PTR_2); Padding_8_FP6_To_8_Bytes(Padded_8_FP8[2], PTR_3); Padding_8_FP6_To_8_Bytes(Padded_8_FP8[3], PTR_4); // unsigned char Seg1_Byte1_T[4]; unsigned char Seg1_Byte2_T[4]; unsigned char Seg2_Byte1_T[4]; unsigned char Seg2_Byte2_T[4]; unsigned char Seg2_Byte3_T[4]; unsigned char Seg2_Byte4_T[4]; for (int t = 0; t < 4; t++) { Seg1_Byte1_T[t] = Extract_2_Bits_From_4_PaddedFP6(Padded_8_FP8[0][0 + t * 2], Padded_8_FP8[0][1 + t * 2], Padded_8_FP8[1][0 + t * 2], Padded_8_FP8[1][1 + t * 2]); Seg1_Byte2_T[t] = Extract_2_Bits_From_4_PaddedFP6(Padded_8_FP8[2][0 + t * 2], Padded_8_FP8[2][1 + t * 2], Padded_8_FP8[3][0 + t * 2], Padded_8_FP8[3][1 + t * 2]); Seg2_Byte1_T[t] = Extract_4_Bits_From_2_PaddedFP6(Padded_8_FP8[0][0 + t * 2], Padded_8_FP8[0][1 + t * 2]); Seg2_Byte2_T[t] = Extract_4_Bits_From_2_PaddedFP6(Padded_8_FP8[1][0 + t * 2], Padded_8_FP8[1][1 + t * 2]); Seg2_Byte3_T[t] = Extract_4_Bits_From_2_PaddedFP6(Padded_8_FP8[2][0 + t * 2], Padded_8_FP8[2][1 + t * 2]); Seg2_Byte4_T[t] = Extract_4_Bits_From_2_PaddedFP6(Padded_8_FP8[3][0 + t * 2], Padded_8_FP8[3][1 + t * 2]); } // for (int t = 0; t < 4; t++) { Seg_2bit[t].push_back(Seg1_Byte1_T[t]); Seg_2bit[t].push_back(Seg1_Byte2_T[t]); Seg_4bit[t].push_back(Seg2_Byte1_T[t]); Seg_4bit[t].push_back(Seg2_Byte2_T[t]); Seg_4bit[t].push_back(Seg2_Byte3_T[t]); Seg_4bit[t].push_back(Seg2_Byte4_T[t]); } return;}
void BitInterleaving_2bit(unsigned char* PTR_4Bytes){ unsigned int* PTR_UINT = reinterpret_cast<unsigned int*>(PTR_4Bytes); unsigned int input = *PTR_UINT; // // int order_2bit[16] = {1,5,9,13,3,7,11,15,2,6,10,14,4,8,12,16}; // pre-defined order for // bit-interleaving in QuantLLM int order_2bit[16] = { 2, 6, 10, 14, 4, 8, 12, 16, 1, 5, 9, 13, 3, 7, 11, 15}; // pre-defined order for // bit-interleaving in QuantLLM unsigned int Frags_2bit[16]; // The highest 2 bits are used to store the extracted fragments. for (int i = 0; i < 16; i++) Frags_2bit[i] = (input << 2 * (order_2bit[i] - 1)) & 0xc0000000; // unsigned int output = 0x00000000; for (int i = 0; i < 16; i++) output |= (Frags_2bit[i] >> (i * 2)); // *PTR_UINT = output;}
void BitInterleaving_4bit(unsigned char* PTR_4Bytes){ unsigned int* PTR_UINT = reinterpret_cast<unsigned int*>(PTR_4Bytes); unsigned int input = *PTR_UINT; // // int order_4bit[8] = {1,5,3,7,2,6,4,8}; // pre-defined order for bit-interleaving in QuantLLM int order_4bit[8] = { 2, 6, 4, 8, 1, 5, 3, 7}; // pre-defined order for bit-interleaving in QuantLLM unsigned int Frags_4bit[8]; // The highest4 bits are used to store the extracted fragments. for (int i = 0; i < 8; i++) Frags_4bit[i] = (input << 4 * (order_4bit[i] - 1)) & 0xf0000000; // unsigned int output = 0x00000000; for (int i = 0; i < 8; i++) output |= (Frags_4bit[i] >> (i * 4)); // *PTR_UINT = output;}
/* * Inputs: * (1) unsigned char Weight_6bit [M*K*6/8] * Outputs: * (1) unsigned char Weight_2bit [M*K*2/8] * (2) unsigned char Weight_4bit [M*K*4/8] * * Assumption: Weight_6bit, Weight_2bit, Weight_4bit all stored continuously in row-major. * 8 FP6 = 6 Bytes * 8 FP4 = 4 Bytes * 8 FP2 = 2 Bytes */void weight_matrix_prepacking(int* FP6Weights, size_t M, size_t K){ assert(M % 64 == 0); assert(K % 64 == 0); // unsigned char* Weight_6bit = reinterpret_cast<unsigned char*>(FP6Weights); unsigned char* Weight_2bit = Weight_6bit; unsigned char* Weight_4bit = Weight_6bit + M * K * 2 / 8; // vector<unsigned char> A_Segment_2bit[32]; vector<unsigned char> A_Segment_4bit[32]; // size_t BytesPerRow = K * 6 / 8; // Pass-1: (1) 2+4 split; (2) assign weights to 32 threads. for (size_t i = 0; i < M / 64; i++) // { for (size_t j = 0; j < K / 16; j++) { for (size_t k = 0; k < 64 / 16; k++) { size_t row = i * 64 + k * 16; size_t col = j * 16; unsigned char* StartPTR_1 = Weight_6bit + row * BytesPerRow + col * 6 / 8; unsigned char* StartPTR_2 = StartPTR_1 + 8 * BytesPerRow; unsigned char* StartPTR_3 = StartPTR_1 + 8 * 6 / 8; unsigned char* StartPTR_4 = StartPTR_2 + 8 * 6 / 8; // Dealing with each 16*16 blocks then... for (int l = 0; l < 8; l++) Assign_32_FP6_To_4_Thread(&A_Segment_2bit[l * 4], &A_Segment_4bit[l * 4], StartPTR_1 + l * BytesPerRow, StartPTR_2 + l * BytesPerRow, StartPTR_3 + l * BytesPerRow, StartPTR_4 + l * BytesPerRow); } } } // Verifying the length of 2_bit segments and 4_bit segments size_t BytesPerThread_2bit = M * K * 2 / 8 / 32; size_t BytesPerThread_4bit = M * K * 4 / 8 / 32; for (int i = 0; i < 32; i++) { assert(A_Segment_2bit[i].size() == BytesPerThread_2bit); assert(A_Segment_4bit[i].size() == BytesPerThread_4bit); } // Pass-2: Optimizing coleasced global memory access for (size_t i = 0; i < BytesPerThread_2bit / 4; i++) for (int t = 0; t < 32; t++) for (int b = 0; b < 4; b++) Weight_2bit[i * 128 + t * 4 + (3 - b)] = A_Segment_2bit[t] [i * 4 + b]; // why (3-b): special byte order within a register for (size_t i = 0; i < BytesPerThread_4bit / 4; i++) for (int t = 0; t < 32; t++) for (int b = 0; b < 4; b++) Weight_4bit[i * 128 + t * 4 + (3 - b)] = A_Segment_4bit[t][i * 4 + b]; // why (3-b):special byte order within a register // Pass-3: Bit-level interleaving for (size_t i = 0; i < BytesPerThread_2bit * 32 / 4; i++) BitInterleaving_2bit(Weight_2bit + 4 * i); for (size_t i = 0; i < BytesPerThread_4bit * 32 / 4; i++) BitInterleaving_4bit(Weight_4bit + 4 * i);}
#endif123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
#include <ATen/cuda/CUDAContext.h>
#include "linear_kernels.h"
namespace {
// For bit-level debugging.template <typename T>void print_bits(T num){ char bits[sizeof(T) * 8 + 1] = {'\0'}; for (int bit = 0; bit < (sizeof(T) * 8); bit++) { bits[sizeof(T) * 8 - 1 - bit] = '0' + (num & 0x01); num = num >> 1; } printf("%s\n", bits);}
void print_bits(half num){ char bits[sizeof(half) * 8 + 1] = {'\0'}; auto int_num = *reinterpret_cast<uint16_t*>(&num); for (int bit = 0; bit < (sizeof(half) * 8); bit++) { bits[sizeof(half) * 8 - 1 - bit] = '0' + (int_num & 0x01); int_num = int_num >> 1; } printf("%s\n", bits);}
/* * Function to pack 4 fake quantized FP16 value into continuously stored 4 FP6 values. */void cast_fp16_fp6(uint16_t* FP16x4, uint8_t* FP6x4){ // Constants for FP6 constexpr int exponent_nbits_fp6 = 3; constexpr int mantissa_nbits_fp6 = 2; constexpr int exp_bias_fp6 = (1 << (exponent_nbits_fp6 - 1)) - 1; // Constants for FP16 constexpr int exponent_nbits_fp16 = 5; constexpr int mantissa_nbits_fp16 = 10; constexpr int exp_bias_fp16 = (1 << (exponent_nbits_fp16 - 1)) - 1;
int fp6_temp[4];
float absmin_nonzero_fp6 = 0.0625; // Note that we regard the exponent of '111' as a regular value rather than NaN or inf. This is // the same with that in qtorch. float absmax_fp6 = 28;
for (int i = 0; i < 4; ++i) { uint16_t source = FP16x4[i]; float fp6_value_abs = std::abs(__half2float(*((half*)(&source)))); if ((fp6_value_abs != 0 && fp6_value_abs < absmin_nonzero_fp6) || fp6_value_abs > absmax_fp6) { // TODO(zhen): a better way may be rounding it to the nearest FP6 value. throw std::invalid_argument("Input value out of range for FP6."); }
// It is not safe to do shift operation on uint16_t. So we promote it to int. int source_promote = int(source);
int sign_bit = (source_promote >> 15); // Extracting exponent represented in FP16. The sign mask 0x7FFF is '0111 1111 1111 1111' int exp_bit = (source_promote & 0x7FFF) >> mantissa_nbits_fp16; // Extracting mantissa represented in FP16 int mant_bit = source_promote & ((1 << mantissa_nbits_fp16) - 1);
int new_exp_bit; int new_mant_bit;
if (exp_bit == 0) { // Subnormal FP16 number. Too small for FP6. new_exp_bit = 0; new_mant_bit = 0; } else { new_mant_bit = mant_bit >> (mantissa_nbits_fp16 - mantissa_nbits_fp6); new_exp_bit = exp_bit - exp_bias_fp16 + exp_bias_fp6;
// Deal with subnormal FP6 values. int target_exp_val = exp_bit - exp_bias_fp16; int min_fp6_exp_val = -exp_bias_fp6 + 1; bool subnormal_fp6 = target_exp_val < min_fp6_exp_val; if (subnormal_fp6) { // TODO(zhen): add the rounding logic. new_exp_bit = 0; // The implicit 1 in the mantissa of FP16 is not present in subnormal FP6. Thus we // need to add it new_mant_bit = (new_mant_bit | (1 << mantissa_nbits_fp6)) >> (min_fp6_exp_val - target_exp_val); } }
fp6_temp[i] = (sign_bit << (exponent_nbits_fp6 + mantissa_nbits_fp6)) | (new_exp_bit << mantissa_nbits_fp6) | new_mant_bit; } // Pack the values FP6x4[0] = fp6_temp[0] << 2 | (fp6_temp[1] >> 4); FP6x4[1] = (fp6_temp[1] & 0x0F) << 4 | (fp6_temp[2] >> 2); FP6x4[2] = (fp6_temp[2] & 0x03) << 6 | fp6_temp[3];}
/* * Function to prepack FP16 weights into continuous FP6 values. * * Parameters: * weight_16bit: input weight in FP16, size M*K * weight_6bit: output weight in packed FP6, continuously stored, size M*K*6/8 * M, K: the shape of the weight */void weight_prepacking_fp16_to_fp6(uint16_t* weight_16bit, uint8_t* weight_6bit_packed, size_t M, size_t K){ // Every four 16-bit elements are packed into three 6-bit values (4*6bit == 3*8bit). if (K * 6 % 8 != 0) { throw std::invalid_argument("(K * 6 % 8) should be 0"); } size_t K_fp6_packed = K * 6 / 8; // #pragma omp parallel for for (auto m = 0; m < M; m++) { uint8_t* ptr_6bit = weight_6bit_packed + m * K_fp6_packed; uint16_t* ptr_16bit = weight_16bit + m * K; for (auto k = 0; k < K; k += 4) { cast_fp16_fp6(ptr_16bit, ptr_6bit); ptr_16bit += 4; ptr_6bit += 3; } }}
} // namespace
/* * Function to execute the FP6 linear kernel. * * Parameters: * output: output tensor, size M*N * hidden_states: input activation tensor, size N*K * weights_2bit: packed 2bit weights, size M*K*2/8 * weights_4bit: packed 4bit weights, size M*K*4/8 * scales: scale tensor, size M * workspace: workspace tensor, size M*N*split_k * M: the output channel number of the weight * N: the token number of the activation * K: the input channel number of the weight * split_k: the split size of the GEMM calculation */void cuda_wf6af16_linear(torch::Tensor& output, torch::Tensor& hidden_states, torch::Tensor& weights_2bit, torch::Tensor& weights_4bit, torch::Tensor& scales, torch::Tensor& workspace, int M, int N, int K, int split_k){ TORCH_CHECK(weights_2bit.device().type() == torch::kCUDA, "weight_2bit must be on CUDA"); TORCH_CHECK(weights_4bit.device().type() == torch::kCUDA, "weight_4bit must be on CUDA"); TORCH_CHECK(hidden_states.device().type() == torch::kCUDA, "X must be on CUDA"); TORCH_CHECK(scales.device().type() == torch::kCUDA, "scales must be on CUDA");
auto status = fp6_linear_kernel(at::cuda::getCurrentCUDAStream(), (uint4*)(weights_2bit.data_ptr<uint8_t>()), (uint4*)(weights_4bit.data_ptr<uint8_t>()), (half*)(scales.data_ptr<at::Half>()), (half*)(hidden_states.data_ptr<at::Half>()), (half*)(output.data_ptr<at::Half>()), M, N, K, workspace.data_ptr<float>(), split_k); if (status != cudaSuccess) { AT_ERROR("fp6_linear_kernel failed with error: ", cudaGetErrorString(status)); }}
/* * Function to prepack the fake 6-bit-quantized FP16 weights into 2bit and 4bit. * * Parameters: * weight: input weight in FP16 (containing the quantized FP6-ranged value), size M*K * Returns: * weight_2bit: output weight in 2bit, size M*K*2/8 * weight_4bit: output weight in 4bit, size M*K*4/8 */std::vector<torch::Tensor> preprocess_weight(torch::Tensor& weight){ TORCH_CHECK(weight.dim() == 2, "weight must be 2-dimensional"); TORCH_CHECK(weight.scalar_type() == torch::kFloat16, "weight must be FP16"); TORCH_CHECK(weight.is_contiguous(), "weight must be contiguous"); TORCH_CHECK(weight.device().type() == torch::kCPU, "weight must be on CPU"); auto M = weight.size(0); auto K = weight.size(1); TORCH_CHECK(K % 4 == 0, "K must be multiple of 4");
// Pack weight from FP16 to FP6. uint16_t* weight_16bit_ptr = reinterpret_cast<uint16_t*>(weight.data_ptr<at::Half>()); std::vector<uint8_t> weight_6bit_packed(M * K * 6 / 8); uint8_t* weight_6bit_ptr = weight_6bit_packed.data(); weight_prepacking_fp16_to_fp6(weight_16bit_ptr, weight_6bit_ptr, M, K);
// Split weight into 2bit and 4bit. weight_matrix_prepacking(reinterpret_cast<int*>(weight_6bit_ptr), M, K); uint8_t* weight_2bit_ptr = weight_6bit_ptr;
// Make sure that the new split tensor does not share the underlying memory with the original // one. Otherwise it will incur some problems when the original tensor is deleted. It also // makes the memory flattern risky. auto weight_2bit = torch::from_blob(weight_2bit_ptr, {M * K * 2 / 8}, torch::kUInt8).clone().detach(); uint8_t* weight_4bit_ptr = weight_2bit_ptr + M * K * 2 / 8; auto weight_4bit = torch::from_blob(weight_4bit_ptr, {M * K * 4 / 8}, torch::kUInt8).clone().detach();
return {weight_2bit, weight_4bit};}12345678910111213141516171819202122232425262728// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
#ifndef DEEPSPEED_CUDA_LINEAR_KERNELS_H#define DEEPSPEED_CUDA_LINEAR_KERNELS_H
#include <c10/cuda/CUDAStream.h>#include <torch/extension.h>#include "ds_kernel_utils.h"
#include "linear_kernels_cuda.h"
void cuda_wf6af16_linear(torch::Tensor& output, torch::Tensor& hidden_states, torch::Tensor& weights_2bit, torch::Tensor& weights_4bit, torch::Tensor& scale, torch::Tensor& workspace, int M, int N, int K, int split_k);
std::vector<torch::Tensor> preprocess_weight(torch::Tensor& Weight);
#endif123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
// This is a copy of FP6-LLM kernel code: https://arxiv.org/abs/2401.14112
// clang-format off// Put the torch headers at the front to avoid conflict with other headers on// `at::nullopt` and `at::optional`.#include <torch/extension.h>#include <ATen/ATen.h>// clang-format on
#include "include/kernel_matmul.cuh"#include "include/kernel_reduction.cuh"#include "include/weight_prepacking.h"
#include <assert.h>#include <stdio.h>
#include "linear_kernels_cuda.h"
template <typename TilingConfig, typename OutputDataType>static void Kernel_Ex(cudaStream_t stream, const uint4* Weight1, const uint4* Weight2, const half* Scales, const half* B, OutputDataType* C, const size_t M_Global, const size_t N_Global, const size_t K_Global, int Split_K){#ifdef DEBUG_MODE printf("\n"); printf("Launcher.cu->Kernel_Ex():\n"); printf("M: %d, N: %d, K: %d, SplitK: %d\n", M_Global, N_Global, K_Global, Split_K); printf("TILE_M: %d, TILE_K: %d, TILE_N: %d\n", TilingConfig::TILE_M, TilingConfig::TILE_K, TilingConfig::TILE_N);#endif static size_t SHMEM_SZ = max(TilingConfig::SMEM_SIZE_B_TILE + SMEM_SIZE_A1_TILE + SMEM_SIZE_A2_TILE, TilingConfig::SMEM_SIZE_C_TILE); cudaFuncSetAttribute(QUANT_GEMM_Kernel<TilingConfig, OutputDataType>, cudaFuncAttributeMaxDynamicSharedMemorySize, SHMEM_SZ); size_t dimN = (N_Global - 1) / TilingConfig::TILE_N + 1; size_t dimM = M_Global * Split_K / TilingConfig::TILE_M; dim3 GridDim(dimN, dimM, 1); dim3 BlockDim(WARP_SIZE * TilingConfig::BLOCK_WARPS, 1, 1);
#ifdef DEBUG_MODE printf( "GridDim.x: %d, GridDim.y: %d, GridDim.z: %d, BlockDim.x: %d, BlockDim.y: %d, BlockDim.z: " "%d SHMEM_SZ: %d\n", GridDim.x, GridDim.y, GridDim.z, BlockDim.x, BlockDim.y, BlockDim.z, SHMEM_SZ); printf("\n");#endif
QUANT_GEMM_Kernel<TilingConfig, OutputDataType><<<GridDim, BlockDim, SHMEM_SZ, stream>>>( Weight1, Weight2, Scales, B, C, M_Global, N_Global, K_Global, Split_K);}
/* * */cudaError_t fp6_linear_kernel(cudaStream_t stream, const uint4* Weight1, const uint4* Weight2, const half* Scales, const half* B, half* C, const size_t M_Global, const size_t N_Global, const size_t K_Global, float* Reduction_Workspace, // Reduction_Workspace_Size = Split_K * // M_Global * N_Global * sizeof(fp32) int Split_K){ assert(M_Global % 256 == 0); assert(K_Global % 64 == 0); assert(N_Global > 0);
// Work around to support more N shapes: size_t N_PowerOf2; if (N_Global > 0 && N_Global <= 8) N_PowerOf2 = 8; if (N_Global > 8 && N_Global <= 16) N_PowerOf2 = 16; if (N_Global > 16 && N_Global <= 32) N_PowerOf2 = 32; if (N_Global > 32 && N_Global <= 64) N_PowerOf2 = 64; if (N_Global > 64 && N_Global <= 128) N_PowerOf2 = 128; if (N_Global > 128) N_PowerOf2 = ((N_Global - 1) / 128 + 1) * 128;
if (Split_K == 1) { switch (N_PowerOf2) { case 8: Kernel_Ex<TilingConfig<4, 1, 1>, half>( stream, Weight1, Weight2, Scales, B, C, M_Global, N_Global, K_Global, Split_K); break; case 16: Kernel_Ex<TilingConfig<4, 1, 2>, half>( stream, Weight1, Weight2, Scales, B, C, M_Global, N_Global, K_Global, Split_K); break; case 32: Kernel_Ex<TilingConfig<4, 1, 4>, half>( stream, Weight1, Weight2, Scales, B, C, M_Global, N_Global, K_Global, Split_K); break; case 64: Kernel_Ex<TilingConfig<4, 1, 8>, half>( stream, Weight1, Weight2, Scales, B, C, M_Global, N_Global, K_Global, Split_K); break; case 128: Kernel_Ex<TilingConfig<4, 1, 8>, half>( stream, Weight1, Weight2, Scales, B, C, M_Global, N_Global, K_Global, Split_K); break; default: if (N_PowerOf2 % 128 != 0) { printf("QuantLLM_API Error: Unsupported N dimension %lu!\n", N_PowerOf2); return cudaErrorUnknown; } Kernel_Ex<TilingConfig<4, 1, 8>, half>( stream, Weight1, Weight2, Scales, B, C, M_Global, N_Global, K_Global, Split_K); break; } } else { switch (N_PowerOf2) { case 8: Kernel_Ex<TilingConfig<4, 1, 1>, float>(stream, Weight1, Weight2, Scales, B, Reduction_Workspace, M_Global, N_Global, K_Global, Split_K); break; case 16: Kernel_Ex<TilingConfig<4, 1, 2>, float>(stream, Weight1, Weight2, Scales, B, Reduction_Workspace, M_Global, N_Global, K_Global, Split_K); break; case 32: Kernel_Ex<TilingConfig<4, 1, 4>, float>(stream, Weight1, Weight2, Scales, B, Reduction_Workspace, M_Global, N_Global, K_Global, Split_K); break; case 64: Kernel_Ex<TilingConfig<4, 1, 8>, float>(stream, Weight1, Weight2, Scales, B, Reduction_Workspace, M_Global, N_Global, K_Global, Split_K); break; case 128: Kernel_Ex<TilingConfig<4, 1, 8>, float>(stream, Weight1, Weight2, Scales, B, Reduction_Workspace, M_Global, N_Global, K_Global, Split_K); break; default: if (N_PowerOf2 % 128 != 0) { printf("QuantLLM_API Error: Unsupported N dimension %lu!\n", N_PowerOf2); return cudaErrorUnknown; } Kernel_Ex<TilingConfig<4, 1, 8>, float>(stream, Weight1, Weight2, Scales, B, Reduction_Workspace, M_Global, N_Global, K_Global, Split_K); break; } // Reduction for SplitK dim3 GridDim((M_Global * N_Global) / REDUCTION_ELEMENT_PER_THREADBLOCK, 1, 1); dim3 BlockDim(WARP_SIZE, 1, 1); SplitK_Reduction<<<GridDim, BlockDim, 0, stream>>>( C, Reduction_Workspace, M_Global, N_Global, Split_K); } return cudaGetLastError();}
/*Computes FP6-FP16 GEMM (PyTorch interface).
[Mathematical Formula]Standard definition of linear layer: Out = In * trans(W), where In, Out, and W are stored inrow-major. After Equivalent transformation : trans(Out) = W * trans(In). Note that we do notperform "transpose" during runtime, we instead interpret the In/Out as column-major matrices whencalling our CUDA kernel.
[Inputs] _in_feats: tensor of shape [B, IC]; // half _weights: int tensor of shape [OC, IC // 16 * 3]; // 3 INT32 words contains 16 FP6 weights. _scales: tensor of shape [OC]; // half splitK: splitting the MatMul problem along K dimension for higher GPU utilization, default 1.[Outputs] _out_feats: tensor of shape [B, OC]; // half*/torch::Tensor fp6_linear_forward_cuda(torch::Tensor _in_feats, torch::Tensor _weights, torch::Tensor _scales, int splitK){ int num_in_feats = _in_feats.size(0); int num_in_channels = _in_feats.size(1); int num_out_channels = _weights.size(0); assert(num_in_channels % 64 == 0); assert((num_in_channels / 16 * 3) == _weights.size(1)); // Making sure the K dimension is matched. // int M = num_out_channels; int K = num_in_channels; int N = num_in_feats; // Input Tensors auto weight1 = reinterpret_cast<const uint4*>( _weights.data_ptr<int>()); // weights is [OC, IC] but in FP6. auto weight2 = weight1 + num_in_channels * num_out_channels * 2 / 128; auto in_feats = reinterpret_cast<const half*>(_in_feats.data_ptr<at::Half>()); auto scales = reinterpret_cast<const half*>(_scales.data_ptr<at::Half>()); // Output Tensors auto options = torch::TensorOptions().dtype(_in_feats.dtype()).device(_in_feats.device()); at::Tensor _out_feats = torch::empty({num_in_feats, num_out_channels}, options); auto out_feats = reinterpret_cast<half*>(_out_feats.data_ptr<at::Half>());
float* Reduction_Workspace = nullptr; if (splitK != 1) { auto options = torch::TensorOptions().dtype(torch::kFloat32).device(_in_feats.device()); at::Tensor _workspace = torch::empty({splitK, num_in_feats, num_out_channels}, options); auto Reduction_Workspace = reinterpret_cast<float*>( _out_feats.data_ptr<float>()); // Reduction_Workspace_Size = Split_K * M_Global * // N_Global * sizeof(fp32) }
fp6_linear_kernel(0, // Using default stream here. weight1, weight2, scales, in_feats, out_feats, M, N, K, Reduction_Workspace, splitK);
return _out_feats;}
/* * Inputs: * (1) unsigned char Weight_6bit [M*K*6/8] * Outputs: * (1) unsigned char Weight_2bit [M*K*2/8] * (2) unsigned char Weight_4bit [M*K*4/8] * * Assumption: Weight_6bit, Weight_2bit, Weight_4bit all stored continuously in row-major. * 8 FP6 = 6 Bytes * 8 FP4 = 4 Bytes * 8 FP2 = 2 Bytes */
/* * Weight prepacking (Pytorch interface). * [Input & Output] * fp6_tensor: int tensor of shape [OC, IC // 16 * 3]; // 3 INT32 words contains 16 FP6 weights. * [Output] * packed_tensor: int tensor of shape [OC, IC // 16 * 3]; */torch::Tensor weight_matrix_prepacking_cpu(torch::Tensor fp6_tensor, size_t OC, size_t IC){ assert((OC % 256 == 0) && (IC % 64 == 0)); assert((fp6_tensor.size(0) == OC) && (fp6_tensor.size(1) == IC / 16 * 3)); // auto packed_tensor = torch::empty_like(fp6_tensor); // auto packed_tensor_ptr = reinterpret_cast<int*>(packed_tensor.data_ptr<int>()); auto fp6_tensor_ptr = reinterpret_cast<int*>(fp6_tensor.data_ptr<int>()); weight_matrix_prepacking(fp6_tensor_ptr, OC, IC); return fp6_tensor;}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051// Copyright (c) Microsoft Corporation.// SPDX-License-Identifier: Apache-2.0
// DeepSpeed Team
// This is a copy of FP6-LLM kernel code: https://arxiv.org/abs/2401.14112
#ifndef DEEPSPEED_CUDA_LINEAR_FP6_LINEAR_CUH#define DEEPSPEED_CUDA_LINEAR_FP6_LINEAR_CUH
#include <cuda.h>#include <cuda_fp16.h>#include <cuda_runtime.h>
#include <torch/extension.h>
/* * Computes FP6-FP16 GEMM (C++ interface). */cudaError_t fp6_linear_kernel(cudaStream_t stream, const uint4* Weight1, const uint4* Weight2, const half* Scales, const half* B, half* C, const size_t M_Global, const size_t N_Global, const size_t K_Global, float* Reduction_Workspace, // Reduction_Workspace_Size = Split_K * // M_Global * N_Global * sizeof(fp32) int Split_K);
/* * Computes FP6-FP16 GEMM (PyTorch interface). */torch::Tensor fp6_linear_forward_cuda(torch::Tensor _in_feats, torch::Tensor _weights, torch::Tensor _scales, int splitK = 1);
/* * In-place weight prepacking (C++ interface). */void weight_matrix_prepacking(int* FP6Weights, size_t M, size_t K);
/* * Weight prepacking (Pytorch interface). */torch::Tensor weight_matrix_prepacking_cpu(torch::Tensor fp6_tensor, size_t M, size_t K);
#endiflib/python3.12/site-packages/deepspeed/inference/v2/__pycache__/engine_factory.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/__init__.pyAdded+6
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/__pycache__/ds_kernel.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/__init__.pyAdded+11
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/bias_activations/__init__.pyAdded+6
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/bias_activations/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/bias_activations/__pycache__/bias_activation.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/bias_activations/bias_activation.cppAdded+68
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/bias_activations/bias_activation.hAdded+22
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/bias_activations/bias_activation.pyAdded+62
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/bias_activations/bias_activation_cuda.cuAdded+140
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/blas_kernels/__init__.pyAdded+6
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/blas_kernels/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/blas_kernels/__pycache__/blas_linear.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/blas_kernels/blas.hAdded+138
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/blas_kernels/blas_linear.pyAdded+55
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/blas_kernels/blas_utils.hAdded+299
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/core_ops.cppAdded+42
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/__init__.pyAdded+8
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/__pycache__/cuda_fp_ln_base.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/__pycache__/cuda_ln.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/__pycache__/cuda_post_ln.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/__pycache__/cuda_pre_ln.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/cuda_fp_ln_base.pyAdded+37
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/cuda_ln.pyAdded+30
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/cuda_post_ln.pyAdded+34
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/cuda_pre_ln.pyAdded+39
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/layer_norm.cppAdded+102
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/layer_norm.hAdded+67
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_layer_norm/layer_norm_cuda.cuAdded+489
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/__init__.pyAdded+6
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/__pycache__/cuda_linear.cpython-312.pycAdded
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/cuda_linear.pyAdded+207
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/include/configs.hAdded+96
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/include/kernel_matmul.cuhAdded+272
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/include/kernel_reduction.cuhAdded+43
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/include/ptx_cp.async.cuhAdded+75
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/include/ptx_mma.cuhAdded+139
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/include/utils_core.cuhAdded+246
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/include/utils_gmem.cuhAdded+86
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/include/utils_paralleldequant.cuhAdded+127
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/include/weight_prepacking.hAdded+209
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/linear_kernels.cppAdded+224
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/linear_kernels.hAdded+28
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/linear_kernels_cuda.cuAdded+318
lib/python3.12/site-packages/deepspeed/inference/v2/kernels/core_ops/cuda_linear/linear_kernels_cuda.hAdded+51