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