forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaot_executor_codegen.cc
More file actions
797 lines (702 loc) · 29.7 KB
/
aot_executor_codegen.cc
File metadata and controls
797 lines (702 loc) · 29.7 KB
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
/*
* 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 relay/backend/graph_codegen.cc
* \brief Graph runtime codegen
*/
#include <tvm/ir/module.h>
#include <tvm/relay/expr_functor.h>
#include <tvm/runtime/device_api.h>
#include <tvm/runtime/object.h>
#include <tvm/tir/analysis.h>
#include <tvm/tir/builtin.h>
#include <tvm/tir/expr.h>
#include <tvm/tir/function.h>
#include <tvm/tir/stmt.h>
#include <tvm/tir/transform.h>
#include <algorithm>
#include <list>
#include <string>
#include <vector>
#include "te_compiler.h"
#include "utils.h"
namespace tvm {
namespace relay {
namespace backend {
using IntegerArray = Array<Integer>;
using StorageMap =
std::unordered_map<Expr, StorageInfo, runtime::ObjectPtrHash, runtime::ObjectPtrEqual>;
/**
* This is an on demand allocator for AOT. A new temporary
* (storage allocator identifier) is allocated for each operation.
*/
class AOTOnDemandAllocator : public ExprVisitor {
public:
// run the visitor on a function.
void Run(const Function& func) {
node_device_map_ = CollectDeviceInfo(func);
for (Expr param : func->params) {
CreateStorage(param.operator->());
}
GetStorage(func->body);
}
std::vector<int> GetReturnIds() const { return return_ids_; }
StorageMap GetStorageMap() const { return storage_device_map_; }
void VisitExpr_(const ConstantNode* op) final {
CreateStorage(op);
AssignReturnSid(GetRef<Expr>(op));
}
void VisitExpr_(const CallNode* op) final {
// create token for the call node.
CreateStorage(op);
for (Expr arg : op->args) {
GetStorage(arg);
}
AssignReturnSid(GetRef<Expr>(op));
}
void VisitExpr_(const VarNode* op) final {
ExprVisitor::VisitExpr_(op);
AssignReturnSid(GetRef<Expr>(op));
}
void VisitExpr_(const FunctionNode* op) final {
// do not recurse into sub function.
}
void VisitExpr_(const GlobalVarNode* op) final {
// Do nothing.
}
void VisitExpr_(const OpNode* op) final {
// Do nothing.
}
void VisitExpr_(const TupleNode* op) final {
std::vector<int64_t> storage_ids;
std::vector<DLDeviceType> device_types;
std::vector<int64_t> storage_sizes_in_bytes;
Expr expr = GetRef<Expr>(op);
for (Expr field : op->fields) {
auto sid = GetStorage(field);
storage_ids.insert(storage_ids.end(), sid->storage_ids.begin(), sid->storage_ids.end());
device_types.insert(device_types.end(), sid->device_types.begin(), sid->device_types.end());
storage_sizes_in_bytes.insert(storage_sizes_in_bytes.end(),
sid->storage_sizes_in_bytes.begin(),
sid->storage_sizes_in_bytes.end());
}
storage_device_map_[expr] = StorageInfo(storage_ids, device_types, storage_sizes_in_bytes);
AssignReturnSid(expr);
}
void VisitExpr_(const TupleGetItemNode* op) final {
Expr expr = GetRef<Expr>(op);
auto sids = GetStorage(op->tuple);
ICHECK_LT(static_cast<size_t>(op->index), sids->storage_ids.size());
storage_device_map_[expr] =
StorageInfo({sids->storage_ids[op->index]}, {sids->device_types[op->index]},
{sids->storage_sizes_in_bytes[op->index]});
AssignReturnSid(expr);
}
void VisitExpr_(const IfNode* op) final { LOG(FATAL) << "if is not supported."; }
void VisitExpr_(const LetNode* op) final { LOG(FATAL) << "let is not supported."; }
private:
void AssignReturnSid(Expr e) {
if (storage_device_map_.find(e) != storage_device_map_.end()) {
StorageInfo& sinfo = storage_device_map_[e];
return_ids_.clear();
for (auto sid : sinfo->storage_ids) {
return_ids_.push_back(sid);
}
}
}
/*!
* \brief ceil(size/word_size) to get number of words.
* \param size The original size.
* \param word_size The element size.
*/
static size_t DivRoundUp(size_t size, size_t word_size) {
return (size + word_size - 1) / word_size;
}
/*!
* \brief Get the memory requirement.
* \param prototype The prototype token.
* \return The required memory size.
*/
size_t GetMemorySizeBytes(const TensorTypeNode* ttype) {
ICHECK(ttype != nullptr);
size_t size = 1;
for (IndexExpr dim : ttype->shape) {
const int64_t* pval = tir::as_const_int(dim);
ICHECK(pval != nullptr) << "Cannot allocate memory symbolic tensor shape " << ttype->shape;
ICHECK_GE(*pval, 0) << "Cannot allocate memory for tensor with negative shape" << *pval;
size *= static_cast<size_t>(pval[0]);
}
size *= DivRoundUp(ttype->dtype.bits() * ttype->dtype.lanes(), 8);
return size;
}
/*!
* \brief Get the necessary storage for the expression.
* \param expr The expression.
* \return The corresponding token.
*/
StorageInfo GetStorage(const Expr& expr) {
this->VisitExpr(expr);
auto it = storage_device_map_.find(expr);
ICHECK(it != storage_device_map_.end());
return it->second;
}
/*!
* \brief Create storage for the expression.
* \param expr The expression.
*/
void CreateStorage(const ExprNode* op) {
std::vector<int64_t> storage_ids;
std::vector<DLDeviceType> device_types;
std::vector<int64_t> storage_sizes_in_bytes;
Expr expr = GetRef<Expr>(op);
int device_type_int =
node_device_map_.count(GetRef<Expr>(op)) ? node_device_map_[expr]->value : 0;
if (const auto* tuple_type = op->checked_type().as<TupleTypeNode>()) {
for (Type t : tuple_type->fields) {
const auto* ttype = t.as<TensorTypeNode>();
ICHECK(ttype);
storage_ids.push_back(next_available_sid_++);
storage_sizes_in_bytes.push_back(GetMemorySizeBytes(ttype));
device_types.push_back(DLDeviceType(device_type_int));
}
} else {
const auto* ttype = op->checked_type().as<TensorTypeNode>();
ICHECK(ttype);
storage_ids.push_back(next_available_sid_++);
storage_sizes_in_bytes.push_back(GetMemorySizeBytes(ttype));
device_types.push_back(DLDeviceType(device_type_int));
}
storage_device_map_[expr] = StorageInfo(storage_ids, device_types, storage_sizes_in_bytes);
}
/*! \brief mapping of expression -> storageInfo*/
StorageMap storage_device_map_;
/*! \brief mapping of expression -> device type*/
Map<Expr, Integer> node_device_map_;
/*! \brief current id of the temporary allocated*/
int next_available_sid_{0};
/*! \brief the set of intermediate tensors that are return variables */
std::vector<int> return_ids_;
};
/*! \brief Code generator for AOT executor */
class AOTExecutorCodegen : public ExprVisitor {
protected:
/*!
* \brief Utility function to allocate a DLTensor or TVMValue
* \param type the type of allocation
* \param num the number of variable to allocate on the stack
* \return PrimExpr representing the allocated object
*/
PrimExpr StackAlloca(std::string type, size_t num) {
Array<PrimExpr> args = {tir::StringImm(type), ConstInt32(num)};
return tir::Call(DataType::Handle(), tir::builtin::tvm_stack_alloca(), args);
}
/*!
* \brief Utility function to convert a concrete integer to a PrimExpr.
* \param num the number to convert
* \return PrimExpr representing num
*/
inline PrimExpr ConstInt32(size_t num) {
ICHECK_LE(num, std::numeric_limits<int>::max());
return tir::make_const(DataType::Int(32), static_cast<int>(num));
}
/*!
* \brief Return a vector of variables that represents the sids for the given Relay Expr
*/
std::vector<tir::Var> PackSid(Expr expr) {
std::vector<tir::Var> buffer_vars;
StorageInfo& sinfo = storage_device_map_[expr];
// Note that an expression can have multiple sids associated with it
// e.g., returning multiple values from a function
for (auto sid : sinfo->storage_ids) {
// Determine if an sid is an output buffer
auto output_iter = std::find(return_sid_.begin(), return_sid_.end(), sid);
if (output_iter != return_sid_.end()) {
int output_index = std::distance(return_sid_.begin(), output_iter);
buffer_vars.push_back(main_signature_[input_vars_.size() + output_index]);
continue;
}
auto sid_value = sids_table_[sid];
buffer_vars.push_back(sid_value);
}
return buffer_vars;
}
/*!
* brief Given an expression return the variable(s) associated with that expression
*/
std::vector<te::Var> FindExpr(Expr arg) {
auto input_iter = std::find(input_vars_.begin(), input_vars_.end(), arg);
if (input_iter != input_vars_.end()) {
// Input variable
int main_index = std::distance(input_vars_.begin(), input_iter);
return {main_signature_[main_index]};
} else {
// Storage identifier (i.e., intermediate memory)
return PackSid(arg);
}
}
/*!
* brief Call a function with a given name
*/
void CreateFuncCall(Call call, std::string func_name) {
tvm::Array<PrimExpr> args{tvm::tir::StringImm(func_name)};
std::vector<tir::Stmt> create_func_call_stmts;
// Pack the inputs
for (Expr arg : call->args) {
if (params_by_expr_.find(arg) != params_by_expr_.end()) {
auto param_handle = tvm::tir::Call(DataType::Handle(), tvm::tir::builtin::lookup_param(),
{tir::StringImm(params_by_expr_[arg])});
args.push_back(param_handle);
} else {
auto var_arg = FindExpr(arg);
args.push_back(var_arg[0]);
}
}
auto ret_expr = Downcast<Expr>(call);
// Pack the return(s) value. A call node can produce multiple outputs
for (const auto& var : PackSid(ret_expr)) {
args.push_back(var);
}
// Use tvm_call_packed to execute the function unless we're calling directly
auto calling_pattern = tvm::tir::builtin::tvm_call_cpacked();
if (use_unpacked_api_) {
calling_pattern = tvm::tir::builtin::call_extern();
}
create_func_call_stmts.push_back(
tir::Evaluate(tvm::tir::Call(DataType::Int(32), calling_pattern, args)));
tir::Stmt body = tir::SeqStmt(create_func_call_stmts);
stmts_.push_back(body);
}
/*!
* brief Copy a variable to the output. This function is mainly used in edge cases
* when we want to return an input or a parameter.
* TODO(giuseros): we should try to avoid unnecessary copy to the output, e.g., in a
* copy-on-write fashion.
*/
void CopyToOutput(PrimExpr out, PrimExpr in, bool pack_input, size_t size) {
// Define intermediate DLTensor to load/store the data
auto tmp0 = te::Var("tmp0", DataType::Handle());
auto tmp1 = te::Var("tmp1", DataType::Handle());
te::Var loop_idx("i", DataType::Int(32));
auto retval_i = tir::Load(DataType::UInt(8), tmp0, loop_idx, tir::const_true());
PrimExpr retval_get = tvm::tir::Call(DataType::Handle(), tvm::tir::builtin::tvm_struct_get(),
{in, 0, tir::builtin::kArrData});
PrimExpr tostore = tvm::tir::Call(DataType::Handle(), tvm::tir::builtin::tvm_struct_get(),
{out, 0, tir::builtin::kArrData});
if (use_unpacked_api_) {
tostore = out;
}
// Do not pack the input if the flag is set or the caller
// explicitly asked to do so (e.g., copying a param to the output)
if (use_unpacked_api_ || !pack_input) {
retval_get = in;
}
// Copy the variable from the input to the output
tir::Stmt copy = tir::For(
loop_idx, 0, ConstInt32(size), tir::ForKind::kSerial,
tir::Store(tmp1, tir::Let(tmp0, retval_get, retval_i), loop_idx, tir::const_true()));
stmts_.push_back(tir::LetStmt(tmp1, tostore, copy));
}
/*!
* Utility function to string together different arguments
*/
template <typename... Args>
std::string MakeString(Args const&... args) {
std::ostringstream ss;
using List = int[];
(void)List{0, ((void)(ss << args), 0)...};
return ss.str();
}
void VisitExpr_(const CallNode* op) override {
// Descend the call tree
for (auto arg : op->args) {
VisitExpr(arg);
}
if (op->op.as<OpNode>()) {
LOG(FATAL) << "Operators should be transformed away; try applying"
<< "the fuse_ops transformation to the expression.";
} else if (op->op.as<GlobalVarNode>()) {
GlobalVar node = GetRef<GlobalVar>(op->op.as<GlobalVarNode>());
CreateFuncCall(GetRef<Call>(op), node->name_hint);
} else {
LOG(FATAL) << "TVM runtime does not support calls to " << op->op->GetTypeKey();
}
}
void VisitExpr_(const VarNode* op) override {
Expr expr = GetRef<Expr>(op);
StorageInfo& sinfo = storage_device_map_[expr];
// If the Var node is an output node we need to copy the content of the variable to the output
// It's safe to check the SID here because Var StorageToken are never reallocated
auto output_iter = std::find(return_sid_.begin(), return_sid_.end(), sinfo->storage_ids[0]);
if (output_iter != return_sid_.end()) {
int output_index = std::distance(return_sid_.begin(), output_iter);
if (params_by_expr_.find(expr) != params_by_expr_.end()) {
auto param_handle = tvm::tir::Call(DataType::Handle(), tvm::tir::builtin::lookup_param(),
{tir::StringImm(params_by_expr_[expr])});
CopyToOutput(main_signature_[input_vars_.size() + output_index], param_handle,
/*pack_input*/ true, sinfo->storage_sizes_in_bytes[0]);
} else {
auto var_expr = FindExpr(expr);
CopyToOutput(main_signature_[input_vars_.size() + output_index], var_expr[0],
/*pack_input*/ true, sinfo->storage_sizes_in_bytes[0]);
}
}
}
void VisitExpr_(const ConstantNode* op) override {
Expr expr = GetRef<Expr>(op);
size_t index = params_.size();
std::string name = "p" + std::to_string(index);
StorageInfo& sinfo = storage_device_map_[expr];
param_storage_ids_[name] = sinfo->storage_ids[0];
params_[name] = op->data;
params_by_expr_.Set(expr, name);
// If the Constant node is an output node we need to copy the content of the parameter to the
// output A Var node can only produce a single output
auto output_iter = std::find(return_sid_.begin(), return_sid_.end(), sinfo->storage_ids[0]);
if (output_iter != return_sid_.end()) {
int output_index = std::distance(return_sid_.begin(), output_iter);
auto param_handle = tvm::tir::Call(DataType::Handle(), tvm::tir::builtin::lookup_param(),
{tir::StringImm(params_by_expr_[expr])});
CopyToOutput(main_signature_[input_vars_.size() + output_index], param_handle, false,
sinfo->storage_sizes_in_bytes[0]);
}
}
void VisitExpr_(const TupleNode* op) override {
for (auto field : op->fields) {
VisitExpr(field);
}
}
void VisitExpr_(const LetNode* op) override {
// TODO(giuseros): support Let nodes in AOT
CHECK(false) << "Let not yet implemented in AOT";
}
void VisitExpr_(const TupleGetItemNode* op) override { VisitExpr(op->tuple); }
void VisitExpr_(const OpNode* op) override {
throw std::runtime_error("can not compile op in non-eta expanded form");
}
void VisitExpr_(const GlobalVarNode* op) override { throw std::runtime_error(""); }
void VisitExpr_(const IfNode* op) override { throw std::invalid_argument("if not supported"); }
void VisitExpr_(const FunctionNode* op) override {
ICHECK(op->GetAttr<String>(attr::kCompiler).defined())
<< "FunctionNode only supported by custom codegen";
}
void VisitExpr_(const RefCreateNode* op) override {
throw std::invalid_argument("reference not supported");
}
void VisitExpr_(const RefReadNode* op) override {
throw std::invalid_argument("reference not supported");
}
void VisitExpr_(const RefWriteNode* op) override {
throw std::invalid_argument("reference not supported");
}
void VisitExpr_(const ConstructorNode* op) override {
throw std::invalid_argument("ADT constructor case not yet implemented");
}
void VisitExpr_(const MatchNode* op) override {
throw std::invalid_argument("match case not yet implemented");
}
// Create the main PrimFunc to execute the graph. Please note that
// the packed function calls don't pack their arguments. The AOT
// runner function needs to be legalized by the LegalizePackedCalls pass.
tir::PrimFunc CreateMainFunc(String mod_name, unsigned int relay_params) {
tir::Stmt body = tir::SeqStmt(stmts_);
// Allocate the sids
std::unordered_map<int, bool> allocated;
for (auto kv : storage_device_map_) {
// Only allocate sids that are needed
const bool is_input =
(std::find(input_vars_.begin(), input_vars_.end(), kv.first) != input_vars_.end());
const bool is_param = (params_by_expr_.find(kv.first) != params_by_expr_.end());
if (is_input || is_param) {
continue;
}
for (unsigned int i = 0; i < kv.second->storage_ids.size(); i++) {
int size = kv.second->storage_sizes_in_bytes[i];
int sid = kv.second->storage_ids[i];
if (std::find(return_sid_.begin(), return_sid_.end(), sid) != return_sid_.end()) {
continue;
}
// TODO(giuseros): we should allocate this once outside the PrimFunc
// so we don't pay the price of allocation for every inference
if (!allocated[sid]) {
body = tir::Allocate(sids_table_[sid], DataType::Int(8), {size}, tir::const_true(), body);
}
allocated[sid] = true;
}
}
// Define the attributes
body = tir::AttrStmt(PrimExpr(), tvm::tir::attr::device_type, 1, body);
body = tir::AttrStmt(PrimExpr(), tvm::tir::attr::device_id, 0, body);
// Define the PrimFunc attributes
Map<String, ObjectRef> dict_attrs;
String run_func_name =
runtime::get_name_mangled(mod_name, runtime::symbol::tvm_run_func_suffix);
dict_attrs.Set("global_symbol", run_func_name);
dict_attrs.Set("runner_function", Bool(true));
// Make the PrimFunc
return tir::PrimFunc(main_signature_, body, VoidType(), Map<tir::Var, tir::Buffer>(),
DictAttrs(dict_attrs));
}
protected:
/*! \brief mod */
runtime::Module* mod_;
/*! \brief list of input expressions (i.e., variable passed by the user) */
std::vector<Var> input_vars_;
/*! \brief input and output variables belonging to the main function signature */
Array<tir::Var> main_signature_;
/*! \brief target device */
tec::TargetMap targets_;
/*! \brief target host */
Target target_host_;
/*!
* \brief unpacked api toggle
* When set to true the code generated will use unpacked calls to functions:
* func(void* arg0, void* arg1)
* Rather than packed calls:
* func(void* args)
* Defaults to using the packed calling convention
*/
Bool use_unpacked_api_;
/*!
* \brief parameters (i.e. ConstantNodes found in the graph).
* These are take as inputs to the GraphRuntime.
* Maps param name to a pair of storage_id and NDArray. At runtime, the storage_id can be
* used to lookup the parameter.
*/
std::unordered_map<std::string, runtime::NDArray> params_;
/*! \brief mapping between expression and parameters */
Map<Expr, String> params_by_expr_;
/*! \brief mapping between parameter names ("p0", "p1", etc..) and storage identifiers*/
std::unordered_map<std::string, int64_t> param_storage_ids_;
/*! \brief plan memory of device result */
StorageMap storage_device_map_;
/*! \brief mapping sid -> tir::Var */
std::unordered_map<int, te::Var> sids_table_;
/*! \brief lowered funcs */
Map<String, FunctionInfo> function_metadata_;
/*! \brief the set of statements that make the program */
std::vector<tir::Stmt> stmts_;
/*! \brief the list of return sids (note that the function might return more then one output */
std::vector<int> return_sid_;
public:
AOTExecutorCodegen(runtime::Module* mod, const tec::TargetMap& targets, Target target_host)
: mod_(mod),
targets_(targets),
target_host_(target_host),
use_unpacked_api_(target_host->GetAttr<Bool>("unpacked-api").value_or(Bool(false))) {}
LoweredOutput Codegen(relay::Function func, String mod_name) {
auto aot_allocator = AOTOnDemandAllocator();
aot_allocator.Run(func);
// Pre-lowering storage map and memory plan
StorageMap initial_storage_map = aot_allocator.GetStorageMap();
StaticMemoryPlan memory_plan(initial_storage_map);
// Build a map from each operation to device.
tec::DeviceMap device_context_map;
for (const auto& it : memory_plan->expr_to_storage_info) {
auto expr = it.first;
auto storage_info = it.second;
auto device_types = storage_info->device_types;
// CHECK_EQ(device_types.size(), 1);
tvm::Device dev;
dev.device_id = 0;
dev.device_type = device_types[0];
device_context_map.insert({expr, dev});
}
// This first phase moves from implicit use of compile engine,
// to instead explicitly lowering the incoming IRModule, and then
// performing the preexisting AOT executor code generation phase.
IRModule mod = IRModule::FromExpr(func);
IRModule new_mod =
LowerTEPass(targets_, device_context_map, memory_plan, mod_name, [this](Function func) {
// We need to maintain the constant map for external
// functions so we pass this processing function which
// allows us to process each function as we lower it.
if (func->GetAttr<String>(attr::kCompiler).defined()) {
UpdateConstants(func, ¶ms_);
}
// TODO(@areusch, @jroesch): We should refactor this to
// execute as a further pass, instead writing data to the
// lowering process directly.
tec::UpdateFunctionMetadata(func, this->function_metadata_);
})(mod);
tec::LoweredModule lowered_module = tec::IRModuleToLoweredModule(new_mod);
function_metadata_.Set(runtime::symbol::tvm_module_main, lowered_module.main_func_info);
auto lowered_main = lowered_module.main_module->Lookup("main");
auto lowered_main_func = GetRef<Function>(lowered_main.as<FunctionNode>());
// Post-lowering storage map for writing main func - this should be the same map as previously
// created, just referencing the new expressions created from lowering
auto new_allocator = AOTOnDemandAllocator();
new_allocator.Run(lowered_main_func);
storage_device_map_ = new_allocator.GetStorageMap();
for (auto input : lowered_main_func->params) {
input_vars_.push_back(input);
main_signature_.push_back(tir::Var("input", DataType::Handle()));
}
// Define the storage allocator ids
for (auto kv : storage_device_map_) {
for (auto sid : kv.second->storage_ids) {
te::Var buffer_var(MakeString("sid_", sid),
PointerType(PrimType(DataType::Int(8)), "global"));
sids_table_[sid] = buffer_var;
}
}
// Retrieve the return sids
return_sid_ = aot_allocator.GetReturnIds();
for (unsigned int output_index = 0; output_index < return_sid_.size(); output_index++) {
main_signature_.push_back(tir::Var("output", DataType::Handle()));
}
VisitExpr(lowered_main_func->body);
// Create the runner function. Please note that the function is not legal yet
// because the packed calls arguments are not wrapped in TVMValues. To make this happen we need
// to run the LegalizePackedCalls pass.
auto prim_func = CreateMainFunc(mod_name, lowered_main_func->params.size());
LoweredOutput ret;
ret.params = std::unordered_map<std::string, std::pair<int, const tvm::runtime::NDArray>>();
for (auto param : params_) {
ret.params.emplace(std::make_pair(
param.first,
std::make_pair(static_cast<int>(param_storage_ids_[param.first]), param.second)));
}
// Build the TIR IRModule for the AOT function
Map<GlobalVar, BaseFunc> symbol_map;
symbol_map.Set(GlobalVar(::tvm::runtime::symbol::tvm_run_func_suffix), prim_func);
IRModule mod_run(symbol_map);
// Apply storage rewrite pass to the runner function to do memory planning
auto storage_rewrite = tir::transform::StorageRewrite();
mod_run = storage_rewrite(mod_run);
// Legalize AOT if needed. This means that all the packed calls
// need to be wrapped in TVMValues (unless use_unpacked_api is set)
if (!use_unpacked_api_) {
auto pack_calls = tir::transform::LegalizePackedCalls();
mod_run = pack_calls(mod_run);
}
ret.function_metadata = std::move(function_metadata_);
ret.lowered_funcs = lowered_module.per_target_module;
ret.external_mods = lowered_module.external_mods;
auto target_host_str = target_host_->str();
if (ret.lowered_funcs.find(target_host_str) != ret.lowered_funcs.end()) {
ret.lowered_funcs[target_host_str]->Update(mod_run);
} else {
ret.lowered_funcs.Set(target_host_str, mod_run);
}
std::vector<String> input_var_names(input_vars_.size());
std::transform(input_vars_.begin(), input_vars_.end(), input_var_names.begin(),
[](Var input_var) -> String { return input_var->name_hint(); });
ret.metadata =
runtime::Metadata(input_var_names, return_sid_.size(), runtime::kTvmExecutorAot, mod_name);
return ret;
}
};
class AOTExecutorCodegenModule : public runtime::ModuleNode {
public:
AOTExecutorCodegenModule() {}
virtual PackedFunc GetFunction(const std::string& name, const ObjectPtr<Object>& sptr_to_self) {
if (name == "init") {
return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) {
ICHECK_EQ(args.num_args, 2) << "The expected of arguments are: "
<< "runtime::Module mod and Map<int, Target> targets";
void* mod = args[0];
Map<Integer, tvm::Target> targets = args[1];
init(mod, targets);
});
} else if (name == "codegen") {
return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) {
Function func = args[0];
String mod_name = args[1];
this->output_ = codegen(func, mod_name);
});
} else if (name == "list_params_name") {
return PackedFunc(
[sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { *rv = list_params_name(); });
} else if (name == "get_param_by_name") {
return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) {
String key = args[0];
*rv = get_param_by_name(key);
});
} else if (name == "get_param_id") {
return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) {
String key = args[0];
*rv = get_param_id(key);
});
} else if (name == "get_irmodule") {
return PackedFunc(
[sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { *rv = get_irmodule(); });
} else if (name == "get_external_modules") {
return PackedFunc(
[sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { *rv = get_external_modules(); });
} else if (name == "get_function_metadata") {
return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) {
*rv = this->output_.function_metadata;
});
} else if (name == "get_metadata") {
return PackedFunc(
[sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { *rv = output_.metadata; });
} else {
return PackedFunc([](TVMArgs args, TVMRetValue* rv) {});
}
}
const char* type_key() const final { return "RelayGraphRuntimeCodegenModule"; }
private:
void init(void* mod, Map<Integer, tvm::Target> tmp) {
tec::TargetMap targets;
Target target_host;
for (const auto& it : tmp) {
auto dev_type = it.first.as<tir::IntImmNode>();
if (!target_host.defined() && it.second->kind->device_type == kDLCPU) {
target_host = it.second;
}
ICHECK(dev_type);
targets[static_cast<DLDeviceType>(dev_type->value)] = it.second;
}
codegen_ = std::make_shared<AOTExecutorCodegen>(reinterpret_cast<runtime::Module*>(mod),
targets, target_host);
}
LoweredOutput codegen(Function func, String mod_name) {
return this->codegen_->Codegen(func, mod_name);
}
Array<runtime::String> list_params_name() {
Array<runtime::String> ret;
for (const auto& kv : this->output_.params) {
ret.push_back(kv.first);
}
return ret;
}
runtime::NDArray get_param_by_name(String key) {
auto it = this->output_.params.find(key);
CHECK(it != this->output_.params.end()) << "no such parameter " << key;
return (*it).second.second;
}
Array<tvm::runtime::Module> get_external_modules() { return output_.external_mods; }
int get_param_id(String key) {
auto it = this->output_.params.find(key);
CHECK(it != this->output_.params.end()) << "no such parameter " << key;
return (*it).second.first;
}
Map<String, IRModule> get_irmodule() { return this->output_.lowered_funcs; }
std::shared_ptr<AOTExecutorCodegen> codegen_;
LoweredOutput output_;
};
runtime::Module CreateAOTExecutorCodegenMod() {
auto ptr = make_object<AOTExecutorCodegenModule>();
return runtime::Module(ptr);
}
TVM_REGISTER_GLOBAL("relay.build_module._AOTExecutorCodegen")
.set_body([](TVMArgs args, TVMRetValue* rv) { *rv = CreateAOTExecutorCodegenMod(); });
} // namespace backend
} // namespace relay
} // namespace tvm