Repository: ggerganov/llama.cpp
File: ggml/src/ggml-cuda/pad_reflect_1d.cu (CUDA)

Code before:
    const int64_t nb3,
    const int p0,
    const int p1) {

    const int64_t i3 = blockIdx.z;
    const int64_t i2 = blockIdx.y;

    const uint2 div_mod_packed = fast_div_modulo(blockIdx.x, ne01_packed);
    const int64_t tile1  = div_mod_packed.y;     // i1
    const int64_t tile0  = div_mod_packed.x;     // nth i0 tile
    const int64_t i1     = tile1;
    const int64_t i0     = threadIdx.x + tile0 * blockDim.x;
    if (i0 >= ne0 || i1 >= ne01 || i2 >= ne02 || i3 >= ne03) {
        return;
    }

    const char * src0_ptr = (const char *)src0 + i3*nb03 + i2*nb02 + i1*nb01;
    char * dst_ptr = (char *)dst + i3*nb3 + i2*nb2 + i1*nb1;

    float value;
    const int64_t j = i0 - p0;

    if (j < 0) {// i0<p0
        // Left padding - reflect
        value = *(const float *)(src0_ptr - j * nb00);
    } else if ( j < ne00 ) { //i0 < ne0 - p1
        // Middle - copy
        value = *(const float *)(src0_ptr + j * nb00);
    } else  {
        // Right padding - reflect
        const int64_t src_idx = (ne0 - p1 - p0) - (p1 + 1 - (ne0 - i0)) - 1;
        value = *(const float *)(src0_ptr + src_idx * nb00);
    }
    *(float *)(dst_ptr + i0 * nb0) = value;
}

void ggml_cuda_op_pad_reflect_1d(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
    const ggml_tensor * src0 = dst->src[0];
    cudaStream_t stream = ctx.stream();

    GGML_ASSERT(src0->type == GGML_TYPE_F32);
    GGML_ASSERT(dst->type == GGML_TYPE_F32);

    const int32_t * opts = (const int32_t *) dst->op_params;
    const int p0 = opts[0];
    const int p1 = opts[1];

    const int64_t ne00 = src0->ne[0];
    const int64_t ne01 = src0->ne[1];
    const uint3   ne01_packed = init_fastdiv_values(ne01);
    const int64_t ne02 = src0->ne[2];

Review comment (suggestion):
```suggestion
    } else if (j < ne00) { //i0 < ne0 - p1
```

Code after:
        const int64_t             nb3,
        const int                 p0,
        const int                 p1) {
    const int64_t i3 = blockIdx.z;
    const int64_t i2 = blockIdx.y;

    const uint2   div_mod_packed = fast_div_modulo(blockIdx.x, ne01);
    const int64_t tile1          = div_mod_packed.y;  // i1
    const int64_t tile0          = div_mod_packed.x;  // nth i0 tile
    const int64_t i1             = tile1;
    const int64_t i0             = threadIdx.x + tile0 * blockDim.x;

    // ne01.z is original value of unpacked ne01 (see init_fastdiv_values in common.cuh)
    if (i0 >= ne0 || i1 >= ne01.z || i2 >= ne02 || i3 >= ne03) {
        return;
    }

    const char * src0_ptr = (const char *) src0 + i3 * nb03 + i2 * nb02 + i1 * nb01;
    char *       dst_ptr  = (char *) dst + i3 * nb3 + i2 * nb2 + i1 * nb1;

    const int64_t rel_i0 = i0 - p0;  // relative i0 in src0
    int64_t src_idx;

    if (rel_i0 < 0) {
        // Left padding - reflect
        src_idx = -rel_i0;
    } else if (rel_i0 < ne00) {
        // Middle - copy
        src_idx = rel_i0;
    } else {
        // Right padding - reflect
        src_idx = 2 * ne00 - 2 - rel_i0;
    }
    const float value               = *(const float *) (src0_ptr + src_idx * nb00);
    *(float *) (dst_ptr + i0 * nb0) = value;
}

void ggml_cuda_op_pad_reflect_1d(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
    const ggml_tensor * src0   = dst->src[0];
    cudaStream_t        stream = ctx.stream();

    GGML_ASSERT(src0->type == GGML_TYPE_F32);
    GGML_ASSERT(dst->type == GGML_TYPE_F32);

    const int32_t * opts = (const int32_t *) dst->op_params;
    const int       p0   = opts[0];
    const int       p1   = opts[1];

    const int64_t ne00        = src0->ne[0];
    const int64_t ne01        = src0->ne[1];
    const uint3   ne01_packed = init_fastdiv_values(ne01);