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.

symbol_resolver.h 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * Copyright 2020 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_IRPASS_SYMBOL_RESOLVER_H_
  17. #define MINDSPORE_CCSRC_OPTIMIZER_IRPASS_SYMBOL_RESOLVER_H_
  18. #include <string>
  19. #include <memory>
  20. #include "optimizer/optimizer.h"
  21. #include "optimizer/irpass.h"
  22. #include "ir/visitor.h"
  23. #include "operator/ops.h"
  24. #include "pipeline/parse/data_converter.h"
  25. #include "pipeline/parse/python_adapter.h"
  26. namespace mindspore {
  27. namespace opt {
  28. namespace irpass {
  29. // {prim::kPrimResolve, Ns, Sym}
  30. class ResolverResolve : public AnfVisitor {
  31. public:
  32. AnfNodePtr operator()(const OptimizerPtr &optimizer, const AnfNodePtr &node) override {
  33. Reset();
  34. AnfVisitor::Match(prim::kPrimResolve, {IsVNode, IsVNode})(node);
  35. if (sym_ != nullptr) {
  36. return parse::ResolveSymbol(optimizer->manager(), ns_, sym_, node);
  37. }
  38. return nullptr;
  39. }
  40. void Visit(const ValueNodePtr &vnode) override {
  41. if (IsValueNode<parse::NameSpace>(vnode)) {
  42. ns_ = GetValueNode<parse::NameSpacePtr>(vnode);
  43. } else if (ns_ != nullptr && IsValueNode<parse::Symbol>(vnode)) {
  44. sym_ = GetValueNode<parse::SymbolPtr>(vnode);
  45. }
  46. }
  47. void Reset() {
  48. ns_ = nullptr;
  49. sym_ = nullptr;
  50. }
  51. private:
  52. parse::NameSpacePtr ns_{nullptr};
  53. parse::SymbolPtr sym_{nullptr};
  54. };
  55. // {prim::kPrimGetAttr, Ns, Str}
  56. class ResolverGetattr : public AnfVisitor {
  57. public:
  58. AnfNodePtr operator()(const OptimizerPtr &optimizer, const AnfNodePtr &node) override {
  59. Reset();
  60. AnfVisitor::Match(prim::kPrimGetAttr, {IsVNode, IsVNode})(node);
  61. if (sym_ != nullptr) {
  62. return parse::ResolveSymbol(optimizer->manager(), ns_, sym_, node);
  63. }
  64. return nullptr;
  65. }
  66. void Visit(const AnfNodePtr &node) override {
  67. if (IsValueNode<parse::NameSpace>(node)) {
  68. ns_ = GetValueNode<parse::NameSpacePtr>(node);
  69. } else if (ns_ != nullptr && IsValueNode<StringImm>(node)) {
  70. auto str = GetValue<std::string>(GetValueNode(node));
  71. sym_ = std::make_shared<parse::Symbol>(str);
  72. }
  73. }
  74. void Reset() {
  75. ns_ = nullptr;
  76. sym_ = nullptr;
  77. }
  78. private:
  79. parse::NameSpacePtr ns_{nullptr};
  80. parse::SymbolPtr sym_{nullptr};
  81. };
  82. } // namespace irpass
  83. } // namespace opt
  84. } // namespace mindspore
  85. #endif // MINDSPORE_CCSRC_OPTIMIZER_IRPASS_SYMBOL_RESOLVER_H_