Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
0c333543
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
0c333543
编写于
3月 15, 2022
作者:
C
Chang Xu
提交者:
GitHub
3月 15, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix truncated norm operator (#40287)
上级
d7112180
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
49 addition
and
47 deletion
+49
-47
paddle/fluid/distributed/ps/table/depends/initializers.h
paddle/fluid/distributed/ps/table/depends/initializers.h
+7
-4
paddle/fluid/operators/truncated_gaussian_random_op.h
paddle/fluid/operators/truncated_gaussian_random_op.h
+2
-15
paddle/fluid/operators/truncated_gaussian_random_op_npu.cc
paddle/fluid/operators/truncated_gaussian_random_op_npu.cc
+7
-2
paddle/fluid/operators/truncated_gaussian_random_op_xpu.cc
paddle/fluid/operators/truncated_gaussian_random_op_xpu.cc
+7
-2
paddle/phi/kernels/cpu/truncated_gaussian_random_kernel.cc
paddle/phi/kernels/cpu/truncated_gaussian_random_kernel.cc
+7
-2
paddle/phi/kernels/gpu/truncated_gaussian_random_kernel.cu
paddle/phi/kernels/gpu/truncated_gaussian_random_kernel.cu
+17
-10
paddle/phi/kernels/truncated_gaussian_random_kernel.h
paddle/phi/kernels/truncated_gaussian_random_kernel.h
+2
-12
未找到文件。
paddle/fluid/distributed/ps/table/depends/initializers.h
浏览文件 @
0c333543
...
...
@@ -23,7 +23,6 @@
#include "gflags/gflags.h"
#include "paddle/fluid/framework/generator.h"
#include "paddle/fluid/operators/truncated_gaussian_random_op.h"
namespace
paddle
{
...
...
@@ -118,9 +117,13 @@ class TruncatedGaussianInitializer : public Initializer {
seed_
=
static_cast
<
unsigned
int
>
(
std
::
stoi
(
attrs
[
1
]));
mean_
=
std
::
stof
(
attrs
[
2
]);
std_
=
std
::
stof
(
attrs
[
3
]);
std
::
uniform_real_distribution
<
float
>
dist_
(
std
::
numeric_limits
<
float
>::
min
(),
1.0
);
auto
normal_cdf
=
[](
float
x
)
{
return
(
1.0
+
std
::
erf
(
x
/
std
::
sqrt
(
2.0
)))
/
2.0
;
};
float
a_normal_cdf
=
normal_cdf
((
-
2.0
-
mean_
)
/
std_
);
float
b_normal_cdf
=
normal_cdf
((
2.0
-
mean_
)
/
std_
);
std
::
uniform_real_distribution
<
float
>
dist_
(
2.0
*
a_normal_cdf
-
1.0
,
2.0
*
b_normal_cdf
-
1.0
);
random_engine_
=
framework
::
GetCPURandomEngine
(
seed_
);
}
...
...
paddle/fluid/operators/truncated_gaussian_random_op.h
浏览文件 @
0c333543
/* 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.
...
...
@@ -140,19 +137,9 @@ T Erfinv(T x) {
template
<
typename
T
>
struct
TruncatedNormal
{
T
mean
,
std
;
T
a_normal_cdf
;
T
b_normal_cdf
;
TruncatedNormal
(
T
mean
,
T
std
)
:
mean
(
mean
),
std
(
std
)
{
auto
normal_cdf
=
[](
T
x
)
{
return
(
1.0
+
std
::
erf
(
x
/
std
::
sqrt
(
2.0
)))
/
2.0
;
};
a_normal_cdf
=
normal_cdf
(
-
2.0
);
b_normal_cdf
=
normal_cdf
(
2.0
);
}
TruncatedNormal
(
T
mean
,
T
std
)
:
mean
(
mean
),
std
(
std
)
{}
T
operator
()(
T
value
)
const
{
auto
p
=
a_normal_cdf
+
(
b_normal_cdf
-
a_normal_cdf
)
*
value
;
return
std
::
sqrt
(
2.0
)
*
Erfinv
(
2
*
p
-
1
)
*
std
+
mean
;
return
std
::
sqrt
(
2.0
)
*
Erfinv
(
value
)
*
std
+
mean
;
}
};
...
...
paddle/fluid/operators/truncated_gaussian_random_op_npu.cc
浏览文件 @
0c333543
...
...
@@ -84,8 +84,13 @@ class NPUTruncatedGaussianRandomKernel : public framework::OpKernel<T> {
Tensor
cpu_tensor
(
tensor
->
dtype
());
cpu_tensor
.
Resize
(
tensor
->
dims
());
T
*
cpu_data
=
cpu_tensor
.
mutable_data
<
T
>
(
platform
::
CPUPlace
());
std
::
uniform_real_distribution
<
T
>
dist
(
std
::
numeric_limits
<
float
>::
min
(),
1.0
);
auto
normal_cdf
=
[](
float
x
)
{
return
(
1.0
+
std
::
erf
(
x
/
std
::
sqrt
(
2.0
)))
/
2.0
;
};
float
a_normal_cdf
=
normal_cdf
((
-
2.0
-
mean
)
/
std
);
float
b_normal_cdf
=
normal_cdf
((
2.0
-
mean
)
/
std
);
std
::
uniform_real_distribution
<
float
>
dist
(
2.0
*
a_normal_cdf
-
1.0
,
2.0
*
b_normal_cdf
-
1.0
);
TruncatedNormal
<
T
>
truncated_normal
(
mean
,
std
);
int64_t
size
=
tensor
->
numel
();
...
...
paddle/fluid/operators/truncated_gaussian_random_op_xpu.cc
浏览文件 @
0c333543
...
...
@@ -32,8 +32,13 @@ class XPUTruncatedGaussianRandomKernel : public framework::OpKernel<T> {
auto
*
tensor
=
context
.
Output
<
framework
::
Tensor
>
(
"Out"
);
T
*
data
=
tensor
->
mutable_data
<
T
>
(
context
.
GetPlace
());
std
::
uniform_real_distribution
<
T
>
dist
(
std
::
numeric_limits
<
float
>::
min
(),
1.0
);
auto
normal_cdf
=
[](
float
x
)
{
return
(
1.0
+
std
::
erf
(
x
/
std
::
sqrt
(
2.0
)))
/
2.0
;
};
float
a_normal_cdf
=
normal_cdf
((
-
2.0
-
mean
)
/
std
);
float
b_normal_cdf
=
normal_cdf
((
2.0
-
mean
)
/
std
);
std
::
uniform_real_distribution
<
float
>
dist
(
2.0
*
a_normal_cdf
-
1.0
,
2.0
*
b_normal_cdf
-
1.0
);
TruncatedNormal
<
T
>
truncated_normal
(
mean
,
std
);
int64_t
size
=
tensor
->
numel
();
...
...
paddle/phi/kernels/cpu/truncated_gaussian_random_kernel.cc
浏览文件 @
0c333543
...
...
@@ -37,8 +37,13 @@ void TruncatedGaussianRandomKernel(const Context& dev_ctx,
T
*
data
=
dev_ctx
.
template
Alloc
<
T
>(
tensor
);
std
::
uniform_real_distribution
<
T
>
dist
(
std
::
numeric_limits
<
float
>::
min
(),
1.0
);
auto
normal_cdf
=
[](
float
x
)
{
return
(
1.0
+
std
::
erf
(
x
/
std
::
sqrt
(
2.0
)))
/
2.0
;
};
float
a_normal_cdf
=
normal_cdf
((
-
2.0
-
mean
)
/
std
);
float
b_normal_cdf
=
normal_cdf
((
2.0
-
mean
)
/
std
);
std
::
uniform_real_distribution
<
float
>
dist
(
2.0
*
a_normal_cdf
-
1.0
,
2.0
*
b_normal_cdf
-
1.0
);
TruncatedNormal
<
T
>
truncated_normal
(
mean
,
std
);
int64_t
size
=
tensor
->
numel
();
...
...
paddle/phi/kernels/gpu/truncated_gaussian_random_kernel.cu
浏览文件 @
0c333543
...
...
@@ -33,23 +33,27 @@ struct GPUTruncatedNormal {
T
mean
,
std
;
T
a_normal_cdf
;
T
b_normal_cdf
;
unsigned
int
seed
;
T
numeric_min
;
__host__
__device__
GPUTruncatedNormal
(
T
mean
,
T
std
,
T
numeric_min
,
int
seed
)
:
mean
(
mean
),
std
(
std
),
seed
(
seed
),
numeric_min
(
numeric_min
)
{
a_normal_cdf
=
(
1.0
+
erff
(
-
2.0
/
sqrtf
(
2.0
)))
/
2.0
;
b_normal_cdf
=
(
1.0
+
erff
(
2.0
/
sqrtf
(
2.0
)))
/
2.0
;
auto
normal_cdf
=
[](
float
x
)
{
return
(
1.0
+
std
::
erf
(
x
/
std
::
sqrt
(
2.0
)))
/
2.0
;
};
a_normal_cdf
=
normal_cdf
((
-
2.0
-
mean
)
/
std
);
b_normal_cdf
=
normal_cdf
((
2.0
-
mean
)
/
std
);
}
__host__
__device__
T
operator
()(
const
unsigned
int
n
)
const
{
thrust
::
minstd_rand
rng
;
rng
.
seed
(
seed
);
thrust
::
uniform_real_distribution
<
T
>
dist
(
numeric_min
,
1
);
thrust
::
uniform_real_distribution
<
T
>
dist
(
2.0
*
a_normal_cdf
-
1.0
,
2.0
*
b_normal_cdf
-
1.0
);
rng
.
discard
(
n
);
T
value
=
dist
(
rng
);
auto
p
=
a_normal_cdf
+
(
b_normal_cdf
-
a_normal_cdf
)
*
value
;
return
std
::
sqrt
(
2.0
)
*
erfinvf
(
2
*
p
-
1
)
*
std
+
mean
;
return
std
::
sqrt
(
2.0
)
*
erfinvf
(
value
)
*
std
+
mean
;
}
};
...
...
@@ -69,18 +73,21 @@ struct TruncatedNormalOffset {
seed
(
seed
),
numeric_min
(
numeric_min
),
offset_
(
offset
)
{
a_normal_cdf
=
(
1.0
+
erff
(
-
2.0
/
sqrtf
(
2.0
)))
/
2.0
;
b_normal_cdf
=
(
1.0
+
erff
(
2.0
/
sqrtf
(
2.0
)))
/
2.0
;
auto
normal_cdf
=
[](
float
x
)
{
return
(
1.0
+
std
::
erf
(
x
/
std
::
sqrt
(
2.0
)))
/
2.0
;
};
a_normal_cdf
=
normal_cdf
((
-
2.0
-
mean
)
/
std
);
b_normal_cdf
=
normal_cdf
((
2.0
-
mean
)
/
std
);
}
__host__
__device__
T
operator
()(
const
unsigned
int
n
)
const
{
thrust
::
minstd_rand
rng
;
rng
.
seed
(
seed
);
thrust
::
uniform_real_distribution
<
T
>
dist
(
numeric_min
,
1
);
thrust
::
uniform_real_distribution
<
T
>
dist
(
2.0
*
a_normal_cdf
-
1.0
,
2.0
*
b_normal_cdf
-
1.0
);
rng
.
discard
(
n
+
offset_
);
T
value
=
dist
(
rng
);
auto
p
=
a_normal_cdf
+
(
b_normal_cdf
-
a_normal_cdf
)
*
value
;
return
std
::
sqrt
(
2.0
)
*
erfinvf
(
2
*
p
-
1
)
*
std
+
mean
;
return
std
::
sqrt
(
2.0
)
*
erfinvf
(
value
)
*
std
+
mean
;
}
};
...
...
paddle/phi/kernels/truncated_gaussian_random_kernel.h
浏览文件 @
0c333543
...
...
@@ -141,19 +141,9 @@ T Erfinv(T x) {
template
<
typename
T
>
struct
TruncatedNormal
{
T
mean
,
std
;
T
a_normal_cdf
;
T
b_normal_cdf
;
TruncatedNormal
(
T
mean
,
T
std
)
:
mean
(
mean
),
std
(
std
)
{
auto
normal_cdf
=
[](
T
x
)
{
return
(
1.0
+
std
::
erf
(
x
/
std
::
sqrt
(
2.0
)))
/
2.0
;
};
a_normal_cdf
=
normal_cdf
(
-
2.0
);
b_normal_cdf
=
normal_cdf
(
2.0
);
}
TruncatedNormal
(
T
mean
,
T
std
)
:
mean
(
mean
),
std
(
std
)
{}
T
operator
()(
T
value
)
const
{
auto
p
=
a_normal_cdf
+
(
b_normal_cdf
-
a_normal_cdf
)
*
value
;
return
std
::
sqrt
(
2.0
)
*
Erfinv
(
2
*
p
-
1
)
*
std
+
mean
;
return
std
::
sqrt
(
2.0
)
*
Erfinv
(
value
)
*
std
+
mean
;
}
};
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录