-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.h
181 lines (145 loc) · 4.74 KB
/
util.h
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
//
// util.h
// AlphaZero
//
// Created by Semin Park on 09/03/2019.
// Copyright © 2019 Semin Park. All rights reserved.
//
#ifndef util_h
#define util_h
#include <algorithm>
#include <csignal>
#include <chrono>
#include <ctime>
#include <fstream>
#include <iostream>
#include <random>
#include <sstream>
#include <string>
#include <sys/file.h>
#include <thread>
#include <vector>
#include <torch/torch.h>
#include "network.hpp"
void msg(std::string str) {
std::cout << str << std::endl;
}
void sighandler(int n)
{
std::stringstream ss;
ss << "Signal: " << n << " | Thread ID: " << std::this_thread::get_id() << std::endl;
std::cout << ss.str();
std::abort();
}
std::vector<float> dirichlet(int size, float alpha = 0.05)
{
static std::mt19937 rng = std::mt19937(std::random_device{}());
std::gamma_distribution<float> dist(alpha);
std::vector<float> vec;
vec.reserve(size);
for (int i = 0; i < size; i++) {
vec.push_back(dist(rng));
}
double sum = std::accumulate(vec.begin(), vec.end(), 0.);
for (float& f : vec)
f /= sum;
return vec;
}
std::string load_network(PVNetwork& net, const std::string& path = "")
{
std::cout << "Loading network." << std::endl;
int fd = open("ckpt_location.txt", O_RDONLY);
flock(fd, LOCK_SH);
std::ifstream ckpt_loc;
ckpt_loc.open("ckpt_location.txt");
if (!ckpt_loc.is_open()) {
char *cwd = getcwd(nullptr, 0);
auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::stringstream ss;
ss << std::endl << "* ERROR *" << std::endl
<< "\"ckpt_location.txt\" must be in the working directory." << std::endl
<< "Furthermore, the path that \"ckpt_location.txt\" points to must exist." << std::endl
<< "Current working directory: " << cwd << std::endl
<< "Current time: " << ctime(&now);
free(cwd);
throw std::runtime_error(ss.str());
}
std::string export_path;
getline(ckpt_loc, export_path);
ckpt_loc.close();
flock(fd, LOCK_UN);
if (export_path == path) {
std::cout << "No change in network. Resuming with network " << path << std::endl;
return path;
}
std::ifstream file(export_path);
if (!file.good()) {
std::cout << "Model path provided by 'ckpt_location.txt' doesn't exist. Creating a new one." << std::endl;
torch::save(net, export_path);
} else {
torch::load(net, export_path);
std::cout << "Network loaded." << std::endl << "Path: " << export_path << std::endl;
}
auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::cout << "Current time: " << ctime(&now);
return export_path;
}
std::string save_network(PVNetwork& net, const std::string& path)
{
std::cout << "(Saving Network) ";
int idx = path.find("_");
auto dot = path.find(".") - 1;
auto diff = dot - idx;
auto ver = path.substr(idx+1, diff);
int version = std::atoi(ver.c_str()) + 1;
std::cout << "Version: " << version << " | ";
std::string new_path = path.substr(0, idx + 1) + std::to_string(version) + ".pt";
torch::save(net, new_path);
int fd = open("ckpt_location.txt", O_WRONLY);
flock(fd, LOCK_EX);
std::ofstream file("ckpt_location.txt");
if (!file.is_open()) {
flock(fd, LOCK_UN);
throw std::runtime_error("ckpt_location.txt does not exist. Did you remove it by accident?");
}
file << new_path;
file.close();
flock(fd, LOCK_UN);
auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::cout << "Saved at " << new_path << " | " << ctime(&now);
return new_path;
}
std::stringstream visualize_stream(torch::Tensor policy)
{
// This function currently is Gomoku / TicTacToe specific
static std::string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::stringstream stream;
int size = policy.sizes()[1];
stream << " ";
for (char c : alphabet.substr(0,size))
stream << c << ' ';
stream << std::endl;
auto policy_a = policy.accessor<float, 3>();
for (int i = 0; i < size; i++) {
stream << std::setw(2) << i << ' ';
for (int j = 0; j < size; j++) {
int lvl = (int) (policy_a[0][i][j] * 10);
if (lvl != 0)
stream << lvl << ' ';
else
stream << '.' << ' ';
}
stream << std::endl;
}
return stream;
}
void adjacent_display(std::stringstream& policy, std::stringstream& board)
{
std::string l,r;
while (true) {
if (!getline(policy, l)) break;
getline(board, r);
std::cout << l << ' ' << r << std::endl;
}
}
#endif /* util_h */