13
13
import pickle
14
14
import shutil
15
15
import sys
16
- import timeit
17
16
from collections import OrderedDict
18
17
from tempfile import mkdtemp
19
18
@@ -2179,15 +2178,13 @@ def scan_fn():
2179
2178
@pytest .mark .skipif (
2180
2179
not config .cxx , reason = "G++ not available, so we need to skip this test."
2181
2180
)
2182
- def test_cython_performance ():
2181
+ def test_cython_performance (benchmark ):
2183
2182
2184
2183
# This implicitly confirms that the Cython version is being used
2185
2184
from pytensor .scan import scan_perform_ext # noqa: F401
2186
2185
2187
2186
# Python usually out-performs PyTensor below 100 iterations
2188
2187
N = 200
2189
- n_timeit = 50
2190
-
2191
2188
M = - 1 / np .arange (1 , 11 ).astype (config .floatX )
2192
2189
r = np .arange (N * 10 ).astype (config .floatX ).reshape (N , 10 )
2193
2190
@@ -2216,17 +2213,11 @@ def f_py():
2216
2213
# Make sure we're actually computing a `Scan`
2217
2214
assert any (isinstance (node .op , Scan ) for node in f_cvm .maker .fgraph .apply_nodes )
2218
2215
2219
- cvm_res = f_cvm ( )
2216
+ cvm_res = benchmark ( f_cvm )
2220
2217
2221
2218
# Make sure the results are the same between the two implementations
2222
2219
assert np .allclose (cvm_res , py_res )
2223
2220
2224
- python_duration = timeit .timeit (lambda : f_py (), number = n_timeit )
2225
- cvm_duration = timeit .timeit (lambda : f_cvm (), number = n_timeit )
2226
- print (f"python={ python_duration } , cvm={ cvm_duration } " )
2227
-
2228
- assert cvm_duration <= python_duration
2229
-
2230
2221
2231
2222
@config .change_flags (mode = "FAST_COMPILE" , compute_test_value = "raise" )
2232
2223
def test_compute_test_values ():
@@ -2662,7 +2653,7 @@ def numpy_implementation(vsample):
2662
2653
n_result = numpy_implementation (v_vsample )
2663
2654
utt .assert_allclose (t_result , n_result )
2664
2655
2665
- def test_reordering (self ):
2656
+ def test_reordering (self , benchmark ):
2666
2657
"""Test re-ordering of inputs.
2667
2658
2668
2659
some rnn with multiple outputs and multiple inputs; other
@@ -2722,14 +2713,14 @@ def f_rnn_cmpl(u1_t, u2_t, x_tm1, y_tm1, y_tm3, W_in1):
2722
2713
v_x [i ] = np .dot (v_u1 [i ], vW_in1 ) + v_u2 [i ] * vW_in2 + np .dot (v_x [i - 1 ], vW )
2723
2714
v_y [i ] = np .dot (v_x [i - 1 ], vWout ) + v_y [i - 1 ]
2724
2715
2725
- (pytensor_dump1 , pytensor_dump2 , pytensor_x , pytensor_y ) = f4 (
2726
- v_u1 , v_u2 , v_x0 , v_y0 , vW_in1
2716
+ (pytensor_dump1 , pytensor_dump2 , pytensor_x , pytensor_y ) = benchmark (
2717
+ f4 , v_u1 , v_u2 , v_x0 , v_y0 , vW_in1
2727
2718
)
2728
2719
2729
2720
utt .assert_allclose (pytensor_x , v_x )
2730
2721
utt .assert_allclose (pytensor_y , v_y )
2731
2722
2732
- def test_scan_as_tensor_on_gradients (self ):
2723
+ def test_scan_as_tensor_on_gradients (self , benchmark ):
2733
2724
to_scan = dvector ("to_scan" )
2734
2725
seq = dmatrix ("seq" )
2735
2726
f1 = dscalar ("f1" )
@@ -2743,7 +2734,12 @@ def scanStep(prev, seq, f1):
2743
2734
function (inputs = [to_scan , seq , f1 ], outputs = scanned , allow_input_downcast = True )
2744
2735
2745
2736
t_grad = grad (scanned .sum (), wrt = [to_scan , f1 ], consider_constant = [seq ])
2746
- function (inputs = [to_scan , seq , f1 ], outputs = t_grad , allow_input_downcast = True )
2737
+ benchmark (
2738
+ function ,
2739
+ inputs = [to_scan , seq , f1 ],
2740
+ outputs = t_grad ,
2741
+ allow_input_downcast = True ,
2742
+ )
2747
2743
2748
2744
def caching_nsteps_by_scan_op (self ):
2749
2745
W = matrix ("weights" )
@@ -3060,7 +3056,7 @@ def inner_fn(tap_m3, tap_m2, tap_m1):
3060
3056
utt .assert_allclose (outputs , expected_outputs )
3061
3057
3062
3058
@pytest .mark .slow
3063
- def test_hessian_bug_grad_grad_two_scans (self ):
3059
+ def test_hessian_bug_grad_grad_two_scans (self , benchmark ):
3064
3060
# Bug reported by Bitton Tenessi
3065
3061
# NOTE : The test to reproduce the bug reported by Bitton Tenessi
3066
3062
# was modified from its original version to be faster to run.
@@ -3094,7 +3090,7 @@ def loss_inner(sum_inner, W):
3094
3090
H = hessian (cost , W )
3095
3091
print ("." , file = sys .stderr )
3096
3092
f = function ([W , n_steps ], H )
3097
- f ( np .ones ((8 ,), dtype = "float32" ), 1 )
3093
+ benchmark ( f , np .ones ((8 ,), dtype = "float32" ), 1 )
3098
3094
3099
3095
def test_grad_connectivity_matrix (self ):
3100
3096
def inner_fn (x_tm1 , y_tm1 , z_tm1 ):
@@ -3710,7 +3706,7 @@ def f_rnn_cmpl(u1_t, u2_t, x_tm1, y_tm1, W_in1):
3710
3706
utt .assert_allclose (pytensor_x , v_x )
3711
3707
utt .assert_allclose (pytensor_y , v_y )
3712
3708
3713
- def test_multiple_outs_taps (self ):
3709
+ def test_multiple_outs_taps (self , benchmark ):
3714
3710
l = 5
3715
3711
rng = np .random .default_rng (utt .fetch_seed ())
3716
3712
@@ -3805,6 +3801,8 @@ def f_rnn_cmpl(u1_t, u2_tm1, u2_t, u2_tp1, x_tm1, y_tm1, y_tm3, W_in1):
3805
3801
np .testing .assert_almost_equal (res [1 ], ny1 )
3806
3802
np .testing .assert_almost_equal (res [2 ], ny2 )
3807
3803
3804
+ benchmark (f , v_u1 , v_u2 , v_x0 , v_y0 , vW_in1 )
3805
+
3808
3806
def _grad_mout_helper (self , n_iters , mode ):
3809
3807
rng = np .random .default_rng (utt .fetch_seed ())
3810
3808
n_hid = 3
0 commit comments