-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e79c04
commit fa56980
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/bin/bash | ||
|
||
# | ||
# simulate multi nodes on one gpu. start N torchrun on X gpu locally. | ||
# example how to run ./scripts/simulate_multi_node.sh 2 1 src/zeroband/train.py @configs/debug/normal.toml | ||
|
||
# Function to get CUDA devices based on the number of GPUs and index | ||
function get_cuda_devices() { | ||
local num_gpu=$1 | ||
local index=$2 | ||
local start_gpu=$((num_gpu * index)) | ||
local end_gpu=$((start_gpu + num_gpu - 1)) | ||
|
||
if [ "$num_gpu" -eq 1 ]; then | ||
echo $start_gpu | ||
else | ||
echo $(seq -s ',' $start_gpu $end_gpu) | ||
fi | ||
} | ||
|
||
# Array to store PIDs of child processes | ||
child_pids=() | ||
|
||
# Function to kill all child processes | ||
cleanup() { | ||
echo "Cleaning up child processes..." | ||
local killed=0 | ||
for pid in "${child_pids[@]}"; do | ||
if kill -TERM "$pid" 2>/dev/null; then | ||
((killed++)) | ||
fi | ||
done | ||
wait | ||
echo "All child processes terminated. Killed $killed processes." | ||
exit | ||
} | ||
|
||
# Check if at least three arguments were passed | ||
if [ "$#" -lt 3 ]; then | ||
echo "Usage: $0 <N> <initial_peer> <num_gpu> [additional_python_args]" | ||
exit 1 | ||
fi | ||
|
||
|
||
N=$1 # Set N from the first argument | ||
NUM_GPU=$2 | ||
shift 2 # Remove the first three arguments so $@ contains only additional Python arguments | ||
|
||
# Register the cleanup function to be called on SIGINT (Ctrl+C) | ||
trap cleanup SIGINT | ||
|
||
|
||
mkdir -p logs | ||
|
||
export GLOBAL_ADDR=localhost | ||
export GLOBAL_PORT=10000 | ||
export GLOBAL_WORLD_SIZE=$N | ||
|
||
for i in $(seq 0 $(($N - 1 ))) | ||
do | ||
> logs/log$i | ||
TORCH_UNIQUE_ID=$i GLOBAL_RANK=$i CUDA_VISIBLE_DEVICES=$(get_cuda_devices $NUM_GPU $i) uv run torchrun --nproc_per_node=$NUM_GPU --node-rank 0 --rdzv-endpoint localhost:$((10001 + $i)) --nnodes=1 $@ > logs/log$i 2>&1 & | ||
child_pids+=($!) | ||
done | ||
|
||
tail -f logs/log0 & | ||
child_pids+=($!) | ||
|
||
wait |