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