Implementation:NVIDIA DALI TensorShape Tests
| Knowledge Sources | |
|---|---|
| Domains | Core, Testing |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
This file contains comprehensive Google Test suites for the TensorShape and TensorListShape classes, covering static-dimension, dynamic-dimension, and mixed-dimension operations.
Description
The TensorShape and TensorListShape test suite validates DALI's core tensor shape abstraction layer. TensorShape<N> represents a shape with a compile-time-known number of dimensions (static), while TensorShape<DynamicDimensions> uses a runtime-determined dimensionality. The tests ensure both variants behave consistently and can interoperate.
The test file is organized into several categories: constructors (default, from arrays, from vectors, expanded argument lists, copy, move), comparisons (static-vs-static, dynamic-vs-dynamic, mixed), sub-shape extraction (first/last with both static template and dynamic integer parameters), shape concatenation (shape_cat), volume computation, dimension collapsing (collapse_dims, collapse_dim), uniform list shape creation, degenerate dimension detection, sample permutation, dimension permutation, appending TensorListShape objects, sample range extraction, and unfolding outer dimensions.
These tests span 1365 lines and provide the primary correctness validation for the shape algebra that underpins all of DALI's tensor processing. Static assertions at the top of the file also verify compile-time size inference traits such as compile_time_size_impl.
Usage
Use these tests as the authoritative specification for TensorShape and TensorListShape behavior. Run the test binary after modifying any shape-related utility in dali/core/tensor_shape.h to ensure correctness. These tests are also useful as examples of how to construct, compare, slice, and transform tensor shapes.
Code Reference
Source Location
- Repository: NVIDIA_DALI
- File: dali/core/tensor_shape_test.cc
- Lines: 1-1365
Signature
// Core types under test
template <int ndim>
class TensorShape; // static or dynamic (ndim == DynamicDimensions)
template <int ndim>
class TensorListShape;
// Key free functions tested
template <int ndim>
TensorShape<ndim> shape_cat(const TensorShape<A> &a, const TensorShape<B> &b);
int64_t volume(const Container &shape);
std::vector<int64_t> flatten_shapes(const std::vector<TensorShape<N>> &shapes);
std::vector<T*> calculate_pointers(T *base, const TensorListShape<N> &tls);
TensorShape<> collapse_dims(const TensorShape<> &shape, span<const pair<int,int>> groups);
void collapse_dim(TensorListShape<out> &out, const TensorListShape<in> &in, int dim);
bool is_uniform(const TensorListShape<N> &tls);
bool is_degenerate_dim(const TensorListShape<> &tls, int dim);
TensorListShape<out> permute_samples(const TensorListShape<in> &tls, const int *perm);
TensorListShape<N> permute_dims(const TensorListShape<N> &tls, const int *perm);
TensorListShape<> sample_range(const TensorListShape<> &tls, int start, int end, int step = 1);
TensorListShape<> unfold_outer_dim(const TensorListShape<N> &tls);
Import
#include <gtest/gtest.h>
#include "dali/core/tensor_shape.h"
#include "dali/core/tensor_view.h"
#include "dali/core/tensor_shape_print.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| N/A | N/A | N/A | Test file has no runtime inputs; shapes are constructed inline within each TEST macro |
Outputs
| Name | Type | Description |
|---|---|---|
| Test results | Google Test assertions | PASS/FAIL verdicts for each test case validating TensorShape and TensorListShape behavior |
Usage Examples
Static and dynamic shape construction
// Static shape
TensorShape<5> static_shape(1, 2, 3, 4, 5);
// Dynamic shape from vector
std::vector<int64_t> dims = {1, 2, 3, 4, 5};
TensorShape<DynamicDimensions> dynamic_shape(dims.begin(), dims.end());
// Static-to-dynamic conversion
TensorShape<DynamicDimensions> converted(static_shape);
auto back_to_static = converted.to_static<5>();
Sub-shape extraction with first/last
TensorShape<5> ts(1, 2, 3, 4, 5);
auto first3 = ts.first<3>(); // TensorShape<3>(1, 2, 3)
auto last2 = ts.last<2>(); // TensorShape<2>(4, 5)
auto first_dyn = ts.first(3); // TensorShape<3>(1, 2, 3) via runtime parameter
Shape concatenation
auto result = shape_cat(TensorShape<2>(1, 2), TensorShape<3>(1, 2, 3));
// result == TensorShape<5>(1, 2, 1, 2, 3)
TensorListShape operations
TensorListShape<3> tls({{1, 2, 3}, {2, 3, 4}, {3, 4, 5}});
EXPECT_EQ(tls.size(), 3);
EXPECT_TRUE(is_uniform(uniform_list_shape<3>(4, {640, 480, 3})));
// Collapse dimensions
TensorListShape<> collapsed = collapse_dims(tls, { { 0, 2 }, { 2, 1 } });
// Permute samples
int perm[] = { 2, 0, 1 };
auto permuted = permute_samples<2>(tls, perm);