Repository navigation
Add float precision tests for storages - #6040
Conversation
|
@nabenabe0928 @sawa3030 @kAIto47802 Could you review this PR? |
|
I looked into
These coincide with the PR description. Plus, I confirmed that the float values each method can receive are also correct in the PR description. |
There was a problem hiding this comment.
I will check each of them once I confirm their tests:
-
set_study_user_attr -
set_study_system_attr -
set_trial_param -
set_trial_state_values -
set_trial_intermediate_value -
set_trial_user_attr -
set_trial_system_attr
nabenabe0928
left a comment
There was a problem hiding this comment.
tests/storages_tests/test_storages.py looks good to me:)
There was a problem hiding this comment.
I will check each of them once I confirm their tests:
-
set_study_user_attr -
set_study_system_attr -
set_trial_param -
set_trial_state_values -
set_trial_intermediate_value -
set_trial_user_attr -
set_trial_system_attr
There was a problem hiding this comment.
Ok, I found out that this file requires exactly the same changes as test_storages.py.
Co-authored-by: Shuhei Watanabe <[email protected]>
|
Thank you. I updated this PR as you suggested. PTAL. |
| # Test setting value. | ||
| for key, value in FLOAT_ATTRS.items(): | ||
| storage.set_study_user_attr(study_id, key, value) | ||
| assert is_equal_floats(storage.get_study_user_attrs(study_id)[key], value) |
There was a problem hiding this comment.
I thought this assertion is also ensured in the following line. Would you mind explaining why this additional check is necessary?
There was a problem hiding this comment.
This is just a copy from another test. I simplified it.
There was a problem hiding this comment.
Sorry for bringing this up again, but after reconsideration, I’m wondering if we really need is_equal_float_dicts. I believe we could replace is_equal_float_dicts with is_equal_floats. This would help keep the test pattern consistent throughout the PR and make it easier to follow.
| for key, value in FLOAT_ATTRS.items(): | ||
| if math.isnan(value): # NOTE: `suggest_float` does not generate `nan`. | ||
| continue | ||
| param_name = "float_" + key | ||
| float_dist = FloatDistribution(low=value, high=value) | ||
| internal_repr = float_dist.to_internal_repr(value) | ||
| storage.set_trial_param(trial_id, param_name, internal_repr, float_dist) | ||
| assert is_equal_floats(storage.get_trial_param(trial_id, param_name), internal_repr) | ||
| assert storage.get_trial(trial_id).distributions[param_name] == float_dist | ||
|
|
||
| for key, value in FLOAT_ATTRS.items(): | ||
| param_name = "categorical_" + key | ||
| categorical_dist = CategoricalDistribution(choices=(value,)) | ||
| internal_repr = categorical_dist.to_internal_repr(value) | ||
| storage.set_trial_param(trial_id, param_name, internal_repr, categorical_dist) | ||
| assert is_equal_floats(storage.get_trial_param(trial_id, param_name), internal_repr) | ||
| assert storage.get_trial(trial_id).distributions[param_name] == categorical_dist |
There was a problem hiding this comment.
How about parameterizing the distribution type to simplify the code?
Co-authored-by: Eri Sawada <[email protected]>
| for key, value in a.items(): | ||
| if not is_equal_floats(value, b[key]): | ||
| False | ||
| return True |
There was a problem hiding this comment.
| for key, value in a.items(): | |
| if not is_equal_floats(value, b[key]): | |
| False | |
| return True | |
| return all(is_equal_floats(value, b[key]) for key, value in a.items()) |
There was a problem hiding this comment.
Once this modification is applied, this PR looks good to me!
sawa3030
left a comment
There was a problem hiding this comment.
Thank you for all the updates. LGTM!
|
This pull request has not seen any recent activity. |
kAIto47802
left a comment
There was a problem hiding this comment.
Thank you for your PR!
I've reviewed test_storages.py, leaving some comments. PTAL ![]()
| # NOTE: suggest_float does not generate NaN. | ||
| if isinstance(distribution, FloatDistribution) and math.isnan(value): |
There was a problem hiding this comment.
As you also mentioned in your PR comments:
- set_trial_param
- finite values (for FloatDistribution)
set_trial_param does not handle ±inf either.
Also, the usage of "NaN" vs. nan is inconsistent (cf. using nan in test_set_trial_state_values_for_floats).
| # NOTE: suggest_float does not generate NaN. | |
| if isinstance(distribution, FloatDistribution) and math.isnan(value): | |
| # NOTE: `suggest_float` does not generate `nan` or `±inf`. | |
| if isinstance(distribution, FloatDistribution) and not math.isfinite(value): |
There was a problem hiding this comment.
On second thought, it seemed inappropriate to assume that suggest_float does not handle infinities. For example, the following sampler can handle infinities. What do you think?
import struct
import numpy as np
import optuna
class Sampler(optuna.samplers.BaseSampler):
def infer_relative_search_space(self, study, trial):
return {}
def sample_relative(self, study, trial, search_space):
return {}
def float_to_int(self, value):
return struct.unpack("q", struct.pack("d", value))[0]
def int_to_float(self, value):
return struct.unpack("d", struct.pack("q", value))[0]
def sample_independent(self, study, trial, param_name, param_distribution):
low = self.float_to_int(param_distribution.low)
high = self.float_to_int(param_distribution.high)
return self.int_to_float(np.random.randint(low, high + 1))
def objective(trial):
return trial.suggest_float("x", 0, float("inf"))
study = optuna.create_study(sampler=Sampler())
study.optimize(objective, n_trials=10)There was a problem hiding this comment.
It may not be a specification that needs testing. In this PR, I changed to test only finite values.
There was a problem hiding this comment.
I see. Thank you for the reply!
kAIto47802
left a comment
There was a problem hiding this comment.
I've also reviewed test_with_server.py, leaving some comments. PTAL ![]()
Note
All of my comments in test_with_server.py are the same as those in test_storages.py.
| float_distribution = FloatDistribution(low=value, high=value) | ||
| categorical_distribution = CategoricalDistribution(choices=(value,)) | ||
| for distribution in (float_distribution, categorical_distribution): | ||
| # NOTE: suggest_float does not generate NaN, and MySQL cannot handle infinities. |
There was a problem hiding this comment.
Same as I mentioned in test_storages.py.
| # NOTE: suggest_float does not generate NaN, and MySQL cannot handle infinities. | |
| # NOTE: `suggest_float` does not generate `nan` or `±inf`, and MySQL cannot handle `±inf`. |
Co-authored-by: kAIto47802 <[email protected]>
|
I think using a fixture like this can significantly reduce the duplicate management of the float tests in @pytest.fixture(params=STORAGE_MODES + [None])
def storage_mode(request: pytest.FixtureRequest) -> Generator[BaseStorage, None, None]:
if request.param is None:
yield get_storage()
else:
with StorageSupplier(request.param) as storage:
yield storageHowever, this approach may not fit well with the current way the tests are split across files. We would likely need to either do all the float tests in |
|
This pull request has not seen any recent activity. |
|
I unified implementations in |
|
This pull request has not seen any recent activity. |
kAIto47802
left a comment
There was a problem hiding this comment.
Thank you for your update! LGTM
Co-authored-by: kAIto47802 <[email protected]>
"infinite" is not the same as "infinity", and it generally includes NaN. So, how about just say "infinite values"? |
I am unsure if |
|
It's a typical convention, for example, IEEE 754 or C's |
|
@not522 |
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.pyandtest_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_valuesset_trial_intermediate_valueset_trial_paramset_study_user_attrset_study_system_attrset_trial_user_attrset_trial_system_attrNote: Outside of the storage layer,
to_internal_repr/to_external_reprof distributions must also keep precision. These are tested intest_distributions.py.These methods handle the following floats:
set_trial_state_valuesset_trial_intermediate_valueset_trial_paramFloatDistribution)CategoricalDistribution)set_study_user_attr/set_study_system_attr/set_trial_user_attr/set_trial_system_attrStorage 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.
InMemoryStorageAll values are stored as Python variables so there are no precision issues.
JournalStorageIt dumps all the values into JSON strings and saves them.
RDBStorageAll
sqlalchemy.Floatspecify double-precision.Note: MySQL cannot handle infinities.
set_trial_state_valuessqlalchemy.FloatandTrialValueType.set_trial_intermediate_valuesqlalchemy.FloatandTrialIntermediateValueType.set_trial_paramFloatDistributionsqlalchemy.Float.CategoricalDistributionsqlalchemy.Floatinstead of values of them.set_study_user_attr/set_study_system_attr/set_trial_user_attr/set_trial_system_attrGrpcStorageProxyThe precision of values are specified in
api.proto.set_trial_state_valuesset_trial_intermediate_valueset_trial_paramFloatDistributionCategoricalDistributionset_study_user_attr/set_study_system_attr/set_trial_user_attr/set_trial_system_attr