Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
6af0593c
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看板
未验证
提交
6af0593c
编写于
5月 15, 2018
作者:
S
Siddharth Goyal
提交者:
GitHub
5月 15, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add FP16 option to load_combine op (#10601)
上级
5f6fd26f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
113 addition
and
13 deletion
+113
-13
paddle/fluid/operators/load_combine_op.cc
paddle/fluid/operators/load_combine_op.cc
+26
-10
paddle/fluid/operators/save_load_combine_op_test.cc
paddle/fluid/operators/save_load_combine_op_test.cc
+87
-3
未找到文件。
paddle/fluid/operators/load_combine_op.cc
浏览文件 @
6af0593c
...
...
@@ -12,7 +12,7 @@ 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 <fstream>
#include "paddle/fluid/framework/data_type_transform.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/platform/device_context.h"
...
...
@@ -31,6 +31,7 @@ class LoadCombineOp : public framework::OperatorBase {
void
RunImpl
(
const
framework
::
Scope
&
scope
,
const
platform
::
Place
&
place
)
const
override
{
auto
filename
=
Attr
<
std
::
string
>
(
"file_path"
);
auto
load_as_fp16
=
Attr
<
bool
>
(
"load_as_fp16"
);
std
::
ifstream
fin
(
filename
);
PADDLE_ENFORCE
(
static_cast
<
bool
>
(
fin
),
...
...
@@ -59,17 +60,25 @@ class LoadCombineOp : public framework::OperatorBase {
// Get data from fin to tensor
DeserializeFromStream
(
fin
,
tensor
,
dev_ctx
);
if
(
platform
::
is_gpu_place
(
place
))
{
// copy CPU to GPU
framework
::
LoDTensor
cpu_tensor
;
cpu_tensor
.
ShareDataWith
(
*
tensor
);
cpu_tensor
.
set_lod
(
tensor
->
lod
());
// reset tensor
auto
in_dtype
=
framework
::
ToDataType
(
tensor
->
type
());
auto
out_dtype
=
load_as_fp16
?
framework
::
proto
::
VarType
::
FP16
:
in_dtype
;
if
(
in_dtype
!=
out_dtype
)
{
// convert to float16 tensor
auto
in_kernel_type
=
framework
::
OpKernelType
(
in_dtype
,
place
);
auto
out_kernel_type
=
framework
::
OpKernelType
(
out_dtype
,
place
);
framework
::
LoDTensor
fp16_tensor
;
// copy LoD info to the new tensor
fp16_tensor
.
set_lod
(
tensor
->
lod
());
framework
::
TransDataType
(
in_kernel_type
,
out_kernel_type
,
*
tensor
,
&
fp16_tensor
);
// reset output tensor
out_var
->
Clear
();
tensor
=
out_var
->
GetMutable
<
framework
::
LoDTensor
>
();
tensor
->
set_lod
(
cpu
_tensor
.
lod
());
TensorCopy
(
cpu_tensor
,
place
,
dev_ctx
,
tensor
);
tensor
->
set_lod
(
fp16
_tensor
.
lod
());
tensor
->
ShareDataWith
(
fp16_
tensor
);
}
}
}
...
...
@@ -82,6 +91,13 @@ class LoadCombineOpProtoMaker : public framework::OpProtoAndCheckerMaker {
"Out"
,
"(vector) The output LoDTensors that will be read from the input file."
)
.
AsDuplicable
();
AddAttr
<
bool
>
(
"load_as_fp16"
,
"(boolean, default false)"
"If true, the tensor will be first loaded and then "
"converted to float16 data type. Otherwise, the tensor will be "
"directly loaded without data type conversion."
)
.
SetDefault
(
false
);
AddAttr
<
std
::
string
>
(
"file_path"
,
"(string) "
"LoDTensors will be loaded from
\"
file_path
\"
."
)
...
...
paddle/fluid/operators/save_load_combine_op_test.cc
浏览文件 @
6af0593c
...
...
@@ -139,8 +139,9 @@ TEST(SaveLoadCombineOp, CPU) {
CheckValues
<
int
,
int
>
(
expect4
,
actual4
,
expect_lod4
,
actual_lod4
,
numel4
);
}
// FP16 version of SaveLoadCombineOp Test
TEST
(
SaveLoadCombineFP16Op
,
CPU
)
{
// FP16 version of SaveLoadCombineOp Test, only altering the saving aspect
// to save as FP16.
TEST
(
SaveCombineFP16Op
,
CPU
)
{
paddle
::
framework
::
Scope
scope
;
paddle
::
platform
::
CPUPlace
place
;
...
...
@@ -169,7 +170,7 @@ TEST(SaveLoadCombineFP16Op, CPU) {
20
,
50
,
lod4
,
"test_var4"
,
place
,
&
scope
,
&
expect_lod4
);
// Set attributes
std
::
string
filename
=
"check_tensor_fp16.ls"
;
std
::
string
filename
=
"check_tensor_fp16
_save
.ls"
;
paddle
::
framework
::
AttributeMap
attrs
;
attrs
.
insert
({
"file_path"
,
std
::
string
(
filename
)});
attrs
.
insert
({
"save_as_fp16"
,
true
});
...
...
@@ -216,6 +217,89 @@ TEST(SaveLoadCombineFP16Op, CPU) {
actual_lod4
,
numel4
);
}
// FP16 version of SaveLoadCombineOp Test, only altering the loading aspect
// to load tensors with FP16 precision.
TEST
(
LoadCombineFP16Op
,
CPU
)
{
paddle
::
framework
::
Scope
scope
;
paddle
::
platform
::
CPUPlace
place
;
std
::
vector
<
int
>
lod1
=
{
0
,
1
,
2
,
3
,
10
};
int
numel1
=
100
;
paddle
::
framework
::
LoD
expect_lod1
;
float
*
expect1
=
CreateForSaveCombineOp
<
float
,
paddle
::
platform
::
float16
>
(
10
,
10
,
lod1
,
"test_var1"
,
place
,
&
scope
,
&
expect_lod1
);
std
::
vector
<
int
>
lod2
=
{
0
,
2
,
5
,
10
};
int
numel2
=
200
;
paddle
::
framework
::
LoD
expect_lod2
;
float
*
expect2
=
CreateForSaveCombineOp
<
float
,
paddle
::
platform
::
float16
>
(
10
,
20
,
lod2
,
"test_var2"
,
place
,
&
scope
,
&
expect_lod2
);
std
::
vector
<
int
>
lod3
=
{
0
,
20
};
int
numel3
=
4000
;
paddle
::
framework
::
LoD
expect_lod3
;
float
*
expect3
=
CreateForSaveCombineOp
<
float
,
paddle
::
platform
::
float16
>
(
20
,
200
,
lod3
,
"test_var3"
,
place
,
&
scope
,
&
expect_lod3
);
std
::
vector
<
int
>
lod4
=
{
0
,
1
,
20
};
int
numel4
=
1000
;
paddle
::
framework
::
LoD
expect_lod4
;
float
*
expect4
=
CreateForSaveCombineOp
<
float
,
paddle
::
platform
::
float16
>
(
20
,
50
,
lod4
,
"test_var4"
,
place
,
&
scope
,
&
expect_lod4
);
// Set attributes
std
::
string
filename
=
"check_tensor_fp16_load.ls"
;
paddle
::
framework
::
AttributeMap
attrs
;
attrs
.
insert
({
"file_path"
,
std
::
string
(
filename
)});
// Run the save_combine_op
auto
save_combine_op
=
paddle
::
framework
::
OpRegistry
::
CreateOp
(
"save_combine"
,
{{
"X"
,
{
"test_var1"
,
"test_var2"
,
"test_var3"
,
"test_var4"
}}},
{},
attrs
);
save_combine_op
->
Run
(
scope
,
place
);
// Set up output vars
auto
load_var1
=
scope
.
Var
(
"out_var1"
);
auto
load_var2
=
scope
.
Var
(
"out_var2"
);
auto
load_var3
=
scope
.
Var
(
"out_var3"
);
auto
load_var4
=
scope
.
Var
(
"out_var4"
);
attrs
.
insert
({
"load_as_fp16"
,
true
});
// Run the load_combine_op
auto
load_combine_op
=
paddle
::
framework
::
OpRegistry
::
CreateOp
(
"load_combine"
,
{},
{{
"Out"
,
{
"out_var1"
,
"out_var2"
,
"out_var3"
,
"out_var4"
}}},
attrs
);
load_combine_op
->
Run
(
scope
,
place
);
auto
*
target1
=
load_var1
->
GetMutable
<
paddle
::
framework
::
LoDTensor
>
();
auto
*
target2
=
load_var2
->
GetMutable
<
paddle
::
framework
::
LoDTensor
>
();
auto
*
target3
=
load_var3
->
GetMutable
<
paddle
::
framework
::
LoDTensor
>
();
auto
*
target4
=
load_var4
->
GetMutable
<
paddle
::
framework
::
LoDTensor
>
();
paddle
::
framework
::
LoD
actual_lod1
,
actual_lod2
,
actual_lod3
,
actual_lod4
;
paddle
::
platform
::
float16
*
actual1
=
GetValuesAfterLoadCombineOp
<
paddle
::
platform
::
float16
>
(
target1
,
scope
,
&
actual_lod1
);
paddle
::
platform
::
float16
*
actual2
=
GetValuesAfterLoadCombineOp
<
paddle
::
platform
::
float16
>
(
target2
,
scope
,
&
actual_lod2
);
paddle
::
platform
::
float16
*
actual3
=
GetValuesAfterLoadCombineOp
<
paddle
::
platform
::
float16
>
(
target3
,
scope
,
&
actual_lod3
);
paddle
::
platform
::
float16
*
actual4
=
GetValuesAfterLoadCombineOp
<
paddle
::
platform
::
float16
>
(
target4
,
scope
,
&
actual_lod4
);
CheckValues
<
float
,
paddle
::
platform
::
float16
>
(
expect1
,
actual1
,
expect_lod1
,
actual_lod1
,
numel1
);
CheckValues
<
float
,
paddle
::
platform
::
float16
>
(
expect2
,
actual2
,
expect_lod2
,
actual_lod2
,
numel2
);
CheckValues
<
float
,
paddle
::
platform
::
float16
>
(
expect3
,
actual3
,
expect_lod3
,
actual_lod3
,
numel3
);
CheckValues
<
float
,
paddle
::
platform
::
float16
>
(
expect4
,
actual4
,
expect_lod4
,
actual_lod4
,
numel4
);
}
// Test with original SaveLoadTest
TEST
(
SaveLoadTestWithCombineOp
,
CPU
)
{
paddle
::
framework
::
Scope
scope
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录