-
Notifications
You must be signed in to change notification settings - Fork 244
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
Dependency injection for model deployments #1787
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 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,13 +1,14 @@ | ||
import inspect | ||
import os | ||
from dataclasses import replace | ||
from typing import Any, Dict, Mapping, Optional, TYPE_CHECKING | ||
from typing import Any, Callable, Dict, Mapping, Optional, TYPE_CHECKING | ||
|
||
from retrying import RetryError, Attempt | ||
|
||
from helm.benchmark.model_deployment_registry import get_model_deployment | ||
from helm.benchmark.model_deployment_registry import ModelDeployment, get_model_deployment | ||
from helm.common.cache import CacheConfig, MongoCacheConfig, SqliteCacheConfig | ||
from helm.common.hierarchical_logger import hlog | ||
from helm.common.object_spec import create_object | ||
from helm.common.object_spec import ObjectSpec, create_object, get_class_by_name | ||
from helm.common.request import Request, RequestResult | ||
from helm.common.tokenization_request import ( | ||
TokenizationRequest, | ||
|
@@ -59,6 +60,48 @@ def _build_cache_config(self, organization: str) -> CacheConfig: | |
# TODO: Allow setting CacheConfig.follower_cache_path from a command line flag. | ||
return SqliteCacheConfig(client_cache_path) | ||
|
||
def _inject_init_args(self, spec: ObjectSpec, injectors: Dict[str, Callable[[], Any]]) -> ObjectSpec: | ||
"""Return arguments needed by the class's __init__'s parameters. | ||
|
||
This does a simple form of dependency injection. For each parameter in the class' __init__, | ||
try to find a corresponding injector and call it to produce the argument value.""" | ||
cls = get_class_by_name(spec.class_name) | ||
init_signature = inspect.signature(cls.__init__) | ||
args = {} | ||
args.update(spec.args) | ||
missing_args = [] | ||
for parameter_name in init_signature.parameters.keys(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I'm confused what injectors are supposed to do... |
||
if parameter_name == "self" or parameter_name in args: | ||
continue | ||
elif parameter_name in injectors: | ||
args[parameter_name] = injectors[parameter_name]() | ||
else: | ||
missing_args.append(parameter_name) | ||
if missing_args: | ||
raise ValueError(f"Missing arguments {missing_args} for client_spec for {spec.class_name}") | ||
return replace(spec, args=args) | ||
|
||
def _create_client_for_model_deployment(self, model_deployment: ModelDeployment): | ||
"""Create a client for the ModelDeployment.""" | ||
|
||
def get_api_key() -> str: | ||
if "deployments" not in self.credentials: | ||
raise AuthenticationError("Could not find key 'deployments' in credentials.conf") | ||
deployment_api_keys = self.credentials["deployments"] | ||
if model_deployment.name not in deployment_api_keys: | ||
raise AuthenticationError( | ||
f"Could not find key '{model_deployment.name}' under key 'deployments' in credentials.conf" | ||
) | ||
return deployment_api_keys[model_deployment.name] | ||
|
||
def get_cache_config() -> CacheConfig: | ||
organization: str = model_deployment.name.split("/")[0] | ||
return self._build_cache_config(organization) | ||
|
||
injectors = {"api_key": get_api_key, "cache_config": get_cache_config} | ||
client_spec_with_injected_args = self._inject_init_args(model_deployment.client_spec, injectors) | ||
return create_object(client_spec_with_injected_args) | ||
|
||
def _get_client(self, model: str) -> Client: | ||
"""Return a client based on the model, creating it if necessary.""" | ||
client: Optional[Client] = self.clients.get(model) | ||
|
@@ -70,19 +113,7 @@ def _get_client(self, model: str) -> Client: | |
# TODO: Migrate all clients to use model deployments | ||
model_deployment = get_model_deployment(model) | ||
if model_deployment: | ||
api_key = None | ||
if "deployments" not in self.credentials: | ||
raise AuthenticationError("Could not find key 'deployments' in credentials.conf") | ||
deployment_api_keys = self.credentials["deployments"] | ||
if model not in deployment_api_keys: | ||
raise AuthenticationError( | ||
f"Could not find key '{model}' under key 'deployments' in credentials.conf" | ||
) | ||
api_key = deployment_api_keys[model] | ||
client = create_object( | ||
model_deployment.client_spec, additional_args={"cache_config": cache_config, "api_key": api_key} | ||
) | ||
|
||
client = self._create_client_for_model_deployment(model_deployment) | ||
elif get_huggingface_model_config(model): | ||
from helm.proxy.clients.huggingface_client import HuggingFaceClient | ||
|
||
|
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Elaborate on documentation of
additional_args
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm...I think it might be cleaner / more modular to have a function that takes a spec and updates the spec with additional_args, so that the
create_object
function can stay the same.