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.3 kB

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