Skip to content

Commit 6772dfa

Browse files
author
Yibing Liu
committed
Merge branch 'develop' of upstream into seq_reshape_op_dev
2 parents c6275ec + f07a226 commit 6772dfa

32 files changed

+1870
-14
lines changed

paddle/framework/var_type.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,32 @@ inline VarDesc::VarType ToVarType(std::type_index type) {
2727
return VarDesc_VarType_LOD_RANK_TABLE;
2828
} else if (type.hash_code() == typeid(LoDTensorArray).hash_code()) {
2929
return VarDesc_VarType_LOD_TENSOR_ARRAY;
30+
} else if (type.hash_code() == typeid(SelectedRows).hash_code()) {
31+
return VarDesc_VarType_SELECTED_ROWS;
3032
} else {
3133
PADDLE_THROW("ToVarType:Unsupported type %s", type.name());
3234
}
3335
}
3436

37+
template <typename Visitor>
38+
inline void VisitVarType(const Variable& var, Visitor visitor) {
39+
switch (ToVarType(var.Type())) {
40+
case VarDesc_VarType_LOD_TENSOR:
41+
visitor(var.Get<framework::LoDTensor>());
42+
return;
43+
case VarDesc_VarType_LOD_RANK_TABLE:
44+
visitor(var.Get<LoDRankTable>());
45+
return;
46+
case VarDesc_VarType_LOD_TENSOR_ARRAY:
47+
visitor(var.Get<LoDTensorArray>());
48+
return;
49+
case VarDesc_VarType_SELECTED_ROWS:
50+
visitor(var.Get<SelectedRows>());
51+
return;
52+
default:
53+
PADDLE_THROW("Not supported visit type, %d", ToVarType(var.Type()));
54+
}
55+
}
56+
3557
} // namespace framework
3658
} // namespace paddle

