Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion autofit/interpolator/covariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ def _relationships_for_path(
"""
analysis = self._analysis_for_path(path)
model = self.model(path_relationship_map=path_relationship_map or {})
search = DynestyStatic()
# A small internal fit, run on one core: the `number_of_cores="auto"` default
# would fork a pool of every core for it.
search = DynestyStatic(number_of_cores=1)
result = search.fit(model=model, analysis=analysis)
return result.instance

Expand Down
7 changes: 6 additions & 1 deletion autofit/non_linear/grid/grid_search/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(
self,
search,
number_of_steps: int = 4,
number_of_cores: int = 1,
number_of_cores: Union[int, str] = 1,
result_output_interval: int = 100,
):
"""
Expand All @@ -41,6 +41,11 @@ def __init__(
The number of steps to go in each direction
search
The class of the search that is run at each step
number_of_cores
The number of processes the grid's jobs (one fit each) run across: a whole number >= 1 (default 1,
the jobs run in turn and each job's search resolves its own `number_of_cores`), or `"auto"`, the
detected cores. A JAX analysis runs its jobs serially (the JAX fork rule); when the jobs run in
parallel each job's search runs with one core.
result_output_interval
The interval between saving a GridSearchResult object via paths
"""
Expand Down
4 changes: 2 additions & 2 deletions autofit/non_linear/search/_template/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

from __future__ import annotations

from typing import Optional
from typing import Optional, Union

import numpy as np

Expand Down Expand Up @@ -96,7 +96,7 @@ def __init__(
initializer: Optional[AbstractInitializer] = None,
iterations_per_quick_update: int = None,
iterations_per_full_update: int = None,
number_of_cores: int = 1,
number_of_cores: Union[int, str] = "auto",
silence: bool = False,
session=None,
**kwargs,
Expand Down
2 changes: 1 addition & 1 deletion autofit/non_linear/search/abstract_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def __init__(
iterations_per_quick_update: Optional[int] = None,
iterations_per_full_update: int = None,
live_visual_update: Optional[bool] = None,
number_of_cores: Union[int, str] = 1,
number_of_cores: Union[int, str] = "auto",
silence: bool = False,
session: Optional[sa.orm.Session] = None,
paths: Optional[AbstractPaths] = None,
Expand Down
4 changes: 2 additions & 2 deletions autofit/non_linear/search/mcmc/abstract_mcmc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Optional, TYPE_CHECKING
from typing import Optional, TYPE_CHECKING, Union

from autofit.non_linear.search.abstract_search import NonLinearSearch
from autofit.non_linear.initializer import Initializer
Expand Down Expand Up @@ -40,7 +40,7 @@ def __init__(
auto_correlation_settings=AutoCorrelationsSettings(),
iterations_per_full_update: Optional[int] = None,
iterations_per_quick_update: int = None,
number_of_cores: int = 1,
number_of_cores: Union[int, str] = "auto",
silence: bool = False,
session: Optional[sa.orm.Session] = None,
**kwargs
Expand Down
4 changes: 2 additions & 2 deletions autofit/non_linear/search/mcmc/blackjax/nuts/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import os
from pathlib import Path
from typing import Optional, TYPE_CHECKING
from typing import Optional, TYPE_CHECKING, Union

import numpy as np

Expand Down Expand Up @@ -88,7 +88,7 @@ def __init__(
auto_correlation_settings: Optional[AutoCorrelationsSettings] = None,
iterations_per_quick_update: Optional[int] = None,
iterations_per_full_update: Optional[int] = None,
number_of_cores: int = 1,
number_of_cores: Union[int, str] = "auto",
silence: bool = False,
session: Optional[sa.orm.Session] = None,
**kwargs,
Expand Down
4 changes: 2 additions & 2 deletions autofit/non_linear/search/mcmc/blackjax/smc/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
import warnings
from pathlib import Path
from typing import List, Optional, Tuple, TYPE_CHECKING
from typing import List, Optional, Tuple, TYPE_CHECKING, Union

import numpy as np

Expand Down Expand Up @@ -108,7 +108,7 @@ def __init__(
*legacy_positional,
iterations_per_quick_update: Optional[int] = None,
iterations_per_full_update: Optional[int] = None,
number_of_cores: int = 1,
number_of_cores: Union[int, str] = "auto",
silence: bool = False,
session: Optional[sa.orm.Session] = None,
**kwargs,
Expand Down
8 changes: 5 additions & 3 deletions autofit/non_linear/search/mcmc/emcee/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import os
from pathlib import Path
from typing import Dict, Optional, TYPE_CHECKING
from typing import Dict, Optional, TYPE_CHECKING, Union

import numpy as np

Expand Down Expand Up @@ -75,7 +75,7 @@ def __init__(
auto_correlation_settings: Optional[AutoCorrelationsSettings] = None,
iterations_per_quick_update: int = None,
iterations_per_full_update: int = None,
number_of_cores: int = 1,
number_of_cores: Union[int, str] = "auto",
silence: bool = False,
session: Optional[sa.orm.Session] = None,
**kwargs,
Expand Down Expand Up @@ -110,7 +110,9 @@ def __init__(
auto_correlation_settings
Customizes and performs auto correlation calculations performed during and after the search.
number_of_cores
The number of cores sampling is performed using a Python multiprocessing Pool instance.
The number of cores sampling is performed using a Python multiprocessing Pool instance: a whole
number >= 1, or `"auto"` (the default), every core this process may use, resolved when the fit starts
(1 for a JAX analysis).
silence
If True, the default print output of the non-linear search is silenced.
session
Expand Down
34 changes: 23 additions & 11 deletions autofit/non_linear/search/mcmc/zeus/search.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import logging
from typing import Dict, Optional, TYPE_CHECKING
from typing import Dict, Optional, TYPE_CHECKING, Union

import numpy as np
import os
Expand Down Expand Up @@ -81,7 +81,7 @@ def __init__(
auto_correlation_settings: Optional[AutoCorrelationsSettings] = None,
iterations_per_quick_update: int = None,
iterations_per_full_update: int = None,
number_of_cores: int = 1,
number_of_cores: Union[int, str] = "auto",
silence: bool = False,
session: Optional[sa.orm.Session] = None,
**kwargs
Expand Down Expand Up @@ -116,7 +116,9 @@ def __init__(
auto_correlation_settings
Customizes and performs auto correlation calculations performed during and after the search.
number_of_cores
The number of cores Zeus sampling is performed using a Python multiprocessing Pool instance.
The number of cores Zeus sampling is performed using a Python multiprocessing Pool instance: a whole
number >= 1, or `"auto"` (the default), every core this process may use, resolved when the fit starts
(1 for a JAX analysis).
silence
If True, the default print output of the non-linear search is silenced.
session
Expand Down Expand Up @@ -210,7 +212,6 @@ def run(self, ctx):
nwalkers=self.nwalkers,
ndim=model.prior_count,
logprob_fn=ctx.fitness.call_wrap,
pool=ctx.pool(),
)

search_internal.ncall_total = 0
Expand All @@ -225,6 +226,11 @@ def run(self, ctx):
total_iterations = 0
iterations_remaining = self.nsteps

# The pool (``None`` on one core) is attached only while zeus samples: a
# multiprocessing pool cannot be pickled, and the sampler is saved (resume state,
# quick updates, the final archive) between and after the sampling calls.
pool = ctx.pool() if iterations_remaining > 0 else None

while iterations_remaining > 0:
# zeus casts internally (``self.nsteps = int(iterations)``), so a
# float never crashes it — but ``total_iterations`` below is
Expand All @@ -233,13 +239,19 @@ def run(self, ctx):
# (PyAutoFit#1422).
iterations = ctx.schedule.next_budget(total_iterations, self.nsteps)

for sample in search_internal.sample(
start=state,
log_prob0=log_posterior_list,
iterations=iterations,
progress=True,
):
pass
search_internal.pool = pool

try:
for sample in search_internal.sample(
start=state,
log_prob0=log_posterior_list,
iterations=iterations,
progress=True,
):
pass
finally:
search_internal.pool = None
search_internal.distribute = map

search_internal.ncall_total += search_internal.ncall

Expand Down
4 changes: 2 additions & 2 deletions autofit/non_linear/search/nest/abstract_nest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from abc import ABC
from typing import Optional, TYPE_CHECKING
from typing import Optional, TYPE_CHECKING, Union

from autofit.non_linear.search.abstract_search import NonLinearSearch
from autofit.non_linear.initializer import AbstractInitializer
Expand Down Expand Up @@ -38,7 +38,7 @@ def __init__(
unique_tag: Optional[str] = None,
iterations_per_quick_update: Optional[int] = None,
iterations_per_full_update: Optional[int] = None,
number_of_cores: int = 1,
number_of_cores: Union[int, str] = "auto",
silence: bool = False,
session: Optional[sa.orm.Session] = None,
initializer: Optional[AbstractInitializer] = None,
Expand Down
7 changes: 4 additions & 3 deletions autofit/non_linear/search/nest/dynesty/search/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def __init__(
maxcall: Optional[int] = None,
iterations_per_quick_update: int = None,
iterations_per_full_update: int = None,
number_of_cores: int = 1,
number_of_cores: Union[int, str] = "auto",
silence: bool = False,
force_x1_cpu: bool = False,
use_jax_jit: Optional[bool] = None,
Expand Down Expand Up @@ -163,8 +163,9 @@ def __init__(
iterations_per_full_update
The number of iterations performed between every Dynesty back-up.
number_of_cores
The number of cores sampling is performed using a Python multiprocessing Pool instance. A value of 1
builds no pool and runs serially.
The number of cores sampling is performed using a Python multiprocessing Pool instance: a whole
number >= 1, or `"auto"` (the default), every core this process may use, resolved when the fit starts
(1 for a JAX analysis). A resolved value of 1 builds no pool and runs serially.
silence
If True, the default print output of the non-linear search is silenced.
force_x1_cpu
Expand Down
8 changes: 5 additions & 3 deletions autofit/non_linear/search/nest/dynesty/search/dynamic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import sys
from typing import Dict, Optional
from typing import Dict, Optional, Union

from autofit.mapper.prior_model.abstract import AbstractPriorModel

Expand Down Expand Up @@ -50,7 +50,7 @@ def __init__(
n_effective: Optional[int] = None,
iterations_per_quick_update: int = None,
iterations_per_full_update: int = None,
number_of_cores: int = 1,
number_of_cores: Union[int, str] = "auto",
silence: bool = False,
**kwargs
):
Expand Down Expand Up @@ -85,7 +85,9 @@ def __init__(
iterations_per_full_update
The number of iterations performed between update (e.g. output latest model to hard-disk, visualization).
number_of_cores
The number of cores sampling is performed using a Python multiprocessing Pool instance.
The number of cores sampling is performed using a Python multiprocessing Pool instance: a whole
number >= 1, or `"auto"` (the default), every core this process may use, resolved when the fit starts
(1 for a JAX analysis).
"""

super().__init__(
Expand Down
6 changes: 4 additions & 2 deletions autofit/non_linear/search/nest/dynesty/search/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __init__(
logl_max: float = float("inf"),
iterations_per_quick_update: int = None,
iterations_per_full_update: int = None,
number_of_cores: int = 1,
number_of_cores: Union[int, str] = "auto",
silence: bool = False,
session: Optional[sa.orm.Session] = None,
**kwargs,
Expand Down Expand Up @@ -86,7 +86,9 @@ def __init__(
iterations_per_full_update
The number of iterations performed between update (e.g. output latest model to hard-disk, visualization).
number_of_cores
The number of cores sampling is performed using a Python multiprocessing Pool instance.
The number of cores sampling is performed using a Python multiprocessing Pool instance: a whole
number >= 1, or `"auto"` (the default), every core this process may use, resolved when the fit starts
(1 for a JAX analysis).
session
An SQLalchemy session instance so the results of the model-fit are written to an SQLite database.
"""
Expand Down
8 changes: 5 additions & 3 deletions autofit/non_linear/search/nest/nautilus/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from contextlib import contextmanager, nullcontext
from pathlib import Path
from time import monotonic
from typing import Dict, Optional, Tuple, TYPE_CHECKING
from typing import Dict, Optional, Tuple, TYPE_CHECKING, Union


from autofit import exc
Expand Down Expand Up @@ -342,7 +342,7 @@ def __init__(
verbose: bool = True,
iterations_per_quick_update: Optional[int] = None,
iterations_per_full_update: int = None,
number_of_cores: int = 1,
number_of_cores: Union[int, str] = "auto",
silence: bool = False,
force_x1_cpu: bool = False,
session: Optional[sa.orm.Session] = None,
Expand Down Expand Up @@ -399,7 +399,9 @@ def __init__(
iterations_per_full_update
The number of iterations performed between update (e.g. output latest model to hard-disk, visualization).
number_of_cores
The number of cores sampling is performed using a Python multiprocessing Pool instance.
The number of cores sampling is performed using a Python multiprocessing Pool instance: a whole
number >= 1, or `"auto"` (the default), every core this process may use, resolved when the fit starts
(1 for a JAX analysis).

Not used by expectation-propagation factor searches, which refuse
`number_of_cores > 1` outright (`AbstractSearch.optimise`, human
Expand Down
4 changes: 2 additions & 2 deletions autofit/non_linear/search/nest/nss/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
import pickle
from pathlib import Path
from typing import Optional, TYPE_CHECKING
from typing import Optional, TYPE_CHECKING, Union

import numpy as np

Expand Down Expand Up @@ -185,7 +185,7 @@ def __init__(
checkpoint_interval: int = 100,
iterations_per_quick_update: Optional[int] = None,
iterations_per_full_update: Optional[int] = None,
number_of_cores: int = 1,
number_of_cores: Union[int, str] = "auto",
seed: int = 42,
silence: bool = False,
session: Optional[sa.orm.Session] = None,
Expand Down
4 changes: 2 additions & 2 deletions autofit/non_linear/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(
self,
path_prefix: str,
unique_tag: Optional[str] = None,
number_of_cores: Optional[Union[int, str]] = 1,
number_of_cores: Optional[Union[int, str]] = "auto",
session: Optional[sa.orm.Session] = None,
info: Optional[dict] = None,
use_jax_vmap: Optional[bool] = None,
Expand All @@ -36,7 +36,7 @@ def __init__(
for most model fits, but is done on a per-fit basis for grid based searches (e.g. sensitivity mapping).
A whole number >= 1, or `"auto"` to use every core this process may use, resolved when the fit starts
(after the analysis has resolved its compute setup; 1 for a JAX analysis, and capped by memory when the
analysis declares `resources_per_eval()`). The default is 1.
analysis declares `resources_per_eval()`). The default is `"auto"`.
session
The SQLite database session which is active means results are directly wrtten to the SQLite database
at the end of a fit and loaded from the database at the start.
Expand Down
2 changes: 1 addition & 1 deletion test_autofit/non_linear/search/mcmc/test_emcee.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test__explicit_params():
assert search.auto_correlation_settings.check_size == 100
assert search.auto_correlation_settings.required_length == 50
assert search.auto_correlation_settings.change_threshold == 0.01
assert search.number_of_cores == 1
assert search.number_of_cores == "auto"


def test__log_posteriors_are_aligned_with_the_thinned_chain(monkeypatch):
Expand Down
2 changes: 1 addition & 1 deletion test_autofit/non_linear/search/mcmc/test_zeus.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test__explicit_params():
assert search.auto_correlation_settings.check_size == 100
assert search.auto_correlation_settings.required_length == 50
assert search.auto_correlation_settings.change_threshold == 0.01
assert search.number_of_cores == 1
assert search.number_of_cores == "auto"


requires_zeus = pytest.mark.skipif(
Expand Down
Loading
Loading