Skip to content

Commit 386ec2f

Browse files
committed
linting for benchmarks
1 parent 8226e2e commit 386ec2f

File tree

11 files changed

+117
-28
lines changed

11 files changed

+117
-28
lines changed

benchmarks/src/graphs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
if name not in TESTS_GRAPH_NAME:
8686
TESTS_GRAPH_NAME[name] = name
8787

88+
8889
def get_benchmark_data():
8990
results = {}
9091
descriptions = {}

benchmarks/src/pytest_benchmark/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,4 @@ def initialize_package(PKG_ID):
104104
elif PKG_ID == "cupynumeric":
105105
pass
106106
else:
107-
raise NotImplementedError()
107+
raise NotImplementedError()

benchmarks/src/pytest_benchmark/test_blackscholes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def cnd(x):
9292

9393
return (C, P)
9494

95+
9596
def black_scholes_cupynumeric(S, X, R, V, T):
9697
# S = Underlying stock price
9798
# X = Strike Price
@@ -116,6 +117,7 @@ def cnd(x):
116117

117118
return (C, P)
118119

120+
119121
def black_scholes_arrayfire(S, X, R, V, T):
120122
def cnd(x):
121123
temp = x > 0
@@ -172,5 +174,5 @@ def generate_arrays(pkgid, count):
172174
"numpy": black_scholes_numpy,
173175
"cupy": black_scholes_cupy,
174176
"arrayfire": black_scholes_arrayfire,
175-
"cupynumeric": black_scholes_cupynumeric
177+
"cupynumeric": black_scholes_cupynumeric,
176178
}

benchmarks/src/pytest_benchmark/test_fft.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ def fft_cupy(arr):
8989
cupy.cuda.runtime.deviceSynchronize()
9090
return res
9191

92+
9293
def fft_cupynumeric(arr):
9394
return cupynumeric.fft.fft(arr)
9495

96+
9597
FUNCS = {"dpnp": fft_dpnp, "numpy": fft_np, "cupy": fft_cupy, "arrayfire": fft_af, "cupynumeric": fft_cupynumeric}

benchmarks/src/pytest_benchmark/test_gemm.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ def gemm_cupy(A, B, C):
120120
cupy.cuda.runtime.deviceSynchronize()
121121
return C
122122

123+
123124
def gemm_cupynumeric(A, B, C):
124125
return alpha * cupynumeric.matmul(A, B) + beta * C
125126

127+
126128
FUNCS = {"numpy": gemm_np, "cupy": gemm_cupy, "arrayfire": gemm_af, "dpnp": gemm_dpnp, "cupynumeric": gemm_cupynumeric}

benchmarks/src/pytest_benchmark/test_kmeans.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ class TestKmeans:
1212
def test_kmeans(self, benchmark, pkgid):
1313
initialize_package(pkgid)
1414
pkg = PKGDICT[pkgid]
15-
kmean_class = {"dpnp": kmeans_dpnp, "numpy": kmeans_numpy, "cupy": kmeans_cupy, "arrayfire": kmeans_af,
16-
"cupynumeric": kmeans_cupynumeric}
15+
kmean_class = {
16+
"dpnp": kmeans_dpnp,
17+
"numpy": kmeans_numpy,
18+
"cupy": kmeans_cupy,
19+
"arrayfire": kmeans_af,
20+
"cupynumeric": kmeans_cupynumeric,
21+
}
1722
obj = kmean_class[pkg.__name__]()
1823

1924
benchmark.extra_info["description"] = f"{NSAMPLES}x{NFEATURES} over {K} centers"
@@ -190,7 +195,6 @@ def kmeans(self):
190195
return centroids, cluster_assignments
191196

192197

