2 * Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef __ONERT_IR_NNPKG_H__
18 #define __ONERT_IR_NNPKG_H__
21 #include <unordered_set>
32 using IODesc = std::tuple<ModelIndex, SubgraphIndex, IOIndex>;
42 bool operator()(const onert::ir::ModelEdge &lhs, const onert::ir::ModelEdge &rhs) const
44 return lhs.from == rhs.from && lhs.to == rhs.to;
50 size_t operator()(const ::onert::ir::ModelEdge &edge) const noexcept
52 unsigned long long h1 = (std::get<0>(edge.from).value() << 24) |
53 (std::get<1>(edge.from).value() << 16) | std::get<2>(edge.from).value();
54 unsigned long long h2 = (std::get<0>(edge.to).value() << 24) |
55 (std::get<1>(edge.to).value() << 16) | std::get<2>(edge.to).value();
60 inline std::ostream &operator<<(std::ostream &o, const IODesc &od)
62 o << std::get<0>(od).value() << ":" << std::get<1>(od).value() << ":" << std::get<2>(od).value();
66 using ModelEdgeSet = std::unordered_set<ir::ModelEdge, ir::ModelEdgeHash, ir::ModelEdgeEqual>;
69 * @brief Struct to gather model I/O information in multimodel NN package
70 * Model I/O will have role one of below
71 * - Package input/output
72 * - Edge's start/finish point between model
76 std::vector<ir::IODesc> pkg_inputs;
77 std::vector<ir::IODesc> pkg_outputs;
85 NNPkg(const NNPkg &obj) = default;
86 NNPkg(NNPkg &&) = default;
87 NNPkg &operator=(const NNPkg &) = default;
88 NNPkg &operator=(NNPkg &&) = default;
91 NNPkg(std::shared_ptr<Model> model) { _models[ModelIndex{0}] = model; }
92 std::shared_ptr<Model> primary_model() { return _models.at(onert::ir::ModelIndex{0}); }
95 * @brief Put model at index
97 * @param[in] model Model to be pushed
98 * @param[in] index Index where Model is to be pushed
100 void push(ModelIndex index, const std::shared_ptr<Model> &model) { _models[index] = model; }
103 * @brief Get the count of model
105 * @return the count of models
107 size_t model_count() const { return _models.size(); }
110 * @brief Get model at index
112 * @param[in] index Index of the model to be returned
113 * @return Model at index
115 const std::shared_ptr<Model> &model(const ModelIndex &index) const { return _models.at(index); }
117 * @brief Get model at index
119 * @param[in] index Index of the model to be returned
120 * @return Model at index
122 std::shared_ptr<Model> &model(const ModelIndex &index) { return _models.at(index); }
125 * @brief Get pkg_input at index
127 * @param[in] index Index of pkg_input to be returned
128 * @return IODesc at index
130 const IODesc &input(uint32_t index) const { return _edges.pkg_inputs[index]; }
132 * @brief Get pkg_input at index
134 * @param[in] index Index of pkg_input to be returned
135 * @return IODesc at index
137 IODesc &input(uint32_t index) { return _edges.pkg_inputs[index]; }
139 * @brief Add input at the end
141 * @param[in] input Input IODesc to be pushed
143 void addInput(const IODesc &input) { _edges.pkg_inputs.push_back(input); }
146 * @brief Get pkg_output at index
148 * @param[in] index Index of pkg_output to be returned
149 * @return IODesc at index
151 const IODesc &output(uint32_t index) const { return _edges.pkg_outputs[index]; }
153 * @brief Get pkg_output at index
155 * @param[in] index Index of pkg_output to be returned
156 * @return IODesc at index
158 IODesc &output(uint32_t index) { return _edges.pkg_outputs[index]; }
160 * @brief Add output at the end
162 * @param[in] output Output IODesc to be pushed
164 void addOutput(const IODesc &output) { _edges.pkg_outputs.push_back(output); }
167 * @brief Add edge between models at the end
169 * @param[in] from from IODesc
170 * @param[in] to to IODesc
172 void addEdge(const IODesc &from, const IODesc &to)
174 std::cout << from << " -> " << to << std::endl;
175 _edges.edges.insert(ModelEdge{from, to});
178 * @brief Get model edge set
179 * @return Edge set reference
181 const ModelEdges &model_edges() { return _edges; }
183 // TODO: Add iterate() or getter for edges
186 std::unordered_map<ModelIndex, std::shared_ptr<Model>> _models;
193 #endif // __ONERT_IR_NNPKG_H__