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.

graph_transform.h 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_OPTIMIZER_GRAPH_TRANSFORM_H
  17. #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_GRAPH_TRANSFORM_H
  18. #include <unordered_map>
  19. #include <string>
  20. #include <vector>
  21. #include <algorithm>
  22. #include <memory>
  23. #include "frontend/optimizer/optimizer.h"
  24. namespace mindspore {
  25. namespace opt {
  26. bool CNodeHasTupleInput(const CNodePtr &cnode);
  27. bool FuncGraphHasTupleInput(const FuncGraphPtr &fg);
  28. std::vector<AnfNodePtr> TransformTupleArgument(const FuncGraphPtr &fg, const AnfNodePtr &node,
  29. const abstract::AbstractTuplePtr &abs);
  30. AnfNodePtr TransformCallGraph(const FuncGraphPtr &trans_fg, const CNodePtr &cnode);
  31. AnfNodePtr TransformPartial(const FuncGraphPtr &trans_fg, const CNodePtr &cnode);
  32. AnfNodePtr TransformSwitchCall(const AnfNodePtr &swtich_node, const CNodePtr &cnode);
  33. class GraphTupleParamTransform {
  34. public:
  35. GraphTupleParamTransform() : cache_() {}
  36. ~GraphTupleParamTransform() { cache_.clear(); }
  37. FuncGraphPtr operator()(const FuncGraphPtr &fg, const FuncGraphManagerPtr &mng) {
  38. if (cache_.find(fg) != cache_.end()) {
  39. return cache_[fg];
  40. }
  41. auto new_fg = TransformGraphParam(fg, mng);
  42. cache_[fg] = new_fg;
  43. return new_fg;
  44. }
  45. AnfNodePtr GenerateTupleParams(const abstract::AbstractTuplePtr &tuple_abs, const FuncGraphPtr &fg,
  46. std::vector<AnfNodePtr> *params) {
  47. std::vector<AnfNodePtr> inputs;
  48. inputs.push_back(NewValueNode(prim::kPrimMakeTuple));
  49. auto &elements = tuple_abs->elements();
  50. for (auto &item : elements) {
  51. if (item->isa<abstract::AbstractTuple>()) {
  52. inputs.push_back(GenerateTupleParams(item->cast<abstract::AbstractTuplePtr>(), fg, params));
  53. } else {
  54. auto p = std::make_shared<Parameter>(fg);
  55. p->set_abstract(item);
  56. params->push_back(p);
  57. inputs.push_back(params->back());
  58. }
  59. }
  60. auto node = fg->NewCNode(inputs);
  61. node->set_abstract(tuple_abs);
  62. return node;
  63. }
  64. FuncGraphPtr TransformGraphParam(const FuncGraphPtr &fg, const FuncGraphManagerPtr &mng) {
  65. Cloner cloner({fg}, false, false, false, std::make_shared<TraceCopy>(), std::make_shared<TraceCopy>());
  66. auto new_fg = cloner[fg];
  67. auto &params = new_fg->parameters();
  68. std::vector<AnfNodePtr> new_params;
  69. std::unordered_map<AnfNodePtr, AnfNodePtr> repl;
  70. for (auto &param : params) {
  71. auto abs = param->abstract();
  72. if (abs != nullptr && abs->isa<abstract::AbstractTuple>()) {
  73. auto tuple_abs = abs->cast<abstract::AbstractTuplePtr>();
  74. std::vector<AnfNodePtr> tuple_params;
  75. repl.emplace(param, GenerateTupleParams(tuple_abs, new_fg, &tuple_params));
  76. std::transform(tuple_params.begin(), tuple_params.end(), std::back_inserter(new_params),
  77. [](AnfNodePtr p) { return p; });
  78. } else {
  79. new_params.push_back(param);
  80. }
  81. }
  82. auto tmp_mng = mindspore::Manage(new_fg, false);
  83. auto tr = tmp_mng->Transact();
  84. for (auto &item : repl) {
  85. bool ret = tr.Replace(item.first, item.second);
  86. if (ret == false) {
  87. MS_LOG(ERROR) << "replace failed" << item.first->DebugString() << " with__" << item.second->DebugString(2);
  88. }
  89. }
  90. tr.SetParameters(new_fg, new_params);
  91. tr.Commit();
  92. mng->AddFuncGraph(new_fg);
  93. return new_fg;
  94. }
  95. std::unordered_map<FuncGraphPtr, FuncGraphPtr> cache_;
  96. };
  97. } // namespace opt
  98. } // namespace mindspore
  99. #endif // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_GRAPH_TRANSFORM_H