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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_OPTIMIZER_OPT_H_
  17. #define MINDSPORE_CCSRC_OPTIMIZER_OPT_H_
  18. #include <vector>
  19. #include <string>
  20. #include <memory>
  21. #include "ir/anf.h"
  22. #include "ir/func_graph.h"
  23. #include "operator/ops.h"
  24. namespace mindspore {
  25. /* namespace to support opt */
  26. namespace opt {
  27. class Optimizer;
  28. using OptimizerPtr = std::shared_ptr<Optimizer>;
  29. using OptimizerWeakPtr = std::weak_ptr<Optimizer>;
  30. using PredicateFuncType = std::function<bool(const AnfNodePtr &)>;
  31. using TransformFuncType = std::function<AnfNodePtr(const OptimizerPtr &, const AnfNodePtr &)>;
  32. // Define the interaction mode between an Optimize pass and Renormalize pass
  33. // FORCE_RENORM: if the pass modified the graph then the next Renormalize will be executed
  34. // CHECK_RENORM: check if the new node is un-typed to decide if the next Renormalize will be executted
  35. enum RenormAction : int { FORCE_RENORM = 0, CHECK_RENORM };
  36. class Substitution {
  37. public:
  38. TransformFuncType transform_{nullptr};
  39. std::string name_;
  40. PredicateFuncType predicate_{nullptr};
  41. // an enum to mark this Substitution relation to renormalize pass
  42. RenormAction renorm_action_;
  43. Substitution(const TransformFuncType &transform, const std::string &name, const PredicateFuncType &predicate,
  44. const RenormAction &renorm_action)
  45. : transform_(transform), name_(name), predicate_(predicate), renorm_action_(renorm_action) {}
  46. ~Substitution() = default;
  47. AnfNodePtr operator()(const OptimizerPtr &optimizer, const AnfNodePtr &node) const;
  48. };
  49. using SubstitutionPtr = std::shared_ptr<Substitution>;
  50. SubstitutionPtr MakeSubstitution(const TransformFuncType &transform, const std::string &name, const PrimitivePtr &prim,
  51. const RenormAction &action_renorm = CHECK_RENORM);
  52. SubstitutionPtr MakeSubstitution(const TransformFuncType &transform, const std::string &name,
  53. const std::vector<PrimitivePtr> &prims,
  54. const RenormAction &action_renorm = CHECK_RENORM);
  55. SubstitutionPtr MakeSubstitution(const TransformFuncType &transform, const std::string &name,
  56. const PredicateFuncType &predicate, const RenormAction &action_renorm = CHECK_RENORM);
  57. class SubstitutionList {
  58. public:
  59. explicit SubstitutionList(const std::vector<SubstitutionPtr> &patterns, bool is_once = false)
  60. : list_(patterns), is_once_(is_once) {}
  61. ~SubstitutionList() = default;
  62. bool operator()(const FuncGraphPtr &func_graph, const OptimizerPtr &optimizer) const;
  63. private:
  64. bool ApplyTransform(const OptimizerPtr &optimizer, const AnfNodePtr &node, const SubstitutionPtr &transform) const;
  65. std::vector<SubstitutionPtr> list_;
  66. // a flag to mark this list of Substitution can only be executed only once
  67. bool is_once_;
  68. };
  69. } // namespace opt
  70. } // namespace mindspore
  71. #endif // MINDSPORE_CCSRC_OPTIMIZER_OPT_H_