|
| 1 | +/* SPDX-License-Identifier: LGPL-2.1-only */ |
| 2 | +/** |
| 3 | + * GStreamer / NNStreamer Sparse Tensor support |
| 4 | + * Copyright (C) 2021 Yongjoo Ahn <yongjoo1.ahn@samsung.com> |
| 5 | + */ |
| 6 | +/** |
| 7 | + * @file tensor_sparse_util.c |
| 8 | + * @date 27 Jul 2021 |
| 9 | + * @brief Util functions for tensor_sparse encoder and decoder. |
| 10 | + * @see https://github.com/nnstreamer/nnstreamer |
| 11 | + * @author Yongjoo Ahn <yongjoo1.ahn@samsung.com> |
| 12 | + * @bug No known bugs except for NYI items |
| 13 | + */ |
| 14 | + |
| 15 | +#include <tensor_common.h> |
| 16 | +#include <tensor_data.h> |
| 17 | +#include "tensor_sparse_util.h" |
| 18 | + |
| 19 | +/** |
| 20 | + * @brief Make dense tensor with input sparse tensor. |
| 21 | + * @param[in,out] meta tensor meta structure to be updated |
| 22 | + * @param[in] in pointer of input sparse tensor data |
| 23 | + * @return pointer of GstMemory with dense tensor data or NULL on error. Caller should handle this newly allocated memory. |
| 24 | + */ |
| 25 | +GstMemory * |
| 26 | +gst_tensor_sparse_to_dense (GstTensorMetaInfo * meta, gpointer in) |
| 27 | +{ |
| 28 | + guint i, nnz; |
| 29 | + guint8 *output, *input; |
| 30 | + guint *indices; |
| 31 | + gsize output_size, element_size; |
| 32 | + |
| 33 | + meta->format = _NNS_TENSOR_FORMAT_STATIC; |
| 34 | + |
| 35 | + element_size = gst_tensor_get_element_size (meta->type); |
| 36 | + output_size = gst_tensor_meta_info_get_data_size (meta); |
| 37 | + |
| 38 | + if (element_size == 0 || output_size == 0) { |
| 39 | + nns_loge ("Got invalid meta info"); |
| 40 | + return NULL; |
| 41 | + } |
| 42 | + |
| 43 | + output = (guint8 *) g_malloc0 (output_size); |
| 44 | + |
| 45 | + nnz = meta->sparse_info.nnz; |
| 46 | + input = (guint8 *) in + gst_tensor_meta_info_get_header_size (meta); |
| 47 | + indices = ((guint *) ((guint8 *) input + element_size * nnz)); |
| 48 | + |
| 49 | + for (i = 0; i < nnz; ++i) { |
| 50 | + switch (meta->type) { |
| 51 | + case _NNS_INT32: |
| 52 | + ((int32_t *) output)[indices[i]] = ((int32_t *) input)[i]; |
| 53 | + break; |
| 54 | + case _NNS_UINT32: |
| 55 | + ((uint32_t *) output)[indices[i]] = ((uint32_t *) input)[i]; |
| 56 | + break; |
| 57 | + case _NNS_INT16: |
| 58 | + ((int16_t *) output)[indices[i]] = ((int16_t *) input)[i]; |
| 59 | + break; |
| 60 | + case _NNS_UINT16: |
| 61 | + ((uint16_t *) output)[indices[i]] = ((uint16_t *) input)[i]; |
| 62 | + break; |
| 63 | + case _NNS_INT8: |
| 64 | + ((int8_t *) output)[indices[i]] = ((int8_t *) input)[i]; |
| 65 | + break; |
| 66 | + case _NNS_UINT8: |
| 67 | + ((uint8_t *) output)[indices[i]] = ((uint8_t *) input)[i]; |
| 68 | + break; |
| 69 | + case _NNS_FLOAT64: |
| 70 | + ((double *) output)[indices[i]] = ((double *) input)[i]; |
| 71 | + break; |
| 72 | + case _NNS_FLOAT32: |
| 73 | + ((float *) output)[indices[i]] = ((float *) input)[i]; |
| 74 | + break; |
| 75 | + case _NNS_INT64: |
| 76 | + ((int64_t *) output)[indices[i]] = ((int64_t *) input)[i]; |
| 77 | + break; |
| 78 | + case _NNS_UINT64: |
| 79 | + ((uint64_t *) output)[indices[i]] = ((uint64_t *) input)[i]; |
| 80 | + break; |
| 81 | + default: |
| 82 | + nns_loge ("Error occured during get tensor value"); |
| 83 | + g_free (output); |
| 84 | + |
| 85 | + return NULL; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return gst_memory_new_wrapped (0, output, output_size, 0, |
| 90 | + output_size, output, g_free); |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * @brief Make sparse tensor with input dense tensor. |
| 95 | + * @param[in,out] meta tensor meta structure to be updated |
| 96 | + * @param[in] in pointer of input dense tensor data |
| 97 | + * @return pointer of GstMemory with sparse tensor data or NULL on error. Caller should handle this newly allocated memory. |
| 98 | + */ |
| 99 | +GstMemory * |
| 100 | +gst_tensor_sparse_from_dense (GstTensorMetaInfo * meta, gpointer in) |
| 101 | +{ |
| 102 | + guint i, nnz = 0; |
| 103 | + guint8 *output; |
| 104 | + tensor_type data_type; |
| 105 | + void *values; |
| 106 | + guint *indices; |
| 107 | + gsize output_size; |
| 108 | + gsize header_size = gst_tensor_meta_info_get_header_size (meta); |
| 109 | + gsize element_size = gst_tensor_get_element_size (meta->type); |
| 110 | + gulong element_count = gst_tensor_get_element_count (meta->dimension); |
| 111 | + |
| 112 | + if (element_size == 0 || element_count == 0) { |
| 113 | + nns_loge ("Got invalid meta info"); |
| 114 | + return NULL; |
| 115 | + } |
| 116 | + |
| 117 | + /** alloc maximum possible size of memory */ |
| 118 | + values = g_malloc0 (element_size * element_count); |
| 119 | + indices = g_malloc0 (sizeof (guint) * element_count); |
| 120 | + |
| 121 | + data_type = meta->type; |
| 122 | + |
| 123 | + /** Consider using macro to reduce loc and readability */ |
| 124 | + for (i = 0; i < element_count; ++i) { |
| 125 | + switch (data_type) { |
| 126 | + case _NNS_INT32: |
| 127 | + if (((int32_t *) in)[i] != 0) { |
| 128 | + ((int32_t *) values)[nnz] = ((int32_t *) in)[i]; |
| 129 | + indices[nnz] = i; |
| 130 | + nnz += 1; |
| 131 | + } |
| 132 | + break; |
| 133 | + case _NNS_UINT32: |
| 134 | + if (((uint32_t *) in)[i] != 0) { |
| 135 | + ((uint32_t *) values)[nnz] = ((uint32_t *) in)[i]; |
| 136 | + indices[nnz] = i; |
| 137 | + nnz += 1; |
| 138 | + } |
| 139 | + break; |
| 140 | + case _NNS_INT16: |
| 141 | + if (((int16_t *) in)[i] != 0) { |
| 142 | + ((int16_t *) values)[nnz] = ((int16_t *) in)[i]; |
| 143 | + indices[nnz] = i; |
| 144 | + nnz += 1; |
| 145 | + } |
| 146 | + break; |
| 147 | + case _NNS_UINT16: |
| 148 | + if (((uint16_t *) in)[i] != 0) { |
| 149 | + ((uint16_t *) values)[nnz] = ((uint16_t *) in)[i]; |
| 150 | + indices[nnz] = i; |
| 151 | + nnz += 1; |
| 152 | + } |
| 153 | + break; |
| 154 | + case _NNS_INT8: |
| 155 | + if (((int8_t *) in)[i] != 0) { |
| 156 | + ((int8_t *) values)[nnz] = ((int8_t *) in)[i]; |
| 157 | + indices[nnz] = i; |
| 158 | + nnz += 1; |
| 159 | + } |
| 160 | + break; |
| 161 | + case _NNS_UINT8: |
| 162 | + if (((uint8_t *) in)[i] != 0) { |
| 163 | + ((uint8_t *) values)[nnz] = ((uint8_t *) in)[i]; |
| 164 | + indices[nnz] = i; |
| 165 | + nnz += 1; |
| 166 | + } |
| 167 | + break; |
| 168 | + case _NNS_FLOAT64: |
| 169 | + if (((double *) in)[i] != 0) { |
| 170 | + ((double *) values)[nnz] = ((double *) in)[i]; |
| 171 | + indices[nnz] = i; |
| 172 | + nnz += 1; |
| 173 | + } |
| 174 | + break; |
| 175 | + case _NNS_FLOAT32: |
| 176 | + if (((float *) in)[i] != 0) { |
| 177 | + ((float *) values)[nnz] = ((float *) in)[i]; |
| 178 | + indices[nnz] = i; |
| 179 | + nnz += 1; |
| 180 | + } |
| 181 | + break; |
| 182 | + case _NNS_INT64: |
| 183 | + if (((int64_t *) in)[i] != 0) { |
| 184 | + ((int64_t *) values)[nnz] = ((int64_t *) in)[i]; |
| 185 | + indices[nnz] = i; |
| 186 | + nnz += 1; |
| 187 | + } |
| 188 | + break; |
| 189 | + case _NNS_UINT64: |
| 190 | + if (((uint64_t *) in)[i] != 0) { |
| 191 | + ((uint64_t *) values)[nnz] = ((uint64_t *) in)[i]; |
| 192 | + indices[nnz] = i; |
| 193 | + nnz += 1; |
| 194 | + } |
| 195 | + break; |
| 196 | + default: |
| 197 | + nns_loge ("Error occured during get tensor value"); |
| 198 | + g_free (values); |
| 199 | + g_free (indices); |
| 200 | + |
| 201 | + return NULL; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + /** update meta nnz info */ |
| 206 | + meta->sparse_info.nnz = nnz; |
| 207 | + |
| 208 | + /** write to output buffer */ |
| 209 | + output_size = element_size * nnz + sizeof (guint) * nnz; |
| 210 | + |
| 211 | + /** add meta info header */ |
| 212 | + output_size += header_size; |
| 213 | + output = g_malloc0 (output_size); |
| 214 | + |
| 215 | + gst_tensor_meta_info_update_header (meta, output); |
| 216 | + |
| 217 | + memcpy (output + header_size, values, element_size * nnz); |
| 218 | + memcpy (output + header_size + (element_size * nnz), |
| 219 | + indices, sizeof (guint) * nnz); |
| 220 | + |
| 221 | + g_free (values); |
| 222 | + g_free (indices); |
| 223 | + |
| 224 | + return gst_memory_new_wrapped (0, output, output_size, 0, |
| 225 | + output_size, output, g_free); |
| 226 | +} |
0 commit comments