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.

map.h 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_FRONTEND_OPERATOR_COMPOSITE_MAP_H_
  17. #define MINDSPORE_CCSRC_FRONTEND_OPERATOR_COMPOSITE_MAP_H_
  18. #include <memory>
  19. #include <set>
  20. #include <utility>
  21. #include <vector>
  22. #include "ir/dtype.h"
  23. #include "ir/meta_func_graph.h"
  24. #include "frontend/operator/composite/multitype_funcgraph.h"
  25. namespace mindspore {
  26. // namespace to support composite operators definition
  27. namespace prim {
  28. using ArgsPairList = std::vector<std::pair<AnfNodePtr, TypePtr>>;
  29. class Map : public MetaFuncGraph {
  30. public:
  31. explicit Map(bool reverse = false, const std::shared_ptr<MultitypeFuncGraph> &fn_leaf = nullptr)
  32. : MetaFuncGraph("map"),
  33. fn_leaf_(fn_leaf),
  34. reverse_(reverse),
  35. broadcast_(false),
  36. nonleaf_({kObjectTypeList, kObjectTypeTuple, kObjectTypeClass}) {
  37. Init();
  38. }
  39. Map(const Map &map)
  40. : MetaFuncGraph("map"),
  41. fn_leaf_(map.fn_leaf_),
  42. reverse_(map.reverse_),
  43. broadcast_(map.broadcast_),
  44. nonleaf_(map.nonleaf_) {
  45. Init();
  46. }
  47. Map &operator=(const Map &map) {
  48. if (this != &map) {
  49. fn_leaf_ = map.fn_leaf_;
  50. reverse_ = map.reverse_;
  51. broadcast_ = map.broadcast_;
  52. nonleaf_ = map.nonleaf_;
  53. if (fn_leaf_) {
  54. name_ = "map[" + fn_leaf_->name() + "]";
  55. }
  56. }
  57. return *this;
  58. }
  59. ~Map() override = default;
  60. MS_DECLARE_PARENT(Map, MetaFuncGraph)
  61. abstract::AbstractBasePtrList NormalizeArgs(const abstract::AbstractBasePtrList &args_spec_list) const override;
  62. FuncGraphPtr GenerateFromTypes(const TypePtrList &args_spec_list) override;
  63. MetaFuncGraphPtr GetFnLeaf() { return fn_leaf_; }
  64. private:
  65. FuncGraphPtr GenerateLeafFunc(const size_t &args_size);
  66. AnfNodePtr FullMakeLeaf(const FuncGraphPtr &func_graph, const AnfNodePtr &fn_arg, const AnfNodePtrList &args);
  67. AnfNodePtr FullMakeList(const std::shared_ptr<List> &type, const FuncGraphPtr &func_graph, const AnfNodePtr &fn_arg,
  68. const ArgsPairList &arg_pairs);
  69. AnfNodePtr FullMakeTuple(const std::shared_ptr<Tuple> &type, const FuncGraphPtr &func_graph, const AnfNodePtr &fn_arg,
  70. const ArgsPairList &arg_pairs);
  71. AnfNodePtr FullMakeClass(const std::shared_ptr<Class> &type, const FuncGraphPtr &func_graph, const AnfNodePtr &fn_arg,
  72. const ArgsPairList &arg_pairs);
  73. AnfNodePtr Make(const FuncGraphPtr &graph, const AnfNodePtr &fn_arg, const ArgsPairList &arg_pairs);
  74. void Init() {
  75. if (fn_leaf_ != nullptr) {
  76. name_ = "map[" + fn_leaf_->name() + "]";
  77. }
  78. signatures_ =
  79. // def map(func:read, *args:ref):
  80. std::vector<Signature>({{"func", SignatureEnumRW::kRWRead, SignatureEnumKind::kKindDefault},
  81. {"args", SignatureEnumRW::kRWRef, SignatureEnumKind::kKindVarPositional}});
  82. }
  83. MultitypeFuncGraphPtr fn_leaf_;
  84. bool reverse_;
  85. bool broadcast_;
  86. std::set<TypeId> nonleaf_;
  87. };
  88. using MapPtr = std::shared_ptr<Map>;
  89. class MapPy : public Map {
  90. public:
  91. explicit MapPy(bool reverse = false, const std::shared_ptr<MultitypeFuncGraph> &fn_leaf = nullptr)
  92. : Map(reverse, fn_leaf) {}
  93. ~MapPy() override = default;
  94. MS_DECLARE_PARENT(MapPy, Map)
  95. };
  96. using MapPyPtr = std::shared_ptr<MapPy>;
  97. } // namespace prim
  98. } // namespace mindspore
  99. #endif // MINDSPORE_CCSRC_FRONTEND_OPERATOR_COMPOSITE_MAP_H_