Skip to content

Commit

Permalink
Op2functor opt1 (PaddlePaddle#27)
Browse files Browse the repository at this point in the history
* replace to small vector and change to const &

* add std::move

Co-authored-by: Chen Weihang <chenweihang@baidu.com>
  • Loading branch information
MingMingShangTian and chenwhql authored Oct 21, 2021
1 parent bbe59bc commit 76a588e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
11 changes: 6 additions & 5 deletions paddle/fluid/framework/operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1284,11 +1284,12 @@ OpKernelType OperatorWithKernel::InnerGetExpectedKernelType(

void OperatorWithKernel::ChoosePtenKernel(const ExecutionContext& ctx) const {
pt_kernel_signature_.reset(
new KernelSignature(this->GetExpectedPtenKernelArgs(ctx)));
new KernelSignature(std::move(this->GetExpectedPtenKernelArgs(ctx))));

VLOG(1) << KernelSignatureToString(*pt_kernel_signature_.get());

kernel_type_.reset(new OpKernelType(InnerGetExpectedKernelType(ctx)));
kernel_type_.reset(
new OpKernelType(std::move(InnerGetExpectedKernelType(ctx))));

auto pt_kernel_name = pten::KernelName(pt_kernel_signature_->first);
auto pt_kernel_key = TransOpKernelTypeToPtenKernelKey(*kernel_type_.get());
Expand Down Expand Up @@ -1780,7 +1781,7 @@ KernelSignature OperatorWithKernel::GetExpectedPtenKernelArgs(
return *(KernelSignatureMap::Instance().GetNullable(Type()));
} else {
KernelArgsNameMakerByOpProto maker(Info().proto_);
auto signature = maker.GetKernelSignature();
auto signature = std::move(maker.GetKernelSignature());
KernelSignatureMap::Instance().Insert(Type(), signature);
return signature;
}
Expand Down Expand Up @@ -1831,8 +1832,8 @@ pten::KernelContext OperatorWithKernel::BuildPtenKernelContext(
<< in_def.layout;

auto ins_vector = ctx.inputs.at(input_names[i]);
std::vector<std::shared_ptr<pten::TensorBase>> tmp_inputs;

paddle::SmallVector<std::shared_ptr<pten::TensorBase>> tmp_inputs;
for (auto var : ins_vector) {
auto pt_in = framework::InputVariableToPtenTensor(*var, in_def);
tmp_inputs.emplace_back(pt_in);
Expand All @@ -1844,7 +1845,7 @@ pten::KernelContext OperatorWithKernel::BuildPtenKernelContext(
auto out_def = output_defs.at(i);
auto outs_vector = ctx.outputs.at(output_names[i]);

std::vector<std::shared_ptr<pten::TensorBase>> tmp_outputs;
paddle::SmallVector<std::shared_ptr<pten::TensorBase>> tmp_outputs;
for (auto var : outs_vector) {
auto pt_out = framework::OutputVariableToPtenTensor(var, out_def);
tmp_outputs.emplace_back(pt_out);
Expand Down
11 changes: 6 additions & 5 deletions paddle/fluid/imperative/prepared_operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "paddle/fluid/framework/details/nan_inf_utils.h"
#include "paddle/fluid/framework/pten_utils.h"
#include "paddle/fluid/imperative/infer_shape_context.h"
#include "paddle/utils/small_vector.h"
#ifdef PADDLE_WITH_XPU
#include "paddle/fluid/platform/xpu/xpu_op_list.h"
#endif
Expand Down Expand Up @@ -262,9 +263,9 @@ static pten::KernelContext BuildDygraphPtenKernelContext(
auto& attr_names = std::get<1>(pt_kernel_signature.second);
auto& output_names = std::get<2>(pt_kernel_signature.second);

auto input_defs = pt_kernel.args_def().input_defs();
auto output_defs = pt_kernel.args_def().output_defs();
auto attr_defs = pt_kernel.args_def().attribute_defs();
auto& input_defs = pt_kernel.args_def().input_defs();
auto& output_defs = pt_kernel.args_def().output_defs();
auto& attr_defs = pt_kernel.args_def().attribute_defs();

PADDLE_ENFORCE_EQ(input_names.size(), input_defs.size(),
platform::errors::InvalidArgument(
Expand All @@ -288,7 +289,7 @@ static pten::KernelContext BuildDygraphPtenKernelContext(
auto& in_def = input_defs.at(i);
auto& ins_vector = ins.at(input_names[i]);

std::vector<std::shared_ptr<pten::TensorBase>> tmp_inputs;
paddle::SmallVector<std::shared_ptr<pten::TensorBase>> tmp_inputs;
for (auto var : ins_vector) {
const auto& variable = var->Var();

Expand All @@ -302,7 +303,7 @@ static pten::KernelContext BuildDygraphPtenKernelContext(
auto& out_def = output_defs.at(i);
auto& outs_vector = outs.at(output_names[i]);

std::vector<std::shared_ptr<pten::TensorBase>> tmp_outputs;
paddle::SmallVector<std::shared_ptr<pten::TensorBase>> tmp_outputs;
for (auto var : outs_vector) {
auto* variable = var->MutableVar();

Expand Down
29 changes: 15 additions & 14 deletions paddle/pten/core/kernel_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "paddle/pten/core/tensor_base.h"
#include "paddle/utils/any.h"
#include "paddle/utils/small_vector.h"

// See Note [ Why still include the fluid headers? ]
#include "paddle/fluid/platform/device_context.h"
Expand All @@ -40,9 +41,9 @@ class KernelContext {
public:
explicit KernelContext(const DeviceContext& dev_ctx) : dev_ctx_(dev_ctx) {}
KernelContext(const DeviceContext& dev_ctx,
const std::vector<std::shared_ptr<TensorBase>>& inputs,
const std::vector<std::shared_ptr<TensorBase>>& outputs,
const std::vector<paddle::any>& attrs)
const paddle::SmallVector<std::shared_ptr<TensorBase>>& inputs,
const paddle::SmallVector<std::shared_ptr<TensorBase>>& outputs,
const paddle::SmallVector<paddle::any>& attrs)
: dev_ctx_(dev_ctx), inputs_(inputs), outputs_(outputs), attrs_(attrs) {}

template <typename CtxType>
Expand All @@ -57,7 +58,8 @@ class KernelContext {
input_range_.emplace_back(std::pair<int, int>(index, index + 1));
}

void EmplaceBackInputs(std::vector<std::shared_ptr<TensorBase>> inputs) {
void EmplaceBackInputs(
const paddle::SmallVector<std::shared_ptr<TensorBase>>& inputs) {
for (auto in : inputs) {
inputs_.emplace_back(in);
}
Expand All @@ -74,7 +76,8 @@ class KernelContext {
output_range_.emplace_back(std::pair<int, int>(index, index + 1));
}

void EmplaceBackOutputs(std::vector<std::shared_ptr<TensorBase>> outputs) {
void EmplaceBackOutputs(
const paddle::SmallVector<std::shared_ptr<TensorBase>>& outputs) {
for (auto out : outputs) {
outputs_.emplace_back(out);
}
Expand Down Expand Up @@ -113,22 +116,20 @@ class KernelContext {
// DeviceContext base class
const DeviceContext& dev_ctx_;

// TODO(chenweihang): replaced by small_vector
// TODO(chenweihang): Tensor -> Tensor*, Tensor should by managed `scope`
// Note: can't use API Tensor here, the inference don't use this API Tensor
std::vector<std::shared_ptr<TensorBase>> inputs_{};
std::vector<std::shared_ptr<TensorBase>> outputs_{};
std::vector<paddle::any> attrs_{};
paddle::SmallVector<std::shared_ptr<TensorBase>> inputs_{};
paddle::SmallVector<std::shared_ptr<TensorBase>> outputs_{};
paddle::SmallVector<paddle::any> attrs_{};

// Only contains input like list[Tensor] need `range`
// TODO(chenweihang): replaced by small_vector
std::vector<std::pair<int, int>> input_range_{{}};
std::vector<std::pair<int, int>> output_range_{{}};
paddle::SmallVector<std::pair<int, int>> input_range_{{}};
paddle::SmallVector<std::pair<int, int>> output_range_{{}};

// Only static graph need `name`
// TODO(chenweihang): replaced by paddle::string_view
std::vector<std::string> input_names_{{}};
std::vector<std::string> output_names_{{}};
paddle::SmallVector<std::string> input_names_{{}};
paddle::SmallVector<std::string> output_names_{{}};
};

} // namespace pten

0 comments on commit 76a588e

Please sign in to comment.