Skip to content
Closed
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
2c349fd
router access latest healith_replicas and sessionmap
DNXie Sep 24, 2025
d351935
fix test
DNXie Sep 24, 2025
1c7efac
add batch routing logic to service + test case
DNXie Sep 24, 2025
821714c
moving endpoint logic to endpoint.py; add decorator for service_endpoint
DNXie Sep 25, 2025
64c7076
buggy version
DNXie Sep 26, 2025
c581a9a
finally working, todo: clean up and add docstr
DNXie Sep 26, 2025
2f87cb1
fix lint and clean up
DNXie Sep 26, 2025
52796d1
more clean up
DNXie Sep 26, 2025
2464ca8
separate batch routing logic to BatchedServiceEndpoint
DNXie Sep 26, 2025
926c601
add docstring
DNXie Sep 26, 2025
4ca60ba
add a test case
DNXie Sep 26, 2025
0131e21
correct test case
DNXie Sep 26, 2025
93d8c9d
Update src/forge/controller/service/endpoint.py
DNXie Sep 26, 2025
90e94b9
Update src/forge/controller/service/interface.py
DNXie Sep 26, 2025
4393a51
resolve comments
DNXie Sep 26, 2025
653001e
move batching logic back to Batcher class, keep router for each endpo…
DNXie Sep 29, 2025
47e7f82
minor
DNXie Sep 29, 2025
baf2ef6
@service_endpoint returns ServiceEndpointProperty
DNXie Sep 29, 2025
595751e
simplify _set_router
DNXie Sep 29, 2025
0085972
update docstring and test cases
DNXie Sep 29, 2025
1de2981
Merge remote-tracking branch 'origin/main' into batch_router
DNXie Sep 29, 2025
1bd0f91
add call/choose/call_one/... to ServiceEndpointV2
DNXie Sep 30, 2025
8b61802
raise error if endpoint already exist in self.routers
DNXie Sep 30, 2025
f4a60d8
call->route; call_all -> fanout
DNXie Sep 30, 2025
e9bd7c7
move get_replica to route
DNXie Sep 30, 2025
8f16006
remove a tmp file (committed by mistake
DNXie Sep 30, 2025
03ff0c2
remove dict for batcher config; add one more test for config
DNXie Sep 30, 2025
1faf6a6
add docstring to explain why ServiceEndpointProperty inherits Endpoin…
DNXie Sep 30, 2025
bcc35bb
fix lint
DNXie Sep 30, 2025
ef1faa4
change router from router obj to callable
DNXie Sep 30, 2025
e2aee83
batch requests
DNXie Sep 30, 2025
9060ec9
add changes to ServiceInterfaceV2 for future adaption
DNXie Sep 30, 2025
57a1abe
fix missing import
DNXie Sep 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/forge/controller/service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

from .endpoint import service_endpoint, ServiceEndpointProperty
from .interface import ServiceInterface, Session, SessionContext
from .metrics import ServiceMetrics
from .replica import Replica, ReplicaMetrics, ReplicaState
from .router import LeastLoadedRouter, RoundRobinRouter, SessionRouter
from .router import Batcher, LeastLoadedRouter, RoundRobinRouter, Router, SessionRouter
from .service import Service, ServiceActor, ServiceConfig

__all__ = [
Expand All @@ -24,4 +25,8 @@
"LeastLoadedRouter",
"RoundRobinRouter",
"SessionRouter",
"service_endpoint",
"ServiceEndpointProperty",
"Router",
"Batcher",
]
199 changes: 199 additions & 0 deletions src/forge/controller/service/endpoint.py
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, 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):
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):
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: 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: Router = RoundRobinRouter(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this instead be a router constructor? Like right now we pass in a full Router object - probably fine to start with, but typically for efficiency reasons we pass in a constructor function, which we create later.

So it'd look like

router: Callable[[], Router]

and

        class MyForgeActor(ForgeActor):
            @service_endpoint(router=RoundRobinRouter, batch_size=16, batch_timeout=0.05)
            async def predict(self, x): ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Done!

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
126 changes: 10 additions & 116 deletions src/forge/controller/service/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,11 @@
"""

import contextvars
import logging
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Dict, Generic, List, ParamSpec, TypeVar

from monarch._src.actor.endpoint import EndpointProperty

from .replica import Replica

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

P = ParamSpec("P")
R = TypeVar("R")
from .endpoint import ServiceEndpoint, ServiceEndpointProperty, ServiceEndpointV2


@dataclass
Expand Down Expand Up @@ -77,94 +68,6 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
self.session_id = None


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._call(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.call_all(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):
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 choose(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 call(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


class ServiceInterface:
"""
A lightweight interface to the base Service class.
Expand All @@ -182,10 +85,15 @@ def __init__(self, _service, actor_def):
# Inspect the actor_def directly to find endpoints
for attr_name in dir(actor_def):
attr_value = getattr(actor_def, attr_name)
if isinstance(attr_value, EndpointProperty):
# Create a ServiceEndpoint that will route through the Service Actor
endpoint = ServiceEndpoint(self._service, attr_name)
setattr(self, attr_name, endpoint)

# ServiceEndpointProperty: created by @service_endpoint
# EndpointProperty: created by @endpoint
if isinstance(attr_value, (EndpointProperty, ServiceEndpointProperty)):
if isinstance(attr_value, ServiceEndpointProperty):
# Register router with service-specific config
self._service._set_router(attr_name, attr_value)

setattr(self, attr_name, ServiceEndpoint(self._service, attr_name))

# Session management methods - handled by ServiceInterface
async def start_session(self) -> str:
Expand Down Expand Up @@ -306,17 +214,3 @@ def __getattr__(self, name: str):
raise AttributeError(
f"'{self.__class__.__name__}' object has no attribute '{name}'"
)


class Router(ABC):
"""Abstract base class for routing logic."""

@abstractmethod
def get_replica(
self,
healthy_replicas: List[Replica],
sess_id: str | None = None,
session_map: Dict[str, int] | None = None,
) -> Replica:
"""Select a replica from the list based on routing logic."""
pass
Loading
Loading