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 19 #include <string> 20 #include <vector> 21 #include <map> 22 #include <memory> 23 #include <utility> 24 #include "include/api/status.h" 25 #include "include/api/types.h" 26 #include "include/api/graph.h" 27 #include "include/api/context.h" 28 #include "include/api/callback/callback.h" 29 #include "include/api/cell.h" 30 #include "include/api/cfg.h" 31 #include "include/api/dual_abi_helper.h" 32 33 namespace mindspore { 34 class ModelImpl; 35 class Metrics; 36 37 namespace dataset { 38 class Dataset; 39 } // namespace dataset 40 /// \brief The Model class is used to define a MindSpore model, facilitating computational graph management. 41 class MS_API Model { 42 public: 43 Model(); 44 ~Model(); 45 46 /// \brief Build a model from model buffer so that it can run on a device. 47 /// 48 /// \param[in] model_data Define the buffer read from a model file. 49 /// \param[in] data_size Define bytes number of model buffer. 50 /// \param[in] model_type Define The type of model file. Options: ModelType::kMindIR, ModelType::kMindIR_Lite. Only 51 /// ModelType::kMindIR_Lite is valid for Device-side Inference. Cloud-side Inference supports options 52 /// ModelType::kMindIR and ModelType::kMindIR_Lite, but option odelType::kMindIR_Lite will be removed in future 53 /// iterations. \param[in] model_context Define the context used to store options during execution. 54 /// 55 /// \return Status. kSuccess: build success, kLiteModelRebuild: build model repeatedly, Other: other types of errors. 56 Status Build(const void *model_data, size_t data_size, ModelType model_type, 57 const std::shared_ptr<Context> &model_context = nullptr); 58 59 /// \brief Load and build a model from model buffer so that it can run on a device. 60 /// 61 /// \param[in] model_path Define the model path. 62 /// \param[in] model_type Define The type of model file. Options: ModelType::kMindIR, ModelType::kMindIR_Lite. Only 63 /// ModelType::kMindIR_Lite is valid for Device-side Inference. Cloud-side Inference supports options 64 /// ModelType::kMindIR and ModelType::kMindIR_Lite, but option odelType::kMindIR_Lite will be removed in future 65 /// iterations. \param[in] model_context Define the context used to store options during execution. 66 /// 67 /// \return Status. kSuccess: build success, kLiteModelRebuild: build model repeatedly, Other: other types of errors. 68 inline Status Build(const std::string &model_path, ModelType model_type, 69 const std::shared_ptr<Context> &model_context = nullptr); 70 /// \brief Build a model from model buffer so that it can run on a device. 71 /// 72 /// \param[in] model_data Define the buffer read from a model file. 73 /// \param[in] data_size Define bytes number of model buffer. 74 /// \param[in] model_type Define The type of model file. Options: ModelType::kMindIR, ModelType::kMindIR_Lite. Only 75 /// ModelType::kMindIR_Lite is valid for Device-side Inference. Cloud-side Inference supports options 76 /// ModelType::kMindIR and ModelType::kMindIR_Lite, but option odelType::kMindIR_Lite will be removed in future 77 /// iterations. \param[in] model_context Define the context used to store options during execution. \param[in] dec_key 78 /// Define the key used to decrypt the ciphertext model. The key length is 16. \param[in] dec_mode Define the 79 /// decryption mode. Options: AES-GCM. \param[in] cropto_lib_path Define the openssl library path. 80 /// 81 /// \return Status. kSuccess: build success, kLiteModelRebuild: build model repeatedly, Other: other types of errors. 82 inline Status Build(const void *model_data, size_t data_size, ModelType model_type, 83 const std::shared_ptr<Context> &model_context, const Key &dec_key, const std::string &dec_mode, 84 const std::string &cropto_lib_path); 85 86 /// \brief Load and build a model from model buffer so that it can run on a device. 87 /// 88 /// \param[in] model_path Define the model path. 89 /// \param[in] model_type Define The type of model file. Options: ModelType::kMindIR, ModelType::kMindIR_Lite. Only 90 /// ModelType::kMindIR_Lite is valid for Device-side Inference. Cloud-side Inference supports options 91 /// ModelType::kMindIR and ModelType::kMindIR_Lite, but option odelType::kMindIR_Lite will be removed in future 92 /// iterations. \param[in] model_context Define the context used to store options during execution. \param[in] dec_key 93 /// Define the key used to decrypt the ciphertext model. The key length is 16. \param[in] dec_mode Define the 94 /// decryption mode. Options: AES-GCM. \param[in] cropto_lib_path Define the openssl library path. 95 /// 96 /// \return Status. kSuccess: build success, kLiteModelRebuild: build model repeatedly, Other: other types of errors. 97 inline Status Build(const std::string &model_path, ModelType model_type, 98 const std::shared_ptr<Context> &model_context, const Key &dec_key, const std::string &dec_mode, 99 const std::string &cropto_lib_path); 100 101 /// \brief Build a model 102 /// 103 /// \param[in] graph GraphCell is a derivative of Cell. Cell is not available currently. GraphCell can be constructed 104 /// from Graph, for example, model.Build(GraphCell(graph), context). 105 /// \param[in] model_context A context used to store options during execution. 106 /// \param[in] train_cfg A config used by training. 107 /// 108 /// \return Status. 109 Status Build(GraphCell graph, const std::shared_ptr<Context> &model_context = nullptr, 110 const std::shared_ptr<TrainCfg> &train_cfg = nullptr); 111 112 /// \brief Build a Transfer Learning model where the backbone weights are fixed and the head weights are trainable 113 /// 114 /// \param[in] backbone The static, non-learnable part of the graph 115 /// \param[in] head The trainable part of the graph 116 /// \param[in] context A context used to store options during execution 117 /// \param[in] train_cfg A config used by training 118 /// 119 /// \return Status 120 Status BuildTransferLearning(GraphCell backbone, GraphCell head, const std::shared_ptr<Context> &context, 121 const std::shared_ptr<TrainCfg> &train_cfg = nullptr); 122 123 /// \brief Resize the shapes of inputs. 124 /// 125 /// \param[in] inputs A vector that includes all input tensors in order. 126 /// \param[in] dims Defines the new shapes of inputs, should be consistent with inputs. 127 /// 128 /// \return Status. 129 Status Resize(const std::vector<MSTensor> &inputs, const std::vector<std::vector<int64_t>> &dims); 130 131 /// \brief Change the size and or content of weight tensors 132 /// 133 /// \param[in] new_weights a vector of tensors with new shapes and data to use in the model 134 /// If data pointer is null, the data of the original tensors will be copied to the new ones 135 /// 136 /// \return Status. 137 Status UpdateWeights(const std::vector<MSTensor> &new_weights); 138 /// \brief Change the size and or content of weight tensors 139 /// 140 /// \param[in] new_weights A vector where model constant are arranged in sequence 141 /// 142 /// \return Status. 143 Status UpdateWeights(const std::vector<std::vector<MSTensor>> &new_weights); 144 /// \brief Inference model API. If use this API in train mode, it's equal to RunStep API. 145 /// 146 /// \param[in] inputs A vector where model inputs are arranged in sequence. 147 /// \param[out] outputs Which is a pointer to a vector. The model outputs are filled in the container in sequence. 148 /// \param[in] before CallBack before predict. 149 /// \param[in] after CallBack after predict. 150 /// 151 /// \return Status. 152 Status Predict(const std::vector<MSTensor> &inputs, std::vector<MSTensor> *outputs, 153 const MSKernelCallBack &before = nullptr, const MSKernelCallBack &after = nullptr); 154 155 /// \brief Inference model API. If use this API in train mode, it's equal to RunStep API. 156 /// 157 /// \param[in] before CallBack before predict. 158 /// \param[in] after CallBack after predict. 159 /// 160 /// \return Status. 161 Status Predict(const MSKernelCallBack &before = nullptr, const MSKernelCallBack &after = nullptr); 162 163 /// \brief Training API. Run model by step. 164 /// 165 /// \param[in] before CallBack before RunStep. 166 /// \param[in] after CallBack after RunStep. 167 /// 168 /// \return Status. 169 Status RunStep(const MSKernelCallBack &before = nullptr, const MSKernelCallBack &after = nullptr); 170 171 /// \brief Inference model with preprocess in model. 172 /// 173 /// \param[in] inputs A vector where model inputs are arranged in sequence. 174 /// \param[out] outputs Which is a pointer to a vector. The model outputs are filled in the container in sequence. 175 /// \param[in] before CallBack before predict. 176 /// \param[in] after CallBack after predict. 177 /// 178 /// \return Status. 179 Status PredictWithPreprocess(const std::vector<std::vector<MSTensor>> &inputs, std::vector<MSTensor> *outputs, 180 const MSKernelCallBack &before = nullptr, const MSKernelCallBack &after = nullptr); 181 182 /// \brief Apply data preprocess if it exits in model. 183 /// 184 /// \param[in] inputs A vector where model inputs are arranged in sequence. 185 /// \param[out] outputs Which is a pointer to a vector. The model outputs are filled in the container in sequence. 186 /// 187 /// \return Status. 188 Status Preprocess(const std::vector<std::vector<MSTensor>> &inputs, std::vector<MSTensor> *outputs); 189 190 /// \brief Check if data preprocess exists in model. 191 /// \return true if data preprocess exists. 192 bool HasPreprocess(); 193 194 /// \brief Load config file. 195 /// 196 /// \param[in] config_path config file path. 197 /// 198 /// \return Status. 199 inline Status LoadConfig(const std::string &config_path); 200 201 /// \brief Update config. 202 /// 203 /// \param[in] section define the config section. 204 /// \param[in] config define the config will be updated. 205 /// 206 /// \return Status. 207 inline Status UpdateConfig(const std::string §ion, const std::pair<std::string, std::string> &config); 208 209 /// \brief Obtains all input tensors of the model. 210 /// 211 /// \return The vector that includes all input tensors. 212 std::vector<MSTensor> GetInputs(); 213 214 /// \brief Obtains the input tensor of the model by name. 215 /// 216 /// \return The input tensor with the given name, if the name is not found, an invalid tensor is returned. 217 inline MSTensor GetInputByTensorName(const std::string &tensor_name); 218 219 /// \brief Obtain all gradient tensors of the model. 220 /// 221 /// \return The vector that includes all gradient tensors. 222 std::vector<MSTensor> GetGradients() const; 223 224 /// \brief Update gradient tensors of the model. 225 /// 226 /// \param[in] gradients A vector new gradients. 227 /// 228 /// \return Status of operation 229 Status ApplyGradients(const std::vector<MSTensor> &gradients); 230 231 /// \brief Obtain all weights tensors of the model. 232 /// 233 /// \return The vector that includes all weights tensors. 234 std::vector<MSTensor> GetFeatureMaps() const; 235 236 /// \brief Obtain all trainable parameters of the model optimizers. 237 /// 238 /// \return The vector that includes all trainable parameters. 239 std::vector<MSTensor> GetTrainableParams() const; 240 241 /// \brief Update weights tensors of the model. 242 /// 243 /// \param[in] new_weights A vector new weights. 244 /// 245 /// \return Status of operation 246 Status UpdateFeatureMaps(const std::vector<MSTensor> &new_weights); 247 248 /// \brief Obtain optimizer params tensors of the model. 249 /// 250 /// \return The vector that includes all params tensors. 251 std::vector<MSTensor> GetOptimizerParams() const; 252 253 /// \brief Update the optimizer parameters. 254 /// 255 /// \param[in] params A vector new optimizer params. 256 /// 257 /// \return Status of operation. 258 Status SetOptimizerParams(const std::vector<MSTensor> ¶ms); 259 260 /// \brief Setup training with virtual batches. 261 /// 262 /// \param[in] virtual_batch_multiplier - virtual batch multiplier, use any number < 1 to disable. 263 /// \param[in] lr - learning rate to use for virtual batch, -1 for internal configuration. 264 /// \param[in] momentum - batch norm momentum to use for virtual batch, -1 for internal configuration. 265 /// 266 /// \return Status of operation. 267 Status SetupVirtualBatch(int virtual_batch_multiplier, float lr = -1.0f, float momentum = -1.0f); 268 269 /// \brief Set the Learning Rate of the training. 270 /// 271 /// \param[in] learning_rate to set. 272 /// 273 /// \return Status of operation. 274 Status SetLearningRate(float learning_rate); 275 276 /// \brief Get the Learning Rate of the optimizer. 277 /// 278 /// \return Learning rate. 0.0 if no optimizer was found. 279 float GetLearningRate(); 280 281 /// \brief Initialize object with metrics. 282 /// 283 /// \param[in] metrics A vector of metrics objects. 284 /// 285 /// \return 0 on success or -1 in case of error 286 Status InitMetrics(std::vector<Metrics *> metrics); 287 288 /// \brief Accessor to TrainLoop metric objects 289 /// 290 /// \return A vector of metrics 291 std::vector<Metrics *> GetMetrics(); 292 293 /// \brief Obtains all output tensors of the model. 294 /// 295 /// \return The vector that includes all output tensors. 296 std::vector<MSTensor> GetOutputs(); 297 298 /// \brief Obtains names of all output tensors of the model. 299 /// 300 /// \return A vector that includes names of all output tensors. 301 inline std::vector<std::string> GetOutputTensorNames(); 302 303 /// \brief Obtains the output tensor of the model by name. 304 /// 305 /// \return The output tensor with the given name, if the name is not found, an invalid tensor is returned. 306 inline MSTensor GetOutputByTensorName(const std::string &tensor_name); 307 308 /// \brief Get output MSTensors of model by node name. 309 /// 310 /// \param[in] node_name Define node name. 311 /// 312 /// \note Deprecated, replace with GetOutputByTensorName 313 /// 314 /// \return The vector of output MSTensor. 315 inline std::vector<MSTensor> GetOutputsByNodeName(const std::string &node_name); 316 317 /// \brief Bind GLTexture2D object to cl Memory. 318 /// 319 /// \param[in] inputGLTexture The input GLTexture id for Model. 320 /// \param[in] outputGLTexture The output GLTexture id for Model. 321 /// 322 /// \return Status of operation. 323 324 Status BindGLTexture2DMemory(const std::map<std::string, unsigned int> &inputGLTexture, 325 std::map<std::string, unsigned int> *outputGLTexture); 326 327 /// \brief Inference model. 328 /// 329 /// \param[in] device_type Device type,options are kGPU, kAscend etc. 330 /// \param[in] model_type The type of model file, options are ModelType::kMindIR, ModelType::kOM. 331 /// 332 /// \return Is supported or not. 333 static bool CheckModelSupport(enum DeviceType device_type, ModelType model_type); 334 335 /// \brief Set the model running mode. 336 /// 337 /// \param[in] train True means model runs in Train Mode, otherwise Eval Mode. 338 /// 339 /// \return Status of operation. 340 Status SetTrainMode(bool train); 341 342 /// \brief Get the model running mode. 343 /// 344 /// \return Is Train Mode or not. 345 bool GetTrainMode() const; 346 347 /// \brief Performs the training Loop in Train Mode. 348 /// 349 /// \param[in] epochs The number of epoch to run. 350 /// \param[in] ds A smart pointer to MindData Dataset object. 351 /// \param[in] cbs A vector of TrainLoopCallBack objects. 352 /// 353 /// \return Status of operation. 354 Status Train(int epochs, std::shared_ptr<dataset::Dataset> ds, std::vector<TrainCallBack *> cbs); 355 356 /// \brief Performs the training loop over all data in Eval Mode. 357 /// 358 /// \param[in] ds A smart pointer to MindData Dataset object. 359 /// \param[in] cbs A vector of TrainLoopCallBack objects. 360 /// 361 /// \return Status of operation. 362 Status Evaluate(std::shared_ptr<dataset::Dataset> ds, std::vector<TrainCallBack *> cbs); 363 364 const std::shared_ptr<ModelImpl> impl() const { return impl_; } 365 366 /// \brief Get model info by key 367 /// 368 /// \param[in] key The key of model info key-value pair 369 /// 370 /// \return The value of the model info associated with the given key. 371 inline std::string GetModelInfo(const std::string &key); 372 373 // release inference resourcec, only used for mindspore_lite's ascend backend now. 374 Status Finalize(); 375 376 private: 377 friend class Serialization; 378 // api without std::string 379 MSTensor GetInputByTensorName(const std::vector<char> &tensor_name); 380 std::vector<std::vector<char>> GetOutputTensorNamesChar(); 381 MSTensor GetOutputByTensorName(const std::vector<char> &tensor_name); 382 std::vector<MSTensor> GetOutputsByNodeName(const std::vector<char> &node_name); 383 Status LoadConfig(const std::vector<char> &config_path); 384 Status UpdateConfig(const std::vector<char> §ion, const std::pair<std::vector<char>, std::vector<char>> &config); 385 Status Build(const std::vector<char> &model_path, ModelType model_type, 386 const std::shared_ptr<Context> &model_context); 387 Status Build(const void *model_data, size_t data_size, ModelType model_type, 388 const std::shared_ptr<Context> &model_context, const Key &dec_key, const std::vector<char> &dec_mode, 389 const std::vector<char> &cropto_lib_path); 390 Status Build(const std::vector<char> &model_path, ModelType model_type, const std::shared_ptr<Context> &model_context, 391 const Key &dec_key, const std::vector<char> &dec_mode, const std::vector<char> &cropto_lib_path); 392 std::vector<char> GetModelInfo(const std::vector<char> &key); 393 std::shared_ptr<ModelImpl> impl_; 394 }; 395 396 MSTensor Model::GetInputByTensorName(const std::string &tensor_name) { 397 return GetInputByTensorName(StringToChar(tensor_name)); 398 } 399 400 std::vector<std::string> Model::GetOutputTensorNames() { return VectorCharToString(GetOutputTensorNamesChar()); } 401 402 MSTensor Model::GetOutputByTensorName(const std::string &tensor_name) { 403 return GetOutputByTensorName(StringToChar(tensor_name)); 404 } 405 406 std::vector<MSTensor> Model::GetOutputsByNodeName(const std::string &node_name) { 407 return GetOutputsByNodeName(StringToChar(node_name)); 408 } 409 410 Status Model::LoadConfig(const std::string &config_path) { return LoadConfig(StringToChar(config_path)); } 411 412 Status Model::UpdateConfig(const std::string §ion, const std::pair<std::string, std::string> &config) { 413 std::pair<std::vector<char>, std::vector<char>> config_pair = {StringToChar(config.first), 414 StringToChar(config.second)}; 415 return UpdateConfig(StringToChar(section), config_pair); 416 } 417 418 Status Model::Build(const void *model_data, size_t data_size, ModelType model_type, 419 const std::shared_ptr<Context> &model_context, const Key &dec_key, const std::string &dec_mode, 420 const std::string &cropto_lib_path) { 421 return Build(model_data, data_size, model_type, model_context, dec_key, StringToChar(dec_mode), 422 StringToChar(cropto_lib_path)); 423 } 424 425 Status Model::Build(const std::string &model_path, ModelType model_type, const std::shared_ptr<Context> &model_context, 426 const Key &dec_key, const std::string &dec_mode, const std::string &cropto_lib_path) { 427 return Build(StringToChar(model_path), model_type, model_context, dec_key, StringToChar(dec_mode), 428 StringToChar(cropto_lib_path)); 429 } 430 431 Status Model::Build(const std::string &model_path, ModelType model_type, 432 const std::shared_ptr<Context> &model_context) { 433 return Build(StringToChar(model_path), model_type, model_context); 434 } 435 436 inline std::string Model::GetModelInfo(const std::string &key) { return CharToString(GetModelInfo(StringToChar(key))); } 437 } // namespace mindspore 438 #endif // MINDSPORE_INCLUDE_API_MODEL_H 439