main.py 1.76 KB
Newer Older
1
2
3
#!/usr/bin/env python3

import sys
one's avatar
one committed
4
5
6
from typing import Annotated

import typer
7

one's avatar
one committed
8
9
from .runner import run_with_input
from .utils import get_mpi_rank
10

one's avatar
one committed
11
12
13
app = typer.Typer(
    help=(
        "RCCL Log Parser Wrapper\n\n"
one's avatar
one committed
14
15
16
        "Usage modes:\n"
        "  1. Pipe input:    cat log.txt | xcl-lens\n"
        "  2. Read files:    xcl-lens log1.txt log2.txt\n"
one's avatar
one committed
17
18
19
20
        "  3. Wrap command:  xcl-lens ./all_reduce_perf"
    ),
    add_completion=False,
)
21

one's avatar
one committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

@app.command(
    context_settings={
        "allow_extra_args": True,
        "ignore_unknown_options": True,
        "help_option_names": ["-h", "--help"],
    }
)
def main(
    ctx: typer.Context,
    summary: Annotated[bool, typer.Option(help="Print summary report only")] = False,
    verbose: Annotated[bool, typer.Option("-v", "--verbose", help="Print verbose reports")] = False,
    hosts: Annotated[str | None, typer.Option(help="Filter by hosts (comma-separated)")] = None,
    ranks: Annotated[str | None, typer.Option(help="Filter by ranks (comma-separated)")] = None,
):
    """RCCL Log Parser Wrapper."""
    rank = get_mpi_rank()
    command: list[str] = ctx.args
40
41

    try:
one's avatar
one committed
42
43
44
45
46
47
48
49
50
51
        hosts_list = [h.strip() for h in hosts.split(",")] if hosts else None
        ranks_list = [r.strip() for r in ranks.split(",")] if ranks else None
        exit_code = run_with_input(
            command=command,
            summary=summary,
            verbose=verbose,
            hosts=hosts_list,
            ranks=ranks_list,
            rank=rank,
        )
one's avatar
one committed
52
53
        if exit_code is not None:
            sys.exit(exit_code)
54
55
    except KeyboardInterrupt:
        sys.exit(130)
one's avatar
one committed
56
57
58

    # If we got here, no command was provided and stdin is a tty
    if rank == 0:
one's avatar
one committed
59
        raise typer.Exit(code=1)
one's avatar
one committed
60
    sys.exit(1)
61
62
63


if __name__ == "__main__":
one's avatar
one committed
64
    app()