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

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <set>
  22. #include <string>
  23. #include <unordered_map>
  24. #include <utility>
  25. #include "frontend/optimizer/opt.h"
  26. #include "frontend/parallel/strategy.h"
  27. #include "frontend/parallel/tensor_layout/tensor_redistribution.h"
  28. #include "pipeline/jit/pipeline.h"
  29. #include "frontend/parallel/ops_info/ops_utils.h"
  30. #include "frontend/parallel/auto_parallel/operator_costmodel.h"
  31. using OperatorInfoPtr = std::shared_ptr<mindspore::parallel::OperatorInfo>;
  32. namespace mindspore {
  33. namespace parallel {
  34. const uint64_t kUSecondInSecond = 1000000;
  35. const int32_t RECURSION_LIMIT = 3;
  36. struct LossNodeInfo {
  37. bool has_tuple_getitem = false;
  38. int64_t dout_index = 0; // now don't support the sens is a tuple
  39. CNodePtr loss_node = nullptr;
  40. };
  41. std::vector<AnfNodePtr> CreateInput(const Operator &op, const AnfNodePtr &node, const std::string &instance_name);
  42. std::string CreateInstanceName(const CNodePtr &node, size_t index);
  43. void ForwardCommunication(OperatorVector forward_op, const CNodePtr &node);
  44. void InsertRedistribution(const RedistributionOpListPtr &redistribution_oplist_ptr, const CNodePtr &node,
  45. const FuncGraphPtr &func_graph, int64_t pos, const CNodePtr &pre_node);
  46. TensorLayout GetTensorInLayout(const CNodePtr &pre_node, const PrimitivePtr &pre_prim,
  47. const OperatorInfoPtr &distribute_operator_pre);
  48. OperatorInfoPtr GetDistributeOperator(const CNodePtr &node);
  49. void Redistribution(const std::pair<AnfNodePtr, int64_t> &node_pair, const OperatorInfoPtr &distribute_operator,
  50. const CNodePtr &middle_node, int64_t index, TensorRedistribution tensor_redistribution,
  51. const CNodePtr &pre_node);
  52. bool StrategyFound(std::unordered_map<std::string, ValuePtr> attrs);
  53. bool IsParallelCareNode(const CNodePtr &cnode);
  54. void MarkForwardCNode(const FuncGraphPtr &root);
  55. bool FindCommunicationOp(const std::vector<AnfNodePtr> &all_nodes);
  56. void StepRedistribution(const CNodePtr &node, const OperatorInfoPtr &distribute_operator, const CNodePtr &insert_node,
  57. const TensorRedistribution &tensor_redistribution, const CNodePtr &pre_node);
  58. std::vector<AnfNodePtr> ReplaceOpInput(const Operator &replace_op, const std::string &instance_name,
  59. const CNodePtr &node);
  60. void StepReplaceOp(OperatorVector replace_op, const CNodePtr &node);
  61. void InsertVirtualDivOp(const VirtualDivOp &virtual_div_op, const CNodePtr &node);
  62. std::pair<AnfNodePtr, bool> FindParameter(const AnfNodePtr &node, const FuncGraphPtr &func_graph);
  63. std::pair<bool, CNodePtr> FindCNode(const AnfNodePtr &anode, const std::string &name, const FuncGraphPtr &func_graph);
  64. // Generate and init parallel operator
  65. OperatorInfoPtr OperatorInstance(const PrimitivePtr &prim, const PrimitiveAttrs &attrs,
  66. const std::vector<Shapes> &shape_list);
  67. // Generate without initing parallel operator
  68. OperatorInfoPtr NewOperatorInstance(const PrimitivePtr &prim, const PrimitiveAttrs &attrs,
  69. std::vector<Shapes> shape_list);
  70. // Extract strategy from attr
  71. StrategyPtr ExtractStrategy(std::unordered_map<std::string, ValuePtr> attrs);
  72. Shapes GetNodeShape(const AnfNodePtr &node);
  73. // Extract shape from anfnode
  74. std::vector<Shapes> ExtractShape(const CNodePtr &node);
  75. // Find finally sub graph
  76. std::pair<AnfNodePtr, int64_t> FindSubGraph(const FuncGraphPtr &func_graph, const AnfNodePtr &parameter);
  77. // Set distribute shape for parameters abstract
  78. std::string SetParallelShape(const AnfNodePtr &parameter, const std::pair<AnfNodePtr, int64_t> &res);
  79. // change parameters'shape in resource
  80. void CoverSliceShape(const FuncGraphPtr &root);
  81. void SetVirtualDatasetStrategy(const CNodePtr &node);
  82. // Create parallel operator for primitive node(has strategy)
  83. void ExtractInformation(const std::vector<AnfNodePtr> &all_nodes, bool is_training = true);
  84. TensorLayout GetInputLayoutFromCNode(const std::pair<AnfNodePtr, int64_t> &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. StrategyPtr GenerateBatchParallelStrategy(const OperatorInfoPtr operator_, const PrimitivePtr prim);
  91. bool IsLastStage();
  92. // Add node for whole graph
  93. void ParallelCommunication(const FuncGraphPtr &root, const std::vector<AnfNodePtr> &all_nodes,
  94. const FuncGraphManagerPtr &manager);
  95. std::vector<std::pair<std::string, int64_t>> NodeParameterName(const CNodePtr &node);
  96. void CheckpointStrategy(const std::vector<AnfNodePtr> &all_nodes);
  97. // main step of Parallel
  98. bool StepParallel(const FuncGraphPtr &func_graph, const opt::OptimizerPtr &optimizer);
  99. int64_t GetTupleGetItemIndex(const CNodePtr &cnode);
  100. Status ParallelInit();
  101. std::set<FuncGraphPtr> ForwardGraph(const FuncGraphPtr &root);
  102. std::vector<std::string> ExtractInputsTensorName(const CNodePtr &node);
  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. bool IsUsedParameter(const FuncGraphPtr &graph, const AnfNodePtr &parameter);
  109. void ApplyParallelOptOnParam(TensorLayout *tensor_layout, const OperatorInfoPtr &distribute_operator,
  110. const CNodePtr &cnode, const AnfNodePtr &parameter, size_t index);
  111. void SetLastNodeStrategy(const StrategyPtr strategyPtr);
  112. bool CreateGroupsByCkptFile(const std::string &file);
  113. void FindLastNodesUniqueId(const std::vector<AnfNodePtr> &all_nodes, std::vector<std::string> *unique_ids);
  114. } // namespace parallel
  115. } // namespace mindspore
  116. #endif // MINDSPORE_CCSRC_FRONTEND_PARALLEL_STEP_PARALLEL_H_