Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:NVIDIA DALI C API V2 Data Objects Tests

From Leeroopedia
Revision as of 15:53, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/NVIDIA_DALI_C_API_V2_Data_Objects_Tests.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Data_Pipeline, C_API
Last Updated 2026-02-08 16:00 GMT

Overview

The C API v2 data objects test suite validates Tensor and TensorList creation, resizing, buffer attachment, reference counting, view-as-tensor conversion, source info, and cross-device copy-out operations.

Description

This file provides comprehensive Google Tests for the DALI C API v2 data object functions defined in dali/c_api_2/data_objects.h and implemented in dali/c_api_2/data_objects.cc. The tests use the DALI C++ wrappers (TensorHandle, TensorListHandle) for RAII-based handle management while exercising the C function interface.

The TensorList tests cover: null handle error reporting, create/destroy lifecycle with reference counting, resize operations on CPU and GPU with validation of invalid arguments (null shapes, negative dimensions, mismatched layouts), contiguous buffer attachment with custom deleters verifying correct deletion callbacks, per-sample buffer attachment with individual deleters, view-as-tensor conversion for uniform-shaped tensor lists (and error for non-contiguous ones), layout queries, and source info get/set operations.

The Tensor tests cover: null handle error reporting, create/destroy lifecycle, resize on CPU and GPU with argument validation, descriptor retrieval, byte-size queries, and source info management.

The CAPI2_CopyOutTest is a parameterized test that validates daliTensorListCopyOut across all combinations of source device (CPU/GPU), destination device (CPU/GPU), and copy flags (default, use_kernel, sync). It fills a tensor list with sequential values and verifies that the copy produces identical output on the destination device.

Usage

Run these tests to validate any changes to the C API v2 data object implementation, including tensor creation, memory attachment, reference counting, and cross-device copy operations. The tests require a CUDA-capable GPU for GPU-related test cases.

Code Reference

Source Location

Signature

// TensorList tests
TEST(CAPI2_TensorListTest, NullHandle);
TEST(CAPI2_TensorListTest, CreateDestroy);
TEST(CAPI2_TensorListTest, AttachBuffer);
TEST(CAPI2_TensorListTest, AttachSamples);
TEST(CAPI2_TensorListTest, ViewAsTensor);
TEST(CAPI2_TensorListTest, ViewAsTensorError);
TEST(CAPI2_TensorListTest, ResizeCPU);
TEST(CAPI2_TensorListTest, ResizeGPU);
TEST(CAPI2_TensorListTest, SourceInfo);

// Tensor tests
TEST(CAPI2_TensorTest, NullHandle);
TEST(CAPI2_TensorTest, CreateDestroy);
TEST(CAPI2_TensorTest, ResizeCPU);
TEST(CAPI2_TensorTest, ResizeGPU);
TEST(CAPI2_TensorTest, SourceInfo);

// Parameterized copy-out test
class CAPI2_CopyOutTest : public ::testing::TestWithParam<CopyTestParams> {};
TEST_P(CAPI2_CopyOutTest, CopyOut);

// Helper functions
inline auto CreateTensorList(daliBufferPlacement_t placement);
inline auto CreateTensor(daliBufferPlacement_t placement);
void TestTensorListResize(daliStorageDevice_t storage_device);
void TestTensorResize(daliStorageDevice_t storage_device);

Import

#include "dali/c_api_2/data_objects.h"
#include <gtest/gtest.h>
#include "dali/dali_cpp_wrappers.h"
#include "dali/c_api_2/test_utils.h"
#include "dali/test/tensor_test_utils.h"

I/O Contract

Inputs

Name Type Required Description
storage_device daliStorageDevice_t Yes DALI_STORAGE_CPU or DALI_STORAGE_GPU for resize tests
CopyTestParams tuple Yes (source_device, dest_device, copy_flags) for copy-out tests
shapes int64_t[] Yes Hardcoded sample shapes for testing (e.g., 480x640x3)
dtype daliDataType_t Yes Data type for test tensors (e.g., DALI_UINT32, DALI_INT16)

Outputs

Name Type Description
Test assertions GTest EXPECT/ASSERT Validates correct behavior of all tensor/tensor list operations
Deleter callback counts TestDeleterCtx Tracks buffer_delete_count and context_delete_count

Usage Examples

Testing TensorList Resize

daliBufferPlacement_t placement{};
placement.device_type = DALI_STORAGE_CPU;
placement.pinned = true;

auto tl = CreateTensorList(placement);

int64_t shapes[] = {480, 640, 3, 600, 800, 4, 348, 720, 1, 1080, 1920, 3};
CHECK_DALI(daliTensorListResize(tl, 4, 3, shapes, DALI_UINT32, "HWC"));

int nsamples, ndim;
const int64_t *shape_data;
CHECK_DALI(daliTensorListGetShape(tl, &nsamples, &ndim, &shape_data));
EXPECT_EQ(nsamples, 4);
EXPECT_EQ(ndim, 3);

Testing Custom Deleter Callbacks

struct TestDeleterCtx {
  void *expected_data;
  int buffer_delete_count;
  int context_delete_count;
};

auto [deleter, ctx] = MakeTestDeleter(data.get());

auto tl = CreateTensorList(placement);
daliTensorListAttachBuffer(tl, num_samples, ndim, shapes,
                            dtype, "HWC", data.get(), offsets, deleter);

tl.reset();  // triggers destruction

EXPECT_EQ(ctx->buffer_delete_count, 1);
EXPECT_EQ(ctx->context_delete_count, 1);

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment