Skip to content

Use _LazyImport for gRPC#5915

Closed
nabenabe0928 wants to merge 3 commits intooptuna:masterfrom
nabenabe0928:use-lazy-import-for-grpc
Closed

Use _LazyImport for gRPC#5915
nabenabe0928 wants to merge 3 commits intooptuna:masterfrom
nabenabe0928:use-lazy-import-for-grpc

Conversation

@nabenabe0928
Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 commented Jan 7, 2025

Motivation

As gRPC did not use lazy import, which potentially reduces the import time, I replaced try_import with _LazyImport.
This PR is a followup of:

Description of the changes

  • Replace try_import with _LazyImport

DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S.%f"


class OptunaStorageProxyService(StorageServiceServicer):
Copy link
Copy Markdown
Contributor Author

@nabenabe0928 nabenabe0928 Jan 7, 2025

Choose a reason for hiding this comment

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

As Git messed up the difference between before and after, I will concisely write down the overview.

Before:

from optuna.storages._grpc.grpc_imports import _imports

if _imports.is_successful():
    from optuna.storages._grpc.grpc_imports import StorageServiceServicer
else:
    class StorageServiceServicer:  # type: ignore
        pass


class OptunaStorageProxyService(StorageServiceServicer):
    ...

def make_server(
    storage: BaseStorage, host: str, port: int, thread_pool: ThreadPoolExecutor | None = None
) -> grpc.Server:
    server = grpc.server(thread_pool or ThreadPoolExecutor(max_workers=10))
    api_pb2_grpc.add_StorageServiceServicer_to_server(
        OptunaStorageProxyService(storage), server
    )  # type: ignore
    server.add_insecure_port(f"{host}:{port}")
    return server

After:

api_pb2_grpc = _LazyImport("optuna.storages._grpc.auto_generated.api_pb2_grpc")


def make_server(
    storage: BaseStorage, host: str, port: int, thread_pool: ThreadPoolExecutor | None = None
) -> grpc.Server:
    class OptunaStorageProxyService(api_pb2_grpc.StorageServiceServicer):
        ...

    server = grpc.server(thread_pool or ThreadPoolExecutor(max_workers=10))
    api_pb2_grpc.add_StorageServiceServicer_to_server(
        OptunaStorageProxyService(storage), server
    )  # type: ignore
    server.add_insecure_port(f"{host}:{port}")
    return server

@HideakiImamura
Copy link
Copy Markdown
Member

@kAIto47802 Could you review this PR?

@nabenabe0928
Copy link
Copy Markdown
Contributor Author

I will close this PR because the import time difference is not super huge (-6.8%):

This PR Master
0.1123 $\pm$ 0.007 [s] 0.1200 $\pm$ 0.005 [s]

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants