SNPEBuilder.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //==============================================================================
  2. //
  3. // Copyright (c) 2017-2021 Qualcomm Technologies, Inc.
  4. // All Rights Reserved.
  5. // Confidential and Proprietary - Qualcomm Technologies, Inc.
  6. //
  7. //==============================================================================
  8. #ifndef _SNPE_BUILDER_HPP_
  9. #define _SNPE_BUILDER_HPP_
  10. #include "SNPE/SNPE.hpp"
  11. #include "DlSystem/DlEnums.hpp"
  12. #include "DlSystem/UDLFunc.hpp"
  13. #include "DlSystem/DlOptional.hpp"
  14. #include "DlSystem/TensorShapeMap.hpp"
  15. #include "DlSystem/PlatformConfig.hpp"
  16. #include "DlSystem/IOBufferDataTypeMap.hpp"
  17. #include "DlSystem/RuntimeList.hpp"
  18. namespace zdl {
  19. namespace DlContainer
  20. {
  21. class IDlContainer;
  22. }
  23. }
  24. struct SNPEBuilderImpl;
  25. namespace zdl { namespace SNPE {
  26. /** @addtogroup c_plus_plus_apis C++
  27. @{ */
  28. /**
  29. * The builder class for creating SNPE objects.
  30. * Not meant to be extended.
  31. */
  32. class ZDL_EXPORT SNPEBuilder final
  33. {
  34. private:
  35. std::unique_ptr<::SNPEBuilderImpl> m_Impl;
  36. public:
  37. /**
  38. * @brief Constructor of NeuralNetwork Builder with a supplied model.
  39. *
  40. * @param[in] container A container holding the model.
  41. *
  42. * @return A new instance of a SNPEBuilder object
  43. * that can be used to configure and build
  44. * an instance of SNPE.
  45. *
  46. */
  47. explicit SNPEBuilder(
  48. zdl::DlContainer::IDlContainer* container);
  49. ~SNPEBuilder();
  50. /**
  51. * NOTE: DEPRECATED, MAY BE REMOVED IN THE FUTURE. Please use
  52. * setRuntimeProcessorOrder()
  53. *
  54. * @brief Sets the runtime processor.
  55. *
  56. * @param[in] targetRuntimeProcessor The target runtime.
  57. *
  58. * @return The current instance of SNPEBuilder.
  59. */
  60. SNPEBuilder& setRuntimeProcessor(
  61. zdl::DlSystem::Runtime_t targetRuntimeProcessor);
  62. /**
  63. * @brief Requests a performance profile.
  64. *
  65. * @param[in] targetRuntimeProfile The target performance profile.
  66. *
  67. * @return The current instance of SNPEBuilder.
  68. */
  69. SNPEBuilder& setPerformanceProfile(
  70. zdl::DlSystem::PerformanceProfile_t performanceProfile);
  71. /**
  72. * @brief Sets the profiling level. Default profiling level for
  73. * SNPEBuilder is off. Off and basic only applies to DSP runtime.
  74. *
  75. * @param[in] profilingLevel The target profiling level.
  76. *
  77. * @return The current instance of SNPEBuilder.
  78. */
  79. SNPEBuilder& setProfilingLevel(
  80. zdl::DlSystem::ProfilingLevel_t profilingLevel);
  81. /**
  82. * @brief Sets a preference for execution priority.
  83. *
  84. * This allows the caller to give coarse hint to SNPE runtime
  85. * about the priority of the network. SNPE runtime is free to use
  86. * this information to co-ordinate between different workloads
  87. * that may or may not extend beyond SNPE.
  88. *
  89. * @param[in] ExecutionPriorityHint_t The target performance profile.
  90. *
  91. * @return The current instance of SNPEBuilder.
  92. */
  93. SNPEBuilder& setExecutionPriorityHint(
  94. zdl::DlSystem::ExecutionPriorityHint_t priority);
  95. /**
  96. * @brief Sets the layers that will generate output.
  97. *
  98. * @param[in] outputLayerNames List of layer names to
  99. * output. An empty list will
  100. * result in only the final
  101. * layer of the model being
  102. * the output layer. The list
  103. * will be copied.
  104. *
  105. * @return The current instance of SNPEBuilder.
  106. */
  107. SNPEBuilder& setOutputLayers(
  108. const zdl::DlSystem::StringList& outputLayerNames);
  109. /**
  110. * @brief Sets the output tensor names.
  111. *
  112. * @param[in] outputTensorNames List of tensor names to
  113. * output. An empty list will
  114. * result in producing output for the final
  115. * output tensor of the model.
  116. * The list will be copied.
  117. *
  118. * @return The current instance of SNPEBuilder.
  119. */
  120. SNPEBuilder& setOutputTensors(
  121. const zdl::DlSystem::StringList& outputTensorNames);
  122. /**
  123. * @brief Passes in a User-defined layer.
  124. *
  125. * @param udlBundle Bundle of udl factory function and a cookie
  126. *
  127. * @return The current instance of SNPEBuilder.
  128. */
  129. SNPEBuilder& setUdlBundle(
  130. zdl::DlSystem::UDLBundle udlBundle);
  131. /**
  132. * @brief Sets whether this neural network will perform inference with
  133. * input from user-supplied buffers, and write output to user-supplied
  134. * buffers. Default behaviour is to use tensors created by
  135. * ITensorFactory.
  136. *
  137. * @param[in] bufferMode Whether to use user-supplied buffer or not.
  138. *
  139. * @return The current instance of SNPEBuilder.
  140. */
  141. SNPEBuilder& setUseUserSuppliedBuffers(
  142. bool bufferMode);
  143. /**
  144. * @brief Sets the debug mode of the runtime.
  145. *
  146. * @param[in] debugMode This enables debug mode for the runtime. It
  147. * does two things. For an empty
  148. * outputLayerNames list, all layers will be
  149. * output. It might also disable some internal
  150. * runtime optimizations (e.g., some networks
  151. * might be optimized by combining layers,
  152. * etc.).
  153. *
  154. * @return The current instance of SNPEBuilder.
  155. */
  156. SNPEBuilder& setDebugMode(
  157. bool debugMode);
  158. /**
  159. * NOTE: DEPRECATED, MAY BE REMOVED IN THE FUTURE. Please use
  160. * setRuntimeProcessorOrder()
  161. *
  162. * @brief Sets the mode of CPU fallback functionality.
  163. *
  164. * @param[in] mode This flag enables/disables the functionality
  165. * of CPU fallback. When the CPU fallback
  166. * functionality is enabled, layers in model that
  167. * violates runtime constraints will run on CPU
  168. * while the rest of non-violating layers will
  169. * run on the chosen runtime processor. In
  170. * disabled mode, models with layers violating
  171. * runtime constraints will NOT run on the chosen
  172. * runtime processor and will result in runtime
  173. * exception. By default, the functionality is
  174. * enabled.
  175. *
  176. * @return The current instance of SNPEBuilder.
  177. */
  178. SNPEBuilder& setCPUFallbackMode(
  179. bool mode);
  180. /**
  181. * @brief Sets network's input dimensions to enable resizing of
  182. * the spatial dimensions of each layer for fully convolutional networks,
  183. * and the batch dimension for all networks.
  184. *
  185. * @param[in] tensorShapeMap The map of input names and their new dimensions.
  186. * The new dimensions overwrite the input dimensions
  187. * embedded in the model and then resize each layer
  188. * of the model. If the model contains
  189. * layers whose dimensions cannot be resized e.g FullyConnected,
  190. * exception will be thrown when SNPE instance is actually built.
  191. * In general the batch dimension is always resizable.
  192. * After resizing of layers' dimensions in model based
  193. * on new input dimensions, the new model is revalidated
  194. * against all runtime constraints, whose failures may
  195. * result in cpu fallback situation.
  196. *
  197. * @return The current instance of SNPEBuilder.
  198. */
  199. SNPEBuilder& setInputDimensions(const zdl::DlSystem::TensorShapeMap& inputDimensionsMap);
  200. /**
  201. * @brief Sets the mode of init caching functionality.
  202. *
  203. * @param[in] mode This flag enables/disables the functionality of init caching.
  204. * When init caching functionality is enabled, a set of init caches
  205. * will be created during network building/initialization process
  206. * and will be added to DLC container. If such DLC container is saved
  207. * by the user, in subsequent network building/initialization processes
  208. * these init caches will be loaded from the DLC so as to reduce initialization time.
  209. * In disable mode, no init caches will be added to DLC container.
  210. *
  211. * @return The current instance of SNPEBuilder.
  212. */
  213. SNPEBuilder& setInitCacheMode(
  214. bool cacheMode);
  215. /**
  216. * @brief Returns an instance of SNPE based on the current parameters.
  217. *
  218. * @return A new instance of a SNPE object that can be used
  219. * to execute models or null if any errors occur.
  220. */
  221. std::unique_ptr<SNPE> build() noexcept;
  222. /**
  223. * @brief Sets the platform configuration.
  224. *
  225. * @param[in] platformConfig The platform configuration.
  226. *
  227. * @return The current instance of SNPEBuilder.
  228. */
  229. SNPEBuilder& setPlatformConfig(const zdl::DlSystem::PlatformConfig& platformConfig);
  230. /**
  231. * @brief Sets network's runtime order of precedence. Example:
  232. * CPU_FLOAT32, GPU_FLOAT16, AIP_FIXED8_TF
  233. * Note:- setRuntimeProcessor() or setCPUFallbackMode() will be silently ignored when
  234. * setRuntimeProcessorOrder() is invoked
  235. *
  236. * @param[in] runtimeList The list of runtime in order of precedence
  237. *
  238. * @return The current instance of SNPEBuilder.
  239. */
  240. SNPEBuilder& setRuntimeProcessorOrder(const zdl::DlSystem::RuntimeList& runtimeList);
  241. /**
  242. * @brief Sets the unconsumed tensors as output
  243. *
  244. * @param[in] setOutput This enables unconsumed tensors (i.e)
  245. * outputs which are not inputs to any
  246. * layer (basically dead ends) to be marked
  247. * for output
  248. *
  249. * @return The current instance of SNPEBuilder.
  250. */
  251. SNPEBuilder& setUnconsumedTensorsAsOutputs(
  252. bool setOutput);
  253. /**
  254. * @brief Execution terminated when exceeding time limit.
  255. * Only valid for dsp runtime currently.
  256. *
  257. * @param[in] timeout Time limit value
  258. *
  259. * @return The current instance of SNPEBuilder.
  260. */
  261. SNPEBuilder& setTimeOut(
  262. uint64_t timeout);
  263. /**
  264. * @brief Sets the datatype of the buffer.
  265. * Only valid for dsp runtime currently.
  266. *
  267. * @param[in] Map of the buffer names and the datatype that needs to be set.
  268. *
  269. * @return The current instance of SNPEBuilder.
  270. */
  271. SNPEBuilder& setBufferDataType(const zdl::DlSystem::IOBufferDataTypeMap& dataTypeMap);
  272. };
  273. /** @} */ /* end_addtogroup c_plus_plus_apis C++ */
  274. }}
  275. #endif