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.

model.h 7.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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_MODEL_H
  17. #define MINDSPORE_INCLUDE_API_MODEL_H
  18. #include <string>
  19. #include <vector>
  20. #include <map>
  21. #include <memory>
  22. #include <utility>
  23. #include "include/api/status.h"
  24. #include "include/api/types.h"
  25. #include "include/api/graph.h"
  26. #include "include/api/context.h"
  27. #include "include/api/callback/callback.h"
  28. #include "include/api/cell.h"
  29. #include "include/api/cfg.h"
  30. #include "include/api/dual_abi_helper.h"
  31. namespace mindspore {
  32. class ModelImpl;
  33. class Metrics;
  34. namespace dataset {
  35. class Dataset;
  36. } // namespace dataset
  37. /// \brief The Model class is used to define a MindSpore model, facilitating computational graph management.
  38. class MS_API Model {
  39. public:
  40. Model();
  41. ~Model();
  42. Model(const Model &) = delete;
  43. void operator=(const Model &) = delete;
  44. /// \brief Builds a model so that it can run on a device.
  45. ///
  46. /// \param[in] graph GraphCell is a derivative of Cell. Cell is not available currently. GraphCell can be constructed
  47. /// from Graph, for example, model.Build(GraphCell(graph), context).
  48. /// \param[in] model_context A context used to store options during execution.
  49. /// \param[in] train_cfg A config used by training.
  50. ///
  51. /// \return Status.
  52. Status Build(GraphCell graph, const std::shared_ptr<Context> &model_context = nullptr,
  53. const std::shared_ptr<TrainCfg> &train_cfg = nullptr);
  54. /// \brief Resizes the shapes of inputs.
  55. ///
  56. /// \param[in] inputs A vector that includes all input tensors in order.
  57. /// \param[in] dims Defines the new shapes of inputs, should be consistent with inputs.
  58. ///
  59. /// \return Status.
  60. Status Resize(const std::vector<MSTensor> &inputs, const std::vector<std::vector<int64_t>> &dims);
  61. /// \brief Inference model.
  62. ///
  63. /// \param[in] inputs A vector where model inputs are arranged in sequence.
  64. /// \param[out] outputs Which is a pointer to a vector. The model outputs are filled in the container in sequence.
  65. /// \param[in] before CallBack before predict.
  66. /// \param[in] after CallBack after predict.
  67. ///
  68. /// \return Status.
  69. Status Predict(const std::vector<MSTensor> &inputs, std::vector<MSTensor> *outputs,
  70. const MSKernelCallBack &before = nullptr, const MSKernelCallBack &after = nullptr);
  71. /// \brief Load config file.
  72. ///
  73. /// \param[in] config_path config file path.
  74. ///
  75. /// \return Status.
  76. Status LoadConfig(const std::string &config_path);
  77. /// \brief Obtains all input tensors of the model.
  78. ///
  79. /// \return The vector that includes all input tensors.
  80. std::vector<MSTensor> GetInputs();
  81. /// \brief Obtains the input tensor of the model by name.
  82. ///
  83. /// \return The input tensor with the given name, if the name is not found, an invalid tensor is returned.
  84. inline MSTensor GetInputByTensorName(const std::string &tensor_name);
  85. Status InitMetrics(std::vector<Metrics *> metrics);
  86. std::vector<Metrics *> GetMetrics();
  87. /// \brief Obtains all output tensors of the model.
  88. ///
  89. /// \return The vector that includes all output tensors.
  90. std::vector<MSTensor> GetOutputs();
  91. /// \brief Obtains names of all output tensors of the model.
  92. ///
  93. /// \return A vector that includes names of all output tensors.
  94. inline std::vector<std::string> GetOutputTensorNames();
  95. /// \brief Obtains the output tensor of the model by name.
  96. ///
  97. /// \return The output tensor with the given name, if the name is not found, an invalid tensor is returned.
  98. inline MSTensor GetOutputByTensorName(const std::string &tensor_name);
  99. /// \brief Get output MSTensors of model by node name.
  100. ///
  101. /// \param[in] node_name Define node name.
  102. ///
  103. /// \note Deprecated, replace with GetOutputByTensorName
  104. ///
  105. /// \return The vector of output MSTensor.
  106. inline std::vector<MSTensor> GetOutputsByNodeName(const std::string &node_name);
  107. /// \brief Inference model.
  108. ///
  109. /// \param[in] device_type Device type,options are kGPU, kAscend910, etc.
  110. /// \param[in] model_type The type of model file, options are ModelType::kMindIR, ModelType::kOM.
  111. ///
  112. /// \return Is supported or not.
  113. static bool CheckModelSupport(enum DeviceType device_type, ModelType model_type);
  114. Status SetTrainMode(bool train);
  115. bool GetTrainMode() const;
  116. Status Train(int epochs, std::shared_ptr<dataset::Dataset> ds, std::vector<TrainCallBack *> cbs);
  117. Status Evaluate(std::shared_ptr<dataset::Dataset> ds, std::vector<TrainCallBack *> cbs);
  118. /// \brief Build a model from model buffer so that it can run on a device. Only valid for Lite.
  119. ///
  120. /// \param[in] model_data Define the buffer read from a model file.
  121. /// \param[in] size Define bytes number of model buffer.
  122. /// \param[in] model_type Define The type of model file. Options: ModelType::kMindIR, ModelType::kOM. Only
  123. /// ModelType::kMindIR is valid for Lite.
  124. /// \param[in] model_context Define the context used to store options during execution.
  125. /// \param[in] dec_key Define the key used to decrypt the ciphertext model. The key length is 16, 24, or 32.
  126. /// \param[in] dec_mode Define the decryption mode. Options: AES-GCM, AES-CBC.
  127. ///
  128. /// \return Status.
  129. Status Build(const void *model_data, size_t data_size, ModelType model_type,
  130. const std::shared_ptr<Context> &model_context = nullptr, const Key &dec_key = {},
  131. const std::string &dec_mode = kDecModeAesGcm);
  132. /// \brief Load and build a model from model buffer so that it can run on a device. Only valid for Lite.
  133. ///
  134. /// \param[in] model_path Define the model path.
  135. /// \param[in] model_type Define The type of model file. Options: ModelType::kMindIR, ModelType::kOM. Only
  136. /// ModelType::kMindIR is valid for Lite.
  137. /// \param[in] model_context Define the context used to store options during execution.
  138. /// \param[in] dec_key Define the key used to decrypt the ciphertext model. The key length is 16, 24, or 32.
  139. /// \param[in] dec_mode Define the decryption mode. Options: AES-GCM, AES-CBC.
  140. ///
  141. /// \return Status.
  142. Status Build(const std::string &model_path, ModelType model_type,
  143. const std::shared_ptr<Context> &model_context = nullptr, const Key &dec_key = {},
  144. const std::string &dec_mode = kDecModeAesGcm);
  145. private:
  146. friend class Serialization;
  147. // api without std::string
  148. MSTensor GetInputByTensorName(const std::vector<char> &tensor_name);
  149. std::vector<std::vector<char>> GetOutputTensorNamesChar();
  150. MSTensor GetOutputByTensorName(const std::vector<char> &tensor_name);
  151. std::vector<MSTensor> GetOutputsByNodeName(const std::vector<char> &node_name);
  152. std::shared_ptr<ModelImpl> impl_;
  153. };
  154. MSTensor Model::GetInputByTensorName(const std::string &tensor_name) {
  155. return GetInputByTensorName(StringToChar(tensor_name));
  156. }
  157. std::vector<std::string> Model::GetOutputTensorNames() { return VectorCharToString(GetOutputTensorNamesChar()); }
  158. MSTensor Model::GetOutputByTensorName(const std::string &tensor_name) {
  159. return GetOutputByTensorName(StringToChar(tensor_name));
  160. }
  161. std::vector<MSTensor> Model::GetOutputsByNodeName(const std::string &node_name) {
  162. return GetOutputsByNodeName(StringToChar(node_name));
  163. }
  164. } // namespace mindspore
  165. #endif // MINDSPORE_INCLUDE_API_MODEL_H