/
measure_record.cc
482 lines (437 loc) · 15.4 KB
/
measure_record.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
/*
* 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/measure_record.cc
* \brief Json serialization format for dumping and loading tuning records.
*/
#include <dmlc/json.h>
#include <tvm/auto_scheduler/loop_state.h>
#include <tvm/auto_scheduler/measure_record.h>
#include <tvm/auto_scheduler/transform_step.h>
#include <tvm/runtime/registry.h>
#include <fstream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include "utils.h"
// Json serialization handler for MeasureInput, MeasureResult
// (and recursively for SearchTask, State, Step, ...)
namespace dmlc {
namespace json {
template <>
struct Handler<::tvm::Array<::tvm::auto_scheduler::Stage>> {
inline static void Write(dmlc::JSONWriter* writer,
const ::tvm::Array<::tvm::auto_scheduler::Stage>& data) {
writer->BeginArray(false);
writer->EndArray();
}
inline static void Read(dmlc::JSONReader* reader,
::tvm::Array<::tvm::auto_scheduler::Stage>* data) {
bool s;
reader->BeginArray();
s = reader->NextArrayItem();
ICHECK(!s);
}
};
template <>
struct Handler<::tvm::Array<::tvm::auto_scheduler::Step>> {
inline static void Write(dmlc::JSONWriter* writer,
const ::tvm::Array<::tvm::auto_scheduler::Step>& data) {
writer->BeginArray(false);
for (const auto& step : data) {
writer->WriteArraySeperator();
writer->BeginArray(false);
step->WriteToRecord(writer);
writer->EndArray();
}
writer->EndArray();
}
inline static void Read(dmlc::JSONReader* reader,
::tvm::Array<::tvm::auto_scheduler::Step>* data) {
bool s;
reader->BeginArray();
data->clear();
while (reader->NextArrayItem()) {
reader->BeginArray();
data->push_back(::tvm::auto_scheduler::StepReadFromRecord(reader));
s = reader->NextArrayItem();
ICHECK(!s);
}
}
};
template <>
struct Handler<::tvm::auto_scheduler::StateNode> {
inline static void Write(dmlc::JSONWriter* writer, const ::tvm::auto_scheduler::StateNode& data) {
writer->BeginArray(false);
writer->WriteArrayItem(data.stages);
writer->WriteArrayItem(data.transform_steps);
writer->EndArray();
}
inline static void Read(dmlc::JSONReader* reader, ::tvm::auto_scheduler::StateNode* data) {
bool s;
reader->BeginArray();
s = reader->NextArrayItem();
ICHECK(s);
reader->Read(&data->stages);
s = reader->NextArrayItem();
ICHECK(s);
reader->Read(&data->transform_steps);
s = reader->NextArrayItem();
ICHECK(!s);
}
};
template <>
struct Handler<::tvm::auto_scheduler::HardwareParamsNode> {
inline static void Write(dmlc::JSONWriter* writer,
const ::tvm::auto_scheduler::HardwareParamsNode& data) {
writer->BeginArray(false);
writer->WriteArrayItem(data.num_cores);
writer->WriteArrayItem(data.vector_unit_bytes);
writer->WriteArrayItem(data.cache_line_bytes);
writer->WriteArrayItem(data.max_shared_memory_per_block);
writer->WriteArrayItem(data.max_local_memory_per_block);
writer->WriteArrayItem(data.max_threads_per_block);
writer->WriteArrayItem(data.max_vthread_extent);
writer->WriteArrayItem(data.warp_size);
writer->EndArray();
}
inline static void Read(dmlc::JSONReader* reader,
::tvm::auto_scheduler::HardwareParamsNode* data) {
bool s;
reader->BeginArray();
s = reader->NextArrayItem();
CHECK(s);
reader->Read(&data->num_cores);
s = reader->NextArrayItem();
CHECK(s);
reader->Read(&data->vector_unit_bytes);
s = reader->NextArrayItem();
CHECK(s);
reader->Read(&data->cache_line_bytes);
s = reader->NextArrayItem();
CHECK(s);
reader->Read(&data->max_shared_memory_per_block);
s = reader->NextArrayItem();
CHECK(s);
reader->Read(&data->max_local_memory_per_block);
s = reader->NextArrayItem();
CHECK(s);
reader->Read(&data->max_threads_per_block);
s = reader->NextArrayItem();
CHECK(s);
reader->Read(&data->max_vthread_extent);
s = reader->NextArrayItem();
CHECK(s);
reader->Read(&data->warp_size);
s = reader->NextArrayItem();
CHECK(!s);
}
};
template <>
struct Handler<::tvm::auto_scheduler::SearchTaskNode> {
inline static void Write(dmlc::JSONWriter* writer,
const ::tvm::auto_scheduler::SearchTaskNode& data) {
writer->BeginArray(false);
writer->WriteArrayItem(std::string(data.workload_key));
writer->WriteArrayItem(data.target->str());
writer->WriteArrayItem(*data.hardware_params.get());
if (data.target_host.defined()) {
writer->WriteArrayItem(data.target_host->str());
} else {
writer->WriteArrayItem(std::string(""));
}
writer->WriteArrayItem(static_cast<int>(data.layout_rewrite_option));
writer->WriteArraySeperator();
writer->BeginArray(false);
for (const auto& i : data.task_input_names) {
writer->WriteArrayItem(std::string(i));
}
writer->EndArray();
writer->EndArray();
}
inline static void Read(dmlc::JSONReader* reader, ::tvm::auto_scheduler::SearchTaskNode* data) {
bool s;
std::string str_value;
int int_value;
auto hardware_params_node = ::tvm::make_object<::tvm::auto_scheduler::HardwareParamsNode>();
reader->BeginArray();
s = reader->NextArrayItem();
ICHECK(s);
reader->Read(&str_value);
data->workload_key = std::move(str_value);
s = reader->NextArrayItem();
ICHECK(s);
reader->Read(&str_value);
data->target = ::tvm::Target(str_value);
s = reader->NextArrayItem();
if (s) {
reader->Read(hardware_params_node.get());
s = reader->NextArrayItem();
data->hardware_params = ::tvm::auto_scheduler::HardwareParams(hardware_params_node);
if (s) {
reader->Read(&str_value);
if (!str_value.empty()) {
data->target_host = ::tvm::Target(str_value);
}
s = reader->NextArrayItem();
ICHECK(s);
reader->Read(&int_value);
data->layout_rewrite_option = ::tvm::auto_scheduler::LayoutRewriteOption(int_value);
s = reader->NextArrayItem();
if (s) {
reader->BeginArray();
s = reader->NextArrayItem();
while (s) {
reader->Read(&str_value);
data->task_input_names.push_back(str_value);
s = reader->NextArrayItem();
}
// Process the end of array
s = reader->NextArrayItem();
}
ICHECK(!s);
}
}
}
};
template <>
struct Handler<::tvm::auto_scheduler::MeasureInputNode> {
inline static void Write(dmlc::JSONWriter* writer,
const ::tvm::auto_scheduler::MeasureInputNode& data) {
writer->BeginArray(false);
writer->WriteArrayItem(*data.task.operator->());
writer->WriteArrayItem(*data.state.operator->());
writer->EndArray();
}
inline static void Read(dmlc::JSONReader* reader, ::tvm::auto_scheduler::MeasureInputNode* data) {
auto task_node = ::tvm::make_object<::tvm::auto_scheduler::SearchTaskNode>();
auto state_node = ::tvm::make_object<::tvm::auto_scheduler::StateNode>();
state_node->concrete = true;
bool s;
reader->BeginArray();
s = reader->NextArrayItem();
ICHECK(s);
reader->Read(task_node.get());
s = reader->NextArrayItem();
ICHECK(s);
reader->Read(state_node.get());
s = reader->NextArrayItem();
ICHECK(!s);
data->task = ::tvm::auto_scheduler::SearchTask(task_node);
data->state = ::tvm::auto_scheduler::State(state_node);
}
};
template <>
struct Handler<::tvm::auto_scheduler::MeasureResultNode> {
inline static void Write(dmlc::JSONWriter* writer,
const ::tvm::auto_scheduler::MeasureResultNode& data) {
writer->BeginArray(false);
writer->WriteArraySeperator();
writer->BeginArray(false);
for (const auto& x : data.costs) {
auto pf = x.as<::tvm::tir::FloatImmNode>();
ICHECK(pf != nullptr) << "Cost can only contain float values";
writer->WriteArrayItem(pf->value);
}
writer->EndArray();
writer->WriteArrayItem(data.error_no);
writer->WriteArrayItem(data.all_cost);
writer->WriteArrayItem(static_cast<int>((data.timestamp)));
writer->EndArray();
}
inline static void Read(dmlc::JSONReader* reader,
::tvm::auto_scheduler::MeasureResultNode* data) {
std::vector<double> double_list;
bool s;
reader->BeginArray();
s = reader->NextArrayItem();
ICHECK(s);
reader->Read(&double_list);
data->costs.clear();
for (const auto& i : double_list) {
data->costs.push_back(::tvm::FloatImm(::tvm::DataType::Float(64), i));
}
s = reader->NextArrayItem();
ICHECK(s);
reader->Read(&data->error_no);
s = reader->NextArrayItem();
ICHECK(s);
reader->Read(&data->all_cost);
s = reader->NextArrayItem();
ICHECK(s);
reader->Read(&data->timestamp);
s = reader->NextArrayItem();
ICHECK(!s);
}
};
} // namespace json
} // namespace dmlc
namespace tvm {
namespace auto_scheduler {
TVM_REGISTER_OBJECT_TYPE(RecordToFileNode);
TVM_REGISTER_OBJECT_TYPE(RecordReaderNode);
RecordToFile::RecordToFile(String filename) {
auto node = make_object<RecordToFileNode>();
node->filename = std::move(filename);
data_ = std::move(node);
}
void WriteMeasureRecords(std::ostream* os, const Array<MeasureInput>& inputs,
const Array<MeasureResult>& results, const std::string log_version) {
dmlc::JSONWriter writer(os);
for (size_t i = 0; i < inputs.size(); ++i) {
writer.BeginObject(false);
writer.WriteObjectKeyValue("i", *inputs[i].operator->());
writer.WriteObjectKeyValue("r", *results[i].operator->());
writer.WriteObjectKeyValue("v", log_version);
writer.EndObject();
*os << "\n";
}
}
void ReadMeasureRecord(const std::string& str, MeasureInputNode* inp, MeasureResultNode* res,
std::string* log_version) {
std::istringstream ss(str);
dmlc::JSONReader reader(&ss);
std::string key;
reader.BeginObject();
while (reader.NextObjectItem(&key)) {
if (key == "i") {
reader.Read(inp);
} else if (key == "r") {
reader.Read(res);
} else if (key == "v") {
reader.Read(log_version);
} else {
LOG(FATAL) << "Invalid key in json log: " << key;
}
}
}
void RecordToFileNode::Callback(const SearchPolicy& policy, const Array<MeasureInput>& inputs,
const Array<MeasureResult>& results) {
std::ofstream ofs(filename, std::ofstream::app);
WriteMeasureRecords(&ofs, inputs, results);
}
RecordReader::RecordReader(String filename) {
auto node = make_object<RecordReaderNode>();
node->filename = filename;
node->infile.open(filename, std::ifstream::in);
data_ = std::move(node);
}
RecordReaderNode::~RecordReaderNode() { infile.close(); }
bool RecordReaderNode::ReadNext(MeasureInputNode* inp, MeasureResultNode* res) {
std::string log_version;
while (std::getline(infile, cur_line_)) {
if (cur_line_[0] == '#' || cur_line_[0] == ' ') {
// skip comment lines begin with '#' or ' '
continue;
}
ReadMeasureRecord(cur_line_, inp, res, &log_version);
return true;
}
return false;
}
std::pair<Array<MeasureInput>, Array<MeasureResult>> RecordReaderNode::ReadLines(int max_size,
int skip_size) {
auto inp = make_object<MeasureInputNode>();
auto res = make_object<MeasureResultNode>();
Array<MeasureInput> inputs;
Array<MeasureResult> results;
while (ReadNext(inp.get(), res.get())) {
if (skip_size > 0) {
skip_size--;
continue;
}
inputs.push_back(inp->copy());
results.push_back(res->copy());
if (max_size > 0 && static_cast<int>(inputs.size()) >= max_size) {
break;
}
}
return std::make_pair(inputs, results);
}
TVM_REGISTER_GLOBAL("auto_scheduler.RecordToFile").set_body_typed([](const String& filename) {
return RecordToFile(filename);
});
TVM_REGISTER_GLOBAL("auto_scheduler.RecordReader").set_body_typed([](const String& filename) {
return RecordReader(filename);
});
TVM_REGISTER_GLOBAL("auto_scheduler.RecordReaderReadLines")
.set_body_typed([](RecordReader reader, int size, int skip_size) {
const auto& res = reader->ReadLines(size, skip_size);
return Array<ObjectRef>{res.first, res.second};
});
TVM_REGISTER_GLOBAL("auto_scheduler.RecordReaderReadNext").set_body_typed([](RecordReader reader) {
auto inp = make_object<MeasureInputNode>();
auto res = make_object<MeasureResultNode>();
if (reader->ReadNext(inp.get(), res.get())) {
return Array<ObjectRef>{ObjectRef(inp), ObjectRef(res)};
} else {
return Array<ObjectRef>();
}
});
TVM_REGISTER_GLOBAL("auto_scheduler.ReadMeasureRecord").set_body_typed([](const std::string& str) {
auto inp = make_object<MeasureInputNode>();
auto res = make_object<MeasureResultNode>();
std::string log_version;
ReadMeasureRecord(str, inp.get(), res.get(), &log_version);
return Array<ObjectRef>{ObjectRef(inp), ObjectRef(res)};
});
TVM_REGISTER_GLOBAL("auto_scheduler.WriteMeasureRecords")
.set_body_typed([](MeasureInput inp, MeasureResult res) {
auto inps = Array<MeasureInput>({inp});
auto ress = Array<MeasureResult>({res});
std::ostringstream ss;
WriteMeasureRecords(&ss, inps, ress);
return String(ss.str());
});
TVM_REGISTER_GLOBAL("auto_scheduler.SaveRecords")
.set_body_typed([](String filename, Array<MeasureInput> in, Array<MeasureResult> res) {
std::ofstream ofs(filename, std::ofstream::app);
WriteMeasureRecords(&ofs, in, res);
});
TVM_REGISTER_GLOBAL("auto_scheduler.SerializeMeasureInput")
.set_body_typed([](const MeasureInput& input) {
std::ostringstream os;
dmlc::JSONWriter writer(&os);
writer.Write(*input.get());
return os.str();
});
TVM_REGISTER_GLOBAL("auto_scheduler.DeserializeMeasureInput").set_body_typed([](String json) {
std::istringstream ss(json);
dmlc::JSONReader reader(&ss);
auto inp = make_object<MeasureInputNode>();
reader.Read(inp.get());
return ObjectRef(inp);
});
TVM_REGISTER_GLOBAL("auto_scheduler.SerializeSearchTask")
.set_body_typed([](const SearchTask& search_task) {
std::ostringstream os;
dmlc::JSONWriter writer(&os);
writer.Write(*search_task.get());
return os.str();
});
TVM_REGISTER_GLOBAL("auto_scheduler.DeserializeSearchTask").set_body_typed([](String json) {
std::istringstream ss(json);
dmlc::JSONReader reader(&ss);
auto search_task = make_object<SearchTaskNode>();
reader.Read(search_task.get());
return ObjectRef(search_task);
});
} // namespace auto_scheduler
} // namespace tvm