Skip to content

Commit 6616812

Browse files
bwastiMikhail Zolotukhin
authored and
Mikhail Zolotukhin
committed
add hard swish bench (pytorch#234)
1 parent 2143b3b commit 6616812

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

benchmarks/tensorexpr/benchmark.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#import pooling
1313
#import conv
1414
#import matmul
15+
import swish
1516

1617

1718
def main():

benchmarks/tensorexpr/framework.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
class BenchmarkBase(object):
99
def __init__(self, mode, device):
1010
self.mode = mode
11+
self.deterministic = False
1112
self.device = device
1213
if mode == 'both':
1314
self.requires_grad = True

benchmarks/tensorexpr/swish.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import framework
2+
import scipy.special
3+
import numpy as np
4+
import torch
5+
6+
7+
class SwishBench(framework.Benchmark):
8+
def __init__(self, mode, device, M, N):
9+
super().__init__(mode, device)
10+
self.M = M
11+
self.N = N
12+
self.data = self.rand([M, N], device=device, requires_grad=self.requires_grad)
13+
self.inputs = [self.data]
14+
self.zeros = torch.zeros(M, N, device=device)
15+
self.six = self.zeros + 6.0
16+
self.three = self.zeros + 3.0
17+
self.sixth = self.zeros + 1.0 / 6.0
18+
19+
def forward(self, inp):
20+
y = inp * (torch.min(torch.relu(inp), self.six) + self.three) * self.sixth
21+
return y
22+
23+
def reference(self):
24+
return self.numpy(self.forward(self.data))
25+
26+
def config(self):
27+
return [self.M, self.N]
28+
29+
@staticmethod
30+
def module():
31+
return "swish"
32+
33+
def memory_workload(self):
34+
if self.mode == "fwd":
35+
sol_count = 1 + 1
36+
algorithmic_count = 3 + 1
37+
else:
38+
sol_count = (1 + 1) + (1 + 1)
39+
algorithmic_count = (3 + 1) + (3 + 1)
40+
41+
buffer_size = self.M * self.N * 4
42+
return {
43+
"sol": buffer_size * sol_count,
44+
"algorithmic": buffer_size * algorithmic_count,
45+
}
46+
47+
@staticmethod
48+
def default_configs():
49+
return [[128, 1 << 16]]
50+
51+
52+
framework.register_benchmark_class(SwishBench)

0 commit comments

Comments
 (0)