Skip to content

Commit a37f6ad

Browse files
committed
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into dev_elementwise_max_min
2 parents f1a8897 + a9899db commit a37f6ad

File tree

103 files changed

+358
-5559
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+358
-5559
lines changed

doc/api/v2/fluid/layers.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,12 @@ split
364364
.. autofunction:: paddle.v2.fluid.layers.split
365365
:noindex:
366366

367+
368+
matmul
369+
------
370+
.. autofunction:: paddle.v2.fluid.layers.matmul
371+
:noindex:
372+
367373
logsigmoid
368374
----------
369375
.. autofunction:: paddle.v2.fluid.layers.logsigmoid

doc/api/v2/fluid/nets.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@ glu
2525
.. autofunction:: paddle.v2.fluid.nets.glu
2626
:noindex:
2727

28+
29+
dot_product_attention
30+
---------------------
31+
.. autofunction:: paddle.v2.fluid.nets.dot_product_attention
32+
:noindex:
33+

doc/howto/usage/capi/organization_of_the_inputs_cn.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
### 基本使用概念
2121

22-
- 在PaddlePaddle内部,神经网络中一个计算层的输入/输出被组织为一个 `Argument` 结构体,如果神经网络有多个输入或者多个输入,每一个输入/输入都会对应有自己的`Argument`
22+
- 在PaddlePaddle内部,神经网络中一个计算层的输入/输出被组织为一个 `Argument` 结构体,如果神经网络有多个输入或者多个输出,每一个输入/输出都会对应有自己的`Argument`
2323
- `Argument` 并不真正“存储”数据,而是将输入/输出信息有机地组织在一起。
2424
-`Argument`内部由`IVector`(对应着上文提到的一维整型数组)和`Matrix`(对应着上文提到的二维浮点型矩阵)来实际存储数据;由 `Sequence Start Positions` (下文详细解释) 来描述输入/输出的序列信息。
2525

paddle/framework/lod_tensor.cc

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,65 @@ bool operator==(const LoD &a, const LoD &b) {
135135
return true;
136136
}
137137

