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.

edge_costmodel.h 8.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 PARALLEL_AUTO_PARALLEL_EDGE_COSTMODEL_H_
  17. #define PARALLEL_AUTO_PARALLEL_EDGE_COSTMODEL_H_
  18. #include <map>
  19. #include <memory>
  20. #include <string>
  21. #include <utility>
  22. #include <vector>
  23. #include "common/utils.h"
  24. #include "parallel/auto_parallel/costmodel.h"
  25. #include "parallel/ops_info/operator_info.h"
  26. #include "parallel/tensor_layout/tensor_info.h"
  27. #include "parallel/tensor_layout/tensor_layout.h"
  28. namespace mindspore {
  29. namespace parallel {
  30. using CostPtrKey = std::pair<StrategyPtr, StrategyPtr>;
  31. using OperatorInfoPtr = std::shared_ptr<mindspore::parallel::OperatorInfo>;
  32. using EdgePtr = std::shared_ptr<mindspore::parallel::Edge>;
  33. class Edge {
  34. // An 'Edge' connects two Operators in the CostGraph.
  35. public:
  36. Edge(const std::string &edge_name, const std::shared_ptr<OperatorInfo> &prev_op,
  37. const std::shared_ptr<OperatorInfo> &next_op, const size_t &output_index_, const size_t &input_index_,
  38. const bool &is_com)
  39. : edge_name_(edge_name),
  40. prev_op_(prev_op),
  41. next_op_(next_op),
  42. prev_op_output_index_(output_index_),
  43. next_op_input_index_(input_index_),
  44. is_combined_(is_com) {
  45. is_identity_edge = false;
  46. }
  47. Edge(const std::string &edge_name, const std::shared_ptr<OperatorInfo> &prev_op,
  48. const std::shared_ptr<OperatorInfo> &next_op, const size_t &output_index_, const size_t &input_index_,
  49. const bool &is_com, const bool &is_iden)
  50. : edge_name_(edge_name),
  51. prev_op_(prev_op),
  52. next_op_(next_op),
  53. prev_op_output_index_(output_index_),
  54. next_op_input_index_(input_index_),
  55. is_combined_(is_com),
  56. is_identity_edge(is_iden) {}
  57. Edge(const std::string &edge_name, const std::shared_ptr<OperatorInfo> &prev_op,
  58. const std::shared_ptr<OperatorInfo> &next_op, const std::vector<size_t> &output_indexs_,
  59. const std::vector<size_t> &input_indexs_, const bool &is_com)
  60. : edge_name_(edge_name),
  61. prev_op_(prev_op),
  62. next_op_(next_op),
  63. pre_op_output_indexs_(output_indexs_),
  64. next_op_input_indexs_(input_indexs_),
  65. is_combined_(is_com) {
  66. prev_op_output_index_ = 0;
  67. next_op_input_index_ = 0;
  68. is_identity_edge = false;
  69. }
  70. ~Edge() = default;
  71. std::shared_ptr<OperatorInfo> prev_operator() const { return prev_op_; }
  72. std::shared_ptr<OperatorInfo> next_operator() const { return next_op_; }
  73. std::string edge_name() const { return edge_name_; }
  74. // Init cost_map_: for each output layout and input layout, calculate the cost
  75. Status InitEdgeCost();
  76. // For two operators u--->v, given the output tensor layout of u,
  77. // and the input tensor layout of v, return the redistribution cost,
  78. // and the op_list to carry out the redistribution.
  79. Status GetRedistributionCost(const TensorLayout &prev_op_output_layout, const TensorLayout &next_op_input_layout,
  80. size_t, TypePtr type, CostPtr *cost);
  81. void set_pre_op_output(const std::vector<std::pair<std::shared_ptr<Strategy>, std::vector<TensorInfo>>> &output_set) {
  82. pre_op_output_ = output_set;
  83. }
  84. void set_next_op_input(const std::vector<std::pair<std::shared_ptr<Strategy>, std::vector<TensorInfo>>> &input_set) {
  85. next_op_input_ = input_set;
  86. }
  87. // Given a pair of output strategy and input strategy, return the corresponding costlist
  88. CostPtrList GetCostList(StrategyPtr output_str, StrategyPtr input_str);
  89. std::vector<std::pair<std::shared_ptr<Strategy>, std::vector<TensorInfo>>> prev_op_output() const {
  90. return pre_op_output_;
  91. }
  92. std::vector<std::pair<std::shared_ptr<Strategy>, std::vector<TensorInfo>>> next_op_input() const {
  93. return next_op_input_;
  94. }
  95. bool is_combined() const { return is_combined_; }
  96. size_t prev_op_output_index() const { return prev_op_output_index_; }
  97. size_t next_op_input_index() const { return next_op_input_index_; }
  98. std::vector<size_t> prev_op_output_indexs() const { return pre_op_output_indexs_; }
  99. std::vector<size_t> next_op_input_indexs() const { return next_op_input_indexs_; }
  100. CostPtrList CreateEdgeEliminationCostList(const StrategyPtr &output_st_ptr,
  101. const std::vector<std::shared_ptr<Edge>> &edges,
  102. const StrategyPtr &input_st_ptr);
  103. // In the Edge Elimination operation in DP algorithm, 'edges' is replaced by a new edge. This method is used to
  104. // set cost for this new edge
  105. void EdgeEliminationSetNewCost(std::shared_ptr<OperatorInfo> u, const std::vector<std::shared_ptr<Edge>> &edges,
  106. std::shared_ptr<OperatorInfo> v);
  107. void CreateOpEliminationSubCostList(StrategyPtr op_strategy, const CostPtrList &left_cost_list,
  108. const CostPtrList &middle_cost_list, const CostPtrList &right_cost_list,
  109. CostPtrList *ret_cost_list);
  110. CostPtrList CreateOpEliminationCostList(const std::shared_ptr<Edge> &e1, const StrategyPtr &output_st_ptr,
  111. const std::shared_ptr<OperatorInfo> &op, const std::shared_ptr<Edge> &e2,
  112. const StrategyPtr &input_st_ptr);
  113. // In the Operation Elimination operation in DP algorithm, 'op', 'e1' and 'e2' are replaced by a new edge.
  114. // This method is used to set cost for this new edge
  115. void OpEliminationSetNewCost(const std::shared_ptr<Edge> &e1, const std::shared_ptr<OperatorInfo> &op,
  116. const std::shared_ptr<Edge> &e2);
  117. void set_selected_cost(const CostPtr &cost) { selected_cost_ = cost; }
  118. const CostPtr &selected_cost() const { return selected_cost_; }
  119. void set_parameter_involve(int para_invol) { is_output_parameter_involve_ = para_invol; }
  120. // When the input of a operator contains WEIGHT or a output from other operators involving WEIGHT, then these input
  121. // should stay in memory until it is used in the backward phase, which is kept in memory at the end of forward phase.
  122. Status CalculateMemoryCost();
  123. private:
  124. std::string edge_name_;
  125. std::shared_ptr<OperatorInfo> prev_op_, next_op_;
  126. std::map<CostPtrKey, CostPtrList> cost_map_;
  127. // pre_op_output_
  128. std::vector<std::pair<std::shared_ptr<Strategy>, std::vector<TensorInfo>>> pre_op_output_;
  129. std::vector<std::pair<std::shared_ptr<Strategy>, std::vector<TensorInfo>>> next_op_input_;
  130. // the index of outputs of prev_op, and the index of inputs of next_op
  131. size_t prev_op_output_index_, next_op_input_index_;
  132. // pre_op_output_indexs_ and next_op_input_indexs_ store the indexs of inputs and outputs if is_combined = true
  133. std::vector<size_t> pre_op_output_indexs_;
  134. std::vector<size_t> next_op_input_indexs_;
  135. // is this edge constructed by combining multiple edges? If is is, then is_combined = true, else is_combined = false
  136. bool is_combined_;
  137. // When a Parameter in the ANF graph being used by multiple operators, we include the Parameter in the costgraph by
  138. // replace the Parameter by a TmpIdentity operator, and connecting this TmpIdentity operator with subsequent
  139. // operators. The resulting edges are different from those normal edges, thus this Bool variable distinguishes them.
  140. // If it is true, then we should guarantee that the strategy for output tensor consistent with the input tensor.
  141. bool is_identity_edge;
  142. CostPtr selected_cost_;
  143. int is_output_parameter_involve_ = -1; // -1: unset; 0: not parameter_involved; 1: parameter_involved
  144. };
  145. } // namespace parallel
  146. } // namespace mindspore
  147. #endif // PARALLEL_AUTO_PARALLEL_EDGE_COSTMODEL_H_