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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_OPERATOR_COMPOSITE_MAP_H_
  17. #define MINDSPORE_CCSRC_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 "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(const std::shared_ptr<MultitypeFuncGraph> &fn_leaf = nullptr)
  32. : MetaFuncGraph("map"),
  33. fn_leaf_(fn_leaf),
  34. broadcast_(false),
  35. nonleaf_({kObjectTypeList, kObjectTypeTuple, kObjectTypeClass}) {
  36. Init();
  37. }
  38. Map(const Map &h) : MetaFuncGraph("map"), fn_leaf_(h.fn_leaf_), broadcast_(h.broadcast_), nonleaf_(h.nonleaf_) {
  39. Init();
  40. }
  41. Map &operator=(const Map &h) {
  42. if (this != &h) {
  43. fn_leaf_ = h.fn_leaf_;
  44. broadcast_ = h.broadcast_;
  45. nonleaf_ = h.nonleaf_;
  46. if (fn_leaf_) {
  47. name_ = "map[" + fn_leaf_->name() + "]";
  48. }
  49. }
  50. return *this;
  51. }
  52. ~Map() override = default;
  53. MS_DECLARE_PARENT(Map, MetaFuncGraph)
  54. abstract::AbstractBasePtrList NormalizeArgs(const abstract::AbstractBasePtrList &args_spec_list) const override;
  55. FuncGraphPtr GenerateFromTypes(const TypePtrList &args_spec_list) override;
  56. MetaFuncGraphPtr GetFnLeaf() { return fn_leaf_; }
  57. private:
  58. FuncGraphPtr GenerateLeafFunc(const size_t &args_size);
  59. AnfNodePtr FullMakeLeaf(const FuncGraphPtr &func_graph, const AnfNodePtr &fn_arg, const AnfNodePtrList &args);
  60. AnfNodePtr FullMakeList(const std::shared_ptr<List> &type, const FuncGraphPtr &func_graph, const AnfNodePtr &fn_arg,
  61. const ArgsPairList &arg_pairs);
  62. AnfNodePtr FullMakeTuple(const std::shared_ptr<Tuple> &type, const FuncGraphPtr &func_graph, const AnfNodePtr &fn_arg,
  63. const ArgsPairList &arg_pairs);
  64. AnfNodePtr FullMakeClass(const std::shared_ptr<Class> &type, const FuncGraphPtr &func_graph, const AnfNodePtr &fn_arg,
  65. const ArgsPairList &arg_pairs);
  66. AnfNodePtr Make(const FuncGraphPtr &graph, const AnfNodePtr &fn_arg, const ArgsPairList &arg_pairs);
  67. void Init() {
  68. if (fn_leaf_ != nullptr) {
  69. name_ = "map[" + fn_leaf_->name() + "]";
  70. }
  71. signatures_ =
  72. // def map(func:read, *args:ref):
  73. std::vector<Signature>({{"func", SignatureEnumRW::kRWRead, SignatureEnumKind::kKindDefault},
  74. {"args", SignatureEnumRW::kRWRef, SignatureEnumKind::kKindVarPositional}});
  75. }
  76. MultitypeFuncGraphPtr fn_leaf_;
  77. bool broadcast_;
  78. std::set<TypeId> nonleaf_;
  79. };
  80. using MapPtr = std::shared_ptr<Map>;
  81. class MapPy : public Map {
  82. public:
  83. explicit MapPy(const std::shared_ptr<MultitypeFuncGraph> &fn_leaf = nullptr) : Map(fn_leaf) {}
  84. ~MapPy() override = default;
  85. MS_DECLARE_PARENT(MapPy, Map)
  86. };
  87. using MapPyPtr = std::shared_ptr<MapPy>;
  88. } // namespace prim
  89. } // namespace mindspore
  90. #endif // MINDSPORE_CCSRC_OPERATOR_COMPOSITE_MAP_H_