#[derive(Debug)]
pub(crate) struct Matrix {
pub(crate) rows: usize,
pub(crate) cols: usize,
pub(crate) data: Vec<f32>,
}
impl Matrix {
fn row(&self, row: usize) -> &[f32] {
&self.data[row * self.cols..(row + 1) * self.cols]
}
}
const TILE: usize = 16;
pub(crate) const BATCH: usize = 4;
const _: () = assert!(BATCH == 4);
#[derive(Debug)]
pub(crate) struct Tiled {
rows: usize,
cols: usize,
columns: Vec<[f32; TILE]>,
}
impl Tiled {
pub(crate) fn new(matrix: &Matrix) -> Self {
let tiles = matrix.rows.div_ceil(TILE);
let mut columns = vec![[0.0; TILE]; tiles * matrix.cols];
for row in 0..matrix.rows {
let (tile, offset) = (row / TILE, row % TILE);
for (col, &value) in matrix.row(row).iter().enumerate() {
columns[tile * matrix.cols + col][offset] = value;
}
}
Tiled {
rows: matrix.rows,
cols: matrix.cols,
columns,
}
}
pub(crate) fn apply_add(&self, input: &[f32], out: &mut [f32]) {
#[cfg(target_arch = "x86_64")]
if fused() {
return unsafe { self.apply_add_fused(input, out) };
}
self.apply_add_with::<false>(input, out);
}
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2,fma")]
fn apply_add_fused(&self, input: &[f32], out: &mut [f32]) {
self.apply_add_with::<true>(input, out);
}
#[inline(always)]
fn apply_add_with<const FUSED: bool>(&self, input: &[f32], out: &mut [f32]) {
let mut tiles = self.columns.chunks_exact(self.cols).enumerate();
while let Some((tile, first)) = tiles.next() {
let Some((_, second)) = tiles.next() else {
let mut sums = [0.0f32; TILE];
for (column, &value) in first.iter().zip(input) {
for j in 0..TILE {
sums[j] = madd::<FUSED>(value, column[j], sums[j]);
}
}
self.add_tile(tile, &sums, out);
break;
};
let (mut a, mut b) = ([0.0f32; TILE], [0.0f32; TILE]);
for ((ca, cb), &value) in first.iter().zip(second).zip(input) {
for j in 0..TILE {
a[j] = madd::<FUSED>(value, ca[j], a[j]);
b[j] = madd::<FUSED>(value, cb[j], b[j]);
}
}
self.add_tile(tile, &a, out);
self.add_tile(tile + 1, &b, out);
}
}
#[inline(always)]
fn add_tile(&self, tile: usize, sums: &[f32; TILE], out: &mut [f32]) {
let start = tile * TILE;
let end = (start + TILE).min(self.rows);
for (out, sum) in out[start..end].iter_mut().zip(sums) {
*out += sum;
}
}
pub(crate) fn apply_all(&self, inputs: &[f32], bias: &[f32], out: &mut [f32]) {
#[cfg(target_arch = "x86_64")]
if fused() {
return unsafe { self.apply_all_fused(inputs, bias, out) };
}
self.apply_all_with::<false>(inputs, bias, out);
}
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2,fma")]
fn apply_all_fused(&self, inputs: &[f32], bias: &[f32], out: &mut [f32]) {
self.apply_all_with::<true>(inputs, bias, out);
}
#[inline(always)]
fn apply_all_with<const FUSED: bool>(&self, inputs: &[f32], bias: &[f32], out: &mut [f32]) {
let count = inputs.len() / self.cols;
let mut padded = vec![[0.0f32; BATCH]; self.cols];
for first in (0..count).step_by(BATCH) {
let batch = (count - first).min(BATCH);
for (col, values) in padded.iter_mut().enumerate() {
for (b, value) in values.iter_mut().enumerate() {
*value = if b < batch {
inputs[(first + b) * self.cols + col]
} else {
0.0
};
}
}
for (tile, columns) in self.columns.chunks_exact(self.cols).enumerate() {
let (mut s0, mut s1, mut s2, mut s3) =
([0.0f32; TILE], [0.0; TILE], [0.0; TILE], [0.0; TILE]);
for (column, values) in columns.iter().zip(&padded) {
axpy::<FUSED>(&mut s0, column, values[0]);
axpy::<FUSED>(&mut s1, column, values[1]);
axpy::<FUSED>(&mut s2, column, values[2]);
axpy::<FUSED>(&mut s3, column, values[3]);
}
let sums = [s0, s1, s2, s3];
for (b, sums) in sums.iter().enumerate().take(batch) {
for (j, sum) in sums.iter().enumerate() {
let row = tile * TILE + j;
if row < self.rows {
out[(first + b) * self.rows + row] = sum + bias[row];
}
}
}
}
}
}
}
#[inline(always)]
fn axpy<const FUSED: bool>(sums: &mut [f32; TILE], column: &[f32; TILE], value: f32) {
for (sum, &weight) in sums.iter_mut().zip(column) {
*sum = madd::<FUSED>(value, weight, *sum);
}
}
#[inline(always)]
fn madd<const FUSED: bool>(a: f32, b: f32, c: f32) -> f32 {
if FUSED || cfg!(target_arch = "aarch64") {
a.mul_add(b, c)
} else {
a * b + c
}
}
#[cfg(target_arch = "x86_64")]
fn fused() -> bool {
std::is_x86_feature_detected!("avx2") && std::is_x86_feature_detected!("fma")
}
#[inline(always)]
pub(crate) fn exp(x: f32) -> f32 {
const ROUND: f32 = 12_582_912.0;
let x = x.clamp(-87.0, 88.0);
let n = (x * std::f32::consts::LOG2_E + ROUND) - ROUND;
let r = x - n * 0.693_145_75 - n * 1.428_606_8e-6;
let p = 1.0
+ r * (1.0
+ r * (0.5 + r * (1.0 / 6.0 + r * (1.0 / 24.0 + r * (1.0 / 120.0 + r / 720.0)))));
p * f32::from_bits(((n as i32 + 127) as u32) << 23)
}
#[inline(always)]
fn sigmoid(x: f32) -> f32 {
1.0 / (1.0 + exp(-x))
}
#[inline(always)]
fn tanh(x: f32) -> f32 {
2.0 / (1.0 + exp(-2.0 * x)) - 1.0
}
#[derive(Debug)]
pub(crate) struct Lstm {
pub(crate) input: Tiled,
pub(crate) hidden: Tiled,
pub(crate) bias: Vec<f32>,
}
pub(crate) fn dot(a: &[f32], b: &[f32]) -> f32 {
let mut sums = [0.0f32; 8];
let (left, right) = (a.as_chunks::<8>(), b.as_chunks::<8>());
for (x, y) in left.0.iter().zip(right.0) {
for i in 0..8 {
sums[i] += x[i] * y[i];
}
}
let tail: f32 = left.1.iter().zip(right.1).map(|(x, y)| x * y).sum();
sums.iter().sum::<f32>() + tail
}
#[inline(always)]
fn step(gates: &mut [f32], h: &mut [f32], c: &mut [f32]) {
let hidden = h.len();
let (sigmoids, rest) = gates.split_at_mut(2 * hidden);
let (candidates, outputs) = rest.split_at_mut(hidden);
sigmoids
.iter_mut()
.for_each(|value| *value = sigmoid(*value));
candidates
.iter_mut()
.for_each(|value| *value = tanh(*value));
outputs
.iter_mut()
.for_each(|value| *value = sigmoid(*value));
let (inputs, forgets) = sigmoids.split_at(hidden);
for i in 0..hidden {
c[i] = forgets[i] * c[i] + inputs[i] * candidates[i];
}
for i in 0..hidden {
h[i] = outputs[i] * tanh(c[i]);
}
}
pub(crate) fn bilstm(layer: &[Lstm; 2], input: &[f32], n: usize, d: usize) -> Vec<f32> {
let hidden = d / 2;
let mut output = vec![0.0f32; n * d];
let gates_input = layer.each_ref().map(|cell| {
let mut gates = vec![0.0f32; n * 4 * hidden];
cell.input.apply_all(input, &cell.bias, &mut gates);
gates
});
let mut states = [(); 2].map(|()| (vec![0.0f32; hidden], vec![0.0f32; hidden]));
let mut gates = [(); 2].map(|()| vec![0.0f32; 4 * hidden]);
for t_step in 0..n {
for direction in 0..2 {
let t = if direction == 0 {
t_step
} else {
n - 1 - t_step
};
let (h, c) = &mut states[direction];
let gates = &mut gates[direction];
gates.copy_from_slice(&gates_input[direction][t * 4 * hidden..(t + 1) * 4 * hidden]);
layer[direction].hidden.apply_add(h, gates);
step(gates, h, c);
output[t * d + direction * hidden..t * d + (direction + 1) * hidden].copy_from_slice(h);
}
}
output
}
pub(crate) fn bilstm_many(
layer: &[Lstm; 2],
inputs: &[&[f32]],
lens: &[usize],
d: usize,
) -> Vec<Vec<f32>> {
debug_assert!(inputs.len() <= BATCH && inputs.len() == lens.len());
let hidden = d / 2;
let count = inputs.len();
let longest = lens.iter().copied().max().unwrap_or(0);
let mut outputs: Vec<Vec<f32>> = lens.iter().map(|&n| vec![0.0f32; n * d]).collect();
let zero = vec![0.0f32; 4 * hidden];
for (direction, cell) in layer.iter().enumerate() {
let gates_input: Vec<Vec<f32>> = inputs
.iter()
.zip(lens)
.map(|(input, &n)| {
let mut gates = vec![0.0f32; n * 4 * hidden];
cell.input.apply_all(input, &cell.bias, &mut gates);
gates
})
.collect();
let mut h = vec![0.0f32; count * hidden];
let mut c = vec![0.0f32; count * hidden];
let mut sums = vec![0.0f32; count * 4 * hidden];
for t_step in 0..longest {
cell.hidden.apply_all(&h, &zero, &mut sums);
for (s, &n) in lens.iter().enumerate() {
if t_step >= n {
continue;
}
let t = if direction == 0 {
t_step
} else {
n - 1 - t_step
};
let gates = &mut sums[s * 4 * hidden..(s + 1) * 4 * hidden];
for (gate, input) in gates
.iter_mut()
.zip(&gates_input[s][t * 4 * hidden..(t + 1) * 4 * hidden])
{
*gate += input;
}
let (hs, cs) = (
&mut h[s * hidden..(s + 1) * hidden],
&mut c[s * hidden..(s + 1) * hidden],
);
step(gates, hs, cs);
outputs[s][t * d + direction * hidden..t * d + (direction + 1) * hidden]
.copy_from_slice(hs);
}
}
}
outputs
}