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.

backend_common_test.cc 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * Copyright 2019 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. #include "common/backend_common_test.h"
  17. #include <vector>
  18. #include <string>
  19. #include <memory>
  20. #include "utils/log_adapter.h"
  21. #include "frontend/operator/ops.h"
  22. #include "debug/anf_ir_dump.h"
  23. #include "backend/session/ascend_session.h"
  24. #include "pipeline/jit/resource.h"
  25. #include "pipeline/jit/action.h"
  26. #include "ir/anf.h"
  27. #include "ir/manager.h"
  28. namespace mindspore {
  29. namespace {
  30. std::vector<AnfNodePtr> GetCNodeList(const FuncGraphPtr &func_graph) {
  31. std::vector<AnfNodePtr> nodes = TopoSort(func_graph->get_return());
  32. std::vector<AnfNodePtr> lst;
  33. for (auto &node : nodes) {
  34. MS_LOG(INFO) << "nodes: " << node->DebugString(10);
  35. if (node->isa<CNode>() && IsValueNode<Primitive>(node->cast<CNodePtr>()->input(0)) &&
  36. !IsPrimitiveCNode(node, prim::kPrimReturn)) {
  37. MS_LOG(INFO) << "push in anf_node list: " << node->DebugString(10);
  38. lst.push_back(node);
  39. }
  40. }
  41. return lst;
  42. }
  43. } // namespace
  44. bool BackendCommon::CheckEqualGraph(const FuncGraphPtr &a, const FuncGraphPtr &b) {
  45. FuncGraphPairMapEquiv equiv_graph_;
  46. NodeMapEquiv equiv_node_;
  47. return Isomorphic(a, b, &equiv_graph_, &equiv_node_);
  48. }
  49. std::shared_ptr<session::KernelGraph> BackendCommon::GetKernelGraph(const FuncGraphPtr &func_graph,
  50. const AbstractBasePtrList &args_spec_list,
  51. bool need_infer) {
  52. FuncGraphPtr inferred_graph = func_graph;
  53. if (need_infer) {
  54. inferred_graph = GetFuncGraph(func_graph, args_spec_list);
  55. }
  56. AnfNodePtrList applies = GetCNodeList(inferred_graph);
  57. AnfNodePtrList ins = inferred_graph->parameters();
  58. AnfNodePtrList outs = {inferred_graph->get_return()->input(1)};
  59. auto session = std::make_shared<session::AscendSession>();
  60. session->Init(0);
  61. auto kernel_graph = session->ConstructKernelGraph(applies, outs);
  62. kernel_graph->SetExecOrderByDefault();
  63. return kernel_graph;
  64. }
  65. FuncGraphPtr BackendCommon::GetFuncGraph(const FuncGraphPtr &func_graph, const AbstractBasePtrList &args_spec_list) {
  66. if (func_graph->manager() == nullptr) {
  67. std::vector<FuncGraphPtr> graphs{func_graph};
  68. FuncGraphManagerPtr manager = std::make_shared<FuncGraphManager>(graphs);
  69. manager->AddFuncGraph(func_graph);
  70. }
  71. // Renormalize func_graph to infer and set shape and type information.
  72. pipeline::ResourcePtr resource_ = std::make_shared<pipeline::Resource>();
  73. return pipeline::Renormalize(resource_, func_graph, args_spec_list);
  74. }
  75. } // namespace mindspore