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