ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-cuda / pad.cu
| 1 | #include "pad.cuh" |
| 2 | |
| 3 | #include <stdint.h> |
| 4 | |
| 5 | __device__ __forceinline__ int64_t wrap_around(int64_t coord, int64_t size) { |
| 6 | // + size ensures negatives are handled properly |
| 7 | return (coord + size) % size; |
| 8 | } |
| 9 | |
| 10 | static __global__ void pad_f32(const float * src, size_t s00, size_t s01, size_t s02, size_t s03, float * dst, |
| 11 | const int lp0, const int rp0, const int lp1, const int rp1, |
| 12 | const int lp2, const int rp2, const int lp3, const int rp3, |
| 13 | const int ne0, const int ne1, const int ne2, const int ne3, |
| 14 | const bool circular) { |
| 15 | // blockIdx.z: i3*ne2+i2 |
| 16 | // blockIdx.y: i1 |
| 17 | // blockIDx.x: i0 / CUDA_PAD_BLOCK_SIZE |
| 18 | // gridDim.y and gridDim.z are capped at 65535, blocks stride over larger ne1 and ne2*ne3 |
| 19 | int i0 = threadIdx.x + blockIdx.x * blockDim.x; |
| 20 | if (i0 >= ne0) { |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | for (int i1 = blockIdx.y; i1 < ne1; i1 += gridDim.y) { |
| 25 | for (int i23 = blockIdx.z; i23 < ne2 * ne3; i23 += gridDim.z) { |
| 26 | int i2 = i23 % ne2; |
| 27 | int i3 = i23 / ne2; |
| 28 | |
| 29 | const int64_t dst_idx = i3 * (ne0 * ne1 * ne2) + i2 * (ne0 * ne1) + i1 * ne0 + i0; |
| 30 | |
| 31 | if (!circular) { |
| 32 | if ((i0 >= lp0 && i0 < ne0 - rp0) && (i1 >= lp1 && i1 < ne1 - rp1) && (i2 >= lp2 && i2 < ne2 - rp2) && |
| 33 | (i3 >= lp3 && i3 < ne3 - rp3)) { |
| 34 | const int64_t i00 = i0 - lp0; |
| 35 | const int64_t i01 = i1 - lp1; |
| 36 | const int64_t i02 = i2 - lp2; |
| 37 | const int64_t i03 = i3 - lp3; |
| 38 | |
| 39 | const int64_t src_idx = i03 * s03 + i02 * s02 + i01 * s01 + i00 * s00; |
| 40 | |
| 41 | dst[dst_idx] = src[src_idx]; |
| 42 | } else { |
| 43 | dst[dst_idx] = 0.0f; |
| 44 | } |
| 45 | } |
| 46 | // circular means on a torus, so x and y wrap around |
| 47 | else { |
| 48 | const int64_t ne00 = ne0 - lp0 - rp0; |
| 49 | const int64_t ne01 = ne1 - lp1 - rp1; |
| 50 | const int64_t ne02 = ne2 - lp2 - rp2; |
| 51 | const int64_t ne03 = ne3 - lp3 - rp3; |
| 52 | |
| 53 | const int64_t i00 = wrap_around(i0 - lp0, ne00); |
| 54 | const int64_t i01 = wrap_around(i1 - lp1, ne01); |
| 55 | const int64_t i02 = wrap_around(i2 - lp2, ne02); |
| 56 | const int64_t i03 = wrap_around(i3 - lp3, ne03); |
| 57 | |
| 58 | const int64_t src_idx = i03 * s03 + i02 * s02 + i01 * s01 + i00 * s00; |
| 59 | |
| 60 | dst[dst_idx] = src[src_idx]; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | |
| 67 | static void pad_f32_cuda(const float * src, size_t s00, size_t s01, size_t s02, size_t s03, float * dst, |
| 68 | const int lp0, const int rp0, const int lp1, const int rp1, |
| 69 | const int lp2, const int rp2, const int lp3, const int rp3, |
| 70 | const int ne0, const int ne1, const int ne2, const int ne3, |
| 71 | const bool circular, cudaStream_t stream) { |
| 72 | int num_blocks = (ne0 + CUDA_PAD_BLOCK_SIZE - 1) / CUDA_PAD_BLOCK_SIZE; |
| 73 | dim3 gridDim(num_blocks, std::min(ne1, 65535), std::min(ne2 * ne3, 65535)); |
| 74 | pad_f32<<<gridDim, CUDA_PAD_BLOCK_SIZE, 0, stream>>>(src, s00, s01, s02, s03, dst, |
| 75 | lp0, rp0, lp1, rp1, lp2, rp2, lp3, rp3, |
| 76 | ne0, ne1, ne2, ne3, circular); |
| 77 | } |
| 78 | |
| 79 | void ggml_cuda_op_pad(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { |
| 80 | const ggml_tensor * src0 = dst->src[0]; |
| 81 | const float * src0_d = (const float *) src0->data; |
| 82 | float * dst_d = (float *) dst->data; |
| 83 | cudaStream_t stream = ctx.stream(); |
| 84 | |
| 85 | GGML_TENSOR_UNARY_OP_LOCALS; |
| 86 | |
| 87 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 88 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 89 | |
| 90 | const int32_t lp0 = ((const int32_t *) (dst->op_params))[0]; |
| 91 | const int32_t rp0 = ((const int32_t *) (dst->op_params))[1]; |
| 92 | const int32_t lp1 = ((const int32_t *) (dst->op_params))[2]; |
| 93 | const int32_t rp1 = ((const int32_t *) (dst->op_params))[3]; |
| 94 | const int32_t lp2 = ((const int32_t *) (dst->op_params))[4]; |
| 95 | const int32_t rp2 = ((const int32_t *) (dst->op_params))[5]; |
| 96 | const int32_t lp3 = ((const int32_t *) (dst->op_params))[6]; |
| 97 | const int32_t rp3 = ((const int32_t *) (dst->op_params))[7]; |
| 98 | const int32_t circular = ((const int32_t *) (dst->op_params))[8]; |
| 99 | |
| 100 | const size_t s00 = nb00 / ggml_type_size(src0->type); |
| 101 | const size_t s01 = nb01 / ggml_type_size(src0->type); |
| 102 | const size_t s02 = nb02 / ggml_type_size(src0->type); |
| 103 | const size_t s03 = nb03 / ggml_type_size(src0->type); |
| 104 | |
| 105 | pad_f32_cuda(src0_d, s00, s01, s02, s03, dst_d, |
| 106 | lp0, rp0, lp1, rp1, lp2, rp2, lp3, rp3, |
| 107 | dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], |
| 108 | (bool) circular, stream); |
| 109 | } |