char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

CrispStrobeCUDA: handle OW > 65535 in im2col (2D and 3D) (#22944)8e1f9d083

char/aliceai-t5-moe
12.4 KiB267 linesraw
1#include "im2col.cuh"
2
3#define MAX_GRIDDIM_Y 65535
4#define MAX_GRIDDIM_Z 65535
5
6template <typename T>
7static  __global__ void im2col_kernel(
8        const float * x, T * dst,
9        int64_t IC, int64_t IW, int64_t IH, int64_t OH, int64_t OW, int64_t KW, int64_t KH,
10        int64_t IC_IH_IW, int64_t IH_IW, int64_t N_OH, int64_t KH_KW, int64_t IC_KH_KW,
11        int s0, int s1, int p0, int p1, int d0, int d1) {
12    const int64_t i = threadIdx.x + blockIdx.x * blockDim.x;
13    if (i >= IC_KH_KW) {
14        return;
15    }
16
17    const int64_t iic = i / (KH_KW);
18    const int64_t rem = i - iic * KH_KW;
19    const int64_t ikh = rem / KW;
20    const int64_t ikw = rem - ikh * KW;
21
22    for (int64_t iow = blockIdx.y; iow < OW; iow += MAX_GRIDDIM_Y) {
23        for (int64_t iz = blockIdx.z; iz < N_OH; iz += MAX_GRIDDIM_Z) {
24            const int64_t  in = iz / OH;
25            const int64_t  ioh = iz - in * OH;
26
27            const int64_t iiw = iow * s0 + ikw * d0 - p0;
28            const int64_t iih = ioh * s1 + ikh * d1 - p1;
29
30            const int64_t offset_dst =
31                ((in * OH + ioh) * OW + iow) * IC_KH_KW + iic * KH_KW + ikh * KW + ikw;
32
33            if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
34                dst[offset_dst] = 0.0f;
35            } else {
36                const int64_t offset_src = iic * IC_IH_IW + in * IH_IW;
37                dst[offset_dst] = x[offset_src + iih * IW + iiw];
38            }
39        }
40    }
41
42    GGML_UNUSED(IC);
43    GGML_UNUSED(KH);
44}
45
46// im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW]
47template <typename T>
48static void im2col_cuda(const float * x, T* dst,
49    int64_t IW, int64_t IH, int64_t OW, int64_t OH, int64_t KW, int64_t KH, int64_t IC,
50    int64_t N, int64_t IC_IH_IW, int64_t IH_IW,
51    int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
52    const int64_t IC_KH_KW = IC * KH * KW;
53    const int64_t num_blocks = (IC_KH_KW + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
54    const int64_t N_OH = N * OH;
55    const int64_t KH_KW = KW*KH;
56    dim3 block_nums(num_blocks, MIN(OW, MAX_GRIDDIM_Y), MIN(N_OH, MAX_GRIDDIM_Z));
57    im2col_kernel<<<block_nums, MIN(IC_KH_KW, CUDA_IM2COL_BLOCK_SIZE) , 0, stream>>>(x, dst, IC, IW, IH, OH, OW, KW, KH,
58                                                                                     IC_IH_IW, IH_IW, N_OH, KH_KW, IC_KH_KW,
59                                                                                     s0, s1, p0, p1, d0, d1);
60}
61
62static void im2col_cuda_f16(const float * x, half * dst,
63    int64_t IW, int64_t IH, int64_t OW, int64_t OH, int64_t KW, int64_t KH, int64_t IC,
64    int64_t N, int64_t IC_IH_IW, int64_t IH_IW,
65    int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
66
67    im2col_cuda<half>(x, dst, IW, IH, OW, OH, KW, KH, IC, N, IC_IH_IW, IH_IW, s0, s1, p0, p1, d0, d1, stream);
68}
69
70static void im2col_cuda_f32(const float * x, float * dst,
71    int64_t IW, int64_t IH, int64_t OW, int64_t OH, int64_t KW, int64_t KH, int64_t IC,
72    int64_t N, int64_t IC_IH_IW, int64_t IH_IW,
73    int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
74
75    im2col_cuda<float>(x, dst, IW, IH, OW, OH, KW, KH, IC, N, IC_IH_IW, IH_IW, s0, s1, p0, p1, d0, d1, stream);
76}
77
78void ggml_cuda_op_im2col(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
79    const ggml_tensor * src0 = dst->src[0];
80    const ggml_tensor * src1 = dst->src[1];
81    const float * src1_d = (const float *)src1->data;
82    float * dst_d = (float *)dst->data;
83    cudaStream_t stream = ctx.stream();
84
85    GGML_ASSERT(src1->type == GGML_TYPE_F32);
86    GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
87
88    const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
89    const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
90    const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
91    const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
92    const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
93    const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
94
95    const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
96
97    const int64_t IC = src1->ne[is_2D ? 2 : 1];
98    const int64_t IH = is_2D ? src1->ne[1] : 1;
99    const int64_t IW =         src1->ne[0];
100
101    const int64_t KH = is_2D ? src0->ne[1] : 1;
102    const int64_t KW =         src0->ne[0];
103
104    const int64_t OH = is_2D ? dst->ne[2] : 1;
105    const int64_t OW =         dst->ne[1];
106
107    const int64_t IC_IH_IW = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
108    const int64_t N        = src1->ne[is_2D ? 3 : 2];
109    const int64_t IH_IW    = src1->nb[is_2D ? 3 : 2] / 4; // nb is byte offset, src is type float32
110
111    if(dst->type == GGML_TYPE_F16) {
112        im2col_cuda_f16(src1_d, (half *) dst_d, IW, IH, OW, OH, KW, KH, IC, N, IC_IH_IW, IH_IW, s0, s1, p0, p1, d0, d1, stream);
113    } else {
114        im2col_cuda_f32(src1_d, (float *) dst_d, IW, IH, OW, OH, KW, KH, IC, N, IC_IH_IW, IH_IW, s0, s1, p0, p1, d0, d1, stream);
115    }
116}
117
118// [N*IC, ID, IH, IW] => [N*OD, OH, OW, IC * KD * KH * KW]
119template <typename T>
120static  __global__ void im2col_3d_kernel(
121        const float * src, T * dst,
122        int64_t N, int64_t IC, int64_t ID, int64_t IH, int64_t IW, int64_t OC,
123        int64_t KD, int64_t KH, int64_t KW, int64_t OD, int64_t OH, int64_t OW,
124        int64_t OH_OW, int64_t KD_KH_KW, int64_t ID_IH_IW, int64_t KH_KW, int64_t IH_IW, int64_t IC_ID_IH_IW,
125        int64_t IC_KD_KH_KW, int64_t OW_KD_KH_KW, int64_t OD_OH_OW_IC_KD_KH_KW, int64_t OH_OW_IC_KD_KH_KW,
126        int64_t OW_IC_KD_KH_KW, int64_t N_OD_OH, int64_t OD_OH,
127        int64_t stride_q, int64_t stride_z, int64_t stride_y, int64_t stride_x,
128        int s0, int s1, int s2, int p0, int p1, int p2, int d0, int d1, int d2) {
129    const int64_t i = threadIdx.x + blockIdx.x * blockDim.x;
130    if (i >= IC_KD_KH_KW) {
131        return;
132    }
133    GGML_UNUSED(N); GGML_UNUSED(OC); GGML_UNUSED(OH_OW); GGML_UNUSED(OD); GGML_UNUSED(OW); GGML_UNUSED(KD); GGML_UNUSED(KH);
134    GGML_UNUSED(ID_IH_IW); GGML_UNUSED(IH_IW); GGML_UNUSED(IC_ID_IH_IW); GGML_UNUSED(OW_KD_KH_KW);
135
136    const int64_t iic = i / KD_KH_KW;
137    const int64_t ikd = (i - iic * KD_KH_KW) / KH_KW;
138    const int64_t ikh = (i - iic * KD_KH_KW - ikd * KH_KW) / KW;
139    const int64_t ikw = i % KW;
140
141    for (int64_t iow = blockIdx.y; iow < OW; iow += MAX_GRIDDIM_Y) {
142        for (int64_t iz = blockIdx.z; iz < N_OD_OH; iz += MAX_GRIDDIM_Z) {
143            const int64_t in  = iz / OD_OH;
144            const int64_t iod = (iz - in*OD_OH) / OH;
145            const int64_t ioh = iz % OH;
146
147            const int64_t iiw = iow * s0 + ikw * d0 - p0;
148            const int64_t iih = ioh * s1 + ikh * d1 - p1;
149            const int64_t iid = iod * s2 + ikd * d2 - p2;
150
151            const int64_t offset_dst = in*OD_OH_OW_IC_KD_KH_KW + iod*OH_OW_IC_KD_KH_KW + ioh*OW_IC_KD_KH_KW + iow*IC_KD_KH_KW + iic*KD_KH_KW + ikd * KH_KW + ikh*KW + ikw;
152
153            if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW || iid < 0 || iid >= ID) {
154                dst[offset_dst] = 0.0f;
155            } else {
156                const int64_t offset_src = ((in * IC + iic) * stride_q) + (iid * stride_z) + (iih * stride_y) + (iiw * stride_x);
157                dst[offset_dst] = src[offset_src];
158            }
159        }
160    }
161}
162
163// [N*IC, ID, IH, IW] => [N*OD, OH, OW, IC * KD * KH * KW]
164template <typename T>
165static void im2col_3d_cuda(const float * src, T* dst,
166    int64_t N, int64_t IC, int64_t ID, int64_t IH, int64_t IW, int64_t OC,
167    int64_t KD, int64_t KH, int64_t KW, int64_t OD, int64_t OH, int64_t OW,
168    int64_t stride_q, int64_t stride_z, int64_t stride_y, int64_t stride_x,
169    int s0, int s1, int s2, int p0, int p1, int p2, int d0, int d1, int d2, cudaStream_t stream) {
170    const int64_t OH_OW = OH*OW;
171    const int64_t KD_KH_KW = KD*KH*KW;
172    const int64_t ID_IH_IW = ID*IH*IW;
173    const int64_t KH_KW = KH*KW;
174    const int64_t IH_IW = IH*IW;
175    const int64_t IC_KD_KH_KW = IC*KD*KH*KW;
176    const int64_t OW_KD_KH_KW = OW*KD*KH*KW;
177    const int64_t N_OD_OH = N*OD*OH;
178    const int64_t OD_OH = OD*OH;
179    const int64_t IC_ID_IH_IW = IC*ID*IH*IW;
180    const int64_t OD_OH_OW_IC_KD_KH_KW = OD*OH*OW*IC*KD*KH*KW;
181    const int64_t OH_OW_IC_KD_KH_KW = OH*OW*IC*KD*KH*KW;
182    const int64_t OW_IC_KD_KH_KW = OW*IC*KD*KH*KW;
183    const int64_t num_blocks = (IC_KD_KH_KW + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
184    dim3 block_nums(num_blocks, MIN(OW, MAX_GRIDDIM_Y), MIN(N_OD_OH, MAX_GRIDDIM_Z));
185    im2col_3d_kernel<<<block_nums, MIN(IC_KD_KH_KW, CUDA_IM2COL_BLOCK_SIZE) , 0, stream>>>(src, dst, N, IC, ID, IH, IW, OC, KD, KH, KW, OD, OH, OW,
186                                                                                           OH_OW, KD_KH_KW, ID_IH_IW, KH_KW, IH_IW, IC_ID_IH_IW,
187                                                                                           IC_KD_KH_KW, OW_KD_KH_KW, OD_OH_OW_IC_KD_KH_KW,
188                                                                                           OH_OW_IC_KD_KH_KW, OW_IC_KD_KH_KW, N_OD_OH, OD_OH,
189                                                                                           stride_q, stride_z, stride_y, stride_x,
190                                                                                           s0, s1, s2, p0, p1, p2, d0, d1, d2);
191}
192
193static void im2col_3d_cuda_f16(const float * src, half * dst,
194    int64_t N, int64_t IC, int64_t ID, int64_t IH, int64_t IW, int64_t OC,
195    int64_t KD, int64_t KH, int64_t KW, int64_t OD, int64_t OH, int64_t OW,
196    int64_t stride_q, int64_t stride_z, int64_t stride_y, int64_t stride_x,
197    int s0, int s1, int s2, int p0, int p1, int p2, int d0, int d1, int d2, cudaStream_t stream) {
198
199    im2col_3d_cuda<half>(src, dst, N, IC, ID, IH, IW, OC, KD, KH, KW, OD, OH, OW,
200                         stride_q, stride_z, stride_y, stride_x,
201                         s0, s1, s2, p0, p1, p2, d0, d1, d2, stream);
202}
203
204static void im2col_3d_cuda_f32(const float * src, float * dst,
205    int64_t N, int64_t IC, int64_t ID, int64_t IH, int64_t IW, int64_t OC,
206    int64_t KD, int64_t KH, int64_t KW, int64_t OD, int64_t OH, int64_t OW,
207    int64_t stride_q, int64_t stride_z, int64_t stride_y, int64_t stride_x,
208    int s0, int s1, int s2, int p0, int p1, int p2, int d0, int d1, int d2, cudaStream_t stream) {
209
210    im2col_3d_cuda<float>(src, dst, N, IC, ID, IH, IW, OC, KD, KH, KW, OD, OH, OW,
211                          stride_q, stride_z, stride_y, stride_x,
212                          s0, s1, s2, p0, p1, p2, d0, d1, d2, stream);
213}
214
215void ggml_cuda_op_im2col_3d(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
216    const ggml_tensor * src0 = dst->src[0];
217    const ggml_tensor * src1 = dst->src[1];
218    const float * src1_d = (const float *)src1->data;
219    float * dst_d = (float *)dst->data;
220    cudaStream_t stream = ctx.stream();
221
222    GGML_ASSERT(src1->type == GGML_TYPE_F32);
223    GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
224
225    GGML_TENSOR_BINARY_OP_LOCALS
226
227    const int32_t s0 = ((const int32_t *)(dst->op_params))[0];
228    const int32_t s1 = ((const int32_t *)(dst->op_params))[1];
229    const int32_t s2 = ((const int32_t *)(dst->op_params))[2];
230    const int32_t p0 = ((const int32_t *)(dst->op_params))[3];
231    const int32_t p1 = ((const int32_t *)(dst->op_params))[4];
232    const int32_t p2 = ((const int32_t *)(dst->op_params))[5];
233    const int32_t d0 = ((const int32_t *)(dst->op_params))[6];
234    const int32_t d1 = ((const int32_t *)(dst->op_params))[7];
235    const int32_t d2 = ((const int32_t *)(dst->op_params))[8];
236    const int32_t IC = ((const int32_t *)(dst->op_params))[9];
237
238    const int64_t N  = ne13 / IC;
239    const int64_t ID = ne12;
240    const int64_t IH = ne11;
241    const int64_t IW = ne10;
242
243    const int64_t OC = ne03 / IC;
244    const int64_t KD = ne02;
245    const int64_t KH = ne01;
246    const int64_t KW = ne00;
247
248    const int64_t OD = ne3 / N;
249    const int64_t OH = ne2;
250    const int64_t OW = ne1;
251
252    const size_t  es       = ggml_element_size(src1);
253    const int64_t stride_x = src1->nb[0] / es;
254    const int64_t stride_y = src1->nb[1] / es;
255    const int64_t stride_z = src1->nb[2] / es;
256    const int64_t stride_q = src1->nb[3] / es;
257
258    if(dst->type == GGML_TYPE_F16) {
259        im2col_3d_cuda_f16(src1_d, (half *) dst_d, N, IC, ID, IH, IW, OC, KD, KH, KW, OD, OH, OW,
260                           stride_q, stride_z, stride_y, stride_x,
261                           s0, s1, s2, p0, p1, p2, d0, d1, d2, stream);
262    } else {
263        im2col_3d_cuda_f32(src1_d, (float *) dst_d, N, IC, ID, IH, IW, OC, KD, KH, KW, OD, OH, OW,
264                           stride_q, stride_z, stride_y, stride_x,
265                           s0, s1, s2, p0, p1, p2, d0, d1, d2, stream);
266    }
267}