-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathbench_linalg.py
More file actions
179 lines (136 loc) · 6.15 KB
/
Copy pathbench_linalg.py
File metadata and controls
179 lines (136 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# -*- coding: utf-8 -*-
# *****************************************************************************
# Copyright (c) 2020, Intel Corporation
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# - Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# - Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
# *****************************************************************************
import numpy
import dpnp
from .common import TYPES1, Benchmark, get_indexes_rand, get_squares_
class Eindot(Benchmark):
params = [
[dpnp, numpy],
[16, 32, 64, 128, 256, 512, 1024],
["float64", "float32", "int64", "int32"],
]
param_names = ["executor", "size", "dtype"]
def setup(self, np, size, dtype):
dt = getattr(np, dtype)
# self.a = np.arange(60000.0).reshape(150, 400)
self.a = np.arange(size * size, dtype=dt).reshape((size, size))
# self.ac = self.a.copy()
# self.at = self.a.T
# self.atc = self.a.T.copy()
# self.b = np.arange(240000.0).reshape(400, 600)
self.b = np.arange(size * size, dtype=dt).reshape((size, size))
# self.c = np.arange(600)
# self.d = np.arange(400)
# self.a3 = np.arange(480000.).reshape(60, 80, 100)
# self.b3 = np.arange(192000.).reshape(80, 60, 40)
def time_dot_a_b(self, np):
np.dot(self.a, self.b)
def time_dot_d_dot_b_c(self, np, *args):
np.dot(self.d, np.dot(self.b, self.c))
def time_dot_trans_a_at(self, np, *args):
np.dot(self.a, self.at)
def time_dot_trans_a_atc(self, np, *args):
np.dot(self.a, self.atc)
def time_dot_trans_at_a(self, np, *args):
np.dot(self.at, self.a)
def time_dot_trans_atc_a(self, np, *args):
np.dot(self.atc, self.a)
def time_einsum_i_ij_j(self, np, *args):
np.einsum("i,ij,j", self.d, self.b, self.c)
def time_einsum_ij_jk_a_b(self, np, *args):
np.einsum("ij,jk", self.a, self.b)
def time_einsum_ijk_jil_kl(self, np, *args):
np.einsum("ijk,jil->kl", self.a3, self.b3)
def time_inner_trans_a_a(self, np, *args):
np.inner(self.a, self.a)
def time_inner_trans_a_ac(self, np, *args):
np.inner(self.a, self.ac)
def time_matmul_a_b(self, np, *args):
np.matmul(self.a, self.b)
def time_matmul_d_matmul_b_c(self, np, *args):
np.matmul(self.d, np.matmul(self.b, self.c))
def time_matmul_trans_a_at(self, np, *args):
np.matmul(self.a, self.at)
def time_matmul_trans_a_atc(self, np, *args):
np.matmul(self.a, self.atc)
def time_matmul_trans_at_a(self, np, *args):
np.matmul(self.at, self.a)
def time_matmul_trans_atc_a(self, np, *args):
np.matmul(self.atc, self.a)
def time_tensordot_a_b_axes_1_0_0_1(self, np, *args):
np.tensordot(self.a3, self.b3, axes=([1, 0], [0, 1]))
class Linalg(Benchmark):
params = [[dpnp, numpy], ["svd", "pinv", "det", "norm"], TYPES1]
param_names = ["executor", "op", "type"]
def setup(self, np, op, typename):
np.seterr(all="ignore")
self.func = getattr(np.linalg, op)
if op == "cholesky":
# we need a positive definite
self.a = np.dot(
get_squares_()[typename], get_squares_()[typename].T
)
else:
self.a = get_squares_()[typename]
# check that dtype is supported at all
try:
self.func(self.a[:2, :2])
except TypeError:
raise NotImplementedError()
def time_op(self, np, op, typename):
self.func(self.a)
class Lstsq(Benchmark):
params = [dpnp, numpy]
param_names = ["executor"]
def setup(self, np):
self.a = get_squares_()["float64"]
self.b = get_indexes_rand()[:100].astype(np.float64)
def time_numpy_linalg_lstsq_a__b_float64(self, np):
np.linalg.lstsq(self.a, self.b, rcond=-1)
# class Einsum(Benchmark):
# param_names = ['dtype']
# params = [[np.float64]]
# def setup(self, dtype):
# self.a = np.arange(2900, dtype=dtype)
# self.b = np.arange(3000, dtype=dtype)
# self.c = np.arange(24000, dtype=dtype).reshape(20, 30, 40)
# self.c1 = np.arange(1200, dtype=dtype).reshape(30, 40)
# self.d = np.arange(10000, dtype=dtype).reshape(10,100,10)
# #outer(a,b): trigger sum_of_products_contig_stride0_outcontig_two
# def time_einsum_outer(self, dtype):
# np.einsum("i,j", self.a, self.b, optimize=True)
# # multiply(a, b):trigger sum_of_products_contig_two
# def time_einsum_multiply(self, dtype):
# np.einsum("..., ...", self.c1, self.c , optimize=True)
# # sum and multiply:trigger sum_of_products_contig_stride0_outstride0_two
# def time_einsum_sum_mul(self, dtype):
# np.einsum(",i...->", 300, self.d, optimize=True)
# # sum and multiply:trigger sum_of_products_stride0_contig_outstride0_two
# def time_einsum_sum_mul2(self, dtype):
# np.einsum("i...,->", self.d, 300, optimize=True)