You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

eltwise.cpp 6.0 kB

[WIP] vulkan compute (#618) * vulkan infrastructure * vkallocator and vkmat * layer interface for vulkan compute * wip... * default vulkan device, command wrapper, upload model weight in load_model to simplify layer interface * simplify command api, vkmat holds staging buffer, relu works * initialize specialization constant, simplify command dispatch, fix staging buffer copy with different shape, convolution works * init extension functions * dynamic local size and group count * group count=1 is invalid * regard device max workgroup size limit * fix relu oooops * decouple command record and staging allocation * create result blob * add pooling shader * buffer is faster than image :) * fix pooling shader * add innerproduct shader * readonly writeonly decoration * simplify buffer creation * decouple command and layer, VK_KHR_descriptor_update_template extension makes descriptor binding update easy :D * fix vulkan building issues in visual studio (#1) * fix building issues on visual studio * ignore benchmark * cancel changes * ... ... * decouple paramdict and vulkandevice * fix staging buffer destroy in model loading * remove vkdev member in option * add padding shader * simplify vulkan layer creation, simplify convolution and pooling shader for no padding, less debug output * add convolutiondepthwise and softmax shader * specialization float type, add leakyrelu * add dropout shader * add batchnorm shader * split vulkan forward * add scale shader * push constant type can be int or float * set_optimal_local_size_xyz * add eltwise shader * concat vulkan forward * fix convolution without bias * add dummy shader for concat and split, more fix ... * optional VK_KHR_descriptor_update_template and VK_KHR_push_descriptor * check VK_KHR_push_descriptor for vkCmdPushDescriptorSetWithTemplateKHR * binaryop and unaryop shader * hide raw command buffer * simple vkbenchncnn benchmark * create device with transfer queue * rename command to vkcompute, add vktransfer and layer upload_model interface * external VkMat, copy and map wrt buffer offset * command copy respect offset and size * decouple weight upload and load, simplify upload weight api, use one big staging buffer for uploading weights * fix build on android * binding count can not vary :( * barrier check state, fix sub-op destruction * declare local_size_xyz constant, fix crash on radv * fix local_size_xyz, second try * more barrier and state fix * fix softmax * reconstruct buffer memory allocator, reuse blob buffer, less verbose output * find unified memory type index * weight staging buffer allocator and weight buffer allocator, respect descriptor buffer offset alignment * use VK_KHR_descriptor_update_template for faster descriptor update if available, multithread pipeline creation * find more useful vulkan extensions and enable them * fix msvc build * respect VK_KHR_dedicated_allocation for weight buffer allocation * fix android build * fix bias name conflicts with metal * decouple pipeline and layer, building shader sources into shader module, dedicated create_pipeline api, simplify pipeline recording * drop dummy shader, inplace softmax, multiple shader module works * fix unique queue family index error * flatten support vulkan * mnasnet run * find shader module by name, each entry point per shader module, fix attribute/id conflict on moltenvk * some minor changes * add some high level api * use dedicated transfer queue to upload weight model * prefer mappable buffer on unified memory * global pooling and convolution fc, reuse staging buffer * implement ring-buffer style blob allocator, add VkBufferMemory capacity * use blob allocator for workspace blob, it works fine :) * vulkan option off * Update layer.cpp * fix build with vulkan off * less verbose output, fix crash on vulkan_compute off * merge benchncnn tool * allocator clear api, use new weight buffer allocator per net * add default locked allocator * mapped mat ptr api, persistent mapped memory works generally :) * travis ci linux vulkan * travis ci vulkan wip ... * more gpu wip ... * more gpu wip ... * wip... * wip... * wip... ... * wip... ios vulkan build... * find glslangValidator on ios build * use dynamic moltenvk library * travis ci wip ... * ios simulator does not support metal at all * fix cpu only extractor * optimize workgroup size, first try * optimize workgroup size, second try * conv1x1s1d1 vec4 * revert build system * fix ncnn2mem build * fix ncnn2mem build
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #include "eltwise.h"
  15. #include <algorithm>
  16. namespace ncnn {
  17. DEFINE_LAYER_CREATOR(Eltwise)
  18. Eltwise::Eltwise()
  19. {
  20. one_blob_only = false;
  21. support_inplace = false;// TODO inplace reduction
  22. }
  23. int Eltwise::load_param(const ParamDict& pd)
  24. {
  25. op_type = pd.get(0, 0);
  26. coeffs = pd.get(1, Mat());
  27. return 0;
  28. }
  29. int Eltwise::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
  30. {
  31. const Mat& bottom_blob = bottom_blobs[0];
  32. int w = bottom_blob.w;
  33. int h = bottom_blob.h;
  34. int channels = bottom_blob.c;
  35. size_t elemsize = bottom_blob.elemsize;
  36. int size = w * h;
  37. Mat& top_blob = top_blobs[0];
  38. top_blob.create(w, h, channels, elemsize, opt.blob_allocator);
  39. if (top_blob.empty())
  40. return -100;
  41. if (op_type == Operation_PROD)
  42. {
  43. // first blob
  44. const Mat& bottom_blob1 = bottom_blobs[1];
  45. #pragma omp parallel for num_threads(opt.num_threads)
  46. for (int q=0; q<channels; q++)
  47. {
  48. const float* ptr = bottom_blob.channel(q);
  49. const float* ptr1 = bottom_blob1.channel(q);
  50. float* outptr = top_blob.channel(q);
  51. for (int i=0; i<size; i++)
  52. {
  53. outptr[i] = ptr[i] * ptr1[i];
  54. }
  55. }
  56. for (size_t b=2; b<bottom_blobs.size(); b++)
  57. {
  58. const Mat& bottom_blob1 = bottom_blobs[b];
  59. #pragma omp parallel for num_threads(opt.num_threads)
  60. for (int q=0; q<channels; q++)
  61. {
  62. const float* ptr = bottom_blob1.channel(q);
  63. float* outptr = top_blob.channel(q);
  64. for (int i=0; i<size; i++)
  65. {
  66. outptr[i] *= ptr[i];
  67. }
  68. }
  69. }
  70. }
  71. else if (op_type == Operation_SUM)
  72. {
  73. if (coeffs.w == 0)
  74. {
  75. // first blob
  76. const Mat& bottom_blob1 = bottom_blobs[1];
  77. #pragma omp parallel for num_threads(opt.num_threads)
  78. for (int q=0; q<channels; q++)
  79. {
  80. const float* ptr = bottom_blob.channel(q);
  81. const float* ptr1 = bottom_blob1.channel(q);
  82. float* outptr = top_blob.channel(q);
  83. for (int i=0; i<size; i++)
  84. {
  85. outptr[i] = ptr[i] + ptr1[i];
  86. }
  87. }
  88. for (size_t b=2; b<bottom_blobs.size(); b++)
  89. {
  90. const Mat& bottom_blob1 = bottom_blobs[b];
  91. #pragma omp parallel for num_threads(opt.num_threads)
  92. for (int q=0; q<channels; q++)
  93. {
  94. const float* ptr = bottom_blob1.channel(q);
  95. float* outptr = top_blob.channel(q);
  96. for (int i=0; i<size; i++)
  97. {
  98. outptr[i] += ptr[i];
  99. }
  100. }
  101. }
  102. }
  103. else
  104. {
  105. // first blob
  106. const Mat& bottom_blob1 = bottom_blobs[1];
  107. float coeff0 = coeffs[0];
  108. float coeff1 = coeffs[1];
  109. #pragma omp parallel for num_threads(opt.num_threads)
  110. for (int q=0; q<channels; q++)
  111. {
  112. const float* ptr = bottom_blob.channel(q);
  113. const float* ptr1 = bottom_blob1.channel(q);
  114. float* outptr = top_blob.channel(q);
  115. for (int i=0; i<size; i++)
  116. {
  117. outptr[i] = ptr[i] * coeff0 + ptr1[i] * coeff1;
  118. }
  119. }
  120. for (size_t b=2; b<bottom_blobs.size(); b++)
  121. {
  122. const Mat& bottom_blob1 = bottom_blobs[b];
  123. float coeff = coeffs[b];
  124. #pragma omp parallel for num_threads(opt.num_threads)
  125. for (int q=0; q<channels; q++)
  126. {
  127. const float* ptr = bottom_blob1.channel(q);
  128. float* outptr = top_blob.channel(q);
  129. for (int i=0; i<size; i++)
  130. {
  131. outptr[i] += ptr[i] * coeff;
  132. }
  133. }
  134. }
  135. }
  136. }
  137. else if (op_type == Operation_MAX)
  138. {
  139. // first blob
  140. const Mat& bottom_blob1 = bottom_blobs[1];
  141. #pragma omp parallel for num_threads(opt.num_threads)
  142. for (int q=0; q<channels; q++)
  143. {
  144. const float* ptr = bottom_blob.channel(q);
  145. const float* ptr1 = bottom_blob1.channel(q);
  146. float* outptr = top_blob.channel(q);
  147. for (int i=0; i<size; i++)
  148. {
  149. outptr[i] = std::max(ptr[i], ptr1[i]);
  150. }
  151. }
  152. for (size_t b=2; b<bottom_blobs.size(); b++)
  153. {
  154. const Mat& bottom_blob1 = bottom_blobs[b];
  155. #pragma omp parallel for num_threads(opt.num_threads)
  156. for (int q=0; q<channels; q++)
  157. {
  158. const float* ptr = bottom_blob1.channel(q);
  159. float* outptr = top_blob.channel(q);
  160. for (int i=0; i<size; i++)
  161. {
  162. outptr[i] = std::max(outptr[i], ptr[i]);
  163. }
  164. }
  165. }
  166. }
  167. return 0;
  168. }
  169. } // namespace ncnn