File size: 9,538 Bytes
d083607 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | #!/bin/bash
# =============================================================================
# vastai_deploy.sh - Deploy Stack 2.9 Training on Vast.ai
# =============================================================================
#
# USAGE:
# ./vastai_deploy.sh [--mode train|inference] [--config CONFIG] [--gpu GPU_NAME]
# ./vastai_deploy.sh [--list-gpus] [--ssh INSTANCE_ID]
#
# EXAMPLES:
# # Find and launch a training instance with A100 80GB
# ./vastai_deploy.sh --mode train --gpu A100-80
#
# # Launch inference on RTX 4090
# ./vastai_deploy.sh --mode inference --gpu RTX-4090
#
# # SSH into running instance
# ./vastai_deploy.sh --ssh 123456
#
# # List available GPU instances
# ./vastai_deploy.sh --list-gpus
#
# PREREQUISITES:
# - vastai CLI installed: pip install vastai
# - Vast.ai account with API key: vastai auth
# - SSH key configured: vastai create-key
# - HF_TOKEN set for gated models
#
# =============================================================================
set -euo pipefail
# ------------------------------ Defaults -------------------------------------
MODE="${MODE:-train}"
CONFIG_PATH="${CONFIG_PATH:-./stack_2_9_training/train_config.yaml}"
GPU_NAME="${GPU_NAME:-A100-80}"
MIN_VRAM_GB="${MIN_VRAM_GB:-40}"
MIN_DL_SPEED="${MIN_DL_SPEED:-800}" # MB/s
MIN_CPU="${MIN_CPU:-8}"
SSH_KEY="${SSH_KEY:-}" # Leave empty to auto-detect
REPO_URL="${REPO_URL:-https://github.com/walidsobhie-code/ai-voice-clone.git}"
REPO_BRANCH="${REPO_BRANCH:-main}"
LOG_FILE="${LOG_FILE:-~/vastai_stack29.log}"
INSTANCE_ID=""
# ------------------------------ Helpers --------------------------------------
usage() {
grep "^#" "$0" | sed 's/^# //;s/^#//'
exit 1
}
log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_FILE"; }
error() { log "ERROR: $*" >&2; exit 1; }
require_cmd() {
command -v "$1" &>/dev/null || error "Required command not found: $1"
}
# GPU name map: friendly -> vastai search string
declare -A GPU_SEARCH_MAP
GPU_SEARCH_MAP["A100-80"]="A100 80GB"
GPU_SEARCH_MAP["A100-40"]="A100 40GB"
GPU_SEARCH_MAP["H100"]="H100"
GPU_SEARCH_MAP["RTX-4090"]="RTX 4090"
GPU_SEARCH_MAP["RTX-3090"]="RTX 3090"
# ------------------------------ Parse Args ----------------------------------
while [[ $# -gt 0 ]]; do
case $1 in
--mode) MODE="$2"; shift 2 ;;
--config) CONFIG_PATH="$2"; shift 2 ;;
--gpu) GPU_NAME="$2"; shift 2 ;;
--ssh) INSTANCE_ID="$2"; shift 2 ;;
--list-gpus) LIST_GPUS=true; shift ;;
--help|-h) usage ;;
*) error "Unknown option: $1" ;;
esac
done
# --------------------------------- List GPUs ---------------------------------
if [[ "${LIST_GPUS:-false}" == "true" ]]; then
log "Fetching available GPU offers..."
vastai search instances "" --gpu "${GPU_SEARCH_MAP[$GPU_NAME]:-$GPU_NAME}" \
--order "dph_total" \
--num 20 2>/dev/null || vastai search offers "" 2>/dev/null
exit 0
fi
# --------------------------------- SSH into Instance ------------------------
if [[ -n "$INSTANCE_ID" ]]; then
log "Connecting to instance $INSTANCE_ID..."
ssh -o StrictHostKeyChecking=no "instance${INSTANCE_ID}@console.vast.ai"
exit 0
fi
# Validate mode
if [[ "$MODE" != "train" && "$MODE" != "inference" ]]; then
error "Mode must be 'train' or 'inference', got: $MODE"
fi
# ------------------------------ Prerequisites --------------------------------
log "Checking prerequisites..."
require_cmd vastai
# ------------------------------ Find Suitable Instance -----------------------
SEARCH_TERM="${GPU_SEARCH_MAP[$GPU_NAME]:-$GPU_NAME}"
log "Searching for GPU: $SEARCH_TERM (min VRAM: ${MIN_VRAM_GB}GB)..."
# Query available offers
# Using: vastai search offers <query>
OFFERS=$(vastai search offers "$SEARCH_TERM" 2>/dev/null || echo "")
if [[ -z "$OFFERS" ]]; then
error "No offers found for GPU: $GPU_NAME. Try --list-gpus to see available options."
fi
# Parse best offer (lowest price, meets requirements)
# Extract the first offer that meets VRAM requirements
BEST_OFFER=$(echo "$OFFERS" | awk -v min_vram="$MIN_VRAM_GB" '
/^[0-9]/ {
# Very rough parsing - in production use jq with vastai API
# This is a simplified heuristic
}
' | head -1)
# Simpler approach: use the CLI directly with filters
log "Finding best available instance..."
# Create instance with inline args
# See: https://docs.vast.ai/cli/#creating-an-instance
CREATE_CMD="vastai create instance \
--gpu \"$SEARCH_TERM\" \
--min-dl-speed $MIN_DL_SPEED \
--min-cpu-cores $MIN_CPU \
--onstart-url https://raw.githubusercontent.com/walidsobhie-code/ai-voice-clone/main/vastai_onstart.sh \
--image nvidia/cuda:12.1.0-runtime-ubuntu22.04 \
--force-yes"
log "Would run: $CREATE_CMD"
log ""
log "NOTE: Vast.ai interactive mode recommended. Run the following manually:"
log ""
log " # Search for available instances:"
log " vastai search offers \"${GPU_SEARCH_MAP[$GPU_NAME]:-$GPU_NAME}\""
log ""
log " # Launch an instance:"
log " vastai create instance \\"
log " --gpu ${GPU_SEARCH_MAP[$GPU_NAME]:-$GPU_NAME} \\"
log " --image nvidia/cuda:12.1.0-runtime-ubuntu22.04 \\"
log " --min-dl-speed $MIN_DL_SPEED \\"
log " --ssh-key $(ssh-add -L 2>/dev/null | cut -d' ' -f2 | head -1 || echo 'YOUR_SSH_KEY_ID')"
log ""
log " # Then SSH in and run training manually (see below)"
log ""
log " # Or use this script in interactive mode with TMUX:"
log " tmux new-session -d -s stack29 'bash'"
log ""
# ------------------------------ Training/Inference Script ---------------------
log "Creating deployment script for instance..."
DEPLOY_SCRIPT="/tmp/stack29_deploy.sh"
cat > "$DEPLOY_SCRIPT" << 'DEPLOY_EOF'
#!/bin/bash
set -euo pipefail
MODE="${1:-train}"
CONFIG_PATH="${2:-./stack_2_9_training/train_config.yaml}"
LOGFILE="/root/stack29_$(date +%Y%m%d_%H%M%S).log"
HF_TOKEN="${HF_TOKEN:-}"
log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOGFILE"; }
log "=== Stack 2.9 Deployment Started ==="
log "Mode: $MODE"
log "Config: $CONFIG_PATH"
log "Log: $LOGFILE"
log "Hostname: $(hostname)"
log "GPU: $(nvidia-smi --query-gpu=name,memory.total --format=csv 2>/dev/null || echo 'nvidia-smi not found')"
log ""
# ---- Env setup ----
export HF_TOKEN="${HF_TOKEN}"
export PYTORCH_CUDA_ALLOC_CONF="max_split_size_mb=512"
export TRANSFORMERS_CACHE="/data/hf_cache"
export HF_HOME="/data/hf_cache"
export CUDA_VISIBLE_DEVICES="0"
mkdir -p /data/hf_cache /data/outputs /data/adapters
# ---- Install deps ----
log "Installing system packages..."
apt-get update -qq && apt-get install -y -qq \
git curl wget build-essential libsndfile1 ffmpeg \
2>&1 | tail -3
log "Installing Python packages..."
pip install --upgrade pip -q
pip install -q \
torch \
transformers \
peft \
accelerate \
bitsandbytes \
datasets \
trl \
scipy \
soundfile \
librosa \
pyyaml \
tqdm \
gradio \
fastapi \
uvicorn \
2>&1 | tail -5
# ---- Clone repo ----
log "Cloning repository..."
cd /data
if [[ ! -d "ai-voice-clone" ]]; then
git clone --depth 1 -b main https://github.com/walidsobhie-code/ai-voice-clone.git ai-voice-clone
fi
cd ai-voice-clone
# Copy config if custom
if [[ "$CONFIG_PATH" != "./stack_2_9_training/train_config.yaml" ]]; then
cp "$CONFIG_PATH" ./stack_2_9_training/train_config.yaml
fi
log "Repository ready. Starting application..."
# ---- Start Training or Inference ----
if [[ "$MODE" == "train" ]]; then
log "Starting LoRA training..."
log "Command: python -m stack_2_9_training.train_lora --config ./stack_2_9_training/train_config.yaml"
python -m stack_2_9_training.train_lora \
--config ./stack_2_9_training/train_config.yaml \
2>&1 | tee -a "$LOGFILE"
else
log "Starting inference server..."
log "Command: python -m uvicorn stack.serve:app --host 0.0.0.0 --port 7860"
python -m uvicorn \
stack.serve:app \
--host 0.0.0.0 \
--port 7860 \
2>&1 | tee -a "$LOGFILE"
fi
DEPLOY_EOF
chmod +x "$DEPLOY_SCRIPT"
log "Deploy script written to: $DEPLOY_SCRIPT"
log "Contents will be transferred to the instance on creation."
# ------------------------------ Full Create Instructions ---------------------
log ""
log "=== Full Vast.ai Deployment Instructions ==="
log ""
log "1. Find a suitable instance:"
log " vastai search offers \"${GPU_SEARCH_MAP[$GPU_NAME]:-$GPU_NAME}\""
log ""
log "2. Create the instance (note the offer ID from step 1):"
log " vastai create instance --offer-id <id> \\"
log " --image nvidia/cuda:12.1.0-devel-ubuntu22.04 \\"
log " --ssh-key <your-ssh-key> \\"
log " --onstart-url https://raw.githubusercontent.com/walidsobhie-code/ai-voice-clone/main/vastai_onstart.sh \\"
log " --onstart-cmd '$MODE /data/ai-voice-clone/stack_2_9_training/train_config.yaml'"
log ""
log "3. SSH into the instance after it starts:"
log " vastai ssh <instance-id>"
log ""
log "4. Or use screen/tmux for persistent sessions:"
log " screen -S stack29"
log " bash /tmp/stack29_deploy.sh $MODE $CONFIG_PATH"
log " # Ctrl+A D to detach"
log ""
log "5. Monitor training:"
log " tail -f $LOGFILE"
log " nvidia-smi -l 1"
log ""
log "=== Clean Shutdown ==="
log "To stop training gracefully:"
log " # Find the process"
log " ps aux | grep train_lora"
log " # Send SIGTERM for graceful shutdown"
log " kill -SIGTERM <pid>"
log ""
log "To stop and destroy the instance:"
log " vastai destroy instance <instance-id>"
|