Skip to content

Commit

Permalink
Fix dtype in continuous comp rep (#415)
Browse files Browse the repository at this point in the history
This PR fixes the DType used when calculating the computational
representation in a continuous space.

Previously, the dtype did not using any of our flags but infers it from
the provided data, always yielding a `float64` representation. This
causes troubles down the line, specifically for continuous constraints:
These then turn out to have bounds in `float64` while other parts are in
`float32`, making them unusable.
  • Loading branch information
AVHopp authored Nov 20, 2024
2 parents e013669 + fdc55e5 commit b402c74
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Rare bug arising from degenerate `SubstanceParameter.comp_df` rows that caused
wrong number of recommendations being returned
- `ContinuousConstraint`s can now be used in single point precision

### Deprecations
- Passing a dataframe via the `data` argument to `Objective.transform` is no longer
Expand Down
2 changes: 2 additions & 0 deletions baybe/searchspace/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from baybe.serialization import SerialMixin, converter, select_constructor_hook
from baybe.utils.basic import to_tuple
from baybe.utils.dataframe import get_transform_objects, pretty_print_df
from baybe.utils.numerical import DTypeFloatNumpy
from baybe.utils.plotting import to_string

if TYPE_CHECKING:
Expand Down Expand Up @@ -280,6 +281,7 @@ def comp_rep_bounds(self) -> pd.DataFrame:
return pd.DataFrame(
{p.name: p.bounds.to_tuple() for p in self.parameters},
index=["min", "max"],
dtype=DTypeFloatNumpy,
)

def _drop_parameters(self, parameter_names: Collection[str]) -> SubspaceContinuous:
Expand Down
13 changes: 7 additions & 6 deletions docs/userguide/envvars.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ BAYBE_CACHE_DIR=""
```
you can turn off disk caching entirely.

## Floating Point Precision
## EXPERIMENTAL: Floating Point Precision
In general, double precision is recommended because numerical stability during optimization
can be bad when single precision is used. This impacts gradient-based optimization,
i.e. search spaces with continuous parameters, more than optimization without gradients.
Expand All @@ -113,10 +113,11 @@ If you still want to use single precision, you can set the following Boolean var
- `BAYBE_NUMPY_USE_SINGLE_PRECISION` (defaults to `False`)
- `BAYBE_TORCH_USE_SINGLE_PRECISION` (defaults to `False`)

```{admonition} Continuous Constraints in Single Precision
```{admonition} Experimental feature only!
:class: warning
Currently, due to explicit casting in BoTorch,
[`ContinuousConstraint`](baybe.constraints.base.ContinuousConstraint)s do not support
single precision and cannot be used if the corresponding environment variables are
activated.
Currently, it cannot be guaranteed that all calculations will be performed in single precision,
even when setting the aforementioned variables. The reason is that there are several code snippets
within `BoTorch` that transform single precision variables to double precision variables.
Consequently, this feature is currently only available as an *experimental* feature.
We are however actively working on fully enabling single precision.
```

0 comments on commit b402c74

Please sign in to comment.