Skip to content

Commit

Permalink
Replaced njobs with chains through all tests and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
fonnesbeck committed Feb 1, 2018
1 parent 860e563 commit 7515d0f
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 45 deletions.
10 changes: 5 additions & 5 deletions benchmarks/benchmarks/benchmarks.py
Expand Up @@ -115,11 +115,11 @@ def time_drug_evaluation(self):
pm.Deterministic('difference of stds', group1_std - group2_std)
pm.Deterministic(
'effect size', diff_of_means / np.sqrt((group1_std**2 + group2_std**2) / 2))
pm.sample(20000, njobs=4, chains=4)
pm.sample(20000, cores=4, chains=4)

def time_glm_hierarchical(self):
with glm_hierarchical_model():
pm.sample(draws=20000, njobs=4, chains=4)
pm.sample(draws=20000, cores=4, chains=4)


class NUTSInitSuite(object):
Expand All @@ -141,7 +141,7 @@ def track_glm_hierarchical_ess(self, init):
with glm_hierarchical_model():
start, step = pm.init_nuts(init=init, chains=self.chains, progressbar=False, random_seed=123)
t0 = time.time()
trace = pm.sample(draws=self.draws, step=step, njobs=4, chains=self.chains,
trace = pm.sample(draws=self.draws, step=step, cores=4, chains=self.chains,
start=start, random_seed=100)
tot = time.time() - t0
ess = pm.effective_n(trace, ('mu_a',))['mu_a']
Expand All @@ -154,7 +154,7 @@ def track_marginal_mixture_model_ess(self, init):
progressbar=False, random_seed=123)
start = [{k: v for k, v in start.items()} for _ in range(self.chains)]
t0 = time.time()
trace = pm.sample(draws=self.draws, step=step, njobs=4, chains=self.chains,
trace = pm.sample(draws=self.draws, step=step, cores=4, chains=self.chains,
start=start, random_seed=100)
tot = time.time() - t0
ess = pm.effective_n(trace, ('mu',))['mu'].min() # worst case
Expand All @@ -178,7 +178,7 @@ def track_glm_hierarchical_ess(self, step):
if step is not None:
step = step()
t0 = time.time()
trace = pm.sample(draws=self.draws, step=step, njobs=4, chains=4,
trace = pm.sample(draws=self.draws, step=step, cores=4, chains=4,
random_seed=100)
tot = time.time() - t0
ess = pm.effective_n(trace, ('mu_a',))['mu_a']
Expand Down
2 changes: 1 addition & 1 deletion pymc3/backends/__init__.py
Expand Up @@ -34,7 +34,7 @@
The call will return the sampling values of `x`, with the values for
all chains concatenated. (For a single call to `sample`, the number of
chains will correspond to the `njobs` argument.)
chains will correspond to the `cores` argument.)
To discard the first N values of each chain, slicing syntax can be
used.
Expand Down
4 changes: 2 additions & 2 deletions pymc3/examples/custom_dists.py
Expand Up @@ -24,7 +24,7 @@
ydata = np.random.normal(ydata, 10)
data = {'x': xdata, 'y': ydata}

# define loglikelihood outside of the model context, otherwise njobs wont work:
# define loglikelihood outside of the model context, otherwise cores wont work:
# Lambdas defined in local namespace are not picklable (see issue #1995)
def loglike1(value):
return -1.5 * tt.log(1 + value**2)
Expand All @@ -40,7 +40,7 @@ def loglike2(value):
like = pm.Normal('y_est', mu=alpha + beta *
xdata, sd=sigma, observed=ydata)

trace = pm.sample(2000, njobs=2)
trace = pm.sample(2000, cores=2)


#################################################
Expand Down
2 changes: 1 addition & 1 deletion pymc3/examples/disaster_model_theano_op.py
Expand Up @@ -56,5 +56,5 @@ def rate_(switchpoint, early_mean, late_mean):
# Initial values for stochastic nodes
start = {'early_mean': 2., 'late_mean': 3.}

tr = pm.sample(1000, tune=500, start=start, step=[step1, step2], njobs=2)
tr = pm.sample(1000, tune=500, start=start, step=[step1, step2], cores=2)
pm.traceplot(tr)
26 changes: 13 additions & 13 deletions pymc3/sampling.py
Expand Up @@ -249,7 +249,7 @@ def sample(draws=500, step=None, init='auto', n_init=200000, start=None,
The number of chains to sample. Running independent chains
is important for some convergence statistics and can also
reveal multiple modes in the posterior. If `None`, then set to
either `njobs` or 2, whichever is larger.
either `chains` or 2, whichever is larger.
cores : int
The number of chains to run in parallel. If `None`, set to the
number of CPUs in the system, but at most 4. Keep in mind that
Expand Down Expand Up @@ -286,7 +286,7 @@ def sample(draws=500, step=None, init='auto', n_init=200000, start=None,
completion ("expected time of arrival"; ETA).
model : Model (optional if in `with` context)
random_seed : int or list of ints
A list is accepted if `njobs` is greater than one.
A list is accepted if `cores` is greater than one.
live_plot : bool
Flag for live plotting the trace while sampling
live_plot_kwargs : dict
Expand Down Expand Up @@ -317,22 +317,22 @@ def sample(draws=500, step=None, init='auto', n_init=200000, start=None,
>>> with pm.Model() as model: # context management
... p = pm.Beta('p', alpha=alpha, beta=beta)
... y = pm.Binomial('y', n=n, p=p, observed=h)
... trace = pm.sample(2000, tune=1000, njobs=4)
... trace = pm.sample(2000, tune=1000, cores=4)
>>> pm.summary(trace)
mean sd mc_error hpd_2.5 hpd_97.5
p 0.604625 0.047086 0.00078 0.510498 0.694774
"""
model = modelcontext(model)

if cores is None:
njobs = min(4, _cpu_count())
cores = min(4, _cpu_count())
if 'njobs' in kwargs:
njobs = kwargs['njobs']
cores = kwargs['njobs']
warnings.warn(
"The njobs argument has been deprecated. Use cores instead.",
DeprecationWarning)
if chains is None:
chains = max(2, njobs)
chains = max(2, cores)
if isinstance(start, dict):
start = [start] * chains
if random_seed == -1:
Expand Down Expand Up @@ -411,7 +411,7 @@ def sample(draws=500, step=None, init='auto', n_init=200000, start=None,
'random_seed': random_seed,
'live_plot': live_plot,
'live_plot_kwargs': live_plot_kwargs,
'njobs': njobs,
'cores': cores,
}

sample_args.update(kwargs)
Expand All @@ -420,9 +420,9 @@ def sample(draws=500, step=None, init='auto', n_init=200000, start=None,
isinstance(m, arraystep.PopulationArrayStepShared)
for m in (step.methods if isinstance(step, CompoundStep) else [step])
])
parallel = njobs > 1 and chains > 1 and not has_population_samplers
parallel = cores > 1 and chains > 1 and not has_population_samplers
if parallel:
_log.info('Multiprocess sampling ({} chains in {} jobs)'.format(chains, njobs))
_log.info('Multiprocess sampling ({} chains in {} jobs)'.format(chains, cores))
_print_step_hierarchy(step)
try:
trace = _mp_sample(**sample_args)
Expand Down Expand Up @@ -572,13 +572,13 @@ def iter_sample(draws, step, start=None, trace=None, chain=0, tune=None,
is given, it must contain samples for the chain number `chain`.
If None or a list of variables, the NDArray backend is used.
chain : int
Chain number used to store sample in backend. If `njobs` is
Chain number used to store sample in backend. If `cores` is
greater than one, chain numbers will start here.
tune : int
Number of iterations to tune, if applicable (defaults to None)
model : Model (optional if in `with` context)
random_seed : int or list of ints
A list is accepted if more if `njobs` is greater than one.
A list is accepted if more if `cores` is greater than one.
Examples
--------
Expand Down Expand Up @@ -945,7 +945,7 @@ def _choose_backend(trace, chain, shortcuts=None, **kwds):


def _mp_sample(**kwargs):
njobs = kwargs.pop('njobs')
cores = kwargs.pop('cores')
chain = kwargs.pop('chain')
rseed = kwargs.pop('random_seed')
start = kwargs.pop('start')
Expand All @@ -955,7 +955,7 @@ def _mp_sample(**kwargs):
pbars = [kwargs.pop('progressbar')] + [False] * (chains - 1)
jobs = (delayed(_sample)(*args, **kwargs)
for args in zip(chain_nums, pbars, rseed, start))
traces = Parallel(n_jobs=njobs)(jobs)
traces = Parallel(n_jobs=cores)(jobs)
return MultiTrace(traces)


Expand Down
2 changes: 1 addition & 1 deletion pymc3/tests/sampler_fixtures.py
Expand Up @@ -128,7 +128,7 @@ def setup_class(cls):
cls.model = cls.make_model()
with cls.model:
cls.step = cls.make_step()
cls.trace = pm.sample(cls.n_samples, tune=cls.tune, step=cls.step, njobs=cls.chains)
cls.trace = pm.sample(cls.n_samples, tune=cls.tune, step=cls.step, cores=cls.chains)
cls.samples = {}
for var in cls.model.unobserved_RVs:
cls.samples[str(var)] = cls.trace.get_values(var, burn=cls.burn)
Expand Down
6 changes: 3 additions & 3 deletions pymc3/tests/test_diagnostics.py
Expand Up @@ -24,7 +24,7 @@ def get_ptrace(self, n_samples):
step1 = Slice([model.early_mean_log__, model.late_mean_log__])
step2 = Metropolis([model.switchpoint])
start = {'early_mean': 7., 'late_mean': 5., 'switchpoint': 10}
ptrace = sample(n_samples, tune=0, step=[step1, step2], start=start, njobs=2,
ptrace = sample(n_samples, tune=0, step=[step1, step2], start=start, cores=2,
progressbar=False, random_seed=[20090425, 19700903])
return ptrace

Expand Down Expand Up @@ -161,7 +161,7 @@ def test_effective_n(self):
start = find_MAP()
step = NUTS(scaling=start)
ptrace = sample(0, tune=n_samples, step=step, start=start,
njobs=n_jobs, discard_tuned_samples=False,
cores=n_jobs, discard_tuned_samples=False,
random_seed=42)

n_effective = effective_n(ptrace)['x']
Expand All @@ -183,7 +183,7 @@ def test_effective_n_right_shape_python_float(self,
start = find_MAP()
step = NUTS(scaling=start)
ptrace = sample(0, tune=n_samples, step=step, start=start,
njobs=n_jobs, discard_tuned_samples=False,
cores=n_jobs, discard_tuned_samples=False,
random_seed=42)

n_effective = effective_n(ptrace)['x']
Expand Down
6 changes: 3 additions & 3 deletions pymc3/tests/test_plots.py
Expand Up @@ -34,7 +34,7 @@ def test_plots():

def test_energyplot():
with asmod.build_model():
trace = sample(njobs=1)
trace = sample(cores=1)

energyplot(trace)
energyplot(trace, shade=0.5, alpha=0)
Expand Down Expand Up @@ -66,15 +66,15 @@ def test_plots_multidimensional():
forestplot(trace)
densityplot(trace)

@pytest.mark.xfail(condition=(theano.config.floatX == "float32"), reason="Fails on GPU due to njobs=2")
@pytest.mark.xfail(condition=(theano.config.floatX == "float32"), reason="Fails on GPU due to cores=2")
def test_multichain_plots():
model = build_disaster_model()
with model:
# Run sampler
step1 = Slice([model.early_mean_log__, model.late_mean_log__])
step2 = Metropolis([model.switchpoint])
start = {'early_mean': 2., 'late_mean': 3., 'switchpoint': 50}
ptrace = sample(1000, tune=0, step=[step1, step2], start=start, njobs=2)
ptrace = sample(1000, tune=0, step=[step1, step2], start=start, cores=2)

forestplot(ptrace, varnames=['early_mean', 'late_mean'])
autocorrplot(ptrace, varnames=['switchpoint'])
Expand Down
20 changes: 10 additions & 10 deletions pymc3/tests/test_sampling.py
Expand Up @@ -33,15 +33,15 @@ def test_sample_does_not_set_seed(self):
assert random_numbers[0] == random_numbers[1]

def test_parallel_sample_does_not_reuse_seed(self):
njobs = 4
cores = 4
random_numbers = []
draws = []
for _ in range(2):
np.random.seed(1) # seeds in other processes don't effect main process
with self.model:
trace = pm.sample(100, tune=0, njobs=njobs)
trace = pm.sample(100, tune=0, cores=cores)
# numpy thread mentioned race condition. might as well check none are equal
for first, second in combinations(range(njobs), 2):
for first, second in combinations(range(cores), 2):
first_chain = trace.get_values('x', chains=first)
second_chain = trace.get_values('x', chains=second)
assert not (first_chain == second_chain).all()
Expand All @@ -53,11 +53,11 @@ def test_parallel_sample_does_not_reuse_seed(self):
assert (draws[0] == draws[1]).all()

def test_sample(self):
test_njobs = [1]
test_cores = [1]
with self.model:
for njobs in test_njobs:
for cores in test_cores:
for steps in [1, 10, 300]:
pm.sample(steps, tune=0, step=self.step, njobs=njobs,
pm.sample(steps, tune=0, step=self.step, cores=cores,
random_seed=self.random_seed)

def test_sample_init(self):
Expand Down Expand Up @@ -93,7 +93,7 @@ def test_iter_sample(self):

def test_parallel_start(self):
with self.model:
tr = pm.sample(0, tune=5, njobs=2,
tr = pm.sample(0, tune=5, cores=2,
discard_tuned_samples=False,
start=[{'x': [10, 10]}, {'x': [-10, -10]}],
random_seed=self.random_seed)
Expand All @@ -102,12 +102,12 @@ def test_parallel_start(self):

def test_sample_tune_len(self):
with self.model:
trace = pm.sample(draws=100, tune=50, njobs=1)
trace = pm.sample(draws=100, tune=50, cores=1)
assert len(trace) == 100
trace = pm.sample(draws=100, tune=50, njobs=1,
trace = pm.sample(draws=100, tune=50, cores=1,
discard_tuned_samples=False)
assert len(trace) == 150
trace = pm.sample(draws=100, tune=50, njobs=4)
trace = pm.sample(draws=100, tune=50, cores=4)
assert len(trace) == 100

@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion pymc3/tests/test_sgfs.py
Expand Up @@ -31,6 +31,6 @@ def f(x, a, b, c):
pm.Normal('y', mu=y, observed=y_obs)

step_method = pm.SGFS(batch_size=batch_size, step_size=1., total_size=total_size)
trace = pm.sample(draws=draws, step=step_method, init=None, njobs=2)
trace = pm.sample(draws=draws, step=step_method, init=None, cores=2)

np.testing.assert_allclose(np.mean(trace['abc'], axis=0), np.asarray([a, b, c]), rtol=0.1)
4 changes: 2 additions & 2 deletions pymc3/tests/test_smc.py
Expand Up @@ -51,9 +51,9 @@ def two_gaussians(x):
def test_sample_n_core(self, n_jobs, stage):

mtrace = smc.sample_smc(samples=self.samples,
n_chains=self.n_chains,
chains=self.n_chains,
stage=stage,
n_jobs=n_jobs,
cores=n_jobs,
progressbar=True,
homepath=self.test_folder,
model=self.ATMIP_test,
Expand Down
4 changes: 2 additions & 2 deletions pymc3/tests/test_step.py
Expand Up @@ -198,10 +198,10 @@ def check_trace(self, step_method):
x = Normal('x', mu=0, sd=1)
if step_method.__name__ == 'SMC':
trace = smc.sample_smc(samples=200,
n_chains=2,
chains=2,
start=[{'x':1.}, {'x':-1.}],
random_seed=1,
n_jobs=1, progressbar=False,
cores=1, progressbar=False,
homepath=self.temp_dir)
elif step_method.__name__ == 'NUTS':
step = step_method(scaling=model.test_point)
Expand Down
2 changes: 1 addition & 1 deletion pymc3/tests/test_text_backend.py
Expand Up @@ -12,7 +12,7 @@ def test_supports_sampler_stats(self):
with pm.Model():
pm.Normal("mu", mu=0, sd=1, shape=2)
db = text.Text(self.name)
pm.sample(20, tune=10, init=None, trace=db, njobs=2)
pm.sample(20, tune=10, init=None, trace=db, cores=2)

def teardown_method(self):
bf.remove_file_or_directory(self.name)
Expand Down

0 comments on commit 7515d0f

Please sign in to comment.