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 8.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. constexpr auto kDeviceTypeAscend310 = "Ascend310";
  26. constexpr auto kDeviceTypeAscend910 = "Ascend910";
  27. constexpr auto kDeviceTypeGPU = "GPU";
  28. struct MS_API Context {
  29. public:
  30. Context();
  31. virtual ~Context() = default;
  32. struct Data;
  33. std::shared_ptr<Data> data;
  34. };
  35. struct MS_API GlobalContext : public Context {
  36. public:
  37. static std::shared_ptr<Context> GetGlobalContext();
  38. static inline void SetGlobalDeviceTarget(const std::string &device_target);
  39. static inline std::string GetGlobalDeviceTarget();
  40. static void SetGlobalDeviceID(const uint32_t &device_id);
  41. static uint32_t GetGlobalDeviceID();
  42. static inline void SetGlobalDumpConfigPath(const std::string &cfg_path);
  43. static inline std::string GetGlobalDumpConfigPath();
  44. private:
  45. // api without std::string
  46. static void SetGlobalDeviceTarget(const std::vector<char> &device_target);
  47. static std::vector<char> GetGlobalDeviceTargetChar();
  48. static void SetGlobalDumpConfigPath(const std::vector<char> &cfg_path);
  49. static std::vector<char> GetGlobalDumpConfigPathChar();
  50. };
  51. struct MS_API ModelContext : public Context {
  52. public:
  53. static inline void SetInsertOpConfigPath(const std::shared_ptr<Context> &context, const std::string &cfg_path);
  54. static inline std::string GetInsertOpConfigPath(const std::shared_ptr<Context> &context);
  55. static inline void SetInputFormat(const std::shared_ptr<Context> &context, const std::string &format);
  56. static inline std::string GetInputFormat(const std::shared_ptr<Context> &context);
  57. static inline void SetInputShape(const std::shared_ptr<Context> &context, const std::string &shape);
  58. static inline std::string GetInputShape(const std::shared_ptr<Context> &context);
  59. static void SetInputShapeMap(const std::shared_ptr<Context> &context, const std::map<int, std::vector<int>> &shape);
  60. static std::map<int, std::vector<int>> GetInputShapeMap(const std::shared_ptr<Context> &context);
  61. static void SetDynamicBatchSize(const std::shared_ptr<Context> &context,
  62. const std::vector<size_t> &dynamic_batch_size);
  63. static inline std::string GetDynamicBatchSize(const std::shared_ptr<Context> &context);
  64. static void SetOutputType(const std::shared_ptr<Context> &context, enum DataType output_type);
  65. static enum DataType GetOutputType(const std::shared_ptr<Context> &context);
  66. static inline void SetPrecisionMode(const std::shared_ptr<Context> &context, const std::string &precision_mode);
  67. static inline std::string GetPrecisionMode(const std::shared_ptr<Context> &context);
  68. static inline void SetOpSelectImplMode(const std::shared_ptr<Context> &context,
  69. const std::string &op_select_impl_mode);
  70. static inline std::string GetOpSelectImplMode(const std::shared_ptr<Context> &context);
  71. static inline void SetFusionSwitchConfigPath(const std::shared_ptr<Context> &context, const std::string &cfg_path);
  72. static inline std::string GetFusionSwitchConfigPath(const std::shared_ptr<Context> &context);
  73. static inline void SetGpuTrtInferMode(const std::shared_ptr<Context> &context, const std::string &gpu_trt_infer_mode);
  74. static inline std::string GetGpuTrtInferMode(const std::shared_ptr<Context> &context);
  75. private:
  76. // api without std::string
  77. static void SetInsertOpConfigPath(const std::shared_ptr<Context> &context, const std::vector<char> &cfg_path);
  78. static std::vector<char> GetInsertOpConfigPathChar(const std::shared_ptr<Context> &context);
  79. static void SetInputFormat(const std::shared_ptr<Context> &context, const std::vector<char> &format);
  80. static std::vector<char> GetInputFormatChar(const std::shared_ptr<Context> &context);
  81. static void SetInputShape(const std::shared_ptr<Context> &context, const std::vector<char> &shape);
  82. static std::vector<char> GetInputShapeChar(const std::shared_ptr<Context> &context);
  83. static void SetPrecisionMode(const std::shared_ptr<Context> &context, const std::vector<char> &precision_mode);
  84. static std::vector<char> GetPrecisionModeChar(const std::shared_ptr<Context> &context);
  85. static void SetOpSelectImplMode(const std::shared_ptr<Context> &context,
  86. const std::vector<char> &op_select_impl_mode);
  87. static std::vector<char> GetOpSelectImplModeChar(const std::shared_ptr<Context> &context);
  88. static void SetFusionSwitchConfigPath(const std::shared_ptr<Context> &context, const std::vector<char> &cfg_path);
  89. static std::vector<char> GetFusionSwitchConfigPathChar(const std::shared_ptr<Context> &context);
  90. static void SetGpuTrtInferMode(const std::shared_ptr<Context> &context, const std::vector<char> &gpu_trt_infer_mode);
  91. static std::vector<char> GetGpuTrtInferModeChar(const std::shared_ptr<Context> &context);
  92. static std::vector<char> GetDynamicBatchSizeChar(const std::shared_ptr<Context> &context);
  93. };
  94. void GlobalContext::SetGlobalDeviceTarget(const std::string &device_target) {
  95. SetGlobalDeviceTarget(StringToChar(device_target));
  96. }
  97. std::string GlobalContext::GetGlobalDeviceTarget() { return CharToString(GetGlobalDeviceTargetChar()); }
  98. void GlobalContext::SetGlobalDumpConfigPath(const std::string &cfg_path) {
  99. SetGlobalDumpConfigPath(StringToChar(cfg_path));
  100. }
  101. std::string GlobalContext::GetGlobalDumpConfigPath() { return CharToString(GetGlobalDumpConfigPathChar()); }
  102. void ModelContext::SetInsertOpConfigPath(const std::shared_ptr<Context> &context, const std::string &cfg_path) {
  103. SetInsertOpConfigPath(context, StringToChar(cfg_path));
  104. }
  105. std::string ModelContext::GetInsertOpConfigPath(const std::shared_ptr<Context> &context) {
  106. return CharToString(GetInsertOpConfigPathChar(context));
  107. }
  108. void ModelContext::SetInputFormat(const std::shared_ptr<Context> &context, const std::string &format) {
  109. SetInputFormat(context, StringToChar(format));
  110. }
  111. std::string ModelContext::GetInputFormat(const std::shared_ptr<Context> &context) {
  112. return CharToString(GetInputFormatChar(context));
  113. }
  114. void ModelContext::SetInputShape(const std::shared_ptr<Context> &context, const std::string &shape) {
  115. SetInputShape(context, StringToChar(shape));
  116. }
  117. std::string ModelContext::GetInputShape(const std::shared_ptr<Context> &context) {
  118. return CharToString(GetInputShapeChar(context));
  119. }
  120. void ModelContext::SetPrecisionMode(const std::shared_ptr<Context> &context, const std::string &precision_mode) {
  121. SetPrecisionMode(context, StringToChar(precision_mode));
  122. }
  123. std::string ModelContext::GetPrecisionMode(const std::shared_ptr<Context> &context) {
  124. return CharToString(GetPrecisionModeChar(context));
  125. }
  126. void ModelContext::SetOpSelectImplMode(const std::shared_ptr<Context> &context,
  127. const std::string &op_select_impl_mode) {
  128. SetOpSelectImplMode(context, StringToChar(op_select_impl_mode));
  129. }
  130. std::string ModelContext::GetOpSelectImplMode(const std::shared_ptr<Context> &context) {
  131. return CharToString(GetOpSelectImplModeChar(context));
  132. }
  133. void ModelContext::SetFusionSwitchConfigPath(const std::shared_ptr<Context> &context, const std::string &cfg_path) {
  134. SetFusionSwitchConfigPath(context, StringToChar(cfg_path));
  135. }
  136. std::string ModelContext::GetFusionSwitchConfigPath(const std::shared_ptr<Context> &context) {
  137. return CharToString(GetFusionSwitchConfigPathChar(context));
  138. }
  139. std::string ModelContext::GetDynamicBatchSize(const std::shared_ptr<Context> &context) {
  140. return CharToString(GetDynamicBatchSizeChar(context));
  141. }
  142. void ModelContext::SetGpuTrtInferMode(const std::shared_ptr<Context> &context, const std::string &gpu_trt_infer_mode) {
  143. SetGpuTrtInferMode(context, StringToChar(gpu_trt_infer_mode));
  144. }
  145. std::string ModelContext::GetGpuTrtInferMode(const std::shared_ptr<Context> &context) {
  146. return CharToString(GetGpuTrtInferModeChar(context));
  147. }
  148. } // namespace mindspore
  149. #endif // MINDSPORE_INCLUDE_API_CONTEXT_H