Skip to content

Decide backend sampling after the sampler chain is built, and drop a freed compute buffer's parked graph split (follow-ups to #7) - #9

Merged
lukekim merged 3 commits into
spiceai-b11803from
lukim/b11803-review-followups
Oct 27, 2026
Merged

lukekim merged 3 commits into
spiceai-b11803from
lukim/b11803-review-followups

Conversation

@lukekim

@lukekim lukekim commented Oct 26, 2026 •

Copy link
Copy Markdown

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 reached spiceai-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_init 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 a response_format request 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 of 59d512374 that 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_buf and erases from parked on 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-context slots_mutex, taken by forget_buffer and held by ggml_backend_meta_graph_compute for the slot bookkeeping only. The test written to show it also caught the static registries in ggml_backend_meta_device (// TODO: this is not thread-safe) and ggml_backend_meta_device_get_buffer_type racing 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 are std::map nodes, so the pointers they return stay valid after the lock is released. tests/test-meta-graph-slots.cpp (new, label main) 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

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.yml runs only on pushes to master, so CI runs the new test without a sanitizer and the ThreadSanitizer run above is local.

…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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread ggml/src/ggml-backend-meta.cpp Resolved
…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.
Copilot AI balanced review requested due to automatic review settings October 26, 2026 06:34
@lukekim

lukekim commented Oct 26, 2026

Copy link
Copy Markdown
Author

Round 2, head 903def3c8.

Copilot's review of c7b0f37cb had one finding: the buffer-free notification touched every live meta backend's slot state from the freeing thread without synchronization with that backend's compute path. Fixed with a per-context slots_mutex (taken by forget_buffer, held by graph_compute for the slot bookkeeping only), plus mutexes on the static meta device and buffer-type registries that the same test found racing. The finding was reproduced under ThreadSanitizer before the fix (two reports, exit 134) and is absent after it (three runs, no report, exit 0); the test, tests/test-meta-graph-slots.cpp, runs in the main ctest label and passes on the plain build. Details and the sanitizer output: #9 (comment) (thread resolved). The PR body carries the third change and its evidence.

Pending for merge: this head's CI (Server, ubuntu) and Copilot's review of it.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Cross-context cache invalidation and backend lifetime synchronization warrant final maintainer review beyond the reported CPU ThreadSanitizer coverage.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@lukekim

lukekim commented Oct 26, 2026

Copy link
Copy Markdown
Author

Read back on 903def3c8: every hosted check passes, and Copilot's review of this head reports no open issues.

Nothing left on my side; ready for review and merge.

@lukekim

lukekim commented Oct 26, 2026

Copy link
Copy Markdown
Author

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Cross-context buffer lifetimes and locking warrant human review, and the new test exposes an unresolved context-ownership leak.

1 open issue

Review effort: Balanced

Comment thread ggml/src/ggml-backend-meta.cpp Resolved
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.
Copilot AI balanced review requested due to automatic review settings October 26, 2026 21:30
@lukekim

lukekim commented Oct 26, 2026

Copy link
Copy Markdown
Author

Addressed #9 (review) in b2d1f26.

Its headline had two parts:

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Cross-context cache lifetime and synchronization changes warrant final human review beyond the reported CPU sanitizer coverage.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@lukekim

lukekim commented Oct 26, 2026

Copy link
Copy Markdown
Author

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.

lukekim added a commit that referenced this pull request Oct 26, 2026
…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.
@lukekim lukekim self-assigned this Oct 27, 2026
@lukekim
lukekim merged commit 41dda9b into spiceai-b11803 Oct 27, 2026
12 of 33 checks passed
lukekim added a commit that referenced this pull request Oct 27, 2026
…) 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants