|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 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 | + |
| 17 | +#include <torch/all.h> |
| 18 | + |
| 19 | +#include <ATen/cuda/CUDAContext.h> |
| 20 | +#include <c10/cuda/CUDAGuard.h> |
| 21 | + |
| 22 | +#include "cute/tensor.hpp" |
| 23 | + |
| 24 | +#include "cutlass/cutlass.h" |
| 25 | +#include "cutlass/kernel_hardware_info.h" |
| 26 | + |
| 27 | +#include "device/sm100_mla.hpp" |
| 28 | +#include "kernel/sm100_mla_tile_scheduler.hpp" |
| 29 | + |
| 30 | +#define CUTLASS_CHECK(status) \ |
| 31 | + { \ |
| 32 | + cutlass::Status error = status; \ |
| 33 | + TORCH_CHECK(error == cutlass::Status::kSuccess, \ |
| 34 | + cutlassGetStatusString(error)); \ |
| 35 | + } |
| 36 | + |
| 37 | +using namespace cute; |
| 38 | +using namespace cutlass::fmha::kernel; |
| 39 | + |
| 40 | +template<bool v> |
| 41 | +struct IsPersistent { |
| 42 | + static const bool value = v; |
| 43 | +}; |
| 44 | + |
| 45 | +template <typename T, typename PersistenceOption = IsPersistent<true>> |
| 46 | +struct MlaSm100 { |
| 47 | + using Element = T; |
| 48 | + using ElementAcc = float; |
| 49 | + using ElementOut = T; |
| 50 | + |
| 51 | + using TileShape = Shape<_128, _128, Shape<_512, _64>>; |
| 52 | + using TileShapeH = cute::tuple_element_t<0, TileShape>; |
| 53 | + using TileShapeD = cute::tuple_element_t<2, TileShape>; |
| 54 | + |
| 55 | + // H K (D_latent D_rope) B |
| 56 | + using ProblemShape = cute::tuple<TileShapeH, int, TileShapeD, int>; |
| 57 | + |
| 58 | + using StrideQ = cute::tuple<int64_t, _1, int64_t>; // H D B |
| 59 | + using StrideK = cute::tuple<int64_t, _1, int64_t>; // K D B |
| 60 | + using StrideO = StrideK; // H D B |
| 61 | + using StrideLSE = cute::tuple<_1, int>; // H B |
| 62 | + |
| 63 | + using TileScheduler = std::conditional_t< |
| 64 | + PersistenceOption::value, |
| 65 | + Sm100MlaPersistentTileScheduler, |
| 66 | + Sm100MlaIndividualTileScheduler>; |
| 67 | + |
| 68 | + using FmhaKernel = |
| 69 | + cutlass::fmha::kernel::Sm100FmhaMlaKernelTmaWarpspecialized< |
| 70 | + TileShape, Element, ElementAcc, ElementOut, ElementAcc, |
| 71 | + TileScheduler, /*kIsCpAsync=*/true>; |
| 72 | + using Fmha = cutlass::fmha::device::MLA<FmhaKernel>; |
| 73 | +}; |
| 74 | + |
| 75 | + |
| 76 | +template <typename T> |
| 77 | +typename T::Fmha::Arguments args_from_options(at::Tensor const& out, |
| 78 | + at::Tensor const& q_nope_and_q_pe, |
| 79 | + at::Tensor const& kv_c_and_k_pe_cache, |
| 80 | + at::Tensor const& seq_lens, |
| 81 | + at::Tensor const& page_table) { |
| 82 | + cutlass::KernelHardwareInfo hw_info; |
| 83 | + hw_info.device_id = q_nope_and_q_pe.device().index(); |
| 84 | + hw_info.sm_count = |
| 85 | + cutlass::KernelHardwareInfo::query_device_multiprocessor_count( |
| 86 | + hw_info.device_id); |
| 87 | + |
| 88 | + int batches = q_nope_and_q_pe.sizes()[0]; |
| 89 | + int page_count_per_seq = page_table.sizes()[1]; |
| 90 | + int page_count_total = kv_c_and_k_pe_cache.sizes()[0]; |
| 91 | + int page_size = kv_c_and_k_pe_cache.sizes()[1]; |
| 92 | + int max_seq_len = page_size * page_count_per_seq; |
| 93 | + using TileShapeH = typename T::TileShapeH; |
| 94 | + using TileShapeD = typename T::TileShapeD; |
| 95 | + auto problem_shape = cute::make_tuple( |
| 96 | + TileShapeH{}, max_seq_len, TileShapeD{}, batches); |
| 97 | + |
| 98 | + auto [H, K, D, B] = problem_shape; |
| 99 | + auto [D_latent, D_rope] = D; |
| 100 | + |
| 101 | + // the scale is based on the non-absorbed sizes, change as appropriate |
| 102 | + // we can't determine this parameter from the info we have, it's an input |
| 103 | + int D_non_latent = 128; |
| 104 | + float scale = 1.0 / sqrt(1.0 * (D_non_latent + D_rope)); |
| 105 | + |
| 106 | + using StrideQ = typename T::StrideQ; |
| 107 | + using StrideK = typename T::StrideK; |
| 108 | + using StrideO = typename T::StrideO; |
| 109 | + using StrideLSE = typename T::StrideLSE; |
| 110 | + |
| 111 | + StrideQ stride_Q = cute::make_tuple( |
| 112 | + static_cast<int64_t>(0 + D_latent + D_rope), |
| 113 | + _1{}, |
| 114 | + static_cast<int64_t>(H * (0 + D_latent + D_rope))); |
| 115 | + StrideK stride_C = cute::make_tuple( |
| 116 | + static_cast<int64_t>(0 + D_latent + D_rope), |
| 117 | + _1{}, |
| 118 | + static_cast<int64_t>(page_size * (D_latent + D_rope))); |
| 119 | + StrideLSE stride_PT = cute::make_stride(_1{}, page_count_per_seq); |
| 120 | + StrideLSE stride_LSE = cute::make_tuple(_1{}, 0 + H); |
| 121 | + StrideO stride_O = cute::make_tuple( |
| 122 | + static_cast<int64_t>(0 + D_latent), |
| 123 | + _1{}, |
| 124 | + static_cast<int64_t>(0 + H * D_latent)); |
| 125 | + |
| 126 | + using Element = typename T::Element; |
| 127 | + using ElementOut = typename T::ElementOut; |
| 128 | + using ElementAcc = typename T::ElementAcc; |
| 129 | + auto Q_ptr = static_cast<Element*>(q_nope_and_q_pe.data_ptr()); |
| 130 | + auto C_ptr = static_cast<Element*>(kv_c_and_k_pe_cache.data_ptr()); |
| 131 | + typename T::Fmha::Arguments arguments{ |
| 132 | + problem_shape, |
| 133 | + { scale, |
| 134 | + Q_ptr, stride_Q, |
| 135 | + Q_ptr + D_latent, stride_Q, |
| 136 | + C_ptr, stride_C, |
| 137 | + C_ptr + D_latent, stride_C, |
| 138 | + static_cast<int*>(seq_lens.data_ptr()), |
| 139 | + static_cast<int*>(page_table.data_ptr()), stride_PT, |
| 140 | + page_count_total, page_size}, |
| 141 | + { static_cast<ElementOut*>(out.data_ptr()), stride_O, |
| 142 | + // static_cast<ElementAcc*>(lse.data_ptr()), stride_LSE}, |
| 143 | + static_cast<ElementAcc*>(nullptr), stride_LSE}, |
| 144 | + hw_info, |
| 145 | + -1, // split_kv |
| 146 | + nullptr, // is_var_split_kv |
| 147 | + }; |
| 148 | + // TODO(kaixih@nvidia): When split_kv=-1 and is_var_split_kv=false, we compute |
| 149 | + // split_kv automatically based on batch size and sequence length to balance |
| 150 | + // workload across available SMs. Consider using var_split_kv for manual |
| 151 | + // control if needed. |
| 152 | + T::Fmha::set_split_kv(arguments); |
| 153 | + return arguments; |
| 154 | +} |
| 155 | + |
| 156 | +template <typename Element> |
| 157 | +void runMla(at::Tensor const& out, |
| 158 | + at::Tensor const& q_nope_and_q_pe, |
| 159 | + at::Tensor const& kv_c_and_k_pe_cache, |
| 160 | + at::Tensor const& seq_lens, |
| 161 | + at::Tensor const& page_table, |
| 162 | + cudaStream_t stream) { |
| 163 | + using MlaSm100Type = MlaSm100<Element>; |
| 164 | + typename MlaSm100Type::Fmha fmha; |
| 165 | + auto arguments = |
| 166 | + args_from_options<MlaSm100Type>(out, q_nope_and_q_pe, kv_c_and_k_pe_cache, |
| 167 | + seq_lens, page_table); |
| 168 | + size_t workspace_size = MlaSm100Type::Fmha::get_workspace_size(arguments); |
| 169 | + auto const workspace_options = |
| 170 | + torch::TensorOptions().dtype(torch::kUInt8).device(q_nope_and_q_pe.device()); |
| 171 | + auto workspace = torch::empty(workspace_size, workspace_options); |
| 172 | + |
| 173 | + CUTLASS_CHECK(fmha.can_implement(arguments)); |
| 174 | + |
| 175 | + CUTLASS_CHECK(fmha.initialize(arguments, workspace.data_ptr(), stream)); |
| 176 | + |
| 177 | + CUTLASS_CHECK(fmha.run(arguments, workspace.data_ptr(), stream)); |
| 178 | +} |
| 179 | + |
| 180 | +void cutlass_mla_decode_sm100a(torch::Tensor const& out, |
| 181 | + torch::Tensor const& q_nope_and_q_pe, |
| 182 | + torch::Tensor const& kv_c_and_k_pe_cache, |
| 183 | + torch::Tensor const& seq_lens, |
| 184 | + torch::Tensor const& page_table) { |
| 185 | + auto in_dtype = q_nope_and_q_pe.dtype(); |
| 186 | + at::cuda::CUDAGuard device_guard{(char)q_nope_and_q_pe.get_device()}; |
| 187 | + const cudaStream_t stream = at::cuda::getCurrentCUDAStream( |
| 188 | + q_nope_and_q_pe.get_device()); |
| 189 | + if (in_dtype == at::ScalarType::Half) { |
| 190 | + runMla<cutlass::half_t>( |
| 191 | + out, q_nope_and_q_pe, kv_c_and_k_pe_cache, seq_lens, page_table, stream); |
| 192 | + } else if (in_dtype == at::ScalarType::BFloat16) { |
| 193 | + runMla<cutlass::bfloat16_t>( |
| 194 | + out, q_nope_and_q_pe, kv_c_and_k_pe_cache, seq_lens, page_table, stream); |
| 195 | + } else if (in_dtype == at::ScalarType::Float8_e4m3fn) { |
| 196 | + runMla<cutlass::float_e4m3_t>( |
| 197 | + out, q_nope_and_q_pe, kv_c_and_k_pe_cache, seq_lens, page_table, stream); |
| 198 | + } else { |
| 199 | + TORCH_CHECK(false, "Unsupported input data type of MLA"); |
| 200 | + } |
| 201 | +} |
0 commit comments