138+
bool CheckLoD(const LoD &in, int tensor_height) {
139+
if (in.empty()) return true;
140+
for (const auto &level : in) {
141+
// check: there should be more than 2 offsets existing in each level.
142+
if (level.size() < 2) return false;
143+
// check: the first offset(the begin offset) of each level should be 0.
144+
if (level.front() != 0) return false;
145+
// check: all the offsets in a level should be ascending(no same items
146+
// allows).
147+
if (!std::is_sorted(level.begin(), level.begin(), [](size_t a, size_t b) {
148+
if (a < b) return true;
149+
return false;
150+
})) {
151+
LOG(INFO) << "ascending error";
152+
return false;
153+
}
154+
}
155+
// check: the lowest level's last offset should equals `tensor_height` if
156+
// tensor_height>0.
157+
if (tensor_height > 0 && (size_t)tensor_height != in.back().back())
158+
return false;
159+
160+
// check: the higher level's last offset should equals the lower level's
161+
// size-1.
162+
// NOTE LoD store the levels from top to bottom, so the higher level goes
163+
// first.
164+
for (size_t level = 0; level < in.size() - 1; level++) {
165+
if (in[level].back() != in[level + 1].size() - 1) return false;
166+
}
167+
return true;
168+
}
169+
170+
bool CheckAbsLoD(const LoD &in, int tensor_height) {
171+
if (in.empty()) return true;
172+
for (const auto &level : in) {
173+
// check: all the offsets in a level should be ascending(no same items
174+
// allows).
175+
if (!std::is_sorted(level.begin(), level.begin(), [](size_t a, size_t b) {
176+
if (a < b) return true;
177+
return false;
178+
})) {
179+
return false;
180+
}
181+
182+
// check: there should be more than 2 offsets existing in each level.
183+
if (level.size() < 2) return false;
184+
185+
// check: the first offset of each level should be 0, and the last should be
186+
// the same(the height of underlying tensor).
187+
if (level.front() != 0) return false;
188+
if (tensor_height < 0) {
189+
tensor_height = level.back();
190+
} else if ((size_t)tensor_height != level.back()) {
191+
return false;
192+
}
193+
}
194+
return true;
195+
}
196+
138197
using LoDAndOffset = std::pair<LoD, std::pair<size_t, size_t>>;
139198
LoDAndOffset GetSubLoDAndAbsoluteOffset(const LoD &lod, size_t start_idx,
140199
size_t end_idx, size_t start_level) {

paddle/framework/lod_tensor.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,38 @@ LoD ToAbsOffset(const LoD& in);
7171

7272
bool operator==(const LoD& a, const LoD& b);
7373

74+
/*
75+
* Check whether this lod's format is valid.
76+
*
77+
* ATTENTION:
78+
* - Empty lod is treated as valid.
79+
*
80+
* It will check two things:
81+
*
82+
* 1. all the offsets in a level should be ascending(no same items allows).
83+
* 2. there should be more than 2 offsets existing in each level.
84+
* 3. the higher level's last offset should equals the lower level's size-1.
85+
* 4. the first offset(the begin offset) of each level should be 0.
86+
* 5. the lowest level's last offset should equals `tensor_height` if
87+
* tensor_height>0.
88+
*/
89+
90+
bool CheckLoD(const LoD& in, int tensor_height = -1);
91+
/*
92+
* Check whether this absolute lod's format is valid.
93+
*
94+
* ATTENTION:
95+
* - Empty lod is treated as valid.
96+
*
97+
* It will check two things:
98+
* 1. all the offsets in a level should be ascending(no same items allows)
99+
* 2. there should be more than 2 offsets existing in each level.
100+
* 3. the first offset of each level should be 0, and the last should be the
101+
* same(the height of underlying tensor) or `tensor_height` if
102+
* tensor_height>0.
103+
*/
104+
bool CheckAbsLoD(const LoD& in, int tensor_height = -1);
105+
74106
/*
75107
* LoDTensor (Level of details Tensor)
76108
* see https://en.wikipedia.org/wiki/Level_of_details for reference.

paddle/framework/lod_tensor_test.cc

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,6 @@ namespace framework {
3737

3838
const int kLodTensorSize = 20 * 128;
3939

40-
class LoDTensorTester : public ::testing::Test {
41-
public:
42-
virtual void SetUp() override {
43-
// tensor's batch_size: 30
44-
// 3 levels
45-
// 0 10 20
46-
// 0 5 10 15 20
47-
// 0 2 5 7 10 12 15 20
48-
LoD lod;
49-
lod.push_back(std::vector<size_t>{0, 2, 3});
50-
lod.push_back(std::vector<size_t>{0, 2, 5, 8});
51-
lod.push_back(std::vector<size_t>{0, 2, 5, 7, 10, 12, 15, 17, 20});
52-
53-
ASSERT_EQ(lod.size(), 3UL);
54-
55-
lod_tensor_.Resize({20 /*batch size*/, 128 /*dim*/});
56-
// malloc memory
57-
float* dst_ptr = lod_tensor_.mutable_data<float>(place);
58-
for (int i = 0; i < kLodTensorSize; ++i) {
59-
dst_ptr[i] = i;
60-
}
61-
62-
lod_tensor_.set_lod(lod);
63-
}
64-
65-
protected:
66-
platform::CPUPlace place;
67-
LoDTensor lod_tensor_;
68-
};
69-
7040
TEST(LodExpand, test) {
7141
LoD lod{{0, 2}};
7242
LoDTensor tensor;
@@ -144,5 +114,53 @@ TEST(LoD, ToAbsOffset) {
144114
EXPECT_EQ(abs_lod, expected);
145115
}
146116

117+
TEST(LoD, CheckLoD) {
118+
LoD relative_lod;
119+
relative_lod.push_back(std::vector<size_t>({0, 2}));
120+
relative_lod.push_back(std::vector<size_t>({0, 1, 3}));
121+
relative_lod.push_back(std::vector<size_t>({0, 2, 4, 5}));
122+
123+
// check compatible
124+
ASSERT_TRUE(CheckLoD(relative_lod));
125+
relative_lod[1].back()++;
126+
ASSERT_FALSE(CheckLoD(relative_lod));
127+
relative_lod[1].back()--; // recover it
128+
129+
// check empty
130+
LoD empty_lod;
131+
ASSERT_TRUE(CheckLoD(empty_lod));
132+
133+
// check less than 2 offsets in a level
134+
LoD some_lod0;
135+
some_lod0.push_back(std::vector<size_t>({0}));
136+
ASSERT_FALSE(CheckLoD(some_lod0));
137+
138+
// check with underlying tensor storage.
139+
ASSERT_TRUE(CheckLoD(relative_lod, 5));
140+
ASSERT_FALSE(CheckLoD(relative_lod, 9));
141+
}
142+
143+
TEST(LoD, CheckAbsLoD) {
144+
LoD relative_lod;
145+
relative_lod.push_back(std::vector<size_t>({0, 2}));
146+
relative_lod.push_back(std::vector<size_t>({0, 1, 3}));
147+
relative_lod.push_back(std::vector<size_t>({0, 2, 4, 5}));
148+
149+
auto abs_lod = ToAbsOffset(relative_lod);
150+
151+
ASSERT_TRUE(CheckAbsLoD(abs_lod));
152+
153+
// check less than 2 offsets in a level.
154+
155+
// check the last item should be compatible with tensor height.
156+
abs_lod.back().back()++;
157+
ASSERT_FALSE(CheckAbsLoD(abs_lod));
158+
abs_lod.back().back()--; // restore
159+
160+
// check less than 2 offsets in a lod.
161+
LoD abs_lod0;
162+
abs_lod0.push_back(std::vector<size_t>({0}));
163+
ASSERT_FALSE(CheckAbsLoD(abs_lod0));
164+
}
147165
} // namespace framework
148166
} // namespace paddle

