|
| 1 | +################################################################################ |
| 2 | +# AiDotNet Distributed Training Launcher (PowerShell) |
| 3 | +# |
| 4 | +# This script launches distributed training using MPI across multiple processes. |
| 5 | +# |
| 6 | +# For Beginners: |
| 7 | +# MPI (Message Passing Interface) is a standard for running programs across |
| 8 | +# multiple computers or processors. Think of it like a coordinator that starts |
| 9 | +# your program on multiple machines at once and helps them communicate. |
| 10 | +# |
| 11 | +# Usage: |
| 12 | +# .\launch-distributed-training.ps1 -NumProcesses <num> -Program <path> [additional args...] |
| 13 | +# |
| 14 | +# Examples: |
| 15 | +# # Run on 4 GPUs locally |
| 16 | +# .\launch-distributed-training.ps1 -NumProcesses 4 -Program ".\MyTrainingApp.exe" |
| 17 | +# |
| 18 | +# # Run on 8 GPUs with additional arguments (note: args after Program are passed to the app) |
| 19 | +# .\launch-distributed-training.ps1 -NumProcesses 8 -Program ".\MyTrainingApp.exe" -- --epochs 100 --lr 0.001 |
| 20 | +# |
| 21 | +# # Run with config file containing spaces in path (use -- separator) |
| 22 | +# .\launch-distributed-training.ps1 -NumProcesses 8 -Program ".\MyTrainingApp.exe" -- --config "My Config.json" |
| 23 | +# |
| 24 | +# # Run across 2 machines with 4 GPUs each |
| 25 | +# .\launch-distributed-training.ps1 -NumProcesses 8 -Program ".\MyTrainingApp.exe" -Hosts "machine1,machine2" |
| 26 | +################################################################################ |
| 27 | + |
| 28 | +param( |
| 29 | + [Parameter(Mandatory=$true, HelpMessage="Number of processes to spawn (typically equals number of GPUs)")] |
| 30 | + [int]$NumProcesses, |
| 31 | + |
| 32 | + [Parameter(Mandatory=$true, HelpMessage="Path to your training program executable")] |
| 33 | + [string]$Program, |
| 34 | + |
| 35 | + [Parameter(Mandatory=$false, HelpMessage="Comma-separated list of host machines")] |
| 36 | + [string]$Hosts = "", |
| 37 | + |
| 38 | + [Parameter( |
| 39 | + Mandatory = $false, |
| 40 | + HelpMessage = "Additional arguments to pass to your program", |
| 41 | + ValueFromRemainingArguments = $true)] |
| 42 | + [string[]]$ProgramArgs = @() |
| 43 | +) |
| 44 | + |
| 45 | +# Display header |
| 46 | +Write-Host "======================================" -ForegroundColor Cyan |
| 47 | +Write-Host "AiDotNet Distributed Training Launcher" -ForegroundColor Cyan |
| 48 | +Write-Host "======================================" -ForegroundColor Cyan |
| 49 | +Write-Host "" |
| 50 | + |
| 51 | +# Display configuration |
| 52 | +Write-Host "Configuration:" -ForegroundColor Yellow |
| 53 | +Write-Host " Number of processes: $NumProcesses" |
| 54 | +Write-Host " Program: $Program" |
| 55 | +if ($ProgramArgs.Count -gt 0) { |
| 56 | + Write-Host " Program arguments: $($ProgramArgs -join ' ')" |
| 57 | +} |
| 58 | +if ($Hosts) { |
| 59 | + Write-Host " Hosts: $Hosts" |
| 60 | +} |
| 61 | +Write-Host "" |
| 62 | + |
| 63 | +# Check if mpiexec is available |
| 64 | +$mpiexec = Get-Command mpiexec -ErrorAction SilentlyContinue |
| 65 | + |
| 66 | +if (-not $mpiexec) { |
| 67 | + Write-Host "Error: mpiexec not found in PATH" -ForegroundColor Red |
| 68 | + Write-Host "" |
| 69 | + Write-Host "For Beginners:" -ForegroundColor Yellow |
| 70 | + Write-Host " You need to install Microsoft MPI to run distributed training on Windows." |
| 71 | + Write-Host " Download from: https://docs.microsoft.com/en-us/message-passing-interface/microsoft-mpi" |
| 72 | + Write-Host "" |
| 73 | + Write-Host " Installation steps:" |
| 74 | + Write-Host " 1. Download MS-MPI installer" |
| 75 | + Write-Host " 2. Install both the runtime (msmpisetup.exe) and SDK (msmpisdk.msi)" |
| 76 | + Write-Host " 3. Restart your terminal/PowerShell" |
| 77 | + exit 1 |
| 78 | +} |
| 79 | + |
| 80 | +Write-Host "Using MPI command: $($mpiexec.Source)" -ForegroundColor Green |
| 81 | +Write-Host "" |
| 82 | + |
| 83 | +# Check if program exists |
| 84 | +if (-not (Test-Path $Program)) { |
| 85 | + Write-Host "Error: Program '$Program' not found" -ForegroundColor Red |
| 86 | + Write-Host "" |
| 87 | + Write-Host "For Beginners:" -ForegroundColor Yellow |
| 88 | + Write-Host " Make sure you've built your training program and the path is correct." |
| 89 | + Write-Host " Example: dotnet publish -c Release -o .\publish" |
| 90 | + Write-Host " Then use: -Program '.\publish\MyTrainingApp.exe'" |
| 91 | + exit 1 |
| 92 | +} |
| 93 | + |
| 94 | +# Security: Validate that Program is an executable file |
| 95 | +$ProgramItem = Get-Item -Path $Program -ErrorAction Stop |
| 96 | +$allowedExtensions = @('.exe', '.dll') |
| 97 | +if ($ProgramItem.Extension -notin $allowedExtensions) { |
| 98 | + Write-Host "Error: Program must be an executable (.exe) or .NET assembly (.dll)" -ForegroundColor Red |
| 99 | + Write-Host " Received: $($ProgramItem.Extension)" -ForegroundColor Red |
| 100 | + Write-Host "" |
| 101 | + Write-Host "Security Note:" -ForegroundColor Yellow |
| 102 | + Write-Host " Only executable files (.exe) and .NET assemblies (.dll) are allowed" |
| 103 | + Write-Host " to prevent execution of potentially malicious scripts or documents." |
| 104 | + exit 1 |
| 105 | +} |
| 106 | + |
| 107 | +# Security: Resolve to absolute path to prevent path traversal attacks |
| 108 | +$Program = $ProgramItem.FullName |
| 109 | +Write-Host "Resolved program path: $Program" -ForegroundColor Green |
| 110 | +Write-Host "" |
| 111 | + |
| 112 | +# Build mpiexec command |
| 113 | +$mpiCommand = "mpiexec" |
| 114 | +$mpiArgsList = @( |
| 115 | + "-n", $NumProcesses.ToString() |
| 116 | +) |
| 117 | + |
| 118 | +# Add hosts if specified |
| 119 | +if ($Hosts) { |
| 120 | + $mpiArgsList += @("-hosts", $Hosts) |
| 121 | +} |
| 122 | + |
| 123 | +# Add the program |
| 124 | +$mpiArgsList += $Program |
| 125 | + |
| 126 | +# Add program arguments if specified |
| 127 | +if ($ProgramArgs.Count -gt 0) { |
| 128 | + $mpiArgsList += $ProgramArgs |
| 129 | +} |
| 130 | + |
| 131 | +# Display command |
| 132 | +Write-Host "Launching distributed training..." -ForegroundColor Yellow |
| 133 | +Write-Host "Command: $mpiCommand $($mpiArgsList -join ' ')" -ForegroundColor Gray |
| 134 | +Write-Host "" |
| 135 | +Write-Host "======================================" -ForegroundColor Cyan |
| 136 | +Write-Host "" |
| 137 | + |
| 138 | +# Launch distributed training |
| 139 | +try { |
| 140 | + # Use Start-Process to capture output and wait for completion |
| 141 | + $process = Start-Process -FilePath $mpiCommand -ArgumentList $mpiArgsList -NoNewWindow -Wait -PassThru |
| 142 | + $exitCode = $process.ExitCode |
| 143 | +} |
| 144 | +catch { |
| 145 | + Write-Host "" |
| 146 | + Write-Host "======================================" -ForegroundColor Cyan |
| 147 | + Write-Host "Error launching training: $_" -ForegroundColor Red |
| 148 | + Write-Host "======================================" -ForegroundColor Cyan |
| 149 | + exit 1 |
| 150 | +} |
| 151 | + |
| 152 | +# Display results |
| 153 | +Write-Host "" |
| 154 | +Write-Host "======================================" -ForegroundColor Cyan |
| 155 | +if ($exitCode -eq 0) { |
| 156 | + Write-Host "Training completed successfully!" -ForegroundColor Green |
| 157 | +} |
| 158 | +else { |
| 159 | + Write-Host "Training failed with exit code: $exitCode" -ForegroundColor Red |
| 160 | + Write-Host "" |
| 161 | + Write-Host "Common issues:" -ForegroundColor Yellow |
| 162 | + Write-Host " - Make sure all nodes can communicate (check firewalls)" |
| 163 | + Write-Host " - Verify MS-MPI is installed on all machines" |
| 164 | + Write-Host " - Check that the program path is correct on all machines" |
| 165 | + Write-Host " - Ensure sufficient GPU memory is available" |
| 166 | + Write-Host " - Try running with fewer processes to check for memory issues" |
| 167 | +} |
| 168 | +Write-Host "======================================" -ForegroundColor Cyan |
| 169 | + |
| 170 | +exit $exitCode |
0 commit comments