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.

predict.cc 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #include "predict/predict.h"
  17. #include <memory>
  18. #include <vector>
  19. #include <string>
  20. namespace mindspore {
  21. namespace predictmodel {
  22. void StepConvertGraph(const KernelGraphPtr &kernel_graph_ptr) {
  23. MS_LOG(INFO) << "start convert_graph step";
  24. // get kernel_graph. this graph can be origin or device, depends on which steps to persistence
  25. MS_EXCEPTION_IF_NULL(kernel_graph_ptr);
  26. bool save_ms_model = MsContext::GetInstance()->save_ms_model_flag();
  27. if (save_ms_model) {
  28. if (kernel_graph_ptr->inputs().empty()) {
  29. return;
  30. }
  31. // set convert_mode: convert cpu info or convert Davnici
  32. executor::Kernel2Ms::GetInstance().set_convert_mode(executor::kConvertCpuMode);
  33. // convert kernel_graph to sub_ms_graph
  34. bool ret = executor::Kernel2Ms::GetInstance().KernelGraph2MsGraph(kernel_graph_ptr);
  35. if (!ret) {
  36. MS_LOG(WARNING) << "convert to mindsporeGraph failed";
  37. } else {
  38. MS_LOG(INFO) << "convert to Graph success";
  39. }
  40. }
  41. }
  42. void StepConvertWeight(const std::vector<tensor::TensorPtr> &inputs) {
  43. MS_LOG(INFO) << "start convert_input step";
  44. // get all inputs tensor
  45. bool save_ms_model = MsContext::GetInstance()->save_ms_model_flag();
  46. std::string save_path = MsContext::GetInstance()->save_ms_model_path();
  47. if (save_ms_model) {
  48. if (inputs.empty()) {
  49. return;
  50. }
  51. MS_LOG(INFO) << "save ms model is true to path " << save_path;
  52. if (!executor::Kernel2Ms::GetInstance().KernelInput2MS(inputs)) {
  53. MS_LOG(WARNING) << "convert mindspore kernel input failed";
  54. }
  55. auto new_ms_graph_ptr = std::make_shared<mindspore::predict::GraphDefT>();
  56. bool ret = executor::Kernel2Ms::GetInstance().SaveDeviceModel(new_ms_graph_ptr, save_path);
  57. if (!ret) {
  58. MS_LOG(WARNING) << "convert to mindsporeGraph failed";
  59. } else {
  60. MS_LOG(INFO) << "save ms model success";
  61. }
  62. }
  63. }
  64. } // namespace predictmodel
  65. } // namespace mindspore