Download scripts/train_gpu.sh from NabuxAi/buddi-asr-source-code: direct link, hf CLI and curl.
- Browser
- Download file 11.2 kB
-
https://huggingface.co/datasets/NabuxAi/buddi-asr-source-code/resolve/main/scripts/train_gpu.sh
- Command line
-
hf download hf://datasets/NabuxAi/buddi-asr-source-code/scripts/train_gpu.sh
-
curl -L -o train_gpu.sh https://huggingface.co/datasets/NabuxAi/buddi-asr-source-code/resolve/main/scripts/train_gpu.sh
11.2 kB
| set -Eeuo pipefail | |
| script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
| project_root="$(cd "$script_dir/.." && pwd)" | |
| cd "$project_root" | |
| usage() { | |
| cat <<'EOF' | |
| Usage: ./scripts/train_gpu.sh [PROFILE] [OPTIONS] | |
| Profiles: | |
| tiny-lora (default) | tiny-full | base-lora | base-full | |
| Options: | |
| --prepare-data auto|yes|no Download/prepare Abjad-Kids (default: auto) | |
| --num-gpus N Number of local CUDA GPUs (default: 1) | |
| --precision auto|fp16|bf16 Mixed precision (default: auto) | |
| --train-batch-size N Override auto-sized per-GPU train batch | |
| --eval-batch-size N Override auto-sized per-GPU eval batch | |
| --gradient-accumulation N Override accumulation chosen for effective batch ~32 | |
| --workers N Audio-loader processes (default: 4) | |
| --resume auto|none|PATH Resume policy (default: auto) | |
| --output-dir PATH Override artifact directory | |
| --smoke Run 20 optimizer steps in a separate output directory | |
| --evaluate-test Evaluate the held-out test set after training | |
| --skip-install Use the already-active Python environment | |
| --prepare-only Build the manifests and splits, then stop (no GPU needed) | |
| --torch-index-url URL Install torch from a PyTorch wheel index before extras | |
| -h, --help Show this help | |
| Environment: | |
| BUDDI_VENV, BUDDI_PYTHON, BUDDI_SALT_FILE, CUDA_VISIBLE_DEVICES, | |
| HF_HOME, HF_TOKEN, BUDDI_TORCH_INDEX_URL | |
| EOF | |
| } | |
| profile="tiny-lora" | |
| if [[ $# -gt 0 && "$1" != --* && "$1" != "-h" ]]; then | |
| profile="$1" | |
| shift | |
| fi | |
| prepare_data="auto" | |
| num_gpus=1 | |
| precision="auto" | |
| train_batch="" | |
| eval_batch="" | |
| gradient_accumulation="" | |
| workers=4 | |
| resume="auto" | |
| output_dir="" | |
| smoke=0 | |
| evaluate_test=0 | |
| skip_install=0 | |
| prepare_only=0 | |
| torch_index_url="${BUDDI_TORCH_INDEX_URL:-}" | |
| while [[ $# -gt 0 ]]; do | |
| case "$1" in | |
| --prepare-data) prepare_data="${2:?missing value}"; shift 2 ;; | |
| --num-gpus) num_gpus="${2:?missing value}"; shift 2 ;; | |
| --precision) precision="${2:?missing value}"; shift 2 ;; | |
| --train-batch-size) train_batch="${2:?missing value}"; shift 2 ;; | |
| --eval-batch-size) eval_batch="${2:?missing value}"; shift 2 ;; | |
| --gradient-accumulation) gradient_accumulation="${2:?missing value}"; shift 2 ;; | |
| --workers) workers="${2:?missing value}"; shift 2 ;; | |
| --resume) resume="${2:?missing value}"; shift 2 ;; | |
| --output-dir) output_dir="${2:?missing value}"; shift 2 ;; | |
| --torch-index-url) torch_index_url="${2:?missing value}"; shift 2 ;; | |
| --smoke) smoke=1; shift ;; | |
| --evaluate-test) evaluate_test=1; shift ;; | |
| --skip-install) skip_install=1; shift ;; | |
| --prepare-only) prepare_only=1; shift ;; | |
| -h|--help) usage; exit 0 ;; | |
| *) echo "Unknown option: $1" >&2; usage >&2; exit 2 ;; | |
| esac | |
| done | |
| case "$profile" in | |
| tiny-lora) config="configs/train_tiny_lora.json" ;; | |
| tiny-full) config="configs/train_tiny_full.json" ;; | |
| base-lora) config="configs/train_base_lora.json" ;; | |
| base-full) config="configs/train_base_full.json" ;; | |
| *) echo "Unknown profile: $profile" >&2; usage >&2; exit 2 ;; | |
| esac | |
| case "$prepare_data" in auto|yes|no) ;; *) echo "Invalid --prepare-data" >&2; exit 2 ;; esac | |
| case "$precision" in auto|fp16|bf16) ;; *) echo "Invalid --precision" >&2; exit 2 ;; esac | |
| for value in "$num_gpus" "$workers"; do | |
| [[ "$value" =~ ^[0-9]+$ ]] || { echo "Numeric options must be integers" >&2; exit 2; } | |
| done | |
| [[ "$num_gpus" -gt 0 ]] || { echo "--num-gpus must be positive" >&2; exit 2; } | |
| if [[ -n "${CUDA_VISIBLE_DEVICES:-}" ]]; then | |
| IFS=',' read -r -a visible_devices <<< "$CUDA_VISIBLE_DEVICES" | |
| if [[ "$num_gpus" -gt "${#visible_devices[@]}" ]]; then | |
| echo "--num-gpus exceeds CUDA_VISIBLE_DEVICES=$CUDA_VISIBLE_DEVICES" >&2 | |
| exit 2 | |
| fi | |
| selected_devices="${visible_devices[0]}" | |
| for ((index = 1; index < num_gpus; index++)); do | |
| selected_devices+=",${visible_devices[index]}" | |
| done | |
| export CUDA_VISIBLE_DEVICES="$selected_devices" | |
| else | |
| selected_devices="0" | |
| for ((index = 1; index < num_gpus; index++)); do | |
| selected_devices+=",$index" | |
| done | |
| export CUDA_VISIBLE_DEVICES="$selected_devices" | |
| fi | |
| python_seed="${BUDDI_PYTHON:-python3}" | |
| # Call the package as a module rather than through its console script. `pip | |
| # install` puts that script wherever the environment's bin directory happens to | |
| # be, and on Colab that is not on the PATH the notebook's shell inherits -- the | |
| # install succeeds and every later command dies with "buddi-asr: command not | |
| # found". `python -m` only needs the package to be importable. | |
| buddi=(python -m buddi_asr.cli) | |
| venv_dir="${BUDDI_VENV:-$project_root/.venv-gpu}" | |
| if [[ "$skip_install" -eq 0 ]]; then | |
| if [[ ! -f "$venv_dir/bin/activate" ]]; then | |
| # A directory holding only a pyvenv.cfg -- a stray one from a cloud run that | |
| # reached version control, or a half-written venv from an interrupted job -- | |
| # makes `python -m venv` fail inside ensurepip instead of rebuilding it. | |
| # Clear the shell before creating one, so a broken tree self-heals. | |
| if [[ -e "$venv_dir" ]]; then | |
| echo "Removing an incomplete virtualenv at $venv_dir" | |
| rm -rf "$venv_dir" | |
| fi | |
| "$python_seed" -m venv --system-site-packages "$venv_dir" | |
| fi | |
| # shellcheck disable=SC1091 | |
| source "$venv_dir/bin/activate" | |
| python -m pip install --upgrade pip setuptools wheel | |
| if [[ -n "$torch_index_url" ]]; then | |
| python -m pip install --index-url "$torch_index_url" 'torch>=2.4,<3' | |
| fi | |
| python -m pip install -e '.[data,train]' | |
| elif [[ -f "$venv_dir/bin/activate" ]]; then | |
| # shellcheck disable=SC1091 | |
| source "$venv_dir/bin/activate" | |
| fi | |
| if [[ "$prepare_only" -eq 0 ]]; then | |
| python - <<'PY' | |
| import torch | |
| if not torch.cuda.is_available(): | |
| raise SystemExit( | |
| "CUDA is not visible to PyTorch. Start from an NVIDIA PyTorch image or pass " | |
| "--torch-index-url with the wheel index selected at pytorch.org/get-started/locally/." | |
| ) | |
| print(f"PyTorch {torch.__version__}; CUDA runtime {torch.version.cuda}") | |
| for index in range(torch.cuda.device_count()): | |
| props = torch.cuda.get_device_properties(index) | |
| print(f"GPU {index}: {props.name}; VRAM {props.total_memory / 1024**3:.2f} GiB") | |
| PY | |
| detected_gpus="$(python -c 'import torch; print(torch.cuda.device_count())')" | |
| if [[ "$num_gpus" -gt "$detected_gpus" ]]; then | |
| echo "Requested $num_gpus GPU(s), but PyTorch sees $detected_gpus" >&2 | |
| exit 2 | |
| fi | |
| if [[ "$precision" == "auto" ]]; then | |
| precision="$(python -c 'import torch; print("bf16" if torch.cuda.is_bf16_supported() else "fp16")')" | |
| fi | |
| vram_mib="$(python -c 'import sys, torch; count=int(sys.argv[1]); print(min(torch.cuda.get_device_properties(i).total_memory for i in range(count)) // 1024**2)' "$num_gpus")" | |
| if [[ -z "$train_batch" ]]; then | |
| case "$profile" in | |
| tiny-lora) | |
| if (( vram_mib < 6000 )); then train_batch=2 | |
| elif (( vram_mib < 10000 )); then train_batch=4 | |
| elif (( vram_mib < 16000 )); then train_batch=8 | |
| else train_batch=16; fi ;; | |
| tiny-full|base-lora) | |
| if (( vram_mib < 6000 )); then train_batch=1 | |
| elif (( vram_mib < 10000 )); then train_batch=2 | |
| elif (( vram_mib < 16000 )); then train_batch=4 | |
| else train_batch=8; fi ;; | |
| base-full) | |
| if (( vram_mib < 10000 )); then train_batch=1 | |
| elif (( vram_mib < 16000 )); then train_batch=2 | |
| elif (( vram_mib < 24000 )); then train_batch=4 | |
| else train_batch=8; fi ;; | |
| esac | |
| fi | |
| [[ "$train_batch" =~ ^[1-9][0-9]*$ ]] || { echo "Invalid train batch size" >&2; exit 2; } | |
| if [[ -z "$eval_batch" ]]; then | |
| eval_batch=$(( train_batch > 1 ? train_batch / 2 : 1 )) | |
| fi | |
| [[ "$eval_batch" =~ ^[1-9][0-9]*$ ]] || { echo "Invalid eval batch size" >&2; exit 2; } | |
| if [[ -z "$gradient_accumulation" ]]; then | |
| batch_across_gpus=$(( train_batch * num_gpus )) | |
| gradient_accumulation=$(( (32 + batch_across_gpus - 1) / batch_across_gpus )) | |
| fi | |
| [[ "$gradient_accumulation" =~ ^[1-9][0-9]*$ ]] || { | |
| echo "Invalid gradient accumulation" >&2 | |
| exit 2 | |
| } | |
| fi | |
| train_manifest="data/manifests/splits/train.jsonl" | |
| validation_manifest="data/manifests/splits/validation.jsonl" | |
| test_manifest="data/manifests/splits/test.jsonl" | |
| if [[ "$prepare_data" == "auto" ]]; then | |
| if [[ -f "$train_manifest" && -f "$validation_manifest" && -f "$test_manifest" ]]; then | |
| prepare_data="no" | |
| else | |
| prepare_data="yes" | |
| fi | |
| fi | |
| mkdir -p reports artifacts/logs | |
| log_file="artifacts/logs/${profile}-$(date -u +%Y%m%dT%H%M%SZ).log" | |
| exec > >(tee -a "$log_file") 2>&1 | |
| if [[ "$prepare_data" == "yes" ]]; then | |
| salt_file="${BUDDI_SALT_FILE:-$project_root/.buddi-speaker.salt}" | |
| if [[ ! -s "$salt_file" ]]; then | |
| umask 077 | |
| python -c 'import secrets; print(secrets.token_hex(32))' > "$salt_file" | |
| fi | |
| "${buddi[@]}" download-abjad data/raw/abjad-kids --skip-if-present | |
| "${buddi[@]}" prepare-abjad \ | |
| data/raw/abjad-kids \ | |
| data/manifests/abjad.jsonl \ | |
| --processed-audio-dir data/processed/abjad \ | |
| --audio-mode hardlink \ | |
| --salt-file "$salt_file" \ | |
| --audit-output reports/abjad-audit.json | |
| "${buddi[@]}" validate \ | |
| data/manifests/abjad.jsonl \ | |
| --report reports/abjad-validation.json | |
| "${buddi[@]}" split data/manifests/abjad.jsonl data/manifests/splits --seed 42 | |
| fi | |
| for manifest in "$train_manifest" "$validation_manifest" "$test_manifest"; do | |
| [[ -f "$manifest" ]] || { | |
| echo "Missing $manifest. Use --prepare-data yes or provide your prepared split." >&2 | |
| exit 2 | |
| } | |
| done | |
| if [[ "$prepare_only" -eq 1 ]]; then | |
| echo "Manifests ready:" | |
| for manifest in "$train_manifest" "$validation_manifest" "$test_manifest"; do | |
| echo " $manifest ($(wc -l < "$manifest") records)" | |
| done | |
| exit 0 | |
| fi | |
| if [[ -z "$output_dir" ]]; then | |
| if [[ "$smoke" -eq 1 ]]; then | |
| output_dir="artifacts/smoke-${profile}" | |
| else | |
| output_dir="artifacts/${profile}" | |
| fi | |
| fi | |
| common_args=( | |
| "$config" | |
| --output-dir "$output_dir" | |
| --resume "$resume" | |
| --precision "$precision" | |
| --train-batch-size "$train_batch" | |
| --eval-batch-size "$eval_batch" | |
| --gradient-accumulation-steps "$gradient_accumulation" | |
| --dataloader-workers "$workers" | |
| ) | |
| if [[ "$smoke" -eq 1 ]]; then | |
| common_args+=(--max-steps 20 --logging-steps 1 --eval-steps 10 --save-steps 10) | |
| fi | |
| echo "Profile: $profile" | |
| echo "CUDA devices: $CUDA_VISIBLE_DEVICES" | |
| echo "Precision: $precision" | |
| echo "Per-GPU train/eval batch: $train_batch/$eval_batch" | |
| echo "Gradient accumulation: $gradient_accumulation" | |
| echo "Output: $output_dir" | |
| echo "Log: $log_file" | |
| "${buddi[@]}" preflight-gpu "${common_args[@]}" --test-manifest "$test_manifest" | |
| if [[ "$num_gpus" -eq 1 ]]; then | |
| "${buddi[@]}" train "${common_args[@]}" | |
| else | |
| accelerate launch \ | |
| --multi_gpu \ | |
| --num_processes "$num_gpus" \ | |
| -m buddi_asr.cli train "${common_args[@]}" | |
| fi | |
| if [[ "$evaluate_test" -eq 1 ]]; then | |
| "${buddi[@]}" evaluate \ | |
| "$output_dir/final" \ | |
| "$test_manifest" \ | |
| "reports/${profile}-test" \ | |
| --device cuda | |
| else | |
| echo "Held-out test was not touched. After freezing the candidate, run:" | |
| echo "buddi-asr evaluate '$output_dir/final' '$test_manifest' 'reports/${profile}-test' --device cuda" | |
| fi | |
| echo "Training finished. Final checkpoint: $output_dir/final" | |