Repository navigation
Conversation
|
CC @am17an |
|
Am I right in assuming that a substantial memory saving of this PR comes from switching out the vector allocations with If so where did you test (windows/linux)? Can we expect that the other system exhibits the same behavior? |
|
Hi @aendk
That is broadly the
I tested it separately on GB300/Linux and RTX 5090/Windows. On both systems, I see lower resident-memory measurements. I think the change is worthwhile given the measured resident-memory savings, though I would not assume the same savings on every system. I think it is allocator/OS dependent. |
8337f4f to
d3d8017
Compare
d3d8017 to
796958b
Compare
aendk
left a comment
There was a problem hiding this comment.
LGTM as far as I can see.
Since you now size the context buffer a bit tighter for each graph, can you come up with a definitive worst-case graph size, and if so, is that the opportunity for an additional test?
If not, i'd be curious if this PR passes the scheduler test suite of #27258
|
Thanks for the review! I worked through the worst case on paper and in a new test, and it checks out. With Maximum of 10 sources per node ( The most it can need is The buffer holds about So even in the worst case and in parallel mode, there is room to spare in the scheduler arena. I turned this into a test case as well and updated the PR. Also, yes, this PR passes the scheduler test suite of #27258. |
|
UUIC there are two changes in this PR:
If so, can you can split them into two PRs? |
|
@am17an sure, will split, thanks |
c070d88 to
a85ed2c
Compare
|
can you please make the PR description more descriptive? |
|
@am17an done |
|
I run a port of llama.cpp on a jailbroken base PS5, where every allocated byte is backed at once with no lazy paging. My port held 874 MiB of CPU heap and about 717 MiB of it was the two schedulers, so I wanted to test this PR. I ran PR head a85ed2c versus merge base 7592b61 with Qwen3.8-27B-UD-Q2_K_XL and its projector, on Debian in Docker, as well as native Windows 11, with a RTX 4090 and a RTX 3080. On Linux the two ggml schedulers were reduced from 716.7 MiB to 123.1 MiB, so 593.6 MiB less. The Windows commit charge dropped by the same amount, and the merge base and PR matched on generated text and top 3 log probabilities on every setup I tried. I did hit one problem with the new check this PR adds in Of the 123.1 MiB that stays reserved, 33.0 MiB is the four id arrays, which this PR leaves at a fixed size. I can send a small separate PR for the id arrays once this one merges. Also, #30406 reports this same reservation as Windows commit charge, which is what my 594.4 and 596.6 MiB figures measure. DetailsMemory I measured the memory the two ggml schedulers reserve on the merge base and with the PR on Linux, and with llama-server loaded and idle on Windows. The Windows commit charge dropped by 594.4 MiB CPU only, and 596.6 MiB on the RTX 4090, so the reservation counts against the commit limit there and the PR removes most of it.
PS5 On the PS5 I carried the PR's change to Outputs The merge base and PR matched on generated text and top 3 log probabilities rounded to 6 decimals on CPU, the 4090, and the 4090 + 3080 with 4 copies. The guard The guard checks an upper estimate instead of counting what each array will hold. With 4 copies, the estimate budgets 8 entries per split input. In the actual graph copy, that input adds 2 entries to the node array and 4 to the separate leaf array. In the 40 node test with 4 copies, the estimate was over the capacity of each array, which caused the PR to abort. The graph actually needed far fewer node and leaf slots, so both fit.
The merge base and master at 3ba1ce0 both allocate the same graph without error and come up clean under ASan and UBSan. The id arrays In my port the id arrays grow with the graph copy and take 0.5 MiB, and arrays that grow don't need the guard. For transparency purposes, it was Claude that found the reservation in my port and wrote its patch. Claude Code (Claude Fable 5.2 and Claude Opus 5.5) wrote the guard test and scripts, built the PS5 test build, and ran the PC builds and measurements, and Codex running GPT 6.0 Astra independently rechecked the CPU results. I directed the work, installed and started the PS5 build on the console, reviewed the results, and wrote this comment myself after looking over all of the data and results. |
| ggml_free(sched->ctx); | ||
|
|
||
| const size_t context_graph_size = std::max(graph->n_nodes, graph->n_leafs); | ||
| const size_t context_buffer_size = context_graph_size*GGML_SCHED_MAX_SPLIT_INPUTS*2*sizeof(struct ggml_tensor); |
There was a problem hiding this comment.
is this size enough? because later down the graph size is calculated as
int graph_size = std::max(graph->n_nodes, graph->n_leafs) + total_inputs * 2 * sched->n_copies + n_dep_nodes;
There was a problem hiding this comment.
Hi @am17an, Yes it's enough, because the 2 sizes are for different things. The dependency nodes live in their own context after the patch, not in the sched->ctx arena. The other size which has the dependency nodes is sizing the node/leaf lists, not the arena. Btw I will rewrite/refactor to make calculations more clear, thanks!
I will reply separately to the other comment from @Adon1s after studying the test case.
There was a problem hiding this comment.
@am17an I updated the PR making the existing code more readable, splitting into functions with comments and using macros to define redundant expressions. This code is very confusing and full of formulas, so I hope the clean up is Ok. Eventually, I think I want to rewrite all this code using C++ namespaces, but I keep this PR restricted to only small cleanups and adjusting the allocation :)
a85ed2c to
d601b78
Compare
Thank you @Adon1s for reporting the issue. I have fixed it in the latest version. Can you test again on your side and confirm the latest PR passes your tests along with seeing the same memory improvements on your setup? |
|
@joelagnel All of the tests pass in your latest PR, and the memory savings are just as good as before. I appreciate you reading through my longwinded comment about an edge case. I am just starting to get my feet wet contributing to llama.cpp. Hope your PR gets merged soon! |
d601b78 to
66bcf63
Compare
|
I can provoke test failures with the following:
|
|
I (=my AI) found another bug: Since this is taking place in the ggml scheduler, we should tidy up this potentially brittle code, even though llama.cpp never really triggers it. Other projects building on top of ggml could run into this. I'll share the repro and possible test internally to keep this thread minimal @joelagnel |
66bcf63 to
671887c
Compare
|
@joelagnel I partly reran the CI and roughly the same amount of tests fail. |
671887c to
b9f4edd
Compare
|
Let's look into moving this forward @ggerganov ? @aendk mentioned to me he's happy with it now too. there's no other open review comment. |
The scheduler context buffer is sized in ggml_backend_sched_new() as
max_splits * GGML_SCHED_MAX_SPLIT_INPUTS * 2 * sizeof(ggml_tensor)
+ ggml_graph_overhead_custom(graph_size, false)
but nothing stores a ggml_cgraph in sched->ctx. The only objects allocated
from it are ggml_tensor structs: the per-backend input copies
(ggml_dup_tensor_layout) and the input dependency views (ggml_view_tensor).
sched->graph is a plain struct member whose nodes[] and leafs[] arrays are
realloc'd separately, and ggml_graph_view() returns by value without
allocating. The graph overhead term therefore reserved space that is never
used, and it scaled with the scheduler's maximum graph_size.
Remove it. The buffer is sized only for the tensors it can hold.
Allocate scheduler scratch space for the graph being split. Keep optimizer allocation-dependency views in a separate context sized for the dependencies actually created.
At most GGML_MAX_SRC * (C + 1) + (C - 1) objects per unit of graph size, C = 1 or GGML_SCHED_MAX_COPIES, against GGML_SCHED_MAX_SPLIT_INPUTS * 2 slots of sizeof(ggml_tensor). The two scheduler macros move to ggml-backend-impl.h so that the test can use them.
b9f4edd to
0db811a
Compare
|
Just rebased on master, no other change. |
Overview
Save host memory on scheduler context sizing.
Make the scheduler context buffer sized at split time from the graph being split (max(n_nodes, n_leafs) times the worst-case object count for n_copies). When a later graph needs more than it holds, a larger block is allocated and the old one is kept until the scheduler is freed, so pointers into it stay valid. It never shrinks. Before it was allocated on scheduler creation, from the largest graph the scheduler was created for. Also separate the dependency ctx from sched ctx as dependency count is known only after backend's graph optimization runs, and add error checking for if we could outgrow the backend id arrays.
Testing
On llama.cpp master vs PR:
Requirements