Skip to content

Commit 33b9871

Browse files
Jammy2211Jammy2211
authored andcommitted
all tests pass
1 parent 74a920a commit 33b9871

6 files changed

Lines changed: 42 additions & 30 deletions

File tree

autofit/jax_wrapper.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
use_jax = conf.instance["general"]["jax"]["use_jax"]
1414

15+
if DISABLE_JAX != 0:
16+
use_jax = False
17+
1518
if use_jax and DISABLE_JAX == 0:
1619

1720
from jax import numpy

autofit/mapper/prior/vectorized.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,14 @@ def __call__(self, cube: np.ndarray) -> np.ndarray:
135135
Transformed parameters of shape (n_samples, n_priors).
136136
"""
137137

138-
n_samples, n_priors = cube.shape
138+
cube_reshaped = False
139+
140+
if len(cube.shape) == 1:
141+
cube = cube[None, :]
142+
143+
cube_reshaped = True
144+
145+
139146
out = np.empty_like(cube)
140147

141148
# 2) Batch‐process all UniformPriors
@@ -177,4 +184,8 @@ def __call__(self, cube: np.ndarray) -> np.ndarray:
177184
+ subcube * (self.loguniform_log_uppers - self.loguniform_log_lowers)
178185
)
179186

187+
if cube_reshaped:
188+
189+
return out[0]
190+
180191
return out

autofit/non_linear/fitness.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import jax
22
import logging
3+
import numpy as np
34
import os
45
import time
56

@@ -108,9 +109,6 @@ def __init__(
108109
self.convert_to_chi_squared = convert_to_chi_squared
109110
self.store_history = store_history
110111

111-
if self.paths is not None:
112-
self.check_log_likelihood(fitness=self)
113-
114112
self.parameters_history_list = []
115113
self.log_likelihood_history_list = []
116114

@@ -122,6 +120,9 @@ def __init__(
122120
if self.use_jax_vmap:
123121
self._call = self._vmap
124122

123+
if self.paths is not None:
124+
self.check_log_likelihood(fitness=self)
125+
125126
def call(self, parameters):
126127
"""
127128
A private method that calls the fitness function with the given parameters and additional keyword arguments.
@@ -189,8 +190,14 @@ def call_wrap(self, parameters):
189190
the log-likelihood itself or another objective function value,
190191
depending on configuration.
191192
"""
193+
if self.use_jax_vmap:
194+
if len(np.array(parameters).shape) == 1:
195+
parameters = np.array(parameters)[None, :]
196+
192197
figure_of_merit = self._call(parameters)
193198

199+
print(figure_of_merit)
200+
194201
if self.fom_is_log_likelihood:
195202
log_likelihood = figure_of_merit
196203
else:
@@ -224,7 +231,7 @@ def __call__(self, parameters, *kwargs):
224231
-------
225232
The figure of merit returned to the non-linear search, which is either the log likelihood or log posterior.
226233
"""
227-
return self._call(parameters)
234+
return self.call_wrap(parameters)
228235

229236
# def __getstate__(self):
230237
# state = self.__dict__.copy()

autofit/non_linear/search/abstract_search.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
from autoconf.output import should_output
2424

25+
from autofit.jax_wrapper import numpy as xp
26+
2527
from autofit import exc, jax_wrapper
2628
from autofit.database.sqlalchemy_ import sa
2729
from autofit.graphical import (
@@ -913,7 +915,6 @@ def perform_update(
913915
)
914916
self.paths.save_samples(samples=samples_save)
915917

916-
latent_samples = None
917918

918919
if (during_analysis and conf.instance["output"]["latent_during_fit"]) or (
919920
not during_analysis and conf.instance["output"]["latent_after_fit"]
@@ -978,7 +979,7 @@ def perform_update(
978979
parameters = samples.max_log_likelihood(as_instance=False)
979980

980981
start = time.time()
981-
figure_of_merit = fitness(parameters)
982+
figure_of_merit = fitness.call_wrap(parameters)
982983

983984
# account for asynchronous JAX calls
984985
np.array(figure_of_merit)

autofit/non_linear/search/nest/nautilus/search.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -145,28 +145,21 @@ def _fit(self, model: AbstractPriorModel, analysis):
145145
analysis=analysis,
146146
)
147147
else:
148-
if not self.using_mpi:
149148

150-
fitness = Fitness(
151-
model=model,
152-
analysis=analysis,
153-
paths=self.paths,
154-
fom_is_log_likelihood=True,
155-
resample_figure_of_merit=-1.0e99,
156-
)
149+
fitness = Fitness(
150+
model=model,
151+
analysis=analysis,
152+
paths=self.paths,
153+
fom_is_log_likelihood=True,
154+
resample_figure_of_merit=-1.0e99,
155+
)
156+
157+
search_internal = self.fit_multiprocessing(
158+
fitness=fitness,
159+
model=model,
160+
analysis=analysis,
161+
)
157162

158-
search_internal = self.fit_multiprocessing(
159-
fitness=fitness,
160-
model=model,
161-
analysis=analysis,
162-
)
163-
else:
164-
search_internal = self.fit_mpi(
165-
fitness=fitness,
166-
model=model,
167-
analysis=analysis,
168-
checkpoint_exists=checkpoint_exists,
169-
)
170163
return search_internal, fitness
171164

172165
@property
@@ -229,7 +222,6 @@ def fit_x1_cpu(self, fitness, model, analysis):
229222
prior=PriorVectorized(model=model),
230223
likelihood=fitness.call_wrap,
231224
n_dim=model.prior_count,
232-
# prior_kwargs={"model": model},
233225
filepath=self.checkpoint_file,
234226
pool=None,
235227
vectorized=True,
@@ -263,7 +255,6 @@ def fit_multiprocessing(self, fitness, model, analysis):
263255
prior=PriorVectorized(model=model),
264256
likelihood=fitness.call_wrap,
265257
n_dim=model.prior_count,
266-
prior_kwargs={"model": model},
267258
filepath=self.checkpoint_file,
268259
pool=self.number_of_cores,
269260
**self.config_dict_search,

test_autofit/non_linear/test_dict.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def make_dynesty_dict():
3434
"sample": "auto",
3535
"slices": 5,
3636
"unique_tag": None,
37-
"use_gradient": False,
3837
"walks": 5,
3938
},
4039
"class_path": "autofit.non_linear.search.nest.dynesty.search.static.DynestyStatic",

0 commit comments

Comments
 (0)