cDCGANs - RuntimeError: Input type (torch.FloatTensor) and weight type (MPSFloatType) should be the same or input should be a MKLDNN tensor and weight is a dense tensor

Hey!

I’m trying to create a conditional DCGAN, and implementing two separate sequential for z and y for my generator and concatenating the outputs to go into the main sequential.

I get the error in the title: “RuntimeError: Input type (torch.FloatTensor) and weight type (MPSFloatType) should be the same, or input should be a MKLDNN tensor and weight is a dense tensor”. The error occurs at:

z = self.z_reshape(z)

DCGANs work with no problem and don’t throw the same error, which is strange.

I have an M2 and use mps. I’ve noticed other Mac OS users get a similar error but with other implementations. Here’s my code, which includes weight initialisation, generator architecture and initialising the generator.

def weights_init(m):
    classname = m.__class__.__name__
    if classname.find('Conv') != -1:
        nn.init.normal_(m.weight.data, 0.0, 0.02)
    elif classname.find('BatchNorm') != -1:
        nn.init.normal_(m.weight.data, 1.0, 0.02)
        nn.init.constant_(m.bias.data, 0)
    class Generator(nn.Module):
        def __init__(self):
            super().__init__()
            self.z_reshape = nn.Sequential(
                nn.ConvTranspose1d(nz, ngf * 64, kernel_size=4, stride=1, padding=0, bias=False),
                print("hello"),
                nn.BatchNorm1d(ngf * 64),
                nn.LeakyReLU(True)
            )
        
            self.y_reshape = nn.Sequential(
                # presuming that 10 is the number of class labels?
                nn.ConvTranspose1d(10, ngf * 64, kernel_size=4, stride=1, padding=0, bias=False),
                nn.BatchNorm1d(ngf * 64),
                nn.LeakyReLU(True)
            )

            self.main = nn.Sequential(
                nn.ConvTranspose1d(ngf * 126, ngf * 64, kernel_size=4, stride=1, padding=0, bias=False),
                nn.BatchNorm1d(ngf * 64),
                nn.LeakyReLU(True),

                nn.ConvTranspose1d(ngf * 64, ngf * 32, kernel_size=4, stride=2, padding=1, bias=False),
                nn.BatchNorm1d(ngf * 32),
                nn.LeakyReLU(True),

                nn.ConvTranspose1d(ngf * 32, ngf * 16, kernel_size=4, stride=2, padding=1, bias=False),
                nn.BatchNorm1d(ngf * 16),
                nn.LeakyReLU(True),

                nn.ConvTranspose1d(ngf * 16, ngf * 8, kernel_size=4, stride=2, padding=1, bias=False),
                nn.BatchNorm1d(ngf * 8),
                nn.LeakyReLU(True),

                nn.ConvTranspose1d(ngf * 8, ngf * 4, kernel_size=4, stride=2, padding=1, bias=False),
                nn.BatchNorm1d(ngf * 4),
                nn.LeakyReLU(True),

                nn.ConvTranspose1d(ngf * 4, ngf * 2, kernel_size=4, stride=2, padding=1, bias=False),
                nn.BatchNorm1d(ngf * 2),
                nn.LeakyReLU(True),

                nn.ConvTranspose1d(ngf * 2, ngf, kernel_size=4, stride=2, padding=1, bias=False),
                nn.BatchNorm1d(ngf),
                nn.LeakyReLU(True),

                nn.ConvTranspose1d(ngf, nc, kernel_size=4, stride=2, padding=1, bias=False),
            )

        def forward(self, z, y):
            print(z.shape)
            print(y.shape)
            z = self.z_reshape(z)
            y = self.y_reshape(y)
            z = torch.cat([z, y], 1)
            z = self.main(z)
            return z
netG = Generator().to(device)
netG.apply(weights_init)

print(netG)

model = Generator()
summary(netG, input_size=[(nz, 1), (1, 1)])

Could anyone lend me a hand?