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/base.h>
23#include <tvm/ffi/container/array.h>
24#include <tvm/ffi/function.h>
25#include <tvm/ffi/optional.h>
26#include <tvm/ffi/reflection/registry.h>
27#include <tvm/ir/prim/expr.h>
28#include <tvm/runtime/base.h>
35
36#include <string>
37#include <vector>
38
39namespace tvm {
40namespace s_tir {
41namespace meta_schedule {
42
43class TaskRecordObj : 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<TaskRecordObj>()
69 .def_ro("ctx", &TaskRecordObj::ctx)
70 .def_ro("task_weight", &TaskRecordObj::task_weight)
71 .def_ro("flop", &TaskRecordObj::flop)
72 .def_ro("is_terminated", &TaskRecordObj::is_terminated)
73 .def_ro("build_error_count", &TaskRecordObj::build_error_count)
74 .def_ro("run_error_count", &TaskRecordObj::run_error_count)
75 .def_ro("measure_candidates", &TaskRecordObj::measure_candidates)
76 .def_ro("builder_results", &TaskRecordObj::builder_results)
77 .def_ro("runner_futures", &TaskRecordObj::runner_futures);
78 }
79
80 static constexpr const bool _type_mutable = true;
81 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.TaskRecord", TaskRecordObj, ffi::Object);
82};
83
88class TaskRecord : public ffi::ObjectRef {
89 public:
91 explicit TaskRecord(TuneContext task, double task_weight);
92
94};
95
132class TaskSchedulerObj : 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 ~TaskSchedulerObj() = default;
149
150 static void RegisterReflection() {
151 namespace refl = tvm::ffi::reflection;
152 refl::ObjectDef<TaskSchedulerObj>()
153 .def_ro("tasks_", &TaskSchedulerObj::tasks_)
154 .def_ro("measure_callbacks_", &TaskSchedulerObj::measure_callbacks_)
155 .def_ro("database_", &TaskSchedulerObj::database_)
156 .def_ro("cost_model_", &TaskSchedulerObj::cost_model_)
157 .def_ro("remaining_tasks_", &TaskSchedulerObj::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);
198 void TerminateTask(int task_id);
203 void TouchTask(int task_id);
206
207 static constexpr const bool _type_mutable = true;
208 TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.TaskScheduler", TaskSchedulerObj, 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<PyTaskSchedulerObj>();
248 }
249
250 int NextTaskId() final;
251 ffi::Array<RunnerResult> JoinRunningTask(int task_id) final;
252 void Tune(ffi::Array<TuneContext> tasks, ffi::Array<FloatImm> task_weights, int max_trials_global,
253 int max_trials_per_task, int num_trials_per_iter, Builder builder, Runner runner,
254 ffi::Array<MeasureCallback> measure_callbacks, ffi::Optional<Database> database,
255 ffi::Optional<CostModel> cost_model) final;
256 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.PyTaskScheduler", PyTaskSchedulerObj,
258};
259
264class TaskScheduler : public ffi::ObjectRef {
265 public:
266 explicit TaskScheduler(ffi::ObjectPtr<TaskSchedulerObj> 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_
TIR expressions.
Managed reference class to FloatImmObj.
Definition expr.h:411
int64_t TRandState
Definition random_engine.h:47
Managed reference to BuilderObj.
Definition builder.h:140
Managed reference to CostModelObj.
Definition cost_model.h:136
Managed reference to DatabaseObj.
Definition database.h:471
Managed reference to MeasureCallbackObj.
Definition measure_callback.h:111
The task scheduler with customized methods on the python-side.
Definition task_scheduler.h:214
int NextTaskId() final
Fetch the next task id.
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
FJoinRunningTask f_join_running_task
The packed function to the JoinRunningTask function.
Definition task_scheduler.h:241
ffi::TypedFunction< int()> FNextTaskId
The function type of NextTaskId method.
Definition task_scheduler.h:220
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
FNextTaskId f_next_task_id
The packed function to the NextTaskId function.
Definition task_scheduler.h:239
static void RegisterReflection()
Definition task_scheduler.h:245
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.PyTaskScheduler", PyTaskSchedulerObj, TaskSchedulerObj)
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.
Managed reference to RunnerResultObj.
Definition runner.h:95
Managed reference to RunnerObj.
Definition runner.h:209
Definition task_scheduler.h:43
TuneContext ctx
The tune context of the task.
Definition task_scheduler.h:46
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.TaskRecord", TaskRecordObj, ffi::Object)
double flop
The FLOP count of the task.
Definition task_scheduler.h:50
static constexpr const bool _type_mutable
Definition task_scheduler.h:80
static void RegisterReflection()
Definition task_scheduler.h:66
int build_error_count
Builder errors happens in the task.
Definition task_scheduler.h:54
double task_weight
The weight of the task.
Definition task_scheduler.h:48
ffi::Optional< ffi::Array< MeasureCandidate > > measure_candidates
The measure candidates.
Definition task_scheduler.h:60
int run_error_count
Runner errors happens in the task.
Definition task_scheduler.h:56
ffi::Optional< ffi::Array< BuilderResult > > builder_results
The building results.
Definition task_scheduler.h:62
std::vector< double > latency_ms
The latency of each run, in milliseconds.
Definition task_scheduler.h:58
ffi::Optional< ffi::Array< RunnerFuture > > runner_futures
Packed functions to fetch the runner results asynchronously.
Definition task_scheduler.h:64
bool is_terminated
Whether the tuning task has been stopped or finished.
Definition task_scheduler.h:52
Managed reference to TaskRecordObj.
Definition task_scheduler.h:88
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TaskRecord, ffi::ObjectRef, TaskRecordObj)
TaskRecord(TuneContext task, double task_weight)
Constructor.
The abstract interface of task schedulers.
Definition task_scheduler.h:132
ffi::Array< MeasureCallback > measure_callbacks_
The list of measure callbacks of the scheduler.
Definition task_scheduler.h:139
int remaining_tasks_
The number of remaining tasks to be tuned.
Definition task_scheduler.h:145
static constexpr const bool _type_mutable
Definition task_scheduler.h:207
ffi::Optional< CostModel > cost_model_
The cost model used in tuning.
Definition task_scheduler.h:143
TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.TaskScheduler", TaskSchedulerObj, ffi::Object)
virtual ~TaskSchedulerObj()=default
The default destructor.
ffi::Function logger
The tuning task's logging function.
Definition task_scheduler.h:135
void TerminateTask(int task_id)
Terminate a task.
virtual int NextTaskId()=0
Fetch the next task id.
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.
ffi::Array< TaskRecord > tasks_
Records for each task.
Definition task_scheduler.h:137
virtual ffi::Array< RunnerResult > JoinRunningTask(int task_id)
Wait until the task is finished.
void PrintTuningStatistics()
Print out a human-readable format of the tuning statistics.
static void RegisterReflection()
Definition task_scheduler.h:150
ffi::Optional< Database > database_
The database used in tuning.
Definition task_scheduler.h:141
void TouchTask(int task_id)
Touch the task and update its status.
Managed reference to TaskSchedulerObj.
Definition task_scheduler.h:264
static TaskScheduler PyTaskScheduler(ffi::Function logger, PyTaskSchedulerObj::FNextTaskId f_next_task_id, PyTaskSchedulerObj::FJoinRunningTask f_join_running_task, PyTaskSchedulerObj::FTune f_tune)
Create a task scheduler with customized methods on the python-side.
TaskScheduler(ffi::ObjectPtr< TaskSchedulerObj > data)
Definition task_scheduler.h:266
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TaskScheduler, ffi::ObjectRef, TaskSchedulerObj)
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.
Managed reference to TuneContextObj.
Definition tune_context.h:100
Parent compiler symbol visibility.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
Random number generator. It provides a generic interface consistent with std::uniform_random_bit_gene...