cinn_launch_context.cc 22.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15
#include "paddle/fluid/operators/cinn/cinn_launch_context.h"
16

17
#include <algorithm>
18
#include <functional>
19
#include <utility>
20
#include <vector>
21

22
#include "cinn/frontend/op_mapper_registry.h"
23 24
#include "cinn/hlir/framework/graph_compiler.h"
#include "cinn/hlir/framework/instruction.h"
25 26 27
#include "cinn/hlir/framework/scope.h"
#include "cinn/hlir/framework/tensor.h"
#include "cinn/runtime/cinn_runtime.h"
28 29
#include "cinn/runtime/intrinsic.h"
#include "paddle/fluid/framework/convert_utils.h"
30 31 32 33 34 35
#include "paddle/fluid/framework/details/build_strategy.h"
#include "paddle/fluid/framework/details/execution_strategy.h"
#include "paddle/fluid/framework/ir/graph.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/paddle2cinn/build_cinn_pass.h"
#include "paddle/fluid/framework/paddle2cinn/cinn_compiler.h"
36
#include "paddle/fluid/framework/paddle2cinn/transform_type.h"
37 38
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/framework/scope.h"
39
#include "paddle/fluid/framework/variable_helper.h"
40
#include "paddle/fluid/operators/cinn/cinn_op_helper.h"
41
#include "paddle/fluid/platform/device_context.h"
42
#include "paddle/fluid/platform/place.h"
43
#include "paddle/fluid/string/printf.h"
44
#include "paddle/phi/core/ddim.h"
45
#include "paddle/utils/string/string_helper.h"
46

