diff --git a/include/api/cfg.h b/include/api/cfg.h index f4e3bbf027e..d2bc7b424d6 100644 --- a/include/api/cfg.h +++ b/include/api/cfg.h @@ -22,6 +22,7 @@ #include #include "include/api/data_type.h" #include "include/api/dual_abi_helper.h" +#include "include/api/types.h" namespace mindspore { diff --git a/include/api/model.h b/include/api/model.h index 84659ed25cb..4a93415ec7a 100644 --- a/include/api/model.h +++ b/include/api/model.h @@ -83,6 +83,14 @@ class MS_API Model { Status Predict(const std::vector &inputs, std::vector *outputs, const MSKernelCallBack &before = nullptr, const MSKernelCallBack &after = nullptr); + /// \brief Train model by step. + /// + /// \param[in] before CallBack before predict. + /// \param[in] after CallBack after predict. + /// + /// \return Status. + Status RunStep(const MSKernelCallBack &before = nullptr, const MSKernelCallBack &after = nullptr); + /// \brief Inference model with preprocess in model. /// /// \param[in] inputs A vector where model inputs are arranged in sequence. @@ -143,7 +151,18 @@ class MS_API Model { /// \return Status of operation Status ApplyGradients(const std::vector &gradients); - /// \brief Obtains optimizer params tensors of the model. + /// \brief Obtains all weights tensors of the model. + /// + /// \return The vector that includes all gradient tensors. + std::vector GetFeatureMaps() const; + + /// \brief update weights tensors of the model. + /// + /// \param[in] inputs A vector new weights. + /// \return Status of operation + Status UpdateFeatureMaps(const std::vector &new_weights); + + /// \brief Obtains optimizer params tensors of the model. /// /// \return The vector that includes all params tensors. std::vector GetOptimizerParams() const; diff --git a/mindspore/lite/src/cxx_api/model/model.cc b/mindspore/lite/src/cxx_api/model/model.cc index 8ee26f01eae..6c032d5e108 100644 --- a/mindspore/lite/src/cxx_api/model/model.cc +++ b/mindspore/lite/src/cxx_api/model/model.cc @@ -19,14 +19,14 @@ #include #endif #include -#include "include/api/types.h" -#include "include/api/context.h" #include "include/api/callback/callback.h" +#include "include/api/context.h" #include "include/api/dual_abi_helper.h" -#include "src/cxx_api/model/model_impl.h" -#include "src/cxx_api/callback/callback_impl.h" -#include "src/cxx_api/callback/callback_adapter.h" +#include "include/api/types.h" #include "src/common/log_adapter.h" +#include "src/cxx_api/callback/callback_adapter.h" +#include "src/cxx_api/callback/callback_impl.h" +#include "src/cxx_api/model/model_impl.h" namespace mindspore { std::mutex g_impl_init_lock; @@ -113,6 +113,16 @@ Status Model::UpdateWeights(const std::vector &new_weights) { return impl_->UpdateWeights(new_weights); } +Status Model::RunStep(const MSKernelCallBack &before, const MSKernelCallBack &after) { + if (impl_ == nullptr) { + MS_LOG(ERROR) << "Model implement is null."; + return kLiteNullptr; + } + auto inputs = impl_->GetInputs(); + auto outputs = impl_->GetOutputs(); + return impl_->Predict(inputs, &outputs, before, after); +} + Status Model::Predict(const std::vector &inputs, std::vector *outputs, const MSKernelCallBack &before, const MSKernelCallBack &after) { if (impl_ == nullptr) { @@ -291,6 +301,23 @@ Status Model::ApplyGradients(const std::vector &gradients) { return impl_->ApplyGradients(gradients); } +std::vector Model::GetFeatureMaps() const { + std::vector empty; + if (impl_ == nullptr) { + MS_LOG(ERROR) << "Model implement is null."; + return empty; + } + return impl_->GetFeatureMaps(); +} + +Status Model::UpdateFeatureMaps(const std::vector &new_weights) { + if ((impl_ == nullptr) || (impl_->session_ == nullptr)) { + MS_LOG(ERROR) << "Model is null."; + return kLiteUninitializedObj; + } + return impl_->UpdateFeatureMaps(new_weights); +} + std::vector Model::GetOptimizerParams() const { std::vector empty; if (impl_ == nullptr) { diff --git a/mindspore/lite/src/cxx_api/model/model_impl.cc b/mindspore/lite/src/cxx_api/model/model_impl.cc index 8eab6f5a0e0..d4601c5ca17 100644 --- a/mindspore/lite/src/cxx_api/model/model_impl.cc +++ b/mindspore/lite/src/cxx_api/model/model_impl.cc @@ -410,6 +410,44 @@ Status ModelImpl::ApplyGradients(const std::vector &gradients) { return static_cast(ret); } +std::vector ModelImpl::GetFeatureMaps() const { + std::vector empty; + if (session_ == nullptr) { + MS_LOG(ERROR) << "Session is null."; + return empty; + } + auto params = session_->GetFeatureMaps(); + if (params.empty()) { + MS_LOG(ERROR) << "No optimizer parameters avelibale."; + return empty; + } + std::vector res = LiteTensorsToMSTensors(params, false); + return res; +} + +Status ModelImpl::UpdateFeatureMaps(const std::vector &new_weights) { + if (session_ == nullptr) { + MS_LOG(ERROR) << "Session is null."; + return kLiteNullptr; + } + if (new_weights.empty()) { + MS_LOG(ERROR) << "gradients is null."; + return kLiteInputParamInvalid; + } + std::vector inner_weights; + inner_weights.resize(new_weights.size()); + for (size_t i = 0; i < new_weights.size(); i++) { + auto new_weight = new_weights[i]; + if (new_weight.impl_ == nullptr || new_weight.impl_->lite_tensor() == nullptr) { + MS_LOG(ERROR) << "gradient tensor " << new_weight.Name() << " is null."; + return kLiteInputTensorError; + } + inner_weights[i] = new_weight.impl_->lite_tensor(); + } + auto ret = session_->UpdateFeatureMaps(inner_weights); + return static_cast(ret); +} + std::vector ModelImpl::GetOptimizerParams() const { std::vector empty; if (session_ == nullptr) { diff --git a/mindspore/lite/src/cxx_api/model/model_impl.h b/mindspore/lite/src/cxx_api/model/model_impl.h index 4c9da69ef79..2cf48991016 100644 --- a/mindspore/lite/src/cxx_api/model/model_impl.h +++ b/mindspore/lite/src/cxx_api/model/model_impl.h @@ -77,6 +77,8 @@ class ModelImpl { std::vector GetOutputs(); std::vector GetGradients() const; Status ApplyGradients(const std::vector &gradients); + std::vector GetFeatureMaps() const; + Status UpdateFeatureMaps(const std::vector &new_weights); std::vector GetOptimizerParams() const; Status SetOptimizerParams(const std::vector ¶ms); MSTensor GetInputByTensorName(const std::string &name); diff --git a/mindspore/lite/test/config/cropped_size.cfg b/mindspore/lite/test/config/cropped_size.cfg index f2d398bff0b..a5188f30f3e 100644 --- a/mindspore/lite/test/config/cropped_size.cfg +++ b/mindspore/lite/test/config/cropped_size.cfg @@ -1 +1 @@ -790864 +839924