Skip to content

Commit d4fe517

Browse files
committed
Make everything const, use __restrict__
1 parent 6d569f4 commit d4fe517

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

ggml/src/ggml-cuda/roll.cu

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "ggml-cuda/common.cuh"
22
#include "roll.cuh"
33

4-
static __forceinline__ __device__ int64_t wrap_index(int64_t idx, int64_t ne) {
4+
static __forceinline__ __device__ int64_t wrap_index(const int64_t idx, const int64_t ne) {
55
if (idx < 0) {
66
return idx + ne;
77
}
@@ -11,32 +11,32 @@ static __forceinline__ __device__ int64_t wrap_index(int64_t idx, int64_t ne) {
1111
return idx;
1212
}
1313

14-
static __global__ void roll_f32_cuda(const float * src,
15-
float * dst,
16-
int64_t ne00,
17-
int64_t ne01,
18-
int64_t ne02,
19-
int64_t ne03,
20-
int s0,
21-
int s1,
22-
int s2,
23-
int s3) {
24-
int64_t idx = int64_t(blockDim.x) * blockIdx.x + threadIdx.x;
25-
int64_t n_elements = ne00 * ne01 * ne02 * ne03;
14+
static __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 int s0,
21+
const int s1,
22+
const int s2,
23+
const int s3) {
24+
const int64_t idx = int64_t(blockDim.x) * blockIdx.x + threadIdx.x;
25+
const int64_t n_elements = ne00 * ne01 * ne02 * ne03;
2626

2727
if (idx >= n_elements) {
2828
return;
2929
}
3030

31-
int64_t i0 = idx % ne00;
32-
int64_t i1 = (idx / ne00) % ne01;
33-
int64_t i2 = (idx / (ne00 * ne01)) % ne02;
34-
int64_t i3 = (idx / (ne00 * ne01 * ne02)) % ne03;
31+
const int64_t i0 = idx % ne00;
32+
const int64_t i1 = (idx / ne00) % ne01;
33+
const int64_t i2 = (idx / (ne00 * ne01)) % ne02;
34+
const int64_t i3 = (idx / (ne00 * ne01 * ne02)) % ne03;
3535

36-
int64_t d0 = wrap_index(i0 - s0, ne00);
37-
int64_t d1 = wrap_index(i1 - s1, ne01);
38-
int64_t d2 = wrap_index(i2 - s2, ne02);
39-
int64_t d3 = wrap_index(i3 - s3, ne03);
36+
const int64_t d0 = wrap_index(i0 - s0, ne00);
37+
const int64_t d1 = wrap_index(i1 - s1, ne01);
38+
const int64_t d2 = wrap_index(i2 - s2, ne02);
39+
const int64_t d3 = wrap_index(i3 - s3, ne03);
4040

4141
dst[i3 * (ne00 * ne01 * ne02) + i2 * (ne01 * ne00) + i1 * ne00 + i0] =
4242
src[d3 * (ne00 * ne01 * ne02) + d2 * (ne01 * ne00) + d1 * ne00 + d0];
@@ -62,5 +62,6 @@ void ggml_cuda_op_roll(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
6262
int64_t sz = (ne00 * ne01 * ne02 * ne03);
6363
int64_t num_blocks = (sz + CUDA_ROLL_BLOCK_SIZE - 1) / CUDA_ROLL_BLOCK_SIZE;
6464

65-
roll_f32_cuda<<<num_blocks, CUDA_ROLL_BLOCK_SIZE, 0, stream>>>(src0_d, dst_d, ne00, ne01, ne02, ne03, s0, s1, s2, s3);
65+
roll_f32_cuda<<<num_blocks, CUDA_ROLL_BLOCK_SIZE, 0, stream>>>(
66+
src0_d, dst_d, ne00, ne01, ne02, ne03, s0, s1, s2, s3);
6667
}

0 commit comments

Comments
 (0)