Skip to content

Fix request.values in OptunaStorageProxyService#6044

Merged
HideakiImamura merged 22 commits intooptuna:masterfrom
hitsgub:fix-grpc-values
May 1, 2025
Merged

Fix request.values in OptunaStorageProxyService#6044
HideakiImamura merged 22 commits intooptuna:masterfrom
hitsgub:fix-grpc-values

Conversation

@hitsgub
Copy link
Copy Markdown
Contributor

@hitsgub hitsgub commented Apr 14, 2025

Motivation

Recording None through a GrpcStorageProxy results in [] being recorded in backend DB.
Loading [] from the DB through GrpcStorageProxy results in None being loaded.
Saving and loading without GrpcStorageProxy does not change.
fix this so the value never changes

Description of the changes

First, add test to check to fail.
Next, fix bug.

@c-bata
Copy link
Copy Markdown
Member

c-bata commented Apr 15, 2025

Let me mark this PR as a draft. Please mark as ready for review after CI passes.

@c-bata c-bata marked this pull request as draft April 15, 2025 04:13
@c-bata c-bata added the bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself. label Apr 15, 2025
@hitsgub
Copy link
Copy Markdown
Contributor Author

hitsgub commented Apr 15, 2025

Added tests for cases where set and get behave differently depending on whether a gRPC proxy is present.
The current implementation fails as expected.

=========================== short test summary info ============================
FAILED tests/storages_tests/test_storages.py::test_set_and_get_trial_state_values_through_grpc_proxy[None-inmemory] - assert [] == None
 +  where [] = FrozenTrial(number=1, state=TrialState.COMPLETE, values=[], datetime_start=datetime.datetime(2025, 4, 15, 9, 0, 45, 19..., 201376), params={}, user_attrs={}, system_attrs={}, intermediate_values={}, distributions={}, trial_id=1, value=None).values
 +    where FrozenTrial(number=1, state=TrialState.COMPLETE, values=[], datetime_start=datetime.datetime(2025, 4, 15, 9, 0, 45, 19..., 201376), params={}, user_attrs={}, system_attrs={}, intermediate_values={}, distributions={}, trial_id=1, value=None) = get_trial(1)
 +      where get_trial = <optuna.storages._in_memory.InMemoryStorage object at 0x7f71c7cb4b80>.get_trial
FAILED tests/storages_tests/test_storages.py::test_set_and_get_trial_state_values_through_grpc_proxy[None-journal] - assert [] == None
 +  where [] = FrozenTrial(number=1, state=TrialState.COMPLETE, values=[], datetime_start=datetime.datetime(2025, 4, 15, 9, 0, 46, 71..., 714433), params={}, user_attrs={}, system_attrs={}, intermediate_values={}, distributions={}, trial_id=1, value=None).values
 +    where FrozenTrial(number=1, state=TrialState.COMPLETE, values=[], datetime_start=datetime.datetime(2025, 4, 15, 9, 0, 46, 71..., 714433), params={}, user_attrs={}, system_attrs={}, intermediate_values={}, distributions={}, trial_id=1, value=None) = get_trial(1)
 +      where get_trial = <optuna.storages.journal._storage.JournalStorage object at 0x7f718f2c4880>.get_trial
FAILED tests/storages_tests/test_storages.py::test_set_and_get_trial_state_values_through_grpc_proxy[None-journal_redis] - assert [] == None
 +  where [] = FrozenTrial(number=1, state=TrialState.COMPLETE, values=[], datetime_start=datetime.datetime(2025, 4, 15, 9, 0, 46, 95..., 960024), params={}, user_attrs={}, system_attrs={}, intermediate_values={}, distributions={}, trial_id=1, value=None).values
 +    where FrozenTrial(number=1, state=TrialState.COMPLETE, values=[], datetime_start=datetime.datetime(2025, 4, 15, 9, 0, 46, 95..., 960024), params={}, user_attrs={}, system_attrs={}, intermediate_values={}, distributions={}, trial_id=1, value=None) = get_trial(1)
 +      where get_trial = <optuna.storages.journal._storage.JournalStorage object at 0x7f718f2ca370>.get_trial
= 3 failed, 4821 passed, 121 skipped, 36 deselected, 13 warnings in 744.66s (0:12:24) =
Error: Process completed with exit code 1.

@hitsgub hitsgub marked this pull request as ready for review April 15, 2025 09:52
@c-bata
Copy link
Copy Markdown
Member

c-bata commented Apr 16, 2025

