v3.8.0 update (#2082)
* 3.8 update * fix Markus' name --------- Co-authored-by: yuzhai <yuzhai@nvidia.com>
This commit is contained in:
@@ -36,9 +36,17 @@
|
||||
|
||||
#include "cutlass/cutlass.h"
|
||||
#include "cutlass/detail/collective.hpp"
|
||||
#include "cutlass/array.h"
|
||||
#include "cutlass/array_subbyte.h"
|
||||
#include "cutlass/library/library.h"
|
||||
#include "library_internal.h"
|
||||
#include "cutlass/gemm/dispatch_policy.hpp"
|
||||
#include "cutlass/util/packed_stride.hpp"
|
||||
#include "cutlass/util/mixed_dtype_utils.hpp"
|
||||
#include "cutlass/util/device_memory.h"
|
||||
#include "cutlass/util/reference/device/tensor_fill.h"
|
||||
#include "cutlass/util/reference/device/tensor_compare.h"
|
||||
#include "cute/tensor.hpp"
|
||||
#include <unordered_map>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -65,7 +73,7 @@ public:
|
||||
using ElementAccumulator = typename Operator::ElementAccumulator;
|
||||
using ElementCompute = typename Operator::EpilogueOutputOp::ElementCompute;
|
||||
|
||||
private:
|
||||
protected:
|
||||
GemmDescription description_;
|
||||
|
||||
public:
|
||||
@@ -178,7 +186,23 @@ public:
|
||||
|
||||
/// Constructor
|
||||
GemmUniversal3xOperation(char const *name = "unknown_gemm"):
|
||||
GemmOperation3xBase<Operator_>(name, GemmKind::kUniversal) {}
|
||||
GemmOperation3xBase<Operator_>(name, GemmKind::kUniversal) {
|
||||
if constexpr (Operator::ArchTag::kMinComputeCapability == 90) {
|
||||
dim3 cluster_dims(
|
||||
cute::size<0>(typename Operator::GemmKernel::ClusterShape{}),
|
||||
cute::size<1>(typename Operator::GemmKernel::ClusterShape{}),
|
||||
cute::size<2>(typename Operator::GemmKernel::ClusterShape{}));
|
||||
uint32_t threads_per_block = Operator::GemmKernel::MaxThreadsPerBlock;
|
||||
void const* kernel_ptr = (void*)(device_kernel<typename Operator::GemmKernel>);
|
||||
max_active_clusters = cutlass::KernelHardwareInfo::query_device_max_active_clusters(
|
||||
cluster_dims,
|
||||
threads_per_block,
|
||||
kernel_ptr);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
int max_active_clusters{};
|
||||
|
||||
protected:
|
||||
|
||||
@@ -227,10 +251,119 @@ protected:
|
||||
}
|
||||
};
|
||||
|
||||
/// Constructs the arguments structure given the configuration and arguments
|
||||
static Status update_arguments_(
|
||||
template<template<int, class, class> class Policy, int Stages, class ClusterShape, class KernelSchedule>
|
||||
static constexpr bool is_mixed_dtype_mainloop_(Policy<Stages, ClusterShape, KernelSchedule> policy) {
|
||||
return (cute::is_same_v<Policy<Stages, ClusterShape, KernelSchedule>,
|
||||
cutlass::gemm::MainloopSm90TmaGmmaRmemAWarpSpecializedMixedInput<Stages, ClusterShape, KernelSchedule>>);
|
||||
}
|
||||
|
||||
template <class DispatchPolicy>
|
||||
static constexpr bool is_mixed_dtype_mainloop_(DispatchPolicy) {
|
||||
return false;
|
||||
}
|
||||
|
||||
template <
|
||||
typename ElementWide,
|
||||
typename ElementNarrow,
|
||||
typename ElementScaleMainloop,
|
||||
class ActualStrideAB,
|
||||
Sm90MixedInputWiderOperand wider_operand,
|
||||
bool is_n4w8,
|
||||
typename ElementScale,
|
||||
typename ElementZero,
|
||||
class Layout_SZ>
|
||||
static void dequantize_encode_(
|
||||
OperatorArguments &operator_args,
|
||||
GemmUniversalArguments const *arguments) {
|
||||
GemmUniversalArguments const *arguments,
|
||||
cudaStream_t stream,
|
||||
const int &problem_mn,
|
||||
const int &problem_k,
|
||||
const int &options_l,
|
||||
const int &options_g,
|
||||
ElementScale *ptr_S,
|
||||
ElementZero *ptr_Z,
|
||||
const size_t &SZ_size,
|
||||
Layout_SZ layout_SZ
|
||||
) {
|
||||
|
||||
auto shape_AB = cute::make_shape(problem_mn, problem_k, options_l);
|
||||
auto stride_AB = cutlass::make_cute_packed_stride(ActualStrideAB{}, shape_AB);
|
||||
auto layout_AB = cute::make_layout(shape_AB, stride_AB);
|
||||
auto *ptr_dequantized_AB = static_cast<ElementWide *>(arguments->dequantized_AB);
|
||||
const ElementNarrow *ptr_AB = nullptr;
|
||||
if constexpr(wider_operand == Sm90MixedInputWiderOperand::A) {
|
||||
ptr_AB = static_cast<const ElementNarrow *>(arguments->B);
|
||||
}
|
||||
else {
|
||||
ptr_AB = static_cast<const ElementNarrow *>(arguments->A);
|
||||
}
|
||||
dequantize(ptr_dequantized_AB, ptr_AB, layout_AB, ptr_S, ptr_Z, layout_SZ, options_g, stream);
|
||||
if constexpr(is_n4w8) {
|
||||
size_t AB_size = cute::size(layout_AB);
|
||||
cutlass::int4b_t *encoded_AB = static_cast<cutlass::int4b_t *>(arguments->encoded_AB);
|
||||
unified_encode_int4b(ptr_AB, encoded_AB, AB_size);
|
||||
if constexpr(wider_operand == Sm90MixedInputWiderOperand::A) {
|
||||
operator_args.mainloop.ptr_B = static_cast<ElementNarrow const *>(encoded_AB);
|
||||
}
|
||||
else {
|
||||
operator_args.mainloop.ptr_A = static_cast<ElementNarrow const *>(encoded_AB);
|
||||
}
|
||||
ElementScaleMainloop *ptr_packed_Scale = static_cast<ElementScaleMainloop *>(arguments->packed_Scale);
|
||||
pack_scale_fp8(ptr_S, ptr_packed_Scale, SZ_size);
|
||||
}
|
||||
}
|
||||
|
||||
template <
|
||||
typename ElementAB,
|
||||
class ActualStrideAB,
|
||||
class LayoutAB_Reordered,
|
||||
class LayoutAtomQuant,
|
||||
Sm90MixedInputWiderOperand wider_operand>
|
||||
static void handle_shuffle_tensor_(
|
||||
OperatorArguments &operator_args,
|
||||
GemmUniversalArguments const *arguments,
|
||||
const int &problem_mn,
|
||||
const int &problem_k,
|
||||
const int &options_l) {
|
||||
|
||||
auto shape_AB = cute::make_shape(problem_mn, problem_k, options_l);
|
||||
auto stride_AB = cutlass::make_cute_packed_stride(ActualStrideAB{}, shape_AB);
|
||||
auto layout_AB = cute::make_layout(shape_AB, stride_AB);
|
||||
LayoutAB_Reordered layout_AB_reordered = cute::tile_to_shape(LayoutAtomQuant{}, shape_AB);
|
||||
if constexpr(wider_operand == Sm90MixedInputWiderOperand::A) {
|
||||
operator_args.mainloop.dB = layout_AB_reordered;
|
||||
}
|
||||
else {
|
||||
operator_args.mainloop.dA = layout_AB_reordered;
|
||||
}
|
||||
if (arguments->generate_dequantized_AB) {
|
||||
size_t AB_size = cute::size(layout_AB);
|
||||
ElementAB *AB_reordered = cutlass::device_memory::allocate<ElementAB>(AB_size);
|
||||
const ElementAB *AB_src = nullptr;
|
||||
if constexpr(wider_operand == Sm90MixedInputWiderOperand::A) {
|
||||
AB_src = static_cast<const ElementAB *>(operator_args.mainloop.ptr_B);
|
||||
}
|
||||
else {
|
||||
AB_src = static_cast<const ElementAB *>(operator_args.mainloop.ptr_A);
|
||||
}
|
||||
reorder_tensor(AB_src, layout_AB, AB_reordered, layout_AB_reordered);
|
||||
ElementAB *AB_dst = static_cast<ElementAB *>(arguments->encoded_AB);
|
||||
cutlass::device_memory::copy_device_to_device(AB_dst, AB_reordered, AB_size);
|
||||
cutlass::device_memory::free(AB_reordered);
|
||||
if constexpr(wider_operand == Sm90MixedInputWiderOperand::A) {
|
||||
operator_args.mainloop.ptr_B = AB_dst;
|
||||
}
|
||||
else {
|
||||
operator_args.mainloop.ptr_A = AB_dst;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Constructs the arguments structure given the configuration and arguments
|
||||
Status update_arguments_(
|
||||
OperatorArguments& operator_args,
|
||||
GemmUniversalArguments const* arguments,
|
||||
cudaStream_t stream = nullptr) const {
|
||||
Status status = Status::kSuccess;
|
||||
|
||||
status = UpdateFusionArgs<decltype(operator_args.epilogue.thread)>::update_(
|
||||
@@ -286,24 +419,173 @@ protected:
|
||||
operator_args.epilogue.ptr_C = static_cast<ElementC const *>(arguments->C);
|
||||
operator_args.epilogue.ptr_D = static_cast<ElementD *>(arguments->D);
|
||||
|
||||
operator_args.mainloop.dA = cute::make_int_tuple_from<typename Operator::GemmKernel::StrideA>(
|
||||
// Stride{A,B} is a Layout if and only if:
|
||||
// (1) This is a mixed dtype kernel, and
|
||||
// (2) This mixed dtype kernel is using shuffling, and
|
||||
// (3) sizeof(narrow_type) == 4 or 8 bits, and
|
||||
// (4) sizeof(wide_type) == 16 bits.
|
||||
// If A/B has the narrow data type, Stride{A/B} will be a Layout
|
||||
constexpr bool is_StrideA_Layout = cute::is_layout<typename CollectiveMainloop::StrideA>::value;
|
||||
constexpr bool is_StrideB_Layout = cute::is_layout<typename CollectiveMainloop::StrideB>::value;
|
||||
static_assert(!(is_StrideA_Layout && is_StrideB_Layout), "Incorrect kernel configuration: StrideA and StrideB are both cute::Layout");
|
||||
if constexpr(!is_StrideA_Layout) {
|
||||
operator_args.mainloop.dA = cute::make_int_tuple_from<typename Operator::GemmKernel::StrideA>(
|
||||
arguments->lda, arguments->batch_stride_A);
|
||||
operator_args.mainloop.dB = cute::make_int_tuple_from<typename Operator::GemmKernel::StrideB>(
|
||||
}
|
||||
if constexpr(!is_StrideB_Layout) {
|
||||
operator_args.mainloop.dB = cute::make_int_tuple_from<typename Operator::GemmKernel::StrideB>(
|
||||
arguments->ldb, arguments->batch_stride_B);
|
||||
}
|
||||
operator_args.epilogue.dC = cute::make_int_tuple_from<typename Operator::GemmKernel::StrideC>(
|
||||
arguments->ldc, arguments->batch_stride_C);
|
||||
operator_args.epilogue.dD = operator_args.epilogue.dC;
|
||||
|
||||
using MainloopPolicy = typename CollectiveMainloop::DispatchPolicy;
|
||||
if constexpr(is_mixed_dtype_mainloop_(MainloopPolicy{})) {
|
||||
int problem_m = arguments->problem_size.m();
|
||||
int problem_n = arguments->problem_size.n();
|
||||
int problem_k = arguments->problem_size.k();
|
||||
int options_l = arguments->batch_count;
|
||||
|
||||
constexpr Sm90MixedInputWiderOperand wider_operand =
|
||||
(cutlass::sizeof_bits<ElementA>::value > cutlass::sizeof_bits<ElementB>::value) ?
|
||||
Sm90MixedInputWiderOperand::A : Sm90MixedInputWiderOperand::B;
|
||||
using ElementWide = std::conditional_t<wider_operand == Sm90MixedInputWiderOperand::A, ElementA, ElementB>;
|
||||
using ElementNarrow = std::conditional_t<wider_operand == Sm90MixedInputWiderOperand::A, ElementB, ElementA>;
|
||||
|
||||
constexpr bool has_scale = !std::is_same_v<typename CollectiveMainloop::ElementScale, void>;
|
||||
constexpr bool has_zero = !std::is_same_v<typename CollectiveMainloop::ElementZero, void>;
|
||||
if constexpr(has_scale) {
|
||||
int options_g = problem_k;
|
||||
int scale_k = (problem_k + options_g - 1) / options_g;
|
||||
|
||||
constexpr bool is_A4B8 = (
|
||||
cutlass::is_same_v<ElementA, cutlass::int4b_t> &&
|
||||
(cutlass::is_same_v<ElementB, cutlass::float_e4m3_t> ||
|
||||
cutlass::is_same_v<ElementB, cutlass::float_e5m2_t>));
|
||||
constexpr bool is_A8B4 = (
|
||||
cutlass::is_same_v<ElementB, cutlass::int4b_t> &&
|
||||
(cutlass::is_same_v<ElementA, cutlass::float_e4m3_t> ||
|
||||
cutlass::is_same_v<ElementA, cutlass::float_e5m2_t>));
|
||||
constexpr bool is_int4_x_fp8 = is_A4B8 || is_A8B4;
|
||||
|
||||
// In int4 * fp8, ElementScale is a cutlass::Array, need to take out it's real element
|
||||
using ElementScaleMainloop = typename CollectiveMainloop::ElementScale;
|
||||
using ElementScale = typename UnderlyingElement<typename CollectiveMainloop::ElementScale>::type;
|
||||
using StrideS = typename CollectiveMainloop::StrideScale;
|
||||
// In ScaleOnly mode, we have allocated the same size of memory for arguments->Z and arguments->S
|
||||
using ElementZero = std::conditional_t<
|
||||
has_zero,
|
||||
typename CollectiveMainloop::ElementZero,
|
||||
ElementScale
|
||||
>;
|
||||
const int SZ_1st_dim = (wider_operand == Sm90MixedInputWiderOperand::A) ? problem_n : problem_m;
|
||||
const size_t SZ_size = static_cast<size_t>(SZ_1st_dim * scale_k * options_l);
|
||||
auto shape_SZ = cute::make_shape(SZ_1st_dim, scale_k, options_l);
|
||||
ElementScale *ptr_S = static_cast<ElementScale *>(arguments->Scale);
|
||||
ElementZero *ptr_Z = static_cast<ElementZero *>(arguments->Zero);
|
||||
|
||||
// 1. If arguments is initialized in profiler, S and Z needs to be allocated and filled
|
||||
if (arguments->generate_scale_and_zero) {
|
||||
// Need to fix max_dequant_val and min_dequant_val?
|
||||
const float elt_max_f = float(cutlass::platform::numeric_limits<ElementScale>::max());
|
||||
const float max_dequant_val = elt_max_f * 0.25f;
|
||||
const float min_dequant_val = 0.5f;
|
||||
const float scale_max = max_dequant_val / elt_max_f;
|
||||
const float scale_min = min_dequant_val / elt_max_f;
|
||||
uint64_t seed = 2023;
|
||||
cutlass::reference::device::BlockFillRandomUniform(
|
||||
ptr_S, SZ_size, seed, ElementScale(scale_max), ElementScale(scale_min));
|
||||
|
||||
// In ScaleOnly mode, set Z as zero for generating dequantized A or B
|
||||
const float zero_max = has_zero ? 2.0f : 0.0f;
|
||||
const float zero_min = has_zero ? -2.0f : 0.0f;
|
||||
cutlass::reference::device::BlockFillRandomUniform(
|
||||
ptr_Z, SZ_size, seed, ElementZero(zero_max), ElementZero(zero_min));
|
||||
} // End of "if (arguments->generate_scale_and_zero)"
|
||||
|
||||
// 2. Generate the dequantized A or B for verification
|
||||
if (arguments->generate_dequantized_AB) {
|
||||
StrideS stride_SZ = cutlass::make_cute_packed_stride(StrideS{}, shape_SZ);
|
||||
auto layout_SZ = cute::make_layout(shape_SZ, stride_SZ);
|
||||
if constexpr(wider_operand == Sm90MixedInputWiderOperand::A) {
|
||||
if constexpr(is_StrideB_Layout) {
|
||||
// The generator only generates row-major A and col-major B at the moment
|
||||
// Need a way to read out the actual layout of B later
|
||||
using ActualLayoutB = cutlass::layout::ColumnMajor;
|
||||
using ActualStrideB = cutlass::detail::TagToStrideB_t<ActualLayoutB>;
|
||||
dequantize_encode_<ElementWide, ElementNarrow, ElementScaleMainloop, ActualStrideB, wider_operand, is_A8B4>(
|
||||
operator_args, arguments, stream, problem_m, problem_k, options_l, options_g, ptr_S, ptr_Z, SZ_size, layout_SZ);
|
||||
}
|
||||
else {
|
||||
using ActualStrideB = typename CollectiveMainloop::StrideB;
|
||||
dequantize_encode_<ElementWide, ElementNarrow, ElementScaleMainloop, ActualStrideB, wider_operand, is_A8B4>(
|
||||
operator_args, arguments, stream, problem_m, problem_k, options_l, options_g, ptr_S, ptr_Z, SZ_size, layout_SZ);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if constexpr(is_StrideA_Layout) {
|
||||
// The generator only generates row-major A and col-major B at the moment
|
||||
// Need a way to read out the actual layout of A later
|
||||
using ActualLayoutA = cutlass::layout::RowMajor;
|
||||
using ActualStrideA = cutlass::detail::TagToStrideA_t<ActualLayoutA>;
|
||||
dequantize_encode_<ElementWide, ElementNarrow, ElementScaleMainloop, ActualStrideA, wider_operand, is_A4B8>(
|
||||
operator_args, arguments, stream, problem_m, problem_k, options_l, options_g, ptr_S, ptr_Z, SZ_size, layout_SZ);
|
||||
}
|
||||
else {
|
||||
using ActualStrideA = typename CollectiveMainloop::StrideA;
|
||||
dequantize_encode_<ElementWide, ElementNarrow, ElementScaleMainloop, ActualStrideA, wider_operand, is_A4B8>(
|
||||
operator_args, arguments, stream, problem_m, problem_k, options_l, options_g, ptr_S, ptr_Z, SZ_size, layout_SZ);
|
||||
}
|
||||
} // End of "if constexpr(wider_operand == Sm90MixedInputWiderOperand::A)"
|
||||
arguments->dequantized_AB_ready[0] = true;
|
||||
} // End of "if (arguments->generate_dequantized_AB)"
|
||||
|
||||
// 3. Put arguments in mainloop
|
||||
if constexpr(is_int4_x_fp8) {
|
||||
operator_args.mainloop.ptr_S = static_cast<ElementScaleMainloop const*>(arguments->packed_Scale);
|
||||
}
|
||||
else {
|
||||
operator_args.mainloop.ptr_S = static_cast<ElementScale const*>(arguments->Scale);
|
||||
}
|
||||
operator_args.mainloop.dS = cutlass::make_cute_packed_stride(StrideS{}, shape_SZ);
|
||||
operator_args.mainloop.group_size = options_g;
|
||||
if constexpr(has_zero) {
|
||||
operator_args.mainloop.ptr_Z = static_cast<ElementZero const*>(arguments->Zero);
|
||||
}
|
||||
} // End of "if constexpr(has_scale)"
|
||||
|
||||
// Handle the shuffling
|
||||
using ValueShuffle = std::conditional_t<
|
||||
cutlass::sizeof_bits<ElementNarrow>::value == 4,
|
||||
cute::Layout<cute::Shape<cute::_2,cute::_4>, cute::Stride<cute::_4,cute::_1>>,
|
||||
cute::Layout<cute::Shape<cute::_2,cute::_2>, cute::Stride<cute::_2,cute::_1>>
|
||||
>;
|
||||
constexpr int NumShuffleAtoms = 1;
|
||||
using MmaAtomShape = cute::Layout<cute::Shape<cute::_1,cute::Int<NumShuffleAtoms>>>;
|
||||
using LayoutAtomQuant = decltype(compute_memory_reordering_atom<ElementWide, MmaAtomShape, ValueShuffle>());
|
||||
// The generator only generates row-major A and col-major B at the moment
|
||||
// Need a way to read out the actual layout and stride of A/B later
|
||||
if constexpr(wider_operand == Sm90MixedInputWiderOperand::A && is_StrideB_Layout) {
|
||||
using ActualLayoutB = cutlass::layout::ColumnMajor;
|
||||
using ActualStrideB = cutlass::detail::TagToStrideB_t<ActualLayoutB>;
|
||||
using LayoutB_Reordered = typename CollectiveMainloop::StrideB;
|
||||
handle_shuffle_tensor_<ElementB, ActualStrideB, LayoutB_Reordered, LayoutAtomQuant, wider_operand>(
|
||||
operator_args, arguments, problem_n, problem_k, options_l);
|
||||
}
|
||||
if constexpr(wider_operand == Sm90MixedInputWiderOperand::B && is_StrideA_Layout) {
|
||||
using ActualLayoutA = cutlass::layout::RowMajor;
|
||||
using ActualStrideA = cutlass::detail::TagToStrideA_t<ActualLayoutA>;
|
||||
using LayoutA_Reordered = typename CollectiveMainloop::StrideA;
|
||||
handle_shuffle_tensor_<ElementA, ActualStrideA, LayoutA_Reordered, LayoutAtomQuant, wider_operand>(
|
||||
operator_args, arguments, problem_m, problem_k, options_l);
|
||||
}
|
||||
} // End of "if constexpr(is_mixed_dtype_mainloop_(MainloopPolicy{}))"
|
||||
|
||||
/* Query device SM count and max active clusters to pass onto the kernel as an argument, where needed */
|
||||
operator_args.hw_info.sm_count = arguments->sm_count;
|
||||
if constexpr (Operator::ArchTag::kMinComputeCapability == 90) {
|
||||
dim3 cluster_dims(cute::size<0>(typename Operator::GemmKernel::ClusterShape{}),
|
||||
cute::size<1>(typename Operator::GemmKernel::ClusterShape{}),
|
||||
cute::size<2>(typename Operator::GemmKernel::ClusterShape{}));
|
||||
uint32_t threads_per_block = Operator::GemmKernel::MaxThreadsPerBlock;
|
||||
void const* kernel_ptr = (void*)(device_kernel<typename Operator::GemmKernel>);
|
||||
operator_args.hw_info.max_active_clusters = cutlass::KernelHardwareInfo::query_device_max_active_clusters(
|
||||
cluster_dims, threads_per_block, kernel_ptr);
|
||||
operator_args.hw_info.max_active_clusters = max_active_clusters;
|
||||
}
|
||||
if constexpr (!std::is_const_v<decltype(operator_args.scheduler.max_swizzle_size)>) {
|
||||
operator_args.scheduler.max_swizzle_size = arguments->swizzle_size;
|
||||
@@ -356,7 +638,10 @@ public:
|
||||
return status;
|
||||
}
|
||||
|
||||
return Operator::can_implement(args);
|
||||
Status can_impl = Operator::can_implement(args);
|
||||
|
||||
//return Operator::can_implement(args);
|
||||
return can_impl;
|
||||
}
|
||||
|
||||
/// Gets the host-side workspace
|
||||
@@ -397,7 +682,7 @@ public:
|
||||
cudaStream_t stream = nullptr) const override {
|
||||
|
||||
OperatorArguments args;
|
||||
Status status = update_arguments_(args, static_cast<GemmUniversalArguments const *>(arguments_ptr));
|
||||
Status status = update_arguments_(args, static_cast<GemmUniversalArguments const *>(arguments_ptr), stream);
|
||||
if (status != Status::kSuccess) {
|
||||
return status;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user