Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Automatic GPU Switch #845

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions gpu-switch.sh
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's really handy man)

Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,24 @@ convert_to_pci_address() {
local device="$1"
local gpu_address=""

if [[ "$device" =~ ^[0-9]+$ ]]; then
# Convert GPU index to PCI address
gpu_address=$(nvidia-smi --id=$device --query-gpu=gpu_bus_id --format=csv,noheader 2>/dev/null | tr -d '[:space:]')
elif [[ "$device" =~ ^GPU-.*$ ]]; then
# Handle UUID
gpu_address=$(nvidia-smi --id=$device --query-gpu=gpu_bus_id --format=csv,noheader 2>/dev/null | tr -d '[:space:]')
if [[ "$device" =~ ^[0-9]+$ || "$device" =~ ^GPU-.*$ ]]; then
# Convert GPU index or UUID to PCI address
gpu_address=$(nvidia-smi --id="$device" --query-gpu=gpu_bus_id --format=csv,noheader 2>/dev/null | tr -d '[:space:]')
else
# Direct PCI address provided
gpu_address=$device
gpu_address="$device"
fi

# Check for valid output
if [ -z "$gpu_address" ]; then
error_exit "Failed to get PCI address for device: $device"
fi

# Standardize format
echo "$gpu_address" | sed 's/0000://' | sed 's/\./:/g'
echo "$gpu_address" | sed -e 's/0000://' -e 's/\./:/g'
}


get_gpu_addresses() {
# Split devices by comma
IFS=',' read -ra DEVICES <<< "$NVIDIA_VISIBLE_DEVICES"
Expand Down