Skip to content

ggml: fix conv_1d result layout for batched input - #28738

Merged
ggerganov merged 2 commits into
ggml-org:masterfrom
ServeurpersoCom:ggml/fix-conv-1d-batch
Oct 14, 2026
Merged

ggerganov merged 2 commits into
ggml-org:masterfrom
ServeurpersoCom:ggml/fix-conv-1d-batch

Conversation

@ServeurpersoCom

@ServeurpersoCom ServeurpersoCom commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Fix batched conv_1d so vocoders can process several streams in one graph. Nothing in mtmd batches a vocoder today, so no in-tree output changes: it is a ggml level fix for downstream projects that do, such as qwentts.cpp, and for the day mtmd wants to.

Additional information

ggml_conv_1d reads the GEMM result as [OL, OC, N] when it is actually [OL, N, OC]. A reshape only relabels the buffer, so the shape looks right and the values come out interleaved between batches and channels. Nothing asserts, the graph just runs and the audio is scrambled. Every caller in the tree is N == 1, where both readings are the same bytes, which is why nobody hit it.

test-backend-ops cannot catch this since it compares a backend against the CPU on the same wrong graph. tests/test-conv-1d.cpp instead compares a batched convolution against N single batch ones: 5 of 7 geometries fail before, all 7 pass after.

Updated :

Reshape to the real layout and permute back, the same way ggml_conv_2d does, so every batch size builds the same graph. For N == 1 this adds a cont on an already ordered tensor: outputs are bit-exact, and in qwentts.cpp it costs about 5% of the codec decode, 0.3% of the whole synthesis.

Requirements

@ServeurpersoCom

Copy link
Copy Markdown
Contributor Author

Rebase only

ServeurpersoCom added a commit to ServeurpersoCom/acestep.cpp that referenced this pull request Oct 7, 2026
The vendored fork rebases onto ggml-org/ggml 0.26.0, which now carries
the fixes upstreamed from these projects:

