← Back to C-Kernel-Engine Docs Doxygen Source Documentation
 
Loading...
Searching...
No Matches
audio_conv_transpose_dense_checked.c
Go to the documentation of this file.
3
4#include <math.h>
5#include <stdint.h>
6#include <string.h>
7
9 size_t output_channels, size_t output_frames, size_t *scratch_elements) {
10 if (!scratch_elements || !output_channels || !output_frames ||
11 output_channels > SIZE_MAX / output_frames ||
12 output_channels * output_frames > SIZE_MAX / sizeof(float))
14 *scratch_elements = output_channels * output_frames;
16}
17
19 const float *input, size_t input_elements, size_t input_stride,
20 const float *weight, size_t weight_elements,
21 const float *bias, size_t bias_elements,
22 float *output, size_t output_elements, size_t output_stride,
23 float *scratch, size_t scratch_elements,
24 size_t input_channels, size_t output_channels, size_t input_frames,
25 size_t kernel_size, size_t stride, size_t padding,
26 size_t output_padding, size_t output_frames) {
27 size_t geometry, input_span, output_span, weight_need, scratch_need;
28 if (!input_channels || !output_channels || !input_frames ||
29 !kernel_size || !stride || !output_frames || output_padding >= stride)
31 if (input_frames - 1 > SIZE_MAX / stride || padding > SIZE_MAX / 2)
33 geometry = (input_frames - 1) * stride;
34 if (geometry > SIZE_MAX - kernel_size ||
35 geometry + kernel_size > SIZE_MAX - output_padding)
37 geometry += kernel_size + output_padding;
38 if (2 * padding >= geometry || geometry - 2 * padding != output_frames)
40 if (!ck_checked_span(input_channels, input_frames, input_stride,
41 &input_span) ||
42 !ck_checked_span(output_channels, output_frames, output_stride,
43 &output_span) ||
44 input_channels > SIZE_MAX / output_channels ||
45 input_channels * output_channels > SIZE_MAX / kernel_size ||
47 output_channels, output_frames, &scratch_need))
49 weight_need = input_channels * output_channels * kernel_size;
50 if (weight_need > SIZE_MAX / sizeof(float))
52 if (input_elements < input_span || weight_elements < weight_need ||
53 bias_elements < output_channels || output_elements < output_span ||
54 scratch_elements < scratch_need)
56 if (!ck_checked_region(input, input_span) ||
57 !ck_checked_region(weight, weight_need) ||
58 !ck_checked_region(bias, output_channels) ||
59 !ck_checked_region(output, output_span) ||
60 !ck_checked_region(scratch, scratch_need) ||
61 ck_checked_overlap(output, output_span, input, input_span) ||
62 ck_checked_overlap(output, output_span, weight, weight_need) ||
63 ck_checked_overlap(output, output_span, bias, output_channels) ||
64 ck_checked_overlap(output, output_span, scratch, scratch_need) ||
65 ck_checked_overlap(scratch, scratch_need, input, input_span) ||
66 ck_checked_overlap(scratch, scratch_need, weight, weight_need) ||
67 ck_checked_overlap(scratch, scratch_need, bias, output_channels))
69 if (!ck_checked_finite(weight, weight_need) ||
70 !ck_checked_finite(bias, output_channels))
72 for (size_t ic = 0; ic < input_channels; ++ic)
73 if (!ck_checked_finite(input + ic * input_stride, input_frames))
75
76 for (size_t oc = 0; oc < output_channels; ++oc)
77 for (size_t frame = 0; frame < output_frames; ++frame)
78 scratch[oc * output_frames + frame] = bias[oc];
79 for (size_t ic = 0; ic < input_channels; ++ic)
80 for (size_t frame = 0; frame < input_frames; ++frame)
81 for (size_t tap = 0; tap < kernel_size; ++tap) {
82 size_t target = frame * stride + tap;
83 if (target < padding || target - padding >= output_frames)
84 continue;
85 size_t out_frame = target - padding;
86 float value = input[ic * input_stride + frame];
87 for (size_t oc = 0; oc < output_channels; ++oc) {
88 size_t index = oc * output_frames + out_frame;
89 scratch[index] = fmaf(value,
90 weight[(ic * output_channels + oc) * kernel_size + tap],
91 scratch[index]);
92 }
93 }
94 if (!ck_checked_finite(scratch, scratch_need))
96 for (size_t oc = 0; oc < output_channels; ++oc)
97 memcpy(output + oc * output_stride,
98 scratch + oc * output_frames,
99 output_frames * sizeof(float));
101}
int audio_conv_transpose1d_dense_f32_workspace(size_t output_channels, size_t output_frames, size_t *scratch_elements)
int audio_conv_transpose1d_dense_channel_major_f32_checked(const float *input, size_t input_elements, size_t input_stride, const float *weight, size_t weight_elements, const float *bias, size_t bias_elements, float *output, size_t output_elements, size_t output_stride, float *scratch, size_t scratch_elements, size_t input_channels, size_t output_channels, size_t input_frames, size_t kernel_size, size_t stride, size_t padding, size_t output_padding, size_t output_frames)
static int ck_checked_finite(const float *ptr, size_t n)
static int ck_checked_span(size_t rows, size_t width, size_t stride, size_t *span)
static int ck_checked_region(const float *ptr, size_t n)
static int ck_checked_overlap(const float *a, size_t na, const float *b, size_t nb)