Doxygen Book
gsttensor_sparseutil.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: LGPL-2.1-only */
15 #include <string.h>
16 #include <tensor_common.h>
17 #include <tensor_data.h>
18 #include "gsttensor_sparseutil.h"
19 
26 GstMemory *
28 {
29  GstMemory *dense = NULL;
30  GstMapInfo map;
31  guint i, nnz;
32  guint8 *output, *input;
33  guint *indices;
34  gsize output_size, element_size;
35 
36  if (!gst_memory_map (mem, &map, GST_MAP_READ)) {
37  nns_loge ("Failed to map given memory");
38  return NULL;
39  }
40 
41  if (!gst_tensor_meta_info_parse_header (meta, map.data)) {
42  nns_loge ("Failed to parse meta info from given memory");
43  goto done;
44  }
45 
47 
48  element_size = gst_tensor_get_element_size (meta->type);
49  output_size = gst_tensor_meta_info_get_data_size (meta);
50 
51  if (element_size == 0 || output_size == 0) {
52  nns_loge ("Got invalid meta info");
53  goto done;
54  }
55 
56  output = (guint8 *) g_malloc0 (output_size);
57 
58  nnz = meta->sparse_info.nnz;
59  input = map.data + gst_tensor_meta_info_get_header_size (meta);
60  indices = (guint *) (input + element_size * nnz);
61 
62  for (i = 0; i < nnz; ++i) {
63  switch (meta->type) {
64  case _NNS_INT32:
65  ((int32_t *) output)[indices[i]] = ((int32_t *) input)[i];
66  break;
67  case _NNS_UINT32:
68  ((uint32_t *) output)[indices[i]] = ((uint32_t *) input)[i];
69  break;
70  case _NNS_INT16:
71  ((int16_t *) output)[indices[i]] = ((int16_t *) input)[i];
72  break;
73  case _NNS_UINT16:
74  ((uint16_t *) output)[indices[i]] = ((uint16_t *) input)[i];
75  break;
76  case _NNS_INT8:
77  ((int8_t *) output)[indices[i]] = ((int8_t *) input)[i];
78  break;
79  case _NNS_UINT8:
80  ((uint8_t *) output)[indices[i]] = ((uint8_t *) input)[i];
81  break;
82  case _NNS_FLOAT64:
83  ((double *) output)[indices[i]] = ((double *) input)[i];
84  break;
85  case _NNS_FLOAT32:
86  ((float *) output)[indices[i]] = ((float *) input)[i];
87  break;
88  case _NNS_INT64:
89  ((int64_t *) output)[indices[i]] = ((int64_t *) input)[i];
90  break;
91  case _NNS_UINT64:
92  ((uint64_t *) output)[indices[i]] = ((uint64_t *) input)[i];
93  break;
94  default:
95  nns_loge ("Error occurred during get tensor value");
96  g_free (output);
97  goto done;
98  }
99  }
100 
101  dense = gst_memory_new_wrapped (0, output, output_size, 0, output_size,
102  output, g_free);
103 
104 done:
105  gst_memory_unmap (mem, &map);
106  return dense;
107 }
108 
115 GstMemory *
117 {
118  GstMemory *sparse = NULL;
119  GstMapInfo map;
120  guint i, nnz = 0;
121  guint8 *output;
122  tensor_type data_type;
123  void *values;
124  guint *indices;
125  gsize output_size, header_size, element_size;
126  gulong element_count;
127 
128  if (!gst_memory_map (mem, &map, GST_MAP_READ)) {
129  nns_loge ("Failed to map given memory");
130  return NULL;
131  }
132 
133  header_size = gst_tensor_meta_info_get_header_size (meta);
134  element_size = gst_tensor_get_element_size (meta->type);
135  element_count = gst_tensor_get_element_count (meta->dimension);
136 
137  if (element_size == 0 || element_count == 0) {
138  nns_loge ("Got invalid meta info");
139  goto done;
140  }
141 
143  values = g_malloc0 (element_size * element_count);
144  indices = g_malloc0 (sizeof (guint) * element_count);
145 
146  data_type = meta->type;
147 
149  for (i = 0; i < element_count; ++i) {
150  switch (data_type) {
151  case _NNS_INT32:
152  if (((int32_t *) map.data)[i] != 0) {
153  ((int32_t *) values)[nnz] = ((int32_t *) map.data)[i];
154  indices[nnz] = i;
155  nnz += 1;
156  }
157  break;
158  case _NNS_UINT32:
159  if (((uint32_t *) map.data)[i] != 0) {
160  ((uint32_t *) values)[nnz] = ((uint32_t *) map.data)[i];
161  indices[nnz] = i;
162  nnz += 1;
163  }
164  break;
165  case _NNS_INT16:
166  if (((int16_t *) map.data)[i] != 0) {
167  ((int16_t *) values)[nnz] = ((int16_t *) map.data)[i];
168  indices[nnz] = i;
169  nnz += 1;
170  }
171  break;
172  case _NNS_UINT16:
173  if (((uint16_t *) map.data)[i] != 0) {
174  ((uint16_t *) values)[nnz] = ((uint16_t *) map.data)[i];
175  indices[nnz] = i;
176  nnz += 1;
177  }
178  break;
179  case _NNS_INT8:
180  if (((int8_t *) map.data)[i] != 0) {
181  ((int8_t *) values)[nnz] = ((int8_t *) map.data)[i];
182  indices[nnz] = i;
183  nnz += 1;
184  }
185  break;
186  case _NNS_UINT8:
187  if (((uint8_t *) map.data)[i] != 0) {
188  ((uint8_t *) values)[nnz] = ((uint8_t *) map.data)[i];
189  indices[nnz] = i;
190  nnz += 1;
191  }
192  break;
193  case _NNS_FLOAT64:
194  if (((double *) map.data)[i] != 0) {
195  ((double *) values)[nnz] = ((double *) map.data)[i];
196  indices[nnz] = i;
197  nnz += 1;
198  }
199  break;
200  case _NNS_FLOAT32:
201  if (((float *) map.data)[i] != 0) {
202  ((float *) values)[nnz] = ((float *) map.data)[i];
203  indices[nnz] = i;
204  nnz += 1;
205  }
206  break;
207  case _NNS_INT64:
208  if (((int64_t *) map.data)[i] != 0) {
209  ((int64_t *) values)[nnz] = ((int64_t *) map.data)[i];
210  indices[nnz] = i;
211  nnz += 1;
212  }
213  break;
214  case _NNS_UINT64:
215  if (((uint64_t *) map.data)[i] != 0) {
216  ((uint64_t *) values)[nnz] = ((uint64_t *) map.data)[i];
217  indices[nnz] = i;
218  nnz += 1;
219  }
220  break;
221  default:
222  nns_loge ("Error occurred during get tensor value");
223  g_free (values);
224  g_free (indices);
225  goto done;
226  }
227  }
228 
231  meta->sparse_info.nnz = nnz;
232 
234  output_size = element_size * nnz + sizeof (guint) * nnz;
235 
237  output_size += header_size;
238  output = g_malloc0 (output_size);
239 
240  gst_tensor_meta_info_update_header (meta, output);
241 
242  memcpy (output + header_size, values, element_size * nnz);
243  memcpy (output + header_size + (element_size * nnz),
244  indices, sizeof (guint) * nnz);
245 
246  g_free (values);
247  g_free (indices);
248 
249  sparse = gst_memory_new_wrapped (0, output, output_size, 0, output_size,
250  output, g_free);
251 
252 done:
253  gst_memory_unmap (mem, &map);
254  return sparse;
255 }
gst_tensor_sparse_from_dense
GstMemory * gst_tensor_sparse_from_dense(GstTensorMetaInfo *meta, GstMemory *mem)
Make sparse tensor with input dense tensor.
Definition: gsttensor_sparseutil.c:116
tensor_data.h
Internal functions to handle various tensor type and value.
_NNS_UINT64
@ _NNS_UINT64
Definition: tensor_typedef.h:149
_NNS_INT64
@ _NNS_INT64
Definition: tensor_typedef.h:148
gst_tensor_meta_info_update_header
gboolean gst_tensor_meta_info_update_header(GstTensorMetaInfo *meta, gpointer header)
Update header from tensor meta.
Definition: nnstreamer_plugin_api_util_impl.c:1505
GstTensorMetaInfo::sparse_info
GstSparseTensorInfo sparse_info
Definition: tensor_typedef.h:323
_NNS_UINT16
@ _NNS_UINT16
Definition: tensor_typedef.h:143
gst_tensor_meta_info_get_data_size
gsize gst_tensor_meta_info_get_data_size(GstTensorMetaInfo *meta)
Get the data size calculated from tensor meta.
Definition: nnstreamer_plugin_api_util_impl.c:1477
GstTensorMetaInfo
Data structure to describe a tensor data. This represents the basic information of a memory block for...
Definition: tensor_typedef.h:310
GstTensorMetaInfo::type
uint32_t type
Definition: tensor_typedef.h:314
GstSparseTensorInfo::nnz
uint32_t nnz
Definition: tensor_typedef.h:296
g_free
g_free(self->option[(opnum) - 1])
opnum: \
tensor_type
enum _nns_tensor_type tensor_type
Possible data element types of other/tensor.
gst_tensor_meta_info_parse_header
gboolean gst_tensor_meta_info_parse_header(GstTensorMetaInfo *meta, gpointer header)
Parse header and fill the tensor meta.
Definition: nnstreamer_plugin_api_util_impl.c:1527
gst_tensor_get_element_count
gulong gst_tensor_get_element_count(const tensor_dim dim)
Count the number of elements of a tensor.
Definition: nnstreamer_plugin_api_util_impl.c:1186
gst_tensor_get_element_size
gsize gst_tensor_get_element_size(tensor_type type)
Get element size of tensor type (byte per element)
Definition: nnstreamer_plugin_api_util_impl.c:1205
gsttensor_sparseutil.h
Util functions for tensor_sparse encoder and decoder.
_NNS_FLOAT32
@ _NNS_FLOAT32
Definition: tensor_typedef.h:147
_NNS_TENSOR_FORMAT_STATIC
@ _NNS_TENSOR_FORMAT_STATIC
Definition: tensor_typedef.h:195
_NNS_TENSOR_FORMAT_SPARSE
@ _NNS_TENSOR_FORMAT_SPARSE
Definition: tensor_typedef.h:197
GstTensorMetaInfo::dimension
tensor_dim dimension
Definition: tensor_typedef.h:315
_NNS_INT32
@ _NNS_INT32
Definition: tensor_typedef.h:140
nns_loge
#define nns_loge
Definition: nnstreamer_log.h:142
tensor_common.h
Common header file for NNStreamer, the GStreamer plugin for neural networks.
_NNS_INT16
@ _NNS_INT16
Definition: tensor_typedef.h:142
GstTensorMetaInfo::format
uint32_t format
Definition: tensor_typedef.h:316
_NNS_FLOAT64
@ _NNS_FLOAT64
Definition: tensor_typedef.h:146
_NNS_UINT32
@ _NNS_UINT32
Definition: tensor_typedef.h:141
_NNS_INT8
@ _NNS_INT8
Definition: tensor_typedef.h:144
gst_tensor_sparse_to_dense
GstMemory * gst_tensor_sparse_to_dense(GstTensorMetaInfo *meta, GstMemory *mem)
Make dense tensor with input sparse tensor.
Definition: gsttensor_sparseutil.c:27
_NNS_UINT8
@ _NNS_UINT8
Definition: tensor_typedef.h:145
gst_tensor_meta_info_get_header_size
gsize gst_tensor_meta_info_get_header_size(GstTensorMetaInfo *meta)
Get the header size to handle a tensor meta.
Definition: nnstreamer_plugin_api_util_impl.c:1456