forked from google-research/google-research
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.cc
428 lines (388 loc) · 16.5 KB
/
generator.cc
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
// Copyright 2020 The Google Research Authors.
//
// 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.
#include "generator.h"
#include "definitions.h"
#include "instruction.pb.h"
#include "instruction.h"
#include "random_generator.h"
#include "absl/memory/memory.h"
namespace automl_zero {
using ::absl::make_unique;
using ::std::endl;
using ::std::make_shared;
using ::std::mt19937;
using ::std::shared_ptr;
using ::std::vector;
void PadComponentFunctionWithInstruction(
const size_t total_instructions,
const shared_ptr<const Instruction>& instruction,
vector<shared_ptr<const Instruction>>* component_function) {
component_function->reserve(total_instructions);
while (component_function->size() < total_instructions) {
component_function->emplace_back(instruction);
}
}
Generator::Generator(
const HardcodedAlgorithmID init_model,
const IntegerT setup_size_init,
const IntegerT predict_size_init,
const IntegerT learn_size_init,
const vector<Op>& allowed_setup_ops,
const vector<Op>& allowed_predict_ops,
const vector<Op>& allowed_learn_ops,
mt19937* bit_gen,
RandomGenerator* rand_gen)
: init_model_(init_model),
setup_size_init_(setup_size_init),
predict_size_init_(predict_size_init),
learn_size_init_(learn_size_init),
allowed_setup_ops_(allowed_setup_ops),
allowed_predict_ops_(allowed_predict_ops),
allowed_learn_ops_(allowed_learn_ops),
rand_gen_(rand_gen),
randomizer_(
allowed_setup_ops,
allowed_predict_ops,
allowed_learn_ops,
bit_gen,
rand_gen_),
no_op_instruction_(make_shared<const Instruction>()) {}
Algorithm Generator::TheInitModel() {
return ModelByID(init_model_);
}
Algorithm Generator::ModelByID(const HardcodedAlgorithmID model) {
switch (model) {
case NO_OP_ALGORITHM:
return NoOp();
case RANDOM_ALGORITHM:
return Random();
case NEURAL_NET_ALGORITHM:
return NeuralNet(
kDefaultLearningRate, 0.1, 0.1);
case INTEGRATION_TEST_DAMAGED_NEURAL_NET_ALGORITHM: {
Algorithm algorithm = NeuralNet(
kDefaultLearningRate, 0.1, 0.1);
// Delete the first two instructions in setup which are the
// gaussian initialization of the first and final layer weights.
algorithm.setup_.erase(algorithm.setup_.begin());
algorithm.setup_.erase(algorithm.setup_.begin());
return algorithm;
}
case LINEAR_ALGORITHM:
return LinearModel(kDefaultLearningRate);
default:
LOG(FATAL) << "Unsupported algorithm ID." << endl;
}
}
inline void FillComponentFunctionWithInstruction(
const IntegerT num_instructions,
const shared_ptr<const Instruction>& instruction,
vector<shared_ptr<const Instruction>>* component_function) {
component_function->reserve(num_instructions);
component_function->clear();
for (IntegerT pos = 0; pos < num_instructions; ++pos) {
component_function->emplace_back(instruction);
}
}
Algorithm Generator::NoOp() {
Algorithm algorithm;
FillComponentFunctionWithInstruction(
setup_size_init_, no_op_instruction_, &algorithm.setup_);
FillComponentFunctionWithInstruction(
predict_size_init_, no_op_instruction_, &algorithm.predict_);
FillComponentFunctionWithInstruction(
learn_size_init_, no_op_instruction_, &algorithm.learn_);
return algorithm;
}
Algorithm Generator::Random() {
Algorithm algorithm = NoOp();
CHECK(setup_size_init_ == 0 || !allowed_setup_ops_.empty());
CHECK(predict_size_init_ == 0 || !allowed_predict_ops_.empty());
CHECK(learn_size_init_ == 0 || !allowed_learn_ops_.empty());
randomizer_.Randomize(&algorithm);
return algorithm;
}
void PadComponentFunctionWithRandomInstruction(
const size_t total_instructions, const Op op,
RandomGenerator* rand_gen,
vector<shared_ptr<const Instruction>>* component_function) {
component_function->reserve(total_instructions);
while (component_function->size() < total_instructions) {
component_function->push_back(make_shared<const Instruction>(op, rand_gen));
}
}
Generator::Generator()
: init_model_(RANDOM_ALGORITHM),
setup_size_init_(6),
predict_size_init_(3),
learn_size_init_(9),
allowed_setup_ops_(
{NO_OP, SCALAR_SUM_OP, MATRIX_VECTOR_PRODUCT_OP, VECTOR_MEAN_OP}),
allowed_predict_ops_(
{NO_OP, SCALAR_SUM_OP, MATRIX_VECTOR_PRODUCT_OP, VECTOR_MEAN_OP}),
allowed_learn_ops_(
{NO_OP, SCALAR_SUM_OP, MATRIX_VECTOR_PRODUCT_OP, VECTOR_MEAN_OP}),
bit_gen_owned_(make_unique<mt19937>(GenerateRandomSeed())),
rand_gen_owned_(make_unique<RandomGenerator>(bit_gen_owned_.get())),
rand_gen_(rand_gen_owned_.get()),
randomizer_(
allowed_setup_ops_,
allowed_predict_ops_,
allowed_learn_ops_,
bit_gen_owned_.get(),
rand_gen_),
no_op_instruction_(make_shared<const Instruction>()) {}
Algorithm Generator::UnitTestNeuralNetNoBiasNoGradient(
const double learning_rate) {
Algorithm algorithm;
// Scalar addresses
constexpr AddressT kLearningRateAddress = 2;
constexpr AddressT kPredictionErrorAddress = 3;
CHECK_GE(kMaxScalarAddresses, 4);
// Vector addresses.
constexpr AddressT kFinalLayerWeightsAddress = 1;
CHECK_EQ(
kFinalLayerWeightsAddress,
Generator::kUnitTestNeuralNetNoBiasNoGradientFinalLayerWeightsAddress);
constexpr AddressT kFirstLayerOutputBeforeReluAddress = 2;
constexpr AddressT kFirstLayerOutputAfterReluAddress = 3;
constexpr AddressT kZerosAddress = 4;
constexpr AddressT kGradientWrtFinalLayerWeightsAddress = 5;
constexpr AddressT kGradientWrtActivationsAddress = 6;
constexpr AddressT kGradientOfReluAddress = 7;
CHECK_GE(kMaxVectorAddresses, 8);
// Matrix addresses.
constexpr AddressT kFirstLayerWeightsAddress = 0;
CHECK_EQ(
kFirstLayerWeightsAddress,
Generator::kUnitTestNeuralNetNoBiasNoGradientFirstLayerWeightsAddress);
constexpr AddressT kGradientWrtFirstLayerWeightsAddress = 1;
CHECK_GE(kMaxMatrixAddresses, 2);
shared_ptr<const Instruction> no_op_instruction =
make_shared<const Instruction>();
algorithm.setup_.emplace_back(make_shared<const Instruction>(
SCALAR_CONST_SET_OP,
kLearningRateAddress,
ActivationDataSetter(learning_rate)));
PadComponentFunctionWithInstruction(
setup_size_init_, no_op_instruction, &algorithm.setup_);
IntegerT num_predict_instructions = 5;
algorithm.predict_.reserve(num_predict_instructions);
// Multiply with first layer weight matrix.
algorithm.predict_.emplace_back(make_shared<const Instruction>(
MATRIX_VECTOR_PRODUCT_OP,
kFirstLayerWeightsAddress, kFeaturesVectorAddress,
kFirstLayerOutputBeforeReluAddress));
// Apply RELU.
algorithm.predict_.emplace_back(make_shared<const Instruction>(
VECTOR_MAX_OP, kFirstLayerOutputBeforeReluAddress, kZerosAddress,
kFirstLayerOutputAfterReluAddress));
// Dot product with final layer weight vector.
algorithm.predict_.emplace_back(make_shared<const Instruction>(
VECTOR_INNER_PRODUCT_OP, kFirstLayerOutputAfterReluAddress,
kFinalLayerWeightsAddress, kPredictionsScalarAddress));
PadComponentFunctionWithInstruction(
predict_size_init_, no_op_instruction, &algorithm.predict_);
algorithm.learn_.reserve(11);
algorithm.learn_.emplace_back(make_shared<const Instruction>(
SCALAR_DIFF_OP, kLabelsScalarAddress, kPredictionsScalarAddress,
kPredictionErrorAddress));
algorithm.learn_.emplace_back(make_shared<const Instruction>(
SCALAR_PRODUCT_OP,
kLearningRateAddress, kPredictionErrorAddress, kPredictionErrorAddress));
algorithm.learn_.emplace_back(make_shared<const Instruction>(
SCALAR_VECTOR_PRODUCT_OP, kPredictionErrorAddress,
kFirstLayerOutputAfterReluAddress, kGradientWrtFinalLayerWeightsAddress));
algorithm.learn_.emplace_back(make_shared<const Instruction>(
VECTOR_SUM_OP,
kFinalLayerWeightsAddress, kGradientWrtFinalLayerWeightsAddress,
kFinalLayerWeightsAddress));
algorithm.learn_.emplace_back(make_shared<const Instruction>(
SCALAR_VECTOR_PRODUCT_OP,
kPredictionErrorAddress, kFinalLayerWeightsAddress,
kGradientWrtActivationsAddress));
algorithm.learn_.emplace_back(make_shared<const Instruction>(
VECTOR_HEAVYSIDE_OP,
kFirstLayerOutputBeforeReluAddress, 0, kGradientOfReluAddress));
algorithm.learn_.emplace_back(make_shared<const Instruction>(
VECTOR_PRODUCT_OP,
kGradientOfReluAddress, kGradientWrtActivationsAddress,
kGradientWrtActivationsAddress));
algorithm.learn_.emplace_back(make_shared<const Instruction>(
VECTOR_OUTER_PRODUCT_OP,
kGradientWrtActivationsAddress, kFeaturesVectorAddress,
kGradientWrtFirstLayerWeightsAddress));
algorithm.learn_.emplace_back(make_shared<const Instruction>(
MATRIX_SUM_OP,
kFirstLayerWeightsAddress, kGradientWrtFirstLayerWeightsAddress,
kFirstLayerWeightsAddress));
PadComponentFunctionWithInstruction(
learn_size_init_, no_op_instruction, &algorithm.learn_);
return algorithm;
}
Algorithm Generator::NeuralNet(
const double learning_rate,
const double first_init_scale,
const double final_init_scale) {
Algorithm algorithm;
// Scalar addresses
constexpr AddressT kFinalLayerBiasAddress = 2;
constexpr AddressT kLearningRateAddress = 3;
constexpr AddressT kPredictionErrorAddress = 4;
CHECK_GE(kMaxScalarAddresses, 5);
// Vector addresses.
constexpr AddressT kFirstLayerBiasAddress = 1;
constexpr AddressT kFinalLayerWeightsAddress = 2;
constexpr AddressT kFirstLayerOutputBeforeReluAddress = 3;
constexpr AddressT kFirstLayerOutputAfterReluAddress = 4;
constexpr AddressT kZerosAddress = 5;
constexpr AddressT kGradientWrtFinalLayerWeightsAddress = 6;
constexpr AddressT kGradientWrtActivationsAddress = 7;
constexpr AddressT kGradientOfReluAddress = 8;
CHECK_GE(kMaxVectorAddresses, 9);
// Matrix addresses.
constexpr AddressT kFirstLayerWeightsAddress = 0;
constexpr AddressT kGradientWrtFirstLayerWeightsAddress = 1;
CHECK_GE(kMaxMatrixAddresses, 2);
shared_ptr<const Instruction> no_op_instruction =
make_shared<const Instruction>();
algorithm.setup_.emplace_back(make_shared<const Instruction>(
VECTOR_GAUSSIAN_SET_OP,
kFinalLayerWeightsAddress,
FloatDataSetter(0.0),
FloatDataSetter(final_init_scale)));
algorithm.setup_.emplace_back(make_shared<const Instruction>(
MATRIX_GAUSSIAN_SET_OP,
kFirstLayerWeightsAddress,
FloatDataSetter(0.0),
FloatDataSetter(first_init_scale)));
algorithm.setup_.emplace_back(make_shared<const Instruction>(
SCALAR_CONST_SET_OP,
kLearningRateAddress,
ActivationDataSetter(learning_rate)));
PadComponentFunctionWithInstruction(
setup_size_init_, no_op_instruction, &algorithm.setup_);
// Multiply with first layer weight matrix.
algorithm.predict_.emplace_back(make_shared<const Instruction>(
MATRIX_VECTOR_PRODUCT_OP,
kFirstLayerWeightsAddress, kFeaturesVectorAddress,
kFirstLayerOutputBeforeReluAddress));
// Add first layer bias.
algorithm.predict_.emplace_back(make_shared<const Instruction>(
VECTOR_SUM_OP, kFirstLayerOutputBeforeReluAddress, kFirstLayerBiasAddress,
kFirstLayerOutputBeforeReluAddress));
// Apply RELU.
algorithm.predict_.emplace_back(make_shared<const Instruction>(
VECTOR_MAX_OP, kFirstLayerOutputBeforeReluAddress, kZerosAddress,
kFirstLayerOutputAfterReluAddress));
// Dot product with final layer weight vector.
algorithm.predict_.emplace_back(make_shared<const Instruction>(
VECTOR_INNER_PRODUCT_OP, kFirstLayerOutputAfterReluAddress,
kFinalLayerWeightsAddress, kPredictionsScalarAddress));
// Add final layer bias.
CHECK_LE(kFinalLayerBiasAddress, kMaxScalarAddresses);
algorithm.predict_.emplace_back(make_shared<const Instruction>(
SCALAR_SUM_OP, kPredictionsScalarAddress, kFinalLayerBiasAddress,
kPredictionsScalarAddress));
PadComponentFunctionWithInstruction(
predict_size_init_, no_op_instruction, &algorithm.predict_);
algorithm.learn_.reserve(11);
algorithm.learn_.emplace_back(make_shared<const Instruction>(
SCALAR_DIFF_OP, kLabelsScalarAddress, kPredictionsScalarAddress,
kPredictionErrorAddress));
algorithm.learn_.emplace_back(make_shared<const Instruction>(
SCALAR_PRODUCT_OP,
kLearningRateAddress, kPredictionErrorAddress, kPredictionErrorAddress));
CHECK_LE(kFinalLayerBiasAddress, kMaxScalarAddresses);
// Update final layer bias.
algorithm.learn_.emplace_back(make_shared<const Instruction>(
SCALAR_SUM_OP, kFinalLayerBiasAddress, kPredictionErrorAddress,
kFinalLayerBiasAddress));
algorithm.learn_.emplace_back(make_shared<const Instruction>(
SCALAR_VECTOR_PRODUCT_OP, kPredictionErrorAddress,
kFirstLayerOutputAfterReluAddress, kGradientWrtFinalLayerWeightsAddress));
algorithm.learn_.emplace_back(make_shared<const Instruction>(
VECTOR_SUM_OP,
kFinalLayerWeightsAddress, kGradientWrtFinalLayerWeightsAddress,
kFinalLayerWeightsAddress));
algorithm.learn_.emplace_back(make_shared<const Instruction>(
SCALAR_VECTOR_PRODUCT_OP,
kPredictionErrorAddress, kFinalLayerWeightsAddress,
kGradientWrtActivationsAddress));
algorithm.learn_.emplace_back(make_shared<const Instruction>(
VECTOR_HEAVYSIDE_OP,
kFirstLayerOutputBeforeReluAddress, 0, kGradientOfReluAddress));
algorithm.learn_.emplace_back(make_shared<const Instruction>(
VECTOR_PRODUCT_OP,
kGradientOfReluAddress, kGradientWrtActivationsAddress,
kGradientWrtActivationsAddress));
// Update first layer bias.
algorithm.learn_.emplace_back(make_shared<const Instruction>(
VECTOR_SUM_OP, kFirstLayerBiasAddress, kGradientWrtActivationsAddress,
kFirstLayerBiasAddress));
algorithm.learn_.emplace_back(make_shared<const Instruction>(
VECTOR_OUTER_PRODUCT_OP,
kGradientWrtActivationsAddress, kFeaturesVectorAddress,
kGradientWrtFirstLayerWeightsAddress));
algorithm.learn_.emplace_back(make_shared<const Instruction>(
MATRIX_SUM_OP,
kFirstLayerWeightsAddress, kGradientWrtFirstLayerWeightsAddress,
kFirstLayerWeightsAddress));
PadComponentFunctionWithInstruction(
learn_size_init_, no_op_instruction, &algorithm.learn_);
return algorithm;
}
Algorithm Generator::LinearModel(const double learning_rate) {
Algorithm algorithm;
// Scalar addresses
constexpr AddressT kLearningRateAddress = 2;
constexpr AddressT kPredictionErrorAddress = 3;
CHECK_GE(kMaxScalarAddresses, 4);
// Vector addresses.
constexpr AddressT kWeightsAddress = 1;
constexpr AddressT kCorrectionAddress = 2;
CHECK_GE(kMaxVectorAddresses, 3);
CHECK_GE(kMaxMatrixAddresses, 0);
shared_ptr<const Instruction> no_op_instruction =
make_shared<const Instruction>();
algorithm.setup_.emplace_back(make_shared<const Instruction>(
SCALAR_CONST_SET_OP,
kLearningRateAddress,
ActivationDataSetter(learning_rate)));
PadComponentFunctionWithInstruction(
setup_size_init_, no_op_instruction, &algorithm.setup_);
algorithm.predict_.emplace_back(make_shared<const Instruction>(
VECTOR_INNER_PRODUCT_OP,
kWeightsAddress, kFeaturesVectorAddress, kPredictionsScalarAddress));
PadComponentFunctionWithInstruction(
predict_size_init_, no_op_instruction, &algorithm.predict_);
algorithm.learn_.emplace_back(make_shared<const Instruction>(
SCALAR_DIFF_OP,
kLabelsScalarAddress, kPredictionsScalarAddress,
kPredictionErrorAddress));
algorithm.learn_.emplace_back(make_shared<const Instruction>(
SCALAR_PRODUCT_OP,
kLearningRateAddress, kPredictionErrorAddress,
kPredictionErrorAddress));
algorithm.learn_.emplace_back(make_shared<const Instruction>(
SCALAR_VECTOR_PRODUCT_OP,
kPredictionErrorAddress, kFeaturesVectorAddress, kCorrectionAddress));
algorithm.learn_.emplace_back(make_shared<const Instruction>(
VECTOR_SUM_OP,
kWeightsAddress, kCorrectionAddress, kWeightsAddress));
PadComponentFunctionWithInstruction(
learn_size_init_, no_op_instruction, &algorithm.learn_);
return algorithm;
}
} // namespace automl_zero