TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
models.h
1 /*
2  * Copyright (c) 2017-present, Facebook, Inc.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #pragma once
9 
10 #include <common/autograd.h>
11 
12 #include <gflags/gflags.h>
13 
14 DECLARE_string(bos_model_type);
15 DECLARE_bool(bos_bo_input);
16 DECLARE_bool(bos_mapid_input);
17 DECLARE_bool(bos_time_input);
18 DECLARE_bool(bos_res_input);
19 DECLARE_bool(bos_tech_input);
20 DECLARE_bool(bos_ptech_input);
21 DECLARE_bool(bos_units_input);
22 DECLARE_bool(bos_fabs_input);
23 DECLARE_int32(bos_hid_dim);
24 DECLARE_int32(bos_num_layers);
25 DECLARE_string(bos_targets);
26 
27 namespace cherrypi {
28 namespace bos {
29 
30 /// Construct a BOS module according to command-line flags
31 ag::Container modelMakeFromCli(double dropout = 0.0);
32 std::map<std::string, std::string> modelFlags();
33 
35  public:
36  void reset() override;
37  ag::Variant forward(ag::Variant input) override;
38 };
39 
40 AUTOGRAD_CONTAINER_CLASS(MapRaceEcoTimeFeaturize) {
41  public:
42  TORCH_ARG(int, bo_embsize) = 8;
43  TORCH_ARG(int, mapid_embsize) = 8;
44  TORCH_ARG(int, n_builds) = -1;
45  TORCH_ARG(int, race_embsize) = 8;
46  TORCH_ARG(int, resources_embsize) = 8;
47  TORCH_ARG(int, tech_embsize) = 8;
48  TORCH_ARG(int, ptech_embsize) = 8;
49  TORCH_ARG(int, time_embsize) = 8;
50 
51  void reset() override;
52 
53  /** Expected input:
54  * - a Bx1 tensor containing the map ID,
55  * - a Bx2 tensor containing the races,
56  * - a Bx4 tensor containing resource features.
57  * - a Bx142 tensor containing upgrades and techonology features.
58  * - a Bx142 tensor containing pending upgrades and techonology features.
59  * - a Bx1 tensor containing the current time (in frames)
60  * - a Bx1 tensor containing the active build
61  *
62  * B can also be TxB.
63  *
64  * Returns:
65  * - a BxN tensor, with N being the sum of all embeddings
66  */
67  ag::Variant forward(ag::Variant input) override;
68 
69  protected:
70  ag::Container embedM_; // map id
71  ag::Container embedR_; // race
72  ag::Container embedRS_; // resources
73  ag::Container embedT_; // tech
74  ag::Container embedPT_; // pending tech and upgrades
75  ag::Container embedTM_; // time
76  ag::Container embedBO_; // current build
77 };
78 
80  public:
81  TORCH_ARG(int, bo_embsize) = 8;
82  TORCH_ARG(int, hid_dim) = 256;
83  TORCH_ARG(int, mapid_embsize) = 8;
84  TORCH_ARG(int, n_builds) = -1;
85  TORCH_ARG(int, n_unit_types) = 118 * 2;
86  TORCH_ARG(int, ptech_embsize) = 8;
87  TORCH_ARG(int, race_embsize) = 8;
88  TORCH_ARG(int, resources_embsize) = 8;
89  TORCH_ARG(std::set<std::string>, target_builds) = {};
90  TORCH_ARG(int, tech_embsize) = 8;
91  TORCH_ARG(int, time_embsize) = 8;
92  TORCH_ARG(bool, use_fabs) = false;
93  TORCH_ARG(bool, zero_units) = false;
94 
95  void reset() override;
96 
97  /** Expected input: Dictionary with "features" as tensor vector:
98  * - a BxF tensor containing the unit type counts (F = n_unit_types)
99  * - a BxF tensor containing our future unit type counts (F = n_unit_types / 2
100  * * 3)
101  * - a Bx1 tensor containing the map ID.
102  * - a Bx2 tensor containing the races,
103  * - a Bx4 tensor containing resource features.
104  * - a Bx142 tensor containing upgrades and techonology features.
105  * - a Bx142 tensor containing pending upgrades and techonology features.
106  * - a Bx1 tensor containing the current time (in frames)
107  * - a Bx1 tensor containing the active build
108  *
109  * Returns an unordered_map:
110  * - "vHeads": predicted values p(win) for all builds, masked wrt opponent
111  * race
112  * - "Pi": softmax over p(win), masked wrt opponent race
113  * - "V": overall value function -- currently just zeros
114  */
115  ag::Variant forward(ag::Variant input) override;
116 
117  protected:
118  ag::Container trunk_; // Featurizer
119  ag::Container linear_; // The linear model
120  ag::Container vHeads_; // Predicts game outcome for each build
121  torch::Tensor masks_; // Model output masks for all races
122 };
123 
125  public:
126  TORCH_ARG(int, bo_embsize) = 8;
127  TORCH_ARG(int, hid_dim) = 256;
128  TORCH_ARG(int, mapid_embsize) = 8;
129  TORCH_ARG(int, n_builds) = -1;
130  TORCH_ARG(int, n_layers) = 3;
131  TORCH_ARG(int, n_unit_types) = 118 * 2;
132  TORCH_ARG(int, race_embsize) = 8;
133  TORCH_ARG(int, resources_embsize) = 8;
134  TORCH_ARG(int, tech_embsize) = 8;
135  TORCH_ARG(int, ptech_embsize) = 8;
136  TORCH_ARG(int, time_embsize) = 8;
137  TORCH_ARG(bool, use_fabs) = false;
138  TORCH_ARG(bool, zero_units) = false;
139  TORCH_ARG(std::set<std::string>, target_builds) = {};
140 
141  void reset() override;
142 
143  /** Expected input: Dictionary with "features" as tensor vector:
144  * - a BxF tensor containing the unit type counts (F = n_unit_types)
145  * - a BxF tensor containing our future unit type counts (F = n_unit_types / 2
146  * * 3)
147  * - a Bx1 tensor containing the map ID.
148  * - a Bx2 tensor containing the races,
149  * - a Bx4 tensor containing resource features.
150  * - a Bx142 tensor containing upgrades and techonology features.
151  * - a Bx142 tensor containing pending upgrades and techonology features.
152  * - a Bx1 tensor containing the current time (in frames)
153  * - a Bx1 tensor containing the active build
154  *
155  * Returns an unordered_map:
156  * - "vHeads": predicted values p(win) for all builds, masked wrt opponent
157  * race
158  * - "Pi": softmax over p(win), masked wrt opponent race
159  * - "V": overall value function -- currently just zeros
160  */
161  ag::Variant forward(ag::Variant input) override;
162 
163  protected:
164  ag::Container trunk_; // Featurizer
165  ag::Container mlp_; // The actual model
166  ag::Container vHeads_; // Predicts game outcome for each build
167  torch::Tensor masks_; // Model output masks for all races
168 };
169 
171  public:
172  TORCH_ARG(int, bo_embsize) = 8;
173  TORCH_ARG(int, hid_dim) = 256;
174  TORCH_ARG(int, mapid_embsize) = 8;
175  TORCH_ARG(int, n_builds) = -1;
176  TORCH_ARG(int, n_layers) = 1;
177  TORCH_ARG(int, n_unit_types) = 118 * 2;
178  TORCH_ARG(int, race_embsize) = 8;
179  TORCH_ARG(int, resources_embsize) = 8;
180  TORCH_ARG(int, tech_embsize) = 8;
181  TORCH_ARG(int, ptech_embsize) = 8;
182  TORCH_ARG(int, time_embsize) = 8;
183  TORCH_ARG(bool, use_fabs) = false;
184  TORCH_ARG(bool, zero_units) = false;
185  TORCH_ARG(std::set<std::string>, target_builds) = {};
186 
187  void reset() override;
188 
189  /** Expected input: Dictionary with "features" as tensor vector:
190  * - a TxBxF tensor containing the unit type counts (F = n_unit_types)
191  * - a TxBxF tensor containing our future unit type counts (F = n_unit_types /
192  * 2 * 3)
193  * - a TxBx1 tensor containing the map ID.
194  * - a 1xBx2 tensor containing the races,
195  * - a TxBx4 tensor containing resource features.
196  * - a TxBx142 tensor containing upgrades and techonology features.
197  * - a TxBx142 tensor containing pending upgrades and techonology features.
198  * - a TxBx1 tensor containing the current time (in frames)
199  * - a TxBx1 tensor containing the active build And optionally
200  * - "hidden": hidden activations for the LSTMs
201  *
202  * The 'T' dimension may be omitted.
203  *
204  * Returns an unordered_map:
205  * - "vHeads": predicted values p(win) for all builds, masked wrt opponent
206  * race
207  * - "Pi": softmax over p(win), masked wrt opponent race
208  * - "V": overall value function -- currently just zeros
209  * - "hidden": the hidden state
210  */
211  ag::Variant forward(ag::Variant input) override;
212 
213  protected:
214  ag::Container trunk_; // Featurizer
215  ag::Container lstm_; // The actual model
216  ag::Container vHeads_; // Predicts game outcome for each build
217  torch::Tensor masks_; // Model output masks for all races
218 };
219 
220 AUTOGRAD_CONTAINER_CLASS(ConvEncLstmModel) {
221  public:
222  TORCH_ARG(int, bo_embsize) = 8;
223  TORCH_ARG(std::function<decltype(torch::relu)>, cnn_nonlinearity) =
224  torch::relu;
225  TORCH_ARG(bool, deep_conv) = false;
226  TORCH_ARG(int, hid_dim) = 256;
227  TORCH_ARG(int, kernel_size) = 5;
228  TORCH_ARG(bool, map_features) = false;
229  TORCH_ARG(int, mapid_embsize) = 8;
230  TORCH_ARG(int, n_builds) = -1;
231  TORCH_ARG(int, n_layers) = 1;
232  TORCH_ARG(int, n_unit_types) = 118 * 2;
233  TORCH_ARG(int, ptech_embsize) = 8;
234  TORCH_ARG(int, race_embsize) = 8;
235  TORCH_ARG(int, resources_embsize) = 8;
236  TORCH_ARG(int, spatial_embsize) = 128;
237  TORCH_ARG(std::set<std::string>, target_builds) = {};
238  TORCH_ARG(int, tech_embsize) = 8;
239  TORCH_ARG(int, time_embsize) = 8;
240  TORCH_ARG(bool, use_fabs) = false;
241 
242  void reset() override;
243 
244  /**
245  * Expected input:
246  * Dictionary with "features" as tensor vector:
247  * - a 1xBxCxhxw tensor containing the map features,
248  * - a TxBx1 tensor containing the map ID.
249  * - a 1xBx2 tensor containing the races,
250  * - a TxBxFxHxW tensor containing the units features,
251  * - a TxBxF tensor containing the unit type counts (F = n_unit_types)
252  * - a TxBxF tensor containing our future unit type counts (F = n_unit_types /
253  * 2 * 3)
254  * - a TxBx4 tensor containing resource features.
255  * - a TxBx142 tensor containing upgrades and techonology features.
256  * - a TxBx142 tensor containing pending upgrades and techonology features.
257  * - a TxBx1 tensor containing the current frame number
258  * - a TxBx1 tensor containing the active build
259  * And optionally
260  * - "hidden": hidden activations for the LSTMs
261  *
262  * The 'T' dimension may be omitted.
263  *
264  * Returns an unordered_map:
265  * - "vHeads": predicted values p(win) for all builds, masked wrt opponent
266  * race
267  * - "Pi": softmax over p(win), masked wrt opponent race
268  * - "V": overall value function -- currently just zeros
269  * - "hidden": the hidden state
270  */
271  ag::Variant forward(ag::Variant input) override;
272 
273  protected:
274  ag::Container trunk_; // Featurizer
275  ag::Container mapConv_;
276  ag::Container convnet_; // CNN on spatial unit features
277  ag::Container cembed_; // CNN output -> embedding
278  ag::Container lstm_; // The actual model
279  ag::Container vHeads_; // Predicts game outcome for each build
280  torch::Tensor masks_; // Model output masks for all races
281 };
282 
283 } // namespace bos
284 } // namespace cherrypi
AUTOGRAD_CONTAINER_CLASS(IdleModel)
Definition: models.h:34
std::map< std::string, std::string > modelFlags()
Definition: models.cpp:159
Main namespace for bot-related code.
Definition: areainfo.cpp:17
ag::Container modelMakeFromCli(double dropout)
Construct a BOS module according to command-line flags.
Definition: models.cpp:84