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

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