interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-cuda / roll.cu

71 lines · 2670 bytes Raw Blame History
1#include "ggml-cuda/common.cuh"
2#include "roll.cuh"
3
4static __forceinline__ __device__ int64_t wrap_index(const int64_t idx, const int64_t ne) {
5 if (idx < 0) {
6 return idx + ne;
7 }
8 if (idx >= ne) {
9 return idx - ne;
10 }
11 return idx;
12}
13
14static __global__ void roll_f32_cuda(const float * __restrict__ src,
15 float * __restrict__ dst,
16 const int64_t ne00,
17 const int64_t ne01,
18 const int64_t ne02,
19 const int64_t ne03,
20 const int64_t nb00,
21 const int64_t nb01,
22 const int64_t nb02,
23 const int64_t nb03,
24 const int s0,
25 const int s1,
26 const int s2,
27 const int s3) {
28 const int64_t idx = int64_t(blockDim.x) * blockIdx.x + threadIdx.x;
29 const int64_t n_elements = ne00 * ne01 * ne02 * ne03;
30
31 if (idx >= n_elements) {
32 return;
33 }
34
35 const int64_t i0 = idx % ne00;
36 const int64_t i1 = (idx / ne00) % ne01;
37 const int64_t i2 = (idx / (ne00 * ne01)) % ne02;
38 const int64_t i3 = (idx / (ne00 * ne01 * ne02)) % ne03;
39
40 const int64_t d0 = wrap_index(i0 - s0, ne00);
41 const int64_t d1 = wrap_index(i1 - s1, ne01);
42 const int64_t d2 = wrap_index(i2 - s2, ne02);
43 const int64_t d3 = wrap_index(i3 - s3, ne03);
44
45 dst[i3 * (ne00 * ne01 * ne02) + i2 * (ne01 * ne00) + i1 * ne00 + i0] =
46 src[(d3 * nb03 + d2 * nb02 + d1 * nb01 + d0 * nb00) / sizeof(float)];
47}
48
49void ggml_cuda_op_roll(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
50 int s0 = dst->op_params[0];
51 int s1 = dst->op_params[1];
52 int s2 = dst->op_params[2];
53 int s3 = dst->op_params[3];
54
55 const ggml_tensor * src0 = dst->src[0];
56 const float * src0_d = (const float *) dst->src[0]->data;
57 float * dst_d = (float *) dst->data;
58
59 GGML_TENSOR_UNARY_OP_LOCALS;
60
61 GGML_ASSERT(dst->src[0]->type == GGML_TYPE_F32);
62 GGML_ASSERT(ggml_are_same_shape(dst->src[0], dst));
63
64 cudaStream_t stream = ctx.stream();
65
66 int64_t sz = (ne00 * ne01 * ne02 * ne03);
67 int64_t num_blocks = (sz + CUDA_ROLL_BLOCK_SIZE - 1) / CUDA_ROLL_BLOCK_SIZE;
68
69 roll_f32_cuda<<<num_blocks, CUDA_ROLL_BLOCK_SIZE, 0, stream>>>(
70 src0_d, dst_d, ne00, ne01, ne02, ne03, nb00, nb01, nb02, nb03, s0, s1, s2, s3);
71}