193-
194198
class kmeans_cupynumeric:
195199
def __init__(self):
196200
self.data = cupynumeric.random.random((NSAMPLES, NFEATURES))
@@ -221,7 +225,9 @@ def assign_to_clusters(self, centroids):
221225
Returns:
222226
np.ndarray: An array of cluster assignments for each data point (n_samples,).
223227
"""
224-
distances = cupynumeric.sqrt(((self.data[:, cupynumeric.newaxis, :] - centroids[cupynumeric.newaxis, :, :]) ** 2).sum(axis=2))
228+
distances = cupynumeric.sqrt(
229+
((self.data[:, cupynumeric.newaxis, :] - centroids[cupynumeric.newaxis, :, :]) ** 2).sum(axis=2)
230+
)
225231
cluster_assignments = cupynumeric.argmin(distances, axis=1)
226232
return cluster_assignments
227233

benchmarks/src/pytest_benchmark/test_linalg.py

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,11 @@ def svd_cupy(arr):
9393
cupy.cuda.runtime.deviceSynchronize()
9494
return x
9595

96+
9697
def svd_cupynumeric(arr):
9798
return cupynumeric.linalg.svd(arr)
9899

100+
99101
def qr_np(arr):
100102
return np.linalg.qr(arr)
101103

@@ -117,9 +119,11 @@ def qr_cupy(arr):
117119
cupy.cuda.runtime.deviceSynchronize()
118120
return x
119121

122+
120123
def qr_cupynumeric(arr):
121124
return cupynumeric.linalg.qr(arr)
122125

126+
123127
def cholesky_np(arr):
124128
return np.linalg.cholesky(arr)
125129

@@ -140,6 +144,7 @@ def cholesky_cupy(arr):
140144
cupy.cuda.runtime.deviceSynchronize()
141145
return x
142146

147+
143148
def cholesky_cupynumeric(arr):
144149
return cupynumeric.linalg.cholesky(arr)
145150

@@ -164,9 +169,11 @@ def inv_cupy(arr):
164169
cupy.cuda.runtime.deviceSynchronize()
165170
return x
166171

172+
167173
def inv_cupynumeric(arr):
168174
return cupynumeric.linalg.inv(arr)
169175

176+
170177
def det_np(arr):
171178
return np.linalg.det(arr)
172179

@@ -186,9 +193,11 @@ def det_cupy(arr):
186193
cupy.cuda.runtime.deviceSynchronize()
187194
return x
188195

196+
189197
def det_cupynumeric(arr):
190198
return cupynumeric.linalg.det(arr)
191199

200+
192201
def norm_np(arr):
193202
return np.linalg.norm(arr)
194203

@@ -208,9 +217,11 @@ def norm_cupy(arr):
208217
cupy.cuda.runtime.deviceSynchronize()
209218
return x
210219

220+
211221
def norm_cupynumeric(arr):
212222
return cupynumeric.linalg.norm(arr)
213223

224+
214225
@pytest.mark.parametrize("pkgid", IDS, ids=IDS)
215226
class TestLinalg:
216227
def test_cholesky(self, benchmark, pkgid):
@@ -220,8 +231,13 @@ def test_cholesky(self, benchmark, pkgid):
220231
benchmark.extra_info["description"] = f"{NSIZE}x{NSIZE} Matrix"
221232
pkg = PKGDICT[pkgid]
222233

223-
CHOLESKY_FUNCS = {"numpy": cholesky_np, "cupy": cholesky_cupy, "arrayfire": cholesky_af, "dpnp": cholesky_dpnp,
224-
"cupynumeric": cholesky_cupynumeric }
234+
CHOLESKY_FUNCS = {
235+
"numpy": cholesky_np,
236+
"cupy": cholesky_cupy,
237+
"arrayfire": cholesky_af,
238+
"dpnp": cholesky_dpnp,
239+
"cupynumeric": cholesky_cupynumeric,
240+
}
225241
result = benchmark.pedantic(
226242
target=CHOLESKY_FUNCS[pkg.__name__], setup=setup, rounds=ROUNDS, iterations=ITERATIONS
227243
)
@@ -233,8 +249,13 @@ def test_svd(self, benchmark, pkgid):
233249
benchmark.extra_info["description"] = f"{NSIZE}x{NSIZE} Matrix"
234250
pkg = PKGDICT[pkgid]
235251

236-
SVD_FUNCS = {"numpy": svd_np, "cupy": svd_cupy, "arrayfire": svd_af, "dpnp": svd_dpnp,
237-
"cupynumeric": svd_cupynumeric }
252+
SVD_FUNCS = {
253+
"numpy": svd_np,
254+
"cupy": svd_cupy,
255+
"arrayfire": svd_af,
256+
"dpnp": svd_dpnp,
257+
"cupynumeric": svd_cupynumeric,
258+
}
238259
result = benchmark.pedantic(target=SVD_FUNCS[pkg.__name__], setup=setup, rounds=ROUNDS, iterations=ITERATIONS)
239260

240261
def test_qr(self, benchmark, pkgid):
@@ -244,8 +265,13 @@ def test_qr(self, benchmark, pkgid):
244265
benchmark.extra_info["description"] = f"{NSIZE}x{NSIZE} Matrix"
245266
pkg = PKGDICT[pkgid]
246267

247-
QR_FUNCS = {"numpy": qr_np, "cupy": qr_cupy, "arrayfire": qr_af, "dpnp": qr_dpnp,
248-
"cupynumeric": qr_cupynumeric }
268+
QR_FUNCS = {
269+
"numpy": qr_np,
270+
"cupy": qr_cupy,
271+
"arrayfire": qr_af,
272+
"dpnp": qr_dpnp,
273+
"cupynumeric": qr_cupynumeric,
274+
}
249275
result = benchmark.pedantic(target=QR_FUNCS[pkg.__name__], setup=setup, rounds=ROUNDS, iterations=ITERATIONS)
250276

251277
def test_inv(self, benchmark, pkgid):
@@ -255,8 +281,13 @@ def test_inv(self, benchmark, pkgid):
255281
benchmark.extra_info["description"] = f"{NSIZE}x{NSIZE} Matrix"
256282
pkg = PKGDICT[pkgid]
257283

258-
INV_FUNCS = {"numpy": inv_np, "cupy": inv_cupy, "arrayfire": inv_af, "dpnp": inv_dpnp,
259-
"cupynumeric": inv_cupynumeric }
284+
INV_FUNCS = {
285+
"numpy": inv_np,
286+
"cupy": inv_cupy,
287+
"arrayfire": inv_af,
288+
"dpnp": inv_dpnp,
289+
"cupynumeric": inv_cupynumeric,
290+
}
260291
result = benchmark.pedantic(target=INV_FUNCS[pkg.__name__], setup=setup, rounds=ROUNDS, iterations=ITERATIONS)
261292

262293
def test_det(self, benchmark, pkgid):
@@ -266,8 +297,13 @@ def test_det(self, benchmark, pkgid):
266297
benchmark.extra_info["description"] = f"{NSIZE}x{NSIZE} Matrix"
267298
pkg = PKGDICT[pkgid]
268299

269-
DET_FUNCS = {"numpy": det_np, "cupy": det_cupy, "arrayfire": det_af, "dpnp": det_dpnp,
270-
"cupynumeric": det_cupynumeric }
300+
DET_FUNCS = {
301+
"numpy": det_np,
302+
"cupy": det_cupy,
303+
"arrayfire": det_af,
304+
"dpnp": det_dpnp,
305+
"cupynumeric": det_cupynumeric,
306+
}
271307
result = benchmark.pedantic(target=DET_FUNCS[pkg.__name__], setup=setup, rounds=ROUNDS, iterations=ITERATIONS)
272308

273309
def test_norm(self, benchmark, pkgid):
@@ -277,6 +313,11 @@ def test_norm(self, benchmark, pkgid):
277313
benchmark.extra_info["description"] = f"{NSIZE}x{NSIZE} Matrix"
278314
pkg = PKGDICT[pkgid]
279315

280-
NORM_FUNCS = {"numpy": norm_np, "cupy": norm_cupy, "arrayfire": norm_af, "dpnp": norm_dpnp,
281-
"cupynumeric": norm_cupynumeric }
316+
NORM_FUNCS = {
317+
"numpy": norm_np,
318+
"cupy": norm_cupy,
319+
"arrayfire": norm_af,
320+
"dpnp": norm_dpnp,
321+
"cupynumeric": norm_cupynumeric,
322+
}
282323
result = benchmark.pedantic(target=NORM_FUNCS[pkg.__name__], setup=setup, rounds=ROUNDS, iterations=ITERATIONS)

benchmarks/src/pytest_benchmark/test_mandelbrot.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ def mandelbrot_cupy():
142142
cupy.cuda.runtime.deviceSynchronize()
143143
return Z_, N_
144144

145+
145146
def mandelbrot_cupynumeric():
146147
# Adapted from
147148
# https://thesamovar.wordpress.com/2009/03/22/fast-fractals-with-python-and-numpy/
@@ -176,6 +177,7 @@ def mandelbrot_cupynumeric():
176177

177178
return Z_.T, N_.T
178179

180+
179181
def mandelbrot_af():
180182
Xi = af.flat(af.range((xn, yn), axis=0, dtype=af.int64))
181183
Yi = af.flat(af.range((xn, yn), axis=1, dtype=af.int64))
@@ -221,4 +223,10 @@ def mandelbrot_af():
221223
return Z_, N_
222224

223225

224-
FUNCS = {"dpnp": mandelbrot_dpnp, "numpy": mandelbrot_np, "cupy": mandelbrot_cupy, "arrayfire": mandelbrot_af, "cupynumeric" : mandelbrot_cupynumeric}
226+
FUNCS = {
227+
"dpnp": mandelbrot_dpnp,
228+
"numpy": mandelbrot_np,
229+
"cupy": mandelbrot_cupy,
230+
"arrayfire": mandelbrot_af,
231+
"cupynumeric": mandelbrot_cupynumeric,
232+
}

benchmarks/src/pytest_benchmark/test_montecarlo_pi.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,17 @@ def calc_pi_dpnp(samples):
4747
y = dpnp.random.rand(samples).astype(dpnp.float32)
4848
return 4.0 * dpnp.sum(in_circle(x, y)) / samples
4949

50+
5051
def calc_pi_cupynumeric(samples):
5152
x = cupynumeric.random.rand(samples).astype(cupynumeric.float32)
5253
y = cupynumeric.random.rand(samples).astype(cupynumeric.float32)
5354
return 4.0 * cupynumeric.sum(in_circle(x, y)) / samples
5455

5556

56-
FUNCS = {"dpnp": calc_pi_dpnp, "numpy": calc_pi_numpy, "cupy": calc_pi_cupy, "arrayfire": calc_pi_af,
57-
"cupynumeric": calc_pi_cupynumeric }
57+
FUNCS = {
58+
"dpnp": calc_pi_dpnp,
59+
"numpy": calc_pi_numpy,
60+
"cupy": calc_pi_cupy,
61+
"arrayfire": calc_pi_af,
62+
"cupynumeric": calc_pi_cupynumeric,
63+
}

benchmarks/src/pytest_benchmark/test_nn.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ def train(self):
282282
def predict(self, X):
283283
return cupy.argmax(self.forward(X), axis=1)
284284

285+
285286
class NeuralNetwork_cupynumeric:
286287
def __init__(self):
287288
self.input_size = INPUT_SIZE
@@ -293,13 +294,16 @@ def __init__(self):
293294
# He initialization (for ReLU) is often a good choice
294295
self.W1 = cupynumeric.random.randn(self.input_size, self.hidden_size) * cupynumeric.sqrt(2.0 / self.input_size)
295296
self.b1 = cupynumeric.zeros((1, self.hidden_size))
296-
self.W2 = cupynumeric.random.randn(self.hidden_size, self.output_size) * cupynumeric.sqrt(2.0 / self.hidden_size)
297+
self.W2 = cupynumeric.random.randn(self.hidden_size, self.output_size) * cupynumeric.sqrt(
298+
2.0 / self.hidden_size
299+
)
297300
self.b2 = cupynumeric.zeros((1, self.output_size))
298301

299302
self.X_train = cupynumeric.random.rand(SAMPLES, INPUT_SIZE)
300303
self.y_train = cupynumeric.zeros((SAMPLES * OUTPUT_SIZE))
301304
self.y_train[
302-
cupynumeric.arange(SAMPLES) * OUTPUT_SIZE + cupynumeric.floor(cupynumeric.random.rand(SAMPLES) * OUTPUT_SIZE).astype(int)
305+
cupynumeric.arange(SAMPLES) * OUTPUT_SIZE
306+
+ cupynumeric.floor(cupynumeric.random.rand(SAMPLES) * OUTPUT_SIZE).astype(int)
303307
] = 1
304308
self.y_train = self.y_train.reshape((SAMPLES, OUTPUT_SIZE))
305309

@@ -310,7 +314,9 @@ def relu_derivative(self, x):
310314
return (x > 0).astype(float)
311315

312316
def softmax(self, x):
313-
exp_scores = cupynumeric.exp(x - cupynumeric.max(x, axis=1, keepdims=True)) # Subtract max for numerical stability
317+
exp_scores = cupynumeric.exp(
318+
x - cupynumeric.max(x, axis=1, keepdims=True)
319+
) # Subtract max for numerical stability
314320
return exp_scores / cupynumeric.sum(exp_scores, axis=1, keepdims=True)
315321

316322
def forward(self, X):
@@ -364,6 +370,7 @@ def train(self):
364370
def predict(self, X):
365371
return cupynumeric.argmax(self.forward(X), axis=1)
366372

373+
367374
class NeuralNetwork_af:
368375
def __init__(self):
369376
self.input_size = INPUT_SIZE

0 commit comments

Comments
 (0)