Skip to content

perf(lfm2_audio): keep several conversations in an S2S session - #853

Merged
0xShug0 merged 3 commits into
0xShug0:mainfrom
Liquid4All:lfm2-audio-multi-conversation
Oct 9, 2026
Merged

0xShug0 merged 3 commits into
0xShug0:mainfrom
Liquid4All:lfm2-audio-multi-conversation

Conversation

@ykhrustalev

Copy link
Copy Markdown
Contributor

Follow-up to #845. An S2S session now remembers up to four conversations instead of one. Before, when two clients took turns on the same server model, each turn threw away the other client's cache, so neither got #845's speedup. Now each keeps its own. Output does not change.

Problem

What this PR changes

  • A new option, lfm2_audio.conversation_cache_slots: how many conversations an S2S session remembers. Default 4. 0 turns it off
  • The backbone keeps the prefill blocks of up to that many conversations, and the session keeps their questions' encoder output. When a fifth comes in, the one used longest ago is dropped
  • A turn finds its conversation by content, checked bit for bit as in perf(lfm2_audio): reuse the turn before's prefill and encoder output #845. There is no new request field and no server, CLI or C API change
  • Conversations that start the same way, such as with the same long system prompt, share those blocks
  • Two timing log values, lfm2_audio.prefill.kept_prompts and lfm2_audio.prefill.kept_mib, show what is kept
  • Tests and docs

What changes for users

  • Output does not change
  • Up to four clients taking turns on one model each get the speedup they would get alone
  • One client alone: same bytes and speed as main
  • Memory: an unused slot takes nothing. A remembered conversation takes about 75 MiB after ten turns, and at most about 350 MiB at the 8192-step limit, so four very long conversations could hold about 1.4 GiB. In the runs below, the server used 51 to 79 MiB more than main with two conversations and 137 MiB more with three
  • To use less memory, set the option in the model's session_options: "1" behaves like perf(lfm2_audio): reuse the turn before's prefill and encoder output #845, "0" keeps nothing. For more clients at once, set it higher

Testing
On an Intel Core i9-13900HK laptop CPU, main 6c3ab9a against this PR, English Q4_0, 8 threads, through the server with default settings. Every turn's reply, text and audio was compared with main's byte for byte.

  • Unit tests pass. The new tests fail if the default changes or the block keeping is broken
  • Same bytes as main on all 160 turns: two clients taking turns (streamed on both server runtimes, and offline), three clients taking turns, one client alone, and the option at 1 and 0
  • With two or three clients, every turn reused as much as it does alone
  • Two clients taking turns:
main this PR
A, turn 10, first audio 11.2 s 1.7 s
B, turn 10, first audio 12.5 s 2.1 s
A, ten turns, prefill 55.6 s 15.9 s
B, ten turns, prefill 65.5 s 16.6 s
  • An earlier version of this branch also matched main's bytes on an M3 Ultra with Metal
  • Not tested: CUDA, Metal at this version, Japanese, other quantizations, more than three clients through the server (four and five are in the unit tests)

Build and run

cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DENGINE_BUILD_TESTS=ON \
  -DAUDIOCPP_MODEL_SET=custom -DAUDIOCPP_MODELS=lfm2_audio
ninja -C build audiocpp_server cpu_extra_buffers_test model_spec_system_test \
  lfm2_audio_asr_inputs_test lfm2_audio_assets_test lfm2_audio_backbone_test lfm2_audio_chat_test \
  lfm2_audio_encoder_test lfm2_audio_features_test lfm2_audio_session_test \
  lfm2_audio_tokenizer_test lfm2_audio_tts_test
(cd build && ctest -R 'lfm2_audio_.*_test|cpu_extra_buffers|model_spec_system_test')
./build/bin/audiocpp_server --config server.json --log-file server.log

with a server.json such as

{"backend": "cpu", "threads": 8,
 "models": [{"id": "lfm2-audio-s2s", "family": "lfm2_audio", "task": "s2s", "mode": "streaming",
   "path": "models/LFM2.5-Audio-1.5B-GGUF",
   "session_options": {"lfm2_audio.model_gguf": "LFM2.5-Audio-1.5B-Q4_0.gguf"}}]}

Send each turn with "return_codes": true and the earlier turns as artifacts, as in the Conversations section of docs/community_models/lfm2_audio.md.

The backbone kept the full prefill blocks of its last chunked prefill
only, so two conversations taking turns on one runtime restored at
most the blocks they start with in common: each turn's blocks
replaced the other's.

