You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

param_replace.h 2.0 kB

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