14
14
#include < gtest/gtest.h>
15
15
16
16
#include < executorch/extension/data_loader/file_data_loader.h>
17
+ #include < executorch/extension/tensor/tensor.h>
17
18
18
19
using namespace ::executorch::extension;
19
20
using namespace ::executorch::runtime;
@@ -121,17 +122,11 @@ TEST_F(ModuleTest, TestNonExistentMethodMeta) {
121
122
122
123
TEST_F (ModuleTest, TestExecute) {
123
124
Module module (model_path_);
125
+ auto tensor = make_tensor_ptr ({1 }, {1 });
124
126
125
- std::array<float , 1 > input{1 };
126
- std::array<int32_t , 1 > sizes{1 };
127
- exec_aten::TensorImpl tensor (
128
- exec_aten::ScalarType::Float, sizes.size (), sizes.data (), input.data ());
129
-
130
- const auto result = module.execute (
131
- " forward" , {exec_aten::Tensor (&tensor), exec_aten::Tensor (&tensor)});
127
+ const auto result = module.execute (" forward" , {tensor, tensor});
132
128
EXPECT_TRUE (result.ok ());
133
129
134
- EXPECT_TRUE (result.ok ());
135
130
EXPECT_TRUE (module.is_loaded ());
136
131
EXPECT_TRUE (module.is_method_loaded (" forward" ));
137
132
@@ -146,13 +141,9 @@ TEST_F(ModuleTest, TestExecutePreload) {
146
141
const auto error = module.load ();
147
142
EXPECT_EQ (error, Error::Ok);
148
143
149
- std::array<float , 1 > input{1 };
150
- std::array<int32_t , 1 > sizes{1 };
151
- exec_aten::TensorImpl tensor (
152
- exec_aten::ScalarType::Float, sizes.size (), sizes.data (), input.data ());
144
+ auto tensor = make_tensor_ptr ({1 }, {1 });
153
145
154
- const auto result = module.execute (
155
- " forward" , {exec_aten::Tensor (&tensor), exec_aten::Tensor (&tensor)});
146
+ const auto result = module.execute (" forward" , {tensor, tensor});
156
147
EXPECT_TRUE (result.ok ());
157
148
158
149
const auto data = result->at (0 ).toTensor ().const_data_ptr <float >();
@@ -166,13 +157,9 @@ TEST_F(ModuleTest, TestExecutePreload_method) {
166
157
const auto error = module.load_method (" forward" );
167
158
EXPECT_EQ (error, Error::Ok);
168
159
169
- std::array<float , 1 > input{1 };
170
- std::array<int32_t , 1 > sizes{1 };
171
- exec_aten::TensorImpl tensor (
172
- exec_aten::ScalarType::Float, sizes.size (), sizes.data (), input.data ());
160
+ auto tensor = make_tensor_ptr ({1 }, {1 });
173
161
174
- const auto result = module.execute (
175
- " forward" , {exec_aten::Tensor (&tensor), exec_aten::Tensor (&tensor)});
162
+ const auto result = module.execute (" forward" , {tensor, tensor});
176
163
EXPECT_TRUE (result.ok ());
177
164
178
165
const auto data = result->at (0 ).toTensor ().const_data_ptr <float >();
@@ -189,13 +176,9 @@ TEST_F(ModuleTest, TestExecutePreloadProgramAndMethod) {
189
176
const auto load_method_error = module.load_method (" forward" );
190
177
EXPECT_EQ (load_method_error, Error::Ok);
191
178
192
- std::array<float , 1 > input{1 };
193
- std::array<int32_t , 1 > sizes{1 };
194
- exec_aten::TensorImpl tensor (
195
- exec_aten::ScalarType::Float, sizes.size (), sizes.data (), input.data ());
179
+ auto tensor = make_tensor_ptr ({1 }, {1 });
196
180
197
- const auto result = module.execute (
198
- " forward" , {exec_aten::Tensor (&tensor), exec_aten::Tensor (&tensor)});
181
+ const auto result = module.execute (" forward" , {tensor, tensor});
199
182
EXPECT_TRUE (result.ok ());
200
183
201
184
const auto data = result->at (0 ).toTensor ().const_data_ptr <float >();
@@ -222,40 +205,27 @@ TEST_F(ModuleTest, TestExecuteOnCurrupted) {
222
205
TEST_F (ModuleTest, TestGet) {
223
206
Module module (model_path_);
224
207
225
- std::array<float , 1 > input{1 };
226
- std::array<int32_t , 1 > sizes{1 };
227
- exec_aten::TensorImpl tensor (
228
- exec_aten::ScalarType::Float, sizes.size (), sizes.data (), input.data ());
229
-
230
- const auto result = module.get (
231
- " forward" , {exec_aten::Tensor (&tensor), exec_aten::Tensor (&tensor)});
208
+ auto tensor = make_tensor_ptr ({1 }, {1 });
232
209
210
+ const auto result = module.get (" forward" , {tensor, tensor});
233
211
EXPECT_TRUE (result.ok ());
234
212
const auto data = result->toTensor ().const_data_ptr <float >();
235
213
EXPECT_NEAR (data[0 ], 2 , 1e-5 );
236
214
}
237
215
238
216
TEST_F (ModuleTest, TestForward) {
239
217
auto module = std::make_unique<Module>(model_path_);
218
+ auto tensor = make_tensor_ptr ({21 .f });
240
219
241
- std::array<float , 1 > input{1 };
242
- std::array<int32_t , 1 > sizes{1 };
243
- exec_aten::TensorImpl tensor (
244
- exec_aten::ScalarType::Float, sizes.size (), sizes.data (), input.data ());
245
-
246
- const auto result =
247
- module->forward ({exec_aten::Tensor (&tensor), exec_aten::Tensor (&tensor)});
220
+ const auto result = module->forward ({tensor, tensor});
248
221
EXPECT_TRUE (result.ok ());
249
222
250
223
const auto data = result->at (0 ).toTensor ().const_data_ptr <float >();
251
224
252
- EXPECT_NEAR (data[0 ], 2 , 1e-5 );
225
+ EXPECT_NEAR (data[0 ], 42 , 1e-5 );
253
226
254
- std::array<float , 2 > input2{2 , 3 };
255
- exec_aten::TensorImpl tensor2 (
256
- exec_aten::ScalarType::Float, sizes.size (), sizes.data (), input2.data ());
257
- const auto result2 = module->forward (
258
- {exec_aten::Tensor (&tensor2), exec_aten::Tensor (&tensor2)});
227
+ auto tensor2 = make_tensor_ptr ({1 }, {2 , 3 });
228
+ const auto result2 = module->forward ({tensor2, tensor2});
259
229
EXPECT_TRUE (result2.ok ());
260
230
261
231
const auto data2 = result->at (0 ).toTensor ().const_data_ptr <float >();
@@ -310,26 +280,20 @@ TEST_F(ModuleTest, TestProgramSharingAndDataLoaderManagement) {
310
280
EXPECT_EQ (load_error, Error::Ok);
311
281
EXPECT_TRUE (module1->is_loaded ());
312
282
313
- std::array<float , 1 > input{1 };
314
- std::array<int32_t , 1 > sizes{1 };
315
- exec_aten::TensorImpl tensor (
316
- exec_aten::ScalarType::Float, sizes.size (), sizes.data (), input.data ());
283
+ auto tensor = make_tensor_ptr ({1 }, {1 });
317
284
318
- auto result1 = module1->execute (
319
- " forward" , {exec_aten::Tensor (&tensor), exec_aten::Tensor (&tensor)});
285
+ const auto result1 = module1->execute (" forward" , {tensor, tensor});
320
286
EXPECT_TRUE (result1.ok ());
321
287
322
288
auto module2 = std::make_unique<Module>(module1->program ());
323
289
324
- auto result2 = module2->execute (
325
- " forward" , {exec_aten::Tensor (&tensor), exec_aten::Tensor (&tensor)});
290
+ const auto result2 = module2->execute (" forward" , {tensor, tensor});
326
291
EXPECT_TRUE (result2.ok ());
327
292
328
293
module1 = std::make_unique<Module>(" /path/to/nonexistent/file.pte" );
329
294
EXPECT_FALSE (module1->is_loaded ());
330
295
331
- auto result3 = module2->execute (
332
- " forward" , {exec_aten::Tensor (&tensor), exec_aten::Tensor (&tensor)});
296
+ const auto result3 = module2->execute (" forward" , {tensor, tensor});
333
297
EXPECT_TRUE (result3.ok ());
334
298
}
335
299
@@ -361,13 +325,9 @@ TEST_F(ModuleTest, TestProgramPersistenceAndReuseAfterModuleDestruction) {
361
325
362
326
EXPECT_EQ (module.program (), shared_program);
363
327
364
- std::array<float , 1 > input{1 };
365
- std::array<int32_t , 1 > sizes{1 };
366
- exec_aten::TensorImpl tensor (
367
- exec_aten::ScalarType::Float, sizes.size (), sizes.data (), input.data ());
328
+ auto tensor = make_tensor_ptr ({1 }, {1 });
368
329
369
- auto result = module.execute (
370
- " forward" , {exec_aten::Tensor (&tensor), exec_aten::Tensor (&tensor)});
330
+ const auto result = module.execute (" forward" , {tensor, tensor});
371
331
EXPECT_TRUE (result.ok ());
372
332
373
333
auto data = result->at (0 ).toTensor ().const_data_ptr <float >();
@@ -392,15 +352,9 @@ TEST_F(ModuleTest, TestConcurrentExecutionWithSharedProgram) {
392
352
auto thread = [](std::shared_ptr<Program> program,
393
353
const std::array<float , 1 >& input) {
394
354
Module module (program);
395
- std::array<int32_t , 1 > sizes{1 };
396
- exec_aten::TensorImpl tensor (
397
- exec_aten::ScalarType::Float,
398
- sizes.size (),
399
- sizes.data (),
400
- (void *)input.data ());
401
-
402
- const auto result = module.forward (
403
- {exec_aten::Tensor (&tensor), exec_aten::Tensor (&tensor)});
355
+ auto tensor = from_blob ((void *)input.data (), {1 });
356
+
357
+ const auto result = module.forward ({tensor, tensor});
404
358
EXPECT_TRUE (result.ok ());
405
359
406
360
const auto data = result->at (0 ).toTensor ().const_data_ptr <float >();
0 commit comments