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.h 1.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. #ifndef MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_AD_ADJOINT_H_
  17. #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_AD_ADJOINT_H_
  18. #include <memory>
  19. #include <utility>
  20. #include <vector>
  21. #include "ir/anf.h"
  22. #include "frontend/optimizer/opt.h"
  23. namespace mindspore {
  24. namespace ad {
  25. class Adjoint {
  26. public:
  27. Adjoint(const AnfNodePtr &primal, const AnfNodePtr &k, const FuncGraphPtr &caller);
  28. ~Adjoint() = default;
  29. AnfNodePtr primal();
  30. AnfNodePtr k();
  31. void UpdateK(const AnfNodePtr &k);
  32. void RegisterKUser(const CNodePtr &user, size_t index);
  33. AnfNodePtr dout();
  34. void AccumulateDout(const AnfNodePtr &dout_factor);
  35. void RegisterDoutUser(const CNodePtr &user, size_t index);
  36. void CallDoutHole();
  37. private:
  38. AnfNodePtr primal_;
  39. FuncGraphPtr caller_;
  40. // For ```def f(x): return expr```, The representation graph k is ```def kf(kx): return expr, bprop{expr}```.
  41. AnfNodePtr k_;
  42. std::vector<std::pair<CNodePtr, size_t>> k_user_;
  43. AnfNodePtr dout_;
  44. AnfNodePtr dout_hole_;
  45. std::vector<std::pair<CNodePtr, size_t>> dout_user_;
  46. };
  47. using AdjointPtr = std::shared_ptr<Adjoint>;
  48. } // namespace ad
  49. } // namespace mindspore
  50. #endif // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_AD_ADJOINT_H_