paddle/framework/op_registry.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,16 @@ class OpKernelRegistrar : public Registrar {
177177
/**
178178
* Macro to register OperatorKernel.
179179
*/
180-
#define REGISTER_OP_KERNEL(op_type, DEVICE_TYPE, place_class, ...) \
181-
STATIC_ASSERT_GLOBAL_NAMESPACE( \
182-
__reg_op_kernel_##op_type##_##DEVICE_TYPE##__, \
183-
"REGISTER_OP_KERNEL must be called in global namespace"); \
184-
static ::paddle::framework::OpKernelRegistrar<place_class, __VA_ARGS__> \
185-
__op_kernel_registrar_##op_type##_##DEVICE_TYPE##__(#op_type, \
186-
#DEVICE_TYPE); \
187-
int TouchOpKernelRegistrar_##op_type##_##DEVICE_TYPE() { \
188-
__op_kernel_registrar_##op_type##_##DEVICE_TYPE##__.Touch(); \
189-
return 0; \
180+
#define REGISTER_OP_KERNEL(op_type, LIBRARY_TYPE, place_class, ...) \
181+
STATIC_ASSERT_GLOBAL_NAMESPACE( \
182+
__reg_op_kernel_##op_type##_##LIBRARY_TYPE##__, \
183+
"REGISTER_OP_KERNEL must be called in global namespace"); \
184+
static ::paddle::framework::OpKernelRegistrar<place_class, __VA_ARGS__> \
185+
__op_kernel_registrar_##op_type##_##LIBRARY_TYPE##__(#op_type, \
186+
#LIBRARY_TYPE); \
187+
int TouchOpKernelRegistrar_##op_type##_##LIBRARY_TYPE() { \
188+
__op_kernel_registrar_##op_type##_##LIBRARY_TYPE##__.Touch(); \
189+
return 0; \
190190
}
191191

