|
| 1 | +#include <dlpack/dlpack.h> |
| 2 | + |
| 3 | +#include <flashinfer/cutlass_utils.cuh> |
| 4 | +#include <flashinfer/gemm/group_gemm_fp8_groupwise_sm100.cuh> |
| 5 | + |
| 6 | +#include "tvm_binding_utils.h" |
| 7 | + |
| 8 | +__global__ void simple_print_kernel(void* data, int dtype_code) { |
| 9 | + if (threadIdx.x == 0 && blockIdx.x == 0) { |
| 10 | + if (dtype_code == kDLBfloat) { |
| 11 | + // bfloat16 |
| 12 | + uint16_t* bf16_data = static_cast<uint16_t*>(data); |
| 13 | + uint32_t full = ((uint32_t)bf16_data[0]) << 16; |
| 14 | + float val = *reinterpret_cast<float*>(&full); |
| 15 | + printf("GPU: D[0] = %.6f\n", val); |
| 16 | + } else { |
| 17 | + // float32 |
| 18 | + float* f32_data = static_cast<float*>(data); |
| 19 | + printf("GPU: D[0] = %.6f\n", f32_data[0]); |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +// following MACROS duplicates from flashinfer/csrc/group_gemm_fp8_groupwise_sm100.cu |
| 25 | +#define DISPATCH_TVM_DTYPE_TO_CTYPE(tvm_dtype_in, tvm_dtype_out, c_type_in, c_type_out, ...) \ |
| 26 | + [&]() -> bool { \ |
| 27 | + if (tvm_dtype_in.code == kDLFloat8_e4m3fn && tvm_dtype_in.bits == 8) { \ |
| 28 | + using c_type_in = cutlass::float_e4m3_t; \ |
| 29 | + if (tvm_dtype_out.code == kDLFloat && tvm_dtype_out.bits == 16) { \ |
| 30 | + using c_type_out = cutlass::half_t; \ |
| 31 | + return __VA_ARGS__(); \ |
| 32 | + } \ |
| 33 | + if (tvm_dtype_out.code == kDLBfloat && tvm_dtype_out.bits == 16) { \ |
| 34 | + using c_type_out = cutlass::bfloat16_t; \ |
| 35 | + return __VA_ARGS__(); \ |
| 36 | + } \ |
| 37 | + } \ |
| 38 | + CHECK(false) << "Unsupported TVM dtype combination: input(" << tvm_dtype_in.code << "," \ |
| 39 | + << tvm_dtype_in.bits << ") output(" << tvm_dtype_out.code << "," \ |
| 40 | + << tvm_dtype_out.bits << ")"; \ |
| 41 | + return false; \ |
| 42 | + }() |
| 43 | + |
| 44 | +#define DISPATCH_MMA_SM(mma_sm, MMA_SM, ...) \ |
| 45 | + [&]() -> bool { \ |
| 46 | + if (mma_sm == 1) { \ |
| 47 | + constexpr int MMA_SM = 1; \ |
| 48 | + return __VA_ARGS__(); \ |
| 49 | + } else if (mma_sm == 2) { \ |
| 50 | + constexpr int MMA_SM = 2; \ |
| 51 | + return __VA_ARGS__(); \ |
| 52 | + } \ |
| 53 | + CHECK(false) << "Unsupported MMA SM: " << mma_sm; \ |
| 54 | + return false; \ |
| 55 | + }() |
| 56 | + |
| 57 | +#define DISPATCH_SCALE_GRANULARITY(scale_granularity_m, scale_granularity_n, scale_granularity_k, \ |
| 58 | + SCALE_GRANULARITY_M, SCALE_GRANULARITY_N, SCALE_GRANULARITY_K, \ |
| 59 | + ...) \ |
| 60 | + [&]() -> bool { \ |
| 61 | + if (scale_granularity_m == 1 && scale_granularity_n == 128 && scale_granularity_k == 128) { \ |
| 62 | + constexpr int SCALE_GRANULARITY_M = 1; \ |
| 63 | + constexpr int SCALE_GRANULARITY_N = 128; \ |
| 64 | + constexpr int SCALE_GRANULARITY_K = 128; \ |
| 65 | + return __VA_ARGS__(); \ |
| 66 | + } else if (scale_granularity_m == 128 && scale_granularity_n == 128 && \ |
| 67 | + scale_granularity_k == 128) { \ |
| 68 | + constexpr int SCALE_GRANULARITY_M = 128; \ |
| 69 | + constexpr int SCALE_GRANULARITY_N = 128; \ |
| 70 | + constexpr int SCALE_GRANULARITY_K = 128; \ |
| 71 | + return __VA_ARGS__(); \ |
| 72 | + } \ |
| 73 | + CHECK(false) << "Unsupported scale granularity: (" << scale_granularity_m << "," \ |
| 74 | + << scale_granularity_n << "," << scale_granularity_k << ")"; \ |
| 75 | + return false; \ |
| 76 | + }() |
| 77 | + |
| 78 | +#define DISPATCH_SCALE_MAJOR_K(scale_major_mode, SCALE_MAJOR_K, ...) \ |
| 79 | + [&]() -> bool { \ |
| 80 | + if (scale_major_mode == 0) { \ |
| 81 | + constexpr bool SCALE_MAJOR_K = true; \ |
| 82 | + return __VA_ARGS__(); \ |
| 83 | + } else if (scale_major_mode == 1) { \ |
| 84 | + constexpr bool SCALE_MAJOR_K = false; \ |
| 85 | + return __VA_ARGS__(); \ |
| 86 | + } \ |
| 87 | + CHECK(false) << "Unsupported Scale Major Mode: " << scale_major_mode; \ |
| 88 | + return false; \ |
| 89 | + }() |
| 90 | + |
| 91 | +namespace flashinfer { |
| 92 | +namespace group_gemm { |
| 93 | + |
| 94 | +template <int ScaleGranularityM, int ScaleGranularityN, int ScaleGranularityK, bool ScaleMajorK, |
| 95 | + int MmaSM, typename DTypeIn, typename DTypeOut> |
| 96 | +cudaError_t CutlassFP8GroupwiseScaledGroupGEMMSM100( |
| 97 | + void* int_buffer, size_t int_buffer_size_in_bytes, void* float_buffer, |
| 98 | + size_t float_buffer_size_in_bytes, DTypeIn* A, DTypeIn* B, float* SFA, float* SFB, DTypeOut* D, |
| 99 | + int* m_indptr, int max_m, int n, int k, int num_groups, cudaStream_t stream); |
| 100 | + |
| 101 | +} |
| 102 | +} // namespace flashinfer |
| 103 | + |
| 104 | +// FP8 Group GEMM implementation with CUTLASS for SM100A (Blackwell) |
| 105 | +void GroupedGemmFp8Run(DLTensor* int_workspace_buffer, DLTensor* float_workspace_buffer, |
| 106 | + DLTensor* A, DLTensor* B, DLTensor* SFA, DLTensor* SFB, DLTensor* D, |
| 107 | + DLTensor* m_indptr, int64_t n, int64_t k, int64_t scale_granularity_m, |
| 108 | + int64_t scale_granularity_n, int64_t scale_granularity_k, |
| 109 | + int64_t scale_major_mode, int64_t mma_sm, TVMStreamHandle cuda_stream) { |
| 110 | + cudaStream_t stream = reinterpret_cast<cudaStream_t>(cuda_stream); |
| 111 | + |
| 112 | + size_t float_workspace_size = |
| 113 | + float_workspace_buffer->shape[0] * DataType(float_workspace_buffer->dtype).bytes(); |
| 114 | + size_t int_workspace_size = |
| 115 | + int_workspace_buffer->shape[0] * DataType(int_workspace_buffer->dtype).bytes(); |
| 116 | + |
| 117 | + int64_t num_groups = m_indptr->shape[0] - 1; |
| 118 | + int64_t max_m = SFA->shape[1]; |
| 119 | + |
| 120 | + try { |
| 121 | + DISPATCH_TVM_DTYPE_TO_CTYPE(A->dtype, D->dtype, c_type_in, c_type_out, [&] { |
| 122 | + return DISPATCH_SCALE_MAJOR_K(scale_major_mode, SCALE_MAJOR_K, [&] { |
| 123 | + return DISPATCH_MMA_SM(mma_sm, MMA_SM, [&] { |
| 124 | + return DISPATCH_SCALE_GRANULARITY( |
| 125 | + scale_granularity_m, scale_granularity_n, scale_granularity_k, SCALE_GRANULARITY_M, |
| 126 | + SCALE_GRANULARITY_N, SCALE_GRANULARITY_K, [&] { |
| 127 | + using cutlass_t_in = flashinfer::cutlass_dtype_t<c_type_in>; |
| 128 | + using cutlass_t_out = flashinfer::cutlass_dtype_t<c_type_out>; |
| 129 | + |
| 130 | + auto status = flashinfer::group_gemm::CutlassFP8GroupwiseScaledGroupGEMMSM100< |
| 131 | + SCALE_GRANULARITY_M, SCALE_GRANULARITY_N, SCALE_GRANULARITY_K, SCALE_MAJOR_K, |
| 132 | + MMA_SM>( |
| 133 | + static_cast<int32_t*>(int_workspace_buffer->data) + |
| 134 | + int_workspace_buffer->byte_offset / sizeof(int32_t), |
| 135 | + int_workspace_buffer->shape[0] * sizeof(int32_t), |
| 136 | + static_cast<float*>(float_workspace_buffer->data) + |
| 137 | + float_workspace_buffer->byte_offset / sizeof(float), |
| 138 | + float_workspace_buffer->shape[0] * sizeof(float), |
| 139 | + static_cast<cutlass_t_in*>(A->data) + A->byte_offset / sizeof(cutlass_t_in), |
| 140 | + static_cast<cutlass_t_in*>(B->data) + B->byte_offset / sizeof(cutlass_t_in), |
| 141 | + static_cast<float*>(SFA->data) + SFA->byte_offset / sizeof(float), |
| 142 | + static_cast<float*>(SFB->data) + SFB->byte_offset / sizeof(float), |
| 143 | + static_cast<cutlass_t_out*>(D->data) + D->byte_offset / sizeof(cutlass_t_out), |
| 144 | + static_cast<int32_t*>(m_indptr->data) + m_indptr->byte_offset / sizeof(int32_t), |
| 145 | + max_m, n, k, num_groups, stream); |
| 146 | + |
| 147 | + // Check for CUDA errors immediately after kernel call |
| 148 | + cudaError_t cuda_error = cudaGetLastError(); |
| 149 | + if (cuda_error != cudaSuccess) { |
| 150 | + return false; |
| 151 | + } |
| 152 | + LOG(INFO) << "Kernel execution completed successfully"; |
| 153 | + return status == cudaSuccess; |
| 154 | + }); |
| 155 | + }); |
| 156 | + }); |
| 157 | + }); |
| 158 | + } catch (const std::exception& e) { |
| 159 | + LOG(INFO) << "Exception caught:" << e.what(); |
| 160 | + } |
| 161 | +} |
0 commit comments