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

fix cinn_instruction_run inplace var not found problem #51769

Merged
merged 1 commit into from
Mar 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions paddle/fluid/eager/to_static/run_program_op_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ inline void RunProgramAPI(

auto input_names = details::GetTensorsName(x);
auto output_names = details::GetTensorsName(out);
auto param_names = details::GetTensorsName(params);
auto dout_names = details::GetTensorsName(dout);

if (VLOG_IS_ON(6)) {
Expand All @@ -319,6 +320,11 @@ inline void RunProgramAPI(
s << name << " ";
}
s << std::endl;
s << "param_names: ";
for (auto name : param_names) {
s << name << " ";
}
s << std::endl;
s << "output_names: ";
for (auto name : output_names) {
s << name << " ";
Expand Down
39 changes: 34 additions & 5 deletions paddle/fluid/operators/cinn/cinn_launch_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ std::unordered_set<std::string> CinnLaunchContext::ExtractInternalVarNames(
input_var_names.begin(), input_var_names.end(), exclude_names_fn);
std::for_each(
output_var_names.begin(), output_var_names.end(), exclude_names_fn);
VLOG(1) << "Internal var list: "
<< string::join_strings(remain_var_names, ", ");
return remain_var_names;
}

Expand Down Expand Up @@ -279,19 +281,23 @@ void CinnLaunchContext::CheckTensorEquivalent(
void CinnLaunchContext::InitializeArguments() {
for (auto&& arg : cinn_argument_names_) {
auto cinn_buffer = std::make_unique<cinn_buffer_t>();
auto cinn_tensor = GetCinnTensorOfVar(cinn2paddle_varmap_.at(arg));
auto paddle_varname = cinn2paddle_varmap_.at(arg);
auto cinn_tensor = GetCinnTensorOfVar(paddle_varname);
// assign dimensions with corresponding compiled tensor
cinn_buffer->resize(cinn_tensor->shape().data().data(),
cinn_tensor->shape().data().size());
cinn_buffer->type = cinn::runtime::ToRuntimeType(cinn_tensor->type());
VLOG(4) << string::Sprintf(
"Append an argument:name(%s),dims(%s),type(%s)",
"Append an argument:name(%s),paddle_name(%s), "
"dims(%s),type(%s),tensor(%p)",
arg,
paddle_varname,
framework::DDim(cinn_buffer->dims, cinn_buffer->dimensions).to_str(),
cinn_tensor->type());
cinn_tensor->type(),
cinn_tensor.get());
name2argument_.emplace(arg, cinn_buffer.get());
auto pdvar2cinnbuf_ = cinn2paddle_varmap_.at(arg);
paddle2argument_.emplace(pdvar2cinnbuf_, cinn_buffer.get());

paddle2argument_.emplace(paddle_varname, cinn_buffer.get());
hold_buffers_.emplace_back(std::move(cinn_buffer));
}
VLOG(4) << "Total argument size:" << name2argument_.size();
Expand Down Expand Up @@ -480,14 +486,37 @@ framework::InterpreterCore* CinnLaunchContext::InitializeInterpreterCore(
<< "; cached_scope_: " << cached_scope_;
VLOG(1) << "Internal var list: "
<< string::join_strings(internal_var_names_, ", ");

for (auto&& var_name : internal_var_names_) {
auto* var = scope->FindVar(var_name);
if (var != nullptr) {
continue;
}
VLOG(4) << "Create Variable " << var_name << " locally";
framework::InitializeVariable(scope->Var(var_name),
framework::proto::VarType::LOD_TENSOR);
}

// Actually, cinn_instruction will not use the var with name
// var_name+InplaceOutSuffix in paddle scope, but use the var with name.
// That means, var 'a' and 'a@InplaceOut' in cinn scope both links to var
// 'a' in paddle scope.

// So, why create 'a@InplaceOut' here?
// In order to make some paddle functions can visit all inputs and outputs
// of cinn_instruction_run op, for example, infer_shape function.

// It should be refined.
for (auto&& var_name : inplace_var_names_) {
auto name = var_name + InplaceOutSuffix;
auto* var = scope->FindVar(name);
if (var != nullptr) {
continue;
}
VLOG(4) << "Create Variable " << name << " locally";
framework::InitializeVariable(scope->Var(name),
framework::proto::VarType::LOD_TENSOR);
}
if (!interpreter_core_) {
framework::interpreter::ExecutionConfig execution_config;
execution_config.create_local_scope = false;
Expand Down