From 4794332cd8be8b1cffdc1c9e868bd6d98d577759 Mon Sep 17 00:00:00 2001 From: cloudgpuapp <129925370+cloudgpuapp@users.noreply.github.com> Date: Fri, 27 Nov 2026 12:00:50 +0800 Subject: [PATCH] Add JSON output for the GPU and rule catalogs --- docs/usage.md | 4 ++-- src/quantpreflight/cli.py | 28 +++++++++++++++++++++------- tests/test_cli_options.py | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 9 deletions(-) diff --git a/docs/usage.md b/docs/usage.md index 1ccf7e3..f6b00e9 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -71,8 +71,8 @@ Exit codes: `0` means nothing reached the `--fail-on` level, `1` means something | :--- | :--- | | `quantpreflight suggest ` | Rank the quantized versions of a model that start. It takes the same target options as `check`, plus `--limit` | | `quantpreflight explain QPF006` | Explain a rule | -| `quantpreflight list-rules` | List every rule | -| `quantpreflight list-gpus` | List GPUs and their capabilities | +| `quantpreflight list-rules [--format json]` | List every rule; JSON includes the rule ID, severity, title, and description | +| `quantpreflight list-gpus [--format json]` | List bundled GPU profiles; JSON includes every profile field | | `quantpreflight validate-profiles` | Check the bundled TOML profiles | You can pass a GPU alias or architecture such as `rtx4090` or `sm_89`. diff --git a/src/quantpreflight/cli.py b/src/quantpreflight/cli.py index d991640..d71e147 100644 --- a/src/quantpreflight/cli.py +++ b/src/quantpreflight/cli.py @@ -3,6 +3,8 @@ from __future__ import annotations import argparse +from dataclasses import asdict +import json import os import pathlib import sys @@ -181,15 +183,20 @@ def _build_parser() -> argparse.ArgumentParser: explain = sub.add_parser("explain", help="Show what a rule means.") explain.add_argument("rule", help="Rule ID, such as QPF006.") - sub.add_parser("list-gpus", help="List bundled GPU hardware profiles.") - sub.add_parser("list-rules", help="List all pre-flight rules.") + list_gpus = sub.add_parser("list-gpus", help="List bundled GPU hardware profiles.") + list_rules = sub.add_parser("list-rules", help="List all pre-flight rules.") + for listing in (list_gpus, list_rules): + listing.add_argument("--format", choices=["text", "json"], default="text", help="Output format (default: text).") sub.add_parser("validate-profiles", help="Validate all TOML architecture and hardware profiles.") return parser -def _cmd_list_gpus() -> int: +def _cmd_list_gpus(output_format: str) -> int: gpus = load_gpu_profiles() + if output_format == "json": + print(json.dumps([asdict(gpus[gid]) for gid in sorted(gpus)], indent=2)) + return 0 print(f"{'GPU':<12} {'Vendor':<8} {'Arch':<8} {'VRAM':<10} {'BF16':<6} {'FP8':<6} {'BlkFP8':<8} {'NVFP4':<6}") print("-" * 72) for gid in sorted(gpus.keys()): @@ -202,8 +209,15 @@ def _cmd_list_gpus() -> int: return 0 -def _cmd_list_rules() -> int: - for rid, (sev, title, desc) in sorted(RULE_CATALOG.items()): +def _cmd_list_rules(output_format: str) -> int: + rules = sorted(RULE_CATALOG.items()) + if output_format == "json": + print(json.dumps([ + {"id": rid, "severity": sev.value, "title": title, "description": desc} + for rid, (sev, title, desc) in rules + ], indent=2)) + return 0 + for rid, (sev, title, desc) in rules: print(f"{rid} [{sev.value.upper():<8}] {title}") print(f" {desc}") return 0 @@ -314,9 +328,9 @@ def main(argv: Sequence[str] | None = None) -> int: parser.print_help() return 0 if args.command == "list-gpus": - return _cmd_list_gpus() + return _cmd_list_gpus(args.format) if args.command == "list-rules": - return _cmd_list_rules() + return _cmd_list_rules(args.format) if args.command == "explain": return _cmd_explain(args.rule) if args.command == "validate-profiles": diff --git a/tests/test_cli_options.py b/tests/test_cli_options.py index d28a762..5bcdca4 100644 --- a/tests/test_cli_options.py +++ b/tests/test_cli_options.py @@ -323,6 +323,44 @@ def test_list_rules_shows_every_rule(capsys: pytest.CaptureFixture[str]) -> None assert "[CRITICAL]" in output +def test_list_gpus_json_includes_every_profile_field(capsys: pytest.CaptureFixture[str]) -> None: + assert main(["list-gpus", "--format", "json"]) == 0 + captured = capsys.readouterr() + assert captured.err == "" + profiles = json.loads(captured.out) + ids = [profile["id"] for profile in profiles] + assert ids == sorted(set(ids)) + h100 = next(profile for profile in profiles if profile["id"] == "H100") + assert set(h100) == { + "id", "aliases", "vendor", "arch", "compute_capability", "vram_gb", + "supports_bf16", "supports_fp8_w8a8", "supports_fp8_block_scale", + "supports_nvfp4", "supports_marlin", + } + assert h100["arch"] == "sm_90" + assert h100["vram_gb"] == 80.0 + assert h100["supports_bf16"] is True + assert "sm_90" in h100["aliases"] + + +def test_list_rules_json_preserves_the_rule_catalog(capsys: pytest.CaptureFixture[str]) -> None: + assert main(["list-rules", "--format", "json"]) == 0 + captured = capsys.readouterr() + assert captured.err == "" + expected = [ + {"id": rule_id, "severity": severity.value, "title": title, "description": description} + for rule_id, (severity, title, description) in sorted(RULE_CATALOG.items()) + ] + assert json.loads(captured.out) == expected + + +@pytest.mark.parametrize("command", ["list-gpus", "list-rules"]) +def test_list_explicit_text_matches_default(command: str, capsys: pytest.CaptureFixture[str]) -> None: + assert main([command]) == 0 + default = capsys.readouterr() + assert main([command, "--format", "text"]) == 0 + assert capsys.readouterr() == default + + # Newer options