Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
4c9c5501
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看板
提交
4c9c5501
编写于
9月 16, 2020
作者:
Z
Zhen Wang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
use fp32 to initilize parameter values.
上级
8d74782e
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
21 addition
and
68 deletion
+21
-68
paddle/fluid/operators/gaussian_random_op.cu
paddle/fluid/operators/gaussian_random_op.cu
+6
-28
paddle/fluid/operators/uniform_random_op.cu
paddle/fluid/operators/uniform_random_op.cu
+6
-10
paddle/fluid/platform/float16.h
paddle/fluid/platform/float16.h
+0
-4
python/paddle/fluid/contrib/mixed_precision/fp16_utils.py
python/paddle/fluid/contrib/mixed_precision/fp16_utils.py
+9
-26
未找到文件。
paddle/fluid/operators/gaussian_random_op.cu
浏览文件 @
4c9c5501
...
@@ -11,31 +11,16 @@ distributed under the License is distributed on an "AS IS" BASIS,
...
@@ -11,31 +11,16 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
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 <thrust/random.h>
#include <thrust/random.h>
#include <thrust/transform.h>
#include <thrust/transform.h>
#include <type_traits>
#include "paddle/fluid/framework/generator.h"
#include "paddle/fluid/framework/generator.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/operators/fill_constant_op.h"
#include "paddle/fluid/operators/fill_constant_op.h"
#include "paddle/fluid/platform/float16.h"
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
namespace
details
{
template
<
typename
T
>
struct
RandomDistributionType
{
using
Type
=
T
;
};
template
<
>
struct
RandomDistributionType
<
platform
::
float16
>
{
using
Type
=
float
;
};
}
// namespace details
template
<
typename
T
>
template
<
typename
T
>
struct
GaussianGenerator
{
struct
GaussianGenerator
{
T
mean_
,
std_
;
T
mean_
,
std_
;
...
@@ -49,16 +34,12 @@ struct GaussianGenerator {
...
@@ -49,16 +34,12 @@ struct GaussianGenerator {
:
mean_
(
mean
),
std_
(
std
),
seed_
(
seed
),
offset_
(
offset
)
{}
:
mean_
(
mean
),
std_
(
std
),
seed_
(
seed
),
offset_
(
offset
)
{}
__host__
__device__
T
operator
()(
const
unsigned
int
n
)
const
{
__host__
__device__
T
operator
()(
const
unsigned
int
n
)
const
{
using
DataType
=
typename
details
::
RandomDistributionType
<
T
>::
Type
;
thrust
::
minstd_rand
rng
;
thrust
::
minstd_rand
rng
;
rng
.
seed
(
seed_
);
rng
.
seed
(
seed_
);
thrust
::
normal_distribution
<
DataType
>
dist
(
static_cast
<
DataType
>
(
mean_
),
thrust
::
normal_distribution
<
T
>
dist
(
mean_
,
std_
);
static_cast
<
DataType
>
(
std_
));
unsigned
int
new_n
=
n
+
offset_
;
unsigned
int
new_n
=
n
+
offset_
;
rng
.
discard
(
new_n
);
rng
.
discard
(
new_n
);
T
out
=
static_cast
<
T
>
(
dist
(
rng
));
return
dist
(
rng
);
return
out
;
}
}
};
};
...
@@ -141,13 +122,10 @@ class GPUGaussianRandomBatchSizeLikeKernel : public framework::OpKernel<T> {
...
@@ -141,13 +122,10 @@ class GPUGaussianRandomBatchSizeLikeKernel : public framework::OpKernel<T> {
}
// namespace operators
}
// namespace operators
}
// namespace paddle
}
// namespace paddle
REGISTER_OP_CUDA_KERNEL
(
REGISTER_OP_CUDA_KERNEL
(
gaussian_random
,
gaussian_random
,
paddle
::
operators
::
GPUGaussianRandomKernel
<
float
>
,
paddle
::
operators
::
GPUGaussianRandomKernel
<
float
>
,
paddle
::
operators
::
GPUGaussianRandomKernel
<
double
>
,
paddle
::
operators
::
GPUGaussianRandomKernel
<
double
>
);
paddle
::
operators
::
GPUGaussianRandomKernel
<
paddle
::
platform
::
float16
>
);
REGISTER_OP_CUDA_KERNEL
(
REGISTER_OP_CUDA_KERNEL
(
gaussian_random_batch_size_like
,
gaussian_random_batch_size_like
,
paddle
::
operators
::
GPUGaussianRandomBatchSizeLikeKernel
<
float
>
,
paddle
::
operators
::
GPUGaussianRandomBatchSizeLikeKernel
<
float
>
,
paddle
::
operators
::
GPUGaussianRandomBatchSizeLikeKernel
<
double
>
,
paddle
::
operators
::
GPUGaussianRandomBatchSizeLikeKernel
<
double
>
);
paddle
::
operators
::
GPUGaussianRandomBatchSizeLikeKernel
<
paddle
::
platform
::
float16
>
);
paddle/fluid/operators/uniform_random_op.cu
浏览文件 @
4c9c5501
...
@@ -18,7 +18,6 @@ limitations under the License. */
...
@@ -18,7 +18,6 @@ limitations under the License. */
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/operators/uniform_random_op.h"
#include "paddle/fluid/operators/uniform_random_op.h"
#include "paddle/fluid/platform/float16.h"
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
...
@@ -164,12 +163,9 @@ class GPUUniformRandomKernel : public framework::OpKernel<T> {
...
@@ -164,12 +163,9 @@ class GPUUniformRandomKernel : public framework::OpKernel<T> {
}
// namespace operators
}
// namespace operators
}
// namespace paddle
}
// namespace paddle
REGISTER_OP_CUDA_KERNEL
(
REGISTER_OP_CUDA_KERNEL
(
uniform_random
,
uniform_random
,
paddle
::
operators
::
GPUUniformRandomKernel
<
float
>
,
paddle
::
operators
::
GPUUniformRandomKernel
<
double
>
,
paddle
::
operators
::
GPUUniformRandomKernel
<
paddle
::
platform
::
float16
>
);
REGISTER_OP_CUDA_KERNEL
(
uniform_random_batch_size_like
,
paddle
::
operators
::
GPUUniformRandomKernel
<
float
>
,
paddle
::
operators
::
GPUUniformRandomKernel
<
float
>
,
paddle
::
operators
::
GPUUniformRandomKernel
<
double
>
,
paddle
::
operators
::
GPUUniformRandomKernel
<
double
>
);
paddle
::
operators
::
GPUUniformRandomKernel
<
paddle
::
platform
::
float16
>
);
REGISTER_OP_CUDA_KERNEL
(
uniform_random_batch_size_like
,
paddle
::
operators
::
GPUUniformRandomKernel
<
float
>
,
paddle
::
operators
::
GPUUniformRandomKernel
<
double
>
);
paddle/fluid/platform/float16.h
浏览文件 @
4c9c5501
...
@@ -131,10 +131,6 @@ struct PADDLE_ALIGN(2) float16 {
...
@@ -131,10 +131,6 @@ struct PADDLE_ALIGN(2) float16 {
#endif
#endif
}
}
HOSTDEVICE
inline
float16
(
int32_t
val
)
:
float16
(
static_cast
<
float
>
(
val
))
{}
HOSTDEVICE
inline
float16
(
uint32_t
val
)
:
float16
(
static_cast
<
float
>
(
val
))
{}
HOSTDEVICE
inline
explicit
float16
(
bool
b
)
:
x
(
b
?
0x3c00
:
0
)
{}
HOSTDEVICE
inline
explicit
float16
(
bool
b
)
:
x
(
b
?
0x3c00
:
0
)
{}
template
<
class
T
>
template
<
class
T
>
...
...
python/paddle/fluid/contrib/mixed_precision/fp16_utils.py
浏览文件 @
4c9c5501
...
@@ -267,35 +267,18 @@ def cast_net_to_fp16(program):
...
@@ -267,35 +267,18 @@ def cast_net_to_fp16(program):
op
.
_set_attr
(
'dtype'
,
core
.
VarDesc
.
VarType
.
FP16
)
op
.
_set_attr
(
'dtype'
,
core
.
VarDesc
.
VarType
.
FP16
)
def
cast_parameters_to_fp16
(
program
):
def
cast_parameters_to_fp16
(
exe
,
program
,
scope
=
None
):
exe_scope
=
scope
if
scope
is
not
None
else
global_scope
()
global_block
=
program
.
global_block
()
global_block
=
program
.
global_block
()
all_parameters
=
global_block
.
all_parameters
()
all_parameters
=
global_block
.
all_parameters
()
is_bn_params
=
lambda
param
:
(
param
.
name
.
find
(
'bn'
)
!=
-
1
and
(
param
.
name
.
endswith
(
'_offset'
)
or
param
.
name
.
endswith
(
'_mean'
)
or
param
.
name
.
endswith
(
'_scale'
)
or
param
.
name
.
endswith
(
'_variance'
)))
all_param_names
=
{
p
.
name
for
p
in
all_parameters
if
not
is_bn_params
(
p
)}
ops
=
global_block
.
ops
for
param
in
all_parameters
:
for
param
in
all_parameters
:
if
param
.
name
in
all_param_names
:
if
not
(
param
.
name
.
find
(
'bn'
)
!=
-
1
and
param_var
=
global_block
.
var
(
param
.
name
)
(
param
.
name
.
endswith
(
'_offset'
)
or
param
.
name
.
endswith
(
'_mean'
)
if
param_var
.
dtype
==
core
.
VarDesc
.
VarType
.
FP32
:
or
param
.
name
.
endswith
(
'_scale'
)
or
param_var
.
desc
.
set_dtype
(
core
.
VarDesc
.
VarType
.
FP16
)
param
.
name
.
endswith
(
'_variance'
))):
param_t
=
exe_scope
.
find_var
(
param
.
name
).
get_tensor
()
for
op
in
ops
:
data
=
np
.
array
(
param_t
)
target_op
=
False
param_t
.
set
(
np
.
float16
(
data
),
exe
.
place
)
for
out_name
in
op
.
output_names
:
for
out_var_name
in
op
.
output
(
out_name
):
if
out_var_name
in
all_param_names
:
target_op
=
True
if
target_op
:
if
op
.
has_attr
(
'in_dtype'
)
and
op
.
attr
(
'in_dtype'
)
==
core
.
VarDesc
.
VarType
.
FP32
:
op
.
_set_attr
(
'in_dtype'
,
core
.
VarDesc
.
VarType
.
FP16
)
if
op
.
has_attr
(
'out_dtype'
)
and
op
.
attr
(
'out_dtype'
)
==
core
.
VarDesc
.
VarType
.
FP32
:
op
.
_set_attr
(
'out_dtype'
,
core
.
VarDesc
.
VarType
.
FP16
)
if
op
.
has_attr
(
'dtype'
)
and
op
.
attr
(
'dtype'
)
==
core
.
VarDesc
.
VarType
.
FP32
:
op
.
_set_attr
(
'dtype'
,
core
.
VarDesc
.
VarType
.
FP16
)
def
rewrite_program
(
main_prog
,
amp_lists
):
def
rewrite_program
(
main_prog
,
amp_lists
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录