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_S_TIR_META_SCHEDULE_RUNNER_H_
20 #define TVM_S_TIR_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>
28 #include <tvm/runtime/object.h>
30 
31 namespace tvm {
32 namespace s_tir {
33 namespace meta_schedule {
34 
36 class RunnerInputNode : public runtime::Object {
37  public:
39  ffi::String artifact_path;
41  ffi::String device_type;
43  ffi::Array<ArgInfo> args_info;
44 
45  static void RegisterReflection() {
46  namespace refl = tvm::ffi::reflection;
47  refl::ObjectDef<RunnerInputNode>()
48  .def_ro("artifact_path", &RunnerInputNode::artifact_path)
49  .def_ro("device_type", &RunnerInputNode::device_type)
50  .def_ro("args_info", &RunnerInputNode::args_info);
51  }
52  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.RunnerInput", RunnerInputNode,
53  runtime::Object);
54 };
55 
60 class RunnerInput : public runtime::ObjectRef {
61  public:
68  TVM_DLL explicit RunnerInput(ffi::String artifact_path, ffi::String device_type,
69  ffi::Array<ArgInfo> args_info);
71 };
72 
74 class RunnerResultNode : public runtime::Object {
75  public:
77  ffi::Optional<ffi::Array<FloatImm>> run_secs;
79  ffi::Optional<ffi::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  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.RunnerResult", RunnerResultNode,
88  runtime::Object);
89 };
90 
95 class RunnerResult : public runtime::ObjectRef {
96  public:
102  TVM_DLL explicit RunnerResult(ffi::Optional<ffi::Array<FloatImm>> run_secs,
103  ffi::Optional<ffi::String> error_msg);
105 };
106 
112 class RunnerFutureNode : public runtime::Object {
113  public:
118  using FDone = ffi::TypedFunction<bool()>;
123  using FResult = ffi::TypedFunction<RunnerResult()>;
124 
129 
130  static void RegisterReflection() {
131  // `f_done` is not registered
132  // `f_result` is not registered
133  namespace refl = tvm::ffi::reflection;
134  refl::ObjectDef<RunnerFutureNode>();
135  }
136 
141  bool Done() const {
142  ICHECK(f_done != nullptr) << "PyRunnerFuture's Done method not implemented!";
143  return f_done();
144  }
150  ICHECK(f_result != nullptr) << "PyRunnerFuture's Result method not implemented!";
151  return f_result();
152  }
153  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.RunnerFuture", RunnerFutureNode,
154  runtime::Object);
155 };
156 
161 class RunnerFuture : public runtime::ObjectRef {
162  public:
165 
171  TVM_DLL explicit RunnerFuture(FDone f_done, FResult f_result);
173 };
174 
176 class RunnerNode : public runtime::Object {
177  public:
184  using FRun = ffi::TypedFunction<ffi::Array<RunnerFuture>(ffi::Array<RunnerInput>)>;
185 
187  virtual ~RunnerNode() = default;
188 
194  virtual ffi::Array<RunnerFuture> Run(ffi::Array<RunnerInput> runner_inputs) = 0;
195 
196  static void RegisterReflection() {
197  namespace refl = tvm::ffi::reflection;
198  refl::ObjectDef<RunnerNode>();
199  }
200 
201  static constexpr const bool _type_mutable = true;
202  TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.Runner", RunnerNode, runtime::Object);
203 };
204 
209 class Runner : public runtime::ObjectRef {
210  public:
216  explicit Runner(ObjectPtr<RunnerNode> data) : ObjectRef(data) { TVM_FFI_ICHECK(data != nullptr); }
222  TVM_DLL static Runner PyRunner(FRun f_run);
224 };
225 
227 class PyRunnerNode : public RunnerNode {
228  public:
231 
232  static void RegisterReflection() {
233  // `f_run` is not registered
234  namespace refl = tvm::ffi::reflection;
235  refl::ObjectDef<PyRunnerNode>();
236  }
237 
238  ffi::Array<RunnerFuture> Run(ffi::Array<RunnerInput> runner_inputs) final {
239  ICHECK(f_run != nullptr) << "PyRunner's Run method not implemented!";
240  return f_run(runner_inputs);
241  }
242  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.PyRunner", PyRunnerNode, RunnerNode);
243 };
244 
245 } // namespace meta_schedule
246 } // namespace s_tir
247 } // namespace tvm
248 
249 #endif // TVM_S_TIR_META_SCHEDULE_RUNNER_H_
An abstract runner with customized build method on the python-side.
Definition: runner.h:227
static void RegisterReflection()
Definition: runner.h:232
FRun f_run
The packed function to run the built artifacts and get runner futures.
Definition: runner.h:230
ffi::Array< RunnerFuture > Run(ffi::Array< RunnerInput > runner_inputs) final
Run the built artifact and get runner futures.
Definition: runner.h:238
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.PyRunner", PyRunnerNode, RunnerNode)
A class to asynchronously fetch runner's output.
Definition: runner.h:112
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.RunnerFuture", RunnerFutureNode, runtime::Object)
ffi::TypedFunction< bool()> FDone
The function type to check whether the runner has finished.
Definition: runner.h:118
RunnerResult Result() const
Fetch the runner's output if it is ready.
Definition: runner.h:149
bool Done() const
Check whether the runner has finished.
Definition: runner.h:141
ffi::TypedFunction< RunnerResult()> FResult
The function type to fetch runner output if it is ready.
Definition: runner.h:123
static void RegisterReflection()
Definition: runner.h:130
FResult f_result
The packed function to fetch runner output if it is ready.
Definition: runner.h:128
FDone f_done
The packed function to check whether the runner has finished.
Definition: runner.h:126
Managed reference to RunnerFutureNode.
Definition: runner.h:161
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(RunnerFuture, runtime::ObjectRef, RunnerFutureNode)
RunnerFuture(FDone f_done, FResult f_result)
Constructor of RunnerFuture.
RunnerFutureNode::FDone FDone
Definition: runner.h:163
RunnerFutureNode::FResult FResult
Definition: runner.h:164
Runner's input containing path of artifact, type of device and argument info.
Definition: runner.h:36
ffi::String device_type
The type of device.
Definition: runner.h:41
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.RunnerInput", RunnerInputNode, runtime::Object)
static void RegisterReflection()
Definition: runner.h:45
ffi::String artifact_path
The path to the built artifact.
Definition: runner.h:39
ffi::Array< ArgInfo > args_info
The argument information.
Definition: runner.h:43
Managed reference to RunnerInputNode.
Definition: runner.h:60
RunnerInput(ffi::String artifact_path, ffi::String device_type, ffi::Array< ArgInfo > args_info)
Constructor of RunnerInput.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(RunnerInput, runtime::ObjectRef, RunnerInputNode)
The abstract runner interface.
Definition: runner.h:176
virtual ffi::Array< RunnerFuture > Run(ffi::Array< RunnerInput > runner_inputs)=0
Run the built artifact and get runner futures.
static void RegisterReflection()
Definition: runner.h:196
ffi::TypedFunction< ffi::Array< RunnerFuture >(ffi::Array< RunnerInput >)> FRun
The function type to run the built artifacts and get runner futures.
Definition: runner.h:184
virtual ~RunnerNode()=default
Default destructor.
static constexpr const bool _type_mutable
Definition: runner.h:201
TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.Runner", RunnerNode, runtime::Object)
Runner's output containing measurement result of MeasureCandidate or error msg if any.
Definition: runner.h:74
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.RunnerResult", RunnerResultNode, runtime::Object)
ffi::Optional< ffi::Array< FloatImm > > run_secs
The run time in seconds.
Definition: runner.h:77
static void RegisterReflection()
Definition: runner.h:81
ffi::Optional< ffi::String > error_msg
The error message, if any.
Definition: runner.h:79
Managed reference to RunnerResultNode.
Definition: runner.h:95
RunnerResult(ffi::Optional< ffi::Array< FloatImm >> run_secs, ffi::Optional< ffi::String > error_msg)
Constructor.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(RunnerResult, runtime::ObjectRef, RunnerResultNode)
Managed reference to RunnerNode.
Definition: runner.h:209
static Runner PyRunner(FRun f_run)
Create a runner with customized build method on the python-side.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Runner, runtime::ObjectRef, RunnerNode)
Runner(ObjectPtr< RunnerNode > data)
Constructor from ObjectPtr<RunnerNode>.
Definition: runner.h:216
RunnerNode::FRun FRun
Definition: runner.h:211
Base expr nodes in TVM.
Definition: repr_printer.h:91
constexpr const char * device_type
The device type.
Definition: stmt.h:1072
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
A managed object in the TVM runtime.