You need to insert proper synchronization, because the GPU runs asynchronously (unless you e.g. launch a CPU <-> GPU copy). There should be a few posts on this forums that should how to do it (search for torch.cuda.synchronize()).