Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
0c37705d
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 2 年 前同步成功
通知
708
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
0c37705d
编写于
8月 07, 2017
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use thrust to implement uniform_random
上级
fd0bdb4f
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
70 addition
and
19 deletion
+70
-19
paddle/operators/uniform_random_op.cc
paddle/operators/uniform_random_op.cc
+1
-2
paddle/operators/uniform_random_op.cu
paddle/operators/uniform_random_op.cu
+50
-3
paddle/operators/uniform_random_op.h
paddle/operators/uniform_random_op.h
+19
-14
未找到文件。
paddle/operators/uniform_random_op.cc
浏览文件 @
0c37705d
...
...
@@ -49,5 +49,4 @@ Used to initialize tensor with uniform random generator.
}
// namespace paddle
REGISTER_OP
(
uniform_random
,
ops
::
RandomOp
,
ops
::
RandomOpMaker
);
REGISTER_OP_CPU_KERNEL
(
uniform_random
,
ops
::
UniformRandomKernel
<
ops
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
uniform_random
,
ops
::
CPUUniformRandomKernel
<
float
>
);
paddle/operators/uniform_random_op.cu
浏览文件 @
0c37705d
...
...
@@ -12,7 +12,54 @@
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/uniform_random_op.h"
#include <thrust/device_ptr.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/random.h>
#include <thrust/transform.h>
#include "paddle/operators/type_alias.h"
REGISTER_OP_GPU_KERNEL
(
uniform_random
,
ops
::
UniformRandomKernel
<
ops
::
GPUPlace
,
float
>
);
namespace
paddle
{
namespace
operators
{
template
<
typename
T
>
struct
UniformGenerator
{
T
min_
,
max_
;
unsigned
int
seed_
;
__host__
__device__
UniformGenerator
(
T
min
,
T
max
,
int
seed
)
:
min_
(
min
),
max_
(
max
),
seed_
(
seed
)
{}
__host__
__device__
T
operator
()(
const
unsigned
int
n
)
const
{
thrust
::
minstd_rand
rng
;
rng
.
seed
(
seed_
);
thrust
::
uniform_real_distribution
<
T
>
dist
(
min_
,
max_
);
rng
.
discard
(
n
);
return
dist
(
rng
);
}
};
template
<
typename
T
>
class
GPUUniformRandomKernel
:
public
OpKernel
{
public:
void
Compute
(
const
ExecutionContext
&
context
)
const
override
{
auto
*
tensor
=
context
.
Output
<
Tensor
>
(
0
);
T
*
data
=
tensor
->
mutable_data
<
T
>
(
context
.
GetPlace
());
unsigned
int
seed
=
static_cast
<
unsigned
int
>
(
context
.
op_
.
GetAttr
<
int
>
(
"seed"
));
if
(
seed
==
0
)
{
seed
=
std
::
random_device
()();
}
T
min
=
static_cast
<
T
>
(
context
.
op_
.
GetAttr
<
float
>
(
"min"
));
T
max
=
static_cast
<
T
>
(
context
.
op_
.
GetAttr
<
float
>
(
"max"
));
thrust
::
counting_iterator
<
unsigned
int
>
index_sequence_begin
(
0
);
ssize_t
N
=
framework
::
product
(
tensor
->
dims
());
thrust
::
transform
(
index_sequence_begin
,
index_sequence_begin
+
N
,
thrust
::
device_ptr
<
T
>
(
data
),
UniformGenerator
<
T
>
(
min
,
max
,
seed
));
}
};
}
// namespace operators
}
// namespace paddle
REGISTER_OP_GPU_KERNEL
(
uniform_random
,
ops
::
GPUUniformRandomKernel
<
float
>
);
paddle/operators/uniform_random_op.h
浏览文件 @
0c37705d
...
...
@@ -13,25 +13,30 @@
limitations under the License. */
#pragma once
#include <random>
#include <type_traits>
#include "paddle/operators/type_alias.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
Place
,
typename
T
>
class
UniformRandomKernel
:
public
OpKernel
{
template
<
typename
T
>
class
CPU
UniformRandomKernel
:
public
OpKernel
{
public:
void
Compute
(
const
ExecutionContext
&
context
)
const
override
{
auto
tensor
=
context
.
Output
<
Tensor
>
(
0
);
tensor
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
eigenTensor
=
EigenVector
<
T
>::
Flatten
(
*
tensor
);
auto
dev
=
context
.
GetEigenDevice
<
Place
>
();
auto
min
=
context
.
op_
.
GetAttr
<
float
>
(
"min"
);
auto
max
=
context
.
op_
.
GetAttr
<
float
>
(
"max"
);
auto
seed
=
static_cast
<
uint64_t
>
(
context
.
op_
.
GetAttr
<
int
>
(
"seed"
));
auto
diff
=
max
-
min
;
Eigen
::
internal
::
UniformRandomGenerator
<
T
>
gen
(
seed
);
eigenTensor
.
device
(
dev
)
=
eigenTensor
.
random
(
gen
)
*
diff
+
min
;
void
Compute
(
const
ExecutionContext
&
context
)
const
override
{
auto
*
tensor
=
context
.
Output
<
Tensor
>
(
0
);
T
*
data
=
tensor
->
mutable_data
<
T
>
(
context
.
GetPlace
());
unsigned
int
seed
=
static_cast
<
unsigned
int
>
(
context
.
op_
.
GetAttr
<
int
>
(
"seed"
));
std
::
minstd_rand
engine
;
if
(
seed
==
0
)
{
seed
=
std
::
random_device
()();
}
engine
.
seed
(
seed
);
std
::
uniform_real_distribution
<
T
>
dist
(
static_cast
<
T
>
(
context
.
op_
.
GetAttr
<
float
>
(
"min"
)),
static_cast
<
T
>
(
context
.
op_
.
GetAttr
<
float
>
(
"max"
)));
for
(
ssize_t
i
=
0
;
i
<
framework
::
product
(
tensor
->
dims
());
++
i
)
{
data
[
i
]
=
dist
(
engine
);
}
}
};
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录