|
| 1 | +/****************************************************************************** |
| 2 | + * Copyright (c) 2024, Tri Dao. |
| 3 | + ******************************************************************************/ |
| 4 | +// clang-format off |
| 5 | +// adapted from https://github.com/Dao-AILab/causal-conv1d/blob/main/csrc/causal_conv1d.h |
| 6 | +#pragma once |
| 7 | + |
| 8 | +#include <cuda_bf16.h> |
| 9 | +#include <cuda_fp16.h> |
| 10 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 11 | + |
| 12 | +struct ConvParamsBase { |
| 13 | + using index_t = uint32_t; |
| 14 | + |
| 15 | + int batch, dim, seqlen, width; |
| 16 | + bool silu_activation; |
| 17 | + |
| 18 | + index_t x_batch_stride; |
| 19 | + index_t x_c_stride; |
| 20 | + index_t x_l_stride; |
| 21 | + index_t weight_c_stride; |
| 22 | + index_t weight_width_stride; |
| 23 | + index_t out_batch_stride; |
| 24 | + index_t out_c_stride; |
| 25 | + index_t out_l_stride; |
| 26 | + |
| 27 | + index_t conv_state_batch_stride; |
| 28 | + index_t conv_state_c_stride; |
| 29 | + index_t conv_state_l_stride; |
| 30 | + |
| 31 | + // Common data pointers. |
| 32 | + void *__restrict__ x_ptr; |
| 33 | + void *__restrict__ weight_ptr; |
| 34 | + void *__restrict__ bias_ptr; |
| 35 | + void *__restrict__ out_ptr; |
| 36 | + |
| 37 | + void *__restrict__ conv_state_ptr; |
| 38 | + |
| 39 | + void *__restrict__ seq_idx_ptr; |
| 40 | + |
| 41 | + // No __restrict__ since initial_states could be the same as final_states. |
| 42 | + void * initial_states_ptr; |
| 43 | + index_t initial_states_batch_stride; |
| 44 | + index_t initial_states_l_stride; |
| 45 | + index_t initial_states_c_stride; |
| 46 | + |
| 47 | + void * final_states_ptr; |
| 48 | + index_t final_states_batch_stride; |
| 49 | + index_t final_states_l_stride; |
| 50 | + index_t final_states_c_stride; |
| 51 | +}; |
| 52 | + |
| 53 | + |
| 54 | +#ifndef USE_ROCM |
| 55 | + #include <cuda_bf16.h> |
| 56 | + |
| 57 | + template<typename T> |
| 58 | + __device__ inline T shuffle_xor(T val, int offset) { |
| 59 | + return __shfl_xor_sync(uint32_t(-1), val, offset); |
| 60 | + } |
| 61 | + |
| 62 | + constexpr size_t custom_max(std::initializer_list<size_t> ilist) |
| 63 | + { |
| 64 | + return std::max(ilist); |
| 65 | + } |
| 66 | + |
| 67 | + template<typename T> |
| 68 | + constexpr T constexpr_min(T a, T b) { |
| 69 | + return std::min(a, b); |
| 70 | + } |
| 71 | + |
| 72 | +#else |
| 73 | + #include <hip/hip_bf16.h> |
| 74 | + |
| 75 | + template<typename T> |
| 76 | + __device__ inline T shuffle_xor(T val, int offset) { |
| 77 | + return __shfl_xor(val, offset); |
| 78 | + } |
| 79 | + constexpr size_t custom_max(std::initializer_list<size_t> ilist) |
| 80 | + { |
| 81 | + return *std::max_element(ilist.begin(), ilist.end()); |
| 82 | + } |
| 83 | + |
| 84 | + template<typename T> |
| 85 | + constexpr T constexpr_min(T a, T b) { |
| 86 | + return a < b ? a : b; |
| 87 | + } |
| 88 | +#endif |
| 89 | + |
| 90 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 91 | + |
| 92 | +template<int BYTES> struct BytesToType {}; |
| 93 | + |
| 94 | +template<> struct BytesToType<16> { |
| 95 | + using Type = uint4; |
| 96 | + static_assert(sizeof(Type) == 16); |
| 97 | +}; |
| 98 | + |
| 99 | +template<> struct BytesToType<8> { |
| 100 | + using Type = uint64_t; |
| 101 | + static_assert(sizeof(Type) == 8); |
| 102 | +}; |
| 103 | + |
| 104 | +template<> struct BytesToType<4> { |
| 105 | + using Type = uint32_t; |
| 106 | + static_assert(sizeof(Type) == 4); |
| 107 | +}; |
| 108 | + |
| 109 | +template<> struct BytesToType<2> { |
| 110 | + using Type = uint16_t; |
| 111 | + static_assert(sizeof(Type) == 2); |
| 112 | +}; |
| 113 | + |
| 114 | +template<> struct BytesToType<1> { |
| 115 | + using Type = uint8_t; |
| 116 | + static_assert(sizeof(Type) == 1); |
| 117 | +}; |
| 118 | + |
| 119 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 120 | + |
| 121 | +template<typename T> |
| 122 | +struct SumOp { |
| 123 | +__device__ inline T operator()(T const & x, T const & y) { return x + y; } |
| 124 | +}; |
| 125 | + |
| 126 | +template<int THREADS> |
| 127 | +struct Allreduce { |
| 128 | + static_assert(THREADS == 32 || THREADS == 16 || THREADS == 8 || THREADS == 4); |
| 129 | + template<typename T, typename Operator> |
| 130 | + static __device__ inline T run(T x, Operator &op) { |
| 131 | + constexpr int OFFSET = THREADS / 2; |
| 132 | + x = op(x, __shfl_xor_sync(uint32_t(-1), x, OFFSET)); |
| 133 | + return Allreduce<OFFSET>::run(x, op); |
| 134 | + } |
| 135 | +}; |
| 136 | + |
| 137 | +template<> |
| 138 | +struct Allreduce<2> { |
| 139 | +template<typename T, typename Operator> |
| 140 | +static __device__ inline T run(T x, Operator &op) { |
| 141 | + x = op(x, __shfl_xor_sync(uint32_t(-1), x, 1)); |
| 142 | + return x; |
| 143 | +} |
| 144 | +}; |
0 commit comments