TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
defogger.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 "cherrypi.h"
11 
12 #include "utils.h"
13 #include <autogradpp/autograd.h>
14 
15 // TODO use torch::Sequential to simplify the code where possible once we don't
16 // need layer-by-layer comparisons.
17 // TODO do nonlins in place (another advantage of making them into modules)
18 
19 namespace cherrypi {
20 namespace defogger {
21 
22 using nonlin_type = std::function<torch::Tensor(torch::Tensor)>;
23 
24 // Type of a function which creates a container (morally, some kind of
25 // convolution), whose parameters are input_size, output_size, kernel_size,
26 // stride, padding and no_bias.
27 using conv_builder =
28  std::function<ag::Container(uint32_t, uint32_t, int, int, int, bool)>;
29 
30 // Simple wrapper for ag::Conv2d.
31 ag::Container conv2dBuilder(
32  uint32_t input_size,
33  uint32_t output_size,
34  int convsize,
35  int stride,
36  int padding,
37  bool no_bias);
38 
39 AUTOGRAD_CONTAINER_CLASS(MapRaceFeaturize) {
40  // This puts the StarCraft map at the same pooling (kernel_size and stride)
41  // as the features coming from the featurizer, and concatenates with inputs.
42  public:
43  TORCH_ARG(int, map_embsize) = 64;
44  TORCH_ARG(int, race_embsize) = 8;
45  TORCH_ARG(int, kernel_size) = 128;
46  TORCH_ARG(int, stride) = 32;
47 
48  void reset() override;
49 
50  ag::Variant forward(ag::Variant input) override;
51 
52  protected:
53  ag::Container conv1_;
54  ag::Container conv2_;
55  ag::Container conv3_;
56  ag::Container embedR_;
57 };
58 
60  public:
61  Convnet(
62  conv_builder conv,
63  nonlin_type nonlin,
64  int convsize_0,
65  int convsize,
66  int padding_0,
67  int padding,
68  int input_size,
69  int interm_size,
70  int output_size)
71  : conv_(conv),
72  nonlin_(nonlin),
73  convsize_0_(convsize_0),
74  convsize_(convsize),
75  padding_0_(padding),
76  padding_(padding),
77  input_size_(input_size),
78  interm_size_(interm_size),
79  output_size_(output_size){};
80 
81  TORCH_ARG(int, depth) = 2;
82  TORCH_ARG(int, stride_0) = 1;
83  TORCH_ARG(int, stride) = 1;
84 
85  void reset() override;
86 
87  ag::Variant forward(ag::Variant input) override;
88 
89  protected:
90  ag::Container conv0_;
91  std::vector<ag::Container> convS_;
92  ag::Container conv_output_;
93 
94  conv_builder conv_;
95  nonlin_type nonlin_;
96  int convsize_0_;
97  int convsize_;
98  int padding_0_;
99  int padding_;
100  int input_size_;
101  int interm_size_;
102  int output_size_;
103 };
104 
105 // Simply a wrapper over Convnet with some defaults.
106 Convnet SimpleConvnet(
107  conv_builder conv,
108  nonlin_type nonlin,
109  int convsize,
110  int padding,
111  int input_size,
112  int output_size,
113  int depth = 2,
114  int stride = 1);
115 
117  public:
118  Decoder(
119  conv_builder conv,
120  nonlin_type nonlin,
121  int convsize_0,
122  int convsize,
123  int input_size,
124  int interm_size,
125  int output_size)
126  : conv_(conv),
127  nonlin_(nonlin),
128  convsize_0_(convsize_0),
129  convsize_(convsize),
130  input_size_(input_size),
131  interm_size_(interm_size),
132  output_size_(output_size){};
133 
134  TORCH_ARG(int, depth) = 2;
135  TORCH_ARG(int, stride_0) = 1;
136  TORCH_ARG(int, stride) = 1;
137 
138  void reset() override;
139 
140  ag::Variant forward(ag::Variant input) override;
141 
142  protected:
143  ag::Container convnet_;
144 
145  conv_builder conv_;
146  nonlin_type nonlin_;
147  int convsize_0_;
148  int convsize_;
149  int input_size_;
150  int interm_size_;
151  int output_size_;
152 };
153 
154 AUTOGRAD_CONTAINER_CLASS(DefoggerModel) {
155  // Multi level LSTM model from starcraft_defogger.
156  public:
157  DefoggerModel(
158  conv_builder conv,
159  nonlin_type nonlin,
160  int kernel_size,
161  int n_inp_feats,
162  int stride)
163  : conv_(conv),
164  nonlin_(nonlin),
165  kernel_size_(kernel_size),
166  n_inp_feats_(n_inp_feats),
167  stride_(stride){};
168 
169  // lstm kwargs
170  TORCH_ARG(int, map_embsize) = 64;
171  TORCH_ARG(int, race_embsize) = 8;
172  TORCH_ARG(int, dec_convsize) = 3;
173  TORCH_ARG(int, dec_depth) = 3;
174  TORCH_ARG(int, dec_embsize) = 128;
175  TORCH_ARG(int, hid_dim) = 256;
176  TORCH_ARG(float, lstm_dropout) = 0;
177 
178  // simple kwargs
179  TORCH_ARG(bool, bypass_encoder) = false;
180  TORCH_ARG(int, enc_convsize) = 3;
181  TORCH_ARG(int, enc_embsize) = 256;
182  TORCH_ARG(int, enc_depth) = 3;
183  TORCH_ARG(int, inp_embsize) = 256;
184  TORCH_ARG(std::string, top_pooling) = "mean"; // TODO change to enum?
185 
186  TORCH_ARG(bool, predict_delta) = false;
187 
188  // multilvl_lstm kwargs
189  TORCH_ARG(int, midconv_kw) = 3;
190  TORCH_ARG(int, midconv_stride) = 2;
191  TORCH_ARG(int, midconv_depth) = 2;
192  TORCH_ARG(int, n_lvls) = 2;
193  TORCH_ARG(utils::UpsampleMode, upsample) = utils::UpsampleMode::Bilinear;
194  TORCH_ARG(std::string, model_name) = "multilvl_lstm";
195 
196  void reset() override;
197 
198  // Reset the hidden state (to call before each game)
199  void zero_hidden();
200 
201  ag::Variant forward(ag::Variant input) override;
202 
203  // Load all parameters from the python ones.
204  void load_parameters(std::string const& path_to_npz);
205 
206  protected:
207  void repackage_hidden();
208 
209  torch::Tensor encode(torch::Tensor x);
210  torch::Tensor do_rnn_middle(torch::Tensor x, at::IntList sz, int i);
211  torch::Tensor pooling(torch::Tensor x, std::string method = "");
212  ag::tensor_list trunk_encode_pool(ag::tensor_list input);
213  torch::Tensor do_rnn(
214  torch::Tensor x, at::IntList size, torch::Tensor & hidden);
215  ag::tensor_list do_heads(torch::Tensor x);
216  ag::tensor_list forward_rest(ag::tensor_list input);
217 
218  conv_builder conv_;
219  nonlin_type nonlin_;
220 
221  ag::Container trunk_; // Map/race featurizer
222  ag::Container sum_pool_embed_;
223  ag::Container conv1x1_;
224  std::vector<ag::Container> midnets_;
225  std::vector<ag::Container> midrnns_;
226  ag::Container rnn_;
227  ag::Container decoder_;
228  ag::Container regression_head_;
229  ag::Container unit_class_head_;
230  ag::Container bldg_class_head_;
231  ag::Container opbt_class_head_;
232 
233  ag::tensor_list append_to_decoder_input_;
234  std::vector<torch::Tensor> hidden_;
235 
236  at::IntList input_sz_;
237  int lstm_nlayers_;
238  int kernel_size_;
239  int n_inp_feats_;
240  int stride_;
241 
242  public:
243 // Global variable holding the activations of the python model for easy
244 // comparisons everywhere in the code. Ugly but only temporary (and avoids
245 // modifying all function signatures).
246 #ifndef WITHOUT_POSIX
247  static std::unique_ptr<cnpy::npz_t> layers;
248 #endif
249  // Global variable used by external containers when comparing activations.
250  static std::string prefix;
251 };
252 
253 } // namespace defogger
254 } // namespace cherrypi
ag::Container conv2dBuilder(uint32_t input_size, uint32_t output_size, int convsize, int stride, int padding, bool no_bias)
Definition: defogger.cpp:15
AUTOGRAD_CONTAINER_CLASS(MapRaceFeaturize)
Definition: defogger.h:39
std::function< torch::Tensor(torch::Tensor)> nonlin_type
Definition: defogger.h:22
Convnet SimpleConvnet(conv_builder conv, nonlin_type nonlin, int convsize, int padding, int input_size, int output_size, int depth, int stride)
Definition: defogger.cpp:169
std::function< ag::Container(uint32_t, uint32_t, int, int, int, bool)> conv_builder
Definition: defogger.h:28
Main namespace for bot-related code.
Definition: areainfo.cpp:17