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.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 KernelGraphPtrNew &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. // set convert_mode: convert cpu info or convert Davnici
  29. executor::Kernel2Ms::GetInstance().set_convert_mode(executor::kConvertCpuMode);
  30. // convert kernel_graph to sub_ms_graph
  31. bool ret = executor::Kernel2Ms::GetInstance().KernelGraph2MsGraph(kernel_graph_ptr);
  32. if (!ret) {
  33. MS_LOG(WARNING) << "convert to mindsporeGraph failed";
  34. } else {
  35. MS_LOG(INFO) << "convert to Graph success";
  36. }
  37. }
  38. }
  39. void StepConvertWeight(const std::vector<tensor::TensorPtr> &inputs) {
  40. MS_LOG(INFO) << "start convert_input step";
  41. // get all inputs tensor
  42. bool save_ms_model = MsContext::GetInstance()->save_ms_model_flag();
  43. std::string save_path = MsContext::GetInstance()->save_ms_model_path();
  44. if (save_ms_model) {
  45. MS_LOG(INFO) << "save ms model is true to path " << save_path;
  46. if (!executor::Kernel2Ms::GetInstance().KernelInput2MS(inputs)) {
  47. MS_LOG(WARNING) << "convert mindspore kernel input failed";
  48. }
  49. auto new_ms_graph_ptr = std::make_shared<mindspore::predict::GraphDefT>();
  50. bool ret = executor::Kernel2Ms::GetInstance().SaveDeviceModel(new_ms_graph_ptr, save_path);
  51. if (!ret) {
  52. MS_LOG(WARNING) << "convert to mindsporeGraph failed";
  53. } else {
  54. MS_LOG(INFO) << "save ms model success";
  55. }
  56. }
  57. }
  58. executor::TargetMode GetDeviceTarget(const std::string &device_target) {
  59. if (device_target == "GPU") {
  60. return executor::kGPUTarget;
  61. } else if (device_target == "Ascend") {
  62. return executor::kCPUTarget;
  63. } else {
  64. return executor::kUnknowTarget;
  65. }
  66. }
  67. } // namespace predictmodel
  68. } // namespace mindspore