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.

kernel2ms.h 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Copyright 2019 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_MINDSPORE_CCSRC_PREDICT_CONVERTER_KERNEL_TO_MS_H_
  17. #define MINDSPORE_MINDSPORE_CCSRC_PREDICT_CONVERTER_KERNEL_TO_MS_H_
  18. #include <string>
  19. #include <unordered_map>
  20. #include <memory>
  21. #include <vector>
  22. #include <utility>
  23. #include "session/kernel_graph.h"
  24. #include "predict/converter/executor_tensor.h"
  25. #include "predict/schema/inner/ms_generated.h"
  26. #include "predict/converter/attr_utils/convert_util.h"
  27. static constexpr size_t kTupleGetItemIndex = 2;
  28. namespace mindspore {
  29. namespace executor {
  30. using KernelGraphPtr = std::shared_ptr<mindspore::session::KernelGraph>;
  31. enum ConvertMode { kConvertCpuMode, kConvertAscendMode, kConvertUnused };
  32. enum TargetMode { kCPUTarget, kGPUTarget, kUnknowTarget };
  33. class Kernel2Ms {
  34. public:
  35. static Kernel2Ms &GetInstance();
  36. Kernel2Ms(const Kernel2Ms &) = delete;
  37. Kernel2Ms &operator=(const Kernel2Ms &) = delete;
  38. bool KernelGraph2MsGraph(const KernelGraphPtr &kernel_graph_ptr);
  39. bool KernelInput2MS(const std::vector<TensorPtr> &input_tensors);
  40. ConvertMode convert_mode() const { return convert_mode_; }
  41. void set_convert_mode(ConvertMode convert_mode) { convert_mode_ = convert_mode; }
  42. TargetMode device_target() const { return device_target_; }
  43. void set_device_target(TargetMode device_target) { device_target_ = device_target; }
  44. bool SaveDeviceModel(const std::shared_ptr<GraphDefT> &new_ms_graph_ptr, const std::string &save_path_name);
  45. private:
  46. Kernel2Ms() : graph_index_(0) {}
  47. void ReleaseContextRes();
  48. ~Kernel2Ms() = default;
  49. bool SetAllTensors(const TensorCachePtr &tensor_cache, SubGraphDefT *sub_graph_def_t);
  50. bool SetOpInputIdx(const CNodePtr &c_node_ptr, const TensorCachePtr &tensor_cache, NodeDef *ms_node);
  51. bool SetOpOutputIdx(const CNodePtr &c_node_ptr, const TensorPtr &output_tensor, const TensorCachePtr &tensor_cache,
  52. int ref_count, size_t order_index, NodeDef *ms_node);
  53. bool SetGraphOutputIdx(const KernelGraphPtr &kernel_graph_ptr, const TensorCachePtr &tensor_cache,
  54. SubGraphDefT *sub_graph_def_t, AllOutputTensors *all_output_tensors);
  55. void TransformGraphIndx();
  56. void GetRealInpoutsPtr(const AnfNodePtr &node, std::vector<AnfNodePtr> *real_inputs,
  57. std::vector<size_t> *real_output_idx);
  58. bool InitGraphIndx(const KernelGraphPtr &kernel_graph_ptr);
  59. bool InitGraphInputsIndx(const KernelGraphPtr &kernel_graph_ptr);
  60. bool InitGraphValueNodesIndx(const KernelGraphPtr &kernel_graph_ptr);
  61. bool InitGraphOpsIndx(const KernelGraphPtr &kernel_graph_ptr);
  62. bool InitGraphOutputsIndx(const KernelGraphPtr &kernel_graph_ptr);
  63. bool SetGraphInputTensors(const KernelGraphPtr &kernel_graph_ptr, const TensorCachePtr &tensor_cache,
  64. SubGraphDefT *sub_graph_def_t);
  65. bool SetGraphValueTensors(const KernelGraphPtr &kernel_graph_ptr, const TensorCachePtr &tensor_cache);
  66. bool SetGraphOpTensors(const KernelGraphPtr &kernel_graph_ptr, const TensorCachePtr &tensor_cache,
  67. SubGraphDefT *sub_graph_def_t);
  68. std::vector<uint32_t> GetAllInputWeightIdxs() const { return input_weight_idxs_; }
  69. std::vector<uint32_t> GetAllInputIdxs() const { return all_input_idxs_; }
  70. bool CheckInputSizes(const std::vector<TensorPtr> &input_tensors, const std::vector<uint32_t> &all_input_idxs);
  71. bool SetMemResue() const;
  72. SubGraphPtr sub_ms_graph_;
  73. AllOutputTensors all_output_tensors_;
  74. std::vector<NodeDef *> tmp_op_nodes_;
  75. std::unordered_map<MsKernelKey, int> node_indexs_;
  76. std::unordered_map<int, MsKernelKey> index_nodes_;
  77. int graph_index_ = 0;
  78. TensorCachePtr tensor_cache_ptr_ = nullptr;
  79. ConvertMode convert_mode_ = kConvertCpuMode;
  80. TargetMode device_target_ = kCPUTarget;
  81. std::vector<uint32_t> input_weight_idxs_;
  82. std::vector<uint32_t> all_input_idxs_;
  83. };
  84. using Kernel2MsPtr = std::shared_ptr<Kernel2Ms>;
  85. } // namespace executor
  86. } // namespace mindspore
  87. #endif // MINDSPORE_MINDSPORE_CCSRC_PREDICT_CONVERTER_KERNEL_TO_MS_H_