forked from Tencent/ncnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayer.cpp
152 lines (120 loc) · 3.23 KB
/
layer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#include "layer.h"
#include <stdio.h>
#include <string.h>
#include "cpu.h"
namespace ncnn {
Option::Option()
{
lightmode = true;
num_threads = get_cpu_count();
blob_allocator = 0;
workspace_allocator = 0;
}
static Option g_default_option;
const Option& get_default_option()
{
return g_default_option;
}
int set_default_option(const Option& opt)
{
if (opt.num_threads <= 0)
{
fprintf(stderr, "invalid option num_threads %d\n", opt.num_threads);
return -1;
}
g_default_option = opt;
return 0;
}
Layer::Layer()
{
one_blob_only = false;
support_inplace = false;
}
Layer::~Layer()
{
}
int Layer::load_param(const ParamDict& /*pd*/)
{
return 0;
}
int Layer::load_model(const ModelBin& /*mb*/)
{
return 0;
}
int Layer::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
{
if (!support_inplace)
return -1;
top_blobs = bottom_blobs;
for (int i = 0; i < (int)top_blobs.size(); i++)
{
top_blobs[i] = bottom_blobs[i].clone(opt.blob_allocator);
if (top_blobs[i].empty())
return -100;
}
return forward_inplace(top_blobs, opt);
}
int Layer::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
{
if (!support_inplace)
return -1;
top_blob = bottom_blob.clone(opt.blob_allocator);
if (top_blob.empty())
return -100;
return forward_inplace(top_blob, opt);
}
int Layer::forward_inplace(std::vector<Mat>& /*bottom_top_blobs*/, const Option& /*opt*/) const
{
return -1;
}
int Layer::forward_inplace(Mat& /*bottom_top_blob*/, const Option& /*opt*/) const
{
return -1;
}
#include "layer_declaration.h"
static const layer_registry_entry layer_registry[] =
{
#include "layer_registry.h"
};
static const int layer_registry_entry_count = sizeof(layer_registry) / sizeof(layer_registry_entry);
#if NCNN_STRING
int layer_to_index(const char* type)
{
for (int i=0; i<layer_registry_entry_count; i++)
{
if (strcmp(type, layer_registry[i].name) == 0)
return i;
}
return -1;
}
Layer* create_layer(const char* type)
{
int index = layer_to_index(type);
if (index == -1)
return 0;
return create_layer(index);
}
#endif // NCNN_STRING
Layer* create_layer(int index)
{
if (index < 0 || index >= layer_registry_entry_count)
return 0;
layer_creator_func layer_creator = layer_registry[index].creator;
if (!layer_creator)
return 0;
return layer_creator();
}
} // namespace ncnn