-
Notifications
You must be signed in to change notification settings - Fork 16
Add Batch routing support via @service_endpoint
with configurable batch size and timeout
#304
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
DNXie
wants to merge
6
commits into
meta-pytorch:main
Choose a base branch
from
DNXie:batch_router
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
6 commits
Select commit
Hold shift + click to select a range
b3252be
catch up where we left
DNXie 6e351aa
make batcher process one request per batch; TODO: kwargs and update t…
DNXie e7653b5
add tmp test
DNXie aea6116
add a todo
DNXie 664ef41
update tests and code
DNXie abd29a5
clean up the code
DNXie 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 |
---|---|---|
@@ -0,0 +1,199 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
""" | ||
Service endpoint management for the Forge framework. | ||
""" | ||
|
||
from typing import Any, Callable, Generic, List, TypeVar | ||
|
||
from monarch._src.actor.endpoint import EndpointProperty | ||
|
||
from typing_extensions import ParamSpec | ||
|
||
from .router import RoundRobinRouter, Router | ||
|
||
P = ParamSpec("P") | ||
R = TypeVar("R") | ||
Propagator = Any | ||
|
||
|
||
class ServiceEndpoint(Generic[P, R]): | ||
""" | ||
This extends Monarch's actor APIs for service endpoints. | ||
- `route(*args, **kwargs)`: Routes the request to a single replica. | ||
- `fanout(*args, **kwargs)`: Broadcasts the request to all healthy replicas. | ||
|
||
Monarch's native actor APIs do not apply for services. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
service, | ||
endpoint_name: str, | ||
): | ||
self.service = service | ||
self.endpoint_name = endpoint_name | ||
|
||
async def route(self, *args: P.args, **kwargs: P.kwargs) -> R: | ||
"""Chooses a replica to call based on context and load balancing strategy.""" | ||
# Extract sess_id from kwargs if present | ||
sess_id = kwargs.pop("sess_id", None) | ||
return await self.service._route(sess_id, self.endpoint_name, *args, **kwargs) | ||
|
||
async def fanout(self, *args: P.args, **kwargs: P.kwargs) -> List[R]: | ||
"""Broadcasts a request to all healthy replicas and returns the results as a list.""" | ||
result = await self.service._fanout(self.endpoint_name, *args, **kwargs) | ||
return result | ||
|
||
async def choose(self, *args: P.args, **kwargs: P.kwargs) -> R: | ||
raise NotImplementedError( | ||
"You tried to use choose() on a service, not an actor. " | ||
"Services only support route() and fanout()." | ||
) | ||
|
||
async def call(self, *args: P.args, **kwargs: P.kwargs) -> List[R]: | ||
raise NotImplementedError( | ||
"You tried to use call() on a service, not an actor. " | ||
"Services only support route() and fanout()." | ||
) | ||
|
||
async def call_one(self, *args: P.args, **kwargs: P.kwargs) -> R: | ||
raise NotImplementedError( | ||
"You tried to use a call_one() on a service, not an actor. " | ||
"Services only support route() and fanout()." | ||
) | ||
|
||
async def broadcast(self, *args: P.args, **kwargs: P.kwargs) -> List[R]: | ||
raise NotImplementedError( | ||
"You tried to use broadcast() on a service, not an actor. " | ||
"Services only support route() and fanout()." | ||
) | ||
|
||
async def generate(self, *args: P.args, **kwargs: P.kwargs): | ||
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. actor also doesn't have |
||
raise NotImplementedError( | ||
"You tried to use generate() on a service, not an actor. " | ||
"Services only support route() and fanout()." | ||
) | ||
|
||
|
||
class ServiceEndpointV2(Generic[P, R]): | ||
"""An endpoint object specific to services. | ||
|
||
This loosely mimics the Endpoint APIs exposed in Monarch, with | ||
a few key differences: | ||
- Only choose and call are retained (dropping stream and call_one) | ||
- Call returns a list directly rather than a ValueMesh. | ||
|
||
These changes are made with Forge use cases in mind, but can | ||
certainly be expanded/adapted in the future. | ||
|
||
""" | ||
|
||
def __init__(self, actor_mesh, endpoint_name: str): | ||
self.actor_mesh = actor_mesh | ||
self.endpoint_name = endpoint_name | ||
|
||
async def route(self, *args: P.args, **kwargs: P.kwargs) -> R: | ||
"""Chooses a replica to call based on context and load balancing strategy.""" | ||
# Extract sess_id from kwargs if present | ||
sess_id = kwargs.pop("sess_id", None) | ||
return await self.actor_mesh.call.call_one( | ||
sess_id, self.endpoint_name, *args, **kwargs | ||
) | ||
|
||
async def fanout(self, *args: P.args, **kwargs: P.kwargs) -> List[R]: | ||
"""Broadcasts a request to all healthy replicas and returns the results as a list.""" | ||
result = await self.actor_mesh.call_all.call_one( | ||
self.endpoint_name, *args, **kwargs | ||
) | ||
return result | ||
|
||
async def choose(self, *args: P.args, **kwargs: P.kwargs) -> R: | ||
raise NotImplementedError( | ||
"You tried to use choose() on a service, not an actor. " | ||
"Services only support route() and fanout()." | ||
) | ||
|
||
async def call(self, *args: P.args, **kwargs: P.kwargs) -> List[R]: | ||
raise NotImplementedError( | ||
"You tried to use call() on a service, not an actor. " | ||
"Services only support route() and fanout()." | ||
) | ||
|
||
async def call_one(self, *args: P.args, **kwargs: P.kwargs) -> R: | ||
raise NotImplementedError( | ||
"You tried to use a call_one() on a service, not an actor. " | ||
"Services only support route() and fanout()." | ||
) | ||
|
||
async def broadcast(self, *args: P.args, **kwargs: P.kwargs) -> List[R]: | ||
raise NotImplementedError( | ||
"You tried to use broadcast() on a service, not an actor. " | ||
"Services only support route() and fanout()." | ||
) | ||
|
||
async def generate(self, *args: P.args, **kwargs: P.kwargs): | ||
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. ? |
||
raise NotImplementedError( | ||
"You tried to use generate() on a service, not an actor. " | ||
"Services only support route() and fanout()." | ||
) | ||
|
||
|
||
class ServiceEndpointProperty(EndpointProperty, Generic[P, R]): | ||
""" | ||
Extension of EndpointProperty that carries service-specific | ||
routing and batching configuration. | ||
|
||
Inherits from EndpointProperty so the method is still registered as | ||
a valid actor endpoint, while also attaching service-specific options | ||
(router, batch_size, batch_timeout). | ||
""" | ||
|
||
def __init__( | ||
self, | ||
method: Any, | ||
propagator: Propagator, | ||
explicit_response_port: bool, | ||
*, | ||
router: Callable[[], Router] = RoundRobinRouter, | ||
batch_size: int = 1, | ||
batch_timeout: float = 0.01, | ||
) -> None: | ||
super().__init__(method, propagator, explicit_response_port) | ||
self.router = router | ||
self.batch_size = batch_size | ||
self.batch_timeout = batch_timeout | ||
|
||
|
||
def service_endpoint( | ||
*, | ||
router: Callable[[], Router] = RoundRobinRouter, | ||
batch_size: int = 1, | ||
batch_timeout: float = 0.01, | ||
propagate=None, | ||
explicit_response_port=False, | ||
): | ||
""" | ||
Marks an actor method as a service endpoint with batching routing support. | ||
|
||
Example: | ||
class MyForgeActor(ForgeActor): | ||
@service_endpoint(router=RoundRobinRouter, batch_size=16, batch_timeout=0.05) | ||
async def predict(self, x): ... | ||
""" | ||
|
||
def decorator(method) -> ServiceEndpointProperty: | ||
return ServiceEndpointProperty( | ||
method, | ||
propagator=propagate, | ||
explicit_response_port=explicit_response_port, | ||
router=router, | ||
batch_size=batch_size, | ||
batch_timeout=batch_timeout, | ||
) | ||
|
||
return decorator |
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.
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.
Do we want to schedule the task to start immediately by making this return a future?
In particular we can fire and forget without
await
ing it - this is actually what happens with monarch native actor api iiuc.