Skip to content

Commit 58b1eaf

Browse files
Jack-Khuumalfet
authored andcommitted
Cleaning up --help: Artifact Management Subcommands (#889)
1 parent 527687d commit 58b1eaf

File tree

3 files changed

+64
-22
lines changed

3 files changed

+64
-22
lines changed

cli.py

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,35 @@
2424
).expanduser()
2525

2626

27-
KNOWN_VERBS = ["chat", "browser", "download", "generate", "eval", "export", "list", "remove", "where"]
27+
# Subcommands related to downloading and managing model artifacts
28+
INVENTORY_VERBS = ["download", "list", "remove", "where"]
29+
30+
# List of all supported subcommands in torchchat
31+
KNOWN_VERBS = ["chat", "browser", "generate", "eval", "export"] + INVENTORY_VERBS
2832

2933
# Handle CLI arguments that are common to a majority of subcommands.
3034
def check_args(args, verb: str) -> None:
3135
# Handle model download. Skip this for download, since it has slightly
3236
# different semantics.
3337
if (
34-
verb not in ["download", "list", "remove"]
38+
verb not in INVENTORY_VERBS
3539
and args.model
3640
and not is_model_downloaded(args.model, args.model_directory)
3741
):
3842
download_and_convert(args.model, args.model_directory, args.hf_token)
3943

4044

41-
def add_arguments_for_verb(parser, verb: str):
45+
def add_arguments_for_verb(parser, verb: str) -> None:
4246
# Model specification. TODO Simplify this.
4347
# A model can be specified using a positional model name or HuggingFace
4448
# path. Alternatively, the model can be specified via --gguf-path or via
4549
# an explicit --checkpoint-dir, --checkpoint-path, or --tokenizer-path.
50+
51+
if verb in INVENTORY_VERBS:
52+
_configure_artifact_inventory_args(parser, verb)
53+
_add_cli_metadata_args(parser)
54+
return
55+
4656
parser.add_argument(
4757
"model",
4858
type=str,
@@ -191,12 +201,6 @@ def add_arguments_for_verb(parser, verb: str):
191201
choices=allowable_dtype_names(),
192202
help="Override the dtype of the model (default is the checkpoint dtype). Options: bf16, fp16, fp32, fast16, fast",
193203
)
194-
parser.add_argument(
195-
"-v",
196-
"--verbose",
197-
action="store_true",
198-
help="Verbose output",
199-
)
200204
parser.add_argument(
201205
"--quantize",
202206
type=str,
@@ -252,6 +256,46 @@ def add_arguments_for_verb(parser, verb: str):
252256
default=5000,
253257
help="Port for the web server in browser mode",
254258
)
259+
_add_cli_metadata_args(parser)
260+
261+
262+
# Add CLI Args that are relevant to any subcommand execution
263+
def _add_cli_metadata_args(parser) -> None:
264+
parser.add_argument(
265+
"-v",
266+
"--verbose",
267+
action="store_true",
268+
help="Verbose output",
269+
)
270+
271+
272+
# Configure CLI Args specific to Model Artifact Management
273+
def _configure_artifact_inventory_args(parser, verb: str) -> None:
274+
if verb in ["download", "remove", "where"]:
275+
parser.add_argument(
276+
"model",
277+
type=str,
278+
nargs="?",
279+
default=None,
280+
help="Model name for well-known models",
281+
)
282+
283+
if verb in INVENTORY_VERBS:
284+
parser.add_argument(
285+
"--model-directory",
286+
type=Path,
287+
default=default_model_dir,
288+
help=f"The directory to store downloaded model artifacts. Default: {default_model_dir}",
289+
)
290+
291+
if verb == "download":
292+
parser.add_argument(
293+
"--hf-token",
294+
type=str,
295+
default=None,
296+
help="A HuggingFace API token to use when downloading model artifacts",
297+
)
298+
255299

256300
# Add CLI Args specific to Model Evaluation
257301
def _add_evaluation_args(parser) -> None:

download.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,6 @@ def is_model_downloaded(model: str, models_dir: Path) -> bool:
125125

126126
# Subcommand to list available models.
127127
def list_main(args) -> None:
128-
# TODO It would be nice to have argparse validate this. However, we have
129-
# model as an optional named parameter for all subcommands, so we'd
130-
# probably need to move it to be registered per-command.
131-
if args.model:
132-
print("Usage: torchchat.py list")
133-
return
134-
135128
model_configs = load_model_configs()
136129

137130
# Build the table in-memory so that we can align the text nicely.

torchchat.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from cli import (
1313
add_arguments_for_verb,
1414
KNOWN_VERBS,
15+
INVENTORY_VERBS,
1516
arg_init,
1617
check_args,
1718
)
@@ -49,7 +50,11 @@
4950

5051
# Now parse the arguments
5152
args = parser.parse_args()
52-
args = arg_init(args)
53+
54+
# Don't initialize for Inventory management subcommands
55+
# TODO: Remove when arg_init is refactored
56+
if args.command not in INVENTORY_VERBS:
57+
args = arg_init(args)
5358
logging.basicConfig(
5459
format="%(message)s", level=logging.DEBUG if args.verbose else logging.INFO
5560
)
@@ -70,11 +75,6 @@
7075
from browser.browser import main as browser_main
7176

7277
browser_main(args)
73-
elif args.command == "download":
74-
check_args(args, "download")
75-
from download import download_main
76-
77-
download_main(args)
7878
elif args.command == "generate":
7979
check_args(args, "generate")
8080
from generate import main as generate_main
@@ -89,6 +89,11 @@
8989
from export import main as export_main
9090

9191
export_main(args)
92+
elif args.command == "download":
93+
check_args(args, "download")
94+
from download import download_main
95+
96+
download_main(args)
9297
elif args.command == "list":
9398
check_args(args, "list")
9499
from download import list_main

0 commit comments

Comments
 (0)