-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
source_module.cc
1103 lines (984 loc) · 40.2 KB
/
source_module.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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 source_module.cc
* \brief Source code module, only for viewing
*/
#include "source_module.h"
#include <dmlc/memory_io.h>
#include <tvm/runtime/metadata.h>
#include <tvm/runtime/module.h>
#include <tvm/runtime/name_transforms.h>
#include <tvm/runtime/ndarray.h>
#include <tvm/runtime/packed_func.h>
#include <tvm/runtime/registry.h>
#include <algorithm>
#include <functional>
#include <numeric>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include "../../relay/backend/name_transforms.h"
#include "../../runtime/file_utils.h"
#include "../../support/str_escape.h"
#include "../func_registry_generator.h"
#include "../metadata.h"
#include "../metadata_utils.h"
#include "codegen_params.h"
#include "codegen_source_base.h"
#include "tvm/relay/executor.h"
namespace tvm {
namespace codegen {
using runtime::PackedFunc;
using runtime::TVMArgs;
using runtime::TVMRetValue;
using runtime::FunctionInfo;
using runtime::GetFileFormat;
using runtime::GetMetaFilePath;
using runtime::SaveBinaryToFile;
// Simulator function
class SourceModuleNode : public runtime::ModuleNode {
public:
SourceModuleNode(std::string code, std::string fmt) : code_(code), fmt_(fmt) {}
const char* type_key() const final { return "source"; }
PackedFunc GetFunction(const String& name, const ObjectPtr<Object>& sptr_to_self) final {
LOG(FATAL) << "Source module cannot execute, to get executable module"
<< " build TVM with \'" << fmt_ << "\' runtime support";
return PackedFunc();
}
String GetSource(const String& format) final { return code_; }
String GetFormat() override { return fmt_; }
protected:
std::string code_;
std::string fmt_;
};
runtime::Module SourceModuleCreate(std::string code, std::string fmt) {
auto n = make_object<SourceModuleNode>(code, fmt);
return runtime::Module(n);
}
// Simulator function
class CSourceModuleNode : public runtime::ModuleNode {
public:
CSourceModuleNode(const std::string& code, const std::string& fmt,
const Array<String>& func_names, const Array<String>& const_vars)
: code_(code), fmt_(fmt), const_vars_(const_vars), func_names_(func_names) {}
const char* type_key() const final { return "c"; }
PackedFunc GetFunction(const String& name, const ObjectPtr<Object>& sptr_to_self) final {
// Currently c-source module is used as demonstration purposes with binary metadata module
// that expects get_symbol interface. When c-source module is used as external module, it
// will only contain one function. However, when its used as an internal module (e.g., target
// "c") it can have many functions.
if (name == "get_symbol") {
return PackedFunc(
[sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { *rv = this->func_names_[0]; });
} else if (name == "get_const_vars") {
return PackedFunc(
[sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { *rv = this->const_vars_; });
} else if (name == "get_func_names") {
return PackedFunc(
[sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { *rv = this->func_names_; });
} else {
return PackedFunc(nullptr);
}
}
String GetSource(const String& format) final { return code_; }
String GetFormat() override { return fmt_; }
void SaveToBinary(dmlc::Stream* stream) final {
stream->Write(code_);
stream->Write(fmt_);
std::vector<std::string> func_names;
for (const auto func_name : func_names_) func_names.push_back(func_name);
std::vector<std::string> const_vars;
for (auto const_var : const_vars_) const_vars.push_back(const_var);
stream->Write(func_names);
stream->Write(const_vars);
}
static runtime::Module LoadFromBinary(void* strm) {
dmlc::Stream* stream = static_cast<dmlc::Stream*>(strm);
std::string code, fmt;
ICHECK(stream->Read(&code)) << "Loading code failed";
ICHECK(stream->Read(&fmt)) << "Loading format failed";
std::vector<std::string> tmp_func_names, tmp_const_vars;
CHECK(stream->Read(&tmp_func_names)) << "Loading func names failed";
CHECK(stream->Read(&tmp_const_vars)) << "Loading const vars failed";
Array<String> func_names;
for (auto func_name : tmp_func_names) func_names.push_back(String(func_name));
Array<String> const_vars;
for (auto const_var : tmp_const_vars) const_vars.push_back(String(const_var));
auto n = make_object<CSourceModuleNode>(code, fmt, func_names, const_vars);
return runtime::Module(n);
}
void SaveToFile(const String& file_name, const String& format) final {
std::string fmt = GetFileFormat(file_name, format);
std::string meta_file = GetMetaFilePath(file_name);
if (fmt == "c" || fmt == "cc" || fmt == "cpp" || fmt == "cu") {
ICHECK_NE(code_.length(), 0);
SaveBinaryToFile(file_name, code_);
} else {
ICHECK_EQ(fmt, fmt_) << "Can only save to format=" << fmt_;
}
}
int GetPropertyMask() const override {
return runtime::ModulePropertyMask::kBinarySerializable |
runtime::ModulePropertyMask::kDSOExportable;
}
bool ImplementsFunction(const String& name, bool query_imports) final {
return std::find(func_names_.begin(), func_names_.end(), name) != func_names_.end();
}
protected:
std::string code_;
std::string fmt_;
Array<String> const_vars_;
Array<String> func_names_;
};
runtime::Module CSourceModuleCreate(const String& code, const String& fmt,
const Array<String>& func_names,
const Array<String>& const_vars) {
auto n = make_object<CSourceModuleNode>(code.operator std::string(), fmt.operator std::string(),
func_names, const_vars);
return runtime::Module(n);
}
TVM_REGISTER_GLOBAL("runtime.module.loadbinary_c")
.set_body_typed(CSourceModuleNode::LoadFromBinary);
/*!
* \brief A concrete class to get access to base methods of CodegenSourceBase.
*
* This class exist to get access to methods of CodegenSourceBase without duplicating
* them. Therefore, keeping alignment with how codegen and source_module here generates
* code.
*/
class ConcreteCodegenSourceBase : public CodeGenSourceBase {
/*!
* \brief Do nothing as this class exist to get access to methods of CodeGenSourceBase
*/
void PrintSSAAssign(const std::string& target, const std::string& src, DataType t) final {
return;
}
};
class CSourceCrtMetadataModuleNode : public runtime::ModuleNode {
public:
CSourceCrtMetadataModuleNode(const Array<String>& func_names, const std::string& fmt,
Target target, relay::Runtime runtime,
relay::backend::ExecutorCodegenMetadata metadata)
: fmt_(fmt),
func_names_(func_names),
target_(target),
runtime_(runtime),
metadata_(metadata) {
CreateSource();
}
const char* type_key() const final { return "c"; }
String GetSource(const String& format) final { return code_.str(); }
String GetFormat() override { return fmt_; }
PackedFunc GetFunction(const String& name, const ObjectPtr<Object>& sptr_to_self) final {
return PackedFunc();
}
void SaveToFile(const String& file_name, const String& format) final {
std::string fmt = GetFileFormat(file_name, format);
std::string meta_file = GetMetaFilePath(file_name);
if (fmt == "c" || fmt == "cc" || fmt == "cpp") {
auto code_str = code_.str();
ICHECK_NE(code_str.length(), 0);
SaveBinaryToFile(file_name, code_str);
} else {
ICHECK_EQ(fmt, fmt_) << "Can only save to format=" << fmt_;
}
}
int GetPropertyMask() const override { return runtime::ModulePropertyMask::kDSOExportable; }
bool ImplementsFunction(const String& name, bool query_imports) final {
return std::find(func_names_.begin(), func_names_.end(), name) != func_names_.end();
}
protected:
std::stringstream code_;
std::string fmt_;
Array<String> func_names_;
Target target_;
relay::Runtime runtime_;
relay::backend::ExecutorCodegenMetadata metadata_;
ConcreteCodegenSourceBase codegen_c_base_;
void CreateFuncRegistry() {
code_ << "#include <tvm/runtime/crt/module.h>\n";
for (const auto& fname : func_names_) {
code_ << "#ifdef __cplusplus\n";
code_ << "extern \"C\"\n";
code_ << "#endif\n";
code_ << "TVM_DLL int32_t " << fname.data();
code_ << "(TVMValue* args, int* type_code, int num_args, TVMValue* out_value, "
"int* out_type_code, void* resource_handle);\n";
}
code_ << "static TVMBackendPackedCFunc _tvm_func_array[] = {\n";
for (auto f : func_names_) {
code_ << " (TVMBackendPackedCFunc)" << f << ",\n";
}
code_ << "};\n";
auto registry = target::GenerateFuncRegistryNames(func_names_);
code_ << "static const TVMFuncRegistry _tvm_func_registry = {\n"
<< " \"" << ::tvm::support::StrEscape(registry.data(), registry.size(), true) << "\","
<< " _tvm_func_array,\n"
<< "};\n";
}
void GenerateCrtSystemLib() {
code_ << "static const TVMModule _tvm_system_lib = {\n"
<< " &_tvm_func_registry,\n"
<< "};\n"
<< "const TVMModule* TVMSystemLibEntryPoint(void) {\n"
<< " return &_tvm_system_lib;\n"
<< "}\n";
}
String GenerateDLTensorStructWrapper(String reference_arg) {
code_ << "DLTensor " << reference_arg << "_dltensor = {\n";
code_ << ".data = &" << reference_arg << "\n";
code_ << "};\n";
code_ << "TVMValue " << reference_arg << "_tvm_value = {\n";
code_ << ".v_handle = &" << reference_arg << "_dltensor\n";
code_ << "};\n";
return reference_arg + "_tvm_value";
}
void GenerateInternalBuffers() {
if (metadata_->pool_inputs.defined()) {
for (const auto& kv : metadata_->pool_inputs.value()) {
tir::usmp::AllocatedPoolInfo allocated_pool_info = kv.second;
if (allocated_pool_info->pool_info->is_internal) {
if (const auto* pool_info = allocated_pool_info->pool_info.as<ConstantPoolInfoNode>()) {
GenerateConstantBuffer(pool_info, allocated_pool_info->allocated_size->value);
} else {
GenerateWorkspaceBuffer(allocated_pool_info->pool_info.as<WorkspacePoolInfoNode>(),
allocated_pool_info->allocated_size->value);
}
}
}
}
}
void GenerateIOWorkspaceMapFunction(const std::string& struct_type,
const std::string& function_name,
const Array<String>& tensor_names) {
std::string map_function = runtime::get_name_mangled(metadata_->mod_name, function_name);
code_ << "struct " << struct_type << " " << map_function << "(\n";
std::string pools_struct = runtime::get_name_mangled(metadata_->mod_name, "workspace_pools");
code_ << " struct " << pools_struct << "* workspace_pools\n";
code_ << "\n){\n";
code_ << "struct " << struct_type << " ret = {\n";
for (const String& name : tensor_names) {
tir::usmp::PoolAllocation pool_allocation = metadata_->io_pool_allocations[name];
code_ << "\t." << name << " = "
<< "&((uint8_t*)workspace_pools->" << pool_allocation->pool_info->pool_name << ")["
<< pool_allocation->byte_offset << "],\n";
}
code_ << "};\n";
code_ << "return ret;\n";
code_ << "}\n\n";
}
void GenerateConstantBuffer(const ConstantPoolInfoNode* pool_info, size_t allocated_size) {
size_t ord = 0;
if (pool_info->constant_info_array.size() > 0) {
// Pool is RO, form an initialized struct
code_ << "__attribute__((section(\".rodata.tvm\"), ";
code_ << "))\n";
code_ << "static const struct " << pool_info->pool_name << " {\n";
// emit struct field names
std::vector<ConstantInfo> const_info_vec(pool_info->constant_info_array.begin(),
pool_info->constant_info_array.end());
std::sort(const_info_vec.begin(), const_info_vec.end(),
[](const ConstantInfo& a, const ConstantInfo& b) {
return a->byte_offset->value < b->byte_offset->value;
});
for (const auto& const_info : const_info_vec) {
const auto& data = const_info->data;
const auto& offs = const_info->byte_offset;
int64_t num_elements = std::accumulate(data.Shape().begin(), data.Shape().end(), 1,
std::multiplies<int64_t>());
code_ << " ";
codegen_c_base_.PrintType(data.DataType(), code_);
code_ << " " << const_info->name_hint << "[" << num_elements << "] __attribute__(("
<< (ord++ ? "packed, " : "") << "aligned(" << metadata_->constant_alignment << ")));";
code_ << " // " << num_elements * data.DataType().bytes()
<< " bytes, aligned offset: " << offs << "\n";
}
code_ << "} " << pool_info->pool_name << " = {\n";
// emit struct field initialization data
for (const auto& const_info : const_info_vec) {
code_ << " ." << const_info->name_hint << " = {\n";
codegen::NDArrayDataToC(const_info->data, 4, code_);
code_ << " },\n";
}
code_ << "};";
code_ << "// of total size " << allocated_size << " bytes\n";
} else {
LOG(FATAL) << "No constant data in constant pool found " << GetRef<ObjectRef>(pool_info);
}
}
void GenerateWorkspaceBuffer(const WorkspacePoolInfoNode* pool_info, size_t allocated_size) {
code_ << "__attribute__((section(\".bss.noinit.tvm\"), ";
code_ << "aligned(" << metadata_->workspace_alignment << ")))\n";
code_ << "static uint8_t " << pool_info->pool_name << "[";
code_ << allocated_size << "];\n";
}
bool IsInternalWorkspaceBuffer(const tir::Var& pool_var) {
if (metadata_->pool_inputs.defined()) {
Map<tir::Var, tir::usmp::AllocatedPoolInfo> allocated_pool_infos =
metadata_->pool_inputs.value();
if (allocated_pool_infos.find(pool_var) != allocated_pool_infos.end()) {
tir::usmp::AllocatedPoolInfo allocate_pool_info = allocated_pool_infos[pool_var];
if (allocate_pool_info->pool_info->is_internal) {
return true;
}
}
}
return false;
}
void GenerateEntrypointForUnpackedAPI(const std::string& entrypoint_name,
const std::string& run_func) {
code_ << "TVM_DLL int32_t " << run_func << "(";
{
std::stringstream call_args_ss;
if (metadata_->io_pool_allocations.empty()) {
for (const tir::Var& input_var : metadata_->inputs) {
if (input_var->type_annotation.defined()) {
codegen_c_base_.PrintType(input_var->type_annotation, call_args_ss);
} else {
codegen_c_base_.PrintType(input_var.dtype(), call_args_ss);
}
call_args_ss << " " << input_var->name_hint << ",";
}
for (unsigned int i = 0; i < metadata_->outputs.size(); ++i) {
call_args_ss << "void* output" << i << ",";
}
}
for (const tir::Var& pool_var : metadata_->pools) {
if (pool_var->type_annotation.defined()) {
codegen_c_base_.PrintType(pool_var->type_annotation, call_args_ss);
} else {
codegen_c_base_.PrintType(pool_var.dtype(), call_args_ss);
}
call_args_ss << " " << pool_var->name_hint << ",";
}
std::string call_args_str = call_args_ss.str();
call_args_str.pop_back();
code_ << call_args_str;
}
code_ << ");\n";
code_ << "int32_t " << entrypoint_name;
code_ << "(void* args, void* type_code, int num_args, void* out_value, void* "
"out_type_code, void* resource_handle) {\n";
code_ << "return " << run_func << "(";
{
std::stringstream call_args_ss;
if (metadata_->io_pool_allocations.empty()) {
for (unsigned int i = 0; i < metadata_->inputs.size(); ++i) {
call_args_ss << "((DLTensor*)(((TVMValue*)args)[" << i << "].v_handle))[0].data,";
}
for (unsigned int i = 0; i < metadata_->outputs.size(); ++i) {
int j = metadata_->inputs.size() + i;
call_args_ss << "((DLTensor*)(((TVMValue*)args)[" << j << "].v_handle))[0].data,";
}
}
for (const tir::Var& pool_var : metadata_->pools) {
if (IsInternalWorkspaceBuffer(pool_var)) {
call_args_ss << "&" << metadata_->pool_inputs.value()[pool_var]->pool_info->pool_name
<< ",";
}
}
std::string call_args_str = call_args_ss.str();
call_args_str.pop_back();
code_ << call_args_str;
code_ << ");\n";
code_ << "}\n";
}
}
std::unordered_map<int, ObjectRef> GenerateRunFuncToEntryPointArgMap() {
std::unordered_map<int, ObjectRef> run_func_to_entry_point_args;
int entrypoint_arg_count = 0;
int run_func_arg_count = 0;
if (metadata_->io_pool_allocations.empty()) {
for (unsigned int i = 0; i < metadata_->inputs.size(); i++) {
run_func_to_entry_point_args[run_func_arg_count] = Integer(entrypoint_arg_count);
entrypoint_arg_count++;
run_func_arg_count++;
}
for (unsigned int i = 0; i < metadata_->outputs.size(); i++) {
run_func_to_entry_point_args[run_func_arg_count] = Integer(entrypoint_arg_count);
entrypoint_arg_count++;
run_func_arg_count++;
}
}
for (const tir::Var& pool_var : metadata_->pools) {
if (IsInternalWorkspaceBuffer(pool_var)) {
tir::usmp::AllocatedPoolInfo allocated_pool_info = metadata_->pool_inputs.value()[pool_var];
run_func_to_entry_point_args[run_func_arg_count] =
allocated_pool_info->pool_info->pool_name;
run_func_arg_count++;
}
}
return run_func_to_entry_point_args;
}
void GenerateEntrypointForPackedAPI(const std::string& entrypoint_name,
const std::string& run_func) {
code_ << "TVM_DLL int32_t " << run_func;
code_ << "(TVMValue* args, int* type_code, int num_args, TVMValue* out_value, int* "
"out_type_code, void* resource_handle);\n\n";
code_ << "int32_t " << entrypoint_name;
code_ << "(TVMValue* args, int* type_code, int num_args, TVMValue* out_value, int* "
"out_type_code, void* resource_handle) {\n";
// We are creating a copy of the set of pointers
size_t number_of_io_tensors = metadata_->inputs.size() + metadata_->outputs.size() +
metadata_->pools.size() - metadata_->io_pool_allocations.size();
code_ << "TVMValue tensors[" << number_of_io_tensors << "];\n";
std::unordered_map<int, ObjectRef> run_func_to_entry_point_args =
GenerateRunFuncToEntryPointArgMap();
for (unsigned int i = 0; i < number_of_io_tensors; i++) {
if (run_func_to_entry_point_args.find(i) != run_func_to_entry_point_args.end()) {
if (run_func_to_entry_point_args[i]->IsInstance<StringObj>()) {
String pool_name = Downcast<String>(run_func_to_entry_point_args[i]);
String pool_name_tvmv = GenerateDLTensorStructWrapper(pool_name);
code_ << "tensors[" << i << "] = " << pool_name_tvmv << ";\n";
} else {
code_ << "tensors[" << i << "] = ((TVMValue*)args)[" << run_func_to_entry_point_args[i]
<< "];\n";
}
}
}
code_ << "return " << run_func;
code_ << "((void*)tensors, type_code, num_args, out_value, out_type_code, resource_handle);\n";
code_ << "}\n";
}
static int isNotAlnum(char c) { return !std::isalnum(c); }
void GenerateCInterfaceEntrypoint(const std::string& entrypoint_name, const std::string& run_func,
const std::string& mod_name) {
code_ << "#include <" << mod_name << ".h>\n";
if (!metadata_->io_pool_allocations.empty()) {
const std::string input_struct_type =
runtime::get_name_mangled(metadata_->mod_name, "inputs");
Array<String> input_tensor_names;
for (const tir::Var& input_var : metadata_->inputs) {
input_tensor_names.push_back(input_var->name_hint);
}
GenerateIOWorkspaceMapFunction(input_struct_type, "map_inputs", input_tensor_names);
const std::string output_struct_type =
runtime::get_name_mangled(metadata_->mod_name, "outputs");
GenerateIOWorkspaceMapFunction(output_struct_type, "map_outputs", metadata_->outputs);
}
code_ << "TVM_DLL int32_t " << run_func << "(";
{
std::stringstream call_args_ss;
if (metadata_->io_pool_allocations.empty()) {
for (const tir::Var& input_var : metadata_->inputs) {
if (input_var->type_annotation.defined()) {
codegen_c_base_.PrintType(input_var->type_annotation, call_args_ss);
} else {
codegen_c_base_.PrintType(input_var.dtype(), call_args_ss);
}
call_args_ss << " " << tvm::runtime::SanitizeName(input_var->name_hint) << ",";
}
for (unsigned int i = 0; i < metadata_->outputs.size(); ++i) {
call_args_ss << "void* output" << i << ",";
}
}
for (const tir::Var& pool_var : metadata_->pools) {
if (pool_var->type_annotation.defined()) {
codegen_c_base_.PrintType(pool_var->type_annotation, call_args_ss);
} else {
codegen_c_base_.PrintType(pool_var.dtype(), call_args_ss);
}
call_args_ss << " " << pool_var->name_hint << ",";
}
for (const String& device : metadata_->devices) {
call_args_ss << "void* " << device << ",";
}
std::string call_args_str = call_args_ss.str();
call_args_str.pop_back();
code_ << call_args_str;
}
code_ << ");\n";
code_ << "int32_t " << entrypoint_name << "(";
{
std::stringstream call_args_ss;
if (metadata_->io_pool_allocations.empty()) {
call_args_ss << "struct " << runtime::get_name_mangled(mod_name, "inputs") << "* inputs,";
call_args_ss << "struct " << runtime::get_name_mangled(mod_name, "outputs") << "* outputs,";
}
if (!metadata_->pools.empty()) {
bool is_external_pools_present = false;
for (tir::Var pool_var : metadata_->pools) {
if (!IsInternalWorkspaceBuffer(pool_var)) {
is_external_pools_present = true;
break;
}
}
if (is_external_pools_present) {
call_args_ss << "struct " << runtime::get_name_mangled(mod_name, "workspace_pools")
<< "* workspace_pools,";
}
}
if (!metadata_->devices.empty()) {
call_args_ss << "struct " << runtime::get_name_mangled(mod_name, "devices") << "* devices,";
}
std::string call_args_str = call_args_ss.str();
call_args_str.pop_back();
code_ << call_args_str;
}
code_ << ") {"
<< "return " << run_func << "(";
{
std::stringstream call_args_ss;
if (metadata_->io_pool_allocations.empty()) {
for (const auto& input : metadata_->inputs) {
call_args_ss << "inputs->" << tvm::runtime::SanitizeName(input->name_hint) << ",";
}
for (const auto& output : metadata_->outputs) {
call_args_ss << "outputs->" << tvm::runtime::SanitizeName(output);
call_args_ss << ",";
}
}
for (const tir::Var& pool_var : metadata_->pools) {
call_args_ss << "((uint8_t*)";
String pool_name = metadata_->pool_inputs.value()[pool_var]->pool_info->pool_name;
if (IsInternalWorkspaceBuffer(pool_var)) {
call_args_ss << "&" << pool_name;
} else {
call_args_ss << "workspace_pools->" << tvm::runtime::SanitizeName(pool_name);
}
call_args_ss << "),";
}
for (const String& device : metadata_->devices) {
call_args_ss << "devices->" << device << ",";
}
std::string call_args_str = call_args_ss.str();
call_args_str.pop_back();
code_ << call_args_str;
}
code_ << ");\n";
code_ << "}\n";
}
void GenerateAOTDescriptor() {
const std::string run_func_suffix = ::tvm::runtime::symbol::tvm_module_main;
const std::string tvm_entrypoint_suffix = ::tvm::runtime::symbol::tvm_entrypoint_suffix;
const std::string run_func_mangled =
runtime::get_name_mangled(metadata_->mod_name, run_func_suffix);
const std::string entrypoint_mangled =
runtime::get_name_mangled(metadata_->mod_name, tvm_entrypoint_suffix);
const std::string network_mangled = runtime::get_name_mangled(metadata_->mod_name, "network");
code_ << "#include \"tvm/runtime/c_runtime_api.h\"\n";
code_ << "#ifdef __cplusplus\n";
code_ << "extern \"C\" {\n";
code_ << "#endif\n";
GenerateInternalBuffers();
if (metadata_->unpacked_api) {
if (metadata_->interface_api == "c") {
GenerateCInterfaceEntrypoint(entrypoint_mangled, run_func_mangled, metadata_->mod_name);
} else {
GenerateEntrypointForUnpackedAPI(entrypoint_mangled, run_func_mangled);
}
} else {
ICHECK_EQ(metadata_->interface_api, "packed")
<< "Packed interface required for packed operators";
GenerateEntrypointForPackedAPI(entrypoint_mangled, run_func_mangled);
}
code_ << "#ifdef __cplusplus\n";
code_ << "}\n";
code_ << "#endif\n";
}
void CreateSource() {
if (runtime_->GetAttr<Bool>("system-lib").value_or(Bool(false)) && !func_names_.empty()) {
CreateFuncRegistry();
GenerateCrtSystemLib();
}
if (metadata_.defined() && metadata_->executor == runtime::kTvmExecutorAot) {
GenerateAOTDescriptor();
}
code_ << ";";
}
};
class MetadataSerializer : public AttrVisitor {
public:
static constexpr const char* kGlobalSymbol = "kTvmgenMetadata";
using MetadataKind = ::tvm::runtime::metadata::MetadataKind;
MetadataSerializer() : is_first_item_{true} {}
void WriteComma() {
if (is_first_item_) {
is_first_item_ = false;
} else {
code_ << ", " << std::endl;
}
}
void WriteKey(const char* key) {
if (key != nullptr) {
code_ << " /* " << key << "*/";
}
}
void Visit(const char* key, double* value) final {
WriteComma();
code_.setf(std::ios::hex | std::ios::showbase | std::ios::fixed | std::ios::scientific,
std::ios::basefield | std::ios::showbase | std::ios::floatfield);
code_ << *value;
WriteKey(key);
}
void Visit(const char* key, int64_t* value) final {
WriteComma();
code_ << *value << "L";
WriteKey(key);
}
void Visit(const char* key, uint64_t* value) final {
WriteComma();
code_ << *value << "UL";
WriteKey(key);
}
void Visit(const char* key, int* value) final {
WriteComma();
code_ << *value;
WriteKey(key);
}
void Visit(const char* key, bool* value) final {
WriteComma();
code_ << *value;
WriteKey(key);
}
void Visit(const char* key, std::string* value) final {
WriteComma();
code_ << "\"" << *value << "\"";
WriteKey(key);
}
void Visit(const char* key, void** value) final {
WriteComma();
code_ << *value;
WriteKey(key);
}
void Visit(const char* key, DataType* value) final {
WriteComma();
code_ << "{" << value->code() << ", " << value->bits() << ", " << value->lanes() << "}";
WriteKey(key);
}
// Serialiding NDArray as tuple of len, data
void Visit(const char* key, runtime::NDArray* value) final {
WriteComma();
std::string bytes;
dmlc::MemoryStringStream stream(&bytes);
value->Save(&stream);
// Serializing length of the data of NDArray
code_ << stream.Tell();
WriteComma();
// Serializing NDArray as bytestream
code_ << "\"";
std::stringstream ss;
char buf[6] = {0};
for (uint8_t c : bytes) {
snprintf(buf, sizeof(buf), "\\x%02x", c);
ss << buf;
}
std::string as_bytes(ss.str());
code_ << as_bytes;
code_ << "\"\n";
}
void VisitArray(runtime::metadata::MetadataArray array) {
auto old_is_first_item = is_first_item_;
is_first_item_ = true;
for (unsigned int i = 0; i < array->array.size(); ++i) {
ObjectRef o = array->array[i];
switch (array->kind) {
case MetadataKind::kUint64: {
int64_t i = Downcast<Integer>(o).IntValue();
CHECK_GT(i, 0)
<< "Metadata is of type uint64_t, but array type contains a negative number";
uint64_t ui = static_cast<uint64_t>(i);
Visit(nullptr, &ui);
continue;
}
case MetadataKind::kInt64: {
int64_t i = Downcast<Integer>(o).IntValue();
Visit(nullptr, &i);
continue;
}
case MetadataKind::kBool: {
bool b = Downcast<Bool>(o);
Visit(nullptr, &b);
break;
}
case MetadataKind::kString: {
std::string s = Downcast<String>(o);
Visit(nullptr, &s);
break;
}
case MetadataKind::kHandle:
CHECK(false) << "Don't know how to serialize handle";
break;
case MetadataKind::kMetadata: {
runtime::metadata::MetadataBase metadata = Downcast<runtime::metadata::MetadataBase>(o);
std::stringstream i_str;
i_str << i;
address_.push_back(i_str.str());
Visit(nullptr, &metadata);
address_.pop_back();
break;
}
default:
CHECK(false) << "Unknown MetadataKind for array: " << array->kind;
break;
}
is_first_item_ = false;
}
is_first_item_ = old_is_first_item;
}
void Visit(const char* key, ObjectRef* value) final {
const runtime::metadata::MetadataArrayNode* arr =
value->as<runtime::metadata::MetadataArrayNode>();
if (arr != nullptr) {
WriteComma();
if (key != nullptr) {
address_.push_back(key);
}
code_ << metadata::AddressFromParts(address_);
if (key != nullptr) {
address_.pop_back();
}
return;
}
runtime::metadata::MetadataBase metadata = Downcast<runtime::metadata::MetadataBase>(*value);
if (key != nullptr) { // NOTE: outermost call passes nullptr key
address_.push_back(key);
}
WriteComma();
code_ << "{\n";
is_first_item_ = true;
ReflectionVTable::Global()->VisitAttrs(metadata.operator->(), this);
code_ << "}\n";
if (key != nullptr) { // NOTE: outermost call passes nullptr key
address_.pop_back();
}
}
private:
void EmitCType(const runtime::metadata::MetadataArrayNode* arr, std::ostream& os) {
switch (arr->kind) {
case MetadataKind::kUint64:
os << "uint64_t";
break;
case MetadataKind::kInt64:
os << "int64_t";
break;
case MetadataKind::kBool:
os << "bool";
break;
case MetadataKind::kString:
os << "const char*";
break;
case MetadataKind::kHandle:
os << "void*";
break;
case MetadataKind::kMetadata:
os << "struct " << arr->get_element_c_struct_name();
break;
default:
CHECK(false) << "Unknown kind in MetadataArray: " << arr->kind
<< " (struct_name=" << arr->get_c_struct_name() << ")";
break;
}
}
public:
void CodegenMetadata(::tvm::runtime::metadata::Metadata metadata) {
decl_ << "#include <inttypes.h>" << std::endl
<< "#include <tvm/runtime/metadata_types.h>" << std::endl
<< "#include <tvm/runtime/c_runtime_api.h>" << std::endl;
std::vector<metadata::DiscoverArraysVisitor::DiscoveredArray> queue;
metadata::DiscoverArraysVisitor array_discover{&queue};
array_discover.Visit(metadata::kMetadataGlobalSymbol, &metadata);
for (auto item : queue) {
auto struct_address = std::get<0>(item);
address_.push_back(struct_address);
auto arr = std::get<1>(item);
// Prepend const with everything except C-string, which needs appending.
code_ << "static ";
if (arr->kind != MetadataKind::kString) {
code_ << "const ";
}
EmitCType(arr.operator->(), code_);
if (arr->kind == MetadataKind::kString) {
code_ << " const";
}
code_ << " " << struct_address << "[" << arr->array.size() << "] = {" << std::endl;
is_first_item_ = true;
VisitArray(arr);
address_.pop_back();
code_ << "};" << std::endl;
}
// Finally, emit overall struct.
address_.push_back(metadata::kMetadataGlobalSymbol);
code_ << "static const struct TVMMetadata " << metadata::AddressFromParts(address_) << "[1] = {"
<< std::endl;
Visit(nullptr, &metadata);
code_ << "};" << std::endl;
address_.pop_back();
}
std::string GetOutput() { return decl_.str() + code_.str(); }
private:
std::vector<std::string> address_;
std::stringstream decl_;
std::stringstream code_;
bool is_first_item_;
std::unordered_set<std::string> generated_struct_decls_;
std::vector<bool> is_defining_struct_;
};
namespace {
runtime::Module CreateAotMetadataModule(runtime::metadata::Metadata aot_metadata,
bool is_c_runtime) {
MetadataSerializer serializer;
serializer.CodegenMetadata(aot_metadata);
std::stringstream lookup_func;
std::string get_c_metadata_func_name;
// NOTE: mangling is not needed in the c++ runtime because the function
// name is looked-up via LibraryModule.
// TODO(alanmacd): unify these two approaches
if (is_c_runtime == true) {
get_c_metadata_func_name = runtime::get_name_mangled(
aot_metadata->mod_name(), ::tvm::runtime::symbol::tvm_get_c_metadata);
} else {
get_c_metadata_func_name = ::tvm::runtime::symbol::tvm_get_c_metadata;
}
lookup_func << "#ifdef __cplusplus\n"
<< "extern \"C\"\n"
<< "#endif\n";
lookup_func << "TVM_DLL int32_t " << get_c_metadata_func_name
<< "(TVMValue* arg_values, int* arg_tcodes, int "
"num_args, TVMValue* ret_values, int* ret_tcodes, void* resource_handle) {"
<< std::endl;
lookup_func << " ret_values[0].v_handle = (void*) &" << MetadataSerializer::kGlobalSymbol
<< ";" << std::endl;
lookup_func << " ret_tcodes[0] = kTVMOpaqueHandle;" << std::endl;
lookup_func << " return 0;" << std::endl;
lookup_func << "};" << std::endl;
std::vector<String> func_names{get_c_metadata_func_name};
return CSourceModuleCreate(serializer.GetOutput() + lookup_func.str(), "c", func_names,
Array<String>());
}
} // namespace
runtime::Module CreateCSourceCrtMetadataModule(const Array<runtime::Module>& modules, Target target,
relay::Runtime runtime,
relay::backend::ExecutorCodegenMetadata metadata,
runtime::metadata::Metadata aot_metadata) {
Array<runtime::Module> final_modules(modules);
Array<String> func_names;
if (metadata.defined()) {
if (metadata->executor == "aot") {
if (aot_metadata.defined()) {
final_modules.push_back(CreateAotMetadataModule(aot_metadata, true));
}
// add the run function (typically "tvmgen_default_run") to function registry
// when using AOT executor
std::string run_func = runtime::get_name_mangled(metadata->mod_name, "run");
func_names.push_back(run_func);
}
}
for (runtime::Module mod : final_modules) {
auto pf_funcs = mod.GetFunction("get_func_names");
if (pf_funcs != nullptr) {
Array<String> func_names_ = pf_funcs();
for (const auto& fname : func_names_) {
func_names.push_back(fname);
}
}
}
auto n = make_object<CSourceCrtMetadataModuleNode>(func_names, "c", target, runtime, metadata);
auto csrc_metadata_module = runtime::Module(n);
for (const auto& mod : final_modules) {