From a9bd6f0c3e2d4174296a2314d236c150a2c49d39 Mon Sep 17 00:00:00 2001 From: CtfGo Date: Fri, 10 Dec 2021 11:28:30 +0800 Subject: [PATCH] change serval variable name and usage related cinn_launch (#38022) --- .../framework/paddle2cinn/cinn_compiler.h | 4 +- .../operators/cinn/cinn_launch_context.cc | 72 ++++++++++--------- .../operators/cinn/cinn_launch_context.h | 8 +-- paddle/fluid/operators/cinn/cinn_launch_op.h | 2 +- 4 files changed, 46 insertions(+), 40 deletions(-) diff --git a/paddle/fluid/framework/paddle2cinn/cinn_compiler.h b/paddle/fluid/framework/paddle2cinn/cinn_compiler.h index 3bc60e55557..5070eb5ce56 100644 --- a/paddle/fluid/framework/paddle2cinn/cinn_compiler.h +++ b/paddle/fluid/framework/paddle2cinn/cinn_compiler.h @@ -35,8 +35,8 @@ namespace paddle { namespace operators { namespace details { class CinnLaunchContext; -} -} +} // namespace details +} // namespace operators namespace framework { namespace paddle2cinn { diff --git a/paddle/fluid/operators/cinn/cinn_launch_context.cc b/paddle/fluid/operators/cinn/cinn_launch_context.cc index f6337a754f9..fa93bf00f2a 100644 --- a/paddle/fluid/operators/cinn/cinn_launch_context.cc +++ b/paddle/fluid/operators/cinn/cinn_launch_context.cc @@ -55,9 +55,11 @@ bool CinnLaunchContext::IsArgumentsInitialized() const { return true; } -bool CinnLaunchContext::IsVariableUsed(const std::string& paddle_name) const { - return paddle2cinn_varmap_.count(paddle_name) > 0 && - cinn_variable_names_.count(paddle2cinn_varmap_.at(paddle_name)) > 0; +bool CinnLaunchContext::IsVariableUsed( + const std::string& paddle_var_name) const { + return paddle2cinn_varmap_.count(paddle_var_name) > 0 && + cinn_variable_names_.count(paddle2cinn_varmap_.at(paddle_var_name)) > + 0; } CinnTensor CinnLaunchContext::GetCinnTensor(const std::string& var_name) { @@ -76,31 +78,33 @@ std::unordered_set CinnLaunchContext::GetInternalVariableNames() { return all_parameters; } -void CinnLaunchContext::CheckTensorEquivalent(const std::string& paddle_name, - const LoDTensor& paddle_tensor, - const CinnTensor& cinn_tensor) { +void CinnLaunchContext::CheckTensorEquivalent( + const std::string& paddle_var_name, const LoDTensor& paddle_tensor, + const CinnTensor& cinn_tensor) { // check dimension auto cinn_dims = framework::make_ddim(cinn_tensor->shape().data()); PADDLE_ENFORCE_EQ(paddle_tensor.dims(), cinn_dims, platform::errors::PreconditionNotMet( "Tensors' shape in variable(%s) are not equivalent, " "paddle's shape = [%s], but cinn's shape = [%s].", - paddle_name, paddle_tensor.dims(), cinn_dims)); + paddle_var_name, paddle_tensor.dims(), cinn_dims)); // TODO(CtfGo): check the underlying data type after CINN ready } -void CinnLaunchContext::AssignExternalVariable(const std::string& paddle_name) { - PADDLE_ENFORCE_EQ(IsVariableUsed(paddle_name), true, - platform::errors::InvalidArgument( - "Paddle variable(%s) not used by cinn", paddle_name)); +void CinnLaunchContext::AssignExternalVariable( + const std::string& paddle_var_name) { + PADDLE_ENFORCE_EQ( + IsVariableUsed(paddle_var_name), true, + platform::errors::InvalidArgument("Paddle variable(%s) not used by cinn", + paddle_var_name)); - const auto& cinn_name = paddle2cinn_varmap_.at(paddle_name); + const auto& cinn_var_name = paddle2cinn_varmap_.at(paddle_var_name); const auto& paddle_tensor = - cached_scope_->GetVar(paddle_name)->Get(); - CinnTensor cinn_tensor = GetCinnTensor(cinn_name); + cached_scope_->GetVar(paddle_var_name)->Get(); + CinnTensor cinn_tensor = GetCinnTensor(cinn_var_name); if (paddle_tensor.IsInitialized()) { - CheckTensorEquivalent(paddle_name, paddle_tensor, cinn_tensor); + CheckTensorEquivalent(paddle_var_name, paddle_tensor, cinn_tensor); } auto cinn_buffer = std::make_unique(); @@ -108,9 +112,9 @@ void CinnLaunchContext::AssignExternalVariable(const std::string& paddle_name) { cinn_buffer->resize(cinn_tensor->shape().data().data(), cinn_tensor->shape().data().size()); cinn_buffer->external_malloc = new std::function( - [this, paddle_name](void* ctx, cinn_buffer_t* buffer) { + [this, paddle_var_name](void* ctx, cinn_buffer_t* buffer) { auto* tensor = - cached_scope_->GetVar(paddle_name)->GetMutable(); + cached_scope_->GetVar(paddle_var_name)->GetMutable(); tensor->Resize(framework::DDim(buffer->dims, buffer->dimensions)); buffer->memory = reinterpret_cast( tensor->mutable_data(*cached_place_)); @@ -124,23 +128,25 @@ void CinnLaunchContext::AssignExternalVariable(const std::string& paddle_name) { return 0; }); - return SetArgument(cinn_name, std::move(cinn_buffer)); + return SetArgument(cinn_var_name, std::move(cinn_buffer)); } -void CinnLaunchContext::AssignInternalVariable(const std::string& cinn_name) { - PADDLE_ENFORCE_GT(cinn_variable_names_.count(cinn_name), 0, - platform::errors::InvalidArgument( - "Variable(%s) not found in cinn socpe.", cinn_name)); - CinnTensor cinn_tensor = GetCinnTensor(cinn_name); +void CinnLaunchContext::AssignInternalVariable( + const std::string& cinn_var_name) { + PADDLE_ENFORCE_GT( + cinn_variable_names_.count(cinn_var_name), 0, + platform::errors::InvalidArgument("Variable(%s) not found in cinn socpe.", + cinn_var_name)); + CinnTensor cinn_tensor = GetCinnTensor(cinn_var_name); auto cinn_buffer = std::make_unique(); // assign dimensions and alloc/free callback of cinn_buffer_t cinn_buffer->resize(cinn_tensor->shape().data().data(), cinn_tensor->shape().data().size()); cinn_buffer->external_malloc = new std::function( - [this, cinn_name](void* ctx, cinn_buffer_t* buffer) { + [this, cinn_var_name](void* ctx, cinn_buffer_t* buffer) { auto* tensor = - cached_temp_scope_->Var(cinn_name)->GetMutable(); + cached_temp_scope_->Var(cinn_var_name)->GetMutable(); tensor->Resize(framework::DDim(buffer->dims, buffer->dimensions)); buffer->memory = reinterpret_cast( tensor->mutable_data(*cached_place_)); @@ -150,22 +156,22 @@ void CinnLaunchContext::AssignInternalVariable(const std::string& cinn_name) { // internal variables should release its buffer immediately // if no instruction use it cinn_buffer->external_free = new std::function( - [this, cinn_name](void* ctx, cinn_buffer_t* buffer) { + [this, cinn_var_name](void* ctx, cinn_buffer_t* buffer) { auto* tensor = - cached_temp_scope_->GetVar(cinn_name)->GetMutable(); + cached_temp_scope_->GetVar(cinn_var_name)->GetMutable(); tensor->clear(); return 0; }); - return SetArgument(cinn_name, std::move(cinn_buffer)); + return SetArgument(cinn_var_name, std::move(cinn_buffer)); } -void CinnLaunchContext::SetArgument(const std::string& cinn_name, +void CinnLaunchContext::SetArgument(const std::string& cinn_var_name, std::unique_ptr&& buffer) { - VLOG(4) << "SetArgument-" << name2argument_.size() << ": name(" << cinn_name - << "), dims(" << framework::DDim(buffer->dims, buffer->dimensions) - << ")."; + VLOG(4) << "SetArgument-" << name2argument_.size() << ": name(" + << cinn_var_name << "), dims(" + << framework::DDim(buffer->dims, buffer->dimensions) << ")."; - name2argument_.emplace(cinn_name, buffer.get()); + name2argument_.emplace(cinn_var_name, buffer.get()); hold_buffers_.emplace_back(std::move(buffer)); } diff --git a/paddle/fluid/operators/cinn/cinn_launch_context.h b/paddle/fluid/operators/cinn/cinn_launch_context.h index 7bf70c9b9d0..7b71d77d8b8 100644 --- a/paddle/fluid/operators/cinn/cinn_launch_context.h +++ b/paddle/fluid/operators/cinn/cinn_launch_context.h @@ -49,13 +49,13 @@ class CinnLaunchContext { bool IsArgumentsInitialized() const; // Return whether a Paddle variable used on compiled kernels - bool IsVariableUsed(const std::string& paddle_name) const; + bool IsVariableUsed(const std::string& paddle_var_name) const; // Assign tensor buffer to input or output variables - void AssignExternalVariable(const std::string& paddle_name); + void AssignExternalVariable(const std::string& paddle_var_name); // Assign tensor buffer to internal variables - void AssignInternalVariable(const std::string& cinn_name); + void AssignInternalVariable(const std::string& cinn_var_name); // Extract internal variable names from CinnScope // by excluding used input and output variables @@ -75,7 +75,7 @@ class CinnLaunchContext { const CinnTensor& cinn_tensor); // Set an argument with (cinn name)->(cinn_buffer_t) pair - void SetArgument(const std::string& cinn_name, + void SetArgument(const std::string& cinn_var_name, std::unique_ptr&& buffer); private: diff --git a/paddle/fluid/operators/cinn/cinn_launch_op.h b/paddle/fluid/operators/cinn/cinn_launch_op.h index 8a5ca166941..170546ed230 100644 --- a/paddle/fluid/operators/cinn/cinn_launch_op.h +++ b/paddle/fluid/operators/cinn/cinn_launch_op.h @@ -103,7 +103,7 @@ class CinnLaunchOpKernel : public framework::OpKernel { compilation_key, inputs_name2tensor, target, stream); details::DebugCinnCompiledResult(cinn_compiled_object); - const auto& launch_context = cinn_compiled_object.launch_context; + auto* launch_context = cinn_compiled_object.launch_context.get(); // Step 3. Prepare arguments needed for the compiled executable program. launch_context->UpdateCapturedEnv(scope, place); if (!launch_context->IsArgumentsInitialized()) { -- GitLab