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.

ascend_control_parser.h 5.3 kB

adapte to remove inline merge me commit for remove inline deal witch multiple cases of switch in ConstructKernelGraph deal with switch and call cases in ConstructKernelGraph fix bug and rebase master ConstructKernelGraph adapte to remove inline fix InsertMultipleAssignToGraph bug add graph input to new graph which is created for switch input replace CreateNewParameterFromCNode to NewParameter in order to set new parameter's abstract and kernel_info avoids create a new switch repeatedly when the cnode is a call switch without real input null pointer check update frontend code Revert "update frontend code" This reverts commit ce1f600d1e9b4b47d9b81122f981bbbe505dd250. update frontend code PR_2948 fix bug of CheckLabalIndex handle switch_layer in ConstructKernelGraph add attr for assign node to avoid erasing by cse pass cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem rebase master Revert "cherry-pick ms commit[59b35f690ddcc94ff35a4f4eaf3816121b32235b]:temporary avoid list getitem problem" This reverts commit 74c258f94260ca0769a1ef69c6ef8e831c301dbf. Revert "handle switch_layer in ConstructKernelGraph" This reverts commit cb5367f02d69facbca8d39e9234c501608aee27f. Revert "update frontend code PR_2948" This reverts commit 234ac583400a96a8ddd641f7a722e1ccd5e056c6. Revert "merge me commit for remove inline" This reverts commit 55c0ebd42b6699c7686f5ce585e745f87dd42280. fix diff after rebase master doing remove inline in me overwrite FindNodePrimitive Revert "doing remove inline in me" This reverts commit b42e893125bc624d323e855ac6ae615333c06e65.
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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_BACKEND_SESSION_ASCEND_CONTROL_PARSER_H
  17. #define MINDSPORE_CCSRC_BACKEND_SESSION_ASCEND_CONTROL_PARSER_H
  18. #include <set>
  19. #include <map>
  20. #include <vector>
  21. #include <tuple>
  22. #include <utility>
  23. #include <functional>
  24. #include <memory>
  25. #include "backend/session/kernel_graph.h"
  26. #include "base/base_ref.h"
  27. #include "utils/contract.h"
  28. #include "utils/union_find_set.h"
  29. namespace mindspore {
  30. namespace session {
  31. class AscendControlParser {
  32. public:
  33. static void LinkGraph(NotNull<KernelGraphPtr> kg);
  34. static void InsertDependToGraph(NotNull<KernelGraphPtr> kg, NotNull<AnfNodePtr> attch_node);
  35. static void InsertControlDependToGraph(NotNull<KernelGraphPtr> kg, NotNull<AnfNodePtr> first_node,
  36. NotNull<AnfNodePtr> second_node);
  37. static void ExecutorValidate(NotNull<KernelGraphPtr> root_graph);
  38. static void InsertMultipleAssignToGraph(NotNull<KernelGraphPtr> from_graph, const AnfNodePtr &jump_node,
  39. NotNull<AnfNodePtr> from, NotNull<AnfNodePtr> to);
  40. private:
  41. class ReferenceCounter;
  42. static void EraseParameter(NotNull<KernelGraphPtr> root_graph, const std::set<KernelGraphPtr> &graph_list);
  43. static void EraseAssign(std::shared_ptr<ReferenceCounter> parameter_count, const std::set<CNodePtr> &all_nodes,
  44. const std::map<AnfNodePtr, CNodePtr> &para_to_written_node,
  45. NotNull<KernelGraphPtr> root_graph, const std::set<KernelGraphPtr> &graph_list);
  46. static void EraseLabel(NotNull<KernelGraphPtr> root_graph);
  47. static void ChildGraphDataAssign(NotNull<KernelGraphPtr> kg,
  48. const NotNull<std::vector<std::pair<AnfNodePtr, AnfNodePtr>> *> link_list,
  49. const NotNull<std::set<KernelGraphPtr> *> memo);
  50. static NotNull<CNodePtr> GetStartLabel(NotNull<KernelGraphPtr> kg, const CNodePtr &last_node,
  51. const CNodePtr &last_label);
  52. static NotNull<CNodePtr> ProcessKernelGraph(NotNull<KernelGraphPtr> kg, const CNodePtr &last_node,
  53. const CNodePtr &last_label,
  54. const NotNull<std::set<KernelGraphPtr> *> memo);
  55. static void RecurseCall(NotNull<KernelGraphPtr> kg, NotNull<CNodePtr> cur_node, const CNodePtr &next_node,
  56. const NotNull<std::set<KernelGraphPtr> *> memo);
  57. static void RecurseSwitch(NotNull<KernelGraphPtr> kg, NotNull<CNodePtr> cur_node, const CNodePtr &next_node,
  58. const NotNull<std::set<KernelGraphPtr> *> memo);
  59. static void RecurseSwitchLayer(NotNull<KernelGraphPtr> kg, NotNull<CNodePtr> cur_node, const CNodePtr &next_node,
  60. const NotNull<std::set<KernelGraphPtr> *> memo);
  61. static void LinkParentGraph(NotNull<KernelGraphPtr> kg, const CNodePtr &from_graph_call_node,
  62. const CNodePtr &last_label);
  63. static AnfNodePtr InsertAssignToGraph(NotNull<KernelGraphPtr> kg, NotNull<AnfNodePtr> from, NotNull<AnfNodePtr> to);
  64. static std::vector<std::pair<KernelGraphPtr, std::vector<AnfNodePtr>>> ParseCallSwitchNode(
  65. NotNull<CNodePtr> call_node);
  66. static std::tuple<KernelGraphPtr, std::vector<AnfNodePtr>> ParsePartial(NotNull<AnfNodePtr> node);
  67. static void AttachChildGraphToReturnNode(NotNull<KernelGraphPtr> graph,
  68. const NotNull<std::set<KernelGraphPtr> *> memo);
  69. // root graph order
  70. static bool CheckLabelIndex(uint32_t index, uint32_t label_index, const CNodePtr &cnode,
  71. KernelGraphPtr *cur_child_graph);
  72. static std::vector<CNodePtr> RecurseGraph(NotNull<KernelGraphPtr> graph,
  73. const NotNull<std::set<KernelGraphPtr> *> memo);
  74. static void AttachOriginalInputsToGraph(NotNull<KernelGraphPtr> graph, const std::vector<AnfNodePtr> orig_inputs);
  75. };
  76. class AscendControlParser::ReferenceCounter {
  77. public:
  78. explicit ReferenceCounter(std::function<bool(int32_t, int32_t)> func) : predicate_(func), count_() {}
  79. ~ReferenceCounter() = default;
  80. void AddReadCount(const AnfNodePtr &key, int32_t num);
  81. void AddWriteCount(const AnfNodePtr &key, int32_t num);
  82. void EraseElem(const AnfNodePtr &key);
  83. bool HasValidElem() const;
  84. std::tuple<AnfNodePtr, int32_t, int32_t> GetOneValidElem() const;
  85. private:
  86. std::function<bool(int32_t, int32_t)> predicate_;
  87. std::map<AnfNodePtr, std::pair<int32_t, int32_t>> count_;
  88. };
  89. } // namespace session
  90. } // namespace mindspore
  91. #endif // MINDSPORE_CCSRC_BACKEND_SESSION_ASCEND_CONTROL_PARSER_H