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/base.h>
23#include <tvm/ffi/container/array.h>
24#include <tvm/ffi/function.h>
25#include <tvm/ffi/reflection/registry.h>
26#include <tvm/ffi/string.h>
27#include <tvm/ir/expr.h>
28#include <tvm/ir/module.h>
29#include <tvm/ir/prim/expr.h>
33#include <tvm/target/target.h>
34
35#include <filesystem>
36#include <memory>
37
38namespace tvm {
39namespace s_tir {
40namespace meta_schedule {
41
42class ModuleEquality;
43
45class WorkloadObj : public ffi::Object {
46 public:
48 using THashCode = size_t;
53
54 static void RegisterReflection() {
55 namespace refl = tvm::ffi::reflection;
56 refl::ObjectDef<WorkloadObj>().def_ro("mod", &WorkloadObj::mod);
57 }
58 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.Workload", WorkloadObj, ffi::Object);
59
64 ffi::ObjectRef AsJSON() const;
65};
66
71class Workload : public ffi::ObjectRef {
72 public:
74 explicit Workload(ffi::UnsafeInit tag, ffi::ObjectPtr<WorkloadObj> data) : ffi::ObjectRef(tag) {
75 data_ = std::move(data);
76 }
81 TVM_DLL explicit Workload(IRModule mod);
87 TVM_DLL explicit Workload(IRModule mod, THashCode shash);
93 TVM_DLL static Workload FromJSON(const ffi::ObjectRef& json_obj);
94
96};
97
100 size_t operator()(const Workload& a) const { return a->shash; }
101};
102
105 explicit WorkloadEqual(const ModuleEquality& mod_eq) : mod_eq_(mod_eq) {}
106
107 bool operator()(const Workload& a, const Workload& b) const;
108
109 private:
111 const ModuleEquality& mod_eq_;
112};
113
115class MeasureCandidate;
116
118class TuningRecordObj : public ffi::Object {
119 public:
123 Workload workload{ffi::UnsafeInit()};
125 ffi::Optional<ffi::Array<FloatImm>> run_secs;
127 ffi::Optional<Target> target;
129 ffi::Optional<ffi::Array<ArgInfo>> args_info;
130
131 static void RegisterReflection() {
132 namespace refl = tvm::ffi::reflection;
133 refl::ObjectDef<TuningRecordObj>()
134 .def_ro("trace", &TuningRecordObj::trace)
135 .def_ro("workload", &TuningRecordObj::workload)
136 .def_ro("run_secs", &TuningRecordObj::run_secs)
137 .def_ro("target", &TuningRecordObj::target)
138 .def_ro("args_info", &TuningRecordObj::args_info);
139 }
140 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.TuningRecord", TuningRecordObj,
141 ffi::Object);
142
151 ffi::ObjectRef AsJSON() const;
156 bool IsValid() const;
157};
158
163class TuningRecord : public ffi::ObjectRef {
164 public:
173 TVM_DLL explicit TuningRecord(s_tir::Trace trace, Workload workload,
174 ffi::Optional<ffi::Array<FloatImm>> run_secs,
175 ffi::Optional<Target> target,
176 ffi::Optional<ffi::Array<ArgInfo>> args_info);
183 TVM_DLL static TuningRecord FromJSON(const ffi::ObjectRef& json_obj, const Workload& workload);
185};
186
187class Database;
188
189/* \brief The abstract interface of database. */
190class DatabaseObj : public ffi::Object {
191 public:
204 explicit DatabaseObj(ffi::String mod_eq_name = "structural");
205
207 virtual ~DatabaseObj();
213 virtual bool HasWorkload(const IRModule& mod) = 0;
219 virtual Workload CommitWorkload(const IRModule& mod) = 0;
224 virtual void CommitTuningRecord(const TuningRecord& record) = 0;
231 virtual ffi::Array<TuningRecord> GetTopK(const Workload& workload, int top_k) = 0;
236 virtual ffi::Array<TuningRecord> GetAllTuningRecords() = 0;
241 virtual int64_t Size() = 0;
249 virtual ffi::Optional<TuningRecord> QueryTuningRecord(const IRModule& mod, const Target& target,
250 const ffi::String& workload_name);
258 virtual ffi::Optional<s_tir::Schedule> QuerySchedule(const IRModule& mod, const Target& target,
259 const ffi::String& workload_name);
267 virtual ffi::Optional<IRModule> QueryIRModule(const IRModule& mod, const Target& target,
268 const ffi::String& workload_name);
273 void DumpPruned(Database destination);
275 const ModuleEquality& GetModuleEquality() const {
276 TVM_FFI_ICHECK(mod_eq_);
277 return *mod_eq_;
278 }
279
280 static constexpr const bool _type_mutable = true;
281 TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.Database", DatabaseObj, ffi::Object);
282
283 private:
285 std::unique_ptr<ModuleEquality> mod_eq_;
286};
287
290 public:
303 explicit PyDatabaseObj(ffi::String mod_eq_name = "structural");
304
310 using FHasWorkload = ffi::TypedFunction<bool(const IRModule&)>;
316 using FCommitWorkload = ffi::TypedFunction<Workload(const IRModule&)>;
321 using FCommitTuningRecord = ffi::TypedFunction<void(const TuningRecord&)>;
328 using FGetTopK = ffi::TypedFunction<ffi::Array<TuningRecord>(const Workload&, int)>;
333 using FGetAllTuningRecords = ffi::TypedFunction<ffi::Array<TuningRecord>()>;
341 using FQueryTuningRecord = ffi::TypedFunction<ffi::Optional<TuningRecord>(
342 const IRModule&, const Target&, const ffi::String&)>;
350 using FQuerySchedule = ffi::TypedFunction<ffi::Optional<s_tir::Schedule>(
351 const IRModule&, const Target&, const ffi::String&)>;
359 using FQueryIRModule = ffi::TypedFunction<ffi::Optional<IRModule>(const IRModule&, const Target&,
360 const ffi::String&)>;
365 using FSize = ffi::TypedFunction<int64_t()>;
366
385
386 static void RegisterReflection() {
387 // ffi::Functions are all not registered, because the reflection system doesn't take care of
388 // them, so it cannot be accessible on the python side. If there is such need from the future,
389 // we can then add corresponding accessor methods to help access on python.
390 // `f_has_workload` is not registered
391 // `f_commit_workload` is not registered
392 // `f_commit_tuning_record` is not registered
393 // `f_get_top_k` is not registered
394 // `f_get_all_tuning_records` is not registered
395 // `f_query_tuning_record` is not registered
396 // `f_query_schedule` is not registered
397 // `f_query_ir_module` is not registered
398 // `f_size` is not registered
399 namespace refl = tvm::ffi::reflection;
400 refl::ObjectDef<PyDatabaseObj>();
401 }
402
403 bool HasWorkload(const IRModule& mod) final {
404 TVM_FFI_ICHECK(f_has_workload != nullptr) << "PyDatabase's HasWorkload method not implemented!";
405 return f_has_workload(mod);
406 }
407
408 Workload CommitWorkload(const IRModule& mod) final {
409 TVM_FFI_ICHECK(f_commit_workload != nullptr)
410 << "PyDatabase's CommitWorkload method not implemented!";
411 return f_commit_workload(mod);
412 }
413
414 void CommitTuningRecord(const TuningRecord& record) final {
415 TVM_FFI_ICHECK(f_commit_tuning_record != nullptr)
416 << "PyDatabase's CommitTuningRecord method not implemented!";
418 }
419
420 ffi::Array<TuningRecord> GetTopK(const Workload& workload, int top_k) final {
421 TVM_FFI_ICHECK(f_get_top_k != nullptr) << "PyDatabase's GetTopK method not implemented!";
422 return f_get_top_k(workload, top_k);
423 }
424
425 ffi::Array<TuningRecord> GetAllTuningRecords() final {
426 TVM_FFI_ICHECK(f_get_all_tuning_records != nullptr)
427 << "PyDatabase's GetAllTuningRecords method not implemented!";
429 }
430
431 ffi::Optional<TuningRecord> QueryTuningRecord(const IRModule& mod, const Target& target,
432 const ffi::String& workload_name) final {
433 if (f_query_tuning_record == nullptr) {
434 return DatabaseObj::QueryTuningRecord(mod, target, workload_name);
435 } else {
436 return f_query_tuning_record(mod, target, workload_name);
437 }
438 }
439
440 ffi::Optional<s_tir::Schedule> QuerySchedule(const IRModule& mod, const Target& target,
441 const ffi::String& workload_name) final {
442 if (f_query_schedule == nullptr) {
443 return DatabaseObj::QuerySchedule(mod, target, workload_name);
444 } else {
445 return f_query_schedule(mod, target, workload_name);
446 }
447 }
448
449 ffi::Optional<IRModule> QueryIRModule(const IRModule& mod, const Target& target,
450 const ffi::String& workload_name) final {
451 if (f_query_ir_module == nullptr) {
452 return DatabaseObj::QueryIRModule(mod, target, workload_name);
453 } else {
454 return f_query_ir_module(mod, target, workload_name);
455 }
456 }
457
458 int64_t Size() final {
459 TVM_FFI_ICHECK(f_size != nullptr) << "PyDatabase's Size method not implemented!";
460 return f_size();
461 }
462
463 static constexpr const bool _type_mutable = true;
465};
466
471class Database : public ffi::ObjectRef {
472 public:
477 explicit Database(ffi::ObjectPtr<DatabaseObj> data) : ffi::ObjectRef(data) {
478 TVM_FFI_ICHECK(data != nullptr);
479 }
484 TVM_DLL static Database MemoryDatabase(ffi::String mod_eq_name = "structural");
491 TVM_DLL static Database ScheduleFnDatabase(ffi::TypedFunction<bool(s_tir::Schedule)> schedule_fn,
492 ffi::String mod_eq_name = "structural");
500 TVM_DLL static Database JSONDatabase(ffi::String path_workload, ffi::String path_tuning_record,
501 bool allow_missing, ffi::String mod_eq_name = "structural");
509 TVM_DLL static Database UnionDatabase(ffi::Array<Database, void> databases);
517 TVM_DLL static Database OrderedUnionDatabase(ffi::Array<Database, void> databases);
532 TVM_DLL static Database PyDatabase(PyDatabaseObj::FHasWorkload f_has_workload,
533 PyDatabaseObj::FCommitWorkload f_commit_workload,
534 PyDatabaseObj::FCommitTuningRecord f_commit_tuning_record,
535 PyDatabaseObj::FGetTopK f_get_top_k,
536 PyDatabaseObj::FGetAllTuningRecords f_get_all_tuning_records,
537 PyDatabaseObj::FQueryTuningRecord f_query_tuning_record,
538 PyDatabaseObj::FQuerySchedule f_query_schedule,
539 PyDatabaseObj::FQueryIRModule f_query_ir_module,
541 ffi::String mod_eq_name = "structural");
543 static ffi::Optional<Database> Current();
548
550};
551
552namespace attr {
553inline constexpr ffi::reflection::AttrKey<ffi::String> kTaskName{"task_name"};
554} // namespace attr
555
556} // namespace meta_schedule
557} // namespace s_tir
558} // namespace tvm
559
560#endif // TVM_S_TIR_META_SCHEDULE_DATABASE_H_
Base expr nodes in TVM.
TIR expressions.
Managed reference class to IRModuleObj.
Definition module.h:292
Managed reference class to TargetObj.
Definition target.h:155
Managed reference to ScheduleObj.
Definition schedule.h:888
Managed reference to TraceObj.
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:275
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:280
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:471
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:477
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:60
The database with customized methods on the python-side.
Definition database.h:289
ffi::Array< TuningRecord > GetAllTuningRecords() final
Get all tuning records from the database.
Definition database.h:425
FHasWorkload f_has_workload
The packed function to the HasWorkload function.
Definition database.h:368
ffi::TypedFunction< ffi::Optional< TuningRecord >(const IRModule &, const Target &, const ffi::String &)> FQueryTuningRecord
The function type of QueryTuningRecord method.
Definition database.h:342
static void RegisterReflection()
Definition database.h:386
FQueryIRModule f_query_ir_module
The packed function to the QueryIRModule function.
Definition database.h:382
int64_t Size() final
Get the size of the database.
Definition database.h:458
ffi::TypedFunction< Workload(const IRModule &)> FCommitWorkload
The function type of CommitWorkload method.
Definition database.h:316
static constexpr const bool _type_mutable
Definition database.h:463
FSize f_size
The packed function to the Size function.
Definition database.h:384
ffi::TypedFunction< ffi::Optional< IRModule >(const IRModule &, const Target &, const ffi::String &)> FQueryIRModule
The function type of QueryIRModule method.
Definition database.h:360
FCommitTuningRecord f_commit_tuning_record
The packed function to the CommitTuningRecord function.
Definition database.h:372
FCommitWorkload f_commit_workload
The packed function to the CommitWorkload function.
Definition database.h:370
FGetAllTuningRecords f_get_all_tuning_records
The packed function to the GetAllTuningRecords function.
Definition database.h:376
ffi::TypedFunction< int64_t()> FSize
The function type of Size method.
Definition database.h:365
bool HasWorkload(const IRModule &mod) final
Check if the database has the given workload.
Definition database.h:403
ffi::TypedFunction< bool(const IRModule &)> FHasWorkload
The function type of HasWorkload method.
Definition database.h:310
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:449
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:440
FQuerySchedule f_query_schedule
The packed function to the QuerySchedule function.
Definition database.h:380
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:431
ffi::TypedFunction< void(const TuningRecord &)> FCommitTuningRecord
The function type of CommitTuningRecord method.
Definition database.h:321
ffi::TypedFunction< ffi::Optional< s_tir::Schedule >(const IRModule &, const Target &, const ffi::String &)> FQuerySchedule
The function type of QuerySchedule method.
Definition database.h:351
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:420
FQueryTuningRecord f_query_tuning_record
The packed function to the QueryTuningRecord function.
Definition database.h:378
void CommitTuningRecord(const TuningRecord &record) final
Add a tuning record to the database.
Definition database.h:414
Workload CommitWorkload(const IRModule &mod) final
Look up or add workload to the database if missing.
Definition database.h:408
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:333
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:328
FGetTopK f_get_top_k
The packed function to the GetTopK function.
Definition database.h:374
The class of tuning records.
Definition database.h:118
ffi::Optional< ffi::Array< ArgInfo > > args_info
The argument information.
Definition database.h:129
ffi::Optional< ffi::Array< FloatImm > > run_secs
The profiling result in seconds.
Definition database.h:125
ffi::Optional< Target > target
The target for tuning.
Definition database.h:127
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:131
Workload workload
The workload.
Definition database.h:123
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:121
The managed reference of TuningRecordObj.
Definition database.h:163
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:45
static void RegisterReflection()
Definition database.h:54
THashCode shash
The workload's structural hash.
Definition database.h:52
size_t THashCode
The type of structural hash.
Definition database.h:48
IRModule mod
The workload's IRModule.
Definition database.h:50
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:71
WorkloadObj::THashCode THashCode
Definition database.h:73
Workload(IRModule mod, THashCode shash)
Constructor of Workload.
Workload(ffi::UnsafeInit tag, ffi::ObjectPtr< WorkloadObj > data)
Definition database.h:74
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)
Parent compiler symbol visibility.
IRModule that holds the functions and type definitions.
constexpr ffi::reflection::AttrKey< ffi::String > kTaskName
Definition database.h:553
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
The equality check for Workload.
Definition database.h:104
bool operator()(const Workload &a, const Workload &b) const
WorkloadEqual(const ModuleEquality &mod_eq)
Definition database.h:105
The hash method for Workload.
Definition database.h:99
size_t operator()(const Workload &a) const
Definition database.h:100
Compilation target object.