@not522 @sawa3030 Could you review this PR?

Copy link
Copy Markdown
Member

@not522 not522 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your PR!
It seems to be many changes for tests. Could you narrow down the changes to those related to bug fixes?


@pytest.mark.parametrize("storage_mode_direct", STORAGE_MODES_DIRECT)
@pytest.mark.parametrize("values", [None, [0.0]])
def test_set_and_get_trial_state_values_through_grpc_proxy(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this test should be in tests/storages_tests/test_grpc.py.

@hitsgub
Copy link
Copy Markdown
Contributor Author

hitsgub commented Apr 16, 2025

ok

tests/storages_tests/test_cached_storage.py .....                        [ 46%]
tests/storages_tests/test_callbacks.py ...                               [ 46%]
tests/storages_tests/test_grpc.py F..FF.........                         [ 46%]
tests/storages_tests/test_heartbeat.py ..........................        [ 47%]
tests/storages_tests/test_storages.py .................................. [ 47%]
........................................................................ [ 49%]
........................................................................ [ 50%]
..........ss.....ss..................................................... [ 52%]
........................................................................ [ 53%]
..............s.........                                                 [ 54%]

hitsgub and others added 2 commits April 22, 2025 15:32
Co-authored-by: Eri Sawada <125344906+sawa3030@users.noreply.github.com>
@hitsgub
Copy link
Copy Markdown
Contributor Author

hitsgub commented Apr 22, 2025

actions workflows have not been running...
I will close and reopen to try to run workflows

@hitsgub hitsgub closed this Apr 22, 2025
@hitsgub hitsgub reopened this Apr 22, 2025
@hitsgub hitsgub requested a review from not522 April 22, 2025 07:37
Copy link
Copy Markdown
Member

@not522 not522 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@not522 not522 removed their assignment Apr 23, 2025
@c-bata c-bata added this to the v4.4.0 milestone Apr 24, 2025
Copy link
Copy Markdown
Member

@c-bata c-bata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your pull request. Let me leave some early feedback comments.

) -> None:
self.storage_specifier = storage_specifier
if base_storage is not None:
assert storage_specifier == "grpc_proxy"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My initial impression is that it could potentially be a bit confusing that storage_specifiers accepts "grpc_proxy", even though "grpc_rdb" and "grpc_journal_file" still exist. I have no idea how to mitigate this right now. Therefore, I'll comment later with a suggested solution.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been thinking about it, but I could not come up with a simple solution.

It seems that larger changes might be needed, which should not be addressed in this PR. Considering that this bug fix should be included in the next minor release, I will merge this PR first and then considering whether to submit a follow-up PR.

I apologize for the delayed review. I will proceed with merging this.

) -> None:
self.storage_specifier = storage_specifier
if base_storage is not None:
assert storage_specifier == "grpc_proxy"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been thinking about it, but I could not come up with a simple solution.

It seems that larger changes might be needed, which should not be addressed in this PR. Considering that this bug fix should be included in the next minor release, I will merge this PR first and then considering whether to submit a follow-up PR.

I apologize for the delayed review. I will proceed with merging this.

@c-bata
Copy link
Copy Markdown
Member

c-bata commented Apr 25, 2025

@sawa3030 please let me know once you have completed your review.

@sawa3030
Copy link
Copy Markdown
Collaborator

Apologies for the delay. I am considering making only minimal changes to StorageSupplier. How about putting base_storage to kwargs?

class StorageSupplier:
    def __init__(
        self, storage_specifier: str, **kwargs: Any
    ) -> None:
        self.storage_specifier = storage_specifier
        self.extra_args = kwargs
        self.tempfile: IO[Any] | None = None
        self.server: grpc.Server | None = None
        self.thread: threading.Thread | None = None
        self.proxy: GrpcStorageProxy | None = None

    def __enter__(
        self,
    ) -> (
        optuna.storages.InMemoryStorage
        | optuna.storages._CachedStorage
        | optuna.storages.RDBStorage
        | optuna.storages.JournalStorage
        | optuna.storages.GrpcStorageProxy
    ):
        if self.storage_specifier == "grpc_proxy":
            base_storage = self.extra_args.get("base_storage")
            assert isinstance(base_storage, BaseStorage)
            return self._create_proxy(base_storage)
        elif self.storage_specifier == "inmemory":

Copy link
Copy Markdown
Collaborator

@sawa3030 sawa3030 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@HideakiImamura HideakiImamura merged commit 3fd8e6f into optuna:master May 1, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants