forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.cpp
402 lines (385 loc) · 13 KB
/
interpreter.cpp
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
#include <torch/csrc/jit/mobile/interpreter.h>
#include <ATen/core/class_type.h>
#include <ATen/core/dynamic_type.h>
#include <ATen/core/function.h>
#include <ATen/core/jit_type.h>
#include <ATen/core/operator_name.h>
#include <ATen/record_function.h>
#include <c10/util/Exception.h>
#include <c10/util/irange.h>
#include <torch/csrc/jit/backends/backend_exception.h>
#include <torch/csrc/jit/mobile/function.h>
#include <torch/csrc/jit/mobile/observer.h>
#include <torch/csrc/jit/mobile/promoted_prim_ops.h>
#include <torch/csrc/jit/runtime/jit_exception.h>
#include <torch/csrc/jit/runtime/vararg_functions.h>
namespace torch::jit {
char const* toString(OpCode op);
std::ostream& operator<<(std::ostream& out, Instruction inst);
namespace mobile {
InterpreterState::InterpreterState(const Code& code) {
enterFrame(code);
}
namespace {
static thread_local std::vector<DebugHandle> exception_debug_handles_;
void createObject(Stack& stack, const at::ClassTypePtr& type) {
auto userObj = c10::ivalue::Object::create(
c10::StrongTypePtr(type->compilation_unit(), type),
type->numAttributes());
push(stack, std::move(userObj));
}
void isinstance(Stack& stack, at::ArrayRef<at::TypePtr> types) {
at::TypePtr ty = pop(stack).type<c10::DynamicType>();
for (const at::TypePtr& candidate : types) {
if (ty->isSubtypeOf(*candidate)) {
push(stack, true);
return;
}
}
push(stack, false);
}
} // namespace
using namespace at;
const std::vector<DebugHandle>& getInterpretersExceptionDebugHandles() {
return exception_debug_handles_;
}
void InterpreterState::enterFrame(const Code& code) {
frames_.emplace_back(code);
registers_.resize(registers_.size() + code.register_size_);
}
void InterpreterState::leaveFrame() {
registers_.resize(
registers_.size() - frames_.back().getCode().register_size_);
frames_.pop_back();
}
void InterpreterState::saveExceptionDebugHandles() {
std::vector<DebugHandle> exception_debug_handles;
for (auto frame = frames_.crbegin(); frame != frames_.crend(); frame++) {
size_t pc = frame->getPC() - (frame != frames_.crbegin() ? 1 : 0);
if (auto handle = frame->getDebugHandle(pc)) {
exception_debug_handles.push_back(*handle);
} else {
exception_debug_handles.push_back(-1);
}
}
exception_debug_handles_ = std::move(exception_debug_handles);
}
void InterpreterState::callFunction(torch::jit::Function& f, Stack& stack) {
bool newFrame =
f.call(stack, [&](const mobile::Code& code) { enterFrame(code); });
(frames_.rbegin() + (newFrame ? 1 : 0))->step();
}
bool InterpreterState::run(Stack& stack) {
while (true) {
try {
auto& frame = frames_.back();
const auto& code = frame.getCode();
const auto pc = frame.getPC();
auto inst = frame.getInstruction();
// If no valid debug handle found then just log pc.
// This is possible when we did not save debug handles
DebugHandle debug_handle = pc;
if (auto handle = frame.getDebugHandle()) {
debug_handle = *handle;
}
// std::cout << "RUNNING " << pc << " " << code.instructions_[pc];
// if (inst.op == OP) {
// std::cout << ", " << code.op_names_[inst.X].name;
// if (!code.op_names_[inst.X].overload_name.empty()) {
// std::cout << "." << code.op_names_[inst.X].overload_name;
// }
// }
// std::cout << std::endl;
// TODO(iliacher): remove the workaround after RecordFunction is in
// Dispatcher
// Check with iliacher if has been done.
// Plus this is not safe as if you throw exception record function will be
// left enabled. That is a TODO
// NOTE: this recordFunction logic takes up ~2-3% of cpu cycles in some
// workflows. do we need it and/or can we opt-out of
// isRecordFunctionEnabled with a macro? if we delete it, things appear to
// work just fine.
bool prev_value = isRecordFunctionEnabled();
if (!prev_value) {
// enable only for the RecordFunction
enableRecordFunction(true);
}
switch (inst.op) {
case OP: {
if (at::hasGlobalCallbacks()) {
if (auto* mobile_debug_info = static_cast<MobileDebugInfo*>(
c10::ThreadLocalDebugInfo::get(
c10::DebugInfoKind::MOBILE_RUNTIME_INFO))) {
mobile_debug_info->setOpIdx(pc);
}
}
if (inst.X < 0 ||
static_cast<size_t>(inst.X) >= code.op_names_.size() ||
static_cast<size_t>(inst.X) >= code.operators_.size()) {
TORCH_CHECK(false, "Can't load op with index: ", inst.X);
}
RECORD_EDGE_SCOPE_WITH_DEBUG_HANDLE_AND_INPUTS(
code.op_names_[inst.X].name, debug_handle, stack);
code.operators_[inst.X](stack);
frame.step();
} break;
case OPN: {
if (inst.X < 0 ||
static_cast<size_t>(inst.X) >= code.op_names_.size() ||
static_cast<size_t>(inst.X) >= code.operators_.size()) {
TORCH_CHECK(false, "Can't load op with index: ", inst.X);
}
stack.emplace_back(inst.N);
RECORD_EDGE_SCOPE_WITH_DEBUG_HANDLE_AND_INPUTS(
code.op_names_[inst.X].name, debug_handle, stack);
code.operators_[inst.X](stack);
frame.step();
} break;
case CALL: {
auto& function = *frame.getCode().functions_.at(inst.X);
callFunction(function, stack);
} break;
case INTERFACE_CALL: {
if (inst.X < 0 ||
static_cast<size_t>(inst.X) >= code.constants_.size()) {
TORCH_CHECK(false, "Can't load constant with index: ", inst.X);
}
if (inst.N == 0 || inst.N > stack.size()) {
TORCH_CHECK(
false,
"INTERFACE_CALL N=",
inst.N,
" not in range [1, ",
stack.size(),
"]");
}
torch::jit::Function& method =
peek(stack, 0, inst.N)
.toObject()
->type()
->getMethod(code.constants_[inst.X].toStringRef());
RECORD_EDGE_SCOPE_WITH_DEBUG_HANDLE_AND_INPUTS(
method.name(), debug_handle, stack);
callFunction(method, stack);
} break;
case LOAD:
stack.emplace_back(reg(inst.X));
frame.step();
break;
case MOVE:
stack.emplace_back(std::move(reg(inst.X)));
frame.step();
break;
case STORE:
reg(inst.X) = pop(stack);
frame.step();
break;
case STOREN:
for (size_t i = inst.N; i > 0; --i) {
reg(inst.X + i - 1) = pop(stack);
}
frame.step();
break;
case DROP:
pop(stack);
frame.step();
break;
case DROPR:
reg(inst.X) = IValue();
frame.step();
break;
case LOADC:
if (inst.X < 0 ||
static_cast<size_t>(inst.X) >= code.constants_.size()) {
TORCH_CHECK(false, "Can't load constant with index: ", inst.X);
}
stack.emplace_back(code.constants_[inst.X]);
frame.step();
break;
case GET_ATTR: {
auto userObj = pop(stack).toObject();
auto value = userObj->getSlot(inst.X);
push(stack, std::move(value));
frame.step();
} break;
case SET_ATTR: {
auto v = pop(stack);
auto userObj = pop(stack).toObject();
// Mobile only: since the number of slots is not known, resize the
// numAttributes before setSlot.
while (static_cast<int>(userObj->type()->numAttributes()) <= inst.X) {
std::stringstream ss;
ss << userObj->type()->numAttributes();
userObj->type()->addAttribute(ss.str(), c10::NoneType::get());
}
userObj->setSlot(inst.X, std::move(v));
frame.step();
} break;
case JF:
frame.jump(pop(stack).toBool() ? 1 : inst.X);
break;
case JMP:
frame.jump(inst.X);
break;
case LOOP: {
// stack: iteration_count, max_iter, cond, loop_carried_deps...
auto sframe = stack.end() - (inst.N + 1);
int64_t trip_count = sframe[0].toInt();
int64_t max_trip_count = sframe[1].toInt();
bool cond = sframe[2].toBool();
if (trip_count < max_trip_count && cond) {
sframe[2] = trip_count;
sframe[0] = trip_count + 1;
frame.step();
} else {
size_t n_loop_carried = inst.N - 2;
for (const auto i : c10::irange(n_loop_carried)) {
sframe[i] = std::move(sframe[i + 3]);
}
drop(stack, 3); // iteration_count, max_iter, cond
frame.jump(inst.X);
}
} break;
case RET:
leaveFrame();
if (!frames_.empty()) {
continue;
}
return false;
case LIST_CONSTRUCT: {
listConstruct(stack, *code.types_.at(inst.X), inst.N);
frame.step();
} break;
case LIST_UNPACK: {
listUnpack(stack, inst.X);
frame.step();
} break;
case TUPLE_CONSTRUCT: {
tupleConstruct(stack, inst.X);
frame.step();
} break;
case TUPLE_SLICE: {
tupleSlice(stack, inst.X, inst.X + inst.N);
frame.step();
} break;
case TUPLE_INDEX: {
tupleIndex(stack);
frame.step();
} break;
case RAISE_EXCEPTION: {
raiseExceptionWithMessage(stack);
frame.step();
} break;
case __IS__: {
is(stack);
frame.step();
} break;
case UN_INITIALIZED: {
unInitialized(stack);
frame.step();
} break;
case __ISNOT__: {
isNot(stack);
frame.step();
} break;
case FORMAT: {
format(stack, inst.X);
frame.step();
} break;
case DEVICE: {
device(stack);
frame.step();
} break;
case DTYPE: {
dtype(stack);
frame.step();
} break;
case DIM: {
dim(stack);
frame.step();
} break;
case __NOT__: {
_not(stack);
frame.step();
} break;
case DICT_INDEX: {
dictIndex(stack);
frame.step();
} break;
case TO_LIST: {
toList(stack);
frame.step();
} break;
case NUM_TO_TENSOR: {
numToTensorScalar(stack);
frame.step();
} break;
case IS_CUDA: {
isCuda(stack);
frame.step();
} break;
case DICT_CONSTRUCT: {
dictConstruct(stack, *code.types_.at(inst.X), inst.N);
frame.step();
} break;
case NAMED_TUPLE_CONSTRUCT: {
namedTupleConstruct(stack, code.types_.at(inst.X), inst.N);
frame.step();
} break;
case CREATE_OBJECT: {
auto type = code.types_.at(inst.X)->expect<c10::ClassType>();
createObject(stack, type);
frame.step();
} break;
case ISINSTANCE: {
at::ArrayRef<TypePtr> types(&code.types_.at(inst.X), inst.N);
isinstance(stack, types);
frame.step();
} break;
case WARN: {
drop(stack, 1);
// Note: Please don't move the pop(stack) code below into the
// TORCH_WARN macro since TORCH_WARN fails to evaluate its arguments
// when STRIP_ERROR_MESSAGES is defined (which happens for production
// mobile builds). This will cause the stack to be in an inconsistent
// state. It has previously resulted in a SEV (S22350).
TORCH_WARN(stack.back().toStringRef());
stack.pop_back();
frame.step();
} break;
default:
TORCH_CHECK(false, toString(inst.op), " is invalid.");
}
if (!prev_value) {
enableRecordFunction(false);
}
// This exception must be caught first as it derived from c10::Error
} catch (c10::BackendRuntimeException& e) {
saveExceptionDebugHandles();
TORCH_RETHROW(e);
} catch (c10::Error& error) {
// Reason for catching and rethrowing the error is so that we can
// set the exception pc that is queried later
saveExceptionDebugHandles();
TORCH_RETHROW(error);
} catch (...) {
saveExceptionDebugHandles();
throw;
}
// for (auto val : stack) {
// if (val.isTensor()) {
// std::cout << val.toTensor().sizes() << std::endl;
// } else {
// std::cout << val << std::endl;
// }
// }
}
return false;
}
IValue& InterpreterState::reg(size_t reg) {
TORCH_CHECK(
reg > 0 && reg <= registers_.size(), "Invalid register index: ", reg);
return *(registers_.end() - reg);
}
} // namespace mobile
} // namespace torch::jit