47
namespace paddle {
48 49
namespace operators::details {

50
using framework::ParallelExecutor;
51
using framework::Scope;
52 53
using CinnInstruction = ::cinn::hlir::framework::Instruction;
using CinnRuntimeProgram = ::cinn::hlir::framework::Program;
54 55
using ::cinn::frontend::paddle::InplaceOutSuffix;
using framework::paddle2cinn::kInplaceVarNames;
56
using framework::paddle2cinn::kMemOptVarInfoFromMainGraph;
57
using framework::paddle2cinn::kSkipGcVarNames;
58
using framework::paddle2cinn::Name2VarInfoMap;
59

60 61 62 63
CinnLaunchContext::CinnLaunchContext(const framework::ir::Graph& graph,
                                     const CinnCompiledObject& compiled_obj)
    : cinn_scope_(compiled_obj.scope) {
  // collect all names of the CINN execution arguments
64
  auto var_names = cinn_scope_->var_names();
65
  cinn_argument_names_.reserve(var_names.size());
66
  std::transform(
67 68
      var_names.begin(),
      var_names.end(),
69
      std::inserter(cinn_argument_names_, cinn_argument_names_.end()),
70
      [](const auto& name_view) { return std::string(name_view.data()); });
71
  // build name map between the original variables and compiled ones
72 73 74 75 76 77
  BuildVarNameMap(compiled_obj.paddle2cinn_varmap, cinn_argument_names_);

  const auto& input_var_names =
      graph.Get<std::vector<std::string>>(framework::paddle2cinn::kInputVars);
  const auto& output_var_names =
      graph.Get<std::vector<std::string>>(framework::paddle2cinn::kOutputVars);
78 79
  inplace_var_names_ =
      graph.Get<std::unordered_set<std::string>>(kInplaceVarNames);
80 81 82 83 84 85 86 87 88 89 90
  internal_var_names_ =
      ExtractInternalVarNames(input_var_names, output_var_names);
  // initialize all execution arguments
  InitializeArguments();
  // DEPRECATED(CtfGo): following callback assignment will be deprecated soon
  for (auto&& var_name : input_var_names) {
    if (IsVariableUsed(var_name)) {
      AssignExternalVariable(var_name);
    }
  }
  for (auto&& var_name : output_var_names) {
91 92 93 94 95 96 97
    if (inplace_var_names_.count(var_name)) {
      VLOG(4) << "Inplaced variable:" << var_name << " -> "
              << var_name + InplaceOutSuffix << " as paddle2cinn varmap key";
      AssignExternalVariable(var_name + InplaceOutSuffix);
    } else {
      AssignExternalVariable(var_name);
    }
98 99 100 101 102 103
  }
  for (auto&& var_name : internal_var_names_) {
    AssignInternalVariable(var_name);
  }

  // Convert the CINN runtime program to a Paddle graph
104 105 106
  runtime_program_desc_ = BuildCompiledProgram(graph, compiled_obj);
  runtime_graph_ =
      std::make_unique<framework::ir::Graph>(*runtime_program_desc_.get());
107 108 109
  auto& outer_varinfo = graph.Get<Name2VarInfoMap>(kMemOptVarInfoFromMainGraph);
  runtime_graph_->SetNotOwned<Name2VarInfoMap>(kMemOptVarInfoFromMainGraph,
                                               &outer_varinfo);
110 111 112 113 114 115 116 117 118 119
  // use kSkipGcVarNames attr of graph to initialize skip_gc_vars_
  if (graph.Has(kSkipGcVarNames)) {
    const auto& skip_gc_vars =
        graph.Get<std::unordered_set<std::string>>(kSkipGcVarNames);
    skip_gc_vars_.insert(skip_gc_vars.begin(), skip_gc_vars.end());
    VLOG(4) << "Append skip_gc_vars:["
            << string::join_strings(skip_gc_vars, ',') << "]";
  }

  // collect variables name list to be skipped in GC
120 121
  skip_eager_vars_.reserve(input_var_names.size() + output_var_names.size());
  auto add_skip_var_fn = [&outer_varinfo, this](const std::string& var_name) {
122 123 124 125 126
    // Always consider Input/Output of Graph as skip_gc_vars, because
    // InterpreterCore has no eager_deletion_op to deal with it.

    VLOG(4) << "Append a skip_gc_var for InterpreterCore:" << var_name;
    skip_gc_vars_.insert(var_name);
127 128
    // if a var exists at the outer_varinfo map, that means it will be
    // erased by the following eager_deletion_op of current cinn_launch op
129 130
    if (!outer_varinfo.count(var_name)) {
      skip_eager_vars_.emplace_back(var_name);
131
      VLOG(4) << "Append a skip_gc_var for PE:" << var_name;
132 133
    }
  };
134 135 136 137
  std::for_each(
      input_var_names.begin(), input_var_names.end(), add_skip_var_fn);
  std::for_each(
      output_var_names.begin(), output_var_names.end(), add_skip_var_fn);
138 139 140 141
  VLOG(4) << string::Sprintf(
      "Distribution of variables in the graph compiled:"
      "input[%lu],internal[%lu],output[%lu],"
      "outer_eager_deletion[%lu],skip_eager_deletion[%lu],"
142
      "skip_gc_vars_[%lu]",
143 144 145 146 147
      input_var_names.size(),
      internal_var_names_.size(),
      output_var_names.size(),
      outer_varinfo.size(),
      skip_eager_vars_.size(),
148
      skip_gc_vars_.size());
149 150 151 152 153 154 155 156 157 158 159 160 161
}

void CinnLaunchContext::BuildVarNameMap(
    const std::unordered_map<std::string, std::string>& compiled_varmap,
    const std::unordered_set<std::string>& argument_names) {
  for (const auto& x : compiled_varmap) {
    if (!argument_names.count(x.second)) {
      // exclude variables not used
      continue;
    }
    // copy to local paddle2cinn map
    paddle2cinn_varmap_.emplace(x.first, x.second);
    // add an entry to local cinn2paddle map reversely
162 163
    auto res = cinn2paddle_varmap_.emplace(x.second, x.first);
    PADDLE_ENFORCE_EQ(
164 165
        res.second,
        true,
166 167
        platform::errors::InvalidArgument(
            "Cinn variable(%s) maps to more than one paddle variable(%s,%s)",
168 169 170
            x.second,
            res.first->second,
            x.first));
171
  }
172 173 174 175
  // supplement the relations of the remain variables
  // not appearing in above map, which are internal variables
  // and here we use the names from cinn compiled.
  for (const auto& var_name : argument_names) {
176 177 178 179 180
    if (!cinn2paddle_varmap_.count(var_name)) {
      cinn2paddle_varmap_.emplace(var_name, var_name);
      paddle2cinn_varmap_.emplace(var_name, var_name);
    }
  }
181 182

  PADDLE_ENFORCE_EQ(
183 184
      paddle2cinn_varmap_.size(),
      cinn2paddle_varmap_.size(),
185 186
      platform::errors::PreconditionNotMet(
          "Size of variables is not euqal, paddle[%ld] vs cinn[%ld]",
187 188
          paddle2cinn_varmap_.size(),
          cinn2paddle_varmap_.size()));
189 190
}

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
void CinnLaunchContext::UpdateCapturedEnv(const framework::Scope& scope,
                                          const platform::Place& place) {
  if (std::addressof(scope) == cached_scope_ &&
      std::addressof(place) == cached_place_) {
    VLOG(4) << "Captured scope:" << cached_scope_ << ", place:" << cached_place_
            << " are not changed";
    return;
  }
  cached_scope_ = std::addressof(scope);
  cached_place_ = std::addressof(place);
  cached_temp_scope_ = scope.NewTmpScope();
  VLOG(4) << "Captured env is update, scope:" << cached_scope_ << "->"
          << std::addressof(scope) << ", place:" << cached_place_ << "->"
          << std::addressof(place);
}

207 208
bool CinnLaunchContext::IsVariableUsed(const std::string& var_name) const {
  return paddle2cinn_varmap_.count(var_name) > 0;
209 210
}

211 212
CinnTensor CinnLaunchContext::GetCinnTensorOfVar(const std::string& var_name) {
  PADDLE_ENFORCE_EQ(
213 214
      IsVariableUsed(var_name),
      true,
215 216
      platform::errors::NotFound("Variable(%s) not applied in CINN", var_name));
  const auto& arg_name = paddle2cinn_varmap_.at(var_name);
217
  return cinn_scope_->GetTensor(arg_name);
218 219
}

220 221 222 223 224
std::unordered_set<std::string> CinnLaunchContext::ExtractInternalVarNames(
    const std::vector<std::string>& input_var_names,
    const std::vector<std::string>& output_var_names) {
  std::unordered_set<std::string> remain_var_names;
  remain_var_names.reserve(paddle2cinn_varmap_.size());
225 226
  std::transform(paddle2cinn_varmap_.begin(),
                 paddle2cinn_varmap_.end(),
227 228 229 230
                 std::inserter(remain_var_names, remain_var_names.end()),
                 [](const auto& name_pair) { return name_pair.first; });

  // exclude the input variables and output variables
231 232
  auto exclude_names_fn = [this,
                           &remain_var_names](const std::string& var_name) {
233
    remain_var_names.erase(var_name);
234 235 236
    if (inplace_var_names_.count(var_name)) {
      remain_var_names.erase(var_name + InplaceOutSuffix);
    }
237
  };
238 239

  VLOG(1) << "Input var list: " << string::join_strings(input_var_names, ", ");
S
Shuangchi He 已提交
240 241
  VLOG(1) << "Output var list: "
          << string::join_strings(output_var_names, ", ");
242 243 244 245
  std::for_each(
      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);
246
  return remain_var_names;
247 248
}

249
void CinnLaunchContext::CheckTensorEquivalent(
250
    const std::string& var_name, const phi::DenseTensor& paddle_tensor) {
251 252
  PADDLE_ENFORCE_EQ(IsVariableUsed(var_name),
                    true,
253 254
                    platform::errors::InvalidArgument(
                        "Variable(%s) not applied in cinn", var_name));
255
  // check dimension
256
  auto cinn_tensor = GetCinnTensorOfVar(var_name);
257
  auto cinn_dims = phi::make_ddim(cinn_tensor->shape().data());
258 259
  PADDLE_ENFORCE_EQ(paddle_tensor.dims(),
                    cinn_dims,
260 261
                    platform::errors::PreconditionNotMet(
                        "Tensors' shape in variable(%s) are not equivalent, "
262
                        "paddle is = [%s], but cinn is = [%s].",
263 264 265
                        var_name,
                        paddle_tensor.dims(),
                        cinn_dims));
266

267 268
  auto cinn_dtype =
      framework::paddle2cinn::TransToPaddleDataType(cinn_tensor->type());
269 270
  PADDLE_ENFORCE_EQ(paddle_tensor.dtype(),
                    cinn_dtype,
271 272 273
                    platform::errors::PreconditionNotMet(
                        "Tensors' dtype in variable(%s) are not equivalent, "
                        "paddle is = [%s], but cinn is = [%s].",
274 275 276
                        var_name,
                        paddle_tensor.dtype(),
                        cinn_dtype));
277 278
}

279 280 281 282 283 284 285
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));
    // assign dimensions with corresponding compiled tensor
    cinn_buffer->resize(cinn_tensor->shape().data().data(),
                        cinn_tensor->shape().data().size());
286
    cinn_buffer->type = cinn::runtime::ToRuntimeType(cinn_tensor->type());
287
    VLOG(4) << string::Sprintf(
288 289
        "Append an argument:name(%s),dims(%s),type(%s)",
        arg,
290
        framework::DDim(cinn_buffer->dims, cinn_buffer->dimensions).to_str(),
291
        cinn_tensor->type());
292
    name2argument_.emplace(arg, cinn_buffer.get());
293 294
    auto pdvar2cinnbuf_ = cinn2paddle_varmap_.at(arg);
    paddle2argument_.emplace(pdvar2cinnbuf_, cinn_buffer.get());
295 296
    hold_buffers_.emplace_back(std::move(cinn_buffer));
  }
297
  VLOG(4) << "Total argument size:" << name2argument_.size();
298 299
}

300
void CinnLaunchContext::AssignExternalVariable(const std::string& var_name) {
301 302
  PADDLE_ENFORCE_EQ(IsVariableUsed(var_name),
                    true,
303 304
                    platform::errors::InvalidArgument(
                        "Variable(%s) not applied in cinn", var_name));
305
  auto* cinn_buffer = GetCinnBufferOfVar(var_name);
306
  std::string revise_var_name = RedirectVarName(var_name);
307
  // assign external malloc/free callbacks of cinn_buffer_t
308
  cinn_buffer->external_malloc = new std::function<int(void*, cinn_buffer_t*)>(
309 310 311
      [this, revise_var_name](void* ctx, cinn_buffer_t* buffer) {
        auto* tensor = cached_scope_->GetVar(revise_var_name)
                           ->GetMutable<phi::DenseTensor>();
312
        tensor->Resize(framework::DDim(buffer->dims, buffer->dimensions));
313 314 315
        buffer->memory = reinterpret_cast<uint8_t*>(tensor->mutable_data(
            *cached_place_,
            framework::paddle2cinn::TransToPaddleDataType(buffer->type)));
316 317 318 319 320 321 322 323 324
        return 0;
      });

  // external variables will be recycled by global gc, so do nothing here
  cinn_buffer->external_free = new std::function<int(void*, cinn_buffer_t*)>(
      [](void* ctx, cinn_buffer_t* buffer) {
        // Do nothing
        return 0;
      });
325
}
326

