nvidia-smi shows the devices by PCI order, while the CUDA driver and Runtime API should sort the devices based on their “performance”.
To get the same order in your PyTorch script, you could try to set
export CUDA_DEVICE_ORDER=PCI_BUS_ID

before running your script.