Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
45467d80
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
45467d80
编写于
2月 08, 2018
作者:
Y
Yancey1989
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
improve split and concat op
上级
ca5dc46a
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
111 addition
and
36 deletion
+111
-36
paddle/framework/ddim.cc
paddle/framework/ddim.cc
+10
-0
paddle/framework/ddim.h
paddle/framework/ddim.h
+2
-0
paddle/operators/concat_op.h
paddle/operators/concat_op.h
+61
-20
paddle/operators/split_op.h
paddle/operators/split_op.h
+32
-10
python/paddle/v2/fluid/tests/test_split_op.py
python/paddle/v2/fluid/tests/test_split_op.py
+6
-6
未找到文件。
paddle/framework/ddim.cc
浏览文件 @
45467d80
...
@@ -314,5 +314,15 @@ DDim stride(const DDim& ddim) {
...
@@ -314,5 +314,15 @@ DDim stride(const DDim& ddim) {
}
}
return
framework
::
make_ddim
(
strides
);
return
framework
::
make_ddim
(
strides
);
}
}
DDim
stride_numel
(
const
framework
::
DDim
&
ddim
)
{
std
::
vector
<
int64_t
>
strides
(
ddim
.
size
());
strides
[
ddim
.
size
()
-
1
]
=
ddim
[
ddim
.
size
()
-
1
];
for
(
int
i
=
ddim
.
size
()
-
2
;
i
>=
0
;
--
i
)
{
strides
[
i
]
=
strides
[
i
+
1
]
*
ddim
[
i
];
}
return
framework
::
make_ddim
(
strides
);
}
}
// namespace framework
}
// namespace framework
}
// namespace paddle
}
// namespace paddle
paddle/framework/ddim.h
浏览文件 @
45467d80
...
@@ -125,6 +125,8 @@ DDim flatten_to_2d(const DDim& src, int num_col_dims);
...
@@ -125,6 +125,8 @@ DDim flatten_to_2d(const DDim& src, int num_col_dims);
DDim
flatten_to_1d
(
const
DDim
&
src
);
DDim
flatten_to_1d
(
const
DDim
&
src
);
DDim
stride
(
const
DDim
&
ddim
);
DDim
stride
(
const
DDim
&
ddim
);
DDim
stride_numel
(
const
DDim
&
ddim
);
}
// namespace framework
}
// namespace framework
}
// namespace paddle
}
// namespace paddle
...
...
paddle/operators/concat_op.h
浏览文件 @
45467d80
...
@@ -15,8 +15,8 @@ limitations under the License. */
...
@@ -15,8 +15,8 @@ limitations under the License. */
#pragma once
#pragma once
#include <vector>
#include <vector>
#include "paddle/framework/ddim.h"
#include "paddle/framework/op_registry.h"
#include "paddle/framework/op_registry.h"
#include "paddle/operators/strided_memcpy.h"
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
...
@@ -28,17 +28,38 @@ class ConcatKernel : public framework::OpKernel<T> {
...
@@ -28,17 +28,38 @@ class ConcatKernel : public framework::OpKernel<T> {
auto
ins
=
ctx
.
MultiInput
<
framework
::
Tensor
>
(
"X"
);
auto
ins
=
ctx
.
MultiInput
<
framework
::
Tensor
>
(
"X"
);
auto
*
out
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
auto
*
out
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
int64_t
axis
=
static_cast
<
int64_t
>
(
ctx
.
Attr
<
int
>
(
"axis"
));
int64_t
axis
=
static_cast
<
int64_t
>
(
ctx
.
Attr
<
int
>
(
"axis"
));
const
size_t
n
=
ins
.
size
();
auto
place
=
ctx
.
GetPlace
();
out
->
mutable_data
<
T
>
(
place
);
auto
out_stride
=
framework
::
stride_numel
(
out
->
dims
());
int64_t
before
=
out_stride
[
0
]
/
out_stride
[
axis
];
int64_t
out_after
=
out_stride
[
axis
];
size_t
output_offset
=
0
;
size_t
output_offset
=
0
;
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
for
(
auto
*
in
:
ins
)
{
auto
out_stride
=
framework
::
stride
(
out
->
dims
());
auto
in_stride
=
framework
::
stride_numel
(
in
->
dims
());
for
(
size_t
i
=
0
;
i
<
n
;
i
++
)
{
int64_t
in_after
=
in_stride
[
axis
];
auto
&
in
=
ins
[
i
];
for
(
int64_t
i
=
0
;
i
<
before
;
++
i
)
{
auto
axis_dim
=
in
->
dims
()[
axis
];
if
(
platform
::
is_cpu_place
(
place
))
{
auto
in_stride
=
framework
::
stride
(
in
->
dims
());
auto
&
cpu_place
=
boost
::
get
<
platform
::
CPUPlace
>
(
place
);
StridedMemcpy
<
T
>
(
ctx
.
device_context
(),
in
->
data
<
T
>
(),
in_stride
,
memory
::
Copy
(
in
->
dims
(),
out_stride
,
out
->
data
<
T
>
()
+
output_offset
);
cpu_place
,
out
->
data
<
T
>
()
+
output_offset
+
i
*
out_after
,
output_offset
+=
axis_dim
*
in_stride
[
axis
];
cpu_place
,
in
->
data
<
T
>
()
+
i
*
in_after
,
sizeof
(
T
)
*
in_after
);
}
else
{
#ifdef PADDLE_WITH_CUDA
auto
&
gpu_place
=
boost
::
get
<
platform
::
CUDAPlace
>
(
place
);
auto
&
cuda_ctx
=
reinterpret_cast
<
const
platform
::
CUDADeviceContext
&>
(
dev_ctx
);
memory
::
Copy
(
gpu_place
,
out
->
data
<
T
>
()
+
output_offset
+
i
*
out_after
,
gpu_place
,
in
->
data
<
T
>
()
+
i
*
in_after
,
sizeof
(
T
)
*
in_after
,
cuda_ctx
.
stream
()));
#else
PADDLE_THROW
(
"Paddle is not compiled with GPU"
);
#endif
}
}
output_offset
+=
in_after
;
}
}
}
}
};
};
...
@@ -50,17 +71,37 @@ class ConcatGradKernel : public framework::OpKernel<T> {
...
@@ -50,17 +71,37 @@ class ConcatGradKernel : public framework::OpKernel<T> {
auto
*
in
=
ctx
.
Input
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
in
=
ctx
.
Input
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
outs
=
ctx
.
MultiOutput
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
outs
=
ctx
.
MultiOutput
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
));
int64_t
axis
=
static_cast
<
int64_t
>
(
ctx
.
Attr
<
int
>
(
"axis"
));
int64_t
axis
=
static_cast
<
int64_t
>
(
ctx
.
Attr
<
int
>
(
"axis"
));
const
size_t
n
=
outs
.
size
();
size_t
input_offset
=
0
;
size_t
input_offset
=
0
;
auto
in_stride
=
framework
::
stride
(
in
->
dims
());
auto
in_stride
=
framework
::
stride_numel
(
in
->
dims
());
for
(
size_t
i
=
0
;
i
<
n
;
i
++
)
{
auto
place
=
ctx
.
GetPlace
();
auto
&
out
=
outs
[
i
];
// numel before the specified axis
int64_t
before
=
in_stride
[
0
]
/
in_stride
[
axis
];
int64_t
in_after
=
in_stride
[
axis
];
for
(
auto
&
out
:
outs
)
{
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
size_t
axis_dim
=
out
->
dims
()[
axis
];
auto
out_stride
=
framework
::
stride_numel
(
out
->
dims
());
auto
out_stride
=
framework
::
stride
(
out
->
dims
());
int64_t
out_after
=
out_stride
[
axis
];
StridedMemcpy
<
T
>
(
ctx
.
device_context
(),
in
->
data
<
T
>
()
+
input_offset
,
for
(
int64_t
i
=
0
;
i
<
before
;
++
i
)
{
in_stride
,
out
->
dims
(),
out_stride
,
out
->
data
<
T
>
());
if
(
platform
::
is_cpu_place
(
place
))
{
input_offset
+=
axis_dim
*
in_stride
[
axis
];
auto
&
cpu_place
=
boost
::
get
<
platform
::
CPUPlace
>
(
place
);
memory
::
Copy
(
cpu_place
,
out
->
data
<
T
>
()
+
i
*
out_after
,
cpu_place
,
in
->
data
<
T
>
()
+
input_offset
+
i
*
in_after
,
sizeof
(
T
)
*
out_after
);
}
else
{
#ifdef PADDLE_WITH_CUDA
auto
&
gpu_place
=
boost
::
get
<
platform
::
CUDAPlace
>
(
place
);
auto
&
cuda_ctx
=
reinterpret_cast
<
const
platform
::
CUDADeviceContext
&>
(
dev_ctx
);
memory
::
Copy
(
gpu_place
,
out
->
data
<
T
>
()
+
i
*
out_after
,
gpu_place
,
in
->
data
<
T
>
()
+
input_offset
+
i
*
in_after
,
sizeof
(
T
)
*
out_after
,
cuda_ctx
.
stream
());
#else
PADDLE_THROW
(
"Paddle is not compiled with GPU"
);
#endif
}
}
input_offset
+=
out_after
;
}
}
}
}
};
};
...
...
paddle/operators/split_op.h
浏览文件 @
45467d80
...
@@ -14,9 +14,10 @@ limitations under the License. */
...
@@ -14,9 +14,10 @@ limitations under the License. */
#pragma once
#pragma once
#include <chrono>
#include <vector>
#include <vector>
#include "paddle/framework/ddim.h"
#include "paddle/framework/op_registry.h"
#include "paddle/framework/op_registry.h"
#include "paddle/operators/strided_memcpy.h"
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
...
@@ -25,20 +26,41 @@ template <typename DeviceContext, typename T>
...
@@ -25,20 +26,41 @@ template <typename DeviceContext, typename T>
class
SplitOpKernel
:
public
framework
::
OpKernel
<
T
>
{
class
SplitOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
// auto start = std::chrono::steady_clock::now();
auto
*
in
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
);
auto
*
in
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
);
auto
outs
=
ctx
.
MultiOutput
<
framework
::
Tensor
>
(
"Out"
);
auto
outs
=
ctx
.
MultiOutput
<
framework
::
Tensor
>
(
"Out"
);
auto
in_stride
=
framework
::
stride
(
in
->
dims
());
auto
in_stride
=
framework
::
stride
_numel
(
in
->
dims
());
int64_t
axis
=
static_cast
<
int64_t
>
(
ctx
.
Attr
<
int
>
(
"axis"
));
int64_t
axis
=
static_cast
<
int64_t
>
(
ctx
.
Attr
<
int
>
(
"axis"
));
const
size_t
n
=
outs
.
size
();
auto
place
=
ctx
.
GetPlace
();
// numel before the specified axis
int64_t
before
=
in_stride
[
0
]
/
in_stride
[
axis
];
int64_t
in_after
=
in_stride
[
axis
];
size_t
input_offset
=
0
;
size_t
input_offset
=
0
;
for
(
size_t
i
=
0
;
i
<
n
;
i
++
)
{
for
(
auto
&
out
:
outs
)
{
auto
&
out
=
outs
[
i
];
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
size_t
axis_dim
=
out
->
dims
()[
axis
];
auto
out_stride
=
framework
::
stride_numel
(
out
->
dims
());
auto
out_stride
=
framework
::
stride
(
out
->
dims
());
int64_t
out_after
=
out_stride
[
axis
];
StridedMemcpy
<
T
>
(
ctx
.
device_context
(),
in
->
data
<
T
>
()
+
input_offset
,
for
(
int64_t
i
=
0
;
i
<
before
;
++
i
)
{
in_stride
,
out
->
dims
(),
out_stride
,
out
->
data
<
T
>
());
if
(
platform
::
is_cpu_place
(
place
))
{
input_offset
+=
axis_dim
*
in_stride
[
axis
];
auto
&
cpu_place
=
boost
::
get
<
platform
::
CPUPlace
>
(
place
);
memory
::
Copy
(
cpu_place
,
out
->
data
<
T
>
()
+
i
*
out_after
,
cpu_place
,
in
->
data
<
T
>
()
+
input_offset
+
i
*
in_after
,
sizeof
(
T
)
*
out_after
);
}
else
{
#ifdef PADDLE_WITH_CUDA
auto
&
gpu_place
=
boost
::
get
<
platform
::
CUDAPlace
>
(
place
);
auto
&
cuda_ctx
=
reinterpret_cast
<
const
platform
::
CUDADeviceContext
&>
(
dev_ctx
);
memory
::
Copy
(
gpu_place
,
out
->
data
<
T
>
()
+
i
*
out_after
,
gpu_place
,
in
->
data
<
T
>
()
+
input_offset
+
i
*
in_after
,
sizeof
(
T
)
*
out_after
,
cuda_ctx
.
stream
());
#else
PADDLE_THROW
(
"Paddle is not compiled with GPU"
);
#endif
}
}
input_offset
+=
out_after
;
}
}
}
}
};
};
...
...
python/paddle/v2/fluid/tests/test_split_op.py
浏览文件 @
45467d80
...
@@ -20,19 +20,19 @@ from op_test import OpTest
...
@@ -20,19 +20,19 @@ from op_test import OpTest
class
TestSplitOp
(
OpTest
):
class
TestSplitOp
(
OpTest
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
op_type
=
"split"
self
.
op_type
=
"split"
axis
=
0
axis
=
1
x
=
np
.
random
.
random
((
4
,
2
,
5
)).
astype
(
'float32'
)
x
=
np
.
random
.
random
((
4
,
5
,
6
)).
astype
(
'float32'
)
out
=
np
.
split
(
x
,
[
1
,
3
],
axis
)
out
=
np
.
split
(
x
,
[
2
,
3
],
axis
)
self
.
inputs
=
{
'X'
:
x
}
self
.
inputs
=
{
'X'
:
x
}
self
.
attrs
=
{
'axis'
:
axis
,
'sections'
:
[
1
,
2
,
1
]}
self
.
attrs
=
{
'axis'
:
axis
,
'sections'
:
[
2
,
1
,
2
]}
self
.
outputs
=
{
'Out'
:
[(
'out%d'
%
i
,
out
[
i
])
\
self
.
outputs
=
{
'Out'
:
[(
'out%d'
%
i
,
out
[
i
])
\
for
i
in
xrange
(
len
(
out
))]}
for
i
in
xrange
(
len
(
out
))]}
def
test_check_output
(
self
):
def
test_check_output
(
self
):
self
.
check_output
()
self
.
check_output
()
def
test_check_grad
(
self
):
#
def test_check_grad(self):
self
.
check_grad
([
'X'
],
[
'out0'
,
'out1'
,
'out2'
])
#
self.check_grad(['X'], ['out0', 'out1', 'out2'])
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录