Skip to content

Commit 74a920a

Browse files
Jammy2211Jammy2211
authored andcommitted
remove MPI
1 parent 02e576e commit 74a920a

9 files changed

Lines changed: 107 additions & 250 deletions

File tree

autofit/example/analysis.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import numpy as np
2+
from typing import Dict, Optional
13

2-
from typing import Dict, List, Optional
3-
4-
from autofit.jax_wrapper import numpy as np
4+
from autofit.jax_wrapper import numpy as xp
55

66
import autofit as af
77

@@ -98,8 +98,8 @@ def model_data_1d_from(self, instance: af.ModelInstance) -> np.ndarray:
9898
The model data of the profiles.
9999
"""
100100

101-
xvalues = np.arange(self.data.shape[0])
102-
model_data_1d = np.zeros(self.data.shape[0])
101+
xvalues = xp.arange(self.data.shape[0])
102+
model_data_1d = xp.zeros(self.data.shape[0])
103103

104104
try:
105105
for profile in instance:

autofit/example/model.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import math
2+
import numpy as np
23
from typing import Tuple
34

4-
from autofit.jax_wrapper import numpy as np
5+
from autofit.jax_wrapper import numpy as xp
56

67
"""
78
The `Gaussian` class in this module is the model components that is fitted to data using a non-linear search. The
@@ -46,7 +47,7 @@ def fwhm(self) -> float:
4647
the free parameters of the model which we are interested and may want to store the full samples information
4748
on (e.g. to create posteriors).
4849
"""
49-
return 2 * np.sqrt(2 * np.log(2)) * self.sigma
50+
return 2 * xp.sqrt(2 * xp.log(2)) * self.sigma
5051

5152
def _tree_flatten(self):
5253
return (self.centre, self.normalization, self.sigma), None
@@ -76,16 +77,16 @@ def model_data_from(self, xvalues: np.ndarray) -> np.ndarray:
7677
"""
7778
transformed_xvalues = xvalues - self.centre
7879

