Skip to content

server: abstract llama_memory calls to common_memory - #26221

Merged
ngxson merged 1 commit into
masterfrom
xsn/common_memory
Jul 28, 2026
Merged

ngxson merged 1 commit into
masterfrom
xsn/common_memory

Conversation

@ngxson

@ngxson ngxson commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

Overview

While trying to implement audio output head for Qwen3-Omni, I realize that we may want to have multiple contexts in server_context that all share the same sequence. So I added a new struct common_memory to be able to synchronize among them.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: yes, for migrating calls on server-context side

@ngxson
ngxson requested a review from ggerganov July 28, 2026 09:39
@ngxson
ngxson requested review from a team as code owners July 28, 2026 09:39
@pwilkin

pwilkin commented Jul 28, 2026

Copy link
Copy Markdown
Member

/bot review

@ggml-gh-bot

ggml-gh-bot Bot commented Jul 28, 2026 •

Copy link
Copy Markdown
Automated code review

Review: server: abstract llama_memory calls to common_memory

Scope: a small refactor introducing struct common_memory (wraps ctx_tgt/ctx_dft, forwards seq_rm/cp/add), migrating ~6 server call sites. Checklists run: Scope/quick-reject, General, Approach-and-design (new component). No ggml/, public-API, or model-arch changes. No security-relevant surface (all inputs are internal seq_id/pos).

Will slow the review

(point 1) Silent behavioral change in the context-shift path. tools/server/server-context.cpp:2879-2880:

slot.mem.seq_rm (slot.id, n_keep            , n_keep + n_discard);
slot.mem.seq_add(slot.id, n_keep + n_discard, slot.prompt.tokens.pos_next(), -n_discard);

The old code called seq_add on ctx_tgt with end slot.prompt.n_tokens() and on ctx_dft with slot.prompt.tokens.pos_next(). These differ when has_mtmd is true: pos_next() returns tokens.size() + sum(n_pos - n_tokens) per media chunk (server_tokens::pos_next, tools/server/server-common.cpp:247-256), so it is strictly larger than n_tokens() for multimodal prompts. The unified call now shifts positions in [n_tokens(), pos_next()) on the target context too — a behavior change for the multimodal context-shift case that wasn't there before.

This is plausibly a latent bug fix (the target side was under-shifting), but it's an unstated semantic change bundled into a pure-refactor PR. At minimum it should be called out in the PR description, ideally committed separately, and covered by a test that exercises the mtmd context-shift path (-ctk/-ctv q8_0 included). If the intent was not to change tgt behavior, the end value must be preserved per context, which the current abstraction can't express (see point 3).

(point 2) Layering: common_memory belongs in tools/server/, not common/. The ctx_tgt/ctx_dft names are server/speculative-decoding terminology (common/common.h:951, common/common.cpp:1538-1564). common/ is shared across all tools; growing it with a server-specific dft abstraction is the wrong direction. The helper is ~6 lines and has no users outside server-context.cpp; it could just as well be a pair of static functions in an anonymous namespace inside tools/server/server-context.cpp, leaving common/ untouched.

(point 3) The abstraction doesn't serve the stated future goal. The PR description says the motivation is "multiple contexts in server_context that all share the same sequence." But common_memory is hard-coded to two named slots, ctx_tgt/ctx_dft. It neither generalizes to N contexts nor moves toward the audio-output-head design. As written it's a near-duplicate-removal only; a maintainer will reasonably ask why the indirection was added. Either make it genuinely generic (e.g. a vector of contexts the methods loop over) or defer until the multi-context feature is actually built (YAGNI).

(point 4) Duplicate pointer state in the slot. server_slot now holds ctx_tgt/ctx_dft (server-context.cpp:164-165) and mem.ctx_tgt/mem.ctx_dft (mem initialized at :1299). They're set once at slot creation today, so they stay in sync, but it's a latent footgun: any future reassignment of slot.ctx_tgt/ctx_dft without mem.init will silently desync. Prefer letting mem own the pointers (expose accessors) or dropping the slot-level duplicate members.

