Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
6f80b5f1
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看板
提交
6f80b5f1
编写于
7月 25, 2017
作者:
D
dongzhihong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
"move to template function"
上级
0d554f1d
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
81 addition
and
34 deletion
+81
-34
paddle/operators/random_op.cc
paddle/operators/random_op.cc
+28
-6
paddle/operators/random_op.cu
paddle/operators/random_op.cu
+4
-3
paddle/operators/random_op.h
paddle/operators/random_op.h
+20
-8
paddle/platform/device_context.h
paddle/platform/device_context.h
+29
-17
未找到文件。
paddle/operators/random_op.cc
浏览文件 @
6f80b5f1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
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 "paddle/operators/random_op.h"
#include "paddle/framework/op_registry.h"
namespace
paddle
{
namespace
operators
{
using
paddle
::
platform
::
GPUPlace
;
template
<
GPUPlace
,
typename
T
,
typename
Generator
>
bool
Gaussian
(
Generator
g
,
T
*
output
,
const
int
size
,
const
T
&
mean
,
const
T
&
std
)
{
// using paddle::platform::CPUPlace;
// template <paddle::platform::CPUPlace, typename T, typename DeviceContext>
template
<
typename
T
>
bool
Gaussian
(
platform
::
CPUDeviceContext
&
ctx
,
framework
::
Tensor
*
output
,
const
int
size
,
const
T
&
mean
,
const
T
&
std
,
const
T
&
seed
)
{
auto
g
=
ctx
.
RandGenerator
(
seed
);
std
::
normal_distribution
<
double
>
distribution
(
mean
,
std
);
for
(
int
i
=
0
;
i
<
size
;
++
i
)
{
output
[
i
]
=
distribution
(
g
());
...
...
@@ -24,7 +44,9 @@ protected:
PADDLE_ENFORCE
(
outputs
.
size
()
==
1
,
"Output size of RandomOp must be one."
);
PADDLE_ENFORCE
(
inputs
[
0
]
!=
nullptr
&&
outputs
[
0
]
!=
nullptr
,
"Inputs/Outputs of RandomOp must all be set."
);
outputs
[
0
]
->
set_dims
(
context
.
op_
.
attrs_
.
at
(
"shape"
));
outputs
[
0
]
->
Resize
(
framework
::
make_ddim
(
this
->
GetAttr
<
std
::
vector
<
int
>>
(
"shape"
)));
// outputs[0]->set_dims(context.op_.attrs_.at("shape"));
}
};
...
...
@@ -32,7 +54,7 @@ class RandomOpMaker : public framework::OpProtoAndCheckerMaker {
public:
RandomOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
framework
::
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddAttr
<
std
::
vector
<
int
>>
(
"
S
hape"
,
"The shape of matrix to be randomized"
);
AddAttr
<
std
::
vector
<
int
>>
(
"
s
hape"
,
"The shape of matrix to be randomized"
);
AddAttr
<
float
>
(
"seed"
,
"random seed generator."
).
SetDefault
(
1337
);
AddAttr
<
float
>
(
"mean"
,
"mean value of random."
).
SetDefault
(
.0
);
AddAttr
<
float
>
(
"std"
,
"minimum value of random value"
)
...
...
paddle/operators/random_op.cu
浏览文件 @
6f80b5f1
...
...
@@ -4,9 +4,10 @@
namespace
paddle
{
namespace
operators
{
using
paddle
::
platform
::
GPUPlace
;
template
<
GPUPlace
,
typename
T
,
typename
Generator
>
bool
Gaussian
(
Generator
g
,
T
*
output
,
const
int
size
,
const
T
&
mean
,
const
T
&
std
)
{
template
<
typename
T
>
bool
Gaussian
(
platform
::
CUDADeviceContext
&
ctx
,
framework
::
Tensor
*
output
,
const
int
size
,
const
T
&
mean
,
const
T
&
std
,
const
T
&
seed
)
{
auto
g
=
RandGenerator
(
seed
);
return
curandGenerateNormal
(
g
,
output
,
size
,
mean
,
std
);
}
...
...
paddle/operators/random_op.h
浏览文件 @
6f80b5f1
...
...
@@ -6,21 +6,33 @@
namespace
paddle
{
namespace
operators
{
template
<
typename
Place
,
typename
T
,
typename
Generator
>
bool
Gaussian
(
Generator
g
,
T
*
output
,
const
int
size
,
const
T
&
mean
,
const
T
&
std
);
template
<
typename
T
,
typename
DeviceContext
>
bool
Gaussian
(
DeviceContext
&
ctx
,
framework
::
Tensor
*
output
,
const
int
size
,
const
T
&
mean
,
const
T
&
std
,
const
T
&
seed
);
template
<
typename
Place
,
typename
T
>
class
RandomOpKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
KernelContext
&
context
)
const
override
{
auto
mean
=
context
.
op_
.
attrs_
.
at
(
"mean"
);
auto
std
=
context
.
op_
.
attrs_
.
at
(
"std"
);
auto
seed
=
context
.
op_
.
attrs_
.
at
(
"seed"
);
auto
mean
=
context
.
op_
.
GetAttr
<
T
>
(
"mean"
);
auto
std
=
context
.
op_
.
GetAttr
<
T
>
(
"std"
);
auto
seed
=
context
.
op_
.
GetAttr
<
T
>
(
"seed"
);
auto
*
output
=
context
.
Output
(
0
)
->
GetMutable
<
framework
::
Tensor
>
();
output
->
mutable_data
<
T
>
(
context
.
GetPlace
());
Gaussian
<
Place
,
T
,
>
(,
output
,
output
->
size
(),
mean
,
std
)
:
Gaussian
(
context
.
device_context_
,
output
,
framework
::
product
(
output
->
dims
()),
mean
,
std
,
seed
);
// Gaussian<T, const platform::DeviceContext>(context.device_context_,
// output,
// framework::product(output->dims()),
// mean, std, seed);
// std::default_random_engine generator(seed);
// std::normal_distribution<double> distribution(mean, std);
...
...
paddle/platform/device_context.h
浏览文件 @
6f80b5f1
...
...
@@ -39,6 +39,7 @@ class DeviceContext {
class
CPUDeviceContext
:
public
DeviceContext
{
public:
typedef
std
::
mt19937
random_generator_type
;
CPUDeviceContext
()
{
eigen_device_
.
reset
(
new
Eigen
::
DefaultDevice
());
}
Eigen
::
DefaultDevice
*
eigen_device
()
const
{
return
eigen_device_
.
get
();
}
...
...
@@ -48,7 +49,17 @@ class CPUDeviceContext : public DeviceContext {
return
retv
;
}
const
random_generator_type
&
RandGenerator
(
const
int
seed
)
{
if
(
!
rand_generator_
)
{
random_seed_
=
seed
;
rand_generator_
.
reset
(
new
random_generator_type
(
random_seed_
));
}
return
*
rand_generator_
.
get
();
}
private:
int
random_seed_
;
std
::
unique_ptr
<
random_generator_type
>
rand_generator_
;
std
::
unique_ptr
<
Eigen
::
DefaultDevice
>
eigen_device_
;
};
...
...
@@ -87,6 +98,24 @@ class CUDADeviceContext : public DeviceContext {
"cudaStreamSynchronize failed"
);
}
const
curandGenerator_t
RandGenerator
(
const
int
seed
)
{
if
(
!
rand_generator_
)
{
random_seed_
=
seed
;
GPUPlaceGuard
guard
(
gpu_place_
);
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
curandCreateGenerator
(
&
rand_generator_
,
CURAND_RNG_PSEUDO_DEFAULT
),
"curandCreateGenerator failed"
);
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
curandSetPseudoRandomGeneratorSeed
(
rand_generator_
,
random_seed_
),
"curandSetPseudoRandomGeneratorSeed failed"
);
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
curandSetStream
(
rand_generator_
,
stream_
),
"curandSetStream failed"
);
}
return
rand_generator_
;
}
cudaStream_t
stream
()
{
return
stream_
;
}
Eigen
::
GpuDevice
*
eigen_device
()
const
{
return
eigen_device_
.
get
();
}
...
...
@@ -115,23 +144,6 @@ class CUDADeviceContext : public DeviceContext {
return
dnn_handle_
;
}
curandGenerator_t
curand_generator
()
{
if
(
!
rand_generator_
)
{
GPUPlaceGuard
guard
(
gpu_place_
);
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
curandCreateGenerator
(
&
rand_generator_
,
CURAND_RNG_PSEUDO_DEFAULT
),
"curandCreateGenerator failed"
);
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
curandSetPseudoRandomGeneratorSeed
(
rand_generator_
,
random_seed_
),
"curandSetPseudoRandomGeneratorSeed failed"
);
PADDLE_ENFORCE
(
paddle
::
platform
::
dynload
::
curandSetStream
(
rand_generator_
,
stream_
),
"curandSetStream failed"
);
}
return
rand_generator_
;
}
~
CUDADeviceContext
()
{
Wait
();
if
(
blas_handle_
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录