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/*!
21 * \file tvm/relay/base.h
22 * \brief Base classes for the Relay IR.
23 */
24#ifndef TVM_RELAY_BASE_H_
25#define TVM_RELAY_BASE_H_
26
27#include <tvm/ir/source_map.h>
28#include <tvm/node/node.h>
29#include <tvm/tir/expr.h>
30
31#include <string>
32#include <vector>
33
34namespace tvm {
35/*!
36 * \brief Relay: a high level functional IR for TVM.
37 *
38 * This namespace contains the abstract syntax tree, and other
39 * essential data structures for the Relay IR.
40 *
41 * You can find more about Relay by reading the language reference.
42 */
43namespace relay {
44
45#define RELAY_DEBUG(...) \
46 { \
47 auto fdebug = runtime::Registry::Get("relay.debug"); \
48 ICHECK(fdebug) << "Could not find Relay Python debugger function."; \
49 (*fdebug)("RELAY_DEBUG", __FILE__, __LINE__, __VA_ARGS__); \
50 }
51
52#define RELAY_DEBUG_INTERP(...) \
53 { \
54 auto fdebug = runtime::Registry::Get("relay.debug_interp"); \
55 ICHECK(fdebug) << "Could not find Relay Python debugger function."; \
56 (*fdebug)("RELAY_DEBUG", __FILE__, __LINE__, __VA_ARGS__); \
57 }
58
59/*!
60 * \brief Symbolic expression for tensor shape.
61 */
62using IndexExpr = ::tvm::PrimExpr;
63
64using SourceName = tvm::SourceName;
65using Span = tvm::Span;
66using SpanNode = tvm::SpanNode;
67
68/*!
69 * \brief This is the base node container of all relay structures.
70 */
71class RelayNode : public Object {
72 public:
73 /*! \brief The location of the program in a SourceFragment can be null,
74 * check with span.defined() */
75 mutable Span span;
76
77 static constexpr const char* _type_key = "relay.Node";
78 TVM_DECLARE_BASE_OBJECT_INFO(RelayNode, Object);
79};
80
81/*!
82 * \brief The unique identifier of variables.
83 *
84 * Id is like name to the variables,
85 * except that id is unique for each Var.
86 *
87 * \note Do not create Id directly, they are created in Var.
88 */
89class IdNode : public Object {
90 public:
91 /*!
92 * \brief The name of the variable,
93 * this only acts as a hint to the user,
94 * and is not used for equality.
95 */
96 String name_hint;
97
98 void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("name_hint", &name_hint); }
99
100 bool SEqualReduce(const IdNode* other, SEqualReducer equal) const {
101 return equal.FreeVarEqualImpl(this, other);
102 }
103
104 void SHashReduce(SHashReducer hash_reduce) const { hash_reduce.FreeVarHashImpl(this); }
105
106 static constexpr const char* _type_key = "relay.Id";
107 static constexpr const bool _type_has_method_sequal_reduce = true;
108 static constexpr const bool _type_has_method_shash_reduce = true;
109 TVM_DECLARE_FINAL_OBJECT_INFO(IdNode, Object);
110};
111
112class Id : public ObjectRef {
113 public:
114 /*!
115 * \brief The constructor
116 * \param name_hint The name of the variable.
117 */
118 TVM_DLL explicit Id(String name_hint);
119
120 TVM_DEFINE_OBJECT_REF_METHODS(Id, ObjectRef, IdNode);
121};
122
123/*!
124 * \brief Pretty print a node for debug purposes.
125 *
126 * \param node The node to be printed.
127 * \return The text reperesentation.
128 * \note This function does not show version or meta-data.
129 * Use AsText if you want to store the text.
130 * \sa AsText.
131 */
132TVM_DLL String PrettyPrint(const ObjectRef& node);
133
134/*!
135 * \brief Render the node as a string in the text format.
136 *
137 * \param node The node to be rendered.
138 * \param show_meta_data Whether to print meta data section.
139 * \param annotate An optional callback function for attaching
140 * additional comment block to an expr.
141 *
142 * \note We support a limited set of IR nodes that are part of
143 * relay IR and
144 *
145 * \sa PrettyPrint.
146 * \return The text representation.
147 */
148TVM_DLL String AsText(const ObjectRef& node, bool show_meta_data = true,
149 runtime::TypedPackedFunc<String(ObjectRef)> annotate = nullptr);
150
151} // namespace relay
152} // namespace tvm
153
154#endif // TVM_RELAY_BASE_H_
155