I have a model net() with attribute ‘self.x’.
I can access this attribute using net.x.
When using DataParallel as
net = nn.DataParallel(net, device_ids=[0])
I can access the attribute as net.module.x.
However, when I use
net = nn.DataParallel(net, device_ids=[0,1])
I get NoneType object in return using net.module.x
Following is a code to reproduce it.
import torch
import numpy as np
import torch.nn as nn
import torch.nn.functional as torchF
from torch.autograd import Variable

class Net(nn.Module):
      def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 3, kernel_size=3, padding=1, stride=1)	#
    	self.conv2 = nn.Conv2d(3, 3, kernel_size=3, padding=1, stride=1)	#
    	self.x = None
      def forward(self,x):
        x = self.conv1(x)
        self.x = x 
        x = self.conv2(x)
        return x


net = Net()
#net = nn.DataParallel(net, device_ids=[0]) #this line does not produce the error
net = nn.DataParallel(net, device_ids=[0,1]) #this line produces the error
net.cuda()
x = Variable(torch.cuda.FloatTensor(np.random.rand(3,3,7,7)))
out_x=net(x)
out = torchF.l1_loss(out_x,Variable(torch.cuda.FloatTensor(-np.random.rand(3,3,7,7))))
net.zero_grad()
out.backward()
print(type(net.module.x))   
print(net.module.x.size())