• 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 "frontend/operator/ops.h"
21 #include "mindspore/core/ops/sparse_tensor_ops.h"
22 #include "mindspore/core/ops/array_ops.h"
23 #include "frontend/optimizer/anf_visitor.h"
24 #include "frontend/optimizer/irpass.h"
25 #include "frontend/optimizer/optimizer.h"
26 #include "ir/pattern_matcher.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;
38     PatternNode y;
39     PatternNode z;
40     auto slices = PPrimitive(prim::kPrimMakeRowTensor, x, y, z).MinExtraNodes(0);
41     MATCH_REPLACE(node, PPrimitive(prim::kPrimRowTensorGetIndices, slices), x);
42     MATCH_REPLACE(node, PPrimitive(prim::kPrimRowTensorGetValues, slices), y);
43     MATCH_REPLACE(node, PPrimitive(prim::kPrimRowTensorGetDenseShape, slices), z);
44     return nullptr;
45   }
46 };
47 
48 // {prim::kPrimRowTensorAdd, rowtensor, zeros_like(x)} -> rowtensor
49 class RowTensorAddZerosLike : public AnfVisitor {
50  public:
operator()51   AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
52     PatternNode x;
53     PatternNode y;
54     auto zeros_like = PPrimitive(prim::kPrimZerosLike, y);
55     MATCH_REPLACE(node, PPrimitive(prim::kPrimRowTensorAdd, x, zeros_like), x);
56     return nullptr;
57   }
58 };
59 }  // namespace irpass
60 }  // namespace opt
61 }  // namespace mindspore
62 #endif  // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_ROW_TENSOR_ELIMINATE_H_
63