Design, plan and the scheduler (leases, priority queue, CPU fallback)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
c3e915afaa
7 files changed
+660
No files matched your search
@@ -0,0 +1 @@
|
||||
bin/
|
||||
@@ -0,0 +1,45 @@
|
||||
# gpu-broker design (2026-11-11)
|
||||
|
||||
## Why
|
||||
|
||||
yveltal has two GPUs (GTX 1060 6 GB = GPU 0, GTX 1080 8 GB = GPU 1) shared by Jellyfin (NVENC
|
||||
transcodes), Immich ML, a native Ollama (dogebot, music-genres, AFFiNE) and a llama.cpp router
|
||||
container. Nothing coordinates them: on 2026-11-11 a 15 GB model didn't fit and Ollama quietly ran
|
||||
20% of it on the CPU, which (with AudioMuse) helped overheat arceus. The user wants work to use
|
||||
the GPUs whenever possible, queue when they are busy, and fall back to the CPU (capped) instead of
|
||||
waiting forever.
|
||||
|
||||
## Approach (user picked "option 2 on top of option 1", 2026-11-11)
|
||||
|
||||
Option 1, rules (base):
|
||||
- Fixed homes: Jellyfin and Immich ML on GPU 0; LLMs prefer GPU 1 (they may use both).
|
||||
- Ollama: idle models unload (keep-alive 10 min), at most 2 loaded.
|
||||
- Models that can never fit (> 14 GB, e.g. gemma4-26b) are CPU-only by rule, never half-loaded.
|
||||
|
||||
Option 2, a small GPU scheduler on yveltal:
|
||||
- `gpu-broker` daemon: samples VRAM per GPU (nvidia-smi, every 2 s), hands out **leases**
|
||||
(client, priority, VRAM needed, max wait). Grants the GPU with room; otherwise the request waits
|
||||
in a priority queue (realtime > interactive > batch, FIFO within a priority). After max wait it
|
||||
answers "CPU" when the caller allows a CPU fallback. A per-GPU reserve (GPU 0: 1.5 GB) keeps room
|
||||
for Jellyfin transcodes, which can't ask first. Leases expire unless renewed.
|
||||
- `gpu-broker ollama-proxy`: listens where Ollama used to (:11434); Ollama moves to
|
||||
127.0.0.1:11435. Inference requests for a model that isn't loaded ask for a lease (interactive,
|
||||
size from Ollama's model list + 20% for context); on "CPU" the request is forwarded with
|
||||
`num_gpu: 0, num_thread: 4`. Already-loaded models pass straight through.
|
||||
- `gpu-run`: wrapper for batch jobs: `gpu-run --vram 3000 --priority batch -- cmd`. Sets
|
||||
`CUDA_VISIBLE_DEVICES` to the granted GPU, or "" plus a CPU cap (systemd-run CPUQuota=400%)
|
||||
on CPU fallback; renews the lease while the command runs and releases it on exit.
|
||||
- API with an OpenAPI spec (oapi-codegen), like media-preload. `/metrics` for Prometheus.
|
||||
|
||||
Alternatives not taken: rules only (no queue); GPUStack/LocalAI (LLM-only, heavy).
|
||||
|
||||
## Non-goals (v1)
|
||||
|
||||
- No per-request GPU pinning inside Ollama (it places models itself; the broker only gates).
|
||||
- llama.cpp router: leases only, no CPU fallback (its presets are GPU presets). Later.
|
||||
- AudioMuse stays masked (thermal) and is CPU-only anyway.
|
||||
|
||||
## Constraints
|
||||
|
||||
- Thermal: CPU fallback is always capped (4 threads / CPUQuota 400%); arceus has a thermal guard.
|
||||
- Go, static binary, OpenAPI, mise, TDD (scheduler logic is pure and clock-injected).
|
||||
@@ -0,0 +1,15 @@
|
||||
# gpu-broker plan (2026-11-11). Design: DESIGN.md. Go TDD; mise run test before each commit.
|
||||
|
||||
1. internal/sched: Scheduler{Observe(gpus), Acquire(ctx, Request) (Grant, error), Renew, Release,
|
||||
Tick, Status}. Free = total - used - reserve - VRAM of leases granted within the load window.
|
||||
Tests: fit/prefer/most-free, reserve, wait then grant, priority order, timeout -> CPU or error,
|
||||
too big -> CPU now, no oversubscription inside the load window, unrenewed leases expire.
|
||||
2. internal/nvsmi: parse `nvidia-smi --query-gpu=index,memory.total,memory.used --format=csv,noheader,nounits`.
|
||||
3. api/openapi.yaml + oapi-codegen strict server (POST /v1/leases, POST /v1/leases/{id}/renew,
|
||||
DELETE /v1/leases/{id}, GET /v1/status, GET /v1/health) + /metrics.
|
||||
4. internal/ollamaproxy: lease per not-yet-loaded model (size from /api/tags * 1.2), CPU fallback
|
||||
injects options {num_gpu: 0, num_thread: 4}; loaded models and non-inference paths pass through.
|
||||
5. cmd/gpu-run: lease, CUDA_VISIBLE_DEVICES, renew loop, CPU cap via systemd-run.
|
||||
6. Deploy on yveltal: broker + proxy units, Ollama -> 127.0.0.1:11435, Ollama env (keep-alive 10m,
|
||||
max loaded 2), Jellyfin + Immich ML -> GPU 0; verify dogebot, music-genres, AFFiNE; Kuma +
|
||||
Prometheus scrape; memory + handoffs notes.
|
||||
@@ -0,0 +1,325 @@
|
||||
// Package sched decides who gets which GPU, who waits, and who falls back to the CPU.
|
||||
//
|
||||
// It knows each GPU's total and used VRAM (from the sampler), the leases it has
|
||||
// handed out, and the requests waiting for room. A lease reserves VRAM only
|
||||
// during its load window: after that the model shows up in nvidia-smi's "used"
|
||||
// figure, and counting it again would double-book it. A per-GPU reserve keeps
|
||||
// room for work that can't ask first (Jellyfin's NVENC transcodes).
|
||||
package sched
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// CPU is the GPU index of a grant that says "run on the CPU".
|
||||
const CPU = -1
|
||||
|
||||
// Priority orders the queue: higher is served first.
|
||||
type Priority int
|
||||
|
||||
const (
|
||||
Batch Priority = iota
|
||||
Interactive
|
||||
Realtime
|
||||
)
|
||||
|
||||
var (
|
||||
ErrTimeout = errors.New("no GPU became free in time")
|
||||
ErrTooBig = errors.New("needs more VRAM than any GPU has")
|
||||
ErrUnknownLease = errors.New("unknown or expired lease")
|
||||
)
|
||||
|
||||
// GPU is one sample of a GPU's memory.
|
||||
type GPU struct {
|
||||
Index int
|
||||
TotalMB int
|
||||
UsedMB int
|
||||
}
|
||||
|
||||
// Request asks for VRAM on some GPU.
|
||||
type Request struct {
|
||||
Client string
|
||||
Priority Priority
|
||||
VRAMMB int
|
||||
MaxWait time.Duration // 0 = wait as long as it takes
|
||||
CPUFallback bool // after MaxWait (or if it can never fit), answer CPU instead of failing
|
||||
Prefer []int // GPUs to try first, in order
|
||||
}
|
||||
|
||||
// Grant is the answer: a GPU (or CPU) and the lease to renew and release.
|
||||
type Grant struct {
|
||||
LeaseID string
|
||||
GPU int
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
// Config tunes the scheduler.
|
||||
type Config struct {
|
||||
ReserveMB map[int]int // VRAM per GPU nobody may lease (Jellyfin transcodes)
|
||||
LoadWindow time.Duration // how long a fresh lease's VRAM counts before nvidia-smi shows it
|
||||
LeaseTTL time.Duration // leases expire unless renewed within this
|
||||
}
|
||||
|
||||
type lease struct {
|
||||
id string
|
||||
client string
|
||||
gpu int
|
||||
vram int
|
||||
granted time.Time
|
||||
expires time.Time
|
||||
}
|
||||
|
||||
type waiter struct {
|
||||
req Request
|
||||
seq uint64
|
||||
since time.Time
|
||||
deadline time.Time // zero = none
|
||||
done chan outcome
|
||||
}
|
||||
|
||||
type outcome struct {
|
||||
grant Grant
|
||||
err error
|
||||
}
|
||||
|
||||
// Scheduler is safe for concurrent use.
|
||||
type Scheduler struct {
|
||||
cfg Config
|
||||
now func() time.Time
|
||||
mu sync.Mutex
|
||||
gpus []GPU
|
||||
leases map[string]*lease
|
||||
waiting []*waiter
|
||||
seq uint64
|
||||
}
|
||||
|
||||
// New returns a scheduler that reads the time from now.
|
||||
func New(cfg Config, now func() time.Time) *Scheduler {
|
||||
return &Scheduler{cfg: cfg, now: now, leases: map[string]*lease{}}
|
||||
}
|
||||
|
||||
// Observe records a fresh VRAM sample and serves whoever now fits.
|
||||
func (s *Scheduler) Observe(gpus []GPU) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.gpus = append([]GPU(nil), gpus...)
|
||||
s.schedule()
|
||||
}
|
||||
|
||||
// Acquire blocks until the request gets a GPU, the CPU, an error, or ctx ends.
|
||||
func (s *Scheduler) Acquire(ctx context.Context, req Request) (Grant, error) {
|
||||
s.mu.Lock()
|
||||
now := s.now()
|
||||
if req.VRAMMB > s.largest() {
|
||||
s.mu.Unlock()
|
||||
if req.CPUFallback {
|
||||
return Grant{GPU: CPU}, nil
|
||||
}
|
||||
return Grant{}, ErrTooBig
|
||||
}
|
||||
s.seq++
|
||||
w := &waiter{req: req, seq: s.seq, since: now, done: make(chan outcome, 1)}
|
||||
if req.MaxWait > 0 {
|
||||
w.deadline = now.Add(req.MaxWait)
|
||||
}
|
||||
s.waiting = append(s.waiting, w)
|
||||
s.schedule()
|
||||
s.mu.Unlock()
|
||||
|
||||
select {
|
||||
case o := <-w.done:
|
||||
return o.grant, o.err
|
||||
case <-ctx.Done():
|
||||
s.mu.Lock()
|
||||
s.remove(w)
|
||||
s.mu.Unlock()
|
||||
// it may have been granted just before we got the lock
|
||||
select {
|
||||
case o := <-w.done:
|
||||
if o.grant.LeaseID != "" {
|
||||
s.Release(o.grant.LeaseID)
|
||||
}
|
||||
default:
|
||||
}
|
||||
return Grant{}, ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
// Renew extends a lease by the TTL.
|
||||
func (s *Scheduler) Renew(id string) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
l, ok := s.leases[id]
|
||||
if !ok {
|
||||
return ErrUnknownLease
|
||||
}
|
||||
l.expires = s.now().Add(s.cfg.LeaseTTL)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Release ends a lease and lets waiting requests in.
|
||||
func (s *Scheduler) Release(id string) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
delete(s.leases, id)
|
||||
s.schedule()
|
||||
}
|
||||
|
||||
// Tick expires unrenewed leases and times out requests past their MaxWait.
|
||||
func (s *Scheduler) Tick() {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
now := s.now()
|
||||
for id, l := range s.leases {
|
||||
if now.After(l.expires) {
|
||||
delete(s.leases, id)
|
||||
}
|
||||
}
|
||||
for _, w := range append([]*waiter(nil), s.waiting...) {
|
||||
if w.deadline.IsZero() || !now.After(w.deadline) {
|
||||
continue
|
||||
}
|
||||
s.remove(w)
|
||||
if w.req.CPUFallback {
|
||||
w.done <- outcome{grant: Grant{GPU: CPU}}
|
||||
} else {
|
||||
w.done <- outcome{err: ErrTimeout}
|
||||
}
|
||||
}
|
||||
s.schedule()
|
||||
}
|
||||
|
||||
// largest is the most VRAM any single GPU could ever lease.
|
||||
func (s *Scheduler) largest() int {
|
||||
best := 0
|
||||
for _, g := range s.gpus {
|
||||
best = max(best, g.TotalMB-s.cfg.ReserveMB[g.Index])
|
||||
}
|
||||
return best
|
||||
}
|
||||
|
||||
// free is what a GPU can still lease right now.
|
||||
func (s *Scheduler) free(now time.Time) map[int]int {
|
||||
free := map[int]int{}
|
||||
for _, g := range s.gpus {
|
||||
free[g.Index] = g.TotalMB - g.UsedMB - s.cfg.ReserveMB[g.Index]
|
||||
}
|
||||
for _, l := range s.leases {
|
||||
if now.Sub(l.granted) < s.cfg.LoadWindow {
|
||||
free[l.gpu] -= l.vram
|
||||
}
|
||||
}
|
||||
return free
|
||||
}
|
||||
|
||||
// schedule grants every waiting request that fits, highest priority and oldest first.
|
||||
// Smaller requests may pass a larger one that doesn't fit yet.
|
||||
func (s *Scheduler) schedule() {
|
||||
if len(s.waiting) == 0 || len(s.gpus) == 0 {
|
||||
return
|
||||
}
|
||||
now := s.now()
|
||||
free := s.free(now)
|
||||
queue := append([]*waiter(nil), s.waiting...)
|
||||
sort.SliceStable(queue, func(i, j int) bool {
|
||||
if queue[i].req.Priority != queue[j].req.Priority {
|
||||
return queue[i].req.Priority > queue[j].req.Priority
|
||||
}
|
||||
return queue[i].seq < queue[j].seq
|
||||
})
|
||||
for _, w := range queue {
|
||||
gpu, ok := pick(w.req, free)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
free[gpu] -= w.req.VRAMMB
|
||||
l := &lease{
|
||||
id: newID(), client: w.req.Client, gpu: gpu, vram: w.req.VRAMMB,
|
||||
granted: now, expires: now.Add(s.cfg.LeaseTTL),
|
||||
}
|
||||
s.leases[l.id] = l
|
||||
s.remove(w)
|
||||
w.done <- outcome{grant: Grant{LeaseID: l.id, GPU: gpu, ExpiresAt: l.expires}}
|
||||
}
|
||||
}
|
||||
|
||||
// pick returns a preferred GPU that fits, else the one with the most room.
|
||||
func pick(req Request, free map[int]int) (int, bool) {
|
||||
for _, g := range req.Prefer {
|
||||
if f, ok := free[g]; ok && f >= req.VRAMMB {
|
||||
return g, true
|
||||
}
|
||||
}
|
||||
best, bestFree := 0, -1
|
||||
for g, f := range free {
|
||||
if f >= req.VRAMMB && (f > bestFree || (f == bestFree && g < best)) {
|
||||
best, bestFree = g, f
|
||||
}
|
||||
}
|
||||
return best, bestFree >= 0
|
||||
}
|
||||
|
||||
func (s *Scheduler) remove(w *waiter) {
|
||||
for i, x := range s.waiting {
|
||||
if x == w {
|
||||
s.waiting = append(s.waiting[:i], s.waiting[i+1:]...)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newID() string {
|
||||
b := make([]byte, 12)
|
||||
_, _ = rand.Read(b)
|
||||
return hex.EncodeToString(b)
|
||||
}
|
||||
|
||||
// Status is a snapshot for the API and metrics.
|
||||
type Status struct {
|
||||
GPUs []GPUStatus
|
||||
Leases []LeaseStatus
|
||||
Waiting []WaitStatus
|
||||
}
|
||||
|
||||
type GPUStatus struct {
|
||||
Index, TotalMB, UsedMB, ReserveMB, FreeMB int
|
||||
}
|
||||
|
||||
type LeaseStatus struct {
|
||||
ID, Client string
|
||||
GPU, VRAMMB int
|
||||
Granted, Expires time.Time
|
||||
}
|
||||
|
||||
type WaitStatus struct {
|
||||
Client string
|
||||
Priority Priority
|
||||
VRAMMB int
|
||||
Since time.Time
|
||||
}
|
||||
|
||||
// Status returns what the scheduler knows right now.
|
||||
func (s *Scheduler) Status() Status {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
now := s.now()
|
||||
free := s.free(now)
|
||||
var st Status
|
||||
for _, g := range s.gpus {
|
||||
st.GPUs = append(st.GPUs, GPUStatus{g.Index, g.TotalMB, g.UsedMB, s.cfg.ReserveMB[g.Index], free[g.Index]})
|
||||
}
|
||||
for _, l := range s.leases {
|
||||
st.Leases = append(st.Leases, LeaseStatus{l.id, l.client, l.gpu, l.vram, l.granted, l.expires})
|
||||
}
|
||||
sort.Slice(st.Leases, func(i, j int) bool { return st.Leases[i].Granted.Before(st.Leases[j].Granted) })
|
||||
for _, w := range s.waiting {
|
||||
st.Waiting = append(st.Waiting, WaitStatus{w.req.Client, w.req.Priority, w.req.VRAMMB, w.since})
|
||||
}
|
||||
return st
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
package sched
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// clock is a settable time source for the scheduler.
|
||||
type clock struct{ t time.Time }
|
||||
|
||||
func (c *clock) now() time.Time { return c.t }
|
||||
func (c *clock) advance(d time.Duration) { c.t = c.t.Add(d) }
|
||||
func newClock() *clock { return &clock{t: time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC)} }
|
||||
|
||||
// yveltal: GPU 0 is the 6 GB 1060 (Jellyfin's home, 1500 MB kept free), GPU 1 the 8 GB 1080.
|
||||
func yveltal(c *clock) *Scheduler {
|
||||
s := New(Config{
|
||||
ReserveMB: map[int]int{0: 1500},
|
||||
LoadWindow: 60 * time.Second,
|
||||
LeaseTTL: 5 * time.Minute,
|
||||
}, c.now)
|
||||
s.Observe([]GPU{{Index: 0, TotalMB: 6144, UsedMB: 0}, {Index: 1, TotalMB: 8192, UsedMB: 0}})
|
||||
return s
|
||||
}
|
||||
|
||||
// acquire runs Acquire in the background and returns a channel with its outcome.
|
||||
func acquire(s *Scheduler, r Request) <-chan result {
|
||||
out := make(chan result, 1)
|
||||
go func() {
|
||||
g, err := s.Acquire(context.Background(), r)
|
||||
out <- result{g, err}
|
||||
}()
|
||||
return out
|
||||
}
|
||||
|
||||
type result struct {
|
||||
grant Grant
|
||||
err error
|
||||
}
|
||||
|
||||
func waitFor(t *testing.T, ch <-chan result) result {
|
||||
t.Helper()
|
||||
select {
|
||||
case r := <-ch:
|
||||
return r
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("request still waiting")
|
||||
return result{}
|
||||
}
|
||||
}
|
||||
|
||||
func stillWaiting(t *testing.T, ch <-chan result) {
|
||||
t.Helper()
|
||||
select {
|
||||
case r := <-ch:
|
||||
t.Fatalf("expected the request to wait, got %+v", r)
|
||||
case <-time.After(50 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
|
||||
// waitQueued blocks until n requests are queued, so tests don't race the goroutines.
|
||||
func waitQueued(t *testing.T, s *Scheduler, n int) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
for len(s.Status().Waiting) < n {
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("only %d requests queued, want %d", len(s.Status().Waiting), n)
|
||||
}
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreferredGPUIsUsedWhenItFits(t *testing.T) {
|
||||
s := yveltal(newClock())
|
||||
r := waitFor(t, acquire(s, Request{Client: "ollama", Priority: Interactive, VRAMMB: 7000, Prefer: []int{1}}))
|
||||
if r.err != nil || r.grant.GPU != 1 {
|
||||
t.Fatalf("got %+v, %v; want GPU 1", r.grant, r.err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithoutPreferenceTheGPUWithMostRoomIsUsed(t *testing.T) {
|
||||
s := yveltal(newClock())
|
||||
// GPU 0 has 6144-1500 = 4644 MB usable, GPU 1 has 8192
|
||||
r := waitFor(t, acquire(s, Request{Client: "whisper", Priority: Batch, VRAMMB: 2000}))
|
||||
if r.grant.GPU != 1 {
|
||||
t.Fatalf("got GPU %d, want 1", r.grant.GPU)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReserveKeepsRoomForJellyfin(t *testing.T) {
|
||||
c := newClock()
|
||||
s := yveltal(c)
|
||||
s.Observe([]GPU{{Index: 0, TotalMB: 6144, UsedMB: 0}, {Index: 1, TotalMB: 8192, UsedMB: 8000}})
|
||||
// 5000 MB fits in GPU 0's 6144 but not once 1500 MB are kept for transcodes
|
||||
ch := acquire(s, Request{Client: "x", Priority: Batch, VRAMMB: 5000, MaxWait: time.Minute})
|
||||
stillWaiting(t, ch)
|
||||
}
|
||||
|
||||
func TestBusyRequestWaitsUntilVRAMFrees(t *testing.T) {
|
||||
c := newClock()
|
||||
s := yveltal(c)
|
||||
s.Observe([]GPU{{Index: 0, TotalMB: 6144, UsedMB: 6000}, {Index: 1, TotalMB: 8192, UsedMB: 7500}})
|
||||
ch := acquire(s, Request{Client: "genres", Priority: Batch, VRAMMB: 3000, MaxWait: time.Hour})
|
||||
stillWaiting(t, ch)
|
||||
s.Observe([]GPU{{Index: 0, TotalMB: 6144, UsedMB: 6000}, {Index: 1, TotalMB: 8192, UsedMB: 1000}})
|
||||
if r := waitFor(t, ch); r.grant.GPU != 1 {
|
||||
t.Fatalf("got %+v", r.grant)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHigherPriorityIsServedFirst(t *testing.T) {
|
||||
c := newClock()
|
||||
s := yveltal(c)
|
||||
s.Observe([]GPU{{Index: 0, TotalMB: 6144, UsedMB: 6144}, {Index: 1, TotalMB: 8192, UsedMB: 8192}})
|
||||
batch := acquire(s, Request{Client: "batch", Priority: Batch, VRAMMB: 5000, MaxWait: time.Hour})
|
||||
waitQueued(t, s, 1)
|
||||
chat := acquire(s, Request{Client: "dogebot", Priority: Interactive, VRAMMB: 5000, MaxWait: time.Hour})
|
||||
waitQueued(t, s, 2)
|
||||
// room for exactly one of them
|
||||
s.Observe([]GPU{{Index: 0, TotalMB: 6144, UsedMB: 6144}, {Index: 1, TotalMB: 8192, UsedMB: 2000}})
|
||||
if r := waitFor(t, chat); r.grant.GPU != 1 {
|
||||
t.Fatalf("interactive got %+v", r.grant)
|
||||
}
|
||||
stillWaiting(t, batch)
|
||||
}
|
||||
|
||||
func TestSamePriorityIsFirstComeFirstServed(t *testing.T) {
|
||||
c := newClock()
|
||||
s := yveltal(c)
|
||||
s.Observe([]GPU{{Index: 0, TotalMB: 6144, UsedMB: 6144}, {Index: 1, TotalMB: 8192, UsedMB: 8192}})
|
||||
first := acquire(s, Request{Client: "first", Priority: Batch, VRAMMB: 5000, MaxWait: time.Hour})
|
||||
waitQueued(t, s, 1)
|
||||
second := acquire(s, Request{Client: "second", Priority: Batch, VRAMMB: 5000, MaxWait: time.Hour})
|
||||
waitQueued(t, s, 2)
|
||||
s.Observe([]GPU{{Index: 0, TotalMB: 6144, UsedMB: 6144}, {Index: 1, TotalMB: 8192, UsedMB: 2000}})
|
||||
waitFor(t, first)
|
||||
stillWaiting(t, second)
|
||||
}
|
||||
|
||||
func TestWaitingTooLongFallsBackToCPU(t *testing.T) {
|
||||
c := newClock()
|
||||
s := yveltal(c)
|
||||
s.Observe([]GPU{{Index: 0, TotalMB: 6144, UsedMB: 6144}, {Index: 1, TotalMB: 8192, UsedMB: 8192}})
|
||||
ch := acquire(s, Request{Client: "genres", Priority: Batch, VRAMMB: 3000, MaxWait: 30 * time.Second, CPUFallback: true})
|
||||
waitQueued(t, s, 1)
|
||||
c.advance(31 * time.Second)
|
||||
s.Tick()
|
||||
if r := waitFor(t, ch); r.err != nil || r.grant.GPU != CPU {
|
||||
t.Fatalf("got %+v, %v; want CPU", r.grant, r.err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWaitingTooLongWithoutFallbackFails(t *testing.T) {
|
||||
c := newClock()
|
||||
s := yveltal(c)
|
||||
s.Observe([]GPU{{Index: 0, TotalMB: 6144, UsedMB: 6144}, {Index: 1, TotalMB: 8192, UsedMB: 8192}})
|
||||
ch := acquire(s, Request{Client: "llama", Priority: Interactive, VRAMMB: 3000, MaxWait: 30 * time.Second})
|
||||
waitQueued(t, s, 1)
|
||||
c.advance(31 * time.Second)
|
||||
s.Tick()
|
||||
if r := waitFor(t, ch); !errors.Is(r.err, ErrTimeout) {
|
||||
t.Fatalf("got %+v, %v; want ErrTimeout", r.grant, r.err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTooBigForAnyGPUGoesToCPUAtOnce(t *testing.T) {
|
||||
s := yveltal(newClock())
|
||||
// gemma4-26b: 14.4 GB, more than either card
|
||||
r := waitFor(t, acquire(s, Request{Client: "ollama", Priority: Interactive, VRAMMB: 17000, MaxWait: time.Hour, CPUFallback: true}))
|
||||
if r.err != nil || r.grant.GPU != CPU {
|
||||
t.Fatalf("got %+v, %v; want CPU", r.grant, r.err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQuickGrantsDoNotOversubscribeBeforeVRAMShowsUp(t *testing.T) {
|
||||
c := newClock()
|
||||
s := yveltal(c)
|
||||
s.Observe([]GPU{{Index: 0, TotalMB: 6144, UsedMB: 6144}, {Index: 1, TotalMB: 8192, UsedMB: 0}})
|
||||
waitFor(t, acquire(s, Request{Client: "a", Priority: Batch, VRAMMB: 5000}))
|
||||
// the sampler hasn't seen a's model load yet
|
||||
second := acquire(s, Request{Client: "b", Priority: Batch, VRAMMB: 5000, MaxWait: time.Hour})
|
||||
stillWaiting(t, second)
|
||||
// a has loaded: nvidia-smi now shows it, and the load window is over
|
||||
c.advance(61 * time.Second)
|
||||
s.Observe([]GPU{{Index: 0, TotalMB: 6144, UsedMB: 6144}, {Index: 1, TotalMB: 8192, UsedMB: 5000}})
|
||||
stillWaiting(t, second)
|
||||
}
|
||||
|
||||
func TestReleasedLeaseLetsTheNextOneIn(t *testing.T) {
|
||||
c := newClock()
|
||||
s := yveltal(c)
|
||||
s.Observe([]GPU{{Index: 0, TotalMB: 6144, UsedMB: 6144}, {Index: 1, TotalMB: 8192, UsedMB: 0}})
|
||||
a := waitFor(t, acquire(s, Request{Client: "a", Priority: Batch, VRAMMB: 5000}))
|
||||
second := acquire(s, Request{Client: "b", Priority: Batch, VRAMMB: 5000, MaxWait: time.Hour})
|
||||
waitQueued(t, s, 1)
|
||||
s.Release(a.grant.LeaseID)
|
||||
if r := waitFor(t, second); r.grant.GPU != 1 {
|
||||
t.Fatalf("got %+v", r.grant)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnrenewedLeasesExpire(t *testing.T) {
|
||||
c := newClock()
|
||||
s := yveltal(c)
|
||||
r := waitFor(t, acquire(s, Request{Client: "crashed", Priority: Batch, VRAMMB: 1000}))
|
||||
c.advance(6 * time.Minute)
|
||||
s.Tick()
|
||||
if err := s.Renew(r.grant.LeaseID); !errors.Is(err, ErrUnknownLease) {
|
||||
t.Fatalf("renew after expiry: %v", err)
|
||||
}
|
||||
if n := len(s.Status().Leases); n != 0 {
|
||||
t.Fatalf("%d leases left", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenewedLeasesStay(t *testing.T) {
|
||||
c := newClock()
|
||||
s := yveltal(c)
|
||||
r := waitFor(t, acquire(s, Request{Client: "long job", Priority: Batch, VRAMMB: 1000}))
|
||||
for range 3 {
|
||||
c.advance(4 * time.Minute)
|
||||
if err := s.Renew(r.grant.LeaseID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.Tick()
|
||||
}
|
||||
if n := len(s.Status().Leases); n != 1 {
|
||||
t.Fatalf("%d leases, want 1", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCancelledRequestLeavesTheQueue(t *testing.T) {
|
||||
c := newClock()
|
||||
s := yveltal(c)
|
||||
s.Observe([]GPU{{Index: 0, TotalMB: 6144, UsedMB: 6144}, {Index: 1, TotalMB: 8192, UsedMB: 8192}})
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := s.Acquire(ctx, Request{Client: "gone", Priority: Batch, VRAMMB: 1000, MaxWait: time.Hour})
|
||||
done <- err
|
||||
}()
|
||||
waitQueued(t, s, 1)
|
||||
cancel()
|
||||
if err := <-done; !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("got %v", err)
|
||||
}
|
||||
if n := len(s.Status().Waiting); n != 0 {
|
||||
t.Fatalf("%d still queued", n)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
[tools]
|
||||
go = "1.27.3"
|
||||
"go:github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen" = "2.8.2"
|
||||
|
||||
[env]
|
||||
CGO_ENABLED = "0"
|
||||
|
||||
[tasks.generate]
|
||||
run = "oapi-codegen -config api/oapi-codegen.yaml api/openapi.yaml"
|
||||
|
||||
[tasks.build]
|
||||
depends = ["generate"]
|
||||
run = "go build -trimpath -ldflags='-s -w' -o bin/ ./cmd/..."
|
||||
|
||||
[tasks.test]
|
||||
run = "go test ./..."
|
||||
|
||||
[tasks.vet]
|
||||
run = "go vet ./..."
|
||||
Reference in new issue
Block a user