tvm
instruction.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_SCHEDULE_INSTRUCTION_H_
20 #define TVM_S_TIR_SCHEDULE_INSTRUCTION_H_
21 
22 #include <tvm/ffi/reflection/registry.h>
23 
24 #include <utility>
25 
26 namespace tvm {
27 
28 // Forward declaration
29 template <typename, typename>
31 
32 namespace s_tir {
33 using namespace tvm::tir;
34 
35 // Forward declaration
36 class Schedule;
37 
47  ffi::TypedFunction<ffi::Array<Any>(Schedule sch, const ffi::Array<Any>& inputs,
48  const ffi::Array<Any>& attrs, const Any& decision)>;
49 
59  ffi::TypedFunction<ffi::String(const ffi::Array<Any>& inputs, const ffi::Array<Any>& attrs,
60  const Any& decision, const ffi::Array<ffi::String>& outputs)>;
61 
68 using FInstructionAttrsAsJSON = ffi::TypedFunction<ObjectRef(ffi::Array<Any> attrs)>;
69 
76 using FInstructionAttrsFromJSON = ffi::TypedFunction<ffi::Array<Any>(ObjectRef json_attrs)>;
77 
90 class InstructionKindNode : public runtime::Object {
91  public:
93  ffi::String name;
100  bool is_pure{false};
102  FInstructionApply f_apply_to_schedule{nullptr};
104  FInstructionAsPython f_as_python{nullptr};
109  FInstructionAttrsAsJSON f_attrs_as_json{nullptr};
114  FInstructionAttrsFromJSON f_attrs_from_json{nullptr};
115 
116  static void RegisterReflection() {
117  namespace refl = tvm::ffi::reflection;
118  refl::ObjectDef<InstructionKindNode>()
119  .def_ro("name", &InstructionKindNode::name)
120  .def_ro("_is_pure", &InstructionKindNode::is_pure);
121  }
122 
124  bool IsPostproc() const;
125  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.InstructionKind", InstructionKindNode, runtime::Object);
126 };
127 
132 class InstructionKind : public runtime::ObjectRef {
133  public:
139  static InstructionKind Get(const ffi::String& name);
142 };
143 
145 class InstructionNode : public runtime::Object {
146  public:
160  ffi::Array<Any> inputs;
166  ffi::Array<Any> attrs;
173  ffi::Array<Any> outputs;
174 
175  static void RegisterReflection() {
176  namespace refl = tvm::ffi::reflection;
177  refl::ObjectDef<InstructionNode>()
178  .def_ro("kind", &InstructionNode::kind)
179  .def_ro("inputs", &InstructionNode::inputs)
180  .def_ro("attrs", &InstructionNode::attrs)
181  .def_ro("outputs", &InstructionNode::outputs);
182  }
183  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.Instruction", InstructionNode, runtime::Object);
184 };
185 
190 class Instruction : public runtime::ObjectRef {
191  public:
199  explicit Instruction(InstructionKind kind, ffi::Array<Any> inputs, ffi::Array<Any> attrs,
200  ffi::Array<Any> outputs);
201 
203 };
204 
210 #define TVM_INST_KIND_REGISTER_VAR_DEF \
211  static DMLC_ATTRIBUTE_UNUSED ::tvm::s_tir::InstructionKindRegEntry& __make_##InstructionKind
212 
230 #define TVM_REGISTER_INST_KIND(InstructionKindName) \
231  TVM_STR_CONCAT(TVM_INST_KIND_REGISTER_VAR_DEF, __COUNTER__) = \
232  ::tvm::s_tir::InstructionKindRegEntry::RegisterOrGet(InstructionKindName).set_name()
233 
236  public:
237  static InstructionKindRegEntry& RegisterOrGet(const ffi::String& name);
238 
240  get_mutable()->name = this->name;
241  return *this;
242  }
243 
245  get_mutable()->is_pure = is_pure;
246  return *this;
247  }
248 
250  get_mutable()->f_apply_to_schedule = std::move(f_apply_to_schedule);
251  return *this;
252  }
253 
255  get_mutable()->f_as_python = std::move(f_as_python);
256  return *this;
257  }
258 
260  get_mutable()->f_attrs_as_json = std::move(f_attrs_as_json);
261  return *this;
262  }
263 
265  get_mutable()->f_attrs_from_json = std::move(f_attrs_from_json);
266  return *this;
267  }
268 
269  private:
271  explicit InstructionKindRegEntry(uint32_t reg_index);
273  InstructionKindNode* get_mutable() const {
274  return const_cast<InstructionKindNode*>(inst_kind_.get());
275  }
276 
278  ffi::String name;
280  InstructionKind inst_kind_;
281  template <typename, typename>
282  friend class ::tvm::AttrRegistry;
283  friend class InstructionKind;
284 };
285 
286 } // namespace s_tir
287 } // namespace tvm
288 
289 #endif // TVM_S_TIR_SCHEDULE_INSTRUCTION_H_
Definition: instruction.h:30
Kind of an instruction, e.g. Split, Reorder, etc. Besides the name, every kind of instruction has its...
Definition: instruction.h:90
ffi::String name
The name of a kind of instructions.
Definition: instruction.h:93
bool IsPostproc() const
Checks if the instruction kind is EnterPostproc.
bool is_pure
Indicates if the instruction is pure, i.e. removing it alone doesn't mutate the schedule state....
Definition: instruction.h:100
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.InstructionKind", InstructionKindNode, runtime::Object)
static void RegisterReflection()
Definition: instruction.h:116
An entry in the registry of InstructionKind.
Definition: instruction.h:235
static InstructionKindRegEntry & RegisterOrGet(const ffi::String &name)
InstructionKindRegEntry & set_apply_to_schedule(FInstructionApply f_apply_to_schedule)
Definition: instruction.h:249
InstructionKindRegEntry & set_attrs_as_json(FInstructionAttrsAsJSON f_attrs_as_json)
Definition: instruction.h:259
InstructionKindRegEntry & set_as_python(FInstructionAsPython f_as_python)
Definition: instruction.h:254
InstructionKindRegEntry & set_attrs_from_json(FInstructionAttrsFromJSON f_attrs_from_json)
Definition: instruction.h:264
InstructionKindRegEntry & set_is_pure(bool is_pure)
Definition: instruction.h:244
InstructionKindRegEntry & set_name()
Definition: instruction.h:239
Managed reference to InstructionKindNode.
Definition: instruction.h:132
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(InstructionKind, runtime::ObjectRef, InstructionKindNode)
static InstructionKind Get(const ffi::String &name)
Retrieve an InstructionKind using its name.
Schedule instructions each corresponds to a schedule primitive.
Definition: instruction.h:145
InstructionKind kind
The kind of the instruction.
Definition: instruction.h:148
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.Instruction", InstructionNode, runtime::Object)
static void RegisterReflection()
Definition: instruction.h:175
ffi::Array< Any > attrs
The attributes of the instruction. Similar to attributes of an operator, attributes of an instruction...
Definition: instruction.h:166
ffi::Array< Any > inputs
The input random variables of the instruction, and the type of each element can be one of the followi...
Definition: instruction.h:160
ffi::Array< Any > outputs
The output random variables of the instruction, and the type of each element can be one of the follow...
Definition: instruction.h:173
Managed reference to InstructionNode.
Definition: instruction.h:190
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Instruction, runtime::ObjectRef, InstructionNode)
Instruction(InstructionKind kind, ffi::Array< Any > inputs, ffi::Array< Any > attrs, ffi::Array< Any > outputs)
Constructor.
Managed reference to ScheduleNode.
Definition: schedule.h:897
Definition: repr_printer.h:91
ffi::TypedFunction< ffi::Array< Any >(Schedule sch, const ffi::Array< Any > &inputs, const ffi::Array< Any > &attrs, const Any &decision)> FInstructionApply
Type of the functor that applies the instruction to a TensorIR schedule.
Definition: instruction.h:48
ffi::TypedFunction< ffi::String(const ffi::Array< Any > &inputs, const ffi::Array< Any > &attrs, const Any &decision, const ffi::Array< ffi::String > &outputs)> FInstructionAsPython
Type of the functor that converts the instruction to a statement in python syntax.
Definition: instruction.h:60
ffi::TypedFunction< ffi::Array< Any >(ObjectRef json_attrs)> FInstructionAttrsFromJSON
Type of the functor that deserialize its attributes from JSON.
Definition: instruction.h:76
ffi::TypedFunction< ObjectRef(ffi::Array< Any > attrs)> FInstructionAttrsAsJSON
Type of the functor that serialize its attributes to JSON.
Definition: instruction.h:68
Definition: axis_group_graph.h:39
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37