forked from facebookresearch/rebel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_benchmark.cc
154 lines (142 loc) · 4.68 KB
/
gen_benchmark.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
// Copyright (c) Facebook, Inc. and its affiliates.
//
// 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 <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <torch/script.h>
#include "rela/context.h"
#include "rela/data_loop.h"
#include "rela/model_locker.h"
#include "rela/prioritized_replay.h"
#include "real_net.h"
#include "recursive_solving.h"
#include "subgame_solving.h"
#include "util.h"
using namespace liars_dice;
using namespace rela;
int get_depth(const Tree& tree, int root = 0) {
int depth = 1;
for (auto child : ChildrenIt(tree[root])) {
depth = std::max(depth, 1 + get_depth(tree, child));
}
return depth;
}
struct Timer {
std::chrono::time_point<std::chrono::system_clock> start =
std::chrono::system_clock::now();
double tick() {
const auto end = std::chrono::system_clock::now();
std::chrono::duration<double> diff = end - start;
return diff.count();
}
};
int main(int argc, char* argv[]) {
int num_dice = 1;
int num_faces = 4;
int fp_iters = 1024;
int mdp_depth = 2;
int num_threads = 10;
int per_gpu = 1;
int num_cycles = 6;
std::string device = "cuda:1";
std::string net_path;
{
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (arg == "--num_dice") {
assert(i + 1 < argc);
num_dice = std::stoi(argv[++i]);
} else if (arg == "--num_faces") {
assert(i + 1 < argc);
num_faces = std::stoi(argv[++i]);
} else if (arg == "--fp_iters") {
assert(i + 1 < argc);
fp_iters = std::stoi(argv[++i]);
} else if (arg == "--mdp_depth") {
assert(i + 1 < argc);
mdp_depth = std::stoi(argv[++i]);
} else if (arg == "--num_threads") {
assert(i + 1 < argc);
num_threads = std::stoi(argv[++i]);
} else if (arg == "--per_gpu") {
assert(i + 1 < argc);
per_gpu = std::stoi(argv[++i]);
} else if (arg == "--num_cycles") {
assert(i + 1 < argc);
num_cycles = std::stoi(argv[++i]);
} else if (arg == "--device") {
assert(i + 1 < argc);
device = argv[++i];
} else if (arg == "--net") {
assert(i + 1 < argc);
net_path = argv[++i];
} else {
std::cerr << "Unknown flag: " << arg << "\n";
return -1;
}
}
}
assert(num_dice != -1);
assert(num_faces != -1);
assert(mdp_depth != -1);
const Game game(num_dice, num_faces);
assert(mdp_depth > 0);
assert(!net_path.empty());
std::cout << "num_dice=" << num_dice << " num_faces=" << num_faces << "\n";
{
const auto full_tree = unroll_tree(game);
std::cout << "Tree of depth " << get_depth(full_tree) << " has "
<< full_tree.size() << " nodes\n";
}
std::vector<TorchJitModel> models;
for (int i = 0; i < per_gpu; ++i) {
auto module = torch::jit::load(net_path);
module.eval();
module.to(device);
models.push_back(module);
}
std::vector<TorchJitModel*> model_ptrs;
for (int i = 0; i < per_gpu; ++i) {
model_ptrs.push_back(&models[i]);
}
auto locker = std::make_shared<ModelLocker>(model_ptrs, device);
auto replay = std::make_shared<ValuePrioritizedReplay>(1 << 20, 1000, 1.0,
0.4, 3, false, false);
auto context = std::make_shared<Context>();
RecursiveSolvingParams cfg;
cfg.num_dice = num_dice;
cfg.num_faces = num_faces;
cfg.subgame_params.num_iters = fp_iters;
cfg.subgame_params.linear_update = true;
cfg.subgame_params.optimistic = false;
cfg.subgame_params.max_depth = mdp_depth;
for (int i = 0; i < num_threads; ++i) {
const int seed = i;
auto connector = std::make_shared<CVNetBufferConnector>(locker, replay);
std::shared_ptr<ThreadLoop> loop =
std::make_shared<DataThreadLoop>(std::move(connector), cfg, seed);
context->pushThreadLoop(loop);
}
std::cout << "Starting the context" << std::endl;
context->start();
Timer t;
for (int i = 0; i < num_cycles; ++i) {
std::this_thread::sleep_for(std::chrono::seconds(10));
double secs = t.tick();
auto added = replay->numAdd();
std::cout << "time=" << secs << " "
<< "items=" << added << " per_second=" << added / secs << "\n";
}
}