ggml: optimize concat op by replacing per-element memcpy with row-level memcpy#24575
Open
sirohikartik wants to merge 1 commit into
Open
ggml: optimize concat op by replacing per-element memcpy with row-level memcpy#24575sirohikartik wants to merge 1 commit into
sirohikartik wants to merge 1 commit into
Conversation
Contributor
Author
|
Hi @ggerganov I think this is ready for review. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Optimize
ggml_compute_forward_concat_anyby replacing per-element memcpy with row-level memcpy.The original implementation called memcpy once per scalar element with a branch inside the innermost loop to select between src0 and src1. For a typical KV-cache concat shape [4096 x 1 x 16 x 1] along dim=2 this results in 65,536 separate memcpy calls of 4 bytes each.
This PR splits the loop into two separate regions (one per source tensor) eliminating the per-element branch, and collapses the i0 loop entirely to copy one full row per memcpy call instead of one element.
Benchmark
Isolated microbenchmark using identical tensor layout and loop logic.
Shape: [4096 x 1 x 16 x 1], concat dim=2, fp32, 200 runs
Apple Silicon (M1 Air):
Cold cache measured by flushing 64MB through memory before every call.
Requirements
code, identify the inefficiency, and verify the approach. All changes reviewed and benchmarked by me.