Expected cuda got cpu

TypeError: expected TensorOptions(dtype=long int, device=cpu, layout=Strided, requires_grad=false (default), pinned_memory=false (default), memory_format=(nullopt)) (got TensorOptions(dtype=long int, device=cuda:0, layout=Strided, requires_grad=false (default), pinned_memory=false (default), memory_format=(nullopt))) site:stackoverflow.com

It doesn’t seems to a general tensor different error. I think pytorch is converting internally my model to cpu. Please explain.
When i convert my tensor input tensor to cpu. I got reverse error that my model is on gpu. Please let me know if model code required. I am troubling with theory.
Its giving error here: torch.LongTensor(nodes)

PyTorch should not transform any data behind your back. Could you post a minimal, executable code snippet reproducing this issue, so that we could debug it, please?

@ptrblck Thanks for such faster help. Here torch.LongTensor(nodes) is giving error where nodes are already simple tensors.

class Encoder(nn.Module):
  def __init__(self,features,adj_matrix,embed_dim,feature_dim,agg,cuda):#in our case its ppi feat dim
    super(Encoder,self).__init__()
    self.features = features
    self.adj_matrix = adj_matrix
    self.embed_dim = embed_dim
    self.weight = nn.Parameter(torch.torch.FloatTensor(embed_dim,2*feature_dim))
    init.xavier_uniform(self.weight)
    self.aggregation = agg
    self.cuda = cuda
    self.aggregation.cuda = cuda
  def forward(self,nodes):
    #print("checking one neigh ",[self.adj_matrix[int(nodes[0])],self.adj_matrix[int(nodes[2])]])
    agg = self.aggregation(nodes,[self.adj_matrix[int(node)] for node in nodes],num_sample=40)
    #print("checks ",agg.is_cuda,"nodes ",torch.LongTensor(nodes).is_cuda)
    if self.cuda:
      print(type(nodes))
      print(type(self.features),torch.LongTensor(nodes).is_cuda)
      node_embed = self.features(torch.LongTensor(nodes))
    else:
        node_embed = self.features(torch.LongTensor(nodes))#.cuda()
    print("working one agg of nodes too")#,node_embed.is_cuda,agg.is_cuda)
    fe1 = torch.cat([node_embed,agg],dim=1)
    #print("checking fe1 ",fe1.is_cuda)
    if self.weight.is_cuda:
      fe1 = fe1.cuda()
    fe2 = F.relu(self.weight.mm(fe1.t()))
    return fe2
1 Like

Thanks for the quick reply. The code is unfortunately not executable, so could you please add the missing pieces so that we could run it?

Here is the whole model architecture

class Encoder(nn.Module):
  def __init__(self,features,adj_matrix,embed_dim,feature_dim,agg,cuda):#in our case its ppi feat dim
    super(Encoder,self).__init__()
    self.features = features
    self.adj_matrix = adj_matrix
    self.embed_dim = embed_dim
    self.weight = nn.Parameter(torch.torch.FloatTensor(embed_dim,2*feature_dim))
    init.xavier_uniform(self.weight)
    self.aggregation = agg
    self.cuda = cuda
    self.aggregation.cuda = cuda
  def forward(self,nodes):
    #print("checking one neigh ",[self.adj_matrix[int(nodes[0])],self.adj_matrix[int(nodes[2])]])
    agg = self.aggregation(nodes,[self.adj_matrix[int(node)] for node in nodes],num_sample=40)
    #print("checks ",agg.is_cuda,"nodes ",torch.LongTensor(nodes).is_cuda)
    if self.cuda:
      print(type(nodes))
      print(type(self.features),torch.LongTensor(nodes.cpu()).is_cuda)
      node_embed = self.features(torch.LongTensor(nodes))
    else:
        node_embed = self.features(torch.LongTensor(nodes))#.cuda()
    print("working one agg of nodes too")#,node_embed.is_cuda,agg.is_cuda)
    fe1 = torch.cat([node_embed,agg],dim=1)
    #print("checking fe1 ",fe1.is_cuda)
    if self.weight.is_cuda:
      fe1 = fe1.cuda()
    fe2 = F.relu(self.weight.mm(fe1.t()))
    return fe2