(point 5) common_memory can't express the dft-only case, so it's bypassed. server-context.cpp:2989-2993 was migrated to a raw llama_memory_seq_rm(llama_get_memory(ctx_dft), ...) + GGML_ABORT instead of slot.mem.seq_rm, because that path must touch only ctx_dft. This is a signal the abstraction is too narrow for the call sites it's meant to unify. Also, the inline abort message ("failed to remove sequence %d\n") lost the p0/p1 context the old common_context_seq_rm abort included ("failed to remove sequence %d with p0=%d, p1=%d\n"), a minor diagnostics regression. Either extend the abstraction to take a target selector, or keep a single shared helper that all call sites use.

Nits

(point 6) No linked issue. This is refactor prep for an unmerged feature (Qwen3-Omni audio output head). Per CONTRIBUTING.md, a nontrivial design change should start as an issue/discussion so maintainers can weigh in on whether common_memory is the right abstraction before it's baked into headers. Worth opening an issue first, especially given points 2-4.

(point 7) Stray space before ( in the new declarations (common/common.h:958, tools/server/server-context.cpp:2879, 3196): seq_rm (. Matches the pre-existing free-function style, so consistent, but the project generally avoids that spacing.

(point 8) common_memory methods are marked const despite mutating the underlying contexts through the stored pointers. That's correct (the pointers are non-owning pointees), just noting it's intentional and worth a one-line comment if reviewers ask.

No security or correctness bugs found beyond the context-shift behavior change in (point 1). The migration of the speculative-decode restore path (:3824-3830) reorders load_tgt/load_dft before a single seq_rm, but the two contexts are independent so the order change is fine.

This review was generated automatically by pi coding agent using zai-org/GLM-5.2. It may contain mistakes. Maintainers make the final call.

@ngxson

ngxson commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator Author

I think the most important review would be one from @ggerganov though, not sure if you think it will be a good idea to structure the contexts this way

@pwilkin

pwilkin commented Jul 28, 2026

Copy link
Copy Markdown
Member

Yeah I just wanted to check if I'm not missing any behavioral changes with the interleaves.

@pwilkin

pwilkin commented Jul 28, 2026

Copy link
Copy Markdown
Member

But I think the "vector of contexts" generalization question is actually a valid one.

Comment on lines +2991 to +2993
if (!llama_memory_seq_rm(llama_get_memory(ctx_dft), slot.id, ckpt.pos_max + 1, -1)) {
GGML_ABORT("failed to remove sequence %d\n", slot.id);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's a good change. This particular call seems a bit out of place - should look for ways in the future to abstract it through common_memory too.

@ngxson

ngxson commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator Author

But I think the "vector of contexts" generalization question is actually a valid one.

the number of contexts are deterministic, it's fine to keep branching code for now. generalization can be applied in the future, just a cosmetic improvement IMO

@ngxson
ngxson merged commit ee3d1b5 into master Jul 28, 2026
23 of 26 checks passed
huaxel pushed a commit to huaxel/strix-llama.cpp that referenced this pull request Aug 2, 2026
huaxel pushed a commit to huaxel/strix-llama.cpp that referenced this pull request Aug 6, 2026
huaxel pushed a commit to huaxel/strix-llama.cpp that referenced this pull request Aug 10, 2026
satindergrewal pushed a commit to satindergrewal/llama.cpp that referenced this pull request Aug 12, 2026
huaxel pushed a commit to huaxel/strix-llama.cpp that referenced this pull request Aug 15, 2026
huaxel pushed a commit to huaxel/strix-llama.cpp that referenced this pull request Aug 26, 2026
thecodacus pushed a commit to thecodacus/llama.cpp that referenced this pull request Sep 7, 2026
huaxel pushed a commit to huaxel/strix-llama.cpp that referenced this pull request Sep 8, 2026
zbrad pushed a commit to zbrad/llama.cpp that referenced this pull request Sep 10, 2026
huaxel pushed a commit to huaxel/strix-llama.cpp that referenced this pull request Sep 12, 2026
huaxel pushed a commit to huaxel/strix-llama.cpp that referenced this pull request Sep 29, 2026
huaxel pushed a commit to huaxel/strix-llama.cpp that referenced this pull request Sep 29, 2026
huaxel pushed a commit to huaxel/strix-llama.cpp that referenced this pull request Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants