source: src/ai/tensor.c @ f90051d

feature/cnnfeature/crepe
Last change on this file since f90051d was f90051d, checked in by Paul Brossier <piem@piem.org>, 2 years ago

[tensor] add have_same_size

  • Property mode set to 100644
File size: 3.5 KB
Line 
1#include "aubio_priv.h"
2#include "fmat.h"
3#include "tensor.h"
4
5aubio_tensor_t *new_aubio_tensor(uint_t ndim, uint_t *shape)
6{
7  aubio_tensor_t *c = AUBIO_NEW(aubio_tensor_t);
8  uint_t items_per_row = 1;
9  uint_t i;
10
11  if ((sint_t)ndim <= 0) goto failure;
12  for (i = 0; i < ndim; i++) {
13    if ((sint_t)shape[i] <= 0) goto failure;
14  }
15
16  c->ndim = ndim;
17  c->shape[0] = shape[0];
18  for (i = 1; i < ndim; i++) {
19    c->shape[i] = shape[i];
20    items_per_row *= shape[i];
21  }
22  c->size = items_per_row * shape[0];
23  c->buffer = AUBIO_ARRAY(smpl_t, c->size);
24  c->data = AUBIO_ARRAY(smpl_t*, shape[0]);
25  for (i = 0; i < c->shape[0]; i++) {
26    c->data[i] = c->buffer + i * items_per_row;
27  }
28
29  return c;
30
31failure:
32  del_aubio_tensor(c);
33  return NULL;
34}
35
36void del_aubio_tensor(aubio_tensor_t *c)
37{
38  if (c->data) {
39    if (c->data[0]) {
40      AUBIO_FREE(c->data[0]);
41    }
42    AUBIO_FREE(c->data);
43  }
44  AUBIO_FREE(c);
45}
46
47uint_t aubio_tensor_as_fvec(aubio_tensor_t *c, fvec_t *o) {
48  if (!c || !o) return AUBIO_FAIL;
49  o->length = c->size;
50  o->data = c->data[0];
51  return AUBIO_OK;
52}
53
54uint_t aubio_fvec_as_tensor(fvec_t *o, aubio_tensor_t *c) {
55  if (!o || !c) return AUBIO_FAIL;
56  c->ndim = 1;
57  c->shape[0] = o->length;
58  c->data = &o->data;
59  c->buffer = o->data;
60  c->size = o->length;
61  return AUBIO_OK;
62}
63
64uint_t aubio_tensor_as_fmat(aubio_tensor_t *c, fmat_t *o) {
65  if (!c || !o) return AUBIO_FAIL;
66  o->height = c->shape[0];
67  o->length = c->size / c->shape[0];
68  o->data = c->data;
69  return AUBIO_OK;
70}
71
72uint_t aubio_fmat_as_tensor(fmat_t *o, aubio_tensor_t *c) {
73  if (!o || !c) return AUBIO_FAIL;
74  c->ndim = 2;
75  c->shape[0] = o->height;
76  c->shape[1] = o->length;
77  c->size = o->height * o->length;
78  c->data = o->data;
79  c->buffer = o->data[0];
80  return AUBIO_OK;
81}
82
83uint_t aubio_tensor_get_subtensor(aubio_tensor_t *t, uint_t i,
84    aubio_tensor_t *st)
85{
86  uint_t j;
87  if (!t || !st) return AUBIO_FAIL;
88  if (i >= t->shape[0]) {
89    AUBIO_ERR("tensor: index %d out of range, only %d subtensors\n",
90        i, t->shape[0]);
91    return AUBIO_FAIL;
92  }
93  if(t->ndim > 1) {
94    st->ndim = t->ndim - 1;
95    for (j = 0; j < st->ndim; j++) {
96      st->shape[j] = t->shape[j + 1];
97    }
98    for (j = st->ndim; j < AUBIO_TENSOR_MAXDIM; j++) {
99      st->shape[j] = 0;
100    }
101    st->size = t->size / t->shape[0];
102  } else {
103    st->ndim = 1;
104    st->shape[0] = 1;
105    st->size = 1;
106  }
107  // st was allocated on the stack, row indices are lost
108  st->data = NULL;
109  st->buffer = &t->buffer[0] + st->size * i;
110  return AUBIO_OK;
111}
112
113uint_t aubio_tensor_have_same_size(aubio_tensor_t *t, aubio_tensor_t *s)
114{
115  uint_t n;
116  if (!t || !s) return 0;
117  if (t->ndim != s->ndim) return 0;
118  if (t->size != s->size) return 0;
119  n = t->ndim;
120  while (n--) {
121    if (t->shape[n] != s->shape[n]) {
122      return 0;
123    }
124  }
125  return 1;
126}
127
128smpl_t aubio_tensor_max(aubio_tensor_t *t)
129{
130  uint_t i;
131  smpl_t max = t->buffer[0];
132  for (i = 0; i < t->size; i++) {
133    max = MAX(t->buffer[i], max);
134  }
135  return max;
136}
137
138const char_t *aubio_tensor_get_shape_string(aubio_tensor_t *t) {
139  uint_t i;
140  if (!t) return NULL;
141  size_t offset = 2;
142  static char_t shape_str[STRN_LENGTH];
143  char_t shape_str_previous[STRN_LENGTH] = "(";
144  for (i = 0; i < t->ndim; i++) {
145    int len = snprintf(shape_str, STRN_LENGTH, "%s%d%s",
146        shape_str_previous, t->shape[i], (i == t->ndim - 1) ? "" : ", ");
147    strncpy(shape_str_previous, shape_str, len);
148  }
149  snprintf(shape_str, strnlen(shape_str, STRN_LENGTH - offset - 1) + offset,
150      "%s)", shape_str_previous);
151  return shape_str;
152}
Note: See TracBrowser for help on using the repository browser.