Repository navigation
Expand file tree
/
Copy pathfeatures.h
More file actions
436 lines (375 loc) · 14.8 KB
/
Copy pathfeatures.h
File metadata and controls
436 lines (375 loc) · 14.8 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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CC_MODEL_FEATURES_H_
#define CC_MODEL_FEATURES_H_
#include <emmintrin.h>
#include <array>
#include <utility>
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "cc/color.h"
#include "cc/constants.h"
#include "cc/logging.h"
#include "cc/model/features_internal.h"
#include "cc/model/types.h"
#include "cc/platform/utils.h"
#include "cc/position.h"
// This header contains the different kinds of input features that we pass to
// models. Each set of input features (the stones on the board, whose turn
// it is, liberty counts, etc) is represented as a templated struct where the
// template type T is the feature type (uint8, float).
//
// Each feature struct has two static members:
// - kNumPlanes : the number of planes for this feature.
// - SetNhwc : a method that sets NHWC feature on an input tensor.
// - SetNchw : a method that sets NCWH feature on an input tensor.
namespace minigo {
// Input feature planes for stones on the board over the most recent N moves.
// Up to 8 feature planes X_t indicate the presence of the current player's
// stones at time t. A further up to 8 feature planes Y_t indicate the presence
// of the opposing player's stones at time t:
// [X_t, Y_t, X_t-1, Y_t-1, ..., X_t-7, Y_t-7].
template <int PositionHistory>
struct StoneFeatures {
static_assert(PositionHistory <= kMaxPositionHistory,
"PositionHistory too large");
static constexpr int kNumPlanes = 2 * PositionHistory;
template <typename T>
MG_ALWAYS_INLINE static void SetNhwc(const ModelInput& input, int num_planes,
T* dst) {
auto my_color = input.position_history[0]->to_play();
auto their_color = OtherColor(my_color);
auto n = std::min(input.position_history.size(), PositionHistory);
// Write the features for the position history that we have.
int j = 0;
for (; j < n; ++j) {
const auto* src = input.position_history[j]->stones().data();
const auto* end = dst + kNumPoints * num_planes;
for (auto* d = dst + j * 2; d < end; d += num_planes) {
auto color = src->color();
src += 1;
d[0] = color == my_color;
d[1] = color == their_color;
}
}
// Pad the features with zeros if we have fewer than 8 moves of history.
for (; j < PositionHistory; ++j) {
const auto* end = dst + kNumPoints * num_planes;
for (auto* d = dst + j * 2; d < end; d += num_planes) {
d[0] = 0;
d[1] = 0;
}
}
}
MG_ALWAYS_INLINE static void SetNchw(const ModelInput& input, float* dst) {
auto my_color = input.position_history[0]->to_play();
auto their_color = OtherColor(my_color);
auto n = std::min(input.position_history.size(), PositionHistory);
// Write the features for the position history that we have.
int j = 0;
for (; j < n; ++j) {
const auto& stones = input.position_history[j]->stones();
for (const auto& stone : stones) {
*dst++ = stone.color() == my_color;
}
for (const auto& stone : stones) {
*dst++ = stone.color() == their_color;
}
}
// Pad the features with zeros if we have fewer than 8 moves of history.
for (; j < PositionHistory; ++j) {
for (int i = 0; i < 2 * kNumPoints; ++i) {
*dst++ = 0;
}
}
}
MG_ALWAYS_INLINE static void SetNchw(const ModelInput& input, uint8_t* dst) {
auto my_color = input.position_history[0]->to_play();
auto their_color = OtherColor(my_color);
auto n = std::min(input.position_history.size(), PositionHistory);
// Stones are stored as bit-packed uint16_t, with the color stored in the
// bottom two bits.
static_assert(sizeof(Stone) == 2, "");
__m128i color_mask = _mm_set1_epi16(3);
// Initialize useful single byte values.
__m128i one = _mm_set1_epi8(1);
__m128i my_color_mm = _mm_set1_epi8(static_cast<int>(my_color));
__m128i their_color_mm = _mm_set1_epi8(static_cast<int>(their_color));
// Write the features for the position history that we have.
int safe_size = (kNumPoints / 16) * 16;
int j = 0;
for (; j < n; ++j) {
const auto* stones = input.position_history[j]->stones().data();
int i = 0;
for (; i < safe_size; i += 16) {
// Load 16 stones.
__m128i a =
_mm_loadu_si128(reinterpret_cast<const __m128i*>(stones + i));
__m128i b =
_mm_loadu_si128(reinterpret_cast<const __m128i*>(stones + i + 8));
// Clear all but the bottom two bits of each stone, leaving just their
// colors.
a = _mm_and_si128(a, color_mask);
b = _mm_and_si128(b, color_mask);
// Extract the color values from a & b and pack them into a single.
__m128i col = _mm_packus_epi16(a, b);
// Generate 16 input features for two planes: current player's stones
// and opponent's stones.
// my[i] = col[i] == my_color ? 1 : 0.
// their[i] = col[i] == their_color ? 1 : 0.
__m128i my = _mm_and_si128(one, _mm_cmpeq_epi8(col, my_color_mm));
__m128i their = _mm_and_si128(one, _mm_cmpeq_epi8(col, their_color_mm));
// Store the input features.
_mm_storeu_si128(reinterpret_cast<__m128i*>(dst), my);
_mm_storeu_si128(reinterpret_cast<__m128i*>(dst + kNumPoints), their);
dst += 16;
}
// Finish up the last few input features.
for (; i < kNumPoints; ++i) {
auto color = stones[i].color();
dst[0] = color == my_color;
dst[kNumPoints] = color == their_color;
dst += 1;
}
dst += kNumPoints;
}
// Pad the features with zeros if we have fewer than 8 moves of history.
for (; j < PositionHistory; ++j) {
for (int i = 0; i < 2 * kNumPoints; ++i) {
*dst++ = 0;
}
}
}
};
// Input feature plane containing all 1s if it's blacks turn to play, or all 0s.
struct ToPlayFeature {
static constexpr int kNumPlanes = 1;
template <typename T>
MG_ALWAYS_INLINE static void SetNhwc(const ModelInput& input, int num_planes,
T* dst) {
T f = input.position_history[0]->to_play() == Color::kBlack;
const auto* end = dst + kNumPoints * num_planes;
for (auto* d = dst; d < end; d += num_planes) {
d[0] = f;
}
}
template <typename T>
MG_ALWAYS_INLINE static void SetNchw(const ModelInput& input, T* dst) {
T f = input.position_history[0]->to_play() == Color::kBlack;
for (int i = 0; i < kNumPoints; ++i) {
*dst++ = f;
}
}
};
// Input feature planes that describe chains with only a few remaining
// liberties.
struct LibertyFeatures {
static constexpr int kNumPlanes = 3;
template <typename T>
MG_ALWAYS_INLINE static void SetNhwc(const ModelInput& input, int num_planes,
T* dst) {
const auto& position = *input.position_history[0];
for (int i = 0; i < kNumPoints; ++i) {
auto num_liberties = position.num_chain_liberties(i);
dst[0] = num_liberties == 1;
dst[1] = num_liberties == 2;
dst[2] = num_liberties >= 3;
dst += num_planes;
}
}
template <typename T>
MG_ALWAYS_INLINE static void SetNchw(const ModelInput& input, T* dst) {
const auto& position = *input.position_history[0];
auto* dst0 = dst;
auto* dst1 = dst0 + kNumPoints;
auto* dst2 = dst1 + kNumPoints;
for (int i = 0; i < kNumPoints; ++i) {
auto num_liberties = position.num_chain_liberties(i);
*dst0++ = num_liberties == 1;
*dst1++ = num_liberties == 2;
*dst2++ = num_liberties >= 3;
}
}
};
struct WouldCaptureFeature {
static constexpr int kNumPlanes = 1;
template <typename T>
MG_ALWAYS_INLINE static void SetNhwc(const ModelInput& input, int num_planes,
T* dst) {
const auto& position = *input.position_history[0];
auto my_color = position.to_play();
auto their_color = OtherColor(my_color);
const auto& stones = position.stones();
for (int i = 0; i < kNumPoints; ++i) {
int f = 0;
if (position.legal_move(i)) {
for (auto nc : kNeighborCoords[i]) {
f |= ((stones[nc].color() == their_color) &
(position.num_chain_liberties(nc) == 1));
}
}
dst[0] = f;
dst += num_planes;
}
}
template <typename T>
MG_ALWAYS_INLINE static void SetNchw(const ModelInput& input, T* dst) {
const auto& position = *input.position_history[0];
auto my_color = position.to_play();
auto their_color = OtherColor(my_color);
const auto& stones = position.stones();
for (int i = 0; i < kNumPoints; ++i) {
int f = 0;
if (position.legal_move(i)) {
for (auto nc : kNeighborCoords[i]) {
f |= ((stones[nc].color() == their_color) &
(position.num_chain_liberties(nc) == 1));
}
}
*dst++ = f;
}
}
};
// TODO(tommadams): Move Features and FeaturesDescriptor into another header so
// that features.h only contains the feature types structs.
// TODO(tommadams): Move the framework tests from features_test.cc to
// model_test.cc too.
// `Features` encodes the input tensor type `T` and the list of input features
// to be used `Fs`.
// StoneFeatures must the first feature in Fs.
template <typename... Fs>
struct Features {
using Impl = internal::FeaturesImpl<Fs...>;
// Total number of input feature planes.
static constexpr int kNumPlanes = Impl::kNumPlanes;
// Number of stone feature planes.
static constexpr int kNumStonePlanes = Impl::kNumFirstPlanes;
// Generate the input features in `Fs` from `input` and write them to
// the `features` tensor.
// CHECK fails if the number of channels in the `features` doesn't match the
// number of feature planes `kNumPlanes`.
template <typename T>
static void SetNhwc(absl::Span<const ModelInput* const> inputs,
Tensor<T>* features) {
MG_CHECK(features->shape.is({-1, kN, kN, Impl::kNumPlanes}))
<< features->shape;
int stride = features->shape[1] * features->shape[2] * features->shape[3];
auto* data = features->data;
std::array<T, kNumPoints * kNumPlanes> raw_features;
for (const auto* input : inputs) {
Impl::SetAllNhwc(*input, Impl::kNumPlanes, raw_features.data());
symmetry::ApplySymmetry<kN, kNumPlanes>(input->sym, raw_features.data(),
data);
data += stride;
}
}
template <typename T>
static void SetNchw(absl::Span<const ModelInput* const> inputs,
Tensor<T>* features) {
MG_CHECK(features->shape.is({-1, Impl::kNumPlanes, kN, kN}))
<< features->shape;
int stride = features->shape[1] * features->shape[2] * features->shape[3];
auto* data = features->data;
std::array<T, kNumPoints * kNumPlanes> raw_features;
for (const auto* input : inputs) {
Impl::SetAllNchw(*input, raw_features.data());
symmetry::ApplySymmetryPlanar<kN, kNumPlanes>(input->sym,
raw_features.data(), data);
data += stride;
}
}
// Returns the index in the list of feature planes of `FeatureType`, or -1
// if `FeatureType` isn't in the list.
// For example:
// using MyFeatures = Features<StoneFeatures<8>, ToPlayFeature>;
//
// // StoneFeatures is the first set of features, so trivially has index 0.
// MyFeatures::GetPlaneIdx<StoneFeatures<8>>() == 0;
//
// // StoneFeatures has 16 planes.
// MyFeatures::GetPlaneIdx<ToPlayFeature>() == 16;
//
// // int isn't in the list of features.
// MyFeatures::GetPlaneIdx<int>() == -1;
template <typename FeatureType>
static constexpr int GetPlaneIdx() {
return Impl::template GetPlaneIdx<FeatureType>(0);
}
};
// Descriptor for a model's input features.
// Basically it turns compile-time information about a set of input features
// encoded as a `Features<...>` type into run-time information.
struct FeatureDescriptor {
enum class Layout {
kNhwc,
kNchw,
};
template <typename T>
using SetFeaturesFn = void (*)(absl::Span<const ModelInput* const>,
Tensor<T>*);
template <typename FeatureType>
static FeatureDescriptor Create(Layout layout) {
switch (layout) {
case Layout::kNhwc:
return FeatureDescriptor{FeatureType::kNumPlanes, layout,
&FeatureType::template SetNhwc<uint8_t>,
&FeatureType::template SetNhwc<float>};
case Layout::kNchw:
return FeatureDescriptor{FeatureType::kNumPlanes, layout,
&FeatureType::template SetNchw<uint8_t>,
&FeatureType::template SetNchw<float>};
}
MG_LOG(FATAL) << "Invalid layout" << static_cast<int>(layout);
return {};
}
static FeatureDescriptor Create(absl::string_view input_features,
absl::string_view input_layout);
// Returns the feature tensor shape for a batch size of `n` that uses the
// descriptors input layout.
TensorShape GetInputShape(int n) const {
switch (layout) {
case Layout::kNhwc:
return TensorShape({n, kN, kN, num_planes});
case Layout::kNchw:
return TensorShape({n, num_planes, kN, kN});
}
MG_LOG(FATAL) << "Invalid layout" << static_cast<int>(layout);
return {};
}
void SetFeatures(absl::Span<const ModelInput* const> inputs,
Tensor<uint8_t>* features) const {
set_bytes(inputs, features);
}
void SetFeatures(absl::Span<const ModelInput* const> inputs,
Tensor<float>* features) const {
set_floats(inputs, features);
}
int num_planes;
Layout layout;
SetFeaturesFn<uint8_t> set_bytes;
SetFeaturesFn<float> set_floats;
};
// StoneFeatures must be listed first.
using AgzFeatures = Features<StoneFeatures<8>, ToPlayFeature>;
// StoneFeatures must be listed first.
using Mlperf07Features = Features<
StoneFeatures<4>, ToPlayFeature, LibertyFeatures, WouldCaptureFeature>;
// Maximum number of feature planes used by these features.
constexpr int kMaxNumFeaturePlanes =
internal::GetMaxNumFeaturePlanes<AgzFeatures, Mlperf07Features>();
// A buffer large enough to hold features for all input types.
template <typename T>
using BoardFeatureBuffer = std::array<T, kNumPoints * kMaxNumFeaturePlanes>;
} // namespace minigo
#endif // CC_MODEL_FEATURES_H_