Skip to content

Commit 6c88f1a

Browse files
committed
add save op float16 support
1 parent 6e0b47b commit 6c88f1a

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

paddle/fluid/operators/save_load_op_test.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,35 @@ TEST(SaveLoadOp, CPU) {
6161
}
6262
}
6363
}
64+
65+
TEST(SaveLoadFP16Op, CPU) {
66+
paddle::framework::Scope scope;
67+
paddle::platform::CPUPlace place;
68+
69+
auto var = scope.Var("test_var");
70+
auto tensor = var->GetMutable<paddle::framework::LoDTensor>();
71+
tensor->Resize({3, 10});
72+
73+
float* expect = tensor->mutable_data<float>(place);
74+
for (int64_t i = 0; i < tensor->numel(); ++i) {
75+
expect[i] = static_cast<float>(i);
76+
}
77+
78+
paddle::framework::AttributeMap attrs;
79+
attrs.insert({"file_path", std::string("tensor.save")});
80+
attrs.insert({"save_as_fp16", true});
81+
82+
auto save_op = paddle::framework::OpRegistry::CreateOp(
83+
"save", {{"X", {"test_var"}}}, {}, attrs);
84+
save_op->Run(scope, place);
85+
86+
auto load_var = scope.Var("out_var");
87+
auto target = load_var->GetMutable<paddle::framework::LoDTensor>();
88+
auto load_op = paddle::framework::OpRegistry::CreateOp(
89+
"load", {}, {{"Out", {"out_var"}}}, attrs);
90+
load_op->Run(scope, place);
91+
paddle::platform::float16* actual = target->data<paddle::platform::float16>();
92+
for (int64_t i = 0; i < tensor->numel(); ++i) {
93+
EXPECT_EQ(expect[i], static_cast<float>(actual[i]));
94+
}
95+
}

paddle/fluid/operators/save_op.cc

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License. */
1818
#include <numeric>
1919

2020
#include "paddle/fluid/framework/data_type.h"
21+
#include "paddle/fluid/framework/data_type_transform.h"
2122
#include "paddle/fluid/framework/framework.pb.h"
2223
#include "paddle/fluid/framework/lod_tensor.h"
2324
#include "paddle/fluid/framework/op_registry.h"
@@ -68,6 +69,7 @@ class SaveOp : public framework::OperatorBase {
6869
const platform::Place &place) const override {
6970
auto filename = Attr<std::string>("file_path");
7071
auto overwrite = Attr<bool>("overwrite");
72+
auto save_as_fp16 = Attr<bool>("save_as_fp16");
7173

7274
if (FileExists(filename) && !overwrite) {
7375
PADDLE_THROW("%s is existed, cannot save to it when overwrite=false",
@@ -96,7 +98,18 @@ class SaveOp : public framework::OperatorBase {
9698
platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
9799
auto &dev_ctx = *pool.Get(place);
98100

99-
framework::SerializeToStream(fout, tensor, dev_ctx);
101+
auto in_dtype = framework::ToDataType(tensor.type());
102+
auto out_dtype = save_as_fp16 ? framework::proto::VarType::FP16 : in_dtype;
103+
104+
if (in_dtype != out_dtype) {
105+
auto in_kernel_type = framework::OpKernelType(in_dtype, place);
106+
auto out_kernel_type = framework::OpKernelType(out_dtype, place);
107+
framework::LoDTensor out;
108+
framework::TransDataType(in_kernel_type, out_kernel_type, tensor, &out);
109+
framework::SerializeToStream(fout, out, dev_ctx);
110+
} else {
111+
framework::SerializeToStream(fout, tensor, dev_ctx);
112+
}
100113
}
101114
};
102115

@@ -114,6 +127,12 @@ This operator will serialize and write a tensor variable to file on disk.
114127
"(boolean, default true)"
115128
"Overwrite the output file if exist")
116129
.SetDefault(true);
130+
AddAttr<bool>("save_as_fp16",
131+
"(boolean, default false)"
132+
"If true, the tensor will be converted to float16 data "
133+
"type and then saved. Otherwise, the tensor will be "
134+
"directly saved without data type conversion.")
135+
.SetDefault(false);
117136
AddAttr<std::string>("file_path",
118137
"(string)"
119138
"The \"file_path\" where the variable will be saved.")

0 commit comments

Comments
 (0)