Repository navigation
Conversation
… caps Both contexts are no_alloc: they hold tensor headers and the graph object, while the activations live in the gallocr buffer. They were still created with ggml_init(arena_bytes), which mallocs the whole arena up front: 4 GiB for the AR prefix-state graph and 6 GiB for the NAR velocity graph, on every backend. Linux overcommit hides it; Windows charges it against the commit limit, and a CPU run dies in ggml_init with GGML_ASSERT(ctx->mem_buffer != NULL) right after the NAR stage starts (0xShug0#656). Size the two contexts as nodes * ggml_tensor_overhead() + ggml_graph_overhead_custom(nodes, false), the idiom the Apollo and UniVSR runtimes use. That is ~30 MB and ~110 MB. yue2.nar_graph_arena_mb has no consumer left and is removed; unknown session options are ignored, so commands that still pass it keep working. yue2.ar_prefill_graph_arena_mb stays because the shared Qwen decode runtime still reads it. Validation (yue2-3b-q8_0 + yue2-vae-f32, seed 1234): CPU output is byte-identical to main, uncapped and under a ulimit -v that made main fail with the reported assert; Vulkan (Intel Arc Pro B70) output is byte-identical to main.
|
@0xShug0 Yeah, it looks like the same root cause. I asked claude to search it, and this bug lives elsewhere too.
This is touching too much stuff that I rather not play with. |
|
I remembered for 151 another solution was to change backend weight store without touching model code, but I was lazy to do regression tests for this global change and did model local changes. Not sure if that solution still works. |
|
I don't know this code base well enough, so I'm going to just relay claude's analysis:
Just brainstorming: If you would rather fix this class globally than merge per-model patches like this one, it doesn't have to be a big-bang change:
If you go that route, this PR is probably unnecessary. |
|
I just did some more testing, and Claude came back with some interesting results. This PR might not be the full picture for memory optimization under windows.
|
|
I will switch to Windows to do some tests tomorrow to confirm the root cause. |
…uffer landed
Every no_alloc ggml context is a host allocation made in full by
ggml_init, whether or not the tensor headers ever fill it. The process
peak in `--metrics` says how much of that there is in total; these
[TIMING] lines say which context, by the name the build_ms line next to
them already uses:
yue2.nar.graph.ctx_reserved_mb 6144
yue2.nar.graph.ctx_used_mb 10.603347778320312
yue2.nar.weights.ctx_reserved_mb 6144
yue2.nar.weights.ctx_used_mb 0.111602783203125
yue2.nar.weights.buffer_mb 1533.761962890625
yue2.nar.weights.buffer_name Vulkan1
New debug::timing_log_context_reservation(name, ctx) reads
ggml_get_mem_size / ggml_used_mem once the graph is built. Called from
the shared Qwen causal decode runtime (prefill, batched prefill, decode,
batched decode, block prefill), the greedy Qwen decoder, the MOSS delay
backbone, the two yue2 graph contexts, and BackendWeightStore::upload(),
which covers every weight store by its own name and adds the backend
buffer's size and name so a host fallback shows up as such.
Gated by the existing timing log; nothing runs without --log.
On this yue2 run (main, defaults, Linux/Vulkan on Intel) the contexts
reserve 29.8 GB for under 30 MB of headers, against a process VmPeak of
32.6 GB. 13.9 GB of that is the weight stores (the 0xShug0#151 class), 5.6 GB
the shared Qwen runtime, 10.2 GB the two yue2 graph contexts that 0xShug0#657
resizes.
Output byte-identical (yue2, 640 tokens, seed 1234).
|
I set up some memory instrumentation you could try on the Windows side, two commits on a branch (not a PR, let me know if you want me to open one): https://github.com/engival/audio.cpp/tree/metrics-process-memory I could only test it on Linux of course.
metrics.memory.peak_rss_mb=1100
[TIMING ts=20260923-114628] yue2.ar.weights.ctx_reserved_mb 6144 On a default yue2 run here the contexts reserve 29.8 GB for under 30 MB of headers. #657 covers the two graph contexts (10.2 GB); the four yue2 weight stores are another 13.9 GB of the same thing (the #151 class), and the shared Qwen runtime 5.6 GB. So depending on the commit limit on your Windows box, #657 alone may pass your test and still not fix the reported issue. The log should make that visible either way. Output is byte-identical with and without the branch. |
Opts yue2 into the FromCapacity sizing: the four weight stores (yue2.ar.weights, yue2.ar.generation.weights, yue2.nar.weights and the VAE's) take a TensorCapacity from the source's tensor count, the three QwenCausalDecodeRuntime configs set graph_arena_sizing, and the VAE runtime gets context_sizing. Their four session options go away: yue2.model_weight_context_mb, yue2.vae_weight_context_mb, yue2.ar_decode_graph_arena_mb, yue2.vae_graph_arena_mb (unknown options are ignored, so old command lines still run). The two contexts yue2 builds itself, the AR prefix-state graph and the NAR graph, keep their options for now (the prefill option now only feeds the prefix-state graph and is described as such); 0xShug0#657 covers those. Per-context log on the same render, before -> after (MiB reserved): yue2.ar.weights 6144 -> 0.22 yue2.ar.generation.weights 64 -> 0.22 yue2.nar.weights 6144 -> 0.22 framework.oobleck_audio_vae.weights 1536 -> 0.12 yue2.ar.prefill.graph (x2, CFG) 4096 -> 48.5 yue2.ar.decode.graph (x2, CFG) 1536 -> 48.5 Process peak footprint 32653 -> 20301 MiB; output byte-identical.
The two contexts yue2 builds itself. Each graph's node cap becomes a named constant next to the runtime and its context is sized from it with no_alloc_graph_context_bytes; the last two arena options (yue2.ar_prefill_graph_arena_mb, yue2.nar_graph_arena_mb) go away. Same ground as 0xShug0#657, done through the shared helper instead of inline arithmetic; drop this commit if that lands first. Per-context log, before -> after (MiB reserved / used): yue2.ar.prefix_state.graph 4096 -> 48.5 (3.0 used) yue2.nar.graph 6144 -> 194.1 (10.6 used) Process peak footprint on the render: 32653 (main) -> 10061 MiB, all yue2-owned reservations gone; output byte-identical, and the removed options are still accepted (ignored) on the command line.
Opts yue2 into the FromCapacity sizing: the four weight stores (yue2.ar.weights, yue2.ar.generation.weights, yue2.nar.weights and the VAE's) take a TensorCapacity from the source's tensor count, the three QwenCausalDecodeRuntime configs set graph_arena_sizing, and the VAE runtime gets context_sizing. Their four session options go away: yue2.model_weight_context_mb, yue2.vae_weight_context_mb, yue2.ar_decode_graph_arena_mb, yue2.vae_graph_arena_mb (unknown options are ignored, so old command lines still run). The two contexts yue2 builds itself, the AR prefix-state graph and the NAR graph, keep their options for now (the prefill option now only feeds the prefix-state graph and is described as such); 0xShug0#657 covers those. Per-context log on the same render, before -> after (MiB reserved): yue2.ar.weights 6144 -> 0.22 yue2.ar.generation.weights 64 -> 0.22 yue2.nar.weights 6144 -> 0.22 framework.oobleck_audio_vae.weights 1536 -> 0.12 yue2.ar.prefill.graph (x2, CFG) 4096 -> 48.5 yue2.ar.decode.graph (x2, CFG) 1536 -> 48.5 Process peak footprint 32653 -> 20301 MiB; output byte-identical.
The two contexts yue2 builds itself. Each graph's node cap becomes a named constant next to the runtime and its context is sized from it with no_alloc_graph_context_bytes; the last two arena options (yue2.ar_prefill_graph_arena_mb, yue2.nar_graph_arena_mb) go away. Same ground as 0xShug0#657, done through the shared helper instead of inline arithmetic; drop this commit if that lands first. Per-context log, before -> after (MiB reserved / used): yue2.ar.prefix_state.graph 4096 -> 48.5 (3.0 used) yue2.nar.graph 6144 -> 194.1 (10.6 used) Process peak footprint on the render: 32653 (main) -> 10061 MiB, all yue2-owned reservations gone; output byte-identical, and the removed options are still accepted (ignored) on the command line.
Opts yue2 into the FromCapacity sizing: the four weight stores (yue2.ar.weights, yue2.ar.generation.weights, yue2.nar.weights and the VAE's) take a TensorCapacity from the source's tensor count, the three QwenCausalDecodeRuntime configs set graph_arena_sizing, and the VAE runtime gets context_sizing. Their four session options go away: yue2.model_weight_context_mb, yue2.vae_weight_context_mb, yue2.ar_decode_graph_arena_mb, yue2.vae_graph_arena_mb (unknown options are ignored, so old command lines still run). The two contexts yue2 builds itself, the AR prefix-state graph and the NAR graph, keep their options for now (the prefill option now only feeds the prefix-state graph and is described as such); 0xShug0#657 covers those. Per-context log on the same render, before -> after (MiB reserved): yue2.ar.weights 6144 -> 0.22 yue2.ar.generation.weights 64 -> 0.22 yue2.nar.weights 6144 -> 0.22 framework.oobleck_audio_vae.weights 1536 -> 0.12 yue2.ar.prefill.graph (x2, CFG) 4096 -> 48.5 yue2.ar.decode.graph (x2, CFG) 1536 -> 48.5 Process peak footprint 32653 -> 20301 MiB; output byte-identical.
The two contexts yue2 builds itself. Each graph's node cap becomes a named constant next to the runtime and its context is sized from it with no_alloc_graph_context_bytes; the last two arena options (yue2.ar_prefill_graph_arena_mb, yue2.nar_graph_arena_mb) go away. Same ground as 0xShug0#657, done through the shared helper instead of inline arithmetic; drop this commit if that lands first. Per-context log, before -> after (MiB reserved / used): yue2.ar.prefix_state.graph 4096 -> 48.5 (3.0 used) yue2.nar.graph 6144 -> 194.1 (10.6 used) Process peak footprint on the render: 32653 (main) -> 10061 MiB, all yue2-owned reservations gone; output byte-identical, and the removed options are still accepted (ignored) on the command line.
Opts yue2 into the FromCapacity sizing: the four weight stores (yue2.ar.weights, yue2.ar.generation.weights, yue2.nar.weights and the VAE's) take a TensorCapacity from the source's tensor count, the three QwenCausalDecodeRuntime configs set graph_arena_sizing, and the VAE runtime gets context_sizing (its two byte fields set to 0, as it requires). Their four session options go away: yue2.model_weight_context_mb, yue2.vae_weight_context_mb, yue2.ar_decode_graph_arena_mb, yue2.vae_graph_arena_mb (unknown options are ignored, so old command lines still run). The two contexts yue2 builds itself, the AR prefix-state graph and the NAR graph, keep their options for now (the prefill option now only feeds the prefix-state graph and is described as such); 0xShug0#657 covers those. Per-context log on the same render, before -> after (MiB reserved): yue2.ar.weights 6144 -> 0.22 yue2.ar.generation.weights 64 -> 0.22 yue2.nar.weights 6144 -> 0.22 framework.oobleck_audio_vae.weights 1536 -> 0.12 yue2.ar.prefill.graph (x2, CFG) 4096 -> 48.5 yue2.ar.decode.graph (x2, CFG) 1536 -> 48.5 framework.oobleck_audio_vae graph 1536 -> 388 Process peak footprint 32653 -> 20301 MiB; output byte-identical.
The two contexts yue2 builds itself. Each graph's node cap becomes a named constant next to the runtime and its context is sized from it with no_alloc_graph_context_bytes; the last two arena options (yue2.ar_prefill_graph_arena_mb, yue2.nar_graph_arena_mb) go away. Same ground as 0xShug0#657, done through the shared helper instead of inline arithmetic; drop this commit if that lands first. Per-context log, before -> after (MiB reserved / used): yue2.ar.prefix_state.graph 4096 -> 48.5 (3.0 used) yue2.nar.graph 6144 -> 194.1 (10.6 used) Process peak footprint on the render: 32653 (main) -> 10061 MiB, all yue2-owned reservations gone; output byte-identical, and the removed options are still accepted (ignored) on the command line.
9bdd1d9 to
4d88768
Compare
…uffer landed
Every no_alloc ggml context is a host allocation made in full by
ggml_init, whether or not the tensor headers ever fill it. The process
peak in `--metrics` says how much of that there is in total; these
[TIMING] lines say which context, by the name the build_ms line next to
them already uses:
yue2.nar.graph.ctx_reserved_mb 6144
yue2.nar.graph.ctx_used_mb 10.603347778320312
yue2.nar.weights.ctx_reserved_mb 6144
yue2.nar.weights.ctx_used_mb 0.111602783203125
yue2.nar.weights.buffer_mb 1533.761962890625
yue2.nar.weights.buffer_name Vulkan1
New debug::timing_log_context_reservation(name, ctx) reads
ggml_get_mem_size / ggml_used_mem once the graph is built. Called from
the shared Qwen causal decode runtime (prefill, batched prefill, decode,
batched decode, block prefill), the greedy Qwen decoder, the MOSS delay
backbone, the two yue2 graph contexts, and BackendWeightStore::upload(),
which covers every weight store by its own name and adds the backend
buffer's size and name so a host fallback shows up as such.
Gated by the existing timing log; nothing runs without --log.
On this yue2 run (main, defaults, Linux/Vulkan on Intel) the contexts
reserve 29.8 GB for under 30 MB of headers, against a process VmPeak of
32.6 GB. 13.9 GB of that is the weight stores (the 0xShug0#151 class), 5.6 GB
the shared Qwen runtime, 10.2 GB the two yue2 graph contexts that 0xShug0#657
resizes.
Output byte-identical (yue2, 640 tokens, seed 1234).
…uffer landed
Every no_alloc ggml context is a host allocation made in full by
ggml_init, whether or not the tensor headers ever fill it. The process
peak in `--metrics` says how much of that there is in total; these
[TIMING] lines say which context, by the name the build_ms line next to
them already uses:
yue2.nar.graph.ctx_reserved_mb 6144
yue2.nar.graph.ctx_used_mb 10.603347778320312
yue2.nar.weights.ctx_reserved_mb 6144
yue2.nar.weights.ctx_used_mb 0.111602783203125
yue2.nar.weights.buffer_mb 1533.761962890625
yue2.nar.weights.buffer_name Vulkan1
New debug::timing_log_context_reservation(name, ctx) reads
ggml_get_mem_size / ggml_used_mem once the graph is built. Called from
the shared Qwen causal decode runtime (prefill, batched prefill, decode,
batched decode, block prefill), the greedy Qwen decoder, the MOSS delay
backbone, the two yue2 graph contexts, and BackendWeightStore::upload(),
which covers every weight store by its own name and adds the backend
buffer's size and name so a host fallback shows up as such.
Gated by the existing timing log; nothing runs without --log.
On this yue2 run (main, defaults, Linux/Vulkan on Intel) the contexts
reserve 29.8 GB for under 30 MB of headers, against a process VmPeak of
32.6 GB. 13.9 GB of that is the weight stores (the 0xShug0#151 class), 5.6 GB
the shared Qwen runtime, 10.2 GB the two yue2 graph contexts that 0xShug0#657
resizes.
Output byte-identical (yue2, 640 tokens, seed 1234).
…context in --log (#688) * metrics: report the process's peak RSS and peak footprint `--metrics` now prints a memory group next to `wall_ms`, read once at the end of the run from figures the OS already keeps (one syscall, nothing sampled during generation): metrics.memory.peak_rss_mb=1100 metrics.memory.peak_footprint_mb=32653 metrics.memory.peak_footprint_source=linux_vsize The footprint is the platform's own idea of what the process claimed beyond resident pages, i.e. what an allocation failure is measured against there, and the source line says which: Windows peak commit charge (PeakPagefileUsage, the figure a commit-limit failure compares to; Task Manager's default column hides it), Linux VmPeak (over-counts commit), macOS phys_footprint (Activity Monitor's "Memory"). A field the platform cannot report is omitted, as available_host_memory_bytes() already does with 0 = unknown. Motivation: memory reports today arrive as "allocation failed" with an RSS number, which on Windows says nothing about the commit charge that actually failed. With this in every shipped binary, "run it with --metrics and paste the output" answers the question after the fact and shows how close a working run sits to the limit. Peaks are process-wide, so a batch item reports the peak reached so far, not its own. Validated on Linux/Vulkan (yue2, 640 tokens, seed 1234): output byte-identical, RSS line matches /usr/bin/time %M. * log: reserved vs used bytes per ggml context, and where each weight buffer landed Every no_alloc ggml context is a host allocation made in full by ggml_init, whether or not the tensor headers ever fill it. The process peak in `--metrics` says how much of that there is in total; these [TIMING] lines say which context, by the name the build_ms line next to them already uses: yue2.nar.graph.ctx_reserved_mb 6144 yue2.nar.graph.ctx_used_mb 10.603347778320312 yue2.nar.weights.ctx_reserved_mb 6144 yue2.nar.weights.ctx_used_mb 0.111602783203125 yue2.nar.weights.buffer_mb 1533.761962890625 yue2.nar.weights.buffer_name Vulkan1 New debug::timing_log_context_reservation(name, ctx) reads ggml_get_mem_size / ggml_used_mem once the graph is built. Called from the shared Qwen causal decode runtime (prefill, batched prefill, decode, batched decode, block prefill), the greedy Qwen decoder, the MOSS delay backbone, the two yue2 graph contexts, and BackendWeightStore::upload(), which covers every weight store by its own name and adds the backend buffer's size and name so a host fallback shows up as such. Gated by the existing timing log; nothing runs without --log. On this yue2 run (main, defaults, Linux/Vulkan on Intel) the contexts reserve 29.8 GB for under 30 MB of headers, against a process VmPeak of 32.6 GB. 13.9 GB of that is the weight stores (the #151 class), 5.6 GB the shared Qwen runtime, 10.2 GB the two yue2 graph contexts that #657 resizes. Output byte-identical (yue2, 640 tokens, seed 1234).
Fixes #656
You may want to review this carefully.
claude codemade a bunch of decisions on how to fix this, including removing an option.Asking claude to justify this patch:
Why yue2.nar_graph_arena_mb is removed rather than resized
The option was the mem_size of a ggml_init call on a no_alloc context. Such a context holds only tensor headers and the graph object. The activations never live in it: ggml_gallocr places them in a backend buffer sized from the graph itself, which is the number logged as yue2.nar.graph.buffer_bytes.
So the option never controlled memory in the sense its name suggests. Raising it could not make a larger song fit, and lowering it below what the headers need would abort at graph build. The only observable effect of the value was the size of an up-front host malloc that was ~99% never touched. On Linux, overcommit made that invisible. On Windows it is charged against the commit limit, which is the crash in #656, and the reporter's natural response, raising it to 8192, made the malloc bigger.
After this change the context is sized from the node cap the graph is created with, so the two cannot disagree and there is nothing left for a user to tune. Keeping the option would mean either ignoring it silently or documenting a knob that does nothing, and both invite the same misreading that produced this issue. Unknown session options are ignored by the runtime, so existing command lines that still pass it continue to work. yue2.ar_prefill_graph_arena_mb stays because the shared Qwen decode runtime still reads it.
What was wrong
Yue2ArRuntime::PrefixStateGraphandYue2NarRuntime::Graphcreate their ggml contexts withggml_init({arena_bytes, nullptr, /*no_alloc=*/true}). Both contexts are no_alloc: they only ever hold tensor headers and the graph object, the activations are placed by the gallocr into a backend buffer. Butggml_initmallocs the wholemem_sizeup front regardless, so every run reserved 4 GiB (AR prefix state, fromyue2.ar_prefill_graph_arena_mb) and 6 GiB (NAR, fromyue2.nar_graph_arena_mb) of host memory that is ~99% never touched, on every backend.Linux overcommit hides that. Windows charges the reservation against the commit limit, and the CPU run in #656 dies in
ggml_initwithGGML_ASSERT(ctx->mem_buffer != NULL)right afteryue2.nar.synthesize.contextis logged, which is where the NAR stage prefills the AR stream and opens the first of those two contexts. Raisingyue2.nar_graph_arena_mbasks for a bigger malloc, so it makes it worse.Change
nodes * ggml_tensor_overhead() + ggml_graph_overhead_custom(nodes, false), withnodesthe same cap passed toggml_new_graph_custom(65536 and 262144). That is ~30 MB and ~110 MB. Same idiom as the Apollo and UniVSR runtimes.yue2.nar_graph_arena_mbhas no consumer left and is removed (session option, model spec, docs). Unknown session options are ignored by the runtime, so commands that still pass it keep working.yue2.ar_prefill_graph_arena_mbstays: the shared Qwen decode runtime still reads it.Model-local only, no framework files touched.
Validation
yue2-3b-q8_0.gguf+yue2-vae-f32.gguf, seed 1234,cot=off,--outcompared withcmpagainst a build ofmain(4e8be77).CPU (
--backend cpu --threads 8, 6 s request, 4 ODE steps, 192 semantic tokens):ulimit -v22 GBGGML_ASSERT(ctx->mem_buffer != NULL)afteryue2.nar.synthesize.context 24576, i.e. the log in #656--session-option yue2.nar_graph_arena_mb=8192still on the command lineThe capped run is a Linux stand-in for the Windows commit limit:
ggml_aligned_malloc: insufficient memory (attempted to allocate 4096.00 MB)fromPrefixStateGraph, then the assert. With a tighter cap (14 GB)mainfails one stage earlier, in the framework'sQwenCausalDecodeRuntime::build_prefill_graph, on the same pattern.