-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
interpreter.cc
811 lines (708 loc) · 26 KB
/
interpreter.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/*!
* \file src/tvm/relay/interpreter.cc
* \brief An interpreter for the Relay IR.
*/
#include <tvm/packed_func_ext.h>
#include <tvm/runtime/device_api.h>
#include <tvm/relay/expr_functor.h>
#include <tvm/relay/pattern_functor.h>
#include <tvm/relay/interpreter.h>
#include <tvm/relay/transform.h>
#include <tvm/relay/analysis.h>
#include <tvm/relay/attrs/debug.h>
#include <tvm/relay/feature.h>
#include "compile_engine.h"
namespace tvm {
namespace relay {
using namespace runtime;
inline const PackedFunc& GetPackedFunc(const std::string& name) {
const PackedFunc* pf = tvm::runtime::Registry::Get(name);
CHECK(pf != nullptr) << "Cannot find function " << name << " in registry";
return *pf;
}
/* Object Implementation */
Closure ClosureNode::make(tvm::Map<Var, ObjectRef> env, Function func) {
ObjectPtr<ClosureNode> n = make_object<ClosureNode>();
n->env = std::move(env);
n->func = std::move(func);
return Closure(n);
}
TVM_REGISTER_GLOBAL("relay._make.Closure")
.set_body_typed(ClosureNode::make);
TVM_STATIC_IR_FUNCTOR(NodePrinter, vtable)
.set_dispatch<ClosureNode>([](const ObjectRef& ref, NodePrinter* p) {
auto* node = static_cast<const ClosureNode*>(ref.get());
p->stream << "ClosureNode(" << node->func << ", " << node->env << ")";
});
// TODO(@jroesch): this doesn't support mutual letrec
/* Object Implementation */
RecClosure RecClosureNode::make(Closure clos, Var bind) {
ObjectPtr<RecClosureNode> n = make_object<RecClosureNode>();
n->clos = std::move(clos);
n->bind = std::move(bind);
return RecClosure(n);
}
TVM_REGISTER_GLOBAL("relay._make.RecClosure")
.set_body_typed(RecClosureNode::make);
TVM_STATIC_IR_FUNCTOR(NodePrinter, vtable)
.set_dispatch<RecClosureNode>([](const ObjectRef& ref, NodePrinter* p) {
auto* node = static_cast<const RecClosureNode*>(ref.get());
p->stream << "RecClosureNode(" << node->clos << ")";
});
TupleValue TupleValueNode::make(tvm::Array<ObjectRef> value) {
ObjectPtr<TupleValueNode> n = make_object<TupleValueNode>();
n->fields = value;
return TupleValue(n);
}
TVM_REGISTER_GLOBAL("relay._make.TupleValue")
.set_body_typed(TupleValueNode::make);
TVM_STATIC_IR_FUNCTOR(NodePrinter, vtable)
.set_dispatch<TupleValueNode>([](const ObjectRef& ref, NodePrinter* p) {
auto* node = static_cast<const TupleValueNode*>(ref.get());
p->stream << "TupleValueNode(" << node->fields << ")";
});
RefValue RefValueNode::make(ObjectRef value) {
ObjectPtr<RefValueNode> n = make_object<RefValueNode>();
n->value = value;
return RefValue(n);
}
TVM_REGISTER_GLOBAL("relay._make.RefValue")
.set_body_typed(RefValueNode::make);
TVM_REGISTER_NODE_TYPE(RefValueNode);
TVM_STATIC_IR_FUNCTOR(NodePrinter, vtable)
.set_dispatch<RefValueNode>([](const ObjectRef& ref, NodePrinter* p) {
auto* node = static_cast<const RefValueNode*>(ref.get());
p->stream << "RefValueNode(" << node->value << ")";
});
ConstructorValue ConstructorValueNode::make(int32_t tag,
tvm::Array<ObjectRef> fields,
Constructor constructor) {
ObjectPtr<ConstructorValueNode> n = make_object<ConstructorValueNode>();
n->tag = tag;
n->fields = fields;
n->constructor = constructor;
return ConstructorValue(n);
}
TVM_REGISTER_GLOBAL("relay._make.ConstructorValue")
.set_body_typed(ConstructorValueNode::make);
TVM_REGISTER_NODE_TYPE(ConstructorValueNode);
TVM_STATIC_IR_FUNCTOR(NodePrinter, vtable)
.set_dispatch<ConstructorValueNode>([](const ObjectRef& ref, NodePrinter* p) {
auto* node = static_cast<const ConstructorValueNode*>(ref.get());
p->stream << "ConstructorValueNode(" << node->tag << ","
<< node->fields << ")";
});
/*!
* \brief A stack frame in the Relay interpreter.
*
* Contains a mapping from relay::Var to relay::ObjectRef.
*/
struct Frame {
/*! \brief The set of local variables and arguments for the frame. */
tvm::Map<Var, ObjectRef> locals;
explicit Frame(tvm::Map<Var, ObjectRef> locals) : locals(locals) {}
};
/*!
* \brief The call stack in the Relay interpreter.
*
* Contains a stack of frames; each corresponding to
* a function call.
*/
struct Stack {
/*! \brief The stack frames. */
std::vector<Frame> frames;
Stack() : frames() { frames.push_back(Frame({})); }
Frame& current_frame() { return frames.back(); }
ObjectRef Lookup(const Var& local) {
for (auto frame = frames.rbegin(); frame != frames.rend(); frame++) {
auto elem = frame->locals.find(local);
if (elem != frame->locals.end()) {
return (*elem).second;
}
}
LOG(FATAL) << "could not find variable binding for " << local
<< "address= " << local.operator->();
return ObjectRef();
}
/*!
* A wrapper around Frame to add RAII semantics to pushing and popping
* stack frames.
*/
struct LocalFrame {
Stack& st;
explicit LocalFrame(Stack& st, const Frame& fr) : st(st) {
st.frames.push_back(fr);
}
~LocalFrame() { st.frames.pop_back(); }
};
};
/*! \brief A representation of the interpreter state which can be passed back to Python. */
class InterpreterState;
/*! \brief A container capturing the state of the interpreter. */
class InterpreterStateNode : public Object {
public:
using Frame = tvm::Map<Var, ObjectRef>;
using Stack = tvm::Array<Frame>;
/*! \brief The current expression under evaluation. */
Expr current_expr;
/*! \brief The call stack of the interpreter. */
Stack stack;
void VisitAttrs(tvm::AttrVisitor* v) {
v->Visit("current_expr", ¤t_expr);
v->Visit("stack", &stack);
}
static InterpreterState make(Expr current_expr, Stack stack);
static constexpr const char* _type_key = "relay.InterpreterState";
TVM_DECLARE_FINAL_OBJECT_INFO(InterpreterStateNode, Object);
};
class InterpreterState : public ObjectRef {
public:
TVM_DEFINE_OBJECT_REF_METHODS(InterpreterState, ObjectRef, InterpreterStateNode);
};
InterpreterState InterpreterStateNode::make(Expr current_expr, Stack stack) {
ObjectPtr<InterpreterStateNode> n = make_object<InterpreterStateNode>();
n->current_expr = std::move(current_expr);
n->stack = std::move(stack);
return InterpreterState(n);
}
// NOTE: the current interpreter assumes A-normal form.
// which is better for execution.
//
// It will run duplicated computations when taking program that
// contains DAG in dataflow-form.
//
// Conversion to ANF is recommended before running the interpretation.
class Interpreter :
public ExprFunctor<ObjectRef(const Expr& n)>,
PatternFunctor<bool(const Pattern& p, const ObjectRef& v)> {
public:
Interpreter(IRModule mod, DLContext context, Target target)
: mod_(mod),
context_(context),
target_(target),
debug_op_(Op::Get("debug")),
shape_of_op_(Op::Get("shape_of")) {
engine_ = CompileEngine::Global();
}
template <typename T>
T WithFrame(const Frame& fr, const std::function<T()>& f) {
Stack::LocalFrame lf(stack_, fr);
return f();
}
void extend(const Var& id, ObjectRef v) {
stack_.current_frame().locals.Set(id, v);
}
ObjectRef Lookup(const Var& local) {
return stack_.Lookup(local);
}
ObjectRef Eval(const Expr& expr) {
return VisitExpr(expr);
}
ObjectRef VisitExpr(const Expr& expr) final {
auto ret = ExprFunctor<ObjectRef(const Expr& n)>::VisitExpr(expr);
return ret;
}
ObjectRef VisitExpr_(const VarNode* var_node) final {
return Lookup(GetRef<Var>(var_node));
}
ObjectRef VisitExpr_(const GlobalVarNode* op) final {
return Eval(mod_->Lookup(GetRef<GlobalVar>(op)));
}
ObjectRef VisitExpr_(const OpNode* id) override {
// TODO(@jroesch): Eta-expand and return in this case.
LOG(FATAL) << "internal error, need to wrap intrinsic into call synthetic call node "
<< "in "
<< "this case, eta expand";
return ObjectRef();
}
ObjectRef VisitExpr_(const ConstantNode* op) final {
return op->data.CopyTo(context_);
}
ObjectRef VisitExpr_(const TupleNode* op) final {
std::vector<ObjectRef> values;
for (const auto& field : op->fields) {
ObjectRef field_value = Eval(field);
values.push_back(field_value);
}
return TupleValueNode::make(values);
}
ObjectRef MakeClosure(const Function& func, Var letrec_name = Var()) {
tvm::Map<Var, ObjectRef> captured_mod;
Array<Var> free_vars = FreeVars(func);
for (const auto& var : free_vars) {
// Evaluate the free var (which could be a function call) if it hasn't
// shown up in a letting binding that has invoked the function.
if (letrec_name.defined() && letrec_name == var) {
continue;
}
captured_mod.Set(var, Eval(var));
}
// We must use mutation here to build a self referential closure.
auto closure = ClosureNode::make(captured_mod, func);
if (letrec_name.defined()) {
return RecClosureNode::make(closure, letrec_name);
}
return std::move(closure);
}
ObjectRef VisitExpr_(const FunctionNode* func_node) final {
auto func = GetRef<Function>(func_node);
return MakeClosure(func);
}
Array<Shape> ComputeDynamicShape(const Function& func,
const Array<ObjectRef>& args) {
auto key = CCacheKeyNode::make(func, Target::Create("llvm"));
auto cfunc = engine_->LowerShapeFunc(key);
size_t arity = cfunc->inputs.size() + cfunc->outputs.size();
std::vector<TVMValue> values(arity);
std::vector<int> codes(arity);
TVMArgsSetter setter(values.data(), codes.data());
std::vector<NDArray> inputs(cfunc->inputs.size());
std::vector<NDArray> outputs(cfunc->outputs.size());
DLContext cpu_ctx;
cpu_ctx.device_type = kDLCPU;
cpu_ctx.device_id = 0;
auto fset_input = [&](size_t i, ObjectRef val, bool need_shape) {
auto nd_array = Downcast<NDArray>(val);
if (need_shape) {
int64_t ndim = nd_array.Shape().size();
NDArray shape_arr;
if (ndim == 0) {
shape_arr = NDArray::Empty({}, DataType::Int(64), cpu_ctx);
} else {
shape_arr = NDArray::Empty({ndim}, DataType::Int(64), cpu_ctx);
int64_t* data = reinterpret_cast<int64_t*>(shape_arr->data);
for (auto j = 0; j < ndim; ++j) {
data[j] = nd_array.Shape()[j];
}
}
inputs[i] = shape_arr;
setter(i, shape_arr);
} else {
auto arr = nd_array.CopyTo(cpu_ctx);
inputs[i] = arr;
setter(i, arr);
}
};
size_t arg_counter = 0;
for (size_t i = 0; i < args.size(); ++i) {
auto arg = args[i];
auto param = func->params[i];
int state = cfunc->shape_func_param_states[i]->value;
if (arg->IsInstance<runtime::NDArray::ContainerType>()) {
if (state & kNeedInputData) {
fset_input(arg_counter++, arg, false);
}
if (state & kNeedInputShape) {
fset_input(arg_counter++, arg, true);
}
} else {
const TupleValueNode* tuple = arg.as<TupleValueNode>();
CHECK(tuple != nullptr);
if (state & kNeedInputData) {
for (size_t i = 0; i < tuple->fields.size(); ++i) {
fset_input(arg_counter++, tuple->fields[i], false);
}
}
if (state & kNeedInputShape) {
for (size_t i = 0; i < tuple->fields.size(); ++i) {
fset_input(arg_counter++, tuple->fields[i], true);
}
}
}
}
CHECK_EQ(arg_counter, cfunc->inputs.size())
<< "Shape function input sizes mismatch";
auto fset_shape_output = [&](size_t i, Type val_type) {
// TODO(@icemelon): allow recursive tuple
const TensorTypeNode* rtype = val_type.as<TensorTypeNode>();
CHECK(rtype != nullptr);
int64_t ndim = rtype->shape.size();
auto arr = NDArray::Empty({ndim}, DataType::Int(64), cpu_ctx);
outputs[i] = arr;
setter(arg_counter + i, arr);
};
auto ret_type = func->body->checked_type();
size_t out_cnt = 0;
if (auto rtype = ret_type.as<TupleTypeNode>()) {
out_cnt = rtype->fields.size();
for (size_t i = 0; i < out_cnt; ++i) {
fset_shape_output(i, rtype->fields[i]);
}
} else {
out_cnt = 1;
auto tt = Downcast<TensorType>(ret_type);
fset_shape_output(0, tt);
}
CHECK_EQ(cfunc->outputs.size(), out_cnt)
<< "Shape function output sizes mismatch";
PackedFunc shape_func;
TVMRetValue rv;
if (const auto* f = runtime::Registry::Get("relay.backend.build")) {
tvm::runtime::Module m = (*f)(cfunc->funcs, cfunc->target);
shape_func = m.GetFunction(cfunc->func_name);
} else {
LOG(FATAL) << "relay.backend.build is not registered";
}
shape_func.CallPacked(TVMArgs(values.data(), codes.data(), arity), &rv);
// Get output shapes
Array<Shape> out_shapes;
for (auto out_tensor : outputs) {
int64_t* shape_data = reinterpret_cast<int64_t*>(out_tensor->data);
Shape out_shape;
for (int i = 0; i < out_tensor->shape[0]; ++i) {
out_shape.push_back(tvm::Integer(shape_data[i]));
}
out_shapes.push_back(out_shape);
}
return out_shapes;
}
ObjectRef InvokePrimitiveOp(const Function& func,
const Array<ObjectRef>& args) {
const auto* call_node = func->body.as<CallNode>();
if (call_node && call_node->op == debug_op_) {
auto dattrs = call_node->attrs.as<DebugAttrs>();
auto interp_state = this->get_state(call_node->args[0]);
if (dattrs->debug_func.defined()) {
dattrs->debug_func(interp_state);
} else {
RELAY_DEBUG_INTERP(interp_state);
}
return args[0];
}
// Marshal the arguments.
// Handle tuple input/output by flattening them.
size_t arg_len = 0;
for (size_t i = 0; i < args.size(); ++i) {
if (args[i]->IsInstance<NDArray::ContainerType>()) {
++arg_len;
} else {
const auto* tvalue = args[i].as<TupleValueNode>();
arg_len += tvalue->fields.size();
}
}
size_t num_inputs = arg_len;
if (const auto* tuple_type = func->body->checked_type().as<TupleTypeNode>()) {
arg_len += tuple_type->fields.size();
} else {
CHECK(func->body->checked_type().as<TensorTypeNode>())
<< func->body->checked_type();
arg_len += 1;
}
std::vector<TVMValue> values(arg_len);
std::vector<int> codes(arg_len);
TVMArgsSetter setter(values.data(), codes.data());
auto fset_input = [&](size_t i, ObjectRef val) {
const auto nd_array = Downcast<NDArray>(val);
setter(i, nd_array);
DLContext arg_ctx = nd_array->ctx;
CHECK(arg_ctx.device_type == context_.device_type &&
arg_ctx.device_id == context_.device_id)
<< "Interpreter expect context to be "
<< context_ << ", but get " << arg_ctx;
};
int arg_counter = 0;
for (ObjectRef arg : args) {
if (arg->IsInstance<NDArray::ContainerType>()) {
fset_input(arg_counter++, arg);
} else {
const TupleValueNode* tuple = arg.as<TupleValueNode>();
CHECK(tuple != nullptr);
for (size_t i = 0; i < tuple->fields.size(); ++i) {
fset_input(arg_counter++, tuple->fields[i]);
}
}
}
// TVM's calling convention is that the final argument is the output
// buffer. To preserve the illusion of being a functional language
// we need to allocate space for the output buffer based on the
// return type.
auto fset_output = [&](size_t i, Type val_type) {
const TensorTypeNode* rtype = val_type.as<TensorTypeNode>();
CHECK(rtype != nullptr);
// Allocate output tensor.
std::vector<int64_t> shape;
for (auto dim : rtype->shape) {
const auto* ivalue = as_const_int(dim);
CHECK(ivalue) << "expected concrete dimensions";
shape.push_back(ivalue[0]);
}
DLDataType dtype = rtype->dtype;
NDArray nd_array = NDArray::Empty(shape, dtype, context_);
setter(num_inputs + i, nd_array);
return nd_array;
};
Array<Shape> out_shapes;
auto ret_type = func->body->checked_type();
bool is_dyn = IsDynamic(func->checked_type());
if (call_node->op == shape_of_op_) {
// The output shape of shape_of must be static since Relay doesn't support
// dynamic rank tensors.
is_dyn = false;
}
if (is_dyn) {
CHECK(func->IsPrimitive());
out_shapes = ComputeDynamicShape(func, args);
}
PackedFunc packed_func = engine_->JIT(CCacheKeyNode::make(func, target_));
TVMRetValue rv;
if (const TupleTypeNode* rtype = func->body->checked_type().as<TupleTypeNode>()) {
CHECK(!is_dyn || out_shapes.size() == rtype->fields.size());
Array<ObjectRef> fields;
for (size_t i = 0; i < rtype->fields.size(); ++i) {
if (is_dyn) {
auto sh = out_shapes[i];
auto tt = Downcast<TensorType>(rtype->fields[i]);
fields.push_back(fset_output(i, TensorTypeNode::make(sh, tt->dtype)));
} else {
fields.push_back(fset_output(i, rtype->fields[i]));
}
}
packed_func.CallPacked(TVMArgs(values.data(), codes.data(), arg_len), &rv);
return TupleValueNode::make(fields);
} else {
ObjectRef out_tensor;
if (is_dyn) {
CHECK_EQ(out_shapes.size(), 1);
auto sh = out_shapes[0];
auto tt = Downcast<TensorType>(ret_type);
out_tensor = fset_output(0, TensorTypeNode::make(sh, tt->dtype));
} else {
out_tensor = fset_output(0, ret_type);
}
packed_func.CallPacked(TVMArgs(values.data(), codes.data(), arg_len), &rv);
return out_tensor;
}
}
// Invoke the closure
ObjectRef Invoke(const Closure& closure,
const tvm::Array<ObjectRef>& args,
const Var& bind = Var()) {
// Get a reference to the function inside the closure.
if (closure->func->IsPrimitive()) {
return InvokePrimitiveOp(closure->func, args);
}
auto func = closure->func;
// Allocate a frame with the parameters and free variables.
tvm::Map<Var, ObjectRef> locals;
CHECK_EQ(func->params.size(), args.size());
for (size_t i = 0; i < func->params.size(); i++) {
CHECK_EQ(locals.count(func->params[i]), 0);
locals.Set(func->params[i], args[i]);
}
// Add the var to value mappings from the Closure's environment.
for (auto it = closure->env.begin(); it != closure->env.end(); ++it) {
CHECK_EQ(locals.count((*it).first), 0);
locals.Set((*it).first, (*it).second);
}
if (bind.defined()) {
locals.Set(bind, RecClosureNode::make(closure, bind));
}
return WithFrame<ObjectRef>(Frame(locals), [&]() { return Eval(func->body); });
}
ObjectRef VisitExpr_(const CallNode* call) final {
tvm::Array<ObjectRef> args;
for (auto arg : call->args) {
args.push_back(Eval(arg));
}
// We should not find operators after running fusion,
// and operator lowering.
//
// We have some functions cotaining chunks of operators
// which will be loaded into operator map.
if (const auto* op_node = call->op.as<OpNode>()) {
LOG(FATAL) << "found " << op_node->name
<< "; operators should be removed by future passes; try "
"fusing and lowering";
}
if (auto con = call->op.as<ConstructorNode>()) {
return ConstructorValueNode::make(con->tag, args, GetRef<Constructor>(con));
}
// Now we just evaluate and expect to find a closure.
ObjectRef fn_val = Eval(call->op);
if (const ClosureNode* closure_node = fn_val.as<ClosureNode>()) {
auto closure = GetRef<Closure>(closure_node);
return this->Invoke(closure, args);
} else if (const RecClosureNode* closure_node = fn_val.as<RecClosureNode>()) {
return this->Invoke(closure_node->clos, args, closure_node->bind);
} else {
LOG(FATAL) << "internal error: type error, expected function value in the call "
<< "position";
return ObjectRef();
}
}
ObjectRef VisitExpr_(const LetNode* let) final {
if (auto func = let->value.as<FunctionNode>()) {
auto clo = MakeClosure(GetRef<Function>(func), let->var);
this->extend(let->var, clo);
} else {
auto value = Eval(let->value);
this->extend(let->var, value);
}
return Eval(let->body);
}
ObjectRef VisitExpr_(const TupleGetItemNode* op) final {
ObjectRef val = Eval(op->tuple);
auto product_node = val.as<TupleValueNode>();
CHECK(product_node)
<< "interal error: when evaluating TupleGetItem expected a tuple value";
CHECK_LT(static_cast<size_t>(op->index), product_node->fields.size())
<< "internal error: index out of bounds";
return product_node->fields[op->index];
}
ObjectRef VisitExpr_(const IfNode* op) final {
ObjectRef v = Eval(op->cond);
if (v->IsInstance<NDArray::ContainerType>()) {
auto nd_array = Downcast<NDArray>(v);
DLContext cpu_ctx;
cpu_ctx.device_type = kDLCPU;
cpu_ctx.device_id = 0;
NDArray cpu_array = nd_array.CopyTo(cpu_ctx);
CHECK_EQ(DataType(cpu_array->dtype), DataType::Bool());
// TODO(@jroesch, @MK): Refactor code into helper from DCE.
if (reinterpret_cast<uint8_t*>(cpu_array->data)[0]) {
return Eval(op->true_branch);
} else {
return Eval(op->false_branch);
}
} else {
LOG(FATAL) << "type error, type system should have caught this";
return ObjectRef();
}
}
ObjectRef VisitExpr_(const RefWriteNode* op) final {
ObjectRef r = Eval(op->ref);
if (const RefValueNode* rv = r.as<RefValueNode>()) {
rv->value = Eval(op->value);
return TupleValueNode::make({});
} else {
LOG(FATAL) << "type error, type system should have caught this";
return ObjectRef();
}
}
ObjectRef VisitExpr_(const RefCreateNode* op) final {
return RefValueNode::make(Eval(op->value));
}
ObjectRef VisitExpr_(const RefReadNode* op) final {
ObjectRef r = Eval(op->ref);
if (const RefValueNode* rv = r.as<RefValueNode>()) {
return rv->value;
} else {
LOG(FATAL) << "type error, type system should have caught this";
return ObjectRef();
}
}
ObjectRef VisitExpr_(const MatchNode* op) final {
ObjectRef v = Eval(op->data);
for (const Clause& c : op->clauses) {
if (VisitPattern(c->lhs, v)) {
return VisitExpr(c->rhs);
}
}
LOG(FATAL) << "did not find any match";
return ObjectRef();
}
bool VisitPattern_(const PatternConstructorNode* op, const ObjectRef& v) final {
const ConstructorValueNode* cvn = v.as<ConstructorValueNode>();
CHECK(cvn) << "need to be a constructor for match";
CHECK_NE(op->constructor->tag, -1);
CHECK_NE(cvn->tag, -1);
if (op->constructor->tag == cvn->tag) {
CHECK_EQ(op->patterns.size(), cvn->fields.size());
for (size_t i = 0; i < op->patterns.size(); ++i) {
if (!VisitPattern(op->patterns[i], cvn->fields[i])) {
return false;
}
}
return true;
}
return false;
}
bool VisitPattern_(const PatternTupleNode* op, const ObjectRef& v) final {
const TupleValueNode* tvn = v.as<TupleValueNode>();
CHECK(tvn) << "need to be a tuple for match";
CHECK_EQ(op->patterns.size(), tvn->fields.size());
for (size_t i = 0; i < op->patterns.size(); ++i) {
if (!VisitPattern(op->patterns[i], tvn->fields[i])) {
return false;
}
}
return true;
}
bool VisitPattern_(const PatternWildcardNode* op, const ObjectRef& v) final {
return true;
}
bool VisitPattern_(const PatternVarNode* op, const ObjectRef& v) final {
extend(op->var, v);
return true;
}
InterpreterState get_state(Expr e = Expr()) const {
InterpreterStateNode::Stack stack;
for (auto fr : this->stack_.frames) {
InterpreterStateNode::Frame frame = fr.locals;
stack.push_back(frame);
}
auto state = InterpreterStateNode::make(e, stack);
return state;
}
private:
// Module
IRModule mod_;
// For simplicity we only run the interpreter on a single context.
// Context to run the interpreter on.
DLContext context_;
// Target parameter being used by the interpreter.
Target target_;
// Object stack.
Stack stack_;
// Backend compile engine.
CompileEngine engine_;
// Cache ops that need to be frequently used later to reduce lookup overhead.
const Op& debug_op_;
const Op& shape_of_op_;
};
TypedPackedFunc<ObjectRef(Expr)>
CreateInterpreter(
IRModule mod,
DLContext context,
Target target) {
if (mod.defined()) {
// eta expand to support constructors in argument position
transform::Sequential seq({
transform::EtaExpand(
/* expand_constructor */ true, /* expand_global_var */ false)});
transform::PassContext pass_ctx = transform::PassContext::Current();
tvm::With<transform::PassContext> ctx(pass_ctx);
mod = seq(mod);
}
auto intrp = std::make_shared<Interpreter>(mod, context, target);
auto packed = [intrp](Expr expr) {
auto f = DetectFeature(expr);
CHECK(f.is_subset_of(FeatureSet::All() - fGraph));
return intrp->Eval(expr);
};
return TypedPackedFunc<ObjectRef(Expr)>(packed);
}
TVM_REGISTER_GLOBAL("relay.backend.CreateInterpreter")
.set_body_typed(CreateInterpreter);
TVM_REGISTER_NODE_TYPE(ClosureNode);
TVM_REGISTER_NODE_TYPE(TupleValueNode);
} // namespace relay
} // namespace tvm