|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +Example usage: |
| 5 | +python scripts/evaluate_best_checkpoint.py \ |
| 6 | + /path/to/checkpoint_dir \ |
| 7 | + --output-file /path/to/output_file |
| 8 | +""" |
| 9 | + |
| 10 | +# Standard |
| 11 | +from pathlib import Path |
| 12 | +from typing import Optional |
| 13 | +import json |
| 14 | + |
| 15 | +# Third Party |
| 16 | +import typer |
| 17 | + |
| 18 | +app = typer.Typer() |
| 19 | + |
| 20 | + |
| 21 | +@app.command() |
| 22 | +def main( |
| 23 | + input_dir: Path = typer.Argument(..., help="Input directory to process"), |
| 24 | + output_file: Optional[Path] = typer.Option(None, help="Optional output file path"), |
| 25 | +): |
| 26 | + """ |
| 27 | + Process files in the input directory and optionally save results to an output file. |
| 28 | + """ |
| 29 | + if not input_dir.exists(): |
| 30 | + typer.echo(f"Error: Input directory '{input_dir}' does not exist") |
| 31 | + raise typer.Exit(1) |
| 32 | + |
| 33 | + if not input_dir.is_dir(): |
| 34 | + typer.echo(f"Error: '{input_dir}' is not a directory") |
| 35 | + raise typer.Exit(1) |
| 36 | + |
| 37 | + checkpoint_dirs = list(input_dir.glob("hf_format/samples_*")) |
| 38 | + typer.echo(f"Found {len(checkpoint_dirs)} samples files") |
| 39 | + |
| 40 | + if not checkpoint_dirs: |
| 41 | + typer.echo( |
| 42 | + f"No checkpoint directories found in the input directory: {input_dir}" |
| 43 | + ) |
| 44 | + raise typer.Exit(1) |
| 45 | + |
| 46 | + typer.echo("importing LeaderboardV2Evaluator, this may take a while...") |
| 47 | + # First Party |
| 48 | + from instructlab.eval.leaderboard import LeaderboardV2Evaluator |
| 49 | + |
| 50 | + checkpoint_results = {} |
| 51 | + for checkpoint in checkpoint_dirs: |
| 52 | + typer.echo(f"Processing checkpoint: {checkpoint}") |
| 53 | + ckpt_output_file = checkpoint / "leaderboard_results.json" |
| 54 | + evaluator = LeaderboardV2Evaluator( |
| 55 | + model_path=str(checkpoint), output_file=ckpt_output_file, num_gpus=8 |
| 56 | + ) |
| 57 | + result = evaluator.run() |
| 58 | + checkpoint_results[checkpoint.name] = result |
| 59 | + typer.echo(f"Checkpoint {checkpoint.name} results: {result['overall_score']}") |
| 60 | + |
| 61 | + # Sort checkpoints by score |
| 62 | + sorted_checkpoints = sorted( |
| 63 | + checkpoint_results.items(), key=lambda x: x[1]["overall_score"], reverse=True |
| 64 | + ) |
| 65 | + typer.echo("Sorted checkpoints by score:") |
| 66 | + for checkpoint_name, result in sorted_checkpoints: |
| 67 | + typer.echo(f"{'=' * 100}") |
| 68 | + typer.echo(json.dumps(result, indent=2)) |
| 69 | + |
| 70 | + typer.echo(f"{'=' * 100}") |
| 71 | + typer.echo(f"Best checkpoint: {sorted_checkpoints[0][0]}") |
| 72 | + |
| 73 | + if output_file: |
| 74 | + typer.echo(f"Output will be saved to: {output_file}") |
| 75 | + with open(output_file, "w") as f: |
| 76 | + json.dump(checkpoint_results, f, indent=2) |
| 77 | + |
| 78 | + # Add your processing logic here |
| 79 | + |
| 80 | + typer.echo("Processing complete!") |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + app() |
0 commit comments