1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//! Normalization and position encoding operations
//!
//! Provides layer normalization, RMS normalization, and rotary position embeddings
//! used in transformer inference.
//!
//! ## Normalization Functions
//!
//! - [`simd_layer_norm`] - Standard layer normalization with mean and variance
//! - [`simd_rms_norm`] - RMS normalization (faster, used in LLaMA/Mistral)
//!
//! ## Position Encoding
//!
//! - [`apply_rope`] - Rotary Position Embeddings (RoPE)
/// SIMD-accelerated layer normalization
///
/// LayerNorm(x) = (x - mean) / sqrt(var + eps) * weight + bias
///
/// # Arguments
///
/// * `input` - Input vector to normalize
/// * `weight` - Scale parameters (gamma)
/// * `bias` - Optional shift parameters (beta)
/// * `eps` - Small constant for numerical stability (typically 1e-5)
///
/// # Example
///
/// ```
/// use realizar::inference::simd_layer_norm;
///
/// let input = vec![1.0, 2.0, 3.0, 4.0];
/// let weight = vec![1.0, 1.0, 1.0, 1.0];
/// let output = simd_layer_norm(&input, &weight, None, 1e-5);
///
/// // Output should have mean ≈ 0 and std ≈ 1
/// let mean: f32 = output.iter().sum::<f32>() / output.len() as f32;
/// assert!(mean.abs() < 1e-5);
/// ```
/// SIMD-accelerated RMS normalization
///
/// RMSNorm(x) = x / sqrt(mean(x^2) + eps) * weight
///
/// RMS normalization is faster than LayerNorm as it doesn't require
/// computing the mean. Used in LLaMA, Mistral, and other modern LLMs.
///
/// # Arguments
///
/// * `input` - Input vector to normalize
/// * `weight` - Scale parameters
/// * `eps` - Small constant for numerical stability (typically 1e-5)
///
/// # Example
///
/// ```
/// use realizar::inference::simd_rms_norm;
///
/// let input = vec![1.0, 2.0, 3.0];
/// let weight = vec![1.0, 1.0, 1.0];
/// let output = simd_rms_norm(&input, &weight, 1e-5);
///
/// // RMS of [1,2,3] ≈ 2.16, so normalized ≈ [0.46, 0.93, 1.39]
/// assert!((output[0] - 0.4629).abs() < 0.01);
/// ```
/// Apply rotary position embeddings (RoPE)
///
/// RoPE encodes position information by rotating pairs of dimensions.
/// This enables relative position encoding that generalizes to longer sequences.
///
/// # Arguments
///
/// * `x` - Mutable slice to apply RoPE to [hidden_dim]
/// * `hidden_dim` - Total hidden dimension (must equal x.len())
/// * `num_heads` - Number of attention heads
/// * `position` - Token position in sequence (0-indexed)
/// * `theta` - Base frequency (typically 10000.0)
///
/// # Algorithm
///
/// For each head and each pair of dimensions (i, i + d/2):
/// ```text
/// freq = 1 / theta^(2i/d)
/// angle = position * freq
/// x[i] = x[i] * cos(angle) - x[i+d/2] * sin(angle)
/// x[i+d/2] = x[i] * sin(angle) + x[i+d/2] * cos(angle)
/// ```
///
/// # Example
///
/// ```
/// use realizar::inference::apply_rope;
///
/// let mut x = vec![1.0; 64]; // 64 hidden dim
/// apply_rope(&mut x, 64, 4, 0, 10000.0); // Position 0
///
/// // At position 0, rotations are identity (angle = 0)
/// assert!((x[0] - 1.0).abs() < 1e-5);
/// ```
include!;