-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwarmup.go
142 lines (123 loc) · 3.27 KB
/
warmup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"bytes"
"encoding/json"
"errors"
"io"
"log"
"net/http"
"os"
"time"
)
func (lw *LlmWrangler) WarmupHost(host string) {
var tr = &http.Transport{
// TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Transport: tr,
Timeout: time.Minute * 2,
}
// Get total slots
type healthResponse struct {
Status string `json:"status"`
SlotsIdle int `json:"slots_idle"`
SlotsProcessing int `json:"slots_processing"`
}
req, _ := http.NewRequest("GET", "http://"+host+"/health", nil)
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
log.Println("Can't contact llama.cpp server:", err)
return
}
data, _ := io.ReadAll(res.Body)
health := &healthResponse{}
json.Unmarshal(data, health)
// warm up each slot
log.Println("Warming up", health.SlotsIdle, "llama.cpp slots on", host)
lw.hostsLock.Lock()
status, hostOk := lw.hosts[host]
if hostOk {
status.OpenSlots = health.SlotsIdle
lw.hosts[host] = status
}
lw.hostsLock.Unlock()
responseTime := make(chan int64)
for i := 0; i < health.SlotsIdle; i++ {
go lw.warmupLlama(host, responseTime)
}
go func() {
// wait for warmup
for i := 0; i < health.SlotsIdle; i++ {
<-responseTime
}
time.Sleep(time.Second * 5) // cooldown
// and one more to get the post-warmup response time
go lw.warmupLlama(host, responseTime)
log.Println("Warmed up response time for", host, ":", <-responseTime, "ms")
}()
}
func (lw *LlmWrangler) warmupLlama(host string, responseTime chan int64) error {
lw.hostsLock.Lock()
status, hostOk := lw.hosts[host]
if !hostOk {
lw.hostsLock.Unlock()
return errors.New("Host is gone and can't be warmed up: " + host)
}
status.UseSlot()
lw.hosts[host] = status
lw.hostsLock.Unlock()
config := Completion{}
config.Cache_prompt = true
config.Mirostat_eta = 0.1
config.Mirostat_tau = 5
config.N_predict = 400
config.Repeat_last_n = 96
config.Repeat_penalty = 1.00
config.Slot_id = -1
config.Stream = true
config.Temperature = 1
config.Tfs_z = 1
config.Top_k = 0
config.Top_p = 1
config.Typical_p = 1
config.Min_p = 0.1
prompt, err := os.ReadFile(lw.warmupPromptFile)
if err != nil {
log.Fatal("Could not read warmup-prompt-file:", lw.warmupPromptFile)
}
config.Prompt = string(prompt)
b, _ := json.Marshal(config)
var tr = &http.Transport{
// TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Transport: tr,
Timeout: 0,
}
startTime := time.Now()
req, _ := http.NewRequest("POST", "http://"+host+"/completion", bytes.NewReader(b))
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
log.Println(err)
}
io.ReadAll(res.Body)
if err != nil {
log.Println(err)
}
res.Body.Close()
loadTime := time.Since(startTime)
lw.hostsLock.Lock()
status, hostOk = lw.hosts[host]
if !hostOk {
lw.hostsLock.Unlock()
return errors.New("Host is gone and can't be warmed up: " + host)
}
status.FreeSlot()
status.ResponseTime = loadTime
lw.hosts[host] = status
lw.hostsLock.Unlock()
responseTime <- loadTime.Milliseconds()
return err
}