sched.go 31 KB
Newer Older
Daniel Hiltgen's avatar
Daniel Hiltgen committed
1
2
3
4
5
6
7
package server

import (
	"context"
	"errors"
	"fmt"
	"log/slog"
Michael Yang's avatar
int  
Michael Yang committed
8
	"os"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
9
	"reflect"
10
	"runtime"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
11
	"sort"
Michael Yang's avatar
int  
Michael Yang committed
12
	"strconv"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
13
14
15
16
17
	"strings"
	"sync"
	"time"

	"github.com/ollama/ollama/api"
18
	"github.com/ollama/ollama/discover"
19
	"github.com/ollama/ollama/envconfig"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
20
	"github.com/ollama/ollama/format"
Michael Yang's avatar
Michael Yang committed
21
	"github.com/ollama/ollama/fs/ggml"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
22
	"github.com/ollama/ollama/llm"
23
	"github.com/ollama/ollama/types/model"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
24
25
26
27
28
29
)

type LlmRequest struct {
	ctx             context.Context //nolint:containedctx
	model           *Model
	opts            api.Options
Daniel Hiltgen's avatar
Daniel Hiltgen committed
30
	origNumCtx      int // Track the initial ctx request
31
	sessionDuration *api.Duration
Daniel Hiltgen's avatar
Daniel Hiltgen committed
32
33
	successCh       chan *runnerRef
	errCh           chan error
34
	schedAttempts   uint
Daniel Hiltgen's avatar
Daniel Hiltgen committed
35
36
37
38
39
40
}

type Scheduler struct {
	pendingReqCh  chan *LlmRequest
	finishedReqCh chan *LlmRequest
	expiredCh     chan *runnerRef
41
	unloadedCh    chan any
Daniel Hiltgen's avatar
Daniel Hiltgen committed
42
43
44
45

	loaded   map[string]*runnerRef
	loadedMu sync.Mutex

Michael Yang's avatar
Michael Yang committed
46
47
	loadFn       func(req *LlmRequest, f *ggml.GGML, gpus discover.GpuInfoList, numParallel int)
	newServerFn  func(gpus discover.GpuInfoList, model string, f *ggml.GGML, adapters []string, projectors []string, opts api.Options, numParallel int) (llm.LlamaServer, error)
48
49
	getGpuFn     func() discover.GpuInfoList
	getCpuFn     func() discover.GpuInfoList
50
	reschedDelay time.Duration
Daniel Hiltgen's avatar
Daniel Hiltgen committed
51
52
}

53
54
55
56
57
58
// Default automatic value for number of models we allow per GPU
// Model will still need to fit in VRAM, but loading many small models
// on a large GPU can cause stalling
var defaultModelsPerGPU = 3

// Default automatic value for parallel setting
59
// Model will still need to fit in VRAM.  If this setting won't fit
60
// we'll back off down to 1 to try to get it to fit
61
var defaultParallel = 2
62

Michael Yang's avatar
lint  
Michael Yang committed
63
var ErrMaxQueue = errors.New("server busy, please try again.  maximum pending requests exceeded")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
64
65

func InitScheduler(ctx context.Context) *Scheduler {
Michael Yang's avatar
int  
Michael Yang committed
66
	maxQueue := envconfig.MaxQueue()
Daniel Hiltgen's avatar
Daniel Hiltgen committed
67
	sched := &Scheduler{
Michael Yang's avatar
int  
Michael Yang committed
68
69
70
		pendingReqCh:  make(chan *LlmRequest, maxQueue),
		finishedReqCh: make(chan *LlmRequest, maxQueue),
		expiredCh:     make(chan *runnerRef, maxQueue),
71
		unloadedCh:    make(chan any, maxQueue),
Daniel Hiltgen's avatar
Daniel Hiltgen committed
72
73
		loaded:        make(map[string]*runnerRef),
		newServerFn:   llm.NewLlamaServer,
74
75
		getGpuFn:      discover.GetGPUInfo,
		getCpuFn:      discover.GetCPUInfo,
76
		reschedDelay:  250 * time.Millisecond,
Daniel Hiltgen's avatar
Daniel Hiltgen committed
77
78
79
80
81
82
	}
	sched.loadFn = sched.load
	return sched
}

// context must be canceled to decrement ref count and release the runner
83
func (s *Scheduler) GetRunner(c context.Context, model *Model, opts api.Options, sessionDuration *api.Duration) (chan *runnerRef, chan error) {
84
85
86
87
	if opts.NumCtx < 4 {
		opts.NumCtx = 4
	}

Daniel Hiltgen's avatar
Daniel Hiltgen committed
88
89
90
91
92
93
94
95
	req := &LlmRequest{
		ctx:             c,
		model:           model,
		opts:            opts,
		sessionDuration: sessionDuration,
		successCh:       make(chan *runnerRef),
		errCh:           make(chan error, 1),
	}
96

Daniel Hiltgen's avatar
Daniel Hiltgen committed
97
98
99
	select {
	case s.pendingReqCh <- req:
	default:
100
		req.errCh <- ErrMaxQueue
Daniel Hiltgen's avatar
Daniel Hiltgen committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
	}
	return req.successCh, req.errCh
}

// Returns immediately, spawns go routines for the scheduler which will shutdown when ctx is done
func (s *Scheduler) Run(ctx context.Context) {
	slog.Debug("starting llm scheduler")
	go func() {
		s.processPending(ctx)
	}()

	go func() {
		s.processCompleted(ctx)
	}()
}

func (s *Scheduler) processPending(ctx context.Context) {
	for {
		select {
		case <-ctx.Done():
			slog.Debug("shutting down scheduler pending loop")
			return
		case pending := <-s.pendingReqCh:
			// Block other requests until we get this pending request running
125
			pending.schedAttempts++
Daniel Hiltgen's avatar
Daniel Hiltgen committed
126
127
			if pending.origNumCtx == 0 {
				pending.origNumCtx = pending.opts.NumCtx
Daniel Hiltgen's avatar
Daniel Hiltgen committed
128
			}
129
130
131
132
133

			if pending.ctx.Err() != nil {
				slog.Debug("pending request cancelled or timed out, skipping scheduling")
				continue
			}
Michael Yang's avatar
Michael Yang committed
134
			numParallel := int(envconfig.NumParallel())
135
			// TODO (jmorganca): mllama doesn't support parallel yet
Daniel Hiltgen's avatar
Daniel Hiltgen committed
136
			// see https://github.com/ollama/ollama/issues/4165
137
			if checkMllamaModelFamily(pending.model) && numParallel != 1 {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
138
				numParallel = 1
139
				slog.Warn("mllama doesn't support parallel requests yet")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
140
			}
141

Daniel Hiltgen's avatar
Daniel Hiltgen committed
142
143
144
145
146
147
148
149
			for {
				var runnerToExpire *runnerRef
				s.loadedMu.Lock()
				runner := s.loaded[pending.model.ModelPath]
				loadedCount := len(s.loaded)
				s.loadedMu.Unlock()
				if runner != nil {
					if runner.needsReload(ctx, pending) {
150
						slog.Debug("reloading", "runner", runner)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
151
152
153
154
155
156
						runnerToExpire = runner
					} else {
						// Runner is usable, return it
						pending.useLoadedRunner(runner, s.finishedReqCh)
						break
					}
Michael Yang's avatar
Michael Yang committed
157
				} else if envconfig.MaxRunners() > 0 && loadedCount >= int(envconfig.MaxRunners()) {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
158
					slog.Debug("max runners achieved, unloading one to make room", "runner_count", loadedCount)
159
					runnerToExpire = s.findRunnerToUnload()
Daniel Hiltgen's avatar
Daniel Hiltgen committed
160
				} else {
161
					// Either no models are loaded or below envconfig.MaxRunners
Daniel Hiltgen's avatar
Daniel Hiltgen committed
162
					// Get a refreshed GPU list
163
					var gpus discover.GpuInfoList
164
165
166
167
168
					if pending.opts.NumGPU == 0 {
						gpus = s.getCpuFn()
					} else {
						gpus = s.getGpuFn()
					}
169

Michael Yang's avatar
int  
Michael Yang committed
170
					if envconfig.MaxRunners() <= 0 {
171
172
173
174
175
176
177
178
179
180
181
						// No user specified MaxRunners, so figure out what automatic setting to use
						// If all GPUs have reliable free memory reporting, defaultModelsPerGPU * the number of GPUs
						// if any GPU has unreliable free memory reporting, 1x the number of GPUs
						allReliable := true
						for _, gpu := range gpus {
							if gpu.UnreliableFreeMemory {
								allReliable = false
								break
							}
						}
						if allReliable {
Michael Yang's avatar
int  
Michael Yang committed
182
183
							// HACK
							os.Setenv("OLLAMA_MAX_LOADED_MODELS", strconv.Itoa(defaultModelsPerGPU*len(gpus)))
184
							slog.Debug("updating default concurrency", "OLLAMA_MAX_LOADED_MODELS", envconfig.MaxRunners(), "gpu_count", len(gpus))
185
						} else {
Michael Yang's avatar
int  
Michael Yang committed
186
187
							// HACK
							os.Setenv("OLLAMA_MAX_LOADED_MODELS", strconv.Itoa(len(gpus)))
188
189
190
							slog.Info("one or more GPUs detected that are unable to accurately report free memory - disabling default concurrency")
						}
					}
191

192
					// Load model for fitting
193
					ggml, err := llm.LoadModel(pending.model.ModelPath, 1024)
194
195
196
197
					if err != nil {
						pending.errCh <- err
						break
					}
198

199
					// Embedding models should always be loaded with parallel=1
200
					if pending.model.CheckCapabilities(model.CapabilityCompletion) != nil {
201
202
203
						numParallel = 1
					}

204
205
					// Evaluate if the model will fit in the available system memory, or if we should unload a model first
					if len(gpus) == 1 && gpus[0].Library == "cpu" {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
206
207
208
209
210
						// simplifying assumption of defaultParallel when in CPU mode
						if numParallel <= 0 {
							numParallel = defaultParallel
						}

211
212
						pending.opts.NumCtx = pending.origNumCtx * numParallel

213
214
						if loadedCount == 0 {
							slog.Debug("cpu mode with first model, loading")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
215
							s.loadFn(pending, ggml, gpus, numParallel)
216
217
218
219
220
							break
						}
						runnerToExpire = s.maybeFindCPURunnerToUnload(pending, ggml, gpus)
						if runnerToExpire == nil {
							slog.Debug("cpu mode with available system memory or first model, loading")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
221
							s.loadFn(pending, ggml, gpus, numParallel)
222
223
224
225
226
							break
						}
						// else we need to expire a runner
					} else if loadedCount == 0 {
						// No models loaded. Load the model but prefer the best fit.
227
						slog.Debug("loading first model", "model", pending.model.ModelPath)
228
						g := pickBestFullFitByLibrary(pending, ggml, gpus, &numParallel)
229
230
						if g != nil {
							gpus = g
231
232
233
						} else {
							// Only allow partial loads when this is the first model
							gpus = pickBestPartialFitByLibrary(pending, ggml, gpus, &numParallel)
234
						}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
235
						s.loadFn(pending, ggml, gpus, numParallel)
236
237
238
						break
					}

239
					if runnerToExpire == nil {
240
241
242
243
244
245
						// More than one loaded model, so we have to see if the
						// new one fits
						//
						// We want to avoid loading on any GPUs that have other
						// models still loading on them to avoid potential races
						// with VRAM consumption ramping up during load
Daniel Hiltgen's avatar
Daniel Hiltgen committed
246
						availGpus := s.filterGPUsWithoutLoadingModels(gpus)
247

248
						// Update free memory from currently loaded models
249
						s.updateFreeSpace(availGpus)
250
						fitGpus := pickBestFullFitByLibrary(pending, ggml, availGpus, &numParallel)
251
						if fitGpus != nil {
252
							slog.Debug("new model fits with existing models, loading")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
253
							s.loadFn(pending, ggml, fitGpus, numParallel)
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
							break
						}

						// We couldn't find a set of GPUs to fully load the new
						// model. If no other models are loading (both GPU lists
						// are the same) then we need to unload another model to
						// make room
						if len(availGpus) < len(gpus) {
							// There are other requests pending, and this one
							// needs more time, so put it on the back of the
							// queue so that we might satisfy other pending
							// requests that aren't blocked
							go func() {
								// Process in a go routine to avoid deadlocking
								// the scheduler if our queue is full
								slog.Debug("delaying scheduling while other models finish loading", "attempts", pending.schedAttempts, "model", pending.model.ModelPath)
								time.Sleep(s.reschedDelay)
								s.pendingReqCh <- pending
							}()
273
274
275
							break
						}
						runnerToExpire = s.findRunnerToUnload()
Daniel Hiltgen's avatar
Daniel Hiltgen committed
276
277
278
279
280
281
282
283
284
285
					}
				}

				if runnerToExpire == nil {
					// Shouildn't happen
					slog.Error("runner to expire was nil!")
					continue
				}
				// Trigger an expiration to unload once it's done
				runnerToExpire.refMu.Lock()
286
				slog.Debug("resetting model to expire immediately to make room", "runner", runnerToExpire, "refCount", runnerToExpire.refCount)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
287
288
289
290
291
292
293
294
295
296
297
298
				if runnerToExpire.expireTimer != nil {
					runnerToExpire.expireTimer.Stop()
					runnerToExpire.expireTimer = nil
				}
				runnerToExpire.sessionDuration = 0
				if runnerToExpire.refCount <= 0 {
					s.expiredCh <- runnerToExpire
				}
				runnerToExpire.refMu.Unlock()
				// Wait for the unload to happen
				// Note: at this point we're queueing up all incoming requests, even if they were for
				// a different model that's loaded and not scheduled to be removed.
299
				slog.Debug("waiting for pending requests to complete and unload to occur", "runner", runnerToExpire)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
300
301
302
303
304
				select {
				case <-ctx.Done():
					slog.Debug("shutting down scheduler pending loop")
					return
				case <-s.unloadedCh:
305
					slog.Debug("unload completed", "runner", runnerToExpire)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
					continue
				}
			}
		case <-s.unloadedCh:
			// An unload request when there are no pending request can be ignored
			slog.Debug("ignoring unload event with no pending requests")
		}
	}
}

