forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration.cc
180 lines (156 loc) · 6.66 KB
/
integration.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
/*
* 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.
*/
#include <tvm/meta_schedule/integration.h>
#include <tvm/relay/function.h>
#include <tvm/tir/function.h>
#include "./utils.h"
namespace tvm {
namespace meta_schedule {
/**************** Utility functions ****************/
template <class FunctionType>
bool HasOnlyOneFunction(const IRModule& mod) {
if (mod->functions.size() != 1) {
return false;
}
for (const auto& kv : mod->functions) {
const BaseFunc& func = kv.second;
if (!func->IsInstance<typename FunctionType::ContainerType>()) {
return false;
}
}
return true;
}
/**************** ExtractedTask ****************/
ExtractedTask::ExtractedTask(String task_name, IRModule mod, Target target,
Array<IRModule> dispatched) {
ObjectPtr<ExtractedTaskNode> n = make_object<ExtractedTaskNode>();
n->task_name = task_name;
n->mod = mod;
n->target = target;
n->dispatched = dispatched;
data_ = n;
}
/**************** MetaScheduleContext ****************/
struct MetaScheduleContextThreadLocalEntry {
Optional<MetaScheduleContext> ctx;
};
using MetaScheduleContextThreadLocalStore =
dmlc::ThreadLocalStore<MetaScheduleContextThreadLocalEntry>;
Optional<MetaScheduleContext> MetaScheduleContext::Current() {
return MetaScheduleContextThreadLocalStore::Get()->ctx;
}
void MetaScheduleContext::EnterWithScope() {
Optional<MetaScheduleContext>& ctx = MetaScheduleContextThreadLocalStore::Get()->ctx;
CHECK(!ctx.defined())
<< "ValueError: Nested MetaScheduleContext context managers are not allowed";
ctx = *this;
}
void MetaScheduleContext::ExitWithScope() {
Optional<MetaScheduleContext>& ctx = MetaScheduleContextThreadLocalStore::Get()->ctx;
ICHECK(ctx.defined());
ctx = NullOpt;
}
Optional<ObjectRef> MetaScheduleContext::QueryInsideWithScope(
runtime::String task_name, IRModule mod, Target target, Optional<Array<IRModule>> dispatched) {
if (Optional<MetaScheduleContext> ctx = MetaScheduleContext::Current()) {
return ctx.value()->Query(task_name, mod, target, dispatched);
}
return NullOpt;
}
/**************** TaskExtraction ****************/
TaskExtraction::TaskExtraction() {
ObjectPtr<TaskExtractionNode> n = make_object<TaskExtractionNode>();
n->tasks = Array<ExtractedTask>();
data_ = n;
}
Optional<ObjectRef> TaskExtractionNode::Query(runtime::String task_name, IRModule mod,
Target target, Optional<Array<IRModule>> dispatched) {
ICHECK(dispatched.defined());
ICHECK_EQ(dispatched.value().size(), 1);
IRModule prim_mod = dispatched.value()[0];
ICHECK(HasOnlyOneFunction<tir::PrimFunc>(prim_mod)) << prim_mod;
ICHECK(HasOnlyOneFunction<relay::Function>(mod)) << mod;
tasks.push_back(ExtractedTask(task_name, mod, target, {prim_mod}));
return NullOpt;
}
/**************** ApplyHistoryBest ****************/
ApplyHistoryBest::ApplyHistoryBest(Database database) {
ObjectPtr<ApplyHistoryBestNode> n = make_object<ApplyHistoryBestNode>();
n->database = database;
data_ = n;
}
Optional<ObjectRef> ApplyHistoryBestNode::Query(runtime::String task_name, IRModule mod,
Target target,
Optional<Array<IRModule>> dispatched) {
ICHECK(dispatched.defined());
ICHECK_EQ(dispatched.value().size(), 1);
ICHECK(HasOnlyOneFunction<relay::Function>(mod)) << mod;
IRModule prim_mod = dispatched.value()[0];
ICHECK(HasOnlyOneFunction<tir::PrimFunc>(prim_mod)) << prim_mod;
// Unify func name to make sure it can be found in database
const auto* parse_mod_func = runtime::Registry::Get("tvm.meta_schedule.tune.parse_mod");
ICHECK(parse_mod_func) << "Parse mod function not defined!";
prim_mod = (*parse_mod_func)(prim_mod);
if (database->HasWorkload(prim_mod)) {
Array<TuningRecord> records = database->GetTopK(database->CommitWorkload(prim_mod), 1);
if (records.size() == 1) {
LOG(INFO) << "Applied history best for " << task_name << ".";
tir::Schedule sch =
tir::Schedule::Traced(records[0]->workload->mod, /*seed=*/-1, /*debug_mask=*/0,
/*error_render_level=*/tir::ScheduleErrorRenderLevel::kNone);
records[0]->trace->ApplyToSchedule(sch, false);
return sch->mod();
}
}
return NullOpt;
}
/**************** FFI ****************/
class MetaScheduleContextInternal {
public:
static void EnterScope(MetaScheduleContext ctx) { ctx.EnterWithScope(); }
static void ExitScope(MetaScheduleContext ctx) { ctx.ExitWithScope(); }
};
TVM_REGISTER_NODE_TYPE(ExtractedTaskNode);
TVM_REGISTER_OBJECT_TYPE(MetaScheduleContextNode);
TVM_REGISTER_NODE_TYPE(TaskExtractionNode);
TVM_REGISTER_NODE_TYPE(ApplyHistoryBestNode);
TVM_REGISTER_GLOBAL("meta_schedule.ExtractedTask")
.set_body_typed([](String task_name, IRModule mod, Target target,
Array<IRModule> dispatched) -> ExtractedTask {
return ExtractedTask(task_name, mod, target, dispatched);
});
TVM_REGISTER_GLOBAL("meta_schedule.MetaScheduleContextEnterScope")
.set_body_typed(MetaScheduleContextInternal::EnterScope);
TVM_REGISTER_GLOBAL("meta_schedule.MetaScheduleContextExitScope")
.set_body_typed(MetaScheduleContextInternal::ExitScope);
TVM_REGISTER_GLOBAL("meta_schedule.MetaScheduleContextCurrent")
.set_body_typed(MetaScheduleContext::Current);
TVM_REGISTER_GLOBAL("meta_schedule.MetaScheduleContextQueryInsideWithScope")
.set_body_typed(MetaScheduleContext::QueryInsideWithScope);
TVM_REGISTER_GLOBAL("meta_schedule.MetaScheduleContextQuery")
.set_body_method<MetaScheduleContext>(&MetaScheduleContextNode::Query);
TVM_REGISTER_GLOBAL("meta_schedule.TaskExtraction").set_body_typed([]() -> TaskExtraction {
return TaskExtraction();
});
TVM_REGISTER_GLOBAL("meta_schedule.ApplyHistoryBest")
.set_body_typed([](Database database) -> ApplyHistoryBest {
return ApplyHistoryBest(database);
});
} // namespace meta_schedule
} // namespace tvm