ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Model.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#ifndef __ONERT_IR_MODEL_H__
18#define __ONERT_IR_MODEL_H__
19
20#include <memory>
21#include <unordered_map>
22
23#include "ir/IGraph.h"
24#include "ir/Index.h"
25#include "util/ObjectManager.h"
26
27namespace onert
28{
29namespace backend
30{
31namespace custom
32{
33class IKernelBuilder;
34} // namespace custom
35} // namespace backend
36} // namespace onert
37
38namespace onert
39{
40namespace ir
41{
42
43class Model
44{
45public:
46 Model() = default;
47 Model(const Model &obj) = default;
48 Model(Model &&) = default;
49 Model &operator=(const Model &) = default;
50 Model &operator=(Model &&) = default;
51 ~Model() = default;
52
60 void push(SubgraphIndex index, const std::shared_ptr<IGraph> &subg) { _subgraphs[index] = subg; }
61
68 void remove(const SubgraphIndex &index) { _subgraphs.erase(index); }
69
76 const std::shared_ptr<IGraph> &at(const SubgraphIndex &index) const
77 {
78 return _subgraphs.at(index);
79 }
86 std::shared_ptr<IGraph> &at(const SubgraphIndex &index) { return _subgraphs.at(index); }
87
94 bool exist(const SubgraphIndex &index) const
95 {
96 auto it = _subgraphs.find(index);
97 return it != _subgraphs.end();
98 }
99
106 void iterate(const std::function<void(const SubgraphIndex &, const IGraph &)> &fn) const
107 {
108 for (const auto &[subg_idx, subg] : _subgraphs)
109 {
110 fn(subg_idx, *subg);
111 }
112 }
113
120 void iterate(const std::function<void(const SubgraphIndex &, IGraph &)> &fn)
121 {
122 for (const auto &[subg_idx, subg] : _subgraphs)
123 {
124 fn(subg_idx, *subg);
125 }
126 }
127
133 size_t subgraphs_count() const { return _subgraphs.size(); }
134
140 std::shared_ptr<IGraph> primary_subgraph() const { return _subgraphs.at(SubgraphIndex{0}); }
141
149 template <typename Graph, std::enable_if_t<std::is_base_of<IGraph, Graph>::value, bool> = true>
150 bool hasOnly()
151 {
152 for (const auto &e : _subgraphs)
153 {
154 if (std::dynamic_pointer_cast<Graph>(e.second) == nullptr)
155 return false;
156 }
157 return true;
158 }
159
160private:
161 std::unordered_map<SubgraphIndex, std::shared_ptr<IGraph>> _subgraphs;
162
163 // Custom operations support
164public:
165 void
166 bindKernelBuilder(const std::shared_ptr<onert::backend::custom::IKernelBuilder> &kernel_builder)
167 {
168 _kernel_builder = kernel_builder;
169 }
170
171 const std::shared_ptr<backend::custom::IKernelBuilder> &getKernelBuilder() const
172 {
173 return _kernel_builder;
174 }
175
176private:
177 std::shared_ptr<backend::custom::IKernelBuilder> _kernel_builder;
178
179public:
180 void add_metadata(const std::string &name, std::unique_ptr<const ir::Data> data)
181 {
182 _metadatas.emplace(name, std::move(data));
183 }
184
185 bool exists_metadata(const std::string &name) const
186 {
187 return _metadatas.find(name) != _metadatas.end();
188 }
189
190 // NOTE The corresponding metadata is deleted from the model and returned
191 std::unique_ptr<const ir::Data> extract_metadata(const std::string name)
192 {
193 auto m = _metadatas.find(name);
194
195 if (m == _metadatas.end())
196 throw std::out_of_range{"no meatdata named " + name};
197
198 auto data = std::move(m->second);
199 _metadatas.erase(m);
200 return data;
201 }
202
203private:
204 std::unordered_map<std::string, std::unique_ptr<const ir::Data>> _metadatas;
205};
206} // namespace ir
207} // namespace onert
208
209#endif // __ONERT_IR_MODEL_H__
Model(const Model &obj)=default
std::unique_ptr< const ir::Data > extract_metadata(const std::string name)
Definition Model.h:191
void iterate(const std::function< void(const SubgraphIndex &, IGraph &)> &fn)
Iterate over the container with given function.
Definition Model.h:120
std::shared_ptr< IGraph > & at(const SubgraphIndex &index)
Get the subgraph that is associated with the given index.
Definition Model.h:86
std::shared_ptr< IGraph > primary_subgraph() const
Return the primary subgraph.
Definition Model.h:140
bool exist(const SubgraphIndex &index) const
Get the subgraph that is associated with the given index.
Definition Model.h:94
Model & operator=(const Model &)=default
Model & operator=(Model &&)=default
void iterate(const std::function< void(const SubgraphIndex &, const IGraph &)> &fn) const
Iterate over the container with given function.
Definition Model.h:106
void remove(const SubgraphIndex &index)
Remove the subgraph that is associated with the given index.
Definition Model.h:68
size_t subgraphs_count() const
Get count of Subgraphs.
Definition Model.h:133
bool hasOnly()
Return whether the model has only typename Graph.
Definition Model.h:150
~Model()=default
Model(Model &&)=default
const std::shared_ptr< backend::custom::IKernelBuilder > & getKernelBuilder() const
Definition Model.h:171
bool exists_metadata(const std::string &name) const
Definition Model.h:185
void add_metadata(const std::string &name, std::unique_ptr< const ir::Data > data)
Definition Model.h:180
void bindKernelBuilder(const std::shared_ptr< onert::backend::custom::IKernelBuilder > &kernel_builder)
Definition Model.h:166
void push(SubgraphIndex index, const std::shared_ptr< IGraph > &subg)
Put subgraph in the container with a new Index for that.
Definition Model.h:60
const std::shared_ptr< IGraph > & at(const SubgraphIndex &index) const
Get the subgraph that is associated with the given index.
Definition Model.h:76