Skip to content

metal : add support for GGML_OP_REPEAT_BACK - #26057

Open
wuisabel-gif wants to merge 1 commit into
ggml-org:masterfrom
wuisabel-gif:feature/metal-repeat-back
Open

wuisabel-gif wants to merge 1 commit into
ggml-org:masterfrom
wuisabel-gif:feature/metal-repeat-back

Conversation

@wuisabel-gif

Copy link
Copy Markdown

The Metal backend did not support GGML_OP_REPEAT_BACK, this PR adds it, addressing part of #14909.

REPEAT_BACK is the backward pass of REPEAT. The forward op tiles a source tensor into multiple copies, so during backpropagation each copy carries its own gradient, and all of them originate from the same source element. The backward op therefore reduces the gradient tensor back to the original shape by summing, for each source element, the gradients at every position that copies it.

Only F32 is implemented and tested, matching the type scope of the CPU and CUDA implementations. Other types are not claimed in supports_op.

Tested on Apple M3 at master c0bc859:

./build-metal/bin/test-backend-ops test -b Metal -o REPEAT_BACK

Before: all 10 cases reported "not supported". After: 10/10 REPEAT_BACK cases passed against the CPU reference, including non-contiguous input cases.

AI usage disclosure: yes, used Codex Sol 5.6 to review and edit. I reviewed the edit changes and tested on hardware.

Add kernel_repeat_back_f32 and wire up pipeline, encode, and
supports_op. One threadgroup per output row (i1, i2, i3), threads
stride over ne0, each thread sums all tiled copies of its element
from the gradient tensor. Offsets computed from byte strides so
non-contiguous views work. Reuses ggml_metal_kargs_repeat since the
argument layout matches the forward op. F32 only, matching CPU/CUDA.

REPEAT previously reached an unconditional return true via
fall-through; made it explicit so the new REPEAT_BACK case does not
change its behavior.

Ref ggml-org#14909
@wuisabel-gif
wuisabel-gif requested a review from a team as a code owner July 24, 2026 02:04
@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning Apple Metal https://en.wikipedia.org/wiki/Metal_(API) labels Jul 24, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Apple Metal https://en.wikipedia.org/wiki/Metal_(API) ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant