I can not reproduce the error using your code and some toy data and model.
def train_model(model, data, actual_labels):
    model.train()
    
    optimizer.zero_grad()
    
    predicted_training_labels = model(data)

    loss = loss_function(predicted_training_labels, actual_labels)
    
    # accuracy = compute_accuracy(predicted_training_labels, actual_labels)

    loss.backward()

    optimizer.step()
    
    return loss.item()

data = torch.randn(1000, 2048)
labels = torch.empty(1000, dtype=torch.long).random_(3)
loss_function = nn.CrossEntropyLoss()
model = nn.Linear(2048, 3)
optimizer = torch.optim.Adam(model.parameters())

for i in range(10):
    l = train_model(model, data, labels)
    print(l)

This code is working fine and without any error.