Skip to content

Refactor common download logic into cli.py #407

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 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 13 additions & 0 deletions cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pathlib import Path

from build.utils import allowable_dtype_names, allowable_params_table
from download import download_and_convert, is_model_downloaded

import torch

Expand All @@ -19,6 +20,18 @@ def check_args(args, name: str) -> None:
pass


# Handle CLI arguments that are common to a majority of subcommands.
def handle_common_args(args) -> None:
# Handle model download. Skip this for download, since it has slightly
# different semantics.
if (
args.command != "download"
and args.model
and not is_model_downloaded(args.model, args.model_directory)
):
download_and_convert(args.model, args.model_directory, args.hf_token)


def add_arguments_for_chat(parser):
# Only chat specific options should be here
_add_arguments_common(parser)
Expand Down
5 changes: 0 additions & 5 deletions eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from build.model import Transformer
from build.utils import set_precision
from cli import add_arguments, add_arguments_for_eval, arg_init
from download import download_and_convert, is_model_downloaded
from generate import encode_tokens, model_forward

torch._dynamo.config.automatic_dynamic_shapes = True
Expand Down Expand Up @@ -221,10 +220,6 @@ def main(args) -> None:
"""

# If a named model was provided and not downloaded, download it.
if args.model and not is_model_downloaded(args.model, args.model_directory):
download_and_convert(args.model, args.model_directory, args.hf_token)

builder_args = BuilderArgs.from_args(args)
tokenizer_args = TokenizerArgs.from_args(args)
quantize = args.quantize
Expand Down
6 changes: 0 additions & 6 deletions export.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

from build.utils import set_backend, set_precision, use_aoti_backend, use_et_backend
from cli import add_arguments, add_arguments_for_export, arg_init, check_args
from download import download_and_convert, is_model_downloaded
from export_aoti import export_model as export_model_aoti

try:
Expand All @@ -35,11 +34,6 @@


def main(args):
# THIS BELONGS INTO CLI
# If a named model was provided and not downloaded, download it.
# if args.model and not is_model_downloaded(args.model, args.model_directory):
# download_and_convert(args.model, args.model_directory, args.hf_token)

builder_args = BuilderArgs.from_args(args)
quantize = args.quantize

Expand Down
5 changes: 0 additions & 5 deletions generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from build.model import Transformer
from build.utils import device_sync, set_precision
from cli import add_arguments, add_arguments_for_generate, arg_init, check_args
from download import download_and_convert, is_model_downloaded

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -586,10 +585,6 @@ def callback(x):


def main(args):
# If a named model was provided and not downloaded, download it.
if args.model and not is_model_downloaded(args.model, args.model_directory):
download_and_convert(args.model, args.model_directory, args.hf_token)

builder_args = BuilderArgs.from_args(args)
speculative_builder_args = BuilderArgs.from_speculative_args(args)
tokenizer_args = TokenizerArgs.from_args(args)
Expand Down
3 changes: 3 additions & 0 deletions torchchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
add_arguments_for_generate,
arg_init,
check_args,
handle_common_args,
)

default_device = "cpu"
Expand Down Expand Up @@ -97,6 +98,8 @@
format="%(message)s", level=logging.DEBUG if args.verbose else logging.INFO
)

handle_common_args(args)

if args.command == "chat":
# enable "chat"
args.chat = True
Expand Down