Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

A problem about using cuda() #1159

Closed
qzhiyue opened this issue Aug 18, 2022 · 2 comments
Closed

A problem about using cuda() #1159

qzhiyue opened this issue Aug 18, 2022 · 2 comments

Comments

@qzhiyue
Copy link

qzhiyue commented Aug 18, 2022

Issue Description

os.environ['TL_BACKEND'] = 'torch'
and using gpu0
When I try to put my model and variable into cuda:

net.cuda()
x, y, y_cls = Variable(x).cuda(), Variable(y).cuda(), Variable(y_cls).cuda()

an error occurred here:

class PSPUpsample(tlx.nn.Module):
    def __init__(self, in_channels, out_channels):
        super().__init__()
        self.conv = tlx.nn.Sequential(
            tlx.nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, padding=1, data_format='channels_first'),
            tlx.nn.BatchNorm2d(num_features=out_channels, data_format='channels_first'),
            tlx.layers.PRelu()
        )
    def forward(self, x):
        h, w = 2 * x.size(2), 2 * x.size(3)
        p = tlx.layers.UpSampling2d(scale=(h / x.size(2), w / x.size(3)), method='bilinear',
                                    data_format='channels_first')(x)
        return self.conv(p) #here

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument weight in method wrapper__prelu)

When I put the 'p' into 'cuda:0' before 'return', like this:

p = p.to_device('cuda:0')

the error also occurred.
When I put the 'p' into cpu, it shows that the model in gpu and it doesn't work.

@hanjr92
Copy link
Member

hanjr92 commented Aug 18, 2022

217904c1ec9352ef87544f232cbd620

@qzhiyue
Copy link
Author

qzhiyue commented Aug 18, 2022

This solution is great, thank you!

@qzhiyue qzhiyue closed this as completed Aug 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants