parse.py 5.75 KB
Newer Older
1
# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# SPDX-License-Identifier: Apache-2.0
# ruff: noqa
# pylint: skip-file

import json
import os
import re

### Slurm configs
SLURM_JOB_ID = "slurm id"
### Model Deployment configurations
PREFILL_TP = "Prefill TP"
PREFILL_DP = "Prefill DP"
DECODE_TP = "Decode TP"
DECODE_DP = "Decode DP"
FRONTENDS = "Frontends"
### Profiler configs
PROFILER_TYPE = "Profiler type"
ISL = "ISL"
OSL = "OSL"
REQUEST_RATE = "Request rate"
CONCURRENCIES = "Concurrencies"
OUTPUT_TPS = "Output TPS"
OUTPUT_TPS_PER_USER = "Output TPS/User"
ITL = "Mean ITL (ms)"
TTFT = "Mean TTFT (ms)"
TPOT = "Mean TPOT (ms)"
### FORMAT PRINT ORDERS
KEY_PRINT_ORDER = [
    SLURM_JOB_ID,
    PREFILL_TP,
    PREFILL_DP,
    DECODE_TP,
    DECODE_DP,
    FRONTENDS,
    PROFILER_TYPE,
    ISL,
    OSL,
    REQUEST_RATE,
    CONCURRENCIES,
    OUTPUT_TPS,
    OUTPUT_TPS_PER_USER,
    ITL,
    TTFT,
    TPOT,
]


def format_key_order():
    report = "================\nThe following log will be reported according to this order:\n----\n"
    for key in KEY_PRINT_ORDER:
        report += f"{key}\n"
    print(report[:-1])


def format_print(result):
    report = "================\n"
    for key in KEY_PRINT_ORDER:
        report += f"{result.get(key, '')}\n"
    print(report[:-1])


def analyze_sgl_out(folder):
    result = []
    for file in os.listdir(folder):
        with open(f"{folder}/{file}", "r") as f:
            content = json.load(f)
            res = [
                content["max_concurrency"],
                content["output_throughput"],
                content["mean_itl_ms"],
                content["mean_ttft_ms"],
                content["request_rate"],
            ]

            if "mean_tpot_ms" in content:
                res.append(content["mean_tpot_ms"])
            result.append(res)
    out = {
        REQUEST_RATE: [],
        CONCURRENCIES: [],
        OUTPUT_TPS: [],
        ITL: [],
        TTFT: [],
        TPOT: [],
    }

    for data in sorted(result, key=lambda x: x[0]):
        con, tps, itl, ttft, req_rate = data[0:5]
        out[CONCURRENCIES].append(con)
        out[OUTPUT_TPS].append(tps)
        out[ITL].append(itl)
        out[TTFT].append(ttft)
        out[REQUEST_RATE].append(req_rate)

        if len(data) >= 6:
            if TPOT not in out:
                out[TPOT] = []
            out[TPOT].append(data[5])

    return out


def analyze_gap_out(folder):
    result = []
    for file in os.listdir(folder):
        with open(f"{folder}/{file}", "r") as f:
            content = json.load(f)
            result.append(
                (
                    content["input_config"]["perf_analyzer"]["stimulus"]["concurrency"],
                    content["output_token_throughput_per_user"]["avg"],
                    content["output_token_throughput"]["avg"],
                )
            )

    out = {CONCURRENCIES: [], OUTPUT_TPS: [], OUTPUT_TPS_PER_USER: []}

    for con, tpspuser, tps in sorted(result, key=lambda x: x[0]):
        out[CONCURRENCIES].append(con)
        out[OUTPUT_TPS].append(tps)
        out[OUTPUT_TPS_PER_USER].append(tpspuser)

    return out


def analyze(p):
    files = os.listdir(p)

    prefill_nodes = {}
    decode_nodes = {}
    frontends = []

    profile_result = {}

    for file in files:
        p_re = re.search(
            "([-_A-Za-z0-9]+)_(prefill|decode|nginx|frontend)_([a-zA-Z0-9]+).out", file
        )
        if p_re is not None:
            _, node_type, number = p_re.groups()
            if node_type == "prefill":
                if number not in prefill_nodes:
                    prefill_nodes[number] = []
                prefill_nodes[number].append(file)
            elif node_type == "decode":
                if number not in decode_nodes:
                    decode_nodes[number] = []
                decode_nodes[number].append(file)
            elif node_type == "frontend":
                frontends.append(file)

        profiler_match = re.match("(sglang|vllm|gap)_isl_([0-9]+)_osl_([0-9]+)", file)
        if profiler_match:
            profiler, isl, osl = profiler_match.groups()
            if profiler == "gap":
                profile_result = analyze_gap_out(f"{p}/{file}")
            else:
                profile_result = analyze_sgl_out(f"{p}/{file}")

            profile_result[PROFILER_TYPE] = profiler
            profile_result[ISL] = isl
            profile_result[OSL] = osl

    config = {SLURM_JOB_ID: p}
    if len(prefill_nodes.values()) != 0:
        config[PREFILL_TP] = f"{len(list(prefill_nodes.values())[0]) * 4}"
        config[PREFILL_DP] = f"{len(prefill_nodes.keys())}"

    if len(decode_nodes.values()) != 0:
        config[DECODE_TP] = f"{len(list(decode_nodes.values())[0]) * 4}"
        config[DECODE_DP] = f"{len(decode_nodes.keys())}"

    if len(frontends) != 0:
        config[FRONTENDS] = f"{len(frontends)}"

    result = {**config}
    for key, value in profile_result.items():
        result[key] = (
            value
            if type(value) != list
            else ", ".join([str(x) for x in value])  # ignore:
        )
    return result


paths = [x for x in os.listdir(".") if ".py" not in x and os.path.isdir(x)]
format_key_order()


def extract_job_id(dirname):
    """Extract job ID from directory name for sorting.

    Handles formats like:
    - 12345_3P_1D_20250104_123456 (disaggregated)
    - 12345_4A_20250104_123456 (aggregated)
    - 12345 (legacy format)
    """
    try:
        return int(dirname.split("_")[0])
    except (ValueError, IndexError):
        # If directory name doesn't match expected format, return -1
        return -1


for path in sorted(paths, key=extract_job_id, reverse=True):
    result = analyze(path)
    if OUTPUT_TPS not in result:
        pass
    else:
        format_print(result)