-
Notifications
You must be signed in to change notification settings - Fork 19
Refactor service spawning: add ForgeActor.options().as_service() API #153
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
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
da21e1d
Add reward interface, math reward, unit tests
DNXie 5c72908
Merge branch 'meta-pytorch:main' into main
DNXie b4d7a61
Merge branch 'meta-pytorch:main' into main
DNXie 02d77c6
Merge branch 'meta-pytorch:main' into main
DNXie fd1d38b
Merge branch 'meta-pytorch:main' into main
DNXie f79beee
Merge branch 'meta-pytorch:main' into main
DNXie d8d775a
Merge branch 'meta-pytorch:main' into main
DNXie e423c44
Merge branch 'meta-pytorch:main' into main
DNXie 4815c05
Merge branch 'meta-pytorch:main' into main
DNXie 77d41e4
Merge branch 'meta-pytorch:main' into main
DNXie a3feb1e
Merge branch 'meta-pytorch:main' into main
DNXie 23d7e02
Merge branch 'meta-pytorch:main' into main
DNXie 2ca881d
refactor, buggy
DNXie 4df5d3a
some tweak
DNXie 0b5c0db
add options under forgeactor, and tested it with vllm main
DNXie 1cc5cf2
add as_service to forgeactor for default config
DNXie a92952a
enable passing serviceConfig obj to options
DNXie 2ce61d1
update all the usages
DNXie f32fef7
Merge branch 'main' into add_options
DNXie f28824d
fix script error and CI broken test
DNXie 549f43a
fix ci test
DNXie 26a4207
remove redundant line
DNXie a311cbd
Update tests/unit_tests/test_service.py
DNXie 1261568
Merge branch 'main' into add_options
DNXie c1854ec
add missing import back
DNXie e595fbd
fix ci
DNXie 52cb676
Merge branch 'add_options' of github.com:DNXie/forge into DNXie-add_o…
DNXie fd38100
Merge branch 'DNXie-add_options'
DNXie 9a80b16
Merge branch 'main' of github.com:DNXie/forge
DNXie 09e7237
merge
DNXie 0165027
make options return a forgeactor and as_service return a serviceinter…
DNXie 721e32a
remove redundant test case
DNXie 915baf1
fix lint
DNXie 4171675
fix broken ci
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
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 |
---|---|---|
|
@@ -8,14 +8,17 @@ | |
|
||
import math | ||
import sys | ||
from typing import Type, TypeVar | ||
|
||
from monarch.actor import Actor, current_rank, current_size, endpoint | ||
|
||
from forge.controller.proc_mesh import get_proc_mesh, stop_proc_mesh | ||
from forge.types import ProcessConfig | ||
|
||
from forge.types import ProcessConfig, ServiceConfig | ||
|
||
logger = logging.getLogger(__name__) | ||
logger.setLevel(logging.DEBUG) | ||
T = TypeVar("T", bound="ForgeActor") | ||
|
||
|
||
class ForgeActor(Actor): | ||
|
@@ -41,6 +44,78 @@ def __init__(self, *args, **kwargs): | |
self.logger.root.addHandler(stdout_handler) | ||
super().__init__(*args, **kwargs) | ||
|
||
@classmethod | ||
def options( | ||
cls: Type[T], | ||
*, | ||
service_config: ServiceConfig | None = None, | ||
num_replicas: int | None = None, | ||
procs_per_replica: int | None = None, | ||
**service_kwargs, | ||
) -> Type[T]: | ||
""" | ||
Returns a subclass of this ForgeActor with a bound ServiceConfig. | ||
The returned subclass can later be launched via `.as_service()`. | ||
|
||
Usage (choose ONE of the following forms): | ||
# Option A: construct ServiceConfig implicitly | ||
service = await MyForgeActor.options( | ||
num_replicas=1, | ||
procs_per_replica=2, | ||
).as_service(...) | ||
await service.shutdown() | ||
|
||
# Option B: provide an explicit ServiceConfig | ||
cfg = ServiceConfig(num_replicas=1, procs_per_replica=2, ..) | ||
service = await MyForgeActor.options(service_config=cfg).as_service(...) | ||
await service.shutdown() | ||
|
||
# Option C: skip options, use the default service config with num_replicas=1, procs_per_replica=1 | ||
service = await MyForgeActor.as_service(...) | ||
await service.shutdown() | ||
""" | ||
|
||
if service_config is not None: | ||
cfg = service_config | ||
else: | ||
if num_replicas is None or procs_per_replica is None: | ||
raise ValueError( | ||
"Must provide either `service_config` or (num_replicas + procs_per_replica)." | ||
) | ||
cfg = ServiceConfig( | ||
num_replicas=num_replicas, | ||
procs_per_replica=procs_per_replica, | ||
**service_kwargs, | ||
) | ||
|
||
return type( | ||
f"{cls.__name__}Configured", | ||
(cls,), | ||
{"_service_config": cfg}, | ||
) | ||
|
||
@classmethod | ||
async def as_service(cls: Type[T], **actor_kwargs) -> "ServiceInterface": | ||
""" | ||
Convenience method to spawn this actor as a Service using default configuration. | ||
If `.options()` was called, it will use the bound ServiceConfig; | ||
otherwise defaults to 1 replica, 1 proc. | ||
""" | ||
# Lazy import to avoid top-level dependency issues | ||
|
||
from forge.controller.service import Service, ServiceInterface | ||
|
||
# Use _service_config if already set by options(), else default | ||
cfg = getattr(cls, "_service_config", None) | ||
if cfg is None: | ||
cfg = ServiceConfig(num_replicas=1, procs_per_replica=1) | ||
# dynamically create a configured subclass for consistency | ||
cls = type(f"{cls.__name__}Configured", (cls,), {"_service_config": cfg}) | ||
|
||
logger.info(("Spawning Service Actor for %s", cls.__name__)) | ||
service = Service(cfg, cls, actor_kwargs) | ||
await service.__initialize__() | ||
return ServiceInterface(service, cls) | ||
|
||
@endpoint | ||
async def setup(self): | ||
"""Sets up the actor. | ||
|
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
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.