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.

opt.h 4.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * Copyright 2019-2021 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_OPTIMIZER_OPT_H_
  17. #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_OPT_H_
  18. #include <deque>
  19. #include <memory>
  20. #include <string>
  21. #include <vector>
  22. #include "utils/hash_map.h"
  23. #include "ir/anf.h"
  24. #include "ir/func_graph.h"
  25. #include "frontend/optimizer/optimizer_caller.h"
  26. #include "frontend/operator/ops.h"
  27. namespace mindspore {
  28. /* namespace to support opt */
  29. namespace opt {
  30. // Define the interaction mode between an Optimize pass and Renormalize pass
  31. // FORCE_RENORM: if the pass modified the graph then the next Renormalize will be executed
  32. // CHECK_RENORM: check if the new node is un-typed to decide if the next Renormalize will be executted
  33. enum RenormAction : int64_t { FORCE_RENORM = 0, CHECK_RENORM };
  34. class Substitution {
  35. public:
  36. OptimizerCallerPtr transform_;
  37. std::string name_;
  38. PredicateFuncType predicate_{nullptr};
  39. // An enum to mark this Substitution relation to renormalize pass.
  40. RenormAction renorm_action_;
  41. // Determine whether it is a priority substitution, that is, some patterns need to be matched prior to others.
  42. bool has_priority_pattern_{false};
  43. Substitution(const OptimizerCallerPtr &transform, const std::string &name, const PredicateFuncType &predicate,
  44. const RenormAction &renorm_action, bool has_priority_pattern)
  45. : transform_(transform),
  46. name_(name),
  47. predicate_(predicate),
  48. renorm_action_(renorm_action),
  49. has_priority_pattern_(has_priority_pattern) {}
  50. ~Substitution() = default;
  51. AnfNodePtr operator()(const OptimizerPtr &optimizer, const AnfNodePtr &node);
  52. };
  53. using SubstitutionPtr = std::shared_ptr<Substitution>;
  54. SubstitutionPtr MakeSubstitution(const OptimizerCallerPtr &transform, const std::string &name, const PrimitivePtr &prim,
  55. const RenormAction &action_renorm = CHECK_RENORM, bool has_priority_pattern = false);
  56. SubstitutionPtr MakeSubstitution(const OptimizerCallerPtr &transform, const std::string &name,
  57. const std::vector<PrimitivePtr> &prims,
  58. const RenormAction &action_renorm = CHECK_RENORM, bool has_priority_pattern = false);
  59. SubstitutionPtr MakeSubstitution(const OptimizerCallerPtr &transform, const std::string &name,
  60. const PredicateFuncType &predicate, const RenormAction &action_renorm = CHECK_RENORM,
  61. bool has_priority_pattern = false);
  62. enum OptTraverseSubstitutionsMode { kOptTraverseFromIRToSubstitutions = 0, kOptTraverseFromSubstitutionsToIR };
  63. class SubstitutionList {
  64. public:
  65. explicit SubstitutionList(const std::vector<SubstitutionPtr> &patterns, bool is_once = false,
  66. bool global_sensitive = false)
  67. : list_(patterns), is_once_(is_once), global_sensitive_(global_sensitive) {}
  68. ~SubstitutionList() = default;
  69. bool operator()(const FuncGraphPtr &func_graph, const OptimizerPtr &optimizer) const;
  70. private:
  71. bool ApplyIRToSubstitutions(const OptimizerPtr &optimizer, const FuncGraphPtr &func_graph) const;
  72. bool ApplySubstitutionToIR(const OptimizerPtr &optimizer, const FuncGraphPtr &func_graph,
  73. const SubstitutionPtr &sub) const;
  74. bool ApplySubstitutionsToIR(const OptimizerPtr &optimizer, const FuncGraphPtr &func_graph) const;
  75. void DisplayStatusOfSubstitution(const mindspore::HashMap<std::string, std::vector<bool>> &status,
  76. const OptimizerPtr &optimizer, size_t space) const;
  77. std::vector<SubstitutionPtr> list_;
  78. // a flag to mark this list of Substitution can only be executed only once
  79. bool is_once_{false};
  80. bool global_sensitive_{false};
  81. };
  82. } // namespace opt
  83. } // namespace mindspore
  84. #endif // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_OPT_H_