Skip to content

Commit 920db1a

Browse files
committed
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into boost_cmake
2 parents 608ebec + d43932c commit 920db1a

23 files changed

+1439
-75
lines changed

doc/api/v2/fluid/layers.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ dynamic_lstm
1818
.. autofunction:: paddle.v2.fluid.layers.dynamic_lstm
1919
:noindex:
2020

21+
dynamic_gru
22+
-----------
23+
.. autofunction:: paddle.v2.fluid.layers.dynamic_gru
24+
:noindex:
25+
2126
data
2227
----
2328
.. autofunction:: paddle.v2.fluid.layers.data
@@ -500,6 +505,11 @@ swish
500505
.. autofunction:: paddle.v2.fluid.layers.swish
501506
:noindex:
502507

508+
im2sequence
509+
------
510+
.. autofunction:: paddle.v2.fluid.layers.im2sequence
511+
:noindex:
512+
503513
edit_distance
504514
---------------
505515
.. autofunction:: paddle.v2.fluid.layers.edit_distance_error

paddle/framework/block_desc.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ std::vector<VarDesc *> BlockDesc::AllVars() const {
7575

7676
OpDesc *BlockDesc::AppendOp() {
7777
need_update_ = true;
78-
ops_.emplace_back(new OpDesc());
78+
ops_.emplace_back(new OpDesc(this));
7979
return ops_.back().get();
8080
}
8181

@@ -86,7 +86,7 @@ void BlockDesc::AppendAllocatedOp(std::unique_ptr<OpDesc> &&op_desc) {
8686

8787
OpDesc *BlockDesc::PrependOp() {
8888
need_update_ = true;
89-
ops_.emplace_front(new OpDesc());
89+
ops_.emplace_front(new OpDesc(this));
9090
return ops_.front().get();
9191
}
9292

@@ -153,7 +153,7 @@ BlockDesc::BlockDesc(ProgramDesc *prog, proto::BlockDesc *desc)
153153
vars_[var_desc.name()].reset(new VarDesc(var_desc));
154154
}
155155
for (const proto::OpDesc &op_desc : desc_->ops()) {
156-
ops_.emplace_back(new OpDesc(op_desc, prog));
156+
ops_.emplace_back(new OpDesc(op_desc, prog, this));
157157
}
158158
}
159159

