• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-2021 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_SWITCH_LAYER_DEFER_INLINE_H_
18 #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_SWITCH_LAYER_DEFER_INLINE_H_
19 
20 #include <vector>
21 #include <algorithm>
22 
23 #include "frontend/optimizer/irpass.h"
24 #include "frontend/optimizer/optimizer.h"
25 #include "frontend/optimizer/anf_visitor.h"
26 #include "frontend/operator/ops.h"
27 
28 namespace mindspore {
29 namespace opt {
30 namespace irpass {
31 // {prim::kPrimSwitch, cond, true_branch, false_branch}
32 class SwitchDeferInline : public AnfVisitor {
33  public:
operator()34   AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
35     auto cnode = node->cast<CNodePtr>();
36     auto true_abstract = dyn_cast<abstract::FuncGraphAbstractClosure>(cnode->input(2)->abstract());
37     if (true_abstract != nullptr) {
38       *(true_abstract->func_graph()->switch_input()) = true;
39     }
40     auto false_abstract = dyn_cast<abstract::FuncGraphAbstractClosure>(cnode->input(3)->abstract());
41     if (false_abstract != nullptr) {
42       *(false_abstract->func_graph()->switch_input()) = true;
43     }
44     return nullptr;
45   }
46 };
47 
48 // {prim::kPrimSwitchLayer, Index, layers}
49 class SwitchLayerDeferInline : public AnfVisitor {
50  public:
operator()51   AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
52     auto cnode = node->cast<CNodePtr>();
53     auto tuple = dyn_cast<abstract::AbstractTuple>(cnode->input(2)->abstract());
54     if (tuple == nullptr) {
55       return nullptr;
56     }
57     for (auto elem : tuple->elements()) {
58       auto abstract = dyn_cast<abstract::FuncGraphAbstractClosure>(elem);
59       if (abstract != nullptr) {
60         *(abstract->func_graph()->switch_layer_input()) = true;
61       }
62     }
63     return nullptr;
64   }
65 };
66 }  // namespace irpass
67 }  // namespace opt
68 }  // namespace mindspore
69 #endif  // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_SWITCH_LAYER_DEFER_INLINE_H_
70