-
Notifications
You must be signed in to change notification settings - Fork 23
/
chxvm_state.cc
303 lines (260 loc) · 9.4 KB
/
chxvm_state.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
#include "runtime/chxvm_state.h"
#include <map>
#include <chainerx/routines/logic.h>
#include <chainerx/routines/manipulation.h>
#include <chainerx/routines/reduction.h>
#include <common/log.h>
#include <common/strutil.h>
#include <runtime/chxvm.h>
#include <runtime/chxvm_op.h>
#include <runtime/chxvm_var.h>
namespace chainer_compiler {
namespace runtime {
ChxVMState::ChxVMState(const ChxVMOptions& options, int num_variables, const InOuts& inputs)
: pc_(0), variables_(num_variables), inputs_(inputs), options_(options) {
}
ChxVMState::~ChxVMState() {
}
chainerx::Array ChxVMState::GetArray(int index) {
CHECK_LE(0, index) << index;
CHECK_GT(variables_.size(), index) << index;
CHECK(variables_[index].get());
return variables_[index]->GetArray();
}
absl::optional<chainerx::Array> ChxVMState::GetOptionalArray(int index) {
if (index < 0) return absl::nullopt;
return GetArray(index);
}
std::vector<chainerx::Array> ChxVMState::GetArrayList(const std::vector<int>& index) {
std::vector<chainerx::Array> vars;
for (int i : index) vars.push_back(GetArray(i));
return vars;
}
void ChxVMState::SetArrayList(const std::vector<int>& index, const std::vector<chainerx::Array>& vars) {
CHECK_EQ(index.size(), vars.size());
for (size_t i = 0; i < index.size(); ++i) SetArray(index[i], vars[i]);
}
ChxVMSequence* ChxVMState::CreateSequence(int index) {
CHECK_LE(0, index) << index;
CHECK_GT(variables_.size(), index) << index;
variables_[index].reset(new ChxVMVar(std::make_shared<ChxVMSequence>()));
return GetSequence(index);
}
ChxVMSequence* ChxVMState::GetSequence(int index) {
CHECK_LE(0, index) << index;
CHECK_GT(variables_.size(), index) << index;
CHECK(variables_[index].get());
return variables_[index]->GetSequence();
}
const ChxVMOpaque& ChxVMState::GetOpaque(int index) {
CHECK_LE(0, index) << index;
CHECK_GT(variables_.size(), index) << index;
CHECK(variables_[index].get());
return *variables_[index]->GetOpaque();
}
void ChxVMState::SetOpaque(int index, ChxVMOpaque* opaque) {
CHECK_LE(0, index) << index;
CHECK_GT(variables_.size(), index) << index;
CHECK(!variables_[index].get());
variables_[index].reset(new ChxVMVar(opaque));
}
ChxVMVar* ChxVMState::GetVar(int index) {
CHECK_LE(0, index) << index;
CHECK_GT(variables_.size(), index) << index;
CHECK(variables_[index].get());
return variables_[index].get();
}
absl::optional<ChxVMVar*> ChxVMState::GetOptionalVar(int index) {
if (index < 0) return absl::nullopt;
return GetVar(index);
}
void ChxVMState::SetVar(int index, const ChxVMVar& var) {
CHECK_LE(0, index) << index;
CHECK_GT(variables_.size(), index) << index;
CHECK(!variables_[index].get());
variables_[index].reset(new ChxVMVar(var));
}
const chainerx::Shape& ChxVMState::GetShape(int index) {
CHECK_LE(0, index) << index;
CHECK_GT(variables_.size(), index) << index;
CHECK(variables_[index].get());
return variables_[index]->GetShape();
}
void ChxVMState::SetShape(int index, chainerx::Shape s) {
CHECK_LE(0, index) << index;
CHECK_GT(variables_.size(), index) << index;
CHECK(!variables_[index].get());
variables_[index].reset(new ChxVMVar(s));
}
const StrictScalar& ChxVMState::GetScalar(int index) {
CHECK_LE(0, index) << index;
CHECK_GT(variables_.size(), index) << index;
CHECK(variables_[index].get());
return variables_[index]->GetScalar();
}
absl::optional<StrictScalar> ChxVMState::GetOptionalScalar(int index) {
if (index < 0) return absl::nullopt;
return GetScalar(index);
}
int64_t ChxVMState::GetOptionalInt(int index, int64_t default_value) {
absl::optional<ChxVMVar*> var = GetOptionalVar(index);
if (var.has_value()) {
return static_cast<int64_t>((*var)->GetScalar());
} else {
return default_value;
}
}
void ChxVMState::SetScalar(int index, StrictScalar s) {
CHECK_LE(0, index) << index;
CHECK_GT(variables_.size(), index) << index;
CHECK(!variables_[index].get());
variables_[index].reset(new ChxVMVar(s));
}
std::string ChxVMState::GetVarString(int index) {
if (index < 0) return "null";
CHECK_GT(variables_.size(), index) << index;
if (!variables_[index].get()) return "UNSET";
if (trace_level() > 1 || options_.verbose_ops[(*program_)[pc_]->op()])
return variables_[index]->DebugString();
else
return variables_[index]->ToString();
}
std::string ChxVMState::GetVarListString(const std::vector<int>& indices) {
std::ostringstream oss;
oss << '[';
bool is_first = true;
for (int index : indices) {
if (!is_first) oss << ", ";
is_first = false;
if (index < 0) {
oss << "null";
continue;
}
ChxVMVar* var = GetVar(index);
if (!var) {
oss << "null";
continue;
}
oss << var->Sigil() << index << '=';
oss << GetVarString(index);
}
oss << ']';
return oss.str();
}
void ChxVMState::SetArray(int index, const chainerx::Array& value) {
CHECK_LE(0, index) << index;
CHECK_GT(variables_.size(), index) << index;
CHECK(!variables_[index].get());
variables_[index].reset(new ChxVMVar(value));
}
void ChxVMState::FreeVar(int index) {
CHECK_LE(0, index) << index;
CHECK_GT(variables_.size(), index) << index;
CHECK(variables_[index].get()) << index;
variables_[index].reset();
}
void ChxVMState::Input(const std::string& name, int index) {
CHECK_LE(0, index) << index;
CHECK_GT(variables_.size(), index) << index;
CHECK(!variables_[index].get()) << index;
auto found = inputs_.find(name);
CHECK(found != inputs_.end()) << "Input value not exist: " << name;
variables_[index].reset(new ChxVMVar(*found->second.get()));
inputs_.erase(found);
}
void ChxVMState::Output(const std::string& name, int index) {
CHECK_LE(0, index) << index;
CHECK_GT(variables_.size(), index) << index;
CHECK(variables_[index].get()) << index;
CHECK(outputs_.emplace(name, std::shared_ptr<ChxVMVar>(new ChxVMVar(*variables_[index]))).second) << "Duplicated output name: " << name;
}
void ChxVMState::ReportInvalidInOuts(const std::vector<int>& inputs, const std::vector<int>& outputs) {
for (size_t i = 0; i < inputs.size(); ++i) {
if (inputs[i] < 0)
std::cerr << "input #" << i << ": null\n";
else
std::cerr << "input #" << i << ": " << GetVarString(inputs[i]) << std::endl;
}
for (size_t i = 0; i < outputs.size(); ++i) {
std::cerr << "output #" << i << ": " << GetVarString(outputs[i]) << std::endl;
}
CHECK(false);
}
namespace {
bool HasElemInArray(chainerx::Array (*pred_fn)(const chainerx::Array&), const chainerx::Array& a) {
chainerx::Array matched = pred_fn(a);
int result = static_cast<int>(chainerx::AsScalar(chainerx::Sum(matched)));
if (result) return true;
return false;
}
bool HasElemInVar(chainerx::Array (*pred_fn)(const chainerx::Array&), const ChxVMVar& var) {
switch (var.kind()) {
case ChxVMVar::Kind::kShape:
case ChxVMVar::Kind::kScalar:
case ChxVMVar::Kind::kArray:
return HasElemInArray(pred_fn, var.GetArray());
case ChxVMVar::Kind::kSequence:
for (const ChxVMVar& v : *var.GetSequence()) {
if (HasElemInVar(pred_fn, v)) return true;
}
return false;
case ChxVMVar::Kind::kString:
case ChxVMVar::Kind::kOpaque:
case ChxVMVar::Kind::kNull:
return false;
}
CHECK(false);
}
} // namespace
void ChxVMState::CheckNans(const std::vector<int>& inputs, const std::vector<int>& outputs) {
for (int output : outputs) {
if (output < 0) continue;
if (!HasElemInVar(chainerx::IsNan, *GetVar(output))) continue;
std::cerr << "NaN detected!\n";
ReportInvalidInOuts(inputs, outputs);
}
}
void ChxVMState::CheckInfs(const std::vector<int>& inputs, const std::vector<int>& outputs) {
for (int output : outputs) {
if (output < 0) continue;
if (!HasElemInVar(chainerx::IsInf, *GetVar(output))) continue;
std::cerr << "Inf detected!\n";
ReportInvalidInOuts(inputs, outputs);
}
}
void ChxVMState::ShowVariableStatus() const {
for (size_t i = 0; i < variables_.size(); ++i) {
const std::unique_ptr<ChxVMVar>& var = variables_[i];
if (!var.get()) continue;
const int64_t size = var->GetNBytes();
std::cerr << "$" << i << ": " << size << std::endl;
}
}
int64_t ChxVMState::GetTotalVariableSize() const {
std::map<void*, int64_t> array_sizes;
for (const auto& v : variables_) {
if (!v) {
continue;
}
for (const chainerx::Array& a : v->GetArrays()) {
array_sizes[a.raw_data()] = std::max(array_sizes[a.raw_data()], a.GetNBytes());
}
}
for (const auto& p : inputs_) {
for (const chainerx::Array& a : p.second->GetArrays()) {
array_sizes[a.raw_data()] = std::max(array_sizes[a.raw_data()], a.GetNBytes());
}
}
for (const auto& p : outputs_) {
for (const chainerx::Array& a : p.second->GetArrays()) {
array_sizes[a.raw_data()] = std::max(array_sizes[a.raw_data()], a.GetNBytes());
}
}
int64_t total_size = 0;
for (const auto& p : array_sizes) {
total_size += p.second;
}
return total_size;
}
} // namespace runtime
} // namespace chainer_compiler