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 "optimizer/irpass/gradient_eliminate.h"
  17. #include <utility>
  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 CheckIfEmbedJFuncGraph(const FuncGraphPtr func_graph) {
  37. // if func graph also contain J FuncGraph, then ignore this funcgraph. 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 contain J Graph, then ignore this graph. ExpandJ innermost graph first;
  49. if (CheckIfEmbedJFuncGraph(func_graph)) {
  50. MS_LOG(DEBUG) << "Funcgraph: " << func_graph->ToString() << " contains J(funcgraph), will expandJ later";
  51. return nullptr;
  52. }
  53. // high_order_grad end;
  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. } // namespace irpass
  65. } // namespace opt
  66. } // namespace mindspore