327
void CinnLaunchContext::AssignInternalVariable(const std::string& var_name) {
328 329
  PADDLE_ENFORCE_EQ(IsVariableUsed(var_name),
                    true,
330 331
                    platform::errors::InvalidArgument(
                        "Variable(%s) not applied in cinn", var_name));
332
  auto* cinn_buffer = GetCinnBufferOfVar(var_name);
333
  std::string revise_var_name = RedirectVarName(var_name);
334
  // assign external malloc/free callbacks of cinn_buffer_t
335
  cinn_buffer->external_malloc = new std::function<int(void*, cinn_buffer_t*)>(
336 337 338
      [this, revise_var_name](void* ctx, cinn_buffer_t* buffer) {
        auto* tensor = cached_temp_scope_->Var(revise_var_name)
                           ->GetMutable<phi::DenseTensor>();
339
        tensor->Resize(framework::DDim(buffer->dims, buffer->dimensions));
340 341 342
        buffer->memory = reinterpret_cast<uint8_t*>(tensor->mutable_data(
            *cached_place_,
            framework::paddle2cinn::TransToPaddleDataType(buffer->type)));
343 344 345
        return 0;
      });

346 347
  // internal variables should release its buffer immediately
  // if no instruction use it
348
  cinn_buffer->external_free = new std::function<int(void*, cinn_buffer_t*)>(
349 350
      [this, revise_var_name](void* ctx, cinn_buffer_t* buffer) {
        auto* tensor = cached_temp_scope_->GetVar(revise_var_name)
351
                           ->GetMutable<phi::DenseTensor>();
352
        tensor->clear();
353 354
        return 0;
      });
355 356
}