@@ -162,7 +162,7 @@ BlockDesc::BlockDesc(const BlockDesc &other, proto::BlockDesc *desc,
162162
: prog_(prog), desc_(desc) {
163163
need_update_ = true;
164164
for (auto &op : other.ops_) {
165-
ops_.emplace_back(new OpDesc(*op));
165+
ops_.emplace_back(new OpDesc(*op, this));
166166
}
167167

168168
for (auto &it : other.vars_) {

paddle/framework/op_desc.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void OpDesc::CopyFrom(const OpDesc &op_desc) {
9797
need_update_ = true;
9898
}
9999

100-
OpDesc::OpDesc(const proto::OpDesc &desc, ProgramDesc *prog)
100+
OpDesc::OpDesc(const proto::OpDesc &desc, ProgramDesc *prog, BlockDesc *block)
101101
: desc_(desc), need_update_(false) {
102102
// restore inputs_
103103
int input_size = desc_.inputs_size();
@@ -131,6 +131,7 @@ OpDesc::OpDesc(const proto::OpDesc &desc, ProgramDesc *prog)
131131
attrs_[attr_name] = prog->MutableBlock(bid);
132132
}
133133
}
134+
this->block_ = block;
134135
}
135136

136137
proto::OpDesc *OpDesc::Proto() {

paddle/framework/op_desc.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,21 @@ namespace framework {
2525

2626
class BlockDesc;
2727
class ProgramDesc;
28-
2928
class OpDesc {
3029
public:
3130
OpDesc() {}
3231

3332
OpDesc(const std::string &type, const VariableNameMap &inputs,
3433
const VariableNameMap &outputs, const AttributeMap &attrs);
3534

36-
OpDesc(const proto::OpDesc &desc, ProgramDesc *prog);
35+
OpDesc(const proto::OpDesc &desc, ProgramDesc *prog, BlockDesc *block);
36+
37+
explicit OpDesc(BlockDesc *block) : block_(block) {}
38+
39+
OpDesc(const OpDesc &other, BlockDesc *block) {
40+
*this = other;
41+
block_ = block;
42+
}
3743

3844
void CopyFrom(const OpDesc &op_desc);
3945

@@ -117,6 +123,10 @@ class OpDesc {
117123

118124
void Flush();
119125

126+
BlockDesc *Block() { return this->block_; }
127+
128+
void SetBlock(BlockDesc *block) { this->block_ = block; }
129+
120130
private:
121131
template <typename MapType>
122132
static std::vector<typename MapType::key_type> MapKeys(const MapType &map) {
@@ -129,6 +139,7 @@ class OpDesc {
129139
}
130140

131141
proto::OpDesc desc_;
142+
BlockDesc *block_; // not_own
132143
// input arg name => input variable names
133144
VariableNameMap inputs_;
134145
// output arg name => output variable names

paddle/framework/var_desc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class VarDesc {
6666

6767
std::string Name() const { return desc_.name(); }
6868

69+
void SetName(std::string name) { desc_.set_name(name); }
70+
6971
void SetShape(const std::vector<int64_t> &dims);
7072

7173
void SetDataType(proto::DataType data_type);

paddle/operators/iou_similarity_op.cc

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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/iou_similarity_op.h"
16+
17+
namespace paddle {
18+
namespace operators {
19+
20+
class IOUSimilarityOp : public framework::OperatorWithKernel {
21+
public:
22+
using framework::OperatorWithKernel::OperatorWithKernel;
23+
24+
protected:
25+
void InferShape(framework::InferShapeContext *ctx) const override {
26+
PADDLE_ENFORCE(ctx->HasInput("X"),
27+
"Input(X) of IOUSimilarityOp should not be null.");
28+
PADDLE_ENFORCE(ctx->HasInput("Y"),
29+
"Input(Y) of IOUSimilarityOp should not be null.");
30+
auto x_dims = ctx->GetInputDim("X");
31+
auto y_dims = ctx->GetInputDim("Y");
32+
33+
PADDLE_ENFORCE_EQ(x_dims.size(), 2UL, "The rank of Input(X) must be 2.");
34+
PADDLE_ENFORCE_EQ(x_dims[1], 4UL, "The shape of X is [N, 4]");
35+
PADDLE_ENFORCE_EQ(y_dims.size(), 2UL, "The rank of Input(Y) must be 2.");
36+
PADDLE_ENFORCE_EQ(y_dims[1], 4UL, "The shape of Y is [M, 4]");
37+
38+
ctx->ShareLoD("X", /*->*/ "Out");
39+
ctx->SetOutputDim("Out", framework::make_ddim({x_dims[0], y_dims[0]}));
40+
}
41+
};
42+
43+
class IOUSimilarityOpMaker : public framework::OpProtoAndCheckerMaker {
44+
public:
45+
IOUSimilarityOpMaker(OpProto *proto, OpAttrChecker *op_checker)
46+
: OpProtoAndCheckerMaker(proto, op_checker) {
47+
AddInput("X",
48+
"(LoDTensor, default LoDTensor<float>) "
49+
"Box list X is a 2-D LoDTensor with shape [N, 4] holds N boxes, "
50+
"each box is represented as [xmin, ymin, xmax, ymax], "
51+
"the shape of X is [N, 4]. [xmin, ymin] is the left top "
52+
"coordinate of the box if the input is image feature map, they "
53+
"are close to the origin of the coordinate system. "
54+
"[xmax, ymax] is the right bottom coordinate of the box. "
55+
"This tensor can contain LoD information to represent a batch "
56+
"of inputs. One instance of this batch can contain different "
57+
"numbers of entities.");
58+
AddInput("Y",
59+
"(Tensor, default Tensor<float>) "
60+
"Box list Y holds M boxes, each box is represented as "
61+
"[xmin, ymin, xmax, ymax], the shape of X is [N, 4]. "
62+
"[xmin, ymin] is the left top coordinate of the box if the "
63+
"input is image feature map, and [xmax, ymax] is the right "
64+
"bottom coordinate of the box.");
65+
66+
AddOutput("Out",
67+
"(LoDTensor, the lod is same as input X) The output of "
68+
"iou_similarity op, a tensor with shape [N, M] "
69+
"representing pairwise iou scores.");
70+
71+
AddComment(R"DOC(
72+
IOU Similarity Operator.
73+
Computes intersection-over-union (IOU) between two box lists.
74+
Box list 'X' should be a LoDTensor and 'Y' is a common Tensor,
75+
boxes in 'Y' are shared by all instance of the batched inputs of X.
76+
Given two boxes A and B, the calculation of IOU is as follows:
77+
78+
$$
79+
IOU(A, B) =
80+
\frac{area(A\cap B)}{area(A)+area(B)-area(A\cap B)}
81+
$$
82+
83+
)DOC");
84+
}
85+
};
86+
} // namespace operators
87+
} // namespace paddle
88+
89+
namespace ops = paddle::operators;
90+
REGISTER_OP_WITHOUT_GRADIENT(iou_similarity, ops::IOUSimilarityOp,
91+
ops::IOUSimilarityOpMaker);
92+
93+
REGISTER_OP_CPU_KERNEL(
94+
iou_similarity,
95+
ops::IOUSimilarityKernel<paddle::platform::CPUDeviceContext, float>,
96+
ops::IOUSimilarityKernel<paddle::platform::CPUDeviceContext, double>);

paddle/operators/iou_similarity_op.cu

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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/iou_similarity_op.h"
16+
17+
namespace ops = paddle::operators;
18+
REGISTER_OP_CUDA_KERNEL(
19+
iou_similarity,
20+
ops::IOUSimilarityKernel<paddle::platform::CUDADeviceContext, float>,
21+
ops::IOUSimilarityKernel<paddle::platform::CUDADeviceContext, double>);

paddle/operators/iou_similarity_op.h

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
#pragma once
16+
#include "paddle/framework/op_registry.h"
17+
#include "paddle/platform/for_range.h"
18+
19+
template <typename T>
20+
inline HOSTDEVICE T IOUSimilarity(T xmin1, T ymin1, T xmax1, T ymax1, T xmin2,
21+
T ymin2, T xmax2, T ymax2) {
22+
constexpr T zero = static_cast<T>(0);
23+
T area1 = (ymax1 - ymin1) * (xmax1 - xmin1);
24+
T area2 = (ymax2 - ymin2) * (xmax2 - xmin2);
25+
T inter_xmax = xmax1 > xmax2 ? xmax2 : xmax1;
26+
T inter_ymax = ymax1 > ymax2 ? ymax2 : ymax1;
27+
T inter_xmin = xmin1 > xmin2 ? xmin1 : xmin2;
28+
T inter_ymin = ymin1 > ymin2 ? ymin1 : ymin2;
29+
T inter_height = inter_ymax - inter_ymin;
30+
T inter_width = inter_xmax - inter_xmin;
31+
inter_height = inter_height > zero ? inter_height : zero;
32+
inter_width = inter_width > zero ? inter_width : zero;
33+
T inter_area = inter_width * inter_height;
34+
T union_area = area1 + area2 - inter_area;
35+
T sim_score = inter_area / union_area;
36+
return sim_score;
37+
}
38+
39+
template <typename T>
40+
struct IOUSimilarityFunctor {
41+
IOUSimilarityFunctor(const T* x, const T* y, T* z, int cols)
42+
: x_(x), y_(y), z_(z), cols_(static_cast<size_t>(cols)) {}
43+
44+
inline HOSTDEVICE void operator()(size_t row_id) const {
45+
T x_min1 = x_[row_id * 4];
46+
T y_min1 = x_[row_id * 4 + 1];
47+
T x_max1 = x_[row_id * 4 + 2];
48+
T y_max1 = x_[row_id * 4 + 3];
49+
for (size_t i = 0; i < cols_; ++i) {
50+
T x_min2 = y_[i * 4];
51+
T y_min2 = y_[i * 4 + 1];
52+
T x_max2 = y_[i * 4 + 2];
53+
T y_max2 = y_[i * 4 + 3];
54+
55+
T sim = IOUSimilarity(x_min1, y_min1, x_max1, y_max1, x_min2, y_min2,
56+
x_max2, y_max2);
57+
58+
z_[row_id * cols_ + i] = sim;
59+
}
60+
}
61+
const T* x_;
62+
const T* y_;
63+
T* z_;
64+
const size_t cols_;
65+
};
66+
67+
namespace paddle {
68+
namespace operators {
69+
70+
template <typename DeviceContext, typename T>
71+
class IOUSimilarityKernel : public framework::OpKernel<T> {
72+
public:
73+
void Compute(const framework::ExecutionContext& ctx) const override {
74+
const framework::LoDTensor* in_x = ctx.Input<framework::LoDTensor>("X");
75+
const framework::Tensor* in_y = ctx.Input<framework::Tensor>("Y");
76+
framework::LoDTensor* out = ctx.Output<framework::LoDTensor>("Out");
77+
78+
int x_n = in_x->dims()[0];
79+
int y_n = in_y->dims()[0];
80+
IOUSimilarityFunctor<T> functor(in_x->data<T>(), in_y->data<T>(),
81+
out->mutable_data<T>(ctx.GetPlace()), y_n);
82+
83+
platform::ForRange<DeviceContext> for_range(
84+
static_cast<const DeviceContext&>(ctx.device_context()), x_n);
85+
for_range(functor);
86+
}
87+
}; // namespace operators
88+
89+
} // namespace operators
90+
} // namespace paddle

paddle/operators/nce_op.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ class NCEOpMaker : public framework::OpProtoAndCheckerMaker {
124124
"This attribute only be used in unitest. Classes "
125125
"in this list wiil be used as negative classes "
126126
"for every samples. Under normal conditions, "
127-
"user should avoid setting this attribute.");
127+
"user should avoid setting this attribute.")
128+
.SetDefault({});
128129
AddComment(R"DOC(
129130
Compute and return the noise-contrastive estimation training loss.
130131
See [Noise-contrastive estimation: A new estimation principle for unnormalized statistical models](http://www.jmlr.org/proceedings/papers/v9/gutmann10a/gutmann10a.pdf).

paddle/operators/nce_op.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ class NCEGradKernel : public framework::OpKernel<T> {
197197
// get d_x
198198
auto d_x = context.Output<Tensor>(framework::GradVarName("Input"));
199199
if (d_x != nullptr) {
200-
d_x->mutable_data<T>(context.GetPlace());
200+
auto* d_x_data = d_x->mutable_data<T>(context.GetPlace());
201+
std::fill(d_x_data, d_x_data + d_x->numel(), 0.0);
201202
auto d_x_matrix = EigenMatrix<T>::From(*d_x);
202203
auto w_matrix = EigenMatrix<T>::From(*(context.Input<Tensor>("Weight")));
203204
for (int64_t i = 0; i < sample_labels->numel(); ++i) {

0 commit comments

Comments
 (0)