-
Notifications
You must be signed in to change notification settings - Fork 251
[Distributed]Integrate toml for configs, sink distributed launch & DCP work to distributed level #898
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
[Distributed]Integrate toml for configs, sink distributed launch & DCP work to distributed level #898
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bcc111e
start inference.sh, toml configs
lessw2020 a25e26f
first toml
lessw2020 0c2627f
add config_manager
lessw2020 0dfc8b2
basic toml load, prep for starting dist
lessw2020 044e51a
sink init and add toml parsing
lessw2020 003425a
toml load working
lessw2020 3ea7c59
add distributed logger
lessw2020 b4b566a
logging working
lessw2020 f2a8a40
ruff and isort
lessw2020 50e697a
remove inference.py
lessw2020 be7db92
better toml breakout, add tomli if python < 3.11
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
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,127 @@ | ||
# 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 argparse | ||
import os | ||
from collections import defaultdict | ||
from pathlib import Path | ||
from typing import Tuple | ||
|
||
import torch | ||
|
||
from distributed.logging_utils import logger | ||
|
||
try: | ||
import tomllib | ||
except ModuleNotFoundError: | ||
import tomli as tomllib | ||
|
||
|
||
TORCH_DTYPE_MAP = { | ||
"float16": torch.float16, | ||
"float32": torch.float32, | ||
"bfloat16": torch.bfloat16, | ||
} | ||
|
||
# this is used for pp placement | ||
def string_list(raw_arg): | ||
return raw_arg.split(",") | ||
|
||
|
||
class InferenceConfig: | ||
""" | ||
A helper class to manage the inference configuration. | ||
Semantics: | ||
- Default config is loaded from a toml file. If no toml file is provided, | ||
then the default config is loaded from argparse defaults. | ||
- if toml file has missing keys, they are filled with argparse defaults. | ||
- if additional explicit cmd args are provided in addition to the toml | ||
file, they will override the toml config and the argparse defaults | ||
|
||
precedence order: cmdline > toml > argparse default | ||
|
||
Arg parsing semantics: | ||
|
||
Each argument starts with <prefix>_ which is the section name in the toml file | ||
followed by name of the option in the toml file. For ex, | ||
model.name translates to: | ||
[model] | ||
name | ||
in the toml file | ||
""" | ||
|
||
def __init__(self): | ||
# main parser | ||
self.parser = argparse.ArgumentParser(description="torchchat arg parser.") | ||
|
||
def parse_args(self, config_file): | ||
|
||
args_dict = defaultdict(defaultdict) | ||
local_path = "inference_configs/"+ config_file | ||
full_path = os.path.join(os.getcwd(), local_path) | ||
file_path = Path(full_path) | ||
|
||
logger.info(f"Loading config file {config_file}") | ||
|
||
if not file_path.is_file(): | ||
raise FileNotFoundError(f"Config file {full_path} does not exist") | ||
|
||
try: | ||
with open(file_path, "rb") as f: | ||
for k, v in tomllib.load(f).items(): | ||
# to prevent overwrite of non-specified keys | ||
print(f"{k} {v}") | ||
args_dict[k] |= v | ||
except (FileNotFoundError, tomllib.TOMLDecodeError) as e: | ||
logger.exception( | ||
f"Error while loading the configuration file: {config_file}" | ||
) | ||
logger.exception(f"Error details: {str(e)}") | ||
raise e | ||
|
||
for k, v in args_dict.items(): | ||
class_type = type(k.title(), (), v) | ||
setattr(self, k, class_type()) | ||
|
||
|
||
def _args_to_two_level_dict(self, args: argparse.Namespace) -> defaultdict: | ||
args_dict = defaultdict(defaultdict) | ||
for k, v in vars(args).items(): | ||
first_level_key, second_level_key = k.split(".", 1) | ||
args_dict[first_level_key][second_level_key] = v | ||
return args_dict | ||
|
||
def _validate_config(self) -> bool: | ||
# TODO: Add more mandatory validations | ||
assert self.model.name and self.model.flavor and self.model.tokenizer_path | ||
return True | ||
|
||
def parse_args_from_command_line( | ||
self, args_list | ||
) -> Tuple[argparse.Namespace, argparse.Namespace]: | ||
""" | ||
Parse command line arguments and return the parsed args and the command line only args | ||
""" | ||
args = self.parser.parse_args(args_list) | ||
|
||
# aux parser to parse the command line only args, with no defaults from main parser | ||
aux_parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS) | ||
for arg, val in vars(args).items(): | ||
if isinstance(val, bool): | ||
aux_parser.add_argument( | ||
"--" + arg, action="store_true" if val else "store_false" | ||
) | ||
elif arg == "inference.pipeline_parallel_split_points": | ||
# without this special case, type inference breaks here, | ||
# since the inferred type is just 'list' and it ends up flattening | ||
# e.g. from ["layers.0", "layers.1"] into ["l", "a", "y", "e", "r", "s", ".0", ...] | ||
aux_parser.add_argument("--" + arg, type=string_list) | ||
else: | ||
aux_parser.add_argument("--" + arg, type=type(val)) | ||
|
||
cmd_args, _ = aux_parser.parse_known_args(args_list) | ||
|
||
return args, cmd_args |
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,42 @@ | ||
# torchchat Distributed Config.toml | ||
|
||
[job] | ||
dump_folder = "./outputs" | ||
description = "Llama 3 distributed inference" | ||
use_for_integration_test = true | ||
|
||
[profiling] | ||
enable_profiling = false | ||
save_traces_folder = "profile_trace" | ||
profile_freq = 10 | ||
enable_memory_snapshot = false | ||
save_memory_snapshot_folder = "memory_snapshot" | ||
|
||
[metrics] | ||
enable_color_printing = true | ||
enable_tensorboard = true | ||
save_tb_folder = "tb" | ||
|
||
[model] | ||
name = "llama3" | ||
flavor = "8B" | ||
tokenizer_path = "./test/assets/test_tiktoken.model" | ||
dtype = "bfloat16" | ||
|
||
[parallel] | ||
pipeline_parallel_degree = 1 | ||
tensor_parallel_degree = 2 | ||
enable_async_tensor_parallel=false | ||
|
||
[inference] | ||
batch_size = 8 | ||
seq_len = 2048 | ||
reps=1 # for profiling inference runs, can run repeatedly | ||
fp8_linear = "" | ||
compile = false | ||
|
||
[pipelining] | ||
pipeline_parallel_split_points= "layers.4" # string list of placements | ||
pipeline_parallel_schedule="gpipe" # TODO - what is best inference schedule for continous batching | ||
pipeline_parallel_split_mode = "manual" | ||
pipeline_parallel_microbatches=1 # TODO - continuous batching |
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,24 @@ | ||
# 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 logging | ||
import os | ||
|
||
logger = logging.getLogger() | ||
|
||
|
||
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) | ||
|
||
# suppress verbose torch.profiler logging | ||
os.environ["KINETO_LOG_LEVEL"] = "5" |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
0.0.1 |
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.