tvm
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_META_SCHEDULE_DATABASE_H_
20 #define TVM_META_SCHEDULE_DATABASE_H_
21 
22 #include <tvm/ir/expr.h>
23 #include <tvm/ir/module.h>
25 #include <tvm/node/reflection.h>
28 #include <tvm/runtime/object.h>
30 #include <tvm/target/target.h>
31 #include <tvm/tir/schedule/trace.h>
32 
33 namespace tvm {
34 namespace meta_schedule {
35 
37 class WorkloadNode : public runtime::Object {
38  public:
40  using THashCode = size_t;
45 
47  v->Visit("mod", &mod);
48  // `shash` is not visited because TVM FFI doesn't support uint64_t
49  }
50 
51  static constexpr const char* _type_key = "meta_schedule.Workload";
53 
58  ObjectRef AsJSON() const;
59 };
60 
65 class Workload : public runtime::ObjectRef {
66  public:
72  TVM_DLL explicit Workload(IRModule mod);
78  TVM_DLL explicit Workload(IRModule mod, THashCode shash);
84  TVM_DLL static Workload FromJSON(const ObjectRef& json_obj);
85 
87 };
88 
90 struct WorkloadHash {
91  size_t operator()(const Workload& a) const { return a->shash; }
92 };
93 
95 struct WorkloadEqual {
96  bool operator()(const Workload& a, const Workload& b) const {
97  return a->shash == b->shash && tvm::StructuralEqual()(a->mod, b->mod);
98  }
99 };
100 
102 class MeasureCandidate;
103 
106  public:
110  Workload workload{nullptr};
117 
119  v->Visit("trace", &trace);
120  v->Visit("workload", &workload);
121  v->Visit("run_secs", &run_secs);
122  v->Visit("target", &target);
123  v->Visit("args_info", &args_info);
124  }
125 
126  static constexpr const char* _type_key = "meta_schedule.TuningRecord";
128 
131  MeasureCandidate AsMeasureCandidate() const;
137  ObjectRef AsJSON() const;
138 };
139 
145  public:
154  TVM_DLL explicit TuningRecord(tir::Trace trace, Workload workload,
155  Optional<Array<FloatImm>> run_secs, Optional<Target> target,
156  Optional<Array<ArgInfo>> args_info);
163  TVM_DLL static TuningRecord FromJSON(const ObjectRef& json_obj, const Workload& workload);
165 };
166 
167 /* \brief The abstract interface of database. */
169  public:
171  virtual ~DatabaseNode() = default;
177  virtual bool HasWorkload(const IRModule& mod) = 0;
183  virtual Workload CommitWorkload(const IRModule& mod) = 0;
188  virtual void CommitTuningRecord(const TuningRecord& record) = 0;
195  virtual Array<TuningRecord> GetTopK(const Workload& workload, int top_k) = 0;
200  virtual Array<TuningRecord> GetAllTuningRecords() = 0;
205  virtual int64_t Size() = 0;
206 
207  static constexpr const char* _type_key = "meta_schedule.Database";
209 };
210 
212 class PyDatabaseNode : public DatabaseNode {
213  public:
248 
261 
263  // PackedFuncs are all not visited, because the reflection system doesn't take care of them,
264  // so it cannot be accessible on the python side. If there is such need from the future,
265  // we can then add corresponding accessor methods to help access on python.
266  // `f_has_workload` is not visited
267  // `f_commit_workload` is not visited
268  // `f_commit_tuning_record` is not visited
269  // `f_get_top_k` is not visited
270  // `f_get_all_tuning_records` is not visited
271  // `f_size` is not visited
272  }
273 
274  bool HasWorkload(const IRModule& mod) final {
275  ICHECK(f_has_workload != nullptr) << "PyDatabase's HasWorkload method not implemented!";
276  return f_has_workload(mod);
277  }
278 
279  Workload CommitWorkload(const IRModule& mod) final {
280  ICHECK(f_commit_workload != nullptr) << "PyDatabase's CommitWorkload method not implemented!";
281  return f_commit_workload(mod);
282  }
283 
284  void CommitTuningRecord(const TuningRecord& record) final {
285  ICHECK(f_commit_tuning_record != nullptr)
286  << "PyDatabase's CommitTuningRecord method not implemented!";
287  f_commit_tuning_record(record);
288  }
289 
290  Array<TuningRecord> GetTopK(const Workload& workload, int top_k) final {
291  ICHECK(f_get_top_k != nullptr) << "PyDatabase's GetTopK method not implemented!";
292  return f_get_top_k(workload, top_k);
293  }
294 
296  ICHECK(f_get_all_tuning_records != nullptr)
297  << "PyDatabase's GetAllTuningRecords method not implemented!";
298  return f_get_all_tuning_records();
299  }
300 
301  int64_t Size() final {
302  ICHECK(f_size != nullptr) << "PyDatabase's Size method not implemented!";
303  return f_size();
304  }
305 
306  static constexpr const char* _type_key = "meta_schedule.PyDatabase";
308 };
309 
314 class Database : public runtime::ObjectRef {
315  public:
322  TVM_DLL static Database JSONDatabase(String path_workload, String path_tuning_record,
323  bool allow_missing);
334  TVM_DLL static Database PyDatabase(PyDatabaseNode::FHasWorkload f_has_workload,
335  PyDatabaseNode::FCommitWorkload f_commit_workload,
336  PyDatabaseNode::FCommitTuningRecord f_commit_tuning_record,
337  PyDatabaseNode::FGetTopK f_get_top_k,
338  PyDatabaseNode::FGetAllTuningRecords f_get_all_tuning_records,
339  PyDatabaseNode::FSize f_size);
341 };
342 
343 } // namespace meta_schedule
344 } // namespace tvm
345 
346 #endif // TVM_META_SCHEDULE_DATABASE_H_
WorkloadNode::THashCode THashCode
Definition: database.h:67
TVM_DECLARE_FINAL_OBJECT_INFO(WorkloadNode, runtime::Object)
FCommitTuningRecord f_commit_tuning_record
The packed function to the CommitTuningRecord function.
Definition: database.h:254
#define TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:758
The hash method for Workload.
Definition: database.h:90
Runtime String container types.
Base expr nodes in TVM.
IRModule that holds the functions and type definitions.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
Optional< Target > target
The target for tuning.
Definition: database.h:114
size_t THashCode
The type of structural hash.
Definition: database.h:40
Definition: database.h:168
FGetTopK f_get_top_k
The packed function to the GetTopK function.
Definition: database.h:256
size_t operator()(const Workload &a) const
Definition: database.h:91
base class of all object containers.
Definition: object.h:167
The database with customized methods on the python-side.
Definition: database.h:212
Content-aware structural equality comparator for objects.
Definition: structural_equal.h:81
tir::Trace trace
The trace tuned.
Definition: database.h:108
Runtime Array container types.
FSize f_size
The packed function to the Size function.
Definition: database.h:260
Visitor class to get the attributes of an AST/IR node. The content is going to be called for each fie...
Definition: reflection.h:52
Optional< Array< ArgInfo > > args_info
The argument information.
Definition: database.h:116
bool operator()(const Workload &a, const Workload &b) const
Definition: database.h:96
Managed reference to DatabaseNode.
Definition: database.h:314
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:270
bool HasWorkload(const IRModule &mod) final
Check if the database has the given workload.
Definition: database.h:274
IRModule mod
The workload&#39;s IRModule.
Definition: database.h:42
Optional< Array< FloatImm > > run_secs
The profiling result in seconds.
Definition: database.h:112
Reference to string objects.
Definition: string.h:124
int64_t Size() final
Get the size of the database.
Definition: database.h:301
void VisitAttrs(tvm::AttrVisitor *v)
Definition: database.h:46
Array< TuningRecord > GetAllTuningRecords() final
Get all tuning records from the database.
Definition: database.h:295
Base class of all object reference.
Definition: object.h:511
Managed reference to MeasureCandidateNode.
Definition: measure_candidate.h:53
The class of tuning records.
Definition: database.h:105
static constexpr const char * _type_key
Definition: database.h:51
A managed object in the TVM runtime.
Managed reference class to IRModuleNode.
Definition: module.h:360
ObjectRef AsJSON() const
Export the workload to a JSON string.
FHasWorkload f_has_workload
The packed function to the HasWorkload function.
Definition: database.h:250
void CommitTuningRecord(const TuningRecord &record) final
Add a tuning record to the database.
Definition: database.h:284
FCommitWorkload f_commit_workload
The packed function to the CommitWorkload function.
Definition: database.h:252
Compilation target object.
Workload CommitWorkload(const IRModule &mod) final
Look up or add workload to the database if missing.
Definition: database.h:279
Managed reference to TraceNode.
Definition: trace.h:141
FGetAllTuningRecords f_get_all_tuning_records
The packed function to the GetAllTuningRecords function.
Definition: database.h:258
THashCode shash
The workload&#39;s structural hash.
Definition: database.h:44
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Managed reference to WorkloadNode.
Definition: database.h:65
Array< TuningRecord > GetTopK(const Workload &workload, int top_k) final
Get the top K tuning records of given workload from the database.
Definition: database.h:290
Reflection and serialization of compiler IR/AST nodes.
void VisitAttrs(tvm::AttrVisitor *v)
Definition: database.h:262
The managed reference of TuningRecordNode.
Definition: database.h:144
#define TVM_DECLARE_BASE_OBJECT_INFO(TypeName, ParentType)
helper macro to declare a base object type that can be inherited.
Definition: object.h:648
#define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:728
Type-erased function used across TVM API.
The equality check for Workload.
Definition: database.h:95
A workload, i.e. an IRModule and its structural hash.
Definition: database.h:37
void VisitAttrs(tvm::AttrVisitor *v)
Definition: database.h:118