Add Batch Sampler: coordinate parallel workers into jointly-selected batches#376
Add Batch Sampler: coordinate parallel workers into jointly-selected batches#376sign-of-fourier wants to merge 3 commits into
Conversation
Adds package/samplers/q_ei_sampler — a batch Bayesian optimisation sampler that delegates GP fitting and q-EI candidate scoring to a remote HTTP endpoint, with random-search fallback during startup. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
@sawa3030 Could you review this PR? |
sawa3030
left a comment
There was a problem hiding this comment.
Thank you for the PR! I've left a few comments. PTAL!
| return {} | ||
|
|
||
| X = [[float(t.params[n]) for n in param_names] for t in usable] | ||
| # Negate values: q-EI maximises, Optuna minimises. |
There was a problem hiding this comment.
As you showed in the example, Optuna can define the optimization direction explicitly. For example, the user can make Optuna maximize the objective by writing:
study = optuna.create_study(direction="maximize", sampler=sampler)In addition, I believe the optimization direction of the remote optimizer depends on its implementer or user. Therefore, it is not necessarily true that remote optimizer always maximizes the objective.
How about leaving it to the user to align the optimization directions between the remote optimizer and Optuna?
There was a problem hiding this comment.
Got it. Don't negate it and the user will align. If neither direction nor directions is chosen, then the default is minimize. So, here just leave it out because this part doesn't depend on a particular backend. It will follow expected, normal Optuna behavior.
| @@ -0,0 +1,145 @@ | |||
| --- | |||
| author: Mark Shipman | |||
| title: Remote GP q-EI Sampler | |||
There was a problem hiding this comment.
I believe this sampler could also be useful for optimization methods other than Bayesian optimization, even though it certainly works well in the Bayesian optimization setting.
If this understanding is correct, would it make sense to make the explanations and naming a bit more general so that the sampler can cover a broader range of optimizers?
There was a problem hiding this comment.
Makes sense. Something like "Remote Sampler", but then for the example, describe q-EI as an example.
| ### Backend endpoint | ||
|
|
||
| You must supply an `api_url` that implements the contract above. Two options: | ||
|
|
||
| **Use the hosted endpoint** (no setup required): | ||
|
|
||
| ``` | ||
| https://markshipman4273--bo-gp-service-gp-suggest.modal.run | ||
| ``` | ||
|
|
||
| This is a publicly available Modal deployment. Pass it directly as `api_url`. | ||
|
|
||
| **Deploy your own** using the open-source backend at | ||
| [sign-of-fourier/quantecarlo](https://github.com/sign-of-fourier/quantecarlo), | ||
| or implement the request/response contract on any HTTP server. |
There was a problem hiding this comment.
Could you please remove this information from the documentation?
As we do not officially support calling third-party APIs in this context, I think it may be better not to include this example here.
Thank you very much for your understanding.
There was a problem hiding this comment.
Yes, I will remove. I'll change the language to explain q-EI as an example as to why one would need to use a remote source for this computation and describe what it would do, but I'll leave out any actual link.
|
Thank you all for taking the time to look at this PR. I will make these changes by next week. |
- Rename package directory and class (qEISampler → BatchSampler) - Replace api_url with suggest_fn: Callable[[X, y, search_space, q], candidates] - Remove all HTTP code, negation, and debug mode — sampler is backend-agnostic - Update README: general framing, callable contract, quantecarlo example - Update example to use fantasize_suggest from quantecarlo (local GP, no external service) - Bump requirements to optuna>=4.0.0
|
Thanks everyone for your feedback. Hopefully, I've addressed everything
|
Summary
BatchSampler, an OptunaBaseSamplerthat solves the coordination problem when runningstudy.optimize(n_jobs=q).BatchSampleruses a shared lock so one worker calls a user-suppliedsuggest_fnto obtainqsuggestions jointly, then hands them out one at a time to the waiting workers.suggest_fnis a plain Python callable — the sampler makes no HTTP calls and has no dependency on any external service. Users can plug in BoTorch, a local GP, or any batch acquisition function.fantasize_suggestfrom thequantecarlopackage (PyPI), a self-contained sequential kriging function that requires no external service.Test plan
ruff check package/samplers/batch_sampler/passesmypy package/samplers/batch_sampler/ --ignore-missing-importspassespip install quantecarlo && python package/samplers/batch_sampler/example.pyruns to completion