func (s *Scheduler) processCompleted(ctx context.Context) {
	// Process completed requests, expired timers, and unloading models
	for {
		select {
		case <-ctx.Done():
			slog.Debug("shutting down scheduler completed loop")
			return
		case finished := <-s.finishedReqCh:
			s.loadedMu.Lock()
			runner := s.loaded[finished.model.ModelPath]
			s.loadedMu.Unlock()
			if runner == nil {
328
				slog.Error("finished request signal received after model unloaded", "modelPath", finished.model.ModelPath)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
329
330
331
332
333
334
				continue
			}
			runner.refMu.Lock()
			runner.refCount--
			if runner.refCount <= 0 {
				if runner.sessionDuration <= 0 {
335
					slog.Debug("runner with zero duration has gone idle, expiring to unload", "runner", runner)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
336
337
338
339
340
341
					if runner.expireTimer != nil {
						runner.expireTimer.Stop()
						runner.expireTimer = nil
					}
					s.expiredCh <- runner
				} else if runner.expireTimer == nil {
342
					slog.Debug("runner with non-zero duration has gone idle, adding timer", "runner", runner, "duration", runner.sessionDuration)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
343
					runner.expireTimer = time.AfterFunc(runner.sessionDuration, func() {
344
						slog.Debug("timer expired, expiring to unload", "runner", runner)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
345
346
347
348
						runner.refMu.Lock()
						defer runner.refMu.Unlock()
						if runner.expireTimer != nil {
							runner.expireTimer.Stop()
349
							runner.expireTimer = nil
Daniel Hiltgen's avatar
Daniel Hiltgen committed
350
351
352
						}
						s.expiredCh <- runner
					})
353
					runner.expiresAt = time.Now().Add(runner.sessionDuration)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
354
				} else {
355
					slog.Debug("runner with non-zero duration has gone idle, resetting timer", "runner", runner, "duration", runner.sessionDuration)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
356
					runner.expireTimer.Reset(runner.sessionDuration)
357
					runner.expiresAt = time.Now().Add(runner.sessionDuration)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
358
359
				}
			}
360
			slog.Debug("after processing request finished event", "runner", runner, "refCount", runner.refCount)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
361
362
			runner.refMu.Unlock()
		case runner := <-s.expiredCh:
363
			slog.Debug("runner expired event received", "runner", runner)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
364
365
			runner.refMu.Lock()
			if runner.refCount > 0 {
366
				slog.Debug("expired event with positive ref count, retrying", "runner", runner, "refCount", runner.refCount)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
367
368
369
370
371
372
373
374
375
376
				go func(runner *runnerRef) {
					// We can't unload yet, but want to as soon as the current request completes
					// So queue up another expired event
					time.Sleep(10 * time.Millisecond)
					s.expiredCh <- runner
				}(runner)
				runner.refMu.Unlock()
				continue
			}

377
			s.loadedMu.Lock()
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
			slog.Debug("got lock to unload expired event", "runner", runner)
			runnerToUnload := s.loaded[runner.modelPath]
			if runnerToUnload == nil {
				// If runnerToUnload is nil, we already processed an event and
				// unloaded it. This double unload can happen if the initial
				// request is canceled and we're trying to load another model
				// that requires this one to be evicted, or the settings change
				// and require a reload
				s.loadedMu.Unlock()
				runner.refMu.Unlock()
				slog.Debug("duplicate expired event, ignoring", "runner", runner)
			} else {
				slog.Debug("starting background wait for VRAM recovery", "runner", runner)
				finished := runner.waitForVRAMRecovery()
				runner.unload()
				delete(s.loaded, runner.modelPath)
				s.loadedMu.Unlock()
				slog.Debug("runner terminated and removed from list, blocking for VRAM recovery", "runner", runner)
				<-finished
				runner.refMu.Unlock()
				slog.Debug("sending an unloaded event", "runner", runner)
				s.unloadedCh <- struct{}{}
			}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
401
402
403
404
405
406
407
408
409
410
411
		}
	}
}

