|
| 1 | +#include "ssm_scan.cuh" |
| 2 | + |
| 3 | +template <int block_size> |
| 4 | +static __global__ void ssm_scan_f32( |
| 5 | + const float * src0, const float * src1, const float * src2, const float * src3, |
| 6 | + const float * src4, const float * src5, const float * src6, |
| 7 | + const int src0_nb1, const int src0_nb2, |
| 8 | + const int src1_nb0, const int src1_nb1, const int src1_nb2, |
| 9 | + const int src2_nb0, const int src2_nb1, |
| 10 | + const int src3_nb1, |
| 11 | + const int src4_nb1, |
| 12 | + const int src5_nb1, |
| 13 | + const int src6_nb1, |
| 14 | + float * dst, |
| 15 | + const int nc, const int nr, const int n_t, const int n_kv) { |
| 16 | + |
| 17 | +// const int row = blockIdx.x*blockDim.y + threadIdx.y; |
| 18 | + const int tid = threadIdx.x; |
| 19 | + |
| 20 | + const int ith = tid; |
| 21 | + const int nth = WARP_SIZE; |
| 22 | + |
| 23 | + // rows per thread |
| 24 | + const int dr = (nr + nth - 1)/nth; |
| 25 | + |
| 26 | + // row range for this thread |
| 27 | + const int ir0 = dr*ith; |
| 28 | + const int ir1 = min(ir0 + dr, nr); |
| 29 | + const int ir = ir1 - ir0; |
| 30 | + |
| 31 | + if (n_kv > 1) { |
| 32 | + // it's hard to know if the source states have already been copied |
| 33 | + // when there are multiple, so copy them already. |
| 34 | + for (int i3 = 0; i3 < n_kv; ++i3) { |
| 35 | + float * s0 = (float *) ((char *) src0 + ir0*src0_nb1 + i3*src0_nb2); |
| 36 | + float * s = (float *) ((char *) dst + ir0*src0_nb1 + i3*src0_nb2 + src1_nb2); |
| 37 | + |
| 38 | + //memcpy(s, s0, nc*ir*sizeof(float)); |
| 39 | + for (int i4 = 0; i4 < nc*ir; i4++) { |
| 40 | + s[i4] = s0[i4]; |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + for (int i2 = 0; i2 < n_t; ++i2) { |
| 46 | + int32_t * sq = (int32_t *) ((char *) src6 + i2*src6_nb1); // {n_kv, n_tokens} |
| 47 | + float * y = (float *) ((char *) dst + ir0*src1_nb0 + i2*src1_nb1); // {d_inner, n_tokens} |
| 48 | + float * s = (float *) ((char *) dst + ir0*src0_nb1 + sq[0]*src0_nb2 + src1_nb2); // {d_state, d_inner, n_kv} |
| 49 | + float * s0; |
| 50 | + float * x = (float *) ((char *) src1 + ir0*src1_nb0 + i2*src1_nb1); // {d_inner, n_tokens} |
| 51 | + float * dt = (float *) ((char *) src2 + ir0*src2_nb0 + i2*src2_nb1); // {d_inner, n_tokens} |
| 52 | + float * A = (float *) ((char *) src3 + ir0*src3_nb1); // {d_state, d_inner} |
| 53 | + float * B = (float *) ((char *) src4 + i2*src4_nb1); // {d_state, n_tokens} |
| 54 | + float * C = (float *) ((char *) src5 + i2*src5_nb1); // {d_state, n_tokens} |
| 55 | + |
| 56 | + // avoid needing to copy the state for the first token |
| 57 | + if (i2 == 0) { |
| 58 | + s0 = (float *) ((char *) src0 + ir0*(src0_nb1) + sq[0]*src0_nb2); // {d_state, d_inner, n_kv} |
| 59 | + } else { |
| 60 | + // otherwise the source is the same as the destination |
| 61 | + s0 = s; |
| 62 | + } |
| 63 | + |
| 64 | + // d_inner |
| 65 | + for (int i1 = 0; i1 < ir; ++i1) { |
| 66 | + // ref: https://github.com/state-spaces/mamba/blob/34076d664838588a3c97727b263478ab9f621a07/mamba_ssm/ops/triton/selective_state_update.py#L78 |
| 67 | + float dt_soft_plus = dt[i1] <= 20.0f ? log1pf(expf(dt[i1])) : dt[i1]; |
| 68 | + float x_dt = x[i1] * dt_soft_plus; |
| 69 | + float sumf = 0.0f; |
| 70 | + // d_state |
| 71 | + for (int i0 = 0; i0 < nc; ++i0) { |
| 72 | + int i = i0 + i1*nc; |
| 73 | + // state = prev_state * dA + dB * x |
| 74 | + float state = (s0[i] * expf(dt_soft_plus * A[i])) + (B[i0] * x_dt); |
| 75 | + // y = rowwise_dotprod(state, C) |
| 76 | + sumf += state * C[i0]; |
| 77 | + s[i] = state; |
| 78 | + } |
| 79 | + y[i1] = sumf; |
| 80 | + } |
| 81 | + |
| 82 | + // handle copies when there are multiple output states |
| 83 | + for (int i3 = 1; i3 < n_kv; ++i3) { |
| 84 | + int32_t seq = sq[i3]; |
| 85 | + if (0 <= seq && seq < n_kv) { |
| 86 | + float * s1 = s + (seq - sq[0])*nc*nr; |
| 87 | + //memcpy(s1, s, nc*ir*sizeof(float)); |
| 88 | + for (int i4 = 0; i4 < nc*ir; i4++) { |
| 89 | + s1[i4] = s[i4]; |
| 90 | + } |
| 91 | + } else { |
| 92 | + // stop at negative or too big seq_ids |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +static void ssm_scan_f32_cuda( |
| 100 | + const float * src0, const float * src1, const float * src2, const float * src3, |
| 101 | + const float * src4, const float * src5, const float * src6, |
| 102 | + const int src0_nb1, const int src0_nb2, |
| 103 | + const int src1_nb0, const int src1_nb1, const int src1_nb2, |
| 104 | + const int src2_nb0, const int src2_nb1, |
| 105 | + const int src3_nb1, |
| 106 | + const int src4_nb1, |
| 107 | + const int src5_nb1, |
| 108 | + const int src6_nb1, |
| 109 | + float * dst, |
| 110 | + const int nc, const int nr, const int n_t, const int n_kv, cudaStream_t stream) { |
| 111 | + |
| 112 | + const dim3 block_dims(WARP_SIZE, 1, 1); |
| 113 | + const int nblocks = 1; // TODO |
| 114 | + |
| 115 | + ssm_scan_f32<WARP_SIZE><<<nblocks, block_dims, 0, stream>>>( |
| 116 | + src0, src1, src2, src3, src4, src5, src6, |
| 117 | + src0_nb1, src0_nb2, |
| 118 | + src1_nb0, src1_nb1, src1_nb2, |
| 119 | + src2_nb0, src2_nb1, |
| 120 | + src3_nb1, |
| 121 | + src4_nb1, |
| 122 | + src5_nb1, |
| 123 | + src6_nb1, |
| 124 | + dst, |
| 125 | + nc, nr, n_t, n_kv); |
| 126 | +} |
| 127 | + |
| 128 | +void ggml_cuda_op_ssm_scan(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { |
| 129 | + const struct ggml_tensor * src0 = dst->src[0]; // s |
| 130 | + const struct ggml_tensor * src1 = dst->src[1]; // x |
| 131 | + const struct ggml_tensor * src2 = dst->src[2]; // dt |
| 132 | + const struct ggml_tensor * src3 = dst->src[3]; // A |
| 133 | + const struct ggml_tensor * src4 = dst->src[4]; // B |
| 134 | + const struct ggml_tensor * src5 = dst->src[5]; // C |
| 135 | + const struct ggml_tensor * src6 = dst->src[6]; // sq |
| 136 | + |
| 137 | + const int64_t nc = src0->ne[0]; // d_state |
| 138 | + const int64_t nr = src0->ne[1]; // d_inner |
| 139 | + const int64_t n_t = src1->ne[1]; // number of tokens in the batch |
| 140 | + const int64_t n_kv = src0->ne[2]; // max number of sequences in the batch |
| 141 | + |
| 142 | + GGML_ASSERT(ggml_nelements(src1) + ggml_nelements(src0) == ggml_nelements(dst)); |
| 143 | + GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 144 | + GGML_ASSERT(src1->nb[0] == sizeof(float)); |
| 145 | + GGML_ASSERT(src2->nb[0] == sizeof(float)); |
| 146 | + GGML_ASSERT(src3->nb[0] == sizeof(float)); |
| 147 | + GGML_ASSERT(src4->nb[0] == sizeof(float)); |
| 148 | + GGML_ASSERT(src5->nb[0] == sizeof(float)); |
| 149 | + // required for the dot product between s and C, and when copying the states |
| 150 | + GGML_ASSERT(src0->nb[1] == src0->ne[0]*sizeof(float)); |
| 151 | + // required for per-sequence offsets for states |
| 152 | + GGML_ASSERT(src0->nb[2] == src0->ne[0]*src0->ne[1]*sizeof(float)); |
| 153 | + // required to get correct offset for state destination (i.e. src1->nb[2]) |
| 154 | + GGML_ASSERT(src1->nb[2] == src1->ne[0]*src1->ne[1]*sizeof(float)); |
| 155 | + |
| 156 | + const float * src0_d = (const float *)src0->data; |
| 157 | + const float * src1_d = (const float *)src1->data; |
| 158 | + const float * src2_d = (const float *)src2->data; |
| 159 | + const float * src3_d = (const float *)src3->data; |
| 160 | + const float * src4_d = (const float *)src4->data; |
| 161 | + const float * src5_d = (const float *)src5->data; |
| 162 | + const float * src6_d = (const float *)src6->data; |
| 163 | + float * dst_d = (float *)dst->data; |
| 164 | + cudaStream_t stream = ctx.stream(); |
| 165 | + |
| 166 | + GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 167 | + GGML_ASSERT( dst->type == GGML_TYPE_F32); |
| 168 | + |
| 169 | + ssm_scan_f32_cuda( |
| 170 | + src0_d, src1_d, src2_d, src3_d, src4_d, src5_d, src6_d, |
| 171 | + src0->nb[1], src0->nb[2], |
| 172 | + src1->nb[0], src1->nb[1], src1->nb[2], |
| 173 | + src2->nb[0], src2->nb[1], |
| 174 | + src3->nb[1], |
| 175 | + src4->nb[1], |
| 176 | + src5->nb[1], |
| 177 | + src6->nb[1], |
| 178 | + dst_d, |
| 179 | + nc, nr, n_t, n_kv, stream); |
| 180 | +} |
0 commit comments