Skip to content

Commit bf09a69

Browse files
authored
Merge pull request #1 from glinscott/master
Update to v4
2 parents ae6d8eb + e269323 commit bf09a69

File tree

7 files changed

+58
-30
lines changed

7 files changed

+58
-30
lines changed

go/src/client/main.go

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ func getExtraParams() map[string]string {
7676
return map[string]string{
7777
"user": *USER,
7878
"password": *PASSWORD,
79-
"version": "3",
79+
"version": "4",
8080
}
8181
}
8282

83-
func uploadGame(httpClient *http.Client, path string, pgn string, nextGame client.NextGameResponse) error {
83+
func uploadGame(httpClient *http.Client, path string, pgn string, nextGame client.NextGameResponse, retryCount uint) error {
8484
extraParams := getExtraParams()
8585
extraParams["training_id"] = strconv.Itoa(int(nextGame.TrainingId))
8686
extraParams["network_id"] = strconv.Itoa(int(nextGame.NetworkId))
@@ -96,13 +96,33 @@ func uploadGame(httpClient *http.Client, path string, pgn string, nextGame clien
9696
body := &bytes.Buffer{}
9797
_, err = body.ReadFrom(resp.Body)
9898
if err != nil {
99+
log.Print(err)
100+
log.Print("Error uploading, retrying...")
101+
time.Sleep(time.Second * (2 << retryCount))
102+
err = uploadGame(httpClient, path, pgn, nextGame, retryCount+1)
99103
return err
100104
}
101105
resp.Body.Close()
102106
fmt.Println(resp.StatusCode)
103107
fmt.Println(resp.Header)
104108
fmt.Println(body)
105109

110+
train_dir := filepath.Dir(path)
111+
if _, err := os.Stat(train_dir); err == nil {
112+
files, err := ioutil.ReadDir(train_dir)
113+
if err != nil {
114+
log.Fatal(err)
115+
}
116+
fmt.Printf("Cleanup training files:\n")
117+
for _, f := range files {
118+
fmt.Printf("%s/%s\n", train_dir, f.Name())
119+
}
120+
err = os.RemoveAll(train_dir)
121+
if err != nil {
122+
log.Fatal(err)
123+
}
124+
}
125+
106126
return nil
107127
}
108128

@@ -252,36 +272,21 @@ func playMatch(baselinePath string, candidatePath string, params []string, flip
252272
return result, game.String(), nil
253273
}
254274

255-
func train(networkPath string, params []string) (string, string) {
275+
func train(networkPath string, count int, params []string) (string, string) {
256276
// pid is intended for use in multi-threaded training
257277
pid := os.Getpid()
258278

259279
dir, _ := os.Getwd()
260-
train_dir := path.Join(dir, fmt.Sprintf("data-%v", pid))
261-
if _, err := os.Stat(train_dir); err == nil {
262-
files, err := ioutil.ReadDir(train_dir)
263-
if err != nil {
264-
log.Fatal(err)
265-
}
266-
fmt.Printf("Cleanup training files:\n")
267-
for _, f := range files {
268-
fmt.Printf("%s/%s\n", train_dir, f.Name())
269-
}
270-
err = os.RemoveAll(train_dir)
271-
if err != nil {
272-
log.Fatal(err)
273-
}
274-
}
275-
280+
train_dir := path.Join(dir, fmt.Sprintf("data-%v-%v", pid, count))
276281
if *DEBUG {
277282
logs_dir := path.Join(dir, fmt.Sprintf("logs-%v", pid))
278283
os.MkdirAll(logs_dir, os.ModePerm)
279284
logfile := path.Join(logs_dir, fmt.Sprintf("%s.log", time.Now().Format("20060102150405")))
280-
params = append(params, "-l" + logfile)
285+
params = append(params, "-l"+logfile)
281286
}
282287

283288
num_games := 1
284-
train_cmd := fmt.Sprintf("--start=train %v %v", pid, num_games)
289+
train_cmd := fmt.Sprintf("--start=train %v-%v %v", pid, count, num_games)
285290
params = append(params, train_cmd)
286291

287292
c := CmdWrapper{}
@@ -319,7 +324,7 @@ func getNetwork(httpClient *http.Client, sha string, clearOld bool) (string, err
319324
return path, nil
320325
}
321326

322-
func nextGame(httpClient *http.Client) error {
327+
func nextGame(httpClient *http.Client, count int) error {
323328
nextGame, err := client.NextGame(httpClient, *HOSTNAME, getExtraParams())
324329
if err != nil {
325330
return err
@@ -343,15 +348,15 @@ func nextGame(httpClient *http.Client) error {
343348
if err != nil {
344349
return err
345350
}
346-
client.UploadMatchResult(httpClient, *HOSTNAME, nextGame.MatchGameId, result, pgn, getExtraParams())
351+
go client.UploadMatchResult(httpClient, *HOSTNAME, nextGame.MatchGameId, result, pgn, getExtraParams())
347352
return nil
348353
} else if nextGame.Type == "train" {
349354
networkPath, err := getNetwork(httpClient, nextGame.Sha, true)
350355
if err != nil {
351356
return err
352357
}
353-
trainFile, pgn := train(networkPath, params)
354-
uploadGame(httpClient, trainFile, pgn, nextGame)
358+
trainFile, pgn := train(networkPath, count, params)
359+
go uploadGame(httpClient, trainFile, pgn, nextGame, 0)
355360
return nil
356361
}
357362

@@ -373,13 +378,16 @@ func main() {
373378
}
374379

375380
httpClient := &http.Client{}
376-
for {
377-
err := nextGame(httpClient)
381+
start := time.Now()
382+
for i := 0; ; i++ {
383+
err := nextGame(httpClient, i)
378384
if err != nil {
379385
log.Print(err)
380386
log.Print("Sleeping for 30 seconds...")
381387
time.Sleep(30 * time.Second)
382388
continue
383389
}
390+
elapsed := time.Since(start)
391+
log.Printf("Completed %d games in %s time", i, elapsed)
384392
}
385393
}

go/src/server/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ sudo apt-get install -y nginx
1717
sudo systemctl status nginx
1818
1919
cp nginx/default /etc/nginx/sites-available/default
20+
21+
# Create cache directory
22+
mkdir -p /home/web/nginx/cache/
2023
```
2124

2225
Installing postgres:
@@ -78,6 +81,11 @@ Connecting through psql:
7881
sudo -u postgres psql -d gorm
7982
```
8083

84+
Restarting nginx:
85+
```
86+
sudo service nginx restart
87+
```
88+
8189
### Setting up backup
8290

8391
```

go/src/server/nginx/default

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ upstream backend {
1515
server 127.0.0.1:8080;
1616
}
1717

18+
proxy_cache_path /home/web/nginx/cache/ levels=1:2 keys_zone=cache:10m max_size=10g
19+
inactive=60m use_temp_path=off;
20+
1821
# Default server configuration
1922
#
2023
server {
@@ -52,6 +55,12 @@ server {
5255
proxy_set_header X-Forwarded-Proto $scheme;
5356

5457
location / {
58+
proxy_cache cache;
59+
60+
# TODO(gary): Should set these from the server...
61+
proxy_ignore_headers Cache-Control;
62+
proxy_cache_valid any 1m;
63+
5564
proxy_pass http://backend;
5665
}
5766
}

go/src/server/templates/index.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{{define "content"}}
2-
<div class="alert alert-primary" role="alert">
3-
Client v3 is now the required version. Download <a href="https://github.com/glinscott/leela-chess/releases/tag/v0.2">here</a>.
2+
<div class="alert alert-warning" role="alert">
3+
v0.4 released, please upgrade. Download <a href="https://github.com/glinscott/leela-chess/releases/tag/v0.4">here</a>.
44
</div>
55

66
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3 border-bottom">

src/UCTNode.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,6 @@ UCTNode::node_ptr_t UCTNode::find_new_root(Key prevroot_full_key, BoardHistory&
384384
UCTNode::node_ptr_t new_root = nullptr;
385385
std::vector<Move> moves;
386386
for (auto pos : boost::adaptors::reverse(new_bh.positions)) {
387-
auto move = pos.get_move();
388387
if (pos.full_key() == prevroot_full_key) {
389388
new_root = find_path(moves);
390389
break;

src/config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
#define USE_OPENBLAS
3232
#endif
3333
//#define USE_MKL
34+
#ifndef FEATURE_USE_CPU_ONLY
3435
#define USE_OPENCL
3536
#define USE_OPENCL_SELFCHECK
37+
#endif
3638
static constexpr int SELFCHECK_PROBABILITY = 2000;
3739
static constexpr int SELFCHECK_MIN_EXPANSIONS = 2'000'000;
3840
//#define USE_TUNER

training/tf/chunkparser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ def convert_v2_to_tuple(self, content):
208208
uint8 planes (120 * 8 * 8 bytes)
209209
"""
210210
(ver, probs, planes, us_ooo, us_oo, them_ooo, them_oo, stm, rule50_count, move_count, winner) = self.v2_struct.unpack(content)
211+
# Enforce move_count to 0
212+
move_count = 0
211213
# Unpack planes.
212214
planes = np.unpackbits(np.frombuffer(planes, dtype=np.uint8))
213215
planes = planes.tobytes() + self.flat_planes[us_ooo] + self.flat_planes[us_oo] + self.flat_planes[them_ooo] + self.flat_planes[them_oo] + self.flat_planes[stm] + self.flat_planes[rule50_count] + self.flat_planes[move_count] + self.flat_planes[0]

0 commit comments

Comments
 (0)