class MeanAggregation(nn.Module):
  def __init__(self,features,num,cuda):
    super(MeanAggregation,self).__init__()
    self.features = features#.cpu()
    self.cuda = cuda
    self.num = num
  def forward(self,nodes,neigh1,num_sample=4):
    print("mean agg and cuda ",self.num, self.cuda)
    num_sample=40
    _sample = random.sample
    _set = set
    neigh = [_set(_sample(to_neigh, num_sample,)) if len(to_neigh) >= num_sample else to_neigh for to_neigh in neigh1]
    unique_node_list = list(set.union(*neigh))
    unique_node_map = {n:i for i,n in enumerate(unique_node_list)}
    if self.cuda:
        embed_matrix = self.features(torch.LongTensor(unique_node_list).cuda())
    else:
        embed_matrix = self.features(torch.LongTensor(unique_node_list))


    mask = Variable(torch.zeros(len(nodes),len(unique_node_list)))
    # print("mask variable deficning taking  ",unique_node_map[116])
    column_indices = []
    for samp_neigh in neigh:
      for n in samp_neigh:
          # print(n)
          column_indices.append(unique_node_map[n])   
          
    row_indices = [i for i in range(len(neigh)) for j in range(len(neigh[i]))]
    mask[row_indices, column_indices] = 1
    # print("before mask sum",mask.shape)
    #self.cuda=True
    if self.cuda:
            mask = mask.cuda()
    mask_cp =mask.sum(1, keepdim=True)

    mask = mask/mask_cp
    print("before mask mm with embed_matrix")

    mkp = mask.mm(embed_matrix)
    return mkp
class GraphSage(nn.Module):
  def __init__(self,num_classes, enc):
    super(GraphSage,self).__init__()
    self.fc1 = nn.Parameter(torch.torch.FloatTensor(num_classes,enc.embed_dim))#.cuda()
    self.enc = enc
    init.xavier_uniform(self.fc1)

    self.xent = nn.L1Loss()#check from paper once #nn.CrossEntropyLoss() 
    #paper uses sigmid with logistics as loss........implement
    self.softmax = torch.nn.Sigmoid()#use signmoid as we gave multiclass labels

  def forward(self, nodes):
    enc_op = self.enc(nodes)
    fe1 = self.fc1.mm(enc_op)
    return self.softmax(fe1)
  def loss(self,nodes,labels):
    op = self.forward(nodes)
    #print(op.shape,labels.shape)
    loss = self.xent(op.t(),labels.cuda())
    return loss

And classes are declared as:

features.weight = nn.Parameter(torch.FloatTensor(feat).cuda(),
                              requires_grad=False)
agg1 = MeanAggregation(features,num=1,cuda=True)
enc1 = Encoder(features,b0_adj_matrix,embed_dim=1096,feature_dim=50,agg=agg1,cuda=True)#check feature_dim these things
agg2 = MeanAggregation(lambda nodes :enc1(nodes).t(),num=2,cuda=True)
enc2 = Encoder(lambda nodes:enc1(nodes).t(),b1_adj_matrix,embed_dim=1096,feature_dim=enc1.embed_dim,agg=agg2,cuda=True)#.cuda()
num_cls=121
graphsage = GraphSage(num_cls,enc2).cuda()
loss = graphsage.loss(train_nodes,train_label)
print(loss)

@ptrblck Please let me know with some kind of data I can send, as an examples. A small graph with 4 nodes can help.

Please create the input data using random shapes.

graph = {

1:[5],

2:[3],

4:[1],`

5:[2, 4],

6 :[4,1 ]
}
nodes=[1,2,3,4,5,6]
For features
import numpy as geek
geek.identity(6, dtype = float)

@ptrblck I found a trespass to this ```

if torch.is_tensor(nodes):
        n1 = nodes
else:
        n1 = torch.LongTensor(nodes)
      node_embed = self.features(n1.cuda())

But still not sure why i am not able to convert to floatTensor. And how its related to cuda error.

I don’t know where the code is coming from and if

But still not sure why i am not able to convert to floatTensor.

is a new issue or related to the device mismatch.
Your current code is unfortunately still not executable.

To debug further, check the stacktrace of the error message and narrow down the line of code which is failing. Once you have it, check the device attribute of all used tensors in this operation and see which one creates the device mismatch. Afterwards track down where this tensor is coming from and how it was created. In case it’s hard-coded as a CPUTensor, use the to(device) method to move it to the device or register it properly as a parameter or buffer, if applicable.

1 Like