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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_PARALLEL_STEP_PARALLEL_H_
  17. #define MINDSPORE_CCSRC_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 "./common.h"
  26. #include "optimizer/opt.h"
  27. #include "parallel/strategy.h"
  28. #include "parallel/tensor_layout/tensor_redistribution.h"
  29. using OperatorInfoPtr = std::shared_ptr<mindspore::parallel::OperatorInfo>;
  30. namespace mindspore {
  31. namespace parallel {
  32. const uint64_t kUSecondInSecond = 1000000;
  33. struct LossNodeInfo {
  34. bool has_tuple_getitem = false;
  35. int dout_index = 0; // now don't support the sens is a tuple
  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, bool is_loss_node);
  62. // Generate and init parallel operator
  63. OperatorInfoPtr OperatorInstance(const PrimitivePtr &prim, const PrimitiveAttrs &attrs,
  64. const std::vector<Shapes> &shape_list);
  65. // Generate without initing parallel operator
  66. OperatorInfoPtr NewOperatorInstance(const PrimitivePtr &prim, const PrimitiveAttrs &attrs,
  67. std::vector<Shapes> shape_list);
  68. // Extract strategy from attr
  69. StrategyPtr ExtractStrategy(std::unordered_map<std::string, ValuePtr> attrs);
  70. Shapes GetNodeShape(const AnfNodePtr &node);
  71. std::vector<AnfNodePtr> FindParameterByRefKeyNode(const AnfNodePtr &node, const FuncGraphPtr &func_graph);
  72. // Extract shape from anfnode
  73. std::vector<Shapes> ExtractShape(const CNodePtr &node);
  74. std::pair<AnfNodePtr, int> FindParallelCareNode(const AnfNodePtr &node);
  75. // Find finally sub graph
  76. std::pair<AnfNodePtr, int> FindSubGraph(const FuncGraphPtr &func_graph, const AnfNodePtr &parameter);
  77. // Set distribute shape for parameters abstract
  78. void SetParallelShape(const AnfNodePtr &parameter, const std::pair<AnfNodePtr, int> &res);
  79. // change parameters'shape in resource
  80. void CoverSliceShape(const FuncGraphPtr &root);
  81. void SetVirtualDatasetStrategy(const CNodePtr &node);
  82. // Creat parallel operator for primitive node(has strategy)
  83. void ExtractInformation(const std::vector<AnfNodePtr> &all_nodes);
  84. TensorLayout GetInputLayoutFromCNode(const std::pair<AnfNodePtr, int> &node_pair);
  85. std::shared_ptr<TensorLayout> FindNextLayout(const CNodePtr &node);
  86. std::shared_ptr<TensorLayout> GetOutputLayoutFromCNode(const CNodePtr &cnode, size_t output_index);
  87. std::shared_ptr<TensorLayout> FindPrevParallelCareNodeLayout(const AnfNodePtr &node, size_t output_index);
  88. std::shared_ptr<TensorLayout> FindPrevLayout(const AnfNodePtr &node);
  89. void ReshapeInit(const std::vector<AnfNodePtr> &all_nodes);
  90. // Add node for whole graph
  91. void ParallelCommunication(const FuncGraphPtr &root, const std::vector<AnfNodePtr> &all_nodes,
  92. const FuncGraphManagerPtr &manager);
  93. void RestoreStrategy(const FuncGraphPtr &func_graph);
  94. void CheckpointStrategy(const FuncGraphPtr &func_graph);
  95. // main step of Parallel
  96. bool StepParallel(const FuncGraphPtr &func_graph, const opt::OptimizerPtr &optimizer);
  97. int32_t GetTupleGetItemIndex(const CNodePtr &cnode);
  98. std::vector<CNodePtr> FindLossCNodeFromRoot(const FuncGraphPtr &root);
  99. Status ParallelInit();
  100. std::vector<std::string> ExtractInputsTensorName(const CNodePtr &node);
  101. std::set<FuncGraphPtr> ForwardGraph(const FuncGraphPtr &root);
  102. } // namespace parallel
  103. } // namespace mindspore
  104. #endif // MINDSPORE_CCSRC_PARALLEL_STEP_PARALLEL_H_