@@ -13,41 +13,50 @@ See the License for the specific language governing permissions and
13
13
limitations under the License. */
14
14
15
15
#include " paddle/fluid/operators/math/selected_rows_functor.h"
16
+ #include < vector>
16
17
#include " gtest/gtest.h"
17
18
#include " paddle/fluid/operators/math/math_function.h"
18
19
19
20
TEST (selected_rows_functor, cpu_add) {
20
- using namespace paddle ::framework;
21
- using namespace paddle ::platform;
22
- using namespace paddle ::operators::math;
23
-
24
- CPUPlace cpu_place;
25
- CPUDeviceContext ctx (cpu_place);
26
- SetConstant<CPUDeviceContext, float > functor;
21
+ paddle::platform::CPUPlace cpu_place;
22
+ paddle::platform::CPUDeviceContext ctx (cpu_place);
23
+ paddle::operators::math::SetConstant<paddle::platform::CPUDeviceContext,
24
+ float >
25
+ functor;
27
26
int64_t height = 10 ;
28
27
int64_t row_numel = 10 ;
29
28
30
29
std::vector<int64_t > rows1{0 , 4 , 7 };
31
- std::unique_ptr<SelectedRows> selected_rows1{new SelectedRows (rows1, height)};
30
+ std::unique_ptr<paddle::framework::SelectedRows> selected_rows1{
31
+ new paddle::framework::SelectedRows (rows1, height)};
32
32
auto * in1_value = selected_rows1->mutable_value ();
33
33
in1_value->mutable_data <float >(
34
- make_ddim ({static_cast <int64_t >(rows1.size ()), row_numel}), cpu_place);
34
+ paddle::framework::make_ddim (
35
+ {static_cast <int64_t >(rows1.size ()), row_numel}),
36
+ cpu_place);
35
37
functor (ctx, in1_value, 1.0 );
36
38
37
39
std::vector<int64_t > rows2{0 , 5 , 7 , 9 };
38
- std::unique_ptr<SelectedRows> selected_rows2{new SelectedRows (rows2, height)};
40
+ std::unique_ptr<paddle::framework::SelectedRows> selected_rows2{
41
+ new paddle::framework::SelectedRows (rows2, height)};
39
42
auto * in2_value = selected_rows2->mutable_value ();
40
43
in2_value->mutable_data <float >(
41
- make_ddim ({static_cast <int64_t >(rows2.size ()), row_numel}), cpu_place);
44
+ paddle::framework::make_ddim (
45
+ {static_cast <int64_t >(rows2.size ()), row_numel}),
46
+ cpu_place);
42
47
functor (ctx, in2_value, 2.0 );
43
48
44
- std::unique_ptr<SelectedRows> output{new SelectedRows ()};
49
+ std::unique_ptr<paddle::framework::SelectedRows> output{
50
+ new paddle::framework::SelectedRows ()};
45
51
auto * out_value = output->mutable_value ();
46
52
47
53
// simplely concat two SelectedRows
48
- out_value->mutable_data <float >(make_ddim ({7 , 10 }), cpu_place);
54
+ out_value->mutable_data <float >(paddle::framework::make_ddim ({7 , 10 }),
55
+ cpu_place);
49
56
50
- SelectedRowsAdd<CPUDeviceContext, float > add_functor;
57
+ paddle::operators::math::SelectedRowsAdd<paddle::platform::CPUDeviceContext,
58
+ float >
59
+ add_functor;
51
60
add_functor (ctx, *selected_rows1, *selected_rows2, output.get ());
52
61
53
62
auto out_height = output->height ();
@@ -78,14 +87,20 @@ TEST(selected_rows_functor, cpu_add) {
78
87
EXPECT_EQ (out_data[5 * row_numel + 7 ], 2.0 );
79
88
EXPECT_EQ (out_data[6 * row_numel + 9 ], 2.0 );
80
89
81
- std::unique_ptr<Tensor> tensor1{new Tensor ()};
82
- tensor1->mutable_data <float >(make_ddim ({height, row_numel}), cpu_place);
90
+ std::unique_ptr<paddle::framework::Tensor> tensor1{
91
+ new paddle::framework::Tensor ()};
92
+ tensor1->mutable_data <float >(
93
+ paddle::framework::make_ddim ({height, row_numel}), cpu_place);
83
94
functor (ctx, tensor1.get (), 3.0 );
84
95
85
- std::unique_ptr<Tensor> tensor2{new Tensor ()};
86
- tensor2->mutable_data <float >(make_ddim ({height, row_numel}), cpu_place);
96
+ std::unique_ptr<paddle::framework::Tensor> tensor2{
97
+ new paddle::framework::Tensor ()};
98
+ tensor2->mutable_data <float >(
99
+ paddle::framework::make_ddim ({height, row_numel}), cpu_place);
87
100
88
- SelectedRowsAddTensor<CPUDeviceContext, float > add_tensor_functor;
101
+ paddle::operators::math::SelectedRowsAddTensor<
102
+ paddle::platform::CPUDeviceContext, float >
103
+ add_tensor_functor;
89
104
add_tensor_functor (ctx, *output, *tensor1, tensor2.get ());
90
105
91
106
auto * tensor2_data = tensor2->data <float >();
@@ -106,38 +121,46 @@ TEST(selected_rows_functor, cpu_add) {
106
121
}
107
122
108
123
TEST (selected_rows_functor, cpu_add_to) {
109
- using namespace paddle ::framework;
110
- using namespace paddle ::platform;
111
- using namespace paddle ::operators::math;
112
-
113
- CPUPlace cpu_place;
114
- CPUDeviceContext ctx (cpu_place);
115
- SetConstant<CPUDeviceContext, float > functor;
124
+ paddle::platform::CPUPlace cpu_place;
125
+ paddle::platform::CPUDeviceContext ctx (cpu_place);
126
+ paddle::operators::math::SetConstant<paddle::platform::CPUDeviceContext,
127
+ float >
128
+ functor;
116
129
int64_t height = 10 ;
117
130
int64_t row_numel = 10 ;
118
131
119
132
std::vector<int64_t > rows1{0 , 4 , 7 };
120
- std::unique_ptr<SelectedRows> selected_rows1{new SelectedRows (rows1, height)};
133
+ std::unique_ptr<paddle::framework::SelectedRows> selected_rows1{
134
+ new paddle::framework::SelectedRows (rows1, height)};
121
135
auto * in1_value = selected_rows1->mutable_value ();
122
136
in1_value->mutable_data <float >(
123
- make_ddim ({static_cast <int64_t >(rows1.size ()), row_numel}), cpu_place);
137
+ paddle::framework::make_ddim (
138
+ {static_cast <int64_t >(rows1.size ()), row_numel}),
139
+ cpu_place);
124
140
functor (ctx, in1_value, 1.0 );
125
141
126
142
std::vector<int64_t > rows2{0 , 5 , 7 , 9 };
127
- std::unique_ptr<SelectedRows> selected_rows2{new SelectedRows (rows2, height)};
143
+ std::unique_ptr<paddle::framework::SelectedRows> selected_rows2{
144
+ new paddle::framework::SelectedRows (rows2, height)};
128
145
auto * in2_value = selected_rows2->mutable_value ();
129
146
in2_value->mutable_data <float >(
130
- make_ddim ({static_cast <int64_t >(rows2.size ()), row_numel}), cpu_place);
147
+ paddle::framework::make_ddim (
148
+ {static_cast <int64_t >(rows2.size ()), row_numel}),
149
+ cpu_place);
131
150
functor (ctx, in2_value, 2.0 );
132
151
133
- std::unique_ptr<SelectedRows> output{new SelectedRows ()};
152
+ std::unique_ptr<paddle::framework::SelectedRows> output{
153
+ new paddle::framework::SelectedRows ()};
134
154
output->set_height (height);
135
155
auto * out_value = output->mutable_value ();
136
156
137
157
// simplely concat two SelectedRows
138
- out_value->mutable_data <float >(make_ddim ({7 , 10 }), cpu_place);
158
+ out_value->mutable_data <float >(paddle::framework::make_ddim ({7 , 10 }),
159
+ cpu_place);
139
160
140
- SelectedRowsAddTo<CPUDeviceContext, float > add_to_functor;
161
+ paddle::operators::math::SelectedRowsAddTo<paddle::platform::CPUDeviceContext,
162
+ float >
163
+ add_to_functor;
141
164
add_to_functor (ctx, *selected_rows1, 0 , output.get ());
142
165
add_to_functor (ctx, *selected_rows2, in1_value->numel (), output.get ());
143
166
@@ -169,11 +192,15 @@ TEST(selected_rows_functor, cpu_add_to) {
169
192
EXPECT_EQ (out_data[5 * row_numel + 7 ], 2.0 );
170
193
EXPECT_EQ (out_data[6 * row_numel + 9 ], 2.0 );
171
194
172
- std::unique_ptr<Tensor> tensor1{new Tensor ()};
173
- tensor1->mutable_data <float >(make_ddim ({height, row_numel}), cpu_place);
195
+ std::unique_ptr<paddle::framework::Tensor> tensor1{
196
+ new paddle::framework::Tensor ()};
197
+ tensor1->mutable_data <float >(
198
+ paddle::framework::make_ddim ({height, row_numel}), cpu_place);
174
199
functor (ctx, tensor1.get (), 3.0 );
175
200
176
- SelectedRowsAddToTensor<CPUDeviceContext, float > add_to_tensor_functor;
201
+ paddle::operators::math::SelectedRowsAddToTensor<
202
+ paddle::platform::CPUDeviceContext, float >
203
+ add_to_tensor_functor;
177
204
add_to_tensor_functor (ctx, *output, tensor1.get ());
178
205
179
206
auto * tensor1_data = tensor1->data <float >();
0 commit comments