Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
0c37705d
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
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看板
提交
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.
...
@@ -49,5 +49,4 @@ Used to initialize tensor with uniform random generator.
}
// namespace paddle
}
// namespace paddle
REGISTER_OP
(
uniform_random
,
ops
::
RandomOp
,
ops
::
RandomOpMaker
);
REGISTER_OP
(
uniform_random
,
ops
::
RandomOp
,
ops
::
RandomOpMaker
);
REGISTER_OP_CPU_KERNEL
(
uniform_random
,
REGISTER_OP_CPU_KERNEL
(
uniform_random
,
ops
::
CPUUniformRandomKernel
<
float
>
);
ops
::
UniformRandomKernel
<
ops
::
CPUPlace
,
float
>
);
paddle/operators/uniform_random_op.cu
浏览文件 @
0c37705d
...
@@ -12,7 +12,54 @@
...
@@ -12,7 +12,54 @@
See the License for the specific language governing permissions and
See the License for the specific language governing permissions and
limitations under the License. */
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
,
namespace
paddle
{
ops
::
UniformRandomKernel
<
ops
::
GPUPlace
,
float
>
);
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 @@
...
@@ -13,25 +13,30 @@
limitations under the License. */
limitations under the License. */
#pragma once
#pragma once
#include <random>
#include <type_traits>
#include "paddle/operators/type_alias.h"
#include "paddle/operators/type_alias.h"
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
template
<
typename
Place
,
typename
T
>
template
<
typename
T
>
class
UniformRandomKernel
:
public
OpKernel
{
class
CPU
UniformRandomKernel
:
public
OpKernel
{
public:
public:
void
Compute
(
const
ExecutionContext
&
context
)
const
override
{
void
Compute
(
const
ExecutionContext
&
context
)
const
override
{
auto
tensor
=
context
.
Output
<
Tensor
>
(
0
);
auto
*
tensor
=
context
.
Output
<
Tensor
>
(
0
);
tensor
->
mutable_data
<
T
>
(
context
.
GetPlace
());
T
*
data
=
tensor
->
mutable_data
<
T
>
(
context
.
GetPlace
());
unsigned
int
seed
=
auto
eigenTensor
=
EigenVector
<
T
>::
Flatten
(
*
tensor
);
static_cast
<
unsigned
int
>
(
context
.
op_
.
GetAttr
<
int
>
(
"seed"
));
auto
dev
=
context
.
GetEigenDevice
<
Place
>
();
std
::
minstd_rand
engine
;
auto
min
=
context
.
op_
.
GetAttr
<
float
>
(
"min"
);
if
(
seed
==
0
)
{
auto
max
=
context
.
op_
.
GetAttr
<
float
>
(
"max"
);
seed
=
std
::
random_device
()();
auto
seed
=
static_cast
<
uint64_t
>
(
context
.
op_
.
GetAttr
<
int
>
(
"seed"
));
}
auto
diff
=
max
-
min
;
engine
.
seed
(
seed
);
Eigen
::
internal
::
UniformRandomGenerator
<
T
>
gen
(
seed
);
std
::
uniform_real_distribution
<
T
>
dist
(
static_cast
<
T
>
(
context
.
op_
.
GetAttr
<
float
>
(
"min"
)),
eigenTensor
.
device
(
dev
)
=
eigenTensor
.
random
(
gen
)
*
diff
+
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录