It now keeps those of up to prefill_cache_slots prompts, a new
trailing constructor parameter (1 by default, as before; 0 keeps
none), and drops the least recently used first. A chunked prefill
compares its full blocks from the start with those of all kept
prompts, one block at a time, and restores the blocks it starts with
from the kept prompt that starts with the most of them. The full
blocks it runs after them go on the end of that kept prompt when the
kept prompt ends there, as for a conversation's next turn, or else
into a new kept prompt that shares the restored blocks. So
conversations that start the same, for example with one long system
prompt, each keep their own blocks from where they part. A kept
prompt that already holds all of a request's full blocks is left as
it is. runtime::CacheSlots finds its entries by equal keys, so it
cannot restore the longest matching start of a kept prompt or share
blocks between prompts; the backbone keeps its own list instead.

Blocks are still compared bit for bit, and restored only as the start
of one kept prompt, so every request gives the bits it gave before.
With one slot, what is kept is what was kept before, except that a
chunked prompt under 256 steps no longer empties it, and a prompt
whose full blocks are all the start of the kept one no longer drops
the blocks after them. Both change only what a later request can
restore. They can keep blocks that were freed before, but no more
than one prompt's.

After each chunked prefill the timing log also reports how many
prompts are kept and the MiB their blocks take, shared blocks once
(lfm2_audio.prefill.kept_prompts and lfm2_audio.prefill.kept_mib).

lfm2_audio_backbone_test runs two prompts taking turns at 1, 2 and 8
threads and with one slot, one prompt run twice with no slots, the
least recently used dropped, a prompt that parts inside a kept one,
prompts of whole blocks, a shorter prompt and one under a block, a
one-shot request in between, and prompts that share their first 300
steps, each chunked request against a fresh runtime bit for bit.
An S2S session kept the questions and prefill blocks of one
conversation. A server entry runs every request on its one session,
so clients that took turns on one entry replaced each other's: each
turn encoded its earlier questions again and ran its prefill again
from where the two conversations differ.

The new session option lfm2_audio.conversation_cache_slots, an
integer of at least 0 (4 by default), sets how many conversations a
session keeps them for. Its backbone keeps the blocks of that many
prompts, and the session keeps that many question lists, the most
recent first. An earlier question takes the encoder output of any
kept question with the same samples. A request's questions go in
front and replace the kept list whose last question the request also
asks (the one holding the most of its earlier questions if several
do); if no list qualifies, the least recent list past the slots is
dropped. With one slot the session keeps the questions it kept
before, and the backbone what the commit before describes; 0 keeps
nothing. ASR and TTS sessions ignore the option.

Output does not depend on it: blocks are restored and encoder output
taken only for inputs that match bit for bit. A slot takes memory
only once a conversation uses it: about 75 MiB at the tenth turn of a
conversation of 2900 steps, and up to about 350 MiB at the 8192-step
limit. So four slots can hold up to about 1.4 GiB, where one held up
to about 350 MiB.

Through the server on an Intel Core i9-13900HK laptop CPU at 8
threads with the Q4_0 package, two clients taking turns for ten turns
each on one streaming entry without the option gave the bytes they
gave before this change, and each turn restored the steps and reused
the questions that it does when its conversation has the entry to
itself. Their ten prefills took 15.9 s and 16.6 s instead of 55.6 s
and 65.5 s, their tenth turns reached their first audio in 1.7 s and
2.1 s instead of 11.2 s and 12.5 s, and the server's peak RSS was
1532 MiB instead of 1458 MiB. With three clients in round robin the
bytes were also the same, and every turn reused all its earlier
questions. With one slot, the two clients restored and reused
nothing, as before this change.

lfm2_audio_chat_test runs conversations taking turns on one session,
offline and streamed. With two slots each turn gives the bytes of a
session of its own and restores and reuses at least what it does
there, also for two conversations with one long system prompt and for
two that ask the same clips, and a third conversation drops what was
used least recently. With one slot and with none the bytes are the
same, and nothing is restored or reused. Without the option, four
conversations taking turns each reuse what they reuse alone and
restore at least as much, and a fifth drops the questions of the
least recently used. test_turns_reuse now sets one slot, as it checks
that another conversation in between replaces what was kept. The test
also checks the option's values.
Describe lfm2_audio.conversation_cache_slots (4 by default): what a
session keeps for each conversation and how a request finds its
conversation's, the host memory that takes, and the timing scalars
that report the kept blocks. After the paragraph on SSE, say that
clients taking turns on one server entry share its slots, how to keep
more conversations, and the memory each adds, which the server's
min_free_memory_mb guard does not count when a model loads. Also say
to set one slot more when clients change or leave out earlier turns,
add the option's row to the session options, and update the Memory
section and the description of the unit tests.
@0xShug0
0xShug0 merged commit 427fa51 into 0xShug0:main Oct 9, 2026
6 checks passed
@0xShug0

0xShug0 commented Oct 9, 2026

Copy link
Copy Markdown
Owner

@ykhrustalev Thanks! PR mrged.

