|
| 1 | +# Copyright 2023–2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +""" Plot loss curves from training logs """ |
| 15 | + |
| 16 | +# Usage: python plot_loss_curves.py logdir |
| 17 | + |
| 18 | +import re |
| 19 | +import os |
| 20 | +import sys |
| 21 | +import argparse |
| 22 | + |
| 23 | +import matplotlib.pyplot as plt |
| 24 | + |
| 25 | + |
| 26 | +def parse_loss_data(file_path): |
| 27 | + """ |
| 28 | + Parses a text file for lines matching the pattern: |
| 29 | + completed step: <int>, seconds: <float>, TFLOP/s/device: <float>, |
| 30 | + Tokens/s/device: <float>, total_weights: <int>, loss: <float> |
| 31 | + Returns a list of tuples with the extracted values. |
| 32 | + """ |
| 33 | + pattern = re.compile( |
| 34 | + r"completed step: (\d+), seconds: ([\d.]+), TFLOP/s/device: ([\d.]+), Tokens/s/device: ([\d.]+), total_weights: (\d+), loss: ([\d.]+)" # pylint: disable=line-too-long |
| 35 | + ) |
| 36 | + results = [] |
| 37 | + with open(file_path, "r", encoding="utf-8") as f: |
| 38 | + for line in f: |
| 39 | + match = pattern.search(line) |
| 40 | + if match: |
| 41 | + step = int(match.group(1)) |
| 42 | + seconds = float(match.group(2)) |
| 43 | + tflops = float(match.group(3)) |
| 44 | + tokens_per_sec = float(match.group(4)) |
| 45 | + total_weights = int(match.group(5)) |
| 46 | + loss = float(match.group(6)) |
| 47 | + results.append((step, seconds, tflops, tokens_per_sec, total_weights, loss)) |
| 48 | + return results |
| 49 | + |
| 50 | + |
| 51 | +def main(args): |
| 52 | + parser = argparse.ArgumentParser(description="Plot training loss curve from log files.") |
| 53 | + parser.add_argument("logdir", type=str, help="Directory containing training log files.") |
| 54 | + parsed_args = parser.parse_args(args) |
| 55 | + |
| 56 | + logdir = parsed_args.logdir |
| 57 | + log_files = [ |
| 58 | + os.path.join(logdir, f) |
| 59 | + for f in os.listdir(logdir) |
| 60 | + if os.path.isfile(os.path.join(logdir, f)) and f.endswith(".log") |
| 61 | + ] |
| 62 | + |
| 63 | + # Extract parallelism configs from filenames |
| 64 | + config_pattern = re.compile(r"dp(\d+)_tpsp(\d+)_fsdp(\d+)") |
| 65 | + configs = {} |
| 66 | + for log_file in log_files: |
| 67 | + fname = os.path.basename(log_file) |
| 68 | + match = config_pattern.search(fname) |
| 69 | + if match: |
| 70 | + dp, tpsp, fsdp = match.groups() |
| 71 | + key = (int(dp), int(tpsp), int(fsdp)) |
| 72 | + configs.setdefault(key, []).append(log_file) |
| 73 | + |
| 74 | + # Plot for each config |
| 75 | + for (dp, tpsp, fsdp), files in configs.items(): |
| 76 | + plt.figure(figsize=(8, 5)) |
| 77 | + for log_file in files: |
| 78 | + data = parse_loss_data(log_file) |
| 79 | + if not data: |
| 80 | + continue |
| 81 | + steps = [item[0] for item in data] |
| 82 | + losses = [item[5] for item in data] |
| 83 | + plt.plot( |
| 84 | + steps, |
| 85 | + losses, |
| 86 | + marker="", |
| 87 | + linestyle="-", |
| 88 | + label=os.path.basename(log_file), |
| 89 | + ) |
| 90 | + plt.legend() |
| 91 | + plt.xlabel("Step") |
| 92 | + plt.ylabel("Loss") |
| 93 | + plt.title(f"Loss Curves (dp={dp}, tpsp={tpsp}, fsdp={fsdp})") |
| 94 | + plt.grid(True) |
| 95 | + plt.tight_layout() |
| 96 | + out_image_path = f"loss_curves_dp{dp}_tpsp{tpsp}_fsdp{fsdp}.png" |
| 97 | + plt.savefig(out_image_path) |
| 98 | + print(f"Saved plot to {out_image_path}") |
| 99 | + plt.close() |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + main(sys.argv[1:]) |
0 commit comments