tvm
Loading...
Searching...
No Matches
op.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
25#ifndef TVM_IR_OP_H_
26#define TVM_IR_OP_H_
27
28#include <tvm/ffi/error.h>
29#include <tvm/ffi/function.h>
30#include <tvm/ffi/reflection/registry.h>
32#include <tvm/ir/attrs.h>
33#include <tvm/ir/env_func.h>
34#include <tvm/ir/expr.h>
35#include <tvm/ir/type.h>
36
37#include <string>
38#include <utility>
39#include <vector>
40
41namespace tvm {
42
43// forward declare name.
44template <typename>
45class OpAttrMap;
46
54class ArgumentInfoNode : public ffi::Object {
55 public:
57 ffi::String name;
59 ffi::String type_info;
61 ffi::String description;
62
63 static void RegisterReflection() {
64 namespace rfl = ffi::reflection;
65 rfl::ObjectDef<ArgumentInfoNode>()
66 .def_ro("name", &ArgumentInfoNode::name)
67 .def_ro("type_info", &ArgumentInfoNode::type_info)
68 .def_ro("description", &ArgumentInfoNode::description);
69 }
70
72
73 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.ArgumentInfo", ArgumentInfoNode, ffi::Object);
74};
75
77class ArgumentInfo : public ffi::ObjectRef {
78 public:
80};
81
82// TODO(tvm-team): migrate low-level intrinsics to use Op
94class OpNode : public ExprNode {
95 public:
97 ffi::String name;
102 ffi::String description;
103 /* \brief Information of input arguments to the operator */
104 ffi::Array<ArgumentInfo> arguments;
109 ffi::String attrs_type_key;
126
127 static void RegisterReflection() {
128 namespace refl = tvm::ffi::reflection;
129 refl::ObjectDef<OpNode>()
130 .def_ro("name", &OpNode::name)
131 .def_ro("description", &OpNode::description, refl::AttachFieldFlag::SEqHashIgnore())
132 .def_ro("arguments", &OpNode::arguments, refl::AttachFieldFlag::SEqHashIgnore())
133 .def_ro("attrs_type_key", &OpNode::attrs_type_key, refl::AttachFieldFlag::SEqHashIgnore())
134 .def_ro("num_inputs", &OpNode::num_inputs, refl::AttachFieldFlag::SEqHashIgnore())
135 .def_ro("support_level", &OpNode::support_level, refl::AttachFieldFlag::SEqHashIgnore());
136 }
137
140
141 private:
143 uint32_t AttrRegistryIndex() const { return index_; }
145 std::string AttrRegistryName() const { return name; }
146
147 // friend class
148 template <typename>
150 template <typename, typename>
151 friend class AttrRegistry;
152 friend class OpRegEntry;
153
154 // Program internal unique index of operator.
155 // Used to help index the program.
156 uint32_t index_{0};
157};
158
163class Op : public Expr {
164 public:
165 explicit Op(ffi::ObjectPtr<OpNode> n) : Expr(std::move(n)) {
166 TVM_FFI_CHECK(defined(), ValueError) << "Op expects a defined OpNode";
167 }
168
176 template <typename ValueType>
177 inline static OpAttrMap<ValueType> GetAttrMap(const ffi::String& attr_name);
183 TVM_DLL static bool HasAttrMap(const ffi::String& attr_name);
190 TVM_DLL static const Op& Get(const ffi::String& op_name);
191
193
194 private:
200 TVM_DLL static const AttrRegistryMapContainerMap<Op>& GetAttrMapContainer(const ffi::String& key);
201};
202
208 public:
210 const Op& op() const { return op_; }
217 inline OpRegEntry& describe(const std::string& descr); // NOLINT(*)
225 inline OpRegEntry& add_argument(const std::string& name, const std::string& type,
226 const std::string& description);
232 template <typename AttrsType>
233 inline OpRegEntry& set_attrs_type();
239 inline OpRegEntry& set_attrs_type_key(const ffi::String& key);
245 inline OpRegEntry& set_num_inputs(int32_t n); // NOLINT(*)
251 inline OpRegEntry& set_support_level(int32_t level); // NOLINT(*)
265 template <typename ValueType>
266 inline OpRegEntry& set_attr(const std::string& attr_name, // NOLINT(*)
267 const ValueType& value, int plevel = 10);
268
273 inline void reset_attr(const std::string& attr_name);
274
275 // set the name of the op to be the same as registry
276 inline OpRegEntry& set_name() { // NOLINT(*)
277 if (get()->name.length() == 0) {
278 get()->name = name;
279 }
280 return *this;
281 }
287 TVM_DLL static OpRegEntry& RegisterOrGet(const ffi::String& name);
288
289 private:
290 template <typename, typename>
291 friend class AttrRegistry;
292 // the name
293 std::string name;
295 Op op_;
297 static Op MakeOp(uint32_t reg_index);
298 // private constructor
300 // return internal pointer to op.
301 inline OpNode* get();
302 // update the attribute OpAttrMap
303 TVM_DLL void UpdateAttr(const ffi::String& key, ffi::Any value, int plevel);
304};
305
310template <typename ValueType>
311class OpAttrMap : public AttrRegistryMap<Op, ValueType> {
312 public:
320 inline ValueType get(const Expr& expr, ValueType def_value) const;
321
323 using TParent::count;
324 using TParent::get;
325 using TParent::operator[];
326
327 private:
328 friend class Op;
329 // constructor
331};
332
333// internal macros to make
334#define TVM_OP_REGISTER_VAR_DEF [[maybe_unused]] static ::tvm::OpRegEntry& __make_##Op
335
351#define TVM_REGISTER_OP(OpName) \
352 TVM_FFI_STR_CONCAT(TVM_OP_REGISTER_VAR_DEF, __COUNTER__) = \
353 ::tvm::OpRegEntry::RegisterOrGet(OpName).set_name()
354
355// implementations
356
357template <typename ValueType>
358inline OpAttrMap<ValueType> Op::GetAttrMap(const ffi::String& key) {
359 return OpAttrMap<ValueType>(Op::GetAttrMapContainer(key));
360}
361
362inline OpNode* OpRegEntry::get() { return const_cast<OpNode*>(op_.operator->()); }
363
364inline OpRegEntry& OpRegEntry::describe(const std::string& descr) { // NOLINT(*)
365 get()->description = descr;
366 return *this;
367}
368
369inline OpRegEntry& OpRegEntry::add_argument(const std::string& name, const std::string& type,
370 const std::string& description) {
371 auto n = ffi::make_object<ArgumentInfoNode>();
372 n->name = name;
373 n->type_info = type;
374 n->description = description;
375 get()->arguments.push_back(ArgumentInfo(n));
376 return *this;
377}
378
380 get()->num_inputs = n;
381 return *this;
382}
383
384template <typename AttrsType>
386 get()->attrs_type_key = AttrsType::_type_key;
387 get()->attrs_type_index = AttrsType::RuntimeTypeIndex();
388 return *this;
389}
390
391inline OpRegEntry& OpRegEntry::set_attrs_type_key(const ffi::String& key) { // NOLINT(*)
392 get()->attrs_type_key = key;
393 get()->attrs_type_index = tvm::ffi::TypeKeyToIndex(key.c_str());
394 return *this;
395}
396
398 get()->support_level = n;
399 return *this;
400}
401
402template <typename ValueType>
403inline OpRegEntry& OpRegEntry::set_attr( // NOLINT(*)
404 const std::string& attr_name, const ValueType& value, int plevel) {
405 TVM_FFI_ICHECK_GT(plevel, 0) << "plevel in set_attr must be greater than 0";
406 UpdateAttr(attr_name, Any(value), plevel);
407 return *this;
408}
409
410// member functions of OpAttrMap
411
412template <typename ValueType>
414 TVM_FFI_ICHECK(expr.defined());
415 if (const OpNode* op = expr.as<OpNode>()) {
416 return this->map_.get(ffi::GetRef<Op>(op), def_value);
417 } else {
418 return def_value;
419 }
420}
421
422} // namespace tvm
423#endif // TVM_IR_OP_H_
Attribute map used in registry.
Helpers for attribute objects.
Information about an input field of an Op (name, type, description).
Definition op.h:54
ffi::String description
detailed description of the type
Definition op.h:61
static void RegisterReflection()
Definition op.h:63
ffi::String name
name of the field
Definition op.h:57
ffi::String type_info
type docstring information in str.
Definition op.h:59
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.ArgumentInfo", ArgumentInfoNode, ffi::Object)
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition op.h:71
Managed reference to ArgumentInfoNode.
Definition op.h:77
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(ArgumentInfo, ffi::ObjectRef, ArgumentInfoNode)
Generic attribute map.
Definition attr_registry_map.h:38
ffi::Map<Key, ValueType> used to store meta-data.
Definition attr_registry_map.h:105
ValueType get(const Op &key, ValueType def_value) const
get the corresponding value element at key with default value.
Definition attr_registry_map.h:136
int count(const Op &key) const
Check if the map has op as key.
Definition attr_registry_map.h:117
Definition instruction.h:31
Base type of all the expressions.
Definition base_expr.h:300
Managed reference to ExprNode.
Definition base_expr.h:335
ffi::Map<Op,ValueType> used to store meta-information about Op.
Definition op.h:311
ValueType get(const Expr &expr, ValueType def_value) const
get the corresponding value element at op with default value.
Definition op.h:413
Primitive Op(builtin intrinsics)
Definition op.h:94
ffi::String attrs_type_key
The type key of the attribute field This can be empty, in which case it defaults to anything.
Definition op.h:109
static void RegisterReflection()
Definition op.h:127
ffi::String description
detailed description of the operator This can be used to generate docstring automatically for the ope...
Definition op.h:102
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.Op", OpNode, ExprNode)
uint32_t attrs_type_index
attribute type index, this field varies in each run and is not exposed to frontend.
Definition op.h:114
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition op.h:138
ffi::Array< ArgumentInfo > arguments
Definition op.h:104
int32_t support_level
support level of the operator, The lower the more priority it contains. This is in analogies to BLAS ...
Definition op.h:125
ffi::String name
name of the operator
Definition op.h:97
int32_t num_inputs
number of input arguments to the operator, -1 means it is variable length
Definition op.h:119
Helper structure to register operators.
Definition op.h:207
const Op & op() const
Definition op.h:210
OpRegEntry & describe(const std::string &descr)
setter function during registration Set the description of operator
Definition op.h:364
void reset_attr(const std::string &attr_name)
Resets an attr of the registry.
OpRegEntry & set_attrs_type_key(const ffi::String &key)
Set the attrs type key and index to be AttrsType.
Definition op.h:391
OpRegEntry & set_name()
Definition op.h:276
OpRegEntry & add_argument(const std::string &name, const std::string &type, const std::string &description)
Add argument information to the function.
Definition op.h:369
OpRegEntry & set_attrs_type()
Set the attrs type key and index to be AttrsType.
Definition op.h:385
OpRegEntry & set_support_level(int32_t level)
Set the support level of op.
Definition op.h:397
OpRegEntry & set_attr(const std::string &attr_name, const ValueType &value, int plevel=10)
Register additional attributes to operator.
Definition op.h:403
OpRegEntry & set_num_inputs(int32_t n)
Set the num_inputs.
Definition op.h:379
static OpRegEntry & RegisterOrGet(const ffi::String &name)
Register or get a new entry.
Managed reference class to OpNode.
Definition op.h:163
static const Op & Get(const ffi::String &op_name)
Get an Op for a given operator name. Will raise an error if the op has not been registered.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Op, Expr, OpNode)
static OpAttrMap< ValueType > GetAttrMap(const ffi::String &attr_name)
Get additional registered attribute about operators. If nothing has been registered,...
Definition op.h:358
static bool HasAttrMap(const ffi::String &attr_name)
Checks if an attr map is present in the registry.
Op(ffi::ObjectPtr< OpNode > n)
Definition op.h:165
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
Serializable global function used in IR.
Base expr nodes in TVM.
IR/AST nodes for TVM types shared across IR variants.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40