analytics: Adding abstraction on tensor dims

Tensor can be row or col major, but it's also possible that the order by we need
to read the tensor with more than two dimension need to be described. The
reserved field in GstTensorDim is there for this purpose. If we need this we
can add  GST_TENSOR_DIM_ORDER_INDEXED, and follow an index defining order for
each dimension.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/6000>
This commit is contained in:
Daniel Morin 2024-09-24 10:53:05 -04:00 committed by GStreamer Marge Bot
parent 8169863f01
commit 6a5a63f051
3 changed files with 32 additions and 10 deletions

View file

@ -353,7 +353,7 @@ GstOnnxClient::GstOnnxClient (GstElement *debug_parent):debug_parent(debug_paren
tensor->batch_size = 1;
for (size_t j = 0; j < tensorShape.size (); ++j)
tensor->dims[j] = tensorShape[j];
tensor->dims[j].size = tensorShape[j];
size_t numElements =
outputTensor.GetTensorTypeAndShapeInfo ().GetElementCount ();

View file

@ -25,7 +25,7 @@
#include "gsttensor.h"
#define GST_TENSOR_SIZE(num_dims) \
(sizeof (GstTensor) + (sizeof (gsize) * num_dims))
(sizeof (GstTensor) + (sizeof (GstTensorDim) * num_dims))
G_DEFINE_BOXED_TYPE (GstTensor, gst_tensor,
(GBoxedCopyFunc) gst_tensor_copy, (GBoxedFreeFunc) gst_tensor_free);

View file

@ -73,15 +73,21 @@ typedef enum _GstTensorDataType
* GstTensorDimOrder:
* @GST_TENSOR_DIM_ORDER_ROW_MAJOR: elements along a row are consecutive in memory
* @GST_TENSOR_DIM_ORDER_COL_MAJOR: elements along a column are consecutive in memory
* @GST_TENSOR_DIM_ORDER_INDEXED: elements storage follow the order defined by
* #GstTensorDim.order_index This mean that when iterating the tensor
* the dimension with index 0 is the most nested in the loops and consecutive
* in memory, followed by other dimensions in the order defined by
* #GstTensorDim.order_index.
*
* Indicate to read tensor from memory in row-major or column-major.
* Indicate to read tensor from memory in row-major or column-major order.
*
* Since: 1.26
*/
typedef enum _GstTensorDimOrder
{
GST_TENSOR_DIM_ORDER_ROW_MAJOR,
GST_TENSOR_DIM_ORDER_COL_MAJOR
GST_TENSOR_DIM_ORDER_COL_MAJOR,
GST_TENSOR_DIM_ORDER_INDEXED
} GstTensorDimOrder;
/**
@ -97,16 +103,32 @@ typedef enum _GstTensorLayout
GST_TENSOR_LAYOUT_STRIDED
} GstTensorLayout;
/**
* GstTensorDim:
* @size: Size of the dimension
* @order_index: Dimension order in memory. @see_also #GST_TENSOR_DIM_ORDER_INDEXED
*
* Hold properties of the tensor's dimension
*
* Since: 1.26
*/
typedef struct _GstTensorDim
{
gsize size;
gsize order_index;
} GstTensorDim;
/**
* GstTensor:
* @id: semantically identify the contents of the tensor
* @num_dims: number of tensor dimensions
* @dims: tensor dimensions
* @dims_order: Indicate tensor elements layout in memory.
* @layout: Indicate tensor layout
* @type: #GstTensorDataType of tensor data
* @data_type: #GstTensorDataType of tensor data
* @batch_size: Model batch size
* @data: #GstBuffer holding tensor data
* @dims_order: Indicate tensor elements layout in memory.
* @num_dims: number of tensor dimensions
* @dims: (array length=num_dims): number of tensor dimensions
*
* Hold tensor data
*
@ -115,13 +137,13 @@ typedef enum _GstTensorLayout
typedef struct _GstTensor
{
GQuark id;
GstTensorDimOrder dims_order;
GstTensorLayout layout;
GstTensorDataType data_type;
gsize batch_size;
GstBuffer *data;
GstTensorDimOrder dims_order;
gsize num_dims;
gsize dims[];
GstTensorDim dims[];
} GstTensor;
G_BEGIN_DECLS