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

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