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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 CheckIfEmbedJFuncGraph(const FuncGraphPtr func_graph) {
  36. // if func graph also contain J FuncGraph, then ignore this funcgraph. ExpandJ innermost graph first;
  37. auto func_graph_manager = func_graph->manager();
  38. MS_EXCEPTION_IF_NULL(func_graph_manager);
  39. return func_graph_manager->func_graph_j_total(func_graph);
  40. }
  41. AnfNodePtr ExpandJ(const ValueNodePtr &vnode, const pipeline::ResourceBasePtr &resource) {
  42. if (IsValueNode<FuncGraph>(vnode)) {
  43. ScopeGuard scope_guard(vnode->scope());
  44. auto func_graph = GetValueNode<FuncGraphPtr>(vnode);
  45. MS_LOG(DEBUG) << "Node is ValueNodeGraph, graph: " << func_graph->ToString();
  46. // high_order_grad begin;
  47. // if graph also contain J Graph, then ignore this graph. ExpandJ innermost graph first;
  48. if (CheckIfEmbedJFuncGraph(func_graph)) {
  49. MS_LOG(DEBUG) << "Funcgraph: " << func_graph->ToString() << " contains J(funcgraph), will expandJ later";
  50. return nullptr;
  51. }
  52. // high_order_grad end;
  53. MS_LOG(DEBUG) << "Funcgraph: " << func_graph->ToString() << " will expandJ now";
  54. auto newfg = ad::Grad(func_graph, resource);
  55. return NewValueNode(newfg);
  56. }
  57. if (IsValueNode<Primitive>(vnode)) {
  58. return ExpandJPrimitive(vnode, resource);
  59. }
  60. return nullptr;
  61. }
  62. } // namespace internal
  63. } // namespace irpass
  64. } // namespace opt
  65. } // namespace mindspore