- Vulkan: mul_mat and mul_mat_id read strided views such as a KV cache
  slice with their real batch stride, and mul_mm keeps exact A and B
  ranges (ggml-org/llama.cpp#28956)
- Metal: GGML_OP_PAD supports left and circular padding
  (ggml-org/llama.cpp#29561)
- CUDA: bitonic argsort handles rows wider than 1024 without CUB, on
  HIP and MUSA (ggml-org/llama.cpp#28957)

Two commits remain on top of upstream:

- cpu: fused snake activation
- ggml: conv_1d result layout for batched input
  (ggml-org/llama.cpp#28738)
ServeurpersoCom added a commit to ServeurpersoCom/minimaxmusic.cpp that referenced this pull request Oct 7, 2026
The vendored fork rebases onto ggml-org/ggml 0.26.0, which now carries
the fixes upstreamed from these projects:

- Vulkan: mul_mat and mul_mat_id read strided views such as a KV cache
  slice with their real batch stride, and mul_mm keeps exact A and B
  ranges (ggml-org/llama.cpp#28956)
- Metal: GGML_OP_PAD supports left and circular padding
  (ggml-org/llama.cpp#29561)
- CUDA: bitonic argsort handles rows wider than 1024 without CUB, on
  HIP and MUSA (ggml-org/llama.cpp#28957)

Two commits remain on top of upstream:

- cpu: fused snake activation
- ggml: conv_1d result layout for batched input
  (ggml-org/llama.cpp#28738)
ServeurpersoCom added a commit to ServeurpersoCom/yue2.cpp that referenced this pull request Oct 7, 2026
The vendored fork rebases onto ggml-org/ggml 0.26.0, which now carries
the fixes upstreamed from these projects:

- Vulkan: mul_mat and mul_mat_id read strided views such as a KV cache
  slice with their real batch stride, and mul_mm keeps exact A and B
  ranges (ggml-org/llama.cpp#28956)
- Metal: GGML_OP_PAD supports left and circular padding
  (ggml-org/llama.cpp#29561)
- CUDA: bitonic argsort handles rows wider than 1024 without CUB, on
  HIP and MUSA (ggml-org/llama.cpp#28957)

Two commits remain on top of upstream:

- cpu: fused snake activation
- ggml: conv_1d result layout for batched input
  (ggml-org/llama.cpp#28738)
ServeurpersoCom added a commit to ServeurpersoCom/omnivoice.cpp that referenced this pull request Oct 7, 2026
The vendored fork rebases onto ggml-org/ggml 0.26.0, which now carries
the fixes upstreamed from these projects:

- Vulkan: mul_mat and mul_mat_id read strided views such as a KV cache
  slice with their real batch stride, and mul_mm keeps exact A and B
  ranges (ggml-org/llama.cpp#28956)
- Metal: GGML_OP_PAD supports left and circular padding
  (ggml-org/llama.cpp#29561)
- CUDA: bitonic argsort handles rows wider than 1024 without CUB, on
  HIP and MUSA (ggml-org/llama.cpp#28957)

Two commits remain on top of upstream:

- cpu: fused snake activation
- ggml: conv_1d result layout for batched input
  (ggml-org/llama.cpp#28738)
ServeurpersoCom added a commit to ServeurpersoCom/qwentts.cpp that referenced this pull request Oct 7, 2026
The vendored fork rebases onto ggml-org/ggml 0.26.0, which now carries
the fixes upstreamed from these projects:

- Vulkan: mul_mat and mul_mat_id read strided views such as a KV cache
  slice with their real batch stride, and mul_mm keeps exact A and B
  ranges (ggml-org/llama.cpp#28956)
- Metal: GGML_OP_PAD supports left and circular padding
  (ggml-org/llama.cpp#29561)
- CUDA: bitonic argsort handles rows wider than 1024 without CUB, on
  HIP and MUSA (ggml-org/llama.cpp#28957)

Two commits remain on top of upstream:

- cpu: fused snake activation
- ggml: conv_1d result layout for batched input
  (ggml-org/llama.cpp#28738)
ServeurpersoCom added a commit to ServeurpersoCom/qwenasr.cpp that referenced this pull request Oct 7, 2026
The vendored fork rebases onto ggml-org/ggml 0.26.0, which now carries
the fixes upstreamed from these projects:

- Vulkan: mul_mat and mul_mat_id read strided views such as a KV cache
  slice with their real batch stride, and mul_mm keeps exact A and B
  ranges (ggml-org/llama.cpp#28956)
- Metal: GGML_OP_PAD supports left and circular padding
  (ggml-org/llama.cpp#29561)
- CUDA: bitonic argsort handles rows wider than 1024 without CUB, on
  HIP and MUSA (ggml-org/llama.cpp#28957)

Two commits remain on top of upstream:

- cpu: fused snake activation
- ggml: conv_1d result layout for batched input
  (ggml-org/llama.cpp#28738)
ServeurpersoCom added a commit to ServeurpersoCom/pockettts.cpp that referenced this pull request Oct 7, 2026
The vendored fork rebases onto ggml-org/ggml 0.26.0, which now carries
the fixes upstreamed from these projects:

- Vulkan: mul_mat and mul_mat_id read strided views such as a KV cache
  slice with their real batch stride, and mul_mm keeps exact A and B
  ranges (ggml-org/llama.cpp#28956)
- Metal: GGML_OP_PAD supports left and circular padding
  (ggml-org/llama.cpp#29561)
- CUDA: bitonic argsort handles rows wider than 1024 without CUB, on
  HIP and MUSA (ggml-org/llama.cpp#28957)

Two commits remain on top of upstream:

- cpu: fused snake activation
- ggml: conv_1d result layout for batched input
  (ggml-org/llama.cpp#28738)
ServeurpersoCom added a commit to ServeurpersoCom/s2s.cpp that referenced this pull request Oct 7, 2026
The vendored fork rebases onto ggml-org/ggml 0.26.0, which now carries
the fixes upstreamed from these projects:

- Vulkan: mul_mat and mul_mat_id read strided views such as a KV cache
  slice with their real batch stride, and mul_mm keeps exact A and B
  ranges (ggml-org/llama.cpp#28956)
- Metal: GGML_OP_PAD supports left and circular padding
  (ggml-org/llama.cpp#29561)
- CUDA: bitonic argsort handles rows wider than 1024 without CUB, on
  HIP and MUSA (ggml-org/llama.cpp#28957)

Two commits remain on top of upstream:

- cpu: fused snake activation
- ggml: conv_1d result layout for batched input
  (ggml-org/llama.cpp#28738)
Comment thread ggml/src/ggml.c Outdated
Comment on lines +4630 to +4637
// the GEMM flattens the batch into its rows, so the result comes back as
// [OC, N, OL] and the batched case needs OC and N swapped back
if (im2col->ne[2] == 1) {
result = ggml_reshape_3d(ctx, result, im2col->ne[1], a->ne[2], 1); // [OC, OL] => [1, OC, OL]
} else {
result = ggml_reshape_3d(ctx, result, im2col->ne[1], im2col->ne[2], a->ne[2]); // [OC, N, OL]
result = ggml_cont(ctx, ggml_permute(ctx, result, 0, 2, 1, 3)); // [N, OC, OL]
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to not have this branch - it would cause different graphs for different shapes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, the branch is gone: conv_1d now always reshapes and permutes like conv_2d, so every batch size builds the same graph. Pushed on my fork + ReTested on qwentts.cpp: outputs are bit-exact, and the extra cont at N == 1 costs about 5% of the codec decode, 0.3% of the whole synthesis.

The GEMM behind conv_1d folds the batch into its rows, so the product
sits in memory as [OL, N, OC] while the reshape claims [OL, OC, N].
The shape is right and the contents are transposed, so nothing fires:
no assert, no abort, the graph just runs and the channels come out
interleaved with the batches. With N == 1 the two readings describe
the same buffer, which is why the three vocoders in the tree never
hit it, they all squeeze to 2d right after the call.

Reshape to the real layout and permute OC and N back. The N == 1 path
keeps its direct reshape, so the current callers build the same graph
and pay for no extra copy.

tests/test-conv-1d.cpp compares one batched convolution against N
single batch convolutions built from the same weight and the same
input slices. Five of the seven geometries fail before this change and
all seven pass after. N == 1 and OC == 1 pass either way, since
swapping two dimensions when one of them is 1 changes nothing.

The batched path matters to downstream audio projects where several
synthesis lanes share a single graph. Nothing in mtmd batches a
vocoder today, each one streams one flow and carries its left context
between calls, but the op is correct now if one of them grows a lane
dimension.
Address review from ggerganov: drop the N == 1 branch of ggml_conv_1d.
The GEMM result is always reshaped to [OC, N, OL] and permuted back,
as ggml_conv_2d does, so every batch size builds the same graph. For
N == 1 this adds a cont on an already ordered tensor.
@ggerganov
ggerganov merged commit 6868a52 into ggml-org:master Oct 14, 2026
29 of 32 checks passed
@BrewTestBot BrewTestBot mentioned this pull request Oct 15, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ggml changes relating to the ggml tensor library for machine learning testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants