Skip to content

Commit 0478196

Browse files
committed
vectorizing computation
1 parent 82250d4 commit 0478196

File tree

4 files changed

+399
-320
lines changed

4 files changed

+399
-320
lines changed

linalg/activations/benches/vm.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ fn crit(c: &mut Criterion, name: &str, r: impl Fn(f32) -> f32, prog: &Program) {
2727
BatchSize::LargeInput,
2828
)
2929
});
30+
group.bench_with_input(BenchmarkId::new("VMVec", size), size, |b, size| {
31+
b.iter_batched(
32+
|| vec![1.0f32; *size as usize],
33+
|mut v| {
34+
prog.compute_slice(black_box(&mut v));
35+
},
36+
BatchSize::LargeInput,
37+
)
38+
});
3039
}
3140
}
3241

linalg/activations/src/definitions.rs

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
2+
use super::Op::*;
3+
use super::RegisterId::*;
4+
use super::*;
5+
6+
pub fn relu() -> Program {
7+
Program { ops: vec![MaxConst(0)], csts: vec![] }
8+
}
9+
10+
pub fn affine(alpha: f32, beta: f32) -> Program {
11+
Program {
12+
#[rustfmt::skip]
13+
ops: vec![
14+
MulConst(2),
15+
AddConst(3),
16+
],
17+
csts: vec![alpha, beta],
18+
}
19+
}
20+
21+
pub fn leaky_relu(alpha: f32) -> Program {
22+
Program {
23+
#[rustfmt::skip]
24+
ops: vec![
25+
Move(B,A),
26+
MulConst(2),
27+
Move(C,A),
28+
Move(A,B),
29+
IfPosTE,
30+
],
31+
csts: vec![alpha],
32+
}
33+
}
34+
35+
pub fn threshold_relu(alpha: f32) -> Program {
36+
Program {
37+
#[rustfmt::skip]
38+
ops: vec![
39+
Move(B,A),
40+
SubConst(2),
41+
Load(C,0),
42+
IfPosTE,
43+
],
44+
csts: vec![alpha],
45+
}
46+
}
47+
48+
pub fn softsign() -> Program {
49+
Program {
50+
#[rustfmt::skip]
51+
ops: vec![
52+
Move(B,A),
53+
Abs,
54+
AddConst(1),
55+
Recip,
56+
Mul,
57+
],
58+
csts: vec![],
59+
}
60+
}
61+
62+
pub fn hardswish() -> Program {
63+
Program {
64+
#[rustfmt::skip]
65+
ops: vec![
66+
Move(B, A),
67+
MulConst(2),
68+
AddConst(3),
69+
MinConst(1),
70+
MaxConst(0),
71+
Mul,
72+
],
73+
csts: vec![1f32 / 6., 0.5],
74+
}
75+
}
76+
77+
pub fn sigmoid() -> Program {
78+
Program {
79+
#[rustfmt::skip]
80+
ops: vec![
81+
MinConst(3),
82+
MaxConst(2),
83+
Move(B, A), // b = x
84+
Move(C, A), // c = x
85+
Mul, // a = x2
86+
Move(B, A), // b = x2
87+
MulConst(4),
88+
AddConst(5), // a = x2 * a13 + a11
89+
FMA(6),
90+
FMA(7),
91+
FMA(8),
92+
FMA(9),
93+
FMA(10),
94+
SwapBC, // c = x2, b = x
95+
Mul, // a = p(x)
96+
Move(B, C), // b = x2
97+
Move(C, A), // c = p(x)
98+
Move(A, B), // a = x2
99+
MulConst(11),
100+
AddConst(12),
101+
FMA(13),
102+
FMA(1), // a = q(x)
103+
Recip,
104+
Move(B,C), // b = p(x)
105+
Mul,
106+
AddConst(14)
107+
],
108+
csts: vec![
109+
-18.6, // const 2
110+
18.6, // const 3
111+
-4.433153405e-18, // const 4, also alpha_13
112+
1.169974371e-14, // const 5, also a11
113+
-1.875289645e-11,
114+
4.257889523e-8,
115+
0.00004811817576, // const 8
116+
0.008163842030,
117+
0.2499999971, // alpha_1
118+
3.922935744e-6, // beta_6
119+
0.001524872358, // const 12
120+
0.1159886749,
121+
0.5, //beta_0
122+
],
123+
}
124+
}
125+
126+
pub fn exp2f() -> Program {
127+
Program {
128+
#[rustfmt::skip]
129+
ops: vec![
130+
MinConst(2),
131+
MaxConst(3),
132+
Move(B, A), // b = x
133+
AddConst(4), // a = x + 0.5
134+
Floor, // a = ipart
135+
Move(C, A), // c = ipart
136+
Move(A, B), // a = x
137+
Move(B, C), // b = ipart
138+
Sub, // a = fpart
139+
Move(B, A), // b = fpart
140+
Load(A, 5), // a = exp2p[0]
141+
FMA(6),
142+
FMA(7),
143+
FMA(8),
144+
FMA(9),
145+
FMA(10),
146+
FMA(1), // a = y
147+
Move(B, A),
148+
Move(A, C),
149+
TwoPowOfInt,
150+
Mul
151+
],
152+
csts: vec![
153+
127f32,
154+
-127f32,
155+
0.5,
156+
1.535336188319500e-4,
157+
1.339887440266574e-3,
158+
9.618437357674640e-3,
159+
5.550332471162809e-2,
160+
2.402264791363012e-1,
161+
6.931472028550421e-1,
162+
],
163+
}
164+
}

0 commit comments

Comments
 (0)