| 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 "binaryop.h" |
| 16 | |
| 17 | namespace ncnn { |
| 18 | |
| 19 | BinaryOp::BinaryOp() |
| 20 | { |
| 21 | one_blob_only = false; |
| 22 | support_inplace = false; |
| 23 | } |
| 24 | |
| 25 | int BinaryOp::load_param(const ParamDict& pd) |
| 26 | { |
| 27 | op_type = pd.get(0, 0); |
| 28 | with_scalar = pd.get(1, 0); |
| 29 | b = pd.get(2, 0.f); |
| 30 | |
| 31 | if (with_scalar != 0) |
| 32 | { |
| 33 | one_blob_only = true; |
| 34 | support_inplace = true; |
| 35 | } |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | // broadcasting rule |
| 41 | // https://github.com/Tencent/ncnn/wiki/binaryop-broadcasting |
| 42 | |
| 43 | template<typename Op> |
| 44 | static void binary_op_broadcast(const Mat& a, const Mat& b, Mat& c, const Option& opt) |
| 45 | { |
| 46 | // general broadcast |
| 47 | const Op op; |
| 48 | |
| 49 | const int dims = c.dims; |
| 50 | const int w = c.w; |
| 51 | const int h = c.h; |
| 52 | const int d = c.d; |
| 53 | const int channels = c.c; |
| 54 | |
| 55 | if (dims == 1) |
| 56 | { |
| 57 | const float* ptr = a; |
| 58 | const float* ptr1 = b; |
| 59 | float* outptr = c; |
| 60 | |
| 61 | const int ainc = a.w > 1 ? 1 : 0; |
| 62 | const int binc = b.w > 1 ? 1 : 0; |
| 63 | |
| 64 | for (int x = 0; x < w; x++) |
| 65 | { |
| 66 | outptr[x] = op(*ptr, *ptr1); |
| 67 | ptr += ainc; |
| 68 | ptr1 += binc; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if (dims == 2) |
| 73 | { |
| 74 | #pragma omp parallel for num_threads(opt.num_threads) |
| 75 | for (int y = 0; y < h; y++) |
| 76 | { |
| 77 | const float* ptr = a.row(std::min(y, a.h - 1)); |
| 78 | const float* ptr1 = b.row(std::min(y, b.h - 1)); |
| 79 | float* outptr = c.row(y); |
| 80 | |
| 81 | const int ainc = a.w > 1 ? 1 : 0; |
| 82 | const int binc = b.w > 1 ? 1 : 0; |
| 83 | |
| 84 | for (int x = 0; x < w; x++) |
| 85 | { |
| 86 | outptr[x] = op(*ptr, *ptr1); |
| 87 | ptr += ainc; |
| 88 | ptr1 += binc; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if (dims == 3 || dims == 4) |
| 94 | { |
| 95 | #pragma omp parallel for num_threads(opt.num_threads) |
| 96 | for (int q = 0; q < channels; q++) |
| 97 | { |
| 98 | float* outptr = c.channel(q); |
| 99 | |
| 100 | const int ainc = a.w > 1 ? 1 : 0; |
| 101 | const int binc = b.w > 1 ? 1 : 0; |
| 102 | |
| 103 | for (int z = 0; z < d; z++) |
| 104 | { |
| 105 | for (int y = 0; y < h; y++) |
| 106 | { |
| 107 | const float* ptr = a.channel(std::min(q, a.c - 1)).depth(std::min(z, a.d - 1)).row(std::min(y, a.h - 1)); |
| 108 | const float* ptr1 = b.channel(std::min(q, b.c - 1)).depth(std::min(z, b.d - 1)).row(std::min(y, b.h - 1)); |
| 109 | |
| 110 | for (int x = 0; x < w; x++) |
| 111 | { |
| 112 | outptr[x] = op(*ptr, *ptr1); |
| 113 | ptr += ainc; |
| 114 | ptr1 += binc; |
| 115 | } |
| 116 | |
| 117 | outptr += w; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | template<typename Op> |
| 125 | static void binary_op_scalar_inplace(Mat& a, float b, const Option& opt) |
| 126 | { |
| 127 | const Op op; |
| 128 | |
| 129 | const int channels = a.c; |
| 130 | const int size = a.w * a.h * a.d; |
| 131 | |
| 132 | #pragma omp parallel for num_threads(opt.num_threads) |
| 133 | for (int q = 0; q < channels; q++) |
| 134 | { |
| 135 | float* ptr = a.channel(q); |
| 136 | |
| 137 | for (int i = 0; i < size; i++) |
| 138 | { |
| 139 | ptr[i] = op(ptr[i], b); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | struct binary_op_add |
| 145 | { |
| 146 | float operator()(const float& x, const float& y) const |
| 147 | { |
| 148 | return x + y; |
| 149 | } |
| 150 | }; |
| 151 | |
| 152 | struct binary_op_sub |
| 153 | { |
| 154 | float operator()(const float& x, const float& y) const |
| 155 | { |
| 156 | return x - y; |
| 157 | } |
| 158 | }; |
| 159 | |
| 160 | struct binary_op_mul |
| 161 | { |
| 162 | float operator()(const float& x, const float& y) const |
| 163 | { |
| 164 | return x * y; |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | struct binary_op_div |
| 169 | { |
| 170 | float operator()(const float& x, const float& y) const |
| 171 | { |
| 172 | return x / y; |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | struct binary_op_max |
| 177 | { |
| 178 | float operator()(const float& x, const float& y) const |
| 179 | { |
| 180 | return std::max(x, y); |
| 181 | } |
| 182 | }; |
| 183 | |
| 184 | struct binary_op_min |
| 185 | { |
| 186 | float operator()(const float& x, const float& y) const |
| 187 | { |
| 188 | return std::min(x, y); |
| 189 | } |
| 190 | }; |
| 191 | |
| 192 | struct binary_op_pow |
| 193 | { |
| 194 | float operator()(const float& x, const float& y) const |
| 195 | { |
| 196 | return (float)powf(x, y); |
| 197 | } |
| 198 | }; |
| 199 | |
| 200 | struct binary_op_rsub |
| 201 | { |
| 202 | float operator()(const float& x, const float& y) const |
| 203 | { |
| 204 | return y - x; |
| 205 | } |
| 206 | }; |
| 207 | |
| 208 | struct binary_op_rdiv |
| 209 | { |
| 210 | float operator()(const float& x, const float& y) const |
| 211 | { |
| 212 | return y / x; |
| 213 | } |
| 214 | }; |
| 215 | |
| 216 | struct binary_op_rpow |
| 217 | { |
| 218 | float operator()(const float& x, const float& y) const |
| 219 | { |
| 220 | return (float)powf(y, x); |
| 221 | } |
| 222 | }; |
| 223 | |
| 224 | struct binary_op_atan2 |
| 225 | { |
| 226 | float operator()(const float& x, const float& y) const |
| 227 | { |
| 228 | return (float)atan2f(x, y); |
| 229 | } |
| 230 | }; |
| 231 | |
| 232 | struct binary_op_ratan2 |
| 233 | { |
| 234 | float operator()(const float& x, const float& y) const |
| 235 | { |
| 236 | return (float)atan2f(y, x); |
| 237 | } |
| 238 | }; |
| 239 | |
| 240 | static void binary_op_broadcast(const Mat& a, const Mat& b, Mat& c, int op_type, const Option& opt) |
| 241 | { |
| 242 | if (op_type == BinaryOp::Operation_ADD) return binary_op_broadcast<binary_op_add>(a, b, c, opt); |
| 243 | if (op_type == BinaryOp::Operation_SUB) return binary_op_broadcast<binary_op_sub>(a, b, c, opt); |
| 244 | if (op_type == BinaryOp::Operation_MUL) return binary_op_broadcast<binary_op_mul>(a, b, c, opt); |
| 245 | if (op_type == BinaryOp::Operation_DIV) return binary_op_broadcast<binary_op_div>(a, b, c, opt); |
| 246 | if (op_type == BinaryOp::Operation_MAX) return binary_op_broadcast<binary_op_max>(a, b, c, opt); |
| 247 | if (op_type == BinaryOp::Operation_MIN) return binary_op_broadcast<binary_op_min>(a, b, c, opt); |
| 248 | if (op_type == BinaryOp::Operation_POW) return binary_op_broadcast<binary_op_pow>(a, b, c, opt); |
| 249 | if (op_type == BinaryOp::Operation_RSUB) return binary_op_broadcast<binary_op_sub>(b, a, c, opt); |
| 250 | if (op_type == BinaryOp::Operation_RDIV) return binary_op_broadcast<binary_op_div>(b, a, c, opt); |
| 251 | if (op_type == BinaryOp::Operation_RPOW) return binary_op_broadcast<binary_op_pow>(b, a, c, opt); |
| 252 | if (op_type == BinaryOp::Operation_ATAN2) return binary_op_broadcast<binary_op_atan2>(a, b, c, opt); |
| 253 | if (op_type == BinaryOp::Operation_RATAN2) return binary_op_broadcast<binary_op_atan2>(b, a, c, opt); |
| 254 | |
| 255 | // should never reach here |
| 256 | } |
| 257 | |
| 258 | static void binary_op_scalar_inplace(Mat& bottom_top_blob, float b, int op_type, const Option& opt) |
| 259 | { |
| 260 | if (op_type == BinaryOp::Operation_ADD) return binary_op_scalar_inplace<binary_op_add>(bottom_top_blob, b, opt); |
| 261 | if (op_type == BinaryOp::Operation_SUB) return binary_op_scalar_inplace<binary_op_sub>(bottom_top_blob, b, opt); |
| 262 | if (op_type == BinaryOp::Operation_MUL) return binary_op_scalar_inplace<binary_op_mul>(bottom_top_blob, b, opt); |
| 263 | if (op_type == BinaryOp::Operation_DIV) return binary_op_scalar_inplace<binary_op_div>(bottom_top_blob, b, opt); |
| 264 | if (op_type == BinaryOp::Operation_MAX) return binary_op_scalar_inplace<binary_op_max>(bottom_top_blob, b, opt); |
| 265 | if (op_type == BinaryOp::Operation_MIN) return binary_op_scalar_inplace<binary_op_min>(bottom_top_blob, b, opt); |
| 266 | if (op_type == BinaryOp::Operation_POW) return binary_op_scalar_inplace<binary_op_pow>(bottom_top_blob, b, opt); |
| 267 | if (op_type == BinaryOp::Operation_RSUB) return binary_op_scalar_inplace<binary_op_rsub>(bottom_top_blob, b, opt); |
| 268 | if (op_type == BinaryOp::Operation_RDIV) return binary_op_scalar_inplace<binary_op_rdiv>(bottom_top_blob, b, opt); |
| 269 | if (op_type == BinaryOp::Operation_RPOW) return binary_op_scalar_inplace<binary_op_rpow>(bottom_top_blob, b, opt); |
| 270 | if (op_type == BinaryOp::Operation_ATAN2) return binary_op_scalar_inplace<binary_op_atan2>(bottom_top_blob, b, opt); |
| 271 | if (op_type == BinaryOp::Operation_RATAN2) return binary_op_scalar_inplace<binary_op_ratan2>(bottom_top_blob, b, opt); |
| 272 | |
| 273 | // should never reach here |
| 274 | } |
| 275 | |
| 276 | int BinaryOp::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 277 | { |
| 278 | const Mat& A = bottom_blobs[0]; |
| 279 | const Mat& B = bottom_blobs[1]; |
| 280 | const int outdims = std::max(A.dims, B.dims); |
| 281 | |
| 282 | Mat A2 = A; |
| 283 | Mat B2 = B; |
| 284 | if (A.dims < outdims) |
| 285 | { |
| 286 | // expand inner axes |
| 287 | if (outdims == 2) |
| 288 | { |
| 289 | if (A.w == B.h) |
| 290 | A2 = A.reshape(1, A.w); |
| 291 | else // if (A.w == B.w) |
| 292 | A2 = A.reshape(A.w, 1); |
| 293 | } |
| 294 | if (outdims == 3 && A.dims == 1) |
| 295 | { |
| 296 | if (A.w == B.c) |
| 297 | A2 = A.reshape(1, 1, A.w); |
| 298 | else // if (A.w == B.w) |
| 299 | A2 = A.reshape(A.w, 1, 1); |
| 300 | } |
| 301 | if (outdims == 3 && A.dims == 2) |
| 302 | A2 = A.reshape(1, A.w, A.h); |
| 303 | if (outdims == 4 && A.dims == 1) |
| 304 | { |
| 305 | if (A.w == B.c) |
| 306 | A2 = A.reshape(1, 1, 1, A.w); |
| 307 | else // if (A.w == B.w) |
| 308 | A2 = A.reshape(A.w, 1, 1, 1); |
| 309 | } |
| 310 | if (outdims == 4 && A.dims == 2) |
| 311 | A2 = A.reshape(1, 1, A.w, A.h); |
| 312 | if (outdims == 4 && A.dims == 3) |
| 313 | A2 = A.reshape(1, A.w, A.h, A.c); |
| 314 | } |
| 315 | if (B.dims < outdims) |
| 316 | { |
| 317 | // expand inner axes |
| 318 | if (outdims == 2) |
| 319 | { |
| 320 | if (B.w == A.h) |
| 321 | B2 = B.reshape(1, B.w); |
| 322 | else // if (B.w == A.w) |
| 323 | B2 = B.reshape(B.w, 1); |
| 324 | } |
| 325 | if (outdims == 3 && B.dims == 1) |
| 326 | { |
| 327 | if (B.w == A.c) |
| 328 | B2 = B.reshape(1, 1, B.w); |
| 329 | else // if (B.w == A.w) |
| 330 | B2 = B.reshape(B.w, 1, 1); |
| 331 | } |
| 332 | if (outdims == 3 && B.dims == 2) |
| 333 | B2 = B.reshape(1, B.w, B.h); |
| 334 | if (outdims == 4 && B.dims == 1) |
| 335 | { |
| 336 | if (B.w == A.c) |
| 337 | B2 = B.reshape(1, 1, 1, B.w); |
| 338 | else // if (B.w == A.w) |
| 339 | B2 = B.reshape(B.w, 1, 1, 1); |
| 340 | } |
| 341 | if (outdims == 4 && B.dims == 2) |
| 342 | B2 = B.reshape(1, 1, B.w, B.h); |
| 343 | if (outdims == 4 && B.dims == 3) |
| 344 | B2 = B.reshape(1, B.w, B.h, B.c); |
| 345 | } |
| 346 | |
| 347 | const int outw = std::max(A2.w, B2.w); |
| 348 | const int outh = std::max(A2.h, B2.h); |
| 349 | const int outd = std::max(A2.d, B2.d); |
| 350 | const int outc = std::max(A2.c, B2.c); |
| 351 | |
| 352 | Mat& top_blob = top_blobs[0]; |
| 353 | if (outdims == 1) |
| 354 | { |
| 355 | top_blob.create(outw, 4u, opt.blob_allocator); |
| 356 | } |
| 357 | if (outdims == 2) |
| 358 | { |
| 359 | top_blob.create(outw, outh, 4u, opt.blob_allocator); |
| 360 | } |
| 361 | if (outdims == 3) |
| 362 | { |
| 363 | top_blob.create(outw, outh, outc, 4u, opt.blob_allocator); |
| 364 | } |
| 365 | if (outdims == 4) |
| 366 | { |
| 367 | top_blob.create(outw, outh, outd, outc, 4u, opt.blob_allocator); |
| 368 | } |
| 369 | if (top_blob.empty()) |
| 370 | return -100; |
| 371 | |
| 372 | binary_op_broadcast(A2, B2, top_blob, op_type, opt); |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | int BinaryOp::forward_inplace(Mat& bottom_top_blob, const Option& opt) const |
| 378 | { |
| 379 | binary_op_scalar_inplace(bottom_top_blob, b, op_type, opt); |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | } // namespace ncnn |