Skip to content

Add float precision tests for storages#6040

Merged
nabenabe0928 merged 12 commits intooptuna:masterfrom
not522:test-precision
Jun 4, 2025
Merged

Add float precision tests for storages#6040
nabenabe0928 merged 12 commits intooptuna:masterfrom
not522:test-precision

Conversation

@not522
Copy link
Copy Markdown
Member

@not522 not522 commented Apr 11, 2025

Motivation

To prevent float precision loss on the storage layer, this PR adds tests for float values.
Related to: #3227 #3327

Description of the changes

Adding precision tests on test_storages.py and test_with_server.py.

Overview of the implementation for the float precision

In the storage layer, the following methods must keep precision of values.

  • set_trial_state_values
  • set_trial_intermediate_value
  • set_trial_param
  • set_study_user_attr
  • set_study_system_attr
  • set_trial_user_attr
  • set_trial_system_attr

Note: Outside of the storage layer, to_internal_repr / to_external_repr of distributions must also keep precision. These are tested in test_distributions.py.

These methods handle the following floats:

  • set_trial_state_values
    • finite values / inf / -inf
  • set_trial_intermediate_value
    • finite values / inf / -inf / nan
  • set_trial_param
    • finite values (for FloatDistribution)
    • finite values / inf / -inf / nan (for CategoricalDistribution)
  • set_study_user_attr / set_study_system_attr / set_trial_user_attr / set_trial_system_attr
    • finite values / inf / -inf / nan

Storage classes

Note: We assume that there is no precision issues in the encoded JSON string https://docs.python.org/3.13/library/json.html#implementation-limitations.

InMemoryStorage

All values ​​are stored as Python variables so there are no precision issues.

JournalStorage

It dumps all the values ​​into JSON strings and saves them.

RDBStorage

All sqlalchemy.Float specify double-precision.

Note: MySQL cannot handle infinities.

  • set_trial_state_values
    • Values are saved as sqlalchemy.Float and TrialValueType.
  • set_trial_intermediate_value
    • Values are saved as sqlalchemy.Float and TrialIntermediateValueType.
  • set_trial_param
    • FloatDistribution
      • Parameters are saved as sqlalchemy.Float.
      • Distributions are saved as encoded JSON strings.
    • CategoricalDistribution
      • Indexes of parameters are saved as sqlalchemy.Float instead of values of them.
      • Distributions are saved as encoded JSON strings.
  • set_study_user_attr / set_study_system_attr / set_trial_user_attr / set_trial_system_attr
    • It dumps all the values ​​into JSON strings and saves them.

GrpcStorageProxy

The precision of values are specified in api.proto.

  • set_trial_state_values
    • Saved as double.
  • set_trial_intermediate_value
    • Saved as double.
  • set_trial_param
    • FloatDistribution
      • Saved as double.
    • CategoricalDistribution
      • Indexes are saved as doubles instead of values.
  • set_study_user_attr / set_study_system_attr / set_trial_user_attr / set_trial_system_attr
    • Saved as JSON strings.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test Unit test.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants