From 4ce3835bcbb00e2ad618d6d8a03fc506197909fd Mon Sep 17 00:00:00 2001 From: Ziad Arafat Date: Thu, 12 Nov 2026 14:36:44 -0700 Subject: [PATCH] Document gpu-broker builds, CLI usage, API, and deployment --- README.md | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..f236c6e --- /dev/null +++ b/README.md @@ -0,0 +1,168 @@ +# gpu-broker + +A small Go service that coordinates GPU memory between jobs sharing an NVIDIA machine. It grants renewable leases, queues work by priority, and lets cooperating clients fall back to the CPU when their wait expires. + +The project provides: + +- **`gpu-broker serve`** — VRAM sampling through `nvidia-smi`, a lease/status HTTP API, and Prometheus metrics. +- **`gpu-broker ollama-proxy`** — an Ollama reverse proxy that requests a lease before loading a model. +- **`gpu-run`** — a command wrapper that leases a GPU or runs the command with a systemd CPU quota. + +This is an advisory scheduler, not a GPU isolation layer. Applications must participate in leasing and honor device-selection environment variables. Other GPU consumers can still allocate memory; per-GPU reserves leave headroom for them. + +## Requirements + +- Go **1.27.3 or newer** to build; no Go runtime is needed for the compiled binaries. +- NVIDIA drivers and `nvidia-smi` on the machine running the broker. +- Linux with systemd for `gpu-run`'s CPU-capped fallback. Non-root use requires a working user systemd manager. +- Ollama only when using the optional proxy. + +## Build + +```sh +git clone https://gitea.godemperordoge.org/ziadarafat/gpu-broker.git +cd gpu-broker + +go test ./... +go vet ./... +mkdir -p bin +CGO_ENABLED=0 go build -trimpath -o bin/gpu-broker ./cmd/gpu-broker +CGO_ENABLED=0 go build -trimpath -o bin/gpu-run ./cmd/gpu-run +``` + +Generated OpenAPI bindings are committed, so a normal build does not require a code generator. + +To build and test with rootless Podman: + +```sh +podman run --rm --userns keep-id --user "$(id -u):$(id -g)" \ + -e HOME=/tmp/build -e GOCACHE=/tmp/go-cache -e GOMODCACHE=/tmp/go-mod \ + -e CGO_ENABLED=0 -v "$PWD:/work" -w /work \ + docker.io/library/golang:1.27.3-alpine \ + sh -c 'go test ./... && go vet ./... && mkdir -p bin && go build -trimpath -o bin/gpu-broker ./cmd/gpu-broker && go build -trimpath -o bin/gpu-run ./cmd/gpu-run' +``` + +On SELinux systems, add the appropriate volume label when required. No GPU is needed to run the tests; they use scheduler fixtures and local HTTP test servers. + +Alternatively, the checked-in `mise.toml` pins Go and `oapi-codegen`: + +```sh +mise install +mise run test +mise run vet +mise run build +``` + +`mise run build` regenerates the API bindings before building both executables. After changing `api/openapi.yaml`, regenerate them with `mise run generate`. + +## Run the broker + +```sh +./bin/gpu-broker serve \ + --listen 127.0.0.1:8130 \ + --reserve 0=1500,1=0 \ + --lease-ttl 5m \ + --load-window 1m \ + --sample 2s +``` + +This leaves 1,500 MB of GPU 0 unavailable for leases. GPU indexes and memory values follow `nvidia-smi`. Adjust the reserve for your workload; the example is not a universal recommendation. + +Requests are prioritized **realtime > interactive > batch**, FIFO within each priority. A fresh lease reserves its requested memory during the load window to account for the delay before it appears in sampled GPU usage. Clients should renew leases and release them when finished; abandoned leases expire after the TTL. + +Inspect the service: + +```sh +curl -fsS http://127.0.0.1:8130/v1/health +curl -fsS http://127.0.0.1:8130/v1/status +curl -fsS http://127.0.0.1:8130/metrics +``` + +Health reports `no_sample` until a GPU sample is available; an HTTP 200 alone is not proof that NVIDIA sampling works. The API has **no authentication**: keep it on loopback or a trusted, protected network. + +## Run a batch command + +```sh +./bin/gpu-run --vram 3000 --priority batch --max-wait 1m \ + --cpu-quota 200 -- ./batch-job arg1 arg2 +``` + +Replace `./batch-job` with your actual command. On a GPU grant, `gpu-run` sets `CUDA_VISIBLE_DEVICES`, `NVIDIA_VISIBLE_DEVICES`, and `GPU_BROKER_DEVICE=gpu`. On CPU fallback it hides the GPUs, sets `GPU_BROKER_DEVICE=cpu`, and starts the command in a systemd scope with the requested quota. `200` means two CPU cores' worth of execution time, not 200% of the whole machine; the default quota is `400`. A quota of `0` disables the cap. + +`gpu-run` currently exports only the grant's primary GPU index to its child; do not assume `--spread` makes multiple GPUs visible to that command. + +`gpu-run` renews GPU leases while the child runs and releases them afterwards. Its exit status follows the wrapped command. If the broker is unavailable, it chooses the CPU fallback. Programs must actually support CPU execution and honor the device environment; the wrapper cannot convert a GPU-only program into a CPU implementation. + +Useful flags: + +| Flag | Default | Purpose | +| --- | --- | --- | +| `--broker` | `http://127.0.0.1:8130` | Broker address | +| `--client` | command name | Label shown in broker status | +| `--vram` | required | Requested GPU memory in MB | +| `--priority` | `batch` | `batch`, `interactive`, or `realtime` | +| `--max-wait` | `10m` | Wait before CPU fallback | +| `--cpu-quota` | `400` | CPU fallback quota; 100 equals one core | +| `--spread` | false | Allow the lease to span multiple GPUs | + +## Put the proxy in front of Ollama + +First move Ollama off its normal port. For a systemd installation, the provided drop-in does this: + +```sh +sudo mkdir -p /etc/systemd/system/ollama.service.d +sudo install -m 644 deploy/ollama-gpu-broker.conf \ + /etc/systemd/system/ollama.service.d/gpu-broker.conf +sudo systemctl daemon-reload +sudo systemctl restart ollama +``` + +The drop-in sets `OLLAMA_HOST=127.0.0.1:11435`, keeps idle models for ten minutes, and allows at most two loaded models. Restarting Ollama interrupts its current requests, so apply this during an appropriate service window. + +Then run the proxy: + +```sh +./bin/gpu-broker ollama-proxy \ + --listen 127.0.0.1:11434 \ + --upstream http://127.0.0.1:11435 \ + --broker http://127.0.0.1:8130 \ + --max-wait 1m --cpu-threads 4 --prefer 1 +``` + +Clients continue using port 11434. The proxy estimates a model's memory need from Ollama's model list plus 20% headroom. Already-loaded models pass through without a new lease. GPU preferences influence the lease; they do not pin Ollama's internal model placement. + +When the broker grants CPU fallback, native Ollama chat/generate/embed/embeddings requests receive `options.num_gpu=0` and the configured `num_thread` value. OpenAI-compatible endpoints request leases too, but cannot be forced onto the CPU through those options. Unknown models and broker failures pass through to Ollama unchanged. These are availability tradeoffs, so the proxy is **not a strict thermal or resource-limit boundary**. + +## Install the systemd services + +```sh +sudo install -m 755 bin/gpu-broker bin/gpu-run /usr/local/bin/ +sudo install -m 644 deploy/gpu-broker.service \ + deploy/gpu-broker-ollama-proxy.service /etc/systemd/system/ +sudo systemctl daemon-reload +sudo systemctl enable --now gpu-broker.service +# After moving Ollama to port 11435: +sudo systemctl enable --now gpu-broker-ollama-proxy.service +``` + +Review the unit files first: their reserve and preferred GPU index reflect the original two-GPU deployment. The supplied proxy unit listens on **all interfaces** on port 11434; change that binding or restrict access with your network firewall. The proxy itself adds no authentication. Use systemd drop-ins for local overrides. + +Logs are available through `journalctl -u gpu-broker` and `journalctl -u gpu-broker-ollama-proxy`. + +## HTTP API + +The authoritative schema is [api/openapi.yaml](api/openapi.yaml). To request a lease: + +```sh +curl -fsS http://127.0.0.1:8130/v1/leases \ + -H 'Content-Type: application/json' \ + -d '{"client":"example","priority":"batch","vram_mb":3000,"max_wait_s":60,"cpu_fallback":true}' +``` + +The request waits until granted or timed out. A GPU grant includes a `lease_id`; a CPU grant uses `device=cpu` and `gpu=-1`. Renew a GPU lease with `POST /v1/leases/LEASE_ID/renew` and release it with `DELETE /v1/leases/LEASE_ID`. A zero or omitted `max_wait_s` waits indefinitely. `/v1/status` shows GPUs, leases, and waiting requests; `/metrics` exposes Prometheus data. + +## Design and limits + +See [docs/DESIGN.md](docs/DESIGN.md) for the original deployment rationale and [docs/PLAN.md](docs/PLAN.md) for its implementation plan. Those documents record historical local decisions; the README describes the current CLI behavior. + +Leases and priorities are in memory. Restarting the broker discards them. This project does not install the original host's external CPU thermal guard, configure container GPU mappings, or integrate every GPU consumer automatically. Keep host-level safeguards and application-specific limits in place where needed.