forked from LeelaChessZero/lczero-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
401 lines (356 loc) · 9.85 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package main
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"time"
"client"
"github.com/Tilps/chess"
)
var HOSTNAME = flag.String("hostname", "http://162.217.248.187", "Address of the server")
var USER = flag.String("user", "", "Username")
var PASSWORD = flag.String("password", "", "Password")
var GPU = flag.Int("gpu", -1, "ID of the OpenCL device to use (-1 for default, or no GPU)")
var DEBUG = flag.Bool("debug", false, "Enable debug mode to see verbose output and save logs")
type Settings struct {
User string
Pass string
}
/*
Reads the user and password from a config file and returns empty strings if anything went wrong.
If the config file does not exists, it prompts the user for a username and password and creates the config file.
*/
func readSettings(path string) (string, string) {
settings := Settings{}
file, err := os.Open(path)
if err != nil {
// File was not found
fmt.Printf("Please enter your username and password, an account will be automatically created.\n")
fmt.Printf("Note that this password will be stored in plain text, so avoid a password that is\n")
fmt.Printf("also used for sensitive applications. It also cannot be recovered.\n")
fmt.Printf("Enter username : ")
fmt.Scanf("%s\n", &settings.User)
fmt.Printf("Enter password : ")
fmt.Scanf("%s\n", &settings.Pass)
jsonSettings, err := json.Marshal(settings)
if err != nil {
log.Fatal("Cannot encode settings to JSON ", err)
return "", ""
}
settingsFile, err := os.Create(path)
if err != nil {
log.Fatal("Could not create output file ", err)
return "", ""
}
fmt.Fprintf(settingsFile, "%s", jsonSettings)
return settings.User, settings.Pass
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&settings)
if err != nil {
log.Fatal("Error decoding JSON ", err)
return "", ""
}
return settings.User, settings.Pass
}
func getExtraParams() map[string]string {
return map[string]string{
"user": *USER,
"password": *PASSWORD,
"version": "10",
}
}
func uploadGame(httpClient *http.Client, path string, pgn string, nextGame client.NextGameResponse, version string, retryCount uint) error {
extraParams := getExtraParams()
extraParams["training_id"] = strconv.Itoa(int(nextGame.TrainingId))
extraParams["network_id"] = strconv.Itoa(int(nextGame.NetworkId))
extraParams["pgn"] = pgn
extraParams["engineVersion"] = version
request, err := client.BuildUploadRequest(*HOSTNAME+"/upload_game", extraParams, "file", path)
if err != nil {
return err
}
resp, err := httpClient.Do(request)
if err != nil {
return err
}
body := &bytes.Buffer{}
_, err = body.ReadFrom(resp.Body)
if err != nil {
log.Print(err)
log.Print("Error uploading, retrying...")
time.Sleep(time.Second * (2 << retryCount))
err = uploadGame(httpClient, path, pgn, nextGame, version, retryCount+1)
return err
}
resp.Body.Close()
fmt.Println(resp.StatusCode)
fmt.Println(resp.Header)
fmt.Println(body)
train_dir := filepath.Dir(path)
if _, err := os.Stat(train_dir); err == nil {
files, err := ioutil.ReadDir(train_dir)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Cleanup training files:\n")
for _, f := range files {
fmt.Printf("%s/%s\n", train_dir, f.Name())
}
err = os.RemoveAll(train_dir)
if err != nil {
log.Fatal(err)
}
}
return nil
}
type CmdWrapper struct {
Cmd *exec.Cmd
Pgn string
Input io.WriteCloser
BestMove chan string
Version string
}
func (c *CmdWrapper) openInput() {
var err error
c.Input, err = c.Cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}
}
func (c *CmdWrapper) launch(networkPath string, args []string, input bool) {
c.BestMove = make(chan string)
weights := fmt.Sprintf("--weights=%s", networkPath)
dir, _ := os.Getwd()
c.Cmd = exec.Command(path.Join(dir, "lczero"), weights, "-t1")
c.Cmd.Args = append(c.Cmd.Args, args...)
if *GPU != -1 {
c.Cmd.Args = append(c.Cmd.Args, fmt.Sprintf("--gpu=%v", *GPU))
}
if !*DEBUG {
c.Cmd.Args = append(c.Cmd.Args, "--quiet")
}
fmt.Printf("Args: %v\n", c.Cmd.Args)
stdout, err := c.Cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
stderr, err := c.Cmd.StderrPipe()
if err != nil {
log.Fatal(err)
}
go func() {
stdoutScanner := bufio.NewScanner(stdout)
reading_pgn := false
for stdoutScanner.Scan() {
line := stdoutScanner.Text()
fmt.Printf("%s\n", line)
if line == "PGN" {
reading_pgn = true
} else if line == "END" {
reading_pgn = false
} else if reading_pgn {
c.Pgn += line + "\n"
} else if strings.HasPrefix(line, "bestmove ") {
c.BestMove <- strings.Split(line, " ")[1]
} else if strings.HasPrefix(line, "id name lczero ") {
c.Version = strings.Split(line, " ")[3]
}
}
}()
go func() {
stderrScanner := bufio.NewScanner(stderr)
for stderrScanner.Scan() {
fmt.Printf("%s\n", stderrScanner.Text())
}
}()
if input {
c.openInput()
}
err = c.Cmd.Start()
if err != nil {
log.Fatal(err)
}
}
func playMatch(baselinePath string, candidatePath string, params []string, flip bool) (int, string, string, error) {
baseline := CmdWrapper{}
baseline.launch(baselinePath, params, true)
defer baseline.Input.Close()
candidate := CmdWrapper{}
candidate.launch(candidatePath, params, true)
defer candidate.Input.Close()
p1 := &candidate
p2 := &baseline
if flip {
p2, p1 = p1, p2
}
io.WriteString(baseline.Input, "uci\n")
io.WriteString(candidate.Input, "uci\n")
// Play a game using UCI
var result int
game := chess.NewGame(chess.UseNotation(chess.LongAlgebraicNotation{}))
move_history := ""
turn := 0
for {
if turn >= 450 || game.Outcome() != chess.NoOutcome || len(game.EligibleDraws()) > 1 {
if game.Outcome() == chess.WhiteWon {
result = 1
} else if game.Outcome() == chess.BlackWon {
result = -1
} else {
result = 0
}
// Always report the result relative to the candidate engine (which defaults to white, unless flip = true)
if flip {
result = -result
}
break
}
var p *CmdWrapper
if game.Position().Turn() == chess.White {
p = p1
} else {
p = p2
}
io.WriteString(p.Input, "position startpos"+move_history+"\n")
io.WriteString(p.Input, "go\n")
select {
case best_move := <-p.BestMove:
err := game.MoveStr(best_move)
if err != nil {
log.Println("Error decoding: " + best_move + " for game:\n" + game.String())
return 0, "", "", err
}
if len(move_history) == 0 {
move_history = " moves"
}
move_history += " " + best_move
turn += 1
case <-time.After(60 * time.Second):
log.Println("Bestmove has timed out, aborting match")
return 0, "", "", errors.New("timeout")
}
}
chess.UseNotation(chess.AlgebraicNotation{})(game)
return result, game.String(), candidate.Version, nil
}
func train(networkPath string, count int, params []string) (string, string, string) {
// pid is intended for use in multi-threaded training
pid := os.Getpid()
dir, _ := os.Getwd()
train_dir := path.Join(dir, fmt.Sprintf("data-%v-%v", pid, count))
if *DEBUG {
logs_dir := path.Join(dir, fmt.Sprintf("logs-%v", pid))
os.MkdirAll(logs_dir, os.ModePerm)
logfile := path.Join(logs_dir, fmt.Sprintf("%s.log", time.Now().Format("20060102150405")))
params = append(params, "-l"+logfile)
}
num_games := 1
train_cmd := fmt.Sprintf("--start=train %v-%v %v", pid, count, num_games)
params = append(params, train_cmd)
c := CmdWrapper{}
c.launch(networkPath, params, false)
err := c.Cmd.Wait()
if err != nil {
log.Fatal(err)
}
return path.Join(train_dir, "training.0.gz"), c.Pgn, c.Version
}
func getNetwork(httpClient *http.Client, sha string, clearOld bool) (string, error) {
// Sha already exists?
path := filepath.Join("networks", sha)
if stat, err := os.Stat(path); err == nil {
if stat.Size() != 0 {
return path, nil
}
}
if clearOld {
// Clean out any old networks
os.RemoveAll("networks")
}
os.MkdirAll("networks", os.ModePerm)
fmt.Printf("Downloading network...\n")
// Otherwise, let's download it
err := client.DownloadNetwork(httpClient, *HOSTNAME, path, sha)
if err != nil {
return "", err
}
return path, nil
}
func nextGame(httpClient *http.Client, count int) error {
nextGame, err := client.NextGame(httpClient, *HOSTNAME, getExtraParams())
if err != nil {
return err
}
var params []string
err = json.Unmarshal([]byte(nextGame.Params), ¶ms)
if err != nil {
return err
}
if nextGame.Type == "match" {
networkPath, err := getNetwork(httpClient, nextGame.Sha, false)
if err != nil {
return err
}
candidatePath, err := getNetwork(httpClient, nextGame.CandidateSha, false)
if err != nil {
return err
}
result, pgn, version, err := playMatch(networkPath, candidatePath, params, nextGame.Flip)
if err != nil {
return err
}
extraParams := getExtraParams()
extraParams["engineVersion"] = version
go client.UploadMatchResult(httpClient, *HOSTNAME, nextGame.MatchGameId, result, pgn, extraParams)
return nil
} else if nextGame.Type == "train" {
networkPath, err := getNetwork(httpClient, nextGame.Sha, true)
if err != nil {
return err
}
trainFile, pgn, version := train(networkPath, count, params)
go uploadGame(httpClient, trainFile, pgn, nextGame, version, 0)
return nil
}
return errors.New("Unknown game type: " + nextGame.Type)
}
func main() {
flag.Parse()
if len(*USER) == 0 || len(*PASSWORD) == 0 {
*USER, *PASSWORD = readSettings("settings.json")
}
if len(*USER) == 0 {
log.Fatal("You must specify a username")
}
if len(*PASSWORD) == 0 {
log.Fatal("You must specify a non-empty password")
}
httpClient := &http.Client{}
start := time.Now()
for i := 0; ; i++ {
err := nextGame(httpClient, i)
if err != nil {
log.Print(err)
log.Print("Sleeping for 30 seconds...")
time.Sleep(30 * time.Second)
continue
}
elapsed := time.Since(start)
log.Printf("Completed %d games in %s time", i+1, elapsed)
}
}