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 | |
---|
21 | #include "aubio_priv.h" |
---|
22 | #include "fmat.h" |
---|
23 | #include "tensor.h" |
---|
24 | |
---|
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 | |
---|
32 | aubio_tensor_t *new_aubio_tensor(uint_t ndim, uint_t *shape) |
---|
33 | { |
---|
34 | aubio_tensor_t *c = AUBIO_NEW(aubio_tensor_t); |
---|
35 | uint_t items_per_row = 1; |
---|
36 | uint_t i; |
---|
37 | |
---|
38 | if ((sint_t)ndim <= 0) goto failure; |
---|
39 | for (i = 0; i < ndim; i++) { |
---|
40 | if ((sint_t)shape[i] <= 0) goto failure; |
---|
41 | } |
---|
42 | |
---|
43 | c->ndim = ndim; |
---|
44 | c->shape[0] = shape[0]; |
---|
45 | for (i = 1; i < ndim; i++) { |
---|
46 | c->shape[i] = shape[i]; |
---|
47 | items_per_row *= shape[i]; |
---|
48 | } |
---|
49 | c->size = items_per_row * shape[0]; |
---|
50 | c->buffer = AUBIO_ARRAY(smpl_t, c->size); |
---|
51 | c->data = AUBIO_ARRAY(smpl_t*, shape[0]); |
---|
52 | for (i = 0; i < c->shape[0]; i++) { |
---|
53 | c->data[i] = c->buffer + i * items_per_row; |
---|
54 | } |
---|
55 | |
---|
56 | return c; |
---|
57 | |
---|
58 | failure: |
---|
59 | del_aubio_tensor(c); |
---|
60 | return NULL; |
---|
61 | } |
---|
62 | |
---|
63 | void 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 | |
---|
74 | uint_t aubio_tensor_as_fvec(aubio_tensor_t *c, fvec_t *o) { |
---|
75 | if (!c || !o) return AUBIO_FAIL; |
---|
76 | o->length = c->size; |
---|
77 | o->data = c->buffer; |
---|
78 | return AUBIO_OK; |
---|
79 | } |
---|
80 | |
---|
81 | uint_t aubio_fvec_as_tensor(fvec_t *o, aubio_tensor_t *c) { |
---|
82 | if (!o || !c) return AUBIO_FAIL; |
---|
83 | c->ndim = 1; |
---|
84 | c->shape[0] = o->length; |
---|
85 | c->data = &o->data; |
---|
86 | c->buffer = o->data; |
---|
87 | c->size = o->length; |
---|
88 | return AUBIO_OK; |
---|
89 | } |
---|
90 | |
---|
91 | uint_t aubio_tensor_as_fmat(aubio_tensor_t *c, fmat_t *o) { |
---|
92 | if (!c || !o) return AUBIO_FAIL; |
---|
93 | o->height = c->shape[0]; |
---|
94 | o->length = c->size / c->shape[0]; |
---|
95 | o->data = c->data; |
---|
96 | return AUBIO_OK; |
---|
97 | } |
---|
98 | |
---|
99 | uint_t aubio_fmat_as_tensor(fmat_t *o, aubio_tensor_t *c) { |
---|
100 | if (!o || !c) return AUBIO_FAIL; |
---|
101 | c->ndim = 2; |
---|
102 | c->shape[0] = o->height; |
---|
103 | c->shape[1] = o->length; |
---|
104 | c->size = o->height * o->length; |
---|
105 | c->data = o->data; |
---|
106 | c->buffer = o->data[0]; |
---|
107 | return AUBIO_OK; |
---|
108 | } |
---|
109 | |
---|
110 | uint_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 | |
---|
140 | uint_t aubio_tensor_have_same_shape(aubio_tensor_t *a, aubio_tensor_t *b) |
---|
141 | { |
---|
142 | uint_t n; |
---|
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]) { |
---|
149 | return 0; |
---|
150 | } |
---|
151 | } |
---|
152 | return 1; |
---|
153 | } |
---|
154 | |
---|
155 | smpl_t aubio_tensor_max(aubio_tensor_t *t) |
---|
156 | { |
---|
157 | uint_t i; |
---|
158 | smpl_t max = t->buffer[0]; |
---|
159 | for (i = 0; i < t->size; i++) { |
---|
160 | max = MAX(t->buffer[i], max); |
---|
161 | } |
---|
162 | return max; |
---|
163 | } |
---|
164 | |
---|
165 | const 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 | // 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 ? " " : ""); |
---|
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); |
---|
183 | return shape_str; |
---|
184 | } |
---|
185 | |
---|
186 | static 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 | |
---|
205 | void 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 | } |
---|
211 | |
---|
212 | void 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 |
---|
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]); |
---|
235 | #endif |
---|
236 | } |
---|