tvm
schedule.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_TIR_SCHEDULE_SCHEDULE_H_
20 #define TVM_TIR_SCHEDULE_SCHEDULE_H_
21 
23 #include <tvm/tir/index_map.h>
24 #include <tvm/tir/schedule/state.h>
25 #include <tvm/tir/schedule/trace.h>
26 
27 namespace tvm {
28 namespace tir {
29 
31 enum class ScheduleErrorRenderLevel : int32_t {
33  kDetail = 0,
35  kFast = 1,
37  kNone = 2,
38 };
39 
41 enum class BufferIndexType : int32_t {
43  kRead = 0,
45  kWrite = 1,
46 };
47 
48 /**************** Random variable: BlockRV ****************/
49 
51 class BlockRVNode : public runtime::Object {
52  public:
54  static constexpr const char* _type_key = "tir.BlockRV";
56 };
57 
62 class BlockRV : public runtime::ObjectRef {
63  public:
65  TVM_DLL BlockRV();
67 };
68 
69 /**************** Random variable: LoopRV ****************/
70 
72 class LoopRVNode : public runtime::Object {
73  public:
75  static constexpr const char* _type_key = "tir.LoopRV";
77 };
78 
83 class LoopRV : public runtime::ObjectRef {
84  public:
86  TVM_DLL LoopRV();
88 };
89 
90 /**************** Random variable: ExprRV ****************/
91 
93 using ExprRV = PrimExpr;
94 
96 
97 /**************** The Schedule class ****************/
98 
99 class Schedule;
100 
103  friend class Schedule;
104 
105  public:
106  virtual ~ScheduleNode() = default;
107 
108  static constexpr const char* _type_key = "tir.Schedule";
110 
111  public:
113  virtual IRModule mod() const { return state()->mod; }
115  virtual ScheduleState state() const = 0;
117  virtual Optional<Trace> trace() const = 0;
126  virtual Schedule Copy() = 0;
131  virtual void Seed(support::LinearCongruentialEngine::TRandState seed) = 0;
133  virtual support::LinearCongruentialEngine::TRandState ForkSeed() = 0;
134 
135  public:
136  /******** Lookup/Remove random variables ********/
142  virtual Block Get(const BlockRV& block_rv) const = 0;
148  virtual For Get(const LoopRV& loop_rv) const = 0;
154  virtual PrimExpr Get(const ExprRV& expr_rv) const = 0;
160  virtual StmtSRef GetSRef(const BlockRV& block_rv) const = 0;
166  virtual StmtSRef GetSRef(const LoopRV& loop_rv) const = 0;
172  virtual bool HasBlock(const BlockRV& block_rv) const = 0;
178  virtual StmtSRef GetSRef(const StmtNode* stmt) const;
184  StmtSRef GetSRef(const Stmt& stmt) const { return this->GetSRef(stmt.get()); }
189  virtual void RemoveRV(const BlockRV& block_rv) = 0;
194  virtual void RemoveRV(const LoopRV& loop_rv) = 0;
199  virtual void RemoveRV(const ExprRV& expr_rv) = 0;
200 
201  public:
202  /******** Schedule: Sampling ********/
210  virtual ExprRV SampleCategorical(const Array<Integer>& candidates, const Array<FloatImm>& probs,
211  Optional<Integer> decision = NullOpt) = 0;
220  virtual Array<ExprRV> SamplePerfectTile(const LoopRV& loop_rv, int n, int max_innermost_factor,
221  Optional<Array<Integer>> decision = NullOpt) = 0;
228  virtual LoopRV SampleComputeLocation(const BlockRV& block_rv,
229  Optional<Integer> decision = NullOpt) = 0;
230 
231  /******** Schedule: Get blocks & loops ********/
239  virtual BlockRV GetBlock(const String& name, const String& func_name = "main") = 0;
245  virtual Array<LoopRV> GetLoops(const BlockRV& block_rv) = 0;
251  virtual Array<BlockRV> GetChildBlocks(const BlockRV& block_rv) = 0;
257  virtual Array<BlockRV> GetChildBlocks(const LoopRV& loop_rv) = 0;
264  virtual Array<BlockRV> GetProducers(const BlockRV& block_rv) = 0;
271  virtual Array<BlockRV> GetConsumers(const BlockRV& block_rv) = 0;
272  /******** Schedule: Transform loops ********/
283  virtual LoopRV Fuse(const Array<LoopRV>& loop_rvs, bool preserve_unit_iters = true) = 0;
294  virtual Array<LoopRV> Split(const LoopRV& loop_rv, const Array<Optional<ExprRV>>& factors,
295  bool preserve_unit_iters = true) = 0;
308  virtual void Reorder(const Array<LoopRV>& ordered_loop_rvs) = 0;
314  virtual LoopRV AddUnitLoop(const BlockRV& block_rv) = 0;
320  virtual LoopRV AddUnitLoop(const LoopRV& loop_rv) = 0;
321  /******** Schedule: Manipulate ForKind ********/
331  virtual void Parallel(const LoopRV& loop_rv) = 0;
341  virtual void Vectorize(const LoopRV& loop_rv) = 0;
353  virtual void Bind(const LoopRV& loop_rv, const String& thread_axis) = 0;
358  virtual void Unroll(const LoopRV& loop_rv) = 0;
359  /******** Schedule: Insert cache stages ********/
369  virtual BlockRV CacheRead(const BlockRV& block_rv, int read_buffer_index,
370  const String& storage_scope) = 0;
380  virtual BlockRV CacheWrite(const BlockRV& block_rv, int write_buffer_index,
381  const String& storage_scope) = 0;
393  virtual BlockRV ReIndex(const BlockRV& block_rv, int buffer_index,
394  BufferIndexType buffer_index_type) = 0;
395  /******** Schedule: Compute location ********/
412  virtual void ComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
413  bool preserve_unit_loops) = 0;
429  virtual void ReverseComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
430  bool preserve_unit_loops) = 0;
441  virtual void ComputeInline(const BlockRV& block) = 0;
453  virtual void ReverseComputeInline(const BlockRV& block) = 0;
454  /******** Schedule: Reduction ********/
470  virtual BlockRV DecomposeReduction(const BlockRV& block_rv, const LoopRV& loop_rv) = 0;
488  virtual BlockRV RFactor(const LoopRV& loop_rv, int factor_axis) = 0;
489  /******** Schedule: Block annotation ********/
502  virtual void StorageAlign(const BlockRV& block_rv, int buffer_index, int axis, int factor,
503  int offset) = 0;
511  virtual void SetScope(const BlockRV& block_rv, int buffer_index, const String& storage_scope) = 0;
512  /******** Schedule: Blockize & Tensorize ********/
518  virtual BlockRV Blockize(const LoopRV& loop_rv) = 0;
524  virtual void Tensorize(const LoopRV& loop_rv, const String& intrin) = 0;
530  virtual void Tensorize(const BlockRV& block_rv, const String& intrin) = 0;
531 
532  /******** Schedule: Annotation ********/
539  virtual void Annotate(const LoopRV& loop_rv, const String& ann_key, const ObjectRef& ann_val) = 0;
546  virtual void Annotate(const BlockRV& block_rv, const String& ann_key,
547  const ObjectRef& ann_val) = 0;
553  virtual void Unannotate(const LoopRV& loop_rv, const String& ann_key) = 0;
559  virtual void Unannotate(const BlockRV& block_rv, const String& ann_key) = 0;
560 
561  /******** Schedule: Layout transformation ********/
573  virtual void TransformLayout(const BlockRV& block_rv, int buffer_index,
574  BufferIndexType buffer_index_type, const IndexMap& index_map) = 0;
575 
584  virtual void TransformBlockLayout(const BlockRV& block_rv, const IndexMap& index_map) = 0;
585 
594  virtual void SetAxisSeparator(const BlockRV& block_rv, int buffer_index,
595  BufferIndexType buffer_index_type,
596  const Array<IntImm>& axis_separators) = 0;
597 
598  /******** Schedule: Misc ********/
600  virtual void EnterPostproc() = 0;
601 };
602 
618 class Schedule : public runtime::ObjectRef {
619  public:
634  int debug_mask, ScheduleErrorRenderLevel error_render_level);
648  TVM_DLL static Schedule Traced(IRModule mod, support::LinearCongruentialEngine::TRandState seed,
649  int debug_mask, ScheduleErrorRenderLevel error_render_level);
651 };
652 
653 } // namespace tir
654 } // namespace tvm
655 
656 #endif // TVM_TIR_SCHEDULE_SCHEDULE_H_
IterVar thread_axis(Range dom, std::string tag)
Create a new IterVar that represents an axis in thread.
Base node of all statements.
Definition: stmt.h:38
void VisitAttrs(tvm::AttrVisitor *v)
Definition: schedule.h:53
Managed reference to BlockNode.
Definition: stmt.h:1260
Random number generator. It provides a generic interface consistent with std::uniform_random_bit_gene...
Type Bind(const Type &type, const Map< TypeVar, Type > &args_map)
Bind free type variables in the type.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
Managed reference to LoopRVNode.
Definition: schedule.h:83
Index of a written buffer.
Managed reference to ForNode.
Definition: stmt.h:967
StmtSRef GetSRef(const Stmt &stmt) const
Get the block/loop sref corresponding to the specific statement.
Definition: schedule.h:184
Managed reference to StmtSRefNode.
Definition: block_scope.h:102
base class of all object containers.
Definition: object.h:167
#define TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:744
ScheduleErrorRenderLevel
The level of detailed error message rendering.
Definition: schedule.h:31
Render a detailed error message.
Managed reference to ScheduleNode.
Definition: schedule.h:618
Defines a remapping of buffer indices.
int64_t TRandState
Definition: random_engine.h:54
Visitor class to get the attributes of an AST/IR node. The content is going to be called for each fie...
Definition: reflection.h:52
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:270
Definition: index_map.h:154
Container of all statements.
Definition: stmt.h:57
Reference to string objects.
Definition: string.h:124
Render the error in fast mode.
const Object * get() const
Definition: object.h:546
The user-facing schedule class.
Definition: schedule.h:102
Managed reference to ScheduleStateNode.
Definition: state.h:190
Base class of all object reference.
Definition: object.h:511
virtual IRModule mod() const
Get the IRModule associated with this schedule.
Definition: schedule.h:113
constexpr const char * axis_separators
Marks the physical axis separators.
Definition: stmt.h:1393
This file defines ScheduleState, the core data structure of TensorIR scheduling.
#define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
helper macro to declare type information in a final class.
Definition: object.h:671
Managed reference class to IRModuleNode.
Definition: module.h:360
A random variable that evaluates to a TensorIR for loop.
Definition: schedule.h:72
A random variable that evaluates to a TensorIR block.
Definition: schedule.h:51
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
BufferIndexType
Type of buffer index.
Definition: schedule.h:41
void VisitAttrs(tvm::AttrVisitor *v)
Definition: schedule.h:74
Reference to PrimExprNode.
Definition: expr.h:112
tvm::PrimExpr mod(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:290
constexpr runtime::NullOptType NullOpt
Definition: optional.h:160
Index of a read buffer.
std::vector< std::string > Split(const std::string &str, const std::string &sub)
Split str according to substring.
Definition: einsum.h:425
#define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:728
Managed reference to BlockRVNode.
Definition: schedule.h:62
Base node of all primitive expressions.
Definition: expr.h:85