Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support inplace in dygraph eager_final state #40695

Merged

Conversation

pangyoki
Copy link
Contributor

@pangyoki pangyoki commented Mar 18, 2022

PR types

New features

PR changes

Others

Describe

中间态inplace功能见PR #40400
本PR为最终态inplace功能实现,主要修改自动代码生成部分。添加了inplace relu_示例。

  • python-c层
static PyObject * eager_final_state_api_relu_(PyObject *self, PyObject *args, PyObject *kwargs)
{
  paddle::platform::RecordEvent pythonc_record_event("relu_ pybind_imperative_func", paddle::platform::TracerEventType::Operator, 1);

  PyThreadState *tstate = nullptr;
  try
  {
    VLOG(6) << "Running Eager Final State API: relu_";

    // Get EagerTensors from args
    auto x = GetTensorFromArgs("relu_", "x", args, 0, false);


    // Parse Attributes


    tstate = PyEval_SaveThread();
    
    auto out = relu__final_state_dygraph_function(x);
    
    PyEval_RestoreThread(tstate);
    tstate = nullptr;

    ssize_t arg_id = GetIdxFromCoreOpsInfoMap(core_ops_final_state_args_info, "final_state_relu_", "x");
    ssize_t return_id = GetIdxFromCoreOpsInfoMap(core_ops_final_state_returns_info, "final_state_relu_", "out");
    return ToPyObject(out, return_id, args, arg_id);

  }
  catch(...) {
    if (tstate) {
      PyEval_RestoreThread(tstate);
    }
    ThrowExceptionToPython(std::current_exception());
    return nullptr;
  }
}
  • 动态图层
paddle::experimental::Tensor relu__final_state_dygraph_function(paddle::experimental::Tensor& x) {
    paddle::platform::RecordEvent dygraph_entrance_record_event("relu_ dygraph", paddle::platform::TracerEventType::Operator, 1);
    


    // Get 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);

    // Check Inplace
    egr::EagerUtils::CheckInplace(x, x_autograd_meta, require_any_grad);


    // Forward API Call
    auto api_result = paddle::experimental::relu_(x);

    // Bump Inplace Version
    x.bump_inplace_version();
    VLOG(3) << "Tensor(" << x.name() << ") uses Inplace Strategy.";


    {

        paddle::platform::RecordEvent node_creation_record_event("relu_ node_creation", paddle::platform::TracerEventType::Operator, 1);

        
        egr::AutogradMeta* out_autograd_meta = egr::EagerUtils::autograd_meta(&api_result);
        if(require_any_grad) {
            egr::EagerUtils::PassStopGradient(false,out_autograd_meta);
            
            // Node Construction
            auto grad_node = std::make_shared<FinalGradNoderelu>(1, 1);
            // SetAttributes

            // SetTensorWrappers
            grad_node->SetTensorWrapperx(x, true);
            // SetGradOutMeta & SetEdges
            grad_node->SetGradOutMeta(x_autograd_meta, 0);
            grad_node->AddEdges(x_autograd_meta, 0);
            // SetOutRank & SetHistory & SetGradInMeta & RetainGrad
            egr::EagerUtils::SetOutRankWithSlot(out_autograd_meta, 0);
            egr::EagerUtils::SetHistory(out_autograd_meta, grad_node);
            grad_node->SetGradInMeta(out_autograd_meta, 0);
            egr::EagerUtils::CheckAndRetainGrad(api_result);
        }
    }



    // Returns
    return api_result;
}

@paddle-bot-old
Copy link

你的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.

Copy link
Contributor

@JiabinYang JiabinYang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@pangyoki pangyoki merged commit 9793fc5 into PaddlePaddle:develop Mar 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants