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

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