• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2023 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_LITE_TOOLS_OPTIMIZER_GRAPH_SCALAR_OP_PASS_H_
18 #define MINDSPORE_LITE_TOOLS_OPTIMIZER_GRAPH_SCALAR_OP_PASS_H_
19 #include <set>
20 #include "include/registry/converter_context.h"
21 #include "include/backend/optimizer/pass.h"
22 #include "tools/optimizer/common/gllo_utils.h"
23 
24 namespace mindspore::opt {
25 class ScalarOpPass : public Pass {
26  public:
ScalarOpPass()27   ScalarOpPass() : Pass("scalar_op_pass") {}
28   ~ScalarOpPass() override = default;
29   bool Run(const FuncGraphPtr &func_graph) override;
30 
31  private:
32   ValueNodePtr GenerateScalarValueTensor(const FuncGraphPtr &func_graph, const AnfNodePtr &anf_node, int input_index);
33   ValueNodePtr GenerateScalarValueTuple(const FuncGraphPtr &func_graph, int64_t value);
34   ValueNodePtr GenerateScalarValue(const FuncGraphPtr &func_graph, int64_t value);
35   CNodePtr GenerateScalarToTensor(const FuncGraphPtr &func_graph, const AnfNodePtr &anf_node, int input_index);
36   CNodePtr GenerateTensorToScalar(const FuncGraphPtr &func_graph, const AnfNodePtr &anf_node,
37                                   bool is_curr_node = false);
38   CNodePtr GenerateTensorShape(const FuncGraphPtr &func_graph, const AnfNodePtr &anf_node);
39   CNodePtr GenerateStridedSlice(const FuncGraphPtr &func_graph, const AnfNodePtr &shape_node,
40                                 const AnfNodePtr &tuple_get_node, const FuncGraphManagerPtr &manager);
41   STATUS ReplaceScalarOp(const FuncGraphPtr &func_graph, const AnfNodePtr &anf_node, const FuncGraphManagerPtr &manager,
42                          const PrimitivePtr &replace_op_prim);
43   STATUS ReplaceMakeTuple(const FuncGraphPtr &func_graph, const AnfNodePtr &anf_node,
44                           const FuncGraphManagerPtr &manager);
45   STATUS ReplaceShapeTupleGet(const FuncGraphPtr &func_graph, const AnfNodePtr &anf_node,
46                               const FuncGraphManagerPtr &manager);
47   STATUS RemoveTensorToScalar(const FuncGraphPtr &func_graph, const AnfNodePtr &anf_node,
48                               const FuncGraphManagerPtr &manager);
49   size_t GetInputNodeIndex(const AnfNodePtr &input, const CNodePtr &user_node);
50   STATUS RunScalarOpPass(const FuncGraphPtr &func_graph, const FuncGraphManagerPtr &manager);
51   STATUS RunMakeTuplePass(const FuncGraphPtr &func_graph, const FuncGraphManagerPtr &manager);
52   STATUS RunShapeTupleGetPass(const FuncGraphPtr &func_graph, const FuncGraphManagerPtr &manager);
53   STATUS RunRemoveTensorToScalarPass(const FuncGraphPtr &func_graph, const FuncGraphManagerPtr &manager);
54   STATUS RunArithmeticCheckPass(const FuncGraphPtr &func_graph, const FuncGraphManagerPtr &manager);
55 };
56 }  // namespace mindspore::opt
57 #endif  // MINDSPORE_LITE_TOOLS_OPTIMIZER_GRAPH_SCALAR_OP_PASS_H_
58