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.

function_block.h 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
  3. *
  4. * Copyright 2019 Huawei Technologies Co., Ltd
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef PIPELINE_PARSE_FUNCTION_BLOCK_H_
  19. #define PIPELINE_PARSE_FUNCTION_BLOCK_H_
  20. #include <vector>
  21. #include <string>
  22. #include <map>
  23. #include <set>
  24. #include <unordered_map>
  25. #include <memory>
  26. #include <utility>
  27. #include "pipeline/parse/parse_base.h"
  28. #include "utils/log_adapter.h"
  29. namespace mindspore {
  30. namespace parse {
  31. class Parser;
  32. class NameSpace;
  33. class Symbol;
  34. class FunctionBlock;
  35. using FunctionBlockPtr = std::shared_ptr<FunctionBlock>;
  36. // A function block is a straight-line code sequence with no branches, every block has one one exit point
  37. // which is return. When parsing function, loop or branch , we use function block to track the structure of
  38. // the original source code.
  39. class FunctionBlock : public std::enable_shared_from_this<FunctionBlock> {
  40. public:
  41. explicit FunctionBlock(const Parser &parser);
  42. virtual ~FunctionBlock() {}
  43. FuncGraphPtr func_graph() { return func_graph_; }
  44. void WriteVariable(const std::string &var_name, const AnfNodePtr &node);
  45. AnfNodePtr ReadVariable(const std::string &var_name);
  46. void AddPrevBlock(const FunctionBlockPtr &block);
  47. void SetPhiArgument(const ParameterPtr &phi);
  48. void CollectRemovablePhi(const ParameterPtr &phi);
  49. // A block is matured if all its predecessors is generated
  50. void Mature();
  51. CNodePtr ForceToBoolNode(const AnfNodePtr &cond);
  52. void Jump(const FunctionBlockPtr &block, AnfNodePtr node);
  53. AnfNodePtr SearchReplaceNode(const std::string &var, const ParameterPtr &phi);
  54. void ConditionalJump(AnfNodePtr condNode, const FunctionBlockPtr &trueBlock, const FunctionBlockPtr &falseBlock);
  55. // record the assign statement of self.xx weight parameter ,which will use state_setitem op
  56. void SetStateAssgin(const AnfNodePtr &target, const std::string &readid);
  57. void AddAutoDepend(const AnfNodePtr &target);
  58. void InsertDependItemsBeforeReturn();
  59. void AddGlobalVar(const std::string &var_name) { (void)global_vars_.insert(var_name); }
  60. bool IsGlobalVar(const std::string &var_name) { return global_vars_.find(var_name) != global_vars_.end(); }
  61. AnfNodePtr MakeResolveAstOp(const py::object &op);
  62. AnfNodePtr MakeResolveClassMember(std::string attr);
  63. AnfNodePtr MakeResolveSymbol(const std::string &value);
  64. AnfNodePtr MakeResolveOperation(const std::string &value);
  65. AnfNodePtr MakeResolve(const std::shared_ptr<NameSpace> &name_space, const std::shared_ptr<Symbol> &resolve_symbol);
  66. const std::unordered_map<ParameterPtr, AnfNodePtr> &removable_phis() const { return removable_phis_; }
  67. private:
  68. // block graph
  69. FuncGraphPtr func_graph_;
  70. // the block's parser
  71. const Parser &parser_;
  72. // A block is matured if all its prev_blocks is processed
  73. bool matured_;
  74. // store the nest-level block
  75. // refer to comments in Parser::func_block_list_;
  76. std::vector<FunctionBlock *> prev_blocks_;
  77. // store args and variable's node
  78. std::map<std::string, AnfNodePtr> vars_;
  79. // phi_nodes map the parameter node to variable, it can be resolved if the block's predecessors are processed
  80. std::map<ParameterPtr, std::string> phi_nodes_;
  81. // jumps map the successor block and the function call that perform jump
  82. // refer to comments in Parser::func_block_list_ that how to break the cyclic reference
  83. std::map<FunctionBlock *, CNodePtr> jumps_;
  84. // keeps all removable phis which will be removed in one pass.
  85. std::unordered_map<ParameterPtr, AnfNodePtr> removable_phis_;
  86. // set state nodes need to insert before function return nodes.
  87. std::unordered_map<AnfNodePtr, std::string> state_assign_;
  88. // hold declared global variables in function
  89. std::set<std::string> global_vars_;
  90. // other depend need to insert before function return nodes.
  91. // summary or some other node
  92. std::vector<AnfNodePtr> auto_depends_;
  93. };
  94. } // namespace parse
  95. } // namespace mindspore
  96. #endif // PIPELINE_PARSE_FUNCTION_BLOCK_H_