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.

visit.h 2.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 MINDSPORE_CCSRC_PRE_ACTIVATE_COMMON_VISIT_H_
  19. #define MINDSPORE_CCSRC_PRE_ACTIVATE_COMMON_VISIT_H_
  20. #include <unordered_map>
  21. #include <stdexcept>
  22. #include <list>
  23. #include <vector>
  24. #include <string>
  25. #include <memory>
  26. #include "ir/base.h"
  27. #include "utils/base_ref.h"
  28. // namespace to support utils definition
  29. namespace mindspore {
  30. using VisitFn = std::function<BaseRef(const BaseRef &)>;
  31. class Visitor {
  32. public:
  33. virtual void SetFn(VisitFn fn) = 0;
  34. virtual bool Visit(const BaseRef &e, BaseRef *out) const = 0;
  35. virtual bool Visit(const VectorRef &e, BaseRef *out) const = 0;
  36. virtual ~Visitor() = default;
  37. };
  38. class DefaultVisitor : public Visitor {
  39. public:
  40. DefaultVisitor() : fn_(nullptr) {}
  41. ~DefaultVisitor() override = default;
  42. void SetFn(VisitFn fn) override { fn_ = fn; };
  43. bool Visit(const VectorRef &e, BaseRef *out) const override;
  44. bool Visit(const BaseRef &e, BaseRef *out) const override;
  45. void Visit(const AnfNodePtr &node, const VisitFn &fn, AnfNodePtr *output) const;
  46. void Visit(const CNodePtr &cnode, const VisitFn &fn, AnfNodePtr *output) const;
  47. void Visit(const ValueNodePtr &vnode, const VisitFn &fn, AnfNodePtr *output) const;
  48. VisitFn fn_;
  49. };
  50. std::shared_ptr<VectorRef> ExpandList(const std::vector<BaseRef> &list);
  51. bool CheckIfNeedExpand(const std::vector<BaseRef> &list);
  52. } // namespace mindspore
  53. #endif // MINDSPORE_CCSRC_PRE_ACTIVATE_COMMON_VISIT_H_