• 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_PARAM_REPLACE_H_
18 #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_PARAM_REPLACE_H_
19 
20 #include <memory>
21 
22 #include "frontend/optimizer/optimizer.h"
23 #include "frontend/optimizer/irpass.h"
24 #include "frontend/optimizer/anf_visitor.h"
25 #include "frontend/operator/ops.h"
26 #include "pipeline/jit/ps/parse/parse.h"
27 
28 namespace mindspore {
29 namespace opt {
30 namespace irpass {
31 class ReplaceOldParam : public AnfVisitor {
32  public:
operator()33   AnfNodePtr operator()(const OptimizerPtr &optimizer, const AnfNodePtr &node) override {
34     if (!IsParam(node)) {
35       return nullptr;
36     }
37     auto resource = std::dynamic_pointer_cast<pipeline::Resource>(optimizer->resource());
38     MS_EXCEPTION_IF_NULL(resource);
39 
40     auto top_graph = resource->func_graph();  // parse::Parser::GetTopFuncGraph();
41     MS_EXCEPTION_IF_NULL(top_graph);
42 
43     auto param_node = node->cast<ParameterPtr>();
44     if (param_node == nullptr || !param_node->has_default() || node->func_graph() == top_graph) {
45       return nullptr;
46     }
47     auto para_name = param_node->name();
48     for (const auto &tnode : top_graph->parameters()) {
49       auto para = tnode->cast<ParameterPtr>();
50       if (para != nullptr && para->name() == para_name) {
51         return para;
52       }
53     }
54     return nullptr;
55   }
56 };
57 }  // namespace irpass
58 }  // namespace opt
59 }  // namespace mindspore
60 #endif  // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_PARAM_REPLACE_H_
61