-
Notifications
You must be signed in to change notification settings - Fork 464
[Refactor] Add structured inference server config objects #3893
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
Draft
vmoens
wants to merge
2
commits into
gh/vmoens/288/base
Choose a base branch
from
gh/vmoens/288/head
base: gh/vmoens/288/base
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.
Draft
Changes from 1 commit
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
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
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 |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # | ||
| # This source code is licensed under the MIT license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
|
|
||
| import torch | ||
|
|
||
|
|
||
| def _as_device(device: torch.device | str | None) -> torch.device | None: | ||
| if device is None: | ||
| return None | ||
| return torch.device(device) | ||
|
|
||
|
|
||
| @dataclass | ||
| class InferenceDeviceConfig: | ||
| """Device placement for asynchronous policy-server collection. | ||
|
|
||
| This config separates the devices used by the environment, the remote | ||
| policy, the actor-side action TensorDict, and the returned collector batch. | ||
|
|
||
| Args: | ||
| policy_device (torch.device or str, optional): device that owns the | ||
| policy and receives batched server inputs. | ||
| output_device (torch.device or str, optional): device for inference | ||
| results returned by the server. | ||
| env_device (torch.device or str, optional): device used by env workers | ||
| when stepping environments. If ``output_device`` is omitted, this is | ||
| the natural device for returned actions. | ||
| storing_device (torch.device or str, optional): device used for | ||
| collected transitions yielded by the collector. | ||
|
|
||
| Examples: | ||
| >>> from torchrl.modules.inference_server import InferenceDeviceConfig | ||
| >>> config = InferenceDeviceConfig(policy_device="cpu", env_device="cpu") | ||
| >>> config.policy_device | ||
| device(type='cpu') | ||
| """ | ||
|
|
||
| policy_device: torch.device | str | None = None | ||
| output_device: torch.device | str | None = None | ||
| env_device: torch.device | str | None = None | ||
| storing_device: torch.device | str | None = None | ||
|
|
||
| def __post_init__(self) -> None: | ||
| self.policy_device = _as_device(self.policy_device) | ||
| self.output_device = _as_device(self.output_device) | ||
| self.env_device = _as_device(self.env_device) | ||
| self.storing_device = _as_device(self.storing_device) | ||
|
|
||
| def server_output_device(self) -> torch.device | None: | ||
| """Return the actor-side device expected from the policy server.""" | ||
| if self.output_device is not None: | ||
| return self.output_device | ||
| return self.env_device | ||
|
|
||
|
|
||
| @dataclass | ||
| class InferenceServerConfig: | ||
| """Server-side batching, timeout, and instrumentation settings. | ||
|
|
||
| Args: | ||
| max_batch_size (int, optional): maximum number of requests per forward | ||
| pass. Defaults to ``64``. | ||
| min_batch_size (int, optional): minimum number of requests to | ||
| accumulate after the first request arrives. Defaults to ``1``. | ||
| timeout (float, optional): seconds to wait for more requests before | ||
| flushing a partial batch. Defaults to ``0.01``. | ||
| collect_stats (bool, optional): whether to collect lightweight | ||
| throughput and latency stats. Defaults to ``True``. | ||
| stats_window_size (int, optional): number of recent timing samples kept | ||
| for percentile stats. Defaults to ``1024``. | ||
|
|
||
| Examples: | ||
| >>> from torchrl.modules.inference_server import InferenceServerConfig | ||
| >>> config = InferenceServerConfig(max_batch_size=8, timeout=0.001) | ||
| >>> config.max_batch_size | ||
| 8 | ||
| """ | ||
|
|
||
| max_batch_size: int = 64 | ||
| min_batch_size: int = 1 | ||
| timeout: float = 0.01 | ||
| collect_stats: bool = True | ||
| stats_window_size: int = 1024 |
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.
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.
this is worth a paragraph in the doc somewhere