Skip to content

Commit 01ad332

Browse files
Jack-Khuufduwjj
authored andcommitted
Cleaning up --help: Artifact Management Subcommands (#889)
1 parent 89d82b1 commit 01ad332

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,
@@ -197,12 +207,6 @@ def add_arguments_for_verb(parser, verb: str):
197207
choices=allowable_dtype_names(),
198208
help="Override the dtype of the model (default is the checkpoint dtype). Options: bf16, fp16, fp32, fast16, fast",
199209
)
200-
parser.add_argument(
201-
"-v",
202-
"--verbose",
203-
action="store_true",
204-
help="Verbose output",
205-
)
206210
parser.add_argument(
207211
"--quantize",
208212
type=str,
@@ -258,6 +262,46 @@ def add_arguments_for_verb(parser, verb: str):
258262
default=5000,
259263
help="Port for the web server in browser mode",
260264
)
265+
_add_cli_metadata_args(parser)
266+
267+
268+
# Add CLI Args that are relevant to any subcommand execution
269+
def _add_cli_metadata_args(parser) -> None:
270+
parser.add_argument(
271+
"-v",
272+
"--verbose",
273+
action="store_true",
274+
help="Verbose output",
275+
)
276+
277+
278+
# Configure CLI Args specific to Model Artifact Management
279+
def _configure_artifact_inventory_args(parser, verb: str) -> None:
280+
if verb in ["download", "remove", "where"]:
281+
parser.add_argument(
282+
"model",
283+
type=str,
284+
nargs="?",
285+
default=None,
286+
help="Model name for well-known models",
287+
)
288+
289+
if verb in INVENTORY_VERBS:
290+
parser.add_argument(
291+
"--model-directory",
292+
type=Path,
293+
default=default_model_dir,
294+
help=f"The directory to store downloaded model artifacts. Default: {default_model_dir}",
295+
)
296+
297+
if verb == "download":
298+
parser.add_argument(
299+
"--hf-token",
300+
type=str,
301+
default=None,
302+
help="A HuggingFace API token to use when downloading model artifacts",
303+
)
304+
261305

262306
# Add CLI Args specific to Model Evaluation
263307
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)