2#include <ae2f/Ann/Conv.h>
7#define ae2f_AnnConv1dSz(in_f, in_g, pad, stride)
13static size_t iter_indices(
14 size_t dim, size_t* indices,
const size_t* max_indices) {
18 if (indices[i] < max_indices[i]) {
26static void get_conv_indices(
28 const size_t* out_indices,
const size_t* filter_indices,
29 const size_t* in_dims,
const size_t* filter_dims,
30 const size_t* stride,
const size_t* pad,
31 size_t* in_idx, size_t* filter_idx, size_t* out_idx,
33 size_t in_stride = 1, filter_stride = 1, out_stride = 1;
36 *in_idx = *filter_idx = *out_idx = 0;
43 out_indices[i] * (stride ? stride[i] : 1) + filter_indices[i];
44 if (cur_in_idx < (pad ? pad[i] : 0) ||
45 cur_in_idx >= (in_dims[i] + (pad ? pad[i] : 0))) {
49 cur_in_idx -= (pad ? pad[i] : 0);
51 *in_idx += cur_in_idx * in_stride;
52 *filter_idx += filter_indices[i] * filter_stride;
53 *out_idx += out_indices[i] * out_stride;
55 in_stride *= in_dims[i];
56 filter_stride *= filter_dims[i];
58 ((in_dims[i] - filter_dims[i] + 2 * (pad ? pad[i] : 0)) /
59 (stride ? stride[i] : 1) +
65
66
67
81 const size_t outc = ((infc - ingc + (pad << 1)) / (stride)) + 1;
83#define return(n) { err = n; goto _return; }
88 for(i = 0; i < outc; i++)
91 const size_t begi = i * stride;
93 for(j = 0; j < ingc; j++)
95 const size_t infi = begi + j;
98 if(infi < pad || infi >= infc + pad) {
103 if(!(infv && ingv)) {
108 infv[(infi - pad)] * ingv[j];
112
113
114
119 if(opt_outc) *opt_outc = outc;
131 ae2f_eAnnCnnPool type
137 size_t outc = 1 + ((inc - window) / stride), i, j;
151 for(i = 0; i < outc; i++)
153#define o (i * stride)
165 for(j = 0; j < window; j++)
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
221 const size_t* stride_opt,
222 const size_t* pad_opt
228 size_t* out_dims = (size_t*)malloc(dim *
sizeof(size_t));
229 size_t total_out_size = 1;
232 for (i = 0; i < dim; i++) {
233 out_dims[i] = ((infc[i] - ingc[i] + 2 * (pad_opt ? pad_opt[i] : 0)) /
234 (stride_opt ? stride_opt[i] : 1) +
236 total_out_size *= out_dims[i];
237 if (outc_opt) outc_opt[i] = out_dims[i];
242 size_t* out_indices = (size_t*)calloc(dim,
sizeof(size_t));
243 size_t* filter_indices = (size_t*)calloc(dim,
sizeof(size_t));
247 size_t in_idx, filter_idx, out_idx;
249 get_conv_indices(dim, out_indices, filter_indices, infc, ingc,
250 stride_opt, pad_opt, &in_idx, &filter_idx,
251 &out_idx, &is_padded);
253 outv[out_idx] += infv[in_idx] * ingv[filter_idx];
255 }
while (iter_indices(dim, filter_indices, ingc));
256 }
while (iter_indices(dim, out_indices, out_dims));
260 free(filter_indices);
268 size_t dim,
const ae2f_float_t* inv,
const size_t* inc, size_t incc,
270 const size_t* window_opt,
const size_t* stride_opt, ae2f_eAnnCnnPool type) {
274 size_t* out_dims = (size_t*)malloc(dim *
sizeof(size_t));
275 size_t total_out_size = 1;
278 for (i = 0; i < dim; i++) {
280 (inc[i] - window_opt[i]) / (stride_opt ? stride_opt[i] : 1) + 1;
281 total_out_size *= out_dims[i];
282 if (opt_outc) opt_outc[i] = out_dims[i];
285 size_t* out_indices = (size_t*)calloc(dim,
sizeof(size_t));
290 size_t out_stride = 1;
292 for (i = dim; i--; ) {
295 out_idx += out_indices[i] * out_stride;
296 out_stride *= out_dims[i];
300 switch (type & 0b11) {
309 size_t* window_indices = (size_t*)calloc(dim,
sizeof(size_t));
310 assert(window_indices);
314 size_t in_stride = 1;
316 for (i = dim; i--; ) {
317 assert((((intptr_t)i) >= 0) &&
"Fishy index");
319 in_idx += (out_indices[i] * (stride_opt ? stride_opt[i] : 1) +
325 switch (type & 0b11) {
331 if (inv[in_idx] > res) res = inv[in_idx];
334 if (inv[in_idx] < res) res = inv[in_idx];
337 }
while (iter_indices(dim, window_indices, window_opt));
340 free(window_indices);
341 }
while (iter_indices(dim, out_indices, out_dims));
351 size_t incc,
ae2f_float_t *outv, size_t *opt_outc, size_t outcc,
352 const size_t *window_opt, size_t windowcc,
353 const size_t *stride_opt, ae2f_eAnnCnnPool type)
358 , outv
, opt_outc
, outcc
359 , window_opt
, stride_opt
366 for(i = 0; i < dim; i++)
368 windowcc *= window_opt ? window_opt[i] : 1;
375 for(i = 0; i < windowcc; i++)
#define ae2f_extern
Suggests the existence of external variable or function, in naming of C. [non-mangling].
#define ae2f_CmpGetGt(a, b)
#define ae2f_CmpGetLs(a, b)
@ ae2f_eAnnCnnPool_MAX
Max.
@ ae2f_eAnnCnnPool_MIN
Min.
@ ae2f_eAnnCnnPool_ADD
Add.
@ ae2f_eAnnCnnPool_AVG
Average.
ae2f_extern ae2f_SHAREDEXPORT ae2f_err_t ae2f_AnnCnnPool1d(const ae2f_float_t *inv, const size_t inc, ae2f_float_t *outv, size_t *opt_outc, const size_t window, const size_t stride, ae2f_eAnnCnnPool type) noexcept
ae2f_extern ae2f_SHAREDEXPORT ae2f_err_t ae2f_AnnCnnPool_imp(size_t dim, const ae2f_float_t *inv, const size_t *inc, size_t incc, ae2f_float_t *outv, size_t *opt_outc, size_t outcc, const size_t *window_opt, const size_t *stride_opt, ae2f_eAnnCnnPool type)
ae2f_extern ae2f_SHAREDEXPORT ae2f_err_t ae2f_AnnCnnPool(size_t dim, const ae2f_float_t *inv, const size_t *inc, size_t incc, ae2f_float_t *outv, size_t *opt_outc, size_t outcc, const size_t *window_opt, size_t windowcc, const size_t *stride_opt, ae2f_eAnnCnnPool type)
ae2f_SHAREDEXPORT ae2f_err_t ae2f_AnnCnnConv1d(const ae2f_float_t *infv, size_t infc, const ae2f_float_t *ingv, size_t ingc, ae2f_float_t *outv, size_t *opt_outc, size_t stride, size_t pad)
all vectors are suggested initiated as 0.
ae2f_SHAREDEXPORT ae2f_err_t ae2f_AnnCnnConv(size_t dim, const ae2f_float_t *infv, const size_t *infc, size_t infcc, const ae2f_float_t *ingv, const size_t *ingc, size_t ingcc, ae2f_float_t *outv, size_t *outc_opt, size_t outcc, const size_t *stride_opt, const size_t *pad_opt)
dim must be the dimension of mmaps, lengths of lists. This function is meant to be recursive....
#define ae2f_mMMapDimLen(mmap,...)
Length vector for every dimension index.
#define ae2f_errGlob_OK
The Operation you've wanted went successful.
#define ae2f_errGlob_WRONG_OPERATION
Found that parameter sent by programmer is invalid. The operation may have been ceased while the midd...
uint8_t ae2f_err_t
Informs that this number represents the error.
#define ae2f_errGlob_PTR_IS_NULL
Failed to refer the pointer either l-value inside the function.
#define ae2f_errGlob_IMP_NOT_FOUND
Failed to find the function on preprocessor which is callable for some reason No operation has beed d...