Repository navigation
Decide backend sampling after the sampler chain is built, and drop a freed compute buffer's parked graph split (follow-ups to #7) - #9
Conversation
…a: a freed compute buffer drops its parked split The backend-sampling decision moved ahead of common_sampler_init in 046d98c so the kept-sampler key could carry it, but common_sampler_init turns backend sampling off for a chain it builds with a grammar or a reasoning budget, which the backend cannot sample; the chain was attached anyway and `GGML_ASSERT(!gsmpl->grmr && "using grammar in combination with backend sampling is not supported")` aborted the server on a response_format request (the Backend sampling CI job, unit/test_chat_completion.py::test_completion_with_response_format). The decision is checked again after the chain is built. A meta backend keeps one graph split per compute buffer while graphs come from two schedulers (graph slots). Recreating the schedulers freed their compute buffers without touching the splits parked under them, so toggling a lever that recreates the schedulers grew the cache by a split per distinct buffer address. A buffer being freed now drops the split kept for it, through a registry of the live meta backends.
There was a problem hiding this comment.
🟡 Changes recommended
Buffer-free notifications access other contexts' cache state without synchronization with graph computation, introducing cross-context data races.
1 open issue
What changed in this PR
This PR follows up on #7 by correcting backend-sampling eligibility and cleaning up cached meta-backend graph splits when compute buffers are freed.
Changes:
- Recheck backend sampling after sampler initialization so incompatible chains use CPU sampling.
- Register live meta backends and notify them when a buffer is freed to remove its cached split.
| File | Description |
|---|---|
tools/server/server-context.cpp |
Rechecks backend-sampling eligibility after building the sampler chain. |
ggml/src/ggml-backend-meta.cpp |
Adds live-backend tracking and buffer-free cache invalidation. |
Review effort: Balanced
…ntext's thread, and the device and buffer-type registries A meta buffer that is freed tells every live meta backend to forget the split kept for it, and the freeing thread can be another context's compute thread (a model unloaded while a second model decodes, a drafter's scheduler recreated while the target computes). That notification read active_buf and erased from parked while the owning context's compute path was switching slots: a data race on the slot state. The slot state now has a mutex that both paths take; the compute path holds it for the slot bookkeeping only. The meta device registry (ggml_backend_meta_device) and the per-device buffer-type registry were static maps with no lock, so two models loading on threads of their own, or a model and its drafter, raced on them too. Both take a mutex now; their entries are std::map nodes, so the pointers they hand out stay valid after the lock is released. test-meta-graph-slots drives the interleaving on a meta device over the CPU device: one meta backend alternates graphs from two compute buffers while another, on a second thread, allocates, computes and frees graphs of its own. Under ThreadSanitizer it reported the slot-state race on the previous commit and the registry races once that was locked, and reports nothing with this one; on every build it checks both backends keep computing the right values.
|
Round 2, head Copilot's review of Pending for merge: this head's CI ( |
|
Read back on
Nothing left on my side; ready for review and merge. |
|
Addressed #9 (review). This review lists no open issues; its headline asks for maintainer review of the cross-context cache invalidation and backend lifetime beyond the reported CPU ThreadSanitizer coverage. The evidence for the two changes:
The remaining cross-context lifetime point is, as the headline says, a maintainer-judgment call rather than a reproduced defect; flagging it as ready for that review. No code change in this round. |
The buffer-type registry held each context as a raw pointer allocated with new and never freed, so under LeakSanitizer the meta-graph-slots test (label main, run by the address-sanitizer job) reported 64 bytes leaked in 2 allocations from ggml_backend_meta_device_get_buffer_type and exited 1. The registry now owns the contexts with unique_ptr, as the meta-device registry beside it already does; the test exits 0 under AddressSanitizer and ThreadSanitizer.
|
Addressed #9 (review) in b2d1f26. Its headline had two parts:
|
|
Addressed #9 (review). This review of b2d1f26 lists no open issues and confirms the leak thread resolved. Its headline asks for final human review of the cross-context cache lifetime and synchronization changes beyond the CPU sanitizer coverage; that is a maintainer-judgment call rather than a reproduced defect, so this PR now waits on a maintainer's review. No code change. |
…or its tests (#10) * ci: run the sanitizers on pull requests that change the meta backend or its tests build-sanitize.yml ran only on pushes to master and on changes to itself, so a pull request to spiceai-b11803 never saw ThreadSanitizer. The meta backend is the one place in the fork where several contexts share process-wide state from different threads (the graph-slot cache a buffer free touches, the device and buffer-type registries), and #9 reproduced a race there under ThreadSanitizer that every other job passed. A pull request that changes the meta backend, the backend interface header, or a meta test now runs the ADDRESS, THREAD and UNDEFINED jobs; the test that drives those paths, test-meta-graph-slots, carries the main label, so ctest runs it under each. * ci: skip the self-hosted UNDEFINED sanitizer leg on pull requests The UNDEFINED leg runs on a self-hosted X64 Linux runner this repository does not have, so on a pull request it stayed pending for good (#10's own run: ADDRESS and THREAD passed on ubuntu-24.04 in 20 and 16 minutes, UNDEFINED never started). A pull request now runs the two GitHub-hosted legs and skips the third; pushes to master are unchanged. * ci: choose the sanitizer legs by event in the matrix, since a job-level if cannot see the matrix The previous commit put the pull-request skip in a job-level if that referenced matrix.sanitizer; the matrix context is not available there, so the workflow failed to parse and its run had no jobs. The matrix is now built from the event with fromJSON: a pull request gets the ADDRESS and THREAD legs on ubuntu-24.04, a push to master keeps all three. * ci: run the UNDEFINED sanitizer leg on a GitHub-hosted runner for pull requests A pull request that changes the meta backend ran ADDRESS and THREAD only, which is not the three sanitizers the change set out to run. The UNDEFINED leg now runs on ubuntu-24.04 for pull requests (its Debug configure step is unchanged) and keeps its self-hosted runner for pushes to master.
…) into the tensor-parallel mesh branch #9 decides backend sampling again after the sampler chain is built; without it a response_format request aborted llama-server under LLAMA_ARG_BACKEND_SAMPLING=1, which failed this branch's "Tests (Backend sampling)" job. #12 conflicted in rpc_server::comm_state: the merge keeps its nccl_wire_bf16 field beside the mesh fields.

What this changes
Two follow-ups to #7, which merged (
59d512374) between its first and second review rounds, so the second round's commit never reachedspiceai-b11803:Backend sampling is decided again after the sampler chain is built. Add opt-in decode levers for tensor-parallel DeepSeek-V4.1 serving: graph slots, backend sampling, sampler reuse, inline yield, RPC pipelining, DSV4 verify-graph reuse #7's first review round moved the backend-sampling decision ahead of
common_sampler_initso the kept-sampler key could carry it, butcommon_sampler_initturns backend sampling off for a chain it builds with a grammar or a reasoning budget, which the backend cannot sample. The chain was attached anyway, and aresponse_formatrequest aborted the server:common/sampling.cpp:629: GGML_ASSERT(!gsmpl->grmr && "using grammar in combination with backend sampling is not supported"). The decision is now checked again after the chain exists (use_backend_sampling &= task.params.sampling.backend_sampling), so such chains sample on the CPU as they did before Add opt-in decode levers for tensor-parallel DeepSeek-V4.1 serving: graph slots, backend sampling, sampler reuse, inline yield, RPC pipelining, DSV4 verify-graph reuse #7. This affects any build of59d512374that runs with backend sampling and receives a JSON-schema or grammar request.A freed compute buffer drops its parked graph split. A meta backend keeps one graph split per compute buffer while graphs come from two schedulers (graph slots). Recreating the schedulers freed their compute buffers without touching the splits parked under them, so toggling a lever that recreates the schedulers grew the cache by a split per distinct buffer address. A buffer being freed now drops the split kept for it, through a registry of the live meta backends (
ggml_backend_meta_forget_buffer).The graph-slot state a buffer free touches, and the meta device and buffer-type registries, take a lock. Copilot's review of this PR found that the buffer-free notification above reads
active_bufand erases fromparkedon every live meta backend from the freeing thread, which can be another context's compute thread (a model unloaded while a second decodes, a drafter's scheduler recreated while the target computes), with nothing ordering it against that context's slot switch. The slot state now has a per-contextslots_mutex, taken byforget_bufferand held byggml_backend_meta_graph_computefor the slot bookkeeping only. The test written to show it also caught the static registries inggml_backend_meta_device(// TODO: this is not thread-safe) andggml_backend_meta_device_get_buffer_typeracing when two threads first ask for a device or its buffer type, as two models loading on threads of their own do; both take a mutex, and their entries arestd::mapnodes, so the pointers they return stay valid after the lock is released.tests/test-meta-graph-slots.cpp(new, labelmain) runs one meta backend alternating graphs from two compute buffers while a second backend on another thread allocates, computes and frees graphs of its own, and checks both keep computing the right values.The first two are the fixes for Copilot's second review of #7 (#7 (review)), answered there in #7 (comment); the commit is the same one (
095078405), cherry-picked onto the merged base.Evidence
Tests (Backend sampling)job on046d98ce5(https://github.com/spiceai/llama.cpp/actions/runs/40271195149/job/121404173282) failedunit/test_chat_completion.py::test_completion_with_response_format[response_format0-6-"42"]with the assertion above, and the server's abort tookunit/test_speculative.py::test_synth_ignores_target_tokensdown with it (RemoteDisconnected). The same job on this PR is the after-evidence.ggml-backend-meta.cpp,select_slot/sched_reserve), not reproduced at runtime: no test toggles backend sampling under graph slots.test-meta-graph-slotsunder ThreadSanitizer (-DGGML_SANITIZE_THREAD=ON -DLLAMA_SANITIZE_THREAD=ON -DGGML_OPENMP=OFF, Apple clang 21, arm64) onc7b0f37cbreported two races,ggml_backend_meta_context::forget_buffer(ggml-backend-meta.cpp:1988, readingactive_buf) againstggml_backend_meta_graph_compute(:2238, writing it) andparkederased while the compute path rehashed it, exit 134. Withslots_mutexalone, five reports remained, all inggml_backend_meta_device_get_buffer_type(:357/:376); with the registry mutexes too, three runs reported nothing, exit 0 (OK: 85607 rounds over two compute buffers while the other backend allocated and freed 50 graphs). The plain Release build passes it in 0.02 s (freed 284/312/387 graphs). The full reports are in the review thread (Decide backend sampling after the sampler chain is built, and drop a freed compute buffer's parked graph split (follow-ups to #7) #9 (comment)).cmake --build build-cpu --target llama-server ggml-rpc-serverexit 0 onc7b0f37cband on903def3c8.What is not covered
Lab runs: none. The levers' measured configuration (greedy, NCCL, completions only, one model and one slot thread) does not reach any of the three changes; the cross-context path exists only when two contexts compute or load on different threads.
build-sanitize.ymlruns only on pushes tomaster, so CI runs the new test without a sanitizer and the ThreadSanitizer run above is local.