tvm
runner.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_RUNNER_H_
20 #define TVM_META_SCHEDULE_RUNNER_H_
21 
22 #include <tvm/ffi/container/array.h>
23 #include <tvm/ffi/function.h>
24 #include <tvm/ffi/optional.h>
25 #include <tvm/ffi/reflection/registry.h>
26 #include <tvm/ffi/string.h>
27 #include <tvm/ir/expr.h>
29 #include <tvm/runtime/object.h>
30 
31 namespace tvm {
32 namespace meta_schedule {
33 
35 class RunnerInputNode : public runtime::Object {
36  public:
38  String artifact_path;
40  String device_type;
42  Array<ArgInfo> args_info;
43 
44  static void RegisterReflection() {
45  namespace refl = tvm::ffi::reflection;
46  refl::ObjectDef<RunnerInputNode>()
47  .def_ro("artifact_path", &RunnerInputNode::artifact_path)
48  .def_ro("device_type", &RunnerInputNode::device_type)
49  .def_ro("args_info", &RunnerInputNode::args_info);
50  }
51 
52  static constexpr const char* _type_key = "meta_schedule.RunnerInput";
53 
55 };
56 
61 class RunnerInput : public runtime::ObjectRef {
62  public:
69  TVM_DLL explicit RunnerInput(String artifact_path, String device_type, Array<ArgInfo> args_info);
71 };
72 
74 class RunnerResultNode : public runtime::Object {
75  public:
77  Optional<Array<FloatImm>> run_secs;
79  Optional<String> error_msg;
80 
81  static void RegisterReflection() {
82  namespace refl = tvm::ffi::reflection;
83  refl::ObjectDef<RunnerResultNode>()
84  .def_ro("run_secs", &RunnerResultNode::run_secs)
85  .def_ro("error_msg", &RunnerResultNode::error_msg);
86  }
87 
88  static constexpr const char* _type_key = "meta_schedule.RunnerResult";
89 
91 };
92 
97 class RunnerResult : public runtime::ObjectRef {
98  public:
104  TVM_DLL explicit RunnerResult(Optional<Array<FloatImm>> run_secs, Optional<String> error_msg);
106 };
107 
113 class RunnerFutureNode : public runtime::Object {
114  public:
119  using FDone = ffi::TypedFunction<bool()>;
124  using FResult = ffi::TypedFunction<RunnerResult()>;
125 
130 
131  static void RegisterReflection() {
132  // `f_done` is not registered
133  // `f_result` is not registered
134  }
135 
140  bool Done() const {
141  ICHECK(f_done != nullptr) << "PyRunnerFuture's Done method not implemented!";
142  return f_done();
143  }
149  ICHECK(f_result != nullptr) << "PyRunnerFuture's Result method not implemented!";
150  return f_result();
151  }
152 
153  static constexpr const char* _type_key = "meta_schedule.RunnerFuture";
155 };
156 
161 class RunnerFuture : public runtime::ObjectRef {
162  public:
165 
171  TVM_DLL explicit RunnerFuture(FDone f_done, FResult f_result);
174 };
175 
177 class RunnerNode : public runtime::Object {
178  public:
185  using FRun = ffi::TypedFunction<Array<RunnerFuture>(Array<RunnerInput>)>;
186 
188  virtual ~RunnerNode() = default;
189 
195  virtual Array<RunnerFuture> Run(Array<RunnerInput> runner_inputs) = 0;
196 
197  static constexpr const char* _type_key = "meta_schedule.Runner";
199 };
200 
205 class Runner : public runtime::ObjectRef {
206  public:
208 
214  TVM_DLL static Runner PyRunner(FRun f_run);
216 };
217 
219 class PyRunnerNode : public RunnerNode {
220  public:
223 
224  static void RegisterReflection() {
225  // `f_run` is not registered
226  }
227 
228  Array<RunnerFuture> Run(Array<RunnerInput> runner_inputs) final {
229  ICHECK(f_run != nullptr) << "PyRunner's Run method not implemented!";
230  return f_run(runner_inputs);
231  }
232 
233  static constexpr const char* _type_key = "meta_schedule.PyRunner";
235 };
236 
237 } // namespace meta_schedule
238 } // namespace tvm
239 
240 #endif // TVM_META_SCHEDULE_RUNNER_H_
An abstract runner with customized build method on the python-side.
Definition: runner.h:219
static constexpr const char * _type_key
Definition: runner.h:233
TVM_DECLARE_FINAL_OBJECT_INFO(PyRunnerNode, RunnerNode)
Array< RunnerFuture > Run(Array< RunnerInput > runner_inputs) final
Run the built artifact and get runner futures.
Definition: runner.h:228
FRun f_run
The packed function to run the built artifacts and get runner futures.
Definition: runner.h:222
static void RegisterReflection()
Definition: runner.h:224
A class to asynchronously fetch runner's output.
Definition: runner.h:113
RunnerResult Result() const
Fetch the runner's output if it is ready.
Definition: runner.h:148
static void RegisterReflection()
Definition: runner.h:131
ffi::TypedFunction< RunnerResult()> FResult
The function type to fetch runner output if it is ready.
Definition: runner.h:124
TVM_DECLARE_FINAL_OBJECT_INFO(RunnerFutureNode, runtime::Object)
FDone f_done
The packed function to check whether the runner has finished.
Definition: runner.h:127
FResult f_result
The packed function to fetch runner output if it is ready.
Definition: runner.h:129
static constexpr const char * _type_key
Definition: runner.h:153
bool Done() const
Check whether the runner has finished.
Definition: runner.h:140
ffi::TypedFunction< bool()> FDone
The function type to check whether the runner has finished.
Definition: runner.h:119
Managed reference to RunnerFutureNode.
Definition: runner.h:161
RunnerFuture(FDone f_done, FResult f_result)
Constructor of RunnerFuture.
RunnerFutureNode::FDone FDone
Definition: runner.h:163
RunnerFutureNode::FResult FResult
Definition: runner.h:164
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(RunnerFuture, runtime::ObjectRef, RunnerFutureNode)
Runner's input containing path of artifact, type of device and argument info.
Definition: runner.h:35
TVM_DECLARE_FINAL_OBJECT_INFO(RunnerInputNode, runtime::Object)
static void RegisterReflection()
Definition: runner.h:44
String device_type
The type of device.
Definition: runner.h:40
Array< ArgInfo > args_info
The argument information.
Definition: runner.h:42
static constexpr const char * _type_key
Definition: runner.h:52
String artifact_path
The path to the built artifact.
Definition: runner.h:38
Managed reference to RunnerInputNode.
Definition: runner.h:61
RunnerInput(String artifact_path, String device_type, Array< ArgInfo > args_info)
Constructor of RunnerInput.
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(RunnerInput, runtime::ObjectRef, RunnerInputNode)
The abstract runner interface.
Definition: runner.h:177
ffi::TypedFunction< Array< RunnerFuture >(Array< RunnerInput >)> FRun
The function type to run the built artifacts and get runner futures.
Definition: runner.h:185
TVM_DECLARE_BASE_OBJECT_INFO(RunnerNode, runtime::Object)
virtual ~RunnerNode()=default
Default destructor.
virtual Array< RunnerFuture > Run(Array< RunnerInput > runner_inputs)=0
Run the built artifact and get runner futures.
static constexpr const char * _type_key
Definition: runner.h:197
Runner's output containing measurement result of MeasureCandidate or error msg if any.
Definition: runner.h:74
Optional< Array< FloatImm > > run_secs
The run time in seconds.
Definition: runner.h:77
Optional< String > error_msg
The error message, if any.
Definition: runner.h:79
static void RegisterReflection()
Definition: runner.h:81
static constexpr const char * _type_key
Definition: runner.h:88
TVM_DECLARE_FINAL_OBJECT_INFO(RunnerResultNode, runtime::Object)
Managed reference to RunnerResultNode.
Definition: runner.h:97
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(RunnerResult, runtime::ObjectRef, RunnerResultNode)
RunnerResult(Optional< Array< FloatImm >> run_secs, Optional< String > error_msg)
Constructor.
Managed reference to RunnerNode.
Definition: runner.h:205
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(Runner, runtime::ObjectRef, RunnerNode)
static Runner PyRunner(FRun f_run)
Create a runner with customized build method on the python-side.
RunnerNode::FRun FRun
Definition: runner.h:207
Base expr nodes in TVM.
Definition: repr_printer.h:91
constexpr const char * device_type
The device type.
Definition: stmt.h:1092
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
A managed object in the TVM runtime.