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.

grad.cc 2.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/ad/grad.h"
  17. #include "frontend/optimizer/ad/dfunctor.h"
  18. #include "ir/func_graph_cloner.h"
  19. #include "utils/ms_context.h"
  20. #include "utils/symbolic.h"
  21. namespace mindspore {
  22. namespace ad {
  23. FuncGraphPtr Grad(const FuncGraphPtr &func_graph, const pipeline::ResourceBasePtr &resources, bool is_top) {
  24. MS_EXCEPTION_IF_NULL(func_graph);
  25. auto gradkv = func_graph->transforms().find("grad");
  26. if (gradkv != func_graph->transforms().end()) {
  27. return gradkv->second.func_graph();
  28. }
  29. auto manager_ptr = resources->manager();
  30. MS_EXCEPTION_IF_NULL(manager_ptr);
  31. manager_ptr->AddFuncGraph(func_graph);
  32. auto multi_graph_sink = [&func_graph](const FuncGraphPtr &f) {
  33. if (MsContext::GetInstance()->get_param<bool>(MS_CTX_IS_MULTI_GRAPH_SINK)) {
  34. if (func_graph->has_flag(FUNC_GRAPH_FLAG_IGNORE_VALUES)) {
  35. f->set_flag(FUNC_GRAPH_FLAG_IGNORE_VALUES, true);
  36. }
  37. }
  38. };
  39. auto f = std::make_shared<DFunctor>(func_graph, resources);
  40. auto user_defined = f->KUserDefined(func_graph);
  41. if (user_defined != nullptr) {
  42. multi_graph_sink(user_defined);
  43. if (is_top) {
  44. DFunctor::Clear();
  45. }
  46. return user_defined;
  47. }
  48. f->Init(is_top);
  49. f->MapObject();
  50. f->MapMorphism();
  51. f->Finish();
  52. auto ret = f->k_graph();
  53. if (is_top) {
  54. DFunctor::Clear();
  55. }
  56. multi_graph_sink(ret);
  57. return ret;
  58. }
  59. FuncGraphPtr Kprim(const ValueNodePtr &value_node, const pipeline::ResourceBasePtr &resources) {
  60. auto fg = g_k_prims.KPrimitive(value_node, resources);
  61. if (fg == nullptr) {
  62. return nullptr;
  63. }
  64. return BasicClone(fg);
  65. }
  66. MetaFuncGraphPtr Kmeta(const PrimitivePtr &prim, const pipeline::ResourceBasePtr &) {
  67. MetaFuncGraphPtr fg = g_k_prims.KMetaFuncGraph(prim);
  68. return fg;
  69. }
  70. void CleanRes() { DFunctor::Clear(); }
  71. } // namespace ad
  72. } // namespace mindspore