// Complete the pending request and send the runner back to the requester
// Wires up a finished event after the request context is completed
// Updates session duration, and resets expiration timer
func (pending *LlmRequest) useLoadedRunner(runner *runnerRef, finished chan *LlmRequest) {
	runner.refMu.Lock()
	defer runner.refMu.Unlock()
	runner.refCount++
412
413
414
415
	if runner.expireTimer != nil {
		runner.expireTimer.Stop()
		runner.expireTimer = nil
	}
416
417
418
	if pending.sessionDuration != nil {
		runner.sessionDuration = pending.sessionDuration.Duration
	}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
419
420
421
	pending.successCh <- runner
	go func() {
		<-pending.ctx.Done()
422
		slog.Debug("context for request finished", "runner", runner)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
423
424
425
426
		finished <- pending
	}()
}

Michael Yang's avatar
Michael Yang committed
427
func (s *Scheduler) load(req *LlmRequest, f *ggml.GGML, gpus discover.GpuInfoList, numParallel int) {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
428
429
430
	if numParallel < 1 {
		numParallel = 1
	}
Michael Yang's avatar
Michael Yang committed
431
	sessionDuration := envconfig.KeepAlive()
432
433
434
	if req.sessionDuration != nil {
		sessionDuration = req.sessionDuration.Duration
	}
Michael Yang's avatar
Michael Yang committed
435
	llama, err := s.newServerFn(gpus, req.model.ModelPath, f, req.model.AdapterPaths, req.model.ProjectorPaths, req.opts, numParallel)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
436
437
438
439
	if err != nil {
		// some older models are not compatible with newer versions of llama.cpp
		// show a generalized compatibility error until there is a better way to
		// check for model compatibility
Michael Yang's avatar
Michael Yang committed
440
		if errors.Is(err, ggml.ErrUnsupportedFormat) || strings.Contains(err.Error(), "failed to load model") {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
441
442
443
444
445
446
			err = fmt.Errorf("%v: this model may be incompatible with your version of Ollama. If you previously pulled this model, try updating it by running `ollama pull %s`", err, req.model.ShortName)
		}
		slog.Info("NewLlamaServer failed", "model", req.model.ModelPath, "error", err)
		req.errCh <- err
		return
	}
447
448
449
450
451
	runner := &runnerRef{
		model:           req.model,
		modelPath:       req.model.ModelPath,
		llama:           llama,
		Options:         &req.opts,
452
		sessionDuration: sessionDuration,
453
454
455
456
		gpus:            gpus,
		estimatedVRAM:   llama.EstimatedVRAM(),
		estimatedTotal:  llama.EstimatedTotal(),
		loading:         true,
457
		pid:             llama.Pid(),
458
	}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
459
	runner.numParallel = numParallel
460
	runner.refMu.Lock() // hold lock until running or aborted
461

Daniel Hiltgen's avatar
Daniel Hiltgen committed
462
	s.loadedMu.Lock()
463
464
465
466
467
468
469
	if oldRunner, ok := s.loaded[req.model.ModelPath]; ok {
		// Shouldn't happen, but safeguard against leaking a runner
		slog.Warn("model was still loaded", "old_runner", oldRunner, "new_runner", runner)
		oldRunner.refMu.Lock()
		oldRunner.unload()
		oldRunner.refMu.Unlock()
	}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
470
471
472
473
474
475
476
477
478
	s.loaded[req.model.ModelPath] = runner
	slog.Info("loaded runners", "count", len(s.loaded))
	s.loadedMu.Unlock()

	go func() {
		defer runner.refMu.Unlock()
		if err = llama.WaitUntilRunning(req.ctx); err != nil {
			slog.Error("error loading llama server", "error", err)
			req.errCh <- err
479
			slog.Debug("triggering expiration for failed load", "runner", runner)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
480
481
482
			s.expiredCh <- runner
			return
		}
483
		slog.Debug("finished setting up", "runner", runner)
484
485
486
		if runner.pid < 0 {
			runner.pid = llama.Pid()
		}
487
		runner.refCount++
Daniel Hiltgen's avatar
Daniel Hiltgen committed
488
489
490
491
492
493
494
495
496
497
		runner.loading = false
		go func() {
			<-req.ctx.Done()
			slog.Debug("context for request finished")
			s.finishedReqCh <- req
		}()
		req.successCh <- runner
	}()
}

