@@ -22,7 +22,7 @@ namespace torch::executor {
22
22
class ModuleTest : public ::testing::Test {
23
23
protected:
24
24
static void SetUpTestSuite () {
25
- model_path_ = std::getenv (" RESOURCES_PATH" ) + std::string (" /model .pte" );
25
+ model_path_ = std::getenv (" RESOURCES_PATH" ) + std::string (" /add .pte" );
26
26
}
27
27
28
28
static std::string model_path_;
@@ -95,17 +95,16 @@ TEST_F(ModuleTest, TestMethodMeta) {
95
95
const auto meta = module .method_meta (" forward" );
96
96
EXPECT_TRUE (meta.ok ());
97
97
EXPECT_STREQ (meta->name (), " forward" );
98
- EXPECT_EQ (meta->num_inputs (), 1 );
98
+ EXPECT_EQ (meta->num_inputs (), 2 );
99
99
EXPECT_EQ (*(meta->input_tag (0 )), Tag::Tensor);
100
100
EXPECT_EQ (meta->num_outputs (), 1 );
101
101
EXPECT_EQ (*(meta->output_tag (0 )), Tag::Tensor);
102
102
103
103
const auto input_meta = meta->input_tensor_meta (0 );
104
104
EXPECT_TRUE (input_meta.ok ());
105
105
EXPECT_EQ (input_meta->scalar_type (), ScalarType::Float);
106
- EXPECT_EQ (input_meta->sizes ().size (), 2 );
106
+ EXPECT_EQ (input_meta->sizes ().size (), 1 );
107
107
EXPECT_EQ (input_meta->sizes ()[0 ], 1 );
108
- EXPECT_EQ (input_meta->sizes ()[1 ], 2 );
109
108
110
109
const auto output_meta = meta->output_tensor_meta (0 );
111
110
EXPECT_TRUE (output_meta.ok ());
@@ -124,19 +123,22 @@ TEST_F(ModuleTest, TestNonExistentMethodMeta) {
124
123
TEST_F (ModuleTest, TestExecute) {
125
124
Module module (model_path_);
126
125
127
- std::array<float , 2 > input{1 , 2 };
128
- std::array<int32_t , 2 > sizes{1 , 2 };
126
+ std::array<float , 1 > input{1 };
127
+ std::array<int32_t , 1 > sizes{1 };
129
128
TensorImpl tensor (
130
129
ScalarType::Float, sizes.size (), sizes.data (), input.data ());
131
130
132
- const auto result = module .execute (" forward" , Tensor (&tensor));
131
+ const auto result =
132
+ module .execute (" forward" , {Tensor (&tensor), Tensor (&tensor)});
133
+ EXPECT_TRUE (result.ok ());
134
+
133
135
EXPECT_TRUE (result.ok ());
134
136
EXPECT_TRUE (module .is_loaded ());
135
137
EXPECT_TRUE (module .is_method_loaded (" forward" ));
136
138
137
139
const auto data = result->at (0 ).toTensor ().const_data_ptr <float >();
138
140
139
- EXPECT_NEAR (data[0 ], 1.5 , 1e-5 );
141
+ EXPECT_NEAR (data[0 ], 2 , 1e-5 );
140
142
}
141
143
142
144
TEST_F (ModuleTest, TestExecutePreload) {
@@ -145,17 +147,18 @@ TEST_F(ModuleTest, TestExecutePreload) {
145
147
const auto error = module .load ();
146
148
EXPECT_EQ (error, Error::Ok);
147
149
148
- std::array<float , 2 > input{1 , 2 };
149
- std::array<int32_t , 2 > sizes{1 , 2 };
150
+ std::array<float , 1 > input{1 };
151
+ std::array<int32_t , 1 > sizes{1 };
150
152
TensorImpl tensor (
151
153
ScalarType::Float, sizes.size (), sizes.data (), input.data ());
152
154
153
- const auto result = module .execute (" forward" , Tensor (&tensor));
155
+ const auto result =
156
+ module .execute (" forward" , {Tensor (&tensor), Tensor (&tensor)});
154
157
EXPECT_TRUE (result.ok ());
155
158
156
159
const auto data = result->at (0 ).toTensor ().const_data_ptr <float >();
157
160
158
- EXPECT_NEAR (data[0 ], 1.5 , 1e-5 );
161
+ EXPECT_NEAR (data[0 ], 2 , 1e-5 );
159
162
}
160
163
161
164
TEST_F (ModuleTest, TestExecutePreload_method) {
@@ -164,17 +167,18 @@ TEST_F(ModuleTest, TestExecutePreload_method) {
164
167
const auto error = module .load_method (" forward" );
165
168
EXPECT_EQ (error, Error::Ok);
166
169
167
- std::array<float , 2 > input{1 , 2 };
168
- std::array<int32_t , 2 > sizes{1 , 2 };
170
+ std::array<float , 1 > input{1 };
171
+ std::array<int32_t , 1 > sizes{1 };
169
172
TensorImpl tensor (
170
173
ScalarType::Float, sizes.size (), sizes.data (), input.data ());
171
174
172
- const auto result = module .execute (" forward" , Tensor (&tensor));
175
+ const auto result =
176
+ module .execute (" forward" , {Tensor (&tensor), Tensor (&tensor)});
173
177
EXPECT_TRUE (result.ok ());
174
178
175
179
const auto data = result->at (0 ).toTensor ().const_data_ptr <float >();
176
180
177
- EXPECT_NEAR (data[0 ], 1.5 , 1e-5 );
181
+ EXPECT_NEAR (data[0 ], 2 , 1e-5 );
178
182
}
179
183
180
184
TEST_F (ModuleTest, TestExecutePreloadProgramAndMethod) {
@@ -186,17 +190,18 @@ TEST_F(ModuleTest, TestExecutePreloadProgramAndMethod) {
186
190
const auto load_method_error = module .load_method (" forward" );
187
191
EXPECT_EQ (load_method_error, Error::Ok);
188
192
189
- std::array<float , 2 > input{1 , 2 };
190
- std::array<int32_t , 2 > sizes{1 , 2 };
193
+ std::array<float , 1 > input{1 };
194
+ std::array<int32_t , 1 > sizes{1 };
191
195
TensorImpl tensor (
192
196
ScalarType::Float, sizes.size (), sizes.data (), input.data ());
193
197
194
- const auto result = module .execute (" forward" , Tensor (&tensor));
198
+ const auto result =
199
+ module .execute (" forward" , {Tensor (&tensor), Tensor (&tensor)});
195
200
EXPECT_TRUE (result.ok ());
196
201
197
202
const auto data = result->at (0 ).toTensor ().const_data_ptr <float >();
198
203
199
- EXPECT_NEAR (data[0 ], 1.5 , 1e-5 );
204
+ EXPECT_NEAR (data[0 ], 2 , 1e-5 );
200
205
}
201
206
202
207
TEST_F (ModuleTest, TestExecuteOnNonExistent) {
@@ -218,41 +223,42 @@ TEST_F(ModuleTest, TestExecuteOnCurrupted) {
218
223
TEST_F (ModuleTest, TestGet) {
219
224
Module module (model_path_);
220
225
221
- std::array<float , 2 > input{1 , 2 };
222
- std::array<int32_t , 2 > sizes{1 , 2 };
226
+ std::array<float , 1 > input{1 };
227
+ std::array<int32_t , 1 > sizes{1 };
223
228
TensorImpl tensor (
224
229
ScalarType::Float, sizes.size (), sizes.data (), input.data ());
225
230
226
- const auto result = module .get (" forward" , Tensor (&tensor));
231
+ const auto result = module .get (" forward" , { Tensor (&tensor), Tensor (&tensor)} );
227
232
228
233
EXPECT_TRUE (result.ok ());
229
234
const auto data = result->toTensor ().const_data_ptr <float >();
230
- EXPECT_NEAR (data[0 ], 1.5 , 1e-5 );
235
+ EXPECT_NEAR (data[0 ], 2 , 1e-5 );
231
236
}
232
237
233
238
TEST_F (ModuleTest, TestForward) {
234
239
auto module = std::make_unique<Module>(model_path_);
235
240
236
- std::array<float , 2 > input{1 , 2 };
237
- std::array<int32_t , 2 > sizes{1 , 2 };
241
+ std::array<float , 1 > input{1 };
242
+ std::array<int32_t , 1 > sizes{1 };
238
243
TensorImpl tensor (
239
244
ScalarType::Float, sizes.size (), sizes.data (), input.data ());
240
- const auto result = module ->forward (Tensor (&tensor));
245
+
246
+ const auto result = module ->forward ({Tensor (&tensor), Tensor (&tensor)});
241
247
EXPECT_TRUE (result.ok ());
242
248
243
249
const auto data = result->at (0 ).toTensor ().const_data_ptr <float >();
244
250
245
- EXPECT_NEAR (data[0 ], 1.5 , 1e-5 );
251
+ EXPECT_NEAR (data[0 ], 2 , 1e-5 );
246
252
247
253
std::array<float , 2 > input2{2 , 3 };
248
254
TensorImpl tensor2 (
249
255
ScalarType::Float, sizes.size (), sizes.data (), input2.data ());
250
- const auto result2 = module ->forward (Tensor (&tensor2));
256
+ const auto result2 = module ->forward ({ Tensor (&tensor2), Tensor (&tensor2)} );
251
257
EXPECT_TRUE (result2.ok ());
252
258
253
259
const auto data2 = result->at (0 ).toTensor ().const_data_ptr <float >();
254
260
255
- EXPECT_NEAR (data2[0 ], 2.5 , 1e-5 );
261
+ EXPECT_NEAR (data2[0 ], 4 , 1e-5 );
256
262
}
257
263
258
264
TEST_F (ModuleTest, TestForwardWithInvalidInputs) {
@@ -303,23 +309,26 @@ TEST_F(ModuleTest, TestProgramSharingAndDataLoaderManagement) {
303
309
EXPECT_EQ (load_error, Error::Ok);
304
310
EXPECT_TRUE (module1->is_loaded ());
305
311
306
- std::array<float , 2 > input{1 , 2 };
307
- std::array<int32_t , 2 > sizes{1 , 2 };
312
+ std::array<float , 1 > input{1 };
313
+ std::array<int32_t , 1 > sizes{1 };
308
314
TensorImpl tensor (
309
315
ScalarType::Float, sizes.size (), sizes.data (), input.data ());
310
316
311
- auto result1 = module1->execute (" forward" , Tensor (&tensor));
317
+ auto result1 =
318
+ module1->execute (" forward" , {Tensor (&tensor), Tensor (&tensor)});
312
319
EXPECT_TRUE (result1.ok ());
313
320
314
321
auto module2 = std::make_unique<Module>(module1->program ());
315
322
316
- auto result2 = module2->execute (" forward" , Tensor (&tensor));
323
+ auto result2 =
324
+ module2->execute (" forward" , {Tensor (&tensor), Tensor (&tensor)});
317
325
EXPECT_TRUE (result2.ok ());
318
326
319
327
module1 = std::make_unique<Module>(" /path/to/nonexistent/file.pte" );
320
328
EXPECT_FALSE (module1->is_loaded ());
321
329
322
- auto result3 = module2->execute (" forward" , Tensor (&tensor));
330
+ auto result3 =
331
+ module2->execute (" forward" , {Tensor (&tensor), Tensor (&tensor)});
323
332
EXPECT_TRUE (result3.ok ());
324
333
}
325
334
@@ -351,17 +360,17 @@ TEST_F(ModuleTest, TestProgramPersistenceAndReuseAfterModuleDestruction) {
351
360
352
361
EXPECT_EQ (module .program (), shared_program);
353
362
354
- std::array<float , 2 > input{1 , 2 };
355
- std::array<int32_t , 2 > sizes{1 , 2 };
363
+ std::array<float , 1 > input{1 };
364
+ std::array<int32_t , 1 > sizes{1 };
356
365
TensorImpl tensor (
357
366
ScalarType::Float, sizes.size (), sizes.data (), input.data ());
358
367
359
- auto result = module .execute (" forward" , Tensor (&tensor));
368
+ auto result = module .execute (" forward" , { Tensor (&tensor), Tensor (&tensor)} );
360
369
EXPECT_TRUE (result.ok ());
361
370
362
371
auto data = result->at (0 ).toTensor ().const_data_ptr <float >();
363
372
364
- EXPECT_NEAR (data[0 ], 1.5 , 1e-5 );
373
+ EXPECT_NEAR (data[0 ], 2 , 1e-5 );
365
374
}
366
375
367
376
TEST_F (ModuleTest, TestConcurrentExecutionWithSharedProgram) {
@@ -379,24 +388,24 @@ TEST_F(ModuleTest, TestConcurrentExecutionWithSharedProgram) {
379
388
EXPECT_TRUE (program != nullptr );
380
389
381
390
auto thread = [](std::shared_ptr<Program> program,
382
- const std::array<float , 2 >& input) {
391
+ const std::array<float , 1 >& input) {
383
392
Module module (program);
384
- std::array<int32_t , 2 > sizes{1 , 2 };
393
+ std::array<int32_t , 1 > sizes{1 };
385
394
TensorImpl tensor (
386
395
ScalarType::Float, sizes.size (), sizes.data (), (void *)input.data ());
387
396
388
- const auto result = module .forward (Tensor (&tensor));
397
+ const auto result = module .forward ({ Tensor (&tensor), Tensor (&tensor)} );
389
398
EXPECT_TRUE (result.ok ());
390
399
391
400
const auto data = result->at (0 ).toTensor ().const_data_ptr <float >();
392
- EXPECT_NEAR (data[0 ], (input[0 ] + input[ 1 ]) / 2.0 , 1e-5 );
401
+ EXPECT_NEAR (data[0 ], (input[0 ] * 2 ) , 1e-5 );
393
402
};
394
403
395
- std::thread t1 (thread, program, std::array<float , 2 >{1 , 2 });
396
- std::thread t2 (thread, program, std::array<float , 2 >{2 , 3 });
397
- std::thread t3 (thread, program, std::array<float , 2 >{3 , 4 });
398
- std::thread t4 (thread, program, std::array<float , 2 >{4 , 5 });
399
- std::thread t5 (thread, program, std::array<float , 2 >{5 , 6 });
404
+ std::thread t1 (thread, program, std::array<float , 1 >{1 });
405
+ std::thread t2 (thread, program, std::array<float , 1 >{2 });
406
+ std::thread t3 (thread, program, std::array<float , 1 >{3 });
407
+ std::thread t4 (thread, program, std::array<float , 1 >{4 });
408
+ std::thread t5 (thread, program, std::array<float , 1 >{5 });
400
409
401
410
t1.join ();
402
411
t2.join ();
0 commit comments