Skip to content
New issue

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

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

Already on GitHub? # to your account

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! #27

Open
anasAloklah opened this issue Jan 23, 2021 · 7 comments

Comments

@anasAloklah
Copy link

anasAloklah commented Jan 23, 2021

File "E:\Transformer-master\Batch.py", line 26, in create_masks
trg_mask = trg_mask & np_mask
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

In the past, he worked with me without problems, but after I format the device and download the programs and offices again,
this problem happened to me and I do not know the reason.

@congson1293
Copy link

congson1293 commented Jan 28, 2021

This error raised because you ran this project on GPU but some tensor ran on CPU.

@anasAloklah
Copy link
Author

anasAloklah commented Jan 31, 2021

I know what is the problem I want to found a solution.
I try to move the trg_mask to GPU
but I have another problem the input and output must in the same device.
To solve this problem you have must move trg_mask to CPU again after the operation.
but not moved again I don't know why.
I installed old ver of Pytorch but is still the same problem

@congson1293
Copy link

congson1293 commented Feb 1, 2021

def create_masks(src, trg, opt):

    src_mask = (src != opt.src_pad).unsqueeze(-2).to(opt.device)

    if trg is not None:
        trg_mask = (trg != opt.trg_pad).unsqueeze(-2).to(opt.device)
        size = trg.size(1) # get seq_len for matrix
        np_mask = nopeak_mask(size, opt).to(opt.device)
        trg_mask = trg_mask & np_mask
        
    else:
        trg_mask = None
    return src_mask, trg_mask

You can try this code.

@ozturkosu
Copy link

I have tried the code above but i also does not work !!!

@anasAloklah
Copy link
Author

anasAloklah commented Mar 11, 2021

I fixed this the problem
I move the src, trg to cuda before call the create_masks function
in traning file line 30 I change:
src = batch.src.transpose(0,1).cuda()
trg = batch.trg.transpose(0,1).cuda()
trg_input = trg[:, :-1]
src_mask, trg_mask = create_masks(src, trg_input, opt)

@A-Kerim
Copy link

A-Kerim commented Apr 7, 2021

Thank you @anasAloklah. Your solution worked for me!

@MohammdReza2020
Copy link

Your solution worked for me! too
thanks a lot

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants