GET was unable to find an engine to execute this computation

Vikas_Amaraneni: <p>RuntimeError                              Traceback (most recent call last)<br>
Cell In[16], line 47<br>
45 num_epochs = 10<br>
46 for epoch in range(num_epochs):<br>
—&gt; 47     train_loss, train_time = train(model, train_loader, criterion, optimizer)<br>
48     val_loss, val_accuracy, val_time = validate(model, val_loader, criterion)<br>
49     print(f’Epoch {epoch+1}/{num_epochs}, Train Loss: {train_loss:.4f}, Train Time: {train_time:.2f}s, ’<br>
50       f’Val Loss: {val_loss:.4f}, Val Accuracy: {val_accuracy:.4f}, Val Time: {val_time:.2f}s’)</p>
<p>Cell In[16], line 13, in train(model, train_loader, criterion, optimizer)<br>
11 outputs = model(inputs)<br>
12 loss = criterion(outputs, labels)  # Calculate loss between model outputs and ground truth<br>
—&gt; 13 loss.backward()<br>
14 optimizer.step()<br>
15 running_loss += loss.item() * inputs.size(0)  # Update running loss</p>
<p>File ~/.conda/envs/torchTest1/lib/python3.12/site-packages/torch/_tensor.py:522, in Tensor.backward(self, gradient, retain_graph, create_graph, inputs)<br>
512 if has_torch_function_unary(self):<br>
513     return handle_torch_function(<br>
514         Tensor.backward,<br>
515         (self,),<br>
(…)<br>
520         inputs=inputs,<br>
521     )<br>
 → 522 torch.autograd.backward(<br>
523     self, gradient, retain_graph, create_graph, inputs=inputs<br>
524 )</p>
<p>File ~/.conda/envs/torchTest1/lib/python3.12/site-packages/torch/autograd/<strong>init</strong>.py:266, in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables, inputs)<br>
261     retain_graph = create_graph<br>
263 # The reason we repeat the same comment below is that<br>
264 # some Python versions print out the first line of a multi-line function<br>
265 # calls in the traceback and some print out the last line<br>
 → 266 Variable.<em>execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass<br>
267     tensors,<br>
268     grad_tensors</em>,<br>
269     retain_graph,<br>
270     create_graph,<br>
271     inputs,<br>
272     allow_unreachable=True,<br>
273     accumulate_grad=True,<br>
274 )</p>
<p>RuntimeError: GET was unable to find an engine to execute this computation.</p>
<p>Hi, Can anyone help me with this error.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/c/ecac3196d664cb93df6ec34385036fc05db119cd.png" data-download-href="https://discuss.pytorch.org/uploads/default/ecac3196d664cb93df6ec34385036fc05db119cd" title="Screenshot 2024-04-12 at 11.13.53 AM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/c/ecac3196d664cb93df6ec34385036fc05db119cd_2_690x169.png" alt="Screenshot 2024-04-12 at 11.13.53 AM" data-base62-sha1="xLHBAMEvUZRzRBTIphso4ULm3iB" width="690" height="169" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/c/ecac3196d664cb93df6ec34385036fc05db119cd_2_690x169.png, https://discuss.pytorch.org/uploads/default/optimized/3X/e/c/ecac3196d664cb93df6ec34385036fc05db119cd_2_1035x253.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/e/c/ecac3196d664cb93df6ec34385036fc05db119cd_2_1380x338.png 2x" data-dominant-color="2C2A2A"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot 2024-04-12 at 11.13.53 AM</span><span class="informations">1852×454 127 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>Could you post a minimal and executable code snippet reproducing the issue, please?</p>

Vikas_Amaraneni: <h1><a name="split-the-image-paths-into-training-and-validation-sets-1" class="anchor" href="#split-the-image-paths-into-training-and-validation-sets-1"></a>Split the image paths into training and validation sets</h1>
<p>train_images = final_gray_images[:92677]<br>
train_labels = labels[1:92678]<br>
val_images = final_gray_images[92678:115845]<br>
val_labels = labels[92679:115846]</p>
<h1><a name="print-the-lengths-of-training-and-validation-sets-2" class="anchor" href="#print-the-lengths-of-training-and-validation-sets-2"></a>Print the lengths of training and validation sets</h1>
<p>print(“shape of training images:”, train_images.shape)<br>
print(“shape of training labels:”, train_labels.shape)<br>
print(“shape of validation images:”, val_images.shape)<br>
print(“shape of validation labels:”, val_labels.shape)</p>
<p>import torch<br>
import torch.nn as nn<br>
import torch.optim as optim<br>
from torch.utils.data import DataLoader, TensorDataset<br>
from sklearn.preprocessing import LabelEncoder</p>
<h1><a name="checking-if-cuda-gpu-is-available-3" class="anchor" href="#checking-if-cuda-gpu-is-available-3"></a>Checking if CUDA (GPU) is available</h1>
<p>device = “cuda” if torch.cuda.is_available() else “cpu”<br>
print(f"Using device: {device}")</p>
<h1><a name="create-a-label-encoder-4" class="anchor" href="#create-a-label-encoder-4"></a>Create a label encoder</h1>
<p>label_encoder = LabelEncoder()</p>
<h1><a name="fit-the-label-encoder-on-the-entire-labels-array-5" class="anchor" href="#fit-the-label-encoder-on-the-entire-labels-array-5"></a>Fit the label encoder on the entire labels array</h1>
<p>label_encoder.fit(labels)</p>
<h1><a name="transform-the-labels-to-numerical-labels-6" class="anchor" href="#transform-the-labels-to-numerical-labels-6"></a>Transform the labels to numerical labels</h1>
<p>train_labels_encoded = label_encoder.transform(train_labels)<br>
val_labels_encoded = label_encoder.transform(val_labels)</p>
<h1><a name="convert-numpy-arrays-to-pytorch-tensors-and-move-to-the-gpu-7" class="anchor" href="#convert-numpy-arrays-to-pytorch-tensors-and-move-to-the-gpu-7"></a>Convert NumPy arrays to PyTorch tensors and move to the GPU</h1>
<p>train_tensor = torch.tensor(train_images).float().to(device)<br>
train_labels_tensor = torch.from_numpy(train_labels_encoded).long().to(device)<br>
val_tensor = torch.tensor(val_images).float().to(device)<br>
val_labels_tensor = torch.from_numpy(val_labels_encoded).long().to(device)<br>
torch.cuda.empty_cache()</p>
<h1><a name="create-tensordatasets-8" class="anchor" href="#create-tensordatasets-8"></a>Create TensorDatasets</h1>
<p>train_dataset = TensorDataset(train_tensor, train_labels_tensor)<br>
val_dataset = TensorDataset(val_tensor, val_labels_tensor)</p>
<h1><a name="create-dataloaders-9" class="anchor" href="#create-dataloaders-9"></a>Create DataLoaders</h1>
<p>train_loader = DataLoader(train_dataset, batch_size=1000, shuffle=False)<br>
val_loader = DataLoader(val_dataset, batch_size=1000, shuffle=False)</p>
<p>class CNNModel(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(CNNModel, self).<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(1, 64, kernel_size=3, padding=1)<br>
self.relu1 = nn.ReLU(inplace=True)<br>
self.maxpool1 = nn.MaxPool2d(kernel_size=2, stride=2)<br>
self.conv2 = nn.Conv2d(64, 128, kernel_size=3, padding=1)<br>
self.relu2 = nn.ReLU(inplace=True)<br>
self.maxpool2 = nn.MaxPool2d(kernel_size=2, stride=2)<br>
self.conv3 = nn.Conv2d(128, 256, kernel_size=3, padding=1)<br>
self.relu3 = nn.ReLU(inplace=True)<br>
self.conv4 = nn.Conv2d(256, 256, kernel_size=3, padding=1)<br>
self.relu4 = nn.ReLU(inplace=True)<br>
self.maxpool3 = nn.MaxPool2d(kernel_size=2, stride=2)<br>
self.conv5 = nn.Conv2d(256, 512, kernel_size=3, padding=1)<br>
self.relu5 = nn.ReLU(inplace=True)<br>
self.conv6 = nn.Conv2d(512, 512, kernel_size=3, padding=1)<br>
self.relu6 = nn.ReLU(inplace=True)<br>
self.maxpool4 = nn.MaxPool2d(kernel_size=2, stride=2)<br>
self.avgpool = nn.AdaptiveAvgPool2d((7, 7))<br>
self.fc = nn.Linear(512 * 7 * 7, 1000)</p>
<pre><code>def forward(self, x):
    x = self.conv1(x)
    x = self.relu1(x)
    x = self.maxpool1(x)
    x = self.conv2(x)
    x = self.relu2(x)
    x = self.maxpool2(x)
    x = self.conv3(x)
    x = self.relu3(x)
    x = self.conv4(x)
    x = self.relu4(x)
    x = self.maxpool3(x)
    x = self.conv5(x)
    x = self.relu5(x)
    x = self.conv6(x)
    x = self.relu6(x)
    x = self.maxpool4(x)
    x = self.avgpool(x)
    x = torch.flatten(x, 1)
    x = self.fc(x)
    
    return x
</code></pre>
<h1><a name="move-the-model-to-the-gpu-if-available-10" class="anchor" href="#move-the-model-to-the-gpu-if-available-10"></a>Move the model to the GPU if available</h1>
<p>model = CNNModel().to(device)<br>
criterion = nn.CrossEntropyLoss()<br>
optimizer = optim.Adam(model.parameters(), lr=0.001)</p>
<h1><a name="train-the-model-11" class="anchor" href="#train-the-model-11"></a>Train the model</h1>
<p>def train(model, train_loader, criterion, optimizer):<br>
model.train()<br>
running_loss = 0.0<br>
start_time = time.time()<br>
for inputs, labels in train_loader:<br>
inputs = inputs.unsqueeze(1)  # Convert input to Float<br>
labels = labels.view(-1)<br>
inputs, labels = inputs.to(device), labels.to(device)<br>
optimizer.zero_grad()<br>
outputs = model(inputs)<br>
loss = criterion(outputs, labels)  # Calculate loss between model outputs and ground truth<br>
loss.backward()<br>
optimizer.step()<br>
running_loss += loss.item() * inputs.size(0)  # Update running loss<br>
end_time = time.time()<br>
return running_loss / len(train_loader.dataset), end_time - start_time</p>
<h1><a name="validation-function-12" class="anchor" href="#validation-function-12"></a>Validation function</h1>
<p>def validate(model, val_loader, criterion):<br>
model.eval()<br>
start_time = time.time()<br>
with torch.no_grad():<br>
running_loss = 0.0<br>
correct_preds = 0<br>
total_preds = 0<br>
for inputs, labels in tqdm(val_loader):<br>
inputs = inputs.to(device)<br>
labels = labels.to(device)</p>
<pre><code>        outputs = model(inputs)
        loss = criterion(outputs, labels)
        running_loss += loss.item() * inputs.size(0)
        
        # Calculate accuracy
        _, preds = torch.max(outputs, 1)
        correct_preds += torch.sum(preds == labels).item()
        total_preds += inputs.size(0)
end_time = time.time()
val_loss = running_loss / len(val_loader.dataset)
val_accuracy = correct_preds / total_preds
return val_loss, val_accuracy, end_time - start_time
</code></pre>
<h1><a name="training-loop-13" class="anchor" href="#training-loop-13"></a>Training loop</h1>
<p>num_epochs = 10<br>
for epoch in range(num_epochs):<br>
train_loss, train_time = train(model, train_loader, criterion, optimizer)<br>
val_loss, val_accuracy, val_time = validate(model, val_loader, criterion)<br>
print(f’Epoch {epoch+1}/{num_epochs}, Train Loss: {train_loss:.4f}, Train Time: {train_time:.2f}s, ’<br>
f’Val Loss: {val_loss:.4f}, Val Accuracy: {val_accuracy:.4f}, Val Time: {val_time:.2f}s’)</p>
<p>This is the code I’m running. I’m using ImageNet-LT dataset which has 1000 classes. While training the model I’m facing the issue. Can you please help me with this.</p>

ptrblck: <p>Your code is neither properly formatted nor is it executable. Remove the data dependency, check if random inputs would reproduce the issue (they should) and use these instead, and format the code by wrapping it into three backticks ```.</p>

Vikas_Amaraneni: <p>I’m sorry. It went wrong while copy and pasting the code</p>

Vikas_Amaraneni: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/b/9b601f29dd3a72d0f3b2093b212ad2ac7d8060d1.png" data-download-href="https://discuss.pytorch.org/uploads/default/9b601f29dd3a72d0f3b2093b212ad2ac7d8060d1" title="Screenshot 2024-04-14 at 1.34.03 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/b/9b601f29dd3a72d0f3b2093b212ad2ac7d8060d1_2_690x234.png" alt="Screenshot 2024-04-14 at 1.34.03 PM" data-base62-sha1="mavWLgWkn4E8QMpyeIbAWyG28k9" width="690" height="234" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/b/9b601f29dd3a72d0f3b2093b212ad2ac7d8060d1_2_690x234.png, https://discuss.pytorch.org/uploads/default/optimized/3X/9/b/9b601f29dd3a72d0f3b2093b212ad2ac7d8060d1_2_1035x351.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/9/b/9b601f29dd3a72d0f3b2093b212ad2ac7d8060d1_2_1380x468.png 2x" data-dominant-color="EDEEEE"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot 2024-04-14 at 1.34.03 PM</span><span class="informations">1914×650 93.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
I’m getting error while executing the code and the error is highlighting at ‘loss = criterion(outputs, labels)’. I’m using ImageNet-LT dataset and it has 1000 classes. torch version is 2.2.1, python version is 3.12.2, and CUDA version is 12.1.</p>