tvm
Loading...
Searching...
No Matches
task_scheduler.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_TASK_SCHEDULER_H_
20#define TVM_S_TIR_META_SCHEDULE_TASK_SCHEDULER_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/ir/prim/expr.h>
27#include <tvm/runtime/base.h>
34
35#include <string>
36#include <vector>
37
38namespace tvm {
39namespace s_tir {
40using namespace tvm::prim;
41namespace meta_schedule {
42
43class TaskRecordNode : public ffi::Object {
44 public:
46 TuneContext ctx{ffi::UnsafeInit()};
48 double task_weight{1.0};
50 double flop{1.0};
52 bool is_terminated = false;
58 std::vector<double> latency_ms = {};
60 ffi::Optional<ffi::Array<MeasureCandidate>> measure_candidates = std::nullopt;
62 ffi::Optional<ffi::Array<BuilderResult>> builder_results = std::nullopt;
64 ffi::Optional<ffi::Array<RunnerFuture>> runner_futures = std::nullopt;
65
66 static void RegisterReflection() {
67 namespace refl = tvm::ffi::reflection;
68 refl::ObjectDef<TaskRecordNode>()
69 .def_ro("ctx", &TaskRecordNode::ctx)
70 .def_ro("task_weight", &TaskRecordNode::task_weight)
71 .def_ro("flop", &TaskRecordNode::flop)
72 .def_ro("is_terminated", &TaskRecordNode::is_terminated)
73 .def_ro("build_error_count", &TaskRecordNode::build_error_count)
74 .def_ro("run_error_count", &TaskRecordNode::run_error_count)
75 .def_ro("measure_candidates", &TaskRecordNode::measure_candidates)
76 .def_ro("builder_results", &TaskRecordNode::builder_results)
77 .def_ro("runner_futures", &TaskRecordNode::runner_futures);
78 }
79
80 static constexpr const bool _type_mutable = true;
81 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.TaskRecord", TaskRecordNode, ffi::Object);
82};
83
88class TaskRecord : public ffi::ObjectRef {
89 public:
91 explicit TaskRecord(TuneContext task, double task_weight);
92
94};
95
132class TaskSchedulerNode : public ffi::Object {
133 public:
135 ffi::Function logger;
137 ffi::Array<TaskRecord> tasks_;
139 ffi::Array<MeasureCallback> measure_callbacks_;
141 ffi::Optional<Database> database_;
143 ffi::Optional<CostModel> cost_model_;
146
148 virtual ~TaskSchedulerNode() = default;
149
150 static void RegisterReflection() {
151 namespace refl = tvm::ffi::reflection;
152 refl::ObjectDef<TaskSchedulerNode>()
153 .def_ro("tasks_", &TaskSchedulerNode::tasks_)
154 .def_ro("measure_callbacks_", &TaskSchedulerNode::measure_callbacks_)
155 .def_ro("database_", &TaskSchedulerNode::database_)
156 .def_ro("cost_model_", &TaskSchedulerNode::cost_model_)
157 .def_ro("remaining_tasks_", &TaskSchedulerNode::remaining_tasks_);
158 }
159
164 virtual int NextTaskId() = 0;
170 virtual ffi::Array<RunnerResult> JoinRunningTask(int task_id);
184 virtual void Tune(ffi::Array<TuneContext> tasks, //
185 ffi::Array<FloatImm> task_weights, //
186 int max_trials_global, //
187 int max_trials_per_task, //
188 int num_trials_per_iter, //
189 Builder builder, //
190 Runner runner, //
191 ffi::Array<MeasureCallback> measure_callbacks, //
192 ffi::Optional<Database> database, //
193 ffi::Optional<CostModel> cost_model);
206
207 static constexpr const bool _type_mutable = true;
208 TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.TaskScheduler", TaskSchedulerNode, ffi::Object);
209};
210
211class TaskScheduler;
212
215 public:
220 using FNextTaskId = ffi::TypedFunction<int()>;
225 using FJoinRunningTask = ffi::TypedFunction<ffi::Array<RunnerResult>(int)>;
227 using FTune = ffi::TypedFunction<void(ffi::Array<TuneContext> tasks, //
228 ffi::Array<FloatImm> task_weights, //
229 int max_trials_global, //
230 int max_trials_per_task, //
231 int num_trials_per_iter, //
232 Builder builder, //
233 Runner runner, //
234 ffi::Array<MeasureCallback> measure_callbacks, //
235 ffi::Optional<Database> database, //
236 ffi::Optional<CostModel> cost_model)>;
237
244
245 static void RegisterReflection() {
246 namespace refl = tvm::ffi::reflection;
247 refl::ObjectDef<PyTaskSchedulerNode>();
248 }
249
252 void Tune(ffi::Array<TuneContext> tasks, ffi::Array<FloatImm> task_weights, int max_trials_global,
256 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.PyTaskScheduler", PyTaskSchedulerNode,
258};
259
264class TaskScheduler : public ffi::ObjectRef {
265 public:
266 explicit TaskScheduler(ffi::ObjectPtr<TaskSchedulerNode> data) : ffi::ObjectRef(data) {
267 TVM_FFI_ICHECK(data != nullptr);
268 }
274 TVM_DLL static TaskScheduler RoundRobin(ffi::Function logger);
283 TVM_DLL static TaskScheduler GradientBased(ffi::Function logger, double alpha, int window_size,
297};
298
299} // namespace meta_schedule
300} // namespace s_tir
301} // namespace tvm
302
303#endif // TVM_S_TIR_META_SCHEDULE_TASK_SCHEDULER_H_
Managed reference class to FloatImmNode.
Definition expr.h:567
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
Managed reference to BuilderNode.
Definition builder.h:137
Managed reference to CostModelNode.
Definition cost_model.h:136
Managed reference to DatabaseNode.
Definition database.h:469
Managed reference to MeasureCallbackNode.
Definition measure_callback.h:111
The task scheduler with customized methods on the python-side.
Definition task_scheduler.h:214
ffi::TypedFunction< int()> FNextTaskId
The function type of NextTaskId method.
Definition task_scheduler.h:220
ffi::TypedFunction< ffi::Array< RunnerResult >(int)> FJoinRunningTask
The function type of JoinRunningTask method.
Definition task_scheduler.h:225
FTune f_tune
The packed function to the Tune function.
Definition task_scheduler.h:243
static void RegisterReflection()
Definition task_scheduler.h:245
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.PyTaskScheduler", PyTaskSchedulerNode, TaskSchedulerNode)
ffi::TypedFunction< void(ffi::Array< TuneContext > tasks, ffi::Array< FloatImm > task_weights, int max_trials_global, int max_trials_per_task, int num_trials_per_iter, Builder builder, Runner runner, ffi::Array< MeasureCallback > measure_callbacks, ffi::Optional< Database > database, ffi::Optional< CostModel > cost_model)> FTune
The function type of Tune method.
Definition task_scheduler.h:236
FJoinRunningTask f_join_running_task
The packed function to the JoinRunningTask function.
Definition task_scheduler.h:241
int NextTaskId() final
Fetch the next task id.
ffi::Array< RunnerResult > JoinRunningTask(int task_id) final
Wait until the task is finished.
void Tune(ffi::Array< TuneContext > tasks, ffi::Array< FloatImm > task_weights, int max_trials_global, int max_trials_per_task, int num_trials_per_iter, Builder builder, Runner runner, ffi::Array< MeasureCallback > measure_callbacks, ffi::Optional< Database > database, ffi::Optional< CostModel > cost_model) final
Jointly tune a given list of tasks.
FNextTaskId f_next_task_id
The packed function to the NextTaskId function.
Definition task_scheduler.h:239
Managed reference to RunnerResultNode.
Definition runner.h:96
Managed reference to RunnerNode.
Definition runner.h:210
Definition task_scheduler.h:43
bool is_terminated
Whether the tuning task has been stopped or finished.
Definition task_scheduler.h:52
double flop
The FLOP count of the task.
Definition task_scheduler.h:50
double task_weight
The weight of the task.
Definition task_scheduler.h:48
ffi::Optional< ffi::Array< BuilderResult > > builder_results
The building results.
Definition task_scheduler.h:62
TuneContext ctx
The tune context of the task.
Definition task_scheduler.h:46
ffi::Optional< ffi::Array< MeasureCandidate > > measure_candidates
The measure candidates.
Definition task_scheduler.h:60
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.TaskRecord", TaskRecordNode, ffi::Object)
int build_error_count
Builder errors happens in the task.
Definition task_scheduler.h:54
ffi::Optional< ffi::Array< RunnerFuture > > runner_futures
Packed functions to fetch the runner results asynchronously.
Definition task_scheduler.h:64
static void RegisterReflection()
Definition task_scheduler.h:66
std::vector< double > latency_ms
The latency of each run, in milliseconds.
Definition task_scheduler.h:58
static constexpr const bool _type_mutable
Definition task_scheduler.h:80
int run_error_count
Runner errors happens in the task.
Definition task_scheduler.h:56
Managed reference to TaskRecordNode.
Definition task_scheduler.h:88
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TaskRecord, ffi::ObjectRef, TaskRecordNode)
TaskRecord(TuneContext task, double task_weight)
Constructor.
The abstract interface of task schedulers.
Definition task_scheduler.h:132
ffi::Optional< CostModel > cost_model_
The cost model used in tuning.
Definition task_scheduler.h:143
void PrintTuningStatistics()
Print out a human-readable format of the tuning statistics.
static constexpr const bool _type_mutable
Definition task_scheduler.h:207
void TerminateTask(int task_id)
Terminate a task.
TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.TaskScheduler", TaskSchedulerNode, ffi::Object)
static void RegisterReflection()
Definition task_scheduler.h:150
ffi::Array< TaskRecord > tasks_
Records for each task.
Definition task_scheduler.h:137
virtual void Tune(ffi::Array< TuneContext > tasks, ffi::Array< FloatImm > task_weights, int max_trials_global, int max_trials_per_task, int num_trials_per_iter, Builder builder, Runner runner, ffi::Array< MeasureCallback > measure_callbacks, ffi::Optional< Database > database, ffi::Optional< CostModel > cost_model)
Jointly tune a given list of tasks.
int remaining_tasks_
The number of remaining tasks to be tuned.
Definition task_scheduler.h:145
virtual int NextTaskId()=0
Fetch the next task id.
virtual ~TaskSchedulerNode()=default
The default destructor.
void TouchTask(int task_id)
Touch the task and update its status.
ffi::Function logger
The tuning task's logging function.
Definition task_scheduler.h:135
virtual ffi::Array< RunnerResult > JoinRunningTask(int task_id)
Wait until the task is finished.
ffi::Array< MeasureCallback > measure_callbacks_
The list of measure callbacks of the scheduler.
Definition task_scheduler.h:139
ffi::Optional< Database > database_
The database used in tuning.
Definition task_scheduler.h:141
Managed reference to TaskSchedulerNode.
Definition task_scheduler.h:264
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TaskScheduler, ffi::ObjectRef, TaskSchedulerNode)
TaskScheduler(ffi::ObjectPtr< TaskSchedulerNode > data)
Definition task_scheduler.h:266
static TaskScheduler RoundRobin(ffi::Function logger)
Create a task scheduler that fetches tasks in a round-robin fashion.
static TaskScheduler GradientBased(ffi::Function logger, double alpha, int window_size, LinearCongruentialEngine::TRandState seed)
Create a task scheduler that fetches tasks in a gradient based fashion.
static TaskScheduler PyTaskScheduler(ffi::Function logger, PyTaskSchedulerNode::FNextTaskId f_next_task_id, PyTaskSchedulerNode::FJoinRunningTask f_join_running_task, PyTaskSchedulerNode::FTune f_tune)
Create a task scheduler with customized methods on the python-side.
Managed reference to TuneContextNode.
Definition tune_context.h:101
TIR expressions.
Definition builtin.h:25
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
Random number generator. It provides a generic interface consistent with std::uniform_random_bit_gene...