I find the solution by the help with @ngimel 1. This is the solution learned from her.

def demo_basic(rank, world_size):

    print(f"Running basic DDP example on rank {rank}.")

    dist.init_process_group("nccl", rank=rank, world_size=world_size)

    torch.cuda.set_device(rank)

    ToyModel = nn.GRU(10, 10, 1)

    model = ToyModel.cuda()

    ddp_model = DDP(model, device_ids=[rank])

    pbar_len = int(1e10 / 2)

    for _ in range(pbar_len):

        input_seq = torch.randn(4, 20,10)

        input_seq = input_seq.float().cuda()

        ddp_model(input_seq)

    dist.destroy_process_group()