357
std::unique_ptr<framework::ProgramDesc> CinnLaunchContext::BuildCompiledProgram(
358 359 360
    const framework::ir::Graph& graph, const CinnCompiledObject& compiled_obj) {
  CinnRuntimeProgram* runtime_program = compiled_obj.runtime_program.get();
  // Step 0: Create an empty program_desc, there will be only one block
361 362 363 364
  // framework::ProgramDesc program_desc;
  std::unique_ptr<framework::ProgramDesc> program_desc(
      new framework::ProgramDesc());
  auto* block = program_desc->MutableBlock(0);
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
  const std::vector<std::unique_ptr<CinnInstruction>>& instructions =
      runtime_program->GetRunInstructions();

  // build a map that links the name of a Paddle variable to its VarDesc
  const std::unordered_set<framework::ir::Node*>& nodes = graph.Nodes();
  std::unordered_map<std::string, framework::VarDesc*> original_vardescs;
  for (auto* node : nodes) {
    if (node->IsVar() && node->Var()) {
      original_vardescs.emplace(node->Name(), node->Var());
    }
  }

  // Step 1: Create a VarDesc for each execution argument:
  //   (1) For those variables that are input or output variables of the
  //   original subgraph, there must exist an original VarDesc, so
  //   we copy some useful info(such as IsParameter,Persistable)
  //   to the new VarDesc.
  //   (2) For all variables, the shape, data type of their VarDescs
  //   are set by values of the corresponding compiled tensors,
  //   including the in/out variables where the equiality between their tensors
  //   and the CINN compiled ones is verified in corresponding cinn_launch_op.
  for (auto&& arg : cinn_argument_names_) {
    const std::string& var_name = cinn2paddle_varmap_.at(arg);
    framework::VarDesc* var_desc = block->Var(var_name);
    var_desc->SetType(framework::proto::VarType::LOD_TENSOR);

    auto res = original_vardescs.find(var_name);
    if (res != original_vardescs.end()) {
      auto* ori_desc = res->second;
      var_desc->SetPersistable(ori_desc->Persistable());
      var_desc->SetIsParameter(ori_desc->IsParameter());
    }

    auto cinn_tensor = GetCinnTensorOfVar(var_name);
399 400
    var_desc->SetDataType(framework::TransToProtoVarType(
        framework::paddle2cinn::TransToPaddleDataType(cinn_tensor->type())));
401 402 403 404 405 406 407 408 409 410 411 412 413
    var_desc->SetShape(std::vector<int64_t>(cinn_tensor->shape().data().begin(),
                                            cinn_tensor->shape().data().end()));
  }

  // transform names of the input or output arguments of a CINN instruction
  // to the corresponding Paddle variable names, and repack them as one vector
  auto trans_and_pack_args_fn =
      [this](const std::vector<std::vector<std::string>>& cinn_args_array) {
        std::vector<std::string> var_names;
        for (auto&& cinn_args : cinn_args_array) {
          for (auto&& arg : cinn_args) {
            auto res = cinn2paddle_varmap_.find(arg);
            PADDLE_ENFORCE_NE(
414 415
                res,
                cinn2paddle_varmap_.end(),
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
                platform::errors::NotFound("Argument(%s) not found", arg));
            var_names.emplace_back(res->second);
          }
        }
        return var_names;
      };

  // Step 2: create a VarDesc of cinn_instruction_run op for
  //         each CINN instruction and append it to the main block
  for (auto ins_idx = 0; ins_idx < instructions.size(); ++ins_idx) {
    auto* ins = instructions.at(ins_idx).get();
    auto in_args = trans_and_pack_args_fn(ins->GetInArgs());
    auto out_args = trans_and_pack_args_fn(ins->GetOutArgs());
    auto* op_desc = block->AppendOp();
    op_desc->SetType("cinn_instruction_run");
    op_desc->SetInput(kX, in_args);
    op_desc->SetOutput(kOutputs, out_args);
    op_desc->SetAttr(kCachedIndex,
                     {static_cast<int64_t>(compiled_obj.cached_index)});
    op_desc->SetAttr(kInstructionIndex, {static_cast<int64_t>(ins_idx)});
  }

  return program_desc;
439 440
}

