Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
e2c08d28
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
e2c08d28
编写于
8月 08, 2017
作者:
D
dongzhihong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
"keep style same with uniform operators"
上级
fcd6f64b
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
60 addition
and
69 deletion
+60
-69
paddle/operators/gaussian_random_op.cc
paddle/operators/gaussian_random_op.cc
+34
-35
paddle/operators/gaussian_random_op.cu
paddle/operators/gaussian_random_op.cu
+26
-17
paddle/operators/gaussian_random_op.h
paddle/operators/gaussian_random_op.h
+0
-17
未找到文件。
paddle/operators/gaussian_random_op.cc
浏览文件 @
e2c08d28
...
...
@@ -12,42 +12,42 @@
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/gaussian_random_op.h"
#include "glog/logging.h"
#include <random>
#include "paddle/framework/op_registry.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
T
>
class
GaussianRandomOpKernel
<
platform
::
CPUPlace
,
T
>
:
public
framework
::
OpKernel
{
class
GaussianRandomKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
mean
=
context
.
op_
.
GetAttr
<
T
>
(
"mean"
);
auto
std
=
context
.
op_
.
GetAttr
<
T
>
(
"std"
);
auto
*
output
=
context
.
Output
(
0
)
->
GetMutable
<
framework
::
Tensor
>
();
T
*
r
=
output
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
ctx
=
static_cast
<
const
platform
::
CPUDeviceContext
*>
(
context
.
device_context_
);
// generator need to modify context
auto
g
=
const_cast
<
platform
::
CPUDeviceContext
*>
(
ctx
)
->
RandGenerator
();
T
mean
=
static_cast
<
T
>
(
context
.
op_
.
GetAttr
<
T
>
(
"mean"
));
T
std
=
static_cast
<
T
>
(
context
.
op_
.
GetAttr
<
T
>
(
"std"
));
auto
*
tensor
=
context
.
Output
<
framework
::
Tensor
>
(
0
);
T
*
data
=
tensor
->
mutable_data
<
T
>
(
context
.
GetPlace
());
// TODO(dzh): attribute does not support unsigned int.
// And we need a global random seed configuration.
int
seed
=
context
.
op_
.
GetAttr
<
int
>
(
"seed"
);
if
(
seed
==
0
)
{
seed
=
std
::
random_device
()();
}
std
::
mt19937
g
(
seed
);
std
::
normal_distribution
<
T
>
distribution
(
mean
,
std
);
for
(
int
i
=
0
;
i
<
framework
::
product
(
output
->
dims
());
++
i
)
{
r
[
i
]
=
distribution
(
g
);
for
(
int
i
=
0
;
i
<
framework
::
product
(
tensor
->
dims
());
++
i
)
{
data
[
i
]
=
distribution
(
g
);
}
}
};
class
GaussianRandomOp
:
public
framework
::
OperatorWithKernel
{
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
PADDLE_ENFORCE
(
inputs
.
size
()
==
0
,
"Input size of RandomOp must be zero."
);
PADDLE_ENFORCE
(
outputs
.
size
()
==
1
,
"Output size of RandomOp must be one."
);
PADDLE_ENFORCE
(
outputs
[
0
]
!=
nullptr
,
"Outputs of RandomOp must all be set."
);
auto
*
tensor
=
ctx
.
Output
<
Tensor
>
(
0
);
auto
dims
=
GetAttr
(
std
::
vector
<
int
>
(
"shape"
));
void
InferShape
(
const
framework
::
InferShapeContext
&
context
)
const
override
{
auto
*
tensor
=
context
.
Output
<
framework
::
Tensor
>
(
0
);
auto
dims
=
GetAttr
<
std
::
vector
<
int
>>
(
"dims"
);
PADDLE_ENFORCE
(
dims
.
size
()
>
0UL
,
"dims can be one int or array. dims must be set."
);
tensor
->
Resize
(
framework
::
make_ddim
(
dims
));
}
};
...
...
@@ -57,26 +57,25 @@ class GaussianRandomOpMaker : public framework::OpProtoAndCheckerMaker {
GaussianRandomOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
framework
::
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddAttr
<
std
::
vector
<
int
>>
(
"shape"
,
"The shape of matrix to be randomized"
);
AddAttr
<
float
>
(
"mean"
,
"mean value of random."
).
SetDefault
(
.0
);
AddAttr
<
float
>
(
"std"
,
"minimum value of random value"
)
.
SetDefault
(
1.0
)
.
LargerThan
(
.0
);
AddOutput
(
"Out"
,
"output matrix of random op"
);
AddComment
(
R"DOC(
GaussianRandom
Operator fill a matrix in normal distribution
.
The eqution : Out = GaussianRandom(Shape=(d0, d1, ...), Dtype, mean, std)
GaussianRandom
operator
.
Use to initialize tensor with gaussian random generator.
)DOC"
);
AddAttr
<
std
::
vector
<
int
>>
(
"dims"
,
"The dimension of random tensor."
);
AddAttr
<
float
>
(
"mean"
,
"mean value of random."
).
SetDefault
(
.0
f
);
AddAttr
<
float
>
(
"std"
,
"minimum value of random value."
).
SetDefault
(
1.0
f
);
AddAttr
<
int
>
(
"seed"
,
"Random seed of generator."
"0 means use system wide seed"
)
.
SetDefault
(
0
);
}
};
}
// namespace operators
}
// namespace paddle
REGISTER_OP
(
gaussian_random
,
paddle
::
operators
::
GaussianRandomOp
,
paddle
::
operators
::
GaussianRandomOpMaker
);
typedef
paddle
::
operators
::
GaussianRandomOpKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
GaussianRandomOpKernel_CPU_float
;
REGISTER_OP_CPU_KERNEL
(
gaussian_random
,
GaussianRandomOpKernel_CPU_float
);
namespace
ops
=
paddle
::
operators
;
REGISTER_OP
(
gaussian_random
,
ops
::
GaussianRandomOp
,
ops
::
GaussianRandomOpMaker
);
REGISTER_OP_CPU_KERNEL
(
gaussian_random
,
ops
::
GaussianRandomKernel
<
float
>
);
paddle/operators/gaussian_random_op.cu
浏览文件 @
e2c08d28
#include <memory>
#include <random>
#include "paddle/platform/dynload/curand.h"
#include "paddle/platform/gpu_info.h"
#include "paddle/framework/op_registry.h"
#include "paddle/operators/guassian_random_op.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
T
>
class
GaussianRandomOpKernel
<
platform
::
GPUPlace
,
T
>
:
public
framework
::
OpKernel
{
class
GaussianRandomKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
KernelContext
&
context
)
const
override
{
auto
mean
=
context
.
op_
.
GetAttr
<
T
>
(
"mean"
);
auto
std
=
context
.
op_
.
GetAttr
<
T
>
(
"std"
);
auto
*
output
=
context
.
Output
(
0
)
->
GetMutable
<
framework
::
Tensor
>
();
T
*
r
=
output
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
ctx
=
static_cast
<
const
platform
::
GPUDeviceContext
*>
(
context
.
device_context_
);
// generator need to modify context
auto
g
=
const_cast
<
platform
::
GPUDeviceContext
*>
(
ctx
)
->
RandGenerator
();
curandGenerateNormal
(
g
,
r
,
framework
::
product
(
output
->
dims
()),
mean
,
std
);
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
T
mean
=
static_cast
<
T
>
(
context
.
op_
.
GetAttr
<
T
>
(
"mean"
));
T
std
=
static_cast
<
T
>
(
context
.
op_
.
GetAttr
<
T
>
(
"std"
));
auto
*
tensor
=
context
.
Output
<
framework
::
Tensor
>
(
0
);
T
*
data
=
tensor
->
mutable_data
<
T
>
(
context
.
GetPlace
());
int
seed
=
context
.
op_
.
GetAttr
<
int
>
(
"seed"
);
if
(
seed
==
0
)
{
seed
=
std
::
random_device
()();
}
curandGenerator_t
g
;
PADDLE_ENFORCE
(
platform
::
dynload
::
curandCreateGenerator
(
&
g
,
CURAND_RNG_PSEUDO_DEFAULT
));
PADDLE_ENFORCE
(
platform
::
dynload
::
curandSetPseudoRandomGeneratorSeed
(
g
,
seed
));
// auto g = const_cast<platform::GPUDeviceContext*>(ctx)->RandGenerator();
curandGenerateNormal
(
g
,
data
,
framework
::
product
(
tensor
->
dims
()),
mean
,
std
);
}
};
}
// namespace operators
}
// namespace paddle
typedef
paddle
::
operators
::
GaussianRandomOpKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
RandomOpKernel_GPU_float
;
REGISTER_OP_GPU_KERNEL
(
gaussian_random
,
GaussianRandomOpKernel_GPU_float
);
\ No newline at end of file
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_GPU_KERNEL
(
gaussian_random
,
ops
::
GaussianRandomKernel
<
float
>
);
\ No newline at end of file
paddle/operators/gaussian_random_op.h
已删除
100644 → 0
浏览文件 @
fcd6f64b
#pragma once
#include <random>
#include "glog/logging.h"
#include "paddle/framework/eigen.h"
#include "paddle/framework/operator.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
Place
,
typename
T
>
class
GaussianRandomOpKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
KernelContext
&
context
)
const
override
{}
};
}
// namespace operators
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录