498
func (s *Scheduler) updateFreeSpace(allGpus discover.GpuInfoList) {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
499
500
501
502
503
504
	type predKey struct {
		Library string
		ID      string
	}
	predMap := map[predKey]uint64{} // Sum up the total predicted usage per GPU for all runners
	s.loadedMu.Lock()
505
	runners := make([]*runnerRef, 0, len(s.loaded))
Daniel Hiltgen's avatar
Daniel Hiltgen committed
506
	for _, r := range s.loaded {
507
508
509
510
		runners = append(runners, r)
	}
	s.loadedMu.Unlock()
	for _, r := range runners {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
511
512
513
		r.refMu.Lock()
		if r.llama != nil {
			for _, gpu := range allGpus {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
514
				predMap[predKey{gpu.Library, gpu.ID}] += r.llama.EstimatedVRAMByGPU(gpu.ID)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
			}
		} else {
			slog.Warn("unexpected nil runner reference, memory prediction may be incorrect")
		}
		r.refMu.Unlock()
	}

	// Now that we've summed up all the GPU usage predictions across all the loaded runners, update the gpu list
	for i := range allGpus {
		if p, ok := predMap[predKey{allGpus[i].Library, allGpus[i].ID}]; ok {
			slog.Debug("gpu reported", "gpu", allGpus[i].ID, "library", allGpus[i].Library, "available", format.HumanBytes2(allGpus[i].FreeMemory))
			if p > allGpus[i].TotalMemory {
				// Shouldn't happen
				slog.Warn("predicted usage exceeds VRAM", "gpu", allGpus[i].ID, "totalMemory", allGpus[i].TotalMemory, "predicted", p)
				allGpus[i].FreeMemory = 0
			} else if (allGpus[i].TotalMemory - p) < allGpus[i].FreeMemory { // predicted free is smaller than reported free, use it
				// TODO maybe we should just always trust our numbers, since cuda's free memory reporting is laggy
				// and we might unload models we didn't actually need to.  The risk is if some other GPU intensive app is loaded
533
				// after we start our first runner, then we'll never account for that, so picking the smallest free value seems prudent.
Daniel Hiltgen's avatar
Daniel Hiltgen committed
534
535
				allGpus[i].FreeMemory = allGpus[i].TotalMemory - p
			}
536
537
538
539
540
541
542
543
544
			slog.Info("updated VRAM based on existing loaded models", "gpu", allGpus[i].ID, "library", allGpus[i].Library, "total", format.HumanBytes2(allGpus[i].TotalMemory), "available", format.HumanBytes2(allGpus[i].FreeMemory))
		}
	}
}

// While models are loading the VRAM consumption numbers will be indeterminate, so we have
// to avoid scheduling another model on the same GPU(s) that haven't stabilized.
// This routine returns the set of GPUs that do not have an active loading model.
// If all GPUs have loading models, an empty list will be returned (not a single CPU entry)
545
546
func (s *Scheduler) filterGPUsWithoutLoadingModels(allGpus discover.GpuInfoList) discover.GpuInfoList {
	ret := append(discover.GpuInfoList{}, allGpus...)
547
548
549
550
551
552
553
554
555
556
557
558
559
	s.loadedMu.Lock()
	defer s.loadedMu.Unlock()
	for _, runner := range s.loaded {
		if runner.loading {
			slog.Debug("overlapping loads detected", "gpus", runner.gpus, "model", runner.modelPath)
			for _, busyGPU := range runner.gpus {
				for i := range ret {
					if ret[i].ID == busyGPU.ID {
						ret = append(ret[:i], ret[i+1:]...)
						break
					}
				}
			}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
560
561
		}
	}
562
	return ret
Daniel Hiltgen's avatar
Daniel Hiltgen committed
563
564
}

