ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Module.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 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#include "coco/IR/Module.h"
18
19#include <memory>
20
21using std::make_unique;
22
23namespace
24{
25
26struct EntityManagerImpl final : public coco::EntityManager
27{
28public:
29 std::unique_ptr<coco::BagManager> _bag;
30
31public:
32 coco::BagManager *bag(void) override { return _bag.get(); }
33 const coco::BagManager *bag(void) const override { return _bag.get(); }
34
35public:
36 std::unique_ptr<coco::ObjectManager> _object;
37
38public:
39 coco::ObjectManager *object(void) override { return _object.get(); }
40 const coco::ObjectManager *object(void) const override { return _object.get(); }
41
42public:
43 std::unique_ptr<coco::OpManager> _op;
44
45public:
46 coco::OpManager *op(void) override { return _op.get(); }
47 const coco::OpManager *op(void) const override { return _op.get(); }
48
49public:
50 coco::InstrManager *instr(void) override { return _instr.get(); }
51 const coco::InstrManager *instr(void) const override { return _instr.get(); }
52
53public:
54 coco::BlockManager *block(void) override { return _block.get(); }
55 const coco::BlockManager *block(void) const override { return _block.get(); }
56
57public:
58 std::unique_ptr<coco::InputManager> _input;
59
60public:
61 coco::InputManager *input(void) override { return _input.get(); }
62 const coco::InputManager *input(void) const override { return _input.get(); }
63
64public:
65 std::unique_ptr<coco::OutputManager> _output;
66
67public:
68 coco::OutputManager *output(void) override { return _output.get(); }
69 const coco::OutputManager *output(void) const override { return _output.get(); }
70
71public:
72 // WARN Do NOT change the order of these fields: _block -> _instr
73 //
74 // Note that each instruction may have a reference to a block, and
75 // the destructor of Instr accesses this 'block' reference.
76 //
77 // Thus, Instr entities SHOULD BE destructed before Block entities are destructed.
78 std::unique_ptr<coco::BlockManager> _block;
79 std::unique_ptr<coco::InstrManager> _instr;
80};
81
82} // namespace
83
84namespace
85{
86
87class ModuleImpl final : public coco::Module
88{
89public:
90 coco::EntityManager *entity(void) override { return _entity.get(); }
91 const coco::EntityManager *entity(void) const override { return _entity.get(); }
92
93public:
94 std::unique_ptr<coco::BlockList> _block;
95
96public:
97 coco::BlockList *block(void) override { return _block.get(); }
98 const coco::BlockList *block(void) const override { return _block.get(); }
99
100public:
101 std::unique_ptr<coco::InputList> _input;
102
103public:
104 coco::InputList *input(void) override { return _input.get(); }
105 const coco::InputList *input(void) const override { return _input.get(); }
106
107public:
108 std::unique_ptr<coco::OutputList> _output;
109
110public:
111 coco::OutputList *output(void) override { return _output.get(); }
112 const coco::OutputList *output(void) const override { return _output.get(); }
113
114public:
115 // WARN _entity SHOULD BE declared after _block in order to allow each Block(s) to detach itself.
116 //
117 // If not, Block is destructed after its corresponding BlockList is destructed, which results
118 // in invalid memory access during the update on BlockList (inside Block's destructor).
119 std::unique_ptr<coco::EntityManager> _entity;
120};
121
122} // namespace
123
124namespace coco
125{
126
127std::unique_ptr<Module> Module::create(void)
128{
129 auto m = make_unique<::ModuleImpl>();
130
131 auto mgr = make_unique<::EntityManagerImpl>();
132 {
133 mgr->_bag = make_unique<coco::BagManager>(m.get());
134 mgr->_object = make_unique<coco::ObjectManager>(m.get());
135 mgr->_op = make_unique<coco::OpManager>(m.get());
136 mgr->_instr = make_unique<coco::InstrManager>(m.get());
137 mgr->_block = make_unique<coco::BlockManager>(m.get());
138 mgr->_input = make_unique<coco::InputManager>(m.get());
139 mgr->_output = make_unique<coco::OutputManager>(m.get());
140 }
141 m->_entity = std::move(mgr);
142
143 m->_block = make_unique<coco::BlockList>(m.get());
144 m->_input = make_unique<coco::InputList>();
145 m->_output = make_unique<coco::OutputList>();
146
147 return m;
148}
149
150} // namespace coco
Top-level element of coco IR which represents a neural network.
Definition Module.h:34
virtual EntityManager * entity(void)=0
virtual BlockList * block(void)=0
virtual OutputList * output(void)=0
static std::unique_ptr< Module > create(void)
Definition Module.cpp:127
virtual InputList * input(void)=0
Meta (lifetime) manager interface.
virtual InputManager * input(void)=0
virtual BlockManager * block(void)=0
virtual OutputManager * output(void)=0
virtual ObjectManager * object(void)=0
virtual OpManager * op(void)=0
virtual BagManager * bag(void)=0
virtual InstrManager * instr(void)=0