@@ -45,24 +45,33 @@ class OpenCLNetwork;
45
45
struct OpenCLWeights {
46
46
const std::vector<float > ip2_val_w;
47
47
const std::vector<float > ip2_val_b;
48
+ const std::vector<float > ip2_mov_w;
49
+ const std::vector<float > ip2_mov_b;
48
50
const size_t num_output_policies = 1858 ;
49
51
const size_t num_value_channels;
52
+ const size_t num_moves_channels;
50
53
51
54
OpenCLWeights (const WeightsFile& file)
52
55
: ip2_val_w(LayerAdapter(file.weights().ip2_val_w()).as_vector()),
53
56
ip2_val_b (LayerAdapter(file.weights().ip2_val_b()).as_vector()),
54
- num_value_channels(LayerAdapter(file.weights().ip1_val_b()).size()) {}
57
+ ip2_mov_w(LayerAdapter(file.weights().ip2_mov_w()).as_vector()),
58
+ ip2_mov_b(LayerAdapter(file.weights().ip2_mov_b()).as_vector()),
59
+ num_value_channels(LayerAdapter(file.weights().ip1_val_b()).size()),
60
+ num_moves_channels(LayerAdapter(file.weights().ip1_mov_b()).size()) {}
55
61
};
56
62
57
63
class OpenCLComputation : public NetworkComputation {
58
64
public:
59
65
OpenCLComputation (const OpenCL_Network& opencl_net,
60
- const OpenCLWeights& weights, const bool wdl)
66
+ const OpenCLWeights& weights, const bool wdl,
67
+ const bool moves_left)
61
68
: opencl_net_(opencl_net),
62
69
weights_ (weights),
63
70
policies_(),
64
71
q_values_(),
65
- wdl_(wdl) {
72
+ m_values_(),
73
+ wdl_(wdl),
74
+ moves_left_(moves_left) {
66
75
buffers_ = opencl_net.acquire_buffers ();
67
76
}
68
77
@@ -82,6 +91,7 @@ class OpenCLComputation : public NetworkComputation {
82
91
83
92
const auto num_output_policies = weights_.num_output_policies ;
84
93
const auto num_value_channels = weights_.num_value_channels ;
94
+ const auto num_moves_channels = weights_.num_moves_channels ;
85
95
86
96
// Typically
87
97
// input_channels = 112
@@ -90,6 +100,7 @@ class OpenCLComputation : public NetworkComputation {
90
100
91
101
std::vector<float > output_pol (largest_batch_size * num_output_policies);
92
102
std::vector<float > output_val (largest_batch_size * num_value_channels);
103
+ std::vector<float > output_mov (largest_batch_size * num_moves_channels);
93
104
std::vector<float > input_data (largest_batch_size * kInputPlanes * kSquares );
94
105
95
106
for (size_t i = 0 ; i < plane_count; i += largest_batch_size) {
@@ -98,7 +109,8 @@ class OpenCLComputation : public NetworkComputation {
98
109
EncodePlanes (planes_[i + j], &input_data[j * kSquares * kInputPlanes ]);
99
110
}
100
111
101
- buffers_->forward (input_data, output_pol, output_val, batch_size);
112
+ buffers_->forward (input_data, output_pol, output_val, output_mov,
113
+ batch_size);
102
114
103
115
for (size_t j = 0 ; j < batch_size; j++) {
104
116
std::vector<float > policy (num_output_policies);
@@ -135,6 +147,16 @@ class OpenCLComputation : public NetworkComputation {
135
147
136
148
q_values_.emplace_back (std::tanh (winrate));
137
149
}
150
+
151
+ if (moves_left_) {
152
+ auto m = weights_.ip2_mov_b [0 ];
153
+ auto ptr_weights = weights_.ip2_mov_w .data ();
154
+ auto ptr_outputs = &output_mov[j * num_moves_channels];
155
+ for (size_t i = 0 ; i < num_moves_channels; i++)
156
+ m += ptr_weights[i] * std::max (0 .0f , ptr_outputs[i]);
157
+
158
+ m_values_.emplace_back (std::max (0 .0f , m));
159
+ }
138
160
}
139
161
}
140
162
}
@@ -162,8 +184,13 @@ class OpenCLComputation : public NetworkComputation {
162
184
}
163
185
}
164
186
165
- float GetMVal (int /* sample */ ) const override {
166
- return 0 .0f ;
187
+ float GetMVal (int sample) const override {
188
+ if (moves_left_) {
189
+ auto d = m_values_[sample];
190
+ return d;
191
+ } else {
192
+ return 0 .0f ;
193
+ }
167
194
}
168
195
169
196
// Returns P value @move_id of @sample.
@@ -185,9 +212,11 @@ class OpenCLComputation : public NetworkComputation {
185
212
186
213
std::vector<std::vector<float >> policies_;
187
214
std::vector<float > q_values_;
215
+ std::vector<float > m_values_;
188
216
189
217
std::unique_ptr<OpenCLBuffers> buffers_;
190
218
bool wdl_;
219
+ bool moves_left_;
191
220
};
192
221
193
222
void OpenCLComputation::EncodePlanes (const InputPlanes& sample, float * buffer) {
@@ -205,7 +234,7 @@ class OpenCLNetwork : public Network {
205
234
206
235
OpenCLNetwork (const WeightsFile& file, const OptionsDict& options)
207
236
: capabilities_{file.format ().network_format ().input (),
208
- pblczero::NetworkFormat::MOVES_LEFT_NONE },
237
+ file. format (). network_format (). moves_left () },
209
238
weights_ (file),
210
239
params_ (),
211
240
opencl_ (),
@@ -222,6 +251,9 @@ class OpenCLNetwork : public Network {
222
251
wdl_ = file.format ().network_format ().output () ==
223
252
pblczero::NetworkFormat::OUTPUT_WDL;
224
253
254
+ moves_left_ = file.format ().network_format ().moves_left () ==
255
+ pblczero::NetworkFormat::MOVES_LEFT_V1;
256
+
225
257
auto max_batch_size_ =
226
258
static_cast <size_t >(options.GetOrDefault <int >(" batch_size" , 16 ));
227
259
if (max_batch_size_ > kHardMaxBatchSize ) {
@@ -241,9 +273,11 @@ class OpenCLNetwork : public Network {
241
273
const auto residual_blocks = weights.residual .size ();
242
274
243
275
const auto num_value_input_planes = weights.value .biases .size ();
276
+ const auto num_moves_input_planes = weights.moves_left .biases .size ();
244
277
const auto num_policy_input_planes = weights.policy .biases .size ();
245
278
const auto num_output_policy = kPolicyOutputs ;
246
279
const auto num_value_channels = weights.ip1_val_b .size ();
280
+ const auto num_moves_channels = weights.ip1_mov_b .size ();
247
281
248
282
// Typically
249
283
// input_channels = 112
@@ -350,11 +384,20 @@ class OpenCLNetwork : public Network {
350
384
weights.value .biases , weights.ip1_val_w ,
351
385
weights.ip1_val_b );
352
386
387
+ if (moves_left_) {
388
+ opencl_net_.push_moves_left (
389
+ channels, num_moves_input_planes,
390
+ num_moves_input_planes * width * height, num_moves_channels,
391
+ weights.moves_left .weights , weights.moves_left .biases ,
392
+ weights.ip1_mov_w , weights.ip1_mov_b );
393
+ }
394
+
353
395
opencl_net_.setMaxMatchSize (max_batch_size_);
354
396
}
355
397
356
398
std::unique_ptr<NetworkComputation> NewComputation () override {
357
- return std::make_unique<OpenCLComputation>(opencl_net_, weights_, wdl_);
399
+ return std::make_unique<OpenCLComputation>(opencl_net_, weights_, wdl_,
400
+ moves_left_);
358
401
}
359
402
360
403
const NetworkCapabilities& GetCapabilities () const override {
@@ -372,6 +415,7 @@ class OpenCLNetwork : public Network {
372
415
OpenCL opencl_;
373
416
OpenCL_Network opencl_net_;
374
417
bool wdl_;
418
+ bool moves_left_;
375
419
};
376
420
377
421
std::unique_ptr<Network> MakeOpenCLNetwork (const WeightsFile& weights,
0 commit comments