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 meta_ref.h |
22 | * \brief A reference into the metadata section of the Relay text format. |
23 | */ |
24 | |
25 | #ifndef TVM_RELAY_PARSER_META_REF_H_ |
26 | #define TVM_RELAY_PARSER_META_REF_H_ |
27 | |
28 | #include <tvm/ir/attrs.h> |
29 | #include <tvm/relay/expr.h> |
30 | #include <tvm/relay/function.h> |
31 | #include <tvm/relay/parser.h> |
32 | |
33 | #include <string> |
34 | |
35 | namespace tvm { |
36 | namespace relay { |
37 | |
38 | /*! |
39 | * \brief Options for allocating storage. |
40 | */ |
41 | struct MetaRefAttrs : public tvm::AttrsNode<MetaRefAttrs> { |
42 | tvm::String node_type_key; |
43 | uint64_t node_index; |
44 | |
45 | TVM_DECLARE_ATTRS(MetaRefAttrs, "relay.attrs.MetaRefAttrs" ) { |
46 | TVM_ATTR_FIELD(node_type_key) |
47 | .describe("The type_key representing the type of the node referenced." ); |
48 | TVM_ATTR_FIELD(node_index).describe("The index into the type specific node array." ); |
49 | } |
50 | }; |
51 | |
52 | /*! \brief A reference to a "meta-expression". |
53 | * |
54 | * In the text format we allow referencing metadata which |
55 | * uses a compact serialization that proceeds the main |
56 | * program body. |
57 | * |
58 | * We can reference this table using an expression of |
59 | * the form `meta[Type][index]`. |
60 | * |
61 | * We must later resolve these references to actual in-memory |
62 | * AST nodes but this requires first parsing the full program |
63 | * then expanding these temporary AST nodes into their corresponding |
64 | * nodes. |
65 | * |
66 | * For example the nth large constant will be pretty-printed as meta[relay.Constant][n] |
67 | * with its compact binary serialization residing in the metadata section at the end |
68 | * of the program. |
69 | * |
70 | * \param type_key The type key of the object in the meta section. |
71 | * \param node_index The index into that subfield. |
72 | * \returns The meta table reference. |
73 | */ |
74 | Expr MetaRef(std::string type_key, uint64_t node_index); |
75 | |
76 | relay::Function ExpandMetaRefs(const MetaTable& meta_table, const relay::Function& func); |
77 | IRModule ExpandMetaRefs(const MetaTable& meta_table, const IRModule& mod); |
78 | |
79 | } // namespace relay |
80 | } // namespace tvm |
81 | |
82 | #endif // TVM_RELAY_PARSER_META_REF_H_ |
83 | |