Skip to content

CUDA: fix cpy transposed path corrupting non-contiguous dst#1570

Open
devYRPauli wants to merge 1 commit into
ggml-org:masterfrom
devYRPauli:fix/cuda-cpy-transpose-noncontiguous-dst
Open

CUDA: fix cpy transposed path corrupting non-contiguous dst#1570
devYRPauli wants to merge 1 commit into
ggml-org:masterfrom
devYRPauli:fix/cuda-cpy-transpose-noncontiguous-dst

Conversation

@devYRPauli

Copy link
Copy Markdown
Contributor

Summary

ggml_cuda_cpy's can_be_transposed gate only inspects src0's layout. When the destination (src1) is non-contiguous - for example a strided view_2d slot such as a KV-cache column write - a same-type F32/F16/BF16/I32 copy still takes the transposed scalar kernel.

That kernel (cpy_scalar_transpose) writes the destination with a contiguous index derived from ne00/ne01 and ignores the destination strides nb10..nb13 (they are handed to GGML_UNUSED_VARS). The result is silent corruption of the strided destination.

Fix

Require ggml_is_contiguous(src1) for the transposed fast path. Non-contiguous destinations now fall back to the general cpy_scalar kernel, which honors nb10..nb13. Contiguous destinations (the common case) are unaffected, so there is no change on the normal path.

Fixes #1497, which has the root-cause analysis and a reproduction (16 ops plus a real transformer decode). Thanks to @OriPekelman for pinpointing the exact gate and proposing this fix.

ggml_cuda_cpy's can_be_transposed gate only inspected src0's layout, so a same-type F32/F16/BF16/I32 copy into a non-contiguous destination (e.g. a strided view_2d slot such as a KV-cache column write) still selected the transposed scalar kernel. That kernel writes dst with a contiguous index derived from ne00/ne01 and ignores the destination strides nb10..nb13 (marked GGML_UNUSED_VARS), silently corrupting the strided destination.

Require ggml_is_contiguous(src1) for the transposed fast path. Strided destinations now fall back to the general cpy_scalar kernel, which honors nb10..nb13.

Fixes ggml-org#1497.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ggml-cuda: cpy ignores destination strides (writes contiguously into a strided view_2d slot)

1 participant