40static int row_stats(
const float *row,
size_t channels,
float epsilon,
41 double *mean,
double *inverse_std)
44 for (
size_t channel = 0u; channel < channels; ++channel)
45 sum += (
double)row[channel];
46 *mean = sum / (double)channels;
48 for (
size_t channel = 0u; channel < channels; ++channel) {
49 const double delta = (double)row[channel] - *mean;
50 squared += delta * delta;
52 const double variance = squared / (double)channels;
53 *inverse_std = 1.0 / sqrt(variance + (
double)epsilon);
54 return isfinite(*mean) && isfinite(*inverse_std);
58 const float *input,
size_t input_elements,
59 const float *
style,
size_t style_elements,
60 const float *projection_weight,
size_t projection_weight_elements,
61 const float *projection_bias,
size_t projection_bias_elements,
62 float *output,
size_t output_elements,
63 float *projection_scratch,
size_t projection_scratch_bytes,
64 int tokens,
int channels,
int style_dim,
65 size_t input_stride,
size_t output_stride,
float epsilon)
67 if (input == NULL ||
style == NULL || projection_weight == NULL ||
68 projection_bias == NULL || output == NULL ||
69 projection_scratch == NULL)
return -1;
70 if (tokens <= 0 || channels <= 0 || style_dim <= 0 ||
71 !isfinite(epsilon) || epsilon <= 0.0f)
return -2;
73 const size_t rows = (size_t)tokens;
74 const size_t width = (size_t)channels;
75 const size_t style_width = (size_t)style_dim;
76 size_t input_required, output_required, projection_width;
77 size_t weight_required, scratch_required;
78 if (input_stride < width || output_stride < width ||
79 !
checked_span(rows, input_stride, width, &input_required) ||
80 !
checked_span(rows, output_stride, width, &output_required) ||
90 if (input_elements < input_required || output_elements < output_required ||
91 style_elements < style_width ||
92 projection_weight_elements < weight_required ||
93 projection_bias_elements < projection_width ||
94 projection_scratch_bytes < scratch_required)
return -3;
99 for (
size_t index = 0u; index < style_width; ++index)
100 if (!isfinite(
style[index]))
return -4;
101 for (
size_t index = 0u; index < weight_required; ++index)
102 if (!isfinite(projection_weight[index]))
return -4;
103 for (
size_t index = 0u; index < projection_width; ++index)
104 if (!isfinite(projection_bias[index]))
return -4;
105 for (
size_t row = 0u; row < rows; ++row)
106 for (
size_t channel = 0u; channel < width; ++channel)
107 if (!isfinite(input[row * input_stride + channel]))
return -4;
109 for (
size_t channel = 0u; channel < projection_width; ++channel) {
110 float value = projection_bias[channel];
111 const float *weight = projection_weight + channel * style_width;
112 for (
size_t index = 0u; index < style_width; ++index)
113 value += weight[index] *
style[index];
114 if (!isfinite(value))
return -4;
115 projection_scratch[channel] = value;
121 for (
int pass = 0; pass < 2; ++pass) {
122 for (
size_t row = 0u; row < rows; ++row) {
123 const float *input_row = input + row * input_stride;
124 float *output_row = output + row * output_stride;
125 double mean, inverse_std;
126 if (!
row_stats(input_row, width, epsilon, &mean, &inverse_std))
128 for (
size_t channel = 0u; channel < width; ++channel) {
129 const float normalized = (float)(
130 ((
double)input_row[channel] - mean) * inverse_std);
131 const float gamma = projection_scratch[channel];
132 const float beta = projection_scratch[width + channel];
133 const float value = (1.0f + gamma) * normalized + beta;
134 if (!isfinite(value))
return -4;
135 if (pass != 0) output_row[channel] = value;
int audio_adaptive_layer_norm_f32(const float *input, size_t input_elements, const float *style, size_t style_elements, const float *projection_weight, size_t projection_weight_elements, const float *projection_bias, size_t projection_bias_elements, float *output, size_t output_elements, float *projection_scratch, size_t projection_scratch_bytes, int tokens, int channels, int style_dim, size_t input_stride, size_t output_stride, float epsilon)