#!/bin/bash

# TODO: Why is this here? Can we delete it?
export SPACE="AIEnergyScore/launch-computation-example"

# Can use this for errors too: trap 'echo "An error occurred."' ERR

config_dir="/optimum-benchmark/examples/energy_star/"

echo "Attempting to run."
# For each line in the requests dataset....
python /parse_requests.py | head -n 10 | while read -r line; do
    # Read the name of the model and the experiment (task).
    IFS="," read model task <<< "${line}"
    echo "Benchmarking Model: ${model}, Task: ${task}"

    # Initialize the directory for output.
    now=$(date +%Y-%m-%d-%H-%M-%S)
    run_dir="/runs/${task}/${model}/${now}"
    mkdir -p "$run_dir"
    # Save the task/model run directory to text file, for tracking purposes.
    echo "${run_dir}" >> /attempts.txt

    { # try
      # Let the benchmarking begin!
      optimum-benchmark --config-name "${task}" --config-dir="${config_dir}" backend.model="${model}" backend.processor="${model}" hydra.run.dir="${run_dir}" 2> "${run_dir}/error.log"
    } || { # catch
        echo "${run_dir}" >> /failed_attempts.txt
    }
done

echo "Finished; updating requests dataset and results dataset."
python /process_runs.py

# Pausing space
echo "Pausing space."
python /pause_space.py
echo "Done."