1 /** 2 * Copyright 2024 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_PIPELINE_PYNATIVE_GRAD_AUTO_GRAD_H_ 18 #define MINDSPORE_CCSRC_PIPELINE_PYNATIVE_GRAD_AUTO_GRAD_H_ 19 20 #include <utility> 21 #include <string> 22 #include <memory> 23 24 #include "ir/anf.h" 25 #include "pipeline/pynative/base.h" 26 #include "pipeline/pynative/grad/ir/ir_bprop.h" 27 28 namespace mindspore::pynative::autograd { 29 class AutoGrad { 30 public: 31 AutoGrad() = default; 32 virtual ~AutoGrad() = default; 33 ir_bprop()34 inline IrBprop *ir_bprop() const { 35 MS_EXCEPTION_IF_NULL(ir_bprop_); 36 return ir_bprop_.get(); 37 } 38 // Used for high grad and jit KPynativeWithFProp(const GradParamPtr & grad_param)39 virtual bool KPynativeWithFProp(const GradParamPtr &grad_param) { return true; } 40 41 // Used for single op KPynativeOp(const GradParamPtr & grad_param)42 virtual bool KPynativeOp(const GradParamPtr &grad_param) { return true; } 43 44 // Update top cell output, record last_node UpdateOutputNodeOfTopCell(const ValuePtr & sens_out)45 virtual void UpdateOutputNodeOfTopCell(const ValuePtr &sens_out) {} 46 47 protected: 48 bool grad_by_value_{true}; 49 std::string device_target_; 50 IrBpropPtr ir_bprop_; 51 }; 52 using AutoGradPtr = std::shared_ptr<AutoGrad>; 53 } // namespace mindspore::pynative::autograd 54 #endif // MINDSPORE_CCSRC_PIPELINE_PYNATIVE_GRAD_AUTO_GRAD_H_ 55