0

I'm trying to train a simple linear regression model with Pytorch and CUDA. PyTorch can detect CUDA, and there is GPU memory consumption. However, GPU is never used in my code. This is my code

class linearRegression(torch.nn.Module):
    def __init__(self, inputSize, outputSize):
        super(linearRegression, self).__init__()
        self.linear = torch.nn.Linear(inputSize, outputSize)

    def forward(self, x):
        out = self.linear(x)
        return out

def linear_demo():
    # create dummy data for training
    n_data = 1000000
    x_values = n_data*np.random.normal(size=n_data)
    x_train = np.array(x_values, dtype=np.float64)
    x_train = x_train.reshape(-1, 1)

    y_values = [2*i + 1 + (np.random.rand(1)-0.5)*5 for i in x_values]
    y_train = np.array(y_values, dtype=np.float64)
    y_train = y_train.reshape(-1, 1)
    x_train /= np.abs(x_train).max()
    y_train /= np.abs(y_train).max()
    print(x_train.shape)
    print(y_train.shape)

    inputDim = 1        # takes variable 'x'
    outputDim = 1       # takes variable 'y'
    learningRate = 1e-2
    epochs = 80
    batch_size = 128*8
    batch_size = n_data

    model = linearRegression(inputDim, outputDim).to(torch.float64)
    ##### For GPU #######
    if torch.cuda.is_available():
        model.cuda()
    criterion = torch.nn.MSELoss(reduction='mean').cuda()
    optimizer = torch.optim.SGD(model.parameters(), lr=learningRate)
    for epoch in range(epochs):
        permutation = torch.randperm(n_data)
        idx = permutation[:batch_size]
        # Converting inputs and labels to Variable
        if torch.cuda.is_available():
            inputs = Variable(torch.from_numpy(x_train[idx]).cuda())
            labels = Variable(torch.from_numpy(y_train[idx]).cuda())
        else:
            inputs = Variable(torch.from_numpy(x_train[idx]))
            labels = Variable(torch.from_numpy(y_train[idx]))
        # Clear gradient buffers because we don't want any gradient from the previous epoch to carry forward, don't want to accumulate gradients
        optimizer.zero_grad()

        # get output from the model, given the inputs
        outputs = model(inputs)

        # get loss for the predicted output
        loss = criterion(outputs, labels)
        if loss.item() > 1:
            print(loss)
        # get gradients w.r.t to parameters
        loss.backward()

        # update parameters
        optimizer.step()

        if (epoch % 10 == 0):
            print('epoch {}, loss {}'.format(epoch, loss.item()))
    return

This is a screenshot of GPU usage. Apparently, GPU is not used in computing. GPU can be detected.

If the computational burden is too low for GPU to display usage, I tried increasing n_data to 1e8, but the GPU usage is still 0%. How can I use GPU computing in this situation? If it's really due to low computational burden, is there any simple way to clearly show that GPU is working?

------update------

  1. I tried top answer here, and the terminal returns the expected info. So I believe the installation is correct. screenshot for GPU info

  2. I also tried device declaration like inputs.to('cuda'), inputs.to(torch.device('cuda')), inputs.cuda(). They turn out to be equivalent.

  • You should be moving the model and the inputs to gpu by using `.to(torch.device('cuda'))` – null Jun 02 '23 at 11:34
  • @null I tried it w/ `model.to(torch.device('cuda'))`, `torch.nn.MSELoss(reduction='mean').to(torch.device('cuda'))`, `inputs = Variable(torch.from_numpy(x_train[idx]).to(torch.device('cuda')))`, and set `n_data=1e7` running in 1 min, but it still has 0 GPU usage. – user1521062 Jun 02 '23 at 12:17
  • Welcome to SO. Are there any cases where it does show GPU usage in `pytorch` for you on that machine? Or is this problem exclusive to when you try to use your custom class? – Brock Brown Jun 02 '23 at 12:38
  • @BrockBrown I'm new to PyTorch and CUDA. I don't have a case where it shows GPU usage... I actually got the code from [this website](https://towardsdatascience.com/linear-regression-with-pytorch-eb6dedead817). The original code shows no GPU usage on my machine either. – user1521062 Jun 02 '23 at 14:01
  • First you need to declare a device: ```device=torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")``` For any model and inputs, you then need to transfer them to that target device ``` device = torch.device("cuda") x = x.to(device) model = model.to(device) ``` – Mahdi Amrollahi Jun 02 '23 at 15:24

0 Answers0