metal : per-op source split + parallel compile - #24021
Merged
Merged
Conversation
forforever73
force-pushed
the
metallib-split
branch
from
June 10, 2026 04:04
0d78e86 to
18a3a5d
Compare
Member
|
I'll take a look at this PR in the next days. |
forforever73
force-pushed
the
metallib-split
branch
from
June 13, 2026 03:24
18a3a5d to
c553f52
Compare
Member
|
We don't need to include diff --git a/ggml/src/ggml-metal/kernels/common.h b/ggml/src/ggml-metal/kernels/common.h
index 05115a628..c4d674394 100644
--- a/ggml/src/ggml-metal/kernels/common.h
+++ b/ggml/src/ggml-metal/kernels/common.h
@@ -1,12 +1,5 @@
#pragma once
-#define GGML_COMMON_DECL_METAL
-#define GGML_COMMON_IMPL_METAL
-#if defined(GGML_METAL_EMBED_LIBRARY)
-__embed_ggml-common.h__
-#else
-#include "ggml-common.h"
-#endif
#include "ggml-metal-impl.h"
#include <metal_stdlib>
diff --git a/ggml/src/ggml-metal/kernels/dequantize.h b/ggml/src/ggml-metal/kernels/dequantize.h
index 80eb4efd7..16a6cad56 100644
--- a/ggml/src/ggml-metal/kernels/dequantize.h
+++ b/ggml/src/ggml-metal/kernels/dequantize.h
@@ -2,6 +2,14 @@
#include "common.h"
+#define GGML_COMMON_DECL_METAL
+#define GGML_COMMON_IMPL_METAL
+#if defined(GGML_METAL_EMBED_LIBRARY)
+__embed_ggml-common.h__
+#else
+#include "ggml-common.h"
+#endif
+
#define QK_NL 16 // shared by mul_mm and get_rows_q instantiations
// NOTE: this is not dequantizing - we are simply fitting the templateThis should reduce the binary size. |
Member
|
@forforever73 This looks OK. I was thinking how to reduce the amount of time that it takes me to review the PRs and avoid blocking the progress towards #23114 (comment) for too long. So I have the following idea:
graph TD
subgraph master
M1["master: Base"] --> M2["master: Unrelated A"] --> M3["master: Unrelated B"] --> M4["master: Unrelated C"] --> M5["master: Final Merge"]
end
subgraph dev-metal
D1["dev-metal: Metal PR #1"] --> D2["dev-metal: Metal PR #2"] --> D3["dev-metal: Rebase onto master"] --> D4["dev-metal: Metal PR #3"] --> D5["dev-metal: Metal PR #4"] --> D6["dev-metal: Rebase onto master"] --> D7["dev-metal: Final FA Opt"]
end
M1 -.-> D1
M2 -.-> D3
M3 -.-> D6
D7 --> M5
|
Contributor
Author
|
@ggerganov Good suggestion. The current process is indeed a bit slow, so I'll retarget this PR to dev-metal and continue the follow-up work there. |
Member
|
Great, after resolving the conflicts and applying the #24021 (comment), will merge it and we can proceed forward. |
added 13 commits
June 20, 2026 16:57
forforever73
force-pushed
the
metallib-split
branch
from
June 20, 2026 09:15
ff08fa1 to
56f1be5
Compare
Contributor
Author
|
this cuts the embed test-backend-ops binary from 6.09 MB to 3.93 MB with all -b MTL0 tests still passing. |
ggerganov
pushed a commit
that referenced
this pull request
Jun 22, 2026
* preliminary extract common header * op source split * split metallib into 8 libs && load in parallel * derive kernel->library routing from functionNames * x-macro lib list + underscore filenames, dedup QK_NL, MRC fixes * op source split 8 to 20 * improve robustness of source fallback * clean up * change bool -> atomic_bool * only prepend headers that source actually includes * no semaphore, use GCD global queue * dedup library compile path, fix NSError lifetime, rename gla * relocate upstream concat/rope_back/repeat kernel changes into split files * move ggml-common.h from common.h into dequantize.h to shrink binary size --------- Co-authored-by: lvyichen <lvyichen@stepfun.com>
ggerganov
pushed a commit
that referenced
this pull request
Jun 27, 2026
* preliminary extract common header * op source split * split metallib into 8 libs && load in parallel * derive kernel->library routing from functionNames * x-macro lib list + underscore filenames, dedup QK_NL, MRC fixes * op source split 8 to 20 * improve robustness of source fallback * clean up * change bool -> atomic_bool * only prepend headers that source actually includes * no semaphore, use GCD global queue * dedup library compile path, fix NSError lifetime, rename gla * relocate upstream concat/rope_back/repeat kernel changes into split files * move ggml-common.h from common.h into dequantize.h to shrink binary size --------- Co-authored-by: lvyichen <lvyichen@stepfun.com>
ggerganov
pushed a commit
that referenced
this pull request
Jul 6, 2026
* preliminary extract common header * op source split * split metallib into 8 libs && load in parallel * derive kernel->library routing from functionNames * x-macro lib list + underscore filenames, dedup QK_NL, MRC fixes * op source split 8 to 20 * improve robustness of source fallback * clean up * change bool -> atomic_bool * only prepend headers that source actually includes * no semaphore, use GCD global queue * dedup library compile path, fix NSError lifetime, rename gla * relocate upstream concat/rope_back/repeat kernel changes into split files * move ggml-common.h from common.h into dequantize.h to shrink binary size --------- Co-authored-by: lvyichen <lvyichen@stepfun.com>
ggerganov
pushed a commit
that referenced
this pull request
Jul 8, 2026
* preliminary extract common header * op source split * split metallib into 8 libs && load in parallel * derive kernel->library routing from functionNames * x-macro lib list + underscore filenames, dedup QK_NL, MRC fixes * op source split 8 to 20 * improve robustness of source fallback * clean up * change bool -> atomic_bool * only prepend headers that source actually includes * no semaphore, use GCD global queue * dedup library compile path, fix NSError lifetime, rename gla * relocate upstream concat/rope_back/repeat kernel changes into split files * move ggml-common.h from common.h into dequantize.h to shrink binary size --------- Co-authored-by: lvyichen <lvyichen@stepfun.com>
ggerganov
pushed a commit
that referenced
this pull request
Jul 10, 2026
* preliminary extract common header * op source split * split metallib into 8 libs && load in parallel * derive kernel->library routing from functionNames * x-macro lib list + underscore filenames, dedup QK_NL, MRC fixes * op source split 8 to 20 * improve robustness of source fallback * clean up * change bool -> atomic_bool * only prepend headers that source actually includes * no semaphore, use GCD global queue * dedup library compile path, fix NSError lifetime, rename gla * relocate upstream concat/rope_back/repeat kernel changes into split files * move ggml-common.h from common.h into dequantize.h to shrink binary size --------- Co-authored-by: lvyichen <lvyichen@stepfun.com>
ggerganov
pushed a commit
that referenced
this pull request
Jul 28, 2026
* preliminary extract common header * op source split * split metallib into 8 libs && load in parallel * derive kernel->library routing from functionNames * x-macro lib list + underscore filenames, dedup QK_NL, MRC fixes * op source split 8 to 20 * improve robustness of source fallback * clean up * change bool -> atomic_bool * only prepend headers that source actually includes * no semaphore, use GCD global queue * dedup library compile path, fix NSError lifetime, rename gla * relocate upstream concat/rope_back/repeat kernel changes into split files * move ggml-common.h from common.h into dequantize.h to shrink binary size --------- Co-authored-by: lvyichen <lvyichen@stepfun.com>
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
cont [#23566 ]
Breaks up the monolithic
ggml-metal.metalinto 20 per-op source files underkernels/, each compiled into its ownMTLLibrary. On master, touching any kernel recompiles the entire 10k-line file; after the split, only the changed file is recompiled. Full builds also benefit frommake -jparallelism across sources, and runtime cold-start compilation runs thenewLibraryWithSource:calls concurrently via GCD.the single .metal file is replaced by:
common.h,dequantize.h,quantize.h)X-macro (
GGML_METAL_LIBS) is the single source of truth for the library list; the enum, name table, embed symbols, and source-path array are all generated from it. Kernel → library routing is derived at init time from each library's[MTLLibrary functionNames]— no hardcoded prefix tables. Adding a new kernel requires no routing changes; adding a new library = one line in the macro + one line in CMakeLists.txt.All three build paths (embed, pre-compiled metallib, source fallback) are supported. Embed and source-fallback compile the 20 sources in parallel via GCD; pre-compiled metallib compiles each source to
.airat build time and links them into a singledefault.metallib.Benchmark
Apple M4 Max, macOS 26.1, 3 trials, median reported.
Offline build time (xcrun metal, during cmake --build)
make -j)Runtime cold-start (Metal shader cache cleared)
Per-library breakdown (embed mode, median trial):
Embed binary size (test-backend-ops, static link)
Increase is from shared headers being duplicated across sources. CMake already skips headers a source doesn't use (e.g. sources that only need
common.hskip the 26 KBdequantize.h), butcommon.h+ggml-common.h+ggml-metal-impl.hare still repeated 20×.Requirements