joshspeagle / dynesty

Dynamic Nested Sampling package for computing Bayesian posteriors and evidences
https://dynesty.readthedocs.io/
MIT License
346 stars 76 forks source link

Resuming pooled sampler with pool doesn't use the pool #444

Closed OGdodge closed 1 year ago

OGdodge commented 1 year ago

Dynesty version Specify the exact version, and how you installed it (pip/git) 2.1.1, pip

Describe the bug [A clear and concise description of what the bug is.] Resuming a sampler in a pool does not actually use the pool

Setup I am runningthe following dynesty setup: [insert a copy of the dynesty code used during initialization and runtime]

import dynesty
...
def dynamic_dynesty(self):
        if not HAS_DYNESTY:
           print("Error: Cannot run nested sampler "
                 "wihout the dynesty package installed!")
           sys.exit(1)
        resume_flag = False
        print('Calling prepare_params')
        self._prepare_params()
        resume_file = glob.glob(f'{self.name}.sampler')
        if len(resume_file) == 1:
           print(f'Saved run found! Resuming w/ {self.name}...')
           if len(glob.glob(f'{self.name}.dyMN')) != 1:
               print('No associated object file, pickling dyMN object...')
               with open(f'{self.name}.dyMN', 'wb') as f:
                   pk.dump(self, f)
           resume_flag = True
        else:
            with open(f'{self.name}.dyMN', 'wb') as f:
                pk.dump(self, f)

        if self.use_pool:
            with mp.Pool(self.npool) as pool:
                pool.size = self.npool
                if resume_flag:
                    sampler = dynesty.DynamicNestedSampler.restore(resume_file[0]), pool=pool)
                    result = sampler.run_nested(resume=True)
                    return result

                sampler = dynesty.DynamicNestedSampler(self.LogLikelihood, self.inv_CDF_prior_transform,
                                                       ndim=self.ndim,
                                                       bound='multi',
                                                       update_interval=10,
                                                       pool=pool)

                result = sampler.run_nested(dlogz_init=0.05,
                                           nlive_init=self.nlive,
                                           nlive_batch=int(self.nlive/5),
                                           checkpoint_file=f'{self.name}.sampler')

                return result

Bug / Dynesty output I have a number of actions that are performed once for the first call of the LogLikelihood function - each with some print statements. These are printed once per pool that is active - so when running the sampler on 20 threads without resuming I get:

0it [00:00, ?it/s]Preparing Icarus objects...
Using Normal
Preparing Icarus objects...
Using Normal
Preparing Icarus objects...
Using Normal
Preparing Icarus objects...
Using Normal
Preparing Icarus objects...
Using Normal
Preparing Icarus objects...
Using Normal
Preparing Icarus objects...
Using Normal
Preparing Icarus objects...
Using Normal
Preparing Icarus objects...
Using Normal
Preparing Icarus objects...
Using Normal
Preparing Icarus objects...
Using Normal
Preparing Icarus objects...
Using Normal
Preparing Icarus objects...
Using Normal
Preparing Icarus objects...
Using Normal
Preparing Icarus objects...
Using Normal
Preparing Icarus objects...
Using Normal
Preparing Icarus objects...
Using Normal
Preparing Icarus objects...
Using Normal
Preparing Icarus objects...
Using Normal
Preparing Icarus objects...
Using Normal

Whereas when resuming, with still with 20 threads specificied:

0it [00:00, ?it/s]Preparing Icarus objects...
Using Normal
Pickling photometry object...

Ie. this is only printed once - so only running for 1 thread. It doesn't error out, just continues only on one thread. I have ensured that it actually enters the pool. Any help would be appriecated - tbh I think I might just be missing something really obvious!

segasai commented 1 year ago

Please try the master branch -- there was a fix for the restoration of the dynamic sampler -- see https://github.com/joshspeagle/dynesty/blob/master/CHANGELOG.md I just didn't yet cut a release.

segasai commented 1 year ago

I've just released the v2.1.2, so you can use that now. hopefully it'll fix your issue

segasai commented 1 year ago

I will close it. Feel free to reopen if the issue is still there