@0xShug0

0xShug0 commented Oct 10, 2026

Copy link
Copy Markdown
Owner

@ykhrustalev Congrats! https://x.com/EricBezzam/status/2108658089797009540?s=20 The audio.cpp version of LFM2.5-Audio ranks #2 on the Higgs Audio Open TTS Leaderboard! Thanks for all the effort you've put into it!

0xShug0 pushed a commit that referenced this pull request Oct 10, 2026
* webui: let a studio panel run the page's request

Model panels are registered per family, and the page always builds and
sends the request itself. A conversation view has to send its own
request and show the reply as it streams in, while it keeps the page's
Run button, Ctrl+Enter, Cancel, elapsed time and status line.

This adds that hook without changing what any existing panel does:

- A panel entry may list the tasks it covers, such as tasks: ['s2s'].
  An entry without the list covers every task of its family, as before.
- With requestMode 'panel', run() hands the request to the runner the
  panel registers through the page's registerPanelRunner, which the
  next commit passes to panels as setPanelRunner; registering returns
  the function that removes it. It passes the source audio, the
  resolved seed, Max tokens, Language, the request options and the
  abort signal that Cancel uses. The hand-off comes before anything is
  awaited, so the panel can start audio inside the Run click or key
  press. The reply the panel returns is shown in the Result column;
  these runs are not added to Run history. Missing source audio, a
  recording still running, or Run from a tab other than Studio is
  reported as a warning, like the page's other checks before a run.
- With UI management the panel can ask for the entry in a given mode.
  This uses the page's ensureLoadedMode unchanged, after reloading an
  entry that is resident with another package or with imported
  settings that differ, as ensureLoaded does for the other runs.
- api.ts gets taskStreamEvents, a reader for /v1/tasks/stream with
  "stream_format": "sse". It yields each event and then the result,
  throws the server's error message, and throws TaskStreamClosedError
  when the stream ends before task.stream.done. The done message
  carries the whole reply again and comes over many reads, so the
  reader keeps its pieces and joins them once, when the message ends.

No panel uses requestMode 'panel' yet.

* webui: LFM2.5-Audio conversation panel for speech-to-speech

The LFM2.5-Audio speech-to-speech entries ran one question per Run, as a
new conversation each time, and showed the reply only when it was done.
The server can now carry a conversation (earlier turns sent as request
artifacts) and stream a reply as server-sent events, so this gives the
two S2S entries a page of their own, as asked on #828.

The panel covers only the S2S entries; ASR and TTS keep the generic
controls. It adds a conversation above the page's own Language, Seed,
Max tokens, Source audio and Model parameters, which it uses as they
are. Each Run:

- uploads the recorded or chosen question, and sends it to
  /v1/tasks/stream with "stream_format": "sse" and return_codes, with
  each earlier turn as its question's upload path and the reply
  artifact that turn returned;
- plays the reply as it streams in, its 80 ms chunks scheduled back to
  back on one Web Audio clock, and shows the reply text as it arrives;
- adds the turn to a list with the question, the reply text and the
  reply audio, and clears the source picker for the next question.

New conversation starts over, and Leave out drops an old turn from the
next request, which is what the server asks for past its 8192-step
limit. A failed or stopped turn, or one whose stream closed before its
result, is never sent as history; the next Run takes its place.
Starting a turn stops the previous reply, and Stop audio stops a reply
that is still playing. If the browser does not let live audio start,
the panel says so and the reply is in its player when it finishes.
Finished turns are kept when the user opens another tab or entry and
comes back (leaving the panel stops a turn still running), and a turn
on the other checkpoint starts a new conversation. With UI management
the entry is loaded in streaming mode when needed; on a server with a
config file, an entry in offline mode is run through /v1/tasks/run and
its reply shows when it is done. An S2S entry configured under an id
that is not the catalog's gets the family's ASR parameters in Model
parameters; turns leave those out and send the page's Max tokens.

Each turn's players and its Leave out button are named with the turn
number for screen readers, and its state badge is a polite live
region.

The page passes panels three more props for this: modelId,
setPanelRunner, which is its registerPanelRunner, and clearSource, its
own clearSourceFile. The other panels do not declare them and ignore
them. model_params.json gets text_temperature and text_top_k for the
two S2S entries, with the server's defaults, and the S2S hints and the
model docs describe the conversation.

* webui: rebuild the page

Rebuilt from this branch's source with the lockfile (node 24.19.0,
npm 11.17.0):

  cd webui/native
  npm ci
  npm run build

The page on main was built from an older tree. It lacks changes to the
model specs or the page from #824, #829, #830, #833, #834, #835, #846,
#851, #853 and #854, so this rebuild also brings those into the page.
Rebuilding main's own source the same way gives a page that differs
from this one only in this branch's changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants