1 /** 2 * Copyright 2024 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 17 #ifndef MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_SEQUENCE_TO_SEQUENCE_ELIMINATE_H_ 18 #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_SEQUENCE_TO_SEQUENCE_ELIMINATE_H_ 19 20 #include <memory> 21 #include <vector> 22 23 #include "frontend/optimizer/optimizer_caller.h" 24 #include "mindspore/core/ops/structure_ops.h" 25 #include "mindspore/core/ops/sequence_ops.h" 26 #include "mindspore/core/ops/framework_ops.h" 27 #include "frontend/optimizer/anf_visitor.h" 28 #include "frontend/operator/ops.h" 29 #include "frontend/optimizer/irpass.h" 30 #include "frontend/optimizer/optimizer.h" 31 #include "include/common/utils/utils.h" 32 33 namespace mindspore { 34 namespace opt { 35 namespace irpass { 36 // {prim::kPrimListToTuple, data} => {prim::kPrimMakeTuple, {prim::kPrimListGetItem, data, 0}, ...} 37 class ListToTupleEliminator : public AnfVisitor { 38 public: operator()39 AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override { 40 if (!IsPrimitiveCNode(node, prim::kPrimListToTuple)) { 41 return nullptr; 42 } 43 auto fg = node->func_graph(); 44 if (fg != nullptr) { 45 auto real_node = node->cast<CNodePtr>()->input(1); 46 MS_EXCEPTION_IF_NULL(real_node); 47 std::vector<AnfNodePtr> args_{NewValueNode(prim::kPrimMakeTuple)}; 48 MS_EXCEPTION_IF_NULL(real_node->abstract()); 49 auto input_abs = real_node->abstract()->cast<abstract::AbstractListPtr>(); 50 MS_EXCEPTION_IF_NULL(input_abs); 51 if (!input_abs->dynamic_len()) { 52 for (size_t i = 0; i < input_abs->size(); ++i) { 53 auto item = fg->NewCNode({NewValueNode(prim::kPrimListGetItem), real_node, NewValueNode(SizeToLong(i))}); 54 item->set_abstract(real_node->abstract()); 55 args_.emplace_back(item); 56 } 57 auto new_node = fg->NewCNode(args_); 58 new_node->set_abstract(node->abstract()); 59 return new_node; 60 } 61 } 62 return nullptr; 63 } 64 }; 65 66 // {prim::kPrimTupleToList, data} => {prim::kPrimMakeList, {prim::kPrimTupleGetItem, data, 0}, ...} 67 class TupleToListEliminator : public AnfVisitor { 68 public: operator()69 AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override { 70 if (!IsPrimitiveCNode(node, prim::kPrimTupleToList)) { 71 return nullptr; 72 } 73 auto fg = node->func_graph(); 74 if (fg != nullptr) { 75 auto real_node = node->cast<CNodePtr>()->input(1); 76 MS_EXCEPTION_IF_NULL(real_node); 77 std::vector<AnfNodePtr> args_{NewValueNode(prim::kPrimMakeList)}; 78 MS_EXCEPTION_IF_NULL(real_node->abstract()); 79 auto input_abs = real_node->abstract()->cast<abstract::AbstractTuplePtr>(); 80 MS_EXCEPTION_IF_NULL(input_abs); 81 if (!input_abs->dynamic_len()) { 82 for (size_t i = 0; i < input_abs->size(); ++i) { 83 auto item = fg->NewCNode({NewValueNode(prim::kPrimTupleGetItem), real_node, NewValueNode(SizeToLong(i))}); 84 item->set_abstract(real_node->abstract()); 85 args_.emplace_back(item); 86 } 87 auto new_node = fg->NewCNode(args_); 88 new_node->set_abstract(node->abstract()); 89 return new_node; 90 } 91 } 92 return nullptr; 93 } 94 }; 95 } // namespace irpass 96 } // namespace opt 97 } // namespace mindspore 98 #endif // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_SEQUENCE_TO_SEQUENCE_ELIMINATE_H_ 99