source: src/ai/tensor.c @ b0b167a

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

[tensor] add matmul

  • Property mode set to 100644
File size: 5.8 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
[dc257cc]140uint_t aubio_tensor_have_same_size(aubio_tensor_t *t, aubio_tensor_t *s)
141{
142  uint_t n;
143  if (!t || !s) return 0;
144  if (t->ndim != s->ndim) return 0;
145  if (t->size != s->size) return 0;
146  n = t->ndim;
147  while (n--) {
148    if (t->shape[n] != s->shape[n]) {
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++) {
172    int len = snprintf(shape_str, STRN_LENGTH, "%s%d%s",
173        shape_str_previous, t->shape[i], (i == t->ndim - 1) ? "" : ", ");
174    strncpy(shape_str_previous, shape_str, len);
175  }
176  snprintf(shape_str, strnlen(shape_str, STRN_LENGTH - offset - 1) + offset,
177      "%s)", shape_str_previous);
[9bffada]178  return shape_str;
[50d7afe]179}
[c4b6b59]180
181static void aubio_tensor_print_subtensor(aubio_tensor_t *t, uint_t depth)
182{
183  uint_t i;
184  AUBIO_MSG("[");
185  for (i = 0; i < t->shape[0]; i ++) {
186    AUBIO_MSG("%*s", i == 0 ? 0 : depth + 1, i == 0 ? "" : " ");
187    if (t->ndim == 1) {
188      AUBIO_MSG(AUBIO_SMPL_TFMT, t->buffer[i]);
189    } else {
190      aubio_tensor_t st;
191      aubio_tensor_get_subtensor(t, i, &st);
192      aubio_tensor_print_subtensor(&st, depth + 1); // recursive call
193    }
194    AUBIO_MSG("%s%s", (i < t->shape[0] - 1) ? "," : "",
195        t->ndim == 1 ? " " : ((i < t->shape[0] - 1) ? "\n" : ""));
196  }
197  AUBIO_MSG("]");
198}
199
200void aubio_tensor_print(aubio_tensor_t *t)
201{
202  AUBIO_MSG("tensor of shape %s\n", aubio_tensor_get_shape_string(t));
203  aubio_tensor_print_subtensor(t, 0);
204  AUBIO_MSG("\n");
205}
[6006760]206
207void aubio_tensor_matmul(aubio_tensor_t *a, aubio_tensor_t *b,
208    aubio_tensor_t *c)
209{
210  AUBIO_ASSERT (a->shape[0] == c->shape[0]);
211  AUBIO_ASSERT (a->shape[1] == b->shape[0]);
212  AUBIO_ASSERT (b->shape[1] == c->shape[1]);
213#if !defined(HAVE_BLAS)
214  uint_t i, j, k;
215  for (i = 0; i < c->shape[0]; i++) {
216    for (j = 0; j < c->shape[1]; j++) {
217      smpl_t sum = 0.;
218      for (k = 0; k < a->shape[1]; k++) {
219          sum += a->buffer[i * a->shape[1] + k]
220            * b->buffer[k * b->shape[1] + j];
221      }
222      c->buffer[i * c->shape[1] + j] = sum;
223    }
224  }
225#else
226  cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, a->shape[0],
227      b->shape[1], b->shape[0], 1.F, a->buffer, a->shape[1], b->buffer,
228      b->shape[1], 0.F, c->buffer, b->shape[1]);
229#endif
230}
Note: See TracBrowser for help on using the repository browser.