79-
return np.multiply(
80-
np.divide(self.normalization, self.sigma * np.sqrt(2.0 * np.pi)),
81-
np.exp(-0.5 * np.square(np.divide(transformed_xvalues, self.sigma))),
80+
return xp.multiply(
81+
xp.divide(self.normalization, self.sigma * xp.sqrt(2.0 * xp.pi)),
82+
xp.exp(-0.5 * xp.square(xp.divide(transformed_xvalues, self.sigma))),
8283
)
8384

8485
def f(self, x: float):
8586
return (
8687
self.normalization
87-
/ (self.sigma * np.sqrt(2 * math.pi))
88-
* np.exp(-0.5 * ((x - self.centre) / self.sigma) ** 2)
88+
/ (self.sigma * xp.sqrt(2 * math.pi))
89+
* xp.exp(-0.5 * ((x - self.centre) / self.sigma) ** 2)
8990
)
9091

9192
def __call__(self, xvalues: np.ndarray) -> np.ndarray:
@@ -147,9 +148,9 @@ def model_data_from(self, xvalues: np.ndarray) -> np.ndarray:
147148
values
148149
The x coordinates in the original reference frame of the grid.
149150
"""
150-
transformed_xvalues = np.subtract(xvalues, self.centre)
151-
return self.normalization * np.multiply(
152-
self.rate, np.exp(-1.0 * self.rate * abs(transformed_xvalues))
151+
transformed_xvalues = xp.subtract(xvalues, self.centre)
152+
return self.normalization * xp.multiply(
153+
self.rate, xp.exp(-1.0 * self.rate * abs(transformed_xvalues))
153154
)
154155

155156
def __call__(self, xvalues: np.ndarray) -> np.ndarray:

autofit/mapper/prior_model/array.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from autoconf.dictable import from_dict
44
from .abstract import AbstractPriorModel
55
from autofit.mapper.prior.abstract import Prior
6-
from autofit.jax_wrapper import numpy as jnp, use_jax
6+
from autofit.jax_wrapper import numpy as xp, use_jax
77
import numpy as np
88

99
from autofit.jax_wrapper import register_pytree_node_class
@@ -77,7 +77,7 @@ def _instance_for_arguments(
7777
-------
7878
The array with the priors replaced.
7979
"""
80-
array = jnp.zeros(self.shape)
80+
array = xp.zeros(self.shape)
8181
for index in self.indices:
8282
value = self[index]
8383
try:

autofit/non_linear/fitness.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from autoconf import cached_property
1111

1212
from autofit import jax_wrapper
13-
from autofit.jax_wrapper import numpy as np
13+
from autofit.jax_wrapper import numpy as xp
1414
from autofit import exc
1515

1616

@@ -36,10 +36,10 @@ def __init__(
3636
analysis : Analysis,
3737
paths : Optional[AbstractPaths] = None,
3838
fom_is_log_likelihood: bool = True,
39-
resample_figure_of_merit: float = -np.inf,
39+
resample_figure_of_merit: float = -xp.inf,
4040
convert_to_chi_squared: bool = False,
4141
store_history: bool = False,
42-
use_jax_vmap : bool = True
42+
use_jax_vmap : bool = False
4343
):
4444
"""
4545
Interfaces with any non-linear search to fit the model to the data and return a log likelihood via
@@ -147,15 +147,15 @@ def call(self, parameters):
147147
log_likelihood = self.analysis.log_likelihood_function(instance=instance)
148148

149149
# Penalize NaNs in the log-likelihood
150-
log_likelihood = np.where(np.isnan(log_likelihood), self.resample_figure_of_merit, log_likelihood)
150+
log_likelihood = xp.where(xp.isnan(log_likelihood), self.resample_figure_of_merit, log_likelihood)
151151

152152
# Determine final figure of merit
153153
if self.fom_is_log_likelihood:
154154
figure_of_merit = log_likelihood
155155
else:
156156
# Ensure prior list is compatible with JAX (must return a JAX array, not list)
157-
log_prior_array = np.array(self.model.log_prior_list_from_vector(vector=parameters))
158-
figure_of_merit = log_likelihood + np.sum(log_prior_array)
157+
log_prior_array = xp.array(self.model.log_prior_list_from_vector(vector=parameters))
158+
figure_of_merit = log_likelihood + xp.sum(log_prior_array)
159159

160160
# Convert to chi-squared scale if requested
161161
if self.convert_to_chi_squared:
@@ -194,8 +194,8 @@ def call_wrap(self, parameters):
194194
if self.fom_is_log_likelihood:
195195
log_likelihood = figure_of_merit
196196
else:
197-
log_prior_list = np.array(self.model.log_prior_list_from_vector(vector=parameters))
198-
log_likelihood = figure_of_merit - np.sum(log_prior_list)
197+
log_prior_list = xp.array(self.model.log_prior_list_from_vector(vector=parameters))
198+
log_likelihood = figure_of_merit - xp.sum(log_prior_list)
199199

200200
if self.store_history:
201201

@@ -322,6 +322,7 @@ def check_log_likelihood(self, fitness):
322322
result
323323
The result containing the maximum log likelihood fit of the model.
324324
"""
325+
import numpy as np
325326

326327
if os.environ.get("PYAUTOFIT_TEST_MODE") == "1":
327328
return

autofit/non_linear/parallel/sneaky.py

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -373,13 +373,6 @@ def __init__(
373373
self.prior_transform_kwargs = prior_transform_kwargs or {}
374374
self.processes = processes
375375
self.pool = None
376-
try:
377-
from mpi4py import MPI
378-
379-
self.comm = MPI.COMM_WORLD
380-
self._processes = self.comm.size
381-
except ModuleNotFoundError:
382-
self._processes = 1
383376

384377
init_args = (
385378
self.fitness_init,
@@ -391,46 +384,16 @@ def __init__(
391384
)
392385
initializer(*init_args)
393386

394-
def check_if_mpi(self):
395-
return self._processes > 1
396-
397-
def is_master(self):
398-
is_mpi = self.check_if_mpi()
399-
if is_mpi:
400-
return_value = self.comm.rank == 0
401-
else:
402-
return_value = True
403-
404-
return return_value
405-
406-
def wait(self):
407-
is_mpi = self.check_if_mpi()
408-
if is_mpi:
409-
self.pool.wait()
410-
else:
411-
pass
412-
warnings.warn("Cannot wait for pool to finish if not using MPI")
413-
414387
def __enter__(self):
415388
"""
416389
Activate the mp / mpi pool
417390
"""
418391

419-
use_mpi = self.check_if_mpi()
420-
421-
if use_mpi:
422-
from schwimmbad import MPIPool
423-
424-
if self.is_master():
425-
logger.info("... using Schwimmbad MPIPool")
426-
self.pool = MPIPool(use_dill=True)
392+
logger.info("... using multiprocessing")
427393

428-
else:
429-
if self.is_master():
430-
logger.info("... using multiprocessing")
431-
self.pool = mp.Pool(
432-
processes=self.processes,
433-
)
394+
self.pool = mp.Pool(
395+
processes=self.processes,
396+
)
434397

435398
return self
436399

0 commit comments

Comments
 (0)