-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtune.cpp
More file actions
246 lines (200 loc) · 6.94 KB
/
Copy pathtune.cpp
File metadata and controls
246 lines (200 loc) · 6.94 KB
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
/*
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2025 The Stockfish developers (see AUTHORS file)
Stockfish is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Stockfish is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tune.h"
#include <algorithm>
#include <cctype>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <map>
#include <optional>
#include <sstream>
#include <string>
#include <vector>
#include "ucioption.h"
using std::string;
namespace engine {
bool Tune::update_on_last;
const Option *LastOption = nullptr;
OptionsMap *Tune::options;
namespace {
std::map<std::string, int> TuneResults;
std::optional<std::string> on_tune(const Option &o) {
if (!Tune::update_on_last || LastOption == &o)
Tune::read_options();
return std::nullopt;
}
} // namespace
void Tune::make_option(OptionsMap *opts, const string &n, int v, const SetRange &r) {
// Do not generate option when there is nothing to tune (ie. min = max)
if (r(v).first == r(v).second)
return;
if (TuneResults.count(n))
v = TuneResults[n];
opts->add(n, Option(v, r(v).first, r(v).second, on_tune));
LastOption = &((*opts)[n]);
auto [a, b] = r(v);
if (!(a <= v && v <= b)) {
std::cerr << "wrong bounds, name: " << n << '\n';
std::exit(1);
}
// Print formatted parameters, ready to be copy-pasted in Fishtest
std::cout << n << "," //
#ifdef OPENBENCH_SUPPORT
// or OpenBench
<< "int" << ","
#endif
<< v << "," //
<< a << "," //
<< b << "," //
<< (b - a) / 20.0 << "," //
<< "0.0020" << '\n';
}
string Tune::next(string &names, bool pop) {
string name;
do {
string token = names.substr(0, names.find(','));
if (pop)
names.erase(0, token.size() + 1);
std::stringstream ws(token);
name += (ws >> token, token); // Remove trailing whitespace
} while (std::count(name.begin(), name.end(), '(') - std::count(name.begin(), name.end(), ')'));
return name;
}
template <> void Tune::Entry<int>::init_option() { make_option(options, name, value, range); }
template <> void Tune::Entry<int>::read_option() {
if (options->count(name))
value = int((*options)[name]);
}
// Instead of a variable here we have a PostUpdate function: just call it
template <> void Tune::Entry<Tune::PostUpdate>::init_option() {}
template <> void Tune::Entry<Tune::PostUpdate>::read_option() { value(); }
template <> void Tune::Entry<int>::print_option(std::ostream &) const {}
template <> void Tune::Entry<Tune::PostUpdate>::print_option(std::ostream &) const {}
namespace {
struct EntryInfo {
std::string base;
int value;
std::vector<int> idx;
};
EntryInfo parse_entry(const std::string &name, int value) {
EntryInfo info;
size_t bracket = name.find('[');
if (bracket == std::string::npos) {
info.base = name;
info.value = value;
return info;
}
info.base = name.substr(0, bracket);
while (bracket != std::string::npos) {
size_t end = name.find(']', bracket);
if (end == std::string::npos)
break;
info.idx.push_back(std::stoi(name.substr(bracket + 1, end - bracket - 1)));
bracket = name.find('[', end + 1);
}
info.value = value;
return info;
}
void deduce_shape(const std::vector<EntryInfo> &entries, std::vector<int> &shape) {
if (entries.empty())
return;
int ndim = (int)entries[0].idx.size();
shape.assign(ndim, 0);
for (auto &e : entries)
for (int d = 0; d < ndim; d++)
if (e.idx[d] >= shape[d])
shape[d] = e.idx[d] + 1;
}
void print_array(std::ostream &os, const std::vector<int> &flat, const int *shape, int ndim, int dim, int &pos) {
if (dim == ndim - 1) {
os << "{ ";
for (int i = 0; i < shape[dim]; i++) {
if (i)
os << ", ";
os << flat[pos++];
}
os << " }";
} else {
os << "{ ";
for (int i = 0; i < shape[dim]; i++) {
if (i)
os << ", ";
print_array(os, flat, shape, ndim, dim + 1, pos);
}
os << " }";
}
}
} // namespace
void Tune::export_weights(std::ostream &os) {
std::vector<EntryInfo> scalars;
std::map<std::string, std::vector<EntryInfo>> groups;
for (auto &e : instance().list) {
auto entry = dynamic_cast<Entry<int> *>(e.get());
if (!entry)
continue;
auto info = parse_entry(entry->name, entry->value);
if (info.idx.empty())
scalars.push_back(info);
else
groups[info.base].push_back(info);
}
os << "#ifndef WEIGHTS_H\n";
os << "#define WEIGHTS_H\n";
os << "#include \"eval.h\"\n";
os << "namespace engine::eval {\n";
for (auto &e : scalars)
os << "inline Value " << e.base << " = " << e.value << ";\n";
for (auto &[base, entries] : groups) {
std::vector<int> shape;
deduce_shape(entries, shape);
int ndim = (int)shape.size();
// Build flat buffer, zero-initialized, fill from entries
int total = 1;
for (int d = 0; d < ndim; d++)
total *= shape[d];
std::vector<int> flat(total, 0);
for (auto &e : entries) {
int linear = 0, stride = total;
for (int d = 0; d < ndim; d++) {
stride /= shape[d];
linear += e.idx[d] * stride;
}
flat[linear] = e.value;
}
os << "inline Value " << base;
for (int d = 0; d < ndim; d++)
os << "[" << shape[d] << "]";
os << " = ";
int pos = 0;
print_array(os, flat, shape.data(), ndim, 0, pos);
os << ";\n";
}
os << "} // namespace engine::eval\n";
os << "#endif\n";
}
} // namespace engine
// Init options with tuning session results instead of default values. Useful to
// get correct bench signature after a tuning session or to test tuned values.
// Just copy fishtest tuning results in a result.txt file and extract the
// values with:
//
// cat results.txt | sed 's/^param: \([^,]*\), best: \([^,]*\).*/
// TuneResults["\1"] = int(round(\2));/'
//
// Then paste the output below, as the function body
namespace engine {
void Tune::read_results() { /* ...insert your values here... */ }
} // namespace engine