[GH-ISSUE #6498] [Issue]: Excessive thrashing between local models for Local LLM + ComfyUI + llama-swap on single GPU system #3639

Closed
opened 2026-10-21 06:05:23 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @JamesMowery on GitHub (Sep 21, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6498

Originally assigned to: @SpicyMarinara on GitHub.

Summary

Note: I wanted to start this more as a general discussion and visibility more than report a conclusive/definitive issue, as logging hasn't been able to pinpoint exactly what is going on.

Using Marinara Engine on a single GPU setup to generate both LLM responses and ComfyUI generations is currently very difficult. Specifically in a situation where ideally the entire LLM's VRAM and ComfyUI's VRAM is made available for generation and then cleared before handing back. It gets really ugly and really difficult to debug when things go wrong.

Installing something like llama.cpp and ComfyUI on the same system with a limited amount of VRAM makes this near impossible. But freeing up VRAM and then generating the LLM request and/or Image Gen request works well. But neither llama.cpp or ComfyUI can talk to each other to figure out when that happens.

llama-swap helps alleviate this issue as it can evict the VRAM in a FIFO fashion for the given request. I recently learned it can also intelligently handle ComfyUI requests, which is greaat!

The problem then is that Marinara Engine can and will constantly ping llama-swap for information on job status, and llama-swap interprets that as a model switch requests and can cause a constant and endless back and forth of loading ComfyUI vs loading llama.cpp models. Add various agents to the mix and it gets crazier.

Expected behavior

Ideally a flow like this: Marinara Engine LLM responds > Marinara Agent requests an image > LLM is evicted from VRAM > ComfyUI is spun up to generate an image with full VRAM available > ComfyUI returns image > ComfyUI unloads VRAM > LLM loads back into VRAM -- conversation continues.

llama-swap operates like this. It operates on a FIFO system. But the problem is that a ComfyUI job does not "reserve" its spot in the queue and block future LLM requests from happening. llama-swap thinks it did its job since it fired off the request. Perhaps Marinara Engine is greedy in expecting ComfyUI to be working independently and doesn't consider a single GPU setup.

The request here (I think) is for Marinara Engine to give consideration for a single GPU setup for requesting local LLM, images, video, and embedding and have a proper queue/system that will allow those processes to finish before firing off more requests.

llama-swap, again, helps massively with this situation. But there are still probably issues with how Marinara Engine assumes that the generation requests for different media/situations (embedding, image gen, video gen) are being worked on independently.

Actual behavior

When using Marinara Engine locally with local LLM and local ComfyUI, there can be lots of issues (I believe) when requests come into the different models.

I, for example, have setup ComfyUI and llama.cpp to be handled via llama-swap (which can dynamically load models as needed and handles this as a FIFO process currently) on my RTX 5090.

This is just a small block from my llama-swap config to handle this:

macros:
  "llama": "/mnt/big/AI/applications/llama.cpp/build/bin/llama-server --port ${PORT} --flash-attn on --jinja --parallel 1"
  "models_path": "/mnt/big/AI/models/llamacpp"
  "layers_all": "--gpu-layers all"
  "q8_cache": "--cache-type-k q8_0 --cache-type-v q8_0"
  "ctx_120k": "--ctx-size 122800 --fit-ctx 98304"

models:
  "Artemis-31B-Q4KM":
    cmd: |
      ${llama} \
      -m ${models_path}/Artemis-31B-v1m-Q4_K_M.gguf \
      ${layers_all} \
      ${ctx_120k} \
      ${q8_cache} \
      --mmproj ${models_path}/Artemis-31B-v1.1-mmproj-bf16.gguf
    ttl: 120

  comfyui_auto:
    name: "ComfyUI"
    checkEndpoint: /
    ttl: 120
    cmd: /usr/bin/python3 /home/james/.config/llama-swap/comfy-gate/comfy_gate.py ${PORT}

Note that this "gate" script (which is attached further below in this post) does the following and solves the problem, according to AI:

llama-swap swaps models per HTTP request. A bare ComfyUI ``POST /prompt``
returns in milliseconds, so from llama-swap's point of view ComfyUI is idle
while the actual render runs -- and the next LLM request evicts (SIGTERMs)
ComfyUI, killing the in-memory queue and the render.

This gate sits between llama-swap and ComfyUI and makes the whole render
one in-flight request:

* starts ComfyUI on an internal loopback port (gate port + 1)
* proxies every request to ComfyUI verbatim
* except ``POST /prompt``: after ComfyUI accepts the prompt, the gate holds
  the HTTP response open, polling ComfyUI's own ``/history``, until the
  render finishes (or errors / times out), then returns the original 200
  body to llama-swap

Result: while a render is in flight, llama-swap sees comfyui_auto as busy,
queues LLM requests instead of evicting ComfyUI, and swaps back to the LLM
automatically when the response completes. When idle, the model's ``ttl``
unloads it as before. Nothing about the ComfyUI API or the public port
changes, so existing connections (e.g. Marinara's /upstream/comfyui_auto
base URL) keep working untouched.

If a render fails (workflow error) or times out, the gate returns 500 with
the error text instead of a success body, so callers fail fast at the
POST instead of polling a dead prompt_id.

This seems to work fine for Convo mode. Where most of the effort is LLM and maybe once in while it will request a selfie. Works great. Can get messy if multiple convos are happening at once at a fast pace.

But when you jump into Roleplay mode and have multiple agents doing multiple things all one after another, it quickly breaks down and can cause lots of errors and issues. I think the ordering of requests (and if done in a "dumb" way) can also impact things going wrong as well more often.

Steps to reproduce

  1. Allow agents to generate images
  2. Use llama-swap on a single GPU setup
  3. Agent fires of ComfyUI request to llama-swap (llama-swap unloads the LLM, starts up ComfyUI)
  4. Immediately fire off an LLM request via Marinara Engine before the ComfyUI request completes.
  5. Marinara Engine keeps hitting an endpoint waiting for job status causing the LLM and ComfyUI models to thrash around and reload endlessly. I believe Marinara Engine has a 10 minute default timeout or something like that.

Environment

  • Marinara Engine version: v2.4.6
  • Install type: (source / installer / docker / termux / other): Docker
  • OS + version: Debian 13 + Niri WM + llama.cpp (local) + llama-swap (local) + ComfyUI (local)
  • Browser or app shell: Brave Origin

Logs, screenshots, or video

Here is the entirety of this gate script that I had AI build to help solve the model thrashing:

comfy_gate.py

The problem is that issue still happen, mostly in RP wherein you have local LLM models vs embedding models vs image gen models and agents firing off things.

Additional context

The comfy_gate.py script I had AI built helps alleviate a lot of issues and makes single GPU LLM + image gen a possibility. But it feels very hackish.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @JamesMowery on GitHub (Sep 21, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6498 Originally assigned to: @SpicyMarinara on GitHub. ## Summary Note: I wanted to start this more as a general discussion and visibility more than report a conclusive/definitive issue, as logging hasn't been able to pinpoint exactly what is going on. Using Marinara Engine on a single GPU setup to generate both LLM responses and ComfyUI generations is currently very difficult. Specifically in a situation where ideally the entire LLM's VRAM and ComfyUI's VRAM is made available for generation and then cleared before handing back. It gets really ugly and really difficult to debug when things go wrong. Installing something like llama.cpp and ComfyUI on the same system with a limited amount of VRAM makes this near impossible. But freeing up VRAM and then generating the LLM request and/or Image Gen request works well. But neither llama.cpp or ComfyUI can talk to each other to figure out when that happens. llama-swap helps alleviate this issue as it can evict the VRAM in a FIFO fashion for the given request. I recently learned it can also intelligently handle ComfyUI requests, which is greaat! The problem then is that Marinara Engine can and will constantly ping llama-swap for information on job status, and llama-swap interprets that as a model switch requests and can cause a constant and endless back and forth of loading ComfyUI vs loading llama.cpp models. Add various agents to the mix and it gets crazier. ## Expected behavior Ideally a flow like this: Marinara Engine LLM responds > Marinara Agent requests an image > LLM is evicted from VRAM > ComfyUI is spun up to generate an image with full VRAM available > ComfyUI returns image > ComfyUI unloads VRAM > LLM loads back into VRAM -- conversation continues. llama-swap operates like this. It operates on a FIFO system. But the problem is that a ComfyUI job does not "reserve" its spot in the queue and block future LLM requests from happening. llama-swap thinks it did its job since it fired off the request. Perhaps Marinara Engine is greedy in expecting ComfyUI to be working independently and doesn't consider a single GPU setup. The request here (I think) is for Marinara Engine to give consideration for a single GPU setup for requesting local LLM, images, video, and embedding and have a proper queue/system that will allow those processes to finish before firing off more requests. llama-swap, again, helps massively with this situation. But there are still probably issues with how Marinara Engine assumes that the generation requests for different media/situations (embedding, image gen, video gen) are being worked on independently. ## Actual behavior When using Marinara Engine locally with local LLM and local ComfyUI, there can be lots of issues (I believe) when requests come into the different models. I, for example, have setup ComfyUI and llama.cpp to be handled via llama-swap (which can dynamically load models as needed and handles this as a FIFO process currently) on my RTX 5090. This is just a small block from my llama-swap config to handle this: ```yaml macros: "llama": "/mnt/big/AI/applications/llama.cpp/build/bin/llama-server --port ${PORT} --flash-attn on --jinja --parallel 1" "models_path": "/mnt/big/AI/models/llamacpp" "layers_all": "--gpu-layers all" "q8_cache": "--cache-type-k q8_0 --cache-type-v q8_0" "ctx_120k": "--ctx-size 122800 --fit-ctx 98304" models: "Artemis-31B-Q4KM": cmd: | ${llama} \ -m ${models_path}/Artemis-31B-v1m-Q4_K_M.gguf \ ${layers_all} \ ${ctx_120k} \ ${q8_cache} \ --mmproj ${models_path}/Artemis-31B-v1.1-mmproj-bf16.gguf ttl: 120 comfyui_auto: name: "ComfyUI" checkEndpoint: / ttl: 120 cmd: /usr/bin/python3 /home/james/.config/llama-swap/comfy-gate/comfy_gate.py ${PORT} ``` Note that this "gate" script (which is attached further below in this post) does the following and solves the problem, according to AI: ``` llama-swap swaps models per HTTP request. A bare ComfyUI ``POST /prompt`` returns in milliseconds, so from llama-swap's point of view ComfyUI is idle while the actual render runs -- and the next LLM request evicts (SIGTERMs) ComfyUI, killing the in-memory queue and the render. This gate sits between llama-swap and ComfyUI and makes the whole render one in-flight request: * starts ComfyUI on an internal loopback port (gate port + 1) * proxies every request to ComfyUI verbatim * except ``POST /prompt``: after ComfyUI accepts the prompt, the gate holds the HTTP response open, polling ComfyUI's own ``/history``, until the render finishes (or errors / times out), then returns the original 200 body to llama-swap Result: while a render is in flight, llama-swap sees comfyui_auto as busy, queues LLM requests instead of evicting ComfyUI, and swaps back to the LLM automatically when the response completes. When idle, the model's ``ttl`` unloads it as before. Nothing about the ComfyUI API or the public port changes, so existing connections (e.g. Marinara's /upstream/comfyui_auto base URL) keep working untouched. If a render fails (workflow error) or times out, the gate returns 500 with the error text instead of a success body, so callers fail fast at the POST instead of polling a dead prompt_id. ``` This seems to work fine for Convo mode. Where most of the effort is LLM and maybe once in while it will request a selfie. Works great. Can get messy if multiple convos are happening at once at a fast pace. But when you jump into Roleplay mode and have multiple agents doing multiple things all one after another, it quickly breaks down and can cause lots of errors and issues. I think the ordering of requests (and if done in a "dumb" way) can also impact things going wrong as well more often. ## Steps to reproduce 1. Allow agents to generate images 2. Use llama-swap on a single GPU setup 3. Agent fires of ComfyUI request to llama-swap (llama-swap unloads the LLM, starts up ComfyUI) 4. Immediately fire off an LLM request via Marinara Engine before the ComfyUI request completes. 5. Marinara Engine keeps hitting an endpoint waiting for job status causing the LLM and ComfyUI models to thrash around and reload endlessly. I believe Marinara Engine has a 10 minute default timeout or something like that. ## Environment - Marinara Engine version: v2.4.6 - Install type: (source / installer / docker / termux / other): Docker - OS + version: Debian 13 + Niri WM + llama.cpp (local) + llama-swap (local) + ComfyUI (local) - Browser or app shell: Brave Origin ## Logs, screenshots, or video Here is the entirety of this gate script that I had AI build to help solve the model thrashing: [comfy_gate.py](https://github.com/user-attachments/files/32480694/comfy_gate.py) The problem is that issue still happen, mostly in RP wherein you have local LLM models vs embedding models vs image gen models and agents firing off things. ## Additional context The comfy_gate.py script I had AI built helps alleviate a lot of issues and makes single GPU LLM + image gen a possibility. But it feels very hackish. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 06:05:23 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@SpicyMarinara commented on GitHub (Sep 21, 2026):

Not a local crowd here, I'm afraid, will ask around.

<!-- gh-comment-id:5765643187 --> @SpicyMarinara commented on GitHub (Sep 21, 2026): Not a local crowd here, I'm afraid, will ask around.
Author
Owner

@SpicyMarinara commented on GitHub (Sep 22, 2026):

Thank you for explaining this in so much detail! Had a closer look, and there is a gap here: Marinara's text requests and image/video queue don't share one queue for a physical GPU. We keep an image job active until it finishes, but that doesn't prevent a separate LLM or embedding request from starting in the meantime.

For ComfyUI, we check /history/<prompt_id> roughly once a second while a render is pending. So overlapping requests could explain the swapping you're seeing, but we still need to confirm which request actually causes each unload/reload before deciding where the fix belongs.

Could you please add:

  • Your exact Marinara build/commit from Support Diagnostics, plus the llama-swap, llama.cpp and ComfyUI versions.
  • The relevant connection routes/model IDs and llama-swap group settings, including the embedding connection if you're using one. The snippet above is helpful; mainly need the missing pieces and which agents share this GPU.
  • A short, timestamped log window from Marinara, llama-swap and ComfyUI covering one failed render, including the request paths, model switches and errors. Please remove keys, private prompts and anything else sensitive.
  • Whether it still happens with just one chat and image generation enabled, then starting a text reply before the image finishes, or only once other agents/embeddings are involved. Please mention whether the gate script was in use for that run.

llama-swap also has a dedicated ComfyUI route now, with special handling for some polling paths. That isn't proof it fixes this particular overlap, so I wouldn't want to send you chasing another workaround blindly. Let's pin down one reproducible case first. Thank you! :>

<!-- gh-comment-id:5772581338 --> @SpicyMarinara commented on GitHub (Sep 22, 2026): Thank you for explaining this in so much detail! Had a closer look, and there is a gap here: Marinara's text requests and image/video queue don't share one queue for a physical GPU. We keep an image job active until it finishes, but that doesn't prevent a separate LLM or embedding request from starting in the meantime. For ComfyUI, we check `/history/<prompt_id>` roughly once a second while a render is pending. So overlapping requests could explain the swapping you're seeing, but we still need to confirm which request actually causes each unload/reload before deciding where the fix belongs. Could you please add: - Your exact Marinara build/commit from Support Diagnostics, plus the llama-swap, llama.cpp and ComfyUI versions. - The relevant connection routes/model IDs and llama-swap group settings, including the embedding connection if you're using one. The snippet above is helpful; mainly need the missing pieces and which agents share this GPU. - A short, timestamped log window from Marinara, llama-swap and ComfyUI covering one failed render, including the request paths, model switches and errors. Please remove keys, private prompts and anything else sensitive. - Whether it still happens with just one chat and image generation enabled, then starting a text reply before the image finishes, or only once other agents/embeddings are involved. Please mention whether the gate script was in use for that run. llama-swap also has a [dedicated ComfyUI route now](https://github.com/mostlygeek/llama-swap/blob/main/docs/kb/guides/upstreams/comfyui.md), with special handling for some polling paths. That isn't proof it fixes this particular overlap, so I wouldn't want to send you chasing another workaround blindly. Let's pin down one reproducible case first. Thank you! :>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#3639
No description provided.