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.

lite_session.h 4.9 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_LITE_INCLUDE_LITE_SESSION_H
  17. #define MINDSPORE_LITE_INCLUDE_LITE_SESSION_H
  18. #include <memory>
  19. #include <vector>
  20. #include <string>
  21. #include <unordered_map>
  22. #include "include/ms_tensor.h"
  23. #include "include/model.h"
  24. #include "include/context.h"
  25. namespace mindspore {
  26. namespace session {
  27. /// \brief LiteSession defined session in MindSpore Lite for compiling Model and forwarding model.
  28. class MS_API LiteSession {
  29. public:
  30. /// \brief Static method to create a LiteSession pointer.
  31. ///
  32. /// \param[in] context Define the context of session to be created.
  33. ///
  34. /// \return Pointer of MindSpore Lite LiteSession.
  35. static LiteSession *CreateSession(const lite::Context *context);
  36. /// \brief Static method to create a LiteSession pointer which has already compiled a model.
  37. ///
  38. /// \param[in] model_buf Define the buffer read from a model file.
  39. /// \param[in] size Define bytes number of model buffer.
  40. /// \param[in] context Define the context of session to be created.
  41. ///
  42. /// \return Pointer of MindSpore Lite LiteSession.
  43. static LiteSession *CreateSession(const char *model_buf, size_t size, const lite::Context *context);
  44. /// \brief Destructor of MindSpore Lite LiteSession.
  45. virtual ~LiteSession() = default;
  46. /// \brief Attempt to bind or unbind threads in the thread pool to or from the specified cpu core.
  47. ///
  48. /// \param[in] if_bind Define whether to bind or unbind threads.
  49. virtual void BindThread(bool if_bind) = 0;
  50. /// \brief Compile MindSpore Lite model.
  51. ///
  52. /// \note CompileGraph should be called before RunGraph.
  53. ///
  54. /// \param[in] model Define the model to be compiled.
  55. ///
  56. /// \return STATUS as an error code of compiling graph, STATUS is defined in errorcode.h.
  57. virtual int CompileGraph(lite::Model *model) = 0;
  58. /// \brief Get input MindSpore Lite MSTensors of model.
  59. ///
  60. /// \return The vector of MindSpore Lite MSTensor.
  61. virtual std::vector<tensor::MSTensor *> GetInputs() const = 0;
  62. /// \brief Get input MindSpore Lite MSTensors of model by tensor name.
  63. ///
  64. /// \param[in] node_name Define tensor name.
  65. ///
  66. /// \return The vector of MindSpore Lite MSTensor.
  67. virtual mindspore::tensor::MSTensor *GetInputsByTensorName(const std::string &tensor_name) const = 0;
  68. /// \brief Run session with callback.
  69. ///
  70. /// \param[in] before Define a call_back_function to be called before running each node.
  71. /// \param[in] after Define a call_back_function called after running each node.
  72. ///
  73. /// \note RunGraph should be called after CompileGraph.
  74. ///
  75. /// \return STATUS as an error code of running graph, STATUS is defined in errorcode.h.
  76. virtual int RunGraph(const KernelCallBack &before = nullptr, const KernelCallBack &after = nullptr) = 0;
  77. /// \brief Get output MindSpore Lite MSTensors of model by node name.
  78. ///
  79. /// \param[in] node_name Define node name.
  80. ///
  81. /// \note Deprecated, replace with GetOutputByTensorName
  82. ///
  83. /// \return The vector of MindSpore Lite MSTensor.
  84. virtual std::vector<tensor::MSTensor *> GetOutputsByNodeName(const std::string &node_name) const = 0;
  85. /// \brief Get output MindSpore Lite MSTensors of model mapped by tensor name.
  86. ///
  87. /// \return The map of output tensor name and MindSpore Lite MSTensor.
  88. virtual std::unordered_map<std::string, mindspore::tensor::MSTensor *> GetOutputs() const = 0;
  89. /// \brief Get name of output tensors of model compiled by this session.
  90. ///
  91. /// \return The vector of string as output tensor names in order.
  92. virtual std::vector<std::string> GetOutputTensorNames() const = 0;
  93. /// \brief Get output MindSpore Lite MSTensors of model by tensor name.
  94. ///
  95. /// \param[in] tensor_name Define tensor name.
  96. ///
  97. /// \return Pointer of MindSpore Lite MSTensor.
  98. virtual mindspore::tensor::MSTensor *GetOutputByTensorName(const std::string &tensor_name) const = 0;
  99. /// \brief Resize inputs shape.
  100. ///
  101. /// \param[in] inputs Define the inputs of the model.
  102. /// \param[in] dims Define the inputs new shape.
  103. ///
  104. /// \return STATUS as an error code of resize inputs, STATUS is defined in errorcode.h.
  105. virtual int Resize(const std::vector<tensor::MSTensor *> &inputs, const std::vector<std::vector<int>> &dims) = 0;
  106. };
  107. } // namespace session
  108. } // namespace mindspore
  109. #endif // MINDSPORE_LITE_INCLUDE_LITE_SESSION_H