Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
557c7ae3
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
557c7ae3
编写于
10月 12, 2017
作者:
C
chengduoZH
浏览文件
操作
浏览文件
下载
差异文件
remove conflict
上级
4aae1fff
d8830802
变更
58
展开全部
隐藏空白更改
内联
并排
Showing
58 changed file
with
2287 addition
and
638 deletion
+2287
-638
cmake/generic.cmake
cmake/generic.cmake
+48
-1
paddle/api/CMakeLists.txt
paddle/api/CMakeLists.txt
+1
-1
paddle/framework/CMakeLists.txt
paddle/framework/CMakeLists.txt
+5
-3
paddle/framework/backward.h
paddle/framework/backward.h
+2
-0
paddle/framework/executor.cc
paddle/framework/executor.cc
+0
-2
paddle/framework/op_desc.cc
paddle/framework/op_desc.cc
+9
-0
paddle/framework/op_desc.h
paddle/framework/op_desc.h
+2
-0
paddle/framework/operator.h
paddle/framework/operator.h
+9
-0
paddle/framework/tensor.h
paddle/framework/tensor.h
+11
-6
paddle/framework/tensor_array.cc
paddle/framework/tensor_array.cc
+10
-5
paddle/framework/tensor_impl.h
paddle/framework/tensor_impl.h
+35
-16
paddle/framework/tensor_test.cc
paddle/framework/tensor_test.cc
+27
-17
paddle/operators/CMakeLists.txt
paddle/operators/CMakeLists.txt
+4
-1
paddle/operators/activation_op.cc
paddle/operators/activation_op.cc
+21
-0
paddle/operators/activation_op.h
paddle/operators/activation_op.h
+28
-1
paddle/operators/conv2d_op.cc
paddle/operators/conv2d_op.cc
+73
-93
paddle/operators/conv2d_op.cu
paddle/operators/conv2d_op.cu
+1
-1
paddle/operators/conv2d_op.h
paddle/operators/conv2d_op.h
+32
-1
paddle/operators/conv3d_op.cc
paddle/operators/conv3d_op.cc
+1
-1
paddle/operators/conv_cudnn_op.cc
paddle/operators/conv_cudnn_op.cc
+47
-0
paddle/operators/conv_cudnn_op.cu
paddle/operators/conv_cudnn_op.cu
+277
-0
paddle/operators/decayed_adagrad_op.cc
paddle/operators/decayed_adagrad_op.cc
+96
-0
paddle/operators/decayed_adagrad_op.cu
paddle/operators/decayed_adagrad_op.cu
+21
-0
paddle/operators/decayed_adagrad_op.h
paddle/operators/decayed_adagrad_op.h
+56
-0
paddle/operators/feed_op.h
paddle/operators/feed_op.h
+1
-1
paddle/operators/fetch_op.h
paddle/operators/fetch_op.h
+2
-1
paddle/operators/margin_rank_loss_op.cc
paddle/operators/margin_rank_loss_op.cc
+124
-0
paddle/operators/margin_rank_loss_op.cu
paddle/operators/margin_rank_loss_op.cu
+24
-0
paddle/operators/margin_rank_loss_op.h
paddle/operators/margin_rank_loss_op.h
+98
-0
paddle/operators/math/CMakeLists.txt
paddle/operators/math/CMakeLists.txt
+4
-2
paddle/operators/math/im2col_test.cc
paddle/operators/math/im2col_test.cc
+17
-15
paddle/operators/math/math_function_test.cc
paddle/operators/math/math_function_test.cc
+18
-14
paddle/operators/math/vol2col_test.cc
paddle/operators/math/vol2col_test.cc
+4
-4
paddle/operators/multiplex_op.cu
paddle/operators/multiplex_op.cu
+4
-2
paddle/operators/pool_op.cc
paddle/operators/pool_op.cc
+167
-143
paddle/operators/pool_op.h
paddle/operators/pool_op.h
+28
-0
paddle/operators/pool_with_index_op.cc
paddle/operators/pool_with_index_op.cc
+39
-14
paddle/operators/recurrent_op.cc
paddle/operators/recurrent_op.cc
+3
-3
paddle/operators/reshape_op.h
paddle/operators/reshape_op.h
+2
-2
paddle/operators/rnn/recurrent_op_utils.cc
paddle/operators/rnn/recurrent_op_utils.cc
+2
-2
paddle/operators/rnn/recurrent_op_utils.h
paddle/operators/rnn/recurrent_op_utils.h
+1
-1
paddle/operators/sequence_concat_op.cc
paddle/operators/sequence_concat_op.cc
+129
-0
paddle/operators/sequence_concat_op.cu
paddle/operators/sequence_concat_op.cu
+25
-0
paddle/operators/sequence_concat_op.h
paddle/operators/sequence_concat_op.h
+155
-0
paddle/platform/cudnn_helper.h
paddle/platform/cudnn_helper.h
+31
-11
paddle/pybind/CMakeLists.txt
paddle/pybind/CMakeLists.txt
+1
-1
paddle/pybind/protobuf.cc
paddle/pybind/protobuf.cc
+7
-0
paddle/pybind/tensor_py.h
paddle/pybind/tensor_py.h
+14
-1
proto/CMakeLists.txt
proto/CMakeLists.txt
+7
-1
python/paddle/trainer_config_helpers/layers.py
python/paddle/trainer_config_helpers/layers.py
+211
-210
python/paddle/v2/framework/tests/test_activation_op.py
python/paddle/v2/framework/tests/test_activation_op.py
+21
-0
python/paddle/v2/framework/tests/test_conv2d_op.py
python/paddle/v2/framework/tests/test_conv2d_op.py
+79
-60
python/paddle/v2/framework/tests/test_conv3d_op.py
python/paddle/v2/framework/tests/test_conv3d_op.py
+28
-1
python/paddle/v2/framework/tests/test_decayed_adagrad_op.py
python/paddle/v2/framework/tests/test_decayed_adagrad_op.py
+71
-0
python/paddle/v2/framework/tests/test_margin_rank_loss_op.py
python/paddle/v2/framework/tests/test_margin_rank_loss_op.py
+39
-0
python/paddle/v2/framework/tests/test_program.py
python/paddle/v2/framework/tests/test_program.py
+30
-0
python/paddle/v2/framework/tests/test_protobuf_descs.py
python/paddle/v2/framework/tests/test_protobuf_descs.py
+6
-0
python/paddle/v2/framework/tests/test_seq_concat_op.py
python/paddle/v2/framework/tests/test_seq_concat_op.py
+79
-0
未找到文件。
cmake/generic.cmake
浏览文件 @
557c7ae3
...
...
@@ -389,13 +389,60 @@ function(go_test TARGET_NAME)
WORKING_DIRECTORY
${
CMAKE_CURRENT_SOURCE_DIR
}
)
endfunction
(
go_test
)
# Modification of standard 'protobuf_generate_cpp()' with protobuf-lite support
# Usage:
# paddle_protobuf_generate_cpp(<proto_srcs> <proto_hdrs> <proto_files>)
function
(
paddle_protobuf_generate_cpp SRCS HDRS
)
if
(
NOT ARGN
)
message
(
SEND_ERROR
"Error: paddle_protobuf_generate_cpp() called without any proto files"
)
return
()
endif
()
set
(
${
SRCS
}
)
set
(
${
HDRS
}
)
if
(
MOBILE_INFERENCE
)
set
(
EXTRA_FLAG
"lite:"
)
else
()
set
(
EXTRA_FLAG
""
)
endif
()
foreach
(
FIL
${
ARGN
}
)
get_filename_component
(
ABS_FIL
${
FIL
}
ABSOLUTE
)
get_filename_component
(
FIL_WE
${
FIL
}
NAME_WE
)
set
(
_protobuf_protoc_src
"
${
CMAKE_CURRENT_BINARY_DIR
}
/
${
FIL_WE
}
.pb.cc"
)
set
(
_protobuf_protoc_hdr
"
${
CMAKE_CURRENT_BINARY_DIR
}
/
${
FIL_WE
}
.pb.h"
)
list
(
APPEND
${
SRCS
}
"
${
_protobuf_protoc_src
}
"
)
list
(
APPEND
${
HDRS
}
"
${
_protobuf_protoc_hdr
}
"
)
add_custom_command
(
OUTPUT
"
${
_protobuf_protoc_src
}
"
"
${
_protobuf_protoc_hdr
}
"
COMMAND
${
CMAKE_COMMAND
}
-E make_directory
"
${
CMAKE_CURRENT_BINARY_DIR
}
"
COMMAND
${
PROTOBUF_PROTOC_EXECUTABLE
}
-I
${
CMAKE_CURRENT_SOURCE_DIR
}
--cpp_out
"
${
EXTRA_FLAG
}${
CMAKE_CURRENT_BINARY_DIR
}
"
${
ABS_FIL
}
DEPENDS
${
ABS_FIL
}
protoc
COMMENT
"Running C++ protocol buffer compiler on
${
FIL
}
"
VERBATIM
)
endforeach
()
set_source_files_properties
(
${${
SRCS
}}
${${
HDRS
}}
PROPERTIES GENERATED TRUE
)
set
(
${
SRCS
}
${${
SRCS
}}
PARENT_SCOPE
)
set
(
${
HDRS
}
${${
HDRS
}}
PARENT_SCOPE
)
endfunction
()
function
(
proto_library TARGET_NAME
)
set
(
oneValueArgs
""
)
set
(
multiValueArgs SRCS DEPS
)
cmake_parse_arguments
(
proto_library
"
${
options
}
"
"
${
oneValueArgs
}
"
"
${
multiValueArgs
}
"
${
ARGN
}
)
set
(
proto_srcs
)
set
(
proto_hdrs
)
protobuf_generate_cpp
(
proto_srcs proto_hdrs
${
proto_library_SRCS
}
)
p
addle_p
rotobuf_generate_cpp
(
proto_srcs proto_hdrs
${
proto_library_SRCS
}
)
cc_library
(
${
TARGET_NAME
}
SRCS
${
proto_srcs
}
DEPS
${
proto_library_DEPS
}
protobuf
)
endfunction
()
...
...
paddle/api/CMakeLists.txt
浏览文件 @
557c7ae3
...
...
@@ -26,7 +26,7 @@ FILE(GLOB PY_PADDLE_PYTHON_FILES ${PADDLE_SOURCE_DIR}/paddle/py_paddle/*.py)
SET_SOURCE_FILES_PROPERTIES
(
Paddle.i PROPERTIES CPLUSPLUS ON
)
SET
(
CMAKE_SWIG_OUTDIR
${
CMAKE_CURRENT_BINARY_DIR
}
)
SET
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-Wno-parentheses-equality -Wno-missing-field-initializers -Wno-self-assign"
)
SET
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-Wno-parentheses-equality -Wno-missing-field-initializers -Wno-self-assign
-ftls-model=global-dynamic
"
)
SET
(
SWIG_MODULE_swig_paddle_EXTRA_DEPS
paddle_parameter
...
...
paddle/framework/CMakeLists.txt
浏览文件 @
557c7ae3
...
...
@@ -42,11 +42,13 @@ add_custom_command(TARGET framework_py_proto POST_BUILD
cc_library
(
backward SRCS backward.cc DEPS net_op
)
cc_test
(
backward_test SRCS backward_test.cc DEPS backward recurrent_op device_context
)
cc_library
(
executor SRCS executor.cc DEPS op_registry device_context scope framework_proto backward
${
GLOB_OP_LIB
}
)
cc_library
(
executor SRCS executor.cc DEPS op_registry device_context scope framework_proto backward
)
set
(
EXECUTOR_TEST_OP elementwise_add_op gaussian_random_op feed_op fetch_op
mul_op sum_op squared_l2_distance_op fill_constant_op sgd_op
)
if
(
WITH_GPU
)
nv_test
(
executor_test SRCS executor_test.cc DEPS executor
)
nv_test
(
executor_test SRCS executor_test.cc DEPS executor
${
EXECUTOR_TEST_OP
}
)
else
()
cc_test
(
executor_test SRCS executor_test.cc DEPS executor
)
cc_test
(
executor_test SRCS executor_test.cc DEPS executor
${
EXECUTOR_TEST_OP
}
)
endif
()
cc_library
(
tensor_array SRCS tensor_array.cc DEPS lod_tensor
)
...
...
paddle/framework/backward.h
浏览文件 @
557c7ae3
...
...
@@ -27,6 +27,8 @@ extern std::unique_ptr<OperatorBase> Backward(
const
OperatorBase
&
forwardOp
,
const
std
::
unordered_set
<
std
::
string
>&
no_grad_vars
);
// TODO(jiayi): Add target as parameter and generate backward op
// according to target.
void
AppendBackward
(
ProgramDescBind
&
program_desc
,
const
std
::
unordered_set
<
std
::
string
>&
no_grad_vars
);
...
...
paddle/framework/executor.cc
浏览文件 @
557c7ae3
...
...
@@ -24,8 +24,6 @@ limitations under the License. */
#include "paddle/framework/op_registry.h"
#include "paddle/framework/scope.h"
#include <boost/range/adaptor/reversed.hpp>
namespace
paddle
{
namespace
framework
{
...
...
paddle/framework/op_desc.cc
浏览文件 @
557c7ae3
...
...
@@ -211,6 +211,15 @@ static InferShapeFuncMap &InferShapeFuncs() {
return
*
g_map
;
}
void
OpDescBind
::
CheckAttrs
()
{
PADDLE_ENFORCE
(
!
Type
().
empty
(),
"CheckAttr() can not be called before type is setted."
);
const
auto
*
checker
=
OpInfoMap
::
Instance
().
Get
(
Type
()).
Checker
();
PADDLE_ENFORCE_NOT_NULL
(
checker
,
"Operator
\"
%s
\"
has no registered checker."
,
Type
());
checker
->
Check
(
attrs_
);
}
void
OpDescBind
::
InferShape
(
const
BlockDescBind
&
block
)
const
{
auto
&
funcs
=
InferShapeFuncs
();
auto
it
=
funcs
.
find
(
this
->
Type
());
...
...
paddle/framework/op_desc.h
浏览文件 @
557c7ae3
...
...
@@ -100,6 +100,8 @@ class OpDescBind {
return
&
this
->
attrs_
;
}
void
CheckAttrs
();
void
InferShape
(
const
BlockDescBind
&
block
)
const
;
private:
...
...
paddle/framework/operator.h
浏览文件 @
557c7ae3
...
...
@@ -289,6 +289,15 @@ class ExecutionContext {
return
device_context_
;
}
#ifdef PADDLE_WITH_CUDA
const
platform
::
CUDADeviceContext
&
cuda_device_context
()
const
{
PADDLE_ENFORCE
(
platform
::
is_gpu_place
(
device_context_
.
GetPlace
()));
auto
cuda_ctx
=
reinterpret_cast
<
const
platform
::
CUDADeviceContext
*>
(
&
device_context_
);
return
*
cuda_ctx
;
}
#endif
private:
const
OperatorBase
&
op_
;
const
Scope
&
scope_
;
...
...
paddle/framework/tensor.h
浏览文件 @
557c7ae3
...
...
@@ -87,26 +87,31 @@ class Tensor {
/**
* @brief Copy the content of external tensor to a new place.
*
* @param[in] src The external tensor.
* @param[in] ctx The device context contains place where to store.
* @param[in] src The external tensor.
* @param[in] dst_place The dst place.
* @param[in] ctx The device context contains device resources.
*
* @note CopyFrom supports CPU <-> GPU, GPU <-> GPU.
*/
// TODO(qijun): https://github.com/PaddlePaddle/Paddle/issues/4647
// Remove `CopyFrom` and `CopyFromVector` from Tensor interface
// and make them global functions
template
<
typename
T
>
inline
void
CopyFrom
(
const
Tensor
&
src
,
const
platform
::
Place
&
dst_place
);
inline
void
CopyFrom
(
const
Tensor
&
src
,
const
platform
::
Place
&
dst_place
,
const
platform
::
DeviceContext
&
ctx
);
/**
* @brief Copy the content of an external vector to a tensor.
*
* @param[in] src
The external vect
or.
* @param[in] ctx
The device context contains place where to store
.
* @param[in] src
The external tens
or.
* @param[in] ctx
The device context contains device resources
.
*
* * @note CopyFromVector assumes that the tensor has been resized
* before invoking.
*/
template
<
typename
T
>
inline
void
CopyFromVector
(
const
std
::
vector
<
T
>&
src
,
const
platform
::
Place
&
dst_place
);
const
platform
::
DeviceContext
&
ctx
);
/**
* @brief Return the slice of the tensor.
...
...
paddle/framework/tensor_array.cc
浏览文件 @
557c7ae3
...
...
@@ -95,7 +95,8 @@ void TensorArray::Write(size_t index, const LoDTensor& value) {
values_
[
index
].
Resize
(
value
.
dims
());
values_
[
index
].
mutable_data
<
value_type
>
(
platform
::
CPUPlace
());
values_
[
index
].
CopyFrom
<
value_type
>
(
value
,
platform
::
CPUPlace
());
values_
[
index
].
CopyFrom
<
value_type
>
(
value
,
platform
::
CPUPlace
(),
platform
::
CPUDeviceContext
());
}
void
TensorArray
::
WriteShared
(
size_t
index
,
const
LoDTensor
&
value
)
{
...
...
@@ -151,7 +152,8 @@ LoDTensor TensorArray::Stack() const {
for
(
size_t
idx
=
0
;
idx
<
size
();
idx
++
)
{
result
.
Slice
<
value_type
>
(
idx
,
idx
+
1
)
.
CopyFrom
<
value_type
>
(
Read
(
idx
),
platform
::
CPUPlace
());
.
CopyFrom
<
value_type
>
(
Read
(
idx
),
platform
::
CPUPlace
(),
platform
::
CPUDeviceContext
());
}
return
result
;
}
...
...
@@ -182,7 +184,8 @@ void TensorArray::Unstack(const LoDTensor& source, bool data_shared) const {
// copy
value
.
Resize
(
value_dims
);
value
.
CopyFrom
<
value_type
>
(
source
.
Slice
<
value_type
>
(
elem
,
elem
+
1
),
platform
::
CPUPlace
());
platform
::
CPUPlace
(),
platform
::
CPUDeviceContext
());
}
}
}
...
...
@@ -236,7 +239,8 @@ LoDTensor DynamicBatchUnpacker::GetBatch(size_t index) {
auto
target
=
result
.
Slice
<
value_type
>
(
i
,
i
+
1
);
auto
source_
=
source
->
Slice
<
value_type
>
(
index
,
index
+
1
);
target
.
CopyFrom
<
value_type
>
(
source_
,
platform
::
CPUPlace
());
target
.
CopyFrom
<
value_type
>
(
source_
,
platform
::
CPUPlace
(),
platform
::
CPUDeviceContext
());
}
return
result
;
...
...
@@ -269,7 +273,8 @@ LoDTensor PackDynamicBatch(const std::vector<LoDTensor>& source,
if
(
index
>=
seq_meta
.
end
)
break
;
auto
source_
=
source
[
batch_id
].
Slice
<
float
>
(
seq_id
,
seq_id
+
1
);
auto
target
=
result
.
Slice
<
float
>
(
index
,
index
+
1
);
target
.
CopyFrom
<
float
>
(
source_
,
platform
::
CPUPlace
());
target
.
CopyFrom
<
float
>
(
source_
,
platform
::
CPUPlace
(),
platform
::
CPUDeviceContext
());
}
}
...
...
paddle/framework/tensor_impl.h
浏览文件 @
557c7ae3
...
...
@@ -88,7 +88,8 @@ inline Tensor& Tensor::ShareDataWith(const Tensor& src) {
template
<
typename
T
>
inline
void
Tensor
::
CopyFrom
(
const
Tensor
&
src
,
const
platform
::
Place
&
dst_place
)
{
const
platform
::
Place
&
dst_place
,
const
platform
::
DeviceContext
&
ctx
)
{
src
.
check_memory_size
<
T
>
();
Resize
(
src
.
dims
());
...
...
@@ -106,26 +107,45 @@ inline void Tensor::CopyFrom(const Tensor& src,
#ifdef PADDLE_WITH_CUDA
else
if
(
platform
::
is_gpu_place
(
src_place
)
&&
platform
::
is_cpu_place
(
dst_place
))
{
memory
::
Copy
(
boost
::
get
<
platform
::
CPUPlace
>
(
dst_place
),
dst_ptr
,
boost
::
get
<
platform
::
GPUPlace
>
(
src_place
),
src_ptr
,
size
,
0
);
auto
src_gpu_place
=
boost
::
get
<
platform
::
GPUPlace
>
(
src_place
);
auto
dst_cpu_place
=
boost
::
get
<
platform
::
CPUPlace
>
(
dst_place
);
auto
ctx_place
=
ctx
.
GetPlace
();
PADDLE_ENFORCE
(
platform
::
is_gpu_place
(
ctx_place
));
auto
ctx_gpu_place
=
boost
::
get
<
platform
::
GPUPlace
>
(
ctx_place
);
PADDLE_ENFORCE_EQ
(
src_gpu_place
,
ctx_gpu_place
);
memory
::
Copy
(
dst_cpu_place
,
dst_ptr
,
src_gpu_place
,
src_ptr
,
size
,
reinterpret_cast
<
const
platform
::
CUDADeviceContext
&>
(
ctx
).
stream
());
}
else
if
(
platform
::
is_cpu_place
(
src_place
)
&&
platform
::
is_gpu_place
(
dst_place
))
{
memory
::
Copy
(
boost
::
get
<
platform
::
GPUPlace
>
(
dst_place
),
dst_ptr
,
boost
::
get
<
platform
::
CPUPlace
>
(
src_place
),
src_ptr
,
size
,
0
);
auto
src_cpu_place
=
boost
::
get
<
platform
::
CPUPlace
>
(
src_place
);
auto
dst_gpu_place
=
boost
::
get
<
platform
::
GPUPlace
>
(
dst_place
);
auto
ctx_place
=
ctx
.
GetPlace
();
PADDLE_ENFORCE
(
platform
::
is_gpu_place
(
ctx_place
));
auto
ctx_gpu_place
=
boost
::
get
<
platform
::
GPUPlace
>
(
ctx_place
);
PADDLE_ENFORCE_EQ
(
dst_gpu_place
,
ctx_gpu_place
);
memory
::
Copy
(
dst_gpu_place
,
dst_ptr
,
src_cpu_place
,
src_ptr
,
size
,
reinterpret_cast
<
const
platform
::
CUDADeviceContext
&>
(
ctx
).
stream
());
}
else
if
(
platform
::
is_gpu_place
(
src_place
)
&&
platform
::
is_gpu_place
(
dst_place
))
{
memory
::
Copy
(
boost
::
get
<
platform
::
GPUPlace
>
(
dst_place
),
dst_ptr
,
boost
::
get
<
platform
::
GPUPlace
>
(
src_place
),
src_ptr
,
size
,
0
);
auto
src_gpu_place
=
boost
::
get
<
platform
::
GPUPlace
>
(
src_place
);
auto
dst_gpu_place
=
boost
::
get
<
platform
::
GPUPlace
>
(
dst_place
);
auto
ctx_place
=
ctx
.
GetPlace
();
PADDLE_ENFORCE
(
platform
::
is_gpu_place
(
ctx_place
));
auto
ctx_gpu_place
=
boost
::
get
<
platform
::
GPUPlace
>
(
ctx_place
);
PADDLE_ENFORCE_EQ
(
src_gpu_place
,
ctx_gpu_place
);
memory
::
Copy
(
dst_gpu_place
,
dst_ptr
,
src_gpu_place
,
src_ptr
,
size
,
reinterpret_cast
<
const
platform
::
CUDADeviceContext
&>
(
ctx
).
stream
());
}
PADDLE_ENFORCE
(
cudaStreamSynchronize
(
0
),
"cudaStreamSynchronize failed in Tensor CopyFrom"
);
#endif
}
template
<
typename
T
>
inline
void
Tensor
::
CopyFromVector
(
const
std
::
vector
<
T
>&
src
,
const
platform
::
Place
&
dst_place
)
{
const
platform
::
DeviceContext
&
ctx
)
{
auto
dst_place
=
ctx
.
GetPlace
();
auto
src_ptr
=
static_cast
<
const
void
*>
(
src
.
data
());
platform
::
CPUPlace
src_place
;
auto
dst_ptr
=
static_cast
<
void
*>
(
mutable_data
<
T
>
(
dst_place
));
...
...
@@ -137,12 +157,11 @@ inline void Tensor::CopyFromVector(const std::vector<T>& src,
}
#ifdef PADDLE_WITH_CUDA
else
if
(
platform
::
is_gpu_place
(
dst_place
))
{
memory
::
Copy
(
boost
::
get
<
platform
::
GPUPlace
>
(
dst_place
),
dst_ptr
,
src_place
,
src_ptr
,
size
,
0
);
memory
::
Copy
(
boost
::
get
<
platform
::
GPUPlace
>
(
dst_place
),
dst_ptr
,
src_place
,
src_ptr
,
size
,
reinterpret_cast
<
const
platform
::
CUDADeviceContext
&>
(
ctx
).
stream
());
}
PADDLE_ENFORCE
(
cudaStreamSynchronize
(
0
),
"cudaStreamSynchronize failed in Tensor CopyFromVector"
);
#endif
}
...
...
paddle/framework/tensor_test.cc
浏览文件 @
557c7ae3
...
...
@@ -194,6 +194,7 @@ TEST(Tensor, CopyFrom) {
{
Tensor
src_tensor
;
Tensor
dst_tensor
;
CPUDeviceContext
cpu_ctx
((
CPUPlace
()));
int
*
src_ptr
=
src_tensor
.
mutable_data
<
int
>
(
make_ddim
({
3
,
3
}),
CPUPlace
());
...
...
@@ -201,7 +202,7 @@ TEST(Tensor, CopyFrom) {
memcpy
(
src_ptr
,
arr
,
9
*
sizeof
(
int
));
auto
cpu_place
=
new
paddle
::
platform
::
CPUPlace
();
dst_tensor
.
CopyFrom
<
int
>
(
src_tensor
,
*
cpu_place
);
dst_tensor
.
CopyFrom
<
int
>
(
src_tensor
,
*
cpu_place
,
cpu_ctx
);
const
int
*
dst_ptr
=
dst_tensor
.
data
<
int
>
();
ASSERT_NE
(
src_ptr
,
dst_ptr
);
...
...
@@ -210,7 +211,7 @@ TEST(Tensor, CopyFrom) {
}
Tensor
slice_tensor
=
src_tensor
.
Slice
<
int
>
(
1
,
2
);
dst_tensor
.
CopyFrom
<
int
>
(
slice_tensor
,
*
cpu_place
);
dst_tensor
.
CopyFrom
<
int
>
(
slice_tensor
,
*
cpu_place
,
cpu_ctx
);
const
int
*
slice_ptr
=
slice_tensor
.
data
<
int
>
();
dst_ptr
=
dst_tensor
.
data
<
int
>
();
ASSERT_NE
(
dst_ptr
,
slice_ptr
);
...
...
@@ -231,13 +232,15 @@ TEST(Tensor, CopyFrom) {
// CPU Tensor to GPU Tensor
auto
gpu_place
=
new
paddle
::
platform
::
GPUPlace
(
0
);
gpu_tensor
.
CopyFrom
<
int
>
(
src_tensor
,
*
gpu_place
);
CUDADeviceContext
gpu_ctx
(
*
gpu_place
);
gpu_tensor
.
CopyFrom
<
int
>
(
src_tensor
,
*
gpu_place
,
gpu_ctx
);
// GPU Tensor to CPU Tensor
auto
cpu_place
=
new
paddle
::
platform
::
CPUPlace
();
dst_tensor
.
CopyFrom
<
int
>
(
gpu_tensor
,
*
cpu_place
);
dst_tensor
.
CopyFrom
<
int
>
(
gpu_tensor
,
*
cpu_place
,
gpu_ctx
);
// Compare Tensors
// Sync before Compare Tensors
gpu_ctx
.
Wait
();
const
int
*
dst_ptr
=
dst_tensor
.
data
<
int
>
();
ASSERT_NE
(
src_ptr
,
dst_ptr
);
for
(
size_t
i
=
0
;
i
<
9
;
++
i
)
{
...
...
@@ -247,12 +250,13 @@ TEST(Tensor, CopyFrom) {
Tensor
slice_tensor
=
src_tensor
.
Slice
<
int
>
(
1
,
2
);
// CPU Slice Tensor to GPU Tensor
gpu_tensor
.
CopyFrom
<
int
>
(
slice_tensor
,
*
gpu_place
);
gpu_tensor
.
CopyFrom
<
int
>
(
slice_tensor
,
*
gpu_place
,
gpu_ctx
);
// GPU Tensor to CPU Tensor
dst_tensor
.
CopyFrom
<
int
>
(
gpu_tensor
,
*
cpu_place
);
dst_tensor
.
CopyFrom
<
int
>
(
gpu_tensor
,
*
cpu_place
,
gpu_ctx
);
// Compare Slice Tensors
// Sync before Compare Slice Tensors
gpu_ctx
.
Wait
();
const
int
*
slice_ptr
=
slice_tensor
.
data
<
int
>
();
dst_ptr
=
dst_tensor
.
data
<
int
>
();
ASSERT_NE
(
dst_ptr
,
slice_ptr
);
...
...
@@ -273,7 +277,8 @@ TEST(Tensor, CopyFromVector) {
// Copy to CPU Tensor
cpu_tensor
.
Resize
(
make_ddim
({
3
,
3
}));
auto
cpu_place
=
new
paddle
::
platform
::
CPUPlace
();
cpu_tensor
.
CopyFromVector
<
int
>
(
src_vec
,
*
cpu_place
);
CPUDeviceContext
cpu_ctx
(
*
cpu_place
);
cpu_tensor
.
CopyFromVector
<
int
>
(
src_vec
,
cpu_ctx
);
// Compare Tensors
const
int
*
cpu_ptr
=
cpu_tensor
.
data
<
int
>
();
...
...
@@ -285,7 +290,7 @@ TEST(Tensor, CopyFromVector) {
src_vec
.
erase
(
src_vec
.
begin
(),
src_vec
.
begin
()
+
5
);
cpu_tensor
.
Resize
(
make_ddim
({
2
,
2
}));
cpu_tensor
.
CopyFromVector
<
int
>
(
src_vec
,
*
cpu_place
);
cpu_tensor
.
CopyFromVector
<
int
>
(
src_vec
,
cpu_ctx
);
cpu_ptr
=
cpu_tensor
.
data
<
int
>
();
src_ptr
=
src_vec
.
data
();
ASSERT_NE
(
src_ptr
,
cpu_ptr
);
...
...
@@ -306,16 +311,19 @@ TEST(Tensor, CopyFromVector) {
// Copy to CPU Tensor
cpu_tensor
.
Resize
(
make_ddim
({
3
,
3
}));
auto
cpu_place
=
new
paddle
::
platform
::
CPUPlace
();
cpu_tensor
.
CopyFromVector
<
int
>
(
src_vec
,
*
cpu_place
);
CPUDeviceContext
cpu_ctx
(
*
cpu_place
);
cpu_tensor
.
CopyFromVector
<
int
>
(
src_vec
,
cpu_ctx
);
// Copy to GPUTensor
gpu_tensor
.
Resize
(
make_ddim
({
3
,
3
}));
auto
gpu_place
=
new
paddle
::
platform
::
GPUPlace
();
gpu_tensor
.
CopyFromVector
<
int
>
(
src_vec
,
*
gpu_place
);
CUDADeviceContext
gpu_ctx
(
*
gpu_place
);
gpu_tensor
.
CopyFromVector
<
int
>
(
src_vec
,
gpu_ctx
);
// Copy from GPU to CPU tensor for comparison
dst_tensor
.
CopyFrom
<
int
>
(
gpu_tensor
,
*
cpu_place
);
dst_tensor
.
CopyFrom
<
int
>
(
gpu_tensor
,
*
cpu_place
,
gpu_ctx
);
// Compare Tensors
// Sync before Compare Tensors
gpu_ctx
.
Wait
();
const
int
*
src_ptr
=
src_vec
.
data
();
const
int
*
cpu_ptr
=
cpu_tensor
.
data
<
int
>
();
const
int
*
dst_ptr
=
dst_tensor
.
data
<
int
>
();
...
...
@@ -329,11 +337,13 @@ TEST(Tensor, CopyFromVector) {
src_vec
.
erase
(
src_vec
.
begin
(),
src_vec
.
begin
()
+
5
);
cpu_tensor
.
Resize
(
make_ddim
({
2
,
2
}));
cpu_tensor
.
CopyFromVector
<
int
>
(
src_vec
,
*
cpu_place
);
cpu_tensor
.
CopyFromVector
<
int
>
(
src_vec
,
cpu_ctx
);
gpu_tensor
.
Resize
(
make_ddim
({
2
,
2
}));
gpu_tensor
.
CopyFromVector
<
int
>
(
src_vec
,
*
gpu_place
);
dst_tensor
.
CopyFrom
<
int
>
(
gpu_tensor
,
*
cpu_place
);
gpu_tensor
.
CopyFromVector
<
int
>
(
src_vec
,
gpu_ctx
);
dst_tensor
.
CopyFrom
<
int
>
(
gpu_tensor
,
*
cpu_place
,
gpu_ctx
);
// Sync before Compare Tensors
gpu_ctx
.
Wait
();
src_ptr
=
src_vec
.
data
();
cpu_ptr
=
cpu_tensor
.
data
<
int
>
();
dst_ptr
=
dst_tensor
.
data
<
int
>
();
...
...
paddle/operators/CMakeLists.txt
浏览文件 @
557c7ae3
...
...
@@ -113,6 +113,8 @@ set(DEPS_OPS
cross_entropy_op
softmax_with_cross_entropy_op
sum_op
pool_op
pool_with_index_op
conv3d_op
)
...
...
@@ -123,7 +125,8 @@ op_library(cross_entropy_op DEPS cross_entropy)
op_library
(
softmax_with_cross_entropy_op DEPS cross_entropy softmax
)
op_library
(
sum_op DEPS net_op
)
op_library
(
conv3d_op DEPS vol2col
)
op_library
(
pool_op DEPS pooling
)
op_library
(
pool_with_index_op DEPS pooling
)
list
(
REMOVE_ITEM GENERAL_OPS
${
DEPS_OPS
}
)
foreach
(
src
${
GENERAL_OPS
}
)
...
...
paddle/operators/activation_op.cc
浏览文件 @
557c7ae3
...
...
@@ -321,6 +321,23 @@ class STanhOpMaker : public framework::OpProtoAndCheckerMaker {
}
};
template
<
typename
AttrType
>
class
ThresholdedReluOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
ThresholdedReluOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"Input of ThresholdedRelu operator"
);
AddOutput
(
"Y"
,
"Output of ThresholdedRelu operator"
);
AddComment
(
"ThresholdedRelu activation operator, "
"thresholded_relu = x for x > threshold, "
"thresholded_relu = 0 otherwise."
);
AddAttr
<
AttrType
>
(
"threshold"
,
"The threshold location of activation"
)
.
SetDefault
(
static_cast
<
AttrType
>
(
1.0
));
}
};
}
// namespace operators
}
// namespace paddle
...
...
@@ -392,6 +409,10 @@ REGISTER_OP(stanh, ops::ActivationOp, ops::STanhOpMaker<float>, stanh_grad,
REGISTER_OP
(
hard_shrink
,
ops
::
ActivationOp
,
ops
::
HardShrinkOpMaker
<
float
>
,
hard_shrink_grad
,
ops
::
ActivationOpGrad
);
REGISTER_OP
(
thresholded_relu
,
ops
::
ActivationOp
,
ops
::
ThresholdedReluOpMaker
<
float
>
,
thresholded_relu_grad
,
ops
::
ActivationOpGrad
);
#define REGISTER_ACTIVATION_CPU_KERNEL(act_type, functor, grad_functor) \
REGISTER_OP_CPU_KERNEL( \
act_type, \
...
...
paddle/operators/activation_op.h
浏览文件 @
557c7ae3
...
...
@@ -590,6 +590,32 @@ struct STanhGradFunctor : public BaseActivationFunctor<T> {
}
};
template
<
typename
T
>
struct
ThresholdedReluFunctor
:
public
BaseActivationFunctor
<
T
>
{
float
threshold
;
typename
BaseActivationFunctor
<
T
>::
AttrPair
GetAttrs
()
{
return
{{
"threshold"
,
&
threshold
}};
}
template
<
typename
Device
,
typename
X
,
typename
Y
>
void
operator
()(
Device
d
,
X
x
,
Y
y
)
const
{
y
.
device
(
d
)
=
(
x
>
static_cast
<
T
>
(
threshold
)).
template
cast
<
T
>()
*
x
;
}
};
template
<
typename
T
>
struct
ThresholdedReluGradFunctor
:
public
BaseActivationFunctor
<
T
>
{
float
threshold
;
typename
BaseActivationFunctor
<
T
>::
AttrPair
GetAttrs
()
{
return
{{
"threshold"
,
&
threshold
}};
}
template
<
typename
Device
,
typename
X
,
typename
Y
,
typename
dY
,
typename
dX
>
void
operator
()(
Device
d
,
X
x
,
Y
y
,
dY
dy
,
dX
dx
)
const
{
dx
.
device
(
d
)
=
dy
*
(
x
>
static_cast
<
T
>
(
threshold
)).
template
cast
<
T
>();
}
};
}
// namespace operators
}
// namespace paddle
...
...
@@ -615,4 +641,5 @@ struct STanhGradFunctor : public BaseActivationFunctor<T> {
__macro(leaky_relu, LeakyReluFunctor, LeakyReluGradFunctor); \
__macro(tanh_shrink, TanhShrinkFunctor, TanhShrinkGradFunctor); \
__macro(elu, ELUFunctor, ELUGradFunctor); \
__macro(hard_shrink, HardShrinkFunctor, HardShrinkGradFunctor)
__macro(hard_shrink, HardShrinkFunctor, HardShrinkGradFunctor); \
__macro(thresholded_relu, ThresholdedReluFunctor, ThresholdedReluGradFunctor);
paddle/operators/conv2d_op.cc
浏览文件 @
557c7ae3
...
...
@@ -12,111 +12,91 @@
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/
gemm_
conv2d_op.h"
#include "paddle/operators/conv2d_op.h"
namespace
paddle
{
namespace
operators
{
int
outputSize
(
int
input_size
,
int
filter_size
,
int
padding
,
int
stride
)
{
int
output_size
=
(
input_size
-
filter_size
+
2
*
padding
)
/
stride
+
1
;
return
output_size
;
void
Conv2DOp
::
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Input"
),
"Input(Input) of Conv2DOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Filter"
),
"Input(Filter) of Conv2DOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Output"
),
"Output(Output) of Conv2DOp should not be null."
);
auto
in_dims
=
ctx
->
GetInputDim
(
"Input"
);
auto
filter_dims
=
ctx
->
GetInputDim
(
"Filter"
);
std
::
vector
<
int
>
strides
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"strides"
);
std
::
vector
<
int
>
paddings
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"paddings"
);
int
groups
=
ctx
->
Attrs
().
Get
<
int
>
(
"groups"
);
int
input_channels
=
in_dims
[
1
];
int
output_channels
=
filter_dims
[
0
];
PADDLE_ENFORCE_EQ
(
in_dims
.
size
(),
4
,
"Conv2DOp input should be 4-D."
);
PADDLE_ENFORCE_EQ
(
filter_dims
.
size
(),
4
,
"Conv2DOp filter should be 4-D."
);
PADDLE_ENFORCE_EQ
(
input_channels
,
filter_dims
[
1
]
*
groups
,
"The number of input channels should be equal to filter "
"channels * groups."
);
PADDLE_ENFORCE_EQ
(
output_channels
%
groups
,
0
,
"The number of output channels should be divided by groups."
);
auto
output_height
=
OutputSize
(
in_dims
[
2
],
filter_dims
[
2
],
paddings
[
0
],
strides
[
0
]);
auto
output_width
=
OutputSize
(
in_dims
[
3
],
filter_dims
[
3
],
paddings
[
1
],
strides
[
1
]);
ctx
->
SetOutputDim
(
"Output"
,
{
in_dims
[
0
],
filter_dims
[
0
],
output_height
,
output_width
});
}
class
Conv2DOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Input"
),
"Input(Input) of Conv2DOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Filter"
),
"Input(Filter) of Conv2DOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Output"
),
"Output(Output) of Conv2DOp should not be null."
);
auto
in_dims
=
ctx
->
GetInputDim
(
"Input"
);
auto
filter_dims
=
ctx
->
GetInputDim
(
"Filter"
);
std
::
vector
<
int
>
strides
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"strides"
);
std
::
vector
<
int
>
paddings
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"paddings"
);
int
groups
=
ctx
->
Attrs
().
Get
<
int
>
(
"groups"
);
int
input_channels
=
in_dims
[
1
];
int
output_channels
=
filter_dims
[
0
];
PADDLE_ENFORCE_EQ
(
in_dims
.
size
(),
4
,
"Conv2DOp input should be 4-D."
);
PADDLE_ENFORCE_EQ
(
filter_dims
.
size
(),
4
,
"Conv2DOp filter should be 4-D."
);
PADDLE_ENFORCE_EQ
(
input_channels
,
filter_dims
[
1
]
*
groups
,
"The number of input channels should be equal to filter "
"channels * groups."
);
PADDLE_ENFORCE_EQ
(
output_channels
%
groups
,
0
,
"The number of output channels should be divided by groups."
);
auto
output_height
=
outputSize
(
in_dims
[
2
],
filter_dims
[
2
],
paddings
[
0
],
strides
[
0
]);
auto
output_width
=
outputSize
(
in_dims
[
3
],
filter_dims
[
3
],
paddings
[
1
],
strides
[
1
]);
ctx
->
SetOutputDim
(
"Output"
,
{
in_dims
[
0
],
filter_dims
[
0
],
output_height
,
output_width
});
}
};
class
Conv2DOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
Conv2DOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"Input"
,
"The input tensor of convolution operator. "
"The format of input tensor is NCHW. Where N is batch size, C is the "
"number of channels, H and W is the height and width of image."
);
AddInput
(
"Filter"
,
"The filter tensor of convolution operator."
"The format of the filter tensor is MCHW, where M is the number of "
"output image channels, C is the number of input image channels, "
"H and W is height and width of filter. "
"If the groups attribute is greater than 1, C equal the number of "
"input image channels divided by the groups."
);
AddOutput
(
"Output"
,
"The output tensor of convolution operator."
"The format of output tensor is also NCHW."
);
AddAttr
<
std
::
vector
<
int
>>
(
"strides"
,
"strides of convolution operator."
)
.
SetDefault
({
1
,
1
});
AddAttr
<
std
::
vector
<
int
>>
(
"paddings"
,
"paddings of convolution operator."
)
.
SetDefault
({
0
,
0
});
AddAttr
<
int
>
(
"groups"
,
"group size of convolution operator. "
"Refer to grouped convolution in Alex Krizhevsky's paper: "
"when group=2, the first half of the filters are only connected to the "
"first half of the input channels, and the second half only connected "
"to the second half."
)
.
SetDefault
(
1
);
AddComment
(
R"DOC(
Conv2DOpMaker
::
Conv2DOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"Input"
,
"The input tensor of convolution operator. "
"The format of input tensor is NCHW. Where N is batch size, C is the "
"number of channels, H and W is the height and width of image."
);
AddInput
(
"Filter"
,
"The filter tensor of convolution operator."
"The format of the filter tensor is MCHW, where M is the number of "
"output image channels, C is the number of input image channels, "
"H and W is height and width of filter. "
"If the groups attribute is greater than 1, C equal the number of "
"input image channels divided by the groups."
);
AddOutput
(
"Output"
,
"The output tensor of convolution operator."
"The format of output tensor is also NCHW."
);
AddAttr
<
std
::
vector
<
int
>>
(
"strides"
,
"strides of convolution operator."
)
.
SetDefault
({
1
,
1
});
AddAttr
<
std
::
vector
<
int
>>
(
"paddings"
,
"paddings of convolution operator."
)
.
SetDefault
({
0
,
0
});
AddAttr
<
int
>
(
"groups"
,
"group size of convolution operator. "
"Refer to grouped convolution in Alex Krizhevsky's paper: "
"when group=2, the first half of the filters are only connected to the "
"first half of the input channels, and the second half only connected "
"to the second half."
)
.
SetDefault
(
1
);
AddComment
(
R"DOC(
The convolution operation calculates the output based on the input, filter
and strides, paddings, groups parameters. The size of each dimension of the
parameters is checked in the infer-shape.
)DOC"
);
}
};
class
Conv2DOpGrad
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
}
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
auto
in_dims
=
ctx
->
GetInputDim
(
"Input"
);
auto
filter_dims
=
ctx
->
GetInputDim
(
"Filter"
);
if
(
ctx
->
HasOutput
(
framework
::
GradVarName
(
"Input"
)))
{
ctx
->
SetOutputDim
(
framework
::
GradVarName
(
"Input"
),
in_dims
);
}
if
(
ctx
->
HasOutput
(
framework
::
GradVarName
(
"Filter"
)))
{
ctx
->
SetOutputDim
(
framework
::
GradVarName
(
"Filter"
),
filter_dims
);
}
void
Conv2DOpGrad
::
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
{
auto
in_dims
=
ctx
->
GetInputDim
(
"Input"
);
auto
filter_dims
=
ctx
->
GetInputDim
(
"Filter"
);
if
(
ctx
->
HasOutput
(
framework
::
GradVarName
(
"Input"
)))
{
ctx
->
SetOutputDim
(
framework
::
GradVarName
(
"Input"
),
in_dims
);
}
};
if
(
ctx
->
HasOutput
(
framework
::
GradVarName
(
"Filter"
)))
{
ctx
->
SetOutputDim
(
framework
::
GradVarName
(
"Filter"
),
filter_dims
);
}
}
}
// namespace operators
}
// namespace paddle
...
...
paddle/operators/conv2d_op.cu
浏览文件 @
557c7ae3
...
...
@@ -12,7 +12,7 @@
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/
gemm_
conv2d_op.h"
#include "paddle/operators/conv2d_op.h"
namespace
ops
=
paddle
::
operators
;
...
...
paddle/operators/
gemm_
conv2d_op.h
→
paddle/operators/conv2d_op.h
浏览文件 @
557c7ae3
...
...
@@ -24,6 +24,38 @@ namespace operators {
using
Tensor
=
framework
::
Tensor
;
// Base convolution operator definations for other conv
// like operators to reuse the implementation.
inline
int
OutputSize
(
int
input_size
,
int
filter_size
,
int
padding
,
int
stride
)
{
int
output_size
=
(
input_size
-
filter_size
+
2
*
padding
)
/
stride
+
1
;
return
output_size
;
}
// Define Op classes in .h file so that other conv
// operator implementations can reuse the code.
class
Conv2DOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
Conv2DOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
);
};
class
Conv2DOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
;
};
class
Conv2DOpGrad
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
;
};
template
<
typename
Place
,
typename
T
>
class
GemmConv2DKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
...
...
@@ -74,7 +106,6 @@ class GemmConv2DKernel : public framework::OpKernel<T> {
framework
::
DDim
output_matrix_shape
=
{
output_channels
,
output_height
*
output_width
};
// convolution operator: im2col + gemm
int
in_step
=
input_channels
/
groups
;
int
out_step
=
output_channels
/
groups
;
...
...
paddle/operators/conv3d_op.cc
浏览文件 @
557c7ae3
...
...
@@ -49,7 +49,7 @@ void Conv3DOp::InferShape(framework::InferShapeContext* ctx) const {
std
::
vector
<
int64_t
>
output_shape
({
in_dims
[
0
],
filter_dims
[
0
]});
for
(
size_t
i
=
0
;
i
<
paddings
.
size
();
++
i
)
{
output_shape
.
push_back
(
OutputSizeConv3d
(
in_dims
[
i
+
2
],
filter_dims
[
i
],
output_shape
.
push_back
(
OutputSizeConv3d
(
in_dims
[
i
+
2
],
filter_dims
[
i
+
2
],
paddings
[
i
],
strides
[
i
]));
}
ctx
->
SetOutputDim
(
"Output"
,
framework
::
make_ddim
(
output_shape
));
...
...
paddle/operators/conv_cudnn_op.cc
0 → 100644
浏览文件 @
557c7ae3
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/conv2d_op.h"
namespace
paddle
{
namespace
operators
{
class
CudnnConvOpMaker
:
public
Conv2DOpMaker
{
public:
CudnnConvOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
Conv2DOpMaker
(
proto
,
op_checker
)
{
AddAttr
<
std
::
vector
<
int
>>
(
"dilations"
,
"dilations of convolution operator."
)
.
SetDefault
(
std
::
vector
<
int
>
{
1
,
1
});
AddAttr
<
int
>
(
"workspace_size_MB"
,
"workspace size for cudnn, in MB, "
"workspace is a section of GPU memory which will be "
"allocated/freed each time the operator runs, larger "
"workspace size can increase performance but also requires "
"better hardward. This size should be carefully setted."
)
.
SetDefault
(
4096
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP
(
conv_cudnn
,
ops
::
Conv2DOp
,
ops
::
CudnnConvOpMaker
,
conv_cudnn_grad
,
ops
::
Conv2DOpGrad
);
REGISTER_OP_CPU_KERNEL
(
conv_cudnn
,
ops
::
GemmConv2DKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
conv_cudnn_grad
,
ops
::
GemmConvGrad2DKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
paddle/operators/conv_cudnn_op.cu
0 → 100644
浏览文件 @
557c7ae3
/* Copyright (c) 2016 PaddlePaddle Authors All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
#include "paddle/memory/memory.h"
#include "paddle/operators/conv2d_op.h"
#include "paddle/platform/assert.h"
#include "paddle/platform/cudnn_helper.h"
namespace
paddle
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
using
ScopedTensorDescriptor
=
platform
::
ScopedTensorDescriptor
;
using
ScopedFilterDescriptor
=
platform
::
ScopedFilterDescriptor
;
using
ScopedConvolutionDescriptor
=
platform
::
ScopedConvolutionDescriptor
;
using
DataLayout
=
platform
::
DataLayout
;
using
CUDADeviceContext
=
platform
::
CUDADeviceContext
;
static
constexpr
size_t
kCONV_CUDNN_WORKSPACE_LIMIT_BYTES
=
1024
*
1024
*
1024
;
// NOTE: framework::vectorize converts to type int64_t
// which does not fit cudnn inputs.
std
::
vector
<
int
>
Dims2Vector
(
const
framework
::
DDim
&
dims
)
{
std
::
vector
<
int
>
ret
;
for
(
int
i
=
0
;
i
<
dims
.
size
();
i
++
)
{
ret
.
push_back
(
dims
[
i
]);
}
return
ret
;
}
template
<
typename
T
>
class
CudnnConvOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
PADDLE_ENFORCE
(
platform
::
is_gpu_place
(
ctx
.
GetPlace
()),
"It must use GPUPlace."
);
auto
*
input
=
ctx
.
Input
<
Tensor
>
(
"Input"
);
auto
*
filter
=
ctx
.
Input
<
Tensor
>
(
"Filter"
);
auto
*
output
=
ctx
.
Output
<
Tensor
>
(
"Output"
);
std
::
vector
<
int
>
strides
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"strides"
);
std
::
vector
<
int
>
paddings
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"paddings"
);
std
::
vector
<
int
>
dilations
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"dilations"
);
int
groups
=
ctx
.
Attr
<
int
>
(
"groups"
);
int
user_workspace_size
=
ctx
.
Attr
<
int
>
(
"workspace_size_MB"
);
const
T
*
input_data
=
input
->
data
<
T
>
();
const
T
*
filter_data
=
filter
->
data
<
T
>
();
T
*
output_data
=
output
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
// ------------------- cudnn descriptors ---------------------
ScopedTensorDescriptor
input_desc
;
ScopedTensorDescriptor
output_desc
;
ScopedFilterDescriptor
filter_desc
;
ScopedConvolutionDescriptor
conv_desc
;
DataLayout
layout
=
DataLayout
::
kNCHW
;
cudnnTensorDescriptor_t
cudnn_input_desc
=
input_desc
.
descriptor
<
T
>
(
layout
,
Dims2Vector
(
input
->
dims
()),
groups
);
cudnnTensorDescriptor_t
cudnn_output_desc
=
output_desc
.
descriptor
<
T
>
(
layout
,
Dims2Vector
(
output
->
dims
()),
groups
);
cudnnFilterDescriptor_t
cudnn_filter_desc
=
filter_desc
.
descriptor
<
T
>
(
layout
,
Dims2Vector
(
filter
->
dims
()),
groups
);
cudnnConvolutionDescriptor_t
cudnn_conv_desc
=
conv_desc
.
descriptor
<
T
>
(
paddings
,
strides
,
dilations
);
int
input_channels
=
input
->
dims
()[
1
];
int
input_height
=
input
->
dims
()[
2
];
int
input_width
=
input
->
dims
()[
3
];
int
output_channels
=
output
->
dims
()[
1
];
int
output_height
=
output
->
dims
()[
2
];
int
output_width
=
output
->
dims
()[
3
];
int
group_offset_in
=
input_channels
/
groups
*
input_height
*
input_width
;
int
group_offset_out
=
output_channels
/
groups
*
output_height
*
output_width
;
int
group_offset_filter
=
filter
->
numel
()
/
groups
;
// ------------------- cudnn conv workspace ---------------------
void
*
cudnn_workspace
=
nullptr
;
size_t
workspace_size_in_bytes
;
// final workspace to allocate.
size_t
workspace_size_limit
=
kCONV_CUDNN_WORKSPACE_LIMIT_BYTES
;
if
(
user_workspace_size
>
0
)
{
workspace_size_limit
=
user_workspace_size
*
1024
*
1024
;
}
// ------------------- cudnn conv algorithm ---------------------
cudnnConvolutionFwdAlgo_t
algo
;
auto
handle
=
ctx
.
cuda_device_context
().
cudnn_handle
();
PADDLE_ENFORCE
(
platform
::
dynload
::
cudnnGetConvolutionForwardAlgorithm
(
handle
,
cudnn_input_desc
,
cudnn_filter_desc
,
cudnn_conv_desc
,
cudnn_output_desc
,
CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT
,
workspace_size_limit
,
&
algo
));
// get workspace size able to allocate
PADDLE_ENFORCE
(
platform
::
dynload
::
cudnnGetConvolutionForwardWorkspaceSize
(
handle
,
cudnn_input_desc
,
cudnn_filter_desc
,
cudnn_conv_desc
,
cudnn_output_desc
,
algo
,
&
workspace_size_in_bytes
));
// Allocate on GPU memory
platform
::
GPUPlace
gpu
=
boost
::
get
<
platform
::
GPUPlace
>
(
ctx
.
GetPlace
());
cudnn_workspace
=
paddle
::
memory
::
Alloc
(
gpu
,
workspace_size_in_bytes
);
// ------------------- cudnn conv forward ---------------------
T
alpha
=
1.0
f
,
beta
=
0.0
f
;
for
(
int
i
=
0
;
i
<
groups
;
i
++
)
{
PADDLE_ENFORCE
(
platform
::
dynload
::
cudnnConvolutionForward
(
handle
,
&
alpha
,
cudnn_input_desc
,
input_data
+
i
*
group_offset_in
,
cudnn_filter_desc
,
filter_data
+
i
*
group_offset_filter
,
cudnn_conv_desc
,
algo
,
cudnn_workspace
,
workspace_size_in_bytes
,
&
beta
,
cudnn_output_desc
,
output_data
+
i
*
group_offset_out
));
}
// Release the cudnn workspace
paddle
::
memory
::
Free
(
gpu
,
cudnn_workspace
);
}
};
template
<
typename
T
>
class
CudnnConvGradOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
PADDLE_ENFORCE
(
platform
::
is_gpu_place
(
ctx
.
GetPlace
()),
"It must use GPUPlace."
);
auto
input
=
ctx
.
Input
<
Tensor
>
(
"Input"
);
auto
filter
=
ctx
.
Input
<
Tensor
>
(
"Filter"
);
auto
output_grad
=
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Output"
));
auto
input_grad
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"Input"
));
auto
filter_grad
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"Filter"
));
const
T
*
input_data
=
input
->
data
<
T
>
();
const
T
*
output_grad_data
=
output_grad
->
data
<
T
>
();
const
T
*
filter_data
=
filter
->
data
<
T
>
();
std
::
vector
<
int
>
strides
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"strides"
);
std
::
vector
<
int
>
paddings
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"paddings"
);
std
::
vector
<
int
>
dilations
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"dilations"
);
int
groups
=
ctx
.
Attr
<
int
>
(
"groups"
);
int
user_workspace_size
=
ctx
.
Attr
<
int
>
(
"workspace_size_MB"
);
// ------------------- cudnn descriptors ---------------------
ScopedTensorDescriptor
input_desc
;
ScopedTensorDescriptor
output_grad_desc
;
ScopedTensorDescriptor
input_grad_desc
;
ScopedFilterDescriptor
filter_desc
;
ScopedFilterDescriptor
filter_grad_desc
;
ScopedConvolutionDescriptor
conv_desc
;
DataLayout
layout
=
DataLayout
::
kNCHW
;
cudnnTensorDescriptor_t
cudnn_input_desc
=
input_desc
.
descriptor
<
T
>
(
layout
,
Dims2Vector
(
input
->
dims
()),
groups
);
cudnnTensorDescriptor_t
cudnn_output_grad_desc
=
output_grad_desc
.
descriptor
<
T
>
(
layout
,
Dims2Vector
(
output_grad
->
dims
()),
groups
);
cudnnFilterDescriptor_t
cudnn_filter_desc
=
filter_desc
.
descriptor
<
T
>
(
layout
,
Dims2Vector
(
filter
->
dims
()),
groups
);
cudnnTensorDescriptor_t
cudnn_input_grad_desc
=
nullptr
;
cudnnFilterDescriptor_t
cudnn_filter_grad_desc
=
nullptr
;
cudnnConvolutionDescriptor_t
cudnn_conv_desc
=
conv_desc
.
descriptor
<
T
>
(
paddings
,
strides
,
dilations
);
int
input_channels
=
input
->
dims
()[
1
];
int
input_height
=
input
->
dims
()[
2
];
int
input_width
=
input
->
dims
()[
3
];
int
output_grad_channels
=
filter
->
dims
()[
0
];
int
output_grad_height
=
output_grad
->
dims
()[
2
];
int
output_grad_width
=
output_grad
->
dims
()[
3
];
int
group_offset_in
=
input_channels
/
groups
*
input_height
*
input_width
;
int
group_offset_out
=
output_grad_channels
/
groups
*
output_grad_height
*
output_grad_width
;
int
group_offset_filter
=
filter
->
numel
()
/
groups
;
// ------------------- cudnn backward algorithm ---------------------
cudnnConvolutionBwdDataAlgo_t
data_algo
;
cudnnConvolutionBwdFilterAlgo_t
filter_algo
;
size_t
workspace_size_in_bytes
=
0
,
tmp_size
=
0
;
size_t
workspace_size_limit
=
kCONV_CUDNN_WORKSPACE_LIMIT_BYTES
;
if
(
user_workspace_size
>
0
)
{
workspace_size_limit
=
user_workspace_size
*
1024
*
1024
;
}
auto
handle
=
ctx
.
cuda_device_context
().
cudnn_handle
();
if
(
input_grad
)
{
cudnn_input_grad_desc
=
input_grad_desc
.
descriptor
<
T
>
(
layout
,
Dims2Vector
(
input_grad
->
dims
()),
groups
);
PADDLE_ENFORCE
(
platform
::
dynload
::
cudnnGetConvolutionBackwardDataAlgorithm
(
handle
,
cudnn_filter_desc
,
// dyDesc: Handle to the previously initialized input differential
// tensor descriptor.
cudnn_output_grad_desc
,
cudnn_conv_desc
,
// dxDesc: Handle to the previously initialized output tensor
// descriptor.
cudnn_input_grad_desc
,
CUDNN_CONVOLUTION_BWD_DATA_SPECIFY_WORKSPACE_LIMIT
,
workspace_size_limit
,
&
data_algo
));
PADDLE_ENFORCE
(
platform
::
dynload
::
cudnnGetConvolutionBackwardDataWorkspaceSize
(
handle
,
cudnn_filter_desc
,
cudnn_output_grad_desc
,
cudnn_conv_desc
,
cudnn_input_grad_desc
,
data_algo
,
&
tmp_size
));
workspace_size_in_bytes
=
std
::
max
(
workspace_size_in_bytes
,
tmp_size
);
}
if
(
filter_grad
)
{
cudnn_filter_grad_desc
=
filter_grad_desc
.
descriptor
<
T
>
(
layout
,
Dims2Vector
(
filter_grad
->
dims
()),
groups
);
PADDLE_ENFORCE
(
platform
::
dynload
::
cudnnGetConvolutionBackwardFilterAlgorithm
(
handle
,
cudnn_input_desc
,
cudnn_output_grad_desc
,
cudnn_conv_desc
,
cudnn_filter_desc
,
CUDNN_CONVOLUTION_BWD_FILTER_SPECIFY_WORKSPACE_LIMIT
,
workspace_size_limit
,
&
filter_algo
));
PADDLE_ENFORCE
(
platform
::
dynload
::
cudnnGetConvolutionBackwardFilterWorkspaceSize
(
handle
,
cudnn_input_desc
,
cudnn_output_grad_desc
,
cudnn_conv_desc
,
cudnn_filter_desc
,
filter_algo
,
&
tmp_size
));
workspace_size_in_bytes
=
std
::
max
(
workspace_size_in_bytes
,
tmp_size
);
}
// ------------------- cudnn conv workspace ---------------------
// Already on GPU
void
*
cudnn_workspace
=
nullptr
;
platform
::
GPUPlace
gpu
=
boost
::
get
<
platform
::
GPUPlace
>
(
ctx
.
GetPlace
());
cudnn_workspace
=
paddle
::
memory
::
Alloc
(
gpu
,
workspace_size_in_bytes
);
// ------------------- cudnn conv backward data ---------------------
// FIXME(typhoonzero): template type T may not be the same as cudnn call.
T
alpha
=
1.0
f
,
beta
=
0.0
f
;
if
(
input_grad
)
{
T
*
input_grad_data
=
input_grad
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
t
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
input_grad
);
t
.
device
(
ctx
.
GetEigenDevice
<
platform
::
GPUPlace
>
())
=
t
.
constant
(
static_cast
<
T
>
(
0
));
for
(
int
i
=
0
;
i
<
groups
;
i
++
)
{
PADDLE_ENFORCE
(
platform
::
dynload
::
cudnnConvolutionBackwardData
(
handle
,
&
alpha
,
cudnn_filter_desc
,
filter_data
+
i
*
group_offset_filter
,
cudnn_output_grad_desc
,
output_grad_data
+
i
*
group_offset_out
,
cudnn_conv_desc
,
data_algo
,
cudnn_workspace
,
workspace_size_in_bytes
,
&
beta
,
cudnn_input_grad_desc
,
input_grad_data
+
i
*
group_offset_in
));
}
}
// ------------------- cudnn conv backward filter ---------------------
if
(
filter_grad
)
{
T
*
filter_grad_data
=
filter_grad
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
t
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
filter_grad
);
t
.
device
(
ctx
.
GetEigenDevice
<
platform
::
GPUPlace
>
())
=
t
.
constant
(
static_cast
<
T
>
(
0
));
for
(
int
i
=
0
;
i
<
groups
;
i
++
)
{
PADDLE_ENFORCE
(
platform
::
dynload
::
cudnnConvolutionBackwardFilter
(
handle
,
&
alpha
,
cudnn_input_desc
,
input_data
+
i
*
group_offset_in
,
cudnn_output_grad_desc
,
output_grad_data
+
i
*
group_offset_out
,
cudnn_conv_desc
,
filter_algo
,
cudnn_workspace
,
workspace_size_in_bytes
,
&
beta
,
cudnn_filter_grad_desc
,
filter_grad_data
+
i
*
group_offset_filter
));
}
}
// Release the cudnn workspace
paddle
::
memory
::
Free
(
gpu
,
cudnn_workspace
);
}
};
}
// namespace operators
}
// namespace paddle
REGISTER_OP_GPU_KERNEL
(
conv_cudnn
,
paddle
::
operators
::
CudnnConvOpKernel
<
float
>
);
REGISTER_OP_GPU_KERNEL
(
conv_cudnn_grad
,
paddle
::
operators
::
CudnnConvGradOpKernel
<
float
>
);
paddle/operators/decayed_adagrad_op.cc
0 → 100644
浏览文件 @
557c7ae3
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/decayed_adagrad_op.h"
namespace
paddle
{
namespace
operators
{
class
DecayedAdagradOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Param"
),
"Input(Param) of DecayedAdagradOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Grad"
),
"Input(Grad) of DecayedAdagradOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Moment"
),
"Input(Moment) of DecayedAdagradOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"LearningRate"
),
"Input(LearningRate) of DecayedAdagradOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"ParamOut"
),
"Output(ParamOut) of DecayedAdagradOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"MomentOut"
),
"Output(MomentOut) of DecayedAdagradOp should not be null."
);
auto
lr_dims
=
ctx
->
GetInputDim
(
"LearningRate"
);
PADDLE_ENFORCE_EQ
(
framework
::
product
(
lr_dims
),
1
,
"LearningRate should have one element"
);
auto
param_dims
=
ctx
->
GetInputDim
(
"Param"
);
PADDLE_ENFORCE_EQ
(
param_dims
,
ctx
->
GetInputDim
(
"Grad"
),
"Param and Grad input of DecayedAdagradOp should have "
"the same dimension."
);
PADDLE_ENFORCE_EQ
(
param_dims
,
ctx
->
GetInputDim
(
"Moment"
),
"Param and Moment input of DecayedAdagradOp should have "
"the same dimension."
);
ctx
->
SetOutputDim
(
"ParamOut"
,
param_dims
);
ctx
->
SetOutputDim
(
"MomentOut"
,
param_dims
);
}
};
class
DecayedAdagradOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
DecayedAdagradOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"Param"
,
"(Tensor) Input parameter"
);
AddInput
(
"Grad"
,
"(Tensor) Input gradient"
);
AddInput
(
"Moment"
,
"(Tensor) Second moment"
);
AddInput
(
"LearningRate"
,
"(Tensor) Learning rate"
);
AddOutput
(
"ParamOut"
,
"(Tensor) Output parameter"
);
AddOutput
(
"MomentOut"
,
"(Tensor) Output second moment"
);
AddAttr
<
float
>
(
"decay"
,
"(float, default 0.95) "
"Discounting factor for coming gradient"
)
.
SetDefault
(
0.95
);
AddAttr
<
float
>
(
"epsilon"
,
"(float, default 1.0e-6) "
"Constant for numerical stability"
)
.
SetDefault
(
1.0e-6
f
);
AddComment
(
R"DOC(
Decayed Adagrad
moment_out = decay * moment + (1 - decay) * grad * grad
param_out = param - learning_rate * grad / (sqrt(moment_out) + epsilon)
)DOC"
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_WITHOUT_GRADIENT
(
decayed_adagrad
,
ops
::
DecayedAdagradOp
,
ops
::
DecayedAdagradOpMaker
);
REGISTER_OP_CPU_KERNEL
(
decayed_adagrad
,
ops
::
DecayedAdagradOpKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
paddle/operators/decayed_adagrad_op.cu
0 → 100644
浏览文件 @
557c7ae3
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#define EIGEN_USE_GPU
#include "paddle/operators/decayed_adagrad_op.h"
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_GPU_KERNEL
(
decayed_adagrad
,
ops
::
DecayedAdagradOpKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
paddle/operators/decayed_adagrad_op.h
0 → 100644
浏览文件 @
557c7ae3
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
Place
,
typename
T
>
class
DecayedAdagradOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
param_out_tensor
=
ctx
.
Output
<
framework
::
Tensor
>
(
"ParamOut"
);
auto
moment_out_tensor
=
ctx
.
Output
<
framework
::
Tensor
>
(
"MomentOut"
);
param_out_tensor
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
moment_out_tensor
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
float
decay
=
ctx
.
Attr
<
float
>
(
"decay"
);
float
epsilon
=
ctx
.
Attr
<
float
>
(
"epsilon"
);
auto
param
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
ctx
.
Input
<
framework
::
Tensor
>
(
"Param"
));
auto
grad
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
ctx
.
Input
<
framework
::
Tensor
>
(
"Grad"
));
auto
moment
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
ctx
.
Input
<
framework
::
Tensor
>
(
"Moment"
));
auto
lr
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
ctx
.
Input
<
framework
::
Tensor
>
(
"LearningRate"
));
auto
param_out
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
param_out_tensor
);
auto
moment_out
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
moment_out_tensor
);
auto
place
=
ctx
.
GetEigenDevice
<
Place
>
();
moment_out
.
device
(
place
)
=
decay
*
moment
+
(
1
-
decay
)
*
grad
*
grad
;
Eigen
::
DSizes
<
int
,
1
>
m_dsize
(
moment_out_tensor
->
numel
());
param_out
.
device
(
place
)
=
param
-
lr
.
broadcast
(
m_dsize
)
*
grad
/
(
moment_out
.
sqrt
()
+
epsilon
);
}
};
}
// namespace operators
}
// namespace paddle
paddle/operators/feed_op.h
浏览文件 @
557c7ae3
...
...
@@ -34,7 +34,7 @@ class FeedKernel : public framework::OpKernel<T> {
// TODO(qijun):
// check tensors[col].dims() with attribute,
// except the first dimenson.
out
->
CopyFrom
<
T
>
(
tensors
[
col
],
ctx
.
GetPlace
());
out
->
CopyFrom
<
T
>
(
tensors
[
col
],
ctx
.
GetPlace
()
,
ctx
.
device_context
()
);
}
};
...
...
paddle/operators/fetch_op.h
浏览文件 @
557c7ae3
...
...
@@ -35,7 +35,8 @@ class FetchKernel : public framework::OpKernel<T> {
PADDLE_ENFORCE_GT
(
tensors
->
size
(),
static_cast
<
size_t
>
(
col
));
(
*
tensors
)[
col
].
Resize
(
input
->
dims
());
(
*
tensors
)[
col
].
mutable_data
<
T
>
(
platform
::
CPUPlace
());
(
*
tensors
)[
col
].
CopyFrom
<
T
>
(
*
input
,
platform
::
CPUPlace
());
(
*
tensors
)[
col
].
CopyFrom
<
T
>
(
*
input
,
platform
::
CPUPlace
(),
ctx
.
device_context
());
// TODO(qijun): need to handle LodTensor later
}
};
...
...
paddle/operators/margin_rank_loss_op.cc
0 → 100644
浏览文件 @
557c7ae3
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/margin_rank_loss_op.h"
namespace
paddle
{
namespace
operators
{
class
MarginRankLossOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
// input check
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Label"
),
"Input(Label) shouldn't be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X1"
),
"Input(X1) shouldn't be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X2"
),
"Input(X2) shouldn't be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Output(Out) shouldn't be null."
);
auto
label_dims
=
ctx
->
GetInputDim
(
"Label"
);
auto
x1_dims
=
ctx
->
GetInputDim
(
"X1"
);
auto
x2_dims
=
ctx
->
GetInputDim
(
"X2"
);
PADDLE_ENFORCE
(
(
label_dims
==
x1_dims
)
&&
(
x1_dims
==
x2_dims
)
&&
(
label_dims
.
size
()
==
2
)
&&
(
label_dims
[
1
]
==
1
),
"All inputs must be 2-D tensor with shape [batch_size x 1]."
);
ctx
->
SetOutputDim
(
"Activated"
,
label_dims
);
ctx
->
SetOutputDim
(
"Out"
,
label_dims
);
}
};
template
<
typename
T
>
class
MarginRankLossOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
MarginRankLossOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X1"
,
"(2-D tensor with shape [batch_size x 1]) The score for "
"one item X1 to be ranked, from pairwise ranking model."
);
AddInput
(
"X2"
,
"(2-D tensor with shape [batch_size x 1]) The score for "
"another item X2 to be ranked, from pairwise ranking model."
);
AddInput
(
"Label"
,
"(2-D tensor with shape [batch_size x 1]) "
"The label indicating X1 ranked higher than X2 or not, "
"can only be +1 or -1."
);
AddAttr
<
T
>
(
"margin"
,
"(scalar, default 0) Margin for MarginRankLossOp."
)
.
SetDefault
(
static_cast
<
T
>
(
0
));
AddOutput
(
"Activated"
,
"(2-D tensor with shape [batch_size x 1]) Intermediate tensor "
"to indicate whether each element of Output(Out) is activated."
)
.
AsIntermediate
();
AddOutput
(
"Out"
,
"(2-D tensor with shape [batch_size x 1]) "
"The output loss of MarginRankLoss operator."
);
AddComment
(
R"DOC(
MarginRankLoss operator measures the loss given a pair of training sample
{`X1`, `X2`} and the `Label` with attribute `margin`, where `Label = +1`
indicating X1 is ranked higher than `X2`, otherwise `Label = -1`. The loss
turns out
loss(X1, X2, Label) = max(0, -Label * (X1 - X2) + margin).
The attribute `margin` involved here helps make the predictions more robust.
Denote the item ranked higher as the positive sample, otherwise the negative
sample. If the score of the two samples satisfies
positive sample - negative sample < margin,
the pair of samples will contribute to the final loss, which will backpropogate
and train the ranking model to enlarge the difference of the two score.
For batch input with size `batch_size`, `X1`, `X2` and `Label`
all have the same shape [batch_size x 1].
)DOC"
);
}
};
class
MarginRankLossGradOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Label"
),
"Input(Label) shouldn't be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X1"
),
"Input(X1) shouldn't be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X2"
),
"Input(X2) shouldn't be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
framework
::
GradVarName
(
"Out"
)),
"Input(Out@GRAD) shouldn't be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Activated"
),
"Intermediate(Activated) shouldn't be null."
);
auto
dims
=
ctx
->
GetInputDim
(
"Label"
);
ctx
->
SetOutputDim
(
framework
::
GradVarName
(
"X1"
),
dims
);
ctx
->
SetOutputDim
(
framework
::
GradVarName
(
"X2"
),
dims
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP
(
margin_rank_loss
,
ops
::
MarginRankLossOp
,
ops
::
MarginRankLossOpMaker
<
float
>
,
margin_rank_loss_grad
,
ops
::
MarginRankLossGradOp
);
REGISTER_OP_CPU_KERNEL
(
margin_rank_loss
,
ops
::
MarginRankLossKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
margin_rank_loss_grad
,
ops
::
MarginRankLossGradKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
paddle/operators/margin_rank_loss_op.cu
0 → 100644
浏览文件 @
557c7ae3
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/margin_rank_loss_op.h"
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_GPU_KERNEL
(
margin_rank_loss
,
ops
::
MarginRankLossKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
REGISTER_OP_GPU_KERNEL
(
margin_rank_loss_grad
,
ops
::
MarginRankLossGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
paddle/operators/margin_rank_loss_op.h
0 → 100644
浏览文件 @
557c7ae3
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
T
>
struct
ReLU
{
HOSTDEVICE
T
operator
()(
const
T
&
val
)
const
{
return
val
>
0
?
val
:
static_cast
<
T
>
(
0
);
}
};
template
<
typename
T
>
struct
Heaviside
{
HOSTDEVICE
T
operator
()(
const
T
&
val
)
const
{
return
static_cast
<
T
>
(
val
>
0
?
1
:
0
);
}
};
template
<
typename
Place
,
typename
T
>
class
MarginRankLossKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
auto
*
out_t
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
auto
*
act_t
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Activated"
);
auto
*
label_t
=
ctx
.
Input
<
framework
::
Tensor
>
(
"Label"
);
auto
*
x1_t
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X1"
);
auto
*
x2_t
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X2"
);
out_t
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
act_t
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
margin
=
static_cast
<
T
>
(
ctx
.
Attr
<
T
>
(
"margin"
));
auto
out
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
out_t
);
auto
act
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
act_t
);
auto
label
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
label_t
);
auto
x1
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
x1_t
);
auto
x2
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
x2_t
);
auto
&
dev
=
ctx
.
GetEigenDevice
<
Place
>
();
out
.
device
(
dev
)
=
(
-
label
*
(
x1
-
x2
)
+
margin
).
unaryExpr
(
ReLU
<
T
>
());
act
.
device
(
dev
)
=
out
.
unaryExpr
(
Heaviside
<
T
>
());
}
};
template
<
typename
Place
,
typename
T
>
class
MarginRankLossGradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
auto
*
d_x1_t
=
ctx
.
Output
<
framework
::
LoDTensor
>
(
framework
::
GradVarName
(
"X1"
));
auto
*
d_x2_t
=
ctx
.
Output
<
framework
::
LoDTensor
>
(
framework
::
GradVarName
(
"X2"
));
auto
*
act_t
=
ctx
.
Input
<
framework
::
Tensor
>
(
"Activated"
);
auto
*
d_out_t
=
ctx
.
Input
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
label_t
=
ctx
.
Input
<
framework
::
Tensor
>
(
"Label"
);
auto
d_out
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
d_out_t
);
auto
act
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
act_t
);
auto
label
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
label_t
);
auto
&
dev
=
ctx
.
GetEigenDevice
<
Place
>
();
// compute d_x1
if
(
d_x1_t
)
{
d_x1_t
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
d_x1
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
d_x1_t
);
d_x1
.
device
(
dev
)
=
-
d_out
*
act
*
label
;
}
// compute d_x2
if
(
d_x2_t
)
{
d_x2_t
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
d_x2
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
d_x2_t
);
d_x2
.
device
(
dev
)
=
d_out
*
act
*
label
;
}
}
};
}
// namespace operators
}
// namespace paddle
paddle/operators/math/CMakeLists.txt
浏览文件 @
557c7ae3
if
(
WITH_GPU
)
nv_library
(
math_function SRCS math_function.cc math_function.cu im2col.cc im2col.cu
pooling.cc pooling.cu
DEPS cblas device_context operator
)
nv_library
(
math_function SRCS math_function.cc math_function.cu im2col.cc im2col.cu DEPS cblas device_context operator
)
nv_test
(
math_function_test SRCS math_function_test.cc DEPS math_function tensor
)
nv_library
(
softmax SRCS softmax.cc softmax.cu DEPS operator
)
nv_library
(
cross_entropy SRCS cross_entropy.cc cross_entropy.cu DEPS operator
)
nv_library
(
pooling SRCS pooling.cc pooling.cu DEPS device_context
)
nv_library
(
vol2col SRCS vol2col.cc vol2col.cu DEPS device_context
)
else
()
cc_library
(
math_function SRCS math_function.cc im2col.cc
pooling.cc
DEPS cblas device_context operator
)
cc_library
(
math_function SRCS math_function.cc im2col.cc DEPS cblas device_context operator
)
cc_test
(
math_function_test SRCS math_function_test.cc DEPS math_function tensor
)
cc_library
(
softmax SRCS softmax.cc DEPS operator
)
cc_library
(
cross_entropy SRCS cross_entropy.cc DEPS operator
)
cc_library
(
pooling SRCS pooling.cc DEPS device_context
)
cc_library
(
vol2col SRCS vol2col.cc DEPS device_context
)
endif
()
...
...
paddle/operators/math/im2col_test.cc
浏览文件 @
557c7ae3
...
...
@@ -49,10 +49,22 @@ void testIm2col() {
memcpy
(
input_ptr
,
arr
,
6
*
sizeof
(
float
));
auto
*
place
=
new
Place
();
paddle
::
platform
::
DeviceContext
*
context
;
if
(
paddle
::
platform
::
is_cpu_place
(
*
place
))
{
context
=
new
paddle
::
platform
::
CPUDeviceContext
(
paddle
::
platform
::
CPUPlace
());
}
else
{
#ifdef PADDLE_WITH_CUDA
context
=
new
paddle
::
platform
::
CUDADeviceContext
(
paddle
::
platform
::
GPUPlace
());
#else
PADDLE_THROW
(
"no GPU support"
);
#endif // PADDLE_ONLY_CPU
}
if
(
paddle
::
platform
::
is_cpu_place
(
*
place
))
{
input
=
input_tmp
;
}
else
{
input
.
CopyFrom
<
float
>
(
input_tmp
,
*
place
);
input
.
CopyFrom
<
float
>
(
input_tmp
,
*
place
,
*
context
);
}
output_cfo
.
mutable_data
<
float
>
(
{
1
,
filter_size
,
filter_size
,
output_height
,
output_width
},
*
place
);
...
...
@@ -66,18 +78,6 @@ void testIm2col() {
paddle
::
operators
::
math
::
ColFormat
::
kOCF
,
Place
,
float
>
im2col_ocf
;
paddle
::
platform
::
DeviceContext
*
context
;
if
(
paddle
::
platform
::
is_cpu_place
(
*
place
))
{
context
=
new
paddle
::
platform
::
CPUDeviceContext
(
paddle
::
platform
::
CPUPlace
());
}
else
{
#ifdef PADDLE_WITH_CUDA
context
=
new
paddle
::
platform
::
CUDADeviceContext
(
paddle
::
platform
::
GPUPlace
());
#else
PADDLE_THROW
(
"no GPU support"
);
#endif // PADDLE_ONLY_CPU
}
im2col
(
*
context
,
input
,
output_cfo
,
stride
,
stride
,
padding
,
padding
);
im2col_ocf
(
*
context
,
input
,
output_ocf
,
stride
,
stride
,
padding
,
padding
);
...
...
@@ -85,7 +85,8 @@ void testIm2col() {
if
(
paddle
::
platform
::
is_cpu_place
(
*
place
))
{
out_cfo_ptr
=
output_cfo
.
data
<
float
>
();
}
else
{
output_tmp
.
CopyFrom
<
float
>
(
output_cfo
,
paddle
::
platform
::
CPUPlace
());
output_tmp
.
CopyFrom
<
float
>
(
output_cfo
,
paddle
::
platform
::
CPUPlace
(),
*
context
);
out_cfo_ptr
=
output_tmp
.
data
<
float
>
();
}
EXPECT_EQ
(
out_cfo_ptr
[
0
],
0
);
...
...
@@ -101,7 +102,8 @@ void testIm2col() {
if
(
paddle
::
platform
::
is_cpu_place
(
*
place
))
{
out_ocf_ptr
=
output_ocf
.
data
<
float
>
();
}
else
{
output_tmp
.
CopyFrom
<
float
>
(
output_ocf
,
paddle
::
platform
::
CPUPlace
());
output_tmp
.
CopyFrom
<
float
>
(
output_ocf
,
paddle
::
platform
::
CPUPlace
(),
*
context
);
out_ocf_ptr
=
output_tmp
.
data
<
float
>
();
}
EXPECT_EQ
(
out_ocf_ptr
[
0
],
0
);
...
...
paddle/operators/math/math_function_test.cc
浏览文件 @
557c7ae3
...
...
@@ -17,17 +17,18 @@ TEST(math_function, notrans_mul_trans) {
auto
*
gpu_place
=
new
paddle
::
platform
::
GPUPlace
(
0
);
paddle
::
platform
::
CUDADeviceContext
context
(
*
gpu_place
);
input1_gpu
.
CopyFrom
<
float
>
(
input1
,
*
gpu_place
);
input2_gpu
.
CopyFrom
<
float
>
(
input1
,
*
gpu_place
);
input1_gpu
.
CopyFrom
<
float
>
(
input1
,
*
gpu_place
,
context
);
input2_gpu
.
CopyFrom
<
float
>
(
input1
,
*
gpu_place
,
context
);
out_gpu
.
mutable_data
<
float
>
({
2
,
2
},
*
gpu_place
);
paddle
::
operators
::
math
::
matmul
<
paddle
::
platform
::
GPUPlace
,
float
>
(
context
,
input1_gpu
,
false
,
input2_gpu
,
true
,
1
,
&
out_gpu
,
0
);
out
.
CopyFrom
<
float
>
(
out_gpu
,
*
cpu_place
);
out
.
CopyFrom
<
float
>
(
out_gpu
,
*
cpu_place
,
context
);
float
*
out_ptr
=
out
.
data
<
float
>
();
context
.
Wait
();
EXPECT_EQ
(
out_ptr
[
0
],
5
);
EXPECT_EQ
(
out_ptr
[
1
],
14
);
EXPECT_EQ
(
out_ptr
[
2
],
14
);
...
...
@@ -50,17 +51,18 @@ TEST(math_function, trans_mul_notrans) {
auto
*
gpu_place
=
new
paddle
::
platform
::
GPUPlace
(
0
);
paddle
::
platform
::
CUDADeviceContext
context
(
*
gpu_place
);
input1_gpu
.
CopyFrom
<
float
>
(
input1
,
*
gpu_place
);
input2_gpu
.
CopyFrom
<
float
>
(
input1
,
*
gpu_place
);
input1_gpu
.
CopyFrom
<
float
>
(
input1
,
*
gpu_place
,
context
);
input2_gpu
.
CopyFrom
<
float
>
(
input1
,
*
gpu_place
,
context
);
out_gpu
.
mutable_data
<
float
>
({
3
,
3
},
*
gpu_place
);
paddle
::
operators
::
math
::
matmul
<
paddle
::
platform
::
GPUPlace
,
float
>
(
context
,
input1_gpu
,
true
,
input2_gpu
,
false
,
1
,
&
out_gpu
,
0
);
out
.
CopyFrom
<
float
>
(
out_gpu
,
*
cpu_place
);
out
.
CopyFrom
<
float
>
(
out_gpu
,
*
cpu_place
,
context
);
float
*
out_ptr
=
out
.
data
<
float
>
();
context
.
Wait
();
EXPECT_EQ
(
out_ptr
[
0
],
9
);
EXPECT_EQ
(
out_ptr
[
1
],
12
);
EXPECT_EQ
(
out_ptr
[
2
],
15
);
...
...
@@ -98,9 +100,9 @@ TEST(math_function, gemm_notrans_cublas) {
auto
*
gpu_place
=
new
paddle
::
platform
::
GPUPlace
(
0
);
paddle
::
platform
::
CUDADeviceContext
context
(
*
gpu_place
);
input1_gpu
.
CopyFrom
<
float
>
(
input1
,
*
gpu_place
);
input2_gpu
.
CopyFrom
<
float
>
(
input2
,
*
gpu_place
);
input3_gpu
.
CopyFrom
<
float
>
(
input3
,
*
gpu_place
);
input1_gpu
.
CopyFrom
<
float
>
(
input1
,
*
gpu_place
,
context
);
input2_gpu
.
CopyFrom
<
float
>
(
input2
,
*
gpu_place
,
context
);
input3_gpu
.
CopyFrom
<
float
>
(
input3
,
*
gpu_place
,
context
);
float
*
a
=
input1_gpu
.
data
<
float
>
();
float
*
b
=
input2_gpu
.
data
<
float
>
();
float
*
c
=
input3_gpu
.
mutable_data
<
float
>
(
*
gpu_place
);
...
...
@@ -108,7 +110,7 @@ TEST(math_function, gemm_notrans_cublas) {
paddle
::
operators
::
math
::
gemm
<
paddle
::
platform
::
GPUPlace
,
float
>
(
context
,
false
,
false
,
m
,
n
,
k
,
1
,
a
,
3
,
b
+
1
,
4
,
1
,
c
+
1
,
4
);
input3
.
CopyFrom
<
float
>
(
input3_gpu
,
*
cpu_place
);
input3
.
CopyFrom
<
float
>
(
input3_gpu
,
*
cpu_place
,
context
);
// numpy code:
// a = np.arange(6).reshape(2, 3)
...
...
@@ -116,6 +118,7 @@ TEST(math_function, gemm_notrans_cublas) {
// c = np.arange(8).reshape(2, 4)[:, 1:]
// out = np.arange(8).reshape(2, 4)
// out[:, 1:] = np.dot(a, b) + c
context
.
Wait
();
EXPECT_EQ
(
input3_ptr
[
0
],
0
);
EXPECT_EQ
(
input3_ptr
[
1
],
24
);
EXPECT_EQ
(
input3_ptr
[
2
],
28
);
...
...
@@ -152,9 +155,9 @@ TEST(math_function, gemm_trans_cublas) {
auto
*
gpu_place
=
new
paddle
::
platform
::
GPUPlace
(
0
);
paddle
::
platform
::
CUDADeviceContext
context
(
*
gpu_place
);
input1_gpu
.
CopyFrom
<
float
>
(
input1
,
*
gpu_place
);
input2_gpu
.
CopyFrom
<
float
>
(
input2
,
*
gpu_place
);
input3_gpu
.
CopyFrom
<
float
>
(
input3
,
*
gpu_place
);
input1_gpu
.
CopyFrom
<
float
>
(
input1
,
*
gpu_place
,
context
);
input2_gpu
.
CopyFrom
<
float
>
(
input2
,
*
gpu_place
,
context
);
input3_gpu
.
CopyFrom
<
float
>
(
input3
,
*
gpu_place
,
context
);
float
*
a
=
input1_gpu
.
data
<
float
>
();
float
*
b
=
input2_gpu
.
data
<
float
>
();
float
*
c
=
input3_gpu
.
mutable_data
<
float
>
(
*
gpu_place
);
...
...
@@ -162,7 +165,8 @@ TEST(math_function, gemm_trans_cublas) {
paddle
::
operators
::
math
::
gemm
<
paddle
::
platform
::
GPUPlace
,
float
>
(
context
,
false
,
true
,
m
,
n
,
k
,
1
,
a
,
3
,
b
+
3
,
3
,
1
,
c
+
1
,
4
);
input3
.
CopyFrom
<
float
>
(
input3_gpu
,
*
cpu_place
);
input3
.
CopyFrom
<
float
>
(
input3_gpu
,
*
cpu_place
,
context
);
context
.
Wait
();
EXPECT_EQ
(
input3_ptr
[
0
],
0
);
EXPECT_EQ
(
input3_ptr
[
1
],
24
);
...
...
paddle/operators/math/vol2col_test.cc
浏览文件 @
557c7ae3
...
...
@@ -78,7 +78,7 @@ void testVol2col() {
if
(
paddle
::
platform
::
is_cpu_place
(
*
place
))
{
input
=
input_tmp
;
}
else
{
input
.
CopyFrom
<
float
>
(
input_tmp
,
*
place
);
input
.
CopyFrom
<
float
>
(
input_tmp
,
*
place
,
*
context
);
}
output
.
mutable_data
<
float
>
({
1
,
filter_size
,
filter_size
,
filter_size
,
output_depth
,
output_height
,
output_width
},
...
...
@@ -93,7 +93,7 @@ void testVol2col() {
if
(
paddle
::
platform
::
is_cpu_place
(
*
place
))
{
out_cfo_ptr
=
output
.
data
<
float
>
();
}
else
{
output_tmp
.
CopyFrom
<
float
>
(
output
,
paddle
::
platform
::
CPUPlace
());
output_tmp
.
CopyFrom
<
float
>
(
output
,
paddle
::
platform
::
CPUPlace
()
,
*
context
);
out_cfo_ptr
=
output_tmp
.
data
<
float
>
();
}
...
...
@@ -107,7 +107,7 @@ void testVol2col() {
if
(
paddle
::
platform
::
is_cpu_place
(
*
place
))
{
input
=
input_tmp
;
}
else
{
input
.
CopyFrom
<
float
>
(
input_tmp
,
*
place
);
input
.
CopyFrom
<
float
>
(
input_tmp
,
*
place
,
*
context
);
}
paddle
::
operators
::
math
::
Col2VolFunctor
<
Place
,
float
>
col2vol
;
...
...
@@ -118,7 +118,7 @@ void testVol2col() {
if
(
paddle
::
platform
::
is_cpu_place
(
*
place
))
{
in_ptr
=
input
.
data
<
float
>
();
}
else
{
input_tmp
.
CopyFrom
<
float
>
(
input
,
paddle
::
platform
::
CPUPlace
());
input_tmp
.
CopyFrom
<
float
>
(
input
,
paddle
::
platform
::
CPUPlace
()
,
*
context
);
in_ptr
=
input_tmp
.
data
<
float
>
();
}
...
...
paddle/operators/multiplex_op.cu
浏览文件 @
557c7ae3
...
...
@@ -33,7 +33,8 @@ class MultiplexGPUKernel : public framework::OpKernel<T> {
auto
cols
=
ins
[
0
]
->
numel
()
/
rows
;
// copy index to cpu
Tensor
index_t_cpu
;
index_t_cpu
.
CopyFrom
<
int32_t
>
(
*
ids
,
platform
::
CPUPlace
());
index_t_cpu
.
CopyFrom
<
int32_t
>
(
*
ids
,
platform
::
CPUPlace
(),
ctx
.
device_context
());
auto
*
index
=
index_t_cpu
.
data
<
int32_t
>
();
auto
stream
=
reinterpret_cast
<
const
platform
::
CUDADeviceContext
&>
(
ctx
.
device_context
())
...
...
@@ -70,7 +71,8 @@ class MultiplexGradGPUKernel : public framework::OpKernel<T> {
auto
cols
=
ins
[
0
]
->
numel
()
/
rows
;
// copy index to cpu
Tensor
index_t_cpu
;
index_t_cpu
.
CopyFrom
<
int32_t
>
(
*
ids
,
platform
::
CPUPlace
());
index_t_cpu
.
CopyFrom
<
int32_t
>
(
*
ids
,
platform
::
CPUPlace
(),
ctx
.
device_context
());
auto
*
index
=
index_t_cpu
.
data
<
int32_t
>
();
auto
stream
=
reinterpret_cast
<
const
platform
::
CUDADeviceContext
&>
(
...
...
paddle/operators/pool_op.cc
浏览文件 @
557c7ae3
...
...
@@ -22,157 +22,181 @@ int OutputSizePool(int input_size, int filter_size, int padding, int stride) {
return
output_size
;
}
class
PoolOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"X(Input) of Pooling should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Out(Output) of Pooling should not be null."
);
auto
in_x_dims
=
ctx
->
GetInputDim
(
"X"
);
std
::
string
pooling_type
=
ctx
->
Attrs
().
Get
<
std
::
string
>
(
"poolingType"
);
std
::
vector
<
int
>
ksize
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"ksize"
);
std
::
vector
<
int
>
strides
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"strides"
);
std
::
vector
<
int
>
paddings
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"paddings"
);
PADDLE_ENFORCE
(
pooling_type
==
"max"
||
pooling_type
==
"avg"
,
"pooling_type should be 'max' or 'avg'"
);
PADDLE_ENFORCE
(
in_x_dims
.
size
()
==
4
||
in_x_dims
.
size
()
==
5
,
"Pooling intput should be 4-D or 5-D"
);
if
(
ctx
->
Attrs
().
Get
<
bool
>
(
"globalPooling"
))
{
ksize
.
resize
(
static_cast
<
size_t
>
(
in_x_dims
.
size
())
-
2
);
for
(
size_t
i
=
0
;
i
<
ksize
.
size
();
++
i
)
ksize
[
i
]
=
static_cast
<
int
>
(
in_x_dims
[
i
+
2
]);
}
PADDLE_ENFORCE
(
in_x_dims
.
size
()
-
ksize
.
size
()
==
2U
,
"Input size and Pooling size should be consistent."
);
PADDLE_ENFORCE
(
ksize
.
size
()
==
2
||
ksize
.
size
()
==
3
,
"Pooling size should be 2 elements. or 3 elements."
);
PADDLE_ENFORCE_EQ
(
ksize
.
size
(),
strides
.
size
(),
"strides size and pooling size should be the same."
);
PADDLE_ENFORCE_EQ
(
ksize
.
size
(),
paddings
.
size
(),
"paddings size and pooling size should be the same."
);
std
::
vector
<
int64_t
>
output_shape
({
in_x_dims
[
0
],
in_x_dims
[
1
]});
for
(
size_t
i
=
0
;
i
<
ksize
.
size
();
++
i
)
{
output_shape
.
push_back
(
OutputSizePool
(
in_x_dims
[
i
+
2
],
ksize
[
i
],
paddings
[
i
],
strides
[
i
]));
}
ctx
->
SetOutputDim
(
"Out"
,
framework
::
make_ddim
(
output_shape
));
void
PoolOp
::
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"X(Input) of Pooling should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Out(Output) of Pooling should not be null."
);
auto
in_x_dims
=
ctx
->
GetInputDim
(
"X"
);
std
::
string
pooling_type
=
ctx
->
Attrs
().
Get
<
std
::
string
>
(
"poolingType"
);
std
::
vector
<
int
>
ksize
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"ksize"
);
std
::
vector
<
int
>
strides
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"strides"
);
std
::
vector
<
int
>
paddings
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"paddings"
);
PADDLE_ENFORCE
(
in_x_dims
.
size
()
==
4
||
in_x_dims
.
size
()
==
5
,
"Pooling intput should be 4-D or 5-D tensor."
);
if
(
ctx
->
Attrs
().
Get
<
bool
>
(
"globalPooling"
))
{
ksize
.
resize
(
static_cast
<
size_t
>
(
in_x_dims
.
size
())
-
2
);
for
(
size_t
i
=
0
;
i
<
ksize
.
size
();
++
i
)
ksize
[
i
]
=
static_cast
<
int
>
(
in_x_dims
[
i
+
2
]);
}
};
class
PoolOpGrad
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"X(Input) of Pooling should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
framework
::
GradVarName
(
"X"
)),
"Input@Grad of Pooling should not be null."
);
ctx
->
SetOutputDim
(
framework
::
GradVarName
(
"X"
),
ctx
->
GetInputDim
(
"X"
));
PADDLE_ENFORCE
(
in_x_dims
.
size
()
-
ksize
.
size
()
==
2U
,
"Input size and pooling size should be consistent."
);
PADDLE_ENFORCE_EQ
(
ksize
.
size
(),
strides
.
size
(),
"Strides size and pooling size should be the same."
);
PADDLE_ENFORCE_EQ
(
ksize
.
size
(),
paddings
.
size
(),
"Paddings size and pooling size should be the same."
);
std
::
vector
<
int64_t
>
output_shape
({
in_x_dims
[
0
],
in_x_dims
[
1
]});
for
(
size_t
i
=
0
;
i
<
ksize
.
size
();
++
i
)
{
output_shape
.
push_back
(
OutputSizePool
(
in_x_dims
[
i
+
2
],
ksize
[
i
],
paddings
[
i
],
strides
[
i
]));
}
};
class
Pool2dOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
Pool2dOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"The input tensor of pooling operator. "
"The format of input tensor is NCHW. Where N is batch size, C is the "
"number of channels, H and W is the height and width of feature."
);
AddOutput
(
"Out"
,
"The output tensor of pooling operator."
"The format of output tensor is also NCHW."
);
AddAttr
<
std
::
string
>
(
"poolingType"
,
"PoolingType of pooling operator."
"Str constant equal to 'max' or 'avg'."
)
.
InEnum
({
"max"
,
"avg"
});
AddAttr
<
std
::
vector
<
int
>>
(
"ksize"
,
"Pooling size(depth, height, width) of pooling operator."
"If globalPooling = true, ksize is ignored and need not be "
"specified."
);
// TODO(Add checker)
AddAttr
<
bool
>
(
"globalPooling"
,
"Whether to use the globalPooling."
"Bool constant equal to false or true."
"Default false."
"If globalPooling = true, ksize is ignored and need not be specified."
)
.
SetDefault
(
false
);
AddAttr
<
std
::
vector
<
int
>>
(
"strides"
,
"Strides(height, width) of pooling operator."
"Default {1,1}"
)
.
SetDefault
({
1
,
1
});
// TODO(Add checker)
AddAttr
<
std
::
vector
<
int
>>
(
"paddings"
,
"Paddings(height, width) of pooling operator."
"Default {0,0}."
)
.
SetDefault
({
0
,
0
});
// TODO(Add checker)
AddComment
(
R"DOC(
ctx
->
SetOutputDim
(
"Out"
,
framework
::
make_ddim
(
output_shape
));
}
void
PoolOpGrad
::
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) must not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
framework
::
GradVarName
(
"X"
)),
"Input(X@GRAD) should not be null."
);
ctx
->
SetOutputDim
(
framework
::
GradVarName
(
"X"
),
ctx
->
GetInputDim
(
"X"
));
}
Pool2dOpMaker
::
Pool2dOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"(Tensor) The input tensor of pooling operator. "
"The format of input tensor is NCHW. Where N is batch size, C is the "
"number of channels, H and W is the height and width of feature."
);
AddOutput
(
"Out"
,
"(Tensor) The output tensor of pooling operator."
"The format of output tensor is also NCHW."
"Where N is batch size, C is "
"the number of channels, H and W is the height and "
"width of feature."
);
AddAttr
<
std
::
string
>
(
"poolingType"
,
"PoolingType of pooling operator."
"Str constant equal to 'max' or 'avg'."
)
.
InEnum
({
"max"
,
"avg"
});
AddAttr
<
std
::
vector
<
int
>>
(
"ksize"
,
"The pooling window size(height, width) of pooling operator."
"If globalPooling = true, ksize is ignored and need not be "
"specified."
);
// TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
AddAttr
<
bool
>
(
"globalPooling"
,
"Whether to use the globalPooling."
"Bool constant equal to false or true."
"Default false."
"If globalPooling = true, ksize is ignored and need not be specified."
)
.
SetDefault
(
false
);
AddAttr
<
std
::
vector
<
int
>>
(
"strides"
,
"The strides(height, width) of pooling window."
"Default {1,1}."
)
.
SetDefault
({
1
,
1
});
// TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
AddAttr
<
std
::
vector
<
int
>>
(
"paddings"
,
"The zero padding(height, width) size on both sides"
"Default {0,0}."
)
.
SetDefault
({
0
,
0
});
// TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
AddComment
(
R"DOC(
The pooling2d operation calculates the output based on
the input, poolingType and ksize, strides, paddings parameters.
Input(X) and output(Out) are in NCHW format. Where N is batch size, C is the
number of channels, H and W is the height and width of feature.
Parameters(ksize, strides, paddings) are two elements.
These two elements represent height and width, respectively.
The input(X) size and output(Out) size may be different.
Example:
Input:
X shape: (N, C, H_in, W_in)
Output:
Out shape: (N, C, H_out, W_out)
Mask shape: (N, C, H_out, W_out)
where
H_out = (H_in - ksize[0] + 2 * paddings[0]) / strides[0] + 1;
W_out = (W_in - ksize[1] + 2 * paddings[1]) / strides[1] + 1;
)DOC"
);
}
};
class
Pool3dOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
Pool3dOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"The input tensor of pooling operator. "
"The format of input tensor is NCDHW. Where N is batch size, C is "
"the "
"number of channels, D, H and W is the depth, height and width of "
"feature."
);
AddOutput
(
"Out"
,
"The output tensor of pooling operator."
"The format of output tensor is also NCDHW."
);
AddAttr
<
std
::
string
>
(
"poolingType"
,
"PoolingType of pooling operator."
"str constant equal to 'max' or 'avg'."
)
.
InEnum
({
"max"
,
"avg"
});
AddAttr
<
std
::
vector
<
int
>>
(
"ksize"
,
"Pooling size(depth, height, width) of pooling operator."
"If globalPooling = true, ksize is ignored and need not be "
"specified."
);
// TODO(Add checker)
AddAttr
<
bool
>
(
"globalPooling"
,
"Whether to use the globalPooling."
"Bool constant equal to false or true."
"Default false."
"If globalPooling = true, ksize is ignored and need not be specified."
)
.
SetDefault
(
false
);
AddAttr
<
std
::
vector
<
int
>>
(
"strides"
,
"Strides(depth, height, width) of pooling operator."
"Default {1,1,1}."
)
.
SetDefault
({
1
,
1
,
1
});
// TODO(Add checker)
AddAttr
<
std
::
vector
<
int
>>
(
"paddings"
,
"Paddings(depth, height, width) of pooling operator."
"Default {0,0,0}."
)
.
SetDefault
({
0
,
0
,
0
});
// TODO(Add checker)
AddComment
(
R"DOC(
}
Pool3dOpMaker
::
Pool3dOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"(Tensor) The input tensor of pooling operator. "
"The format of input tensor is NCDHW. Where N is batch size, C is "
"the number of channels, D, H and W is the depth, height and width of "
"feature."
);
AddOutput
(
"Out"
,
"(Tensor) The output tensor of pooling operator."
"The format of output tensor is also NCDHW."
"Where N is batch size, C is "
"the number of channels, D, H and W is the depth, height and "
"width of feature."
);
AddAttr
<
std
::
string
>
(
"poolingType"
,
"PoolingType of pooling operator."
"Str constant equal to 'max' or 'avg'."
)
.
InEnum
({
"max"
,
"avg"
});
AddAttr
<
std
::
vector
<
int
>>
(
"ksize"
,
"The pooling window size(depth, height, width) of pooling operator."
"If globalPooling = true, ksize is ignored and need not be "
"specified."
);
// TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
AddAttr
<
bool
>
(
"globalPooling"
,
"Whether to use the globalPooling."
"Bool constant equal to false or true."
"Default false."
"If globalPooling = true, ksize is ignored and need not be specified."
)
.
SetDefault
(
false
);
AddAttr
<
std
::
vector
<
int
>>
(
"strides"
,
"Strides(depth, height, width) of pooling operator."
"Default {1,1,1}."
)
.
SetDefault
({
1
,
1
,
1
});
// TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
AddAttr
<
std
::
vector
<
int
>>
(
"paddings"
,
"Paddings(depth, height, width) of pooling operator."
"Default {0,0,0}."
)
.
SetDefault
({
0
,
0
,
0
});
// TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
AddComment
(
R"DOC(
The pooling3d operation calculates the output based on
the input, poolingType and ksize, strides, paddings parameters.
Input(X) and output(Out) are in NCDHW format. Where N is batch
size, C is the number of channels, D, H and W is the depth, height and
width of feature. Parameters(ksize, strides, paddings) are three elements.
These three elements represent depth, height and width, respectively.
The input(X) size and output(Out) size may be different.
Example:
Input:
X shape: (N, C, D_in, H_in, W_in)
Output:
Out shape: (N, C, D_out, H_out, W_out)
Mask shape: (N, C, D_out, H_out, W_out)
where
D_out = (D_in - ksize[0] + 2 * paddings[0]) / strides[0] + 1;
H_out = (H_in - ksize[1] + 2 * paddings[1]) / strides[1] + 1;
W_out = (W_in - ksize[2] + 2 * paddings[2]) / strides[2] + 1;
)DOC"
);
}
};
}
}
// namespace operators
}
// namespace paddle
...
...
paddle/operators/pool_op.h
浏览文件 @
557c7ae3
...
...
@@ -24,6 +24,34 @@ namespace operators {
using
Tensor
=
framework
::
Tensor
;
class
PoolOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
;
};
class
PoolOpGrad
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
;
};
class
Pool2dOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
Pool2dOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
);
};
class
Pool3dOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
Pool3dOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
);
};
template
<
typename
Place
,
typename
T
>
class
PoolKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
...
...
paddle/operators/pool_with_index_op.cc
浏览文件 @
557c7ae3
...
...
@@ -43,7 +43,7 @@ class MaxPoolWithIndexOp : public framework::OperatorWithKernel {
std
::
vector
<
int
>
paddings
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"paddings"
);
PADDLE_ENFORCE
(
in_x_dims
.
size
()
==
4
||
in_x_dims
.
size
()
==
5
,
"Pooling intput should be 4-D or 5-D"
);
"Pooling intput should be 4-D or 5-D
tensor.
"
);
if
(
ctx
->
Attrs
().
Get
<
bool
>
(
"globalPooling"
))
{
ksize
.
resize
(
static_cast
<
size_t
>
(
in_x_dims
.
size
())
-
2
);
...
...
@@ -52,7 +52,7 @@ class MaxPoolWithIndexOp : public framework::OperatorWithKernel {
}
PADDLE_ENFORCE
(
in_x_dims
.
size
()
-
ksize
.
size
()
==
2U
,
"In
t
put size and pooling size should be consistent."
);
"Input size and pooling size should be consistent."
);
PADDLE_ENFORCE_EQ
(
ksize
.
size
(),
strides
.
size
(),
"Strides size and pooling size should be the same."
);
PADDLE_ENFORCE_EQ
(
ksize
.
size
(),
paddings
.
size
(),
...
...
@@ -74,6 +74,7 @@ class MaxPoolWithIndexOpGrad : public framework::OperatorWithKernel {
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Mask"
),
"Input(Mask) must not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) must not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
framework
::
GradVarName
(
"X"
)),
"Input(X@GRAD) should not be null."
);
...
...
@@ -88,17 +89,17 @@ class MaxPool2dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker {
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"The input tensor of pooling operator. "
"
(Tensor)
The input tensor of pooling operator. "
"The format of input tensor is NCHW. Where N is batch size, C is the "
"number of channels, H and W is the height and width of image."
);
AddOutput
(
"Out"
,
"The output tensor of pooling operator."
"
(Tensor)
The output tensor of pooling operator."
"The format of output tensor is also NCHW."
"Where N is batch size, C is "
"the number of channels, H and W is the height and "
"width of image."
);
AddOutput
(
"Mask"
,
"The Mask tensor of pooling operator."
"
(Tensor)
The Mask tensor of pooling operator."
"The format of output tensor is also NCHW."
"Where N is batch size, C is the number of channels, H and W "
"is the height and width of image."
...
...
@@ -106,7 +107,7 @@ class MaxPool2dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker {
AddAttr
<
std
::
vector
<
int
>>
(
"ksize"
,
"The pooling size(height, width) of pooling operator."
"The pooling
window
size(height, width) of pooling operator."
"If globalPooling = true, ksize is ignored and need not be "
"specified."
);
// TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
...
...
@@ -118,13 +119,14 @@ class MaxPool2dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker {
"If globalPooling = true, ksize is ignored and need not be specified."
)
.
SetDefault
(
false
);
AddAttr
<
std
::
vector
<
int
>>
(
"strides"
,
"
Strides(height, width) of pooling operator
."
"
The strides(height, width) of pooling window
."
"Default {1,1}."
)
.
SetDefault
({
1
,
1
});
// TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
AddAttr
<
std
::
vector
<
int
>>
(
"paddings"
,
"Paddings(height, width) of pooling operator."
"Default {0,0}."
)
AddAttr
<
std
::
vector
<
int
>>
(
"paddings"
,
"The zero padding(height, width) size on both sides"
"Default {0,0}."
)
.
SetDefault
({
0
,
0
});
// TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
...
...
@@ -135,6 +137,17 @@ output(Out, Mask) are in NCHW format. Where N is batch size, C is the
number of channels, H and W is the height and width of feature.
Parameters(ksize, strides, paddings) are two elements.
These two elements represent height and width, respectively.
The input(X) size and output(Out, Mask) size may be different.
Example:
Input:
X shape: (N, C, H_in, W_in)
Output:
Out shape: (N, C, H_out, W_out)
Mask shape: (N, C, H_out, W_out)
where
H_out = (H_in - ksize[0] + 2 * paddings[0]) / strides[0] + 1;
W_out = (W_in - ksize[1] + 2 * paddings[1]) / strides[1] + 1;
)DOC"
);
}
};
...
...
@@ -146,18 +159,18 @@ class MaxPool3dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker {
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"The input tensor of pooling operator. "
"
(Tensor)
The input tensor of pooling operator. "
"The format of input tensor is NCDHW. Where N is batch size, C is "
"the number of channels, D, H and W is the depth, height and width of "
"image."
);
AddOutput
(
"Out"
,
"The output tensor of pooling operator."
"
(Tensor)
The output tensor of pooling operator."
"The format of output tensor is also NCDHW."
"Where N is batch size, C is "
"the number of channels, D, H and W is the depth, height and "
"width of image."
);
AddOutput
(
"Mask"
,
"The Mask tensor of pooling operator."
"
(Tensor)
The Mask tensor of pooling operator."
"The format of output tensor is also NCDHW."
"Where N is batch size, C is the number of channels, D, H and W "
"is the depth, height and width of image."
...
...
@@ -165,7 +178,7 @@ class MaxPool3dWithIndexOpMaker : public framework::OpProtoAndCheckerMaker {
AddAttr
<
std
::
vector
<
int
>>
(
"ksize"
,
"The pooling size(depth, height, width) of pooling operator."
"The pooling
window
size(depth, height, width) of pooling operator."
"If globalPooling = true, ksize is ignored and need not be "
"specified."
);
// TODO(Chengduo): Add checker. (Currently,
// TypedAttrChecker don't support vector type.)
...
...
@@ -196,6 +209,18 @@ Input(X) and output(Out, Mask) are in NCDHW format. Where N is batch
size, C is the number of channels, D, H and W is the depth, height and
width of feature. Parameters(ksize, strides, paddings) are three elements.
These three elements represent depth, height and width, respectively.
The input(X) size and output(Out, Mask) size may be different.
Example:
Input:
X shape: (N, C, D_in, H_in, W_in)
Output:
Out shape: (N, C, D_out, H_out, W_out)
Mask shape: (N, C, D_out, H_out, W_out)
where
D_out = (D_in - ksize[0] + 2 * paddings[0]) / strides[0] + 1;
H_out = (H_in - ksize[1] + 2 * paddings[1]) / strides[1] + 1;
W_out = (W_in - ksize[2] + 2 * paddings[2]) / strides[2] + 1;
)DOC"
);
}
};
...
...
paddle/operators/recurrent_op.cc
浏览文件 @
557c7ae3
...
...
@@ -46,7 +46,7 @@ void RecurrentAlgorithm::Run(const Scope& scope,
}
(
*
stepnet_
)
->
Run
(
*
step_scopes
[
step_id
],
dev_ctx
);
}
rnn
::
ConcatOutputs
(
step_scopes
,
arg_
->
outlinks
,
seq_len
);
rnn
::
ConcatOutputs
(
step_scopes
,
arg_
->
outlinks
,
seq_len
,
dev_ctx
);
}
void
RecurrentAlgorithm
::
CreateScopes
(
const
Scope
&
scope
,
...
...
@@ -151,12 +151,12 @@ void RecurrentGradientAlgorithm::Run(
auto
&
step_scopes
=
GetStepScopes
(
scope
);
rnn
::
SegmentInputs
(
step_scopes
,
arg_
->
inlinks
,
seq_len
);
for
(
int
step_id
=
seq_len
-
1
;
step_id
>=
0
;
--
step_id
)
{
if
(
st
ep_id
!=
seq_len
-
1
)
{
if
(
st
atic_cast
<
size_t
>
(
step_id
)
!=
seq_len
-
1
)
{
rnn
::
LinkMemories
(
step_scopes
,
arg_
->
memories
,
step_id
,
1
);
}
(
*
stepnet_
)
->
Run
(
*
step_scopes
[
step_id
],
dev_ctx
);
}
rnn
::
ConcatOutputs
(
step_scopes
,
arg_
->
outlinks
,
seq_len
);
rnn
::
ConcatOutputs
(
step_scopes
,
arg_
->
outlinks
,
seq_len
,
dev_ctx
);
LinkBootMemoryGradients
(
step_scopes
[
0
]);
}
...
...
paddle/operators/reshape_op.h
浏览文件 @
557c7ae3
...
...
@@ -33,7 +33,7 @@ class ReshapeKernel : public framework::OpKernel<T> {
std
::
transform
(
shape
.
begin
(),
shape
.
end
(),
shape_int64
.
begin
(),
[](
int
a
)
{
return
static_cast
<
int64_t
>
(
a
);
});
auto
out_dims
=
framework
::
make_ddim
(
shape_int64
);
out
->
CopyFrom
<
T
>
(
*
in
,
ctx
.
GetPlace
());
out
->
CopyFrom
<
T
>
(
*
in
,
ctx
.
GetPlace
()
,
ctx
.
device_context
()
);
out
->
Resize
(
out_dims
);
}
};
...
...
@@ -47,7 +47,7 @@ class ReshapeGradKernel : public framework::OpKernel<T> {
d_x
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
in_dims
=
d_x
->
dims
();
d_x
->
CopyFrom
<
T
>
(
*
d_out
,
ctx
.
GetPlace
());
d_x
->
CopyFrom
<
T
>
(
*
d_out
,
ctx
.
GetPlace
()
,
ctx
.
device_context
()
);
d_x
->
Resize
(
in_dims
);
}
};
...
...
paddle/operators/rnn/recurrent_op_utils.cc
浏览文件 @
557c7ae3
...
...
@@ -51,7 +51,7 @@ void SegmentInputs(const std::vector<Scope*>& step_scopes,
void
ConcatOutputs
(
const
std
::
vector
<
Scope
*>&
step_scopes
,
const
std
::
vector
<
std
::
string
>&
outlinks
,
const
size_t
seq_len
)
{
const
size_t
seq_len
,
const
platform
::
DeviceContext
&
ctx
)
{
for
(
size_t
i
=
0
;
i
<
outlinks
.
size
();
i
++
)
{
auto
*
output_var
=
step_scopes
[
0
]
->
parent
().
FindVar
(
outlinks
[
i
]);
PADDLE_ENFORCE_NOT_NULL
(
output_var
,
"output link [%s] is not in scope."
,
...
...
@@ -72,7 +72,7 @@ void ConcatOutputs(const std::vector<Scope*>& step_scopes,
// TODO(luotao02) data type and platform::DeviceContext() should set
// correctly
(
output
->
Slice
<
float
>
(
j
,
j
+
1
))
.
CopyFrom
<
float
>
(
*
step_output
,
platform
::
CPUPlace
());
.
CopyFrom
<
float
>
(
*
step_output
,
platform
::
CPUPlace
()
,
ctx
);
}
}
}
...
...
paddle/operators/rnn/recurrent_op_utils.h
浏览文件 @
557c7ae3
...
...
@@ -71,7 +71,7 @@ void SegmentInputs(const std::vector<Scope*>& step_scopes,
*/
void
ConcatOutputs
(
const
std
::
vector
<
Scope
*>&
step_scopes
,
const
std
::
vector
<
std
::
string
>&
outlinks
,
const
size_t
seq_len
);
const
size_t
seq_len
,
const
platform
::
DeviceContext
&
ctx
);
void
LinkMemories
(
const
std
::
vector
<
Scope
*>&
step_scopes
,
const
std
::
vector
<
MemoryAttr
>&
memories
,
const
size_t
step_id
,
...
...
paddle/operators/sequence_concat_op.cc
0 → 100644
浏览文件 @
557c7ae3
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/sequence_concat_op.h"
namespace
paddle
{
namespace
operators
{
class
SequenceConcatOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInputs
(
"X"
),
"Inputs(X) of SequenceConcatOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Output(Out) of SequenceConcatOp should not be null."
);
const
size_t
level
=
static_cast
<
size_t
>
(
ctx
->
Attrs
().
Get
<
int
>
(
"level"
));
const
size_t
axis
=
static_cast
<
size_t
>
(
ctx
->
Attrs
().
Get
<
int
>
(
"axis"
));
PADDLE_ENFORCE
(
level
==
0UL
||
level
==
1UL
,
"The sequence_concat operator only accepts sequence "
"or a nested sequence as its input."
);
auto
ins_dims
=
ctx
->
GetInputsDim
(
"X"
);
framework
::
DDim
out_dims
=
ins_dims
[
0
];
const
size_t
n
=
ins_dims
.
size
();
for
(
size_t
i
=
1
;
i
<
n
;
++
i
)
{
out_dims
[
axis
]
+=
ins_dims
[
i
][
axis
];
}
ctx
->
SetOutputDim
(
"Out"
,
out_dims
);
}
};
class
SequenceConcatOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
SequenceConcatOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"(A vector of LoDTensor), the input is a vector of LoDTensor, "
"each of which is a variable-length sequence or nested sequence."
)
.
AsDuplicable
();
AddOutput
(
"Out"
,
"(A LoDTensor), the variable-length output of "
"sequence_concat Op."
);
AddAttr
<
int
>
(
"axis"
,
"(int, default 0)"
"The axis which the inputs will be joined with. "
"If axis is 0, the inputs will be joined with LoD index."
)
.
SetDefault
(
0
);
AddAttr
<
int
>
(
"level"
,
"(int, default 0)"
"The level at which the inputs will be joined. "
"If the level is 0, the inputs will be joined at the nested "
"sequence level. "
"If the level is 1, the inputs will be joined at the "
"sequence level. "
"The level should be less than the level number of inputs."
)
.
SetDefault
(
0
);
AddComment
(
R"DOC(
The sequence_concat operator concatenates multiple LoDTensors.
It only supports sequence (LoD Tensor with level number is 1)
or a nested sequence (LoD tensor with level number is 2) as its input.
- Case1:
If the axis is other than 0(here, axis is 1 and level is 1),
each input should have the same LoD information and the LoD
information of the output keeps the same as the input.
LoD(x0) = {{0,2,4}, {0,1,2,3,4}}; Dims(x0) = (4,3,4)
LoD(x1) = {{0,2,4}, {0,1,2,3,4}}; Dims(x1) = (4,4,4)
LoD(Out) = {{0,2,4}, {0,1,2,3,4}}; Dims(Out) = (4,7,4)
- Case2:
If the axis is 0(here, leve is 0), the inputs are concatenated along
time steps, the LoD information of the output need to re-compute.
LoD(x0) = {{0,2,4}, {0,1,2,3,4}}; Dims(x0) = (4,3,4)
LoD(x1) = {{0,3,5}, {0,1,2,3,5}}; Dims(x1) = (5,3,4)
LoD(Out) = {{0,5,9}, {0,1,2,3,4,5,6,7,9}}; Dims(Out) = (9,3,4)
- Case3:
If the axis is 0(here, level is 1).
LoD(x0) = {{0,2,4}, {0,1,2,3,4}}; Dims(x0) = (4,3,4)
LoD(x1) = {{0,3,5}, {0,1,3,4,5}}; Dims(x1) = (5,3,4)
LoD(Out) = {{0,5,9}, {0,2,5,7,9}}; Dims(Out) = (9,3,4)
NOTE: The levels of all the inputs should be the same.
)DOC"
);
}
};
class
SequenceConcatGradOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
framework
::
GradVarName
(
"Out"
)),
"The gradient of Out should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutputs
(
framework
::
GradVarName
(
"X"
)),
"The gradient of X should not be null."
);
ctx
->
SetOutputsDim
(
framework
::
GradVarName
(
"X"
),
ctx
->
GetInputsDim
(
"X"
));
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP
(
sequence_concat
,
ops
::
SequenceConcatOp
,
ops
::
SequenceConcatOpMaker
,
sequence_concat_grad
,
ops
::
SequenceConcatGradOp
);
REGISTER_OP_CPU_KERNEL
(
sequence_concat
,
ops
::
SequenceConcatOpKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
sequence_concat_grad
,
ops
::
SequenceConcatGradOpKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
paddle/operators/sequence_concat_op.cu
0 → 100644
浏览文件 @
557c7ae3
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#define EIGEN_USE_GPU
#include "paddle/operators/sequence_concat_op.h"
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_GPU_KERNEL
(
sequence_concat
,
ops
::
SequenceConcatOpKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
REGISTER_OP_GPU_KERNEL
(
sequence_concat_grad
,
ops
::
SequenceConcatGradOpKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
paddle/operators/sequence_concat_op.h
0 → 100644
浏览文件 @
557c7ae3
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include "paddle/framework/op_registry.h"
#include "paddle/operators/strided_memcpy.h"
namespace
paddle
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
using
LoDTensor
=
framework
::
LoDTensor
;
using
LoD
=
framework
::
LoD
;
template
<
typename
T
>
LoD
concatLoD
(
const
std
::
vector
<
const
T
*>
ins
,
const
size_t
axis
,
const
size_t
level
)
{
auto
out_lod
=
ins
[
0
]
->
lod
();
const
size_t
n
=
ins
.
size
();
if
(
axis
==
0UL
)
{
for
(
size_t
i
=
1
;
i
<
n
;
++
i
)
{
for
(
size_t
j
=
0
;
j
<
ins
[
i
]
->
lod
()[
0
].
size
();
++
j
)
{
out_lod
[
0
][
j
]
+=
ins
[
i
]
->
lod
()[
0
][
j
];
}
if
(
ins
[
0
]
->
NumLevels
()
==
2
)
{
for
(
size_t
j
=
1
;
j
<
ins
[
i
]
->
lod
()[
1
].
size
();
++
j
)
{
if
(
level
==
0UL
)
{
out_lod
[
1
].
push_back
(
out_lod
[
1
].
back
()
+
ins
[
i
]
->
lod
()[
1
][
j
]
-
ins
[
i
]
->
lod
()[
1
][
j
-
1
]);
}
else
if
(
level
==
1UL
)
{
out_lod
[
1
][
j
]
+=
ins
[
1
]
->
lod
()[
1
][
j
];
}
}
}
}
}
return
out_lod
;
}
template
<
typename
Place
,
typename
T
>
class
SequenceConcatOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
ins
=
ctx
.
MultiInput
<
LoDTensor
>
(
"X"
);
auto
*
out
=
ctx
.
Output
<
LoDTensor
>
(
"Out"
);
const
size_t
axis
=
static_cast
<
size_t
>
(
ctx
.
Attr
<
int
>
(
"axis"
));
const
size_t
level
=
static_cast
<
size_t
>
(
ctx
.
Attr
<
int
>
(
"level"
));
const
size_t
n
=
ins
.
size
();
for
(
size_t
i
=
1
;
i
<
n
;
++
i
)
{
PADDLE_ENFORCE_EQ
(
ins
[
0
]
->
NumLevels
(),
ins
[
i
]
->
NumLevels
(),
"The levels of all the input LoDTensors "
"should be the same."
);
PADDLE_ENFORCE_EQ
(
ins
[
0
]
->
dims
().
size
(),
ins
[
i
]
->
dims
().
size
(),
"The dimension size of all the input LoDTensors "
"should be the same."
);
const
size_t
dims_size
=
ins
[
i
]
->
dims
().
size
();
for
(
size_t
j
=
0
;
j
<
dims_size
;
++
j
)
{
if
(
j
==
axis
)
continue
;
PADDLE_ENFORCE_EQ
(
ins
[
0
]
->
dims
()[
j
],
ins
[
i
]
->
dims
()[
j
],
"Except for the dimension of the specified "
"axis along which all the inputs are concatenated, "
"dimensions of all the other axises of the input "
"LoDTensors should be the same."
);
}
}
PADDLE_ENFORCE_GT
(
ins
[
0
]
->
NumLevels
(),
level
,
"The levels of all the input LoDTensors "
"should be greater than the specify level"
);
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
out_lod
=
concatLoD
<
LoDTensor
>
(
ins
,
axis
,
level
);
out
->
set_lod
(
out_lod
);
auto
out_lod_level
=
out_lod
[
level
];
for
(
size_t
i
=
0
;
i
<
out_lod_level
.
size
()
-
1
;
++
i
)
{
Tensor
out_t
=
out
->
Slice
<
T
>
(
static_cast
<
int
>
(
out_lod_level
[
i
]),
static_cast
<
int
>
(
out_lod_level
[
i
+
1
]));
auto
out_stride
=
framework
::
stride
(
out_t
.
dims
());
size_t
offset
=
0
;
for
(
size_t
j
=
0
;
j
<
n
;
++
j
)
{
auto
in_lod_level
=
ins
[
j
]
->
lod
()[
level
];
auto
in_stride
=
framework
::
stride
(
ins
[
j
]
->
dims
());
Tensor
in_t
=
ins
[
j
]
->
Slice
<
T
>
(
static_cast
<
int
>
(
in_lod_level
[
i
]),
static_cast
<
int
>
(
in_lod_level
[
i
+
1
]));
size_t
axis_dim
=
in_t
.
dims
()[
axis
];
StridedMemcpy
<
T
>
(
ctx
.
device_context
(),
in_t
.
data
<
T
>
(),
in_stride
,
in_t
.
dims
(),
out_stride
,
out_t
.
data
<
T
>
()
+
offset
);
offset
+=
axis_dim
*
in_stride
[
axis
];
}
}
}
};
template
<
typename
Place
,
typename
T
>
class
SequenceConcatGradOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
ins
=
ctx
.
MultiInput
<
framework
::
LoDTensor
>
(
"X"
);
auto
*
out_grad
=
ctx
.
Input
<
framework
::
LoDTensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
x_grads
=
ctx
.
MultiOutput
<
framework
::
LoDTensor
>
(
framework
::
GradVarName
(
"X"
));
size_t
axis
=
static_cast
<
size_t
>
(
ctx
.
Attr
<
int
>
(
"axis"
));
size_t
level
=
static_cast
<
size_t
>
(
ctx
.
Attr
<
int
>
(
"level"
));
const
size_t
n
=
x_grads
.
size
();
// Set Grad(X) LoD as X
for
(
size_t
i
=
0
;
i
<
n
;
i
++
)
{
x_grads
[
i
]
->
set_lod
(
ins
[
i
]
->
lod
());
x_grads
[
i
]
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
}
auto
out_lod
=
concatLoD
<
LoDTensor
>
(
ins
,
axis
,
level
);
auto
out_lod_level
=
out_lod
[
level
];
for
(
size_t
i
=
0
;
i
<
out_lod_level
.
size
()
-
1
;
++
i
)
{
Tensor
out_grad_t
=
out_grad
->
Slice
<
T
>
(
static_cast
<
int
>
(
out_lod_level
[
i
]),
static_cast
<
int
>
(
out_lod_level
[
i
+
1
]));
auto
out_grad_stride
=
framework
::
stride
(
out_grad_t
.
dims
());
size_t
offset
=
0
;
for
(
size_t
j
=
0
;
j
<
n
;
++
j
)
{
auto
x_grad_lod_level
=
x_grads
[
j
]
->
lod
()[
level
];
auto
x_grad_stride
=
framework
::
stride
(
x_grads
[
j
]
->
dims
());
Tensor
x_grad_t
=
x_grads
[
j
]
->
Slice
<
T
>
(
static_cast
<
int
>
(
x_grad_lod_level
[
i
]),
static_cast
<
int
>
(
x_grad_lod_level
[
i
+
1
]));
size_t
axis_dim
=
x_grad_t
.
dims
()[
axis
];
StridedMemcpy
<
T
>
(
ctx
.
device_context
(),
out_grad_t
.
data
<
T
>
()
+
offset
,
out_grad_stride
,
out_grad_t
.
dims
(),
x_grad_stride
,
x_grad_t
.
data
<
T
>
());
offset
+=
axis_dim
*
out_grad_stride
[
axis
];
}
}
}
};
}
// namespace operators
}
// namespace paddle
paddle/platform/cudnn_helper.h
浏览文件 @
557c7ae3
...
...
@@ -71,23 +71,32 @@ class ScopedTensorDescriptor {
inline
cudnnTensorDescriptor_t
descriptor
(
const
cudnnTensorFormat_t
format
,
const
cudnnDataType_t
type
,
const
std
::
vector
<
int
>&
dims
)
{
// the format is not used now, but it maybe useful feature
const
std
::
vector
<
int
>&
dims
,
const
int
groups
=
1
)
{
// the format is not used now, will add later
std
::
vector
<
int
>
strides
(
dims
.
size
());
strides
[
dims
.
size
()
-
1
]
=
1
;
for
(
int
i
=
dims
.
size
()
-
2
;
i
>=
0
;
i
--
)
{
strides
[
i
]
=
dims
[
i
+
1
]
*
strides
[
i
+
1
];
}
// Update tensor descriptor dims setting if groups > 1
// FIXME(typhoonzero): Assume using NCHW order
std
::
vector
<
int
>
dims_with_group
(
dims
.
begin
(),
dims
.
end
());
// copy
if
(
groups
>
1
)
{
dims_with_group
[
1
]
=
dims_with_group
[
1
]
/
groups
;
}
PADDLE_ENFORCE
(
dynload
::
cudnnSetTensorNdDescriptor
(
desc_
,
type
,
dims
.
size
(),
dims
.
data
(),
strides
.
data
()));
desc_
,
type
,
dims_with_group
.
size
(),
dims_with_group
.
data
(),
strides
.
data
()));
return
desc_
;
}
template
<
typename
T
>
inline
cudnnTensorDescriptor_t
descriptor
(
const
DataLayout
&
order
,
const
std
::
vector
<
int
>&
dims
)
{
return
descriptor
(
GetCudnnTensorFormat
(
order
),
CudnnDataType
<
T
>::
type
,
dims
);
const
std
::
vector
<
int
>&
dims
,
const
int
groups
=
1
)
{
return
descriptor
(
GetCudnnTensorFormat
(
order
),
CudnnDataType
<
T
>::
type
,
dims
,
groups
);
}
private:
...
...
@@ -106,18 +115,29 @@ class ScopedFilterDescriptor {
inline
cudnnFilterDescriptor_t
descriptor
(
const
cudnnTensorFormat_t
format
,
const
cudnnDataType_t
type
,
const
std
::
vector
<
int
>&
kernel
)
{
// filter layout: output input spatial_dim_y spatial_dim_x
const
std
::
vector
<
int
>&
kernel
,
const
int
groups
=
1
)
{
// filter layout: MCHW, where M is the number of
// output image channels, C is the number of input image channels,
// H and W is height and width of filter.
std
::
vector
<
int
>
kernel_with_group
(
kernel
.
begin
(),
kernel
.
end
());
if
(
groups
>
1
)
{
// M /= groups
kernel_with_group
[
0
]
/=
groups
;
// NOTE: input filter(C) of the filter is already asserted to be C/groups.
}
PADDLE_ENFORCE
(
dynload
::
cudnnSetFilterNdDescriptor
(
desc_
,
type
,
format
,
kernel
.
size
(),
kernel
.
data
()));
desc_
,
type
,
format
,
kernel_with_group
.
size
(),
kernel_with_group
.
data
()));
return
desc_
;
}
template
<
typename
T
>
inline
cudnnFilterDescriptor_t
descriptor
(
const
DataLayout
&
order
,
const
std
::
vector
<
int
>&
kernel
)
{
const
std
::
vector
<
int
>&
kernel
,
const
int
groups
=
1
)
{
return
descriptor
(
GetCudnnTensorFormat
(
order
),
CudnnDataType
<
T
>::
type
,
kernel
);
kernel
,
groups
);
}
private:
...
...
paddle/pybind/CMakeLists.txt
浏览文件 @
557c7ae3
if
(
WITH_PYTHON
)
cc_library
(
paddle_pybind SHARED
SRCS pybind.cc exception.cc protobuf.cc
DEPS pybind python backward proto_desc tensor_array
DEPS pybind python backward proto_desc tensor_array
paddle_memory
${
GLOB_OP_LIB
}
)
endif
(
WITH_PYTHON
)
paddle/pybind/protobuf.cc
浏览文件 @
557c7ae3
...
...
@@ -15,6 +15,7 @@ limitations under the License. */
#include "paddle/pybind/protobuf.h"
#include <deque>
#include <iostream>
#include "paddle/framework/backward.h"
#include "paddle/framework/block_desc.h"
#include "paddle/framework/op_desc.h"
#include "paddle/framework/program_desc.h"
...
...
@@ -116,6 +117,11 @@ void BindProgramDesc(py::module &m) {
py
::
return_value_policy
::
reference
)
.
def
(
"append_block"
,
&
ProgramDescBind
::
AppendBlock
,
py
::
return_value_policy
::
reference
)
.
def
(
"append_backward"
,
[](
ProgramDescBind
&
program_desc
,
const
std
::
unordered_set
<
std
::
string
>
&
no_grad_vars
)
{
AppendBackward
(
program_desc
,
no_grad_vars
);
})
.
def
(
"block"
,
&
ProgramDescBind
::
Block
,
py
::
return_value_policy
::
reference
)
.
def
(
"num_blocks"
,
&
ProgramDescBind
::
Size
);
}
...
...
@@ -199,6 +205,7 @@ void BindOpDesc(py::module &m) {
.
def
(
"attr"
,
&
OpDescBind
::
GetAttr
)
.
def
(
"set_block_attr"
,
&
OpDescBind
::
SetBlockAttr
)
.
def
(
"get_block_attr"
,
&
OpDescBind
::
GetBlockAttr
)
.
def
(
"check_attrs"
,
&
OpDescBind
::
CheckAttrs
)
.
def
(
"infer_shape"
,
&
OpDescBind
::
InferShape
);
}
...
...
paddle/pybind/tensor_py.h
浏览文件 @
557c7ae3
...
...
@@ -57,7 +57,18 @@ struct CastToPyBufferImpl<true, I, ARGS...> {
}
framework
::
Tensor
dst_tensor
;
if
(
paddle
::
platform
::
is_gpu_place
(
tensor
.
place
()))
{
dst_tensor
.
CopyFrom
<
CUR_TYPE
>
(
tensor
,
platform
::
CPUPlace
());
#ifdef PADDLE_WITH_CUDA
auto
*
src_ptr
=
static_cast
<
const
void
*>
(
tensor
.
data
<
CUR_TYPE
>
());
auto
*
dst_ptr
=
static_cast
<
void
*>
(
dst_tensor
.
mutable_data
<
CUR_TYPE
>
(
tensor
.
dims
(),
platform
::
CPUPlace
()));
// TODO(qijun): Here we use default CUDA stream to set GPU Tensor to
// a Python numpy array. It's better to manage CDUA stream unifiedly.
paddle
::
platform
::
GpuMemcpySync
(
dst_ptr
,
src_ptr
,
sizeof
(
CUR_TYPE
)
*
tensor
.
numel
(),
cudaMemcpyDeviceToHost
);
#else
PADDLE_THROW
(
"'GPUPlace' is not supported in CPU only device."
);
#endif
}
else
if
(
paddle
::
platform
::
is_cpu_place
(
tensor
.
place
()))
{
dst_tensor
=
tensor
;
}
...
...
@@ -120,6 +131,8 @@ void PyCUDATensorSetFromArray(
self
.
Resize
(
framework
::
make_ddim
(
dims
));
auto
*
dst
=
self
.
mutable_data
<
T
>
(
place
);
// TODO(qijun): Here we use default CUDA stream to set a Python numpy
// array to a GPU Tensor. It's better to manage CDUA stream unifiedly.
paddle
::
platform
::
GpuMemcpySync
(
dst
,
array
.
data
(),
sizeof
(
T
)
*
array
.
size
(),
cudaMemcpyHostToDevice
);
}
...
...
proto/CMakeLists.txt
浏览文件 @
557c7ae3
file
(
GLOB proto_filenames . *.proto
)
if
(
MOBILE_INFERENCE
)
file
(
GLOB proto_filenames . ModelConfig.proto ParameterConfig.proto
TrainerConfig.proto DataConfig.proto
)
else
()
file
(
GLOB proto_filenames . *.proto
)
endif
()
include_directories
(
${
CMAKE_CURRENT_BINARY_DIR
}
)
proto_library
(
paddle_proto SRCS
${
proto_filenames
}
)
...
...
python/paddle/trainer_config_helpers/layers.py
浏览文件 @
557c7ae3
此差异已折叠。
点击以展开。
python/paddle/v2/framework/tests/test_activation_op.py
浏览文件 @
557c7ae3
...
...
@@ -363,5 +363,26 @@ class TestSoftsign(OpTest):
self
.
check_grad
([
'X'
],
'Y'
,
max_relative_error
=
0.007
)
class
TestThresholdedRelu
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"thresholded_relu"
threshold
=
0.25
self
.
relative_error
=
0.005
X
=
np
.
random
.
uniform
(
-
1
,
1
,
[
11
,
17
]).
astype
(
"float32"
)
# Same reason as TestAbs
X
[
np
.
abs
(
X
-
threshold
)
<
self
.
relative_error
]
=
threshold
+
0.2
self
.
inputs
=
{
'X'
:
X
}
self
.
attrs
=
{
'threshold'
:
threshold
}
self
.
outputs
=
{
'Y'
:
(
X
>
threshold
)
*
X
}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Y'
,
max_relative_error
=
self
.
relative_error
)
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/v2/framework/tests/test_conv2d_op.py
浏览文件 @
557c7ae3
...
...
@@ -3,70 +3,56 @@ import numpy as np
from
op_test
import
OpTest
def
conv2d_forward_naive
(
input
,
filter
,
group
,
conv_param
):
in_n
,
in_c
,
in_h
,
in_w
=
input
.
shape
out_c
,
f_c
,
f_h
,
f_w
=
filter
.
shape
assert
f_c
*
group
==
in_c
assert
np
.
mod
(
out_c
,
group
)
==
0
sub_out_c
=
out_c
/
group
stride
,
pad
=
conv_param
[
'stride'
],
conv_param
[
'pad'
]
out_h
=
1
+
(
in_h
+
2
*
pad
[
0
]
-
f_h
)
/
stride
[
0
]
out_w
=
1
+
(
in_w
+
2
*
pad
[
1
]
-
f_w
)
/
stride
[
1
]
out
=
np
.
zeros
((
in_n
,
out_c
,
out_h
,
out_w
))
input_pad
=
np
.
pad
(
input
,
((
0
,
),
(
0
,
),
(
pad
[
0
],
),
(
pad
[
1
],
)),
mode
=
'constant'
,
constant_values
=
0
)
for
i
in
range
(
out_h
):
for
j
in
range
(
out_w
):
for
g
in
range
(
group
):
input_pad_masked
=
\
input_pad
[:,
g
*
f_c
:(
g
+
1
)
*
f_c
,
i
*
stride
[
0
]:
i
*
stride
[
0
]
+
f_h
,
j
*
stride
[
1
]:
j
*
stride
[
1
]
+
f_w
]
f_sub
=
filter
[
g
*
sub_out_c
:(
g
+
1
)
*
sub_out_c
,
:,
:,
:]
for
k
in
range
(
sub_out_c
):
out
[:,
g
*
sub_out_c
+
k
,
i
,
j
]
=
\
np
.
sum
(
input_pad_masked
*
f_sub
[
k
,
:,
:,
:],
axis
=
(
1
,
2
,
3
))
return
out
class
TestConv2dOp
(
OpTest
):
def
setUp
(
self
):
self
.
init_groups
()
self
.
op_type
=
"conv2d"
batch_size
=
2
input_channels
=
3
input_height
=
5
input_width
=
5
output_channels
=
6
filter_height
=
3
filter_width
=
3
stride
=
1
padding
=
0
output_height
=
(
input_height
-
filter_height
+
2
*
padding
)
/
stride
+
1
output_width
=
(
input_width
-
filter_width
+
2
*
padding
)
/
stride
+
1
input
=
np
.
random
.
random
((
batch_size
,
input_channels
,
input_height
,
input_width
)).
astype
(
"float32"
)
filter
=
np
.
random
.
random
(
(
output_channels
,
input_channels
/
self
.
groups
,
filter_height
,
filter_width
)).
astype
(
"float32"
)
output
=
np
.
ndarray
(
(
batch_size
,
output_channels
,
output_height
,
output_width
))
self
.
init_op_type
()
self
.
init_group
()
self
.
init_test_case
()
conv2d_param
=
{
'stride'
:
self
.
stride
,
'pad'
:
self
.
pad
}
input
=
np
.
random
.
random
(
self
.
input_size
).
astype
(
"float32"
)
filter
=
np
.
random
.
random
(
self
.
filter_size
).
astype
(
"float32"
)
output
=
conv2d_forward_naive
(
input
,
filter
,
self
.
groups
,
conv2d_param
)
self
.
inputs
=
{
'Input'
:
input
,
'Filter'
:
filter
}
self
.
attrs
=
{
'strides'
:
[
1
,
1
],
'paddings'
:
[
0
,
0
],
'groups'
:
self
.
groups
'strides'
:
self
.
stride
,
'paddings'
:
self
.
pad
,
'groups'
:
self
.
groups
,
'dilations'
:
self
.
dilations
}
output_group_channels
=
output_channels
/
self
.
groups
input_group_channels
=
input_channels
/
self
.
groups
for
batchid
in
xrange
(
batch_size
):
for
group
in
xrange
(
self
.
groups
):
for
outchannelid
in
range
(
group
*
output_group_channels
,
(
group
+
1
)
*
output_group_channels
):
for
rowid
in
xrange
(
output_height
):
for
colid
in
xrange
(
output_width
):
start_h
=
(
rowid
*
stride
)
-
padding
start_w
=
(
colid
*
stride
)
-
padding
output_value
=
0.0
for
inchannelid
in
range
(
group
*
input_group_channels
,
(
group
+
1
)
*
input_group_channels
):
for
frowid
in
xrange
(
filter_height
):
for
fcolid
in
xrange
(
filter_width
):
input_value
=
0.0
inrowid
=
start_h
+
frowid
incolid
=
start_w
+
fcolid
if
((
inrowid
>=
0
and
inrowid
<
input_height
)
and
(
incolid
>=
0
and
incolid
<
input_width
)):
input_value
=
input
[
batchid
][
inchannelid
][
inrowid
][
incolid
]
filter_value
=
filter
[
outchannelid
][
inchannelid
%
input_group_channels
][
frowid
][
fcolid
]
output_value
+=
input_value
*
filter_value
output
[
batchid
][
outchannelid
][
rowid
][
colid
]
=
output_value
self
.
outputs
=
{
'Output'
:
output
}
def
test_check_output
(
self
):
...
...
@@ -90,14 +76,47 @@ class TestConv2dOp(OpTest):
max_relative_error
=
0.05
,
no_grad_set
=
set
([
'Input'
]))
def
init_groups
(
self
):
def
init_test_case
(
self
):
# self.groups = 1
# self.op_type = "conv2d"
self
.
pad
=
[
0
,
0
]
self
.
stride
=
[
1
,
1
]
self
.
dilations
=
[
1
,
1
]
self
.
input_size
=
[
2
,
3
,
5
,
5
]
# NCHW
assert
np
.
mod
(
self
.
input_size
[
1
],
self
.
groups
)
==
0
f_c
=
self
.
input_size
[
1
]
/
self
.
groups
self
.
filter_size
=
[
6
,
f_c
,
3
,
3
]
def
init_group
(
self
):
self
.
groups
=
1
def
init_op_type
(
self
):
self
.
op_type
=
"conv2d"
class
TestWithGroup
(
TestConv2dOp
):
def
init_group
s
(
self
):
def
init_group
(
self
):
self
.
groups
=
3
def
init_op_type
(
self
):
self
.
op_type
=
"conv2d"
class
TestCudnn
(
TestConv2dOp
):
def
init_group
(
self
):
self
.
groups
=
1
def
init_op_type
(
self
):
self
.
op_type
=
"conv_cudnn"
class
TestCudnnWithGroup
(
TestConv2dOp
):
def
init_group
(
self
):
self
.
groups
=
3
def
init_op_type
(
self
):
self
.
op_type
=
"conv_cudnn"
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/v2/framework/tests/test_conv3d_op.py
浏览文件 @
557c7ae3
...
...
@@ -96,7 +96,26 @@ class TestConv3dOp(OpTest):
self
.
op_type
=
"conv3d"
class
TestWithGroup
(
TestConv3dOp
):
class
TestCase1
(
TestConv3dOp
):
def
init_test_case
(
self
):
# self.groups = 1
# self.op_type = "conv3d"
self
.
pad
=
[
1
,
1
,
1
]
self
.
stride
=
[
1
,
1
,
1
]
self
.
input_size
=
[
2
,
3
,
5
,
5
,
5
]
# NCDHW
assert
np
.
mod
(
self
.
input_size
[
1
],
self
.
groups
)
==
0
f_c
=
self
.
input_size
[
1
]
/
self
.
groups
self
.
filter_size
=
[
6
,
f_c
,
3
,
3
,
3
]
def
init_group
(
self
):
self
.
groups
=
1
def
init_op_type
(
self
):
self
.
op_type
=
"conv3d"
'''
class TestWithGroup1(TestConv3dOp):
def init_group(self):
self.groups = 3
...
...
@@ -104,5 +123,13 @@ class TestWithGroup(TestConv3dOp):
self.op_type = "conv3d"
class TestWithGroup2(TestCase1):
def init_group(self):
self.groups = 3
def init_op_type(self):
self.op_type = "conv3d"
'''
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/v2/framework/tests/test_decayed_adagrad_op.py
0 → 100644
浏览文件 @
557c7ae3
import
unittest
import
numpy
as
np
from
op_test
import
OpTest
class
TestDecayedAdagradOp1
(
OpTest
):
''' Test DecayedAdagrad operator with explicit attributes
'''
def
setUp
(
self
):
self
.
op_type
=
"decayed_adagrad"
param
=
np
.
random
.
random
((
123
,
321
)).
astype
(
"float32"
)
grad
=
np
.
random
.
random
((
123
,
321
)).
astype
(
"float32"
)
moment
=
np
.
zeros
((
123
,
321
)).
astype
(
"float32"
)
lr
=
0.01
decay
=
0.80
epsilon
=
1e-8
self
.
inputs
=
{
'Param'
:
param
,
'Grad'
:
grad
,
'Moment'
:
moment
,
'LearningRate'
:
np
.
array
([
lr
]).
astype
(
"float32"
)
}
self
.
attrs
=
{
'decay'
:
decay
,
'epsilon'
:
epsilon
}
moment_out
=
decay
*
moment
+
(
1
-
decay
)
*
grad
*
grad
param_out
=
param
-
lr
*
grad
/
(
np
.
sqrt
(
moment_out
)
+
epsilon
)
self
.
outputs
=
{
'ParamOut'
:
param_out
,
'MomentOut'
:
moment_out
}
def
test_check_output
(
self
):
self
.
check_output
()
class
TestDecayedAdagradOp2
(
OpTest
):
''' Test DecayedAdagrad operator with default attributes
'''
def
setUp
(
self
):
self
.
op_type
=
"decayed_adagrad"
param
=
np
.
random
.
random
((
123
,
321
)).
astype
(
"float32"
)
grad
=
np
.
random
.
random
((
123
,
321
)).
astype
(
"float32"
)
moment
=
np
.
zeros
((
123
,
321
)).
astype
(
"float32"
)
lr
=
0.01
decay
=
0.95
epsilon
=
1e-6
self
.
inputs
=
{
'Param'
:
param
,
'Grad'
:
grad
,
'Moment'
:
moment
,
'LearningRate'
:
np
.
array
([
lr
]).
astype
(
"float32"
)
}
self
.
attrs
=
{
'decay'
:
decay
,
'epsilon'
:
epsilon
}
moment_out
=
decay
*
moment
+
(
1
-
decay
)
*
grad
*
grad
param_out
=
param
-
lr
*
grad
/
(
np
.
sqrt
(
moment_out
)
+
epsilon
)
self
.
outputs
=
{
'ParamOut'
:
param_out
,
'MomentOut'
:
moment_out
}
def
test_check_output
(
self
):
self
.
check_output
()
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/v2/framework/tests/test_margin_rank_loss_op.py
0 → 100644
浏览文件 @
557c7ae3
import
unittest
import
numpy
as
np
from
op_test
import
OpTest
class
TestMarginRankLossOp
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"margin_rank_loss"
batch_size
=
5
margin
=
0.5
# labels_{i} = {-1, 1}
label
=
2
*
np
.
random
.
randint
(
0
,
2
,
size
=
(
batch_size
,
1
)).
astype
(
"float32"
)
-
1
x1
=
np
.
random
.
random
((
batch_size
,
1
)).
astype
(
"float32"
)
x2
=
np
.
random
.
random
((
batch_size
,
1
)).
astype
(
"float32"
)
# loss = max(0, -label * (x1 - x2) + margin)
loss
=
-
label
*
(
x1
-
x2
)
+
margin
loss
=
np
.
where
(
loss
>
0
,
loss
,
0
)
act
=
np
.
where
(
loss
>
0
,
1.
,
0.
)
self
.
attrs
=
{
'margin'
:
margin
}
self
.
inputs
=
{
'Label'
:
label
,
'X1'
:
x1
,
'X2'
:
x2
}
self
.
outputs
=
{
'Activated'
:
act
,
'Out'
:
loss
}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
"X1"
,
"X2"
],
"Out"
)
def
test_check_grad_ignore_x1
(
self
):
self
.
check_grad
([
"X2"
],
"Out"
,
no_grad_set
=
set
(
'X1'
))
def
test_check_grad_ignore_x2
(
self
):
self
.
check_grad
([
"X1"
],
"Out"
,
no_grad_set
=
set
(
'X2'
))
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/v2/framework/tests/test_program.py
浏览文件 @
557c7ae3
import
unittest
import
paddle.v2.framework.core
as
core
from
paddle.v2.framework.graph
import
g_program
...
...
@@ -31,6 +33,34 @@ class TestProgram(unittest.TestCase):
self
.
assertEqual
(
1
,
b
.
idx
)
self
.
assertEqual
(
0
,
b
.
parent_idx
)
def
test_append_backward
(
self
):
prog
=
core
.
ProgramDesc
.
__create_program_desc__
()
self
.
assertIsNotNone
(
prog
)
block
=
prog
.
block
(
0
)
self
.
assertIsNotNone
(
block
)
mul_op_desc
=
block
.
append_op
()
mul_op_desc
.
set_type
(
"mul"
)
mul_op_desc
.
set_input
(
"X"
,
[
"x1"
])
mul_op_desc
.
set_input
(
"Y"
,
[
"y1"
])
mul_op_desc
.
set_output
(
"Out"
,
[
"out1"
])
sum_op_desc
=
block
.
append_op
()
sum_op_desc
.
set_type
(
"elementwise_add"
)
sum_op_desc
.
set_input
(
"X"
,
[
"out1"
])
sum_op_desc
.
set_input
(
"Y"
,
[
"b1"
])
sum_op_desc
.
set_output
(
"Out"
,
[
"out2"
])
expect_ops
=
[
"mul"
,
"elementwise_add"
,
"elementwise_add_grad"
,
"mul_grad"
]
actual_ops
=
[]
prog
.
append_backward
(
set
())
for
op
in
block
.
all_ops
():
actual_ops
.
append
(
op
.
type
())
print
(
actual_ops
)
self
.
assertEqual
(
actual_ops
,
expect_ops
)
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/v2/framework/tests/test_protobuf_descs.py
浏览文件 @
557c7ae3
...
...
@@ -55,6 +55,12 @@ class TestOpDesc(unittest.TestCase):
op
.
set_block_attr
(
"block_attr"
,
prog
.
block
(
0
))
self
.
assertEqual
(
0
,
op
.
get_block_attr
(
"block_attr"
))
mul_op
=
block
.
append_op
()
mul_op
.
set_type
(
"mul"
)
mul_op
.
check_attrs
()
self
.
assertEqual
(
mul_op
.
attr
(
"x_num_col_dims"
),
1
)
self
.
assertEqual
(
mul_op
.
attr
(
"y_num_col_dims"
),
1
)
class
TestProgramDesc
(
unittest
.
TestCase
):
def
test_instance
(
self
):
...
...
python/paddle/v2/framework/tests/test_seq_concat_op.py
0 → 100644
浏览文件 @
557c7ae3
import
unittest
import
numpy
as
np
import
sys
from
op_test
import
OpTest
class
TestConcatOp
(
OpTest
):
def
set_data
(
self
):
# two level, batch size is 3
x0
=
np
.
random
.
random
((
4
,
6
,
3
)).
astype
(
'float32'
)
lod0
=
[[
0
,
2
,
4
],
[
0
,
1
,
2
,
3
,
4
]]
x1
=
np
.
random
.
random
((
4
,
8
,
3
)).
astype
(
'float32'
)
lod1
=
[[
0
,
2
,
4
],
[
0
,
1
,
2
,
3
,
4
]]
axis
=
1
level
=
1
self
.
inputs
=
{
'X'
:
[(
'x0'
,
(
x0
,
lod0
)),
(
'x1'
,
(
x1
,
lod1
))]}
self
.
attrs
=
{
'axis'
:
axis
,
'level'
:
level
}
outs
=
[]
for
i
in
range
(
4
):
sub_x0
=
x0
[
lod0
[
level
][
i
]:
lod0
[
level
][
i
+
1
],
:]
sub_x1
=
x1
[
lod1
[
level
][
i
]:
lod1
[
level
][
i
+
1
],
:]
outs
.
append
(
np
.
concatenate
((
sub_x0
,
sub_x1
),
axis
=
axis
))
self
.
outputs
=
{
'Out'
:
np
.
concatenate
(
outs
,
axis
=
0
)}
def
setUp
(
self
):
self
.
op_type
=
"sequence_concat"
self
.
set_data
()
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'x0'
],
'Out'
)
class
TestConcatOpDiffLod
(
TestConcatOp
):
def
set_data
(
self
):
# two level, batch size is 3
x0
=
np
.
random
.
random
((
4
,
6
,
3
)).
astype
(
'float32'
)
lod0
=
[[
0
,
2
,
4
],
[
0
,
1
,
2
,
3
,
4
]]
x1
=
np
.
random
.
random
((
5
,
6
,
3
)).
astype
(
'float32'
)
lod1
=
[[
0
,
3
,
5
],
[
0
,
1
,
2
,
3
,
5
]]
axis
=
0
level
=
1
self
.
inputs
=
{
'X'
:
[(
'x0'
,
(
x0
,
lod0
)),
(
'x1'
,
(
x1
,
lod1
))]}
self
.
attrs
=
{
'axis'
:
axis
,
'level'
:
level
}
outs
=
[]
for
i
in
range
(
4
):
sub_x0
=
x0
[
lod0
[
level
][
i
]:
lod0
[
level
][
i
+
1
],
:]
sub_x1
=
x1
[
lod1
[
level
][
i
]:
lod1
[
level
][
i
+
1
],
:]
outs
.
append
(
np
.
concatenate
((
sub_x0
,
sub_x1
),
axis
=
axis
))
self
.
outputs
=
{
'Out'
:
np
.
concatenate
(
outs
,
axis
=
0
)}
class
TestConcatOpLevelZero
(
TestConcatOp
):
def
set_data
(
self
):
# two level, batch size is 3
x0
=
np
.
random
.
random
((
4
,
3
,
4
)).
astype
(
'float32'
)
lod0
=
[[
0
,
2
,
4
],
[
0
,
1
,
2
,
3
,
4
]]
x1
=
np
.
random
.
random
((
5
,
3
,
4
)).
astype
(
'float32'
)
lod1
=
[[
0
,
3
,
5
],
[
0
,
1
,
3
,
4
,
5
]]
axis
=
0
level
=
0
self
.
inputs
=
{
'X'
:
[(
'x0'
,
(
x0
,
lod0
)),
(
'x1'
,
(
x1
,
lod1
))]}
self
.
attrs
=
{
'axis'
:
axis
,
'level'
:
level
}
outs
=
[]
for
i
in
range
(
2
):
sub_x0
=
x0
[
lod0
[
level
][
i
]:
lod0
[
level
][
i
+
1
],
:]
sub_x1
=
x1
[
lod1
[
level
][
i
]:
lod1
[
level
][
i
+
1
],
:]
outs
.
append
(
np
.
concatenate
((
sub_x0
,
sub_x1
),
axis
=
axis
))
self
.
outputs
=
{
'Out'
:
np
.
concatenate
(
outs
,
axis
=
0
)}
if
__name__
==
'__main__'
:
sys
.
exit
(
0
)
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录