From 6a5a63f0519e1ca14ee362fd40180105820456ee Mon Sep 17 00:00:00 2001 From: Daniel Morin Date: Tue, 24 Sep 2024 10:53:05 -0400 Subject: [PATCH] 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: --- .../ext/onnx/gstonnxclient.cpp | 2 +- .../gst-libs/gst/analytics/gsttensor.c | 2 +- .../gst-libs/gst/analytics/gsttensor.h | 38 +++++++++++++++---- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/subprojects/gst-plugins-bad/ext/onnx/gstonnxclient.cpp b/subprojects/gst-plugins-bad/ext/onnx/gstonnxclient.cpp index 19d49c69d5..e39f060376 100644 --- a/subprojects/gst-plugins-bad/ext/onnx/gstonnxclient.cpp +++ b/subprojects/gst-plugins-bad/ext/onnx/gstonnxclient.cpp @@ -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 (); diff --git a/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensor.c b/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensor.c index ac69016a82..9101390633 100644 --- a/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensor.c +++ b/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensor.c @@ -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); diff --git a/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensor.h b/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensor.h index cfbee177a9..297c70f931 100644 --- a/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensor.h +++ b/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensor.h @@ -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