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.

gradient_eliminate.cc 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include "frontend/optimizer/irpass/gradient_eliminate.h"
  17. namespace mindspore {
  18. namespace opt {
  19. namespace irpass {
  20. namespace internal {
  21. AnfNodePtr ExpandJPrimitive(const ValueNodePtr &vnode, const pipeline::ResourceBasePtr &resource) {
  22. ScopeGuard scope_guard(vnode->scope());
  23. auto newg = ad::Kprim(vnode, resource);
  24. if (newg != nullptr) {
  25. return NewValueNode(newg);
  26. }
  27. // when find in J failed, try in Jmeta
  28. auto prim = GetValueNode<PrimitivePtr>(vnode);
  29. MetaFuncGraphPtr meta = ad::Kmeta(prim, resource);
  30. if (meta != nullptr) {
  31. return NewValueNode(meta);
  32. }
  33. return nullptr;
  34. }
  35. bool CheckIfEmbedJ(const CNodePtr &j_node) {
  36. auto &value_node = j_node->input(1);
  37. if (IsValueNode<Primitive>(value_node)) {
  38. return false;
  39. }
  40. auto func_graph = GetValueNode<FuncGraphPtr>(value_node);
  41. if (func_graph == nullptr) {
  42. MS_LOG(EXCEPTION) << "Unexpected j node:" << j_node->DebugString();
  43. }
  44. auto func_graph_manager = func_graph->manager();
  45. MS_EXCEPTION_IF_NULL(func_graph_manager);
  46. return func_graph_manager->func_graph_j_total(func_graph);
  47. }
  48. AnfNodePtr ExpandJ(const ValueNodePtr &vnode, const pipeline::ResourceBasePtr &resource) {
  49. if (IsValueNode<FuncGraph>(vnode)) {
  50. ScopeGuard scope_guard(vnode->scope());
  51. auto func_graph = GetValueNode<FuncGraphPtr>(vnode);
  52. MS_LOG(DEBUG) << "Funcgraph: " << func_graph->ToString() << " will expandJ now";
  53. auto newfg = ad::Grad(func_graph, resource);
  54. return NewValueNode(newfg);
  55. }
  56. if (IsValueNode<Primitive>(vnode)) {
  57. return ExpandJPrimitive(vnode, resource);
  58. }
  59. return nullptr;
  60. }
  61. } // namespace internal
  62. bool ExpandJPrim::operator()(const FuncGraphPtr &func_graph, const OptimizerPtr &optimizer) {
  63. // Search all j nodes.
  64. GetJPrim(optimizer->resource()->manager());
  65. // Get j nodes that don't have embed j nodes.
  66. std::vector<CNodePtr> todo;
  67. // If graph also contains J(FuncGraph) or J(Primitive), then ignore this graph.
  68. // ExpandJ innermost graph or primitive first.
  69. std::copy_if(j_nodes_.begin(), j_nodes_.end(), std::back_inserter(todo),
  70. [](const CNodePtr &j_node) { return !internal::CheckIfEmbedJ(j_node); });
  71. // Expand j nodes that don't have embed j nodes.
  72. bool change = false;
  73. for (auto &j_node : todo) {
  74. auto expanded_j = internal::ExpandJ(j_node->input(1)->cast<ValueNodePtr>(), optimizer->resource());
  75. optimizer->resource()->manager()->Replace(j_node, expanded_j);
  76. change = true;
  77. }
  78. return change;
  79. }
  80. void ExpandJPrim::GetJPrim(const FuncGraphManagerPtr &manager) {
  81. j_nodes_.clear();
  82. for (auto &fg : manager->func_graphs()) {
  83. std::vector<AnfNodePtr> &&toposet = TopoSort(fg->get_return());
  84. for (const auto &node : toposet) {
  85. if (IsPrimitiveCNode(node, prim::kPrimJ)) {
  86. j_nodes_.push_back(node->cast<CNodePtr>());
  87. }
  88. }
  89. }
  90. }
  91. } // namespace irpass
  92. } // namespace opt
  93. } // namespace mindspore