-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[RunInference] Add content-aware dynamic batching via element_size_fn… #37428
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
Eliaaazzz
wants to merge
2
commits into
apache:master
Choose a base branch
from
Eliaaazzz:users/Eliaaazzz/37414-dynamic-batching
base: master
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.
+419
−238
Open
Changes from all commits
Commits
Show all changes
2 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 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 |
|---|---|---|
|
|
@@ -167,10 +167,48 @@ class KeyModelPathMapping(Generic[KeyT]): | |
|
|
||
| class ModelHandler(Generic[ExampleT, PredictionT, ModelT]): | ||
| """Has the ability to load and apply an ML model.""" | ||
| def __init__(self): | ||
| """Environment variables are set using a dict named 'env_vars' before | ||
| loading the model. Child classes can accept this dict as a kwarg.""" | ||
| self._env_vars = {} | ||
| def __init__( | ||
| self, | ||
| *, | ||
| min_batch_size: Optional[int] = None, | ||
| max_batch_size: Optional[int] = None, | ||
| max_batch_duration_secs: Optional[int] = None, | ||
| max_batch_weight: Optional[int] = None, | ||
| element_size_fn: Optional[Callable[[Any], int]] = None, | ||
| large_model: bool = False, | ||
| model_copies: Optional[int] = None, | ||
| **kwargs): | ||
| """Initializes the ModelHandler. | ||
|
|
||
| Args: | ||
| min_batch_size: the minimum batch size to use when batching inputs. | ||
| max_batch_size: the maximum batch size to use when batching inputs. | ||
| max_batch_duration_secs: the maximum amount of time to buffer a batch | ||
| before emitting; used in streaming contexts. | ||
| max_batch_weight: the maximum weight of a batch. Requires element_size_fn. | ||
| element_size_fn: a function that returns the size (weight) of an element. | ||
| large_model: set to true if your model is large enough to run into | ||
| memory pressure if you load multiple copies. | ||
| model_copies: The exact number of models that you would like loaded | ||
| onto your machine. | ||
| kwargs: 'env_vars' can be used to set environment variables | ||
| before loading the model. | ||
| """ | ||
| self._env_vars = kwargs.get('env_vars', {}) | ||
| self._batching_kwargs: dict[str, Any] = {} | ||
| if min_batch_size is not None: | ||
| self._batching_kwargs['min_batch_size'] = min_batch_size | ||
| if max_batch_size is not None: | ||
| self._batching_kwargs['max_batch_size'] = max_batch_size | ||
| if max_batch_duration_secs is not None: | ||
| self._batching_kwargs['max_batch_duration_secs'] = max_batch_duration_secs | ||
| if max_batch_weight is not None: | ||
| self._batching_kwargs['max_batch_weight'] = max_batch_weight | ||
| if element_size_fn is not None: | ||
| self._batching_kwargs['element_size_fn'] = element_size_fn | ||
|
Comment on lines
+198
to
+208
Contributor
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. This series of batching_params = {
'min_batch_size': min_batch_size,
'max_batch_size': max_batch_size,
'max_batch_duration_secs': max_batch_duration_secs,
'max_batch_weight': max_batch_weight,
'element_size_fn': element_size_fn,
}
self._batching_kwargs: dict[str, Any] = {
k: v for k, v in batching_params.items() if v is not None
} |
||
| self._large_model = large_model | ||
| self._model_copies = model_copies | ||
| self._share_across_processes = large_model or (model_copies is not None) | ||
|
|
||
| def load_model(self) -> ModelT: | ||
| """Loads and initializes a model for processing.""" | ||
|
|
@@ -220,7 +258,7 @@ def batch_elements_kwargs(self) -> Mapping[str, Any]: | |
| Returns: | ||
| kwargs suitable for beam.BatchElements. | ||
| """ | ||
| return {} | ||
| return getattr(self, '_batching_kwargs', {}) | ||
|
|
||
| def validate_inference_args(self, inference_args: Optional[dict[str, Any]]): | ||
| """ | ||
|
|
@@ -325,14 +363,14 @@ def share_model_across_processes(self) -> bool: | |
| memory. Multi-process support may vary by runner, but this will fallback to | ||
| loading per process as necessary. See | ||
| https://beam.apache.org/releases/pydoc/current/apache_beam.utils.multi_process_shared.html""" | ||
| return False | ||
| return getattr(self, '_share_across_processes', False) | ||
|
|
||
| def model_copies(self) -> int: | ||
| """Returns the maximum number of model copies that should be loaded at one | ||
| time. This only impacts model handlers that are using | ||
| share_model_across_processes to share their model across processes instead | ||
| of being loaded per process.""" | ||
| return 1 | ||
| return getattr(self, '_model_copies', None) or 1 | ||
|
|
||
| def override_metrics(self, metrics_namespace: str = '') -> bool: | ||
| """Returns a boolean representing whether or not a model handler will | ||
|
|
||
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
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.