192192
#define REGISTER_OP_CUDA_KERNEL(op_type, ...) \
@@ -208,14 +208,14 @@ class OpKernelRegistrar : public Registrar {
208208
static int use_op_itself_##op_type##_ __attribute__((unused)) = \
209209
TouchOpRegistrar_##op_type()
210210

211-
#define USE_OP_DEVICE_KERNEL(op_type, DEVICE_TYPE) \
212-
STATIC_ASSERT_GLOBAL_NAMESPACE( \
213-
__use_op_kernel_##op_type##_##DEVICE_TYPE##__, \
214-
"USE_OP_DEVICE_KERNEL must be in global namespace"); \
215-
extern int TouchOpKernelRegistrar_##op_type##_##DEVICE_TYPE(); \
216-
static int use_op_kernel_##op_type##_##DEVICE_TYPE##_ \
217-
__attribute__((unused)) = \
218-
TouchOpKernelRegistrar_##op_type##_##DEVICE_TYPE()
211+
#define USE_OP_DEVICE_KERNEL(op_type, LIBRARY_TYPE) \
212+
STATIC_ASSERT_GLOBAL_NAMESPACE( \
213+
__use_op_kernel_##op_type##_##LIBRARY_TYPE##__, \
214+
"USE_OP_DEVICE_KERNEL must be in global namespace"); \
215+
extern int TouchOpKernelRegistrar_##op_type##_##LIBRARY_TYPE(); \
216+
static int use_op_kernel_##op_type##_##LIBRARY_TYPE##_ \
217+
__attribute__((unused)) = \
218+
TouchOpKernelRegistrar_##op_type##_##LIBRARY_TYPE()
219219

220220
// TODO(fengjiayi): The following macros
221221
// seems ugly, do we have better method?

paddle/gserver/layers/MKLDNNConcatLayer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void MKLDNNConcatLayer::reshape(
4343
channels_[0] = ic;
4444
oc = ic;
4545
for (size_t i = 1; i < inputLayers_.size(); i++) {
46-
int batchsize, height, witdh;
46+
int batchsize = 0, height = 0, witdh = 0;
4747
reshapeInput(batchsize, height, witdh, i);
4848
CHECK_EQ(bs, batchsize);
4949
CHECK_EQ(ih, height);
@@ -84,6 +84,7 @@ void MKLDNNConcatLayer::resetFwdBuffers(std::vector<MKLDNNMatrixPtr>& inputs,
8484
bool has8c = false, has16c = false, hasnc = false;
8585
for (size_t i = 0; i < inputs.size(); i++) {
8686
resetInValue(inputs[i], nullptr, i, channels_[i]);
87+
inputs[i]->downSpatial();
8788
CHECK(inputs[i]);
8889
auto dm = inputs[i]->getDims();
8990
// inputs format can be different, but ndims must equal

paddle/gserver/tests/img_conv_cudnn.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
1+
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
22
#
33
#Licensed under the Apache License, Version 2.0 (the "License");
44
#you may not use this file except in compliance with the License.
@@ -11,20 +11,6 @@
1111
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
#See the License for the specific language governing permissions and
1313
#limitations under the License.
14-
#edit-mode: -*- python -*-
15-
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
16-
#
17-
# Licensed under the Apache License, Version 2.0 (the "License");
18-
# you may not use this file except in compliance with the License.
19-
# You may obtain a copy of the License at
20-
#
21-
# http://www.apache.org/licenses/LICENSE-2.0
22-
#
23-
# Unless required by applicable law or agreed to in writing, software
24-
# distributed under the License is distributed on an "AS IS" BASIS,
25-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26-
# See the License for the specific language governing permissions and
27-
# limitations under the License.
2814

2915
from paddle.trainer_config_helpers import *
3016

paddle/gserver/tests/img_conv_exconv.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
1+
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
22
#
33
#Licensed under the Apache License, Version 2.0 (the "License");
44
#you may not use this file except in compliance with the License.
@@ -11,20 +11,6 @@
1111
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
#See the License for the specific language governing permissions and
1313
#limitations under the License.
14-
#edit-mode: -*- python -*-
15-
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
16-
#
17-
# Licensed under the Apache License, Version 2.0 (the "License");
18-
# you may not use this file except in compliance with the License.
19-
# You may obtain a copy of the License at
20-
#
21-
# http://www.apache.org/licenses/LICENSE-2.0
22-
#
23-
# Unless required by applicable law or agreed to in writing, software
24-
# distributed under the License is distributed on an "AS IS" BASIS,
25-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26-
# See the License for the specific language governing permissions and
27-
# limitations under the License.
2814

2915
from paddle.trainer_config_helpers import *
3016

0 commit comments

Comments
 (0)