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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 Fábio Henrique de Lima Silva (fhl.bsb@gmail.com) All rights reserved.
macro_rules! impl_avx512_activations {
() => {
#[inline(always)]
// SAFETY: dest and src are valid slices with dest.len() <= src.len();
// CPU supports AVX-512F+VL (verified by dispatch). Kernel uses unaligned loads/stores.
unsafe fn accumulate_head(dest: &mut [f32], src: &[f32]) {
// SAFETY: dest and src satisfy the function's documented invariants.
unsafe { crate::math::wavenet::accumulate::accumulate_head_avx512(dest, src) }
}
#[inline(always)]
// SAFETY: head_input and block are valid mutable slices with head_input.len() >= block.len();
// CPU supports AVX-512F+VL (verified by dispatch). Kernel uses unaligned loads/stores.
unsafe fn tanh_and_accumulate_block(head_input: &mut [f32], block: &mut [f32]) {
// SAFETY: head_input and block satisfy the function's documented invariants.
unsafe {
crate::math::wavenet::accumulate::tanh_and_accumulate_block_avx512(
head_input, block,
)
}
}
#[inline(always)]
// SAFETY: head_input and block are valid mutable slices with head_input.len() >= block.len();
// block.len() % ch == 0; CPU supports AVX-512F+VL (verified by dispatch).
unsafe fn gated_activation_and_accumulate_block(
head_input: &mut [f32],
block: &mut [f32],
ch: usize,
) {
// SAFETY: head_input, block, and ch satisfy the function's documented invariants.
unsafe {
crate::math::wavenet::accumulate::gated_activation_and_accumulate_block_avx512(
head_input, block, ch,
)
}
}
#[inline(always)]
// SAFETY: head_input and block are valid mutable slices of equal length;
// CPU supports AVX-512F+VL (verified by dispatch).
unsafe fn tanh_and_overwrite_block(head_input: &mut [f32], block: &mut [f32]) {
// SAFETY: head_input and block satisfy the function's documented invariants.
unsafe {
crate::math::wavenet::accumulate::tanh_and_overwrite_block_avx512(head_input, block)
}
}
#[inline(always)]
// SAFETY: head_input, block, and seed are valid slices of equal length;
// CPU supports AVX-512F+VL (verified by dispatch).
unsafe fn tanh_and_accumulate_with_seed(
head_input: &mut [f32],
block: &mut [f32],
seed: &[f32],
) {
// SAFETY: arguments satisfy the function's documented invariants.
unsafe {
crate::math::wavenet::accumulate::tanh_and_accumulate_with_seed_avx512(
head_input, block, seed,
)
}
}
#[inline(always)]
// SAFETY: head_input and block are valid mutable slices of equal length;
// block.len() % ch == 0; CPU supports AVX-512F+VL (verified by dispatch).
unsafe fn gated_activation_and_overwrite_block(
head_input: &mut [f32],
block: &mut [f32],
ch: usize,
) {
// SAFETY: head_input, block, and ch satisfy the function's documented invariants.
unsafe {
crate::math::wavenet::accumulate::gated_activation_and_overwrite_block_avx512(
head_input, block, ch,
)
}
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; CPU supports AVX-512F (verified by dispatch).
unsafe fn tanh_slice(slice: &mut [f32]) {
crate::math::activations::tanh_slice_avx512(slice)
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; CPU supports AVX-512F (verified by dispatch).
unsafe fn sigmoid_slice(slice: &mut [f32]) {
crate::math::activations::sigmoid_slice_avx512(slice)
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; CPU supports AVX-512F+VL (verified by dispatch).
unsafe fn tanh_slice_hf(slice: &mut [f32]) {
crate::math::activations::tanh::high_fidelity::tanh_poly_slice_avx512(slice)
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; CPU supports AVX-512F+VL (verified by dispatch).
unsafe fn sigmoid_slice_hf(slice: &mut [f32]) {
crate::math::activations::sigmoid::high_fidelity::sigmoid_poly_slice_avx512(slice)
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; CPU supports AVX-512F (verified by dispatch).
unsafe fn relu_slice(slice: &mut [f32]) {
crate::math::activations::relu_slice_avx512(slice)
}
#[inline(always)]
// SAFETY: slice and slopes are valid f32 slices with slopes.len() >= slice.len();
// CPU supports AVX-512F (verified by dispatch).
unsafe fn prelu_slice(slice: &mut [f32], slopes: &[f32]) {
crate::math::activations::prelu_slice_avx512(slice, slopes)
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; CPU supports AVX-512F (verified by dispatch).
unsafe fn softsign_slice(slice: &mut [f32]) {
crate::math::activations::softsign_slice_avx512(slice)
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; CPU supports AVX-512F (verified by dispatch).
unsafe fn silu_slice(slice: &mut [f32]) {
crate::math::activations::silu_slice_avx512(slice)
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; CPU supports AVX-512F (verified by dispatch).
unsafe fn hard_tanh_slice(slice: &mut [f32]) {
crate::math::activations::hard_tanh_slice_avx512(slice)
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; CPU supports AVX-512F (verified by dispatch).
unsafe fn hard_swish_slice(slice: &mut [f32]) {
crate::math::activations::hard_swish_slice_avx512(slice)
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; CPU supports AVX-512F (verified by dispatch).
unsafe fn fast_tanh_slice(slice: &mut [f32]) {
crate::math::activations::fast_tanh_slice_avx512(slice)
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; all scalar params (min_val, max_val,
// min_slope, max_slope) are finite f32 values; CPU supports AVX-512F (verified by dispatch).
unsafe fn leaky_hard_tanh_slice(
slice: &mut [f32],
min_val: f32,
max_val: f32,
min_slope: f32,
max_slope: f32,
) {
crate::math::activations::leaky_hard_tanh_slice_avx512(
slice, min_val, max_val, min_slope, max_slope,
)
}
#[inline(always)]
// SAFETY: buf is a valid mutable f32 buffer; CPU supports AVX-512F (verified by dispatch).
unsafe fn activation_tanh_block(buf: &mut [f32]) {
crate::math::activations::tanh_slice_avx512(buf)
}
#[inline(always)]
// SAFETY: gates, cell_state, and hidden_state are valid mutable f32 slices with
// gates.len() == 4 * hidden_size, cell_state.len() == hidden_size,
// hidden_state.len() == hidden_size; CPU supports AVX-512F (verified by dispatch).
unsafe fn fused_lstm_gates_dyn(
gates: &mut [f32],
cell_state: &mut [f32],
cell_error: &mut [f32],
hidden_state: &mut [f32],
hidden_size: usize,
) {
// SAFETY: gates, cell_state, hidden_state, and hidden_size satisfy the function's
// documented invariants.
unsafe {
crate::math::lstm::fused_lstm_gates_dyn_avx512(
gates,
cell_state,
cell_error,
hidden_state,
hidden_size,
)
}
}
};
}
macro_rules! impl_avx512vnni_bf16_activations {
() => {
#[inline(always)]
// SAFETY: dest and src are valid slices; CPU supports AVX-512 VNNI+BF16 (verified by dispatch).
unsafe fn accumulate_head(dest: &mut [f32], src: &[f32]) {
Avx512Math::accumulate_head(dest, src)
}
#[inline(always)]
// SAFETY: head_input and block are valid mutable slices; CPU supports AVX-512 VNNI+BF16.
unsafe fn tanh_and_accumulate_block(head_input: &mut [f32], block: &mut [f32]) {
Avx512Math::tanh_and_accumulate_block(head_input, block)
}
#[inline(always)]
// SAFETY: head_input and block are valid mutable slices; block.len() % ch == 0;
// CPU supports AVX-512 VNNI+BF16.
unsafe fn gated_activation_and_accumulate_block(
head_input: &mut [f32],
block: &mut [f32],
ch: usize,
) {
Avx512Math::gated_activation_and_accumulate_block(head_input, block, ch)
}
#[inline(always)]
// SAFETY: head_input and block are valid mutable slices of equal length;
// CPU supports AVX-512 VNNI+BF16.
unsafe fn tanh_and_overwrite_block(head_input: &mut [f32], block: &mut [f32]) {
Avx512Math::tanh_and_overwrite_block(head_input, block)
}
#[inline(always)]
// SAFETY: head_input, block, and seed are valid slices of equal length;
// CPU supports AVX-512 VNNI+BF16.
unsafe fn tanh_and_accumulate_with_seed(
head_input: &mut [f32],
block: &mut [f32],
seed: &[f32],
) {
Avx512Math::tanh_and_accumulate_with_seed(head_input, block, seed)
}
#[inline(always)]
// SAFETY: head_input and block are valid mutable slices of equal length;
// block.len() % ch == 0; CPU supports AVX-512 VNNI+BF16.
unsafe fn gated_activation_and_overwrite_block(
head_input: &mut [f32],
block: &mut [f32],
ch: usize,
) {
Avx512Math::gated_activation_and_overwrite_block(head_input, block, ch)
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; CPU supports AVX-512 VNNI+BF16.
unsafe fn tanh_slice(slice: &mut [f32]) {
Avx512Math::tanh_slice(slice)
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; CPU supports AVX-512 VNNI+BF16.
unsafe fn sigmoid_slice(slice: &mut [f32]) {
Avx512Math::sigmoid_slice(slice)
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; CPU supports AVX-512 VNNI+BF16.
unsafe fn tanh_slice_hf(slice: &mut [f32]) {
Avx512Math::tanh_slice_hf(slice)
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; CPU supports AVX-512 VNNI+BF16.
unsafe fn sigmoid_slice_hf(slice: &mut [f32]) {
Avx512Math::sigmoid_slice_hf(slice)
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; CPU supports AVX-512 VNNI+BF16.
unsafe fn relu_slice(slice: &mut [f32]) {
Avx512Math::relu_slice(slice)
}
#[inline(always)]
// SAFETY: slice and slopes are valid f32 slices; CPU supports AVX-512 VNNI+BF16.
unsafe fn prelu_slice(slice: &mut [f32], slopes: &[f32]) {
Avx512Math::prelu_slice(slice, slopes)
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; CPU supports AVX-512 VNNI+BF16.
unsafe fn softsign_slice(slice: &mut [f32]) {
Avx512Math::softsign_slice(slice)
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; CPU supports AVX-512 VNNI+BF16.
unsafe fn silu_slice(slice: &mut [f32]) {
Avx512Math::silu_slice(slice)
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; CPU supports AVX-512 VNNI+BF16.
unsafe fn hard_tanh_slice(slice: &mut [f32]) {
Avx512Math::hard_tanh_slice(slice)
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; CPU supports AVX-512 VNNI+BF16.
unsafe fn hard_swish_slice(slice: &mut [f32]) {
Avx512Math::hard_swish_slice(slice)
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; CPU supports AVX-512 VNNI+BF16.
unsafe fn fast_tanh_slice(slice: &mut [f32]) {
Avx512Math::fast_tanh_slice(slice)
}
#[inline(always)]
// SAFETY: slice is a valid mutable f32 buffer; all scalar params are finite f32;
// CPU supports AVX-512 VNNI+BF16.
unsafe fn leaky_hard_tanh_slice(
slice: &mut [f32],
min_val: f32,
max_val: f32,
min_slope: f32,
max_slope: f32,
) {
Avx512Math::leaky_hard_tanh_slice(slice, min_val, max_val, min_slope, max_slope)
}
#[inline(always)]
// SAFETY: buf is a valid mutable f32 buffer; CPU supports AVX-512 VNNI+BF16.
unsafe fn activation_tanh_block(buf: &mut [f32]) {
Avx512Math::activation_tanh_block(buf)
}
#[inline(always)]
// SAFETY: gates, cell_state, and hidden_state are valid mutable f32 slices;
// gates.len() == 4 * hidden_size, cell_state.len() == hidden_state.len() == hidden_size;
// CPU supports AVX-512 VNNI+BF16.
unsafe fn fused_lstm_gates_dyn(
gates: &mut [f32],
cell_state: &mut [f32],
cell_error: &mut [f32],
hidden_state: &mut [f32],
hidden_size: usize,
) {
Avx512Math::fused_lstm_gates_dyn(
gates,
cell_state,
cell_error,
hidden_state,
hidden_size,
)
}
};
}
pub(super) use impl_avx512_activations;
pub(super) use impl_avx512vnni_bf16_activations;