source: src/ai/tensor.c @ e2010b3

feature/crepe_org
Last change on this file since e2010b3 was cc74a29, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[tensor] add copy

  • Property mode set to 100644
File size: 6.4 KB
RevLine 
[aa5cc08]1/*
2  Copyright (C) 2018 Paul Brossier <piem@aubio.org>
3
4  This file is part of aubio.
5
6  aubio is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10
11  aubio is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
[4c33f81]21#include "aubio_priv.h"
22#include "fmat.h"
23#include "tensor.h"
24
[c4b6b59]25#define STRN_LENGTH 40
26#if !HAVE_AUBIO_DOUBLE
27#define AUBIO_SMPL_TFMT "% 9.4f"
28#else
29#define AUBIO_SMPL_TFMT "% 9.4lf"
30#endif /* HAVE_AUBIO_DOUBLE */
31
[5010e61]32aubio_tensor_t *new_aubio_tensor(uint_t ndim, uint_t *shape)
[4c33f81]33{
34  aubio_tensor_t *c = AUBIO_NEW(aubio_tensor_t);
[f5ea4fb]35  uint_t items_per_row = 1;
[4c33f81]36  uint_t i;
37
[5010e61]38  if ((sint_t)ndim <= 0) goto failure;
39  for (i = 0; i < ndim; i++) {
40    if ((sint_t)shape[i] <= 0) goto failure;
[4c33f81]41  }
42
[5010e61]43  c->ndim = ndim;
44  c->shape[0] = shape[0];
45  for (i = 1; i < ndim; i++) {
46    c->shape[i] = shape[i];
[9ca7923]47    items_per_row *= shape[i];
[4c33f81]48  }
[9ca7923]49  c->size = items_per_row * shape[0];
[f5ea4fb]50  c->buffer = AUBIO_ARRAY(smpl_t, c->size);
[5010e61]51  c->data = AUBIO_ARRAY(smpl_t*, shape[0]);
[f5ea4fb]52  for (i = 0; i < c->shape[0]; i++) {
53    c->data[i] = c->buffer + i * items_per_row;
[4c33f81]54  }
55
56  return c;
57
58failure:
59  del_aubio_tensor(c);
60  return NULL;
61}
62
63void del_aubio_tensor(aubio_tensor_t *c)
64{
65  if (c->data) {
66    if (c->data[0]) {
67      AUBIO_FREE(c->data[0]);
68    }
69    AUBIO_FREE(c->data);
70  }
71  AUBIO_FREE(c);
72}
73
74uint_t aubio_tensor_as_fvec(aubio_tensor_t *c, fvec_t *o) {
[427a48c]75  if (!c || !o) return AUBIO_FAIL;
76  o->length = c->size;
[71655fee]77  o->data = c->buffer;
[4c33f81]78  return AUBIO_OK;
79}
80
81uint_t aubio_fvec_as_tensor(fvec_t *o, aubio_tensor_t *c) {
[427a48c]82  if (!o || !c) return AUBIO_FAIL;
[5010e61]83  c->ndim = 1;
84  c->shape[0] = o->length;
[4c33f81]85  c->data = &o->data;
[427a48c]86  c->buffer = o->data;
[849210c]87  c->size = o->length;
[4c33f81]88  return AUBIO_OK;
89}
90
91uint_t aubio_tensor_as_fmat(aubio_tensor_t *c, fmat_t *o) {
[427a48c]92  if (!c || !o) return AUBIO_FAIL;
[5010e61]93  o->height = c->shape[0];
[427a48c]94  o->length = c->size / c->shape[0];
[4c33f81]95  o->data = c->data;
96  return AUBIO_OK;
97}
98
99uint_t aubio_fmat_as_tensor(fmat_t *o, aubio_tensor_t *c) {
[427a48c]100  if (!o || !c) return AUBIO_FAIL;
[9d35014]101  c->ndim = 2;
102  c->shape[0] = o->height;
103  c->shape[1] = o->length;
[849210c]104  c->size = o->height * o->length;
[4c33f81]105  c->data = o->data;
[427a48c]106  c->buffer = o->data[0];
[4c33f81]107  return AUBIO_OK;
108}
109
[7e0b641]110uint_t aubio_tensor_get_subtensor(aubio_tensor_t *t, uint_t i,
111    aubio_tensor_t *st)
112{
113  uint_t j;
114  if (!t || !st) return AUBIO_FAIL;
115  if (i >= t->shape[0]) {
116    AUBIO_ERR("tensor: index %d out of range, only %d subtensors\n",
117        i, t->shape[0]);
118    return AUBIO_FAIL;
119  }
120  if(t->ndim > 1) {
121    st->ndim = t->ndim - 1;
122    for (j = 0; j < st->ndim; j++) {
123      st->shape[j] = t->shape[j + 1];
124    }
125    for (j = st->ndim; j < AUBIO_TENSOR_MAXDIM; j++) {
126      st->shape[j] = 0;
127    }
128    st->size = t->size / t->shape[0];
129  } else {
130    st->ndim = 1;
131    st->shape[0] = 1;
132    st->size = 1;
133  }
134  // st was allocated on the stack, row indices are lost
135  st->data = NULL;
136  st->buffer = &t->buffer[0] + st->size * i;
137  return AUBIO_OK;
138}
139
[4b2d174]140uint_t aubio_tensor_have_same_shape(aubio_tensor_t *a, aubio_tensor_t *b)
[dc257cc]141{
142  uint_t n;
[4b2d174]143  AUBIO_ASSERT(a && b);
144  if (a->ndim != b->ndim) {
145    return 0;
146  }
147  for (n = 0; n < a->ndim; n++) {
148    if (a->shape[n] != b->shape[n]) {
[dc257cc]149      return 0;
150    }
151  }
152  return 1;
153}
154
[4c33f81]155smpl_t aubio_tensor_max(aubio_tensor_t *t)
156{
157  uint_t i;
[a85c7f3]158  smpl_t max = t->buffer[0];
[9ca7923]159  for (i = 0; i < t->size; i++) {
[a85c7f3]160    max = MAX(t->buffer[i], max);
[4c33f81]161  }
162  return max;
163}
[50d7afe]164
165const char_t *aubio_tensor_get_shape_string(aubio_tensor_t *t) {
166  uint_t i;
167  if (!t) return NULL;
168  size_t offset = 2;
169  static char_t shape_str[STRN_LENGTH];
170  char_t shape_str_previous[STRN_LENGTH] = "(";
171  for (i = 0; i < t->ndim; i++) {
[21631e9]172    // and space last if not the last one
173    int add_space = (i < t->ndim - 1);
174    // add coma first if this not last, or always if 1d
175    int add_coma = add_space || (t->ndim == 1);
176    int len = snprintf(shape_str, STRN_LENGTH, "%s%d%s%s",
177        shape_str_previous, t->shape[i],
178        add_coma ? "," : "", add_space ? " " : "");
[50d7afe]179    strncpy(shape_str_previous, shape_str, len);
180  }
181  snprintf(shape_str, strnlen(shape_str, STRN_LENGTH - offset - 1) + offset,
182      "%s)", shape_str_previous);
[9bffada]183  return shape_str;
[50d7afe]184}
[c4b6b59]185
186static void aubio_tensor_print_subtensor(aubio_tensor_t *t, uint_t depth)
187{
188  uint_t i;
189  AUBIO_MSG("[");
190  for (i = 0; i < t->shape[0]; i ++) {
191    AUBIO_MSG("%*s", i == 0 ? 0 : depth + 1, i == 0 ? "" : " ");
192    if (t->ndim == 1) {
193      AUBIO_MSG(AUBIO_SMPL_TFMT, t->buffer[i]);
194    } else {
195      aubio_tensor_t st;
196      aubio_tensor_get_subtensor(t, i, &st);
197      aubio_tensor_print_subtensor(&st, depth + 1); // recursive call
198    }
199    AUBIO_MSG("%s%s", (i < t->shape[0] - 1) ? "," : "",
200        t->ndim == 1 ? " " : ((i < t->shape[0] - 1) ? "\n" : ""));
201  }
202  AUBIO_MSG("]");
203}
204
205void aubio_tensor_print(aubio_tensor_t *t)
206{
207  AUBIO_MSG("tensor of shape %s\n", aubio_tensor_get_shape_string(t));
208  aubio_tensor_print_subtensor(t, 0);
209  AUBIO_MSG("\n");
210}
[6006760]211
212void aubio_tensor_matmul(aubio_tensor_t *a, aubio_tensor_t *b,
213    aubio_tensor_t *c)
214{
215  AUBIO_ASSERT (a->shape[0] == c->shape[0]);
216  AUBIO_ASSERT (a->shape[1] == b->shape[0]);
217  AUBIO_ASSERT (b->shape[1] == c->shape[1]);
218#if !defined(HAVE_BLAS)
219  uint_t i, j, k;
220  for (i = 0; i < c->shape[0]; i++) {
221    for (j = 0; j < c->shape[1]; j++) {
222      smpl_t sum = 0.;
223      for (k = 0; k < a->shape[1]; k++) {
224          sum += a->buffer[i * a->shape[1] + k]
225            * b->buffer[k * b->shape[1] + j];
226      }
227      c->buffer[i * c->shape[1] + j] = sum;
228    }
229  }
230#else
[1df9cd1]231  aubio_cblas__gemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, a->shape[0],
232      b->size/b->shape[0], b->shape[0], 1.F, a->buffer, a->size/a->shape[0],
233      b->buffer,
234      b->size/b->shape[0], 0.F, c->buffer, b->size/b->shape[0]);
[6006760]235#endif
236}
[cc74a29]237
238void aubio_tensor_copy(aubio_tensor_t *s, aubio_tensor_t *t)
239{
240  if (!aubio_tensor_have_same_shape(s, t)) {
241    AUBIO_ERR("tensor: not copying source tensor %s",
242        aubio_tensor_get_shape_string(s));
243    AUBIO_ERR(" to destination tensor %s",
244        aubio_tensor_get_shape_string(t));
245    return;
246  }
247  AUBIO_MEMCPY(t->buffer, s->buffer, s->size);
248}
Note: See TracBrowser for help on using the repository browser.