tvm
measure_callback.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 
20 #ifndef TVM_S_TIR_META_SCHEDULE_MEASURE_CALLBACK_H_
21 #define TVM_S_TIR_META_SCHEDULE_MEASURE_CALLBACK_H_
22 
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/runtime/object.h>
33 
34 namespace tvm {
35 namespace s_tir {
36 namespace meta_schedule {
37 
38 class TaskScheduler;
39 
41 class MeasureCallbackNode : public runtime::Object {
42  public:
44  virtual ~MeasureCallbackNode() = default;
45 
46  static void RegisterReflection() {
47  namespace refl = tvm::ffi::reflection;
48  refl::ObjectDef<MeasureCallbackNode>();
49  }
50 
59  virtual void Apply(const TaskScheduler& task_scheduler, //
60  int task_id, //
61  const ffi::Array<MeasureCandidate>& measure_candidates, //
62  const ffi::Array<BuilderResult>& builder_results, //
63  const ffi::Array<RunnerResult>& runner_results) = 0;
64 
65  static constexpr const bool _type_mutable = true;
66  TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.MeasureCallback", MeasureCallbackNode, Object);
67 };
68 
71  public:
81  using FApply = ffi::TypedFunction<void(const TaskScheduler& task_scheduler, //
82  int task_id, //
83  const ffi::Array<MeasureCandidate>& measure_candidates, //
84  const ffi::Array<BuilderResult>& builds, //
85  const ffi::Array<RunnerResult>& results)>;
90  using FAsString = ffi::TypedFunction<ffi::String()>;
91 
96 
97  static void RegisterReflection() {
98  // `f_apply` is not registered
99  // `f_as_string` is not registered
100  namespace refl = tvm::ffi::reflection;
101  refl::ObjectDef<PyMeasureCallbackNode>();
102  }
103 
104  void Apply(const TaskScheduler& task_scheduler, //
105  int task_id, //
106  const ffi::Array<MeasureCandidate>& measure_candidates, //
107  const ffi::Array<BuilderResult>& builds, //
108  const ffi::Array<RunnerResult>& results);
109  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.PyMeasureCallback", PyMeasureCallbackNode,
111 };
112 
117 class MeasureCallback : public runtime::ObjectRef {
118  public:
123  TVM_DLL static MeasureCallback AddToDatabase();
143  TVM_DLL static ffi::Array<MeasureCallback, void> Default();
145 };
146 
147 } // namespace meta_schedule
148 } // namespace s_tir
149 } // namespace tvm
150 
151 #endif // TVM_S_TIR_META_SCHEDULE_MEASURE_CALLBACK_H_
Rules to apply after measure results is available.
Definition: measure_callback.h:41
virtual void Apply(const TaskScheduler &task_scheduler, int task_id, const ffi::Array< MeasureCandidate > &measure_candidates, const ffi::Array< BuilderResult > &builder_results, const ffi::Array< RunnerResult > &runner_results)=0
Apply a measure callback rule with given arguments.
static constexpr const bool _type_mutable
Definition: measure_callback.h:65
virtual ~MeasureCallbackNode()=default
Virtual destructor.
static void RegisterReflection()
Definition: measure_callback.h:46
TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.MeasureCallback", MeasureCallbackNode, Object)
Managed reference to MeasureCallbackNode.
Definition: measure_callback.h:117
static MeasureCallback AddToDatabase()
Create a measure callback that adds the measurement results into the database.
static MeasureCallback RemoveBuildArtifact()
Create a measure callback that removes the build artifacts from the disk.
static MeasureCallback UpdateCostModel()
Create a measure callback that updates the cost model with measurement result.
static ffi::Array< MeasureCallback, void > Default()
The default list of measure callbacks.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(MeasureCallback, ObjectRef, MeasureCallbackNode)
static MeasureCallback PyMeasureCallback(PyMeasureCallbackNode::FApply f_apply, PyMeasureCallbackNode::FAsString f_as_string)
Create a measure callback with customized methods on the python-side.
The measure callback with customized methods on the python-side.
Definition: measure_callback.h:70
ffi::TypedFunction< ffi::String()> FAsString
Get the measure callback function as string with name.
Definition: measure_callback.h:90
FAsString f_as_string
The packed function to the AsString function.
Definition: measure_callback.h:95
ffi::TypedFunction< void(const TaskScheduler &task_scheduler, int task_id, const ffi::Array< MeasureCandidate > &measure_candidates, const ffi::Array< BuilderResult > &builds, const ffi::Array< RunnerResult > &results)> FApply
Apply a measure callback to the given schedule.
Definition: measure_callback.h:85
static void RegisterReflection()
Definition: measure_callback.h:97
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.PyMeasureCallback", PyMeasureCallbackNode, MeasureCallbackNode)
void Apply(const TaskScheduler &task_scheduler, int task_id, const ffi::Array< MeasureCandidate > &measure_candidates, const ffi::Array< BuilderResult > &builds, const ffi::Array< RunnerResult > &results)
Apply a measure callback rule with given arguments.
FApply f_apply
The packed function to the Apply function.
Definition: measure_callback.h:93
Managed reference to TaskSchedulerNode.
Definition: task_scheduler.h:262
Definition: repr_printer.h:91
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
A managed object in the TVM runtime.