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.

context.h 18 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_INCLUDE_API_CONTEXT_H
  17. #define MINDSPORE_INCLUDE_API_CONTEXT_H
  18. #include <string>
  19. #include <memory>
  20. #include <vector>
  21. #include <map>
  22. #include "include/api/types.h"
  23. #include "include/api/dual_abi_helper.h"
  24. namespace mindspore {
  25. enum DeviceType {
  26. kCPU = 0,
  27. kGPU,
  28. kKirinNPU,
  29. kAscend910,
  30. kAscend310,
  31. // add new type here
  32. kInvalidDeviceType = 100,
  33. };
  34. class Allocator;
  35. class Delegate;
  36. class DeviceInfoContext;
  37. /// \brief Context is used to store environment variables during execution.
  38. class MS_API Context {
  39. public:
  40. struct Data;
  41. Context();
  42. ~Context() = default;
  43. /// \brief Set the number of threads at runtime. Only valid for Lite.
  44. ///
  45. /// \param[in] thread_num the number of threads at runtime.
  46. void SetThreadNum(int32_t thread_num);
  47. /// \brief Get the current thread number setting. Only valid for Lite.
  48. ///
  49. /// \return The current thread number setting.
  50. int32_t GetThreadNum() const;
  51. /// \brief Set the thread affinity to CPU cores. Only valid for Lite.
  52. ///
  53. /// \param[in] mode: 0: no affinities, 1: big cores first, 2: little cores first
  54. void SetThreadAffinity(int mode);
  55. /// \brief Get the thread affinity of CPU cores. Only valid for Lite.
  56. ///
  57. /// \return Thread affinity to CPU cores. 0: no affinities, 1: big cores first, 2: little cores first
  58. int GetThreadAffinityMode() const;
  59. /// \brief Set the thread lists to CPU cores. Only valid for Lite.
  60. ///
  61. /// \note If core_list and mode are set by SetThreadAffinity at the same time, the core_list is effective, but the
  62. /// mode is not effective.
  63. ///
  64. /// \param[in] core_list: a vector of thread core lists.
  65. void SetThreadAffinity(const std::vector<int> &core_list);
  66. /// \brief Get the thread lists of CPU cores. Only valid for Lite.
  67. ///
  68. /// \return core_list: a vector of thread core lists.
  69. std::vector<int32_t> GetThreadAffinityCoreList() const;
  70. /// \brief Set the status whether to perform model inference or training in parallel. Only valid for Lite.
  71. ///
  72. /// \param[in] is_parallel: true, parallel; false, not in parallel.
  73. void SetEnableParallel(bool is_parallel);
  74. /// \brief Get the status whether to perform model inference or training in parallel. Only valid for Lite.
  75. ///
  76. /// \return Bool value that indicates whether in parallel.
  77. bool GetEnableParallel() const;
  78. /// \brief Set Delegate to access third-party AI framework. Only valid for Lite.
  79. ///
  80. /// \param[in] Pointer to the custom delegate.
  81. void SetDelegate(const std::shared_ptr<Delegate> &delegate);
  82. /// \brief Get the delegate of the third-party AI framework. Only valid for Lite.
  83. ///
  84. /// \return Pointer to the custom delegate.
  85. std::shared_ptr<Delegate> GetDelegate() const;
  86. /// \brief Get a mutable reference of DeviceInfoContext vector in this context. Only MindSpore Lite supports
  87. /// heterogeneous scenarios with multiple members in the vector.
  88. ///
  89. /// \return Mutable reference of DeviceInfoContext vector in this context.
  90. std::vector<std::shared_ptr<DeviceInfoContext>> &MutableDeviceInfo();
  91. private:
  92. std::shared_ptr<Data> data_;
  93. };
  94. /// \brief DeviceInfoContext defines different device contexts.
  95. class MS_API DeviceInfoContext : public std::enable_shared_from_this<DeviceInfoContext> {
  96. public:
  97. struct Data;
  98. DeviceInfoContext();
  99. virtual ~DeviceInfoContext() = default;
  100. /// \brief Get the type of this DeviceInfoContext.
  101. ///
  102. /// \return Type of this DeviceInfoContext.
  103. virtual enum DeviceType GetDeviceType() const = 0;
  104. /// \brief A similar function to RTTI is provided when the -fno-rtti compilation option is turned on, which converts
  105. /// DeviceInfoContext to a shared pointer of type T, and returns nullptr if the conversion fails.
  106. ///
  107. /// \param T Type
  108. /// \return A pointer of type T after conversion. If the conversion fails, it will be nullptr.
  109. template <class T>
  110. std::shared_ptr<T> Cast() {
  111. static_assert(std::is_base_of<DeviceInfoContext, T>::value, "Wrong cast type.");
  112. if (GetDeviceType() != T().GetDeviceType()) {
  113. return nullptr;
  114. }
  115. return std::static_pointer_cast<T>(shared_from_this());
  116. }
  117. /// \brief obtain provider's name
  118. ///
  119. /// \return provider's name.
  120. inline std::string GetProvider() const;
  121. /// \brief set provider's name.
  122. ///
  123. /// \param[in] provider define the provider's name.
  124. inline void SetProvider(const std::string &provider);
  125. /// \brief obtain provider's device type.
  126. ///
  127. /// \return provider's device type.
  128. inline std::string GetProviderDevice() const;
  129. /// \brief set provider's device type.
  130. ///
  131. /// \param[in] device define the provider's device type.EG: CPU.
  132. inline void SetProviderDevice(const std::string &device);
  133. /// \brief set memory allocator.
  134. ///
  135. /// \param[in] allocator define the memory allocator which can be defined by user.
  136. void SetAllocator(const std::shared_ptr<Allocator> &allocator);
  137. /// \brief obtain memory allocator.
  138. ///
  139. /// \return memory allocator.
  140. std::shared_ptr<Allocator> GetAllocator() const;
  141. protected:
  142. std::vector<char> GetProviderChar() const;
  143. void SetProvider(const std::vector<char> &provider);
  144. std::vector<char> GetProviderDeviceChar() const;
  145. void SetProviderDevice(const std::vector<char> &device);
  146. std::shared_ptr<Data> data_;
  147. };
  148. std::string DeviceInfoContext::GetProvider() const { return CharToString(GetProviderChar()); }
  149. void DeviceInfoContext::SetProvider(const std::string &provider) { SetProvider(StringToChar(provider)); }
  150. std::string DeviceInfoContext::GetProviderDevice() const { return CharToString(GetProviderDeviceChar()); }
  151. void DeviceInfoContext::SetProviderDevice(const std::string &device) { SetProviderDevice(StringToChar(device)); }
  152. /// \brief Derived from DeviceInfoContext, The configuration of the model running on the CPU. This option is only valid
  153. /// for MindSpore Lite.
  154. class MS_API CPUDeviceInfo : public DeviceInfoContext {
  155. public:
  156. /// \brief Get the type of this DeviceInfoContext.
  157. ///
  158. /// \return Type of this DeviceInfoContext.
  159. enum DeviceType GetDeviceType() const override { return DeviceType::kCPU; };
  160. /// \brief Set enables to perform the float16 inference
  161. ///
  162. /// \param[in] is_fp16 Enable float16 inference or not.
  163. void SetEnableFP16(bool is_fp16);
  164. /// \brief Get enables to perform the float16 inference
  165. ///
  166. /// \return Whether enable float16 inference.
  167. bool GetEnableFP16() const;
  168. };
  169. /// \brief Derived from DeviceInfoContext, The configuration of the model running on the NPU. This option is only valid
  170. /// for MindSpore Lite.
  171. class MS_API KirinNPUDeviceInfo : public DeviceInfoContext {
  172. public:
  173. /// \brief Get the type of this DeviceInfoContext.
  174. ///
  175. /// \return Type of this DeviceInfoContext.
  176. enum DeviceType GetDeviceType() const override { return DeviceType::kKirinNPU; };
  177. /// \brief Set the NPU frequency.
  178. ///
  179. /// \param[in] frequency Can be set to 1 (low power consumption), 2 (balanced), 3 (high performance), 4 (extreme
  180. /// performance), default as 3.
  181. void SetFrequency(int frequency);
  182. /// \brief Get the NPU frequency.
  183. ///
  184. /// \return NPU frequency
  185. int GetFrequency() const;
  186. };
  187. /// \brief Derived from DeviceInfoContext, The configuration of the model running on the GPU.
  188. class MS_API GPUDeviceInfo : public DeviceInfoContext {
  189. public:
  190. /// \brief Get the type of this DeviceInfoContext.
  191. ///
  192. /// \return Type of this DeviceInfoContext.
  193. enum DeviceType GetDeviceType() const override { return DeviceType::kGPU; };
  194. /// \brief Set device id.
  195. ///
  196. /// \param[in] device_id The device id.
  197. void SetDeviceID(uint32_t device_id);
  198. /// \brief Get the device id.
  199. ///
  200. /// \return The device id.
  201. uint32_t GetDeviceID() const;
  202. /// \brief Get the distribution rank id.
  203. ///
  204. /// \return The device id.
  205. int GetRankID() const;
  206. /// \brief Get the distribution group size.
  207. ///
  208. /// \return The device id.
  209. int GetGroupSize() const;
  210. /// \brief Set the precision mode.
  211. ///
  212. /// \param[in] precision_mode Optional "origin", "fp16". "origin" is set as default.
  213. inline void SetPrecisionMode(const std::string &precision_mode);
  214. /// \brief Get the precision mode.
  215. ///
  216. /// \return The precision mode.
  217. inline std::string GetPrecisionMode() const;
  218. /// \brief Set enables to perform the float16 inference
  219. ///
  220. /// \param[in] is_fp16 Enable float16 inference or not.
  221. void SetEnableFP16(bool is_fp16);
  222. /// \brief Get enables to perform the float16 inference
  223. ///
  224. /// \return Whether enable float16 inference.
  225. bool GetEnableFP16() const;
  226. /// \brief Set enables to sharing mem with OpenGL
  227. ///
  228. /// \param[in] is_enable_sharing_mem_with_gl Enable sharing OpenCL Memory with OpenGL or not.
  229. void SetEnableGLTexture(bool is_enable_gl_texture);
  230. /// \brief Get enables to sharing mem with OpenGL
  231. ///
  232. /// \return Whether enable sharing mem with OpenGL.
  233. bool GetEnableGLTexture() const;
  234. private:
  235. void SetPrecisionMode(const std::vector<char> &precision_mode);
  236. std::vector<char> GetPrecisionModeChar() const;
  237. };
  238. void GPUDeviceInfo::SetPrecisionMode(const std::string &precision_mode) {
  239. SetPrecisionMode(StringToChar(precision_mode));
  240. }
  241. std::string GPUDeviceInfo::GetPrecisionMode() const { return CharToString(GetPrecisionModeChar()); }
  242. /// \brief Derived from DeviceInfoContext, The configuration of the model running on the Ascend910. This option is
  243. /// invalid for MindSpore Lite.
  244. class MS_API Ascend910DeviceInfo : public DeviceInfoContext {
  245. public:
  246. /// \brief Get the type of this DeviceInfoContext.
  247. ///
  248. /// \return Type of this DeviceInfoContext.
  249. enum DeviceType GetDeviceType() const override { return DeviceType::kAscend910; };
  250. /// \brief Set device id.
  251. ///
  252. /// \param[in] device_id The device id.
  253. void SetDeviceID(uint32_t device_id);
  254. /// \brief Get the device id.
  255. ///
  256. /// \return The device id.
  257. uint32_t GetDeviceID() const;
  258. };
  259. /// \brief Derived from DeviceInfoContext, The configuration of the model running on the Ascend310. This option is
  260. /// invalid for MindSpore Lite.
  261. class MS_API Ascend310DeviceInfo : public DeviceInfoContext {
  262. public:
  263. /// \brief Get the type of this DeviceInfoContext.
  264. ///
  265. /// \return Type of this DeviceInfoContext.
  266. enum DeviceType GetDeviceType() const override { return DeviceType::kAscend310; };
  267. /// \brief Set device id.
  268. ///
  269. /// \param[in] device_id The device id.
  270. void SetDeviceID(uint32_t device_id);
  271. /// \brief Get the device id.
  272. ///
  273. /// \return The device id.
  274. uint32_t GetDeviceID() const;
  275. /// \brief Set AIPP configuration file path.
  276. ///
  277. /// \param[in] cfg_path AIPP configuration file path.
  278. inline void SetInsertOpConfigPath(const std::string &cfg_path);
  279. /// \brief Get AIPP configuration file path.
  280. ///
  281. /// \return AIPP configuration file path.
  282. inline std::string GetInsertOpConfigPath() const;
  283. /// \brief Set format of model inputs.
  284. ///
  285. /// \param[in] format Optional "NCHW", "NHWC", etc.
  286. inline void SetInputFormat(const std::string &format);
  287. /// \brief Get format of model inputs.
  288. ///
  289. /// \return The format of model inputs.
  290. inline std::string GetInputFormat() const;
  291. /// \brief Set shape of model inputs.
  292. ///
  293. /// \param[in] shape e.g. "input_op_name1: 1,2,3,4;input_op_name2: 4,3,2,1".
  294. inline void SetInputShape(const std::string &shape);
  295. /// \brief Get shape of model inputs.
  296. ///
  297. /// \return The shape of model inputs.
  298. inline std::string GetInputShape() const;
  299. /// \brief Set shape of model inputs.
  300. ///
  301. /// \param[in] shape e.g. {{1, {1,2,3,4}}, {2, {4,3,2,1}}} means the first input shape 1,2,3,4 and the second input
  302. /// shape 4,3,2,1.
  303. void SetInputShapeMap(const std::map<int, std::vector<int>> &shape);
  304. /// \brief Get shape of model inputs.
  305. ///
  306. /// \return The shape of model inputs.
  307. std::map<int, std::vector<int>> GetInputShapeMap() const;
  308. void SetDynamicBatchSize(const std::vector<size_t> &dynamic_batch_size);
  309. inline std::string GetDynamicBatchSize() const;
  310. /// \brief Set the dynamic image size of model inputs.
  311. ///
  312. /// \param[in] image size hw e.g. "66,88;32,64" means h1:66,w1:88; h2:32,w2:64.
  313. inline void SetDynamicImageSize(const std::string &dynamic_image_size);
  314. /// \brief Get dynamic image size of model inputs.
  315. ///
  316. /// \return The image size of model inputs.
  317. inline std::string GetDynamicImageSize() const;
  318. /// \brief Set type of model outputs.
  319. ///
  320. /// \param[in] output_type FP32, UINT8 or FP16, default as FP32.
  321. void SetOutputType(enum DataType output_type);
  322. /// \brief Get type of model outputs.
  323. ///
  324. /// \return The set type of model outputs.
  325. enum DataType GetOutputType() const;
  326. /// \brief Set precision mode of model.
  327. ///
  328. /// \param[in] precision_mode Optional "force_fp16", "allow_fp32_to_fp16", "must_keep_origin_dtype" and
  329. /// "allow_mix_precision", "force_fp16" is set as default
  330. inline void SetPrecisionMode(const std::string &precision_mode);
  331. /// \brief Get precision mode of model.
  332. ///
  333. /// \return The set type of model outputs
  334. inline std::string GetPrecisionMode() const;
  335. /// \brief Set op select implementation mode.
  336. ///
  337. /// \param[in] op_select_impl_mode Optional "high_performance" and "high_precision", "high_performance" is set as
  338. /// default.
  339. inline void SetOpSelectImplMode(const std::string &op_select_impl_mode);
  340. /// \brief Get op select implementation mode.
  341. ///
  342. /// \return The set op select implementation mode.
  343. inline std::string GetOpSelectImplMode() const;
  344. inline void SetFusionSwitchConfigPath(const std::string &cfg_path);
  345. inline std::string GetFusionSwitchConfigPath() const;
  346. // Optional "l1_optimize", "l2_optimize", "off_optimize" or "l1_and_l2_optimize", default as "l2_optimize"
  347. inline void SetBufferOptimizeMode(const std::string &buffer_optimize_mode);
  348. inline std::string GetBufferOptimizeMode() const;
  349. private:
  350. void SetInsertOpConfigPath(const std::vector<char> &cfg_path);
  351. std::vector<char> GetInsertOpConfigPathChar() const;
  352. void SetInputFormat(const std::vector<char> &format);
  353. std::vector<char> GetInputFormatChar() const;
  354. void SetInputShape(const std::vector<char> &shape);
  355. std::vector<char> GetInputShapeChar() const;
  356. std::vector<char> GetDynamicBatchSizeChar() const;
  357. void SetDynamicImageSize(const std::vector<char> &dynamic_image_size);
  358. std::vector<char> GetDynamicImageSizeChar() const;
  359. void SetPrecisionMode(const std::vector<char> &precision_mode);
  360. std::vector<char> GetPrecisionModeChar() const;
  361. void SetOpSelectImplMode(const std::vector<char> &op_select_impl_mode);
  362. std::vector<char> GetOpSelectImplModeChar() const;
  363. void SetFusionSwitchConfigPath(const std::vector<char> &cfg_path);
  364. std::vector<char> GetFusionSwitchConfigPathChar() const;
  365. void SetBufferOptimizeMode(const std::vector<char> &buffer_optimize_mode);
  366. std::vector<char> GetBufferOptimizeModeChar() const;
  367. };
  368. void Ascend310DeviceInfo::SetInsertOpConfigPath(const std::string &cfg_path) {
  369. SetInsertOpConfigPath(StringToChar(cfg_path));
  370. }
  371. std::string Ascend310DeviceInfo::GetInsertOpConfigPath() const { return CharToString(GetInsertOpConfigPathChar()); }
  372. void Ascend310DeviceInfo::SetInputFormat(const std::string &format) { SetInputFormat(StringToChar(format)); }
  373. std::string Ascend310DeviceInfo::GetInputFormat() const { return CharToString(GetInputFormatChar()); }
  374. void Ascend310DeviceInfo::SetInputShape(const std::string &shape) { SetInputShape(StringToChar(shape)); }
  375. std::string Ascend310DeviceInfo::GetInputShape() const { return CharToString(GetInputShapeChar()); }
  376. std::string Ascend310DeviceInfo::GetDynamicBatchSize() const { return CharToString(GetDynamicBatchSizeChar()); }
  377. void Ascend310DeviceInfo::SetDynamicImageSize(const std::string &dynamic_image_size) {
  378. SetDynamicImageSize(StringToChar(dynamic_image_size));
  379. }
  380. std::string Ascend310DeviceInfo::GetDynamicImageSize() const { return CharToString(GetDynamicImageSizeChar()); }
  381. void Ascend310DeviceInfo::SetPrecisionMode(const std::string &precision_mode) {
  382. SetPrecisionMode(StringToChar(precision_mode));
  383. }
  384. std::string Ascend310DeviceInfo::GetPrecisionMode() const { return CharToString(GetPrecisionModeChar()); }
  385. void Ascend310DeviceInfo::SetOpSelectImplMode(const std::string &op_select_impl_mode) {
  386. SetOpSelectImplMode(StringToChar(op_select_impl_mode));
  387. }
  388. std::string Ascend310DeviceInfo::GetOpSelectImplMode() const { return CharToString(GetOpSelectImplModeChar()); }
  389. void Ascend310DeviceInfo::SetFusionSwitchConfigPath(const std::string &cfg_path) {
  390. SetFusionSwitchConfigPath(StringToChar(cfg_path));
  391. }
  392. std::string Ascend310DeviceInfo::GetFusionSwitchConfigPath() const {
  393. return CharToString(GetFusionSwitchConfigPathChar());
  394. }
  395. void Ascend310DeviceInfo::SetBufferOptimizeMode(const std::string &buffer_optimize_mode) {
  396. SetBufferOptimizeMode(StringToChar(buffer_optimize_mode));
  397. }
  398. std::string Ascend310DeviceInfo::GetBufferOptimizeMode() const { return CharToString(GetBufferOptimizeModeChar()); }
  399. } // namespace mindspore
  400. #endif // MINDSPORE_INCLUDE_API_CONTEXT_H