-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
sketch_policy.cc
437 lines (378 loc) · 17.2 KB
/
sketch_policy.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
/*
* 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 auto_scheduler/search_policy/sketch_search_policy.h
* \brief The search policy that searches in a hierarchical search space defined by sketches.
* The policy randomly samples programs from the space defined by sketches
* and use evolutionary search to fine-tune them.
*/
#include "sketch_policy.h"
#include <tvm/runtime/registry.h>
#include <algorithm>
#include <iomanip>
#include <limits>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include "sketch_policy_rules.h"
namespace tvm {
namespace auto_scheduler {
/********** Sketch generation rules **********/
static RuleSkipStage rule_skip_stage;
static RuleAlwaysInline rule_always_inline;
static RuleMultiLevelTiling rule_multi_level_tiling;
static RuleMultiLevelTilingWithFusion rule_multi_level_tiling_with_fusion;
static RuleAddCacheRead rule_add_cache_read_stage;
static RuleAddCacheWrite rule_add_cache_write_stage;
static RuleAddRfactor rule_add_rfactor;
static RuleCrossThreadReduction rule_cross_thread_reduction;
static RuleSimplifyComputeWithConstTensor rule_simplify_compute_with_const_tensor;
static RuleSpecialComputeLocationGPU rule_special_compute_location_gpu;
/********** Init population rules **********/
static InitFillTileSize init_fill_tile_size;
static InitChangeComputeLocation init_change_compute_location;
static InitParallel init_parallel;
static InitUnroll init_unroll;
static InitVectorization init_vectorization;
static InitThreadBind init_thread_bind;
/********** Sketch policy **********/
TVM_REGISTER_NODE_TYPE(SketchPolicyNode);
SketchPolicy::SketchPolicy(SearchTask task, CostModel schedule_cost_model,
Map<String, ObjectRef> params, int seed, int verbose,
Optional<Array<SearchCallback>> init_search_callbacks) {
auto node = make_object<SketchPolicyNode>();
node->search_task = std::move(task);
node->schedule_cost_model = std::move(schedule_cost_model);
node->rand_gen = std::mt19937(seed);
node->params = std::move(params);
node->verbose = verbose;
if (init_search_callbacks) {
PrintTitle("Call init-search callbacks", verbose);
// Candidates:
// - auto_scheduler.PreloadMeasuredStates: Load already measured states to
// `measured_states_set_`, `measured_states_vector_` and `measured_states_throughputs_`.
// - auto_scheduler.PreloadCustomSketchRule: Add user custom sketch rules to `sketch_rules`,
// these rules will be processed prior to the default rules.
node->RunCallbacks(init_search_callbacks.value());
}
// Notice: Some rules require us to skip all the rest rules after they are applied.
// So the rules below should be ordered carefully.
if (IsCPUTask(node->search_task)) {
// The default sketch rules for CPU policy
node->sketch_rules.push_back(&rule_always_inline);
node->sketch_rules.push_back(&rule_simplify_compute_with_const_tensor);
node->sketch_rules.push_back(&rule_add_rfactor);
node->sketch_rules.push_back(&rule_add_cache_write_stage);
node->sketch_rules.push_back(&rule_multi_level_tiling_with_fusion);
node->sketch_rules.push_back(&rule_multi_level_tiling);
} else if (IsCUDATask(node->search_task)) {
// The default sketch rules for CUDA policy
node->sketch_rules.push_back(&rule_add_cache_read_stage);
node->sketch_rules.push_back(&rule_always_inline);
node->sketch_rules.push_back(&rule_special_compute_location_gpu);
node->sketch_rules.push_back(&rule_simplify_compute_with_const_tensor);
node->sketch_rules.push_back(&rule_cross_thread_reduction);
node->sketch_rules.push_back(&rule_add_cache_write_stage);
node->sketch_rules.push_back(&rule_multi_level_tiling_with_fusion);
node->sketch_rules.push_back(&rule_multi_level_tiling);
} else {
LOG(FATAL) << "No default sketch rules for target: " << task->target;
}
node->sketch_rules.push_back(&rule_skip_stage); // This should always be the last rule
node->init_rules.push_back(&init_fill_tile_size); // This should always be the first rule
if (IsCPUTask(node->search_task)) {
// The default init population rules for CPU policy
node->init_rules.push_back(&init_change_compute_location);
node->init_rules.push_back(&init_parallel);
node->init_rules.push_back(&init_unroll);
node->init_rules.push_back(&init_vectorization);
} else if (IsCUDATask(node->search_task)) {
// The default init population rules for CUDA policy
node->init_rules.push_back(&init_thread_bind);
node->init_rules.push_back(&init_unroll);
} else {
LOG(FATAL) << "No default init rules for target: " << task->target;
}
data_ = std::move(node);
}
State SketchPolicyNode::Search(int n_trials, int early_stopping, int num_measure_per_iter,
ProgramMeasurer measurer) {
num_measure_per_iter_ = num_measure_per_iter;
if (n_trials <= 1) {
// No measurement is allowed
const Array<State>& best_states = SearchOneRound(0);
CHECK_GT(best_states.size(), 0);
return best_states[0];
} else {
int num_random =
static_cast<int>(GetDoubleParam(params, SketchParamKey::eps_greedy) * num_measure_per_iter);
early_stopping = early_stopping < 0 ? std::numeric_limits<int>::max() >> 1 : early_stopping;
measurer->Reset();
int ct = 0;
int empty_retry_count = GetIntParam(params, SketchParamKey::empty_retry_count);
Array<MeasureInput> inputs;
Array<MeasureResult> results;
while (ct < n_trials) {
if (!inputs.empty()) {
// Retrain cost models before the next search round
PrintTitle("Train cost model", verbose);
schedule_cost_model->Update(inputs, results);
}
// Search one round to get promising states
PrintTitle("Search", verbose);
Array<State> random_states;
Array<State> best_states = SearchOneRound(num_random, &random_states);
// Infer bound. This is necessary for computing the correct ToStr() for redundancy check
best_states = search_task->compute_dag.InferBound(best_states);
random_states = search_task->compute_dag.InferBound(random_states);
// Pick `num_measure_per_iter` states to measure, check hash to remove already measured state
// Also pick some random states to do eps-greedy
inputs = PickStatesWithEpsGreedy(best_states, random_states, n_trials - ct);
// Currently it's hard to detect if all of the search space has been traversed
// Stop if no extra valid states found in several retries
if (inputs.empty()) {
if (empty_retry_count-- > 0) {
continue;
} else {
StdCout(verbose) << "It seems all candidates in the search space have been measured."
<< std::endl;
break;
}
} else {
// Reset the retry count
empty_retry_count = GetIntParam(params, SketchParamKey::empty_retry_count);
}
// Measure candidate states
PrintTitle("Measure", verbose);
measurer->Measure(search_task, GetRef<SearchPolicy>(this), inputs, &results);
ct += inputs.size();
// Check if reach the early stopping condition
if (ct - measurer->best_ct[search_task->workload_key] > early_stopping) {
StdCout(verbose) << "Stop early since no performance improvement in the last "
<< early_stopping << " measure steps.\n";
break;
}
// Update measured states throughputs. These states will join the EvolutionarySearch in later
// search rounds.
for (const auto& res : results) {
measured_states_throughputs_.push_back(1.0 / FloatArrayMean(res->costs));
}
}
PrintTitle("Done", verbose);
return measurer->best_state[search_task->workload_key];
}
}
Array<State> SketchPolicyNode::SearchOneRound(int num_random_states, Array<State>* random_states) {
// Temporal object to be used if the input pointer is nullptr
Array<State> temp_random_states;
if (random_states == nullptr) {
random_states = &temp_random_states;
} else {
random_states->clear();
}
// Get parameters
int population = GetIntParam(params, SketchParamKey::EvolutionarySearch::population);
int num_use_measured =
std::min(static_cast<int>(measured_states_vector_.size()),
static_cast<int>(
GetDoubleParam(params, SketchParamKey::EvolutionarySearch::use_measured_ratio) *
population));
bool is_cost_model_reasonable = !schedule_cost_model->IsInstance<RandomModelNode>();
// 1. Generate sketches
const Array<State>& sketches = GenerateSketches();
// 2. Sample the init population
Array<State> init_populations = SampleInitPopulation(
sketches, is_cost_model_reasonable ? population - num_use_measured : population);
// 3. If the cost model is useless (i.e. RandomCostModel), just random pick some generated
// states, else perform evolutionary search
if (is_cost_model_reasonable) {
// Also insert already measured good states to the initial population
std::vector<int> indices = Argsort(measured_states_throughputs_);
for (int i = 0; i < num_use_measured; i++) {
init_populations.push_back(measured_states_vector_[indices[i]]);
}
// Sample some random states for eps-greedy
*random_states = RandomSampleStates(init_populations, &rand_gen, num_random_states * 10);
return EvolutionarySearch(init_populations, num_measure_per_iter_ * 2);
} else {
return RandomSampleStates(init_populations, &rand_gen, num_measure_per_iter_ * 3);
}
}
Array<State> SketchPolicyNode::GenerateSketches() {
const State& init_state = search_task->compute_dag->init_state;
// Two ping pong buffers to avoid copy
Array<State> states_buf1{init_state}, states_buf2;
Array<State>* pnow = &states_buf1;
Array<State>* pnext = &states_buf2;
// A map that maps state to its current working position (stage_id)
std::unordered_map<State, int, ObjectHash, ObjectEqual> cur_stage_id_map;
cur_stage_id_map[init_state] = static_cast<int>(init_state->stages.size() - 1);
// Derivation rule based enumeration
Array<State> out_states;
while (!pnow->empty()) {
pnext->clear();
for (const State& state : *pnow) {
int stage_id = cur_stage_id_map[state];
// Reaches to the terminal stage
if (stage_id < 0) {
out_states.push_back(state);
continue;
}
// Try all derivation rules
for (const auto& rule : sketch_rules) {
auto cond = rule->MeetCondition(*this, state, stage_id);
if (cond != SketchGenerationRule::ConditionKind::kSkip) {
for (const auto& pair : rule->Apply(*this, state, stage_id)) {
cur_stage_id_map[pair.first] = pair.second;
pnext->push_back(pair.first);
}
// Skip the reset rules
if (cond == SketchGenerationRule::ConditionKind::kApplyAndSkipRest) {
break;
}
}
}
}
std::swap(pnow, pnext);
}
// Hack for rfactor: Replace the split factor for rfactor to the undefined Expr(),
// so later we can sample random value for the split factor.
// Why don't we use Expr() when doing the split for rfactor at the first time?
// Because during ApplySteps, a rfactor with undefined Expr() will crash TVM.
// So rfactor with undefined Expr() will conflict with cache_write, cache_read, rfactor
// in other stages
for (size_t i = 0; i < out_states.size(); ++i) {
auto state = out_states[i];
auto pstate = state.CopyOnWrite();
for (size_t step_id = 0; step_id < pstate->transform_steps.size(); ++step_id) {
if (pstate->transform_steps[step_id]->IsInstance<RfactorStepNode>()) {
CHECK_GE(step_id, 1);
int split_step_id = static_cast<int>(step_id - 1);
auto step = pstate->transform_steps[split_step_id].as<SplitStepNode>();
CHECK(step != nullptr);
pstate->transform_steps.Set(
split_step_id, SplitStep(step->stage_id, step->iter_id, step->extent, {NullOpt},
step->inner_to_outer));
}
}
out_states.Set(i, std::move(state));
}
StdCout(verbose) << "Generate Sketches\t\t#s: " << out_states.size() << std::endl;
return out_states;
}
Array<State> SketchPolicyNode::SampleInitPopulation(const Array<State>& sketches, int out_size) {
int fail_ct = 0;
Array<State> out_states;
auto tic_begin = std::chrono::high_resolution_clock::now();
// TODO(jcf94, merrymercy): Use parallel_for to run this loop in parallel
while (static_cast<int>(out_states.size()) < out_size && fail_ct < static_cast<int>(out_size)) {
// Random choose a starting sketch
// TODO(jcf94, merrymercy): Maybe choose sketches in different possibility for they may have
// different potential on generating state with better performance
State tmp_s = sketches[(rand_gen)() % sketches.size()];
// Derivation rule based enumeration
bool valid = true;
for (const auto& rule : init_rules) {
if (rule->Apply(this, &tmp_s) == InitPopulationRule::ResultKind::kInvalid) {
valid = false;
break;
}
}
if (valid) {
out_states.push_back(std::move(tmp_s));
} else {
fail_ct++;
}
}
double duration = std::chrono::duration_cast<std::chrono::duration<double>>(
std::chrono::high_resolution_clock::now() - tic_begin)
.count();
StdCout(verbose) << "Sample Initial Population\t#s: " << out_states.size()
<< "\tfail_ct: " << fail_ct << "\tTime elapsed: " << std::fixed
<< std::setprecision(2) << duration << std::endl;
return out_states;
}
Array<State> SketchPolicyNode::EvolutionarySearch(const Array<State>& init_populations,
int out_size) {
Array<State> best_states;
auto tic_begin = std::chrono::high_resolution_clock::now();
// TODO(comaniac, merrymercy, jcf94): Since we haven't finished porting the cost model part
// yet, currently delete the implementation of EvolutionarySearch. To be added later.
double duration = std::chrono::duration_cast<std::chrono::duration<double>>(
std::chrono::high_resolution_clock::now() - tic_begin)
.count();
StdCout(verbose) << "EvolutionarySearch\t\t#s: " << best_states.size()
<< "\tTime elapsed: " << std::fixed << std::setprecision(2) << duration
<< std::endl;
return best_states;
}
Array<MeasureInput> SketchPolicyNode::PickStatesWithEpsGreedy(const Array<State>& best_states,
const Array<State>& random_states,
int remaining_n_trials) {
int num_random =
static_cast<int>(GetDoubleParam(params, SketchParamKey::eps_greedy) * num_measure_per_iter_);
int num_good = num_measure_per_iter_ - num_random;
Array<MeasureInput> inputs;
size_t offset_best = 0, offset_random = 0;
while (static_cast<int>(inputs.size()) < std::min(num_measure_per_iter_, remaining_n_trials)) {
State state;
bool has_best = offset_best < best_states.size();
bool has_random = offset_random < random_states.size();
if (static_cast<int>(inputs.size()) < num_good) {
// prefer best states
if (has_best) {
state = best_states[offset_best++];
} else if (has_random) {
state = random_states[offset_random++];
} else {
break;
}
} else {
// prefer random states
if (has_random) {
state = random_states[offset_random++];
} else if (has_best) {
state = best_states[offset_best++];
} else {
break;
}
}
// Check if it has already been measured
std::string state_str = state.ToStr();
if (!measured_states_set_.count(state_str)) {
measured_states_set_.insert(std::move(state_str));
measured_states_vector_.push_back(state);
inputs.push_back(MeasureInput(search_task, state));
}
}
return inputs;
}
TVM_REGISTER_GLOBAL("auto_scheduler.SketchPolicy")
.set_body_typed([](SearchTask task, CostModel schedule_cost_model,
Map<String, ObjectRef> params, int seed, int verbose,
Optional<Array<SearchCallback>> init_search_callbacks) {
return SketchPolicy(task, schedule_cost_model, params, seed, verbose, init_search_callbacks);
});
TVM_REGISTER_GLOBAL("auto_scheduler.SketchPolicyGenerateSketches")
.set_body_typed([](SketchPolicy policy) { return policy->GenerateSketches(); });
} // namespace auto_scheduler
} // namespace tvm