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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. private:
  42. // api without std::string
  43. static void SetGlobalDeviceTarget(const std::vector<char> &device_target);
  44. static std::vector<char> GetGlobalDeviceTargetChar();
  45. };
  46. struct MS_API ModelContext : public Context {
  47. public:
  48. static inline void SetInsertOpConfigPath(const std::shared_ptr<Context> &context, const std::string &cfg_path);
  49. static inline std::string GetInsertOpConfigPath(const std::shared_ptr<Context> &context);
  50. static inline void SetInputFormat(const std::shared_ptr<Context> &context, const std::string &format);
  51. static inline std::string GetInputFormat(const std::shared_ptr<Context> &context);
  52. static inline void SetInputShape(const std::shared_ptr<Context> &context, const std::string &shape);
  53. static inline std::string GetInputShape(const std::shared_ptr<Context> &context);
  54. static void SetOutputType(const std::shared_ptr<Context> &context, enum DataType output_type);
  55. static enum DataType GetOutputType(const std::shared_ptr<Context> &context);
  56. static inline void SetPrecisionMode(const std::shared_ptr<Context> &context, const std::string &precision_mode);
  57. static inline std::string GetPrecisionMode(const std::shared_ptr<Context> &context);
  58. static inline void SetOpSelectImplMode(const std::shared_ptr<Context> &context,
  59. const std::string &op_select_impl_mode);
  60. static inline std::string GetOpSelectImplMode(const std::shared_ptr<Context> &context);
  61. private:
  62. // api without std::string
  63. static void SetInsertOpConfigPath(const std::shared_ptr<Context> &context, const std::vector<char> &cfg_path);
  64. static std::vector<char> GetInsertOpConfigPathChar(const std::shared_ptr<Context> &context);
  65. static void SetInputFormat(const std::shared_ptr<Context> &context, const std::vector<char> &format);
  66. static std::vector<char> GetInputFormatChar(const std::shared_ptr<Context> &context);
  67. static void SetInputShape(const std::shared_ptr<Context> &context, const std::vector<char> &shape);
  68. static std::vector<char> GetInputShapeChar(const std::shared_ptr<Context> &context);
  69. static void SetPrecisionMode(const std::shared_ptr<Context> &context, const std::vector<char> &precision_mode);
  70. static std::vector<char> GetPrecisionModeChar(const std::shared_ptr<Context> &context);
  71. static void SetOpSelectImplMode(const std::shared_ptr<Context> &context,
  72. const std::vector<char> &op_select_impl_mode);
  73. static std::vector<char> GetOpSelectImplModeChar(const std::shared_ptr<Context> &context);
  74. };
  75. void GlobalContext::SetGlobalDeviceTarget(const std::string &device_target) {
  76. SetGlobalDeviceTarget(StringToChar(device_target));
  77. }
  78. std::string GlobalContext::GetGlobalDeviceTarget() { return CharToString(GetGlobalDeviceTargetChar()); }
  79. void ModelContext::SetInsertOpConfigPath(const std::shared_ptr<Context> &context, const std::string &cfg_path) {
  80. SetInsertOpConfigPath(context, StringToChar(cfg_path));
  81. }
  82. std::string ModelContext::GetInsertOpConfigPath(const std::shared_ptr<Context> &context) {
  83. return CharToString(GetInsertOpConfigPathChar(context));
  84. }
  85. void ModelContext::SetInputFormat(const std::shared_ptr<Context> &context, const std::string &format) {
  86. SetInputFormat(context, StringToChar(format));
  87. }
  88. std::string ModelContext::GetInputFormat(const std::shared_ptr<Context> &context) {
  89. return CharToString(GetInputFormatChar(context));
  90. }
  91. void ModelContext::SetInputShape(const std::shared_ptr<Context> &context, const std::string &shape) {
  92. SetInputShape(context, StringToChar(shape));
  93. }
  94. std::string ModelContext::GetInputShape(const std::shared_ptr<Context> &context) {
  95. return CharToString(GetInputShapeChar(context));
  96. }
  97. void ModelContext::SetPrecisionMode(const std::shared_ptr<Context> &context, const std::string &precision_mode) {
  98. SetPrecisionMode(context, StringToChar(precision_mode));
  99. }
  100. std::string ModelContext::GetPrecisionMode(const std::shared_ptr<Context> &context) {
  101. return CharToString(GetPrecisionModeChar(context));
  102. }
  103. void ModelContext::SetOpSelectImplMode(const std::shared_ptr<Context> &context,
  104. const std::string &op_select_impl_mode) {
  105. SetOpSelectImplMode(context, StringToChar(op_select_impl_mode));
  106. }
  107. std::string ModelContext::GetOpSelectImplMode(const std::shared_ptr<Context> &context) {
  108. return CharToString(GetOpSelectImplModeChar(context));
  109. }
  110. } // namespace mindspore
  111. #endif // MINDSPORE_INCLUDE_API_CONTEXT_H