mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
Each served request spawned a second async write for the calibrator counters. They now ride the batch accountWith already sends, and only when shared state is on and a store is configured.
153 lines
5.1 KiB
Go
153 lines
5.1 KiB
Go
package traefikllmgateway
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
// smartDefaultBytesPerToken is the uncalibrated estimate.
|
|
smartDefaultBytesPerToken = 4
|
|
// smartCalibMinSamples keeps a few odd requests from steering the estimate.
|
|
smartCalibMinSamples = 50
|
|
// smartCalibMinBytes skips small prompts, where fixed provider overhead dominates the token count.
|
|
smartCalibMinBytes = 256
|
|
// smartCalibMinRatio and smartCalibMaxRatio bound the learned bytes per token against bad usage data.
|
|
smartCalibMinRatio = 1.5
|
|
smartCalibMaxRatio = 6.0
|
|
// smartCalibAlpha weights the newest sample; 0.1 follows drift within tens of requests.
|
|
smartCalibAlpha = 0.1
|
|
// smartCalibMaxModels bounds the map; models come from config and discovery, so it is a safety cap.
|
|
smartCalibMaxModels = 1024
|
|
|
|
// Fleet counters per model and day: prompt bytes, prompt tokens and sample count.
|
|
calibKind = "cal"
|
|
calibMetricBytes = "b"
|
|
calibMetricToken = "t"
|
|
calibMetricCount = "n"
|
|
// calibFleetDays is how many day buckets the fleet ratio spans, so it does not reset at midnight.
|
|
calibFleetDays = 2
|
|
)
|
|
|
|
type smartCalibKey struct {
|
|
provider, model string
|
|
}
|
|
|
|
type smartCalibStat struct {
|
|
ratio float64
|
|
samples int
|
|
}
|
|
|
|
// smartTokenCalibrator learns bytes per prompt token for each upstream model
|
|
// from reported usage. It keeps two numbers per model and no request data.
|
|
type smartTokenCalibrator struct {
|
|
stats map[smartCalibKey]*smartCalibStat
|
|
// fleet holds the ratios the router-learning loop read from the shared counters; nil without sharedState.
|
|
fleet map[smartCalibKey]smartCalibStat
|
|
now func() time.Time
|
|
mu sync.Mutex
|
|
}
|
|
|
|
// share makes fleetEntries return the shared counters to add. Call it before the calibrator serves requests.
|
|
func (c *smartTokenCalibrator) share(now func() time.Time) {
|
|
c.now = now
|
|
c.fleet = make(map[smartCalibKey]smartCalibStat)
|
|
}
|
|
|
|
func (c *smartTokenCalibrator) sharing() bool {
|
|
return c != nil && c.now != nil
|
|
}
|
|
|
|
// setFleet replaces the shared ratios with those of the latest learn refresh.
|
|
func (c *smartTokenCalibrator) setFleet(m map[smartCalibKey]smartCalibStat) {
|
|
c.mu.Lock()
|
|
c.fleet = m
|
|
c.mu.Unlock()
|
|
}
|
|
|
|
func calibFleetKey(k smartCalibKey, metric, bucket string) string {
|
|
return windowKeyForBucket(calibKind, k.provider+"/"+k.model, metric, windowDay, bucket)
|
|
}
|
|
|
|
// fleetEntries returns the day's shared counters for one usable sample, for the caller to add to its own batch.
|
|
// It returns nil when not sharing or the sample is unusable.
|
|
func (c *smartTokenCalibrator) fleetEntries(provider, model string, promptBytes, promptTokens int64) []counterIncr {
|
|
if !c.sharing() || !calibUsable(promptBytes, promptTokens) {
|
|
return nil
|
|
}
|
|
key := smartCalibKey{provider: provider, model: model}
|
|
bucket := bucketFor(c.now(), windowDay)
|
|
return []counterIncr{
|
|
{key: calibFleetKey(key, calibMetricBytes, bucket), delta: promptBytes, ttl: dayWindowTTL, enforceTTL: enforceTTLDayWindow},
|
|
{key: calibFleetKey(key, calibMetricToken, bucket), delta: promptTokens, ttl: dayWindowTTL, enforceTTL: enforceTTLDayWindow},
|
|
{key: calibFleetKey(key, calibMetricCount, bucket), delta: 1, ttl: dayWindowTTL, enforceTTL: enforceTTLDayWindow},
|
|
}
|
|
}
|
|
|
|
func calibUsable(promptBytes, promptTokens int64) bool {
|
|
return promptTokens > 0 && promptBytes >= smartCalibMinBytes
|
|
}
|
|
|
|
func newSmartTokenCalibrator() *smartTokenCalibrator {
|
|
return &smartTokenCalibrator{stats: make(map[smartCalibKey]*smartCalibStat)}
|
|
}
|
|
|
|
// observe records one request's estimated prompt bytes against the upstream's reported prompt tokens.
|
|
func (c *smartTokenCalibrator) observe(provider, model string, promptBytes, promptTokens int64) {
|
|
if c == nil || !calibUsable(promptBytes, promptTokens) {
|
|
return
|
|
}
|
|
sample := float64(promptBytes) / float64(promptTokens)
|
|
key := smartCalibKey{provider: provider, model: model}
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
st := c.stats[key]
|
|
if st == nil {
|
|
if len(c.stats) >= smartCalibMaxModels {
|
|
return
|
|
}
|
|
c.stats[key] = &smartCalibStat{ratio: sample, samples: 1}
|
|
return
|
|
}
|
|
st.ratio += smartCalibAlpha * (sample - st.ratio)
|
|
if st.samples < smartCalibMinSamples {
|
|
st.samples++
|
|
}
|
|
}
|
|
|
|
// promptTokens estimates the prompt tokens for promptBytes on a model: the
|
|
// clamped learned ratio once enough samples exist, else one token per four bytes.
|
|
func (c *smartTokenCalibrator) promptTokens(provider, model string, promptBytes int64) int64 {
|
|
if c != nil {
|
|
key := smartCalibKey{provider: provider, model: model}
|
|
c.mu.Lock()
|
|
st := c.stats[key]
|
|
ratio, ready := 0.0, false
|
|
if st != nil && st.samples >= smartCalibMinSamples {
|
|
ratio, ready = st.ratio, true
|
|
}
|
|
// The fleet ratio covers every pod's samples, so it wins when it has enough of them.
|
|
if fs, ok := c.fleet[key]; ok && fs.samples >= smartCalibMinSamples {
|
|
ratio, ready = fs.ratio, true
|
|
}
|
|
c.mu.Unlock()
|
|
if ready {
|
|
if ratio < smartCalibMinRatio {
|
|
ratio = smartCalibMinRatio
|
|
}
|
|
if ratio > smartCalibMaxRatio {
|
|
ratio = smartCalibMaxRatio
|
|
}
|
|
tokens := int64(float64(promptBytes)/ratio + 0.999999)
|
|
return tokens
|
|
}
|
|
}
|
|
return (promptBytes + smartDefaultBytesPerToken - 1) / smartDefaultBytesPerToken
|
|
}
|
|
|
|
func (c *smartTokenCalibrator) size() int {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
return len(c.stats)
|
|
}
|