Skip to content

Commit 0d0abdb

Browse files
kyleabeauchamptwiecki
authored andcommitted
Test fixes for CUDA (part 3 / n) (#2000)
* Refactor float32 test helpers, fix test_step * Adjust precision for CPU float32 * Revert some of the extra floatX * Move fxarray to floatX_array in theanof * Fix floatX in test_stats for CUDA * Fixes for test_starting on CUDA * Fix lint
1 parent ce65a4f commit 0d0abdb

File tree

5 files changed

+28
-21
lines changed

5 files changed

+28
-21
lines changed

pymc3/tests/helpers.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from logging.handlers import BufferingHandler
22
import numpy.random as nr
3-
import numpy as np
43
from theano.sandbox.rng_mrg import MRG_RandomStreams
54
from ..theanof import set_tt_rng, tt_rng
6-
from pymc3.theanof import floatX
75
import theano
86

97

@@ -22,6 +20,7 @@ def setup_method(self):
2220
def teardown_method(self):
2321
set_tt_rng(self.old_tt_rng)
2422

23+
2524
class LoggingHandler(BufferingHandler):
2625
def __init__(self, matcher):
2726
# BufferingHandler takes a "capacity" argument
@@ -48,6 +47,7 @@ def matches(self, **kwargs):
4847
break
4948
return result
5049

50+
5151
class Matcher(object):
5252

5353
_partial_matches = ('msg', 'message')
@@ -86,7 +86,3 @@ def select_by_precision(float64, float32):
8686
"""Helper function to choose reasonable decimal cutoffs for different floatX modes."""
8787
decimal = float64 if theano.config.floatX == "float64" else float32
8888
return decimal
89-
90-
91-
def fxarray(x):
92-
return floatX(np.array(x))

pymc3/tests/models.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import pymc3 as pm
44
from itertools import product
55
import theano.tensor as tt
6+
import theano
67
from theano.compile.ops import as_op
7-
from .helpers import fxarray
8+
from pymc3.theanof import floatX_array
9+
810

911
def simple_model():
1012
mu = -2.1
@@ -16,8 +18,8 @@ def simple_model():
1618

1719

1820
def simple_categorical():
19-
p = fxarray([0.1, 0.2, 0.3, 0.4])
20-
v = fxarray([0.0, 1.0, 2.0, 3.0])
21+
p = floatX_array([0.1, 0.2, 0.3, 0.4])
22+
v = floatX_array([0.0, 1.0, 2.0, 3.0])
2123
with Model() as model:
2224
Categorical('x', p, shape=3, testval=[1, 2, 3])
2325

@@ -36,14 +38,16 @@ def multidimensional_model():
3638

3739

3840
def simple_arbitrary_det():
39-
@as_op(itypes=[tt.dscalar], otypes=[tt.dscalar])
41+
scalar_type = tt.dscalar if theano.config.floatX == "float64" else tt.fscalar
42+
43+
@as_op(itypes=[scalar_type], otypes=[scalar_type])
4044
def arbitrary_det(value):
4145
return value
4246

4347
with Model() as model:
4448
a = Normal('a')
4549
b = arbitrary_det(a)
46-
Normal('obs', mu=b.astype('float64'), observed=fxarray([1, 3, 5]))
50+
Normal('obs', mu=b.astype('float64'), observed=floatX_array([1, 3, 5]))
4751

4852
return model.test_point, model
4953

@@ -66,15 +70,15 @@ def simple_2model():
6670

6771

6872
def mv_simple():
69-
mu = fxarray([-.1, .5, 1.1])
70-
p = fxarray([
73+
mu = floatX_array([-.1, .5, 1.1])
74+
p = floatX_array([
7175
[2., 0, 0],
7276
[.05, .1, 0],
7377
[1., -0.05, 5.5]])
7478
tau = np.dot(p, p.T)
7579
with pm.Model() as model:
7680
pm.MvNormal('x', tt.constant(mu), tau=tt.constant(tau),
77-
shape=3, testval=fxarray([.1, 1., .8]))
81+
shape=3, testval=floatX_array([.1, 1., .8]))
7882
H = tau
7983
C = np.linalg.inv(H)
8084
return model.test_point, model, (mu, C)
@@ -83,7 +87,7 @@ def mv_simple():
8387
def mv_simple_discrete():
8488
d = 2
8589
n = 5
86-
p = fxarray([.15, .85])
90+
p = floatX_array([.15, .85])
8791
with pm.Model() as model:
8892
pm.Multinomial('x', n, tt.constant(p), shape=d, testval=np.array([1, 4]))
8993
mu = n * p
@@ -106,7 +110,7 @@ def mv_prior_simple():
106110
K = pm.gp.cov.ExpQuad(1, 1)(X).eval()
107111
L = np.linalg.cholesky(K)
108112
K_noise = K + noise * np.eye(n)
109-
obs = fxarray([-0.1, 0.5, 1.1])
113+
obs = floatX_array([-0.1, 0.5, 1.1])
110114

111115
# Posterior mean
112116
L_noise = np.linalg.cholesky(K_noise)

pymc3/tests/test_starting.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33
from pymc3.tuning import starting
44
from pymc3 import Model, Uniform, Normal, Beta, Binomial, find_MAP, Point
55
from .models import simple_model, non_normal, exponential_beta, simple_arbitrary_det
6+
from .helpers import select_by_precision
7+
68

79
def test_accuracy_normal():
810
_, model, (mu, _) = simple_model()
911
with model:
1012
newstart = find_MAP(Point(x=[-10.5, 100.5]))
11-
close_to(newstart['x'], [mu, mu], 1e-5)
13+
close_to(newstart['x'], [mu, mu], select_by_precision(float64=1e-5, float32=1E-4))
1214

1315

1416
def test_accuracy_non_normal():
1517
_, model, (mu, _) = non_normal(4)
1618
with model:
1719
newstart = find_MAP(Point(x=[.5, .01, .95, .99]))
18-
close_to(newstart['x'], mu, 1e-5)
20+
close_to(newstart['x'], mu, select_by_precision(float64=1e-5, float32=1E-4))
1921

2022

2123
def test_errors():

pymc3/tests/test_stats.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from ..tests import backend_fixtures as bf
88
from ..backends import ndarray
99
from ..stats import df_summary, autocorr, hpd, mc_error, quantiles, make_indices
10+
from ..theanof import floatX_array
1011
from numpy.random import random, normal
1112
from numpy.testing import assert_equal, assert_almost_equal, assert_array_almost_equal
1213
from scipy import stats as st
@@ -116,7 +117,7 @@ def test_summary_0d_variable_model(self):
116117
mu = -2.1
117118
tau = 1.3
118119
with Model() as model:
119-
Normal('x', mu, tau, testval=.1)
120+
Normal('x', mu, tau, testval=floatX_array(.1))
120121
step = Metropolis(model.vars, np.diag([1.]), blocked=True)
121122
trace = pm.sample(100, step=step)
122123
pm.summary(trace)
@@ -125,7 +126,7 @@ def test_summary_1d_variable_model(self):
125126
mu = -2.1
126127
tau = 1.3
127128
with Model() as model:
128-
Normal('x', mu, tau, shape=2, testval=[.1, .1])
129+
Normal('x', mu, tau, shape=2, testval=floatX_array([.1, .1]))
129130
step = Metropolis(model.vars, np.diag([1.]), blocked=True)
130131
trace = pm.sample(100, step=step)
131132
pm.summary(trace)
@@ -135,7 +136,7 @@ def test_summary_2d_variable_model(self):
135136
tau = 1.3
136137
with Model() as model:
137138
Normal('x', mu, tau, shape=(2, 2),
138-
testval=np.tile(.1, (2, 2)))
139+
testval=floatX_array(np.tile(.1, (2, 2))))
139140
step = Metropolis(model.vars, np.diag([1.]), blocked=True)
140141
trace = pm.sample(100, step=step)
141142
pm.summary(trace)

pymc3/theanof.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,7 @@ def __init__(self, multiplier):
409409

410410
def grad(self, args, g_outs):
411411
return [self.multiplier * g_out for g_out in g_outs]
412+
413+
414+
def floatX_array(x):
415+
return floatX(np.array(x))

0 commit comments

Comments
 (0)