Okay, here is the adapted bash script. It introduces a loop for the number of CPUs (N_CPUS), runs the train.py script for each combination of corpus, pretokenizer, and CPU count, times each execution, and saves the output and timing results to separate log files.
#!/usr/bin/env bash
set -euo pipefail # Exit immediately on error, treat unset variables as error, exit on pipefail
# --- Configuration ---
PRETOKENIZERS=(bytes_gpt4_cb bytes_gpt4o_cb scriptenc_cb scriptenc_gpt4o_cb)
corpora=(CulturaX-subsample-100-bal2)
N=256000 # Sample count
# Define the list of CPU counts to test
CPU_COUNTS=(128 64 32 16 8)
# Directory to store logs
LOG_DIR="training_timing_logs"
# --- Setup ---
echo "Starting training runs with varying CPU counts for performance testing..."
echo "Corpora: ${corpora[*]}"
echo "Pretokenizers: ${PRETOKENIZERS[*]}"
echo "Sample count (N): ${N}"
echo "CPU counts to test: ${CPU_COUNTS[*]}"
echo "--------------------------------------------------"
# Create log directory if it doesn't exist
mkdir -p "$LOG_DIR" || { echo "Error: Could not create log directory $LOG_DIR"; exit 1; }
# --- Run Experiments ---
# Iterate through the defined CPU counts
for cpu_count in "${CPU_COUNTS[@]}"; do
echo ">>> Starting runs with --parallel $cpu_count CPUs <<<"
# Iterate through corpora
for corpus in "${corpora[@]}"; do
# Iterate through pretokenizers
for pretokenizer in "${PRETOKENIZERS[@]}"; do
# Generate unique log filenames based on parameters
local_log_prefix="${corpus}_${pretokenizer}_${cpu_count}cpus"
stdout_log="${LOG_DIR}/${local_log_prefix}.stdout.log"
time_log="${LOG_DIR}/${local_log_prefix}.time.log"
# Combining stdout/stderr into one log for simplicity
full_log="${LOG_DIR}/${local_log_prefix}.full.log"
echo " -> Training corpus: $corpus, pretokenizer: $pretokenizer, CPUs: $cpu_count"
echo " (Full log: $full_log, Timing log: $time_log)"
# Construct the training command string.
# Quoting variables ensures spaces/special chars in names are handled.
TRAIN_CMD="uv run script_bpe/train.py --report -n \"$N\" --corpus \"$corpus\" --pretokenizer \"$pretokenizer\" --parallel \"$cpu_count\""
# Use `time -p` to get portable time output.
# We wrap the command in { ...; } to ensure time measures the whole block.
# Redirect the command's stdout and stderr to the full log file (> ... 2>&1).
# Redirect `time -p`'s stderr (where it prints results) to the time log file (2> ...).
# The `eval` command is used because we are constructing the full command string with redirections.
# Be cautious with eval, ensure the constructed string is safe. In this case,
# variables within TRAIN_CMD are quoted, and the log file paths are also quoted,
# making it relatively safe for this specific use case.
echo " Running command: time -p { $TRAIN_CMD > \"$full_log\" 2>&1; } 2> \"$time_log\""
# Execute the timed command
# The `set -e` at the top means if the `eval` fails, the script will exit.
# If you prefer to continue to the next configuration on failure,
# remove `set -e` and add error checking after `eval`.
# Example of how to run and check:
# if eval "time -p { $TRAIN_CMD > \"$full_log\" 2>&1; } 2> \"$time_log\""; then
# echo " -> Run completed successfully."
# else
# echo " -> Run FAILED! Check $full_log"
# fi
# Using `set -e` and letting it fail fast:
eval "time -p { $TRAIN_CMD > \"$full_log\" 2>&1; } 2> \"$time_log\""
# If we reached here, the eval command (and the training) succeeded (due to set -e or successful run)
echo " -> Run completed successfully."
echo " Timing results:"
cat "$time_log" # Print timing results to console for immediate view
echo "--------------------" # Separator for runs
done
done
echo "<<< Finished runs with --parallel $cpu_count CPUs >>>"
echo "" # Add a blank line between CPU groups
done
echo "--------------------------------------------------"
echo "All requested training runs completed."
echo "Logs and timing results are stored in the '$LOG_DIR' directory."
echo "Corpora tested: ${corpora[*]}"
echo "Pretokenizers tested: ${PRETOKENIZERS[*]}"
echo "CPU counts tested: ${CPU_COUNTS[*]}"
Explanation of Changes:
CPU_COUNTS=(128 64 32 16 8): A new array is defined to hold the different numbers of CPUs you want to test.
- Outer Loop: A
for cpu_count in "${CPU_COUNTS[@]}"; do ... done loop is added around the existing loops to iterate through the desired CPU configurations.
LOG_DIR="training_timing_logs": A variable is introduced for the directory where all log files will be stored, keeping your project root cleaner. mkdir -p "$LOG_DIR" ensures the directory exists.
- Unique Log Files: Inside the loops,
local_log_prefix, full_log, and time_log variables are constructed. The filenames now include the corpus name, pretokenizer name, and the CPU count, ensuring each run has its own distinct log files.
*.full.log: Contains the standard output and standard error from the uv run script_bpe/train.py command.
*.time.log: Contains the timing information (real, user, sys) output by the time -p command.
- Timing the Command:
time -p { ... }: The time -p command is used. The -p flag provides a portable output format (real, user, sys on separate lines). The command to be timed (uv run ...) is enclosed in { ...; }. This ensures time measures the duration of the entire block.
> "$full_log" 2>&1: This redirects the standard output (>) and standard error (2>&1) of the command inside the {} block (the uv run command) to the full_log file.
2> "$time_log": This redirects the standard error (2>) of the time -p command itself (where time -p prints its results) to the time_log file.
TRAIN_CMD="..." and eval: The command string is built in a variable TRAIN_CMD. Because the command structure includes redirections (> ... 2>&1) which need to be interpreted by the shell after time -p is invoked, eval is used to execute the constructed command string. This is necessary to correctly apply the redirections within the timed block. Variables inside the string are quoted (`"...\ guard against spaces/special chars in names.
This script will execute a total of len(CPU_COUNTS) * len(corpora) * len(PRETOKENIZERS) training runs. After it completes, you will find separate log files for each run in the training_timing_logs directory, and the timing results will also be printed to your console as the script progresses.