Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
urhero
Paddle
提交
6b4a51ba
P
Paddle
项目概览
urhero
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
6b4a51ba
编写于
4月 13, 2020
作者:
M
mapingshuo
提交者:
GitHub
4月 13, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add cuda kernel for seed, test=develop (#23749)
* add cuda kernel for seed, test=develop
上级
a9fe09f8
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
207 addition
and
15 deletion
+207
-15
paddle/fluid/operators/dropout_op.cu
paddle/fluid/operators/dropout_op.cu
+53
-14
paddle/fluid/operators/seed_op.cc
paddle/fluid/operators/seed_op.cc
+1
-1
paddle/fluid/operators/seed_op.cu
paddle/fluid/operators/seed_op.cu
+47
-0
python/paddle/fluid/tests/unittests/test_optimizer.py
python/paddle/fluid/tests/unittests/test_optimizer.py
+106
-0
未找到文件。
paddle/fluid/operators/dropout_op.cu
浏览文件 @
6b4a51ba
...
...
@@ -18,9 +18,11 @@ limitations under the License. */
#include <thrust/random.h>
#include <thrust/transform.h>
#include <string>
#include "paddle/fluid/memory/memcpy.h"
#include "paddle/fluid/operators/dropout_op.h"
#include "paddle/fluid/platform/dynload/curand.h"
#include "paddle/fluid/platform/float16.h"
namespace
paddle
{
namespace
operators
{
...
...
@@ -59,6 +61,41 @@ __global__ void RandomGenerator(const size_t n, const int seed,
}
}
template
<
typename
T
,
typename
MaskType
>
__global__
void
RandomGeneratorWithSeed
(
const
size_t
n
,
const
int
*
seed
,
const
float
dropout_prob
,
const
T
*
src
,
MaskType
*
mask_data
,
T
*
dst
,
bool
is_upscale_in_train
)
{
curandStatePhilox4_32_10_t
state
;
int
idx
=
blockDim
.
x
*
blockIdx
.
x
+
threadIdx
.
x
;
int
step_size
=
0
;
MaskType
mask
;
T
dest
;
for
(;
idx
<
n
;
idx
+=
blockDim
.
x
*
gridDim
.
x
)
{
T
s
=
src
[
idx
];
if
(
step_size
==
0
)
{
curand_init
(
seed
[
0
],
idx
,
idx
,
&
state
);
step_size
=
blockDim
.
x
*
gridDim
.
x
;
}
else
{
curand_init
(
seed
[
0
],
idx
,
step_size
,
&
state
);
}
if
(
curand_uniform
(
&
state
)
<
dropout_prob
)
{
mask
=
0
;
dest
=
0
;
}
else
{
mask
=
1
;
if
(
is_upscale_in_train
)
{
dest
=
s
/
static_cast
<
T
>
(
1.0
f
-
dropout_prob
);
}
else
{
dest
=
s
;
}
}
mask_data
[
idx
]
=
mask
;
dst
[
idx
]
=
dest
;
}
}
// It seems that Eigen::Tensor::setRandom in GPU will SEGFAULT.
// Use std::random and thrust::random(thrust is a std library in CUDA) to
// implement uniform random.
...
...
@@ -86,20 +123,6 @@ class GPUDropoutKernel : public framework::OpKernel<T> {
auto
*
mask_data
=
mask
->
mutable_data
<
uint8_t
>
(
context
.
GetPlace
());
size_t
size
=
framework
::
product
(
mask
->
dims
());
auto
*
x_data
=
x
->
data
<
T
>
();
int
seed_data
;
std
::
random_device
rnd
;
if
(
seed
)
{
if
(
platform
::
is_gpu_place
(
seed
->
place
()))
{
framework
::
Tensor
temp
;
TensorCopySync
(
*
seed
,
platform
::
CPUPlace
(),
&
temp
);
seed_data
=
*
(
temp
.
data
<
int
>
());
}
else
{
seed_data
=
*
(
seed
->
data
<
int
>
());
}
}
else
{
seed_data
=
context
.
Attr
<
bool
>
(
"fix_seed"
)
?
context
.
Attr
<
int
>
(
"seed"
)
:
rnd
();
}
auto
*
y_data
=
y
->
mutable_data
<
T
>
(
context
.
GetPlace
());
if
(
dropout_prob
==
1.0
f
)
{
PADDLE_ENFORCE_CUDA_SUCCESS
(
...
...
@@ -111,6 +134,22 @@ class GPUDropoutKernel : public framework::OpKernel<T> {
int
threads
=
512
;
int
grid
=
(
x_numel
+
threads
-
1
)
/
threads
;
if
(
seed
&&
platform
::
is_gpu_place
(
seed
->
place
()))
{
auto
seed_gpu_data
=
seed
->
data
<
int
>
();
RandomGeneratorWithSeed
<
T
,
uint8_t
><<<
grid
,
threads
,
0
,
stream
>>>
(
size
,
seed_gpu_data
,
dropout_prob
,
x_data
,
mask_data
,
y_data
,
upscale_in_train
);
return
;
}
int
seed_data
;
std
::
random_device
rnd
;
if
(
seed
)
{
seed_data
=
*
(
seed
->
data
<
int
>
());
}
else
{
seed_data
=
context
.
Attr
<
bool
>
(
"fix_seed"
)
?
context
.
Attr
<
int
>
(
"seed"
)
:
rnd
();
}
RandomGenerator
<
T
,
uint8_t
><<<
grid
,
threads
,
0
,
stream
>>>
(
size
,
seed_data
,
dropout_prob
,
x_data
,
mask_data
,
y_data
,
upscale_in_train
);
...
...
paddle/fluid/operators/seed_op.cc
浏览文件 @
6b4a51ba
...
...
@@ -30,7 +30,7 @@ class SeedOp : public framework::OperatorWithKernel {
framework
::
OpKernelType
GetExpectedKernelType
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
return
framework
::
OpKernelType
(
framework
::
proto
::
VarType
::
INT32
,
platform
::
CPUPlace
());
ctx
.
device_context
());
}
};
...
...
paddle/fluid/operators/seed_op.cu
0 → 100644
浏览文件 @
6b4a51ba
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// 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 <cuda.h>
#include "paddle/fluid/operators/seed_op.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
Place
,
typename
T
>
class
GPUSeedKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
out
=
context
.
Output
<
Tensor
>
(
"Out"
);
auto
*
out_data
=
out
->
mutable_data
<
T
>
(
context
.
GetPlace
());
int
user_seed
=
context
.
Attr
<
int
>
(
"seed"
);
std
::
random_device
rnd
;
int
seed
;
if
(
user_seed
!=
0
)
{
seed
=
user_seed
;
}
else
{
seed
=
rnd
();
}
auto
target_gpu_place
=
boost
::
get
<
platform
::
CUDAPlace
>
(
context
.
GetPlace
());
auto
stream
=
context
.
cuda_device_context
().
stream
();
memory
::
Copy
(
target_gpu_place
,
out_data
,
platform
::
CPUPlace
(),
&
seed
,
sizeof
(
int
),
stream
);
}
};
}
// namespace operators
}
// namespace paddle
REGISTER_OP_CUDA_KERNEL
(
seed
,
paddle
::
operators
::
GPUSeedKernel
<
paddle
::
platform
::
CUDADeviceContext
,
int
>
);
python/paddle/fluid/tests/unittests/test_optimizer.py
浏览文件 @
6b4a51ba
...
...
@@ -16,10 +16,14 @@ from __future__ import print_function
import
unittest
import
paddle.fluid
as
fluid
import
paddle.fluid.framework
as
framework
import
paddle.fluid.optimizer
as
optimizer
import
paddle.fluid.core
as
core
import
paddle.compat
as
cpt
import
numpy
as
np
from
paddle.fluid.backward
import
append_backward
from
paddle.fluid.framework
import
Program
,
program_guard
class
TestOptimizer
(
unittest
.
TestCase
):
...
...
@@ -841,6 +845,108 @@ class TestRecomputeOptimizer(unittest.TestCase):
"sgd"
,
"sgd"
,
"sgd"
])
def
test_dropout_with_seed
(
self
):
"""
when we recompute a dropout op, make sure that the recomputed one
is the same as the original var.
"""
def
gen_data
():
return
{
"x"
:
np
.
random
.
random
(
size
=
(
100
,
3
)).
astype
(
'float32'
),
"y"
:
np
.
random
.
randint
(
2
,
size
=
(
100
,
1
)).
astype
(
'int64'
)
}
def
mlp
(
input_x
,
input_y
):
drop_res
=
fluid
.
layers
.
dropout
(
input_x
,
dropout_prob
=
0.5
,
name
=
"dropout_with_seed_cpu"
)
prediction
=
fluid
.
layers
.
fc
(
input
=
[
drop_res
],
size
=
2
,
act
=
'softmax'
)
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
prediction
,
label
=
input_y
)
sum_cost
=
fluid
.
layers
.
reduce_mean
(
cost
)
return
drop_res
,
prediction
,
sum_cost
main_program
=
Program
()
startup_program
=
Program
()
scope
=
fluid
.
Scope
()
with
fluid
.
scope_guard
(
scope
):
with
program_guard
(
main_program
,
startup_program
):
input_x
=
fluid
.
layers
.
data
(
name
=
"x"
,
shape
=
[
3
],
dtype
=
'float32'
)
input_y
=
fluid
.
layers
.
data
(
name
=
"y"
,
shape
=
[
1
],
dtype
=
'int64'
)
drop_res
,
prediction
,
cost
=
mlp
(
input_x
,
input_y
)
sgd
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
0.01
)
sgd
=
fluid
.
optimizer
.
RecomputeOptimizer
(
sgd
)
sgd
.
_set_checkpoints
([
prediction
])
sgd
.
minimize
(
cost
)
place
=
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
exe
.
run
(
fluid
.
default_startup_program
())
feed_data
=
gen_data
()
drop_vec
=
exe
.
run
(
feed
=
feed_data
,
program
=
fluid
.
default_main_program
(),
fetch_list
=
[
"dropout_with_seed_cpu.tmp_1"
,
"dropout_with_seed_cpu.tmp_1.subprog_0"
])
self
.
assertEqual
(
drop_vec
[
0
].
tolist
(),
drop_vec
[
1
].
tolist
())
@
unittest
.
skipIf
(
not
core
.
is_compiled_with_cuda
(),
"core is not compiled with CUDA"
)
class
TestRecomputeOptimizerCUDA
(
unittest
.
TestCase
):
def
test_dropout_with_seed
(
self
):
"""
when we recompute a dropout op, make sure that the recomputed one
is the same as the original var.
"""
def
gen_data
():
return
{
"x"
:
np
.
random
.
random
(
size
=
(
100
,
3
)).
astype
(
'float32'
),
"y"
:
np
.
random
.
randint
(
2
,
size
=
(
100
,
1
)).
astype
(
'int64'
)
}
def
mlp
(
input_x
,
input_y
):
drop_res
=
fluid
.
layers
.
dropout
(
input_x
,
dropout_prob
=
0.5
,
name
=
"dropout_with_seed_gpu"
)
prediction
=
fluid
.
layers
.
fc
(
input
=
[
drop_res
],
size
=
2
,
act
=
'softmax'
)
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
prediction
,
label
=
input_y
)
sum_cost
=
fluid
.
layers
.
reduce_mean
(
cost
)
return
drop_res
,
prediction
,
sum_cost
main_program
=
Program
()
startup_program
=
Program
()
scope
=
fluid
.
Scope
()
with
fluid
.
scope_guard
(
scope
):
with
program_guard
(
main_program
,
startup_program
):
input_x
=
fluid
.
layers
.
data
(
name
=
"x"
,
shape
=
[
3
],
dtype
=
'float32'
)
input_y
=
fluid
.
layers
.
data
(
name
=
"y"
,
shape
=
[
1
],
dtype
=
'int64'
)
drop_res
,
prediction
,
cost
=
mlp
(
input_x
,
input_y
)
sgd
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
0.01
)
sgd
=
fluid
.
optimizer
.
RecomputeOptimizer
(
sgd
)
sgd
.
_set_checkpoints
([
prediction
])
sgd
.
minimize
(
cost
)
place
=
fluid
.
CUDAPlace
(
0
)
exe
=
fluid
.
Executor
(
place
)
exe
.
run
(
fluid
.
default_startup_program
())
feed_data
=
gen_data
()
drop_vec
=
exe
.
run
(
feed
=
feed_data
,
program
=
fluid
.
default_main_program
(),
fetch_list
=
[
"dropout_with_seed_gpu.tmp_1"
,
"dropout_with_seed_gpu.tmp_1.subprog_0"
])
self
.
assertEqual
(
drop_vec
[
0
].
tolist
(),
drop_vec
[
1
].
tolist
())
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录