565
// TODO consolidate sched_types.go
Daniel Hiltgen's avatar
Daniel Hiltgen committed
566
type runnerRef struct {
567
	refMu    sync.Mutex
Daniel Hiltgen's avatar
Daniel Hiltgen committed
568
569
	refCount uint // prevent unloading if > 0

570
	llama          llm.LlamaServer
571
	pid            int
572
573
	loading        bool                 // True only during initial load, then false forever
	gpus           discover.GpuInfoList // Recorded at time of provisioning
574
575
	estimatedVRAM  uint64
	estimatedTotal uint64
Daniel Hiltgen's avatar
Daniel Hiltgen committed
576
577
578

	sessionDuration time.Duration
	expireTimer     *time.Timer
579
	expiresAt       time.Time
Daniel Hiltgen's avatar
Daniel Hiltgen committed
580

Daniel Hiltgen's avatar
Daniel Hiltgen committed
581
582
583
	model       *Model
	modelPath   string
	numParallel int
Daniel Hiltgen's avatar
Daniel Hiltgen committed
584
585
586
587
588
	*api.Options
}

// The refMu must already be held when calling unload
func (runner *runnerRef) unload() {
589
590
591
592
	if runner.expireTimer != nil {
		runner.expireTimer.Stop()
		runner.expireTimer = nil
	}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
593
594
595
	if runner.llama != nil {
		runner.llama.Close()
	}
596
	runner.model = nil
Daniel Hiltgen's avatar
Daniel Hiltgen committed
597
598
599
600
601
602
603
604
605
	runner.llama = nil
	runner.Options = nil
	runner.gpus = nil
}

func (runner *runnerRef) needsReload(ctx context.Context, req *LlmRequest) bool {
	slog.Debug("evaluating already loaded", "model", req.model.ModelPath)
	runner.refMu.Lock()
	defer runner.refMu.Unlock()
606

Daniel Hiltgen's avatar
Daniel Hiltgen committed
607
608
609
610
	timeout := 10 * time.Second
	if runner.loading {
		timeout = 2 * time.Minute // Initial load can take a long time for big models on slow systems...
	}
611

612
613
614
615
	if runner.Options == nil {
		return true
	}

616
617
618
619
620
621
622
623
	// Don't reload runner if num_gpu=-1 was provided
	optsExisting := runner.Options.Runner
	optsNew := req.opts.Runner
	if optsNew.NumGPU < 0 {
		optsExisting.NumGPU = -1
		optsNew.NumGPU = -1
	}

Daniel Hiltgen's avatar
Daniel Hiltgen committed
624
625
626
	// Normalize the NumCtx for parallelism
	optsExisting.NumCtx = optsExisting.NumCtx / runner.numParallel

627
	ctx, cancel := context.WithTimeout(ctx, timeout)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
628
	defer cancel()
629
630
	if !reflect.DeepEqual(runner.model.AdapterPaths, req.model.AdapterPaths) || // have the adapters changed?
		!reflect.DeepEqual(runner.model.ProjectorPaths, req.model.ProjectorPaths) || // have the projectors changed?
Daniel Hiltgen's avatar
Daniel Hiltgen committed
631
632
633
634
		!reflect.DeepEqual(optsExisting, optsNew) || // have the runner options changed?
		runner.llama.Ping(ctx) != nil {
		return true
	}
635

Daniel Hiltgen's avatar
Daniel Hiltgen committed
636
637
638
	return false
}

639
640
641
642
643
644
645
646
// Free memory reporting on GPUs can lag for a while even after the runner
// exits, so we have to keep checking until we see the available memory recover,
// otherwise subsequent model loads will get far less layers loaded or worse
// case, may completely fall back to CPU mode.
// This routine must be called before the runner unloads so it can establish
// a before and after GPU memory allocation.  The returned channel
// will be notified when we're done waiting, or have timed out and should
// proceed anyway
647
648
func (runner *runnerRef) waitForVRAMRecovery() chan any {
	finished := make(chan any, 1)
649

650
651
	// CPU or Metal don't need checking, so no waiting required
	// windows can page VRAM, only cuda currently can report accurate used vram usage
652
653
	if len(runner.gpus) == 0 ||
		(len(runner.gpus) == 1 && (runner.gpus[0].Library == "cpu" || runner.gpus[0].Library == "metal")) ||
654
		(runtime.GOOS == "windows" && runner.gpus[0].Library != "cuda") {
655
		finished <- struct{}{}
656
		slog.Debug("no need to wait for VRAM recovery", "runner", runner)
657
658
659
660
661
		return finished
	}
	start := time.Now()

	// Establish a baseline before we unload
662
	gpusBefore := discover.GetGPUInfo()
663
664
665
666
667
668
669
670
671
672
673
674
	var totalMemoryBefore, freeMemoryBefore uint64
	for _, gpu := range gpusBefore {
		totalMemoryBefore += gpu.TotalMemory
		freeMemoryBefore += gpu.FreeMemory
	}
	go func() {
		expiresAt := start.Add(5 * time.Second) // typical convergence is 0.5-1.5s
		ticker := time.NewTicker(250 * time.Millisecond)
		defer ticker.Stop()
		for {
			<-ticker.C
			if time.Now().After(expiresAt) {
675
				slog.Warn("gpu VRAM usage didn't recover within timeout", "seconds", time.Since(start).Seconds(), "runner", runner)
676
677
678
679
				finished <- struct{}{}
			}

			// Query GPUs, look for free to go back up
680
			gpusNow := discover.GetGPUInfo()
681
682
683
684
685
686
687
			var totalMemoryNow, freeMemoryNow uint64
			for _, gpu := range gpusNow {
				totalMemoryNow += gpu.TotalMemory
				freeMemoryNow += gpu.FreeMemory
			}
			// If we're within ~80% of the estimated memory usage recovered, bail out
			if float32(freeMemoryNow-freeMemoryBefore) > float32(runner.estimatedVRAM)*0.8 {
688
				slog.Debug(fmt.Sprintf("gpu VRAM free memory converged after %0.2f seconds", time.Since(start).Seconds()), "runner", runner)
689
690
691
692
693
694
695
696
				finished <- struct{}{}
				return
			}
		}
	}()
	return finished
}

