• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_BACKEND_OPTIMIZER_GRAPH_KERNEL_ELIMINATE_REDUNDANT_OUTPUT_H_
17 #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_ELIMINATE_REDUNDANT_OUTPUT_H_
18 
19 #include "backend/optimizer/common/optimizer.h"
20 
21 namespace mindspore {
22 namespace opt {
23 /* Eliminate the output without external user
24  *   %1 = call @graph_kernel(p1, p2)
25  *   %2 = tuple_getitem(%1, 0)   // the getitem(1) does not exist.
26  *   %3 = op(%2)
27  *   graph_kernel:
28  *      %1 = TensorAdd(p1, p2)
29  *      %2 = Sub(p1, p2)
30  *      return make_tuple(%1, %2)
31  *   --->
32  *   %1 = call @graph_kernel(p1, p2)
33  *   %3 = op(%1)                 // if only one output remains, the getitem is not used
34  *   graph_kernel:
35  *      %1 = TensorAdd(p1, p2)
36  *      return %1                // the Sub was eliminated
37  */
38 class EliminateHangingOutput : public Pass {
39  public:
40   bool Run(const FuncGraphPtr &func_graph) override;
41 
42  private:
43   // update the GetItem(node, i) to GetItem(node, i - offset)
44   void UpdateGetitemIndex(const AnfNodePtr &getitem, size_t offset) const;
45   AnfNodePtr ReplaceMakeTuple(const AnfNodePtr &node, const AnfNodePtrList &getitems) const;
46 };
47 
48 // Remove the output without user or with virtual user (like UpdateState)
49 class EliminateRedundantOutput : public Pass {
50  public:
EliminateRedundantOutput()51   EliminateRedundantOutput() : Pass("eliminate_redundant_output") {}
52   ~EliminateRedundantOutput() override = default;
53   bool Run(const FuncGraphPtr &func_graph) override;
54 };
55 
56 bool IsSideEffectNode(const AnfNodePtr &node);
57 AnfNodePtrList FindGraphKernelsWithMultiOutput(const FuncGraphPtr &func_graph);
58 
59 /**
60  * @brief Get the GraphKernel's user getitems
61  *
62  * @param mng FuncGraphManagerPtr for the main func_graph
63  * @param node The cnode that indicates the GraphKernel
64  * @param getitem_list The user getitem list.
65  * @param merge_repeated_getitem If true, getitems with same index will be merged,
66  *                               otherwise, only one getitem will be outputted.
67  * @return If the graph was changed, returns true, otherwise returns false.
68  */
69 bool GetGraphKernelGetitemList(const FuncGraphManagerPtr &mng, const AnfNodePtr &node, AnfNodePtrList *getitem_list,
70                                bool merge_repeated_getitem = false);
71 }  // namespace opt
72 }  // namespace mindspore
73 #endif  // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_ELIMINATE_REDUNDANT_OUTPUT_H_
74