|
| 1 | +// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +#pragma once |
| 15 | + |
| 16 | +#include "fp8_common.h" |
| 17 | +#include "cutlass/cutlass.h" |
| 18 | +#include "cutlass/float8.h" |
| 19 | +#include "cutlass/gemm/device/gemm_universal.h" |
| 20 | + |
| 21 | +#include "cutlass/util/packed_stride.hpp" |
| 22 | +#include "cutlass/gemm/dispatch_policy.hpp" |
| 23 | +#include "cutlass/gemm/collective/collective_builder.hpp" |
| 24 | +#include "cutlass/epilogue/collective/collective_builder.hpp" |
| 25 | +#include "cutlass/gemm/device/gemm_universal_adapter.h" |
| 26 | +#include "cutlass/gemm/kernel/gemm_universal.hpp" |
| 27 | +#include "cutlass/gemm/kernel/tile_scheduler.hpp" |
| 28 | +#include "cutlass_kernels/gemm/collective/collective_builder_gated.hpp" |
| 29 | +#include "cutlass_kernels/gemm/kernel/gemm_universal_gated.hpp" |
| 30 | + |
| 31 | +using namespace cute; |
| 32 | + |
| 33 | +template <typename InputType, typename CTAShape, typename ClusterShape, |
| 34 | + typename MainloopScheduleType, typename EpilogueScheduleType, typename TileSchedulerType = void, |
| 35 | + template <class /* ElementCompute */> class Activation = cutlass::epilogue::thread::SiLu, bool SwapAB = true> |
| 36 | +bool dispatch_dual_gemm_act_sm90(DualGemmEpilogueAllParams params) { |
| 37 | + using ElementA = typename std::conditional_t< |
| 38 | + std::is_same_v<InputType, phi::dtype::float8_e4m3fn>, |
| 39 | + cutlass::float_e4m3_t, |
| 40 | + cutlass::float_e5m2_t>; |
| 41 | + using LayoutA = cutlass::layout::RowMajor; // Layout type for A matrix operand |
| 42 | + static constexpr int AlignmentA |
| 43 | + = 128 / cutlass::sizeof_bits<ElementA>::value; // Memory access granularity/alignment of A |
| 44 | + // matrix in units of elements (up to 16 bytes) |
| 45 | + |
| 46 | + // B matrix configuration |
| 47 | + using ElementB = ElementA; // Element type for B matrix operand |
| 48 | + using LayoutB = cutlass::layout::ColumnMajor; // Layout type for B matrix operand |
| 49 | + static constexpr int AlignmentB |
| 50 | + = 128 / cutlass::sizeof_bits<ElementB>::value; // Memory access granularity/alignment of B |
| 51 | + // matrix in units of elements (up to 16 bytes) |
| 52 | + |
| 53 | + using ElementC = ElementA; // Element type for C matrix operands |
| 54 | + |
| 55 | + using LayoutC = cute::conditional_t<SwapAB, cutlass::layout::ColumnMajor, cutlass::layout::RowMajor>; |
| 56 | + static constexpr int AlignmentC |
| 57 | + = 128 / cutlass::sizeof_bits<ElementC>::value; // Memory access granularity/alignment of C matrices in units of |
| 58 | + // elements (up to 16 bytes) |
| 59 | + |
| 60 | + // Output matrix configuration |
| 61 | + using ElementOutput = ElementA; // Element type for output matrix operands |
| 62 | + // using LayoutOutput = cutlass::layout::RowMajor; // Layout type for output matrix operands |
| 63 | + using LayoutOutput = cute::conditional_t<SwapAB, cutlass::layout::ColumnMajor, cutlass::layout::RowMajor>; |
| 64 | + static constexpr int AlignmentOutput = 128 / cutlass::sizeof_bits<ElementOutput>::value; |
| 65 | + |
| 66 | + // Multiply-accumulate blocking/pipelining details |
| 67 | + using ElementAccumulator = float; // Element type for internal accumulation |
| 68 | + using ElementCompute = float; // Element type for compute |
| 69 | + using ArchTag = cutlass::arch::Sm90; // Tag indicating the minimum SM that supports the intended feature |
| 70 | + using OperatorClass = cutlass::arch::OpClassTensorOp; // Operator class tag |
| 71 | + using TileShape = CTAShape; // Threadblock-level tile size |
| 72 | + using KernelSchedule = MainloopScheduleType; |
| 73 | + using EpilogueSchedule = EpilogueScheduleType; |
| 74 | + using TileScheduler = TileSchedulerType; |
| 75 | + |
| 76 | + using EpilogueTileType = cutlass::epilogue::collective::EpilogueTileAuto; |
| 77 | + |
| 78 | + static constexpr auto RoundStyle = cutlass::FloatRoundStyle::round_to_nearest; |
| 79 | + using FusionOperation = cutlass::epilogue::fusion::ScaledAcc<ElementOutput, ElementCompute>; |
| 80 | + |
| 81 | + using CollectiveEpilogue = typename cutlass::epilogue::collective::CollectiveBuilder<ArchTag, OperatorClass, |
| 82 | + TileShape, ClusterShape, EpilogueTileType, ElementAccumulator, ElementAccumulator, ElementC, LayoutC, |
| 83 | + AlignmentC, ElementOutput, LayoutOutput, AlignmentOutput, EpilogueSchedule, FusionOperation>::CollectiveOp; |
| 84 | + |
| 85 | + using CollectiveMainloop = typename cutlass::gemm::collective::CollectiveBuilderGated<ArchTag, OperatorClass, |
| 86 | + ElementA, LayoutA, AlignmentA, ElementB, LayoutB, AlignmentB, ElementAccumulator, TileShape, ClusterShape, |
| 87 | + cutlass::gemm::collective::StageCountAutoCarveout<static_cast<int>( |
| 88 | + sizeof(typename CollectiveEpilogue::SharedStorage))>, |
| 89 | + KernelSchedule, Activation, SwapAB>::CollectiveOp; |
| 90 | + |
| 91 | + using GemmKernel = cutlass::gemm::kernel::GemmUniversalGated<Shape<int, int, int, int>, // Indicates ProblemShape |
| 92 | + CollectiveMainloop, CollectiveEpilogue, TileScheduler>; |
| 93 | + |
| 94 | + using Gemm = cutlass::gemm::device::GemmUniversalAdapter<GemmKernel>; |
| 95 | + |
| 96 | + using StrideA = typename Gemm::GemmKernel::StrideA; |
| 97 | + using StrideB = typename Gemm::GemmKernel::StrideB; |
| 98 | + using StrideC = typename Gemm::GemmKernel::StrideC; |
| 99 | + using StrideD = typename Gemm::GemmKernel::StrideD; |
| 100 | + |
| 101 | + int arg_m = params.M; |
| 102 | + int arg_n = params.N; |
| 103 | + ElementA const* ptr_A = reinterpret_cast<ElementA const*>(params.A); |
| 104 | + ElementB const* ptr_B0 = reinterpret_cast<ElementB const*>(params.B0); |
| 105 | + ElementB const* ptr_B1 = reinterpret_cast<ElementB const*>(params.B1); |
| 106 | + if constexpr (SwapAB) |
| 107 | + { |
| 108 | + arg_m = params.N; |
| 109 | + arg_n = params.M; |
| 110 | + ptr_A = reinterpret_cast<ElementB const*>(params.B0); |
| 111 | + ptr_B0 = reinterpret_cast<ElementA const*>(params.A); |
| 112 | + } |
| 113 | + StrideA stride_A = cutlass::make_cute_packed_stride(StrideA{}, cute::make_shape(arg_m, params.K, 1)); |
| 114 | + StrideB stride_B = cutlass::make_cute_packed_stride(StrideB{}, cute::make_shape(arg_n, params.K, 1)); |
| 115 | + StrideC stride_C; |
| 116 | + StrideD stride_D = cutlass::make_cute_packed_stride(StrideD{}, cute::make_shape(arg_m, arg_n, 1)); |
| 117 | + |
| 118 | + typename Gemm::Arguments arguments = {cutlass::gemm::GemmUniversalMode::kGemm, {arg_m, arg_n, params.K, 1}, |
| 119 | + {ptr_A, stride_A, ptr_B0, ptr_B1, stride_B, params.scale0, params.scale1}, |
| 120 | + {{}, // epilogue.thread |
| 121 | + nullptr, stride_C, reinterpret_cast<ElementOutput*>(params.D), stride_D}}; |
| 122 | + arguments.epilogue.thread.alpha = params.scale_out; |
| 123 | + |
| 124 | + Gemm gemm_op; |
| 125 | + |
| 126 | + cutlass::Status status = gemm_op.can_implement(arguments); |
| 127 | + |
| 128 | + if (status != cutlass::Status::kSuccess) { |
| 129 | + std::cerr << "Gemm::can_implement() failed" << std::endl; |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + size_t workspace_size = Gemm::get_workspace_size(arguments); |
| 134 | + phi::Allocator* allocator = paddle::GetAllocator(params.place); |
| 135 | + auto workspace = allocator->Allocate(workspace_size); |
| 136 | + |
| 137 | + // |
| 138 | + // Run the GEMM |
| 139 | + // |
| 140 | + status = gemm_op(arguments, workspace->ptr(), params.stream); |
| 141 | + if (status != cutlass::Status::kSuccess) { |
| 142 | + std::cerr << "Gemm::run() failed" << std::endl; |
| 143 | + return false; |
| 144 | + } |
| 145 | + return true; |
| 146 | +} |
| 147 | + |
0 commit comments