-
Notifications
You must be signed in to change notification settings - Fork 461
Add profiler #1
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
Add profiler #1
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d91d34f
add context based profiling, control via cmd line args
lessw2020 006e854
ruff formatting
lessw2020 0707d23
address feedback - adds user config control for profiling, seperate p…
lessw2020 fa9306c
profiling now uses profiling_frequency concept to enable repeated pro…
lessw2020 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,11 @@ __pycache__ | |
.DS_Store | ||
*.egg-info | ||
build | ||
outputs | ||
|
||
# data | ||
data | ||
out | ||
wandb | ||
*.model | ||
*.json |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
torch | ||
sentencepiece | ||
datasets | ||
tomli >= 1.1.0 ; python_version < "3.11" |
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 |
---|---|---|
|
@@ -9,4 +9,4 @@ NGPU=8 | |
MP=4 | ||
|
||
torchrun --nproc_per_node=${NGPU} \ | ||
${TRAINER_DIR}/train.py | ||
train.py --steps 10 |
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,20 @@ | ||
import torch | ||
import logging | ||
|
||
logger = logging.getLogger() | ||
|
||
|
||
def rank0_log(msg): | ||
if torch.distributed.get_rank() == 0: | ||
logger.info(msg) | ||
|
||
|
||
def init_logger(): | ||
logger.setLevel(logging.INFO) | ||
ch = logging.StreamHandler() | ||
ch.setLevel(logging.INFO) | ||
formatter = logging.Formatter( | ||
"%(asctime)s - %(name)s - %(levelname)s - %(message)s" | ||
) | ||
ch.setFormatter(formatter) | ||
logger.addHandler(ch) |
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,79 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
|
||
import contextlib | ||
import os | ||
import torch | ||
|
||
try: | ||
import tomllib | ||
except ModuleNotFoundError: | ||
import tomli as tomllib | ||
|
||
from torchtrain.logging_utils import rank0_log | ||
|
||
_config_file = "./torchtrain/train_config.toml" | ||
|
||
|
||
def get_config_from_toml(config_path: str = _config_file) -> dict: | ||
""" | ||
Reads a config file in TOML format and returns a dictionary. | ||
""" | ||
with open(config_path, "rb") as f: | ||
config = tomllib.load(f) | ||
return config | ||
|
||
|
||
@contextlib.contextmanager | ||
def maybe_run_profiler(*pos_args, **kwargs): | ||
config = get_config_from_toml() | ||
|
||
# get user defined profiler settings | ||
run_profiler = config["profiling"].get("run_profiler", False) | ||
|
||
if run_profiler: | ||
dump_dir = config["global"]["dump_folder"] | ||
save_trace_dir = config["profiling"]["save_traces_folder"] | ||
trace_dir = os.path.join(dump_dir, save_trace_dir) | ||
iter_frequency = config["profiling"]["profile_every_x_iter"] | ||
|
||
_global_iter_count = 0 | ||
|
||
rank = torch.distributed.get_rank() | ||
|
||
def trace_handler(prof): | ||
nonlocal _global_iter_count | ||
_global_iter_count += iter_frequency | ||
curr_trace_dir_name = "iteration_" + str(_global_iter_count) | ||
curr_trace_dir = os.path.join(trace_dir, curr_trace_dir_name) | ||
if not os.path.exists(curr_trace_dir): | ||
os.makedirs(curr_trace_dir) | ||
rank0_log(f"exporting profile traces to {curr_trace_dir}") | ||
|
||
prof.export_chrome_trace(f"{curr_trace_dir}/rank{rank}_trace.json") | ||
|
||
rank0_log(f"Profiling active. Traces will be saved at {trace_dir}") | ||
|
||
if not os.path.exists(trace_dir): | ||
os.makedirs(trace_dir) | ||
|
||
with torch.profiler.profile( | ||
activities=[ | ||
torch.profiler.ProfilerActivity.CPU, | ||
torch.profiler.ProfilerActivity.CUDA, | ||
], | ||
schedule=torch.profiler.schedule( | ||
wait=iter_frequency - 2, | ||
warmup=1, | ||
active=1, | ||
repeat=0, | ||
), | ||
on_trace_ready=trace_handler, | ||
profile_memory=True, | ||
with_stack=False, | ||
record_shapes=True, | ||
) as torch_profiler: | ||
yield torch_profiler | ||
else: | ||
torch_profiler = contextlib.nullcontext() | ||
yield None |
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,9 @@ | ||
# TorchTrain Config.toml | ||
[global] | ||
dump_folder = "./torchtrain/outputs" | ||
|
||
[profiling] | ||
run_profiler = true | ||
save_traces_folder = "profiling/traces" | ||
# profiling frequency - example: 10 means every 10th iter will be profiled | ||
profile_every_x_iter = 10 |
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
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.