Repository navigation
Expand file tree
/
Copy pathdual_net.h
More file actions
112 lines (90 loc) · 4 KB
/
Copy pathdual_net.h
File metadata and controls
112 lines (90 loc) · 4 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
// Copyright 2018 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_DUAL_NET_DUAL_NET_H_
#define CC_DUAL_NET_DUAL_NET_H_
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/types/span.h"
#include "cc/constants.h"
#include "cc/position.h"
namespace minigo {
// The input features to the DualNet neural network have 17 binary feature
// planes. 8 feature planes X_t indicate the presence of the current player's
// stones at time t. A further 8 feature planes Y_t indicate the presence of
// the opposing player's stones at time t. The final feature plane C holds all
// 1s if black is to play, or 0s if white is to play. The planes are
// concatenated together to give input features:
// [X_t, Y_t, X_t-1, Y_t-1, ..., X_t-7, Y_t-7, C].
class DualNet {
public:
// Size of move history in the stone features.
static constexpr int kMoveHistory = 8;
// Number of features per stone.
static constexpr int kNumStoneFeatures = kMoveHistory * 2 + 1;
// Index of the per-stone feature that describes whether the black or white
// player is to play next.
static constexpr int kPlayerFeature = kMoveHistory * 2;
// Total number of features for the board.
static constexpr int kNumBoardFeatures = kN * kN * kNumStoneFeatures;
// TODO(tommadams): Change features type from float to uint8_t.
using StoneFeatures = std::array<float, kNumStoneFeatures>;
using BoardFeatures = std::array<float, kNumBoardFeatures>;
enum class InputLayout {
kNHWC,
kNCHW,
};
// Generates the board features from the history of recent moves, where
// history[0] is the current board position, and history[i] is the board
// position from i moves ago.
// history.size() must be <= kMoveHistory.
// TODO(tommadams): Move Position::Stones out of the Position class so we
// don't need to depend on position.h.
static void SetFeatures(absl::Span<const Position::Stones* const> history,
Color to_play, BoardFeatures* features);
struct Output {
std::array<float, kNumMoves> policy;
float value;
};
virtual ~DualNet();
// Runs inference on a batch of input features.
// TODO(tommadams): rename model -> model_name.
virtual void RunMany(std::vector<const BoardFeatures*> features,
std::vector<Output*> outputs, std::string* model) = 0;
// Potentially prepares the DualNet to avoid expensive operations during
// RunMany() calls with up to 'capacity' features.
virtual void Reserve(size_t capacity);
virtual InputLayout GetInputLayout() const;
};
// Factory that creates DualNet instances.
// All implementations are required to be thread safe.
class DualNetFactory {
public:
virtual ~DualNetFactory();
// Returns the ideal number of inference requests in flight for DualNet
// instances created by this factory.
virtual int GetBufferCount() const;
// TODO(tommadams): rename model to model_path
virtual std::unique_ptr<DualNet> NewDualNet(const std::string& model) = 0;
// TODO(tommadams): StartGame and EndGame are only required by the
// BatchingDualNetFactory; after main.cc has been split into multiple
// binaries (selfplay, gtp, eval, etc) refactor those binaries to work with
// the BatchingDualNetFactory subclass rather than the abstract DualNetFactory
// and remove StartGame and EndGame from this base class.
virtual void StartGame(DualNet* black, DualNet* white);
virtual void EndGame(DualNet* black, DualNet* white);
};
} // namespace minigo
#endif // CC_DUAL_NET_DUAL_NET_H_