ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Frontend.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 "Frontend.h"
18#include "Context.h"
20
23
24#include <map>
25#include <set>
26#include <string>
27
28#include <cassert>
29#include <stdexcept>
30
31using namespace nncc::core::ADT;
32
34
35Frontend::Frontend() : _prototxt{new ::caffe::NetParameter}, _caffemodel{new ::caffe::NetParameter}
36{
37 // DO NOTHING
38}
39
41{
42 auto module = coco::Module::create();
43 auto blk = module->entity()->block()->create();
44 module->block()->append(blk);
45
46 auto data = coco::Data::create();
47
48 // For weight access
49 caffeimport::WeightContext weight_ctx(_caffemodel.get());
50
51 // For inter-layer communication
52 std::map<std::string, tensor::Shape> shape_ctx;
53 std::map<std::string, coco::Bag *> bag_ctx;
54
55 std::set<std::string> bags;
56 std::map<std::string, uint32_t> def_count;
57 std::map<std::string, uint32_t> use_count;
58
59 auto def = [&bags, &def_count, &use_count](const std::string &name) {
60 if (bags.find(name) == bags.end())
61 {
62 bags.insert(name);
63 def_count[name] = 0;
64 use_count[name] = 0;
65 }
66
67 def_count.at(name) += 1;
68 };
69
70 auto use = [&use_count](const std::string &name) { use_count.at(name) += 1; };
71
72 auto outputs = [&bags, &def_count, &use_count](void) {
73 std::set<std::string> res;
74
75 for (const auto &bag : bags)
76 {
77 if (def_count.at(bag) > use_count.at(bag))
78 {
79 res.insert(bag);
80 }
81 }
82
83 return res;
84 };
85
86 caffeimport::GraphBuilderContext opbuilder_context(module.get(), data.get(), blk, shape_ctx,
87 bag_ctx, weight_ctx);
88
89 for (const auto &layer : _prototxt->layer())
90 {
91 assert(layer.has_name());
92 assert(layer.has_type());
93
94 for (uint32_t n = 0; n < layer.top().size(); ++n)
95 {
96 def(layer.top(n));
97 }
98
99 for (uint32_t n = 0; n < layer.bottom().size(); ++n)
100 {
101 use(layer.bottom(n));
102 }
103
104 if (const auto *graph_builder = caffeimport::GraphBuilderRegistry::get().lookup(layer.type()))
105 {
106 graph_builder->build(layer, &opbuilder_context);
107 }
108 else
109 {
110 throw std::runtime_error{"Not supported: " + layer.type()};
111 }
112 }
113
114 // Finalize: Create output for each top blob
115 for (const auto &name : outputs())
116 {
117 const auto &shape = shape_ctx.at(name);
118 auto bag = bag_ctx.at(name);
119
120 auto output = module->entity()->output()->create(shape);
121
122 output->bag(bag);
123 output->name(name);
124 output->reorder<LexicalLayout>();
125
126 module->output()->insert(output);
127 }
128
129 enco::Bundle bundle;
130
131 bundle.module(std::move(module));
132 bundle.data(std::move(data));
133
134 return std::move(bundle);
135}
enco::Bundle load(void) const override
Definition Frontend.cpp:40
static GraphBuilderRegistry & get()
coco::Data * data(void) const
Definition Bundle.h:38
coco::Module * module(void) const
Definition Bundle.h:34
int32_t size[5]
Definition Slice.cpp:35
static std::unique_ptr< Data > create(void)
Definition Data.cpp:202