pytorch/benchmarks/tensorexpr/swish.py
Mikhail Zolotukhin e93e7b2795 [TensorExpr] Add tensorexpr benchmarks. (#34230)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/34230

This PR adds some benchmarks that we used to assess tensor expressions performance.

Differential Revision: D20251830

Test Plan: Imported from OSS

Pulled By: ZolotukhinM

fbshipit-source-id: bafd66ce32f63077e3733112d854f5c750d5b1af
2020-03-16 11:49:39 -07:00

53 lines
1.4 KiB
Python

import framework
import scipy.special
import numpy as np
import torch
class SwishBench(framework.Benchmark):
def __init__(self, mode, device, M, N):
super().__init__(mode, device)
self.M = M
self.N = N
self.data = self.rand([M, N], device=device, requires_grad=self.requires_grad)
self.inputs = [self.data]
self.zeros = torch.zeros(M, N, device=device)
self.six = self.zeros + 6.0
self.three = self.zeros + 3.0
self.sixth = self.zeros + 1.0 / 6.0
def forward(self, inp):
y = inp * (torch.min(torch.relu(inp), self.six) + self.three) * self.sixth
return y
def reference(self):
return self.numpy(self.forward(self.data))
def config(self):
return [self.M, self.N]
@staticmethod
def module():
return "swish"
def memory_workload(self):
if self.mode == "fwd":
sol_count = 1 + 1
algorithmic_count = 3 + 1
else:
sol_count = (1 + 1) + (1 + 1)
algorithmic_count = (3 + 1) + (3 + 1)
buffer_size = self.M * self.N * 4
return {
"sol": buffer_size * sol_count,
"algorithmic": buffer_size * algorithmic_count,
}
@staticmethod
def default_configs():
return [[128, 1 << 16]]
framework.register_benchmark_class(SwishBench)