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 {
40namespace meta_schedule {
41
42class TaskRecordObj : public ffi::Object {
43 public:
45 TuneContext ctx{ffi::UnsafeInit()};
47 double task_weight{1.0};
49 double flop{1.0};
51 bool is_terminated = false;
57 std::vector<double> latency_ms = {};
59 ffi::Optional<ffi::Array<MeasureCandidate>> measure_candidates = std::nullopt;
61 ffi::Optional<ffi::Array<BuilderResult>> builder_results = std::nullopt;
63 ffi::Optional<ffi::Array<RunnerFuture>> runner_futures = std::nullopt;
64
65 static void RegisterReflection() {
66 namespace refl = tvm::ffi::reflection;
67 refl::ObjectDef<TaskRecordObj>()
68 .def_ro("ctx", &TaskRecordObj::ctx)
69 .def_ro("task_weight", &TaskRecordObj::task_weight)
70 .def_ro("flop", &TaskRecordObj::flop)
71 .def_ro("is_terminated", &TaskRecordObj::is_terminated)
72 .def_ro("build_error_count", &TaskRecordObj::build_error_count)
73 .def_ro("run_error_count", &TaskRecordObj::run_error_count)
74 .def_ro("measure_candidates", &TaskRecordObj::measure_candidates)
75 .def_ro("builder_results", &TaskRecordObj::builder_results)
76 .def_ro("runner_futures", &TaskRecordObj::runner_futures);
77 }
78
79 static constexpr const bool _type_mutable = true;
80 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.TaskRecord", TaskRecordObj, ffi::Object);
81};
82
87class TaskRecord : public ffi::ObjectRef {
88 public:
90 explicit TaskRecord(TuneContext task, double task_weight);
91
93};
94
131class TaskSchedulerObj : public ffi::Object {
132 public:
134 ffi::Function logger;
136 ffi::Array<TaskRecord> tasks_;
138 ffi::Array<MeasureCallback> measure_callbacks_;
140 ffi::Optional<Database> database_;
142 ffi::Optional<CostModel> cost_model_;
145
147 virtual ~TaskSchedulerObj() = default;
148
149 static void RegisterReflection() {
150 namespace refl = tvm::ffi::reflection;
151 refl::ObjectDef<TaskSchedulerObj>()
152 .def_ro("tasks_", &TaskSchedulerObj::tasks_)
153 .def_ro("measure_callbacks_", &TaskSchedulerObj::measure_callbacks_)
154 .def_ro("database_", &TaskSchedulerObj::database_)
155 .def_ro("cost_model_", &TaskSchedulerObj::cost_model_)
156 .def_ro("remaining_tasks_", &TaskSchedulerObj::remaining_tasks_);
157 }
158
163 virtual int NextTaskId() = 0;
169 virtual ffi::Array<RunnerResult> JoinRunningTask(int task_id);
183 virtual void Tune(ffi::Array<TuneContext> tasks, //
184 ffi::Array<FloatImm> task_weights, //
185 int max_trials_global, //
186 int max_trials_per_task, //
187 int num_trials_per_iter, //
188 Builder builder, //
189 Runner runner, //
190 ffi::Array<MeasureCallback> measure_callbacks, //
191 ffi::Optional<Database> database, //
192 ffi::Optional<CostModel> cost_model);
197 void TerminateTask(int task_id);
202 void TouchTask(int task_id);
205
206 static constexpr const bool _type_mutable = true;
207 TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.TaskScheduler", TaskSchedulerObj, ffi::Object);
208};
209
210class TaskScheduler;
211
214 public:
219 using FNextTaskId = ffi::TypedFunction<int()>;
224 using FJoinRunningTask = ffi::TypedFunction<ffi::Array<RunnerResult>(int)>;
226 using FTune = ffi::TypedFunction<void(ffi::Array<TuneContext> tasks, //
227 ffi::Array<FloatImm> task_weights, //
228 int max_trials_global, //
229 int max_trials_per_task, //
230 int num_trials_per_iter, //
231 Builder builder, //
232 Runner runner, //
233 ffi::Array<MeasureCallback> measure_callbacks, //
234 ffi::Optional<Database> database, //
235 ffi::Optional<CostModel> cost_model)>;
236
243
244 static void RegisterReflection() {
245 namespace refl = tvm::ffi::reflection;
246 refl::ObjectDef<PyTaskSchedulerObj>();
247 }
248
249 int NextTaskId() final;
250 ffi::Array<RunnerResult> JoinRunningTask(int task_id) final;
251 void Tune(ffi::Array<TuneContext> tasks, ffi::Array<FloatImm> task_weights, int max_trials_global,
252 int max_trials_per_task, int num_trials_per_iter, Builder builder, Runner runner,
253 ffi::Array<MeasureCallback> measure_callbacks, ffi::Optional<Database> database,
254 ffi::Optional<CostModel> cost_model) final;
255 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.PyTaskScheduler", PyTaskSchedulerObj,
257};
258
263class TaskScheduler : public ffi::ObjectRef {
264 public:
265 explicit TaskScheduler(ffi::ObjectPtr<TaskSchedulerObj> data) : ffi::ObjectRef(data) {
266 TVM_FFI_ICHECK(data != nullptr);
267 }
273 TVM_DLL static TaskScheduler RoundRobin(ffi::Function logger);
282 TVM_DLL static TaskScheduler GradientBased(ffi::Function logger, double alpha, int window_size,
296};
297
298} // namespace meta_schedule
299} // namespace s_tir
300} // namespace tvm
301
302#endif // TVM_S_TIR_META_SCHEDULE_TASK_SCHEDULER_H_
Managed reference class to FloatImmNode.
Definition expr.h:232
int64_t TRandState
Definition random_engine.h:47
Managed reference to BuilderObj.
Definition builder.h:139
Managed reference to CostModelObj.
Definition cost_model.h:135
Managed reference to DatabaseObj.
Definition database.h:470
Managed reference to MeasureCallbackObj.
Definition measure_callback.h:110
The task scheduler with customized methods on the python-side.
Definition task_scheduler.h:213
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:224
FTune f_tune
The packed function to the Tune function.
Definition task_scheduler.h:242
FJoinRunningTask f_join_running_task
The packed function to the JoinRunningTask function.
Definition task_scheduler.h:240
ffi::TypedFunction< int()> FNextTaskId
The function type of NextTaskId method.
Definition task_scheduler.h:219
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:235
FNextTaskId f_next_task_id
The packed function to the NextTaskId function.
Definition task_scheduler.h:238
static void RegisterReflection()
Definition task_scheduler.h:244
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:94
Managed reference to RunnerObj.
Definition runner.h:208
Definition task_scheduler.h:42
TuneContext ctx
The tune context of the task.
Definition task_scheduler.h:45
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:49
static constexpr const bool _type_mutable
Definition task_scheduler.h:79
static void RegisterReflection()
Definition task_scheduler.h:65
int build_error_count
Builder errors happens in the task.
Definition task_scheduler.h:53
double task_weight
The weight of the task.
Definition task_scheduler.h:47
ffi::Optional< ffi::Array< MeasureCandidate > > measure_candidates
The measure candidates.
Definition task_scheduler.h:59
int run_error_count
Runner errors happens in the task.
Definition task_scheduler.h:55
ffi::Optional< ffi::Array< BuilderResult > > builder_results
The building results.
Definition task_scheduler.h:61
std::vector< double > latency_ms
The latency of each run, in milliseconds.
Definition task_scheduler.h:57
ffi::Optional< ffi::Array< RunnerFuture > > runner_futures
Packed functions to fetch the runner results asynchronously.
Definition task_scheduler.h:63
bool is_terminated
Whether the tuning task has been stopped or finished.
Definition task_scheduler.h:51
Managed reference to TaskRecordObj.
Definition task_scheduler.h:87
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:131
ffi::Array< MeasureCallback > measure_callbacks_
The list of measure callbacks of the scheduler.
Definition task_scheduler.h:138
int remaining_tasks_
The number of remaining tasks to be tuned.
Definition task_scheduler.h:144
static constexpr const bool _type_mutable
Definition task_scheduler.h:206
ffi::Optional< CostModel > cost_model_
The cost model used in tuning.
Definition task_scheduler.h:142
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:134
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:136
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:149
ffi::Optional< Database > database_
The database used in tuning.
Definition task_scheduler.h:140
void TouchTask(int task_id)
Touch the task and update its status.
Managed reference to TaskSchedulerObj.
Definition task_scheduler.h:263
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:265
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:99
TIR expressions.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
Random number generator. It provides a generic interface consistent with std::uniform_random_bit_gene...