I was testing cross-platform resume for Efficio (an open-source PyTorch training library): start a run on Kaggle's 2x T4 GPUs, finish it on Colab's single T4. Training resumed fine, right up until it tried to restore the random number generator state. Then it crashed with an IndexError.
Here's what was going on, and the fix.
The setup
Efficio checkpoints save CUDA's RNG state so a resumed run is numerically consistent, not just "roughly where you left off." That means saving torch.cuda.get_rng_state_all(), which returns one RNG state per GPU that was active when the checkpoint was written.
The restore side used torch.cuda.set_rng_state_all() to put them back. That function expects a list with exactly as many entries as GPUs available right now. My checkpoint had 2 entries (from Kaggle's 2 GPUs). Colab had 1. Index out of range.
Why this is an easy bug to miss
Most resume testing happens on the same machine, or at least the same GPU count. Nothing about a single-GPU test run would ever expose this — it only shows up when the GPU count actually changes between where you checkpoint and where you resume, which for Efficio's whole pitch (train on whatever free GPU you can get right now) is a completely normal thing to happen.
The fix
Instead of handing the whole saved state list to set_rng_state_all, restore device-by-device with set_rng_state, and only restore as many as actually exist now:
def _restore_cuda_rng_state(saved_states): available = torch.cuda.device_count() for i, state in enumerate(saved_states[:available]): torch.cuda.set_rng_state(state, device=i) saved_states[:available] is the whole fix. If you checkpointed with 2 GPUs and resume with 1, you restore GPU 0's state and move on, instead of crashing on an index that doesn't exist. If you resume with more GPUs than you saved with, the extras just keep whatever RNG state they already had, which is fine. I added a test that checkpoints with a mocked 2-GPU count and resumes with 1, 2 Mand 2 GPUs to make sure all three directions work, and verified the 2→1 direction for real, on actual Kaggle and Colab hardware.
The broader point
A function named set_rng_state_all sounds like exactly what you want when you're restoring "all the RNG state." But "all" implicitly assumes the same "all" you started with. Any time a checkpoint/restore API takes a list sized to the current environment, changing the environment between save and load is worth testing explicitly, not just assuming it degrades gracefully.
Efficio is at github.com/HuNTer8272/Efficio, pip install efficio to try it.