697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
func (runner *runnerRef) LogValue() slog.Value {
	if runner == nil {
		return slog.StringValue("nil")
	}
	attrs := []slog.Attr{}
	if runner.model != nil {
		attrs = append(attrs, slog.String("name", runner.model.Name))
	}
	if len(runner.gpus) > 0 {
		attrs = append(attrs,
			slog.String("inference", runner.gpus[0].Library),
			slog.Int("devices", len(runner.gpus)),
		)
	}
	attrs = append(attrs,
		slog.String("size", format.HumanBytes2(runner.estimatedTotal)),
		slog.String("vram", format.HumanBytes2(runner.estimatedVRAM)),
		slog.Int("parallel", runner.numParallel),
		slog.Int("pid", runner.pid),
		slog.String("model", runner.modelPath),
	)
718
719
720
	if runner.Options != nil {
		attrs = append(attrs, slog.Int("num_ctx", runner.Options.NumCtx))
	}
721
722
723
	return slog.GroupValue(attrs...)
}

724
725
726
727
728
729
730
731
732
733
734
735
736
type ByDurationAndName []*runnerRef

func (a ByDurationAndName) Len() int      { return len(a) }
func (a ByDurationAndName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByDurationAndName) Less(i, j int) bool {
	// Primary sort by session duration (uint64 to handle negatives)
	d1 := uint64(a[i].sessionDuration)
	d2 := uint64(a[j].sessionDuration)
	if d1 != d2 {
		return d1 < d2
	}
	// Secondary sort by model path lex order
	return a[i].modelPath < a[j].modelPath
Daniel Hiltgen's avatar
Daniel Hiltgen committed
737
738
739
740
741
742
743
744
}

// TODO - future consideration to pick runners based on size
// type BySize []*runnerRef
// func (a BySize) Len() int           { return len(a) }
// func (a BySize) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
// func (a BySize) Less(i, j int) bool { return a[i].estimatedVRAM < a[j].estimatedVRAM }

745
746
// pickBestFullFitByLibrary will try to find the optimal placement of the model in the available GPUs where the model fully fits
// The list of GPUs returned will always be the same brand (library)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
747
// If the model can not be fit fully within the available GPU(s) nil is returned
748
// If numParallel is <= 0, this will attempt try to optimize parallelism based on available VRAM, and adjust
Daniel Hiltgen's avatar
Daniel Hiltgen committed
749
// opts.NumCtx accordingly
Michael Yang's avatar
Michael Yang committed
750
func pickBestFullFitByLibrary(req *LlmRequest, f *ggml.GGML, gpus discover.GpuInfoList, numParallel *int) discover.GpuInfoList {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
751
	var estimatedVRAM uint64
Daniel Hiltgen's avatar
Daniel Hiltgen committed
752
753
754
755

	var numParallelToTry []int
	if *numParallel <= 0 {
		// If no specific parallel setting was provided, try larger then smaller, always end with 1
756
		numParallelToTry = append(numParallelToTry, defaultParallel, 1)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
757
758
759
760
	} else {
		numParallelToTry = []int{*numParallel}
	}

Daniel Hiltgen's avatar
Daniel Hiltgen committed
761
762
	for _, gl := range gpus.ByLibrary() {
		var ok bool
763
		sgl := append(make(discover.GpuInfoList, 0, len(gl)), gl...)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
764
765

		// TODO - potentially sort by performance capability, existing models loaded, etc.
Daniel Hiltgen's avatar
Daniel Hiltgen committed
766
		// TODO - Eliminate any GPUs that already have envconfig.MaxRunners loaded on them
Daniel Hiltgen's avatar
Daniel Hiltgen committed
767
		// Note: at present, this will favor more VRAM over faster GPU speed in mixed setups
768
		sort.Sort(sort.Reverse(discover.ByFreeMemory(sgl)))
Daniel Hiltgen's avatar
Daniel Hiltgen committed
769
770

		// First attempt to fit the model into a single GPU
Daniel Hiltgen's avatar
Daniel Hiltgen committed
771
		for _, p := range numParallelToTry {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
772
			req.opts.NumCtx = req.origNumCtx * p
Michael Yang's avatar
bool  
Michael Yang committed
773
			if !envconfig.SchedSpread() {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
774
				for _, g := range sgl {
775
					if ok, estimatedVRAM = llm.PredictServerFit([]discover.GpuInfo{g}, f, req.model.AdapterPaths, req.model.ProjectorPaths, req.opts, p); ok {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
776
777
						slog.Info("new model will fit in available VRAM in single GPU, loading", "model", req.model.ModelPath, "gpu", g.ID, "parallel", p, "available", g.FreeMemory, "required", format.HumanBytes2(estimatedVRAM))
						*numParallel = p
778
						return []discover.GpuInfo{g}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
779
					}
780
				}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
781
782
783
784
785
786
787
788
			}
		}

		// TODO future refinements
		// - if multiple Libraries, see if any single GPU in any Library will fit
		// - try subsets of GPUs instead of just falling back to 1 or all in a family

		// Now try all the GPUs
Daniel Hiltgen's avatar
Daniel Hiltgen committed
789
		for _, p := range numParallelToTry {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
790
			req.opts.NumCtx = req.origNumCtx * p
791
			if ok, estimatedVRAM = llm.PredictServerFit(sgl, f, req.model.AdapterPaths, req.model.ProjectorPaths, req.opts, p); ok {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
792
793
794
795
				slog.Info("new model will fit in available VRAM, loading", "model", req.model.ModelPath, "library", sgl[0].Library, "parallel", p, "required", format.HumanBytes2(estimatedVRAM))
				*numParallel = p
				return sgl
			}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
796
797
798
799
800
		}
	}
	return nil
}

