Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-cuda/ssm-conv.cu # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-cuda / ssm-conv.cu` 207 lines ยท 9322 bytes Raw Blame History 1 #include "common.cuh" 2 #include "ssm-conv.cuh" 3 #include "unary.cuh" 4 5 template 6 static __global__ void ssm_conv_f32(const float * src0_ptr, const float * src1_ptr, 7 const float * bias_ptr, 8 const int src0_nb0, const int src0_nb1, const int src0_nb2, const int src1_nb1, 9 float * dst_ptr, const int dst_nb0, const int dst_nb1, const int dst_nb2, 10 const int64_t n_t) { 11 ggml_cuda_pdl_lc(); 12 const float * GGML_CUDA_RESTRICT src0 = src0_ptr; 13 const float * GGML_CUDA_RESTRICT src1 = src1_ptr; 14 const float * GGML_CUDA_RESTRICT bias = bias_ptr; 15 float * GGML_CUDA_RESTRICT dst = dst_ptr; 16 GGML_UNUSED(src0_nb0); 17 const int tid = threadIdx.x; 18 const int bidx = blockIdx.x; 19 const int bidy = blockIdx.y; 20 21 const float * x_block = (const float *) ((const char *) src0 + bidx * src0_nb2 + bidy * split_d_inner * src0_nb1); 22 const float * w_block = (const float *) ((const char *) src1 + bidy * split_d_inner * src1_nb1); 23 float * y_block = (float *) ((char *) dst + bidx * dst_nb2 + bidy * split_d_inner * dst_nb0); 24 25 const int stride_x = src0_nb1 / sizeof(float); 26 const int stride_w = src1_nb1 / sizeof(float); 27 const int stride_y = dst_nb1 / sizeof(float); 28 29 float x[d_conv] = { 0.0f }; 30 float w[d_conv] = { 0.0f }; 31 32 ggml_cuda_pdl_sync(); 33 #pragma unroll 34 for (size_t j = 0; j < d_conv; j++) { 35 w[j] = w_block[tid * stride_w + j]; 36 } 37 38 float b = bias != nullptr ? bias[bidy * split_d_inner + tid] : 0.0f; 39 40 for (int64_t i = 0; i < n_t; i++) { 41 float sumf = 0.0f; 42 43 if (i == 0) { 44 for (size_t j = 0; j < d_conv; j++) { 45 x[j] = x_block[tid * stride_x + j]; 46 } 47 } else { 48 x[(i - 1) % d_conv] = x_block[tid * stride_x + i + d_conv - 1]; 49 } 50 51 #pragma unroll 52 for (size_t j = 0; j < d_conv; j++) { 53 sumf += x[(i + j) % d_conv] * w[j]; 54 } 55 sumf += b; 56 y_block[i * stride_y + tid] = apply_silu ? ggml_cuda_op_silu_single(sumf) : sumf; 57 } 58 } 59 60 template 61 static __global__ void ssm_conv_long_token_f32(const float * __restrict__ src0, const float * __restrict__ src1, 62 const float * __restrict__ bias, 63 const int src0_nb0, const int src0_nb1, const int src0_nb2, 64 const int src1_nb1, float * __restrict__ dst, const int dst_nb0, 65 const int dst_nb1, const int dst_nb2, const int64_t n_t) { 66 const int tid = threadIdx.x; 67 const int bidx = blockIdx.x; 68 const int bidy = blockIdx.y; 69 const int bidz = blockIdx.z; 70 71 const float * x_block = (const float *) ((const char *) src0 + bidx * src0_nb2 + bidy * split_d_inner * src0_nb1 + 72 bidz * split_n_t * src0_nb0); 73 const float * w_block = (const float *) ((const char *) src1 + bidy * split_d_inner * src1_nb1); 74 float * y_block = 75 (float *) ((char *) dst + bidx * dst_nb2 + bidz * split_n_t * dst_nb1 + bidy * split_d_inner * dst_nb0); 76 77 const int stride_x = src0_nb1 / sizeof(float); 78 const int stride_w = src1_nb1 / sizeof(float); 79 const int stride_y = dst_nb1 / sizeof(float); 80 81 const int64_t local_n_t = min(split_n_t, n_t - bidz * split_n_t); 82 const int n_cols = d_conv - 1 + split_n_t; 83 84 extern __shared__ float smem[]; 85 86 constexpr int load_cols = d_conv - 1 + split_n_t; 87 constexpr int total_elems = split_d_inner * load_cols; 88 int row = tid / load_cols; 89 int col = tid % load_cols; 90 #pragma unroll 91 for (int idx = 0; idx < total_elems; idx += split_d_inner) { 92 if (row < (int)split_d_inner) { 93 smem[row * n_cols + col] = x_block[row * stride_x + col]; 94 } 95 96 col += split_d_inner; 97 row += col / load_cols; 98 col = col % load_cols; 99 if (idx >= total_elems - tid - split_d_inner) { 100 break; 101 } 102 } 103 __syncthreads(); 104 105 // Load weights into registers (done once, small) 106 float w[d_conv] = { 0.0f }; 107 #pragma unroll 108 for (size_t j = 0; j < d_conv; j++) { 109 w[j] = w_block[tid * stride_w + j]; 110 } 111 112 float b = bias != nullptr ? bias[bidy * split_d_inner + tid] : 0.0f; 113 114 // Compute from shared memory 115 for (int64_t i = 0; i < local_n_t; i++) { 116 float sumf = 0.0f; 117 #pragma unroll 118 for (size_t j = 0; j < d_conv; j++) { 119 sumf += smem[tid * n_cols + i + j] * w[j]; 120 } 121 sumf += b; 122 y_block[i * stride_y + tid] = apply_silu ? ggml_cuda_op_silu_single(sumf) : sumf; 123 } 124 } 125 126 template 127 static void ssm_conv_f32_cuda(const float * src0, const float * src1, const float * bias, const int src0_nb0, const int src0_nb1, 128 const int src0_nb2, const int src1_nb1, float * dst, const int dst_nb0, const int dst_nb1, 129 const int dst_nb2, const int64_t nc, const int64_t nr, const int64_t n_t, 130 const int64_t n_s, cudaStream_t stream) { 131 const int threads = 128; 132 GGML_ASSERT(nr % threads == 0); 133 134 auto launch_kernel = [&](auto NC) { 135 constexpr int kNC = decltype(NC)::value; 136 if (n_t <= 32) { 137 const dim3 blocks(n_s, (nr + threads - 1) / threads, 1); 138 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(blocks, threads, 0, stream); 139 ggml_cuda_kernel_launch(ssm_conv_f32, launch_params, src0, src1, bias, src0_nb0, src0_nb1, 140 src0_nb2, src1_nb1, dst, dst_nb0, dst_nb1, dst_nb2, n_t); 141 } else { 142 const int64_t split_n_t = 32; 143 dim3 blocks(n_s, (nr + threads - 1) / threads, (n_t + split_n_t - 1) / split_n_t); 144 const size_t smem_size = threads * (kNC - 1 + split_n_t) * sizeof(float); 145 ssm_conv_long_token_f32<<>>( 146 src0, src1, bias, src0_nb0, src0_nb1, src0_nb2, src1_nb1, dst, dst_nb0, dst_nb1, dst_nb2, n_t); 147 } 148 }; 149 150 switch (nc) { 151 case 3: launch_kernel(std::integral_constant{}); break; 152 case 4: launch_kernel(std::integral_constant{}); break; 153 case 5: launch_kernel(std::integral_constant{}); break; 154 case 7: launch_kernel(std::integral_constant{}); break; 155 case 9: launch_kernel(std::integral_constant{}); break; 156 case 15: launch_kernel(std::integral_constant{}); break; 157 default: GGML_ABORT("Only support kernel sizes 3, 4, 5, 7, 9, 15 right now."); 158 } 159 } 160 161 void ggml_cuda_op_ssm_conv(ggml_backend_cuda_context & ctx, ggml_tensor * dst, ggml_tensor * bias_add_node, ggml_tensor * silu_dst) { 162 const struct ggml_tensor * src0 = dst->src[0]; // conv_x 163 const struct ggml_tensor * src1 = dst->src[1]; // conv1d.weight 164 const bool fuse_bias = bias_add_node != nullptr; 165 const bool fuse_silu = silu_dst != nullptr; 166 167 // bias always comes with silu. 168 GGML_ASSERT(!fuse_bias || fuse_silu); 169 170 // The bias (when fused) is the non-conv operand of the ADD node. 171 const struct ggml_tensor * bias = fuse_bias ? (bias_add_node->src[0] == dst ? bias_add_node->src[1] : bias_add_node->src[0]) : nullptr; 172 173 // When fusing, write to silu_dst (the node downstream references). 174 const struct ggml_tensor * out = fuse_silu ? silu_dst : dst; 175 176 const int64_t nc = src1->ne[0]; // d_conv 177 const int64_t nr = src0->ne[1]; // d_inner 178 const int64_t n_t = out->ne[1]; // tokens per sequence 179 const int64_t n_s = out->ne[2]; // number of sequences in the batch 180 181 GGML_ASSERT(out->ne[0] == nr); 182 GGML_ASSERT(src0->nb[0] == sizeof(float)); 183 GGML_ASSERT(src1->nb[0] == sizeof(float)); 184 GGML_ASSERT(src0->nb[1] == src0->ne[0] * sizeof(float)); 185 186 const float * src0_d = (const float *) src0->data; 187 const float * src1_d = (const float *) src1->data; 188 const float * bias_d = fuse_bias ? (const float *) bias->data : nullptr; 189 float * dst_d = (float *) out->data; 190 cudaStream_t stream = ctx.stream(); 191 192 GGML_ASSERT(src0->type == GGML_TYPE_F32); 193 GGML_ASSERT(out->type == GGML_TYPE_F32); 194 if (fuse_bias) { 195 GGML_ASSERT(bias->type == GGML_TYPE_F32); 196 GGML_ASSERT(ggml_is_contiguous(bias)); 197 GGML_ASSERT(ggml_nelements(bias) == nr); 198 } 199 200 if (fuse_silu) { 201 ssm_conv_f32_cuda(src0_d, src1_d, bias_d, src0->nb[0], src0->nb[1], src0->nb[2], src1->nb[1], dst_d, out->nb[0], out->nb[1], 202 out->nb[2], nc, nr, n_t, n_s, stream); 203 } else { 204 ssm_conv_f32_cuda(src0_d, src1_d, bias_d, src0->nb[0], src0->nb[1], src0->nb[2], src1->nb[1], dst_d, out->nb[0], out->nb[1], 205 out->nb[2], nc, nr, n_t, n_s, stream); 206 } 207 }