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.

step_parallel.h 6.9 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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_CCSRC_FRONTEND_PARALLEL_STEP_PARALLEL_H_
  17. #define MINDSPORE_CCSRC_FRONTEND_PARALLEL_STEP_PARALLEL_H_
  18. #include <vector>
  19. #include <map>
  20. #include <memory>
  21. #include <string>
  22. #include <unordered_map>
  23. #include <utility>
  24. #include <set>
  25. #include "frontend/optimizer/opt.h"
  26. #include "frontend/parallel/strategy.h"
  27. #include "frontend/parallel/tensor_layout/tensor_redistribution.h"
  28. using OperatorInfoPtr = std::shared_ptr<mindspore::parallel::OperatorInfo>;
  29. namespace mindspore {
  30. namespace parallel {
  31. const uint64_t kUSecondInSecond = 1000000;
  32. struct LossNodeInfo {
  33. bool has_tuple_getitem = false;
  34. int dout_index = 0; // now don't support the sens is a tuple
  35. CNodePtr loss_node = nullptr;
  36. };
  37. std::vector<AnfNodePtr> CreateInput(const Operator &op, const AnfNodePtr &node, const std::string &instance_name);
  38. std::string CreateInstanceName(const CNodePtr &node, size_t index);
  39. void ForwardCommunication(OperatorVector forward_op, const CNodePtr &node);
  40. void InsertRedistribution(const RedistributionOpListPtr &redistribution_oplist_ptr, const CNodePtr &node,
  41. const FuncGraphPtr &func_graph, int pos, const CNodePtr &pre_node);
  42. TensorLayout GetTensorInLayout(const CNodePtr &pre_node, const PrimitivePtr &pre_prim,
  43. const OperatorInfoPtr &distribute_operator_pre);
  44. OperatorInfoPtr GetDistributeOperator(const CNodePtr &node);
  45. void Redistribution(const std::pair<AnfNodePtr, int> &node_pair, const OperatorInfoPtr &distribute_operator,
  46. const CNodePtr &middle_node, int index, TensorRedistribution tensor_redistribution,
  47. const CNodePtr &pre_node);
  48. bool StrategyFound(std::unordered_map<std::string, ValuePtr> attrs);
  49. bool IsParallelCareNode(const CNodePtr &cnode);
  50. void MarkForwardCNode(const FuncGraphPtr &root);
  51. bool FindCommunicationOp(const std::vector<AnfNodePtr> &all_nodes);
  52. void StepRedistribution(const CNodePtr &node, const OperatorInfoPtr &distribute_operator, const CNodePtr &insert_node,
  53. const TensorRedistribution &tensor_redistribution, const CNodePtr &pre_node);
  54. std::vector<AnfNodePtr> ReplaceOpInput(const Operator &replace_op, const std::string &instance_name,
  55. const CNodePtr &node);
  56. void StepReplaceOp(OperatorVector replace_op, const CNodePtr &node);
  57. void InsertVirtualDivOp(const VirtualDivOp &virtual_div_op, const CNodePtr &node);
  58. std::pair<AnfNodePtr, bool> FindParameter(const AnfNodePtr &node, const FuncGraphPtr &func_graph);
  59. std::pair<bool, CNodePtr> FindCNode(const AnfNodePtr &anode, const std::string &name, const FuncGraphPtr &func_graph);
  60. void InsertMirrorOps(const MirrorOps &mirror_ops, const CNodePtr &node);
  61. void BackwardCommunication(const OperatorInfoPtr &distribute_operator, const CNodePtr &node,
  62. const std::vector<std::pair<CNodePtr, LossNodeInfo>> &sens_loss_pairs);
  63. // Generate and init parallel operator
  64. OperatorInfoPtr OperatorInstance(const PrimitivePtr &prim, const PrimitiveAttrs &attrs,
  65. const std::vector<Shapes> &shape_list);
  66. // Generate without initing parallel operator
  67. OperatorInfoPtr NewOperatorInstance(const PrimitivePtr &prim, const PrimitiveAttrs &attrs,
  68. std::vector<Shapes> shape_list);
  69. // Extract strategy from attr
  70. StrategyPtr ExtractStrategy(std::unordered_map<std::string, ValuePtr> attrs);
  71. Shapes GetNodeShape(const AnfNodePtr &node);
  72. std::vector<AnfNodePtr> FindParameterByRefKeyNode(const AnfNodePtr &node, const FuncGraphPtr &func_graph);
  73. // Extract shape from anfnode
  74. std::vector<Shapes> ExtractShape(const CNodePtr &node);
  75. std::pair<AnfNodePtr, int> FindParallelCareNode(const AnfNodePtr &node);
  76. // Find finally sub graph
  77. std::pair<AnfNodePtr, int> FindSubGraph(const FuncGraphPtr &func_graph, const AnfNodePtr &parameter);
  78. // Set distribute shape for parameters abstract
  79. void SetParallelShape(const AnfNodePtr &parameter, const std::pair<AnfNodePtr, int> &res);
  80. // change parameters'shape in resource
  81. void CoverSliceShape(const FuncGraphPtr &root);
  82. void SetVirtualDatasetStrategy(const CNodePtr &node);
  83. // Creat parallel operator for primitive node(has strategy)
  84. void ExtractInformation(const std::vector<AnfNodePtr> &all_nodes);
  85. TensorLayout GetInputLayoutFromCNode(const std::pair<AnfNodePtr, int> &node_pair);
  86. std::shared_ptr<TensorLayout> FindNextLayout(const CNodePtr &node);
  87. std::shared_ptr<TensorLayout> GetOutputLayoutFromCNode(const CNodePtr &cnode, size_t output_index);
  88. std::shared_ptr<TensorLayout> FindPrevParallelCareNodeLayout(const AnfNodePtr &node, size_t output_index);
  89. std::shared_ptr<TensorLayout> FindPrevLayout(const AnfNodePtr &node);
  90. void ReshapeInit(const std::vector<AnfNodePtr> &all_nodes);
  91. // Add node for whole graph
  92. void ParallelCommunication(const FuncGraphPtr &root, const std::vector<AnfNodePtr> &all_nodes,
  93. const FuncGraphManagerPtr &manager);
  94. std::vector<std::pair<std::string, int>> NodeParameterName(const CNodePtr &node);
  95. void CheckpointStrategy(const std::vector<AnfNodePtr> &all_nodes);
  96. // main step of Parallel
  97. bool StepParallel(const FuncGraphPtr &func_graph, const opt::OptimizerPtr &optimizer);
  98. int32_t GetTupleGetItemIndex(const CNodePtr &cnode);
  99. Status ParallelInit();
  100. std::set<FuncGraphPtr> ForwardGraph(const FuncGraphPtr &root);
  101. std::vector<std::string> ExtractInputsTensorName(const CNodePtr &node);
  102. bool AnfNodeIsPrimitive(const AnfNodePtr &anf_node, const std::string &prim_name);
  103. using RefKeyPair = std::pair<AnfNodePtr, std::vector<AnfNodePtr>>;
  104. using ParameterUsersInfo = std::pair<std::string, std::pair<AnfNodePtr, AnfNodeIndexSet>>;
  105. RefKeyPair CNodeWithRefKeys(const AnfNodePtr &cnode);
  106. std::shared_ptr<TensorLayout> FindParameterNextLayout(const AnfNodePtr &node);
  107. ParameterUsersInfo FindParameterUsers(const AnfNodePtr &node, bool (*IsCareNode)(const CNodePtr &));
  108. void ApplyParallelOptOnParam(TensorLayout *tensor_layout, const OperatorInfoPtr &distribute_operator,
  109. const CNodePtr &cnode, const AnfNodePtr &parameter, size_t index);
  110. } // namespace parallel
  111. } // namespace mindspore
  112. #endif // MINDSPORE_CCSRC_FRONTEND_PARALLEL_STEP_PARALLEL_H_