Inference: fix mask rcnn model diff, optim memory usage, memory leak. (#18532)
* Fix Mask rcnn predictor 1. refine memory optim algorithm to support the model with the block op. 2. output diff : modify the affine channel fuse 3. add condition_block_infer op add interface for setting trt calib table dir test=develop * add the missing files. test=developsum_op
parent
1529154821
commit
88b52a27fe
@ -0,0 +1,47 @@
|
||||
// Copyright (c) 2018 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.
|
||||
|
||||
#include "paddle/fluid/inference/analysis/passes/inference_op_replace_pass.h"
|
||||
#include <unordered_map>
|
||||
|
||||
namespace paddle {
|
||||
namespace inference {
|
||||
namespace analysis {
|
||||
|
||||
void InferenceOpReplacePass::RunImpl(Argument* argument) {
|
||||
if (!argument->use_gpu()) return;
|
||||
std::unordered_map<std::string, std::string> replaced_map{
|
||||
{"conditional_block", "conditional_block_infer"},
|
||||
};
|
||||
|
||||
auto& graph = argument->main_graph();
|
||||
auto nodes = graph.Nodes();
|
||||
|
||||
for (auto& node : nodes) {
|
||||
if (!node->IsOp()) continue;
|
||||
auto* op_desc = node->Op();
|
||||
std::string op_type = op_desc->Type();
|
||||
if (!replaced_map.count(op_type)) continue;
|
||||
op_desc->SetType(replaced_map[op_type]);
|
||||
op_desc->Flush();
|
||||
}
|
||||
}
|
||||
|
||||
std::string InferenceOpReplacePass::repr() const {
|
||||
return "inference-op-replace-pass";
|
||||
}
|
||||
|
||||
} // namespace analysis
|
||||
} // namespace inference
|
||||
} // namespace paddle
|
@ -0,0 +1,43 @@
|
||||
// Copyright (c) 2018 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "paddle/fluid/framework/ir/fuse_pass_base.h"
|
||||
#include "paddle/fluid/framework/scope.h"
|
||||
#include "paddle/fluid/inference/analysis/analysis_pass.h"
|
||||
#include "paddle/fluid/platform/place.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace inference {
|
||||
namespace analysis {
|
||||
|
||||
/*
|
||||
* There are some ops (while, conditional_block_op etc) which have different
|
||||
* optimization points under predicion and training conditions.
|
||||
* So, We added the corresponding inference impl to these ops separately.
|
||||
* This pass replaces these ops with corresponding inference ops.
|
||||
*/
|
||||
class InferenceOpReplacePass : public AnalysisPass {
|
||||
public:
|
||||
void RunImpl(Argument *argument) override;
|
||||
std::string repr() const override;
|
||||
};
|
||||
|
||||
} // namespace analysis
|
||||
} // namespace inference
|
||||
} // namespace paddle
|
@ -0,0 +1,74 @@
|
||||
/* Copyright (c) 2016 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. */
|
||||
|
||||
#include "paddle/fluid/operators/controlflow/conditional_block_op.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
/* We will implement the op with block separately in the future.
|
||||
* The main reason is that some of the training requirements
|
||||
* in these OPS can lead to problems(such as memory leaks) during inference.
|
||||
*/
|
||||
class ConditionalBlockInferOp : public ConditionalOp {
|
||||
public:
|
||||
ConditionalBlockInferOp(const std::string &type,
|
||||
const framework::VariableNameMap &inputs,
|
||||
const framework::VariableNameMap &outputs,
|
||||
const framework::AttributeMap &attrs)
|
||||
: ConditionalOp(type, inputs, outputs, attrs) {}
|
||||
|
||||
private:
|
||||
void RunImpl(const framework::Scope &scope,
|
||||
const platform::Place &dev_place) const override {
|
||||
bool need_run;
|
||||
if (Attr<bool>("is_scalar_condition")) {
|
||||
// When is_scalar_condition is True, the conditional variable is a scalar,
|
||||
// whether need to execute the operators in sub-block depends on the
|
||||
// conditional variable (Cond).
|
||||
auto xs = InputTensors(scope, "Cond");
|
||||
need_run = ScalarCondition(xs);
|
||||
} else {
|
||||
// When is_scalar_condition is False, the conditional variable maybe a
|
||||
// vector or tensor, whether need to execute the operators in sub-block
|
||||
// depends on the input variables (Input).
|
||||
auto xs = InputTensors(scope, "Input");
|
||||
need_run = std::all_of(
|
||||
xs.begin(), xs.end(),
|
||||
[](const framework::LoDTensor *t) { return t->numel() != 0; });
|
||||
}
|
||||
|
||||
if (need_run) {
|
||||
auto *scope_var = scope.FindVar(Output("Scope"));
|
||||
PADDLE_ENFORCE(scope_var != nullptr, "Must set scope");
|
||||
auto *scopes = scope_var->GetMutable<std::vector<framework::Scope *>>();
|
||||
scopes->resize(1);
|
||||
scopes->front() = &scope.NewScope();
|
||||
auto &cur_scope = *scopes->front();
|
||||
|
||||
framework::Executor exec(dev_place);
|
||||
auto *block = Attr<framework::BlockDesc *>("sub_block");
|
||||
exec.Run(*block->Program(), &cur_scope, block->ID(), false);
|
||||
scope.DeleteScope(scopes->front());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OPERATOR(conditional_block_infer, ops::ConditionalBlockInferOp,
|
||||
ops::ConditionalBlockOpProtoMaker,
|
||||
paddle::framework::EmptyGradOpMaker);
|
@ -0,0 +1,111 @@
|
||||
/* Copyright (c) 2016 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. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "paddle/fluid/framework/executor.h"
|
||||
#include "paddle/fluid/framework/op_registry.h"
|
||||
#include "paddle/fluid/framework/var_type.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
class ConditionalOp : public framework::OperatorBase {
|
||||
public:
|
||||
ConditionalOp(const std::string &type,
|
||||
const framework::VariableNameMap &inputs,
|
||||
const framework::VariableNameMap &outputs,
|
||||
const framework::AttributeMap &attrs)
|
||||
: OperatorBase(type, inputs, outputs, attrs) {}
|
||||
|
||||
protected:
|
||||
std::vector<const framework::LoDTensor *> InputTensors(
|
||||
const framework::Scope &scope, const std::string &in_name) const {
|
||||
std::vector<const framework::LoDTensor *> retv;
|
||||
auto xs = Inputs(in_name);
|
||||
retv.resize(xs.size(), nullptr);
|
||||
std::transform(
|
||||
xs.begin(), xs.end(), retv.begin(),
|
||||
[&scope](const std::string &var_name) -> const framework::LoDTensor * {
|
||||
auto *var = scope.FindVar(var_name);
|
||||
PADDLE_ENFORCE(var != nullptr, "Cannot find variable %s", var_name);
|
||||
return &var->Get<framework::LoDTensor>();
|
||||
});
|
||||
return retv;
|
||||
}
|
||||
|
||||
bool ScalarCondition(
|
||||
const std::vector<const framework::LoDTensor *> &ips) const {
|
||||
if (!(ips.size() == 1UL && ips[0]->IsInitialized())) {
|
||||
PADDLE_THROW("should have one initialized input as condition");
|
||||
}
|
||||
|
||||
PADDLE_ENFORCE(ips[0]->type() == framework::proto::VarType::BOOL &&
|
||||
ips[0]->numel() == 1,
|
||||
"condition input's data type should be bool, "
|
||||
"numel should be 1, actual numel is %d",
|
||||
ips[0]->numel());
|
||||
bool res = false;
|
||||
if (platform::is_gpu_place(ips[0]->place())) {
|
||||
#ifdef PADDLE_WITH_CUDA
|
||||
framework::LoDTensor cpu_tensor;
|
||||
framework::TensorCopy(*ips[0], platform::CPUPlace(), &cpu_tensor);
|
||||
platform::DeviceContextPool::Instance().Get(ips[0]->place())->Wait();
|
||||
res = cpu_tensor.data<bool>()[0];
|
||||
#endif
|
||||
} else {
|
||||
res = ips[0]->data<bool>()[0];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
class ConditionalBlockOpProtoMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
void Make() override {
|
||||
AddInput("Cond",
|
||||
"The conditional variable of this operator. If Cond is empty, the "
|
||||
"whole sub-block will not be executed.")
|
||||
.AsDuplicable();
|
||||
AddInput("Input", "The input variables of the sub-block.").AsDuplicable();
|
||||
AddOutput("Out", "The output variables of the sub-block.").AsDuplicable();
|
||||
AddOutput("Scope",
|
||||
"(std::vector<Scope*>) The step scope of conditional block. To "
|
||||
"unify the conditional block, rnn and while op, the type of "
|
||||
"scope is std::vector<Scope*>");
|
||||
AddAttr<framework::BlockDesc *>(
|
||||
"sub_block", "The step block of conditional block operator");
|
||||
AddAttr<bool>("is_scalar_condition",
|
||||
"The conditional variable (Cond) is used as scalar "
|
||||
"condition.")
|
||||
.SetDefault(false);
|
||||
AddComment(R"DOC(Conditional block operator
|
||||
|
||||
If `is_scalar_condition` is True, the conditional variable (Cond) is a scalar,
|
||||
run the operators in sub-block if Cond is True.
|
||||
|
||||
If `is_scalar_condition` is False, the conditional variable (Cond) is a vector or
|
||||
tensor, run the operators in sub-block if all of input variables are not empty.
|
||||
|
||||
|
||||
)DOC");
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
Loading…
Reference in new issue