23 double *mean,
double *inverse_std)
26 for (
size_t frame = 0u; frame < frames; ++frame)
27 total += (
double)row[frame];
28 *mean = total / (double)frames;
30 for (
size_t frame = 0u; frame < frames; ++frame) {
31 const double delta = (double)row[frame] - *mean;
32 squared += delta * delta;
34 *inverse_std = 1.0 / sqrt(squared / (
double)frames + (
double)epsilon);
35 return isfinite(*mean) && isfinite(*inverse_std);
39 const float *input,
size_t input_elements,
size_t input_stride,
40 const float *norm_weight,
size_t norm_weight_elements,
41 const float *norm_bias,
size_t norm_bias_elements,
42 const float *style_affine,
size_t style_affine_elements,
43 float *output,
size_t output_elements,
size_t output_stride,
44 size_t channels,
size_t frames,
float epsilon)
46 if (input == NULL || norm_weight == NULL || norm_bias == NULL ||
47 style_affine == NULL || output == NULL ||
48 !isfinite(epsilon) || epsilon <= 0.0f ||
49 channels == 0u || channels > SIZE_MAX / 2u)
return -1;
50 size_t input_required, output_required;
51 if (!
checked_span(channels, input_stride, frames, &input_required) ||
52 !
checked_span(channels, output_stride, frames, &output_required))
54 if (input_elements < input_required || output_elements < output_required ||
55 norm_weight_elements < channels || norm_bias_elements < channels ||
56 style_affine_elements < 2u * channels)
return -2;
58 for (
size_t channel = 0u; channel < channels; ++channel) {
59 if (!isfinite(norm_weight[channel]) || !isfinite(norm_bias[channel]) ||
60 !isfinite(style_affine[channel]) ||
61 !isfinite(style_affine[channels + channel]))
return -3;
62 const float *row = input + channel * input_stride;
63 for (
size_t frame = 0u; frame < frames; ++frame)
64 if (!isfinite(row[frame]))
return -3;
69 for (
int pass = 0; pass < 2; ++pass) {
70 for (
size_t channel = 0u; channel < channels; ++channel) {
71 const float *row = input + channel * input_stride;
72 float *output_row = output + channel * output_stride;
73 double mean, inverse_std;
74 if (!
channel_stats(row, frames, epsilon, &mean, &inverse_std))
76 const float weight = norm_weight[channel];
77 const float bias = norm_bias[channel];
78 const float gamma = style_affine[channel];
79 const float beta = style_affine[channels + channel];
80 for (
size_t frame = 0u; frame < frames; ++frame) {
81 const float normalized = (float)(
82 ((
double)row[frame] - mean) * inverse_std);
83 const float affine = normalized * weight + bias;
84 const float value = (1.0f + gamma) * affine + beta;
85 if (!isfinite(value))
return -3;
86 if (pass != 0) output_row[frame] = value;
int audio_adain_instance_norm_f32(const float *input, size_t input_elements, size_t input_stride, const float *norm_weight, size_t norm_weight_elements, const float *norm_bias, size_t norm_bias_elements, const float *style_affine, size_t style_affine_elements, float *output, size_t output_elements, size_t output_stride, size_t channels, size_t frames, float epsilon)