• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2022 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 
17 #include "src/litert/cxx_api/model/model_impl.h"
18 #include "include/train/train_cfg.h"
19 #include "src/litert/cxx_api/converters.h"
20 #include "src/train/transfer_session.h"
21 namespace mindspore {
BuildTransferLearning(const std::shared_ptr<Graph> & backbone,const std::shared_ptr<Graph> & head)22 Status ModelImpl::BuildTransferLearning(const std::shared_ptr<Graph> &backbone, const std::shared_ptr<Graph> &head) {
23   const auto b_graph_data = backbone->graph_data_;
24   const auto h_graph_data = head->graph_data_;
25   if (b_graph_data == nullptr || h_graph_data == nullptr) {
26     MS_LOG(ERROR) << "graph data cannot be nullptr";
27     return kLiteNullptr;
28   }
29   bool is_train_session = h_graph_data->IsTrainModel();
30   if (is_train_session) {
31     const auto b_model = reinterpret_cast<lite::LiteModel *>(b_graph_data->lite_model().get());
32     const auto h_model = reinterpret_cast<lite::LiteModel *>(h_graph_data->lite_model().get());
33     if (h_model == nullptr || h_model->buf == nullptr || b_model == nullptr || b_model->buf == nullptr) {
34       MS_LOG(ERROR) << "Lite model has been freed.";
35       return kLiteNullptr;
36     }
37 
38     lite::TrainCfg train_cfg;
39     if (cfg_ != nullptr) {
40       auto status = A2L_ConvertConfig(cfg_.get(), &train_cfg);
41       if (status != kSuccess) {
42         MS_LOG(ERROR) << "Failed to convert Config to Lite Config";
43         return status;
44       }
45     }
46 
47     auto session = std::shared_ptr<lite::LiteSession>(
48       CreateTransferSessionInt(b_model->buf, b_model->buf_size_, h_model->buf, h_model->buf_size_,
49                                ContextUtils::Convert(context_.get()), true, &train_cfg));
50     if (session == nullptr) {
51       MS_LOG(ERROR) << "create session failed";
52       return kLiteMemoryFailed;
53     }
54     session_.swap(session);
55     return kSuccess;
56   }
57   MS_LOG(DEBUG) << "Session is not a train session.";
58   return kLiteError;
59 }
60 }  // namespace mindspore
61