| 1 | #pragma once |
| 2 | #include <stddef.h> |
| 3 | #include <torch/csrc/stable/tensor.h> |
| 4 | #include <torch/csrc/stable/ops.h> |
| 5 | |
| 6 | #include "libtorch_stable/torch_utils.h" |
| 7 | |
| 8 | // clang-format will break include orders |
| 9 | // clang-format off |
| 10 | #include "cute/tensor.hpp" |
| 11 | #include "cute/atom/mma_atom.hpp" |
| 12 | #include "cutlass/numeric_types.h" |
| 13 | |
| 14 | #include "cutlass/cutlass.h" |
| 15 | #include "cutlass/gemm_coord.h" |
| 16 | #include "cutlass/arch/mma_sm75.h" |
| 17 | #include "cutlass/arch/arch.h" |
| 18 | #include "cutlass/arch/mma.h" |
| 19 | #include "cutlass/gemm/device/gemm.h" |
| 20 | #include "cutlass/gemm/device/gemm_universal_adapter.h" |
| 21 | |
| 22 | #include "cutlass/epilogue/threadblock/fusion/visitors.hpp" |
| 23 | #include "cutlass/gemm/kernel/default_gemm_universal_with_visitor.h" |
| 24 | |
| 25 | #include "libtorch_stable/core/math.hpp" |
| 26 | #include "libtorch_stable/cutlass_extensions/common.hpp" |
| 27 | // clang-format on |
| 28 | |
| 29 | using namespace cute; |
| 30 | |
| 31 | /* |
| 32 | Epilogues defined in, |
| 33 | csrc/cutlass_extensions/epilogue/scaled_mm_epilogues_c2x.hpp |
| 34 | must contain a public type named EVTCompute of type Sm80EVT, |
| 35 | as well as a static prepare_args function that constructs an |
| 36 | EVTCompute::Arguments struct. |
| 37 | */ |
| 38 | |
| 39 | namespace vllm { |
| 40 | template <typename Arch, template <typename> typename ArchGuard, |
| 41 | typename ElementAB_, typename ElementD_, |
| 42 | template <typename, typename> typename Epilogue_, typename TileShape, |
| 43 | typename WarpShape, typename InstructionShape, int32_t MainLoopStages, |
| 44 | typename FP8MathOperator = cutlass::arch::OpMultiplyAdd> |
| 45 | struct cutlass_2x_gemm { |
| 46 | using ElementAB = ElementAB_; |
| 47 | using ElementD = ElementD_; |
| 48 | |
| 49 | using ElementAcc = |
| 50 | typename std::conditional<std::is_same_v<ElementAB, int8_t>, int32_t, |
| 51 | float>::type; |
| 52 | |
| 53 | using Operator = |
| 54 | typename std::conditional<std::is_same_v<ElementAB, int8_t>, |
| 55 | cutlass::arch::OpMultiplyAddSaturate, |
| 56 | FP8MathOperator>::type; |
| 57 | |
| 58 | using OutputTileThreadMap = |
| 59 | cutlass::epilogue::threadblock::OutputTileThreadLayout< |
| 60 | TileShape, WarpShape, float, 4, 1 /* epilogue stages */ |
| 61 | >; |
| 62 | |
| 63 | using Epilogue = Epilogue_<ElementD, OutputTileThreadMap>; |
| 64 | using EVTCompute = typename Epilogue::EVTCompute; |
| 65 | |
| 66 | using D = cutlass::epilogue::threadblock::VisitorAuxStore< |
| 67 | OutputTileThreadMap, ElementD, cutlass::FloatRoundStyle::round_to_nearest, |
| 68 | Stride<int64_t, Int<1>, Int<0>>>; |
| 69 | |
| 70 | using EVTD = cutlass::epilogue::threadblock::Sm80EVT<D, EVTCompute>; |
| 71 | |
| 72 | // These are the minimum alignments needed for the kernels to compile |
| 73 | static constexpr int AlignmentAB = |
| 74 | 128 / cutlass::sizeof_bits<ElementAB>::value; |
| 75 | static constexpr int AlignmentCD = 4; |
| 76 | |
| 77 | // clang-format off |
| 78 | using RowMajor = typename cutlass::layout::RowMajor; |
| 79 | using ColumnMajor = typename cutlass::layout::ColumnMajor; |
| 80 | using KernelType = |
| 81 | ArchGuard<typename cutlass::gemm::kernel::DefaultGemmWithVisitor< |
| 82 | ElementAB, RowMajor, cutlass::ComplexTransform::kNone, AlignmentAB, |
| 83 | ElementAB, ColumnMajor, cutlass::ComplexTransform::kNone, AlignmentAB, |
| 84 | float, cutlass::layout::RowMajor, AlignmentCD, |
| 85 | ElementAcc, float, cutlass::arch::OpClassTensorOp, |
| 86 | Arch, |
| 87 | TileShape, WarpShape, InstructionShape, |
| 88 | EVTD, |
| 89 | cutlass::gemm::threadblock::ThreadblockSwizzleStreamK, |
| 90 | MainLoopStages, Operator, |
| 91 | 1 /* epilogue stages */ |
| 92 | >::GemmKernel>; |
| 93 | // clang-format on |
| 94 | |
| 95 | using Op = cutlass::gemm::device::GemmUniversalAdapter<KernelType>; |
| 96 | }; |
| 97 | |
| 98 | template <typename Gemm, typename... EpilogueArgs> |
| 99 | inline void cutlass_gemm_caller(torch::stable::Tensor& out, |
| 100 | torch::stable::Tensor const& a, |
| 101 | torch::stable::Tensor const& b, |
| 102 | EpilogueArgs&&... epilogue_params) { |
| 103 | using ElementAB = typename Gemm::ElementAB; |
| 104 | using ElementD = typename Gemm::ElementD; |
| 105 | |
| 106 | int32_t m = a.size(0); |
| 107 | int32_t n = b.size(1); |
| 108 | int32_t k = a.size(1); |
| 109 | cutlass::gemm::GemmCoord problem_size{m, n, k}; |
| 110 | |
| 111 | int64_t lda = a.stride(0); |
| 112 | int64_t ldb = b.stride(1); |
| 113 | int64_t ldc = out.stride(0); |
| 114 | |
| 115 | using StrideC = Stride<int64_t, Int<1>, Int<0>>; |
| 116 | StrideC c_stride{ldc, Int<1>{}, Int<0>{}}; |
| 117 | |
| 118 | auto a_ptr = static_cast<ElementAB const*>(a.data_ptr()); |
| 119 | auto b_ptr = static_cast<ElementAB const*>(b.data_ptr()); |
| 120 | auto c_ptr = static_cast<ElementD*>(out.data_ptr()); |
| 121 | |
| 122 | typename Gemm::D::Arguments d_args{c_ptr, c_stride}; |
| 123 | |
| 124 | using Epilogue = typename Gemm::Epilogue; |
| 125 | auto evt_args = |
| 126 | Epilogue::prepare_args(std::forward<EpilogueArgs>(epilogue_params)...); |
| 127 | |
| 128 | typename Gemm::EVTD::Arguments epilogue_args{ |
| 129 | evt_args, |
| 130 | d_args, |
| 131 | }; |
| 132 | |
| 133 | typename Gemm::Op::Arguments args{ |
| 134 | cutlass::gemm::GemmUniversalMode::kGemmSplitKParallel, // universal mode |
| 135 | problem_size, // problem size |
| 136 | 1, // batch count |
| 137 | epilogue_args, |
| 138 | a_ptr, |
| 139 | b_ptr, |
| 140 | nullptr, |
| 141 | nullptr, |
| 142 | 0, |
| 143 | 0, |
| 144 | 0, |
| 145 | 0, |
| 146 | lda, |
| 147 | ldb, |
| 148 | ldc, |
| 149 | ldc}; |
| 150 | |
| 151 | // Launch the CUTLASS GEMM kernel. |
| 152 | typename Gemm::Op gemm_op; |
| 153 | size_t workspace_size = gemm_op.get_workspace_size(args); |
| 154 | auto device = a.device(); |
| 155 | auto workspace = |
| 156 | torch::stable::empty(workspace_size, torch::headeronly::ScalarType::Byte, |
| 157 | std::nullopt, device); |
| 158 | |
| 159 | const torch::stable::accelerator::DeviceGuard device_guard(device.index()); |
| 160 | auto stream = get_current_cuda_stream(device.index()); |
| 161 | |
| 162 | CUTLASS_CHECK(gemm_op.can_implement(args)); |
| 163 | cutlass::Status status = gemm_op(args, workspace.data_ptr(), stream); |
| 164 | CUTLASS_CHECK(status); |
| 165 | } |
| 166 | |
| 167 | template <typename Gemm, typename FallbackGemm, typename... EpilogueArgs> |
| 168 | inline void fallback_cutlass_gemm_caller(torch::stable::Tensor& out, |
| 169 | torch::stable::Tensor const& a, |
| 170 | torch::stable::Tensor const& b, |
| 171 | EpilogueArgs&&... args) { |
| 172 | // In some cases, the GPU isn't able to accommodate the |
| 173 | // shared memory requirements of the Gemm. In such cases, use |
| 174 | // the FallbackGemm instead. |
| 175 | static const int max_shared_mem_per_block_opt_in = |
| 176 | get_cuda_max_shared_memory_per_block_opt_in(0); |
| 177 | |
| 178 | size_t const gemm_shared_mem_size = |
| 179 | sizeof(typename Gemm::KernelType::SharedStorage); |
| 180 | size_t const fallback_gemm_shared_mem_size = |
| 181 | sizeof(typename FallbackGemm::KernelType::SharedStorage); |
| 182 | |
| 183 | if (gemm_shared_mem_size <= max_shared_mem_per_block_opt_in) { |
| 184 | return cutlass_gemm_caller<Gemm>(out, a, b, |
| 185 | std::forward<EpilogueArgs>(args)...); |
| 186 | } else { |
| 187 | STD_TORCH_CHECK(fallback_gemm_shared_mem_size <= |
| 188 | max_shared_mem_per_block_opt_in); |
| 189 | return cutlass_gemm_caller<FallbackGemm>( |
| 190 | out, a, b, std::forward<EpilogueArgs>(args)...); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | } // namespace vllm |