Elffuss BraidHub

kikocis/ncnn public

d7a5f98c0f60 · 5.9 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#include "eltwise.h"
16
17namespace ncnn {
18
19Eltwise::Eltwise()
20{
21 one_blob_only = false;
22 support_inplace = false; // TODO inplace reduction
23}
24
25int Eltwise::load_param(const ParamDict& pd)
26{
27 op_type = pd.get(0, 0);
28 coeffs = pd.get(1, Mat());
29
30 return 0;
31}
32
33int Eltwise::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
34{
35 const Mat& bottom_blob = bottom_blobs[0];
36 int w = bottom_blob.w;
37 int h = bottom_blob.h;
38 int d = bottom_blob.d;
39 int channels = bottom_blob.c;
40 int size = w * h * d;
41
42 Mat& top_blob = top_blobs[0];
43 top_blob.create_like(bottom_blob, opt.blob_allocator);
44 if (top_blob.empty())
45 return -100;
46
47 if (op_type == Operation_PROD)
48 {
49 // first blob
50 const Mat& bottom_blob1 = bottom_blobs[1];
51 #pragma omp parallel for num_threads(opt.num_threads)
52 for (int q = 0; q < channels; q++)
53 {
54 const float* ptr = bottom_blob.channel(q);
55 const float* ptr1 = bottom_blob1.channel(q);
56 float* outptr = top_blob.channel(q);
57
58 for (int i = 0; i < size; i++)
59 {
60 outptr[i] = ptr[i] * ptr1[i];
61 }
62 }
63
64 for (size_t b = 2; b < bottom_blobs.size(); b++)
65 {
66 const Mat& bottom_blob1 = bottom_blobs[b];
67 #pragma omp parallel for num_threads(opt.num_threads)
68 for (int q = 0; q < channels; q++)
69 {
70 const float* ptr = bottom_blob1.channel(q);
71 float* outptr = top_blob.channel(q);
72
73 for (int i = 0; i < size; i++)
74 {
75 outptr[i] *= ptr[i];
76 }
77 }
78 }
79 }
80 else if (op_type == Operation_SUM)
81 {
82 if (coeffs.w == 0)
83 {
84 // first blob
85 const Mat& bottom_blob1 = bottom_blobs[1];
86 #pragma omp parallel for num_threads(opt.num_threads)
87 for (int q = 0; q < channels; q++)
88 {
89 const float* ptr = bottom_blob.channel(q);
90 const float* ptr1 = bottom_blob1.channel(q);
91 float* outptr = top_blob.channel(q);
92
93 for (int i = 0; i < size; i++)
94 {
95 outptr[i] = ptr[i] + ptr1[i];
96 }
97 }
98
99 for (size_t b = 2; b < bottom_blobs.size(); b++)
100 {
101 const Mat& bottom_blob1 = bottom_blobs[b];
102 #pragma omp parallel for num_threads(opt.num_threads)
103 for (int q = 0; q < channels; q++)
104 {
105 const float* ptr = bottom_blob1.channel(q);
106 float* outptr = top_blob.channel(q);
107
108 for (int i = 0; i < size; i++)
109 {
110 outptr[i] += ptr[i];
111 }
112 }
113 }
114 }
115 else
116 {
117 // first blob
118 const Mat& bottom_blob1 = bottom_blobs[1];
119 float coeff0 = coeffs[0];
120 float coeff1 = coeffs[1];
121 #pragma omp parallel for num_threads(opt.num_threads)
122 for (int q = 0; q < channels; q++)
123 {
124 const float* ptr = bottom_blob.channel(q);
125 const float* ptr1 = bottom_blob1.channel(q);
126 float* outptr = top_blob.channel(q);
127
128 for (int i = 0; i < size; i++)
129 {
130 outptr[i] = ptr[i] * coeff0 + ptr1[i] * coeff1;
131 }
132 }
133
134 for (size_t b = 2; b < bottom_blobs.size(); b++)
135 {
136 const Mat& bottom_blob1 = bottom_blobs[b];
137 float coeff = coeffs[b];
138 #pragma omp parallel for num_threads(opt.num_threads)
139 for (int q = 0; q < channels; q++)
140 {
141 const float* ptr = bottom_blob1.channel(q);
142 float* outptr = top_blob.channel(q);
143
144 for (int i = 0; i < size; i++)
145 {
146 outptr[i] += ptr[i] * coeff;
147 }
148 }
149 }
150 }
151 }
152 else if (op_type == Operation_MAX)
153 {
154 // first blob
155 const Mat& bottom_blob1 = bottom_blobs[1];
156 #pragma omp parallel for num_threads(opt.num_threads)
157 for (int q = 0; q < channels; q++)
158 {
159 const float* ptr = bottom_blob.channel(q);
160 const float* ptr1 = bottom_blob1.channel(q);
161 float* outptr = top_blob.channel(q);
162
163 for (int i = 0; i < size; i++)
164 {
165 outptr[i] = std::max(ptr[i], ptr1[i]);
166 }
167 }
168
169 for (size_t b = 2; b < bottom_blobs.size(); b++)
170 {
171 const Mat& bottom_blob1 = bottom_blobs[b];
172 #pragma omp parallel for num_threads(opt.num_threads)
173 for (int q = 0; q < channels; q++)
174 {
175 const float* ptr = bottom_blob1.channel(q);
176 float* outptr = top_blob.channel(q);
177
178 for (int i = 0; i < size; i++)
179 {
180 outptr[i] = std::max(outptr[i], ptr[i]);
181 }
182 }
183 }
184 }
185
186 return 0;
187}
188
189} // namespace ncnn