@@ -17,15 +17,17 @@ limitations under the License. */
17
17
#include < vector>
18
18
#include " gtest/gtest.h"
19
19
#include " paddle/fluid/framework/op_registry.h"
20
+ #include " paddle/fluid/platform/float16.h"
20
21
21
22
USE_NO_KERNEL_OP (save_combine);
22
23
USE_NO_KERNEL_OP (load_combine);
23
24
24
- int * CreateForSaveCombineOp (int x, int y, const std::vector<int >& lod_info,
25
- std::string var_name,
26
- const paddle::platform::CPUPlace& place,
27
- paddle::framework::Scope* scope,
28
- paddle::framework::LoD* expect_lod) {
25
+ template <typename T, typename U>
26
+ T* CreateForSaveCombineOp (int x, int y, const std::vector<int >& lod_info,
27
+ std::string var_name,
28
+ const paddle::platform::CPUPlace& place,
29
+ paddle::framework::Scope* scope,
30
+ paddle::framework::LoD* expect_lod) {
29
31
auto var = scope->Var (var_name);
30
32
auto tensor = var->GetMutable <paddle::framework::LoDTensor>();
31
33
tensor->Resize ({x, y});
@@ -34,9 +36,10 @@ int* CreateForSaveCombineOp(int x, int y, const std::vector<int>& lod_info,
34
36
(*expect_lod)[0 ].push_back (lod_info[i]);
35
37
}
36
38
tensor->set_lod (*expect_lod);
37
- int * expect = tensor->mutable_data <int >(place);
39
+ T * expect = tensor->mutable_data <T >(place);
38
40
for (int64_t i = 0 ; i < tensor->numel (); ++i) {
39
- expect[i] = static_cast <int >(i);
41
+ expect[i] = static_cast <T>(
42
+ static_cast <U>(i)); // For FP16, we intend to do float(float16(i))
40
43
}
41
44
return expect;
42
45
}
@@ -48,18 +51,20 @@ paddle::framework::LoDTensor* GeneratePlaceholderBeforeLoad(
48
51
return target;
49
52
}
50
53
51
- int * GetValuesAfterLoadCombineOp (paddle::framework::LoDTensor* target,
52
- const paddle::framework::Scope& scope,
53
- paddle::framework::LoD* actual_lod) {
54
- int * actual = target->data <int >();
54
+ template <typename T>
55
+ T* GetValuesAfterLoadCombineOp (paddle::framework::LoDTensor* target,
56
+ const paddle::framework::Scope& scope,
57
+ paddle::framework::LoD* actual_lod) {
58
+ T* actual = target->data <T>();
55
59
*actual_lod = target->lod ();
56
60
return actual;
57
61
}
58
62
59
- void CheckValues (int * expect, int * actual, paddle::framework::LoD expect_lod,
60
- paddle::framework::LoD actual_lod, const int & numel) {
61
- for (int64_t i = 0 ; i < numel; ++i) {
62
- EXPECT_EQ (expect[i], actual[i]);
63
+ template <typename T, typename U>
64
+ void CheckValues (T* expect, U* actual, const paddle::framework::LoD& expect_lod,
65
+ const paddle::framework::LoD& actual_lod, const int & numel) {
66
+ for (int i = 0 ; i < numel; ++i) {
67
+ EXPECT_EQ (expect[i], static_cast <T>(actual[i]));
63
68
}
64
69
EXPECT_EQ (expect_lod.size (), actual_lod.size ());
65
70
for (size_t i = 0 ; i < expect_lod.size (); ++i) {
@@ -78,26 +83,26 @@ TEST(SaveLoadCombineOp, CPU) {
78
83
std::vector<int > lod1 = {0 , 1 , 2 , 3 , 10 };
79
84
int numel1 = 100 ;
80
85
paddle::framework::LoD expect_lod1;
81
- int * expect1 = CreateForSaveCombineOp (10 , 10 , lod1, " test_var1" , place ,
82
- &scope, &expect_lod1);
86
+ int * expect1 = CreateForSaveCombineOp< int , int > (10 , 10 , lod1, " test_var1" ,
87
+ place, &scope, &expect_lod1);
83
88
84
89
std::vector<int > lod2 = {0 , 2 , 5 , 10 };
85
90
int numel2 = 200 ;
86
91
paddle::framework::LoD expect_lod2;
87
- int * expect2 = CreateForSaveCombineOp (10 , 20 , lod2, " test_var2" , place ,
88
- &scope, &expect_lod2);
92
+ int * expect2 = CreateForSaveCombineOp< int , int > (10 , 20 , lod2, " test_var2" ,
93
+ place, &scope, &expect_lod2);
89
94
90
95
std::vector<int > lod3 = {0 , 2 , 3 , 20 };
91
96
int numel3 = 4000 ;
92
97
paddle::framework::LoD expect_lod3;
93
- int * expect3 = CreateForSaveCombineOp (20 , 200 , lod3, " test_var3" , place ,
94
- &scope, &expect_lod3);
98
+ int * expect3 = CreateForSaveCombineOp< int , int > (20 , 200 , lod3, " test_var3" ,
99
+ place, &scope, &expect_lod3);
95
100
96
101
std::vector<int > lod4 = {0 , 1 , 20 };
97
102
int numel4 = 1000 ;
98
103
paddle::framework::LoD expect_lod4;
99
- int * expect4 = CreateForSaveCombineOp (20 , 50 , lod4, " test_var4" , place ,
100
- &scope, &expect_lod4);
104
+ int * expect4 = CreateForSaveCombineOp< int , int > (20 , 50 , lod4, " test_var4" ,
105
+ place, &scope, &expect_lod4);
101
106
102
107
// Set attributes
103
108
std::string filename = " check_tensor.ls" ;
@@ -123,15 +128,92 @@ TEST(SaveLoadCombineOp, CPU) {
123
128
load_combine_op->Run (scope, place);
124
129
125
130
paddle::framework::LoD actual_lod1, actual_lod2, actual_lod3, actual_lod4;
126
- int * actual1 = GetValuesAfterLoadCombineOp (target1, scope, &actual_lod1);
127
- int * actual2 = GetValuesAfterLoadCombineOp (target2, scope, &actual_lod2);
128
- int * actual3 = GetValuesAfterLoadCombineOp (target3, scope, &actual_lod3);
129
- int * actual4 = GetValuesAfterLoadCombineOp (target4, scope, &actual_lod4);
130
-
131
- CheckValues (expect1, actual1, expect_lod1, actual_lod1, numel1);
132
- CheckValues (expect2, actual2, expect_lod2, actual_lod2, numel2);
133
- CheckValues (expect3, actual3, expect_lod3, actual_lod3, numel3);
134
- CheckValues (expect4, actual4, expect_lod4, actual_lod4, numel4);
131
+ int * actual1 = GetValuesAfterLoadCombineOp<int >(target1, scope, &actual_lod1);
132
+ int * actual2 = GetValuesAfterLoadCombineOp<int >(target2, scope, &actual_lod2);
133
+ int * actual3 = GetValuesAfterLoadCombineOp<int >(target3, scope, &actual_lod3);
134
+ int * actual4 = GetValuesAfterLoadCombineOp<int >(target4, scope, &actual_lod4);
135
+
136
+ CheckValues<int , int >(expect1, actual1, expect_lod1, actual_lod1, numel1);
137
+ CheckValues<int , int >(expect2, actual2, expect_lod2, actual_lod2, numel2);
138
+ CheckValues<int , int >(expect3, actual3, expect_lod3, actual_lod3, numel3);
139
+ CheckValues<int , int >(expect4, actual4, expect_lod4, actual_lod4, numel4);
140
+ }
141
+
142
+ // FP16 version of SaveLoadCombineOp Test
143
+ TEST (SaveLoadCombineFP16Op, CPU) {
144
+ paddle::framework::Scope scope;
145
+ paddle::platform::CPUPlace place;
146
+
147
+ std::vector<int > lod1 = {0 , 1 , 2 , 3 , 10 };
148
+ int numel1 = 100 ;
149
+ paddle::framework::LoD expect_lod1;
150
+ float * expect1 = CreateForSaveCombineOp<float , paddle::platform::float16>(
151
+ 10 , 10 , lod1, " test_var1" , place, &scope, &expect_lod1);
152
+
153
+ std::vector<int > lod2 = {0 , 2 , 5 , 10 };
154
+ int numel2 = 200 ;
155
+ paddle::framework::LoD expect_lod2;
156
+ float * expect2 = CreateForSaveCombineOp<float , paddle::platform::float16>(
157
+ 10 , 20 , lod2, " test_var2" , place, &scope, &expect_lod2);
158
+
159
+ std::vector<int > lod3 = {0 , 20 };
160
+ int numel3 = 4000 ;
161
+ paddle::framework::LoD expect_lod3;
162
+ float * expect3 = CreateForSaveCombineOp<float , paddle::platform::float16>(
163
+ 20 , 200 , lod3, " test_var3" , place, &scope, &expect_lod3);
164
+
165
+ std::vector<int > lod4 = {0 , 1 , 20 };
166
+ int numel4 = 1000 ;
167
+ paddle::framework::LoD expect_lod4;
168
+ float * expect4 = CreateForSaveCombineOp<float , paddle::platform::float16>(
169
+ 20 , 50 , lod4, " test_var4" , place, &scope, &expect_lod4);
170
+
171
+ // Set attributes
172
+ std::string filename = " check_tensor_fp16.ls" ;
173
+ paddle::framework::AttributeMap attrs;
174
+ attrs.insert ({" file_path" , std::string (filename)});
175
+ attrs.insert ({" save_as_fp16" , true });
176
+
177
+ // Run the save_combine_op
178
+ auto save_combine_op = paddle::framework::OpRegistry::CreateOp (
179
+ " save_combine" ,
180
+ {{" X" , {" test_var1" , " test_var2" , " test_var3" , " test_var4" }}}, {}, attrs);
181
+ save_combine_op->Run (scope, place);
182
+
183
+ // Set up output vars
184
+ auto target1 = GeneratePlaceholderBeforeLoad (" out_var1" , &scope);
185
+ auto target2 = GeneratePlaceholderBeforeLoad (" out_var2" , &scope);
186
+ auto target3 = GeneratePlaceholderBeforeLoad (" out_var3" , &scope);
187
+ auto target4 = GeneratePlaceholderBeforeLoad (" out_var4" , &scope);
188
+
189
+ // Run the load_combine_op
190
+ auto load_combine_op = paddle::framework::OpRegistry::CreateOp (
191
+ " load_combine" , {},
192
+ {{" Out" , {" out_var1" , " out_var2" , " out_var3" , " out_var4" }}}, attrs);
193
+ load_combine_op->Run (scope, place);
194
+
195
+ paddle::framework::LoD actual_lod1, actual_lod2, actual_lod3, actual_lod4;
196
+ paddle::platform::float16* actual1 =
197
+ GetValuesAfterLoadCombineOp<paddle::platform::float16>(target1, scope,
198
+ &actual_lod1);
199
+ paddle::platform::float16* actual2 =
200
+ GetValuesAfterLoadCombineOp<paddle::platform::float16>(target2, scope,
201
+ &actual_lod2);
202
+ paddle::platform::float16* actual3 =
203
+ GetValuesAfterLoadCombineOp<paddle::platform::float16>(target3, scope,
204
+ &actual_lod3);
205
+ paddle::platform::float16* actual4 =
206
+ GetValuesAfterLoadCombineOp<paddle::platform::float16>(target4, scope,
207
+ &actual_lod4);
208
+
209
+ CheckValues<float , paddle::platform::float16>(expect1, actual1, expect_lod1,
210
+ actual_lod1, numel1);
211
+ CheckValues<float , paddle::platform::float16>(expect2, actual2, expect_lod2,
212
+ actual_lod2, numel2);
213
+ CheckValues<float , paddle::platform::float16>(expect3, actual3, expect_lod3,
214
+ actual_lod3, numel3);
215
+ CheckValues<float , paddle::platform::float16>(expect4, actual4, expect_lod4,
216
+ actual_lod4, numel4);
135
217
}
136
218
137
219
// Test with original SaveLoadTest
@@ -141,7 +223,7 @@ TEST(SaveLoadTestWithCombineOp, CPU) {
141
223
142
224
auto var = scope.Var (" test_var" );
143
225
auto tensor = var->GetMutable <paddle::framework::LoDTensor>();
144
- tensor->Resize ({3 , 10 });
226
+ tensor->Resize ({3 , 4000 });
145
227
paddle::framework::LoD expect_lod;
146
228
expect_lod.resize (1 );
147
229
expect_lod[0 ].push_back (0 );
0 commit comments