441 442 443 444
ParallelExecutor* CinnLaunchContext::InitializePE(const platform::Place& place,
                                                  framework::Scope* scope) {
  if (!parallel_executor_) {
    framework::details::ExecutionStrategy exec_strategy;
445 446
    exec_strategy.num_threads_ = 1;
    exec_strategy.use_device_ = platform::Place2DeviceType(place);
447 448 449 450 451 452
    framework::details::BuildStrategy build_strategy;
    parallel_executor_ = std::make_unique<ParallelExecutor>(
        place, scope, exec_strategy, build_strategy, runtime_graph_.get());
  }

  // update the scope bound to an OpHandle and rebuild temporary variables
453
  VLOG(4) << "Reset scope and initialize temporary variables";
454 455 456
  std::unordered_map<Scope*, Scope*> scope_map = {
      {parallel_executor_->GetLocalScopes().front(), scope}};
  parallel_executor_->ResetOpHandleScopeMapOfGraphs(scope_map);
457 458 459 460 461 462 463 464 465 466 467 468 469 470
  // instead of using the PrepareVariables function of ParallelExecutor to
  // initialize all variables, here we only initialize internal variables
  // because external variables are already included in parent scope.
  for (auto&& var_name : internal_var_names_) {
    auto* var = scope->FindVar(var_name);
    if (var != nullptr) {
      VLOG(5) << "internal variable:" << var_name
              << " has been initialized beforehand in global scope, skipped.";
      continue;
    }
    framework::InitializeVariable(scope->Var(var_name),
                                  framework::proto::VarType::LOD_TENSOR);
  }

471
  return parallel_executor_.get();
472 473
}

474 475 476 477 478 479 480
framework::InterpreterCore* CinnLaunchContext::InitializeInterpreterCore(
    const platform::Place& place, framework::Scope* scope) {
  if (!interpreter_core_ || scope != cached_scope_) {
    VLOG(1) << "interpreter_core_ is null or scope != cached_scope_: "
               "interpreter_core_: "
            << interpreter_core_.get() << "; scope: " << scope
            << "; cached_scope_: " << cached_scope_;
481 482
    VLOG(1) << "Internal var list: "
            << string::join_strings(internal_var_names_, ", ");
483 484 485 486 487 488 489 490 491
    for (auto&& var_name : internal_var_names_) {
      auto* var = scope->FindVar(var_name);
      if (var != nullptr) {
        continue;
      }
      framework::InitializeVariable(scope->Var(var_name),
                                    framework::proto::VarType::LOD_TENSOR);
    }
    if (!interpreter_core_) {
492 493 494 495
      framework::interpreter::ExecutionConfig execution_config;
      execution_config.create_local_scope = false;
      execution_config.used_for_cinn = true;
      execution_config.skip_gc_vars = skip_gc_vars_;
496
      interpreter_core_ = std::make_unique<framework::InterpreterCore>(
497
          place, runtime_program_desc_->Block(0), scope, execution_config);
498 499 500 501 502 503 504 505
    } else {
      interpreter_core_->reset_scope(scope);
    }
    UpdateCapturedEnv(*scope, place);
  }
  return interpreter_core_.get();
}

506 507 508 509 510 511 512
std::string CinnLaunchContext::RedirectVarName(const std::string& var_name) {
  auto pos = var_name.find(InplaceOutSuffix);
  if (pos == std::string::npos) {
    return var_name;
  }
  std::string remove_suffix_name = var_name.substr(0, pos);
  if (!inplace_var_names_.count(remove_suffix_name)) {
513
    return var_name;
514 515 516 517 518 519
  }
  VLOG(4) << "Inplaced variable:" << var_name << " redirect to "
          << remove_suffix_name;
  return remove_suffix_name;
}

520
cinn_buffer_t* CinnLaunchContext::GetCinnBufferOfVar(
521
    const std::string& var_name) {
522
  auto res = paddle2argument_.find(var_name);
523
  PADDLE_ENFORCE_NE(
524 525 526 527
      res,
      paddle2argument_.end(),
      platform::errors::NotFound("Variable(%s) not found in compilation result",
                                 var_name));
528
  return static_cast<cinn_buffer_t*>(res->second);
529 530
}

531
}  // namespace operators::details
532
}  // namespace paddle