tvm
Loading...
Searching...
No Matches
schedule_rule.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
20#ifndef TVM_S_TIR_META_SCHEDULE_SCHEDULE_RULE_H_
21#define TVM_S_TIR_META_SCHEDULE_SCHEDULE_RULE_H_
22
23#include <tvm/ffi/container/array.h>
24#include <tvm/ffi/container/map.h>
25#include <tvm/ffi/function.h>
26#include <tvm/ffi/optional.h>
27#include <tvm/ffi/reflection/registry.h>
28#include <tvm/ffi/string.h>
29#include <tvm/ir/expr.h>
30#include <tvm/ir/prim/expr.h>
32
33namespace tvm {
34namespace s_tir {
35using namespace tvm::prim;
36namespace meta_schedule {
37
38class TuneContext;
39class ScheduleRule;
40
42class ScheduleRuleNode : public ffi::Object {
43 public:
45 virtual ~ScheduleRuleNode() = default;
46
47 static void RegisterReflection() {
48 namespace refl = tvm::ffi::reflection;
49 refl::ObjectDef<ScheduleRuleNode>();
50 }
51
58
65 virtual ffi::Array<s_tir::Schedule> Apply(const s_tir::Schedule& sch,
66 const s_tir::SBlockRV& block) = 0;
67
72 virtual ScheduleRule Clone() const = 0;
73
74 static constexpr const bool _type_mutable = true;
75 TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.ScheduleRule", ScheduleRuleNode, ffi::Object);
76};
77
82class ScheduleRule : public ffi::ObjectRef {
83 public:
88 using FInitializeWithTuneContext = ffi::TypedFunction<void(const TuneContext&)>;
95 using FApply = ffi::TypedFunction<ffi::Array<s_tir::Schedule>(const s_tir::Schedule&,
96 const s_tir::SBlockRV&)>;
101 using FClone = ffi::TypedFunction<ScheduleRule()>;
122 bool into_consumer, //
123 bool inline_const_tensor, //
124 bool disallow_if_then_else, //
125 bool require_injective, //
126 bool require_ordered, //
127 ffi::Optional<ffi::Array<ffi::String>> disallow_op);
128
137
158 ffi::String structure, //
159 ffi::Optional<ffi::Array<ffi::String>> tile_binds, //
160 ffi::Optional<int64_t> max_innermost_factor, //
161 ffi::Optional<ffi::Array<int64_t>> vector_load_lens, //
162 ffi::Optional<ffi::Map<ffi::String, ffi::Any>> reuse_read, //
163 ffi::Optional<ffi::Map<ffi::String, ffi::Any>> reuse_write,
164 ffi::Optional<ffi::Function> filter_fn = std::nullopt);
165
184 ffi::String intrin_name, ffi::String structure,
185 ffi::Optional<ffi::Array<ffi::String>> tile_binds,
186 ffi::Optional<int64_t> max_innermost_factor,
187 ffi::Optional<ffi::Array<int64_t>> vector_load_lens,
188 ffi::Optional<ffi::Map<ffi::String, ffi::Any>> reuse_read,
189 ffi::Optional<ffi::Map<ffi::String, ffi::Any>> reuse_write);
190
212 ffi::Array<ffi::Map<ffi::String, ffi::String>> intrin_groups, ffi::String structure,
213 ffi::Optional<ffi::Array<ffi::String>> tile_binds,
214 ffi::Optional<int64_t> max_innermost_factor,
215 ffi::Optional<ffi::Array<int64_t>> vector_load_lens,
216 ffi::Optional<ffi::Map<ffi::String, ffi::Any>> reuse_read,
217 ffi::Optional<ffi::Map<ffi::String, ffi::Any>> reuse_write, bool use_software_pipeline);
218
232 ffi::Optional<int64_t> max_innermost_factor,
233 ffi::Optional<ffi::Map<ffi::String, ffi::Any>> reuse_read,
234 ffi::Optional<ffi::Map<ffi::String, ffi::Any>> reuse_write);
235
245 ffi::Optional<int64_t> max_innermost_factor);
273 ffi::Array<int64_t> unroll_max_steps, //
274 bool unroll_explicit);
284 int max_threads_per_block = -1);
293 FInitializeWithTuneContext f_initialize_with_tune_context, //
294 FApply f_apply, //
295 FClone f_clone);
296
298 TVM_DLL static ffi::Array<ScheduleRule, void> DefaultLLVM();
300 TVM_DLL static ffi::Array<ScheduleRule, void> DefaultX86(const ffi::String& type);
302 TVM_DLL static ffi::Array<ScheduleRule, void> DefaultCUDA();
304 TVM_DLL static ffi::Array<ScheduleRule, void> DefaultCUDATensorCore();
306 TVM_DLL static ffi::Array<ScheduleRule, void> DefaultHexagon();
308 TVM_DLL static ffi::Array<ScheduleRule, void> DefaultARM(const ffi::String& type);
310 TVM_DLL static ffi::Array<ScheduleRule, void> DefaultRISCV(int vlen);
311
313};
314
317 public:
321
328
329 static void RegisterReflection() {
330 // `f_initialize_with_tune_context` is not registered
331 // `f_apply` is not registered
332 // `f_clone` is not registered
333 namespace refl = tvm::ffi::reflection;
334 refl::ObjectDef<PyScheduleRuleNode>();
335 }
336
338 ffi::Array<s_tir::Schedule> Apply(const s_tir::Schedule& sch, const s_tir::SBlockRV& block) final;
340 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.PyScheduleRule", PyScheduleRuleNode,
342};
343
344} // namespace meta_schedule
345} // namespace s_tir
346} // namespace tvm
347
348#endif // TVM_S_TIR_META_SCHEDULE_SCHEDULE_RULE_H_
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
Managed reference to SBlockRVNode.
Definition schedule.h:67
Managed reference to ScheduleNode.
Definition schedule.h:887
The schedule rule with customized methods on the python-side.
Definition schedule_rule.h:316
FInitializeWithTuneContext f_initialize_with_tune_context
The packed function to the InitializeWithTuneContext function.
Definition schedule_rule.h:323
ScheduleRule Clone() const final
Deep clone the schedule rule.
ScheduleRule::FClone FClone
Definition schedule_rule.h:320
ScheduleRule::FInitializeWithTuneContext FInitializeWithTuneContext
Definition schedule_rule.h:318
static void RegisterReflection()
Definition schedule_rule.h:329
ScheduleRule::FApply FApply
Definition schedule_rule.h:319
FApply f_apply
The packed function to the Apply function.
Definition schedule_rule.h:325
ffi::Array< s_tir::Schedule > Apply(const s_tir::Schedule &sch, const s_tir::SBlockRV &block) final
Apply a schedule rule to the specific block in the given schedule.
FClone f_clone
The packed function to the Clone function.
Definition schedule_rule.h:327
void InitializeWithTuneContext(const TuneContext &context) final
Initialize the design space generator with tuning context.
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.PyScheduleRule", PyScheduleRuleNode, ScheduleRuleNode)
Rules to modify a block in a schedule.
Definition schedule_rule.h:42
virtual ffi::Array< s_tir::Schedule > Apply(const s_tir::Schedule &sch, const s_tir::SBlockRV &block)=0
Apply a schedule rule to the specific block in the given schedule.
virtual ScheduleRule Clone() const =0
Deep clone the schedule rule.
static void RegisterReflection()
Definition schedule_rule.h:47
virtual ~ScheduleRuleNode()=default
Virtual destructor.
virtual void InitializeWithTuneContext(const TuneContext &context)=0
Initialize the design space generator with tuning context.
TVM_FFI_DECLARE_OBJECT_INFO("s_tir.meta_schedule.ScheduleRule", ScheduleRuleNode, ffi::Object)
static constexpr const bool _type_mutable
Definition schedule_rule.h:74
Managed reference to ScheduleRuleNode.
Definition schedule_rule.h:82
static ScheduleRule ParallelizeVectorizeUnroll(int max_jobs_per_core, int max_vectorize_extent, ffi::Array< int64_t > unroll_max_steps, bool unroll_explicit)
Mark parallelize, vectorize and unroll to the root block. The mark will be applied to each block in a...
static ffi::Array< ScheduleRule, void > DefaultCUDA()
Create default schedule rules for CUDA.
static ffi::Array< ScheduleRule, void > DefaultARM(const ffi::String &type)
Create default schedule rules for ARM CPU (NEON and DOTPROD)
static ScheduleRule MultiLevelTiling(ffi::String structure, ffi::Optional< ffi::Array< ffi::String > > tile_binds, ffi::Optional< int64_t > max_innermost_factor, ffi::Optional< ffi::Array< int64_t > > vector_load_lens, ffi::Optional< ffi::Map< ffi::String, ffi::Any > > reuse_read, ffi::Optional< ffi::Map< ffi::String, ffi::Any > > reuse_write, ffi::Optional< ffi::Function > filter_fn=std::nullopt)
Create a mega rule: multi-level tiling with data reuse.
static ffi::Array< ScheduleRule, void > DefaultRISCV(int vlen)
Create default schedule rules for RISCV CPU (RVV)
static ScheduleRule RandomComputeLocation()
A rule that randomly select a compute-at location for a free block.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(ScheduleRule, ffi::ObjectRef, ScheduleRuleNode)
static ScheduleRule ApplyCustomRule()
Create a rule that applies customized rules registered using block attribute schedule_rule....
static ScheduleRule CrossThreadReduction(ffi::Array< int64_t > thread_extents)
Create a schedule rule which applies cross-thread reduction to some reduction blocks correspondingly ...
static ffi::Array< ScheduleRule, void > DefaultLLVM()
Create default schedule rules for LLVM.
static ScheduleRule InlineConstantScalars()
Inline blocks that produce a constant scalar. Such blocks get in the way of ReverseComputeInline duri...
ffi::TypedFunction< ffi::Array< s_tir::Schedule >(const s_tir::Schedule &, const s_tir::SBlockRV &)> FApply
The function type of Apply method.
Definition schedule_rule.h:96
ffi::TypedFunction< ScheduleRule()> FClone
The function type of Clone method.
Definition schedule_rule.h:101
static ffi::Array< ScheduleRule, void > DefaultX86(const ffi::String &type)
Create default schedule rules for x86 (AVX512 and VNNI)
static ScheduleRule PyScheduleRule(FInitializeWithTuneContext f_initialize_with_tune_context, FApply f_apply, FClone f_clone)
Create a schedule rule with customized methods on the python-side.
static ScheduleRule AddRFactor(int max_jobs_per_core, ffi::Optional< int64_t > max_innermost_factor)
Create a rule: add-rfactor to some blocks if needed.
static bool IsApplyCustomRule(const ScheduleRule &rule)
Check if the rule is ApplyCustomRule
ffi::TypedFunction< void(const TuneContext &)> FInitializeWithTuneContext
The function type of InitializeWithTuneContext method.
Definition schedule_rule.h:88
static ScheduleRule MultiLevelTilingWideVector(ffi::String structure, int64_t vector_length_in_bits, ffi::Optional< int64_t > max_innermost_factor, ffi::Optional< ffi::Map< ffi::String, ffi::Any > > reuse_read, ffi::Optional< ffi::Map< ffi::String, ffi::Any > > reuse_write)
Extension of MultiLevelTiling for backends with wide vectors. The loop over the innermost spatial axi...
static ScheduleRule MultiLevelTilingTensorCore(ffi::Array< ffi::Map< ffi::String, ffi::String > > intrin_groups, ffi::String structure, ffi::Optional< ffi::Array< ffi::String > > tile_binds, ffi::Optional< int64_t > max_innermost_factor, ffi::Optional< ffi::Array< int64_t > > vector_load_lens, ffi::Optional< ffi::Map< ffi::String, ffi::Any > > reuse_read, ffi::Optional< ffi::Map< ffi::String, ffi::Any > > reuse_write, bool use_software_pipeline)
Extension of MultiLevelTiling for auto-tensorization with multiple groups of candidate tensor core in...
static ffi::Array< ScheduleRule, void > DefaultHexagon()
Create default schedule rules for Hexagon.
static ffi::Array< ScheduleRule, void > DefaultCUDATensorCore()
Create default postprocessors for CUDA with TensorCore.
static ScheduleRule MultiLevelTilingWithIntrin(ffi::String intrin_name, ffi::String structure, ffi::Optional< ffi::Array< ffi::String > > tile_binds, ffi::Optional< int64_t > max_innermost_factor, ffi::Optional< ffi::Array< int64_t > > vector_load_lens, ffi::Optional< ffi::Map< ffi::String, ffi::Any > > reuse_read, ffi::Optional< ffi::Map< ffi::String, ffi::Any > > reuse_write)
Extension of MultiLevelTiling for auto-tensorization with a single intrinsic.
static ScheduleRule AutoBind(int max_threadblocks, ffi::Array< int64_t > thread_extents, int max_threads_per_block=-1)
Auto bind loops around the block to BlockIdx and ThreadIdx.
static ScheduleRule AutoInline(bool into_producer, bool into_consumer, bool inline_const_tensor, bool disallow_if_then_else, bool require_injective, bool require_ordered, ffi::Optional< ffi::Array< ffi::String > > disallow_op)
Create an auto-inline rule that inlines spatial blocks if it satisfies some conditions.
Managed reference to TuneContextNode.
Definition tune_context.h:101
Base expr nodes in TVM.
TIR expressions.
Definition builtin.h:25
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40