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.

adjoint.cc 3.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/adjoint.h"
  17. #include <utility>
  18. #include "ir/anf.h"
  19. #include "frontend/optimizer/ad/dfunctor.h"
  20. namespace mindspore {
  21. namespace ad {
  22. Adjoint::Adjoint(const AnfNodePtr &primal, const AnfNodePtr &k, const FuncGraphPtr &caller)
  23. : primal_(primal), caller_(caller), dout_(nullptr) {
  24. if (k != nullptr) {
  25. k_ = k;
  26. MS_LOG(DEBUG) << "Add adjoint for " << primal->ToString() << " " << k_->ToString();
  27. } else {
  28. // Init k hole in a recursive case.
  29. auto k_hole = std::make_shared<Primitive>("k_hole");
  30. (void)k_hole->AddAttr("info", MakeValue(primal->ToString()));
  31. k_ = NewValueNode(k_hole);
  32. MS_LOG(DEBUG) << "Add hole for " << primal->ToString() << " " << k_->ToString();
  33. }
  34. dout_hole_ = caller_->NewCNode({NewValueNode(prim::GetPythonOps("zeros_like")), k_});
  35. RegisterKUser(dout_hole_->cast<CNodePtr>(), 1);
  36. }
  37. AnfNodePtr Adjoint::k() { return k_; }
  38. void Adjoint::RegisterKUser(const CNodePtr &user, size_t index) { k_user_.emplace_back(std::make_pair(user, index)); }
  39. void Adjoint::UpdateK(const AnfNodePtr &new_k) {
  40. MS_EXCEPTION_IF_NULL(new_k);
  41. MS_LOG(DEBUG) << "Replace k " << k_->ToString() << " with " << new_k->ToString();
  42. // In recursive case, it needs update.
  43. for (auto &user : k_user_) {
  44. MS_LOG(DEBUG) << "Update k user " << user.first->ToString() << " " << user.second << " input with new_k"
  45. << new_k->ToString();
  46. if (user.first->input(user.second) != k_) {
  47. MS_LOG(EXCEPTION) << "Update k user " << user.first->ToString() << " " << user.second << " input with new_k "
  48. << new_k->ToString() << ", user relation is set wrongly";
  49. }
  50. user.first->set_input(user.second, new_k);
  51. }
  52. k_ = new_k;
  53. }
  54. AnfNodePtr Adjoint::primal() { return primal_; }
  55. AnfNodePtr Adjoint::dout() { return dout_hole_; }
  56. void Adjoint::RegisterDoutUser(const CNodePtr &user, size_t index) {
  57. dout_user_.emplace_back(std::make_pair(user, index));
  58. }
  59. void Adjoint::AccumulateDout(const AnfNodePtr &dout_factor) {
  60. if (dout_ != nullptr) {
  61. MS_LOG(DEBUG) << "Update dout " << dout_->ToString() << " with dout_factor " << dout_factor->ToString();
  62. auto add = prim::GetPythonOps("hyper_add");
  63. dout_ = caller_->NewCNode({NewValueNode(add), dout_, dout_factor});
  64. return;
  65. }
  66. dout_ = dout_factor;
  67. }
  68. void Adjoint::CallDoutHole() {
  69. if (dout_ != nullptr) {
  70. for (auto &user : dout_user_) {
  71. MS_LOG(DEBUG) << "Update dout user " << user.first->ToString() << " " << user.second << " input with dout "
  72. << dout_->ToString();
  73. if (user.first->input(user.second) != dout_hole_) {
  74. MS_LOG(EXCEPTION) << "Update dout user " << user.first->ToString() << " " << user.second << " input with dout "
  75. << dout_->ToString() << ", user relation is set wrongly";
  76. }
  77. user.first->set_input(user.second, dout_);
  78. }
  79. }
  80. }
  81. } // namespace ad
  82. } // namespace mindspore