|
#!/bin/zsh |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
run_training_script() { |
|
local script_path="$1" |
|
local args_array=("${@:2}") |
|
|
|
|
|
local current_dir=$(pwd) |
|
|
|
|
|
local script_dir=$(dirname "$script_path") |
|
local script_name=$(basename "$script_path") |
|
cd "$script_dir" || return 1 |
|
|
|
|
|
[[ ! -f "$script_name" ]] && echo "\e[31mERROR\e[0m: Script not found: $script_name" && return 1 |
|
|
|
echo "Working directory: $(pwd)\nRunning $script_name arguments:" |
|
for arg in "${args_array[@]}"; do |
|
echo " $arg" |
|
done |
|
|
|
if [[ -n "$DEBUG" ]]; then |
|
echo "This was a dry run, exiting." | tee "$OUTPUT_DIR/$NAME/sdscripts.log" |
|
local exit_code=0 |
|
else |
|
python "$(basename "$script_path")" "${args_array[@]}" | tee "$OUTPUT_DIR/$NAME/sdscripts.log" |
|
local exit_code=$? |
|
fi |
|
|
|
|
|
cd "$current_dir" |
|
|
|
return $exit_code |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setup_training_vars() { |
|
local name="$1" |
|
|
|
|
|
typeset -g DATASET_NAME="${name%-*}" |
|
typeset -g TRAINING_DIR="${TRAINING_DIR:-"${HOME}/datasets/${DATASET_NAME}"}" |
|
typeset -g STEPS=${STEPS:-"${name##*[^0-9]}"} |
|
typeset -g OUTPUT_DIR="${HOME}/output_dir" |
|
|
|
echo "\e[35moutput_name\e[0m: $name, \e[35msteps\e[0m: $STEPS, \e[35mtraining_dir\e[0m: $(realpath --relative-to=. $TRAINING_DIR), \e[35moutput_dir\e[0m: $(realpath --relative-to=. "$OUTPUT_DIR/$name")" |
|
echo "\e[35mconda_env\e[0m: $CONDA_PREFIX" |
|
|
|
|
|
[[ ! -d "$TRAINING_DIR" ]] && echo "ERROR: Training directory not found" && exit 1 |
|
if [[ -d "$OUTPUT_DIR/$name" ]]; then |
|
echo "ERROR: Output directory already exists: $OUTPUT_DIR/$name" |
|
exit 1 |
|
fi |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setup_conda_env() { |
|
local env_name="$1" |
|
[[ -z "$env_name" ]] && echo "\e[31mERROR\e[0m: Environment name required" && return 1 |
|
|
|
local conda_path="${2:-$HOME/miniconda3}" |
|
[[ ! -d "$conda_path" ]] && echo "\e[31mERROR\e[0m: Conda installation not found at $conda_path" && return 1 |
|
|
|
|
|
if __conda_setup="$("$conda_path/bin/conda" 'shell.zsh' 'hook' 2>/dev/null)" && eval "$__conda_setup"; then |
|
unset __conda_setup |
|
else |
|
echo "\e[31mERROR\e[0m: Failed to initialize conda environment" && return 1 |
|
fi |
|
|
|
|
|
conda activate "$env_name" |
|
if [ $? -ne 0 ]; then |
|
echo "\e[31mERROR\e[0m: Failed to activate conda environment: $env_name" |
|
return 1 |
|
fi |
|
echo "Conda environment: $CONDA_PREFIX" |
|
|
|
|
|
if ! conda env list | grep -q "^${env_name} "; then |
|
echo "ERROR: Environment $env_name not found" |
|
return 1 |
|
fi |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
store_commits_hashes() { |
|
|
|
local output_dir="$OUTPUT_DIR/$NAME" |
|
|
|
local output_file="$output_dir/repos.git" |
|
|
|
[[ ! -d "$output_dir" ]] && mkdir -p "$output_dir" |
|
|
|
: >"$output_file" |
|
|
|
local summary="" |
|
local res=0 |
|
|
|
for repo_path in "$@"; do |
|
local repo_name=$(basename "$repo_path") |
|
if [[ -d "$repo_path/.git" ]]; then |
|
if local commit_sha=$(git -C "$repo_path" rev-parse HEAD 2>/dev/null); then |
|
|
|
if local branch_name=$(git -C "$repo_path" rev-parse --abbrev-ref HEAD 2>/dev/null); then |
|
echo "$repo_path: ($branch_name) $commit_sha" >>"$output_file" |
|
summary+="✓ $repo_name: $repo_path ${commit_sha:0:8} ($branch_name)\n" |
|
else |
|
echo "$repo_path: $commit_sha (Failed to get branch)" >>"$output_file" |
|
summary+="⚠️ $repo_name: $repo_path ${commit_sha:0:8} (Failed to get branch)\n" |
|
res=1 |
|
fi |
|
else |
|
echo "$repo_path: Git command failed" >>"$output_file" |
|
summary+="⚠️ $repo_name: $repo_path (Git command failed) \n" |
|
res=1 |
|
fi |
|
else |
|
echo "$repo_path: Not a git repository" >>"$output_file" |
|
summary+="⚠️ $repo_name: Not a git repository $repo_path\n" |
|
res=1 |
|
fi |
|
done |
|
|
|
|
|
local script_path=$(readlink -f "$ZSH_SCRIPT") |
|
cp "$script_path" "$output_dir/$(basename "$script_path")" |
|
[[ -n "$DEBUG" ]] && echo "Copied $script_path to $output_dir" |
|
|
|
|
|
local script_sha=$(sha1sum "$script_path" | cut -f1 -d' ') |
|
echo "$script_path: $script_sha" >>"$output_file" |
|
summary+="✓ Training script: $ZSH_SCRIPT ${script_sha:0:8}\n" |
|
|
|
|
|
local config_sha=$(sha1sum "$TRAINING_DIR/config.toml" | cut -f1 -d' ') |
|
local prompts_sha=$(sha1sum "$TRAINING_DIR/sample-prompts.txt" | cut -f1 -d' ') |
|
cp "$TRAINING_DIR/config.toml" "$output_dir/config.toml" |
|
cp "$TRAINING_DIR/sample-prompts.txt" "$output_dir/sample-prompts.txt" |
|
echo "$TRAINING_DIR/config.toml: $config_sha" >>"$output_file" |
|
echo "$TRAINING_DIR/sample-prompts.txt: $prompts_sha" >>"$output_file" |
|
summary+="✓ Training config: $TRAINING_DIR/config.toml ${config_sha:0:8}\n" |
|
summary+="✓ Training prompts: $TRAINING_DIR/sample-prompts.txt ${prompts_sha:0:8}\n" |
|
|
|
echo -e "$summary" |
|
return $res |
|
} |
|
|
|
get_lycoris_repo() { |
|
python -c """ |
|
import importlib.util |
|
import pathlib |
|
spec = importlib.util.find_spec('lycoris') |
|
print(pathlib.Path(spec.origin).parent.parent) |
|
""" |
|
} |
|
|
|
cleanup_empty_output() { |
|
[[ -n "$DEBUG" ]] && echo "\e[33mDEBUG\e[0m: Cleanup triggered for $OUTPUT_DIR/$NAME" |
|
|
|
|
|
[[ ! -d "$OUTPUT_DIR/$NAME" ]] && { |
|
[[ -n "$DEBUG" ]] && echo "\e[33mDEBUG\e[0m: Output directory doesn't exist, skipping cleanup" |
|
return 0 |
|
} |
|
|
|
|
|
local samples=("$OUTPUT_DIR/$NAME"/**/*.png(N)) |
|
local models=("$OUTPUT_DIR/$NAME"/**/*.safetensors(N)) |
|
local git_repos=("$OUTPUT_DIR/$NAME"/**/.git(N)) |
|
|
|
[[ -n "$DEBUG" ]] && { |
|
echo "\e[33mDEBUG\e[0m: Found ${#git_repos[@]} git repositories" |
|
echo "\e[33mDEBUG\e[0m: Found ${#samples[@]} sample files" |
|
echo "\e[33mDEBUG\e[0m: Found ${#models[@]} model files" |
|
} |
|
|
|
if [[ ${#samples[@]} -eq 0 && ${#models[@]} -eq 0 && ${#git_repos[@]} -eq 0 ]]; then |
|
if [[ -z "$NO_CLEAN" ]]; then |
|
echo "No samples or model files found, deleting empty output directory" |
|
rm -rf "$OUTPUT_DIR/$NAME" |
|
else |
|
echo "NO_CLEAN set, not deleting directory" |
|
fi |
|
else |
|
echo "Directory contains files, keeping it" |
|
fi |
|
} |
|
|