• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2023 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 #include "backend/common/pass/convert_list_to_tuple.h"
17 #include <map>
18 #include <utility>
19 #include <algorithm>
20 #include "include/common/utils/anfalgo.h"
21 #include "include/backend/anf_runtime_algorithm.h"
22 
23 namespace mindspore {
24 namespace opt {
25 static const size_t kMaxRecursiveDepth = 6;
26 
Run(const FuncGraphPtr & graph)27 bool ConvertListToTuple::Run(const FuncGraphPtr &graph) {
28   MS_EXCEPTION_IF_NULL(graph);
29   std::vector<AnfNodePtr> node_list = TopoSort(graph->get_return());
30   for (auto node : node_list) {
31     MS_EXCEPTION_IF_NULL(node);
32     // List abstract --> tuple abstract.
33     auto new_abs = ConvertSequenceAbsToTupleAbs(node->abstract());
34     if (new_abs != nullptr) {
35       node->set_abstract(new_abs);
36       if (node->isa<CNode>()) {
37         common::AnfAlgo::SetNodeAttr(kAttrAbstractAdaptationProcessed, MakeValue(true), node);
38       }
39       MS_LOG(INFO) << "Convert sequence abstract to tuple abstract for op:" << node->fullname_with_scope()
40                    << ",debug name:" << node->DebugString();
41     }
42   }
43   auto manager = graph->manager();
44   MS_EXCEPTION_IF_NULL(manager);
45   for (auto node : graph->parameters()) {
46     MS_EXCEPTION_IF_NULL(node);
47     // Convert unused list parameter to tuple.
48     if (manager->node_users().find(node) != manager->node_users().end()) {
49       continue;
50     }
51     auto new_abs = ConvertSequenceAbsToTupleAbs(node->abstract());
52     if (new_abs != nullptr) {
53       node->set_abstract(new_abs);
54       MS_LOG(INFO) << "Convert sequence abstract to tuple abstract for op:" << node->fullname_with_scope()
55                    << ",debug name:" << node->DebugString();
56     }
57   }
58   return true;
59 }
60 
61 // AbstractSequence --> AbstractTuple.
ConvertSequenceAbsToTupleAbs(const AbstractBasePtr & abs,size_t depth) const62 AbstractBasePtr ConvertListToTuple::ConvertSequenceAbsToTupleAbs(const AbstractBasePtr &abs, size_t depth) const {
63   if (abs == nullptr || !abs->isa<abstract::AbstractSequence>()) {
64     return nullptr;
65   }
66 
67   if (depth > kMaxRecursiveDepth) {
68     MS_LOG(EXCEPTION) << "List nesting is not allowed more than " << kMaxRecursiveDepth << " levels.";
69   }
70 
71   auto abs_seq = abs->cast<abstract::AbstractSequencePtr>();
72   MS_EXCEPTION_IF_NULL(abs_seq);
73   // Dynamic length sequence convert by the dynamic abs.
74   if (abs_seq->dynamic_len() && abs_seq->isa<abstract::AbstractList>()) {
75     auto converted_dynamic_abs_tuple =
76       std::make_shared<abstract::AbstractTuple>(abs_seq->elements(), abs_seq->sequence_nodes());
77     converted_dynamic_abs_tuple->set_dynamic_len(true);
78     converted_dynamic_abs_tuple->set_dynamic_len_element_abs(abs_seq->dynamic_len_element_abs());
79     return converted_dynamic_abs_tuple;
80   }
81   const auto &seq_elements = abs_seq->elements();
82   // First we check if elements should be converted,
83   // changed_elements maps old element to new element.
84   mindspore::HashMap<AbstractBasePtr, AbstractBasePtr> changed_elements;
85   for (const auto &element : seq_elements) {
86     auto new_element = ConvertSequenceAbsToTupleAbs(element, depth + 1);
87     if (new_element != nullptr) {
88       (void)changed_elements.emplace(element, new_element);
89     }
90   }
91   if (changed_elements.empty()) {
92     if (abs->isa<abstract::AbstractTuple>()) {
93       // If no elements changed and it is an AbstractTuple, do not convert.
94       return nullptr;
95     }
96     // If no elements changed but it is not an AbstractTuple, convert it by copy elements.
97     return std::make_shared<abstract::AbstractTuple>(seq_elements);
98   }
99   // Always make new AbstractTuple when elements changed.
100   std::vector<AbstractBasePtr> elements;
101   elements.reserve(seq_elements.size());
102   for (const auto &element : seq_elements) {
103     auto iter = changed_elements.find(element);
104     if (iter != changed_elements.end()) {
105       (void)elements.emplace_back(iter->second);
106     } else {
107       (void)elements.emplace_back(element);
108     }
109   }
110   return std::make_shared<abstract::AbstractTuple>(std::move(elements));
111 }
112 }  // namespace opt
113 }  // namespace mindspore
114