okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-1b (033cfdd8c) into f-own-int: nothing downloads llama.cppb2de2641f

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