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

[Inference] support collect shape in sub block #60451

Merged
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
3 changes: 2 additions & 1 deletion paddle/fluid/framework/naive_executor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ void NaiveExecutor::Run() {
func(op.get(), scope_);
}

if (op->Type() == "while") {
if (op->Type() == "while" || op->Type() == "conditional_block") {
op->SetOutputHooks(output_hookfuncs_);
op->SetInputHooks(input_hookfuncs_);
}

#ifdef PADDLE_WITH_NVTX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,16 +635,12 @@ void BuildOpFuncList(const platform::Place& place,
hook(op, local_scope);
}

if (op->Type() == "while") {
if (op->Type() == "while" || op->Type() == "conditional_block") {
op->SetInputHooks(input_hookfuncs);
op->SetOutputHooks(output_hookfuncs);
auto runtime_attrs = op->RuntimeAttrs();
runtime_attrs.insert(std::make_pair("used_for_inference", true));
op->SetRuntimeAttributeMap(runtime_attrs);
} else if (op->Type() == "conditional_block") {
auto runtime_attrs = op->RuntimeAttrs();
runtime_attrs.insert(std::make_pair("used_for_inference", true));
op->SetRuntimeAttributeMap(runtime_attrs);
}
}

Expand Down
6 changes: 1 addition & 5 deletions paddle/fluid/framework/new_executor/program_interpreter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -915,16 +915,12 @@ void ProgramInterpreter::RunOperator(const Instruction& instr_node) {
hook(op, local_scope);
}

if (op->Type() == "while") {
if (op->Type() == "while" || op->Type() == "conditional_block") {
op->SetInputHooks(input_hookfuncs_);
op->SetOutputHooks(output_hookfuncs_);
auto runtime_attrs = op->RuntimeAttrs();
runtime_attrs.insert(std::make_pair("used_for_inference", true));
op->SetRuntimeAttributeMap(runtime_attrs);
} else if (op->Type() == "conditional_block") {
auto runtime_attrs = op->RuntimeAttrs();
runtime_attrs.insert(std::make_pair("used_for_inference", true));
op->SetRuntimeAttributeMap(runtime_attrs);
}
}

Expand Down
52 changes: 27 additions & 25 deletions paddle/fluid/inference/api/analysis_predictor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2392,9 +2392,15 @@ bool AnalysisPredictor::ExpRunWithExternalStream(const gpuStream_t stream) {
#endif

void AnalysisPredictor::HookCollectShapeRangeInfo() {
if (config_.new_executor_enabled()) {
LOG_FIRST_N(WARNING, 1)
<< "When collecting shapes, it is recommended to run multiple loops to "
"obtain more accurate shape information.";
}

auto hook = [&](const std::string &op_type,
const std::string &input_name,
const paddle::Tensor &var) -> void {
const paddle::Tensor &input_tensor) -> void {
paddle::platform::DeviceContextPool &pool =
paddle::platform::DeviceContextPool::Instance();
if (config_.use_gpu()) {
Expand All @@ -2409,26 +2415,22 @@ void AnalysisPredictor::HookCollectShapeRangeInfo() {
#endif
}

auto *new_var = sub_scope_->GetVar(input_name);
if (!new_var) return;
if (!new_var->IsType<phi::DenseTensor>()) {
return;
}
auto tensor = new_var->Get<phi::DenseTensor>();
if (!tensor.initialized()) return;
framework::DDim dim = tensor.dims();
if (!input_tensor.is_dense_tensor()) return;
auto tensor =
std::dynamic_pointer_cast<phi::DenseTensor>(input_tensor.impl()).get();
framework::DDim dim = tensor->dims();
std::vector<int32_t> shape(dim.size());
for (int i = 0; i < static_cast<int>(shape.size()); ++i)
shape[i] = static_cast<int32_t>(dim[i]);
if (!shape.empty()) {
shape_info_[input_name].emplace_back(shape);
} else if (tensor.numel() > 0) {
} else if (tensor->numel() > 0) {
// This must be a zero dimension tensor.
PADDLE_ENFORCE_EQ(tensor.numel(),
PADDLE_ENFORCE_EQ(tensor->numel(),
1UL,
platform::errors::PreconditionNotMet(
"This tensor must have one element, but got %ld.",
tensor.numel()));
tensor->numel()));
std::vector<int32_t> zero_shape(1, 1);
shape_info_[input_name].emplace_back(zero_shape);
}
Expand All @@ -2438,34 +2440,34 @@ void AnalysisPredictor::HookCollectShapeRangeInfo() {
// assumption that all shape tensors in the model have numbers <= 8.
// This is a simple method to identify all shape tensors with some
// mistakes, but it doesn't matter.
auto is_shape_tensor = tensor.numel() <= 8 && tensor.numel() >= 1;
if ((tensor.dtype() == phi::DataType::INT32 ||
tensor.dtype() == phi::DataType::INT64) &&
auto is_shape_tensor = tensor->numel() <= 8 && tensor->numel() >= 1;
if ((tensor->dtype() == phi::DataType::INT32 ||
tensor->dtype() == phi::DataType::INT64) &&
is_shape_tensor) {
std::vector<int> int32_host(tensor.numel());
std::vector<int> int32_host(tensor->numel());

if (platform::is_cpu_place(tensor.place())) {
auto &int32_tensor = tensor;
if (tensor.dtype() == phi::DataType::INT64) {
if (platform::is_cpu_place(tensor->place())) {
auto &int32_tensor = *tensor;
if (tensor->dtype() == phi::DataType::INT64) {
auto *cpu_ctx = pool.Get(platform::CPUPlace());
int32_tensor = phi::funcs::TransDataType(
reinterpret_cast<const phi::CPUContext &>(*cpu_ctx),
tensor,
*tensor,
DataType::INT32);
}
paddle::memory::Copy(platform::CPUPlace(),
int32_host.data(),
platform::CPUPlace(),
int32_tensor.data<int>(),
int32_tensor.numel() * sizeof(int));
} else if (platform::is_gpu_place(tensor.place())) {
} else if (platform::is_gpu_place(tensor->place())) {
#if defined(PADDLE_WITH_CUDA)
auto *dev_ctx = pool.Get(tensor.place());
auto &int32_tensor = tensor;
if (tensor.dtype() == phi::DataType::INT64) {
auto *dev_ctx = pool.Get(tensor->place());
auto &int32_tensor = *tensor;
if (tensor->dtype() == phi::DataType::INT64) {
int32_tensor = phi::funcs::TransDataType(
reinterpret_cast<const phi::GPUContext &>(*dev_ctx),
tensor,
*tensor,
DataType::INT32);
}
paddle::memory::Copy(platform::CPUPlace(),
Expand Down
2 changes: 2 additions & 0 deletions paddle/fluid/operators/controlflow/conditional_block_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ class ConditionalBlockOp : public ConditionalOp {
#endif
core_.reset(new InterpreterCore(
dev_place, *block, &cur_scope, execution_config));
core_->SetOutputHooks(output_hookfuncs_);
core_->SetInputHooks(input_hookfuncs_);
VLOG(10) << "[interpreterCore] created:" << core_;
} else {
BuildScopeForControlFlowOp(*core_, *block, &cur_scope);
Expand Down