-
Notifications
You must be signed in to change notification settings - Fork 24
metric logger - backend (1/N) #191
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 20 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
e162788
metric logger simple example
9f13bfb
it works
4b9aada
delete old files
2864324
refactoring + docstrings
c337083
docstring
16f2267
comments
40e16c2
update method name
d7c175d
no circular import
538e8f2
update command
64c71f2
Merge branch 'main' of https://github.com/pytorch-labs/forge into met…
166b5d4
update arg name
e27d451
move metric actor out of asyncio lock
11ea544
Merge branch 'main' of https://github.com/pytorch-labs/forge into met…
4a8db51
Merge branch 'main' into metric_logging
5cadbee
fix deregister
cb33d5f
lint
f28097c
docstring
3772620
Merge branch 'main' of github.com:meta-pytorch/forge into metric_logging
06afbb5
fix result extraction and add logger shutdown
5369939
fix shutdown order
ffe09b9
simplification + docstrings
185504d
bug fix + register if respawn
052e937
it works
efb639d
use procmesh as key
781906d
docstring
f2a9e09
remove protected imports
8e157bd
create get_metric_logger
5465080
Merge branch 'main' of github.com:meta-pytorch/forge into metric_logging
5736c79
call became fanout
2b0bb05
Merge branch 'main' of github.com:meta-pytorch/forge into metric_logging
a426cd5
upstream changes
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,118 @@ | ||
# 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. | ||
|
||
import asyncio | ||
|
||
import logging | ||
import sys | ||
import time | ||
|
||
from forge.controller.actor import ForgeActor | ||
from forge.controller.provisioner import shutdown | ||
from forge.observability.metric_actors import GlobalLoggingActor | ||
from forge.observability.metrics import record_metric, ReductionType | ||
|
||
from monarch.actor import current_rank, endpoint, get_or_spawn_controller | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
|
||
|
||
class TrainActor(ForgeActor): | ||
@endpoint | ||
async def train_step(self, step: int): | ||
rank = current_rank().rank | ||
value = rank * 1000 + 100 * step | ||
print(f"🔧 Train rank {rank}: Step {step}, loss={value}") | ||
await record_metric("train/loss", value) | ||
|
||
|
||
class GeneratorActor(ForgeActor): | ||
@endpoint | ||
async def generate_step(self, step: int, substep: int): | ||
rank = current_rank().rank | ||
value = rank * 1000 + step * 100 + substep * 10 | ||
print(f"🎯 Gen rank {rank}: Step {step}.{substep}, tokens={value}") | ||
await record_metric("generate/tokens", value, ReductionType.SUM) | ||
|
||
|
||
# Main | ||
async def main(mode: str = "wandb_all_log_all"): | ||
group = f"experiment_group_{int(time.time())}" | ||
if mode == "wandb_all_log_all": | ||
backends = [ | ||
{"class": "console", "log_per_rank": True}, | ||
{ | ||
"class": "wandb", | ||
"project": "my_project", | ||
"group": group, | ||
"mode": "wandb_all_log_all", | ||
"log_per_rank": True, | ||
}, | ||
] | ||
elif mode == "wandb_rank_0_reduce_all": | ||
backends = [ | ||
{"class": "console", "log_per_rank": False}, | ||
{ | ||
"class": "wandb", | ||
"project": "my_project", | ||
"group": group, | ||
"mode": "wandb_rank_0_reduce_all", | ||
"log_per_rank": False, | ||
}, | ||
] | ||
else: # wandb_rank_0_log_all | ||
backends = [ | ||
{ | ||
"class": "wandb", | ||
"project": "my_project", | ||
"group": group, | ||
"mode": "wandb_rank_0_log_all", | ||
"log_per_rank": True, | ||
}, | ||
] | ||
|
||
logging_config = { | ||
"backends": backends, | ||
} | ||
service_config = {"procs": 2, "num_replicas": 2, "with_gpus": False} | ||
|
||
# Spawn services first (triggers registrations via provisioner hook) | ||
trainer = await TrainActor.options(**service_config).as_service() | ||
generator = await GeneratorActor.options(**service_config).as_service() | ||
|
||
# Now init config on global (inits backends eagerly across fetchers) | ||
global_logger = await get_or_spawn_controller("global_logger", GlobalLoggingActor) | ||
await global_logger.initialize_backends.call_one(logging_config) | ||
felipemello1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
for i in range(3): | ||
print(f"\n=== Global Step {i} ===") | ||
await trainer.train_step.call(i) | ||
for sub in range(3): | ||
await generator.generate_step.call(i, sub) | ||
await global_logger.flush.call_one(i) | ||
|
||
# shutdown | ||
await asyncio.gather(global_logger.shutdown.call_one()) | ||
|
||
await asyncio.gather( | ||
trainer.shutdown(), | ||
generator.shutdown(), | ||
) | ||
|
||
await shutdown() | ||
|
||
|
||
if __name__ == "__main__": | ||
mode = sys.argv[1] if len(sys.argv) > 1 else "wandb_all_log_all" | ||
valid_modes = [ | ||
felipemello1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
"wandb_all_log_all", | ||
"wandb_rank_0_log_all", | ||
"wandb_rank_0_reduce_all", | ||
] | ||
if mode not in valid_modes: | ||
print(f"Invalid mode: {mode}. Use {valid_modes}") | ||
sys.exit(1) | ||
asyncio.run(main(mode)) |
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,5 @@ | ||
# 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. |
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.