internal/router: priority queues so batch jobs yield to interactive requests
The GPU is a size-1 resource, so a single long job monopolises the box for its
whole duration and every interactive request queues behind it. Callers can now
declare intent with an X-LlamaSwap-Priority header and the serial scheduler
dispatches by score instead of by arrival.
- X-LlamaSwap-Priority: signed integer, 0 default, absent/unparseable means 0.
interactive/normal/batch aliases resolve to +100/0/-100. Values are not
clamped: the caller composes band and any per-user offset itself.
- serial dispatch score = priority + swap affinity + aging. Bands sit 100 apart
so a small caller offset orders work inside a band without crossing one;
aging is unbounded so low-priority work cannot starve.
- routing.scheduler.settings.serial.{agingDivisor,swapAffinityBonus}, defaulting
to 60s/point and +10. swapAffinityBonus is capped at 99 so it can never
promote a request into the next band.
- fifo adds the header to its per-model priority, so the header is not silently
ignored under that scheduler.
- /metrics exports per-band queue depth, oldest wait and dispatch counts, plus
counters for how often aging or swap affinity changed the pick. Each request
records its priority, band, score and queue wait in the activity log.
Note swapAffinityBonus defaults to 10, so equal-priority requests for the
already-loaded model now run before older requests that need a swap. Set it to
0 for the previous strict arrival order.
fixes #9
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01WUyhZBgv8BBCC5MduX88gE
This commit is contained in:
@@ -167,13 +167,67 @@ type SchedulerConfig struct {
|
||||
}
|
||||
|
||||
type SchedulerSettings struct {
|
||||
Fifo FifoConfig `yaml:"fifo"`
|
||||
Fifo FifoConfig `yaml:"fifo"`
|
||||
Serial SerialConfig `yaml:"serial"`
|
||||
}
|
||||
|
||||
type FifoConfig struct {
|
||||
Priority map[string]int `yaml:"priority"` // model ID -> priority, default 0
|
||||
}
|
||||
|
||||
// Serial scheduler scoring defaults. See SerialConfig.
|
||||
const (
|
||||
// DefaultAgingDivisor gives a waiting request one priority point per
|
||||
// minute, so a batch job at -100 overtakes normal traffic after ~100
|
||||
// minutes rather than starving behind it forever.
|
||||
DefaultAgingDivisor = 60
|
||||
|
||||
// DefaultSwapAffinityBonus is large enough to break near-ties in favour
|
||||
// of the already-loaded model (cold loads cost 2-3x) and far too small to
|
||||
// promote a request across a 100-point priority band.
|
||||
DefaultSwapAffinityBonus = 10
|
||||
)
|
||||
|
||||
// SerialConfig tunes how the serial scheduler scores queued requests. At each
|
||||
// dispatch it picks the highest scoring waiting request, where
|
||||
//
|
||||
// score = request priority + swap affinity + aging
|
||||
//
|
||||
// Both terms are pointers so an explicit 0 (disable) is distinguishable from
|
||||
// "unset" (use the default); read them through the accessors below.
|
||||
type SerialConfig struct {
|
||||
// AgingDivisor is how many seconds a request must wait to gain one
|
||||
// priority point. Aging is unbounded on purpose — it is the only term
|
||||
// allowed to promote a request across a band, which is what stops a
|
||||
// low-priority job from starving. 0 disables aging.
|
||||
AgingDivisor *int `yaml:"agingDivisor"`
|
||||
|
||||
// SwapAffinityBonus is added to a request that can be served without
|
||||
// swapping models, so strict priority does not force a cold load on every
|
||||
// dispatch. Bounded well below the 100-point band spacing so it can only
|
||||
// break near-ties, never promote batch work above interactive. 0 disables
|
||||
// it, restoring strict priority-then-arrival order.
|
||||
SwapAffinityBonus *int `yaml:"swapAffinityBonus"`
|
||||
}
|
||||
|
||||
// GetAgingDivisor returns the configured aging divisor in seconds, or
|
||||
// DefaultAgingDivisor when unset.
|
||||
func (c SerialConfig) GetAgingDivisor() int {
|
||||
if c.AgingDivisor == nil {
|
||||
return DefaultAgingDivisor
|
||||
}
|
||||
return *c.AgingDivisor
|
||||
}
|
||||
|
||||
// GetSwapAffinityBonus returns the configured swap affinity bonus, or
|
||||
// DefaultSwapAffinityBonus when unset.
|
||||
func (c SerialConfig) GetSwapAffinityBonus() int {
|
||||
if c.SwapAffinityBonus == nil {
|
||||
return DefaultSwapAffinityBonus
|
||||
}
|
||||
return *c.SwapAffinityBonus
|
||||
}
|
||||
|
||||
type RouterConfig struct {
|
||||
Use string `yaml:"use"` // "group" (default) | "matrix"
|
||||
Settings RouterSettings `yaml:"settings"`
|
||||
|
||||
Reference in New Issue
Block a user