feat: add support of seeding #251
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces full support for custom RNG objects across the library. Previously, NePS relied on general/global RNG handling. With this update, users can now provide their own NumPy and PyTorch RNG instances for complete control over reproducibility.
Key Changes
Support for custom RNGs instead of global RNGs
The library no longer depends on global RNG state. Users who need strict reproducibility can now supply their own numpy_rng or torch_rng objects. If both are provided, NePS will use them directly and ignore the seed argument for NumPy and Torch.
Custom Categorical distribution with RNG support
PyTorch’s built-in Categorical distribution does not accept a Generator. To enable deterministic sampling, a custom wrapper class is added that forwards calls using the provided torch Generator.
Handling for Bayesian optimization internals
The BO backend is not thread-safe and temporarily modifies the global RNG because acquisition functions in PyTorch only work with the global generator. Since these APIs do not support custom RNGs, the modification is isolated and restored immediately.
Examples
Using an internal seed:
Creates new custom NumPy and Torch RNGs based on the given seed.
Providing external RNGs:
NePS uses the user-supplied RNG objects directly. In this case, the seed argument is ignored for NumPy and Torch.