• 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 
17 #ifndef MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_ROW_TENSOR_ELIMINATE_H_
18 #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_ROW_TENSOR_ELIMINATE_H_
19 
20 #include <vector>
21 #include <algorithm>
22 
23 #include "frontend/operator/ops.h"
24 #include "frontend/optimizer/anf_visitor.h"
25 #include "frontend/optimizer/irpass.h"
26 #include "frontend/optimizer/optimizer.h"
27 
28 namespace mindspore {
29 namespace opt {
30 namespace irpass {
31 // {prim::kPrimRowTensorGetIndices, {prim::kPrimMakeRowTensor, Xs}}
32 // {prim::kPrimRowTensorGetValues, {prim::kPrimMakeRowTensor, Xs}}
33 // {prim::kPrimRowTensorGetDenseShape, {prim::kPrimMakeRowTensor, Xs}}
34 class RowTensorEliminater : public OptimizerCaller {
35  public:
operator()36   AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
37     PatternNode x, y, z;
38     auto slices = PPrimitive(prim::kPrimMakeRowTensor, x, y, z).MinExtraNodes(0);
39     MATCH_REPLACE(node, PPrimitive(prim::kPrimRowTensorGetIndices, slices), x);
40     MATCH_REPLACE(node, PPrimitive(prim::kPrimRowTensorGetValues, slices), y);
41     MATCH_REPLACE(node, PPrimitive(prim::kPrimRowTensorGetDenseShape, slices), z);
42     return nullptr;
43   }
44 };
45 
46 // {prim::kPrimRowTensorAdd, rowtensor, zeros_like(x)} -> rowtensor
47 class RowTensorAddZerosLike : public AnfVisitor {
operator()48   AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
49     PatternNode x, y;
50     auto zeros_like = PPrimitive(prim::kPrimZerosLike, y);
51     MATCH_REPLACE(node, PPrimitive(prim::kPrimRowTensorAdd, x, zeros_like), x);
52     return nullptr;
53   }
54 };
55 }  // namespace irpass
56 }  // namespace opt
57 }  // namespace mindspore
58 #endif  // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_ROW_TENSOR_ELIMINATE_H_
59