Commit 6fd26a7ea for llama.cpp

commit 6fd26a7ea0f23585d27b4e42919f6f70c43ccd2c
Author: Hongbo Wen <157263272+archidoge0@users.noreply.github.com>
Date:   Tue Dec 1 22:23:34 2026 +0800

    cuda : use dst strides in unary_op_kernel_strided (#33322)

    Index dst via dst->nb[0..3] byte strides in unary_op_kernel_strided and dispatch the strided path whenever src0 or dst is non-contiguous so inplace unary ops on strided views do not overwrite padding.

    Assisted-by: Antigravity

diff --git a/ggml/src/ggml-cuda/unary.cu b/ggml/src/ggml-cuda/unary.cu
index b3b6aa48d..260b90e03 100644
--- a/ggml/src/ggml-cuda/unary.cu
+++ b/ggml/src/ggml-cuda/unary.cu
@@ -135,7 +135,11 @@ static void unary_cuda(const T * x, T * dst, const int k, cudaStream_t stream) {
 }

 template <float (*op)(float), typename T>
-static __global__ void unary_op_kernel_strided(const T * x, T * dst, const int k,const int64_t ne00,const int64_t ne01,const int64_t ne02,const size_t nb00,const size_t nb01,const size_t nb02,const size_t nb03) {
+static __global__ void unary_op_kernel_strided(
+        const T * x, T * dst, const int k,
+        const int64_t ne00, const int64_t ne01, const int64_t ne02,
+        const size_t nb00, const size_t nb01, const size_t nb02, const size_t nb03,
+        const size_t nb0,  const size_t nb1,  const size_t nb2,  const size_t nb3) {
     ggml_cuda_pdl_lc();
     const int i = blockDim.x*blockIdx.x + threadIdx.x;

@@ -149,17 +153,25 @@ static __global__ void unary_op_kernel_strided(const T * x, T * dst, const int k
     const int64_t i2 = rem % ne02;
     const int64_t i3 = rem / ne02;
     const size_t src_byte_offset = i0 * nb00 + i1 * nb01 + i2 * nb02 + i3 * nb03;
-    const T * src_ptr = (const T *)((const char *)x + src_byte_offset);
+    const size_t dst_byte_offset = i0 * nb0  + i1 * nb1  + i2 * nb2  + i3 * nb3;
+    const T * src_ptr = (const T *)((const char *)x   + src_byte_offset);
+          T * dst_ptr = (      T *)((      char *)dst + dst_byte_offset);

     ggml_cuda_pdl_sync();
-    dst[i] = ggml_cuda_cast<T>(op(ggml_cuda_cast<float>(*src_ptr)));
+    *dst_ptr = ggml_cuda_cast<T>(op(ggml_cuda_cast<float>(*src_ptr)));
 }

 template <float (*op)(float), typename T>
-static void unary_cuda_strided(const T * x, T * dst, const int k,const int64_t ne00,const int64_t ne01,const int64_t ne02,const size_t nb00,const size_t nb01,const size_t nb02,const size_t nb03, cudaStream_t stream) {
+static void unary_cuda_strided(
+        const T * x, T * dst, const int k,
+        const int64_t ne00, const int64_t ne01, const int64_t ne02,
+        const size_t nb00, const size_t nb01, const size_t nb02, const size_t nb03,
+        const size_t nb0,  const size_t nb1,  const size_t nb2,  const size_t nb3,
+        cudaStream_t stream) {
     const int num_blocks = (k + CUDA_NEG_BLOCK_SIZE - 1) / CUDA_NEG_BLOCK_SIZE;
     const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params((dim3)num_blocks, CUDA_NEG_BLOCK_SIZE, 0, stream);
-    ggml_cuda_kernel_launch(unary_op_kernel_strided<op, T>, launch_params, x, dst, k, ne00,ne01,ne02,nb00,nb01,nb02,nb03);
+    ggml_cuda_kernel_launch(unary_op_kernel_strided<op, T>, launch_params,
+        x, dst, k, ne00, ne01, ne02, nb00, nb01, nb02, nb03, nb0, nb1, nb2, nb3);
 }

 template <float (*op)(float)>
@@ -172,8 +184,9 @@ void ggml_cuda_op_unary(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {

     GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16);
     GGML_ASSERT(src0->type == dst->type);
+    GGML_ASSERT(ggml_are_same_shape(src0, dst));

-    if (ggml_is_contiguous(src0)) {
+    if (ggml_is_contiguous(src0) && ggml_is_contiguous(dst)) {
         if (src0->type == GGML_TYPE_F16) {
             unary_cuda<op>((const half *)src0_d, (half *)dst_d, ggml_nelements(src0), stream);
         } else if (src0->type == GGML_TYPE_BF16) {
@@ -185,15 +198,18 @@ void ggml_cuda_op_unary(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
         if (src0->type == GGML_TYPE_F16) {
             unary_cuda_strided<op>((const half *)src0_d, (half *)dst_d, ggml_nelements(src0),
                 src0->ne[0], src0->ne[1], src0->ne[2],
-                src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3], stream);
+                src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3],
+                dst->nb[0],  dst->nb[1],  dst->nb[2],  dst->nb[3], stream);
         } else if (src0->type == GGML_TYPE_BF16) {
             unary_cuda_strided<op>((const nv_bfloat16 *)src0_d, (nv_bfloat16 *)dst_d, ggml_nelements(src0),
                 src0->ne[0], src0->ne[1], src0->ne[2],
-                src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3], stream);
+                src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3],
+                dst->nb[0],  dst->nb[1],  dst->nb[2],  dst->nb[3], stream);
         } else {
             unary_cuda_strided<op>((const float *)src0_d, (float *)dst_d, ggml_nelements(src0),
                 src0->ne[0], src0->ne[1], src0->ne[2],
-                src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3], stream);
+                src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3],
+                dst->nb[0],  dst->nb[1],  dst->nb[2],  dst->nb[3], stream);
         }
     }
 }
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index c33cbc3a6..5136668ff 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -2160,9 +2160,10 @@ struct test_unary : public test_case {
     const ggml_type type;
     const std::array<int64_t, 4> ne_a;
     int v; // view (1 : non-contiguous a)
+    bool inplace;

     std::string vars() override {
-        return VARS_TO_STR3(type, ne_a, v);
+        return VARS_TO_STR4(type, ne_a, v, inplace);
     }

     double max_nmse_err(ggml_backend_t backend) override {
@@ -2177,13 +2178,14 @@ struct test_unary : public test_case {
     test_unary(ggml_unary_op op,
             ggml_type type = GGML_TYPE_F32,
             std::array<int64_t, 4> ne_a = {128, 2, 2, 2},
-            int v = 0)
-        : op(op), type(type), ne_a(ne_a), v(v) {}
+            int v = 0,
+            bool inplace = false)
+        : op(op), type(type), ne_a(ne_a), v(v), inplace(inplace) {}

     ggml_tensor * build_graph(ggml_context * ctx) override {
-        const bool grad_supported = op == GGML_UNARY_OP_ABS || op == GGML_UNARY_OP_SGN || op == GGML_UNARY_OP_NEG ||
+        const bool grad_supported = !inplace && (op == GGML_UNARY_OP_ABS || op == GGML_UNARY_OP_SGN || op == GGML_UNARY_OP_NEG ||
             op == GGML_UNARY_OP_STEP || op == GGML_UNARY_OP_RELU || op == GGML_UNARY_OP_SILU ||
-            op == GGML_UNARY_OP_EXPM1 || op == GGML_UNARY_OP_SOFTPLUS || op == GGML_UNARY_OP_SIGMOID || op == GGML_UNARY_OP_TANH;
+            op == GGML_UNARY_OP_EXPM1 || op == GGML_UNARY_OP_SOFTPLUS || op == GGML_UNARY_OP_SIGMOID || op == GGML_UNARY_OP_TANH);

         ggml_tensor * a;
         if (v & 1) {
@@ -2208,7 +2210,7 @@ struct test_unary : public test_case {
             ggml_set_name(a, "a");
         }

-        ggml_tensor * out = ggml_unary(ctx, a, op);
+        ggml_tensor * out = inplace ? ggml_unary_inplace(ctx, a, op) : ggml_unary(ctx, a, op);
         ggml_set_name(out, "out");

         return out;
@@ -12070,7 +12072,11 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
                     continue; // need extra params, separate test
                 }
                 test_cases.emplace_back(new test_unary((ggml_unary_op) op, type, { 128, 2, 2, 2 }, v));
+                test_cases.emplace_back(new test_unary((ggml_unary_op) op, type, { 128, 2, 2, 2 }, v, true));
                 test_cases.emplace_back(new test_unary((ggml_unary_op) op, type, { 5, 7, 11, 13 }, v));
+                if (v == 1) {
+                    test_cases.emplace_back(new test_unary((ggml_unary_op) op, type, { 5, 7, 2, 2 }, v, true));
+                }
                 if (type == GGML_TYPE_F16) {
                     test_cases.emplace_back(new test_unary((ggml_unary_op) op, type, { 33, 2, 2, 2 }, v));
                     test_cases.emplace_back(new test_unary((ggml_unary_op) op, type, { 65, 2, 2, 2 }, v));