-
Notifications
You must be signed in to change notification settings - Fork 838
PERF: Lazily load all package exports #2456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Richard Lundeen (rlundeen2)
wants to merge
8
commits into
microsoft:main
Choose a base branch
from
rlundeen2:rlundeen2-all-package-lazy-imports
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ad6d9d3
PERF: Lazily load model package exports
rlundeen2 ba973cc
TEST: Cover lazy model exports in process
rlundeen2 ed6a298
PERF: Lazily load package exports
rlundeen2 e5e83a5
Merge origin/main into lazy package exports
rlundeen2 8fc5904
DOC: Clarify lazy export maintenance
rlundeen2 0316027
DOCS: Preserve version import-cycle rationale
rlundeen2 c874fdb
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
rlundeen2 f6be6b7
Merge remote-tracking branch 'origin/main' into rlundeen2-all-package…
rlundeen2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,60 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| __name__ = "pyrit" | ||
| # Remove dev suffix when releasing and keep in sync with pyproject.toml | ||
| # NOTE: __version__ must be set before imports below to avoid circular import issues. | ||
| # Submodules (e.g., component_identifier, memory_models) reference pyrit.__version__ | ||
| # and get imported transitively during the .common import chain. | ||
| __version__ = "1.1.0.dev0" | ||
|
|
||
| from .common import turn_off_transformers_warning # noqa: F401 | ||
| from .show_versions import show_versions # noqa: F401 | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
| # ruff: noqa: F401 | ||
|
|
||
| """PyRIT public package API.""" | ||
|
|
||
| import os | ||
| import sys | ||
| from types import ModuleType | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from pyrit.common.lazy_imports import get_lazy_dir, resolve_lazy_export | ||
|
|
||
| if TYPE_CHECKING: | ||
| import pyrit.common.turn_off_transformers_warning as turn_off_transformers_warning | ||
| from pyrit._version import __version__ | ||
| from pyrit.show_versions import show_versions | ||
|
|
||
| # Most people install PyRIT without torch, so suppress the transformers advisory | ||
| # before any PyRIT submodule can import transformers. | ||
| os.environ["TRANSFORMERS_NO_ADVISORY_WARNINGS"] = "True" | ||
|
|
||
| _LAZY_EXPORTS: dict[str, str | tuple[str, str | None]] = { | ||
| "__version__": "pyrit._version", | ||
| "show_versions": "pyrit.show_versions", | ||
| "turn_off_transformers_warning": ("pyrit.common.turn_off_transformers_warning", None), | ||
| } | ||
|
|
||
| __all__ = list(_LAZY_EXPORTS) | ||
|
|
||
|
|
||
| class _LazyPyRITModule(ModuleType): | ||
| """Resolve exports that share a name with an imported child module.""" | ||
|
|
||
| def __getattribute__(self, name: str) -> object: | ||
| if name == "show_versions": | ||
| module_globals = ModuleType.__getattribute__(self, "__dict__") | ||
| return resolve_lazy_export( | ||
| name=name, | ||
| module_name=__name__, | ||
| module_globals=module_globals, | ||
| exports=_LAZY_EXPORTS, | ||
| ) | ||
| return ModuleType.__getattribute__(self, name) | ||
|
|
||
|
|
||
| sys.modules[__name__].__class__ = _LazyPyRITModule | ||
|
|
||
|
|
||
| def __getattr__(name: str) -> object: | ||
| return resolve_lazy_export( | ||
| name=name, | ||
| module_name=__name__, | ||
| module_globals=globals(), | ||
| exports=_LAZY_EXPORTS, | ||
| ) | ||
|
|
||
|
|
||
| def __dir__() -> list[str]: | ||
| return get_lazy_dir(module_globals=globals(), exports=_LAZY_EXPORTS) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| """PyRIT package version.""" | ||
|
|
||
| # Keep this module dependency-free to avoid circular imports. Submodules such | ||
| # as component identifiers and memory models reference ``pyrit.__version__`` | ||
| # and can be imported transitively while the package is still initializing. | ||
| # Remove the development suffix when releasing and keep this value in sync with pyproject.toml. | ||
| __version__ = "1.1.0.dev0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,39 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
|
rlundeen2 marked this conversation as resolved.
|
||
| # Licensed under the MIT license. | ||
| # ruff: noqa: F401 | ||
|
|
||
| """Analytics module for PyRIT conversation and result analysis.""" | ||
|
|
||
| from pyrit.analytics.conversation_analytics import ConversationAnalytics | ||
| from pyrit.analytics.result_analysis import ( | ||
| AttackStats, | ||
| analyze_results, | ||
| get_cached_results_for_technique, | ||
| ) | ||
| from pyrit.analytics.text_matching import ( | ||
| ApproximateTextMatching, | ||
| ExactTextMatching, | ||
| TextMatching, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "analyze_results", | ||
| "ApproximateTextMatching", | ||
| "AttackStats", | ||
| "ConversationAnalytics", | ||
| "ExactTextMatching", | ||
| "get_cached_results_for_technique", | ||
| "TextMatching", | ||
| ] | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from pyrit.common.lazy_imports import get_lazy_dir, resolve_lazy_export | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pyrit.analytics.conversation_analytics import ConversationAnalytics | ||
| from pyrit.analytics.result_analysis import AttackStats, analyze_results, get_cached_results_for_technique | ||
| from pyrit.analytics.text_matching import ApproximateTextMatching, ExactTextMatching, TextMatching | ||
|
|
||
| _LAZY_EXPORTS: dict[str, str | tuple[str, str | None]] = { | ||
| "analyze_results": "pyrit.analytics.result_analysis", | ||
| "ApproximateTextMatching": "pyrit.analytics.text_matching", | ||
| "AttackStats": "pyrit.analytics.result_analysis", | ||
| "ConversationAnalytics": "pyrit.analytics.conversation_analytics", | ||
| "ExactTextMatching": "pyrit.analytics.text_matching", | ||
| "get_cached_results_for_technique": "pyrit.analytics.result_analysis", | ||
| "TextMatching": "pyrit.analytics.text_matching", | ||
| } | ||
|
|
||
| __all__ = list(_LAZY_EXPORTS) | ||
|
|
||
|
|
||
| def __getattr__(name: str) -> object: | ||
| return resolve_lazy_export( | ||
| name=name, | ||
| module_name=__name__, | ||
| module_globals=globals(), | ||
| exports=_LAZY_EXPORTS, | ||
| ) | ||
|
|
||
|
|
||
| def __dir__() -> list[str]: | ||
| return get_lazy_dir(module_globals=globals(), exports=_LAZY_EXPORTS) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,63 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| """ | ||
| Authentication functionality for a variety of services. | ||
| """ | ||
|
|
||
| from pyrit.auth.authenticator import Authenticator | ||
| from pyrit.auth.azure_auth import ( | ||
| AsyncTokenProviderCredential, | ||
| AzureAuth, | ||
| TokenProviderCredential, | ||
| ensure_async_token_provider, | ||
| get_azure_async_token_provider, | ||
| get_azure_openai_auth, | ||
| get_azure_token_provider, | ||
| get_default_azure_scope, | ||
| is_azure_ml_endpoint, | ||
| is_azure_openai_endpoint, | ||
| ) | ||
| from pyrit.auth.azure_storage_auth import AzureStorageAuth | ||
| from pyrit.auth.copilot_authenticator import CopilotAuthenticator | ||
| from pyrit.auth.manual_copilot_authenticator import ManualCopilotAuthenticator | ||
| from pyrit.auth.openai_auth import resolve_openai_auth | ||
|
|
||
| __all__ = [ | ||
| "AsyncTokenProviderCredential", | ||
| "Authenticator", | ||
| "AzureAuth", | ||
| "AzureStorageAuth", | ||
| "CopilotAuthenticator", | ||
| "ManualCopilotAuthenticator", | ||
| "resolve_openai_auth", | ||
| "TokenProviderCredential", | ||
| "ensure_async_token_provider", | ||
| "get_azure_token_provider", | ||
| "get_azure_async_token_provider", | ||
| "get_default_azure_scope", | ||
| "get_azure_openai_auth", | ||
| "is_azure_ml_endpoint", | ||
| "is_azure_openai_endpoint", | ||
| ] | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
| # ruff: noqa: F401 | ||
|
|
||
| """ | ||
| Authentication functionality for a variety of services. | ||
| """ | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from pyrit.common.lazy_imports import get_lazy_dir, resolve_lazy_export | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pyrit.auth.authenticator import Authenticator | ||
| from pyrit.auth.azure_auth import ( | ||
| AsyncTokenProviderCredential, | ||
| AzureAuth, | ||
| TokenProviderCredential, | ||
| ensure_async_token_provider, | ||
| get_azure_async_token_provider, | ||
| get_azure_openai_auth, | ||
| get_azure_token_provider, | ||
| get_default_azure_scope, | ||
| is_azure_ml_endpoint, | ||
| is_azure_openai_endpoint, | ||
| ) | ||
| from pyrit.auth.azure_storage_auth import AzureStorageAuth | ||
| from pyrit.auth.copilot_authenticator import CopilotAuthenticator | ||
| from pyrit.auth.manual_copilot_authenticator import ManualCopilotAuthenticator | ||
| from pyrit.auth.openai_auth import resolve_openai_auth | ||
|
|
||
| _LAZY_EXPORTS: dict[str, str | tuple[str, str | None]] = { | ||
| "AsyncTokenProviderCredential": "pyrit.auth.azure_auth", | ||
| "Authenticator": "pyrit.auth.authenticator", | ||
| "AzureAuth": "pyrit.auth.azure_auth", | ||
| "AzureStorageAuth": "pyrit.auth.azure_storage_auth", | ||
| "CopilotAuthenticator": "pyrit.auth.copilot_authenticator", | ||
| "ManualCopilotAuthenticator": "pyrit.auth.manual_copilot_authenticator", | ||
| "resolve_openai_auth": "pyrit.auth.openai_auth", | ||
| "TokenProviderCredential": "pyrit.auth.azure_auth", | ||
| "ensure_async_token_provider": "pyrit.auth.azure_auth", | ||
| "get_azure_token_provider": "pyrit.auth.azure_auth", | ||
| "get_azure_async_token_provider": "pyrit.auth.azure_auth", | ||
| "get_default_azure_scope": "pyrit.auth.azure_auth", | ||
| "get_azure_openai_auth": "pyrit.auth.azure_auth", | ||
| "is_azure_ml_endpoint": "pyrit.auth.azure_auth", | ||
| "is_azure_openai_endpoint": "pyrit.auth.azure_auth", | ||
| } | ||
|
|
||
| __all__ = list(_LAZY_EXPORTS) | ||
|
|
||
|
|
||
| def __getattr__(name: str) -> object: | ||
| return resolve_lazy_export( | ||
| name=name, | ||
| module_name=__name__, | ||
| module_globals=globals(), | ||
| exports=_LAZY_EXPORTS, | ||
| ) | ||
|
|
||
|
|
||
| def __dir__() -> list[str]: | ||
| return get_lazy_dir(module_globals=globals(), exports=_LAZY_EXPORTS) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,35 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
| # ruff: noqa: F401 | ||
|
|
||
| """Middleware module for backend.""" | ||
|
|
||
| from pyrit.backend.middleware.error_handlers import register_error_handlers | ||
| from pyrit.backend.middleware.request_id import RequestIdMiddleware | ||
| from pyrit.backend.middleware.security_headers import SecurityHeadersMiddleware | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| __all__ = ["register_error_handlers", "RequestIdMiddleware", "SecurityHeadersMiddleware"] | ||
| from pyrit.common.lazy_imports import get_lazy_dir, resolve_lazy_export | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pyrit.backend.middleware.error_handlers import register_error_handlers | ||
| from pyrit.backend.middleware.request_id import RequestIdMiddleware | ||
| from pyrit.backend.middleware.security_headers import SecurityHeadersMiddleware | ||
|
|
||
| _LAZY_EXPORTS: dict[str, str | tuple[str, str | None]] = { | ||
| "register_error_handlers": "pyrit.backend.middleware.error_handlers", | ||
| "RequestIdMiddleware": "pyrit.backend.middleware.request_id", | ||
| "SecurityHeadersMiddleware": "pyrit.backend.middleware.security_headers", | ||
| } | ||
|
|
||
| __all__ = list(_LAZY_EXPORTS) | ||
|
|
||
|
|
||
| def __getattr__(name: str) -> object: | ||
| return resolve_lazy_export( | ||
| name=name, | ||
| module_name=__name__, | ||
| module_globals=globals(), | ||
| exports=_LAZY_EXPORTS, | ||
| ) | ||
|
|
||
|
|
||
| def __dir__() -> list[str]: | ||
| return get_lazy_dir(module_globals=globals(), exports=_LAZY_EXPORTS) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.