The vLLM vs llama.cpp question on this machine gets the same operating style as the edge build in moving 15 WordPress sites to Cloudflare Workers: one job per unit, explicit state, and a rollback that does not depend on a person remembering the order.
Why does one GPU need a switch at all?
One GPU needs a switch because model weights, KV cache and diffusion pipelines all allocate from the same pool, and oversubscribing it ends in an out-of-memory error or a driver reset. The card has 32 GiB, which is enough for one serious workload and not enough for two.
The workloads also have different shapes. A chat model loads once and holds weights plus a growing KV cache. A video workflow loads an encoder, a diffusion model and a VAE in stages, then frees them between jobs. vLLM reserves a large share of the card for KV cache up front so it can batch requests (vLLM optimization guide). Running any two of those at once is a memory race with an unpredictable loser.
The cheap answer is a bigger card. The practical answer is scheduling: decide which workload owns the GPU, make the handover explicit, and make the wrong state hard to reach.
The three stacks compared
The three stacks are not competitors. They serve different workloads, and the switch exists because they share one card. All three run on ROCm, AMD’s compute stack for the card (ROCm documentation).
| Stack | Workload | Serving shape | Memory behaviour | When to use it |
|---|---|---|---|---|
| llama.cpp | Chat, coding and agent models | OpenAI-compatible HTTP server with a /health endpoint (llama.cpp server) |
Weights plus KV cache; one model loaded at a time | Interactive chat and tool-calling work |
| vLLM | Long-context and batch jobs | OpenAI-compatible server with continuous batching (vLLM) | Reserves VRAM for KV cache up front | Overnight and batch runs where throughput matters |
| ComfyUI | Image and video generation | Queue plus HTTP API, with /system_stats for readiness (ComfyUI) |
Loads and frees models per workflow | Visual generation, not text serving |
Which should you choose? Pick by workload: llama.cpp is the interactive default, vLLM takes the overnight batch jobs, and ComfyUI gets the card when someone is generating images or video. The dashboard automates the llama.cpp and ComfyUI pair because those are the two that get switched by hand. vLLM runs are scheduled work, so they follow the same exclusion rule without needing a button.
How does Conflicts= enforce one service at a time?
Conflicts= enforces exclusion because starting a unit with a conflict stops the other unit, and the relation works in both directions (systemd.unit). It is not an ordering rule, though, so a unit that needs the other one stopped first also declares After= on it.
[Unit]
Description=ComfyUI
Conflicts=llama-server.service
After=llama-server.service
After=network-online.target
[Service]
ExecStart=/opt/comfyui/.venv/bin/python main.py --listen 127.0.0.1 --port 8188
Restart=on-failure
[Install]
WantedBy=multi-user.target
Two details matter. First, the exclusion is enforced by systemd, not by the dashboard, so a systemctl start typed in a terminal cannot leave both services running. Second, Conflicts= does not wait for the other unit to finish dying: it schedules a stop. The dashboard still polls systemctl is-active because it needs the stop to have completed before it touches VRAM.
That combination is the point. The unit files make the wrong state hard to reach, and the dashboard makes the right sequence reliable.
The switch sequence
The switch sequence is stop, settle, start, verify. Each step exists because the previous one was not enough on its own.
- Accept one request at a time. A second request while a switch is in flight gets a 409, so two clicks cannot race each other.
- Stop the other unit and wait for inactive.
systemctl stopreturns before every process has released the device. - Wait for VRAM to settle. Poll
mem_info_vram_useduntil it stops falling. A fixed threshold does not work because the desktop session holds a variable baseline. - Start the target. systemd forks the process, which is not the same as the model being loaded.
- Poll the health endpoint.
/healthfor llama.cpp,/system_statsfor ComfyUI, until the service answers. The page shows “loading” until then.
The API reflects that state machine. POST /api/switch returns immediately with a target, and the UI watches a switch.phase field in GET /api/status rather than holding a request open. A switch that times out surfaces the systemd failure but not the cause, so the dashboard also exposes the last lines of the unit’s journal.
A scoped sudo rule instead of a root daemon
A scoped sudo rule is safer than running the dashboard as root because the web process can only invoke two verbs on two units. Everything else it does — reading sysfs, serving HTTP, tailing the journal — runs as an ordinary user.
Cmnd_Alias GPU_UNITS = /usr/bin/systemctl start llama-server.service, \
/usr/bin/systemctl stop llama-server.service, \
/usr/bin/systemctl start comfyui.service, \
/usr/bin/systemctl stop comfyui.service
automation ALL=(root) NOPASSWD: GPU_UNITS
The installer validates the file with visudo -c before moving it into place, which catches a typo before it locks out sudo. The dashboard calls sudo -n, so a missing rule fails immediately instead of hanging on a password prompt (sudoers manual).
The rule is only as tight as the unit files. If the service user can edit comfyui.service, the sudo rule becomes a way to run anything as root. Unit files belong to root, and the service user gets read access only.
Reading GPU state from sysfs
Telemetry comes from the kernel driver’s sysfs nodes, not from a ROCm Python stack. The amdgpu driver exposes GPU state under /sys/class/drm/, including VRAM usage and busy percentage (kernel amdgpu documentation).
That choice keeps the dashboard to the Python standard library: no virtual environment, no pip packages, and nothing to break when the system Python is upgraded. It reads a handful of files, formats them, and serves a page. For a single card on a single host, that is enough.
The dashboard also tails each unit’s journal for the logs panel. It does not parse the journal; it asks journalctl for the last N lines and shows them. When a switch fails, the answer is usually in the target service’s own log, not in the dashboard’s.
Why the box boots idle
The box boots idle on purpose: neither workload autostarts, so a reboot leaves the GPU free and only the dashboard comes up. A default model load at boot is convenient until the service crash-loops and holds VRAM while you are trying to diagnose it.
Enabling a default is one command per unit. The unit files ship disabled, and the README says so, because the failure mode of “the GPU is busy at 3 a.m. with a model nobody asked for” is worse than the inconvenience of one click.
What broke and what we changed
The undervolt passed the stress test and failed the real workload. A -80 mV offset passed an fp16 GEMM stress test, then hung the GPU under a sustained vLLM workload: “MES failed to respond”, a mode1 reset, VRAM lost, and vLLM crashed. A -140 mV offset also hung during testing, and the measured gain from undervolting was only about 1–2%. The offset was removed and a 270 W power cap kept. The lesson is that a synthetic matrix workload does not exercise the same power states as a batched inference server. A GPU reset also clears the power cap, so gpu-tune has to be restarted after one.
“Active” was not “ready”. A started unit is not a finished switch: ComfyUI can take about 15 seconds to load before /system_stats answers, and a model load takes longer than the fork. The page says “loading” until the health endpoint responds, which removes a class of “it says it is up but the request fails” confusion.
A fixed VRAM floor does not hold. Waiting for VRAM to drop below a number works on a fresh boot and fails once the desktop session has allocated its own buffers. Polling until the value stops falling is less tidy and works in both cases.
Manual processes are invisible to systemd. A model server started by hand from a shell has no unit, so Conflicts= cannot stop it and the dashboard cannot see it. Keeping the workloads in unit files is what makes the exclusion total.
Related
- Moving 15 WordPress sites to Cloudflare Workers — the same one-job-per-unit discipline at the edge.
- Zero open ports: Cloudflare Tunnel and Access in front of everything — how the GPU services are reached without a public port.
- systemd timers as a deploy pipeline — the other place where unit files carry the operational rules.
- Capping Docker build memory with cgroups v2 — the same problem of one workload starving its neighbours.
- Infrastructure and edge work — the discipline this project sits under.
Frequently asked questions
Can llama.cpp and ComfyUI share one GPU?
Not on this card. The LLM's weights and KV cache plus ComfyUI's video pipeline exceed 32 GiB, and even when they fit, the two workloads compete for memory bandwidth. Time-slicing works for small models and short jobs; for a 27B model and video generation, one at a time is the reliable option.
What does Conflicts= do in systemd?
Conflicts= declares a negative dependency: starting a unit stops the units it conflicts with, and starting any of those stops it. It does not imply ordering, so a unit that needs the other stopped first should also declare After= or Before=. It is enforced by systemd itself, so it applies to manual systemctl calls too.
Why not run the switching dashboard as root?
Because a web process that runs as root turns any bug into a root compromise. Running it as an ordinary user with a sudo rule for two units limits the blast radius to starting or stopping those units. The installer validates the rule with visudo -c, and the dashboard uses sudo -n so a missing rule fails fast.
How do you know the model is ready, not just the service?
Poll the workload's own health endpoint. llama.cpp exposes /health, and ComfyUI exposes /system_stats; a 200 from either means the server is answering, which is later than systemd reporting the unit active. The dashboard keeps the UI in a loading state until that check passes.
Is vLLM better than llama.cpp?
Neither is better in general. vLLM's continuous batching and KV-cache reservation suit many concurrent requests and long-context batch jobs. llama.cpp builds against ROCm here, runs a single model well, and exposes the endpoints agent tools expect. The choice here is by workload, not by benchmark.
Why read GPU stats from sysfs instead of ROCm tools?
Because the values the dashboard needs — VRAM used, GPU busy, temperature, power — are already exposed by the kernel driver under /sys/class/drm/. Reading them avoids a ROCm Python dependency and keeps the dashboard to the standard library, so a system Python upgrade cannot break it.
What happens after a GPU reset?
The reset clears the power cap set by gpu-tune, so the card returns to its default limit until that unit is restarted. The workload that triggered the reset is already down, and the dashboard shows the unit as failed. Restarting gpu-tune and then the workload is the recovery order.