paddle/gserver/layers/ROIPoolLayer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void ROIPoolLayer::forward(PassType passType) {
9898
size_t roiStartH = round(bottomROIs[2] * spatialScale_);
9999
size_t roiEndW = round(bottomROIs[3] * spatialScale_);
100100
size_t roiEndH = round(bottomROIs[4] * spatialScale_);
101-
CHECK_GE(roiBatchIdx, 0);
101+
CHECK_GE(roiBatchIdx, 0UL);
102102
CHECK_LT(roiBatchIdx, batchSize);
103103
size_t roiHeight = std::max(roiEndH - roiStartH + 1, 1UL);
104104
size_t roiWidth = std::max(roiEndW - roiStartW + 1, 1UL);

paddle/gserver/tests/test_MKLDNN.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ static void getAddtoConfig(TestConfig& cfg,
297297
}
298298

299299
void testAddtoLayer(const testImageDesc& pm, const size_t nInputs) {
300-
CHECK_GE(nInputs, 1);
300+
CHECK_GE(nInputs, 1UL);
301301
TestConfig dnnConfig;
302302
getAddtoConfig(dnnConfig, pm, nInputs);
303303
dnnConfig.layerConfig.set_type("mkldnn_addto");

paddle/operators/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ set(GLOB_OP_LIB ${OP_LIBRARY} CACHE INTERNAL "Global OP library")
214214
cc_test(gather_test SRCS gather_test.cc DEPS tensor)
215215
cc_test(net_op_test SRCS net_op_test.cc DEPS net_op)
216216
cc_test(scatter_test SRCS scatter_test.cc DEPS tensor)
217+
cc_test(beam_search_decode_op_test SRCS beam_search_decode_op_test.cc DEPS lod_tensor)
217218
cc_test(strided_memcpy_test SRCS strided_memcpy_test.cc DEPS tensor paddle_memory)
218219
cc_test(dynamic_recurrent_op_test SRCS dynamic_recurrent_op_test.cc
219220
rnn/recurrent_op_utils.cc

paddle/operators/assign_op.cc

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include "paddle/framework/data_type.h"
16+
#include "paddle/framework/op_registry.h"
17+
#include "paddle/framework/var_type.h"
18+
19+
namespace paddle {
20+
namespace operators {
21+
class AssignFunctor {
22+
public:
23+
AssignFunctor(framework::Variable *out,
24+
const platform::DeviceContext &dev_ctx)
25+
: out_(out), dev_ctx_(dev_ctx) {}
26+
27+
void operator()(const framework::LoDTensor &lod_tensor) const {
28+
auto &out_tensor = *out_->GetMutable<framework::LoDTensor>();
29+
copy_tensor(lod_tensor, &out_tensor);
30+
}
31+
32+
void operator()(const framework::LoDTensorArray &array) const {
33+
auto &out_array = *out_->GetMutable<framework::LoDTensorArray>();
34+
out_array.resize(array.size());
35+
for (size_t i = 0; i < array.size(); ++i) {
36+
copy_tensor(array[i], &out_array[i]);
37+
}
38+
}
39+
40+
void operator()(const framework::SelectedRows &rows) const {
41+
framework::SelectedRows &out_rows =
42+
*out_->GetMutable<framework::SelectedRows>();
43+
out_rows.set_rows(rows.rows());
44+
out_rows.set_height(rows.height());
45+
auto &t = rows.value();
46+
out_rows.mutable_value()->CopyFrom(t, t.place(), dev_ctx_);
47+
}
48+
49+
template <typename T>
50+
void operator()(const T &v) const {
51+
PADDLE_THROW("Not support type for assign op %s", typeid(T).name());
52+
}
53+
54+
private:
55+
void copy_tensor(const framework::LoDTensor &lod_tensor,
56+
framework::LoDTensor *out) const {
57+
auto &out_tensor = *out;
58+
out_tensor.CopyFrom(lod_tensor, lod_tensor.place(), dev_ctx_);
59+
out_tensor.set_lod(lod_tensor.lod());
60+
}
61+
62+
framework::Variable *out_;
63+
const platform::DeviceContext &dev_ctx_;
64+
};
65+
66+
class AssignOp : public framework::OperatorBase {
67+
public:
68+
AssignOp(const std::string &type, const framework::VariableNameMap &inputs,
69+
const framework::VariableNameMap &outputs,
70+
const framework::AttributeMap &attrs)
71+
: OperatorBase(type, inputs, outputs, attrs) {}
72+
void Run(const framework::Scope &scope,
73+
const platform::DeviceContext &dev_ctx) const override {
74+
auto *x = scope.FindVar(Input("X"));
75+
if (x == nullptr) {
76+
return;
77+
}
78+
auto *out = scope.FindVar(Output("Out"));
79+
PADDLE_ENFORCE(
80+
out != nullptr,
81+
"The Output(Out) should not be null if the Input(X) is set.");
82+
framework::VisitVarType(*x, AssignFunctor(out, dev_ctx));
83+
}
84+
};
85+
86+
class AssignOpProtoMaker : public framework::OpProtoAndCheckerMaker {
87+
public:
88+
AssignOpProtoMaker(framework::OpProto *proto,
89+
framework::OpAttrChecker *op_checker)
90+
: OpProtoAndCheckerMaker(proto, op_checker) {
91+
AddInput("X",
92+
"(LoDTensor, SelectedRows or LoDTensorArray) The input variable "
93+
"could be LoDTensor, SelectedRows or LoDTensorArray.")
94+
.AsDispensable();
95+
AddOutput("Out",
96+
"(LoDTensor, SelectedRows or LoDTensorArray) The type of output "
97+
"is the same as input X.");
98+
AddComment(R"DOC(Assign Operator
99+
100+
Out = X, when type in [LoDTensor/SelectedRows/LoDTensorArray]
101+
raise error if the type is not listed above.
102+
)DOC");
103+
}
104+
};
105+
106+
class AssignInferShape : public framework::InferShapeBase {
107+
public:
108+
void operator()(framework::InferShapeContext *context) const override {
109+
if (context->HasInput("X")) {
110+
auto type = context->GetInputsVarType("X")[0];
111+
if (type == framework::VarDesc_VarType_SELECTED_ROWS ||
112+
type == framework::VarDesc_VarType_LOD_TENSOR) {
113+
context->SetOutputDim("Out", context->GetInputDim("X"));
114+
}
115+
}
116+
}
117+
};
118+
119+
class AssignGradMaker : public framework::SingleGradOpDescMaker {
120+
public:
121+
using framework::SingleGradOpDescMaker::SingleGradOpDescMaker;
122+
123+
protected:
124+
std::unique_ptr<framework::OpDescBind> Apply() const override {
125+
auto *op = new framework::OpDescBind();
126+
op->SetType("assign");
127+
op->SetInput("X", OutputGrad("Out"));
128+
op->SetOutput("Out", InputGrad("X"));
129+
return std::unique_ptr<framework::OpDescBind>(op);
130+
}
131+
};
132+
133+
} // namespace operators
134+
} // namespace paddle
135+
136+
namespace ops = paddle::operators;
137+
REGISTER_OPERATOR(assign, ops::AssignOp, ops::AssignGradMaker,
138+
ops::AssignInferShape, ops::AssignOpProtoMaker);
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include "paddle/operators/beam_search_decode_op.h"
16+
17+
namespace paddle {
18+
namespace operators {
19+
20+
class BeamSearchDecodeOp : public framework::OperatorBase {
21+
public:
22+
BeamSearchDecodeOp(const std::string& type,
23+
const framework::VariableNameMap& inputs,
24+
const framework::VariableNameMap& outputs,
25+
const framework::AttributeMap& attrs)
26+
: OperatorBase(type, inputs, outputs, attrs) {}
27+
void Run(const framework::Scope& scope,
28+
const platform::DeviceContext& dev_ctx) const override {
29+
framework::ExecutionContext ctx(*this, scope, dev_ctx);
30+
const LoDTensorArray* ids = ctx.Input<LoDTensorArray>("Ids");
31+
const LoDTensorArray* scores = ctx.Input<LoDTensorArray>("Scores");
32+
const size_t step_num = ids->size();
33+
PADDLE_ENFORCE_GT(step_num, 0UL,
34+
"beam search steps should be larger than 0");
35+
const size_t source_num = ids->at(0).lod().at(0).size() - 1;
36+
PADDLE_ENFORCE_GT(source_num, 0UL, "source num should be larger than 0");
37+
38+
for (size_t i = 0; i < step_num; ++i) {
39+
PADDLE_ENFORCE_EQ(ids->at(i).lod().size(), 2UL,
40+
"Level of LodTensor should be 2");
41+
}
42+
43+
// prepare output
44+
LoDTensor* sentenceIds = ctx.Output<LoDTensor>("SentenceIds");
45+
LoDTensor* sentenceScores = ctx.Output<LoDTensor>("SentenceScores");
46+
47+
BeamSearchDecoder<float> beam_search_decoder;
48+
beam_search_decoder.PackAllSteps(*ids, *scores, sentenceIds,
49+
sentenceScores);
50+
}
51+
};
52+
53+
class BeamSearchDecodeOpProtoMaker : public framework::OpProtoAndCheckerMaker {
54+
public:
55+
BeamSearchDecodeOpProtoMaker(framework::OpProto* proto,
56+
framework::OpAttrChecker* op_checker)
57+
: OpProtoAndCheckerMaker(proto, op_checker) {
58+
AddInput("Ids",
59+
"(LodTensorArray)"
60+
"score of the candidate words in each step");
61+
AddInput("Scores",
62+
"(LodTensorArray)"
63+
"score of the candidate words in each step");
64+
AddOutput("SentenceIds",
65+
"(LodTensor)"
66+
"All possible result sentences of word ids");
67+
AddOutput("SentenceScores",
68+
"(LodTensor)"
69+
"All possible result sentences of word scores");
70+
AddComment(R"DOC(
71+
Pack the result of Beam search op into SentenceIds and SentenceScores.
72+
)DOC");
73+
}
74+
};
75+
76+
class BeamSearchDecodeInferShape : public framework::InferShapeBase {
77+
public:
78+
void operator()(framework::InferShapeContext* context) const override {
79+
PADDLE_ENFORCE(context->HasInput("Ids"),
80+
"BeamSearchDecodeOp must has input Ids");
81+
PADDLE_ENFORCE(context->HasInput("Scores"),
82+
"BeamSearchDecodeOp must has input Scores");
83+
PADDLE_ENFORCE(context->HasOutput("SentenceIds"),
84+
"BeamSearchDecodeOp must has output SentenceIds");
85+
PADDLE_ENFORCE(context->HasOutput("SentenceScores"),
86+
"BeamSearchDecodeOp must has output SentenceScores");
87+
}
88+
};
89+
90+
class BeamSearchDecodeInferVarType : public framework::VarTypeInference {
91+
public:
92+
void operator()(const framework::OpDescBind& op_desc,
93+
framework::BlockDescBind* block) const override {
94+
for (auto& o : op_desc.Output("SentenceIds")) {
95+
block->Var(o)->SetType(framework::VarDesc::LOD_TENSOR);
96+
}
97+
for (auto& o : op_desc.Output("SentenceScores")) {
98+
block->Var(o)->SetType(framework::VarDesc::LOD_TENSOR);
99+
}
100+
}
101+
};
102+
103+
} // namespace operators
104+
} // namespace paddle
105+
106+
REGISTER_OPERATOR(beam_search_decode, paddle::operators::BeamSearchDecodeOp,
107+
paddle::operators::BeamSearchDecodeOpProtoMaker,
108+
paddle::operators::BeamSearchDecodeInferShape,
109+
paddle::operators::BeamSearchDecodeInferVarType,
110+
paddle::framework::EmptyGradOpMaker);

0 commit comments

Comments
 (0)