layer.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #ifndef NCNN_LAYER_H
  15. #define NCNN_LAYER_H
  16. #include "mat.h"
  17. #include "modelbin.h"
  18. #include "option.h"
  19. #include "paramdict.h"
  20. #include "platform.h"
  21. #include <math.h>
  22. #if NCNN_VULKAN
  23. #include "command.h"
  24. #include "pipeline.h"
  25. #include <vulkan/vulkan.h>
  26. #endif // NCNN_VULKAN
  27. namespace ncnn {
  28. class NCNN_EXPORT Layer
  29. {
  30. public:
  31. // empty
  32. Layer();
  33. // virtual destructor
  34. virtual ~Layer();
  35. // load layer specific parameter from parsed dict
  36. // return 0 if success
  37. virtual int load_param(const ParamDict& pd);
  38. // load layer specific weight data from model binary
  39. // return 0 if success
  40. virtual int load_model(const ModelBin& mb);
  41. // layer implementation specific setup
  42. // return 0 if success
  43. virtual int create_pipeline(const Option& opt);
  44. // layer implementation specific clean
  45. // return 0 if success
  46. virtual int destroy_pipeline(const Option& opt);
  47. public:
  48. // one input and one output blob
  49. bool one_blob_only;
  50. // support inplace inference
  51. bool support_inplace;
  52. // support vulkan compute
  53. bool support_vulkan;
  54. // accept input blob with packed storage
  55. bool support_packing;
  56. // accept bf16
  57. bool support_bf16_storage;
  58. // accept fp16
  59. bool support_fp16_storage;
  60. // accept int8
  61. bool support_int8_storage;
  62. // shader image storage
  63. bool support_image_storage;
  64. // shader tensor storage
  65. bool support_tensor_storage;
  66. bool support_reserved_00;
  67. bool support_reserved_0;
  68. bool support_reserved_1;
  69. bool support_reserved_2;
  70. bool support_reserved_3;
  71. bool support_reserved_4;
  72. bool support_reserved_5;
  73. bool support_reserved_6;
  74. bool support_reserved_7;
  75. bool support_reserved_8;
  76. bool support_reserved_9;
  77. // feature disabled set
  78. int featmask;
  79. public:
  80. // implement inference
  81. // return 0 if success
  82. virtual int forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const;
  83. virtual int forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const;
  84. // implement inplace inference
  85. // return 0 if success
  86. virtual int forward_inplace(std::vector<Mat>& bottom_top_blobs, const Option& opt) const;
  87. virtual int forward_inplace(Mat& bottom_top_blob, const Option& opt) const;
  88. #if NCNN_VULKAN
  89. public:
  90. // upload weight blob from host to device
  91. virtual int upload_model(VkTransfer& cmd, const Option& opt);
  92. public:
  93. // implement inference
  94. // return 0 if success
  95. virtual int forward(const std::vector<VkMat>& bottom_blobs, std::vector<VkMat>& top_blobs, VkCompute& cmd, const Option& opt) const;
  96. virtual int forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const;
  97. // implement inference
  98. // return 0 if success
  99. virtual int forward(const std::vector<VkImageMat>& bottom_blobs, std::vector<VkImageMat>& top_blobs, VkCompute& cmd, const Option& opt) const;
  100. virtual int forward(const VkImageMat& bottom_blob, VkImageMat& top_blob, VkCompute& cmd, const Option& opt) const;
  101. // implement inplace inference
  102. // return 0 if success
  103. virtual int forward_inplace(std::vector<VkMat>& bottom_top_blobs, VkCompute& cmd, const Option& opt) const;
  104. virtual int forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const;
  105. // implement inplace inference
  106. // return 0 if success
  107. virtual int forward_inplace(std::vector<VkImageMat>& bottom_top_blobs, VkCompute& cmd, const Option& opt) const;
  108. virtual int forward_inplace(VkImageMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const;
  109. public:
  110. // assigned immediately after creating this layer
  111. const VulkanDevice* vkdev;
  112. #endif // NCNN_VULKAN
  113. public:
  114. // custom user data
  115. void* userdata;
  116. // layer type index
  117. int typeindex;
  118. #if NCNN_STRING
  119. // layer type name
  120. std::string type;
  121. // layer name
  122. std::string name;
  123. #endif // NCNN_STRING
  124. // blob index which this layer needs as input
  125. std::vector<int> bottoms;
  126. // blob index which this layer produces as output
  127. std::vector<int> tops;
  128. // shape hint
  129. std::vector<Mat> bottom_shapes;
  130. std::vector<Mat> top_shapes;
  131. };
  132. // layer factory function
  133. typedef Layer* (*layer_creator_func)(void*);
  134. typedef void (*layer_destroyer_func)(Layer*, void*);
  135. struct layer_registry_entry
  136. {
  137. #if NCNN_STRING
  138. // layer type name
  139. const char* name;
  140. #endif // NCNN_STRING
  141. // layer factory entry
  142. layer_creator_func creator;
  143. };
  144. struct custom_layer_registry_entry
  145. {
  146. #if NCNN_STRING
  147. // layer type name
  148. const char* name;
  149. #endif // NCNN_STRING
  150. // layer factory entry
  151. layer_creator_func creator;
  152. layer_destroyer_func destroyer;
  153. void* userdata;
  154. };
  155. #if NCNN_STRING
  156. // get layer type from type name
  157. NCNN_EXPORT int layer_to_index(const char* type);
  158. // create layer from type name
  159. NCNN_EXPORT Layer* create_layer(const char* type);
  160. #endif // NCNN_STRING
  161. // create layer from layer type
  162. NCNN_EXPORT Layer* create_layer(int index);
  163. #define DEFINE_LAYER_CREATOR(name) \
  164. ::ncnn::Layer* name##_layer_creator(void* /*userdata*/) \
  165. { \
  166. return new name; \
  167. }
  168. #define DEFINE_LAYER_DESTROYER(name) \
  169. void name##_layer_destroyer(::ncnn::Layer* layer, void* /*userdata*/) \
  170. { \
  171. delete layer; \
  172. }
  173. } // namespace ncnn
  174. #endif // NCNN_LAYER_H