RuntimeError: [enforce fail at ..\c10\core\CPUAllocator.cpp:72] data. DefaultCPUAllocator: not enough memory: you tried to allocate 274877906944 bytes. Buy new RAM!

I’m trying to replicate a NumPy function from here: https://github.com/google/deepdream/blob/master/dream.ipynb

From the link, the code is: “trying to maximize the dot-products between activations of current image, and their best matching correspondences from the guide image.”

When I run the following code, or any variation that has torch.mm(x.T, y), I get a out of memory error:

def guide_output(input, guide):
    x = input.squeeze(0)
    y = guide.squeeze(0)
    ch = x.shape[0]
    x = x.reshape(ch,-1)
    y = y.reshape(ch,-1)
    A = torch.mm(x.T, y) # compute the matrix of dot-products with guide features
    x.reshape(ch,-1)[:] = y[:,A.argmax(1)] # select ones that match best

input = torch.randn(3,512,512).unsqueeze(0)
guide = torch.randn(3,512,512).unsqueeze(0)
output = guide_output(input, guide)

The error message:

Traceback (most recent call last):
  File "test.py", line 76, in <module>
    output = guide_output(input, guide)
  File "test.py", line 63, in guide_output
    A = torch.mm(x.T, y) # compute the matrix of dot-products with guide features
RuntimeError: [enforce fail at ..\c10\core\CPUAllocator.cpp:72] data. DefaultCPUAllocator: not enough memory: you tried to allocate 274877906944 bytes. Buy new RAM!
import torch

def guide_output(input, guide):
    x = input.squeeze(0)
    y = guide.squeeze(0)
    ch = x.shape[0]
    x = x.reshape(ch,-1)
    y = y.reshape(ch,-1)
    print(x.T.shape, y.shape)
    A = torch.mm(x.T, y) # compute the matrix of dot-products with guide features
    x.reshape(ch,-1)[:] = y[:,A.argmax(1)] # select ones that match best

input = torch.randn(3,512,512).unsqueeze(0)
guide = torch.randn(3,512,512).unsqueeze(0)
output = guide_output(input, guide)

The printed shape is torch.Size([262144, 3]) torch.Size([3, 262144]). Thus the size of the output of your torch.mm is a tensor of shape (262144,262144) which requires
274877906944 bytes i.e. 274.877906944 Gigabytes. And since the system can’t provide it, we see the error.