tvm
Loading...
Searching...
No Matches
database.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19#ifndef TVM_S_TIR_META_SCHEDULE_DATABASE_H_
20#define TVM_S_TIR_META_SCHEDULE_DATABASE_H_
21
22#include <tvm/ffi/container/array.h>
23#include <tvm/ffi/function.h>
24#include <tvm/ffi/reflection/registry.h>
25#include <tvm/ffi/string.h>
26#include <tvm/ir/expr.h>
27#include <tvm/ir/module.h>
28#include <tvm/ir/prim/expr.h>
32#include <tvm/target/target.h>
33
34#include <filesystem>
35#include <memory>
36
37namespace tvm {
38namespace s_tir {
39namespace meta_schedule {
40
41class ModuleEquality;
42
44class WorkloadObj : public ffi::Object {
45 public:
47 using THashCode = size_t;
52
53 static void RegisterReflection() {
54 namespace refl = tvm::ffi::reflection;
55 refl::ObjectDef<WorkloadObj>().def_ro("mod", &WorkloadObj::mod);
56 }
57 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.Workload", WorkloadObj, ffi::Object);
58
63 ffi::ObjectRef AsJSON() const;
64};
65
70class Workload : public ffi::ObjectRef {
71 public:
73 explicit Workload(ffi::UnsafeInit tag, ffi::ObjectPtr<WorkloadObj> data) : ffi::ObjectRef(tag) {
74 data_ = std::move(data);
75 }
80 TVM_DLL explicit Workload(IRModule mod);
86 TVM_DLL explicit Workload(IRModule mod, THashCode shash);
92 TVM_DLL static Workload FromJSON(const ffi::ObjectRef& json_obj);
93
95};
96
99 size_t operator()(const Workload& a) const { return a->shash; }
100};
101
104 explicit WorkloadEqual(const ModuleEquality& mod_eq) : mod_eq_(mod_eq) {}
105
106 bool operator()(const Workload& a, const Workload& b) const;
107
108 private:
110 const ModuleEquality& mod_eq_;
111};
112
114class MeasureCandidate;
115
117class TuningRecordObj : public ffi::Object {
118 public:
122 Workload workload{ffi::UnsafeInit()};
124 ffi::Optional<ffi::Array<FloatImm>> run_secs;
126 ffi::Optional<Target> target;
128 ffi::Optional<ffi::Array<ArgInfo>> args_info;
129
130 static void RegisterReflection() {
131 namespace refl = tvm::ffi::reflection;
132 refl::ObjectDef<TuningRecordObj>()
133 .def_ro("trace", &TuningRecordObj::trace)
134 .def_ro("workload", &TuningRecordObj::workload)
135 .def_ro("run_secs", &TuningRecordObj::run_secs)
136 .def_ro("target", &TuningRecordObj::target)
137 .def_ro("args_info", &TuningRecordObj::args_info);
138 }
139 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.TuningRecord", TuningRecordObj,
140 ffi::Object);
141
150 ffi::ObjectRef AsJSON() const;
155 bool IsValid() const;
156};
157
162class TuningRecord : public ffi::ObjectRef {
163 public:
172 TVM_DLL explicit TuningRecord(s_tir::Trace trace, Workload workload,
173 ffi::Optional<ffi::Array<FloatImm>> run_secs,
174 ffi::Optional<Target> target,
175 ffi::Optional<ffi::Array<ArgInfo>> args_info);
182 TVM_DLL static TuningRecord FromJSON(const ffi::ObjectRef& json_obj, const Workload& workload);
184};
185
186class Database;
187
188/* \brief The abstract interface of database. */
189class DatabaseObj : public ffi::Object {
190 public:
203 explicit DatabaseObj(ffi::String mod_eq_name = "structural");
204
206 virtual ~DatabaseObj();
212 virtual bool HasWorkload(const IRModule& mod) = 0;
218 virtual Workload CommitWorkload(const IRModule& mod) = 0;
223 virtual void CommitTuningRecord(const TuningRecord& record) = 0;
230 virtual ffi::Array<TuningRecord> GetTopK(const Workload& workload, int top_k) = 0;
235 virtual ffi::Array<TuningRecord> GetAllTuningRecords() = 0;
240 virtual int64_t Size() = 0;
248 virtual ffi::Optional<TuningRecord> QueryTuningRecord(const IRModule& mod, const Target& target,
249 const ffi::String& workload_name);
257 virtual ffi::Optional<s_tir::Schedule> QuerySchedule(const IRModule& mod, const Target& target,
258 const ffi::String& workload_name);
266 virtual ffi::Optional<IRModule> QueryIRModule(const IRModule& mod, const Target& target,
267 const ffi::String& workload_name);
272 void DumpPruned(Database destination);
274 const ModuleEquality& GetModuleEquality() const {
275 TVM_FFI_ICHECK(mod_eq_);
276 return *mod_eq_;
277 }
278
279 static constexpr const bool _type_mutable = true;
280 TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.Database", DatabaseObj, ffi::Object);
281
282 private:
284 std::unique_ptr<ModuleEquality> mod_eq_;
285};
286
289 public:
302 explicit PyDatabaseObj(ffi::String mod_eq_name = "structural");
303
309 using FHasWorkload = ffi::TypedFunction<bool(const IRModule&)>;
315 using FCommitWorkload = ffi::TypedFunction<Workload(const IRModule&)>;
320 using FCommitTuningRecord = ffi::TypedFunction<void(const TuningRecord&)>;
327 using FGetTopK = ffi::TypedFunction<ffi::Array<TuningRecord>(const Workload&, int)>;
332 using FGetAllTuningRecords = ffi::TypedFunction<ffi::Array<TuningRecord>()>;
340 using FQueryTuningRecord = ffi::TypedFunction<ffi::Optional<TuningRecord>(
341 const IRModule&, const Target&, const ffi::String&)>;
349 using FQuerySchedule = ffi::TypedFunction<ffi::Optional<s_tir::Schedule>(
350 const IRModule&, const Target&, const ffi::String&)>;
358 using FQueryIRModule = ffi::TypedFunction<ffi::Optional<IRModule>(const IRModule&, const Target&,
359 const ffi::String&)>;
364 using FSize = ffi::TypedFunction<int64_t()>;
365
384
385 static void RegisterReflection() {
386 // ffi::Functions are all not registered, because the reflection system doesn't take care of
387 // them, so it cannot be accessible on the python side. If there is such need from the future,
388 // we can then add corresponding accessor methods to help access on python.
389 // `f_has_workload` is not registered
390 // `f_commit_workload` is not registered
391 // `f_commit_tuning_record` is not registered
392 // `f_get_top_k` is not registered
393 // `f_get_all_tuning_records` is not registered
394 // `f_query_tuning_record` is not registered
395 // `f_query_schedule` is not registered
396 // `f_query_ir_module` is not registered
397 // `f_size` is not registered
398 namespace refl = tvm::ffi::reflection;
399 refl::ObjectDef<PyDatabaseObj>();
400 }
401
402 bool HasWorkload(const IRModule& mod) final {
403 TVM_FFI_ICHECK(f_has_workload != nullptr) << "PyDatabase's HasWorkload method not implemented!";
404 return f_has_workload(mod);
405 }
406
407 Workload CommitWorkload(const IRModule& mod) final {
408 TVM_FFI_ICHECK(f_commit_workload != nullptr)
409 << "PyDatabase's CommitWorkload method not implemented!";
410 return f_commit_workload(mod);
411 }
412
413 void CommitTuningRecord(const TuningRecord& record) final {
414 TVM_FFI_ICHECK(f_commit_tuning_record != nullptr)
415 << "PyDatabase's CommitTuningRecord method not implemented!";
417 }
418
419 ffi::Array<TuningRecord> GetTopK(const Workload& workload, int top_k) final {
420 TVM_FFI_ICHECK(f_get_top_k != nullptr) << "PyDatabase's GetTopK method not implemented!";
421 return f_get_top_k(workload, top_k);
422 }
423
424 ffi::Array<TuningRecord> GetAllTuningRecords() final {
425 TVM_FFI_ICHECK(f_get_all_tuning_records != nullptr)
426 << "PyDatabase's GetAllTuningRecords method not implemented!";
428 }
429
430 ffi::Optional<TuningRecord> QueryTuningRecord(const IRModule& mod, const Target& target,
431 const ffi::String& workload_name) final {
432 if (f_query_tuning_record == nullptr) {
433 return DatabaseObj::QueryTuningRecord(mod, target, workload_name);
434 } else {
435 return f_query_tuning_record(mod, target, workload_name);
436 }
437 }
438
439 ffi::Optional<s_tir::Schedule> QuerySchedule(const IRModule& mod, const Target& target,
440 const ffi::String& workload_name) final {
441 if (f_query_schedule == nullptr) {
442 return DatabaseObj::QuerySchedule(mod, target, workload_name);
443 } else {
444 return f_query_schedule(mod, target, workload_name);
445 }
446 }
447
448 ffi::Optional<IRModule> QueryIRModule(const IRModule& mod, const Target& target,
449 const ffi::String& workload_name) final {
450 if (f_query_ir_module == nullptr) {
451 return DatabaseObj::QueryIRModule(mod, target, workload_name);
452 } else {
453 return f_query_ir_module(mod, target, workload_name);
454 }
455 }
456
457 int64_t Size() final {
458 TVM_FFI_ICHECK(f_size != nullptr) << "PyDatabase's Size method not implemented!";
459 return f_size();
460 }
461
462 static constexpr const bool _type_mutable = true;
464};
465
470class Database : public ffi::ObjectRef {
471 public:
476 explicit Database(ffi::ObjectPtr<DatabaseObj> data) : ffi::ObjectRef(data) {
477 TVM_FFI_ICHECK(data != nullptr);
478 }
483 TVM_DLL static Database MemoryDatabase(ffi::String mod_eq_name = "structural");
490 TVM_DLL static Database ScheduleFnDatabase(ffi::TypedFunction<bool(s_tir::Schedule)> schedule_fn,
491 ffi::String mod_eq_name = "structural");
499 TVM_DLL static Database JSONDatabase(ffi::String path_workload, ffi::String path_tuning_record,
500 bool allow_missing, ffi::String mod_eq_name = "structural");
508 TVM_DLL static Database UnionDatabase(ffi::Array<Database, void> databases);
516 TVM_DLL static Database OrderedUnionDatabase(ffi::Array<Database, void> databases);
531 TVM_DLL static Database PyDatabase(PyDatabaseObj::FHasWorkload f_has_workload,
532 PyDatabaseObj::FCommitWorkload f_commit_workload,
533 PyDatabaseObj::FCommitTuningRecord f_commit_tuning_record,
534 PyDatabaseObj::FGetTopK f_get_top_k,
535 PyDatabaseObj::FGetAllTuningRecords f_get_all_tuning_records,
536 PyDatabaseObj::FQueryTuningRecord f_query_tuning_record,
537 PyDatabaseObj::FQuerySchedule f_query_schedule,
538 PyDatabaseObj::FQueryIRModule f_query_ir_module,
540 ffi::String mod_eq_name = "structural");
542 static ffi::Optional<Database> Current();
547
549};
550
551namespace attr {
552constexpr const char* kTaskName = "task_name";
553} // namespace attr
554
555} // namespace meta_schedule
556} // namespace s_tir
557} // namespace tvm
558
559#endif // TVM_S_TIR_META_SCHEDULE_DATABASE_H_
Managed reference class to IRModuleNode.
Definition module.h:255
Managed reference class to TargetObj.
Definition target.h:136
Managed reference to ScheduleObj.
Definition schedule.h:887
Managed reference to TraceNode.
Definition trace.h:145
virtual ~DatabaseObj()
Default destructor.
virtual Workload CommitWorkload(const IRModule &mod)=0
Look up or add workload to the database if missing.
TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.Database", DatabaseObj, ffi::Object)
virtual ffi::Optional< s_tir::Schedule > QuerySchedule(const IRModule &mod, const Target &target, const ffi::String &workload_name)
Query the best schedule of the given workload from the database.
virtual ffi::Optional< IRModule > QueryIRModule(const IRModule &mod, const Target &target, const ffi::String &workload_name)
Query the best IRModule of the given workload from the database.
virtual int64_t Size()=0
Get the size of the database.
DatabaseObj(ffi::String mod_eq_name="structural")
Constructor.
virtual ffi::Array< TuningRecord > GetAllTuningRecords()=0
Get all tuning records from the database.
virtual ffi::Array< TuningRecord > GetTopK(const Workload &workload, int top_k)=0
Get the top K valid tuning records of given workload from the database.
const ModuleEquality & GetModuleEquality() const
Return a reference to the owned module equality method instance.
Definition database.h:274
virtual ffi::Optional< TuningRecord > QueryTuningRecord(const IRModule &mod, const Target &target, const ffi::String &workload_name)
Query the best record of the given workload from the database.
static constexpr const bool _type_mutable
Definition database.h:279
virtual void CommitTuningRecord(const TuningRecord &record)=0
Add a tuning record to the database.
virtual bool HasWorkload(const IRModule &mod)=0
Check if the database has the given workload.
void DumpPruned(Database destination)
Prune the database and dump it a given database.
Managed reference to DatabaseObj.
Definition database.h:470
void EnterWithScope()
Entering the scope of the context manager.
static Database ScheduleFnDatabase(ffi::TypedFunction< bool(s_tir::Schedule)> schedule_fn, ffi::String mod_eq_name="structural")
A database for injecting handcrafted schedule functions.
static Database JSONDatabase(ffi::String path_workload, ffi::String path_tuning_record, bool allow_missing, ffi::String mod_eq_name="structural")
Create a default database that uses JSON file for tuning records.
static Database MemoryDatabase(ffi::String mod_eq_name="structural")
An in-memory database.
Database(ffi::ObjectPtr< DatabaseObj > data)
Constructor from ffi::ObjectPtr<DatabaseObj>.
Definition database.h:476
static Database PyDatabase(PyDatabaseObj::FHasWorkload f_has_workload, PyDatabaseObj::FCommitWorkload f_commit_workload, PyDatabaseObj::FCommitTuningRecord f_commit_tuning_record, PyDatabaseObj::FGetTopK f_get_top_k, PyDatabaseObj::FGetAllTuningRecords f_get_all_tuning_records, PyDatabaseObj::FQueryTuningRecord f_query_tuning_record, PyDatabaseObj::FQuerySchedule f_query_schedule, PyDatabaseObj::FQueryIRModule f_query_ir_module, PyDatabaseObj::FSize f_size, ffi::String mod_eq_name="structural")
Create a database with customized methods on the python-side.
static Database OrderedUnionDatabase(ffi::Array< Database, void > databases)
A database composed of multiple databases, allowing users to guide IR rewriting using combined knowle...
static Database UnionDatabase(ffi::Array< Database, void > databases)
A database composed of multiple databases, allowing users to guide IR rewriting using combined knowle...
void ExitWithScope()
Exiting the scope of the context manager.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Database, ffi::ObjectRef, DatabaseObj)
static ffi::Optional< Database > Current()
Managed reference to MeasureCandidateObj.
Definition measure_candidate.h:59
The database with customized methods on the python-side.
Definition database.h:288
ffi::Array< TuningRecord > GetAllTuningRecords() final
Get all tuning records from the database.
Definition database.h:424
FHasWorkload f_has_workload
The packed function to the HasWorkload function.
Definition database.h:367
ffi::TypedFunction< ffi::Optional< TuningRecord >(const IRModule &, const Target &, const ffi::String &)> FQueryTuningRecord
The function type of QueryTuningRecord method.
Definition database.h:341
static void RegisterReflection()
Definition database.h:385
FQueryIRModule f_query_ir_module
The packed function to the QueryIRModule function.
Definition database.h:381
int64_t Size() final
Get the size of the database.
Definition database.h:457
ffi::TypedFunction< Workload(const IRModule &)> FCommitWorkload
The function type of CommitWorkload method.
Definition database.h:315
static constexpr const bool _type_mutable
Definition database.h:462
FSize f_size
The packed function to the Size function.
Definition database.h:383
ffi::TypedFunction< ffi::Optional< IRModule >(const IRModule &, const Target &, const ffi::String &)> FQueryIRModule
The function type of QueryIRModule method.
Definition database.h:359
FCommitTuningRecord f_commit_tuning_record
The packed function to the CommitTuningRecord function.
Definition database.h:371
FCommitWorkload f_commit_workload
The packed function to the CommitWorkload function.
Definition database.h:369
FGetAllTuningRecords f_get_all_tuning_records
The packed function to the GetAllTuningRecords function.
Definition database.h:375
ffi::TypedFunction< int64_t()> FSize
The function type of Size method.
Definition database.h:364
bool HasWorkload(const IRModule &mod) final
Check if the database has the given workload.
Definition database.h:402
ffi::TypedFunction< bool(const IRModule &)> FHasWorkload
The function type of HasWorkload method.
Definition database.h:309
ffi::Optional< IRModule > QueryIRModule(const IRModule &mod, const Target &target, const ffi::String &workload_name) final
Query the best IRModule of the given workload from the database.
Definition database.h:448
ffi::Optional< s_tir::Schedule > QuerySchedule(const IRModule &mod, const Target &target, const ffi::String &workload_name) final
Query the best schedule of the given workload from the database.
Definition database.h:439
FQuerySchedule f_query_schedule
The packed function to the QuerySchedule function.
Definition database.h:379
ffi::Optional< TuningRecord > QueryTuningRecord(const IRModule &mod, const Target &target, const ffi::String &workload_name) final
Query the best record of the given workload from the database.
Definition database.h:430
ffi::TypedFunction< void(const TuningRecord &)> FCommitTuningRecord
The function type of CommitTuningRecord method.
Definition database.h:320
ffi::TypedFunction< ffi::Optional< s_tir::Schedule >(const IRModule &, const Target &, const ffi::String &)> FQuerySchedule
The function type of QuerySchedule method.
Definition database.h:350
ffi::Array< TuningRecord > GetTopK(const Workload &workload, int top_k) final
Get the top K valid tuning records of given workload from the database.
Definition database.h:419
FQueryTuningRecord f_query_tuning_record
The packed function to the QueryTuningRecord function.
Definition database.h:377
void CommitTuningRecord(const TuningRecord &record) final
Add a tuning record to the database.
Definition database.h:413
Workload CommitWorkload(const IRModule &mod) final
Look up or add workload to the database if missing.
Definition database.h:407
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.PyDatabase", PyDatabaseObj, DatabaseObj)
ffi::TypedFunction< ffi::Array< TuningRecord >()> FGetAllTuningRecords
The function type of GetAllTuningRecords method.
Definition database.h:332
PyDatabaseObj(ffi::String mod_eq_name="structural")
Constructor.
ffi::TypedFunction< ffi::Array< TuningRecord >(const Workload &, int)> FGetTopK
The function type of GetTopK method.
Definition database.h:327
FGetTopK f_get_top_k
The packed function to the GetTopK function.
Definition database.h:373
The class of tuning records.
Definition database.h:117
ffi::Optional< ffi::Array< ArgInfo > > args_info
The argument information.
Definition database.h:128
ffi::Optional< ffi::Array< FloatImm > > run_secs
The profiling result in seconds.
Definition database.h:124
ffi::Optional< Target > target
The target for tuning.
Definition database.h:126
ffi::ObjectRef AsJSON() const
Export the tuning record to a JSON string.
MeasureCandidate AsMeasureCandidate() const
Construct the measure candidate given the initial IR module and trace stored in the tuning record.
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.TuningRecord", TuningRecordObj, ffi::Object)
static void RegisterReflection()
Definition database.h:130
Workload workload
The workload.
Definition database.h:122
bool IsValid() const
Check if this tuning record has valid trace instructions and successful run results.
s_tir::Trace trace
The trace tuned.
Definition database.h:120
The managed reference of TuningRecordObj.
Definition database.h:162
static TuningRecord FromJSON(const ffi::ObjectRef &json_obj, const Workload &workload)
Create a tuning record from a json object.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TuningRecord, ffi::ObjectRef, TuningRecordObj)
TuningRecord(s_tir::Trace trace, Workload workload, ffi::Optional< ffi::Array< FloatImm > > run_secs, ffi::Optional< Target > target, ffi::Optional< ffi::Array< ArgInfo > > args_info)
Constructor of a tuning record.
A workload, i.e. an IRModule and its structural hash.
Definition database.h:44
static void RegisterReflection()
Definition database.h:53
THashCode shash
The workload's structural hash.
Definition database.h:51
size_t THashCode
The type of structural hash.
Definition database.h:47
IRModule mod
The workload's IRModule.
Definition database.h:49
ffi::ObjectRef AsJSON() const
Export the workload to a JSON string.
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.Workload", WorkloadObj, ffi::Object)
Managed reference to WorkloadObj.
Definition database.h:70
WorkloadObj::THashCode THashCode
Definition database.h:72
Workload(IRModule mod, THashCode shash)
Constructor of Workload.
Workload(ffi::UnsafeInit tag, ffi::ObjectPtr< WorkloadObj > data)
Definition database.h:73
static Workload FromJSON(const ffi::ObjectRef &json_obj)
Create a workload from a json object.
Workload(IRModule mod)
Constructor of Workload.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Workload, ffi::ObjectRef, WorkloadObj)
Base expr nodes in TVM.
TIR expressions.
IRModule that holds the functions and type definitions.
constexpr const char * kTaskName
Definition database.h:552
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
The equality check for Workload.
Definition database.h:103
bool operator()(const Workload &a, const Workload &b) const
WorkloadEqual(const ModuleEquality &mod_eq)
Definition database.h:104
The hash method for Workload.
Definition database.h:98
size_t operator()(const Workload &a) const
Definition database.h:99
Compilation target object.