refactor: migrate scripts from argparse to click, add YAML config support

- Replace argparse with click in all scripts (train, server, generate,
  preprocess, benchmark)
- Add --config YAML support to train.py with CLI flag override
- Add --dry-run mode to validate config before training
- Add type annotations throughout benchmark.py
- Unify docstring format across all commands
- Remove redundant deps httpx, requests, pyyaml, rich from pyproject.toml
- Net -346 lines while adding YAML config support
This commit is contained in:
2026-07-27 06:55:46 +08:00
parent b99485f462
commit 4de42d83c2
8 changed files with 571 additions and 711 deletions
+39 -53
View File
@@ -1,72 +1,58 @@
import argparse
from pathlib import Path
import click
import torch
from astrai.inference import run_server
_DTYPES = ["bfloat16", "float16", "float32"]
def main():
parser = argparse.ArgumentParser(description="Start AstrAI inference HTTP server")
parser.add_argument(
"--host", default="0.0.0.0", help="Host address (default: 0.0.0.0)"
)
parser.add_argument(
"--port", type=int, default=8000, help="Port number (default: 8000)"
)
parser.add_argument(
"--reload", action="store_true", help="Enable auto-reload for development"
)
parser.add_argument(
"--param_path",
type=Path,
default=None,
help="Path to model parameters (default: project_root/params)",
)
parser.add_argument(
"--device",
type=str,
default="cuda",
help="Device to load model on (default: cuda)",
)
parser.add_argument(
"--dtype",
type=str,
default="bfloat16",
choices=["bfloat16", "float16", "float32"],
help="Data type for model weights (default: bfloat16)",
)
parser.add_argument(
"--max_batch_size",
type=int,
default=16,
help="Maximum batch size for continuous batching (default: 16)",
)
args = parser.parse_args()
# Convert dtype string to torch dtype
@click.command(name="serve", help="Launch inference server (OpenAI-compatible API).")
@click.option("--host", default="0.0.0.0", help="Host address.")
@click.option("--port", type=int, default=8000, help="Port number.")
@click.option("--reload", is_flag=True, help="Enable auto-reload for development.")
@click.option(
"--param_path",
type=click.Path(exists=True),
default=None,
help="Path to model parameters.",
)
@click.option("--device", default="cuda", help="Device to load model on.")
@click.option(
"--dtype",
type=click.Choice(_DTYPES),
default="bfloat16",
help="Data type for model weights.",
)
@click.option(
"--max_batch_size",
type=int,
default=16,
help="Maximum batch size for continuous batching.",
)
def server_command(host, port, reload, param_path, device, dtype, max_batch_size):
"""Launch inference server (OpenAI-compatible API)."""
dtype_map = {
"bfloat16": torch.bfloat16,
"float16": torch.float16,
"float32": torch.float32,
}
dtype = dtype_map[args.dtype]
project_root = Path(__file__).parent.parent.parent
param_path = args.param_path or (project_root / "params")
print(f"Starting AstrAI inference server on http://{args.host}:{args.port}")
print(f"Model parameters expected at: {param_path}")
print(f"Device: {args.device}, Dtype: {args.dtype}")
param_path = param_path or str(project_root / "params")
click.echo(f"Starting server on http://{host}:{port}")
click.echo(f"Model: {param_path} | Device: {device} | Dtype: {dtype}")
run_server(
host=args.host,
port=args.port,
reload=args.reload,
device=args.device,
dtype=dtype,
param_path=param_path,
max_batch_size=args.max_batch_size,
host=host,
port=port,
reload=reload,
device=device,
dtype=dtype_map[dtype],
param_path=Path(param_path),
max_batch_size=max_batch_size,
)
if __name__ == "__main__":
main()
server_command()