Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vectorized activation vm #1008

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions linalg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,10 @@ name = "x86_64"
harness = false

[[bench]]
bench = false
name = "intel"
harness = false

[[bench]]
name = "activations"
harness = false
20 changes: 20 additions & 0 deletions linalg/activations/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "activations"
version = "0.1.0"
edition = "2021"

[workspace]
members = []

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dev-dependencies]
criterion = "0.4.0"
proptest = "1.1.0"

[dependencies]


[[bench]]
name = "vm"
harness = false
50 changes: 50 additions & 0 deletions linalg/activations/benches/vm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use activations::{definitions, reference, Program};
use criterion::{black_box, criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};

fn crit(c: &mut Criterion, name: &str, r: impl Fn(f32) -> f32, prog: &Program) {
let mut group = c.benchmark_group(name);
for size in [1i32, 32, 256, 1024, 8192].iter() {
group.throughput(criterion::Throughput::Elements(*size as u64));
group.bench_with_input(BenchmarkId::new("Reference", size), size, |b, size| {
b.iter_batched(
|| vec![1.0f32; *size as usize],
|v| {
for x in v {
r(black_box(x));
}
},
BatchSize::LargeInput,
)
});
group.bench_with_input(BenchmarkId::new("VM", size), size, |b, size| {
b.iter_batched(
|| vec![1.0f32; *size as usize],
|v| {
for x in v {
prog.compute(black_box(x));
}
},
BatchSize::LargeInput,
)
});
group.bench_with_input(BenchmarkId::new("VMVec", size), size, |b, size| {
b.iter_batched(
|| vec![1.0f32; *size as usize],
|mut v| {
prog.compute_slice(black_box(&mut v));
},
BatchSize::LargeInput,
)
});
}
}

fn criterion_benchmark(c: &mut Criterion) {
crit(c, "relu", reference::relu, &definitions::relu());
crit(c, "hardswish", reference::hardswish, &definitions::hardswish());
crit(c, "exp2f", reference::exp2f, &definitions::exp2f());
crit(c, "sigmoid", reference::sigmoid, &definitions::sigmoid());
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
164 changes: 164 additions & 0 deletions linalg/activations/src/definitions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@

use super::Op::*;
use super::RegisterId::*;
use super::*;

pub fn relu() -> Program {
Program { ops: vec![MaxConst(0)], csts: vec![] }
}

pub fn affine(alpha: f32, beta: f32) -> Program {
Program {
#[rustfmt::skip]
ops: vec![
MulConst(2),
AddConst(3),
],
csts: vec![alpha, beta],
}
}

pub fn leaky_relu(alpha: f32) -> Program {
Program {
#[rustfmt::skip]
ops: vec![
Move(B,A),
MulConst(2),
Move(C,A),
Move(A,B),
IfPosTE,
],
csts: vec![alpha],
}
}

pub fn threshold_relu(alpha: f32) -> Program {
Program {
#[rustfmt::skip]
ops: vec![
Move(B,A),
SubConst(2),
Load(C,0),
IfPosTE,
],
csts: vec![alpha],
}
}

pub fn softsign() -> Program {
Program {
#[rustfmt::skip]
ops: vec![
Move(B,A),
Abs,
AddConst(1),
Recip,
Mul,
],
csts: vec![],
}
}

pub fn hardswish() -> Program {
Program {
#[rustfmt::skip]
ops: vec![
Move(B, A),
MulConst(2),
AddConst(3),
MinConst(1),
MaxConst(0),
Mul,
],
csts: vec![1f32 / 6., 0.5],
}
}

pub fn sigmoid() -> Program {
Program {
#[rustfmt::skip]
ops: vec![
MinConst(3),
MaxConst(2),
Move(B, A), // b = x
Move(C, A), // c = x
Mul, // a = x2
Move(B, A), // b = x2
MulConst(4),
AddConst(5), // a = x2 * a13 + a11
FMA(6),
FMA(7),
FMA(8),
FMA(9),
FMA(10),
SwapBC, // c = x2, b = x
Mul, // a = p(x)
Move(B, C), // b = x2
Move(C, A), // c = p(x)
Move(A, B), // a = x2
MulConst(11),
AddConst(12),
FMA(13),
FMA(1), // a = q(x)
Recip,
Move(B,C), // b = p(x)
Mul,
AddConst(14)
],
csts: vec![
-18.6, // const 2
18.6, // const 3
-4.433153405e-18, // const 4, also alpha_13
1.169974371e-14, // const 5, also a11
-1.875289645e-11,
4.257889523e-8,
0.00004811817576, // const 8
0.008163842030,
0.2499999971, // alpha_1
3.922935744e-6, // beta_6
0.001524872358, // const 12
0.1159886749,
0.5, //beta_0
],
}
}

pub fn exp2f() -> Program {
Program {
#[rustfmt::skip]
ops: vec![
MinConst(2),
MaxConst(3),
Move(B, A), // b = x
AddConst(4), // a = x + 0.5
Floor, // a = ipart
Move(C, A), // c = ipart
Move(A, B), // a = x
Move(B, C), // b = ipart
Sub, // a = fpart
Move(B, A), // b = fpart
Load(A, 5), // a = exp2p[0]
FMA(6),
FMA(7),
FMA(8),
FMA(9),
FMA(10),
FMA(1), // a = y
Move(B, A),
Move(A, C),
TwoPowOfInt,
Mul
],
csts: vec![
127f32,
-127f32,
0.5,
1.535336188319500e-4,
1.339887440266574e-3,
9.618437357674640e-3,
5.550332471162809e-2,
2.402264791363012e-1,
6.931472028550421e-1,
],
}
}
Loading