Files
glpi-neural-brain/internal/engine/speed_mode.go
jbergner 94dbd4ccab
All checks were successful
release-tag / release-image (push) Successful in 2m32s
RC-4
2026-08-09 18:41:47 +02:00

116 lines
2.8 KiB
Go

package engine
import (
"context"
"runtime"
"time"
"github.com/local/glpi-neural-brain/internal/model"
"github.com/local/glpi-neural-brain/internal/ollama"
)
const speedModeVersion = "throughput-v1"
func (e *Engine) effectiveSpeedCPUWorkers(settings RuntimeSettings) int {
workers := settings.SpeedCPUWorkers
if workers < 1 {
workers = runtime.NumCPU()
}
if workers < 1 {
workers = 1
}
if workers > 256 {
workers = 256
}
return workers
}
func (e *Engine) effectiveSpeedGPUInflight(settings RuntimeSettings) int {
limit := settings.SpeedGPUInflight
if limit < 1 {
limit = 1
}
if limit > 64 {
limit = 64
}
return limit
}
func (e *Engine) applyRuntimePerformance(settings RuntimeSettings) {
if e == nil {
return
}
sharedInflight := e.Cfg.ResearchOllamaMaxInflight
queueSize := e.Cfg.ResearchOllamaQueueSize
gpuInflight := e.Cfg.OllamaNodeMaxInflight
if sharedInflight < 1 {
sharedInflight = 1
}
if queueSize < 1 {
queueSize = 1
}
if gpuInflight < 1 {
gpuInflight = 1
}
if settings.SpeedMode {
cpu := e.effectiveSpeedCPUWorkers(settings)
gpu := e.effectiveSpeedGPUInflight(settings)
// The shared limiter covers model calls and outbound research work. Do not
// let it become the bottleneck below either requested CPU fan-out or total
// per-node GPU fan-out.
nodeCount := 1
if e.Ollama != nil {
nodeCount = len(e.Ollama.NodeStatuses())
}
if nodeCount < 1 {
nodeCount = 1
}
sharedInflight = maxInt(cpu, gpu*nodeCount)
queueSize = maxInt(queueSize, sharedInflight*16)
gpuInflight = gpu
}
if e.sharedWork != nil {
e.sharedWork.SetLimits(sharedInflight, queueSize)
}
if e.Ollama != nil {
e.Ollama.SetNodeMaxInflight(gpuInflight)
}
}
func (e *Engine) signalSpeedWork() {
if e == nil || !e.SpeedModeEnabled() {
return
}
if e.ThinkingEnabled() {
e.RequestEnrich("speed-mode")
}
if e.AutonomousResearchEnabled() {
e.RequestAutonomousResearchScan("speed-mode")
e.signalAutonomousResearch()
}
if e.sourceInboxWake != nil {
select {
case e.sourceInboxWake <- struct{}{}:
default:
}
}
if e.Broker != nil {
settings := e.RuntimeSettings()
e.Broker.Publish(model.Activity{Type: "performance.speed.pulse", Source: "brain", Phase: "performance", Message: "Speed-Modus treibt vorhandene CPU-/GPU-Arbeit ohne künstliche Pacing-Pausen weiter", Strength: .58, Metadata: map[string]any{"algorithm": speedModeVersion, "cpu_tasks": settings.SpeedCPUWorkers, "gpu_tasks_per_node": settings.SpeedGPUInflight}})
}
}
func (e *Engine) backgroundOllamaContext(ctx context.Context) context.Context {
if e != nil && e.SpeedModeEnabled() {
return ctx
}
return ollama.WithLowPriority(ctx)
}
func (e *Engine) effectiveEnrichStepDelay() time.Duration {
if e != nil && e.SpeedModeEnabled() {
return 0
}
return e.Cfg.EnrichStepDelay
}