@@ -34,15 +34,15 @@ TEST(CAPI2_TensorListTest, CreateDestroy) {
34
34
ASSERT_NE (h, nullptr );
35
35
dali::c_api::TensorListHandle tl (h);
36
36
ASSERT_EQ (h, tl.get ());
37
- ASSERT_EQ (r, DALI_SUCCESS);
37
+ ASSERT_EQ (r, DALI_SUCCESS) << daliGetLastErrorMessage () ;
38
38
39
39
int ref = -1 ;
40
- EXPECT_EQ (daliTensorListRefCount (h, &ref), DALI_SUCCESS);
40
+ EXPECT_EQ (daliTensorListRefCount (h, &ref), DALI_SUCCESS) << daliGetLastErrorMessage () ;
41
41
EXPECT_EQ (ref, 1 );
42
42
ref = -1 ;
43
43
44
44
h = tl.release ();
45
- EXPECT_EQ (daliTensorListDecRef (h, &ref), DALI_SUCCESS);
45
+ EXPECT_EQ (daliTensorListDecRef (h, &ref), DALI_SUCCESS) << daliGetLastErrorMessage () ;
46
46
EXPECT_EQ (ref, 0 );
47
47
}
48
48
@@ -81,17 +81,21 @@ void TestTensorListResize(daliStorageDevice_t storage_device) {
81
81
shapes[0 ] = -1 ;
82
82
EXPECT_EQ (daliTensorListResize (tl, 4 , 3 , shapes, dtype, " HWC" ), DALI_ERROR_INVALID_ARGUMENT);
83
83
shapes[0 ] = 480 ;
84
- EXPECT_EQ (daliTensorListResize (tl, 1 , 3 , shapes, dtype, " HWC" ), DALI_SUCCESS);
84
+ EXPECT_EQ (daliTensorListResize (tl, 1 , 3 , shapes, dtype, " HWC" ), DALI_SUCCESS)
85
+ << daliGetLastErrorMessage ();
85
86
// resize, but keep the layout
86
- EXPECT_EQ (daliTensorListResize (tl, 4 , 3 , shapes, dtype, nullptr ), DALI_SUCCESS);
87
+ EXPECT_EQ (daliTensorListResize (tl, 4 , 3 , shapes, dtype, nullptr ), DALI_SUCCESS)
88
+ << daliGetLastErrorMessage ();
87
89
88
90
size_t element_size = dali::TypeTable::GetTypeInfo (dtype).size ();
89
91
90
- EXPECT_EQ (daliTensorListGetShape (tl, nullptr , nullptr , nullptr ), DALI_SUCCESS);
92
+ EXPECT_EQ (daliTensorListGetShape (tl, nullptr , nullptr , nullptr ), DALI_SUCCESS)
93
+ << daliGetLastErrorMessage ();
91
94
{
92
95
int nsamples = -1 , ndim = -1 ;
93
96
const int64_t *shape_data = nullptr ;
94
- EXPECT_EQ (daliTensorListGetShape (tl, &nsamples, &ndim, &shape_data), DALI_SUCCESS);
97
+ EXPECT_EQ (daliTensorListGetShape (tl, &nsamples, &ndim, &shape_data), DALI_SUCCESS)
98
+ << daliGetLastErrorMessage ();
95
99
ASSERT_NE (shape_data, nullptr );
96
100
EXPECT_EQ (nsamples, 4 );
97
101
EXPECT_EQ (ndim, 3 );
@@ -105,13 +109,13 @@ void TestTensorListResize(daliStorageDevice_t storage_device) {
105
109
const char *base;
106
110
for (int i = 0 ; i < 4 ; i++) {
107
111
daliTensorDesc_t desc{};
108
- EXPECT_EQ (daliTensorListGetTensorDesc (tl, &desc, i), DALI_SUCCESS);
112
+ EXPECT_EQ (daliTensorListGetTensorDesc (tl, &desc, i), DALI_SUCCESS) << daliGetLastErrorMessage () ;
109
113
ASSERT_EQ (desc.ndim , 3 );
110
- ASSERT_NE (desc.data , nullptr );
111
114
if (i == 0 )
112
115
base = static_cast <char *>(desc.data );
113
116
EXPECT_EQ (desc.data , base + offset);
114
117
EXPECT_EQ (desc.dtype , dtype);
118
+ ASSERT_NE (desc.shape , nullptr );
115
119
for (int j = 0 ; j < 3 ; j++)
116
120
EXPECT_EQ (desc.shape [j], shapes[3 * i + j]);
117
121
size_t sample_bytes = volume (dali::make_cspan (desc.shape , desc.ndim )) * element_size;
@@ -148,7 +152,7 @@ MakeTestDeleter(element_t *expected_data) {
148
152
EXPECT_EQ (ctx->buffer_delete_count , 0 );
149
153
EXPECT_EQ (data, ctx->expected_data );
150
154
ctx->buffer_delete_count ++;
151
- delete [] static_cast < element_t *>( data);
155
+ // do not actually delete the data
152
156
};
153
157
deleter.destroy_context = [](void *vctx) {
154
158
auto *ctx = static_cast <TestDeleterCtx *>(vctx);
@@ -189,21 +193,22 @@ TEST(CAPI2_TensorListTest, AttachBuffer) {
189
193
" HWC" ,
190
194
data.get (),
191
195
offsets,
192
- deleter), DALI_SUCCESS);
196
+ deleter), DALI_SUCCESS) << daliGetLastErrorMessage (); ;
193
197
194
- void *data_ptr = data. release (); // the buffer is now owned by the tensor list
198
+ // The deleter doesn't actually delete - we still own the data.
195
199
196
200
ptrdiff_t offset = 0 ;
197
- const char *base = static_cast <const char *>(data_ptr );
201
+ const char *base = reinterpret_cast <const char *>(data. get () );
198
202
for (int i = 0 ; i < 4 ; i++) {
199
203
daliTensorDesc_t desc{};
200
- EXPECT_EQ (daliTensorListGetTensorDesc (tl, &desc, i), DALI_SUCCESS);
204
+ EXPECT_EQ (daliTensorListGetTensorDesc (tl, &desc, i), DALI_SUCCESS) << daliGetLastErrorMessage () ;
201
205
ASSERT_EQ (desc.ndim , 3 );
202
- ASSERT_NE (desc.data , nullptr );
203
206
EXPECT_EQ (desc.data , base + offset);
204
207
EXPECT_EQ (desc.dtype , dtype);
208
+ ASSERT_NE (desc.shape , nullptr );
205
209
for (int j = 0 ; j < 3 ; j++)
206
210
EXPECT_EQ (desc.shape [j], lshape[i][j]);
211
+ EXPECT_STREQ (desc.layout , " HWC" );
207
212
size_t sample_bytes = volume (dali::make_cspan (desc.shape , desc.ndim )) * sizeof (element_t );
208
213
offset += sample_bytes;
209
214
}
@@ -215,6 +220,74 @@ TEST(CAPI2_TensorListTest, AttachBuffer) {
215
220
}
216
221
217
222
223
+ TEST (CAPI2_TensorListTest, AttachSamples) {
224
+ daliBufferPlacement_t placement{};
225
+ placement.device_type = DALI_STORAGE_CPU;
226
+ using element_t = int ;
227
+ daliDataType_t dtype = dali::type2id<element_t >::value;
228
+ dali::TensorListShape<> lshape ({
229
+ { 480 , 640 , 3 },
230
+ { 600 , 800 , 4 },
231
+ { 348 , 720 , 1 },
232
+ { 1080 , 1920 , 3 }
233
+ });
234
+ auto size = lshape.num_elements ();
235
+ int N = lshape.num_samples ();
236
+ std::vector<std::unique_ptr<element_t >> data (N);
237
+
238
+ for (int i = 0 ; i < N; i++) {
239
+ data[i].reset (new element_t [size]);
240
+ }
241
+
242
+ std::vector<daliDeleter_t> deleters (N);
243
+ std::vector<std::unique_ptr<TestDeleterCtx>> deleter_ctxs (N);
244
+
245
+ for (int i = 0 ; i < N; i++) {
246
+ std::tie (deleters[i], deleter_ctxs[i]) = MakeTestDeleter (data[i].get ());
247
+ }
248
+
249
+ std::vector<daliTensorDesc_t> samples (N);
250
+
251
+ for (int i = 0 ; i < N; i++) {
252
+ samples[i].ndim = lshape.sample_dim ();
253
+ samples[i].dtype = dtype;
254
+ samples[i].layout = i == 0 ? " HWC" : nullptr ;
255
+ samples[i].shape = lshape.tensor_shape_span (i).data ();
256
+ samples[i].data = data[i].get ();
257
+ }
258
+
259
+ auto tl = CreateTensorList (placement);
260
+ ASSERT_EQ (daliTensorListAttachSamples (
261
+ tl,
262
+ lshape.num_samples (),
263
+ -1 ,
264
+ DALI_NO_TYPE,
265
+ nullptr ,
266
+ samples.data (),
267
+ deleters.data ()), DALI_SUCCESS) << daliGetLastErrorMessage ();
268
+
269
+ // The deleter doesn't actually delete - we still own the data.
270
+ for (int i = 0 ; i < 4 ; i++) {
271
+ daliTensorDesc_t desc{};
272
+ EXPECT_EQ (daliTensorListGetTensorDesc (tl, &desc, i), DALI_SUCCESS) << daliGetLastErrorMessage ();
273
+ ASSERT_EQ (desc.ndim , 3 );
274
+ EXPECT_EQ (desc.data , data[i].get ());
275
+ EXPECT_EQ (desc.dtype , dtype);
276
+ ASSERT_NE (desc.shape , nullptr );
277
+ for (int j = 0 ; j < 3 ; j++)
278
+ EXPECT_EQ (desc.shape [j], lshape[i][j]);
279
+ EXPECT_STREQ (desc.layout , " HWC" );
280
+ }
281
+
282
+ tl.reset ();
283
+
284
+ for (auto &ctx : deleter_ctxs) {
285
+ EXPECT_EQ (ctx->buffer_delete_count , 1 ) << " Buffer deleter not called" ;
286
+ EXPECT_EQ (ctx->context_delete_count , 1 ) << " Deleter context not destroyed" ;
287
+ }
288
+ }
289
+
290
+
218
291
TEST (CAPI2_TensorListTest, ViewAsTensor) {
219
292
daliBufferPlacement_t placement{};
220
293
placement.device_type = DALI_STORAGE_CPU;
@@ -245,18 +318,18 @@ TEST(CAPI2_TensorListTest, ViewAsTensor) {
245
318
" HWC" ,
246
319
data.get (),
247
320
offsets,
248
- deleter), DALI_SUCCESS);
321
+ deleter), DALI_SUCCESS) << daliGetLastErrorMessage () ;
249
322
250
- void *data_ptr = data. release (); // the buffer is now owned by the tensor list
323
+ // The deleter doesn't actually delete - we still own the data.
251
324
252
325
daliTensor_h ht = nullptr ;
253
- EXPECT_EQ (daliTensorListViewAsTensor (tl, &ht), DALI_SUCCESS);
326
+ EXPECT_EQ (daliTensorListViewAsTensor (tl, &ht), DALI_SUCCESS) << daliGetLastErrorMessage () ;
254
327
ASSERT_NE (ht, nullptr );
255
328
dali::c_api::TensorHandle t (ht);
256
329
257
330
daliTensorDesc_t desc{};
258
- EXPECT_EQ (daliTensorGetDesc (t, &desc), DALI_SUCCESS);
259
- EXPECT_EQ (desc.data , data_ptr );
331
+ EXPECT_EQ (daliTensorGetDesc (t, &desc), DALI_SUCCESS) << daliGetLastErrorMessage () ;
332
+ EXPECT_EQ (desc.data , data. get () );
260
333
EXPECT_EQ (desc.shape [0 ], lshape.num_samples ());
261
334
ASSERT_EQ (desc.ndim , 4 );
262
335
ASSERT_NE (desc.shape , nullptr );
@@ -265,10 +338,10 @@ TEST(CAPI2_TensorListTest, ViewAsTensor) {
265
338
EXPECT_EQ (desc.shape [3 ], lshape[0 ][2 ]);
266
339
EXPECT_STREQ (desc.layout , " NHWC" );
267
340
EXPECT_EQ (desc.dtype , dtype);
268
- EXPECT_EQ (daliTensorGetShape (t, nullptr , nullptr ), DALI_SUCCESS);
341
+ EXPECT_EQ (daliTensorGetShape (t, nullptr , nullptr ), DALI_SUCCESS) << daliGetLastErrorMessage () ;
269
342
int ndim = -1 ;
270
343
const int64_t *shape = nullptr ;
271
- EXPECT_EQ (daliTensorGetShape (t, &ndim, &shape), DALI_SUCCESS);
344
+ EXPECT_EQ (daliTensorGetShape (t, &ndim, &shape), DALI_SUCCESS) << daliGetLastErrorMessage () ;
272
345
EXPECT_EQ (ndim, 4 );
273
346
EXPECT_EQ (shape, desc.shape );
274
347
@@ -315,9 +388,9 @@ TEST(CAPI2_TensorListTest, ViewAsTensorError) {
315
388
" HWC" ,
316
389
data.get (),
317
390
offsets,
318
- deleter), DALI_SUCCESS);
391
+ deleter), DALI_SUCCESS) << daliGetLastErrorMessage () ;
319
392
320
- void *data_ptr = data. release (); // the buffer is now owned by the tensor list
393
+ // The deleter doesn't actually delete - we still own the data.
321
394
322
395
daliTensor_h ht = nullptr ;
323
396
EXPECT_EQ (daliTensorListViewAsTensor (tl, &ht), DALI_ERROR_INVALID_OPERATION);
@@ -352,15 +425,15 @@ TEST(CAPI2_TensorTest, CreateDestroy) {
352
425
ASSERT_NE (h, nullptr );
353
426
dali::c_api::TensorHandle tl (h);
354
427
ASSERT_EQ (h, tl.get ());
355
- ASSERT_EQ (r, DALI_SUCCESS);
428
+ ASSERT_EQ (r, DALI_SUCCESS) << daliGetLastErrorMessage () ;
356
429
357
430
int ref = -1 ;
358
- EXPECT_EQ (daliTensorRefCount (h, &ref), DALI_SUCCESS);
431
+ EXPECT_EQ (daliTensorRefCount (h, &ref), DALI_SUCCESS) << daliGetLastErrorMessage () ;
359
432
EXPECT_EQ (ref, 1 );
360
433
ref = -1 ;
361
434
362
435
h = tl.release ();
363
- EXPECT_EQ (daliTensorDecRef (h, &ref), DALI_SUCCESS);
436
+ EXPECT_EQ (daliTensorDecRef (h, &ref), DALI_SUCCESS) << daliGetLastErrorMessage () ;
364
437
EXPECT_EQ (ref, 0 );
365
438
}
366
439
@@ -396,20 +469,22 @@ void TestTensorResize(daliStorageDevice_t storage_device) {
396
469
shape[0 ] = -1 ;
397
470
EXPECT_EQ (daliTensorResize (t, 3 , shape, dtype, " HWC" ), DALI_ERROR_INVALID_ARGUMENT);
398
471
shape[0 ] = 1 ;
399
- EXPECT_EQ (daliTensorResize (t, 3 , shape, dtype, " HWC" ), DALI_SUCCESS);
472
+ EXPECT_EQ (daliTensorResize (t, 3 , shape, dtype, " HWC" ), DALI_SUCCESS)
473
+ << daliGetLastErrorMessage ();
400
474
401
475
shape[0 ] = 1080 ;
402
- EXPECT_EQ (daliTensorResize (t, 3 , shape, dtype, nullptr ), DALI_SUCCESS);
476
+ EXPECT_EQ (daliTensorResize (t, 3 , shape, dtype, nullptr ), DALI_SUCCESS)
477
+ << daliGetLastErrorMessage ();
403
478
404
479
size_t element_size = dali::TypeTable::GetTypeInfo (dtype).size ();
405
480
406
481
ptrdiff_t offset = 0 ;
407
482
daliTensorDesc_t desc{};
408
- EXPECT_EQ (daliTensorGetDesc (t, &desc), DALI_SUCCESS);
483
+ EXPECT_EQ (daliTensorGetDesc (t, &desc), DALI_SUCCESS) << daliGetLastErrorMessage () ;
409
484
ASSERT_EQ (desc.ndim , 3 );
410
- ASSERT_NE (desc.data , nullptr );
411
485
EXPECT_STREQ (desc.layout , " HWC" );
412
486
EXPECT_EQ (desc.dtype , dtype);
487
+ ASSERT_NE (desc.shape , nullptr );
413
488
for (int j = 0 ; j < 3 ; j++)
414
489
EXPECT_EQ (desc.shape [j], shape[j]);
415
490
size_t sample_bytes = volume (dali::make_cspan (desc.shape , desc.ndim )) * element_size;
0 commit comments