You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Pcard-72375
add Input copy for inpalce api backward calculate
Normal api change
Before:
paddle::Tensor pow_ad_func(const paddle::Tensor& x, paddle::experimental::Scalar y) {
...
// Get Input AutoGradMeta
egr::AutogradMeta* x_autograd_meta = egr::EagerUtils::nullable_autograd_meta(x);
...
// Forward API Call
auto api_result = paddle::experimental::pow(x, y);
...
...
// Get Outputs
auto& out = api_result;
// Get Output AutoGradMeta
egr::AutogradMeta* out_autograd_meta = egr::EagerUtils::autograd_meta(&out);
bool trace_backward = egr::Controller::Instance().HasGrad();
bool require_any_grad = egr::EagerUtils::ComputeRequireGrad(trace_backward,x_autograd_meta);
// Check Inplace if needed
// Node Creation
if(require_any_grad) {
...
// Node Construction
auto grad_node = std::shared_ptr<PowGradNode>(new PowGradNode(1, 1));
// Set for forward trace
if (FLAGS_check_nan_inf) {
grad_node->SetForwardTrace(egr::Controller::Instance().GetPythonStack());
}
// SetAttributes if needed
grad_node->SetAttributefactor(factor);
// Set TensorWrappers for Forward Inputs if needed
grad_node->SetTensorWrapperx(x);
// SetGradOutMeta & SetEdges
grad_node->SetGradOutMeta(x, 0);
// SetOutRank & SetHistory & SetGradInMeta
if (out_autograd_meta) {
egr::EagerUtils::SetOutRankWithSlot(out_autograd_meta, 0);
}
if (out_autograd_meta) {
egr::EagerUtils::SetHistory(out_autograd_meta, grad_node);
}
grad_node->SetGradInMeta(out, 0);
// Set TensorWrappers for Forward Outputs if needed
}
After this pr merge:
paddle::Tensor pow_ad_func(const paddle::Tensor& x, paddle::experimental::Scalar y) {
...
// Get Input AutoGradMeta
egr::AutogradMeta* x_autograd_meta = egr::EagerUtils::nullable_autograd_meta(x);
bool trace_backward = egr::Controller::Instance().HasGrad();
bool require_any_grad = egr::EagerUtils::ComputeRequireGrad(trace_backward,x_autograd_meta);
// Node Declaration
std::shared_ptr<PowGradNode> grad_node;
// Set grad_node before API Call
if(require_any_grad) {
paddle::platform::RecordEvent node_creation_record_event("pow node_creation", paddle::platform::TracerEventType::OperatorInner, 1);
// Node Construction
grad_node = std::shared_ptr<PowGradNode>(new PowGradNode(1, 1));
// Set for forward trace
if (FLAGS_check_nan_inf) {
grad_node->SetForwardTrace(egr::Controller::Instance().GetPythonStack());
}
// SetAttributes if needed
grad_node->SetAttributey(y);
// Set TensorWrappers for Forward Inputs if needed
grad_node->SetTensorWrapperx(x);
}
// Forward API Call
auto api_result = paddle::experimental::pow(x, y);
...
// Get Outputs
auto& out = api_result;
// Get Output AutoGradMeta
egr::AutogradMeta* out_autograd_meta = egr::EagerUtils::autograd_meta(&out);
...
// Set grad_node after API call
if(require_any_grad) {
egr::EagerUtils::PassStopGradient(false,out_autograd_meta);
// SetGradOutMeta & SetEdges
grad_node->SetGradOutMeta(x, 0);
// SetOutRank & SetHistory & SetGradInMeta
if (out_autograd_meta) {
egr::EagerUtils::SetOutRankWithSlot(out_autograd_meta, 0);
}
if (out_autograd_meta) {
egr::EagerUtils::SetHistory(out_autograd_meta, grad_node);
}
grad_node->SetGradInMeta(out, 0);
// Set TensorWrappers for Forward Outputs if needed
}
VLOG(4) << "Finish AD API: pow";`
Inplace api change
will add Input copy in inplace pow as follow
paddle::Tensor pow__ad_func(const paddle::Tensor& x, paddle::experimental::Scalar y) {
...
// Set grad_node before API Call
if(require_any_grad) {
paddle::platform::RecordEvent node_creation_record_event("pow node_creation", paddle::platform::TracerEventType::OperatorInner, 1);
// Node Construction
grad_node = std::shared_ptr<PowGradNode>(new PowGradNode(1, 1));
// Set for forward trace
if (FLAGS_check_nan_inf) {
grad_node->SetForwardTrace(egr::Controller::Instance().GetPythonStack());
}
// SetAttributes if needed
grad_node->SetAttributey(y);
// Set TensorWrappers for Forward Inputs if needed
auto x_clone = paddle::experimental::assign(x);
grad_node->SetTensorWrapperx(x_clone);
}
// Forward API Call
auto api_result = paddle::experimental::pow(x, y);
// Check NaN and Inf if needed
// Get Outputs
auto& out = api_result;
...
你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册。
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.
in description, before code of pow_ad_func, auto api_result = paddle::experimental::sparse::pow(x, factor); shall be auto api_result = paddle::experimental::pow(x, y); ?
in description, before code of pow_ad_func, auto api_result = paddle::experimental::sparse::pow(x, factor); shall be auto api_result = paddle::experimental::pow(x, y); ?
It has been changed to auto api_result = paddle::experimental::pow(x, y);
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR types
Others
PR changes
Others
Description
Pcard-72375
add Input copy for inpalce api backward calculate
Normal api change
Before:
After this pr merge:
Inplace api change
will add Input copy in inplace pow as follow