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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 Fábio Henrique de Lima Silva (fhl.bsb@gmail.com) All rights reserved.
use crate::math::common::{AlignedVec, SimdMath};
/// 1x1 Dense Layer (The Channel Mixer):
/// Think of this layer as a 'digital mixing console'. It blends the various
/// audio channels coming from the previous stage to create the final timbre combination.
#[derive(Clone)]
pub struct DenseLayer<const IN: usize, const OUT: usize> {
/// Weight matrix: Defines 'how much' of each channel goes into the mix.
pub weights: AlignedVec<f32>,
/// Bias: A basic 'volume' adjustment for each output channel.
pub bias: AlignedVec<f32>,
/// Flag indicating whether bias should be applied.
pub do_bias: bool,
}
impl<const IN: usize, const OUT: usize> DenseLayer<IN, OUT> {
/// Full-precision f32 fused residual batch.
///
/// Fuses the 1x1 GEMV with bias and residual addition into a single SIMD
/// pass using the native f32 weight tensor.
///
/// # Safety
/// The caller must guarantee compatible sizes and buffer validity.
#[inline(always)]
pub unsafe fn process_residual_batch<M: SimdMath>(
&self,
input: &[f32],
residual: &[f32],
output: &mut [f32],
num_frames: usize,
) {
unsafe {
// Slots for which we have specialised const-generic kernels
// (endereçamento imediato — no leaq chains).
#[cfg(target_arch = "x86_64")]
{
if IN == 4 && OUT == 4 {
crate::math::gemm::gemm_batch::fused_gemm_residual_batch_f32_const::<4, 4>(
input,
&self.weights,
&self.bias,
residual,
output,
num_frames,
self.do_bias,
);
return;
}
if IN == 8 && OUT == 8 {
crate::math::gemm::gemm_batch::fused_gemm_residual_batch_f32_const::<8, 8>(
input,
&self.weights,
&self.bias,
residual,
output,
num_frames,
self.do_bias,
);
return;
}
if IN == 12 && OUT == 12 {
crate::math::gemm::gemm_batch::fused_gemm_residual_batch_f32_12x12(
input,
&self.weights,
&self.bias,
residual,
output,
num_frames,
self.do_bias,
);
return;
}
if IN == 16 && OUT == 16 {
crate::math::gemm::gemm_batch::fused_gemm_residual_batch_f32_const::<16, 16>(
input,
&self.weights,
&self.bias,
residual,
output,
num_frames,
self.do_bias,
);
return;
}
}
M::fused_gemm_residual_batch_f32(
input,
&self.weights,
&self.bias,
residual,
output,
num_frames,
self.do_bias,
);
}
}
/// Full-precision f32 head projection.
///
/// Dispatches to the appropriate SIMD kernel via the `SimdMath` trait,
/// replacing the previous scalar triple-nested loop with shape-dependent
/// vectorization (frame-batching for OUT≤4, channel-batching for OUT≥8).
///
/// # Safety
/// The caller must ensure that `in_frame` and `out_frame` have sizes
/// compatible with `IN`, `OUT`, and `num_frames`, and that the SIMD
/// instructions for `M` are available on the host CPU.
#[inline(always)]
pub unsafe fn process_block<M: SimdMath>(
&self,
input: &[f32],
output: &mut [f32],
num_frames: usize,
) {
unsafe {
#[cfg(target_arch = "x86_64")]
{
if IN == 1 {
if self.do_bias {
crate::math::gemm::gemv::broadcast_scale_with_bias_f32_avx2::<OUT>(
input,
&self.weights,
&self.bias,
output,
);
} else {
crate::math::gemm::gemv::broadcast_scale_f32_avx2::<OUT>(
input,
&self.weights,
output,
);
}
return;
}
}
if self.do_bias {
M::gemv_with_bias_f32(input, &self.weights, &self.bias, output, num_frames);
} else {
M::gemv_no_bias_f32(input, &self.weights, output, num_frames);
}
}
}
}