From 7c925eae610800d49d3b1b134159fbe33b50d6f8 Mon Sep 17 00:00:00 2001 From: Daniel Morin Date: Wed, 17 Jul 2024 14:39:42 -0400 Subject: [PATCH] analytics: Move batch to GstTensor - batch_size is required to interpret the tensor depending on the tensor format the batch are not necessarily memory plane therefore it's preferable to keep it inside GstTensor. Part-of: --- .../ext/onnx/gstonnxclient.cpp | 3 +- .../ext/onnx/gstonnxinference.cpp | 1 - .../gst-libs/gst/analytics/gsttensor.h | 38 ++++++++++++++++++- .../gst-libs/gst/analytics/gsttensormeta.h | 1 - 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/subprojects/gst-plugins-bad/ext/onnx/gstonnxclient.cpp b/subprojects/gst-plugins-bad/ext/onnx/gstonnxclient.cpp index 63f2bd5307..b83a30de0f 100644 --- a/subprojects/gst-plugins-bad/ext/onnx/gstonnxclient.cpp +++ b/subprojects/gst-plugins-bad/ext/onnx/gstonnxclient.cpp @@ -348,7 +348,8 @@ GstOnnxClient::GstOnnxClient (GstElement *debug_parent):debug_parent(debug_paren tensor->id = 0; auto tensorShape = outputTensor.GetTensorTypeAndShapeInfo ().GetShape (); tensor->num_dims = tensorShape.size (); - tensor->dims = g_new (int64_t, tensor->num_dims); + tensor->dims = g_new (gsize, tensor->num_dims); + tensor->batch_size = 1; for (size_t j = 0; j < tensorShape.size (); ++j) tensor->dims[j] = tensorShape[j]; diff --git a/subprojects/gst-plugins-bad/ext/onnx/gstonnxinference.cpp b/subprojects/gst-plugins-bad/ext/onnx/gstonnxinference.cpp index a271a736b7..d53a1d8872 100644 --- a/subprojects/gst-plugins-bad/ext/onnx/gstonnxinference.cpp +++ b/subprojects/gst-plugins-bad/ext/onnx/gstonnxinference.cpp @@ -587,7 +587,6 @@ gst_onnx_inference_process (GstBaseTransform * trans, GstBuffer * buf) if (!meta) return FALSE; GST_TRACE_OBJECT (trans, "Num tensors:%zu", meta->num_tensors); - meta->batch_size = 1; } catch (Ort::Exception & ortex) { GST_ERROR_OBJECT (self, "%s", ortex.what ()); 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 32b89177be..f1d5466a0c 100644 --- a/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensor.h +++ b/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensor.h @@ -69,12 +69,43 @@ typedef enum _GstTensorDataType GST_TENSOR_TYPE_BFLOAT16, } 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 + * + * Indicate to read tensor from memory in row-major or column-major. + * + * Since: 1.26 + */ +typedef enum _GstTensorDimOrder +{ + GST_TENSOR_DIM_ORDER_ROW_MAJOR, + GST_TENSOR_DIM_ORDER_COL_MAJOR +} GstTensorDimOrder; + +/** + * GstTensorLayout: + * @GST_TENSOR_LAYOUT_STRIDED: indicate the tensor is stored in a dense format in memory + * + * Indicate tensor storage in memory. + * + * Since: 1.26 + */ +typedef enum _GstTensorLayout +{ + GST_TENSOR_LAYOUT_STRIDED +} GstTensorLayout; + /** * 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 + * @batch_size: Model batch size * @data: #GstBuffer holding tensor data * * Hold tensor data @@ -84,9 +115,12 @@ typedef enum _GstTensorDataType typedef struct _GstTensor { GQuark id; - gint num_dims; - int64_t *dims; + gsize num_dims; + gsize *dims; + GstTensorDimOrder dims_order; + GstTensorLayout layout; GstTensorDataType data_type; + gsize batch_size; GstBuffer *data; } GstTensor; diff --git a/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensormeta.h b/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensormeta.h index a458df3aa8..24298b1547 100644 --- a/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensormeta.h +++ b/subprojects/gst-plugins-bad/gst-libs/gst/analytics/gsttensormeta.h @@ -46,7 +46,6 @@ typedef struct _GstTensorMeta gsize num_tensors; GstTensor *tensor; - gsize batch_size; } GstTensorMeta; G_BEGIN_DECLS