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.

session.cc 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * Copyright 2019 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. #include "include/session.h"
  17. #include <map>
  18. #include <atomic>
  19. #include "include/errorcode.h"
  20. #include "common/mslog.h"
  21. #include "src/graph.h"
  22. #include "src/graph_execution.h"
  23. namespace mindspore {
  24. namespace predict {
  25. Context m_ctx;
  26. bool m_isConfig = false;
  27. // In 32bits, this evaluates to 2GB - 1
  28. static constexpr auto MAX_BUFFER_SIZE = ((1ULL << (sizeof(int32_t) * 8 - 1)) - 1);
  29. std::shared_ptr<Session> CreateSession(const char *graphBuf, size_t size, const Context &ctx) {
  30. if (graphBuf == nullptr) {
  31. MS_LOGE("the graphBuf is nullptr");
  32. return nullptr;
  33. }
  34. if (size > MAX_BUFFER_SIZE) {
  35. MS_LOGE("the size is invalid");
  36. return nullptr;
  37. }
  38. auto session = std::make_shared<Session>(ctx);
  39. MS_ASSERT(session != nullptr);
  40. auto ret = session->Init(graphBuf, size);
  41. if (ret != RET_OK) {
  42. MS_LOGE("Init session failed.");
  43. return nullptr;
  44. }
  45. return session;
  46. }
  47. Session::Session(const Context &ctx) : _ctx(ctx) {
  48. Context cfgCtx;
  49. cfgCtx = ctx;
  50. if (cfgCtx.threadNum > m_ctx.threadNum) {
  51. cfgCtx.threadNum = m_ctx.threadNum;
  52. }
  53. }
  54. int Session::Init(const char *graphBuf, size_t size) {
  55. _graph = Graph::CreateFromBuf(graphBuf, size, _ctx);
  56. if (_graph == nullptr) {
  57. MS_LOGE("Graph create from buf failed.");
  58. return RET_NULL_PTR;
  59. }
  60. auto ret = this->InitExecutor();
  61. if (ret != RET_OK) {
  62. MS_LOGE("Init Executor failed");
  63. return ret;
  64. }
  65. return ret;
  66. }
  67. int Session::InitExecutor() {
  68. if (_executor != nullptr) {
  69. delete _executor;
  70. _executor = nullptr;
  71. }
  72. if (_graph != nullptr) {
  73. _executor = new (std::nothrow) GraphExecution(_ctx, _graph);
  74. if (_executor == nullptr) {
  75. MS_LOGE("new GraphExecution fail");
  76. return RET_ERROR;
  77. }
  78. return RET_OK;
  79. } else {
  80. MS_LOGE("the graph is nullptr");
  81. return RET_ERROR;
  82. }
  83. }
  84. Session::~Session() {
  85. if (_executor != nullptr) {
  86. delete _executor;
  87. }
  88. if (_graph != nullptr) {
  89. delete _graph;
  90. }
  91. }
  92. int Session::Run(const std::vector<Tensor *> &inputs) {
  93. auto ret = RET_OK;
  94. if (reinitExecutor) {
  95. ret = this->InitExecutor();
  96. if (ret != RET_OK) {
  97. MS_LOGE("Init Executor failed");
  98. return ret;
  99. }
  100. }
  101. if (_executor == nullptr) {
  102. MS_LOGE("_executor is nullptr");
  103. return ret;
  104. }
  105. ret = _executor->Run(inputs);
  106. return ret;
  107. }
  108. std::vector<Tensor *> Session::GetInput() {
  109. if (_executor == nullptr) {
  110. MS_LOGE("_executor is nullptr");
  111. return std::vector<Tensor *>{};
  112. }
  113. auto inputs = _executor->GetInput();
  114. if (inputs.empty()) {
  115. MS_LOGI("output is empty.");
  116. }
  117. return inputs;
  118. }
  119. std::vector<Tensor *> Session::GetOutput(const std::string &nodeName) {
  120. if (_executor == nullptr) {
  121. MS_LOGE("graph's executor is nullptr.");
  122. return std::vector<Tensor *>{};
  123. }
  124. auto outputs = _executor->GetOutput(nodeName);
  125. if (outputs.empty()) {
  126. MS_LOGI("output is empty.");
  127. }
  128. return outputs;
  129. }
  130. std::map<std::string, std::vector<Tensor *>> Session::GetAllOutput() {
  131. if (_executor == nullptr) {
  132. MS_LOGE("graph's executor is nullptr.");
  133. return std::map<std::string, std::vector<Tensor *>>{};
  134. }
  135. auto outputs = _executor->GetAllOutput();
  136. if (outputs.empty()) {
  137. MS_LOGI("outputs is empty.");
  138. }
  139. return outputs;
  140. }
  141. } // namespace predict
  142. } // namespace mindspore