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

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