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 4.0 kB

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