801
// If multiple Libraries are detected, pick the Library which loads the most layers for the model
Michael Yang's avatar
Michael Yang committed
802
func pickBestPartialFitByLibrary(req *LlmRequest, f *ggml.GGML, gpus discover.GpuInfoList, numParallel *int) discover.GpuInfoList {
803
804
	if *numParallel <= 0 {
		*numParallel = 1
Richard Lyons's avatar
Richard Lyons committed
805
806
		req.opts.NumCtx = req.origNumCtx
	}
807
808
809
810
811
812
813
	byLibrary := gpus.ByLibrary()
	if len(byLibrary) <= 1 {
		return gpus
	}
	var bestEstimate uint64
	var bestFit int
	for i, gl := range byLibrary {
814
		_, estimatedVRAM := llm.PredictServerFit(gl, f, req.model.AdapterPaths, req.model.ProjectorPaths, req.opts, *numParallel)
815
816
817
818
819
820
821
822
		if estimatedVRAM > bestEstimate {
			bestEstimate = estimatedVRAM
			bestFit = i
		}
	}
	return byLibrary[bestFit]
}

Daniel Hiltgen's avatar
Daniel Hiltgen committed
823
// findRunnerToUnload finds a runner to unload to make room for a new model
824
func (s *Scheduler) findRunnerToUnload() *runnerRef {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
825
826
827
828
829
830
	s.loadedMu.Lock()
	runnerList := make([]*runnerRef, 0, len(s.loaded))
	for _, r := range s.loaded {
		runnerList = append(runnerList, r)
	}
	s.loadedMu.Unlock()
831
832
833
834
	if len(runnerList) == 0 {
		slog.Debug("no loaded runner to unload")
		return nil
	}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
835
836
837

	// In the future we can enhance the algorithm to be smarter about picking the optimal runner to unload
	// e.g., if we have multiple options, will one make room for the request?
838
	sort.Sort(ByDurationAndName(runnerList))
Daniel Hiltgen's avatar
Daniel Hiltgen committed
839
840
841
842
843
844
845

	// First try to find a runner that's already idle
	for _, runner := range runnerList {
		runner.refMu.Lock()
		rc := runner.refCount
		runner.refMu.Unlock()
		if rc == 0 {
846
			slog.Debug("found an idle runner to unload", "runner", runner)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
847
848
849
850
			return runner
		}
	}
	// None appear idle, just wait for the one with the shortest duration
851
	slog.Debug("no idle runners, picking the shortest duration", "runner_count", len(runnerList), "runner", runnerList[0])
Daniel Hiltgen's avatar
Daniel Hiltgen committed
852
853
854
855
856
857
858
859
860
861
862
863
864
	return runnerList[0]
}

func (s *Scheduler) unloadAllRunners() {
	s.loadedMu.Lock()
	defer s.loadedMu.Unlock()
	for model, runner := range s.loaded {
		if runner.llama != nil {
			slog.Debug("shutting down runner", "model", model)
			runner.llama.Close()
		}
	}
}
865

Patrick Devine's avatar
Patrick Devine committed
866
867
868
func (s *Scheduler) expireRunner(model *Model) {
	s.loadedMu.Lock()
	runner, ok := s.loaded[model.ModelPath]
869
	s.loadedMu.Unlock()
Patrick Devine's avatar
Patrick Devine committed
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
	if ok {
		runner.refMu.Lock()
		runner.expiresAt = time.Now()
		if runner.expireTimer != nil {
			runner.expireTimer.Stop()
			runner.expireTimer = nil
		}
		runner.sessionDuration = 0
		if runner.refCount <= 0 {
			s.expiredCh <- runner
		}
		runner.refMu.Unlock()
	}
}

885
886
// If other runners are loaded, make sure the pending request will fit in system memory
// If not, pick a runner to unload, else return nil and the request can be loaded
Michael Yang's avatar
Michael Yang committed
887
func (s *Scheduler) maybeFindCPURunnerToUnload(req *LlmRequest, f *ggml.GGML, gpus discover.GpuInfoList) *runnerRef {
888
	slog.Debug("evaluating if CPU model load will fit in available system memory")
889
	estimate := llm.EstimateGPULayers(gpus, f, req.model.ProjectorPaths, req.opts, req.opts.NumCtx/req.origNumCtx)
890
891
892
893
894
895
896
897
898
	if estimate.TotalSize <= gpus[0].FreeMemory {
		slog.Debug("cpu inference mode, model fits in available system memory", "model", format.HumanBytes2(estimate.TotalSize), "available", format.HumanBytes2(gpus[0].FreeMemory))
		return nil
	}

	// TODO - optimization: try to find CPU only runners first, or partial offloads with enough in system memory to make room

	return s.findRunnerToUnload()
}