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