|
| 1 | +#!/bin/bash |
| 2 | +# Monitor power consumption for llama-bench with different thread configurations |
| 3 | +# Usage: ./monitor_power.sh <model_path> <output_csv> <pp_threads> <tg_threads> |
| 4 | +# Example: ./monitor_power.sh models/model.gguf results.csv "1,2,4,8" "1,2,4,8" |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +# Parse arguments |
| 9 | +if [ $# -ne 4 ]; then |
| 10 | + echo "Usage: $0 <model_path> <output_csv> <pp_threads> <tg_threads>" |
| 11 | + echo "Example: $0 models/model.gguf results.csv \"1,2,4,8\" \"1,2,4,8\"" |
| 12 | + exit 1 |
| 13 | +fi |
| 14 | + |
| 15 | +MODEL_PATH="$1" |
| 16 | +OUTPUT_CSV="$2" |
| 17 | +PP_THREADS="$3" |
| 18 | +TG_THREADS="$4" |
| 19 | + |
| 20 | +TEMP_LOG="/tmp/power_monitor_$$.log" |
| 21 | +PID_FILE="/tmp/monitor_$$.pid" |
| 22 | +BENCH_OUTPUT="/tmp/bench_output_$$.txt" |
| 23 | + |
| 24 | +# Validate model exists |
| 25 | +if [ ! -f "$MODEL_PATH" ]; then |
| 26 | + echo "Error: Model file not found: $MODEL_PATH" |
| 27 | + exit 1 |
| 28 | +fi |
| 29 | + |
| 30 | +# Create output directory if needed |
| 31 | +mkdir -p "$(dirname "$OUTPUT_CSV")" |
| 32 | + |
| 33 | +# Function to monitor CPU stats |
| 34 | +monitor_cpu() { |
| 35 | + local log_file="$1" |
| 36 | + echo "Timestamp,CPU_Usage(%),Avg_Freq(MHz)" > "$log_file" |
| 37 | + while [ -f "$PID_FILE" ]; do |
| 38 | + cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print 100-$8}') |
| 39 | + avg_freq=$(grep "cpu MHz" /proc/cpuinfo | awk '{sum+=$4; count++} END {printf "%.0f", sum/count}') |
| 40 | + timestamp=$(date +%s.%N) |
| 41 | + echo "$timestamp,$cpu_usage,$avg_freq" >> "$log_file" |
| 42 | + sleep 0.5 |
| 43 | + done |
| 44 | +} |
| 45 | + |
| 46 | +# Function to calculate average power |
| 47 | +calculate_power() { |
| 48 | + local log_file="$1" |
| 49 | + awk -F',' 'NR>1 {sum_cpu+=$2; count++} END { |
| 50 | + if (count > 0) { |
| 51 | + avg_cpu = sum_cpu/count |
| 52 | + est_power = avg_cpu * 200 / 100 |
| 53 | + printf "%.2f", est_power |
| 54 | + } else { |
| 55 | + print "0" |
| 56 | + } |
| 57 | + }' "$log_file" |
| 58 | +} |
| 59 | + |
| 60 | +# Function to extract throughput from llama-bench output |
| 61 | +extract_throughput() { |
| 62 | + local bench_output="$1" |
| 63 | + local workload="$2" |
| 64 | + grep "$workload" "$bench_output" | awk '{ |
| 65 | + # Extract mean from "mean ± std" format |
| 66 | + for (i=1; i<=NF; i++) { |
| 67 | + if ($(i+1) == "±") { |
| 68 | + printf "%.2f", $i |
| 69 | + exit |
| 70 | + } |
| 71 | + } |
| 72 | + }' |
| 73 | +} |
| 74 | + |
| 75 | +# Function to run single benchmark |
| 76 | +run_benchmark() { |
| 77 | + local workload="$1" # "pp" or "tg" |
| 78 | + local threads="$2" |
| 79 | + local n_flag="" |
| 80 | + |
| 81 | + if [ "$workload" = "pp" ]; then |
| 82 | + n_flag="-n 0" |
| 83 | + workload_name="pp128" |
| 84 | + else |
| 85 | + n_flag="-n 128" |
| 86 | + workload_name="tg128" |
| 87 | + fi |
| 88 | + |
| 89 | + # Output progress to stderr (won't be captured in CSV) |
| 90 | + echo "Testing $workload_name with $threads threads..." >&2 |
| 91 | + |
| 92 | + # Start monitoring |
| 93 | + touch "$PID_FILE" |
| 94 | + monitor_cpu "$TEMP_LOG" & |
| 95 | + local monitor_pid=$! |
| 96 | + |
| 97 | + # Run benchmark |
| 98 | + ./build/bin/llama-bench -m "$MODEL_PATH" -p 128 $n_flag -t "$threads" -ngl 0 > "$BENCH_OUTPUT" 2>&1 |
| 99 | + |
| 100 | + # Stop monitoring |
| 101 | + rm -f "$PID_FILE" |
| 102 | + wait $monitor_pid 2>/dev/null || true |
| 103 | + |
| 104 | + # Extract results |
| 105 | + local throughput=$(extract_throughput "$BENCH_OUTPUT" "$workload_name") |
| 106 | + local power=$(calculate_power "$TEMP_LOG") |
| 107 | + |
| 108 | + if [ -z "$throughput" ] || [ "$throughput" = "0" ]; then |
| 109 | + echo "Warning: Failed to extract throughput for $workload_name, threads=$threads" >&2 |
| 110 | + throughput="0" |
| 111 | + fi |
| 112 | + |
| 113 | + # Calculate J/t (Joules per token) |
| 114 | + local j_per_token=$(awk -v p="$power" -v t="$throughput" 'BEGIN { |
| 115 | + if (t > 0) printf "%.4f", p/t; else print "0" |
| 116 | + }') |
| 117 | + |
| 118 | + # Output progress to stderr |
| 119 | + echo " Throughput: $throughput t/s, Power: $power W, Energy: $j_per_token J/t" >&2 |
| 120 | + |
| 121 | + # Only output CSV line to stdout (this will be captured) |
| 122 | + echo "$workload_name,$threads,$throughput,$power,$j_per_token" |
| 123 | +} |
| 124 | + |
| 125 | +# Initialize CSV |
| 126 | +echo "Workload,Threads,Throughput(t/s),Power(W),Energy(J/t)" > "$OUTPUT_CSV" |
| 127 | + |
| 128 | +# Test PP workloads |
| 129 | +IFS=',' read -ra PP_ARRAY <<< "$PP_THREADS" |
| 130 | +for threads in "${PP_ARRAY[@]}"; do |
| 131 | + threads=$(echo "$threads" | xargs) # trim whitespace |
| 132 | + result=$(run_benchmark "pp" "$threads") |
| 133 | + echo "$result" >> "$OUTPUT_CSV" |
| 134 | +done |
| 135 | + |
| 136 | +# Test TG workloads |
| 137 | +IFS=',' read -ra TG_ARRAY <<< "$TG_THREADS" |
| 138 | +for threads in "${TG_ARRAY[@]}"; do |
| 139 | + threads=$(echo "$threads" | xargs) # trim whitespace |
| 140 | + result=$(run_benchmark "tg" "$threads") |
| 141 | + echo "$result" >> "$OUTPUT_CSV" |
| 142 | +done |
| 143 | + |
| 144 | +# Cleanup |
| 145 | +rm -f "$TEMP_LOG" "$BENCH_OUTPUT" "$PID_FILE" |
| 146 | + |
| 147 | +echo "" |
| 148 | +echo "=== Benchmark Complete ===" |
| 149 | +echo "Results saved to: $OUTPUT_CSV" |
| 150 | +echo "" |
| 151 | +cat "$OUTPUT_CSV" |
0 commit comments