Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
78808b20
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看板
提交
78808b20
编写于
9月 28, 2017
作者:
Z
zchen0211
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
1 api
上级
b851515b
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
55 addition
and
87 deletion
+55
-87
paddle/operators/cond_op.cc
paddle/operators/cond_op.cc
+2
-2
paddle/operators/gather.cu.h
paddle/operators/gather.cu.h
+11
-19
paddle/operators/gather.h
paddle/operators/gather.h
+15
-23
paddle/operators/gather_op.cu
paddle/operators/gather_op.cu
+2
-2
paddle/operators/gather_op.h
paddle/operators/gather_op.h
+1
-1
paddle/operators/gather_test.cc
paddle/operators/gather_test.cc
+1
-1
paddle/operators/scatter.cu.h
paddle/operators/scatter.cu.h
+14
-22
paddle/operators/scatter.h
paddle/operators/scatter.h
+6
-14
paddle/operators/scatter_op.cu
paddle/operators/scatter_op.cu
+2
-2
paddle/operators/scatter_op.h
paddle/operators/scatter_op.h
+1
-1
未找到文件。
paddle/operators/cond_op.cc
浏览文件 @
78808b20
...
...
@@ -169,8 +169,8 @@ void CondOp::Run(const Scope& scope,
tensor_child
->
Resize
(
dim
);
tensor_child
->
mutable_data
<
float
>
(
dim
,
platform
::
CPUPlace
());
CPU
T
Gather
<
float
>
(
dev_ctx
.
GetPlace
(),
tensor_parent
,
&
index_tensors
[
i
],
tensor_child
);
CPUGather
<
float
>
(
dev_ctx
.
GetPlace
(),
tensor_parent
,
&
index_tensors
[
i
],
tensor_child
);
}
}
...
...
paddle/operators/gather.cu.h
浏览文件 @
78808b20
...
...
@@ -38,19 +38,6 @@ __global__ void GatherCUDAKernel(const T* params, const int* indices, T* output,
}
}
// Implementation of GPU copy:
template
<
typename
T
>
struct
GPUGather
{
void
operator
()(
const
T
*
src
,
const
int
*
index
,
const
int
slice_size
,
const
int
index_size
,
T
*
output
)
{
int
block
=
512
;
int
n
=
slice_size
*
index_size
;
int
grid
=
(
n
+
block
-
1
)
/
block
;
GatherCUDAKernel
<
T
><<<
grid
,
block
>>>
(
src
,
index
,
output
,
index_size
,
slice_size
);
}
};
/**
* A thin wrapper on gpu tensor
* Return a new tensor from source tensor, gathered according to index
...
...
@@ -59,8 +46,8 @@ struct GPUGather {
* return: output tensor
*/
template
<
typename
T
>
void
GPU
T
Gather
(
const
Place
&
place
,
const
Tensor
*
src
,
const
Tensor
*
index
,
Tensor
*
output
)
{
void
GPUGather
(
const
Place
&
place
,
const
Tensor
*
src
,
const
Tensor
*
index
,
Tensor
*
output
)
{
PADDLE_ENFORCE
(
platform
::
is_gpu_place
(
place
));
// check index of shape 1-D
PADDLE_ENFORCE
(
index
->
dims
().
size
()
==
1
);
...
...
@@ -74,10 +61,15 @@ void GPUTGather(const Place& place, const Tensor* src, const Tensor* index,
int
slice_size
=
1
;
for
(
int
i
=
1
;
i
<
src_dims
.
size
();
++
i
)
slice_size
*=
src_dims
[
i
];
// Gathering
GPUGather
<
T
>
gather_functor
;
gather_functor
(
src
->
data
<
T
>
(),
index
->
data
<
int
>
(),
slice_size
,
index_size
,
output
->
data
<
T
>
());
const
T
*
p_src
=
src
->
data
<
T
>
();
const
int
*
p_index
=
index
->
data
<
int
>
();
T
*
p_output
=
output
->
data
<
T
>
();
int
block
=
512
;
int
n
=
slice_size
*
index_size
;
int
grid
=
(
n
+
block
-
1
)
/
block
;
GatherCUDAKernel
<
T
><<<
grid
,
block
>>>
(
p_src
,
p_index
,
p_output
,
index_size
,
slice_size
);
}
}
// namespace operators
...
...
paddle/operators/gather.h
浏览文件 @
78808b20
...
...
@@ -24,32 +24,18 @@ limitations under the License. */
namespace
paddle
{
namespace
operators
{
// Implementation of CPU copy
template
<
typename
T
>
struct
CPUGather
{
void
operator
()(
const
T
*
src
,
const
int
*
indices
,
const
int
slice_size
,
const
int
index_size
,
T
*
output
)
{
const
size_t
slice_bytes
=
slice_size
*
sizeof
(
T
);
for
(
int
i
=
0
;
i
<
index_size
;
++
i
)
{
int
index_
=
indices
[
i
];
memcpy
(
output
+
i
*
slice_size
,
src
+
index_
*
slice_size
,
slice_bytes
);
}
}
};
/**
* A thin wrapper on cpu tensor
* A thin wrapper
for gathering
on cpu tensor
* Return a new tensor from source tensor, gathered according to index
* input[src]: type-T source Tensor
* input[index]: type-int index Tensor (1-D)
* return: output tensor
*/
template
<
typename
T
>
void
CPU
T
Gather
(
const
platform
::
Place
&
place
,
const
paddle
::
framework
::
Tensor
*
src
,
const
paddle
::
framework
::
Tensor
*
index
,
paddle
::
framework
::
Tensor
*
output
)
{
void
CPUGather
(
const
platform
::
Place
&
place
,
const
paddle
::
framework
::
Tensor
*
src
,
const
paddle
::
framework
::
Tensor
*
index
,
paddle
::
framework
::
Tensor
*
output
)
{
PADDLE_ENFORCE
(
platform
::
is_cpu_place
(
place
));
// check index of shape 1-D
PADDLE_ENFORCE
(
index
->
dims
().
size
()
==
1
);
...
...
@@ -59,14 +45,20 @@ void CPUTGather(const platform::Place& place,
framework
::
DDim
output_dims
(
src_dims
);
output_dims
[
0
]
=
index_size
;
const
T
*
p_src
=
src
->
data
<
T
>
();
const
int
*
p_index
=
index
->
data
<
int
>
();
T
*
p_output
=
output
->
data
<
T
>
();
// slice size
int
slice_size
=
1
;
for
(
int
i
=
1
;
i
<
src_dims
.
size
();
++
i
)
slice_size
*=
src_dims
[
i
];
// Gathering
CPUGather
<
T
>
gather_functor
;
gather_functor
(
src
->
data
<
T
>
(),
index
->
data
<
int
>
(),
slice_size
,
index_size
,
output
->
data
<
T
>
());
const
size_t
slice_bytes
=
slice_size
*
sizeof
(
T
);
for
(
int
i
=
0
;
i
<
index_size
;
++
i
)
{
int
index_
=
p_index
[
i
];
memcpy
(
p_output
+
i
*
slice_size
,
p_src
+
index_
*
slice_size
,
slice_bytes
);
}
}
}
// namespace operators
...
...
paddle/operators/gather_op.cu
浏览文件 @
78808b20
...
...
@@ -32,7 +32,7 @@ class GatherOpCUDAKernel : public framework::OpKernel<T> {
output
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
GPU
T
Gather
<
T
>
(
ctx
.
GetPlace
(),
x
,
index
,
output
);
GPUGather
<
T
>
(
ctx
.
GetPlace
(),
x
,
index
,
output
);
}
};
...
...
@@ -53,7 +53,7 @@ class GatherGradOpCUDAKernel : public framework::OpKernel<T> {
auto
place
=
ctx
.
GetEigenDevice
<
platform
::
GPUPlace
>
();
dxt
.
device
(
place
)
=
dxt
.
constant
(
static_cast
<
T
>
(
0
));
GPU
TScatter
<
T
>
(
ctx
.
GetPlace
(),
dO
,
Index
,
dX
);
GPU
ScatterAssign
<
T
>
(
ctx
.
GetPlace
(),
dO
,
Index
,
dX
);
}
};
...
...
paddle/operators/gather_op.h
浏览文件 @
78808b20
...
...
@@ -36,7 +36,7 @@ class GatherOpKernel : public framework::OpKernel<T> {
output
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
CPU
T
Gather
<
T
>
(
ctx
.
GetPlace
(),
x
,
index
,
output
);
CPUGather
<
T
>
(
ctx
.
GetPlace
(),
x
,
index
,
output
);
}
};
...
...
paddle/operators/gather_test.cc
浏览文件 @
78808b20
...
...
@@ -41,7 +41,7 @@ TEST(Gather, GatherData) {
int
*
p_output
=
output
->
mutable_data
<
int
>
(
make_ddim
({
2
,
4
}),
CPUPlace
());
CPU
T
Gather
<
int
>
(
CPUPlace
(),
src
,
index
,
output
);
CPUGather
<
int
>
(
CPUPlace
(),
src
,
index
,
output
);
for
(
int
i
=
0
;
i
<
4
;
++
i
)
EXPECT_EQ
(
p_output
[
i
],
i
+
4
);
for
(
int
i
=
4
;
i
<
8
;
++
i
)
EXPECT_EQ
(
p_output
[
i
],
i
-
4
);
...
...
paddle/operators/scatter.cu.h
浏览文件 @
78808b20
...
...
@@ -36,20 +36,6 @@ __global__ void ScatterCUDAKernel(const T* params, const int* indices,
}
}
// Implementation of GPU copy:
template
<
typename
T
>
struct
GPUScatterAssign
{
void
operator
()(
const
T
*
src
,
const
int
*
index
,
const
int
slice_size
,
const
int
index_size
,
T
*
output
)
{
int
block
=
512
;
int
n
=
slice_size
*
index_size
;
int
grid
=
(
n
+
block
-
1
)
/
block
;
// printf("grid, block: %d %d\n", grid, block);
ScatterCUDAKernel
<
T
><<<
grid
,
block
>>>
(
src
,
index
,
output
,
index_size
,
slice_size
);
}
};
/**
* A thin wrapper on gpu tensor
* Return a new updated tensor from source tensor, scatter-assigned according to
...
...
@@ -59,10 +45,10 @@ struct GPUScatterAssign {
* return: output tensor
*/
template
<
typename
T
>
void
GPU
TScatter
(
const
platform
::
Place
&
place
,
const
paddle
::
framework
::
Tensor
*
src
,
const
paddle
::
framework
::
Tensor
*
index
,
paddle
::
framework
::
Tensor
*
output
)
{
void
GPU
ScatterAssign
(
const
platform
::
Place
&
place
,
const
paddle
::
framework
::
Tensor
*
src
,
const
paddle
::
framework
::
Tensor
*
index
,
paddle
::
framework
::
Tensor
*
output
)
{
PADDLE_ENFORCE
(
platform
::
is_gpu_place
(
place
));
// check index of shape 1-D
PADDLE_ENFORCE
(
index
->
dims
().
size
()
==
1
);
...
...
@@ -76,10 +62,16 @@ void GPUTScatter(const platform::Place& place,
int
slice_size
=
1
;
for
(
int
i
=
1
;
i
<
src_dims
.
size
();
++
i
)
slice_size
*=
src_dims
[
i
];
// Scatter Assign
GPUScatterAssign
<
T
>
scatter_functor
;
scatter_functor
(
src
->
data
<
T
>
(),
index
->
data
<
int
>
(),
slice_size
,
index_size
,
output
->
data
<
T
>
());
const
T
*
p_src
=
src
->
data
<
T
>
();
const
int
*
p_index
=
index
->
data
<
int
>
();
T
*
p_output
=
output
->
data
<
T
>
();
int
block
=
512
;
int
n
=
slice_size
*
index_size
;
int
grid
=
(
n
+
block
-
1
)
/
block
;
ScatterCUDAKernel
<
T
><<<
grid
,
block
>>>
(
p_src
,
p_index
,
p_output
,
index_size
,
slice_size
);
}
}
// namespace operators
...
...
paddle/operators/scatter.h
浏览文件 @
78808b20
...
...
@@ -25,19 +25,6 @@ namespace operators {
using
Tensor
=
framework
::
Tensor
;
// Implementation of CPU copy
template
<
typename
T
>
void
CPUScatterAssign
(
const
T
*
src
,
const
int
*
index
,
const
int
slice_size
,
const
int
index_size
,
T
*
output
)
{
// paddle::framework::DDim output_dims = output->dims();
const
size_t
slice_bytes
=
slice_size
*
sizeof
(
T
);
for
(
int
i
=
0
;
i
<
index_size
;
++
i
)
{
int
index_
=
index
[
i
];
memcpy
(
output
+
index_
*
slice_size
,
src
+
i
*
slice_size
,
slice_bytes
);
}
}
/**
* Return a updated tensor from source tensor, scattered according to index:
* dst[i] = src[index[i]]
...
...
@@ -70,7 +57,12 @@ void ScatterAssign(const platform::Place& place,
size_t
slice_size
=
1
;
for
(
int
i
=
1
;
i
<
src_dims
.
size
();
++
i
)
slice_size
*=
src_dims
[
i
];
CPUScatterAssign
<
T
>
(
p_src
,
p_index
,
slice_size
,
index_size
,
p_output
);
const
size_t
slice_bytes
=
slice_size
*
sizeof
(
T
);
for
(
int
i
=
0
;
i
<
index_size
;
++
i
)
{
int
index_
=
p_index
[
i
];
memcpy
(
p_output
+
index_
*
slice_size
,
p_src
+
i
*
slice_size
,
slice_bytes
);
}
}
}
// namespace operators
...
...
paddle/operators/scatter_op.cu
浏览文件 @
78808b20
...
...
@@ -32,7 +32,7 @@ class ScatterOpCUDAKernel : public framework::OpKernel<T> {
Out
->
ShareDataWith
<
T
>
(
*
Ref
);
GPU
TScatter
<
T
>
(
ctx
.
GetPlace
(),
Updates
,
Index
,
Out
);
GPU
ScatterAssign
<
T
>
(
ctx
.
GetPlace
(),
Updates
,
Index
,
Out
);
}
};
...
...
@@ -51,7 +51,7 @@ class ScatterGradOpCUDAKernel : public framework::OpKernel<T> {
dRef
->
ShareDataWith
<
T
>
(
*
dOut
);
dUpdates
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
// Gradient by Gather: dUpdates = dO[Index]
GPU
T
Gather
<
T
>
(
ctx
.
GetPlace
(),
dOut
,
Index
,
dUpdates
);
GPUGather
<
T
>
(
ctx
.
GetPlace
(),
dOut
,
Index
,
dUpdates
);
}
};
...
...
paddle/operators/scatter_op.h
浏览文件 @
78808b20
...
...
@@ -56,7 +56,7 @@ class ScatterGradientOpKernel : public framework::OpKernel<T> {
dRef
->
ShareDataWith
<
T
>
(
*
dOut
);
dUpdates
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
// Gradient by Gather: dUpdates += dO[Index]
CPU
T
Gather
<
T
>
(
ctx
.
GetPlace
(),
dOut
,
Index
,
dUpdates
);
CPUGather
<
T
>
(
ctx
.
GetPlace
(),
dOut
,
Index
,
dUpdates
);
}
};
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录