|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 by FlashInfer team. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#ifndef FLASHINFER_GROUP_GEMM_CUTLASS_CUH_ |
| 17 | +#define FLASHINFER_GROUP_GEMM_CUTLASS_CUH_ |
| 18 | + |
| 19 | +#include "cutlass/cutlass.h" |
| 20 | +#include "cutlass/gemm/device/gemm_grouped.h" |
| 21 | +#include "cutlass/gemm/kernel/default_gemm_grouped.h" |
| 22 | +#include "cutlass/layout/matrix.h" |
| 23 | +#include "cutlass/numeric_types.h" |
| 24 | + |
| 25 | +namespace flashinfer { |
| 26 | + |
| 27 | +namespace group_gemm { |
| 28 | + |
| 29 | +template <typename T> |
| 30 | +struct cutlass_dtype { |
| 31 | + using type = T; |
| 32 | +}; |
| 33 | + |
| 34 | +template <> |
| 35 | +struct cutlass_dtype<half> { |
| 36 | + using type = cutlass::half_t; |
| 37 | +}; |
| 38 | + |
| 39 | +template <> |
| 40 | +struct cutlass_dtype<nv_bfloat16> { |
| 41 | + using type = cutlass::bfloat16_t; |
| 42 | +}; |
| 43 | + |
| 44 | +template <typename T> |
| 45 | +__global__ void compute_cutlass_group_gemm_args(cutlass::gemm::GemmCoord* all_problems, T** ptr_x, |
| 46 | + T** ptr_w, T** ptr_y, int64_t* ld_x, int64_t* ld_w, |
| 47 | + int64_t* ld_y, T* x, T* w, T* y, int64_t* xy_indptr, |
| 48 | + int64_t* w_indices, size_t d_in, size_t d_out, |
| 49 | + bool w_column_major) { |
| 50 | + int i = blockIdx.x; |
| 51 | + int m = xy_indptr[i + 1] - xy_indptr[i], k = d_in, n = d_out; |
| 52 | + all_problems[i] = cutlass::gemm::GemmCoord(m, n, k); |
| 53 | + ptr_w[i] = w + (w_indices == nullptr ? i : w_indices[i]) * d_in * d_out; |
| 54 | + ptr_x[i] = x + xy_indptr[i] * d_in; |
| 55 | + ptr_y[i] = y + xy_indptr[i] * d_out; |
| 56 | + ld_x[i] = k; // m * k |
| 57 | + ld_w[i] = w_column_major ? k : n; // k * n if column major, n * k if row major |
| 58 | + ld_y[i] = n; // m * n |
| 59 | +} |
| 60 | + |
| 61 | +} // namespace group_gemm |
| 62 | + |
| 63 | +} // namespace flashinfer |
| 64 | + |
| 65 | +#endif // FLASHINFER_GROUP_GEMM_CUTLASS_WRAPPER_CUH_ |
0 commit comments