Skip to content

Commit 29c2582

Browse files
author
wanghaox
committed
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into sub_sequence_op
2 parents 8e7c8bb + 4adc8a7 commit 29c2582

File tree

171 files changed

+689
-287
lines changed

Some content is hidden

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

171 files changed

+689
-287
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ third_party/
2121
cmake-build-*
2222

2323
# generated while compiling
24-
python/paddle/v2/framework/core.so
24+
python/paddle/v2/fluid/core.so
2525
paddle/pybind/pybind.h
2626
CMakeFiles
2727
cmake_install.cmake

paddle/operators/beam_search_decode_op.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class BeamSearchDecodeOp : public framework::OperatorBase {
2727
void Run(const framework::Scope& scope,
2828
const platform::DeviceContext& dev_ctx) const override {
2929
framework::ExecutionContext ctx(*this, scope, dev_ctx);
30+
3031
const LoDTensorArray* ids = ctx.Input<LoDTensorArray>("Ids");
3132
const LoDTensorArray* scores = ctx.Input<LoDTensorArray>("Scores");
3233
const size_t step_num = ids->size();

paddle/operators/l1_norm_op.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class L1NormKernel : public framework::OpKernel<T> {
2929
Out->mutable_data<T>(context.GetPlace());
3030

3131
auto x = framework::EigenVector<T>::Flatten(*X);
32-
auto out = framework::EigenVector<T>::Flatten(*Out);
32+
auto out = framework::EigenScalar<T>::From(*Out);
3333
auto place = context.GetEigenDevice<Place>();
3434

3535
out.device(place) = x.abs().sum();

paddle/operators/lod_reset_op.cc

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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/lod_reset_op.h"
16+
17+
namespace paddle {
18+
namespace operators {
19+
20+
class LoDResetOp : public framework::OperatorWithKernel {
21+
public:
22+
using framework::OperatorWithKernel::OperatorWithKernel;
23+
24+
void InferShape(framework::InferShapeContext *ctx) const override {
25+
// input check
26+
PADDLE_ENFORCE(ctx->HasInput("X"),
27+
"Input(X) of LoDResetOp should not be null.");
28+
PADDLE_ENFORCE(ctx->HasOutput("Out"),
29+
"Output(Out) of LoDResetOp should not be null.");
30+
// If target LoD is not set form Input(), then it must be set from Attr().
31+
if (!ctx->HasInput("TargetLoD")) {
32+
auto level0 = ctx->Attrs().Get<std::vector<int>>("target_lod");
33+
PADDLE_ENFORCE(level0.size() > 1,
34+
"Target LoD is not found, should be set to be a valid one "
35+
"through Input() or Attr().");
36+
}
37+
ctx->SetOutputDim("Out", ctx->GetInputDim("X"));
38+
}
39+
40+
protected:
41+
framework::OpKernelType GetKernelType(
42+
const framework::ExecutionContext &ctx) const override {
43+
return framework::OpKernelType(
44+
framework::ToDataType(ctx.Input<framework::LoDTensor>("X")->type()),
45+
ctx.device_context());
46+
}
47+
};
48+
49+
class LoDResetOpMaker : public framework::OpProtoAndCheckerMaker {
50+
public:
51+
LoDResetOpMaker(framework::OpProto *proto,
52+
framework::OpAttrChecker *op_checker)
53+
: OpProtoAndCheckerMaker(proto, op_checker) {
54+
AddInput("X", "(LoDTensor) The input tensor of lod_reset operator.");
55+
AddInput("TargetLoD",
56+
"(Tensor, optional) The target level 0 LoD from Input().")
57+
.AsDispensable();
58+
AddOutput("Out", "(LoDTensor) The output tensor of lod_reset operator.");
59+
AddAttr<std::vector<int>>("target_lod",
60+
"The target level 0 LoD from Attr().")
61+
.SetDefault(std::vector<int>{});
62+
AddComment(R"DOC(LoDReset operator
63+
64+
Reset LoD of Input(X) into a new one specified by Input(TargetLoD) or
65+
Attr(target_lod), or set LoD for Input(X) if it doesn't have one.
66+
Currently the lod_reset operator only supports the reset of level 0 LoD.
67+
At least one of Input(TargetLoD) and Attr(target_lod) must be set,
68+
and if both of them are set, Input(TargetLoD) will be chosen as the
69+
target LoD.
70+
71+
An example:
72+
Given a float LoDTensor X with shape (6, 1), its transpose form represents
73+
74+
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
75+
76+
with LoD = [[0, 2, 5, 6]] and the three (transposed) sequences look like
77+
78+
[1.0, 2.0], [3.0, 4.0, 5.0], [6.0].
79+
80+
If target LoD = [0, 4, 6], the lod_reset operator will reset the LoD and
81+
the sequences that the LoDTensor Output(Out) contains becomes:
82+
83+
[1.0, 2.0, 3.0, 4.0], [5.0, 6.0].
84+
85+
)DOC");
86+
}
87+
};
88+
89+
class LoDResetGradOp : public framework::OperatorWithKernel {
90+
public:
91+
using framework::OperatorWithKernel::OperatorWithKernel;
92+
93+
void InferShape(framework::InferShapeContext *ctx) const override {
94+
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) shouldn't be null.");
95+
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
96+
"Input(Out@GRAD) shouldn't be null.");
97+
ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
98+
}
99+
100+
protected:
101+
framework::OpKernelType GetKernelType(
102+
const framework::ExecutionContext &ctx) const override {
103+
return framework::OpKernelType(
104+
framework::ToDataType(ctx.Input<framework::LoDTensor>("X")->type()),
105+
ctx.device_context());
106+
}
107+
};
108+
109+
} // namespace operators
110+
} // namespace paddle
111+
112+
namespace ops = paddle::operators;
113+
REGISTER_OP(lod_reset, ops::LoDResetOp, ops::LoDResetOpMaker, lod_reset_grad,
114+
ops::LoDResetGradOp);
115+
REGISTER_OP_CPU_KERNEL(lod_reset,
116+
ops::LoDResetKernel<paddle::platform::CPUPlace, float>,
117+
ops::LoDResetKernel<paddle::platform::CPUPlace, double>);
118+
REGISTER_OP_CPU_KERNEL(
119+
lod_reset_grad, ops::LoDResetGradKernel<paddle::platform::CPUPlace, float>,
120+
ops::LoDResetGradKernel<paddle::platform::CPUPlace, double>);

paddle/operators/lod_reset_op.cu

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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/lod_reset_op.h"
16+
17+
namespace ops = paddle::operators;
18+
19+
REGISTER_OP_GPU_KERNEL(lod_reset,
20+
ops::LoDResetKernel<paddle::platform::GPUPlace, float>,
21+
ops::LoDResetKernel<paddle::platform::GPUPlace, double>);
22+
REGISTER_OP_GPU_KERNEL(
23+
lod_reset_grad, ops::LoDResetGradKernel<paddle::platform::GPUPlace, float>,
24+
ops::LoDResetGradKernel<paddle::platform::GPUPlace, double>);

paddle/operators/lod_reset_op.h

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
17+
#include "paddle/framework/eigen.h"
18+
#include "paddle/framework/op_registry.h"
19+
20+
namespace paddle {
21+
namespace operators {
22+
23+
template <typename Place, typename T>
24+
class LoDResetKernel : public framework::OpKernel<T> {
25+
public:
26+
void Compute(const framework::ExecutionContext& ctx) const {
27+
auto* out = ctx.Output<framework::LoDTensor>("Out");
28+
auto* in = ctx.Input<framework::LoDTensor>("X");
29+
auto* lod_t = ctx.Input<framework::Tensor>("TargetLoD");
30+
31+
std::vector<int> level0;
32+
if (lod_t) {
33+
auto* lod = lod_t->data<int>();
34+
if (platform::is_gpu_place(ctx.GetPlace())) {
35+
framework::Tensor lod_cpu;
36+
lod_cpu.CopyFrom(*lod_t, platform::CPUPlace(), ctx.device_context());
37+
lod = lod_cpu.data<int>();
38+
}
39+
level0 = std::vector<int>(lod, lod + lod_t->numel());
40+
} else {
41+
level0 = ctx.Attr<std::vector<int>>("target_lod");
42+
}
43+
44+
PADDLE_ENFORCE(level0.size() > 1UL,
45+
"The size of target LoD should be greater than 1.");
46+
PADDLE_ENFORCE(level0[0] == 0,
47+
"Target LoD should be a vector starting from 0.");
48+
PADDLE_ENFORCE(level0.back() == in->dims()[0],
49+
"Target LoD should be a vector end with the "
50+
"first dimension of Input(X).");
51+
for (size_t i = 0; i < level0.size() - 1; ++i) {
52+
PADDLE_ENFORCE(level0[i + 1] > level0[i],
53+
"Target LoD should be an ascending vector.");
54+
}
55+
56+
out->ShareDataWith(*in);
57+
// cast level0 to size_t
58+
std::vector<size_t> ulevel0(level0.size(), 0);
59+
std::transform(level0.begin(), level0.end(), ulevel0.begin(),
60+
[](int a) { return static_cast<size_t>(a); });
61+
framework::LoD target_lod;
62+
target_lod.push_back(ulevel0);
63+
out->set_lod(target_lod);
64+
}
65+
};
66+
67+
template <typename Place, typename T>
68+
class LoDResetGradKernel : public framework::OpKernel<T> {
69+
public:
70+
void Compute(const framework::ExecutionContext& ctx) const {
71+
auto* d_out = ctx.Input<framework::Tensor>(framework::GradVarName("Out"));
72+
auto* d_x = ctx.Output<framework::Tensor>(framework::GradVarName("X"));
73+
74+
d_x->ShareDataWith(*d_out);
75+
}
76+
};
77+
} // namespace operators
78+
} // namespace paddle

paddle/operators/squared_l2_norm_op.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class SquaredL2NormKernel : public framework::OpKernel<T> {
2929
Out->mutable_data<T>(context.GetPlace());
3030

3131
auto x = framework::EigenVector<T>::Flatten(*X);
32-
auto out = framework::EigenVector<T>::Flatten(*Out);
32+
auto out = framework::EigenScalar<T>::From(*Out);
3333
auto place = context.GetEigenDevice<Place>();
3434

3535
out.device(place) = x.square().sum();

paddle/pybind/pybind.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ limitations under the License. */
4242
#include "paddle/platform/gpu_info.h"
4343
#endif
4444

45+
// disable auto conversion to list in Python
46+
PYBIND11_MAKE_OPAQUE(paddle::framework::LoDTensorArray);
47+
4548
namespace paddle {
4649
namespace pybind {
4750
static size_t UniqueIntegerGenerator(const std::string &prefix) {

python/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in
3737
${CMAKE_CURRENT_BINARY_DIR}/setup.py)
3838

3939

40-
add_custom_command(OUTPUT ${PADDLE_SOURCE_DIR}/python/paddle/v2/framework/core.so
41-
COMMAND cmake -E copy $<TARGET_FILE:paddle_pybind> ${PADDLE_SOURCE_DIR}/python/paddle/v2/framework/core.so
40+
add_custom_command(OUTPUT ${PADDLE_SOURCE_DIR}/python/paddle/v2/fluid/core.so
41+
COMMAND cmake -E copy $<TARGET_FILE:paddle_pybind> ${PADDLE_SOURCE_DIR}/python/paddle/v2/fluid/core.so
4242
DEPENDS paddle_pybind)
43-
add_custom_target(copy_paddle_pybind ALL DEPENDS ${PADDLE_SOURCE_DIR}/python/paddle/v2/framework/core.so)
43+
add_custom_target(copy_paddle_pybind ALL DEPENDS ${PADDLE_SOURCE_DIR}/python/paddle/v2/fluid/core.so)
4444

4545

4646
add_custom_command(OUTPUT ${PADDLE_PYTHON_BUILD_DIR}/.timestamp
@@ -66,7 +66,7 @@ if (WITH_TESTING)
6666
add_subdirectory(paddle/v2/tests)
6767
add_subdirectory(paddle/v2/reader/tests)
6868
add_subdirectory(paddle/v2/plot/tests)
69-
add_subdirectory(paddle/v2/framework/tests)
69+
add_subdirectory(paddle/v2/fluid/tests)
7070
endif()
7171
endif()
7272
install(DIRECTORY ${PADDLE_PYTHON_PACKAGE_DIR}

0 commit comments

Comments
 (0)