← Back to C-Kernel-Engine Docs Doxygen Source Documentation
 
Loading...
Searching...
No Matches
audio_adaptive_layer_norm.c File Reference
#include "ckernel_audio.h"
#include <math.h>
#include <stdint.h>

Go to the source code of this file.

Functions

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)
 
static int checked_float_extent (size_t elements)
 
static int checked_product (size_t left, size_t right, size_t *result)
 
static int checked_span (size_t rows, size_t stride, size_t width, size_t *result)
 
static int row_stats (const float *row, size_t channels, float epsilon, double *mean, double *inverse_std)
 

Function Documentation

◆ audio_adaptive_layer_norm_f32()

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 
)

Definition at line 57 of file audio_adaptive_layer_norm.c.

66{
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;
72
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) ||
81 !checked_product(width, 2u, &projection_width) ||
82 !checked_product(projection_width, style_width, &weight_required) ||
83 !checked_product(projection_width, sizeof(float), &scratch_required) ||
84 !checked_float_extent(input_required) ||
85 !checked_float_extent(output_required) ||
86 !checked_float_extent(style_width) ||
87 !checked_float_extent(weight_required) ||
88 !checked_float_extent(projection_width))
89 return -2;
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;
95
96 /* Validate all external values before changing output or scratch. Padding
97 * is deliberately not read; only the declared logical columns are valid.
98 */
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;
108
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;
116 }
117
118 /* Validate every produced value before the first output write. The second
119 * pass repeats cheap normalization arithmetic without a hidden T*C buffer.
120 */
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))
127 return -4;
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;
136 }
137 }
138 }
139 return 0;
140}
static int checked_product(size_t left, size_t right, size_t *result)
static int row_stats(const float *row, size_t channels, float epsilon, double *mean, double *inverse_std)
static int checked_span(size_t rows, size_t stride, size_t width, size_t *result)
static int checked_float_extent(size_t elements)
CKSpacePrefixStyle style
Definition tokenizer.h:288

References checked_float_extent(), checked_product(), checked_span(), row_stats(), and style.

◆ checked_float_extent()

static int checked_float_extent ( size_t  elements)
static

Definition at line 35 of file audio_adaptive_layer_norm.c.

36{
37 return elements <= (size_t)PTRDIFF_MAX / sizeof(float);
38}

Referenced by audio_adaptive_layer_norm_f32().

◆ checked_product()

static int checked_product ( size_t  left,
size_t  right,
size_t *  result 
)
static

Definition at line 18 of file audio_adaptive_layer_norm.c.

19{
20 if (right != 0u && left > SIZE_MAX / right) return 0;
21 *result = left * right;
22 return 1;
23}
const char * left
Definition true_bpe.h:138
const char const char * right
Definition true_bpe.h:139

References left, and right.

Referenced by audio_adaptive_layer_norm_f32(), and checked_span().

◆ checked_span()

static int checked_span ( size_t  rows,
size_t  stride,
size_t  width,
size_t *  result 
)
static

Definition at line 25 of file audio_adaptive_layer_norm.c.

27{
28 size_t prefix;
29 if (rows == 0u || !checked_product(rows - 1u, stride, &prefix) ||
30 prefix > SIZE_MAX - width) return 0;
31 *result = prefix + width;
32 return 1;
33}

References checked_product().

Referenced by audio_adaptive_layer_norm_f32().

◆ row_stats()

static int row_stats ( const float *  row,
size_t  channels,
float  epsilon,
double *  mean,
double *  inverse_std 
)
static

Definition at line 40 of file audio_adaptive_layer_norm.c.

42{
43 double sum = 0.0;
44 for (size_t channel = 0u; channel < channels; ++channel)
45 sum += (double)row[channel];
46 *mean = sum / (double)channels;
47 double squared = 0.0;
48 for (size_t channel = 0u; channel < channels; ++channel) {
49 const double delta = (double)row[channel] - *mean;
50 squared += delta * delta;
51 }
52 const double variance = squared / (double)channels;
53 *inverse_std = 1.0 / sqrt(variance + (double)epsilon);
54 return isfinite(*mean) && isfinite(*inverse_std);
55}

Referenced by audio_adaptive_layer_norm_f32().