Repository navigation
Metal: spread long graphs over more command buffers (GPU watchdog) #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danielhanchen
wants to merge
4
commits into
master
Choose a base branch
from
metal-split-long-graphs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ea4d2fd
ggml-metal: spread big graphs over more command buffers (GPU watchdog)
danielhanchen b380462
Renumber the patch to 0018 (0014-0017 are taken by open PRs)
danielhanchen e4232fa
Merge master into metal-split-long-graphs
danielhanchen d58d651
Renumber the patch to 0021 (0018 is taken by the CPU iqk GEMM patch i…
danielhanchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
scripts/unsloth/ggml-patches/0021-ggml-metal-split-long-graphs.patch
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| diff --git a/src/ggml-metal/ggml-metal-context.m b/src/ggml-metal/ggml-metal-context.m | ||
| index e9064666..b4bac890 100644 | ||
| --- a/src/ggml-metal/ggml-metal-context.m | ||
| +++ b/src/ggml-metal/ggml-metal-context.m | ||
| @@ -53,6 +53,7 @@ struct ggml_metal { | ||
|
|
||
| // command buffer state | ||
| int n_cb; // number of extra threads used to submit the command buffers | ||
| + int n_cb_user; // n_cb as requested by ggml_metal_set_n_cb; graph_compute may raise n_cb for big graphs | ||
| int n_nodes_0; // number of nodes submitted by the main thread | ||
| int n_nodes_1; // remaining number of nodes submitted by the n_cb threads | ||
| int n_nodes_per_cb; | ||
| @@ -156,6 +157,7 @@ ggml_metal_t ggml_metal_init(ggml_metal_device_t dev) { | ||
| if (ggml_metal_fusion_info_stats(res->finfo)) { | ||
| ggml_metal_fusion_info_labels_init(res->finfo); | ||
| res->n_cb = 0; | ||
| + res->n_cb_user = 0; | ||
| } | ||
|
|
||
| GGML_LOG_INFO("%s: use fusion = %s\n", __func__, ggml_metal_fusion_info_enabled(res->finfo) ? "true" : "false"); | ||
| @@ -484,6 +486,22 @@ enum ggml_status ggml_metal_graph_compute(ggml_metal_t ctx, struct ggml_cgraph * | ||
| // number of nodes encoded by the main thread (empirically determined) | ||
| const int n_main = MAX(64, 0.1*gf->n_nodes); | ||
|
|
||
| + // a command buffer that runs for too long is killed by the macOS GPU watchdog (status 5, | ||
| + // kIOGPUCommandBufferCallbackErrorTimeout): one Z-Image 512 DiT step in one buffer does that on a slow GPU. | ||
| + // Spread big graphs over more command buffers (GGML_METAL_NODES_PER_CB, default 256; 0 = off). | ||
| + { | ||
| + static int nodes_per_cb = -1; | ||
| + if (nodes_per_cb < 0) { | ||
| + const char * v = getenv("GGML_METAL_NODES_PER_CB"); | ||
| + nodes_per_cb = v ? MAX(0, atoi(v)) : 256; | ||
| + } | ||
| + int n_cb_eff = ctx->n_cb_user; | ||
| + if (nodes_per_cb > 0 && n_cb_eff > 0) { | ||
| + n_cb_eff = MAX(n_cb_eff, MIN(GGML_METAL_MAX_COMMAND_BUFFERS, (gf->n_nodes - n_main + nodes_per_cb - 1)/nodes_per_cb)); | ||
| + } | ||
| + ctx->n_cb = n_cb_eff; | ||
| + } | ||
| + | ||
| // number of threads in addition to the main thread | ||
| const int n_cb = ctx->n_cb; | ||
|
|
||
| @@ -712,6 +730,8 @@ void ggml_metal_set_n_cb(ggml_metal_t ctx, int n_cb) { | ||
| n_cb = 0; | ||
| } | ||
|
|
||
| + ctx->n_cb_user = MIN(n_cb, GGML_METAL_MAX_COMMAND_BUFFERS); | ||
| + | ||
| if (ctx->n_cb != n_cb) { | ||
| ctx->n_cb = MIN(n_cb, GGML_METAL_MAX_COMMAND_BUFFERS); | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This patch's context does not match the pinned ggml commit
135ce1e0e3f2b0693d3de93d4f6b5fa0a1947820: for example, that revision has no blank line betweencapture_scopeand// command buffer state, or between the fusion-statistics block andGGML_LOG_INFO. The earlier patches do not modify this file, so they cannot resolve these mismatches. The source-preparation step in.github/workflows/unsloth-sd-prebuilt.yml:231–233applies every patch unconditionally, which means this addition stops prebuilt release preparation for every platform, not just Metal. Regenerate the hunks against the pinned source and verify the complete patch sequence withgit apply --check(documented by localgit apply -has “see if the patch is applicable”).Useful? React with 👍 / 👎.